Skip to content

fix(libkernel): release the pool expansion and return EFAULT when sceKernelMemoryPoolExpand cannot write its output - #19699

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
roboterspinne:fix/libkernel-memory-pool-expand-unwritable-output
Nov 16, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
roboterspinne:fix/libkernel-memory-pool-expand-unwritable-output

Conversation

@roboterspinne

Copy link
Copy Markdown
Contributor

What

sceKernelMemoryPoolExpand (core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp:95-107 on main 1d1ff11) checks only for a null phys_addr_out, the alignments and the range (:96-99), then passes the guest pointer straight to DirectMemoryAlloc (:100). DirectMemoryAlloc (core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp:316-322) forwards to PhysicalMemoryPool::Alloc, whose only pointer check is if (!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. sceKernelAllocateDirectMemory in the same library (core/libs/prx/libkernel/DirectMemory/Export.cpp:276-288, #18815), the only other production caller of DirectMemoryAlloc, allocates into a local, copies it out with GuestCopyOut_nid_no_patch, and when the copy fails calls DirectMemoryFree(physical, len) and returns SCE_KERNEL_ERROR_EFAULT (:282-287). sceKernelMemoryPoolGetBlockStats in the same file returns SCE_KERNEL_ERROR_EFAULT for 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.

  • Rule. sceKernelMemoryPoolExpand allocates into a local int64_t physical and returns any non-zero result of DirectMemoryAlloc unchanged, so a failed allocation still leaves the output untouched. It then copies the address out with GuestCopyOut_nid_no_patch. If the copy fails, it frees the range with DirectMemoryFree(physical, len) and returns SCE_KERNEL_ERROR_EFAULT without adding to the pool's expandedBytes; only after a successful copy does it add len under the pool mutex. The argument checks at :96-99 are unchanged. Valid pointers take the same path with the same results.
  • Test. 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 with sceKernelAllocateDirectMemoryForApp and releases it, then checks that sceKernelMemoryPoolExpand restricted to that range fails with SCE_KERNEL_ERROR_EFAULT for the output (int64_t*)1 and for an output on a read-only page (VirtualAlloc PAGE_READONLY on Windows, mmap PROT_READ elsewhere). After both, sceKernelMemoryPoolGetBlockStats reports available_flushed_blocks == 0 and allocated_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 report available_flushed_blocks == 1. The range is released at the end. The test runs in under 0.2 s.
  • Not changed. sceKernelMemoryPoolReserve (:127) and sceKernelMemoryPoolBatch (:81) also write guest outputs directly; they are a separate topic. PhysicalMemoryPool::Alloc is not touched. docs/dev/debt/libkernel.md:90 names sceKernelMemoryPoolExpand only for its search ceiling, which this PR does not change, so no debt entry changes.
  • Overlap. pr_overlap.py --branch reports no overlap with open pull requests (main f36f094). pr_overlap.py --names lists 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 change MemoryPoolExport.cpp.

Tested

Windows 11, WinLibs GCC 15.2.0, build directory configured with Ninja, Release and BUILD_TESTING=ON through its 8.3 short path, on this branch based on main 1d1ff11. Before each run, every .prx in build/core/libs/libs/unpatched that also exists in build/tests was copied there with cmake -E copy_if_different, and the md5sum of libkernel.prx and libc.prx matched in both places; each link gives libkernel.prx a new checksum, so the check is that the two copies match on every run.

  • Counter-check, new test with MemoryPoolExport.cpp of 1d1ff11: cmake --build build --parallel 4 --target guest_direct_memory_for_app_tests, then ctest --test-dir build -R '^guest_direct_memory_for_app$' --output-on-failure failed with ***Exception: SegFault: FATAL: unhandled exception 0xc0000005, write of 0x0000000000000001, rip libkernel.prx+0x7c130, with the return address libkernel.prx+0x7edc3 0x93 bytes into sceKernelMemoryPoolExpand (nm puts it at +0x7ed30).
  • With the change, the same build and ctest command: passed.
  • Related tests, every test that calls sceKernelMemoryPoolExpand (GuestDirectMemoryForApp.cpp, GuestMemory.cpp, GuestFlexibleMemoryBudget.cpp) plus MemoryPoolTest, which tests MemoryPool.cpp directly: cmake --build build --parallel 4 --target guest_memory_tests guest_flexible_memory_budget_tests MemoryPoolTest, then ctest --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 eight guest_flexible_memory_* cases), 18.09 s in total.
  • Each case on its own, in local edits that were not committed, each followed by cmake --build build --parallel 4 --target guest_direct_memory_for_app_tests and the same ctest command:
    • MemoryPoolExport.cpp of 1d1ff11 with the (int64_t*)1 line removed from the test, and the read-only page's address printed: readOnlyPage=000000fc00100000, then 0xc0000005 with write of 0x000000fc00100000, so the old code also faults on a read-only output.
    • The change without its DirectMemoryFree(physical, len); line: the test aborted at the read-only page Require (GuestDirectMemoryForApp.cpp:56), because the range left allocated by the first failed expand made the second expand of the same range return something other than SCE_KERNEL_ERROR_EFAULT.
    • The change with expandedBytes += len moved before the copy-out: the test aborted at the available_flushed_blocks == 0 check (GuestDirectMemoryForApp.cpp:64).
  • Negative control at the end: with only MemoryPoolExport.cpp restored to 1d1ff11 and the test kept, guest_direct_memory_for_app failed again with ***Exception: SegFault and write of 0x0000000000000001; with the change restored it passed.
  • Not run here: Linux and macOS. The test is registered on every host with BUILD_TESTING (core/libs/prx/libkernel/CMakeLists.txt:130-134), so their CI runs it. There GuestCopyOut_nid_no_patch uses process_vm_writev and mach_vm_write (core/libs/prx/libc/src/General.cpp:880-895), which fail for an unmapped or PROT_READ page, 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 (upstream main = 1d1ff11) with this title and description passed.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

roboterspinne and others added 2 commits November 13, 2026 20:36
…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.
@c1b3r-p3d1a

Copy link
Copy Markdown
Contributor

Linux run for this branch: 4734338 merged onto main 1edac2f. Ubuntu 24.04 under WSL2, GCC 13.3.0, glibc 2.39, Ninja, Release, BUILD_TESTING=ON.

  • ninja -C build guest_direct_memory_for_app_tests, then ctest -R '^guest_direct_memory_for_app$' --output-on-failure: passed (1/1). The mmap(PROT_READ) branch of the new test runs here, and so does the check that sceKernelMemoryPoolExpand returns EFAULT and leaves both block counts at 0.
  • Counter-check: with core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp from main and the new test kept, the test dies with SEGFAULT.

Code: the allocation is released before EFAULT is returned, and expandedBytes only grows after the copy succeeds. The later successful expand of the same 2 MiB range at freeBlock passing confirms that the failed calls left the range free. Nothing else found.

@JJenkx

JJenkx commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 47343385a1, merged onto main 752c0f776b (8eac4283c8, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_direct_memory_for_app passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 6149 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9938 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11628 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11896 docs/dev/debt/libkernel.md
PR 12682 docs/dev/debt/libkernel.md
PR 12688 docs/dev/debt/libkernel.md
PR 12700 docs/dev/debt/libkernel.md
PR 12965 docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 15040 docs/dev/debt/libkernel.md
PR 16790 docs/dev/debt/libkernel.md
PR 16945 docs/dev/debt/libkernel.md
PR 17718 docs/dev/debt/libkernel.md
PR 18515 docs/dev/debt/libkernel.md
PR 18654 docs/dev/debt/libkernel.md
PR 19650 docs/dev/debt/libkernel.md

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 47343385a1:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_direct_memory_for_app_tests built 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.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4553: guest_direct_memory_for_app ......***Exception: SegFault  0.13 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • 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

@oneandonlydean
oneandonlydean merged commit f8d3890 into boykopovar:main Nov 16, 2026
13 checks passed
@roboterspinne
roboterspinne deleted the fix/libkernel-memory-pool-expand-unwritable-output branch November 17, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants