VirtualBox

Changeset 7632 in vbox for trunk/src


Ignore:
Timestamp:
Mar 28, 2008 5:05:00 PM (17 years ago)
Author:
vboxsync
Message:

Added optional fences to the hyper heap.

Location:
trunk/src/VBox/VMM
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/MMInternal.h

    r6796 r7632  
    145145#if defined(VBOX_STRICT) || defined(__DOXYGEN__)
    146146# define MMHYPER_HEAP_FREE_POISON   0xCB
     147#endif
     148
     149/** @def MMHYPER_HEAP_STRICT
     150 * Enables a bunch of assertions in the heap code. */
     151#if defined(VBOX_STRICT) || defined(__DOXYGEN__)
     152# define MMHYPER_HEAP_STRICT 1
     153# if 0 || defined(__DOXYGEN__)
     154/** @def MMHYPER_HEAP_STRICT_FENCE
     155 * Enables tail fence. */
     156#  define MMHYPER_HEAP_STRICT_FENCE
     157/** @def MMHYPER_HEAP_STRICT_FENCE_SIZE
     158 * The fence size in bytes. */
     159#  define MMHYPER_HEAP_STRICT_FENCE_SIZE    256
     160/** @def MMHYPER_HEAP_STRICT_FENCE_U32
     161 * The fence filler. */
     162#  define MMHYPER_HEAP_STRICT_FENCE_U32     0xdeadbeef
     163# endif
    147164#endif
    148165
  • trunk/src/VBox/VMM/VMMAll/MMAllHyper.cpp

    r6529 r7632  
    3737*   Defined Constants And Macros                                               *
    3838*******************************************************************************/
    39 #ifdef DEBUG
    40 # define MMHYPER_HEAP_STRICT 1
    41 #endif
    42 
    4339#define ASSERT_L(u1, u2)    AssertMsg((u1) <  (u2), ("u1=%#x u2=%#x\n", u1, u2))
    4440#define ASSERT_LE(u1, u2)   AssertMsg((u1) <= (u2), ("u1=%#x u2=%#x\n", u1, u2))
     
    149145static int mmHyperFree(PMMHYPERHEAP pHeap, PMMHYPERCHUNK pChunk);
    150146#ifdef MMHYPER_HEAP_STRICT
    151 static void mmR3HyperHeapCheck(PMMHYPERHEAP pHeap);
     147static void mmHyperHeapCheck(PMMHYPERHEAP pHeap);
    152148#endif
    153149
     
    299295    Log3(("mmHyperAllocChunk: Enter cb=%#x uAlignment=%#x\n", cb, uAlignment));
    300296#ifdef MMHYPER_HEAP_STRICT
    301     mmR3HyperHeapCheck(pHeap);
     297    mmHyperHeapCheck(pHeap);
     298#endif
     299#ifdef MMHYPER_HEAP_STRICT_FENCE
     300    uint32_t cbFence = RT_MAX(MMHYPER_HEAP_STRICT_FENCE_SIZE, uAlignment);
     301    cb += cbFence;
    302302#endif
    303303
     
    509509    }
    510510
     511#ifdef MMHYPER_HEAP_STRICT_FENCE
     512    uint32_t *pu32End = (uint32_t *)((uint8_t *)(pRet + 1) + cb);
     513    uint32_t *pu32EndReal = pRet->offNext
     514                          ? (uint32_t *)((uint8_t *)pRet + pRet->offNext)
     515                          : (uint32_t *)(pHeap->CTXSUFF(pbHeap) + pHeap->cbHeap);
     516    cbFence += (uintptr_t)pu32EndReal - (uintptr_t)pu32End; Assert(!(cbFence & 0x3));
     517    ASMMemFill32((uint8_t *)pu32EndReal - cbFence, cbFence, MMHYPER_HEAP_STRICT_FENCE_U32);
     518    pu32EndReal[-1] = cbFence;
     519#endif
    511520#ifdef MMHYPER_HEAP_STRICT
    512     mmR3HyperHeapCheck(pHeap);
     521    mmHyperHeapCheck(pHeap);
    513522#endif
    514523    return pRet;
     
    531540
    532541#ifdef MMHYPER_HEAP_STRICT
    533     mmR3HyperHeapCheck(pHeap);
     542    mmHyperHeapCheck(pHeap);
    534543#endif
    535544
     
    615624
    616625#ifdef MMHYPER_HEAP_STRICT
    617     mmR3HyperHeapCheck(pHeap);
     626    mmHyperHeapCheck(pHeap);
    618627#endif
    619628    return pvRet;
     
    776785                    VERR_INVALID_POINTER);
    777786
     787#ifdef MMHYPER_HEAP_STRICT
     788    mmHyperHeapCheck(pHeap);
     789#endif
     790
    778791#if defined(VBOX_WITH_STATISTICS) || defined(MMHYPER_HEAP_FREE_POISON)
    779792    /* calc block size. */
     
    857870    Log3(("mmHyperFree: Enter pHeap=%p pChunk=%p\n", pHeap, pChunk));
    858871    PMMHYPERCHUNKFREE   pFree = (PMMHYPERCHUNKFREE)pChunk;
    859 
    860 #ifdef MMHYPER_HEAP_STRICT
    861     mmR3HyperHeapCheck(pHeap);
    862 #endif
    863872
    864873    /*
     
    10091018
    10101019#ifdef MMHYPER_HEAP_STRICT
    1011     mmR3HyperHeapCheck(pHeap);
     1020    mmHyperHeapCheck(pHeap);
    10121021#endif
    10131022    return VINF_SUCCESS;
     
    10151024
    10161025
     1026#if defined(DEBUG) || defined(MMHYPER_HEAP_STRICT)
     1027/**
     1028 * Dumps a heap chunk to the log.
     1029 *
     1030 * @param   pHeap       Pointer to the heap.
     1031 * @param   pCur        Pointer to the chunk.
     1032 */
     1033static void mmHyperHeapDumpOne(PMMHYPERHEAP pHeap, PMMHYPERCHUNKFREE pCur)
     1034{
     1035    if (MMHYPERCHUNK_ISUSED(&pCur->core))
     1036    {
     1037        if (pCur->core.offStat)
     1038        {
     1039            PMMHYPERSTAT pStat = (PMMHYPERSTAT)((uintptr_t)pCur + pCur->core.offStat);
     1040            const char *pszSelf = pCur->core.offStat == sizeof(MMHYPERCHUNK) ? " stat record" : "";
     1041#ifdef IN_RING3
     1042            Log(("%p  %06x USED offNext=%06x offPrev=-%06x %s%s\n",
     1043                 pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
     1044                 pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core),
     1045                 mmR3GetTagName((MMTAG)pStat->Core.Key), pszSelf));
     1046#else
     1047            Log(("%p  %06x USED offNext=%06x offPrev=-%06x %d%s\n",
     1048                 pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
     1049                 pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core),
     1050                 (MMTAG)pStat->Core.Key, pszSelf));
     1051#endif
     1052        }
     1053        else
     1054            Log(("%p  %06x USED offNext=%06x offPrev=-%06x\n",
     1055                 pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
     1056                 pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core)));
     1057    }
     1058    else
     1059        Log(("%p  %06x FREE offNext=%06x offPrev=-%06x : cb=%06x offNext=%06x offPrev=-%06x\n",
     1060             pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
     1061             pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core), pCur->cb, pCur->offNext, pCur->offPrev));
     1062}
     1063#endif /* DEBUG || MMHYPER_HEAP_STRICT */
     1064
     1065
    10171066#ifdef MMHYPER_HEAP_STRICT
    10181067/**
    10191068 * Internal consitency check.
    10201069 */
    1021 static void mmR3HyperHeapCheck(PMMHYPERHEAP pHeap)
     1070static void mmHyperHeapCheck(PMMHYPERHEAP pHeap)
    10221071{
    10231072    PMMHYPERCHUNKFREE pPrev = NULL;
     
    10331082                      ("pPrev->core.offNext=%d offPrev=%d\n", pPrev->core.offNext, MMHYPERCHUNK_GET_OFFPREV(&pCur->core)));
    10341083
     1084# ifdef MMHYPER_HEAP_STRICT_FENCE
     1085        uint32_t off = (uint8_t *)pCur - CTXSUFF(pHeap->pbHeap);
     1086        if (    MMHYPERCHUNK_ISUSED(&pCur->core)
     1087            &&  off < pHeap->offPageAligned)
     1088        {
     1089            uint32_t cbCur = pCur->core.offNext
     1090                           ? pCur->core.offNext
     1091                           : pHeap->cbHeap - off;
     1092            uint32_t *pu32End = ((uint32_t *)((uint8_t *)pCur + cbCur));
     1093            uint32_t cbFence = pu32End[-1];
     1094            if (RT_UNLIKELY(    cbFence >= cbCur - sizeof(*pCur)
     1095                            ||  cbFence < MMHYPER_HEAP_STRICT_FENCE_SIZE))
     1096            {
     1097                mmHyperHeapDumpOne(pHeap, pCur);
     1098                Assert(cbFence < cbCur - sizeof(*pCur));
     1099                Assert(cbFence >= MMHYPER_HEAP_STRICT_FENCE_SIZE);
     1100            }
     1101
     1102            uint32_t *pu32Bad = ASMMemIsAllU32((uint8_t *)pu32End - cbFence, cbFence - sizeof(uint32_t), MMHYPER_HEAP_STRICT_FENCE_U32);
     1103            if (RT_UNLIKELY(pu32Bad))
     1104            {
     1105                mmHyperHeapDumpOne(pHeap, pCur);
     1106                Assert(!pu32Bad);
     1107            }
     1108        }
     1109# endif
     1110
    10351111        /* next */
    10361112        if (!pCur->core.offNext)
     
    10431119
    10441120
     1121/**
     1122 * Performs consistency checks on the heap if MMHYPER_HEAP_STRICT was
     1123 * defined at build time.
     1124 *
     1125 * @param   pVM         Pointer to the shared VM structure.
     1126 */
     1127MMDECL(void) MMHyperHeapCheck(PVM pVM)
     1128{
     1129#ifdef MMHYPER_HEAP_STRICT
     1130    mmHyperHeapCheck(CTXSUFF(pVM->mm.s.pHyperHeap));
     1131#endif
     1132}
     1133
     1134
    10451135#ifdef DEBUG
    10461136/**
     
    10551145    for (;;)
    10561146    {
    1057         if (MMHYPERCHUNK_ISUSED(&pCur->core))
    1058         {
    1059             if (pCur->core.offStat)
    1060             {
    1061                 PMMHYPERSTAT pStat = (PMMHYPERSTAT)((uintptr_t)pCur + pCur->core.offStat);
    1062                 const char *pszSelf = pCur->core.offStat == sizeof(MMHYPERCHUNK) ? " stat record" : "";
    1063 #ifdef IN_RING3
    1064                 Log(("%p  %06x USED offNext=%06x offPrev=-%06x %s%s\n",
    1065                      pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
    1066                      pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core),
    1067                      mmR3GetTagName((MMTAG)pStat->Core.Key), pszSelf));
    1068 #else
    1069                 Log(("%p  %06x USED offNext=%06x offPrev=-%06x %d%s\n",
    1070                      pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
    1071                      pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core),
    1072                      (MMTAG)pStat->Core.Key, pszSelf));
    1073 #endif
    1074             }
    1075             else
    1076                 Log(("%p  %06x USED offNext=%06x offPrev=-%06x\n",
    1077                      pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
    1078                      pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core)));
    1079         }
    1080         else
    1081             Log(("%p  %06x FREE offNext=%06x offPrev=-%06x : cb=%06x offNext=%06x offPrev=-%06x\n",
    1082                  pCur, (uintptr_t)pCur - (uintptr_t)CTXSUFF(pHeap->pbHeap),
    1083                  pCur->core.offNext, -MMHYPERCHUNK_GET_OFFPREV(&pCur->core), pCur->cb, pCur->offNext, pCur->offPrev));
     1147        mmHyperHeapDumpOne(pHeap, pCur);
    10841148
    10851149        /* next */
Note: See TracChangeset for help on using the changeset viewer.

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