memory: avoid redundant Windows view protection

This commit is contained in:
nmzik
2026-10-07 22:27:35 +04:00
parent 038d2257de
commit c30ac7c904
3 changed files with 49 additions and 1 deletions
+30
View File
@@ -3621,6 +3621,36 @@ uint64_t TestGuestBackingSize() {
return g_guest_address_space->GetBackingSize();
}
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
bool TestWindowsBackingViewModes() {
constexpr uint64_t size = 0x10000;
GuestBackingStore backing(size);
void* placeholder = VirtualAlloc2(GetCurrentProcess(), nullptr, size,
MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS,
nullptr, 0);
if (placeholder == nullptr) {
return false;
}
const auto address = reinterpret_cast<uint64_t>(placeholder);
bool valid = true;
for (auto [mode, protection]: std::array {
std::pair {VirtualMemory::Mode::NoAccess, DWORD {PAGE_NOACCESS}},
std::pair {VirtualMemory::Mode::Read, DWORD {PAGE_READONLY}},
std::pair {VirtualMemory::Mode::ReadWrite, DWORD {PAGE_READWRITE}},
std::pair {VirtualMemory::Mode::ExecuteReadWrite, DWORD {PAGE_EXECUTE_READWRITE}}}) {
if (!backing.MapFixed(address, size, 0, mode)) {
valid = false;
break;
}
MEMORY_BASIC_INFORMATION info {};
valid = VirtualQuery(placeholder, &info, sizeof(info)) != 0 &&
info.Type == MEM_MAPPED && info.Protect == protection && valid;
EXIT_IF(!backing.Unmap(address, size));
}
return VirtualFree(placeholder, 0, MEM_RELEASE) != 0 && valid;
}
#endif
bool TestGuestFreeRangeBounds() {
return GuestFreeRangeContains(0x10000, 0x20000, 0x18000, 0x4000) &&
!GuestFreeRangeContains(0x10000, 0x20000, 0x40000, 0x4000) &&
+1 -1
View File
@@ -597,7 +597,7 @@ private:
return nullptr;
}
if (!ApplyProtection(ptr, size, mode)) {
if (mode == VirtualMemory::Mode::NoAccess && !ApplyProtection(ptr, size, mode)) {
UnmapViewOfFile2(GetCurrentProcess(), ptr, MEM_PRESERVE_PLACEHOLDER);
SetFailure(failure_reason, FailureReason::ProtectFailed);
return nullptr;