fix(libkernel_sys): read and write the aiocb of aio_read, aio_write and aio_fsync as FreeBSD's aio_aqueue does #14403
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14403"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-11-01T11:35:00Z.
What
On
26fa1212b8(line numbers below refer to it)aio_read,aio_writeandaio_fsyncgo toQueue()(core/libs/prx/libkernel_sys/Aio.cpp:208-219), which handles only a null aiocb (line 152), then storesstatus,errorandkernelinfostraight into the guest aiocb (156-158, 160, 182, 184) and copies it withconst GuestAiocb request = *user;(164);Consume()(133-140), reached fromaio_returnandaio_waitcomplete, storeserrorandstatusthe same way. An aiocb at a bad address such as 0x10, an aiocb in a read-only page, or a completed request whose aiocb became read-only therefore crashes the host process (FATAL: unhandled exception 0xc0000005, exit 139) instead of failing. FreeBSD 11.4'saio_aqueuesets up the process's aio state first (1466-1467), makes the firststatus/error/kernelinfostores without checking them (1471-1473), checks the queue limit forEAGAIN(1475-1479), copies the aiocb in and fails withEFAULTwhen that faults (1484-1489), fails withEINVALwithout queuing when thekernelinfostore faults after the descriptor, opcode and offset checks (1576-1580), and ignores theEINPROGRESSstore (1609) and the error store ataqueue_fail(1650);kern_aio_returnandkern_aio_waitcompletestoreerrorandstatuswithsuwordand ignore the result. The library's debt entry (docs/dev/debt/libkernel_sys.md:357) already says the aio rules follow FreeBSD 11vfs_aio.c, and the existing checkaio_read(nullptr)givesEFAULT(core/libs/tests/GuestKernelSysAio.cpp:76) already encodes the copy-in rule, for null only.Queue()followsaio_aqueue: it setsaio.usedfirst; every store into the aiocb goes throughGuestCopyOut_nid_no_patchwith the result ignored, via a smallStorehelper that takes the aiocb, a pointer to the member and the member's own type and forms the field's address as integer arithmetic from the aiocb pointer, so a null aiocb never forms a member address; the aiocb is read withGuestCopyIn_nid_no_patch, and a failed copy storesEFAULT(ignored) and fails withEFAULT; a failedkernelinfostore fails withEINVALand does not queue the request. The separate null check is gone:GuestCopyIn_nid_no_patchfails for address 0 (core/libs/prx/libc/src/General.cpp:856-860), so null still givesEFAULT, and the queue-fullEAGAINnow comes first, as in FreeBSD.Consume()storeserrorand thenstatusthroughGuestCopyOut_nid_no_patchand ignores the result. The order of the other checks and every result for a valid aiocb are unchanged. The checked copies are the existing helpers: on WindowsVirtualQueryandReadProcessMemoryplus theGuestArena::HostWriteguard, so write-tracked guest pages still count as writable; on Linuxprocess_vm_readv/process_vm_writev; on macOSmach_vm_read_overwrite/mach_vm_write. The debt entry now also says that a bad pointer passed toaio_error,aio_suspendoraio_waitcompletestill crashes the host; those are left for a follow-up, since changingaio_errorwould change its null result and #11416 rewritesaio_suspendandaio_waitcomplete.Validation()inGuestKernelSysAio.cppgains four checks, with page helpers in the#ifdef _WIN32VirtualAlloc/VirtualProtect, elsemmap/mprotect, style ofGuestFileVectorMemory.cpp: an aiocb at 0x10 gives-1/EFAULT; an aiocb whose last 40 bytes run into a no-access page gives-1/EFAULT, withstatus-1 anderrorEFAULTstored in its readable part; a fully valid read request in a read-only page gives-1/EINVALand is not queued (statuskeeps its 0 andaio_errorreturnsEINVAL); a completed request whose page is then made read-only still returns its status fromaio_return, keeps its oldstatusanderror, and a secondaio_returngivesEINVAL.pr_overlap.py --branchreports shared lines only indocs/dev/debt/libkernel_sys.mdwith #13286, and no shared exports or new files: #13286 changes thereadlinkentry just above this branch's aio entry, so a merge after it is a one-line conflict resolved by keeping both entries; it does not touchAio.cpp, its test or the aio entry itself. #10625, which added thesymlinkentry just below the aio entry, is merged; the merge ofmain(b28bb77217) into this branch keeps both entries. It cannot compare #9019, #9162, #9297, #9302, #12130, #12642, #13129, #13253, #13256, #14101, #14212 and #14299, which also change that debt file but do not merge withmain. Checked by hand: a trial merge of this branch with each of them conflicts in exactly the files thatmain(b28bb77217) with it already conflicts in, so this branch adds no conflict.Tested
Windows 11, WinLibs GCC 15.2.0, Ninja, Release,
BUILD_TESTING=ON, on28af4b5e97(based on26fa1212b8):cmake --build build --parallel 6 --target guest_kernel_sys_aio_tests, thenbuild/tests/guest_kernel_sys_aio_tests.exewithbuild/core/libs/libs/unpatchedand the WinLibsbinfirst onPATH. On untouched26fa1212b8the test without the new checks printsguest_kernel_sys_aio passed(0.32 s).Aio.cpp, each new check fails.FATAL: unhandled exception 0xc0000005,rip ... libkernel_sys.prx+0x1301f,write of 0x0000000000000058, exit 139.check failed at line 123(the straddling check; the variant file is one line shorter), exit 1: the old code does not fault there, but the call's result is not-1/EFAULTwithstatus-1 anderrorEFAULT.write of 0x...2048atlibkernel_sys.prx+0x1301f, exit 139.write of 0x...3038atlibkernel_sys.prx+0x123ed(inConsume), exit 139.guest_kernel_sys_aio passed, exit 0, 0.19 s; 10 consecutive runs all passed.ctest --test-dir build -R '^guest_kernel_sys_aio$': 1/1 passed.a59f3d947cchanges only howStoreforms the field address (a pointer to the member and integer arithmetic instead of&user->field); the order and every result stay the same. Ona59f3d947c,cmake --build build --parallel 6 --target guest_kernel_sys_aio_tests, thenctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure: 1/1 passed (0.14 s), which includes the null aiocb checkaio_read(nullptr)->-1/EFAULTat line 117 ofcore/libs/tests/GuestKernelSysAio.cpp.Storeshape was checked with a stand-alone probe built with-fsanitize=undefined -fno-sanitize=vptr -fsanitize-trap=all(GCC 15.2.0): the shape of28af4b5e97traps on a null aiocb (Illegal instruction, exit 132), the shape ofa59f3d947creturns-1without a trap.mainb28bb77217into this branch (2dbd3cdfed; the debt-file conflict with the merged #10625 is resolved by keeping both entries, andmain's change to the AIO waits inAio.cppand its test,adef5635c8, merges without conflict):cmake --build build --parallel 6 --target guest_kernel_sys_aio_tests, thenctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure: 1/1 passed (0.23 s).main4f41f4c375into this branch (aeaf5fda38; no conflicts, and among this branch's filesmainchanged onlydocs/dev/debt/libkernel_sys.mdsinceb28bb77217):cmake --build build --parallel 6 --target guest_kernel_sys_aio_tests, thenctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure: 1/1 passed (0.13 s). The hosted Windows job ataeaf5fda38(job 125879104155) runs it:1/1 Test #654: guest_kernel_sys_aio ... Passed 0.34 sec. The job at2dbd3cdfedbuilt the test but ranconventions_git_errors(Test #54), becausemainthen lacked #14413 and the CTest index file lost its last digit.python tools/check_conventions.py --base 4f41f4c375 --head HEADwith this title and description passed.process_vm_writev/mprotectandmach_vm_write), since there is no such host here.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T12:01:51Z.
Review: COMMENTED
Reviewed — no blocking findings
The aio change reads correctly from source, but the NULL-pointer rejection in
Queuewas removed and no test covers a NULL request; approval waits for the hosted Windows run at28af4b5e97.What I ran
Exact head
28af4b5e97bfe4b9dd59a826c000d51946e91d0e; PR baseb2620e56d114ef053f8b45433b1d48aff7ec37ba, unchanged at completion. No build and no test run. Commands:The precheck's ABI and test-quality helpers were missing from the isolated head tree, so I ran both by hand from
/marc/AnyPS5/tools.Results
possible-unregistered-cpp-testfinding forGuestKernelSysAio.cpp; false positive, because CMake registersguest_kernel_sys_aio_testsatcore/libs/CMakeLists.txtL1144Findings
Blocking: none.
Non-blocking:
QueueinAio.cppno longer rejects a NULLuserbefore it takes the lock and setsaio.used = true(Aio.cpp L156-L162 at this head). TheStore(&user->status, ...)calls then form&user->statusfrom a null pointer, which is undefined behaviour in C++ and a null member access that UBSan reports. A NULL request still ends in EFAULT, becauseGuestCopyIn_nid_no_patchreturns false for a zero source address (General.cpp L856-L861 at this head). The new test only passes address0x10, so the NULL path is not pinned. Suggested fix: restoreif (user == nullptr) return Fail(GuestEfault);before the lock, and add a NULL case that asserts EFAULT and an unchangedaio.used.Sources
Queueat the reviewed headGuestCopyIn_nid_no_patchat the reviewed headNot verified
guest_kernel_sys_aio_testson any platform; the hosted Windows run at this head was not complete at review time.aio_read,aio_writeandaio_fsyncis not checked here.To get this to a good state
The NULL-pointer suggestion above is non-blocking. The remaining gate is the hosted Windows run at
28af4b5e97; the same checks will be rerun if the head changes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T12:17:20Z.
Review: APPROVED
Reviewed — looks good
The aio change is correct at
28af4b5e97, and the hosted Windows run for this exact head passedguest_kernel_sys_aio_tests, which was the remaining gate in my earlier comment.What I ran
Exact head
28af4b5e97bfe4b9dd59a826c000d51946e91d0e; PR baseb2620e56d114ef053f8b45433b1d48aff7ec37ba, unchanged at completion. No build and no local test run. Commands:The precheck's ABI and test-quality helpers were missing from the isolated head tree; the ABI helper was run by hand from
/marc/AnyPS5/tools.Results
core/libs/CMakeLists.txtL1144guest_kernel_sys_aio_testsbuilt and passed (1 of 1)Unedited hosted log lines (Windows job)
Findings
Blocking: none.
Non-blocking: the NULL-pointer point in my earlier comment still applies.
Queueno longer rejects a NULLuserbefore taking the lock and settingaio.used, and no test pins the NULL case. The restored early return, or a NULL test case, would close it. This does not block approval.Sources
Queueat the reviewed headNot verified
aio_read,aio_writeandaio_fsyncis not checked.To get this to a good state
Nothing is required for the approval. The NULL-pointer suggestion is optional.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T07:33:31Z.
Review: COMMENTED
Reviewed — no blocking findings from the read; the aio test was built on Windows but did not run
This PR changes
aio_read,aio_writeandaio_fsyncso that the guest aiocb is read and written field by field, in the way FreeBSD'saio_aqueuedoes. The change goes through aStorehelper that writes one field at its member offset withGuestCopyOut. I read the production change in full. The Windows CTest run builtguest_kernel_sys_aio_tests.exebut did not select it, so the new behaviour has no hosted pass.What I ran
a59f3d947c948fe756535f186c6f5ab1907c64ad; baseb2620e56d114ef053f8b45433b1d48aff7ec37ba. 3 files, +79/-11:core/libs/prx/libkernel_sys/Aio.cpp(+27/-),core/libs/tests/GuestKernelSysAio.cpp(+61), anddocs/dev/debt/libkernel_sys.md(one line).Aio.cppchange in full. The request path now copies the aiocb in withGuestCopyIn(EFAULT on failure) and writeskernelinfo,errorandstatusthroughStore, which returns the copy-out result. The completion path writeserrorandstatusthroughStorebut ignores the result. A failedkernelinfostore in the request path returns EINVAL.GuestKernelSysAio.cpp.guest_kernel_sys_aio_tests.exewas built and linked. The CTest selection had one case,conventions_ci_gate(Test #52). No aio case ran.library-portabilityandreportsucceeded. Linux, relinkers and write-watch skipped.Results
guest_kernel_sys_aio_tests(hosted, Windows)Aio.cppchange (read)GuestKernelSysAio.cppRead-only check output
Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. ABI audit: passed (8 symbols). Stack check: clean, no declared dependencies, no errors. Hosted:
gh api repos/boykopovar/AnyPS5/commits/a59f3d947c948fe756535f186c6f5ab1907c64ad/check-runs, and the Windows job log.Negative control
Not run here. A negative control needs a Windows build and run of the aio test, and code from this head is not run here.
Findings
Blocking
None from the code read.
Non-blocking
Storewhen it writeserrorandstatus, while the request path checks its stores. If a copy-out fails when a job finishes, the guest is not told. The diff does not say whether that is intended.kernelinfostore in the request path returns EINVAL, and a failed copy-in returns EFAULT. The diff does not give the reason for the different codes.Storetakes member offsets from a staticGuestAiocbthroughreinterpret_cast. It assumes the guest layout matches the member offsets. That assumption was not checked here.Sources
github.com/boykopovar/AnyPS5@a59f3d947c/core/libs/prx/libkernel_sys/Aio.cppgithub.com/boykopovar/AnyPS5@a59f3d947c/core/libs/tests/GuestKernelSysAio.cppwindowsona59f3d947cNot verified
GuestKernelSysAio.cpp.Store.To get this to a good state
guest_kernel_sys_aio_testsat this head in hosted CI. Say in the PR whether the completion-path copy-out result should be checked, and why the error codes differ.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T11:16:40Z.
Review: APPROVED
Reviewed — approved
aio_read,aio_writeandaio_fsyncnow use checked copies to read and write the guest aiocb, in FreeBSDaio_aqueueorder, andConsumemakes its stores without checking the result. On Linux the PR merges and builds cleanly, the aio test passes, and withAio.cppreverted the test crashes.What I ran
Head
aeaf5fda38cb6d60046bdbfa55394004ee694bf0,main4f41f4c375f5abb487dbd184fbc150f31994d762, merged treea0b9f39c8f5bb2a10c572fa8314fa0198747d569. Debian 13, GCC 14.2.0, Release, Ninja,BUILD_TESTING=ON.Results
git diff --checkcheck_conventions.pylibssucceededunwrap_self_UnwrapTest_test_new_output_inherits_the_destination_directory_group, also fails onmainand has nothing to do with this changeguest_kernel_sys_aio, 10 runsNegative control output
Negative control
I restored
core/libs/prx/libkernel_sys/Aio.cppfrommain, kept the new test, then rebuiltlibkernel_sys.prxandguest_kernel_sys_aio_tests. The test crashes with SIGSEGV on the first new check, the0x10aiocb (GuestKernelSysAio.cpp:120). Because the crash ends the run there, this control does not cover the three later new checks one by one. The description reports a separate counter-check for each of them on Windows.After restoring the merged
Aio.cppand rebuilding, the test passes andgit status --porcelainis empty.Code review notes
Queue(core/libs/prx/libkernel_sys/Aio.cpp:158-202) with FreeBSD'saio_aqueue, using the description's quotes and line numbers. My reading of FreeBSD comes from memory; I could not open the source (see Not verified). The order matches:aio.usedis set first;status,errorandkernelinfostores are unchecked;EAGAINbefore the copy-in;EFAULTand returnsEFAULT;kernelinfostore givesEINVALthrough theaqueue_fail-style error store, and the request is not queued;EINPROGRESSstore is unchecked.Consume(Aio.cpp:140-147) ignores the store results, which matcheskern_aio_returnandkern_aio_waitcompleteusingsuwordwithout a check. This answers MarcZX10's question about why the completion path does not check its stores.Store(Aio.cpp:94-99) computes the field offset from a static zero-initialisedGuestAiocband adds it touseras an integer. No member address is ever formed from a nulluser, which deals with MarcZX10's UB point. The layout is fixed by thestatic_asserts atAio.cpp:55-56.GuestCopyIn_nid_no_patchrejects address 0 (core/libs/prx/libc/src/General.cpp:856-861), and the existing check atGuestKernelSysAio.cpp:119passed in this run. I confirm the author's reply to MarcZX10's NULL point, now with an executed result.GuestCopyOut_nid_no_patchonly succeeds whenprocess_vm_writevcopies every byte (General.cpp:839-847), so a partial write counts as a failure. The author could not run this Linux path; this run exercises it, including the straddling and read-only-page cases.aio_error,aio_suspendoraio_waitcomplete). The code backs this up:aio_errorreadsaiocb->statusdirectly (Aio.cpp:235), andaio_waitcompletewrites*result(Aio.cpp:277). The change adds no code comments, and the checklist lines are all present.Findings
Blocking
None.
Non-blocking
GuestKernelSysAio.cpp:123: the four pages fromPages(page * 4)are never unmapped (munmap/VirtualFree). This does no harm in a short-lived test process, but a matching release helper would keep the test tidy.Not verified
VirtualQueryplusHostWrite) and macOS (mach_vm_write) copy paths; only Linux ran here.vfs_aio.cline numbers were not opened here, because there is no network.