Skip to content

fix(libkernel): return EFAULT from the AIO submit calls for an unwritable id output before running the requests - #19702

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
roboterspinne:fix/libkernel-aio-submit-unwritable-ids
Nov 14, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
roboterspinne:fix/libkernel-aio-submit-unwritable-ids

Conversation

@roboterspinne

Copy link
Copy Markdown
Contributor

What

SubmitCommands (core/libs/prx/libkernel/Aio/src/Aio.cpp:221-234 on main 1d1ff11), which backs sceKernelAioSubmitReadCommands, sceKernelAioSubmitWriteCommands and their Multiple forms (:357-371), checks the id output only for null (:222). It then reserves the ids (:230), runs every request with real file reads and writes (:231) and calls Reservation::Publish (:232), which stores output[index] = ids[index] through the raw guest pointer under g_mutex (:117-124). So an unmapped or read-only id output crashes the host with an access violation after the reads and writes have already happened. The poll, wait, cancel and delete exports and sceKernelAioSetParam in the same file copy their output with GuestCopyOut_nid_no_patch and return SCE_KERNEL_ERROR_EFAULT when they cannot (CopyStateOut :244-246, CopyStatesOut :252-255, sceKernelAioSetParam :354), documented as a host-safety fallback in docs/dev/debt/libkernel.md:84 (#14417, #18169). For a call with side effects before its output, sceZlibGetResult (core/libs/prx/libSceZlib/Export.cpp:133-141) checks the outputs first with a GuestArena::HostWrite guard and GuestWritable_nid_no_patch, saving and restoring errno. Console behaviour of the submit calls on a bad id output is unmeasured; #18766's PS5 probe did not cover them.

Tested

Windows 11, WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, build directory configured through its 8.3 short path. Built with cmake --build <build> --parallel 4 --target guest_aio_tests guest_aio_registry_tests guest_memory_tests guest_kernel_app_archive_tests agc_driver_storage_file_write_tests (guest_aio_registry_tests compiles Aio.cpp directly, core/libs/prx/libkernel/CMakeLists.txt:664-666). Before every run the .prx files in <build>/tests were refreshed from <build>/core/libs/libs/unpatched and libkernel.prx (and libSceAgcDriver.prx for the AGC test) had the same MD5 in both places.

  • Counter-check, new test on the unchanged Aio.cpp of 1d1ff11: ctest --test-dir <build> -R '^guest_aio$' --output-on-failure failed with ***Exception: SegFault, FATAL: unhandled exception 0xc0000005, rip ... libkernel.prx+0xc8be3, write of 0x0000000000000010, the store in Publish after the request had run.
  • With the change: ctest --test-dir <build> -R '^guest_aio$' --output-on-failure passed (1/1).
  • Related tests, the guest ctests whose sources call a submit export (GuestAio.cpp, GuestAioRegistry.cpp, GuestKernelAppArchive.cpp, GuestMemory.cpp), guest_aio_registry_tests also compiling Aio.cpp directly: ctest --test-dir <build> -R '^(guest_aio(_.+)?|guest_memory|guest_kernel_app_archive_.+)$' passed 12/12 (guest_aio, guest_aio_identity, guest_aio_admission, guest_aio_rollback, guest_aio_identity-exhaustion, guest_aio_processing, guest_aio_processing-rollback, guest_aio_bad_descriptors (Windows only), guest_kernel_app_archive_archive, guest_kernel_app_archive_plain, guest_kernel_app_archive_mmap, guest_memory), 3.72 s in total. agc_driver_storage_file_write (core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp), which submits writes with a valid id while the AGC host-write hook is set, now hit by the id check and the copy-out: ctest --test-dir <build> -R '^agc_driver_storage_file_write$' --output-on-failure passed (1/1, 3.01 s); after the debt wording change the 12 ctests above passed again.
  • Negative control at the end: with only Aio.cpp reverted to 1d1ff11 and the test kept, guest_aio failed again with the same write of 0x0000000000000010 in libkernel.prx+0xc8be3; with the change restored and rebuilt, guest_aio passed and -R '^guest_aio(_.+)?$' passed 8/8.
  • Not run here: Linux and macOS. The test is registered on every host and MapReadOnlyPage uses mmap(PROT_READ) there. GuestWritable_nid_no_patch rejects an unmapped or non-writable range through the host mappings on Linux and mach_vm_region on macOS (General.cpp:842-856), and an address no mapping covers gives std::nullopt from HostMapping::Find (core/libs/prx/libc/include/specifics/linux/HostMapping.hpp:115-129) rather than an exception, the non-Windows GuestArenaBeginHostWrite_nid_postfix (core/libs/prx/libc/src/GuestArena.cpp:941-949) does not make a read-only page writable (on macOS it only restores the protection a watched page already has, core/libs/prx/libc/src/GuestWriteWatch.cpp:603-615), so the check fails there too, and without the change the raw store faults on those hosts as well.
  • 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)

…able id output before running the requests

SubmitCommands checked the id output only for null and Publish stored
the ids raw, so an unmapped or read-only id crashed the process after
every read and write had already run. The submit calls now check the
id output with a host-write guard and GuestWritable before reserving
or running any request and return SCE_KERNEL_ERROR_EFAULT, as the
poll, wait, cancel, delete and set-param exports do for an unwritable
output. Publish copies the ids out with GuestCopyOut outside the
registry lock and releases the slots if that copy fails. GuestAio
passes an unmapped and a read-only id to all four submit calls and
checks that the buffers, results and file stay untouched.
@c1b3r-p3d1a

Copy link
Copy Markdown
Contributor

Linux run for this branch: 39df4bf 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_aio_tests guest_aio_registry_tests guest_memory_tests guest_kernel_app_archive_tests
  • ctest -R '^(guest_aio(_.+)?|guest_memory|guest_kernel_app_archive)$' --output-on-failure -j4: 8/8 passed (guest_aio, guest_aio_identity, guest_aio_admission, guest_aio_rollback, guest_aio_identity-exhaustion, guest_aio_processing, guest_aio_processing-rollback, guest_memory). My regex did not select a test named guest_kernel_app_archive, so that one wasn't run.
  • Counter-check: with core/libs/prx/libkernel/Aio/src/Aio.cpp from main and the new test block kept, guest_aio dies with SEGFAULT on the first unwritable id output.

Code: if Publish's copy-out still fails after the pre-check (another thread unmaps the page in between), published stays false. ~Reservation then clears the reserved slots, so the registry is not left with ids that the guest never received. One detail: by then the requests have already run and the results are written. The debt entry says the results are still written directly, so the behaviour is recorded. Nothing else found.

@JJenkx

JJenkx commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 39df4bf5e6, merged onto main 729ce5474c (6cd74930d7, 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_aio, guest_aio_bad_descriptors pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

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 core/libs/prx/libkernel/Aio/src/Aio.cpp
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 17551 docs/dev/debt/libkernel.md
PR 17609 docs/dev/debt/libkernel.md
PR 17638 docs/dev/debt/libkernel.md
PR 18515 docs/dev/debt/libkernel.md

@oneandonlydean
oneandonlydean merged commit 2ac33b4 into boykopovar:main Nov 14, 2026
14 checks passed
@roboterspinne
roboterspinne deleted the fix/libkernel-aio-submit-unwritable-ids branch November 14, 2026 08:41
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.

4 participants