memsafer-generic.cpp revision b013dbfb0905a5fc4c7218cc08f63b156d0126a0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/* $Id$ */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/** @file
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * IPRT - Memory Allocate for Sensitive Data, generic heap-based implementation.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/*
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * Copyright (C) 2006-2014 Oracle Corporation
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * This file is part of VirtualBox Open Source Edition (OSE), as
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * available from http://www.virtualbox.org. This file is free software;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * you can redistribute it and/or modify it under the terms of the GNU
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * General Public License (GPL) as published by the Free Software
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * Foundation, in version 2 as it comes in the "COPYING" file of the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * The contents of this file may alternatively be used under the terms
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * of the Common Development and Distribution License Version 1.0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * VirtualBox OSE distribution, in which case the provisions of the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * CDDL are applicable instead of those of the GPL.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * You may elect to license modified versions of this file under the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * terms and conditions of either the GPL or the CDDL or both.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy/*******************************************************************************
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy* Header Files *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy*******************************************************************************/
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#include "internal/iprt.h"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#include <iprt/memsafer.h>
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#include <iprt/assert.h>
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#include <iprt/string.h>
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#if defined(IN_SUP_R3) && defined(VBOX) && !defined(RT_NO_GIP)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# include <iprt/param.h>
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# include <VBox/sup.h>
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#endif /* IN_SUP_R3 */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/*******************************************************************************
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy* Defined Constants And Macros *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy*******************************************************************************/
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/** Allocation size alignment. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#define RTMEMSAFER_ALIGN 16
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/** Padding after the block to avoid small overruns. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#define RTMEMSAFER_PAD_BEFORE 96
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy/** Padding after the block to avoid small underruns. */
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy#define RTMEMSAFER_PAD_AFTER 32
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy/*******************************************************************************
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy* Structures and Typedefs *
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy*******************************************************************************/
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/**
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * Supported allocation methods.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedytypedef enum RTMEMSAFERALLOCMETHOD
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** Invalid method. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy RTMEMSAFERALLOCMETHOD_INVALID = 0,
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** RTMem{Alloc|Free} methods, least secure!. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy RTMEMSAFERALLOCMETHOD_RTMEM,
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** Support library. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy RTMEMSAFERALLOCMETHOD_SUPR3,
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** 32bit hack. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy RTMEMSAFERALLOCMETHOD_32BIT_HACK = 0x7fffffff
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy} RTMEMSAFERALLOCMETHOD;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/** Pointer to a allocation method enum. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedytypedef RTMEMSAFERALLOCMETHOD *PRTMEMSAFERALLOCMETHOD;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/**
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * Memory header for safer memory allocations.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * @note: There is no magic value used deliberately to make identifying this structure
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * as hard as possible.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedytypedef struct RTMEMSAFERHDR
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** Flags passed to this allocation - used for freeing and reallocation. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy uint32_t fFlags;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** Allocation method used. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy RTMEMSAFERALLOCMETHOD enmAllocMethod;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /** Amount of bytes allocated. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy size_t cb;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy} RTMEMSAFERHDR;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/** Pointer to a safer memory header. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedytypedef RTMEMSAFERHDR *PRTMEMSAFERHDR;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/** Make sure we are staying in the padding area. */
f38cb554a534c6df738be3f4d23327e69888e634John Wren KennedyAssertCompile(sizeof(RTMEMSAFERHDR) < RTMEMSAFER_PAD_BEFORE);
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/*******************************************************************************
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy* Global Variables *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy*******************************************************************************/
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy/** XOR scrambler value.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * @todo determine this at runtime */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#if ARCH_BITS == 32
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedystatic uintptr_t g_uScramblerXor = UINT32_C(0x867af88d);
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#elif ARCH_BITS == 64
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedystatic uintptr_t g_uScramblerXor = UINT64_C(0xed95ecc99416d312);
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#else
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy# error "Bad ARCH_BITS value"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#endif
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy/**
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * Support (SUPR3) based allocator.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy *
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * @returns VBox status code.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * @retval VERR_NOT_SUPPORTED if this allocation method is not supported in this
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * version of the library.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * @param ppvNew Where to store the pointer to the new buffer on success.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * @param cb Amount of bytes to allocate.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy *
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * @note: The allocation will have an extra page allocated before and after the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy * user area with all access rights removed if the host supports that to
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * prevent heartbleed like attacks.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy */
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedystatic int rtMemSaferSupR3Alloc(void **ppvNew, size_t cb)
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#if defined(IN_SUP_R3) && defined(VBOX) && !defined(RT_NO_GIP)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy /*
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * Allocate locked memory from the support library.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy *
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy */
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy size_t cbUser = RT_ALIGN_Z(cb, PAGE_SIZE);
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy size_t cPages = cbUser / PAGE_SIZE + 2; /* For the extra guarding pages. */
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy void *pvNew = NULL;
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy int rc = SUPR3PageAllocEx(cPages, 0 /* fFlags */, &pvNew, NULL /* pR0Ptr */, NULL /* paPages */);
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy if (RT_SUCCESS(rc))
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy {
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy /*
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * Change the memory protection of the pages guarding the allocation.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * Some hosts don't support changing the page protection, ignore these
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy * errors.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy */
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy rc = SUPR3PageProtect(pvNew, NIL_RTR0PTR, 0, PAGE_SIZE, RTMEM_PROT_NONE);
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy if (RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy {
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy Assert(PAGE_SIZE + cbUser == (size_t)((uint32_t)(PAGE_SIZE + cbUser)));
if (rc == VERR_NOT_SUPPORTED)
rc = VINF_SUCCESS;
else
rc = SUPR3PageProtect(pvNew, NIL_RTR0PTR, PAGE_SIZE + (uint32_t)cbUser, PAGE_SIZE, RTMEM_PROT_NONE);
if (RT_SUCCESS(rc))
{
*ppvNew = (uint8_t *)pvNew + PAGE_SIZE;
return VINF_SUCCESS;
}
}
rc = SUPR3PageFreeEx(pvNew, cPages);
AssertRC(rc);
}
return rc;
#else
return VERR_NOT_SUPPORTED;
#endif
}
/**
* Free method for memory allocated using the Support (SUPR3) based allocator.
*
* @returns nothing.
* @param pv Pointer to the memory to free.
* @param cb Amount of bytes allocated.
*/
static void rtMemSafeSupR3Free(void *pv, size_t cb)
{
#if defined(IN_SUP_R3) && defined(VBOX) && !defined(RT_NO_GIP)
size_t cbUser = RT_ALIGN_Z(cb, PAGE_SIZE);
size_t cPages = cbUser / PAGE_SIZE + 2; /* For the extra pages. */
void *pvStart = (uint8_t *)pv - PAGE_SIZE;
int rc = SUPR3PageFreeEx(pvStart, cPages);
AssertRC(rc);
#else
AssertMsgFailed(("SUPR3 allocated memory but freeing is not supported, messed up\n"));
#endif
}
RTDECL(int) RTMemSaferScramble(void *pv, size_t cb)
{
PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)((char *)pv - RTMEMSAFER_PAD_BEFORE);
AssertMsg(pHdr->cb == cb, ("pHdr->cb=%#zx cb=%#zx\n", pHdr->cb, cb));
/* Note! This isn't supposed to be safe, just less obvious. */
uintptr_t *pu = (uintptr_t *)pv;
cb = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
while (cb > 0)
{
*pu ^= g_uScramblerXor;
pu++;
cb -= sizeof(*pu);
}
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTMemSaferScramble);
RTDECL(int) RTMemSaferUnscramble(void *pv, size_t cb)
{
PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)((char *)pv - RTMEMSAFER_PAD_BEFORE);
AssertMsg(pHdr->cb == cb, ("pHdr->cb=%#zx cb=%#zx\n", pHdr->cb, cb));
/* Note! This isn't supposed to be safe, just less obvious. */
uintptr_t *pu = (uintptr_t *)pv;
cb = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
while (cb > 0)
{
*pu ^= g_uScramblerXor;
pu++;
cb -= sizeof(*pu);
}
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTMemSaferUnscramble);
RTDECL(int) RTMemSaferAllocZExTag(void **ppvNew, size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW
{
AssertReturn(cb, VERR_INVALID_PARAMETER);
AssertPtrReturn(ppvNew, VERR_INVALID_PARAMETER);
*ppvNew = NULL;
/*
* Don't request zeroed memory. We want random heap garbage in the
* padding zones, nothing that makes our allocations easier to find.
*/
RTMEMSAFERALLOCMETHOD enmAllocMethod = RTMEMSAFERALLOCMETHOD_SUPR3;
size_t cbUser = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
void *pvNew = NULL;
int rc = rtMemSaferSupR3Alloc(&pvNew, cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER);
if ( RT_FAILURE(rc)
&& fFlags & RTMEMSAFER_ALLOC_EX_ALLOW_PAGEABLE_BACKING)
{
/* Pageable memory allowed. */
enmAllocMethod = RTMEMSAFERALLOCMETHOD_RTMEM;
pvNew = RTMemAlloc(cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER);
}
if (pvNew)
{
PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)pvNew;
pHdr->fFlags = fFlags;
pHdr->cb = cb;
pHdr->enmAllocMethod = enmAllocMethod;
#ifdef RT_STRICT /* For checking input in strict builds. */
memset((char *)pvNew + sizeof(RTMEMSAFERHDR), 0xad, RTMEMSAFER_PAD_BEFORE - sizeof(RTMEMSAFERHDR));
memset((char *)pvNew + RTMEMSAFER_PAD_BEFORE + cb, 0xda, RTMEMSAFER_PAD_AFTER + (cbUser - cb));
#endif
void *pvUser = (char *)pvNew + RTMEMSAFER_PAD_BEFORE;
*ppvNew = pvUser;
/* You don't use this API for performance, so we always clean memory. */
RT_BZERO(pvUser, cb);
return VINF_SUCCESS;
}
return rc;
}
RT_EXPORT_SYMBOL(RTMemSaferAllocZExTag);
RTDECL(void) RTMemSaferFree(void *pv, size_t cb) RT_NO_THROW
{
if (pv)
{
Assert(cb);
size_t cbUser = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
void *pvStart = (char *)pv - RTMEMSAFER_PAD_BEFORE;
PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)pvStart;
AssertMsg(pHdr->cb == cb, ("pHdr->cb=%#zx cb=%#zx\n", pHdr->cb, cb));
RTMemWipeThoroughly(pv, RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN), 3);
switch (pHdr->enmAllocMethod)
{
case RTMEMSAFERALLOCMETHOD_SUPR3:
rtMemSafeSupR3Free(pvStart, cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER);
break;
case RTMEMSAFERALLOCMETHOD_RTMEM:
RTMemFree(pvStart);
break;
default:
AssertMsgFailed(("Invalid allocation method, corrupted header\n"));
}
}
else
Assert(cb == 0);
}
RT_EXPORT_SYMBOL(RTMemSaferFree);
RTDECL(int) RTMemSaferReallocZExTag(size_t cbOld, void *pvOld, size_t cbNew, void **ppvNew, uint32_t fFlags, const char *pszTag) RT_NO_THROW
{
/*
* We cannot let the heap move us around because we will be failing in our
* duty to clean things up. So, allocate a new block, copy over the old
* content, and free the old one.
*/
int rc;
/* Real realloc. */
if (cbNew && cbOld)
{
PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)((char *)pvOld - RTMEMSAFER_PAD_BEFORE);
AssertPtr(pvOld);
AssertMsg(*(size_t *)((char *)pvOld - RTMEMSAFER_PAD_BEFORE) == cbOld,
("*pvStart=%#zx cbOld=%#zx\n", *(size_t *)((char *)pvOld - RTMEMSAFER_PAD_BEFORE), cbOld));
void *pvNew;
rc = RTMemSaferAllocZExTag(&pvNew, cbNew, pHdr->fFlags, pszTag);
if (RT_SUCCESS(rc))
{
memcpy(pvNew, pvOld, RT_MIN(cbNew, cbOld));
RTMemSaferFree(pvOld, cbOld);
*ppvNew = pvNew;
}
}
/* First allocation. */
else if (!cbOld)
{
Assert(pvOld == NULL);
rc = RTMemSaferAllocZExTag(ppvNew, cbNew, fFlags, pszTag);
}
/* Free operation*/
else
{
RTMemSaferFree(pvOld, cbOld);
rc = VINF_SUCCESS;
}
return rc;
}
RT_EXPORT_SYMBOL(RTMemSaferReallocZExTag);
RTDECL(void *) RTMemSaferAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
{
void *pvNew = NULL;
int rc = RTMemSaferAllocZExTag(&pvNew, cb, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT, pszTag);
if (RT_SUCCESS(rc))
return pvNew;
return NULL;
}
RT_EXPORT_SYMBOL(RTMemSaferAllocZTag);
RTDECL(void *) RTMemSaferReallocZTag(size_t cbOld, void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
{
void *pvNew = NULL;
int rc = RTMemSaferReallocZExTag(cbOld, pvOld, cbNew, &pvNew, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT, pszTag);
if (RT_SUCCESS(rc))
return pvNew;
return NULL;
}
RT_EXPORT_SYMBOL(RTMemSaferReallocZTag);