VirtualBox

Changeset 97938 in vbox for trunk/src/VBox/Additions/common


Ignore:
Timestamp:
Jan 2, 2023 3:15:00 PM (2 years ago)
Author:
vboxsync
Message:

Add/VBoxGuestR0LibPhysHeap.cpp: Renamed some members and variables, no code change.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR0LibPhysHeap.cpp

    r97937 r97938  
    163163
    164164    /** Size of user data in the block. Does not include this block header. */
    165     uint32_t cbDataSize : 31;
     165    uint32_t cbUser : 31;
    166166    /** The top bit indicates whether it's allocated or free. */
    167167    uint32_t fAllocated : 1;
     
    202202
    203203    /** Size of the chunk. Includes the chunk header. */
    204     uint32_t cbSize;
     204    uint32_t cbChunk;
    205205
    206206    /** Physical address of the chunk (contiguous). */
     
    238238   for (VBGLPHYSHEAPCHUNK *pChunk = g_vbgldata.pChunkHead; pChunk; pChunk = pChunk->pNext)
    239239       VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, cBlocks = %8d, cFreeBlocks=%8d, phys = %08X\n",
    240                         pChunk, pChunk->pNext, pChunk->pPrev, pChunk->u32Signature, pChunk->cbSize,
     240                        pChunk, pChunk->pNext, pChunk->pPrev, pChunk->u32Signature, pChunk->cbChunk,
    241241                        pChunk->cBlocks, pChunk->cFreeBlocks, pChunk->physAddr));
    242242
     
    244244   for (VBGLPHYSHEAPBLOCK *pBlock = g_vbgldata.pBlockHead; pBlock; pBlock = pBlock->pNext)
    245245       VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, size = %05x, sign = %08X, %s, pChunk = %p\n",
    246                         pBlock, pBlock->pNext, pBlock->pPrev, pBlock->cbDataSize,
     246                        pBlock, pBlock->pNext, pBlock->pPrev, pBlock->cbUser,
    247247                        pBlock->u32Signature,  pBlock->fAllocated ? "allocated" : "     free", pBlock->pChunk));
    248248
     
    250250   for (VBGLPHYSHEAPFREEBLOCK *pBlock = g_vbgldata.pFreeHead; pBlock; pBlock = pBlock->pNextFree)
    251251       VBGL_PH_dprintf(("%p: pNextFree = %p, pPrevFree = %p, size = %05x, sign = %08X, pChunk = %p%s\n",
    252                         pBlock, pBlock->pNextFree, pBlock->pPrevFree, pBlock->Core.cbDataSize,
     252                        pBlock, pBlock->pNextFree, pBlock->pPrevFree, pBlock->Core.cbUser,
    253253                        pBlock->Core.u32Signature, pBlock->Core.pChunk,
    254254                        !pBlock->Core.fAllocated ? "" : " !!allocated-block-on-freelist!!"));
     
    259259
    260260
    261 static void vbglPhysHeapInitFreeBlock(VBGLPHYSHEAPFREEBLOCK *pBlock, VBGLPHYSHEAPCHUNK *pChunk, uint32_t cbDataSize)
     261static void vbglPhysHeapInitFreeBlock(VBGLPHYSHEAPFREEBLOCK *pBlock, VBGLPHYSHEAPCHUNK *pChunk, uint32_t cbUser)
    262262{
    263263    VBGL_PH_ASSERT(pBlock != NULL);
     
    265265
    266266    pBlock->Core.u32Signature = VBGL_PH_BLOCKSIGNATURE;
    267     pBlock->Core.cbDataSize   = cbDataSize;
     267    pBlock->Core.cbUser       = cbUser;
    268268    pBlock->Core.fAllocated   = false;
    269269    pBlock->Core.pNext        = NULL;
     
    282282    g_vbgldata.cBlocks      += 1;
    283283    pBlock->pChunk->cBlocks += 1;
    284     AssertMsg((uint32_t)pBlock->pChunk->cBlocks <= pBlock->pChunk->cbSize / sizeof(VBGLPHYSHEAPFREEBLOCK),
    285               ("pChunk=%p: cbSize=%#x cBlocks=%d\n", pBlock->pChunk, pBlock->pChunk->cbSize, pBlock->pChunk->cBlocks));
     284    AssertMsg((uint32_t)pBlock->pChunk->cBlocks <= pBlock->pChunk->cbChunk / sizeof(VBGLPHYSHEAPFREEBLOCK),
     285              ("pChunk=%p: cbChunk=%#x cBlocks=%d\n", pBlock->pChunk, pBlock->pChunk->cbChunk, pBlock->pChunk->cBlocks));
    286286}
    287287
     
    363363    pBlock->pChunk->cBlocks -= 1;
    364364    AssertMsg(pBlock->pChunk->cBlocks >= 0,
    365               ("pChunk=%p: cbSize=%#x cBlocks=%d\n", pBlock->pChunk, pBlock->pChunk->cbSize, pBlock->pChunk->cBlocks));
     365              ("pChunk=%p: cbChunk=%#x cBlocks=%d\n", pBlock->pChunk, pBlock->pChunk->cbChunk, pBlock->pChunk->cBlocks));
    366366    Assert(g_vbgldata.cBlocks >= 0);
    367367}
     
    465465    pBlock->Core.pChunk->cFreeBlocks -= 1;
    466466    AssertMsg(pBlock->Core.pChunk->cFreeBlocks >= 0,
    467               ("pChunk=%p: cbSize=%#x cFreeBlocks=%d\n",
    468                pBlock->Core.pChunk, pBlock->Core.pChunk->cbSize, pBlock->Core.pChunk->cFreeBlocks));
     467              ("pChunk=%p: cbChunk=%#x cFreeBlocks=%d\n",
     468               pBlock->Core.pChunk, pBlock->Core.pChunk->cbChunk, pBlock->Core.pChunk->cFreeBlocks));
    469469    Assert(g_vbgldata.cFreeBlocks >= 0);
    470470}
     
    527527         */
    528528        pChunk->u32Signature     = VBGL_PH_CHUNKSIGNATURE;
    529         pChunk->cbSize           = cbChunk;
     529        pChunk->cbChunk          = cbChunk;
    530530        pChunk->physAddr         = (uint32_t)PhysAddr;
    531531        pChunk->cBlocks          = 0;
     
    557557        g_vbgldata.pChunkHead    = pChunk;
    558558
    559         VBGL_PH_dprintf(("Allocated chunk %p LB %#x, block %p LB %#x\n", pChunk, cbChunk, pBlock, pBlock->Core.cbDataSize));
     559        VBGL_PH_dprintf(("Allocated chunk %p LB %#x, block %p LB %#x\n", pChunk, cbChunk, pBlock, pBlock->Core.cbUser));
    560560        return pBlock;
    561561    }
     
    574574    VBGL_PH_ASSERT_MSG(pChunk->u32Signature == VBGL_PH_CHUNKSIGNATURE, ("pChunk->u32Signature = %08X\n", pChunk->u32Signature));
    575575
    576     VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk, pChunk->cbSize));
     576    VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk, pChunk->cbChunk));
    577577
    578578    /*
     
    584584     *       continue to work if the block list ends in an unsorted state.
    585585     */
    586     uEnd = (uintptr_t)pChunk + pChunk->cbSize;
     586    uEnd = (uintptr_t)pChunk + pChunk->cbChunk;
    587587    uCur = (uintptr_t)(pChunk + 1);
    588588
     
    593593        Assert(pBlock->pChunk == pChunk);
    594594
    595         uCur += pBlock->cbDataSize + sizeof(VBGLPHYSHEAPBLOCK);
     595        uCur += pBlock->cbUser + sizeof(VBGLPHYSHEAPBLOCK);
    596596        Assert(uCur == (uintptr_t)pBlock->pNext || uCur >= uEnd);
    597597
     
    601601    }
    602602
    603     VBGL_PH_ASSERT_MSG(uCur == uEnd, ("uCur = %p, uEnd = %p, pChunk->cbSize = %08X\n", uCur, uEnd, pChunk->cbSize));
     603    VBGL_PH_ASSERT_MSG(uCur == uEnd, ("uCur = %p, uEnd = %p, pChunk->cbChunk = %08X\n", uCur, uEnd, pChunk->cbChunk));
    604604
    605605    /*
     
    621621     * Finally, free the chunk memory.
    622622     */
    623     RTMemContFree(pChunk, pChunk->cbSize);
    624 }
    625 
    626 
    627 DECLR0VBGL(void *) VbglR0PhysHeapAlloc(uint32_t cbSize)
     623    RTMemContFree(pChunk, pChunk->cbChunk);
     624}
     625
     626
     627DECLR0VBGL(void *) VbglR0PhysHeapAlloc(uint32_t cb)
    628628{
    629629    VBGLPHYSHEAPFREEBLOCK  *pBlock;
     
    639639     * and align the size to prevent pointer misalignment and whatnot.
    640640     */
    641     cbSize = RT_MAX(cbSize, VBGL_PH_SMALLEST_ALLOC_SIZE);
    642     cbSize = RT_ALIGN_32(cbSize, VBGL_PH_ALLOC_ALIGN);
     641    cb = RT_MAX(cb, VBGL_PH_SMALLEST_ALLOC_SIZE);
     642    cb = RT_ALIGN_32(cb, VBGL_PH_ALLOC_ALIGN);
    643643    AssertCompile(VBGL_PH_ALLOC_ALIGN <= sizeof(pBlock->Core));
    644644
     
    653653     */
    654654#ifdef VBGL_PH_STOP_SEARCH_AT_EXCESS
    655     cbAlwaysSplit = cbSize + VBGL_PH_STOP_SEARCH_AT_EXCESS;
     655    cbAlwaysSplit = cb + VBGL_PH_STOP_SEARCH_AT_EXCESS;
    656656#endif
    657657    cLeft         = VBGL_PH_MAX_FREE_SEARCH;
    658658    pBlock        = NULL;
    659     if (cbSize <= PAGE_SIZE / 4 * 3)
     659    if (cb <= PAGE_SIZE / 4 * 3)
    660660    {
    661661        /* Smaller than 3/4 page:  Prefer a free block that can keep the request within a single page,
     
    665665        {
    666666            AssertBreak(pIter->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE);
    667             if (pIter->Core.cbDataSize >= cbSize)
     667            if (pIter->Core.cbUser >= cb)
    668668            {
    669                 if (pIter->Core.cbDataSize == cbSize)
     669                if (pIter->Core.cbUser == cb)
    670670                {
    671                     if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cbSize)
     671                    if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cb)
    672672                    {
    673673                        pBlock = pIter;
     
    678678                else
    679679                {
    680                     if (!pFallback || pIter->Core.cbDataSize < pFallback->Core.cbDataSize)
     680                    if (!pFallback || pIter->Core.cbUser < pFallback->Core.cbUser)
    681681                        pFallback = pIter;
    682                     if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cbSize)
     682                    if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cb)
    683683                    {
    684                         if (!pBlock || pIter->Core.cbDataSize < pBlock->Core.cbDataSize)
     684                        if (!pBlock || pIter->Core.cbUser < pBlock->Core.cbUser)
    685685                            pBlock = pIter;
    686686#ifdef VBGL_PH_STOP_SEARCH_AT_EXCESS
    687                         else if (pIter->Core.cbDataSize >= cbAlwaysSplit)
     687                        else if (pIter->Core.cbUser >= cbAlwaysSplit)
    688688                        {
    689689                            pBlock = pIter;
     
    710710        {
    711711            AssertBreak(pIter->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE);
    712             if (pIter->Core.cbDataSize >= cbSize)
     712            if (pIter->Core.cbUser >= cb)
    713713            {
    714                 if (pIter->Core.cbDataSize == cbSize)
     714                if (pIter->Core.cbUser == cb)
    715715                {
    716716                    /* Exact match - we're done! */
     
    720720
    721721#ifdef VBGL_PH_STOP_SEARCH_AT_EXCESS
    722                 if (pIter->Core.cbDataSize >= cbAlwaysSplit)
     722                if (pIter->Core.cbUser >= cbAlwaysSplit)
    723723                {
    724724                    /* Really big block - no point continue searching! */
     
    728728#endif
    729729                /* Looking for a free block with nearest size. */
    730                 if (!pBlock || pIter->Core.cbDataSize < pBlock->Core.cbDataSize)
     730                if (!pBlock || pIter->Core.cbUser < pBlock->Core.cbUser)
    731731                    pBlock = pIter;
    732732
     
    743743        /* No free blocks, allocate a new chunk, the only free block of the
    744744           chunk will be returned. */
    745         pBlock = vbglPhysHeapChunkAlloc(cbSize);
     745        pBlock = vbglPhysHeapChunkAlloc(cb);
    746746    }
    747747
     
    763763         */
    764764        AssertCompile(VBGL_PH_MIN_SPLIT_FREE_BLOCK >= sizeof(*pBlock) - sizeof(pBlock->Core));
    765         if (pBlock->Core.cbDataSize >= sizeof(VBGLPHYSHEAPBLOCK) * 2 + VBGL_PH_MIN_SPLIT_FREE_BLOCK + cbSize)
     765        if (pBlock->Core.cbUser >= sizeof(VBGLPHYSHEAPBLOCK) * 2 + VBGL_PH_MIN_SPLIT_FREE_BLOCK + cb)
    766766        {
    767             pIter = (VBGLPHYSHEAPFREEBLOCK *)((uintptr_t)(&pBlock->Core + 1) + cbSize);
    768             vbglPhysHeapInitFreeBlock(pIter, pBlock->Core.pChunk, pBlock->Core.cbDataSize - cbSize - sizeof(VBGLPHYSHEAPBLOCK));
    769 
    770             pBlock->Core.cbDataSize = cbSize;
     767            pIter = (VBGLPHYSHEAPFREEBLOCK *)((uintptr_t)(&pBlock->Core + 1) + cb);
     768            vbglPhysHeapInitFreeBlock(pIter, pBlock->Core.pChunk, pBlock->Core.cbUser - cb - sizeof(VBGLPHYSHEAPBLOCK));
     769
     770            pBlock->Core.cbUser = cb;
    771771
    772772            /* Insert the new 'pIter' block after the 'pBlock' in the block list
     
    789789        rc = RTSemFastMutexRelease(g_vbgldata.mutexHeap);
    790790
    791         VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns %p size %x\n", pBlock + 1, pBlock->Core.cbDataSize));
     791        VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns %p size %x\n", pBlock + 1, pBlock->Core.cbUser));
    792792        return &pBlock->Core + 1;
    793793    }
     
    799799    AssertRC(rc);
    800800
    801     VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns NULL (requested %#x bytes)\n", cbSize));
     801    VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns NULL (requested %#x bytes)\n", cb));
    802802    return NULL;
    803803}
     
    823823
    824824        AssertMsgFailed(("Use after free or corrupt pointer variable: pv=%p pBlock=%p: u32Signature=%#x cb=%#x fAllocated=%d\n",
    825                          pv, pBlock, pBlock->u32Signature, pBlock->cbDataSize, pBlock->fAllocated));
     825                         pv, pBlock, pBlock->u32Signature, pBlock->cbUser, pBlock->fAllocated));
    826826    }
    827827    else
     
    848848        if (   pBlock->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE
    849849            && pBlock->Core.fAllocated
    850             && pBlock->Core.cbDataSize >= VBGL_PH_SMALLEST_ALLOC_SIZE)
     850            && pBlock->Core.cbUser >= VBGL_PH_SMALLEST_ALLOC_SIZE)
    851851        {
    852852            VBGLPHYSHEAPCHUNK *pChunk;
     
    856856             * Change the block status to freeed.
    857857             */
    858             VBGL_PH_dprintf(("VbglR0PhysHeapFree: %p size %#x\n", pv, pBlock->Core.cbDataSize));
     858            VBGL_PH_dprintf(("VbglR0PhysHeapFree: %p size %#x\n", pv, pBlock->Core.cbUser));
    859859
    860860            pBlock->Core.fAllocated = false;
     
    874874                && pNeighbour->pChunk == pChunk)
    875875            {
    876                 Assert((uintptr_t)pBlock + sizeof(pBlock->Core) + pBlock->Core.cbDataSize == (uintptr_t)pNeighbour);
     876                Assert((uintptr_t)pBlock + sizeof(pBlock->Core) + pBlock->Core.cbUser == (uintptr_t)pNeighbour);
    877877
    878878                /* Adjust size of current memory block */
    879                 pBlock->Core.cbDataSize += pNeighbour->cbDataSize + sizeof(VBGLPHYSHEAPBLOCK);
     879                pBlock->Core.cbUser += pNeighbour->cbUser + sizeof(VBGLPHYSHEAPBLOCK);
    880880
    881881                /* Unlink the following node and invalid it. */
     
    884884
    885885                pNeighbour->u32Signature = ~VBGL_PH_BLOCKSIGNATURE;
    886                 pNeighbour->cbDataSize   = UINT32_MAX / 4;
     886                pNeighbour->cbUser       = UINT32_MAX / 4;
    887887
    888888                dumpheap("post merge after");
     
    897897                && pNeighbour->pChunk == pChunk)
    898898            {
    899                 Assert((uintptr_t)pNeighbour + sizeof(*pNeighbour) + pNeighbour->cbDataSize == (uintptr_t)pBlock);
     899                Assert((uintptr_t)pNeighbour + sizeof(*pNeighbour) + pNeighbour->cbUser == (uintptr_t)pBlock);
    900900
    901901                /* Adjust size of the block before us */
    902                 pNeighbour->cbDataSize += pBlock->Core.cbDataSize + sizeof(VBGLPHYSHEAPBLOCK);
     902                pNeighbour->cbUser += pBlock->Core.cbUser + sizeof(VBGLPHYSHEAPBLOCK);
    903903
    904904                /* Unlink this node and invalid it. */
     
    907907
    908908                pBlock->Core.u32Signature = ~VBGL_PH_BLOCKSIGNATURE;
    909                 pBlock->Core.cbDataSize   = UINT32_MAX / 8;
     909                pBlock->Core.cbUser       = UINT32_MAX / 8;
    910910
    911911                pBlock = NULL; /* invalid */
     
    949949        else
    950950            AssertMsgFailed(("pBlock: %p: u32Signature=%#x cb=%#x fAllocated=%d - double free?\n",
    951                              pBlock, pBlock->Core.u32Signature, pBlock->Core.cbDataSize, pBlock->Core.fAllocated));
     951                             pBlock, pBlock->Core.u32Signature, pBlock->Core.cbUser, pBlock->Core.fAllocated));
    952952
    953953        rc = RTSemFastMutexRelease(g_vbgldata.mutexHeap);
     
    978978        Assert(pCurBlock->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE);
    979979        Assert(!pCurBlock->Core.fAllocated);
    980         cbTotal += pCurBlock->Core.cbDataSize;
     980        cbTotal += pCurBlock->Core.cbUser;
    981981    }
    982982
     
    10091009
    10101010        const VBGLPHYSHEAPBLOCK *pCurBlock   = (const VBGLPHYSHEAPBLOCK *)(pCurChunk + 1);
    1011         uintptr_t const          uEnd        = (uintptr_t)pCurChunk + pCurChunk->cbSize;
     1011        uintptr_t const          uEnd        = (uintptr_t)pCurChunk + pCurChunk->cbChunk;
    10121012        unsigned                 acBlocks[2] = { 0, 0 };
    10131013        while ((uintptr_t)pCurBlock < uEnd)
     
    10191019                         RTErrInfoSetF(pErrInfo, VERR_INTERNAL_ERROR_2,
    10201020                                       "pCurBlock=%p: pChunk=%p, expected %p", pCurBlock, pCurBlock->pChunk, pCurChunk));
    1021             AssertReturn(   pCurBlock->cbDataSize >= VBGL_PH_SMALLEST_ALLOC_SIZE
    1022                          && pCurBlock->cbDataSize <= VBGL_PH_LARGEST_ALLOC_SIZE
    1023                          && RT_ALIGN_32(pCurBlock->cbDataSize, VBGL_PH_ALLOC_ALIGN) == pCurBlock->cbDataSize,
     1021            AssertReturn(   pCurBlock->cbUser >= VBGL_PH_SMALLEST_ALLOC_SIZE
     1022                         && pCurBlock->cbUser <= VBGL_PH_LARGEST_ALLOC_SIZE
     1023                         && RT_ALIGN_32(pCurBlock->cbUser, VBGL_PH_ALLOC_ALIGN) == pCurBlock->cbUser,
    10241024                         RTErrInfoSetF(pErrInfo, VERR_INTERNAL_ERROR_3,
    1025                                        "pCurBlock=%p: cbDataSize=%#x", pCurBlock, pCurBlock->cbDataSize));
     1025                                       "pCurBlock=%p: cbUser=%#x", pCurBlock, pCurBlock->cbUser));
    10261026            AssertReturn(pCurBlock == pCurBlockListEntry,
    10271027                         RTErrInfoSetF(pErrInfo, VERR_INTERNAL_ERROR_4,
     
    10381038            pPrevBlockListEntry = pCurBlock;
    10391039            pCurBlockListEntry  = pCurBlock->pNext;
    1040             pCurBlock = (const VBGLPHYSHEAPBLOCK *)((uintptr_t)(pCurBlock + 1) + pCurBlock->cbDataSize);
     1040            pCurBlock = (const VBGLPHYSHEAPBLOCK *)((uintptr_t)(pCurBlock + 1) + pCurBlock->cbUser);
    10411041        }
    10421042        AssertReturn((uintptr_t)pCurBlock == uEnd,
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