Changeset 101142 in vbox
- Timestamp:
- Sep 18, 2023 11:12:16 AM (16 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/mem.h
r98103 r101142 507 507 /** Try prevent the memory from ending up in a dump/core. */ 508 508 #define RTMEMPAGEALLOC_F_ADVISE_NO_DUMP RT_BIT_32(2) 509 /** Allocate pages that are readable, writeable and executable. 510 * @note This may fail on some platforms for security policy reasons. */ 511 #define RTMEMPAGEALLOC_F_EXECUTABLE RT_BIT_32(3) 509 512 /** Valid bit mask. */ 510 #define RTMEMPAGEALLOC_F_VALID_MASK UINT32_C(0x0000000 7)513 #define RTMEMPAGEALLOC_F_VALID_MASK UINT32_C(0x0000000f) 511 514 /** @} */ 512 515 -
trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-heap-posix.cpp
r99779 r101142 550 550 551 551 /** 552 * Allocates one or more pages off the heap.552 * Frees an allocation. 553 553 * 554 554 * @returns IPRT status code. 555 * @retval VERR_NOT_FOUND if pv isn't within any of the memory blocks in the 556 * heap. 557 * @retval VERR_INVALID_POINTER if the given memory range isn't exactly one 558 * allocation block. 555 559 * @param pHeap The page heap. 556 560 * @param pv Pointer to what RTHeapPageAlloc returned. … … 652 656 } 653 657 else 654 rc = VERR_ INVALID_POINTER;658 rc = VERR_NOT_FOUND; /* Distinct return code for this so rtMemPagePosixFree and others can try alternative heaps. */ 655 659 656 660 RTCritSectLeave(&pHeap->CritSect); … … 737 741 * Free memory allocated by rtMemPagePosixAlloc. 738 742 * 739 * @param pv The address of the memory to free. 740 * @param cb The size. 741 * @param pHeap The heap. 742 */ 743 static void rtMemPagePosixFree(void *pv, size_t cb, PRTHEAPPAGE pHeap) 743 * @param pv The address of the memory to free. 744 * @param cb The size. 745 * @param pHeap1 The most probable heap. 746 * @param pHeap2 The less probable heap. 747 */ 748 static void rtMemPagePosixFree(void *pv, size_t cb, PRTHEAPPAGE pHeap1, PRTHEAPPAGE pHeap2) 744 749 { 745 750 /* … … 763 768 else 764 769 { 765 int rc = RTHeapPageFree(pHeap, pv, cb >> PAGE_SHIFT); 770 int rc = RTHeapPageFree(pHeap1, pv, cb >> PAGE_SHIFT); 771 if (rc == VERR_NOT_FOUND) 772 rc = RTHeapPageFree(pHeap2, pv, cb >> PAGE_SHIFT); 766 773 AssertRC(rc); 767 774 } … … 787 794 { 788 795 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL); 789 return rtMemPagePosixAlloc(cb, pszTag, fFlags, &g_MemPagePosixHeap); 796 return rtMemPagePosixAlloc(cb, pszTag, fFlags, 797 !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? &g_MemPagePosixHeap : &g_MemExecPosixHeap); 790 798 } 791 799 … … 793 801 RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF 794 802 { 795 r eturn rtMemPagePosixFree(pv, cb, &g_MemPagePosixHeap);796 } 797 803 rtMemPagePosixFree(pv, cb, &g_MemPagePosixHeap, &g_MemExecPosixHeap); 804 } 805 -
trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-posix.cpp
r100310 r101142 173 173 { 174 174 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL); 175 return rtMemPagePosixAlloc(cb, pszTag, fFlags, 0);175 return rtMemPagePosixAlloc(cb, pszTag, fFlags, !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? 0 : PROT_EXEC); 176 176 } 177 177 -
trunk/src/VBox/Runtime/r3/win/alloc-win.cpp
r98103 r101142 56 56 57 57 58 /** @todo merge the page alloc code with the heap-based mmap stuff as 59 * _aligned_malloc just isn't well suited for exec. */ 60 61 58 62 RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF 59 63 { … … 77 81 78 82 #ifdef USE_VIRTUAL_ALLOC 79 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE); 83 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, 84 !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? PAGE_READWRITE : PAGE_EXECUTE_READWRITE); 80 85 #else 81 86 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE); 82 87 #endif 83 88 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL); 89 90 #ifdef USE_VIRTUAL_ALLOC 91 if (fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) 92 { 93 DWORD fIgn = 0; 94 BOOL const fRc = VirtualProtect(pv, cbAligned, PAGE_EXECUTE_READWRITE, &fIgn); 95 AssertMsgReturnStmt(fRc, ("%p LB %#zx: %#u\n", pv, cbAligned, GetLastError()), _aligned_free(pv), NULL); 96 } 97 #endif 84 98 85 99 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED) … … 127 141 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError())); 128 142 #else 143 /** @todo The exec version of this doesn't really work well... */ 144 MEMORY_BASIC_INFORMATION MemInfo = { NULL } 145 SIZE_T cbRet = VirtualQuery(pv, &MemInfo, cb); 146 Assert(cbRet > 0) 147 if (cbRet >= 0 && MemInfo.Protect == PAGE_EXECUTE_READWRITE) 148 { 149 DWORD fIgn = 0; 150 BOOL const fRc = VirtualProtect(pv, cb, PAGE_READWRITE, &fIgn); 151 Assert(fRc); RT_NOREF(fRc); 152 } 129 153 _aligned_free(pv); 130 154 #endif
Note:
See TracChangeset
for help on using the changeset viewer.