Repository navigation
fix(libkernel): release the pool expansion and return EFAULT when sceKernelMemoryPoolExpand cannot write its output - #19699
Conversation
…KernelMemoryPoolExpand cannot write its output sceKernelMemoryPoolExpand passed the guest phys_addr_out straight to DirectMemoryAlloc, which stores the address with a raw write after the range is already marked used, so an unwritable output crashed the host. It now allocates into a local and copies it out with GuestCopyOut_nid_no_patch, as sceKernelAllocateDirectMemory does since boykopovar#18815. When the copy fails it frees the range with DirectMemoryFree and returns SCE_KERNEL_ERROR_EFAULT, and the pool counts the expansion only after a successful copy. guest_direct_memory_for_app checks both cases, the block statistics and that the same range can be expanded again.
|
Linux run for this branch: 4734338 merged onto
Code: the allocation is released before |
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 47343385a1:
- Conventions:
tools/check_conventions.pypassed;git diff --checkclean. - Build:
guest_direct_memory_for_app_testsbuilt cleanly. - CTest (
guest_direct_memory_for_app):1/1 Test #4553: guest_direct_memory_for_app ...... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #4553: guest_direct_memory_for_app ......***Exception: SegFault 0.13 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified that sceKernelMemoryPoolExpand (MemoryPoolExport.cpp:97-100) preserves existing parameter validation, rejecting null phys_addr_out with SCE_KERNEL_ERROR_EINVAL prior to allocation.
- Verified that DirectMemoryAlloc allocates into a stack-local int64_t physical and returns ret immediately if allocation fails without modifying pool.expandedBytes or accessing phys_addr_out.
- Verified that copy-out failure in GuestCopyOut_nid_no_patch invokes DirectMemoryFree(physical, len), leaves pool.expandedBytes unmodified, and returns SCE_KERNEL_ERROR_EFAULT matching sceKernelAllocateDirectMemory in Export.cpp:276-288.
- Verified that successful copy-out acquires pool.mutex under std::lock_guard before incrementing pool.expandedBytes by len and returning 0.
- Verified cross-platform 64-bit portability (LP64 Linux vs LLP64 Windows) with explicit int64_t and size_t typing across all direct memory calls.
- Verified that GuestDirectMemoryForApp.cpp thoroughly exercises invalid output pointers, read-only memory pages, direct memory reclamation, and pool statistics without leaking memory or leaving pool state corrupted.
- Verified strict compliance with AGENTS.md rules: zero comments added in source code, technical debt accurately recorded in docs/dev/debt/libkernel.md, and no extraneous files added.
Looks Good To Me
What
sceKernelMemoryPoolExpand(core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp:95-107onmain1d1ff11) checks only for a nullphys_addr_out, the alignments and the range (:96-99), then passes the guest pointer straight toDirectMemoryAlloc(:100).DirectMemoryAlloc(core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp:316-322) forwards toPhysicalMemoryPool::Alloc, whose only pointer check isif (!physOut)(:61); under its mutex it marks the range used with_commit(:78) and then stores the address with*physOut = static_cast<int64_t>(cur);(:79). So an unmapped or read-only output crashes the host with an access violation after the range is already allocated, instead of failing.sceKernelAllocateDirectMemoryin the same library (core/libs/prx/libkernel/DirectMemory/Export.cpp:276-288, #18815), the only other production caller ofDirectMemoryAlloc, allocates into a local, copies it out withGuestCopyOut_nid_no_patch, and when the copy fails callsDirectMemoryFree(physical, len)and returnsSCE_KERNEL_ERROR_EFAULT(:282-287).sceKernelMemoryPoolGetBlockStatsin the same file returnsSCE_KERNEL_ERROR_EFAULTfor an unwritable output too (:120, #18818). Console behaviour for a bad output pointer is not measured; this is the repository's host-safety behaviour, as in #18815.sceKernelMemoryPoolExpandallocates into a localint64_t physicaland returns any non-zero result ofDirectMemoryAllocunchanged, so a failed allocation still leaves the output untouched. It then copies the address out withGuestCopyOut_nid_no_patch. If the copy fails, it frees the range withDirectMemoryFree(physical, len)and returnsSCE_KERNEL_ERROR_EFAULTwithout adding to the pool'sexpandedBytes; only after a successful copy does it addlenunder the pool mutex. The argument checks at:96-99are unchanged. Valid pointers take the same path with the same results.guest_direct_memory_for_app(core/libs/tests/GuestDirectMemoryForApp.cpp) gains a block after the existing fix(libkernel): release direct allocation when output faults #18815 bad-output case. It finds a free 2 MiB-aligned 2 MiB range withsceKernelAllocateDirectMemoryForAppand releases it, then checks thatsceKernelMemoryPoolExpandrestricted to that range fails withSCE_KERNEL_ERROR_EFAULTfor the output(int64_t*)1and for an output on a read-only page (VirtualAllocPAGE_READONLYon Windows,mmapPROT_READelsewhere). After both,sceKernelMemoryPoolGetBlockStatsreportsavailable_flushed_blocks == 0andallocated_flushed_blocks == 0, so neither failure was counted. An expand of the same range with a valid output then succeeds and returns the start of the range, which shows the range was freed, and the statistics reportavailable_flushed_blocks == 1. The range is released at the end. The test runs in under 0.2 s.sceKernelMemoryPoolReserve(:127) andsceKernelMemoryPoolBatch(:81) also write guest outputs directly; they are a separate topic.PhysicalMemoryPool::Allocis not touched.docs/dev/debt/libkernel.md:90namessceKernelMemoryPoolExpandonly for its search ceiling, which this PR does not change, so no debt entry changes.pr_overlap.py --branchreports no overlap with open pull requests (mainf36f094).pr_overlap.py --nameslists only feat(libs): mirror libkernel exports through libkernel_web #18112 and feat(libs): mirror libkernel exports through libkernel_sys #18190 for the touched exports; they mirror libkernel exports through libkernel_web and libkernel_sys and do not changeMemoryPoolExport.cpp.Tested
Windows 11, WinLibs GCC 15.2.0, build directory configured with Ninja, Release and
BUILD_TESTING=ONthrough its 8.3 short path, on this branch based onmain1d1ff11. Before each run, every.prxinbuild/core/libs/libs/unpatchedthat also exists inbuild/testswas copied there withcmake -E copy_if_different, and themd5sumoflibkernel.prxandlibc.prxmatched in both places; each link giveslibkernel.prxa new checksum, so the check is that the two copies match on every run.MemoryPoolExport.cppof 1d1ff11:cmake --build build --parallel 4 --target guest_direct_memory_for_app_tests, thenctest --test-dir build -R '^guest_direct_memory_for_app$' --output-on-failurefailed with***Exception: SegFault:FATAL: unhandled exception 0xc0000005,write of 0x0000000000000001,rip libkernel.prx+0x7c130, with the return addresslibkernel.prx+0x7edc30x93 bytes intosceKernelMemoryPoolExpand(nmputs it at+0x7ed30).ctestcommand: passed.sceKernelMemoryPoolExpand(GuestDirectMemoryForApp.cpp,GuestMemory.cpp,GuestFlexibleMemoryBudget.cpp) plusMemoryPoolTest, which testsMemoryPool.cppdirectly:cmake --build build --parallel 4 --target guest_memory_tests guest_flexible_memory_budget_tests MemoryPoolTest, thenctest --test-dir build -R '^(guest_direct_memory_for_app|guest_memory|guest_flexible_memory_.*|MemoryPoolTest)$' --output-on-failure: 11/11 passed (MemoryPoolTest,guest_direct_memory_for_app,guest_memory, and the eightguest_flexible_memory_*cases), 18.09 s in total.cmake --build build --parallel 4 --target guest_direct_memory_for_app_testsand the samectestcommand:MemoryPoolExport.cppof 1d1ff11 with the(int64_t*)1line removed from the test, and the read-only page's address printed:readOnlyPage=000000fc00100000, then0xc0000005withwrite of 0x000000fc00100000, so the old code also faults on a read-only output.DirectMemoryFree(physical, len);line: the test aborted at the read-only pageRequire(GuestDirectMemoryForApp.cpp:56), because the range left allocated by the first failed expand made the second expand of the same range return something other thanSCE_KERNEL_ERROR_EFAULT.expandedBytes += lenmoved before the copy-out: the test aborted at theavailable_flushed_blocks == 0check (GuestDirectMemoryForApp.cpp:64).MemoryPoolExport.cpprestored to 1d1ff11 and the test kept,guest_direct_memory_for_appfailed again with***Exception: SegFaultandwrite of 0x0000000000000001; with the change restored it passed.BUILD_TESTING(core/libs/prx/libkernel/CMakeLists.txt:130-134), so their CI runs it. ThereGuestCopyOut_nid_no_patchusesprocess_vm_writevandmach_vm_write(core/libs/prx/libc/src/General.cpp:880-895), which fail for an unmapped orPROT_READpage, and the unchanged code stores through the pointer directly, so the negative control fails there too, as reported on Linux for the same pattern in fix(libkernel): release direct allocation when output faults #18815.python tools/check_conventions.py --base upstream/main --head HEAD(upstreammain= 1d1ff11) with this title and description passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)