fix(libkernel): clear select bits from nfds to the end of their word #10777

Merged
harisibrar1 merged 1 commit from upstream-pr/10777 into main 2026-10-25 06:04:39 +00:00
harisibrar1 commented 2026-10-25 00:01:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-25T00:01:34Z.


What

select leaves set bits in place from nfds up to the end of their 64-bit word, where the console clears them.

@orezraey measured this on a PS5 (firmware 13.60, payload process, pselect.log from #10310):

  • nfds_below_fd: nfds 16 with descriptor 16 set and a zero timeout returns 0 with the bit cleared (bit=0).
  • nfds_0_ready: nfds 0 leaves the bit set (bit=1).

FreeBSD 11.4's kern_select explains both cases (sys_generic.c). It sizes the sets as ncpubytes = roundup(nd, NFDBITS) / NBBY and, on success, copies ncpubytes of its output sets back to each non-null set (putbits, L1156-L1164). Those output sets hold only the ready bits. So every bit from nfds to the end of its 64-bit word comes back as 0, words past it are not touched, and with nfds 0 nothing is copied. On an error (EINTR, EBADF, EINVAL) nothing is copied either.

On main, select_nid_postfix (Socket.cpp) clears only the descriptors it asked about, all of them below nfds, on both the ready and the timeout return. This adds ClearDescriptorsFrom, which clears the bits from nfds to the end of that word in each non-null set, and calls it on those two returns. Error returns still leave the sets alone. It does nothing when nfds is a multiple of 64, 0 included.

Two existing tests pinned the old behaviour, and neither cites a measurement:

  • GuestSelect.cpp required the bit at nfds to survive a timeout (the select_nid_postfix(reader, …) case). That is the measured nfds_below_fd case, so it now requires the bit to be cleared, along with bit 63, while bit 64 in the next word is kept. New cases cover nfds 0 (bit kept, as measured) and the ready path (bits past nfds cleared, the ready bit kept).
  • GuestSelectWait.cpp required bit 63 to survive select with nfds 1. It is in the same word, so it is now cleared, and a bit in the next word is required to survive.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main f26434afa3 with this change: no failed steps.

  • guest_select, guest_select_wait, guest_kernel_sys_pselect, guest_socket_descriptors, guest_sce_net and guest_kernel_sys pass, 3 runs of 6/6.
  • Negative control: with main's Socket.cpp and these tests, guest_select and guest_select_wait both fail (2 runs).
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux and macOS. The change is host-independent bit arithmetic on the guest's sets.

Overlap

  • #10310 (@orezraey) records this difference in the select debt entry ("where AnyPS5 keeps it"). Whichever lands second should drop that sentence. I'm happy to do it here if #10310 lands first.
  • pr_overlap lists #9515, #9578 and #10456 for Socket.cpp/GuestSelect.cpp. They conflict with main and don't touch these lines: #10456 changes the descriptor loop and adds a test function before Kqueues. My own #10501 (pselect) calls select and saves and restores whole words up to nfds, so it is unaffected.

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 (debt-line order with #10310 above)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000297403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10777). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-25T00:01:34Z. --- ### What `select` leaves set bits in place from `nfds` up to the end of their 64-bit word, where the console clears them. @orezraey measured this on a PS5 (firmware 13.60, payload process, [`pselect.log`](https://gist.github.com/orezraey/7006c6d29c4543bc8f48b7de8c19b661) from #10310): - `nfds_below_fd`: `nfds` 16 with descriptor 16 set and a zero timeout returns 0 with the bit cleared (`bit=0`). - `nfds_0_ready`: `nfds` 0 leaves the bit set (`bit=1`). FreeBSD 11.4's `kern_select` explains both cases ([`sys_generic.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L1038-L1040)). It sizes the sets as `ncpubytes = roundup(nd, NFDBITS) / NBBY` and, on success, copies `ncpubytes` of its output sets back to each non-null set (`putbits`, [L1156-L1164](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L1156-L1164)). Those output sets hold only the ready bits. So every bit from `nfds` to the end of its 64-bit word comes back as 0, words past it are not touched, and with `nfds` 0 nothing is copied. On an error (`EINTR`, `EBADF`, `EINVAL`) nothing is copied either. On main, `select_nid_postfix` (`Socket.cpp`) clears only the descriptors it asked about, all of them below `nfds`, on both the ready and the timeout return. This adds `ClearDescriptorsFrom`, which clears the bits from `nfds` to the end of that word in each non-null set, and calls it on those two returns. Error returns still leave the sets alone. It does nothing when `nfds` is a multiple of 64, 0 included. Two existing tests pinned the old behaviour, and neither cites a measurement: - `GuestSelect.cpp` required the bit at `nfds` to survive a timeout (the `select_nid_postfix(reader, …)` case). That is the measured `nfds_below_fd` case, so it now requires the bit to be cleared, along with bit 63, while bit 64 in the next word is kept. New cases cover `nfds` 0 (bit kept, as measured) and the ready path (bits past `nfds` cleared, the ready bit kept). - `GuestSelectWait.cpp` required bit 63 to survive `select` with `nfds` 1. It is in the same word, so it is now cleared, and a bit in the next word is required to survive. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main f26434afa3 with this change: no failed steps. - `guest_select`, `guest_select_wait`, `guest_kernel_sys_pselect`, `guest_socket_descriptors`, `guest_sce_net` and `guest_kernel_sys` pass, 3 runs of 6/6. - Negative control: with main's `Socket.cpp` and these tests, `guest_select` and `guest_select_wait` both fail (2 runs). - `tools/check_conventions.py --base origin/main` passes. - Not tested: Linux and macOS. The change is host-independent bit arithmetic on the guest's sets. ### Overlap - #10310 (@orezraey) records this difference in the `select` debt entry ("where AnyPS5 keeps it"). Whichever lands second should drop that sentence. I'm happy to do it here if #10310 lands first. - `pr_overlap` lists #9515, #9578 and #10456 for `Socket.cpp`/`GuestSelect.cpp`. They conflict with main and don't touch these lines: #10456 changes the descriptor loop and adds a test function before `Kqueues`. My own #10501 (`pselect`) calls `select` and saves and restores whole words up to `nfds`, so it is unaffected. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (debt-line order with #10310 above) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 00:08:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:08:48Z.

Review: APPROVED


The PS5 measurements and FreeBSD comparison support clearing only through the end of the final 64-bit word selected by nfds. The implementation applies this on readiness and timeout returns, preserves words beyond that boundary, and leaves error returns untouched. Tests cover nfds == 0, timeout, readiness, an excluded descriptor within the final word, and preservation of the next word. Local git diff --check and the conventions checker pass; hosted test/build jobs are skipped by the current path policy, so I could not confirm a hosted native run.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640710596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10777#pullrequestreview-5640710596). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:08:48Z. **Review:** APPROVED --- The PS5 measurements and FreeBSD comparison support clearing only through the end of the final 64-bit word selected by `nfds`. The implementation applies this on readiness and timeout returns, preserves words beyond that boundary, and leaves error returns untouched. Tests cover `nfds == 0`, timeout, readiness, an excluded descriptor within the final word, and preservation of the next word. Local `git diff --check` and the conventions checker pass; hosted test/build jobs are skipped by the current path policy, so I could not confirm a hosted native run.
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!10777
No description provided.