mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(agc): throw when guest memory can be neither imported, aliased nor copied exactly
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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<const void*>(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<std::uint64_t>(info.BaseAddress) + info.RegionSize;
|
||||
}
|
||||
if (!privateWritable) {
|
||||
entry.alias = mapped ? GuestArena::GuestArenaMapAlias_nid_postfix(static_cast<std::uintptr_t>(base), static_cast<std::size_t>(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<unsigned long long>(base), static_cast<unsigned long long>(bytes), reinterpret_cast<unsigned long long>(refused.BaseAddress), refused.Type, refused.Protect);
|
||||
throw std::runtime_error(text);
|
||||
}
|
||||
entry.alias = GuestArena::GuestArenaMapAlias_nid_postfix(static_cast<std::uintptr_t>(base), static_cast<std::size_t>(bytes));
|
||||
}
|
||||
#endif
|
||||
decideImportWatch(context, state);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
@@ -11,6 +12,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
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<unsigned long long>(address), static_cast<unsigned long long>(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<unsigned long long>(address), static_cast<unsigned long long>(bytes));
|
||||
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), text);
|
||||
}
|
||||
return alias;
|
||||
}
|
||||
|
||||
void UnmapAlias(void* alias) {
|
||||
|
||||
Reference in New Issue
Block a user