Changeset 97938 in vbox for trunk/src/VBox/Additions/common
- Timestamp:
- Jan 2, 2023 3:15:00 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR0LibPhysHeap.cpp
r97937 r97938 163 163 164 164 /** Size of user data in the block. Does not include this block header. */ 165 uint32_t cb DataSize: 31;165 uint32_t cbUser : 31; 166 166 /** The top bit indicates whether it's allocated or free. */ 167 167 uint32_t fAllocated : 1; … … 202 202 203 203 /** Size of the chunk. Includes the chunk header. */ 204 uint32_t cb Size;204 uint32_t cbChunk; 205 205 206 206 /** Physical address of the chunk (contiguous). */ … … 238 238 for (VBGLPHYSHEAPCHUNK *pChunk = g_vbgldata.pChunkHead; pChunk; pChunk = pChunk->pNext) 239 239 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->cb Size,240 pChunk, pChunk->pNext, pChunk->pPrev, pChunk->u32Signature, pChunk->cbChunk, 241 241 pChunk->cBlocks, pChunk->cFreeBlocks, pChunk->physAddr)); 242 242 … … 244 244 for (VBGLPHYSHEAPBLOCK *pBlock = g_vbgldata.pBlockHead; pBlock; pBlock = pBlock->pNext) 245 245 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, size = %05x, sign = %08X, %s, pChunk = %p\n", 246 pBlock, pBlock->pNext, pBlock->pPrev, pBlock->cb DataSize,246 pBlock, pBlock->pNext, pBlock->pPrev, pBlock->cbUser, 247 247 pBlock->u32Signature, pBlock->fAllocated ? "allocated" : " free", pBlock->pChunk)); 248 248 … … 250 250 for (VBGLPHYSHEAPFREEBLOCK *pBlock = g_vbgldata.pFreeHead; pBlock; pBlock = pBlock->pNextFree) 251 251 VBGL_PH_dprintf(("%p: pNextFree = %p, pPrevFree = %p, size = %05x, sign = %08X, pChunk = %p%s\n", 252 pBlock, pBlock->pNextFree, pBlock->pPrevFree, pBlock->Core.cb DataSize,252 pBlock, pBlock->pNextFree, pBlock->pPrevFree, pBlock->Core.cbUser, 253 253 pBlock->Core.u32Signature, pBlock->Core.pChunk, 254 254 !pBlock->Core.fAllocated ? "" : " !!allocated-block-on-freelist!!")); … … 259 259 260 260 261 static void vbglPhysHeapInitFreeBlock(VBGLPHYSHEAPFREEBLOCK *pBlock, VBGLPHYSHEAPCHUNK *pChunk, uint32_t cb DataSize)261 static void vbglPhysHeapInitFreeBlock(VBGLPHYSHEAPFREEBLOCK *pBlock, VBGLPHYSHEAPCHUNK *pChunk, uint32_t cbUser) 262 262 { 263 263 VBGL_PH_ASSERT(pBlock != NULL); … … 265 265 266 266 pBlock->Core.u32Signature = VBGL_PH_BLOCKSIGNATURE; 267 pBlock->Core.cb DataSize = cbDataSize;267 pBlock->Core.cbUser = cbUser; 268 268 pBlock->Core.fAllocated = false; 269 269 pBlock->Core.pNext = NULL; … … 282 282 g_vbgldata.cBlocks += 1; 283 283 pBlock->pChunk->cBlocks += 1; 284 AssertMsg((uint32_t)pBlock->pChunk->cBlocks <= pBlock->pChunk->cb Size/ sizeof(VBGLPHYSHEAPFREEBLOCK),285 ("pChunk=%p: cb Size=%#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)); 286 286 } 287 287 … … 363 363 pBlock->pChunk->cBlocks -= 1; 364 364 AssertMsg(pBlock->pChunk->cBlocks >= 0, 365 ("pChunk=%p: cb Size=%#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)); 366 366 Assert(g_vbgldata.cBlocks >= 0); 367 367 } … … 465 465 pBlock->Core.pChunk->cFreeBlocks -= 1; 466 466 AssertMsg(pBlock->Core.pChunk->cFreeBlocks >= 0, 467 ("pChunk=%p: cb Size=%#x cFreeBlocks=%d\n",468 pBlock->Core.pChunk, pBlock->Core.pChunk->cb Size, pBlock->Core.pChunk->cFreeBlocks));467 ("pChunk=%p: cbChunk=%#x cFreeBlocks=%d\n", 468 pBlock->Core.pChunk, pBlock->Core.pChunk->cbChunk, pBlock->Core.pChunk->cFreeBlocks)); 469 469 Assert(g_vbgldata.cFreeBlocks >= 0); 470 470 } … … 527 527 */ 528 528 pChunk->u32Signature = VBGL_PH_CHUNKSIGNATURE; 529 pChunk->cb Size= cbChunk;529 pChunk->cbChunk = cbChunk; 530 530 pChunk->physAddr = (uint32_t)PhysAddr; 531 531 pChunk->cBlocks = 0; … … 557 557 g_vbgldata.pChunkHead = pChunk; 558 558 559 VBGL_PH_dprintf(("Allocated chunk %p LB %#x, block %p LB %#x\n", pChunk, cbChunk, pBlock, pBlock->Core.cb DataSize));559 VBGL_PH_dprintf(("Allocated chunk %p LB %#x, block %p LB %#x\n", pChunk, cbChunk, pBlock, pBlock->Core.cbUser)); 560 560 return pBlock; 561 561 } … … 574 574 VBGL_PH_ASSERT_MSG(pChunk->u32Signature == VBGL_PH_CHUNKSIGNATURE, ("pChunk->u32Signature = %08X\n", pChunk->u32Signature)); 575 575 576 VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk, pChunk->cb Size));576 VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk, pChunk->cbChunk)); 577 577 578 578 /* … … 584 584 * continue to work if the block list ends in an unsorted state. 585 585 */ 586 uEnd = (uintptr_t)pChunk + pChunk->cb Size;586 uEnd = (uintptr_t)pChunk + pChunk->cbChunk; 587 587 uCur = (uintptr_t)(pChunk + 1); 588 588 … … 593 593 Assert(pBlock->pChunk == pChunk); 594 594 595 uCur += pBlock->cb DataSize+ sizeof(VBGLPHYSHEAPBLOCK);595 uCur += pBlock->cbUser + sizeof(VBGLPHYSHEAPBLOCK); 596 596 Assert(uCur == (uintptr_t)pBlock->pNext || uCur >= uEnd); 597 597 … … 601 601 } 602 602 603 VBGL_PH_ASSERT_MSG(uCur == uEnd, ("uCur = %p, uEnd = %p, pChunk->cb Size = %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)); 604 604 605 605 /* … … 621 621 * Finally, free the chunk memory. 622 622 */ 623 RTMemContFree(pChunk, pChunk->cb Size);624 } 625 626 627 DECLR0VBGL(void *) VbglR0PhysHeapAlloc(uint32_t cb Size)623 RTMemContFree(pChunk, pChunk->cbChunk); 624 } 625 626 627 DECLR0VBGL(void *) VbglR0PhysHeapAlloc(uint32_t cb) 628 628 { 629 629 VBGLPHYSHEAPFREEBLOCK *pBlock; … … 639 639 * and align the size to prevent pointer misalignment and whatnot. 640 640 */ 641 cb Size = RT_MAX(cbSize, VBGL_PH_SMALLEST_ALLOC_SIZE);642 cb Size = 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); 643 643 AssertCompile(VBGL_PH_ALLOC_ALIGN <= sizeof(pBlock->Core)); 644 644 … … 653 653 */ 654 654 #ifdef VBGL_PH_STOP_SEARCH_AT_EXCESS 655 cbAlwaysSplit = cb Size+ VBGL_PH_STOP_SEARCH_AT_EXCESS;655 cbAlwaysSplit = cb + VBGL_PH_STOP_SEARCH_AT_EXCESS; 656 656 #endif 657 657 cLeft = VBGL_PH_MAX_FREE_SEARCH; 658 658 pBlock = NULL; 659 if (cb Size<= PAGE_SIZE / 4 * 3)659 if (cb <= PAGE_SIZE / 4 * 3) 660 660 { 661 661 /* Smaller than 3/4 page: Prefer a free block that can keep the request within a single page, … … 665 665 { 666 666 AssertBreak(pIter->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE); 667 if (pIter->Core.cb DataSize >= cbSize)667 if (pIter->Core.cbUser >= cb) 668 668 { 669 if (pIter->Core.cb DataSize == cbSize)669 if (pIter->Core.cbUser == cb) 670 670 { 671 if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cb Size)671 if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cb) 672 672 { 673 673 pBlock = pIter; … … 678 678 else 679 679 { 680 if (!pFallback || pIter->Core.cb DataSize < pFallback->Core.cbDataSize)680 if (!pFallback || pIter->Core.cbUser < pFallback->Core.cbUser) 681 681 pFallback = pIter; 682 if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cb Size)682 if (PAGE_SIZE - ((uintptr_t)(pIter + 1) & PAGE_OFFSET_MASK) >= cb) 683 683 { 684 if (!pBlock || pIter->Core.cb DataSize < pBlock->Core.cbDataSize)684 if (!pBlock || pIter->Core.cbUser < pBlock->Core.cbUser) 685 685 pBlock = pIter; 686 686 #ifdef VBGL_PH_STOP_SEARCH_AT_EXCESS 687 else if (pIter->Core.cb DataSize>= cbAlwaysSplit)687 else if (pIter->Core.cbUser >= cbAlwaysSplit) 688 688 { 689 689 pBlock = pIter; … … 710 710 { 711 711 AssertBreak(pIter->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE); 712 if (pIter->Core.cb DataSize >= cbSize)712 if (pIter->Core.cbUser >= cb) 713 713 { 714 if (pIter->Core.cb DataSize == cbSize)714 if (pIter->Core.cbUser == cb) 715 715 { 716 716 /* Exact match - we're done! */ … … 720 720 721 721 #ifdef VBGL_PH_STOP_SEARCH_AT_EXCESS 722 if (pIter->Core.cb DataSize>= cbAlwaysSplit)722 if (pIter->Core.cbUser >= cbAlwaysSplit) 723 723 { 724 724 /* Really big block - no point continue searching! */ … … 728 728 #endif 729 729 /* Looking for a free block with nearest size. */ 730 if (!pBlock || pIter->Core.cb DataSize < pBlock->Core.cbDataSize)730 if (!pBlock || pIter->Core.cbUser < pBlock->Core.cbUser) 731 731 pBlock = pIter; 732 732 … … 743 743 /* No free blocks, allocate a new chunk, the only free block of the 744 744 chunk will be returned. */ 745 pBlock = vbglPhysHeapChunkAlloc(cb Size);745 pBlock = vbglPhysHeapChunkAlloc(cb); 746 746 } 747 747 … … 763 763 */ 764 764 AssertCompile(VBGL_PH_MIN_SPLIT_FREE_BLOCK >= sizeof(*pBlock) - sizeof(pBlock->Core)); 765 if (pBlock->Core.cb DataSize >= 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) 766 766 { 767 pIter = (VBGLPHYSHEAPFREEBLOCK *)((uintptr_t)(&pBlock->Core + 1) + cb Size);768 vbglPhysHeapInitFreeBlock(pIter, pBlock->Core.pChunk, pBlock->Core.cb DataSize - cbSize- sizeof(VBGLPHYSHEAPBLOCK));769 770 pBlock->Core.cb DataSize = 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; 771 771 772 772 /* Insert the new 'pIter' block after the 'pBlock' in the block list … … 789 789 rc = RTSemFastMutexRelease(g_vbgldata.mutexHeap); 790 790 791 VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns %p size %x\n", pBlock + 1, pBlock->Core.cb DataSize));791 VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns %p size %x\n", pBlock + 1, pBlock->Core.cbUser)); 792 792 return &pBlock->Core + 1; 793 793 } … … 799 799 AssertRC(rc); 800 800 801 VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns NULL (requested %#x bytes)\n", cb Size));801 VBGL_PH_dprintf(("VbglR0PhysHeapAlloc: returns NULL (requested %#x bytes)\n", cb)); 802 802 return NULL; 803 803 } … … 823 823 824 824 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->cb DataSize, pBlock->fAllocated));825 pv, pBlock, pBlock->u32Signature, pBlock->cbUser, pBlock->fAllocated)); 826 826 } 827 827 else … … 848 848 if ( pBlock->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE 849 849 && pBlock->Core.fAllocated 850 && pBlock->Core.cb DataSize>= VBGL_PH_SMALLEST_ALLOC_SIZE)850 && pBlock->Core.cbUser >= VBGL_PH_SMALLEST_ALLOC_SIZE) 851 851 { 852 852 VBGLPHYSHEAPCHUNK *pChunk; … … 856 856 * Change the block status to freeed. 857 857 */ 858 VBGL_PH_dprintf(("VbglR0PhysHeapFree: %p size %#x\n", pv, pBlock->Core.cb DataSize));858 VBGL_PH_dprintf(("VbglR0PhysHeapFree: %p size %#x\n", pv, pBlock->Core.cbUser)); 859 859 860 860 pBlock->Core.fAllocated = false; … … 874 874 && pNeighbour->pChunk == pChunk) 875 875 { 876 Assert((uintptr_t)pBlock + sizeof(pBlock->Core) + pBlock->Core.cb DataSize== (uintptr_t)pNeighbour);876 Assert((uintptr_t)pBlock + sizeof(pBlock->Core) + pBlock->Core.cbUser == (uintptr_t)pNeighbour); 877 877 878 878 /* Adjust size of current memory block */ 879 pBlock->Core.cb DataSize += pNeighbour->cbDataSize+ sizeof(VBGLPHYSHEAPBLOCK);879 pBlock->Core.cbUser += pNeighbour->cbUser + sizeof(VBGLPHYSHEAPBLOCK); 880 880 881 881 /* Unlink the following node and invalid it. */ … … 884 884 885 885 pNeighbour->u32Signature = ~VBGL_PH_BLOCKSIGNATURE; 886 pNeighbour->cb DataSize= UINT32_MAX / 4;886 pNeighbour->cbUser = UINT32_MAX / 4; 887 887 888 888 dumpheap("post merge after"); … … 897 897 && pNeighbour->pChunk == pChunk) 898 898 { 899 Assert((uintptr_t)pNeighbour + sizeof(*pNeighbour) + pNeighbour->cb DataSize== (uintptr_t)pBlock);899 Assert((uintptr_t)pNeighbour + sizeof(*pNeighbour) + pNeighbour->cbUser == (uintptr_t)pBlock); 900 900 901 901 /* Adjust size of the block before us */ 902 pNeighbour->cb DataSize += pBlock->Core.cbDataSize+ sizeof(VBGLPHYSHEAPBLOCK);902 pNeighbour->cbUser += pBlock->Core.cbUser + sizeof(VBGLPHYSHEAPBLOCK); 903 903 904 904 /* Unlink this node and invalid it. */ … … 907 907 908 908 pBlock->Core.u32Signature = ~VBGL_PH_BLOCKSIGNATURE; 909 pBlock->Core.cb DataSize= UINT32_MAX / 8;909 pBlock->Core.cbUser = UINT32_MAX / 8; 910 910 911 911 pBlock = NULL; /* invalid */ … … 949 949 else 950 950 AssertMsgFailed(("pBlock: %p: u32Signature=%#x cb=%#x fAllocated=%d - double free?\n", 951 pBlock, pBlock->Core.u32Signature, pBlock->Core.cb DataSize, pBlock->Core.fAllocated));951 pBlock, pBlock->Core.u32Signature, pBlock->Core.cbUser, pBlock->Core.fAllocated)); 952 952 953 953 rc = RTSemFastMutexRelease(g_vbgldata.mutexHeap); … … 978 978 Assert(pCurBlock->Core.u32Signature == VBGL_PH_BLOCKSIGNATURE); 979 979 Assert(!pCurBlock->Core.fAllocated); 980 cbTotal += pCurBlock->Core.cb DataSize;980 cbTotal += pCurBlock->Core.cbUser; 981 981 } 982 982 … … 1009 1009 1010 1010 const VBGLPHYSHEAPBLOCK *pCurBlock = (const VBGLPHYSHEAPBLOCK *)(pCurChunk + 1); 1011 uintptr_t const uEnd = (uintptr_t)pCurChunk + pCurChunk->cb Size;1011 uintptr_t const uEnd = (uintptr_t)pCurChunk + pCurChunk->cbChunk; 1012 1012 unsigned acBlocks[2] = { 0, 0 }; 1013 1013 while ((uintptr_t)pCurBlock < uEnd) … … 1019 1019 RTErrInfoSetF(pErrInfo, VERR_INTERNAL_ERROR_2, 1020 1020 "pCurBlock=%p: pChunk=%p, expected %p", pCurBlock, pCurBlock->pChunk, pCurChunk)); 1021 AssertReturn( pCurBlock->cb DataSize>= VBGL_PH_SMALLEST_ALLOC_SIZE1022 && pCurBlock->cb DataSize<= VBGL_PH_LARGEST_ALLOC_SIZE1023 && RT_ALIGN_32(pCurBlock->cb DataSize, 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, 1024 1024 RTErrInfoSetF(pErrInfo, VERR_INTERNAL_ERROR_3, 1025 "pCurBlock=%p: cb DataSize=%#x", pCurBlock, pCurBlock->cbDataSize));1025 "pCurBlock=%p: cbUser=%#x", pCurBlock, pCurBlock->cbUser)); 1026 1026 AssertReturn(pCurBlock == pCurBlockListEntry, 1027 1027 RTErrInfoSetF(pErrInfo, VERR_INTERNAL_ERROR_4, … … 1038 1038 pPrevBlockListEntry = pCurBlock; 1039 1039 pCurBlockListEntry = pCurBlock->pNext; 1040 pCurBlock = (const VBGLPHYSHEAPBLOCK *)((uintptr_t)(pCurBlock + 1) + pCurBlock->cb DataSize);1040 pCurBlock = (const VBGLPHYSHEAPBLOCK *)((uintptr_t)(pCurBlock + 1) + pCurBlock->cbUser); 1041 1041 } 1042 1042 AssertReturn((uintptr_t)pCurBlock == uEnd,
Note:
See TracChangeset
for help on using the changeset viewer.