VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/alloc-ef.cpp@ 26522

Last change on this file since 26522 was 26258, checked in by vboxsync, 15 years ago

two more

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.4 KB
Line 
1/* $Id: alloc-ef.cpp 26258 2010-02-05 01:30:40Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, electric fence.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "alloc-ef.h"
36#include <iprt/log.h>
37#include <iprt/asm.h>
38#include <iprt/thread.h>
39#include <VBox/sup.h>
40#include <iprt/err.h>
41#include <errno.h>
42#include <stdio.h>
43#include <stdlib.h>
44
45#include <iprt/alloc.h>
46#include <iprt/assert.h>
47#include <iprt/param.h>
48#include <iprt/string.h>
49
50
51/*******************************************************************************
52* Global Variables *
53*******************************************************************************/
54#ifdef RTALLOC_EFENCE_TRACE
55/** Spinlock protecting the allthe blocks globals. */
56static volatile uint32_t g_BlocksLock;
57/** Tree tracking the allocations. */
58static AVLPVTREE g_BlocksTree;
59#ifdef RTALLOC_EFENCE_FREE_DELAYED
60/** Tail of the delayed blocks. */
61static volatile PRTMEMBLOCK g_pBlocksDelayHead;
62/** Tail of the delayed blocks. */
63static volatile PRTMEMBLOCK g_pBlocksDelayTail;
64/** Number of bytes in the delay list (includes fences). */
65static volatile size_t g_cbBlocksDelay;
66#endif
67#endif
68/** Array of pointers free watches for. */
69void *gapvRTMemFreeWatch[4] = {NULL, NULL, NULL, NULL};
70/** Enable logging of all freed memory. */
71bool gfRTMemFreeLog = false;
72
73
74/*******************************************************************************
75* Internal Functions *
76*******************************************************************************/
77/**
78 * Complains about something.
79 */
80static void rtmemComplain(const char *pszOp, const char *pszFormat, ...)
81{
82 va_list args;
83 fprintf(stderr, "RTMem error: %s: ", pszOp);
84 va_start(args, pszFormat);
85 vfprintf(stderr, pszFormat, args);
86 va_end(args);
87 RTAssertDoPanic();
88}
89
90/**
91 * Log an event.
92 */
93static inline void rtmemLog(const char *pszOp, const char *pszFormat, ...)
94{
95#if 0
96 va_list args;
97 fprintf(stderr, "RTMem info: %s: ", pszOp);
98 va_start(args, pszFormat);
99 vfprintf(stderr, pszFormat, args);
100 va_end(args);
101#endif
102}
103
104
105#ifdef RTALLOC_EFENCE_TRACE
106
107/**
108 * Aquires the lock.
109 */
110static inline void rtmemBlockLock(void)
111{
112 unsigned c = 0;
113 while (!ASMAtomicCmpXchgU32(&g_BlocksLock, 1, 0))
114 RTThreadSleep(((++c) >> 2) & 31);
115}
116
117
118/**
119 * Releases the lock.
120 */
121static inline void rtmemBlockUnlock(void)
122{
123 Assert(g_BlocksLock == 1);
124 ASMAtomicXchgU32(&g_BlocksLock, 0);
125}
126
127
128/**
129 * Creates a block.
130 */
131static inline PRTMEMBLOCK rtmemBlockCreate(RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
132{
133 PRTMEMBLOCK pBlock = (PRTMEMBLOCK)malloc(sizeof(*pBlock));
134 if (pBlock)
135 {
136 pBlock->enmType = enmType;
137 pBlock->cb = cb;
138 pBlock->pvCaller = pvCaller;
139 pBlock->iLine = iLine;
140 pBlock->pszFile = pszFile;
141 pBlock->pszFunction = pszFunction;
142 }
143 return pBlock;
144}
145
146
147/**
148 * Frees a block.
149 */
150static inline void rtmemBlockFree(PRTMEMBLOCK pBlock)
151{
152 free(pBlock);
153}
154
155
156/**
157 * Insert a block from the tree.
158 */
159static inline void rtmemBlockInsert(PRTMEMBLOCK pBlock, void *pv)
160{
161 pBlock->Core.Key = pv;
162 rtmemBlockLock();
163 bool fRc = RTAvlPVInsert(&g_BlocksTree, &pBlock->Core);
164 rtmemBlockUnlock();
165 AssertRelease(fRc);
166}
167
168
169/**
170 * Remove a block from the tree and returns it to the caller.
171 */
172static inline PRTMEMBLOCK rtmemBlockRemove(void *pv)
173{
174 rtmemBlockLock();
175 PRTMEMBLOCK pBlock = (PRTMEMBLOCK)RTAvlPVRemove(&g_BlocksTree, pv);
176 rtmemBlockUnlock();
177 return pBlock;
178}
179
180/**
181 * Gets a block.
182 */
183static inline PRTMEMBLOCK rtmemBlockGet(void *pv)
184{
185 rtmemBlockLock();
186 PRTMEMBLOCK pBlock = (PRTMEMBLOCK)RTAvlPVGet(&g_BlocksTree, pv);
187 rtmemBlockUnlock();
188 return pBlock;
189}
190
191/**
192 * Dumps one allocation.
193 */
194static DECLCALLBACK(int) RTMemDumpOne(PAVLPVNODECORE pNode, void *pvUser)
195{
196 PRTMEMBLOCK pBlock = (PRTMEMBLOCK)pNode;
197 fprintf(stderr, "%p %08lx %p\n",
198 pBlock->Core.Key,
199 (long)pBlock->cb,
200 pBlock->pvCaller);
201 return 0;
202}
203
204/**
205 * Dumps the allocated blocks.
206 * This is something which you should call from gdb.
207 */
208extern "C" void RTMemDump(void);
209void RTMemDump(void)
210{
211 fprintf(stderr, "address size caller\n");
212 RTAvlPVDoWithAll(&g_BlocksTree, true, RTMemDumpOne, NULL);
213}
214
215
216#ifdef RTALLOC_EFENCE_FREE_DELAYED
217/**
218 * Insert a delayed block.
219 */
220static inline void rtmemBlockDelayInsert(PRTMEMBLOCK pBlock)
221{
222 size_t cbBlock = RT_ALIGN_Z(pBlock->cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
223 pBlock->Core.pRight = NULL;
224 pBlock->Core.pLeft = NULL;
225 rtmemBlockLock();
226 if (g_pBlocksDelayHead)
227 {
228 g_pBlocksDelayHead->Core.pLeft = (PAVLPVNODECORE)pBlock;
229 pBlock->Core.pRight = (PAVLPVNODECORE)g_pBlocksDelayHead;
230 g_pBlocksDelayHead = pBlock;
231 }
232 else
233 {
234 g_pBlocksDelayTail = pBlock;
235 g_pBlocksDelayHead = pBlock;
236 }
237 g_cbBlocksDelay += cbBlock;
238 rtmemBlockUnlock();
239}
240
241/**
242 * Removes a delayed block.
243 */
244static inline PRTMEMBLOCK rtmemBlockDelayRemove(void)
245{
246 PRTMEMBLOCK pBlock = NULL;
247 rtmemBlockLock();
248 if (g_cbBlocksDelay > RTALLOC_EFENCE_FREE_DELAYED)
249 {
250 pBlock = g_pBlocksDelayTail;
251 if (pBlock)
252 {
253 g_pBlocksDelayTail = (PRTMEMBLOCK)pBlock->Core.pLeft;
254 if (pBlock->Core.pLeft)
255 pBlock->Core.pLeft->pRight = NULL;
256 else
257 g_pBlocksDelayHead = NULL;
258 g_cbBlocksDelay -= RT_ALIGN_Z(pBlock->cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
259 }
260 }
261 rtmemBlockUnlock();
262 return pBlock;
263}
264
265
266#endif /* DELAY */
267
268#endif /* RTALLOC_EFENCE_TRACE */
269
270
271/**
272 * Internal allocator.
273 */
274void *rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
275{
276 /*
277 * Sanity.
278 */
279 if ( RT_ALIGN_Z(RTALLOC_EFENCE_SIZE, PAGE_SIZE) != RTALLOC_EFENCE_SIZE
280 && RTALLOC_EFENCE_SIZE <= 0)
281 {
282 rtmemComplain(pszOp, "Invalid E-fence size! %#x\n", RTALLOC_EFENCE_SIZE);
283 return NULL;
284 }
285 if (!cb)
286 {
287#if 0
288 rtmemComplain(pszOp, "Request of ZERO bytes allocation!\n");
289 return NULL;
290#else
291 cb = 1;
292#endif
293 }
294
295#ifdef RTALLOC_EFENCE_TRACE
296 /*
297 * Allocate the trace block.
298 */
299 PRTMEMBLOCK pBlock = rtmemBlockCreate(enmType, cb, pvCaller, iLine, pszFile, pszFunction);
300 if (!pBlock)
301 {
302 rtmemComplain(pszOp, "Failed to allocate trace block!\n");
303 return NULL;
304 }
305#endif
306
307 /*
308 * Allocate a block with page alignment space + the size of the E-fence.
309 */
310 size_t cbBlock = RT_ALIGN_Z(cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
311 void *pvBlock = RTMemPageAlloc(cbBlock);
312 if (pvBlock)
313 {
314 /*
315 * Calc the start of the fence and the user block
316 * and then change the page protection of the fence.
317 */
318 #ifdef RTALLOC_EFENCE_IN_FRONT
319 void *pvEFence = pvBlock;
320 void *pv = (char *)pvEFence + RTALLOC_EFENCE_SIZE;
321 #else
322 void *pvEFence = (char *)pvBlock + (cbBlock - RTALLOC_EFENCE_SIZE);
323 void *pv = (char *)pvEFence - cb;
324 #endif
325 int rc = RTMemProtect(pvEFence, RTALLOC_EFENCE_SIZE, RTMEM_PROT_NONE);
326 if (!rc)
327 {
328 #ifdef RTALLOC_EFENCE_TRACE
329 rtmemBlockInsert(pBlock, pv);
330 #endif
331 if (enmType == RTMEMTYPE_RTMEMALLOCZ)
332 memset(pv, 0, cb);
333#ifdef RTALLOC_EFENCE_FILLER
334 else
335 memset(pv, RTALLOC_EFENCE_FILLER, cb);
336#endif
337
338 rtmemLog(pszOp, "returns %p (pvBlock=%p cbBlock=%#x pvEFence=%p cb=%#x)\n", pv, pvBlock, cbBlock, pvEFence, cb);
339 return pv;
340 }
341 rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);
342 RTMemPageFree(pvBlock);
343 }
344 else
345 rtmemComplain(pszOp, "Failed to allocated %d bytes.\n", cb);
346
347#ifdef RTALLOC_EFENCE_TRACE
348 rtmemBlockFree(pBlock);
349#endif
350 return NULL;
351}
352
353
354/**
355 * Internal free.
356 */
357void rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
358{
359 /*
360 * Simple case.
361 */
362 if (!pv)
363 return;
364
365 /*
366 * Check watch points.
367 */
368 for (unsigned i = 0; i < RT_ELEMENTS(gapvRTMemFreeWatch); i++)
369 if (gapvRTMemFreeWatch[i] == pv)
370 RTAssertDoPanic();
371
372#ifdef RTALLOC_EFENCE_TRACE
373 /*
374 * Find the block.
375 */
376 PRTMEMBLOCK pBlock = rtmemBlockRemove(pv);
377 if (pBlock)
378 {
379 if (gfRTMemFreeLog)
380 RTLogPrintf("RTMem %s: pv=%p pvCaller=%p cb=%#x\n", pszOp, pv, pvCaller, pBlock->cb);
381
382 #ifdef RTALLOC_EFENCE_FREE_FILL
383 /*
384 * Fill the user part of the block.
385 */
386 memset(pv, RTALLOC_EFENCE_FREE_FILL, pBlock->cb);
387 #endif
388
389 #if defined(RTALLOC_EFENCE_FREE_DELAYED) && RTALLOC_EFENCE_FREE_DELAYED > 0
390 /*
391 * We're doing delayed freeing.
392 * That means we'll expand the E-fence to cover the entire block.
393 */
394 int rc = RTMemProtect(pv, pBlock->cb, RTMEM_PROT_NONE);
395 if (RT_SUCCESS(rc))
396 {
397 /*
398 * Insert it into the free list and process pending frees.
399 */
400 rtmemBlockDelayInsert(pBlock);
401 while ((pBlock = rtmemBlockDelayRemove()) != NULL)
402 {
403 pv = pBlock->Core.Key;
404 #ifdef RTALLOC_EFENCE_IN_FRONT
405 void *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;
406 #else
407 void *pvBlock = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
408 #endif
409 size_t cbBlock = RT_ALIGN_Z(pBlock->cb, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;
410 rc = RTMemProtect(pvBlock, cbBlock, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
411 if (RT_SUCCESS(rc))
412 RTMemPageFree(pvBlock);
413 else
414 rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvBlock, cbBlock, rc);
415 rtmemBlockFree(pBlock);
416 }
417 }
418 else
419 rtmemComplain(pszOp, "Failed to expand the efence of pv=%p cb=%d, rc=%d.\n", pv, pBlock, rc);
420
421 #else /* !RTALLOC_EFENCE_FREE_DELAYED */
422
423 /*
424 * Turn of the E-fence and free it.
425 */
426 #ifdef RTALLOC_EFENCE_IN_FRONT
427 void *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;
428 void *pvEFence = pvBlock;
429 #else
430 void *pvBlock = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
431 void *pvEFence = (char *)pv + pBlock->cb;
432 #endif
433 int rc = RTMemProtect(pvEFence, RTALLOC_EFENCE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
434 if (RT_SUCCESS(rc))
435 RTMemPageFree(pvBlock);
436 else
437 rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);
438 rtmemBlockFree(pBlock);
439
440 #endif /* !RTALLOC_EFENCE_FREE_DELAYED */
441 }
442 else
443 rtmemComplain(pszOp, "pv=%p not found! Incorrect free!\n", pv);
444
445#else /* !RTALLOC_EFENCE_TRACE */
446
447 /*
448 * We have no size tracking, so we're not doing any freeing because
449 * we cannot if the E-fence is after the block.
450 * Let's just expand the E-fence to the first page of the user bit
451 * since we know that it's around.
452 */
453 int rc = RTMemProtect((void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK), PAGE_SIZE, RTMEM_PROT_NONE);
454 if (RT_FAILURE(rc))
455 rtmemComplain(pszOp, "RTMemProtect(%p, PAGE_SIZE, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK), rc);
456#endif /* !RTALLOC_EFENCE_TRACE */
457}
458
459/**
460 * Internal realloc.
461 */
462void *rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
463{
464 /*
465 * Allocate new and copy.
466 */
467 if (!pvOld)
468 return rtMemAlloc(pszOp, enmType, cbNew, pvCaller, iLine, pszFile, pszFunction);
469 if (!cbNew)
470 {
471 rtMemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, iLine, pszFile, pszFunction);
472 return NULL;
473 }
474
475#ifdef RTALLOC_EFENCE_TRACE
476
477 /*
478 * Get the block, allocate the new, copy the data, free the old one.
479 */
480 PRTMEMBLOCK pBlock = rtmemBlockGet(pvOld);
481 if (pBlock)
482 {
483 void *pvRet = rtMemAlloc(pszOp, enmType, cbNew, pvCaller, iLine, pszFile, pszFunction);
484 if (pvRet)
485 {
486 memcpy(pvRet, pvOld, RT_MIN(cbNew, pBlock->cb));
487 rtMemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, iLine, pszFile, pszFunction);
488 }
489 return pvRet;
490 }
491 else
492 rtmemComplain(pszOp, "pvOld=%p was not found!\n", pvOld);
493 return NULL;
494
495#else /* !RTALLOC_EFENCE_TRACE */
496
497 rtmemComplain(pszOp, "Not supported if RTALLOC_EFENCE_TRACE isn't defined!\n");
498 return NULL;
499
500#endif /* !RTALLOC_EFENCE_TRACE */
501}
502
503
504
505
506/**
507 * Same as RTMemTmpAlloc() except that it's fenced.
508 *
509 * @returns Pointer to the allocated memory.
510 * @returns NULL on failure.
511 * @param cb Size in bytes of the memory block to allocate.
512 */
513RTDECL(void *) RTMemEfTmpAlloc(size_t cb) RT_NO_THROW
514{
515 return RTMemEfAlloc(cb);
516}
517
518
519/**
520 * Same as RTMemTmpAllocZ() except that it's fenced.
521 *
522 * @returns Pointer to the allocated memory.
523 * @returns NULL on failure.
524 * @param cb Size in bytes of the memory block to allocate.
525 */
526RTDECL(void *) RTMemEfTmpAllocZ(size_t cb) RT_NO_THROW
527{
528 return RTMemEfAllocZ(cb);
529}
530
531
532/**
533 * Same as RTMemTmpFree() except that it's for fenced memory.
534 *
535 * @param pv Pointer to memory block.
536 */
537RTDECL(void) RTMemEfTmpFree(void *pv) RT_NO_THROW
538{
539 RTMemEfFree(pv);
540}
541
542
543/**
544 * Same as RTMemAlloc() except that it's fenced.
545 *
546 * @returns Pointer to the allocated memory. Free with RTMemEfFree().
547 * @returns NULL on failure.
548 * @param cb Size in bytes of the memory block to allocate.
549 */
550RTDECL(void *) RTMemEfAlloc(size_t cb) RT_NO_THROW
551{
552 return rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, ((void **)&cb)[-1], 0, NULL, NULL);
553}
554
555
556/**
557 * Same as RTMemAllocZ() except that it's fenced.
558 *
559 * @returns Pointer to the allocated memory.
560 * @returns NULL on failure.
561 * @param cb Size in bytes of the memory block to allocate.
562 */
563RTDECL(void *) RTMemEfAllocZ(size_t cb) RT_NO_THROW
564{
565 return rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, ((void **)&cb)[-1], 0, NULL, NULL);
566}
567
568
569/**
570 * Same as RTMemRealloc() except that it's fenced.
571 *
572 * @returns Pointer to the allocated memory.
573 * @returns NULL on failure.
574 * @param pvOld The memory block to reallocate.
575 * @param cbNew The new block size (in bytes).
576 */
577RTDECL(void *) RTMemEfRealloc(void *pvOld, size_t cbNew) RT_NO_THROW
578{
579 return rtMemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ((void **)&pvOld)[-1], 0, NULL, NULL);
580}
581
582
583/**
584 * Free memory allocated by any of the RTMemEf* allocators.
585 *
586 * @param pv Pointer to memory block.
587 */
588RTDECL(void) RTMemEfFree(void *pv) RT_NO_THROW
589{
590 if (pv)
591 rtMemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
592}
593
594
595/**
596 * Same as RTMemDup() except that it's fenced.
597 *
598 * @returns New heap block with the duplicate data.
599 * @returns NULL if we're out of memory.
600 * @param pvSrc The memory to duplicate.
601 * @param cb The amount of memory to duplicate.
602 */
603RTDECL(void *) RTMemEfDup(const void *pvSrc, size_t cb) RT_NO_THROW
604{
605 void *pvDst = RTMemEfAlloc(cb);
606 if (pvDst)
607 memcpy(pvDst, pvSrc, cb);
608 return pvDst;
609}
610
611
612/**
613 * Same as RTMemDupEx except that it's fenced.
614 *
615 * @returns New heap block with the duplicate data.
616 * @returns NULL if we're out of memory.
617 * @param pvSrc The memory to duplicate.
618 * @param cbSrc The amount of memory to duplicate.
619 * @param cbExtra The amount of extra memory to allocate and zero.
620 */
621RTDECL(void *) RTMemEfDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW
622{
623 void *pvDst = RTMemEfAlloc(cbSrc + cbExtra);
624 if (pvDst)
625 {
626 memcpy(pvDst, pvSrc, cbSrc);
627 memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
628 }
629 return pvDst;
630}
631
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette