fix(libkernel): return EFAULT from kevent for an unreadable changelist or timeout or an unwritable eventlist #12503
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12503"
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 babyhuey on 2026-10-28T16:04:15Z.
What
keventread the changelist and the timeout and wrote the eventlist in place, so a non-null pointer into unmapped or read-only memory crashed the host. FreeBSD'ssys_keventcopies the timeout in first,kqueue_registercopies each change in, andkqueue_scancopies events out, returningEFAULTfor any failed copy.GuestCopyIn_nid_no_patchbefore the changes are applied; its values are still validated where they were.EV_ERROR/EV_RECEIPTentry is copied out withGuestCopyOut_nid_no_patch.GetTriggeredEvents/WaitForEventsUntilcall as before, now into a host buffer (on the stack up to 64 events, on the heap above that, and at most 1024 events whateverneventsasks for), and are copied out once.keventmay return fewer events than requested; any left in the queue come back on the next call. Keeping one queue call matters: level-triggered events are requeued after they're returned, so collecting in several calls would return them twice in onekevent.EFAULTif anything triggered).Tested
BadPointersFaultAndBatchesFillincore/libs/tests/GuestKqueue.cpp, run last inmain: with 12 level-triggered user events, polls with room for 16, 100 (the heap path), 9 and 5 return 12, 12, 9 and 5, and the 16-slot poll reports all 12 idents once. An unmapped changelist, timeout or eventlist fails withEFAULT; a read-only eventlist fails withEFAULTboth for triggered events and for anEV_ERRORentry, and is left unchanged. A request forINT_MAXevents on the 16-slot buffer returns the 12 pending events, and a queue of 1,030 events returns 1,024 per call.anyps5-buildCI image, Ubuntu 24.04, GCC 13, Release,BUILD_TESTING=ON), this heade73a56f8abaonmaind4cb89300fe:guest_kqueuepasses; the fullctestpasses (2,712 of 2,712), andcheck_conventions.pypasses. Withmain'sKqueue.cppthe new test segfaults.BUILD_TESTING=ON), this head: full build andlibssucceed;guest_kqueuepasses 3 of 3 alone, andguest_equeue_eventspasses alone; withmain'sKqueue.cppthe new test crashes (0xc0000005). CI's test selection for this change (2,364 tests): 1,635 pass, 726 skip, and three failures unrelated to this change:guest_raise_exception, which also fails onmainon this machine (#11813), and two load timeouts under--parallel 4,check_main_sync(120 s) andselect_tests_SelectArgumentTests(30 s), which pass 3 of 3 alone on this head in at most 49 s and 15 s.check_conventions.pypasses.Cost. Each checked copy is a host system call. A
keventpoll with 4 events ready goes from about 98 ns onmainto about 2,800 ns here (Linux, same build). That's measured and discussed with the same question forclock_gettimeon #12418; until that's settled this shouldn't merge.The test function sits before
CreateAndCloseand is called afterTimeout, away from the spots where #9575 and #10014 add tests; both of their current heads merge with this one without conflicts.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-10-28T16:06:13Z.
Review: CHANGES_REQUESTED
I found one blocking issue in the new staging buffer.
capacityis the caller-controlledneventsvalue after receipt slots are deducted, and when it exceeds 64 this allocatesnew KernelEvent[capacity]before asking the queue how many events are actually available. A guest can requestINT_MAXevents with a valid eventlist pointer and force an allocation proportional to roughly two billionKernelEvents; on constrained hosts that can throwstd::bad_allocout of this C ABI entry point or consume a very large virtual/committed address range. The queue may have only one pending event, so this cost is not tied to useful results.Please bound the temporary buffer independently of the guest's
neventsor handle allocation failure as a guest-visible error, and add a regression for an oversizedneventsrequest that does not require allocating the full requested capacity. I have not run the focusedguest_kqueuetest locally: the full build configuration currently stops because the installed system Vulkan headers lackVK_KHR_maintenance8; your PR description reports Linux/Windows focused runs. I have not approved this PR.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T18:08:47Z.
Review: CHANGES_REQUESTED
The EFAULT handling matches FreeBSD's
sys_kevent/kqueue_register/kqueue_scan, and the single queue call is the right call for level-triggered events. One blocking problem, the same one @MarcZX10 raised:core/libs/prx/libkernel/Equeue/src/Kqueue.cpp:645:new KernelEvent[capacity]is sized by the guest'snevents(up toINT_MAX, about 64 GB) before the queue says how many events exist. That throwsstd::bad_allocout of a C ABI entry point, or commits a huge range for one pending event. Bound the staging buffer independently ofnevents.keventmay return fewer events than requested, and the events left in the queue are returned by the next call, so clampingcapacity(for example to 1024) is enough. Alternatively, loop over fixed-size chunks only if you can keep the single-call requeue behaviour.BadPointersFaultAndBatchesFillwith a valid eventlist and a very largenevents(for exampleINT_MAXon the 16-slot buffer's address, or the clamp boundary) showing it returns the pending events without a huge allocation.The overlap bot lists #9575 and #10014 touching the same test insertion spot; that is only adjacency, so rebase when one lands.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:26:14Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The unbounded staging allocation is fixed and the added guest-memory fault coverage is useful. I still cannot approve the current head: the author’s own matched-path measurements show a substantial hot-poll regression, and the test addition still conflicts with open PR #10016.
What I ran
e73a56f8abagainst current main52c09d567d.Results
Negative control
Findings
Blocking
Measured hot-path regression. The author reports a matched Linux measurement for four ready events: about 98 ns per call on main versus 2,773–2,861 ns on this head, roughly 28× slower. The author also explicitly concluded that #12503 should not merge with these guarded copies as-is, pending a fast-copy design decision. This is a measured cost on the operation’s polling path, not a theoretical concern. Please either address the per-event copy overhead without losing the EFAULT safety this PR adds, or get an explicit maintainer decision that accepts this measured cost before treating the change as ready.
Unresolved overlap with #10016. The current overlap report identifies a conflict in GuestKqueue.cpp, and my pairwise merge-tree check still reports that conflict. The body says the test was moved away from #9575/#10014, but does not mention #10016. Please coordinate with that author and move/sequence the insertion so both changes can integrate cleanly; declare Depends on if an order is required.
I manually compared the current #7088 and #7121 hunks in Kqueue.cpp: they modify registration/modify paths, while this PR stages and copies results in kevent_nid_postfix, so I found no same-line dependency there.
Sources
Not verified
To get this to a good state
Resolve the measured copy cost with maintainers, coordinate the still-conflicting #10016 test insertion, and rerun focused tests and the paired benchmark on the integrated head.