fix(libkernel): return EFAULT from kevent for an unreadable changelist or timeout or an unwritable eventlist #12503

Merged
babyhuey merged 2 commits from upstream-pr/12503 into main 2026-10-29 21:49:08 +00:00
babyhuey commented 2026-10-28 16:04:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-28T16:04:15Z.


What

kevent read 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's sys_kevent copies the timeout in first, kqueue_register copies each change in, and kqueue_scan copies events out, returning EFAULT for any failed copy.

  • The timeout is copied in with GuestCopyIn_nid_no_patch before the changes are applied; its values are still validated where they were.
  • Each change is copied in before it's registered, and each EV_ERROR / EV_RECEIPT entry is copied out with GuestCopyOut_nid_no_patch.
  • Triggered events come from the same single GetTriggeredEvents / WaitForEventsUntil call as before, now into a host buffer (on the stack up to 64 events, on the heap above that, and at most 1024 events whatever nevents asks for), and are copied out once. kevent may 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 one kevent.
  • A null eventlist keeps its existing behaviour (an 8-event scratch buffer, EFAULT if anything triggered).

Tested

BadPointersFaultAndBatchesFill in core/libs/tests/GuestKqueue.cpp, run last in main: 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 with EFAULT; a read-only eventlist fails with EFAULT both for triggered events and for an EV_ERROR entry, and is left unchanged. A request for INT_MAX events on the 16-slot buffer returns the 12 pending events, and a queue of 1,030 events returns 1,024 per call.

  • Linux (anyps5-build CI image, Ubuntu 24.04, GCC 13, Release, BUILD_TESTING=ON), this head e73a56f8aba on main d4cb89300fe: guest_kqueue passes; the full ctest passes (2,712 of 2,712), and check_conventions.py passes. With main's Kqueue.cpp the new test segfaults.
  • Windows (Windows 11, Ryzen 7 5800X, RTX 3080, WinLibs GCC 15.2, Release, BUILD_TESTING=ON), this head: full build and libs succeed; guest_kqueue passes 3 of 3 alone, and guest_equeue_events passes alone; with main's Kqueue.cpp the 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 on main on this machine (#11813), and two load timeouts under --parallel 4, check_main_sync (120 s) and select_tests_SelectArgumentTests (30 s), which pass 3 of 3 alone on this head in at most 49 s and 15 s. check_conventions.py passes.

Cost. Each checked copy is a host system call. A kevent poll with 4 events ready goes from about 98 ns on main to about 2,800 ns here (Linux, same build). That's measured and discussed with the same question for clock_gettime on #12418; until that's settled this shouldn't merge.

