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) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Michon
2026-09-29 18:52:54 +02:00
parent 0f6205aa36
commit 962fc34e5a
4 changed files with 43 additions and 26 deletions
@@ -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<void*>(allocationAddress)); });
reject([&] { mutation.Remove(reinterpret_cast<void*>(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
@@ -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<void()>& apply);
void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function<void(const void*, bool)>& apply);
void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function<void(const void*, std::size_t, const void*, bool)>& 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<void(const void*, bool)>& apply) { GuestAllocationsUnmap_nid_postfix(handle, pointer, bytes, apply); }
void Unmap(const void* pointer, std::size_t bytes, const std::function<void(const void*, std::size_t, const void*, bool)>& apply) { GuestAllocationsUnmap_nid_postfix(handle, pointer, bytes, apply); }
void Protect(const void* pointer, std::size_t bytes, bool readable, bool writable, const std::function<void()>& apply) { GuestAllocationsProtect_nid_postfix(handle, pointer, bytes, readable, writable, apply); }
private:
+29 -13
View File
@@ -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<void(const void*, bool)>& apply) {
void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function<void(const void*, std::size_t, const void*, bool)>& apply) {
GuestAllocationsRequireUnpinned_nid_postfix(mutation, pointer, bytes);
recordChange(mutation, pointer, bytes);
const auto address = reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(pointer) + bytes;
auto cursor = reinterpret_cast<std::uintptr_t>(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<std::uint64_t>(end, range.allocationAddress + range.allocationBytes);
auto replacement = replaceRange(reinterpret_cast<const void*>(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<const void*>(cursor), pieceEnd - cursor, reinterpret_cast<const void*>(range.allocationAddress), last);
registry().ranges.swap(replacement);
any = true;
cursor = pieceEnd;
}
apply(reinterpret_cast<const void*>(range.allocationAddress), last);
registry().ranges.swap(replacement);
require(any, "unmap address is not registered");
}
Lease GuestAllocationsAcquire_nid_postfix() {
@@ -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<void*>(piece);
std::lock_guard lock(g_directLock);
EraseMappings(reinterpret_cast<std::uintptr_t>(addr), reinterpret_cast<std::uintptr_t>(addr) + len);
EraseMappings(reinterpret_cast<std::uintptr_t>(piece), reinterpret_cast<std::uintptr_t>(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<void*>(allocation));
else munmap(addr, len);
else munmap(pieceAddress, pieceBytes);
#endif
});
RecordProtection(addr, len, -1);