From 35ae8742a965a5851d010089e30a866d52255db2 Mon Sep 17 00:00:00 2001 From: kjhq Date: Wed, 7 Oct 2026 10:41:01 +0000 Subject: [PATCH 1/2] feat(libkernel): implement POSIX mprotect mprotect changes the protection of guest memory through the same path as sceKernelMprotect and reports failures through errno: EINVAL for a null or overflowing range or a range the allocation tracker rejects, ENOMEM when allocation fails. A zero length succeeds. Co-Authored-By: Claude Opus 5.5 --- .../libkernel/DirectMemory/PosixMemory.cpp | 19 +++++++++++++++++++ core/libs/tests/GuestMemory.cpp | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp index e3d2f1796..bac216bbc 100644 --- a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp @@ -78,4 +78,23 @@ int APS5_VABI munmap_nid_postfix(void* address, std::size_t length) noexcept { } } +int APS5_VABI mprotect_nid_postfix(void* address, std::size_t length, int protection) noexcept { + const auto failed = [](int error) { + SetError(error); + return -1; + }; + const auto start = reinterpret_cast(address); + if (length == 0) return 0; + if (start == 0 || length > std::numeric_limits::max() - start) + return failed(GuestInvalid); + try { + if (DoMprotect(address, length, protection) != 0) return failed(GuestInvalid); + return 0; + } catch (const std::bad_alloc&) { + return failed(GuestNoMemory); + } catch (const std::exception&) { + return failed(GuestInvalid); + } +} + } diff --git a/core/libs/tests/GuestMemory.cpp b/core/libs/tests/GuestMemory.cpp index 27908100c..e400e7e0a 100644 --- a/core/libs/tests/GuestMemory.cpp +++ b/core/libs/tests/GuestMemory.cpp @@ -36,6 +36,7 @@ extern "C" { void* APS5_VABI mmap_nid_postfix(void*, std::size_t, int, int, int, std::int64_t) noexcept; int APS5_VABI munmap_nid_postfix(void*, std::size_t) noexcept; +int APS5_VABI mprotect_nid_postfix(void*, std::size_t, int) noexcept; int* APS5_VABI __error_nid_postfix(); int APS5_VABI sceKernelMapNamedFlexibleMemory(void**, std::size_t, int, int, const char*); int APS5_VABI sceKernelMapNamedFlexibleMemoryInternal(void**, std::size_t, int, int, const char*); @@ -1099,6 +1100,20 @@ int main() { const auto range = mutation.Find(memory); Require(range.bytes == page * 3 && range.readable && range.writable); } + Require(mprotect_nid_postfix(memory, 0, 1) == 0); + Require(mprotect_nid_postfix(nullptr, page, 1) == -1 && *__error_nid_postfix() == 22); + Require(mprotect_nid_postfix(memory + 1, 1, 1) == 0); + { + GuestAllocations::Mutation mutation; + const auto range = mutation.Find(memory); + Require(range.readable && !range.writable); + } + Require(memory[0] == 42); + Require(mprotect_nid_postfix(memory, page, 3) == 0); + { + GuestAllocations::Mutation mutation; + Require(mutation.Find(memory).writable); + } Require(munmap_nid_postfix(memory + 1, page) == -1 && *__error_nid_postfix() == 22); Require(munmap_nid_postfix(memory, 0) == -1 && *__error_nid_postfix() == 22); Require(memory[0] == 42); From 53a31b512ebe01e25aba09fc7f2812872e3340f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Thu, 8 Oct 2026 08:24:12 +0200 Subject: [PATCH 2/2] test(guest_memory): pin mprotect on an unmapped and a wrapping range Both return -1/EINVAL: FreeBSD kern_mprotect (sys/vm/vm_mmap.c) maps only PROTECTION_FAILURE, RESOURCE_SHORTAGE and OUT_OF_BOUNDS to their own errno, so KERN_INVALID_ADDRESS falls to EINVAL. Co-Authored-By: Claude --- core/libs/tests/GuestMemory.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/libs/tests/GuestMemory.cpp b/core/libs/tests/GuestMemory.cpp index e400e7e0a..bde88f82e 100644 --- a/core/libs/tests/GuestMemory.cpp +++ b/core/libs/tests/GuestMemory.cpp @@ -1123,6 +1123,11 @@ int main() { Require(memory[page * 2] == 73); Require(munmap_nid_postfix(memory + page * 2, page) == 0); Require(munmap_nid_postfix(memory, page) == -1); + *__error_nid_postfix() = 0; + Require(mprotect_nid_postfix(memory, page, 1) == -1 && *__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(mprotect_nid_postfix(memory, std::numeric_limits::max(), 1) == -1 && + *__error_nid_postfix() == 22); for (int protection : {0, 1, 3, 5}) { void* mapped = mmap_nid_postfix(memory, 1, protection, 0x1002, -1, 0); Require(mapped != failed);