From f4be8f435d15a1e5a549fd754893640da86387a5 Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Wed, 30 Sep 2026 19:14:22 +0200 Subject: [PATCH] fix(agc): throw when guest memory can be neither imported, aliased nor copied exactly Co-Authored-By: Claude Opus 5.5 (1M context) --- .../Graphics/src/GuestBufferMemory.cpp | 27 ++++++++++++------- .../libs/prx/libc/include/WindowsMappings.hpp | 25 ++++++++++++----- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp index aaaabca33..2eec24da3 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -279,25 +279,34 @@ const HostImport* importAllocation(const Context& context, HostImports& state, s HostImport entry{base, bytes, VK_NULL_HANDLE, VK_NULL_HANDLE, 0}; #ifdef _WIN32 // Drivers pin imported pages, so every page must be committed and accessible. - bool privateWritable = true; - bool mapped = false; + bool writable = true; + bool readOnly = true; + MEMORY_BASIC_INFORMATION refused{}; for (std::uint64_t cursor = base; cursor < base + bytes;) { MEMORY_BASIC_INFORMATION info{}; if (VirtualQuery(reinterpret_cast(cursor), &info, sizeof(info)) == 0 || info.State != MEM_COMMIT || (info.Protect & (PAGE_NOACCESS | PAGE_GUARD)) != 0) { state.failed.insert(base); return nullptr; } - if (info.Type == MEM_MAPPED) mapped = true; const auto protection = info.Protect & 0xffu; - if (info.Type != MEM_PRIVATE || (protection != PAGE_READWRITE && protection != PAGE_EXECUTE_READWRITE)) privateWritable = false; + const bool pageWritable = (info.Type == MEM_PRIVATE || info.Type == MEM_IMAGE) && (protection == PAGE_READWRITE || protection == PAGE_EXECUTE_READWRITE || protection == PAGE_WRITECOPY || protection == PAGE_EXECUTE_WRITECOPY); + const bool pageReadOnly = info.Type != MEM_MAPPED && (protection == PAGE_READONLY || protection == PAGE_EXECUTE_READ); + if (!pageReadOnly) readOnly = false; + if (info.Type != MEM_MAPPED && !pageWritable && !pageReadOnly && refused.BaseAddress == nullptr) refused = info; + if (!pageWritable) writable = false; cursor = reinterpret_cast(info.BaseAddress) + info.RegionSize; } - if (!privateWritable) { - entry.alias = mapped ? GuestArena::GuestArenaMapAlias_nid_postfix(static_cast(base), static_cast(bytes)) : nullptr; - if (entry.alias == nullptr) { - state.failed.insert(base); - return nullptr; + if (!writable && readOnly) { + state.failed.insert(base); + return nullptr; + } + if (!writable) { + if (refused.BaseAddress != nullptr) { + char text[256]; + std::snprintf(text, sizeof(text), "AGC graphics: host import of 0x%llx+0x%llx: memory at 0x%llx (type 0x%lx, protection 0x%lx) is neither read-write, read-only nor a shared mapping", static_cast(base), static_cast(bytes), reinterpret_cast(refused.BaseAddress), refused.Type, refused.Protect); + throw std::runtime_error(text); } + entry.alias = GuestArena::GuestArenaMapAlias_nid_postfix(static_cast(base), static_cast(bytes)); } #endif decideImportWatch(context, state); diff --git a/core/libs/prx/libc/include/WindowsMappings.hpp b/core/libs/prx/libc/include/WindowsMappings.hpp index 153e6c749..c5f29b1a9 100644 --- a/core/libs/prx/libc/include/WindowsMappings.hpp +++ b/core/libs/prx/libc/include/WindowsMappings.hpp @@ -4,6 +4,7 @@ #ifdef _WIN32 #include #include +#include #include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #include namespace GuestArena { @@ -115,19 +117,30 @@ public: void* MapAlias(std::uintptr_t address, std::size_t bytes) { std::lock_guard lock(mutex); - if (address % pageBytes != 0 || bytes % pageBytes != 0 || bytes == 0) return nullptr; + const auto refuse = [&](const char* reason) { + char text[192]; + std::snprintf(text, sizeof(text), "read-write alias of shared guest memory 0x%llx+0x%llx: %s", static_cast(address), static_cast(bytes), reason); + return std::runtime_error(text); + }; + if (address % pageBytes != 0 || bytes % pageBytes != 0 || bytes == 0) throw refuse("the range is not made of whole shared pages"); auto view = views.find(address); - if (view == views.end()) return nullptr; + if (view == views.end()) throw refuse("the range does not start at a shared view"); const auto section = view->second.section; const auto offset = view->second.offset; SYSTEM_INFO system{}; GetSystemInfo(&system); - if (offset % system.dwAllocationGranularity != 0) return nullptr; + if (offset % system.dwAllocationGranularity != 0) throw refuse("the section offset is not a multiple of the allocation granularity"); for (std::size_t done = 0; done < bytes; done += pageBytes, ++view) { - if (view == views.end() || view->first != address + done || view->second.offset != offset + done) return nullptr; - if (view->second.section != section && !sameSection(view->second.section->handle, section->handle)) return nullptr; + if (view == views.end() || view->first != address + done || view->second.offset != offset + done) throw refuse("the range is not one contiguous run of views of a section"); + if (view->second.section != section && !sameSection(view->second.section->handle, section->handle)) throw refuse("the range spans several sections"); } - return map(section->handle, GetCurrentProcess(), nullptr, offset, bytes, 0, PAGE_READWRITE, nullptr, 0); + void* alias = map(section->handle, GetCurrentProcess(), nullptr, offset, bytes, 0, PAGE_READWRITE, nullptr, 0); + if (alias == nullptr) { + char text[160]; + std::snprintf(text, sizeof(text), "MapViewOfFile3 of a read-write alias of shared guest memory 0x%llx+0x%llx", static_cast(address), static_cast(bytes)); + throw std::system_error(static_cast(GetLastError()), std::system_category(), text); + } + return alias; } void UnmapAlias(void* alias) {