|
10 | 10 | #ifdef NTDDI_WIN10_RS2
|
11 | 11 | #if (NTDDI_VERSION >= NTDDI_WIN10_RS2)
|
12 | 12 | #define USEFILEMAP2 1
|
| 13 | +#define USEVIRTUALUNLOCKEX 1 |
13 | 14 | #endif
|
14 | 15 | #endif
|
15 | 16 |
|
16 | 17 | namespace Memory
|
17 | 18 | {
|
| 19 | + |
| 20 | +void UnlockMemory(HANDLE process, LPVOID address, SIZE_T size) |
| 21 | +{ |
| 22 | +#if USEVIRTUALUNLOCKEX |
| 23 | + VirtualUnlockEx(process, address, size); |
| 24 | +#else |
| 25 | + NtdllLibrary::Instance->UnlockVirtualMemory(process, &address, &size, NtdllLibrary::MAP_PROCESS); |
| 26 | +#endif |
| 27 | +} |
18 | 28 |
|
19 | 29 | void CloseSectionHandle(HANDLE handle)
|
20 | 30 | {
|
@@ -593,12 +603,6 @@ SectionAllocWrapper::AllocPages(LPVOID requestAddress, size_t pageCount, DWORD a
|
593 | 603 | return nullptr;
|
594 | 604 | }
|
595 | 605 | address = requestAddress;
|
596 |
| - |
597 |
| - if ((allocationType & MEM_COMMIT) == MEM_COMMIT) |
598 |
| - { |
599 |
| - const DWORD allocProtectFlags = AutoSystemInfo::Data.IsCFGEnabled() ? PAGE_EXECUTE_RO_TARGETS_INVALID : PAGE_EXECUTE_READ; |
600 |
| - address = VirtualAllocEx(this->process, address, dwSize, MEM_COMMIT, allocProtectFlags); |
601 |
| - } |
602 | 606 | }
|
603 | 607 |
|
604 | 608 | return address;
|
@@ -661,11 +665,7 @@ BOOL SectionAllocWrapper::Free(LPVOID lpAddress, size_t dwSize, DWORD dwFreeType
|
661 | 665 | ZeroMemory(localAddr, AutoSystemInfo::PageSize);
|
662 | 666 | FreeLocal(localAddr);
|
663 | 667 | }
|
664 |
| - DWORD oldFlags = NULL; |
665 |
| - if (!VirtualProtectEx(this->process, lpAddress, dwSize, PAGE_NOACCESS, &oldFlags)) |
666 |
| - { |
667 |
| - return FALSE; |
668 |
| - } |
| 668 | + UnlockMemory(this->process, lpAddress, dwSize); |
669 | 669 | }
|
670 | 670 |
|
671 | 671 | return TRUE;
|
@@ -924,37 +924,15 @@ LPVOID PreReservedSectionAllocWrapper::AllocPages(LPVOID lpAddress, DECLSPEC_GUA
|
924 | 924 | AssertMsg(freeSegmentsBVIndex < PreReservedAllocationSegmentCount, "Invalid BitVector index calculation?");
|
925 | 925 | AssertMsg(dwSize % AutoSystemInfo::PageSize == 0, "COMMIT is managed at AutoSystemInfo::PageSize granularity");
|
926 | 926 |
|
927 |
| - char * allocatedAddress = nullptr; |
928 |
| - |
929 |
| - if ((allocationType & MEM_COMMIT) != 0) |
930 |
| - { |
931 |
| -#if defined(ENABLE_JIT_CLAMP) |
932 |
| - AutoEnableDynamicCodeGen enableCodeGen; |
933 |
| -#endif |
934 |
| - |
935 |
| - const DWORD allocProtectFlags = AutoSystemInfo::Data.IsCFGEnabled() ? PAGE_EXECUTE_RO_TARGETS_INVALID : PAGE_EXECUTE_READ; |
936 |
| - allocatedAddress = (char *)VirtualAllocEx(this->process, addressToReserve, dwSize, MEM_COMMIT, allocProtectFlags); |
937 |
| - if (allocatedAddress == nullptr) |
938 |
| - { |
939 |
| - MemoryOperationLastError::RecordLastError(); |
940 |
| - } |
941 |
| - } |
942 |
| - else |
943 |
| - { |
944 |
| - // Just return the uncommitted address if we didn't ask to commit it. |
945 |
| - allocatedAddress = addressToReserve; |
946 |
| - } |
947 |
| - |
948 | 927 | // Keep track of the committed pages within the preReserved Memory Region
|
949 |
| - if (lpAddress == nullptr && allocatedAddress != nullptr) |
| 928 | + if (lpAddress == nullptr) |
950 | 929 | {
|
951 |
| - Assert(allocatedAddress == addressToReserve); |
952 | 930 | Assert(requestedNumOfSegments != 0);
|
953 | 931 | freeSegments.ClearRange(freeSegmentsBVIndex, static_cast<uint>(requestedNumOfSegments));
|
954 |
| - } |
| 932 | + } |
955 | 933 |
|
956 |
| - PreReservedHeapTrace(_u("MEM_COMMIT: StartAddress: 0x%p of size: 0x%x * 0x%x bytes \n"), allocatedAddress, requestedNumOfSegments, AutoSystemInfo::Data.GetAllocationGranularityPageSize()); |
957 |
| - return allocatedAddress; |
| 934 | + PreReservedHeapTrace(_u("MEM_COMMIT: StartAddress: 0x%p of size: 0x%x * 0x%x bytes \n"), addressToReserve, requestedNumOfSegments, AutoSystemInfo::Data.GetAllocationGranularityPageSize()); |
| 935 | + return addressToReserve; |
958 | 936 | }
|
959 | 937 | }
|
960 | 938 |
|
@@ -989,11 +967,7 @@ PreReservedSectionAllocWrapper::Free(LPVOID lpAddress, size_t dwSize, DWORD dwFr
|
989 | 967 | FreeLocal(localAddr);
|
990 | 968 | }
|
991 | 969 |
|
992 |
| - DWORD oldFlags = NULL; |
993 |
| - if(!VirtualProtectEx(this->process, lpAddress, dwSize, PAGE_NOACCESS, &oldFlags)) |
994 |
| - { |
995 |
| - return FALSE; |
996 |
| - } |
| 970 | + UnlockMemory(this->process, lpAddress, dwSize); |
997 | 971 |
|
998 | 972 | size_t requestedNumOfSegments = dwSize / AutoSystemInfo::Data.GetAllocationGranularityPageSize();
|
999 | 973 | Assert(requestedNumOfSegments <= MAXUINT32);
|
|
0 commit comments