mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-29 16:51:20 +00:00
memory: avoid redundant Windows view protection
This commit is contained in:
1 parent
038d2257de
commit
c30ac7c904
3 files changed
+49
-1
No files matched your search
@@ -3621,6 +3621,36 @@ uint64_t TestGuestBackingSize() {
|
|||||||
return g_guest_address_space->GetBackingSize();
|
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() {
|
bool TestGuestFreeRangeBounds() {
|
||||||
return GuestFreeRangeContains(0x10000, 0x20000, 0x18000, 0x4000) &&
|
return GuestFreeRangeContains(0x10000, 0x20000, 0x18000, 0x4000) &&
|
||||||
!GuestFreeRangeContains(0x10000, 0x20000, 0x40000, 0x4000) &&
|
!GuestFreeRangeContains(0x10000, 0x20000, 0x40000, 0x4000) &&
|
||||||
|
|||||||
@@ -597,7 +597,7 @@ private:
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ApplyProtection(ptr, size, mode)) {
|
if (mode == VirtualMemory::Mode::NoAccess && !ApplyProtection(ptr, size, mode)) {
|
||||||
UnmapViewOfFile2(GetCurrentProcess(), ptr, MEM_PRESERVE_PLACEHOLDER);
|
UnmapViewOfFile2(GetCurrentProcess(), ptr, MEM_PRESERVE_PLACEHOLDER);
|
||||||
SetFailure(failure_reason, FailureReason::ProtectFailed);
|
SetFailure(failure_reason, FailureReason::ProtectFailed);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@@ -60,6 +60,12 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
|
||||||
|
namespace Libs::LibKernel::Memory {
|
||||||
|
bool TestWindowsBackingViewModes();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Libs::Fiber {
|
namespace Libs::Fiber {
|
||||||
struct FiberObject;
|
struct FiberObject;
|
||||||
struct FiberOptParam;
|
struct FiberOptParam;
|
||||||
@@ -1355,6 +1361,15 @@ void TestPartialUnmapPreservesHostPermissions() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
|
||||||
|
void TestWindowsBackingViewPermissions() {
|
||||||
|
const char* test = "WindowsBackingViewPermissions";
|
||||||
|
Check(test, Libs::LibKernel::Memory::TestWindowsBackingViewModes(),
|
||||||
|
"backing view permissions differ from the requested mode");
|
||||||
|
std::printf("[host] %-48s ok\n", test);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void TestDirectMapValidationBeforeOwnerMutation() {
|
void TestDirectMapValidationBeforeOwnerMutation() {
|
||||||
const char* test = "DirectMapValidationBeforeOwnerMutation";
|
const char* test = "DirectMapValidationBeforeOwnerMutation";
|
||||||
int64_t invalid = -1;
|
int64_t invalid = -1;
|
||||||
@@ -4248,6 +4263,9 @@ int main(int argc, char** argv) {
|
|||||||
RunTest(TestDirectPartialProtectUnmapPreservesNeighbors);
|
RunTest(TestDirectPartialProtectUnmapPreservesNeighbors);
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
RunTest(TestPartialUnmapPreservesHostPermissions);
|
RunTest(TestPartialUnmapPreservesHostPermissions);
|
||||||
|
#endif
|
||||||
|
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
|
||||||
|
RunTest(TestWindowsBackingViewPermissions);
|
||||||
#endif
|
#endif
|
||||||
RunTest(TestDirectMapValidationBeforeOwnerMutation);
|
RunTest(TestDirectMapValidationBeforeOwnerMutation);
|
||||||
RunTest(TestDirectReleaseRollbackRestoresOwnerMapping);
|
RunTest(TestDirectReleaseRollbackRestoresOwnerMapping);
|
||||||
|
|||||||
Reference in new issue
Block a user