Merge pull request #652 from Celegans12/fix/guest-arena-app-map-area

fix(libc): reach the application map area end with the guest arena
This commit is contained in:
Dean
2026-10-06 08:04:36 +13:00
committed by GitHub
3 changed files with 54 additions and 7 deletions
+20 -7
View File
@@ -21,7 +21,7 @@ namespace {
constexpr std::uintptr_t ArenaStart = 0x0000000200000000ull;
constexpr std::uintptr_t SystemReservedStart = 0x00000007FFFFC000ull;
constexpr std::uintptr_t SystemReservedEnd = 0x0000001000000000ull;
constexpr std::size_t ArenaSize = 0x0000007000000000ull;
constexpr std::uintptr_t ApplicationAreaEnd = 0x000000FC00000000ull;
std::uintptr_t alignUp(std::uintptr_t value, std::size_t alignment) {
return (value + alignment - 1) & ~(static_cast<std::uintptr_t>(alignment) - 1);
@@ -69,8 +69,11 @@ public:
std::lock_guard lock(_lock);
const auto start = reinterpret_cast<std::uintptr_t>(pointer);
const auto next = _used.lower_bound(start);
if ((next != _used.end() && next->first < start + bytes) || (next != _used.begin() && std::prev(next)->second > start))
throw std::runtime_error("fixed guest mapping overlaps a guest arena range");
if ((next != _used.end() && next->first < start + bytes) || (next != _used.begin() && std::prev(next)->second > start)) {
char message[160];
std::snprintf(message, sizeof(message), "fixed guest mapping 0x%llx+0x%zx overlaps %s", static_cast<unsigned long long>(start), bytes, OverlapsHostRegion(start, bytes) ? "a host region in the guest arena" : "a guest arena range");
throw std::runtime_error(message);
}
_used.emplace(start, start + bytes);
}
@@ -103,7 +106,7 @@ private:
SYSTEM_INFO system{};
GetSystemInfo(&system);
const std::uintptr_t granularity = system.dwAllocationGranularity;
const std::uintptr_t end = ArenaStart + ArenaSize;
const std::uintptr_t end = ApplicationAreaEnd;
for (std::uintptr_t cursor = ArenaStart; cursor < end;) {
MEMORY_BASIC_INFORMATION info{};
if (VirtualQuery(reinterpret_cast<const void*>(cursor), &info, sizeof(info)) == 0) throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "query the guest arena range");
@@ -197,18 +200,28 @@ bool GuestArenaHostRegionOverlaps_nid_postfix(std::uintptr_t address, std::size_
return Arena::Get().OverlapsHostRegion(address, bytes);
}
namespace {
std::invalid_argument OutsideArena(const char* operation, const void* pointer, std::size_t bytes) {
char message[128];
std::snprintf(message, sizeof(message), "%s 0x%llx+0x%zx outside the guest arena", operation, static_cast<unsigned long long>(reinterpret_cast<std::uintptr_t>(pointer)), bytes);
return std::invalid_argument(message);
}
}
void GuestArenaCommit_nid_postfix(void* pointer, std::size_t bytes, std::uint32_t protection, std::size_t granule) {
if (!Arena::Get().Contains(pointer, bytes)) throw std::invalid_argument("commit outside the guest arena");
if (!Arena::Get().Contains(pointer, bytes)) throw OutsideArena("commit", pointer, bytes);
WindowsMappings::Get().Commit(pointer, bytes, protection, granule, Arena::Get().WriteWatched());
}
void GuestArenaReset_nid_postfix(void* pointer, std::size_t bytes) {
if (!Arena::Get().Contains(pointer, bytes)) throw std::invalid_argument("reset outside the guest arena");
if (!Arena::Get().Contains(pointer, bytes)) throw OutsideArena("reset", pointer, bytes);
WindowsMappings::Get().Reset(pointer, bytes);
}
void GuestArenaMap_nid_postfix(void* pointer, std::size_t bytes, void* section, std::uint64_t offset, std::uint32_t protection) {
if (!Arena::Get().Contains(pointer, bytes)) throw std::invalid_argument("shared mapping outside the guest arena");
if (!Arena::Get().Contains(pointer, bytes)) throw OutsideArena("shared mapping", pointer, bytes);
WindowsMappings::Get().Map(pointer, bytes, section, offset, protection);
}
+33
View File
@@ -236,6 +236,38 @@ static void CheckNoOverwriteRejectsHostOccupiedMapping() {
GuestArena::GuestArenaReset_nid_postfix(target, page);
Require(sceKernelReleaseDirectMemory(phys, page) == 0);
}
static void CheckFixedMappingsReachTheApplicationAreaEnd() {
constexpr std::size_t page = 0x4000;
constexpr std::uintptr_t applicationAreaEnd = 0xFC00000000ull;
std::uintptr_t base = 0;
std::size_t size = 0;
GuestArena::GuestArenaRange_nid_postfix(&base, &size);
Require(base == 0x200000000ull && base + size == applicationAreaEnd);
std::int64_t phys = 0;
Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page * 2, 0, 0, &phys) == 0);
void* const requested = reinterpret_cast<void*>(applicationAreaEnd - page * 2);
void* mapped = requested;
Require(sceKernelMapDirectMemory(&mapped, page * 2, 3, 0x90, phys, 0) == 0);
Require(mapped == requested);
static_cast<volatile unsigned char*>(mapped)[page * 2 - 1] = 7;
void* alias = nullptr;
Require(sceKernelMapDirectMemory(&alias, page, 3, 0, phys + page, 0) == 0);
Require(static_cast<volatile unsigned char*>(alias)[page - 1] == 7);
VirtualQueryInfo info{};
Require(sceKernelVirtualQuery(mapped, 0, &info, sizeof(info)) == 0 && info.is_direct && info.end == applicationAreaEnd);
void* beyond = reinterpret_cast<void*>(applicationAreaEnd);
bool refused = false;
try {
refused = sceKernelMapDirectMemory(&beyond, page, 3, 0x10, phys, 0) != 0;
} catch (const std::exception&) {
refused = true;
}
Require(refused && beyond == reinterpret_cast<void*>(applicationAreaEnd));
Require(sceKernelMunmap(alias, page) == 0);
Require(sceKernelMunmap(mapped, page * 2) == 0);
Require(sceKernelReleaseDirectMemory(phys, page * 2) == 0);
}
#endif
#if defined(__linux__)
@@ -674,6 +706,7 @@ int main() {
CheckHeapAfterMappingReuse();
#ifdef _WIN32
CheckNoOverwriteRejectsHostOccupiedMapping();
CheckFixedMappingsReachTheApplicationAreaEnd();
#endif
CheckSharedWriteTracking();
CheckReadsIntoSharedWriteTracking();
+1
View File
@@ -46,6 +46,7 @@ Throughout the project, every function at every stage either **does exactly what
- [sceKernelSyncOnAddressWait](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the only known caller passes a null timeout and a name string as the fourth argument; the timeout is assumed to point to microseconds like the other kernel waits, and the name is ignored
- [sceKernelSyncOnAddressWait32 / sceKernelSyncOnAddressWait64](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - libc's `__std_atomic_wait_direct_32/64` pass a microsecond timeout or null and ignore the result; the by-value comparand, return values and Wait64's 8-byte alignment are assumed
- [sceKernelMapNamedFlexibleMemoryInternal](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - flag 0x8000 unknown; only the sceKernelMapNamedFlexibleMemory flags are accepted
- [Guest arena](../../core/libs/prx/libc/src/GuestArena.cpp) (libc) - the application map area is assumed to end at 0xFC_0000_0000, as `SCE_KERNEL_APP_MAP_AREA_END_ADDR` does on the PS4. On Windows, host allocations made in the arena before libc loads stay, and a fixed guest mapping over one throws
- [sceLibcInternalBacktraceForGame](../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libSceLibcInternal, implemented in libc) - unknown signature
- [sceLibcInternalHeapErrorReportForGame](../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libSceLibcInternal, implemented in libc) - unknown signature
- [_sceLibcInternalThreadAtexit, _sceLibcInternalThreadDtors](../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - FreeBSD `__cxa_thread_atexit` semantics assumed; a null or non-image destructor and an allocation failure throw