mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-22 20:06:54 +02:00
memory: avoid redundant Windows view protection
This commit is contained in:
@@ -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) &&
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user