The test function sits before CreateAndClose and is called after Timeout, away from the spots where #9575 and #10014 add tests; both of their current heads merge with this one without conflicts.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5053648724 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12503). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-28T16:04:15Z. --- ### What `kevent` read 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's `sys_kevent` copies the timeout in first, `kqueue_register` copies each change in, and `kqueue_scan` copies events out, returning `EFAULT` for any failed copy. - The timeout is copied in with `GuestCopyIn_nid_no_patch` before the changes are applied; its values are still validated where they were. - Each change is copied in before it's registered, and each `EV_ERROR` / `EV_RECEIPT` entry is copied out with `GuestCopyOut_nid_no_patch`. - Triggered events come from the same single `GetTriggeredEvents` / `WaitForEventsUntil` call as before, now into a host buffer (on the stack up to 64 events, on the heap above that, and at most 1024 events whatever `nevents` asks for), and are copied out once. `kevent` may 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 one `kevent`. - A null eventlist keeps its existing behaviour (an 8-event scratch buffer, `EFAULT` if anything triggered). ### Tested `BadPointersFaultAndBatchesFill` in `core/libs/tests/GuestKqueue.cpp`, run last in `main`: 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 with `EFAULT`; a read-only eventlist fails with `EFAULT` both for triggered events and for an `EV_ERROR` entry, and is left unchanged. A request for `INT_MAX` events on the 16-slot buffer returns the 12 pending events, and a queue of 1,030 events returns 1,024 per call. - **Linux** (`anyps5-build` CI image, Ubuntu 24.04, GCC 13, Release, `BUILD_TESTING=ON`), this head `e73a56f8aba` on `main` `d4cb89300fe`: `guest_kqueue` passes; the full `ctest` passes (2,712 of 2,712), and `check_conventions.py` passes. With `main`'s `Kqueue.cpp` the new test segfaults. - **Windows** (Windows 11, Ryzen 7 5800X, RTX 3080, WinLibs GCC 15.2, Release, `BUILD_TESTING=ON`), this head: full build and `libs` succeed; `guest_kqueue` passes 3 of 3 alone, and `guest_equeue_events` passes alone; with `main`'s `Kqueue.cpp` the 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 on `main` on this machine (#11813), and two load timeouts under `--parallel 4`, `check_main_sync` (120 s) and `select_tests_SelectArgumentTests` (30 s), which pass 3 of 3 alone on this head in at most 49 s and 15 s. `check_conventions.py` passes. **Cost.** Each checked copy is a host system call. A `kevent` poll with 4 events ready goes from about 98 ns on `main` to about 2,800 ns here (Linux, same build). That's measured and discussed with the same question for `clock_gettime` on #12418; until that's settled this shouldn't merge. The test function sits before `CreateAndClose` and is called after `Timeout`, away from the spots where #9575 and #10014 add tests; both of their current heads merge with this one without conflicts. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-28 16:06:13 +00:00 (Migrated from github.com)

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. capacity is the caller-controlled nevents value after receipt slots are deducted, and when it exceeds 64 this allocates new KernelEvent[capacity] before asking the queue how many events are actually available. A guest can request INT_MAX events with a valid eventlist pointer and force an allocation proportional to roughly two billion KernelEvents; on constrained hosts that can throw std::bad_alloc out 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 nevents or handle allocation failure as a guest-visible error, and add a regression for an oversized nevents request that does not require allocating the full requested capacity. I have not run the focused guest_kqueue test locally: the full build configuration currently stops because the installed system Vulkan headers lack VK_KHR_maintenance8; your PR description reports Linux/Windows focused runs. I have not approved this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5685809268 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12503#pullrequestreview-5685809268). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T16:06:13Z. **Review:** CHANGES_REQUESTED --- I found one blocking issue in the new staging buffer. `capacity` is the caller-controlled `nevents` value after receipt slots are deducted, and when it exceeds 64 this allocates `new KernelEvent[capacity]` before asking the queue how many events are actually available. A guest can request `INT_MAX` events with a valid eventlist pointer and force an allocation proportional to roughly two billion `KernelEvent`s; on constrained hosts that can throw `std::bad_alloc` out 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 `nevents` or handle allocation failure as a guest-visible error, and add a regression for an oversized `nevents` request that does not require allocating the full requested capacity. I have not run the focused `guest_kqueue` test locally: the full build configuration currently stops because the installed system Vulkan headers lack `VK_KHR_maintenance8`; your PR description reports Linux/Windows focused runs. I have not approved this PR.
adriadam10 commented 2026-10-28 18:08:47 +00:00 (Migrated from github.com)

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's nevents (up to INT_MAX, about 64 GB) before the queue says how many events exist. That throws std::bad_alloc out of a C ABI entry point, or commits a huge range for one pending event. Bound the staging buffer independently of nevents. kevent may return fewer events than requested, and the events left in the queue are returned by the next call, so clamping capacity (for example to 1024) is enough. Alternatively, loop over fixed-size chunks only if you can keep the single-call requeue behaviour.
  • Add a case to BadPointersFaultAndBatchesFill with a valid eventlist and a very large nevents (for example INT_MAX on 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5687464945 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12503#pullrequestreview-5687464945). Originally posted by [adriadam10](https://github.com/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's `nevents` (up to `INT_MAX`, about 64 GB) before the queue says how many events exist. That throws `std::bad_alloc` out of a C ABI entry point, or commits a huge range for one pending event. Bound the staging buffer independently of `nevents`. `kevent` may return fewer events than requested, and the events left in the queue are returned by the next call, so clamping `capacity` (for example to 1024) is enough. Alternatively, loop over fixed-size chunks only if you can keep the single-call requeue behaviour. - Add a case to `BadPointersFaultAndBatchesFill` with a valid eventlist and a very large `nevents` (for example `INT_MAX` on 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.
MarcZX10 commented 2026-10-29 05:26:14 +00:00 (Migrated from github.com)

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

  • Reviewed exact head e73a56f8ab against current main 52c09d567d.
  • Ran git merge-tree --write-tree and git diff --check against current main.
  • Compared the current diff with open #7088, #7121, and #10016. The #10016 test insertion still conflicts in GuestKqueue.cpp; the implementation hunks I inspected for #7088 and #7121 are in separate registration/modify paths.
  • Inspected the bounded capacity logic and the author’s paired performance measurements. I did not run the local tests.

Results

Check Result
Current-main merge preview Clean
git diff --check Pass
Bounded staging Capacity is capped at 1,024 regardless of nevents; stack storage is used through 64
Author-reported Linux tests Full CTest 2,712/2,712, focused test passes, conventions pass; negative control segfaults with main Kqueue.cpp
Author/reviewer-reported Windows tests Focused guest_kqueue passes 3/3; negative control crashes with main Kqueue.cpp
Hosted checks Windows, conventions, eligibility, labels, and report pass; Linux, register-context, relinker, ASan, and write-watch jobs are skipped
Overlap preview Pairwise merge-tree with #10016 conflicts in GuestKqueue.cpp

Negative control

  • The author reports that the new test crashes when run with main’s Kqueue.cpp and passes with the PR implementation on Linux and Windows. I did not reproduce that locally.

Findings

Blocking

  1. 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.

  2. 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

  • I did not independently rerun the Linux/Windows suites or reproduce the timing benchmark. Test and timing figures above are author/reviewer-reported.
  • The hosted product matrix is mostly skipped; the passing Windows check does not substitute for the skipped Linux and specialized jobs.

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693250260 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12503#pullrequestreview-5693250260). Originally posted by [MarcZX10](https://github.com/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 - Reviewed exact head e73a56f8aba17afec4ecc4c0adc915c99551454c against current main 52c09d567d2fa453494dd720fcf53a98149468e4. - Ran git merge-tree --write-tree and git diff --check against current main. - Compared the current diff with open #7088, #7121, and #10016. The #10016 test insertion still conflicts in GuestKqueue.cpp; the implementation hunks I inspected for #7088 and #7121 are in separate registration/modify paths. - Inspected the bounded capacity logic and the author’s paired performance measurements. I did not run the local tests. ## Results | Check | Result | |---|---| | Current-main merge preview | Clean | | git diff --check | Pass | | Bounded staging | Capacity is capped at 1,024 regardless of nevents; stack storage is used through 64 | | Author-reported Linux tests | Full CTest 2,712/2,712, focused test passes, conventions pass; negative control segfaults with main Kqueue.cpp | | Author/reviewer-reported Windows tests | Focused guest_kqueue passes 3/3; negative control crashes with main Kqueue.cpp | | Hosted checks | Windows, conventions, eligibility, labels, and report pass; Linux, register-context, relinker, ASan, and write-watch jobs are skipped | | Overlap preview | Pairwise merge-tree with #10016 conflicts in GuestKqueue.cpp | ## Negative control - The author reports that the new test crashes when run with main’s Kqueue.cpp and passes with the PR implementation on Linux and Windows. I did not reproduce that locally. ## Findings ### Blocking 1. **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. 2. **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 - [Current PR head and changed files](https://github.com/boykopovar/AnyPS5/tree/e73a56f8aba17afec4ecc4c0adc915c99551454c) - [Repository overlap and dependency rule](https://github.com/boykopovar/AnyPS5/blob/52c09d567d2fa453494dd720fcf53a98149468e4/CONTRIBUTING.md#L62-L67) - [Overlapping open PR #10016](https://github.com/boykopovar/AnyPS5/pull/10016) - [Related registration PR #7088](https://github.com/boykopovar/AnyPS5/pull/7088) - [Related vnode-note PR #7121](https://github.com/boykopovar/AnyPS5/pull/7121) ## Not verified - I did not independently rerun the Linux/Windows suites or reproduce the timing benchmark. Test and timing figures above are author/reviewer-reported. - The hosted product matrix is mostly skipped; the passing Windows check does not substitute for the skipped Linux and specialized jobs. ## 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12503
No description provided.