[1] | 1 | /* $Id: alloc-ef.cpp 103005 2024-01-23 23:55:58Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Memory Allocation, electric fence.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #include "alloc-ef.h"
|
---|
[28271] | 42 | #include <iprt/mem.h>
|
---|
[1] | 43 | #include <iprt/log.h>
|
---|
| 44 | #include <iprt/asm.h>
|
---|
[103005] | 45 | #include <iprt/asm-mem.h>
|
---|
[1] | 46 | #include <iprt/thread.h>
|
---|
| 47 | #include <VBox/sup.h>
|
---|
[76452] | 48 | #include <iprt/errcore.h>
|
---|
[95818] | 49 | #ifndef IPRT_NO_CRT
|
---|
| 50 | # include <errno.h>
|
---|
| 51 | # include <stdio.h>
|
---|
| 52 | # include <stdlib.h>
|
---|
| 53 | #endif
|
---|
[1] | 54 |
|
---|
| 55 | #include <iprt/alloc.h>
|
---|
| 56 | #include <iprt/assert.h>
|
---|
| 57 | #include <iprt/param.h>
|
---|
| 58 | #include <iprt/string.h>
|
---|
[100313] | 59 | #include <iprt/system.h>
|
---|
[1] | 60 |
|
---|
[53278] | 61 | #ifdef RTALLOC_REPLACE_MALLOC
|
---|
| 62 | # include <VBox/dis.h>
|
---|
| 63 | # include <VBox/disopcode.h>
|
---|
| 64 | # include <dlfcn.h>
|
---|
| 65 | # ifdef RT_OS_DARWIN
|
---|
| 66 | # include <malloc/malloc.h>
|
---|
| 67 | # endif
|
---|
| 68 | #endif
|
---|
[1] | 69 |
|
---|
[53278] | 70 |
|
---|
[57358] | 71 | /*********************************************************************************************************************************
|
---|
| 72 | * Defined Constants And Macros *
|
---|
| 73 | *********************************************************************************************************************************/
|
---|
[53278] | 74 | #ifdef RTALLOC_REPLACE_MALLOC
|
---|
| 75 | # define RTMEM_REPLACMENT_ALIGN(a_cb) ((a_cb) >= 16 ? RT_ALIGN_Z(a_cb, 16) \
|
---|
| 76 | : (a_cb) >= sizeof(uintptr_t) ? RT_ALIGN_Z(a_cb, sizeof(uintptr_t)) : (a_cb))
|
---|
| 77 | #endif
|
---|
| 78 |
|
---|
| 79 |
|
---|
[57358] | 80 | /*********************************************************************************************************************************
|
---|
| 81 | * Global Variables *
|
---|
| 82 | *********************************************************************************************************************************/
|
---|
[1] | 83 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
[33540] | 84 | /** Spinlock protecting the all the block's globals. */
|
---|
[1] | 85 | static volatile uint32_t g_BlocksLock;
|
---|
| 86 | /** Tree tracking the allocations. */
|
---|
| 87 | static AVLPVTREE g_BlocksTree;
|
---|
[39091] | 88 | # ifdef RTALLOC_EFENCE_FREE_DELAYED
|
---|
[1] | 89 | /** Tail of the delayed blocks. */
|
---|
| 90 | static volatile PRTMEMBLOCK g_pBlocksDelayHead;
|
---|
| 91 | /** Tail of the delayed blocks. */
|
---|
| 92 | static volatile PRTMEMBLOCK g_pBlocksDelayTail;
|
---|
| 93 | /** Number of bytes in the delay list (includes fences). */
|
---|
| 94 | static volatile size_t g_cbBlocksDelay;
|
---|
[39091] | 95 | # endif /* RTALLOC_EFENCE_FREE_DELAYED */
|
---|
[53278] | 96 | # ifdef RTALLOC_REPLACE_MALLOC
|
---|
| 97 | /** @name For calling the real allocation API we've replaced.
|
---|
| 98 | * @{ */
|
---|
| 99 | void * (*g_pfnOrgMalloc)(size_t);
|
---|
| 100 | void * (*g_pfnOrgCalloc)(size_t, size_t);
|
---|
| 101 | void * (*g_pfnOrgRealloc)(void *, size_t);
|
---|
| 102 | void (*g_pfnOrgFree)(void *);
|
---|
| 103 | size_t (*g_pfnOrgMallocSize)(void *);
|
---|
| 104 | /** @} */
|
---|
| 105 | # endif
|
---|
[39091] | 106 | #endif /* RTALLOC_EFENCE_TRACE */
|
---|
[1] | 107 | /** Array of pointers free watches for. */
|
---|
[26258] | 108 | void *gapvRTMemFreeWatch[4] = {NULL, NULL, NULL, NULL};
|
---|
[1] | 109 | /** Enable logging of all freed memory. */
|
---|
| 110 | bool gfRTMemFreeLog = false;
|
---|
| 111 |
|
---|
| 112 |
|
---|
[57358] | 113 | /*********************************************************************************************************************************
|
---|
| 114 | * Internal Functions *
|
---|
| 115 | *********************************************************************************************************************************/
|
---|
[53278] | 116 | #ifdef RTALLOC_REPLACE_MALLOC
|
---|
| 117 | static void rtMemReplaceMallocAndFriends(void);
|
---|
| 118 | #endif
|
---|
| 119 |
|
---|
| 120 |
|
---|
[1] | 121 | /**
|
---|
| 122 | * Complains about something.
|
---|
| 123 | */
|
---|
| 124 | static void rtmemComplain(const char *pszOp, const char *pszFormat, ...)
|
---|
| 125 | {
|
---|
| 126 | va_list args;
|
---|
| 127 | fprintf(stderr, "RTMem error: %s: ", pszOp);
|
---|
| 128 | va_start(args, pszFormat);
|
---|
| 129 | vfprintf(stderr, pszFormat, args);
|
---|
| 130 | va_end(args);
|
---|
[13306] | 131 | RTAssertDoPanic();
|
---|
[1] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Log an event.
|
---|
| 136 | */
|
---|
[28298] | 137 | DECLINLINE(void) rtmemLog(const char *pszOp, const char *pszFormat, ...)
|
---|
[1] | 138 | {
|
---|
| 139 | #if 0
|
---|
| 140 | va_list args;
|
---|
| 141 | fprintf(stderr, "RTMem info: %s: ", pszOp);
|
---|
| 142 | va_start(args, pszFormat);
|
---|
| 143 | vfprintf(stderr, pszFormat, args);
|
---|
| 144 | va_end(args);
|
---|
[39083] | 145 | #else
|
---|
| 146 | NOREF(pszOp); NOREF(pszFormat);
|
---|
[1] | 147 | #endif
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
[33540] | 154 | * Acquires the lock.
|
---|
[1] | 155 | */
|
---|
[28298] | 156 | DECLINLINE(void) rtmemBlockLock(void)
|
---|
[1] | 157 | {
|
---|
| 158 | unsigned c = 0;
|
---|
| 159 | while (!ASMAtomicCmpXchgU32(&g_BlocksLock, 1, 0))
|
---|
[39443] | 160 | RTThreadSleepNoLog(((++c) >> 2) & 31);
|
---|
[1] | 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * Releases the lock.
|
---|
| 166 | */
|
---|
[28298] | 167 | DECLINLINE(void) rtmemBlockUnlock(void)
|
---|
[1] | 168 | {
|
---|
| 169 | Assert(g_BlocksLock == 1);
|
---|
| 170 | ASMAtomicXchgU32(&g_BlocksLock, 0);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * Creates a block.
|
---|
| 176 | */
|
---|
[28298] | 177 | DECLINLINE(PRTMEMBLOCK) rtmemBlockCreate(RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
|
---|
[31157] | 178 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL)
|
---|
[1] | 179 | {
|
---|
[53278] | 180 | # ifdef RTALLOC_REPLACE_MALLOC
|
---|
| 181 | if (!g_pfnOrgMalloc)
|
---|
| 182 | rtMemReplaceMallocAndFriends();
|
---|
| 183 | PRTMEMBLOCK pBlock = (PRTMEMBLOCK)g_pfnOrgMalloc(sizeof(*pBlock));
|
---|
| 184 | # else
|
---|
[1] | 185 | PRTMEMBLOCK pBlock = (PRTMEMBLOCK)malloc(sizeof(*pBlock));
|
---|
[53278] | 186 | # endif
|
---|
[1] | 187 | if (pBlock)
|
---|
| 188 | {
|
---|
[28298] | 189 | pBlock->enmType = enmType;
|
---|
| 190 | pBlock->cbUnaligned = cbUnaligned;
|
---|
| 191 | pBlock->cbAligned = cbAligned;
|
---|
[31157] | 192 | pBlock->pszTag = pszTag;
|
---|
[28298] | 193 | pBlock->pvCaller = pvCaller;
|
---|
| 194 | pBlock->iLine = iLine;
|
---|
| 195 | pBlock->pszFile = pszFile;
|
---|
[1] | 196 | pBlock->pszFunction = pszFunction;
|
---|
| 197 | }
|
---|
| 198 | return pBlock;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * Frees a block.
|
---|
| 204 | */
|
---|
[28298] | 205 | DECLINLINE(void) rtmemBlockFree(PRTMEMBLOCK pBlock)
|
---|
[1] | 206 | {
|
---|
[53278] | 207 | # ifdef RTALLOC_REPLACE_MALLOC
|
---|
| 208 | g_pfnOrgFree(pBlock);
|
---|
| 209 | # else
|
---|
[1] | 210 | free(pBlock);
|
---|
[53278] | 211 | # endif
|
---|
[1] | 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Insert a block from the tree.
|
---|
| 217 | */
|
---|
[28298] | 218 | DECLINLINE(void) rtmemBlockInsert(PRTMEMBLOCK pBlock, void *pv)
|
---|
[1] | 219 | {
|
---|
| 220 | pBlock->Core.Key = pv;
|
---|
| 221 | rtmemBlockLock();
|
---|
| 222 | bool fRc = RTAvlPVInsert(&g_BlocksTree, &pBlock->Core);
|
---|
| 223 | rtmemBlockUnlock();
|
---|
| 224 | AssertRelease(fRc);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Remove a block from the tree and returns it to the caller.
|
---|
| 230 | */
|
---|
[28298] | 231 | DECLINLINE(PRTMEMBLOCK) rtmemBlockRemove(void *pv)
|
---|
[1] | 232 | {
|
---|
| 233 | rtmemBlockLock();
|
---|
| 234 | PRTMEMBLOCK pBlock = (PRTMEMBLOCK)RTAvlPVRemove(&g_BlocksTree, pv);
|
---|
| 235 | rtmemBlockUnlock();
|
---|
| 236 | return pBlock;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
| 240 | * Gets a block.
|
---|
| 241 | */
|
---|
[28298] | 242 | DECLINLINE(PRTMEMBLOCK) rtmemBlockGet(void *pv)
|
---|
[1] | 243 | {
|
---|
| 244 | rtmemBlockLock();
|
---|
| 245 | PRTMEMBLOCK pBlock = (PRTMEMBLOCK)RTAvlPVGet(&g_BlocksTree, pv);
|
---|
| 246 | rtmemBlockUnlock();
|
---|
| 247 | return pBlock;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Dumps one allocation.
|
---|
| 252 | */
|
---|
| 253 | static DECLCALLBACK(int) RTMemDumpOne(PAVLPVNODECORE pNode, void *pvUser)
|
---|
| 254 | {
|
---|
| 255 | PRTMEMBLOCK pBlock = (PRTMEMBLOCK)pNode;
|
---|
[28298] | 256 | fprintf(stderr, "%p %08lx(+%02lx) %p\n",
|
---|
[1] | 257 | pBlock->Core.Key,
|
---|
[28298] | 258 | (unsigned long)pBlock->cbUnaligned,
|
---|
| 259 | (unsigned long)(pBlock->cbAligned - pBlock->cbUnaligned),
|
---|
[1] | 260 | pBlock->pvCaller);
|
---|
[39083] | 261 | NOREF(pvUser);
|
---|
[1] | 262 | return 0;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /**
|
---|
| 266 | * Dumps the allocated blocks.
|
---|
| 267 | * This is something which you should call from gdb.
|
---|
| 268 | */
|
---|
| 269 | extern "C" void RTMemDump(void);
|
---|
| 270 | void RTMemDump(void)
|
---|
| 271 | {
|
---|
[28298] | 272 | fprintf(stderr, "address size(alg) caller\n");
|
---|
[1] | 273 | RTAvlPVDoWithAll(&g_BlocksTree, true, RTMemDumpOne, NULL);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[39091] | 276 | # ifdef RTALLOC_EFENCE_FREE_DELAYED
|
---|
[1] | 277 |
|
---|
| 278 | /**
|
---|
| 279 | * Insert a delayed block.
|
---|
| 280 | */
|
---|
[28298] | 281 | DECLINLINE(void) rtmemBlockDelayInsert(PRTMEMBLOCK pBlock)
|
---|
[1] | 282 | {
|
---|
[100313] | 283 | size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
|
---|
| 284 | size_t const cbBlock = RTSystemPageAlignSize(pBlock->cbAligned) + cbFence;
|
---|
[1] | 285 | pBlock->Core.pRight = NULL;
|
---|
| 286 | pBlock->Core.pLeft = NULL;
|
---|
| 287 | rtmemBlockLock();
|
---|
| 288 | if (g_pBlocksDelayHead)
|
---|
| 289 | {
|
---|
| 290 | g_pBlocksDelayHead->Core.pLeft = (PAVLPVNODECORE)pBlock;
|
---|
| 291 | pBlock->Core.pRight = (PAVLPVNODECORE)g_pBlocksDelayHead;
|
---|
| 292 | g_pBlocksDelayHead = pBlock;
|
---|
| 293 | }
|
---|
| 294 | else
|
---|
| 295 | {
|
---|
| 296 | g_pBlocksDelayTail = pBlock;
|
---|
| 297 | g_pBlocksDelayHead = pBlock;
|
---|
| 298 | }
|
---|
| 299 | g_cbBlocksDelay += cbBlock;
|
---|
| 300 | rtmemBlockUnlock();
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /**
|
---|
| 304 | * Removes a delayed block.
|
---|
| 305 | */
|
---|
[28298] | 306 | DECLINLINE(PRTMEMBLOCK) rtmemBlockDelayRemove(void)
|
---|
[1] | 307 | {
|
---|
| 308 | PRTMEMBLOCK pBlock = NULL;
|
---|
| 309 | rtmemBlockLock();
|
---|
| 310 | if (g_cbBlocksDelay > RTALLOC_EFENCE_FREE_DELAYED)
|
---|
| 311 | {
|
---|
| 312 | pBlock = g_pBlocksDelayTail;
|
---|
| 313 | if (pBlock)
|
---|
| 314 | {
|
---|
| 315 | g_pBlocksDelayTail = (PRTMEMBLOCK)pBlock->Core.pLeft;
|
---|
| 316 | if (pBlock->Core.pLeft)
|
---|
| 317 | pBlock->Core.pLeft->pRight = NULL;
|
---|
| 318 | else
|
---|
| 319 | g_pBlocksDelayHead = NULL;
|
---|
[100313] | 320 |
|
---|
| 321 | size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
|
---|
| 322 | g_cbBlocksDelay -= RTSystemPageAlignSize(pBlock->cbAligned) + cbFence;
|
---|
[1] | 323 | }
|
---|
| 324 | }
|
---|
| 325 | rtmemBlockUnlock();
|
---|
| 326 | return pBlock;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[86296] | 329 |
|
---|
| 330 | /**
|
---|
| 331 | * Dumps the freed blocks.
|
---|
| 332 | * This is something which you should call from gdb.
|
---|
| 333 | */
|
---|
| 334 | extern "C" void RTMemDumpFreed(void);
|
---|
| 335 | void RTMemDumpFreed(void)
|
---|
| 336 | {
|
---|
| 337 | fprintf(stderr, "address size(alg) caller\n");
|
---|
| 338 | for (PRTMEMBLOCK pCur = g_pBlocksDelayHead; pCur; pCur = (PRTMEMBLOCK)pCur->Core.pRight)
|
---|
| 339 | RTMemDumpOne(&pCur->Core, NULL);
|
---|
| 340 |
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[39091] | 343 | # endif /* RTALLOC_EFENCE_FREE_DELAYED */
|
---|
[1] | 344 |
|
---|
| 345 | #endif /* RTALLOC_EFENCE_TRACE */
|
---|
| 346 |
|
---|
| 347 |
|
---|
[53278] | 348 | #if defined(RTALLOC_REPLACE_MALLOC) && defined(RTALLOC_EFENCE_TRACE)
|
---|
| 349 | /*
|
---|
| 350 | *
|
---|
| 351 | * Replacing malloc, calloc, realloc, & free.
|
---|
| 352 | *
|
---|
| 353 | */
|
---|
| 354 |
|
---|
| 355 | /** Replacement for malloc. */
|
---|
| 356 | static void *rtMemReplacementMalloc(size_t cb)
|
---|
| 357 | {
|
---|
| 358 | size_t cbAligned = RTMEM_REPLACMENT_ALIGN(cb);
|
---|
| 359 | void *pv = rtR3MemAlloc("r-malloc", RTMEMTYPE_RTMEMALLOC, cb, cbAligned, "heap", ASMReturnAddress(), RT_SRC_POS);
|
---|
| 360 | if (!pv)
|
---|
| 361 | pv = g_pfnOrgMalloc(cb);
|
---|
| 362 | return pv;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /** Replacement for calloc. */
|
---|
| 366 | static void *rtMemReplacementCalloc(size_t cbItem, size_t cItems)
|
---|
| 367 | {
|
---|
| 368 | size_t cb = cbItem * cItems;
|
---|
| 369 | size_t cbAligned = RTMEM_REPLACMENT_ALIGN(cb);
|
---|
| 370 | void *pv = rtR3MemAlloc("r-calloc", RTMEMTYPE_RTMEMALLOCZ, cb, cbAligned, "heap", ASMReturnAddress(), RT_SRC_POS);
|
---|
| 371 | if (!pv)
|
---|
| 372 | pv = g_pfnOrgCalloc(cbItem, cItems);
|
---|
| 373 | return pv;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /** Replacement for realloc. */
|
---|
| 377 | static void *rtMemReplacementRealloc(void *pvOld, size_t cbNew)
|
---|
| 378 | {
|
---|
| 379 | if (pvOld)
|
---|
| 380 | {
|
---|
| 381 | /* We're not strict about where the memory was allocated. */
|
---|
| 382 | PRTMEMBLOCK pBlock = rtmemBlockGet(pvOld);
|
---|
| 383 | if (pBlock)
|
---|
| 384 | {
|
---|
| 385 | size_t cbAligned = RTMEM_REPLACMENT_ALIGN(cbNew);
|
---|
| 386 | return rtR3MemRealloc("r-realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbAligned, "heap", ASMReturnAddress(), RT_SRC_POS);
|
---|
| 387 | }
|
---|
| 388 | return g_pfnOrgRealloc(pvOld, cbNew);
|
---|
| 389 | }
|
---|
| 390 | return rtMemReplacementMalloc(cbNew);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /** Replacement for free(). */
|
---|
| 394 | static void rtMemReplacementFree(void *pv)
|
---|
| 395 | {
|
---|
| 396 | if (pv)
|
---|
| 397 | {
|
---|
| 398 | /* We're not strict about where the memory was allocated. */
|
---|
| 399 | PRTMEMBLOCK pBlock = rtmemBlockGet(pv);
|
---|
| 400 | if (pBlock)
|
---|
[83546] | 401 | rtR3MemFree("r-free", RTMEMTYPE_RTMEMFREE, pv, 0, ASMReturnAddress(), RT_SRC_POS);
|
---|
[53278] | 402 | else
|
---|
| 403 | g_pfnOrgFree(pv);
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | # ifdef RT_OS_DARWIN
|
---|
| 408 | /** Replacement for malloc. */
|
---|
| 409 | static size_t rtMemReplacementMallocSize(void *pv)
|
---|
| 410 | {
|
---|
| 411 | size_t cb;
|
---|
| 412 | if (pv)
|
---|
| 413 | {
|
---|
| 414 | /* We're not strict about where the memory was allocated. */
|
---|
| 415 | PRTMEMBLOCK pBlock = rtmemBlockGet(pv);
|
---|
| 416 | if (pBlock)
|
---|
| 417 | cb = pBlock->cbUnaligned;
|
---|
| 418 | else
|
---|
| 419 | cb = g_pfnOrgMallocSize(pv);
|
---|
| 420 | }
|
---|
| 421 | else
|
---|
| 422 | cb = 0;
|
---|
| 423 | return cb;
|
---|
| 424 | }
|
---|
| 425 | # endif
|
---|
| 426 |
|
---|
| 427 |
|
---|
| 428 | static void rtMemReplaceMallocAndFriends(void)
|
---|
| 429 | {
|
---|
| 430 | struct
|
---|
| 431 | {
|
---|
| 432 | const char *pszName;
|
---|
| 433 | PFNRT pfnReplacement;
|
---|
| 434 | PFNRT pfnOrg;
|
---|
| 435 | PFNRT *ppfnJumpBack;
|
---|
| 436 | } aApis[] =
|
---|
| 437 | {
|
---|
| 438 | { "free", (PFNRT)rtMemReplacementFree, (PFNRT)free, (PFNRT *)&g_pfnOrgFree },
|
---|
| 439 | { "realloc", (PFNRT)rtMemReplacementRealloc, (PFNRT)realloc, (PFNRT *)&g_pfnOrgRealloc },
|
---|
| 440 | { "calloc", (PFNRT)rtMemReplacementCalloc, (PFNRT)calloc, (PFNRT *)&g_pfnOrgCalloc },
|
---|
| 441 | { "malloc", (PFNRT)rtMemReplacementMalloc, (PFNRT)malloc, (PFNRT *)&g_pfnOrgMalloc },
|
---|
| 442 | #ifdef RT_OS_DARWIN
|
---|
| 443 | { "malloc_size", (PFNRT)rtMemReplacementMallocSize, (PFNRT)malloc_size, (PFNRT *)&g_pfnOrgMallocSize },
|
---|
| 444 | #endif
|
---|
| 445 | };
|
---|
| 446 |
|
---|
| 447 | /*
|
---|
| 448 | * Initialize the jump backs to avoid recursivly entering this function.
|
---|
| 449 | */
|
---|
| 450 | for (unsigned i = 0; i < RT_ELEMENTS(aApis); i++)
|
---|
| 451 | *aApis[i].ppfnJumpBack = aApis[i].pfnOrg;
|
---|
| 452 |
|
---|
| 453 | /*
|
---|
| 454 | * Give the user an option to skip replacing malloc.
|
---|
| 455 | */
|
---|
| 456 | if (getenv("IPRT_DONT_REPLACE_MALLOC"))
|
---|
| 457 | return;
|
---|
| 458 |
|
---|
| 459 | /*
|
---|
| 460 | * Allocate a page for jump back code (we leak it).
|
---|
| 461 | */
|
---|
[100313] | 462 | size_t const cbPage = RTSystemGetPageSize();
|
---|
| 463 | uint8_t *pbExecPage = (uint8_t *)RTMemPageAlloc(cbPage); AssertFatal(pbExecPage);
|
---|
| 464 | int rc = RTMemProtect(pbExecPage, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC); AssertFatalRC(rc);
|
---|
[53278] | 465 |
|
---|
| 466 | /*
|
---|
| 467 | * Do the ground work.
|
---|
| 468 | */
|
---|
| 469 | uint8_t *pb = pbExecPage;
|
---|
| 470 | for (unsigned i = 0; i < RT_ELEMENTS(aApis); i++)
|
---|
| 471 | {
|
---|
| 472 | /* Resolve it. */
|
---|
| 473 | PFNRT pfnOrg = (PFNRT)(uintptr_t)dlsym(RTLD_DEFAULT, aApis[i].pszName);
|
---|
| 474 | if (pfnOrg)
|
---|
| 475 | aApis[i].pfnOrg = pfnOrg;
|
---|
| 476 | else
|
---|
| 477 | pfnOrg = aApis[i].pfnOrg;
|
---|
| 478 |
|
---|
| 479 | /* Figure what we can replace and how much to duplicate in the jump back code. */
|
---|
| 480 | # ifdef RT_ARCH_AMD64
|
---|
| 481 | uint32_t cbNeeded = 12;
|
---|
| 482 | DISCPUMODE const enmCpuMode = DISCPUMODE_64BIT;
|
---|
| 483 | # elif defined(RT_ARCH_X86)
|
---|
| 484 | uint32_t const cbNeeded = 5;
|
---|
| 485 | DISCPUMODE const enmCpuMode = DISCPUMODE_32BIT;
|
---|
| 486 | # else
|
---|
| 487 | # error "Port me"
|
---|
| 488 | # endif
|
---|
| 489 | uint32_t offJmpBack = 0;
|
---|
| 490 | uint32_t cbCopy = 0;
|
---|
| 491 | while (offJmpBack < cbNeeded)
|
---|
| 492 | {
|
---|
| 493 | DISCPUSTATE Dis;
|
---|
| 494 | uint32_t cbInstr = 1;
|
---|
| 495 | rc = DISInstr((void *)((uintptr_t)pfnOrg + offJmpBack), enmCpuMode, &Dis, &cbInstr); AssertFatalRC(rc);
|
---|
| 496 | AssertFatal(!(Dis.pCurInstr->fOpType & (DISOPTYPE_CONTROLFLOW)));
|
---|
| 497 | # ifdef RT_ARCH_AMD64
|
---|
| 498 | # ifdef RT_OS_DARWIN
|
---|
| 499 | /* Kludge for: cmp [malloc_def_zone_state], 1; jg 2; call _malloc_initialize; 2: */
|
---|
| 500 | if ( Dis.ModRM.Bits.Mod == 0
|
---|
| 501 | && Dis.ModRM.Bits.Rm == 5 /* wrt RIP */
|
---|
| 502 | && (Dis.Param2.fUse & (DISUSE_IMMEDIATE16_SX8 | DISUSE_IMMEDIATE32_SX8 | DISUSE_IMMEDIATE64_SX8))
|
---|
| 503 | && Dis.Param2.uValue == 1
|
---|
| 504 | && Dis.pCurInstr->uOpcode == OP_CMP)
|
---|
| 505 | {
|
---|
| 506 | cbCopy = offJmpBack;
|
---|
| 507 |
|
---|
| 508 | offJmpBack += cbInstr;
|
---|
| 509 | rc = DISInstr((void *)((uintptr_t)pfnOrg + offJmpBack), enmCpuMode, &Dis, &cbInstr); AssertFatalRC(rc);
|
---|
| 510 | if ( Dis.pCurInstr->uOpcode == OP_JNBE
|
---|
| 511 | && Dis.Param1.uDisp.i8 == 5)
|
---|
| 512 | {
|
---|
| 513 | offJmpBack += cbInstr + 5;
|
---|
| 514 | AssertFatal(offJmpBack >= cbNeeded);
|
---|
| 515 | break;
|
---|
| 516 | }
|
---|
| 517 | }
|
---|
| 518 | # endif
|
---|
| 519 | AssertFatal(!(Dis.ModRM.Bits.Mod == 0 && Dis.ModRM.Bits.Rm == 5 /* wrt RIP */));
|
---|
| 520 | # endif
|
---|
| 521 | offJmpBack += cbInstr;
|
---|
| 522 | }
|
---|
| 523 | if (!cbCopy)
|
---|
| 524 | cbCopy = offJmpBack;
|
---|
| 525 |
|
---|
| 526 | /* Assemble the jump back. */
|
---|
| 527 | memcpy(pb, (void *)(uintptr_t)pfnOrg, cbCopy);
|
---|
| 528 | uint32_t off = cbCopy;
|
---|
| 529 | # ifdef RT_ARCH_AMD64
|
---|
| 530 | pb[off++] = 0xff; /* jmp qword [$+8 wrt RIP] */
|
---|
| 531 | pb[off++] = 0x25;
|
---|
| 532 | *(uint32_t *)&pb[off] = 0;
|
---|
| 533 | off += 4;
|
---|
| 534 | *(uint64_t *)&pb[off] = (uintptr_t)pfnOrg + offJmpBack;
|
---|
| 535 | off += 8;
|
---|
| 536 | off = RT_ALIGN_32(off, 16);
|
---|
| 537 | # elif defined(RT_ARCH_X86)
|
---|
| 538 | pb[off++] = 0xe9; /* jmp rel32 */
|
---|
| 539 | *(uint32_t *)&pb[off] = (uintptr_t)pfnOrg + offJmpBack - (uintptr_t)&pb[4];
|
---|
| 540 | off += 4;
|
---|
| 541 | off = RT_ALIGN_32(off, 8);
|
---|
| 542 | # else
|
---|
| 543 | # error "Port me"
|
---|
| 544 | # endif
|
---|
| 545 | *aApis[i].ppfnJumpBack = (PFNRT)(uintptr_t)pb;
|
---|
| 546 | pb += off;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | /*
|
---|
| 550 | * Modify the APIs.
|
---|
| 551 | */
|
---|
| 552 | for (unsigned i = 0; i < RT_ELEMENTS(aApis); i++)
|
---|
| 553 | {
|
---|
| 554 | pb = (uint8_t *)(uintptr_t)aApis[i].pfnOrg;
|
---|
| 555 | rc = RTMemProtect(pb, 16, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC); AssertFatalRC(rc);
|
---|
| 556 |
|
---|
| 557 | # ifdef RT_ARCH_AMD64
|
---|
| 558 | /* Assemble the LdrLoadDll patch. */
|
---|
| 559 | *pb++ = 0x48; /* mov rax, qword */
|
---|
| 560 | *pb++ = 0xb8;
|
---|
| 561 | *(uint64_t *)pb = (uintptr_t)aApis[i].pfnReplacement;
|
---|
| 562 | pb += 8;
|
---|
| 563 | *pb++ = 0xff; /* jmp rax */
|
---|
| 564 | *pb++ = 0xe0;
|
---|
| 565 | # elif defined(RT_ARCH_X86)
|
---|
| 566 | *pb++ = 0xe9; /* jmp rel32 */
|
---|
| 567 | *(uint32_t *)pb = (uintptr_t)aApis[i].pfnReplacement - (uintptr_t)&pb[4];
|
---|
| 568 | # else
|
---|
| 569 | # error "Port me"
|
---|
| 570 | # endif
|
---|
| 571 | }
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | #endif /* RTALLOC_REPLACE_MALLOC && RTALLOC_EFENCE_TRACE */
|
---|
| 575 |
|
---|
| 576 |
|
---|
[1] | 577 | /**
|
---|
| 578 | * Internal allocator.
|
---|
| 579 | */
|
---|
[28298] | 580 | RTDECL(void *) rtR3MemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
|
---|
[31157] | 581 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL)
|
---|
[1] | 582 | {
|
---|
| 583 | /*
|
---|
| 584 | * Sanity.
|
---|
| 585 | */
|
---|
[100313] | 586 | size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
|
---|
| 587 | size_t const cbPage = RTSystemGetPageSize();
|
---|
| 588 | if (RTALLOC_EFENCE_SIZE_FACTOR <= 0)
|
---|
[1] | 589 | {
|
---|
[100313] | 590 | rtmemComplain(pszOp, "Invalid E-fence size! %#x\n", RTALLOC_EFENCE_SIZE_FACTOR);
|
---|
[1] | 591 | return NULL;
|
---|
| 592 | }
|
---|
[28298] | 593 | if (!cbUnaligned)
|
---|
[1] | 594 | {
|
---|
| 595 | #if 0
|
---|
| 596 | rtmemComplain(pszOp, "Request of ZERO bytes allocation!\n");
|
---|
| 597 | return NULL;
|
---|
| 598 | #else
|
---|
[28298] | 599 | cbAligned = cbUnaligned = 1;
|
---|
[1] | 600 | #endif
|
---|
| 601 | }
|
---|
[28298] | 602 |
|
---|
[27404] | 603 | #ifndef RTALLOC_EFENCE_IN_FRONT
|
---|
| 604 | /* Alignment decreases fence accuracy, but this is at least partially
|
---|
| 605 | * counteracted by filling and checking the alignment padding. When the
|
---|
| 606 | * fence is in front then then no extra alignment is needed. */
|
---|
[28298] | 607 | cbAligned = RT_ALIGN_Z(cbAligned, RTALLOC_EFENCE_ALIGNMENT);
|
---|
[27404] | 608 | #endif
|
---|
[1] | 609 |
|
---|
| 610 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
| 611 | /*
|
---|
| 612 | * Allocate the trace block.
|
---|
| 613 | */
|
---|
[31157] | 614 | PRTMEMBLOCK pBlock = rtmemBlockCreate(enmType, cbUnaligned, cbAligned, pszTag, pvCaller, RT_SRC_POS_ARGS);
|
---|
[1] | 615 | if (!pBlock)
|
---|
| 616 | {
|
---|
| 617 | rtmemComplain(pszOp, "Failed to allocate trace block!\n");
|
---|
| 618 | return NULL;
|
---|
| 619 | }
|
---|
| 620 | #endif
|
---|
| 621 |
|
---|
| 622 | /*
|
---|
| 623 | * Allocate a block with page alignment space + the size of the E-fence.
|
---|
| 624 | */
|
---|
[100313] | 625 | size_t cbBlock = RT_ALIGN_Z(cbAligned, cbPage) + cbFence;
|
---|
[1] | 626 | void *pvBlock = RTMemPageAlloc(cbBlock);
|
---|
| 627 | if (pvBlock)
|
---|
| 628 | {
|
---|
| 629 | /*
|
---|
| 630 | * Calc the start of the fence and the user block
|
---|
| 631 | * and then change the page protection of the fence.
|
---|
| 632 | */
|
---|
[28273] | 633 | #ifdef RTALLOC_EFENCE_IN_FRONT
|
---|
[1] | 634 | void *pvEFence = pvBlock;
|
---|
[100313] | 635 | void *pv = (char *)pvEFence + cbFence;
|
---|
[27575] | 636 | # ifdef RTALLOC_EFENCE_NOMAN_FILLER
|
---|
[100313] | 637 | memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - cbFence - cbUnaligned);
|
---|
[27575] | 638 | # endif
|
---|
[28273] | 639 | #else
|
---|
[100313] | 640 | void *pvEFence = (char *)pvBlock + (cbBlock - cbFence);
|
---|
[28298] | 641 | void *pv = (char *)pvEFence - cbAligned;
|
---|
[27575] | 642 | # ifdef RTALLOC_EFENCE_NOMAN_FILLER
|
---|
[100313] | 643 | memset(pvBlock, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - cbFence - cbAligned);
|
---|
[28298] | 644 | memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbAligned - cbUnaligned);
|
---|
[27575] | 645 | # endif
|
---|
[28273] | 646 | #endif
|
---|
[27293] | 647 |
|
---|
| 648 | #ifdef RTALLOC_EFENCE_FENCE_FILLER
|
---|
[100313] | 649 | memset(pvEFence, RTALLOC_EFENCE_FENCE_FILLER, cbFence);
|
---|
[27293] | 650 | #endif
|
---|
[100313] | 651 | int rc = RTMemProtect(pvEFence, cbFence, RTMEM_PROT_NONE);
|
---|
[1] | 652 | if (!rc)
|
---|
| 653 | {
|
---|
[28273] | 654 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
[1] | 655 | rtmemBlockInsert(pBlock, pv);
|
---|
[28273] | 656 | #endif
|
---|
[1] | 657 | if (enmType == RTMEMTYPE_RTMEMALLOCZ)
|
---|
[28298] | 658 | memset(pv, 0, cbUnaligned);
|
---|
[1] | 659 | #ifdef RTALLOC_EFENCE_FILLER
|
---|
| 660 | else
|
---|
[28298] | 661 | memset(pv, RTALLOC_EFENCE_FILLER, cbUnaligned);
|
---|
[1] | 662 | #endif
|
---|
| 663 |
|
---|
[28298] | 664 | rtmemLog(pszOp, "returns %p (pvBlock=%p cbBlock=%#x pvEFence=%p cbUnaligned=%#x)\n", pv, pvBlock, cbBlock, pvEFence, cbUnaligned);
|
---|
[1] | 665 | return pv;
|
---|
| 666 | }
|
---|
[100313] | 667 | rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, cbFence, rc);
|
---|
[28317] | 668 | RTMemPageFree(pvBlock, cbBlock);
|
---|
[1] | 669 | }
|
---|
| 670 | else
|
---|
[28298] | 671 | rtmemComplain(pszOp, "Failed to allocated %lu (%lu) bytes.\n", (unsigned long)cbBlock, (unsigned long)cbUnaligned);
|
---|
[1] | 672 |
|
---|
| 673 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
| 674 | rtmemBlockFree(pBlock);
|
---|
| 675 | #endif
|
---|
| 676 | return NULL;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 |
|
---|
| 680 | /**
|
---|
| 681 | * Internal free.
|
---|
| 682 | */
|
---|
[83546] | 683 | RTDECL(void) rtR3MemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, size_t cbUser, void *pvCaller, RT_SRC_POS_DECL)
|
---|
[1] | 684 | {
|
---|
[39083] | 685 | NOREF(enmType); RT_SRC_POS_NOREF();
|
---|
| 686 |
|
---|
[1] | 687 | /*
|
---|
| 688 | * Simple case.
|
---|
| 689 | */
|
---|
| 690 | if (!pv)
|
---|
| 691 | return;
|
---|
| 692 |
|
---|
| 693 | /*
|
---|
| 694 | * Check watch points.
|
---|
| 695 | */
|
---|
[13836] | 696 | for (unsigned i = 0; i < RT_ELEMENTS(gapvRTMemFreeWatch); i++)
|
---|
[1] | 697 | if (gapvRTMemFreeWatch[i] == pv)
|
---|
[13306] | 698 | RTAssertDoPanic();
|
---|
[1] | 699 |
|
---|
[100313] | 700 | size_t cbPage = RTSystemGetPageSize();
|
---|
[1] | 701 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
| 702 | /*
|
---|
| 703 | * Find the block.
|
---|
| 704 | */
|
---|
| 705 | PRTMEMBLOCK pBlock = rtmemBlockRemove(pv);
|
---|
| 706 | if (pBlock)
|
---|
| 707 | {
|
---|
| 708 | if (gfRTMemFreeLog)
|
---|
[28298] | 709 | RTLogPrintf("RTMem %s: pv=%p pvCaller=%p cbUnaligned=%#x\n", pszOp, pv, pvCaller, pBlock->cbUnaligned);
|
---|
[1] | 710 |
|
---|
[28273] | 711 | # ifdef RTALLOC_EFENCE_NOMAN_FILLER
|
---|
[27293] | 712 | /*
|
---|
| 713 | * Check whether the no man's land is untouched.
|
---|
| 714 | */
|
---|
[28273] | 715 | # ifdef RTALLOC_EFENCE_IN_FRONT
|
---|
[59747] | 716 | void *pvWrong = ASMMemFirstMismatchingU8((char *)pv + pBlock->cbUnaligned,
|
---|
[100313] | 717 | RT_ALIGN_Z(pBlock->cbAligned, cbPage) - pBlock->cbUnaligned,
|
---|
[59747] | 718 | RTALLOC_EFENCE_NOMAN_FILLER);
|
---|
[28273] | 719 | # else
|
---|
[27404] | 720 | /* Alignment must match allocation alignment in rtMemAlloc(). */
|
---|
[59747] | 721 | void *pvWrong = ASMMemFirstMismatchingU8((char *)pv + pBlock->cbUnaligned,
|
---|
| 722 | pBlock->cbAligned - pBlock->cbUnaligned,
|
---|
| 723 | RTALLOC_EFENCE_NOMAN_FILLER);
|
---|
[27575] | 724 | if (pvWrong)
|
---|
| 725 | RTAssertDoPanic();
|
---|
[100313] | 726 | pvWrong = ASMMemFirstMismatchingU8((void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()),
|
---|
| 727 | RT_ALIGN_Z(pBlock->cbAligned, cbPage) - pBlock->cbAligned,
|
---|
[59747] | 728 | RTALLOC_EFENCE_NOMAN_FILLER);
|
---|
[28273] | 729 | # endif
|
---|
[27575] | 730 | if (pvWrong)
|
---|
[27404] | 731 | RTAssertDoPanic();
|
---|
[28273] | 732 | # endif
|
---|
[27293] | 733 |
|
---|
[1] | 734 | /*
|
---|
| 735 | * Fill the user part of the block.
|
---|
| 736 | */
|
---|
[83546] | 737 | AssertMsg(enmType != RTMEMTYPE_RTMEMFREEZ || cbUser == pBlock->cbUnaligned,
|
---|
| 738 | ("cbUser=%#zx cbUnaligned=%#zx\n", cbUser, pBlock->cbUnaligned));
|
---|
[83548] | 739 | RT_NOREF(cbUser);
|
---|
[83546] | 740 | if (enmType == RTMEMTYPE_RTMEMFREEZ)
|
---|
| 741 | RT_BZERO(pv, pBlock->cbUnaligned);
|
---|
[83547] | 742 | # ifdef RTALLOC_EFENCE_FREE_FILL
|
---|
[83546] | 743 | else
|
---|
| 744 | memset(pv, RTALLOC_EFENCE_FREE_FILL, pBlock->cbUnaligned);
|
---|
[28273] | 745 | # endif
|
---|
[1] | 746 |
|
---|
[100313] | 747 | size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize();
|
---|
[28273] | 748 | # if defined(RTALLOC_EFENCE_FREE_DELAYED) && RTALLOC_EFENCE_FREE_DELAYED > 0
|
---|
[1] | 749 | /*
|
---|
| 750 | * We're doing delayed freeing.
|
---|
| 751 | * That means we'll expand the E-fence to cover the entire block.
|
---|
| 752 | */
|
---|
[28298] | 753 | int rc = RTMemProtect(pv, pBlock->cbAligned, RTMEM_PROT_NONE);
|
---|
[1] | 754 | if (RT_SUCCESS(rc))
|
---|
| 755 | {
|
---|
| 756 | /*
|
---|
| 757 | * Insert it into the free list and process pending frees.
|
---|
| 758 | */
|
---|
| 759 | rtmemBlockDelayInsert(pBlock);
|
---|
| 760 | while ((pBlock = rtmemBlockDelayRemove()) != NULL)
|
---|
| 761 | {
|
---|
| 762 | pv = pBlock->Core.Key;
|
---|
[28273] | 763 | # ifdef RTALLOC_EFENCE_IN_FRONT
|
---|
[100313] | 764 | void *pvBlock = (char *)pv - cbFence;
|
---|
[28273] | 765 | # else
|
---|
[100313] | 766 | void *pvBlock = (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask());
|
---|
[28273] | 767 | # endif
|
---|
[100313] | 768 | size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence;
|
---|
[1] | 769 | rc = RTMemProtect(pvBlock, cbBlock, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
|
---|
| 770 | if (RT_SUCCESS(rc))
|
---|
[100313] | 771 | RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence);
|
---|
[1] | 772 | else
|
---|
| 773 | rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvBlock, cbBlock, rc);
|
---|
| 774 | rtmemBlockFree(pBlock);
|
---|
| 775 | }
|
---|
| 776 | }
|
---|
| 777 | else
|
---|
| 778 | rtmemComplain(pszOp, "Failed to expand the efence of pv=%p cb=%d, rc=%d.\n", pv, pBlock, rc);
|
---|
| 779 |
|
---|
[28273] | 780 | # else /* !RTALLOC_EFENCE_FREE_DELAYED */
|
---|
[1] | 781 |
|
---|
| 782 | /*
|
---|
| 783 | * Turn of the E-fence and free it.
|
---|
| 784 | */
|
---|
[28273] | 785 | # ifdef RTALLOC_EFENCE_IN_FRONT
|
---|
[100313] | 786 | void *pvBlock = (char *)pv - cbFence;
|
---|
[1] | 787 | void *pvEFence = pvBlock;
|
---|
[28273] | 788 | # else
|
---|
[100313] | 789 | void *pvBlock = (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask());
|
---|
[1] | 790 | void *pvEFence = (char *)pv + pBlock->cb;
|
---|
[28273] | 791 | # endif
|
---|
[100313] | 792 | int rc = RTMemProtect(pvEFence, cbFence, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
|
---|
[1] | 793 | if (RT_SUCCESS(rc))
|
---|
[100313] | 794 | RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence);
|
---|
[1] | 795 | else
|
---|
[100313] | 796 | rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, cbFence, rc);
|
---|
[1] | 797 | rtmemBlockFree(pBlock);
|
---|
| 798 |
|
---|
[28273] | 799 | # endif /* !RTALLOC_EFENCE_FREE_DELAYED */
|
---|
[1] | 800 | }
|
---|
| 801 | else
|
---|
| 802 | rtmemComplain(pszOp, "pv=%p not found! Incorrect free!\n", pv);
|
---|
| 803 |
|
---|
| 804 | #else /* !RTALLOC_EFENCE_TRACE */
|
---|
| 805 |
|
---|
| 806 | /*
|
---|
| 807 | * We have no size tracking, so we're not doing any freeing because
|
---|
| 808 | * we cannot if the E-fence is after the block.
|
---|
| 809 | * Let's just expand the E-fence to the first page of the user bit
|
---|
| 810 | * since we know that it's around.
|
---|
| 811 | */
|
---|
[83547] | 812 | if (enmType == RTMEMTYPE_RTMEMFREEZ)
|
---|
| 813 | RT_BZERO(pv, cbUser);
|
---|
[100313] | 814 | int rc = RTMemProtect((void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), cbPage, RTMEM_PROT_NONE);
|
---|
[1] | 815 | if (RT_FAILURE(rc))
|
---|
[100313] | 816 | rtmemComplain(pszOp, "RTMemProtect(%p, cbPage, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), rc);
|
---|
[1] | 817 | #endif /* !RTALLOC_EFENCE_TRACE */
|
---|
| 818 | }
|
---|
| 819 |
|
---|
[28271] | 820 |
|
---|
[1] | 821 | /**
|
---|
| 822 | * Internal realloc.
|
---|
| 823 | */
|
---|
[31157] | 824 | RTDECL(void *) rtR3MemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew,
|
---|
| 825 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL)
|
---|
[1] | 826 | {
|
---|
| 827 | /*
|
---|
| 828 | * Allocate new and copy.
|
---|
| 829 | */
|
---|
| 830 | if (!pvOld)
|
---|
[31157] | 831 | return rtR3MemAlloc(pszOp, enmType, cbNew, cbNew, pszTag, pvCaller, RT_SRC_POS_ARGS);
|
---|
[1] | 832 | if (!cbNew)
|
---|
| 833 | {
|
---|
[83546] | 834 | rtR3MemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, 0, pvCaller, RT_SRC_POS_ARGS);
|
---|
[1] | 835 | return NULL;
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
| 839 |
|
---|
| 840 | /*
|
---|
| 841 | * Get the block, allocate the new, copy the data, free the old one.
|
---|
| 842 | */
|
---|
| 843 | PRTMEMBLOCK pBlock = rtmemBlockGet(pvOld);
|
---|
| 844 | if (pBlock)
|
---|
| 845 | {
|
---|
[31157] | 846 | void *pvRet = rtR3MemAlloc(pszOp, enmType, cbNew, cbNew, pszTag, pvCaller, RT_SRC_POS_ARGS);
|
---|
[1] | 847 | if (pvRet)
|
---|
| 848 | {
|
---|
[28298] | 849 | memcpy(pvRet, pvOld, RT_MIN(cbNew, pBlock->cbUnaligned));
|
---|
[83546] | 850 | rtR3MemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, 0, pvCaller, RT_SRC_POS_ARGS);
|
---|
[1] | 851 | }
|
---|
| 852 | return pvRet;
|
---|
| 853 | }
|
---|
| 854 | else
|
---|
| 855 | rtmemComplain(pszOp, "pvOld=%p was not found!\n", pvOld);
|
---|
| 856 | return NULL;
|
---|
| 857 |
|
---|
| 858 | #else /* !RTALLOC_EFENCE_TRACE */
|
---|
| 859 |
|
---|
| 860 | rtmemComplain(pszOp, "Not supported if RTALLOC_EFENCE_TRACE isn't defined!\n");
|
---|
| 861 | return NULL;
|
---|
| 862 |
|
---|
| 863 | #endif /* !RTALLOC_EFENCE_TRACE */
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 |
|
---|
| 867 |
|
---|
| 868 |
|
---|
[57432] | 869 | RTDECL(void *) RTMemEfTmpAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 870 | {
|
---|
[31157] | 871 | return rtR3MemAlloc("TmpAlloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 872 | }
|
---|
| 873 |
|
---|
| 874 |
|
---|
[57432] | 875 | RTDECL(void *) RTMemEfTmpAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 876 | {
|
---|
[31157] | 877 | return rtR3MemAlloc("TmpAlloc", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 878 | }
|
---|
| 879 |
|
---|
| 880 |
|
---|
[57432] | 881 | RTDECL(void) RTMemEfTmpFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 882 | {
|
---|
[28298] | 883 | if (pv)
|
---|
[83546] | 884 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, 0, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 885 | }
|
---|
| 886 |
|
---|
| 887 |
|
---|
[83546] | 888 | RTDECL(void) RTMemEfTmpFreeZ(void *pv, size_t cb, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
| 889 | {
|
---|
| 890 | if (pv)
|
---|
| 891 | rtR3MemFree("FreeZ", RTMEMTYPE_RTMEMFREEZ, pv, cb, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 |
|
---|
[57432] | 895 | RTDECL(void *) RTMemEfAlloc(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 896 | {
|
---|
[31157] | 897 | return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 898 | }
|
---|
| 899 |
|
---|
| 900 |
|
---|
[57432] | 901 | RTDECL(void *) RTMemEfAllocZ(size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 902 | {
|
---|
[31157] | 903 | return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 904 | }
|
---|
| 905 |
|
---|
| 906 |
|
---|
[57432] | 907 | RTDECL(void *) RTMemEfAllocVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[28271] | 908 | {
|
---|
| 909 | size_t cbAligned;
|
---|
| 910 | if (cbUnaligned >= 16)
|
---|
| 911 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
| 912 | else
|
---|
| 913 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
[31157] | 914 | return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[28271] | 915 | }
|
---|
| 916 |
|
---|
| 917 |
|
---|
[57432] | 918 | RTDECL(void *) RTMemEfAllocZVar(size_t cbUnaligned, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[28271] | 919 | {
|
---|
| 920 | size_t cbAligned;
|
---|
| 921 | if (cbUnaligned >= 16)
|
---|
| 922 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
| 923 | else
|
---|
| 924 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
[31157] | 925 | return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[28271] | 926 | }
|
---|
| 927 |
|
---|
| 928 |
|
---|
[57432] | 929 | RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 930 | {
|
---|
[31157] | 931 | return rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 932 | }
|
---|
| 933 |
|
---|
| 934 |
|
---|
[83368] | 935 | RTDECL(void *) RTMemEfReallocZ(void *pvOld, size_t cbOld, size_t cbNew, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
| 936 | {
|
---|
| 937 | void *pvDst = rtR3MemRealloc("ReallocZ", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
| 938 | if (pvDst && cbNew > cbOld)
|
---|
| 939 | memset((uint8_t *)pvDst + cbOld, 0, cbNew - cbOld);
|
---|
| 940 | return pvDst;
|
---|
| 941 | }
|
---|
| 942 |
|
---|
| 943 |
|
---|
[57432] | 944 | RTDECL(void) RTMemEfFree(void *pv, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 945 | {
|
---|
| 946 | if (pv)
|
---|
[83546] | 947 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, 0, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
[1] | 948 | }
|
---|
| 949 |
|
---|
| 950 |
|
---|
[83546] | 951 | RTDECL(void) RTMemEfFreeZ(void *pv, size_t cb, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
| 952 | {
|
---|
| 953 | if (pv)
|
---|
| 954 | rtR3MemFree("FreeZ", RTMEMTYPE_RTMEMFREEZ, pv, cb, ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 |
|
---|
[57432] | 958 | RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[1] | 959 | {
|
---|
[31157] | 960 | void *pvDst = RTMemEfAlloc(cb, pszTag, RT_SRC_POS_ARGS);
|
---|
[1] | 961 | if (pvDst)
|
---|
| 962 | memcpy(pvDst, pvSrc, cb);
|
---|
| 963 | return pvDst;
|
---|
| 964 | }
|
---|
| 965 |
|
---|
| 966 |
|
---|
[57432] | 967 | RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag, RT_SRC_POS_DECL) RT_NO_THROW_DEF
|
---|
[28298] | 968 | {
|
---|
[31157] | 969 | void *pvDst = RTMemEfAlloc(cbSrc + cbExtra, pszTag, RT_SRC_POS_ARGS);
|
---|
[28298] | 970 | if (pvDst)
|
---|
| 971 | {
|
---|
| 972 | memcpy(pvDst, pvSrc, cbSrc);
|
---|
| 973 | memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
|
---|
| 974 | }
|
---|
| 975 | return pvDst;
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 |
|
---|
| 979 |
|
---|
| 980 |
|
---|
| 981 | /*
|
---|
[1] | 982 | *
|
---|
[28298] | 983 | * The NP (no position) versions.
|
---|
| 984 | *
|
---|
[1] | 985 | */
|
---|
[28298] | 986 |
|
---|
| 987 |
|
---|
| 988 |
|
---|
[57432] | 989 | RTDECL(void *) RTMemEfTmpAllocNP(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[1] | 990 | {
|
---|
[31157] | 991 | return rtR3MemAlloc("TmpAlloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 992 | }
|
---|
| 993 |
|
---|
| 994 |
|
---|
[57432] | 995 | RTDECL(void *) RTMemEfTmpAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 996 | {
|
---|
[31157] | 997 | return rtR3MemAlloc("TmpAllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 998 | }
|
---|
| 999 |
|
---|
| 1000 |
|
---|
[57432] | 1001 | RTDECL(void) RTMemEfTmpFreeNP(void *pv) RT_NO_THROW_DEF
|
---|
[28298] | 1002 | {
|
---|
| 1003 | if (pv)
|
---|
[83546] | 1004 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1005 | }
|
---|
| 1006 |
|
---|
| 1007 |
|
---|
[83546] | 1008 | RTDECL(void) RTMemEfTmpFreeZNP(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
| 1009 | {
|
---|
| 1010 | if (pv)
|
---|
| 1011 | rtR3MemFree("FreeZ", RTMEMTYPE_RTMEMFREEZ, pv, cb, ASMReturnAddress(), NULL, 0, NULL);
|
---|
| 1012 | }
|
---|
| 1013 |
|
---|
| 1014 |
|
---|
[57432] | 1015 | RTDECL(void *) RTMemEfAllocNP(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1016 | {
|
---|
[31157] | 1017 | return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1018 | }
|
---|
| 1019 |
|
---|
| 1020 |
|
---|
[57432] | 1021 | RTDECL(void *) RTMemEfAllocZNP(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1022 | {
|
---|
[31157] | 1023 | return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1024 | }
|
---|
| 1025 |
|
---|
| 1026 |
|
---|
[57432] | 1027 | RTDECL(void *) RTMemEfAllocVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1028 | {
|
---|
| 1029 | size_t cbAligned;
|
---|
| 1030 | if (cbUnaligned >= 16)
|
---|
| 1031 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
| 1032 | else
|
---|
| 1033 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
[31157] | 1034 | return rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1035 | }
|
---|
| 1036 |
|
---|
| 1037 |
|
---|
[57432] | 1038 | RTDECL(void *) RTMemEfAllocZVarNP(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1039 | {
|
---|
| 1040 | size_t cbAligned;
|
---|
| 1041 | if (cbUnaligned >= 16)
|
---|
| 1042 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
| 1043 | else
|
---|
| 1044 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
[31157] | 1045 | return rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1046 | }
|
---|
| 1047 |
|
---|
| 1048 |
|
---|
[57432] | 1049 | RTDECL(void *) RTMemEfReallocNP(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1050 | {
|
---|
[31157] | 1051 | return rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1052 | }
|
---|
| 1053 |
|
---|
| 1054 |
|
---|
[83368] | 1055 | RTDECL(void *) RTMemEfReallocZNP(void *pvOld, size_t cbOld, size_t cbNew, const char *pszTag) RT_NO_THROW_DEF
|
---|
| 1056 | {
|
---|
| 1057 | void *pvDst = rtR3MemRealloc("ReallocZ", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
| 1058 | if (pvDst && cbNew > cbOld)
|
---|
| 1059 | memset((uint8_t *)pvDst + cbOld, 0, cbNew - cbOld);
|
---|
| 1060 | return pvDst;
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
| 1063 |
|
---|
[57432] | 1064 | RTDECL(void) RTMemEfFreeNP(void *pv) RT_NO_THROW_DEF
|
---|
[28298] | 1065 | {
|
---|
| 1066 | if (pv)
|
---|
[83546] | 1067 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
[28298] | 1068 | }
|
---|
| 1069 |
|
---|
| 1070 |
|
---|
[83546] | 1071 | RTDECL(void) RTMemEfFreeZNP(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
| 1072 | {
|
---|
| 1073 | if (pv)
|
---|
| 1074 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREEZ, pv, cb, ASMReturnAddress(), NULL, 0, NULL);
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 |
|
---|
[57432] | 1078 | RTDECL(void *) RTMemEfDupNP(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1079 | {
|
---|
[31157] | 1080 | void *pvDst = RTMemEfAlloc(cb, pszTag, NULL, 0, NULL);
|
---|
[1] | 1081 | if (pvDst)
|
---|
[28298] | 1082 | memcpy(pvDst, pvSrc, cb);
|
---|
| 1083 | return pvDst;
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 |
|
---|
[57432] | 1087 | RTDECL(void *) RTMemEfDupExNP(const void *pvSrc, size_t cbSrc, size_t cbExtra, const char *pszTag) RT_NO_THROW_DEF
|
---|
[28298] | 1088 | {
|
---|
[31157] | 1089 | void *pvDst = RTMemEfAlloc(cbSrc + cbExtra, pszTag, NULL, 0, NULL);
|
---|
[28298] | 1090 | if (pvDst)
|
---|
[1] | 1091 | {
|
---|
| 1092 | memcpy(pvDst, pvSrc, cbSrc);
|
---|
| 1093 | memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
|
---|
| 1094 | }
|
---|
| 1095 | return pvDst;
|
---|
| 1096 | }
|
---|
| 1097 |
|
---|