Changeset 100313 in vbox for trunk/src/VBox/Runtime/r3/alloc-ef.cpp
- Timestamp:
- Jun 28, 2023 10:29:57 AM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/alloc-ef.cpp
r98103 r100313 56 56 #include <iprt/param.h> 57 57 #include <iprt/string.h> 58 #include <iprt/system.h> 58 59 59 60 #ifdef RTALLOC_REPLACE_MALLOC … … 279 280 DECLINLINE(void) rtmemBlockDelayInsert(PRTMEMBLOCK pBlock) 280 281 { 281 size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE; 282 size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize(); 283 size_t const cbBlock = RTSystemPageAlignSize(pBlock->cbAligned) + cbFence; 282 284 pBlock->Core.pRight = NULL; 283 285 pBlock->Core.pLeft = NULL; … … 315 317 else 316 318 g_pBlocksDelayHead = NULL; 317 g_cbBlocksDelay -= RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE; 319 320 size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize(); 321 g_cbBlocksDelay -= RTSystemPageAlignSize(pBlock->cbAligned) + cbFence; 318 322 } 319 323 } … … 455 459 * Allocate a page for jump back code (we leak it). 456 460 */ 457 uint8_t *pbExecPage = (uint8_t *)RTMemPageAlloc(PAGE_SIZE); AssertFatal(pbExecPage); 458 int rc = RTMemProtect(pbExecPage, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC); AssertFatalRC(rc); 461 size_t const cbPage = RTSystemGetPageSize(); 462 uint8_t *pbExecPage = (uint8_t *)RTMemPageAlloc(cbPage); AssertFatal(pbExecPage); 463 int rc = RTMemProtect(pbExecPage, cbPage, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC); AssertFatalRC(rc); 459 464 460 465 /* … … 578 583 * Sanity. 579 584 */ 580 if ( RT_ALIGN_Z(RTALLOC_EFENCE_SIZE, PAGE_SIZE) != RTALLOC_EFENCE_SIZE 581 && RTALLOC_EFENCE_SIZE <= 0) 582 { 583 rtmemComplain(pszOp, "Invalid E-fence size! %#x\n", RTALLOC_EFENCE_SIZE); 585 size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize(); 586 size_t const cbPage = RTSystemGetPageSize(); 587 if (RTALLOC_EFENCE_SIZE_FACTOR <= 0) 588 { 589 rtmemComplain(pszOp, "Invalid E-fence size! %#x\n", RTALLOC_EFENCE_SIZE_FACTOR); 584 590 return NULL; 585 591 } … … 616 622 * Allocate a block with page alignment space + the size of the E-fence. 617 623 */ 618 size_t cbBlock = RT_ALIGN_Z(cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;624 size_t cbBlock = RT_ALIGN_Z(cbAligned, cbPage) + cbFence; 619 625 void *pvBlock = RTMemPageAlloc(cbBlock); 620 626 if (pvBlock) … … 626 632 #ifdef RTALLOC_EFENCE_IN_FRONT 627 633 void *pvEFence = pvBlock; 628 void *pv = (char *)pvEFence + RTALLOC_EFENCE_SIZE;634 void *pv = (char *)pvEFence + cbFence; 629 635 # ifdef RTALLOC_EFENCE_NOMAN_FILLER 630 memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE- cbUnaligned);636 memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - cbFence - cbUnaligned); 631 637 # endif 632 638 #else 633 void *pvEFence = (char *)pvBlock + (cbBlock - RTALLOC_EFENCE_SIZE);639 void *pvEFence = (char *)pvBlock + (cbBlock - cbFence); 634 640 void *pv = (char *)pvEFence - cbAligned; 635 641 # ifdef RTALLOC_EFENCE_NOMAN_FILLER 636 memset(pvBlock, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - RTALLOC_EFENCE_SIZE- cbAligned);642 memset(pvBlock, RTALLOC_EFENCE_NOMAN_FILLER, cbBlock - cbFence - cbAligned); 637 643 memset((char *)pv + cbUnaligned, RTALLOC_EFENCE_NOMAN_FILLER, cbAligned - cbUnaligned); 638 644 # endif … … 640 646 641 647 #ifdef RTALLOC_EFENCE_FENCE_FILLER 642 memset(pvEFence, RTALLOC_EFENCE_FENCE_FILLER, RTALLOC_EFENCE_SIZE);643 #endif 644 int rc = RTMemProtect(pvEFence, RTALLOC_EFENCE_SIZE, RTMEM_PROT_NONE);648 memset(pvEFence, RTALLOC_EFENCE_FENCE_FILLER, cbFence); 649 #endif 650 int rc = RTMemProtect(pvEFence, cbFence, RTMEM_PROT_NONE); 645 651 if (!rc) 646 652 { … … 658 664 return pv; 659 665 } 660 rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);666 rtmemComplain(pszOp, "RTMemProtect failed, pvEFence=%p size %d, rc=%d\n", pvEFence, cbFence, rc); 661 667 RTMemPageFree(pvBlock, cbBlock); 662 668 } … … 691 697 RTAssertDoPanic(); 692 698 699 size_t cbPage = RTSystemGetPageSize(); 693 700 #ifdef RTALLOC_EFENCE_TRACE 694 701 /* … … 707 714 # ifdef RTALLOC_EFENCE_IN_FRONT 708 715 void *pvWrong = ASMMemFirstMismatchingU8((char *)pv + pBlock->cbUnaligned, 709 RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) - pBlock->cbUnaligned,716 RT_ALIGN_Z(pBlock->cbAligned, cbPage) - pBlock->cbUnaligned, 710 717 RTALLOC_EFENCE_NOMAN_FILLER); 711 718 # else … … 716 723 if (pvWrong) 717 724 RTAssertDoPanic(); 718 pvWrong = ASMMemFirstMismatchingU8((void *)((uintptr_t)pv & ~ (uintptr_t)PAGE_OFFSET_MASK),719 RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) - pBlock->cbAligned,725 pvWrong = ASMMemFirstMismatchingU8((void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), 726 RT_ALIGN_Z(pBlock->cbAligned, cbPage) - pBlock->cbAligned, 720 727 RTALLOC_EFENCE_NOMAN_FILLER); 721 728 # endif … … 737 744 # endif 738 745 746 size_t const cbFence = RTALLOC_EFENCE_SIZE_FACTOR * RTSystemGetPageSize(); 739 747 # if defined(RTALLOC_EFENCE_FREE_DELAYED) && RTALLOC_EFENCE_FREE_DELAYED > 0 740 748 /* … … 753 761 pv = pBlock->Core.Key; 754 762 # ifdef RTALLOC_EFENCE_IN_FRONT 755 void *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;763 void *pvBlock = (char *)pv - cbFence; 756 764 # else 757 void *pvBlock = (void *)((uintptr_t)pv & ~ (uintptr_t)PAGE_OFFSET_MASK);765 void *pvBlock = (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()); 758 766 # endif 759 size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE;767 size_t cbBlock = RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence; 760 768 rc = RTMemProtect(pvBlock, cbBlock, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 761 769 if (RT_SUCCESS(rc)) 762 RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE);770 RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence); 763 771 else 764 772 rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvBlock, cbBlock, rc); … … 775 783 */ 776 784 # ifdef RTALLOC_EFENCE_IN_FRONT 777 void *pvBlock = (char *)pv - RTALLOC_EFENCE_SIZE;785 void *pvBlock = (char *)pv - cbFence; 778 786 void *pvEFence = pvBlock; 779 787 # else 780 void *pvBlock = (void *)((uintptr_t)pv & ~ (uintptr_t)PAGE_OFFSET_MASK);788 void *pvBlock = (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()); 781 789 void *pvEFence = (char *)pv + pBlock->cb; 782 790 # endif 783 int rc = RTMemProtect(pvEFence, RTALLOC_EFENCE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);791 int rc = RTMemProtect(pvEFence, cbFence, RTMEM_PROT_READ | RTMEM_PROT_WRITE); 784 792 if (RT_SUCCESS(rc)) 785 RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, PAGE_SIZE) + RTALLOC_EFENCE_SIZE);793 RTMemPageFree(pvBlock, RT_ALIGN_Z(pBlock->cbAligned, cbPage) + cbFence); 786 794 else 787 rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, RTALLOC_EFENCE_SIZE, rc);795 rtmemComplain(pszOp, "RTMemProtect(%p, %#x, RTMEM_PROT_READ | RTMEM_PROT_WRITE) -> %d\n", pvEFence, cbFence, rc); 788 796 rtmemBlockFree(pBlock); 789 797 … … 803 811 if (enmType == RTMEMTYPE_RTMEMFREEZ) 804 812 RT_BZERO(pv, cbUser); 805 int rc = RTMemProtect((void *)((uintptr_t)pv & ~ (uintptr_t)PAGE_OFFSET_MASK), PAGE_SIZE, RTMEM_PROT_NONE);813 int rc = RTMemProtect((void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), cbPage, RTMEM_PROT_NONE); 806 814 if (RT_FAILURE(rc)) 807 rtmemComplain(pszOp, "RTMemProtect(%p, PAGE_SIZE, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK), rc);815 rtmemComplain(pszOp, "RTMemProtect(%p, cbPage, RTMEM_PROT_NONE) -> %d\n", (void *)((uintptr_t)pv & ~RTSystemGetPageOffsetMask()), rc); 808 816 #endif /* !RTALLOC_EFENCE_TRACE */ 809 817 }
Note:
See TracChangeset
for help on using the changeset viewer.