From 962fc34e5a76fa8e10b880d3bbe545e631dc5ddd Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Tue, 29 Sep 2026 18:52:54 +0200 Subject: [PATCH] fix(libkernel): unmap ranges that span several mappings As on FreeBSD, a munmap range covering more than one registered allocation (or gaps between them) is unmapped allocation by allocation instead of throwing "unmap crosses allocation boundaries". GTA V unmaps such ranges in its Story Mode prologue. Co-Authored-By: Claude Opus 5.5 (1M context) Co-Authored-By: Claude Sonnet 5 --- .../tests/GuestAllocations.cpp | 12 +++--- .../prx/libc/include/GuestAllocations.hpp | 4 +- core/libs/prx/libc/src/GuestAllocations.cpp | 42 +++++++++++++------ .../libkernel/DirectMemory/DirectMemory.cpp | 11 ++--- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp b/core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp index a9e0e5e99..4d3f215aa 100644 --- a/core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp @@ -57,21 +57,21 @@ void RunGuestAllocationTests() { const auto lease = GuestAllocations::GuestAllocationsAcquire_nid_postfix(); Require(lease.size() == 3 && lease[0]->bytes == 32 && !lease[1]->writable && lease[2]->bytes == 64, "partial protection did not split the mapping"); GuestAllocations::Mutation mutation; - reject([&] { mutation.Unmap(mapping.data() + 32, 32, [](const void*, bool) {}); }); + reject([&] { mutation.Unmap(mapping.data() + 32, 32, [](const void*, std::size_t, const void*, bool) {}); }); } { GuestAllocations::Mutation mutation; bool applied = false; - mutation.Unmap(mapping.data() + 32, 32, [&](const void* allocation, bool last) { + mutation.Unmap(mapping.data() + 32, 32, [&](const void*, std::size_t, const void* allocation, bool last) { Require(allocation == mapping.data() && !last, "partial unmap released the allocation"); applied = true; }); Require(applied, "partial unmap callback was not called"); reject([&] { mutation.Protect(mapping.data(), mapping.size(), true, true, [] {}); }); - mutation.Unmap(mapping.data(), 32, [&](const void* allocation, bool last) { + mutation.Unmap(mapping.data(), 32, [&](const void*, std::size_t, const void* allocation, bool last) { Require(allocation == mapping.data() && !last, "first fragment released remaining mapping"); }); - mutation.Unmap(mapping.data() + 64, 64, [&](const void* allocation, bool last) { + mutation.Unmap(mapping.data() + 64, 64, [&](const void*, std::size_t, const void* allocation, bool last) { Require(allocation == mapping.data() && last, "last fragment did not release the original allocation"); }); } @@ -102,7 +102,7 @@ void RunGuestAllocationTests() { reject([&] { mutation.Find(reinterpret_cast(allocationAddress)); }); reject([&] { mutation.Remove(reinterpret_cast(allocationAddress)); }); bool applied = false; - reject([&] { mutation.Unmap(&imageProbe, 1, [&](const void*, bool) { applied = true; }); }); + reject([&] { mutation.Unmap(&imageProbe, 1, [&](const void*, std::size_t, const void*, bool) { applied = true; }); }); Require(!applied, "image memory was unmapped"); reject([&] { mutation.Protect(&imageProbe, 1, true, false, [] { throw std::runtime_error("host protection failure"); }); }); } @@ -111,7 +111,7 @@ void RunGuestAllocationTests() { GuestAllocations::Mutation mutation; mutation.Protect(&imageProbe, 1, true, true, [] {}); bool applied = false; - reject([&] { mutation.Unmap(&imageProbe, 1, [&](const void*, bool) { applied = true; }); }); + reject([&] { mutation.Unmap(&imageProbe, 1, [&](const void*, std::size_t, const void*, bool) { applied = true; }); }); Require(!applied, "split image memory became releasable"); } #endif diff --git a/core/libs/prx/libc/include/GuestAllocations.hpp b/core/libs/prx/libc/include/GuestAllocations.hpp index c58dfc3b5..a292f5d6e 100644 --- a/core/libs/prx/libc/include/GuestAllocations.hpp +++ b/core/libs/prx/libc/include/GuestAllocations.hpp @@ -33,7 +33,7 @@ bool GuestAllocationsCovers_nid_postfix(void* mutation, const void* pointer, std Range GuestAllocationsFind_nid_postfix(void* mutation, const void* pointer); void GuestAllocationsRemove_nid_postfix(void* mutation, const void* pointer); void GuestAllocationsProtect_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, bool readable, bool writable, const std::function& apply); -void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function& apply); +void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function& apply); Lease GuestAllocationsAcquire_nid_postfix(); // Changes whenever a mutation ends, so callers can cache facts about guest mappings between changes. std::uint64_t GuestAllocationsGeneration_nid_postfix(); @@ -62,7 +62,7 @@ public: bool Covers(const void* pointer, std::size_t bytes) const { return GuestAllocationsCovers_nid_postfix(handle, pointer, bytes); } Range Find(const void* pointer) const { return GuestAllocationsFind_nid_postfix(handle, pointer); } void Remove(const void* pointer) { GuestAllocationsRemove_nid_postfix(handle, pointer); } - void Unmap(const void* pointer, std::size_t bytes, const std::function& apply) { GuestAllocationsUnmap_nid_postfix(handle, pointer, bytes, apply); } + void Unmap(const void* pointer, std::size_t bytes, const std::function& apply) { GuestAllocationsUnmap_nid_postfix(handle, pointer, bytes, apply); } void Protect(const void* pointer, std::size_t bytes, bool readable, bool writable, const std::function& apply) { GuestAllocationsProtect_nid_postfix(handle, pointer, bytes, readable, writable, apply); } private: diff --git a/core/libs/prx/libc/src/GuestAllocations.cpp b/core/libs/prx/libc/src/GuestAllocations.cpp index 8686e99a5..64c246c2a 100644 --- a/core/libs/prx/libc/src/GuestAllocations.cpp +++ b/core/libs/prx/libc/src/GuestAllocations.cpp @@ -340,22 +340,38 @@ void GuestAllocationsProtect_nid_postfix(void* mutation, const void* pointer, st registry().ranges.swap(replacement); } -void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function& apply) { +void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function& apply) { GuestAllocationsRequireUnpinned_nid_postfix(mutation, pointer, bytes); recordChange(mutation, pointer, bytes); - const auto address = reinterpret_cast(pointer); - const auto found = registry().ranges.upper_bound(address); - require(found != registry().ranges.begin(), "unmap address is not registered"); - const auto& range = *std::prev(found)->second; - require(range.releasable, "guest image memory cannot be unmapped"); - require(address >= range.address && address - range.allocationAddress <= range.allocationBytes && bytes <= range.allocationBytes - (address - range.allocationAddress), "unmap crosses allocation boundaries"); - auto replacement = replaceRange(pointer, bytes, true, false, false); - bool last = true; - for (const auto& [base, entry] : replacement) { - if (entry->allocationAddress == range.allocationAddress) last = false; + const auto end = reinterpret_cast(pointer) + bytes; + auto cursor = reinterpret_cast(pointer); + bool any = false; + while (cursor < end) { + const auto found = registry().ranges.upper_bound(cursor); + const Range* containing = nullptr; + if (found != registry().ranges.begin()) { + const auto& candidate = *std::prev(found)->second; + if (cursor < candidate.allocationAddress + candidate.allocationBytes) containing = &candidate; + } + if (containing == nullptr) { + if (found == registry().ranges.end() || found->first >= end) break; + cursor = found->first; + continue; + } + const auto range = *containing; + require(range.releasable, "guest image memory cannot be unmapped"); + const auto pieceEnd = std::min(end, range.allocationAddress + range.allocationBytes); + auto replacement = replaceRange(reinterpret_cast(cursor), pieceEnd - cursor, true, false, false); + bool last = true; + for (const auto& [base, entry] : replacement) { + if (entry->allocationAddress == range.allocationAddress) last = false; + } + apply(reinterpret_cast(cursor), pieceEnd - cursor, reinterpret_cast(range.allocationAddress), last); + registry().ranges.swap(replacement); + any = true; + cursor = pieceEnd; } - apply(reinterpret_cast(range.allocationAddress), last); - registry().ranges.swap(replacement); + require(any, "unmap address is not registered"); } Lease GuestAllocationsAcquire_nid_postfix() { diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp index a1440a0f5..080952c65 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp @@ -543,15 +543,16 @@ int DoMunmap(void* addr, size_t len) { Trace("unmap %p+0x%zx", addr, len); if (len == 0 || (len & (PS5_PAGE_SIZE - 1)) != 0 || !addr) return SCE_KERNEL_ERROR_EINVAL; GuestAllocations::Mutation mutation; - mutation.Unmap(addr, len, [&](const void* allocation, bool last) { + mutation.Unmap(addr, len, [&](const void* piece, std::size_t pieceBytes, const void* allocation, bool last) { + auto* pieceAddress = const_cast(piece); std::lock_guard lock(g_directLock); - EraseMappings(reinterpret_cast(addr), reinterpret_cast(addr) + len); + EraseMappings(reinterpret_cast(piece), reinterpret_cast(piece) + pieceBytes); #if defined(__linux__) - Unmap(addr, len); + Unmap(pieceAddress, pieceBytes); #else - if (KernelArena::Get().Contains(addr, len)) munmap(addr, len); + if (KernelArena::Get().Contains(pieceAddress, pieceBytes)) munmap(pieceAddress, pieceBytes); else if (last) munmap_release(const_cast(allocation)); - else munmap(addr, len); + else munmap(pieceAddress, pieceBytes); #endif }); RecordProtection(addr, len, -1);