fix(libkernel): return EFAULT from poll for an unreadable or unwritable descriptor array #12462

Merged
babyhuey merged 1 commit from upstream-pr/12462 into main 2026-10-29 13:39:56 +00:00
babyhuey commented 2026-10-28 14:46:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-28T14:46:28Z.


What

poll read and wrote the guest pollfd array in place, so a non-null array in unmapped or read-only memory crashed the host. FreeBSD's sys_poll copies the array in with copyin and the results out with copyout, returning EFAULT if either fails.

  • The existing body moves unchanged to a static PollDescriptors, which now runs on a local copy.
  • poll_nid_postfix keeps the existing timeout < -1 (EINVAL) and null-array (EFAULT) checks first, copies the array in with GuestCopyIn_nid_no_patch, calls PollDescriptors, and on success copies it back with GuestCopyOut_nid_no_patch. Either copy failing returns EFAULT (14). A failed poll copies nothing back.

Tested

core/libs/tests/GuestPoll.cpp adds: an unmapped array (0x10) fails with EFAULT, and a read-only array holding one valid entry fails with EFAULT at the copy-out and is left unchanged.

  • Linux (anyps5-build CI image, Ubuntu 24.04, GCC 13, Release, BUILD_TESTING=ON), this head 364a28eb2d7 on main 2227f136618: guest_poll passes; the full ctest passes (2,696 of 2,696), and check_conventions.py passes. With main's Udp.cpp the same 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_poll passes 3 of 3 alone; with main's Udp.cpp it crashes (0xc0000005). CI's test selection for this change (2,348 tests): 1,621 pass, 726 skip, and the one failure is check_main_sync timing out at 120 s under --parallel 4 (it takes 97-100 s under the same load on main); alone on this head it passes 3 of 3 in 53-63 s. check_conventions.py passes.

#11223 also edits the body of poll; it doesn't touch the lines this changes, and its current head merges 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:5052479020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12462). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-28T14:46:28Z. --- ### What `poll` read and wrote the guest `pollfd` array in place, so a non-null array in unmapped or read-only memory crashed the host. FreeBSD's `sys_poll` copies the array in with `copyin` and the results out with `copyout`, returning `EFAULT` if either fails. - The existing body moves unchanged to a static `PollDescriptors`, which now runs on a local copy. - `poll_nid_postfix` keeps the existing `timeout < -1` (`EINVAL`) and null-array (`EFAULT`) checks first, copies the array in with `GuestCopyIn_nid_no_patch`, calls `PollDescriptors`, and on success copies it back with `GuestCopyOut_nid_no_patch`. Either copy failing returns `EFAULT` (14). A failed poll copies nothing back. ### Tested `core/libs/tests/GuestPoll.cpp` adds: an unmapped array (`0x10`) fails with `EFAULT`, and a read-only array holding one valid entry fails with `EFAULT` at the copy-out and is left unchanged. - **Linux** (`anyps5-build` CI image, Ubuntu 24.04, GCC 13, Release, `BUILD_TESTING=ON`), this head `364a28eb2d7` on `main` `2227f136618`: `guest_poll` passes; the full `ctest` passes (2,696 of 2,696), and `check_conventions.py` passes. With `main`'s `Udp.cpp` the same 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_poll` passes 3 of 3 alone; with `main`'s `Udp.cpp` it crashes (`0xc0000005`). CI's test selection for this change (2,348 tests): 1,621 pass, 726 skip, and the one failure is `check_main_sync` timing out at 120 s under `--parallel 4` (it takes 97-100 s under the same load on `main`); alone on this head it passes 3 of 3 in 53-63 s. `check_conventions.py` passes. #11223 also edits the body of `poll`; it doesn't touch the lines this changes, and its current head merges 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 14:51:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:51:22Z.

Review: CHANGES_REQUESTED


Blocking: the kqueue retry path still calls the public poll_nid_postfix wrapper from inside PollDescriptors. After this refactor, descriptors there points to the wrapper's local std::vector, not guest memory. When an open kqueue is not readable, either recursive call at lines 1986/1988 sends that host pointer to GuestCopyIn_nid_no_patch; the call then fails with EFAULT instead of waiting/rechecking, so poll on a non-readable kqueue returns early. Please route this internal retry through PollDescriptors itself (or otherwise keep the guest-copy boundary only at the exported entry point), and add/extend a test for an open, initially non-readable kqueue with both finite and infinite waits if the harness supports it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5684652494 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12462#pullrequestreview-5684652494). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T14:51:22Z. **Review:** CHANGES_REQUESTED --- Blocking: the kqueue retry path still calls the public `poll_nid_postfix` wrapper from inside `PollDescriptors`. After this refactor, `descriptors` there points to the wrapper's local `std::vector`, not guest memory. When an open kqueue is not readable, either recursive call at lines 1986/1988 sends that host pointer to `GuestCopyIn_nid_no_patch`; the call then fails with `EFAULT` instead of waiting/rechecking, so `poll` on a non-readable kqueue returns early. Please route this internal retry through `PollDescriptors` itself (or otherwise keep the guest-copy boundary only at the exported entry point), and add/extend a test for an open, initially non-readable kqueue with both finite and infinite waits if the harness supports it.
MarcZX10 commented 2026-10-28 15:24:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T15:24:35Z.

Review: APPROVED


Reviewed exact head 364a28eb2d7e403b4d91655787055ac81d37b5f7. The wrapper preserves timeout/null validation order, copies the guest descriptor array in, runs the existing poll logic on the local copy, and copies results back only after a successful poll. The new tests exercise both an unmapped input and a read-only output array, including the unchanged-output expectation. Hosted Linux full CTest and Windows guest_poll results are reported in the PR; my independent git diff --check and merge-tree check against current main also pass.

I previously raised a concern about the kqueue retry with the local vector and then withdrew it after confirming the guest-copy helpers support host-side vector storage. I found no remaining blocker in this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5685177462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12462#pullrequestreview-5685177462). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T15:24:35Z. **Review:** APPROVED --- Reviewed exact head `364a28eb2d7e403b4d91655787055ac81d37b5f7`. The wrapper preserves timeout/null validation order, copies the guest descriptor array in, runs the existing poll logic on the local copy, and copies results back only after a successful poll. The new tests exercise both an unmapped input and a read-only output array, including the unchanged-output expectation. Hosted Linux full CTest and Windows `guest_poll` results are reported in the PR; my independent `git diff --check` and merge-tree check against current `main` also pass. I previously raised a concern about the kqueue retry with the local vector and then withdrew it after confirming the guest-copy helpers support host-side vector storage. I found no remaining blocker in this change.
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!12462
No description provided.