fix(libkernel): select descriptors that are not sockets #1706

Closed
Mubarak-11 wants to merge 1 commit from upstream-pr/1706 into main
Mubarak-11 commented 2026-10-08 17:08:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mubarak-11 on 2026-10-08T17:08:45Z.


1|### What
2|
3|select threw NotImplemented_nid_no_patch("select on descriptors that are not libSceNet sockets")
4|for every descriptor it could be asked about, so it never worked for anything. It now polls
5|descriptors below GuestSockets::FirstDescriptor on the host and keeps the libSceNet poller for
6|the rest.
7|
8|The fd space is already partitioned by number — File/src/Stdio.cpp dispatches on
9|d >= GuestSockets::FirstDescriptor — and sceKernelOpen returns the host descriptor directly,
10|so a descriptor below FirstDescriptor is the host descriptor. select now follows the same
11|rule: a file-local HostPoll builds struct pollfd and calls ::poll, mirroring
12|poll_nid_postfix in Socket/src/Udp.cpp.
13|
14|Two behaviours changed with it:
15|
16|- Both NotImplemented_nid_no_patch calls are gone. A descriptor the socket poller reports as
17| Unknown is now EBADF, as is an invalid host descriptor (POLLNVAL).
18|- POLLPRI is not requested for host descriptors. A pipe or regular file has no out-of-band
19| data, so exceptfds cannot become ready for one. Requesting it reported a spurious
20| exceptional condition — macOS poll echoes POLLPRI back on a pipe when asked for it.
21|
22|New test core/libs/tests/GuestSelect.cpp, registered as guest_select beside guest_poll.
23|
24|One thing I did not change, because it looks like a design call: SelectDescriptorLimit is 1024
25|while GuestSockets::FirstDescriptor is 0x10000000, and the guest fd_set is 1024 bits. So a
26|socket can never be represented in a guest fd_set, and an nfds large enough to include one is
27|rejected as EINVAL — the socket branch of select is unreachable. This change makes select
28|work for the descriptors it can be asked about, rather than changing that limit.
29|
30|Non-socket descriptors still throw on Windows, unchanged, because WSAPoll cannot poll pipes or
31|files. Worth a TechnicalDebt.md line if you agree.
32|
33|### Tested
34|
35|macOS 15.7.4 arm64. The library suite was not run locally: libkernel does not build on macOS
36|at all — File/src/Stdio.cpp uses the Linux struct dirent64 — and libc/src/JumpBuffer.cpp is
37|x86-64 setjmp/longjmp assembly with no arm64 path, so the libs are Linux/Windows only. CI's
38|Linux job is the first real run of this test.
39|
40|Locally I linked GuestSelect.cpp against the real Socket.cpp, with pipe, close, write,
41|read and __error shimmed to the host syscalls, and ran it both ways:
42|
43|- this branch: 23/23 checks pass
44|- main: exits on the uncaught select on descriptors that are not libSceNet sockets exception
45|
46|Covered: an empty pipe waits out the timeout and clears the descriptor; a byte written makes the
47|read end ready and leaves exceptfds clear; the write end is writable; two descriptors ready at
48|once; nfds excludes a descriptor; nfds above the limit is EINVAL; a closed descriptor is
49|EBADF rather than a throw.
50|
51|### Checklist

  • Based on current main; no other open PR implements the same functions (tools/pr_overlap.py --branch reports no overlap)
  • 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 in TechnicalDebt
  • 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
  • AI-assisted: yes (DeepSeek). I reviewed and verified every line; the fd partition, the POLLPRI behaviour and the before/after runs above are my own checks.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4790268905 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1706). Originally posted by [Mubarak-11](https://github.com/Mubarak-11) on 2026-10-08T17:08:45Z. --- 1|### What 2| 3|`select` threw `NotImplemented_nid_no_patch("select on descriptors that are not libSceNet sockets")` 4|for every descriptor it could be asked about, so it never worked for anything. It now polls 5|descriptors below `GuestSockets::FirstDescriptor` on the host and keeps the libSceNet poller for 6|the rest. 7| 8|The fd space is already partitioned by number — `File/src/Stdio.cpp` dispatches on 9|`d >= GuestSockets::FirstDescriptor` — and `sceKernelOpen` returns the host descriptor directly, 10|so a descriptor below `FirstDescriptor` *is* the host descriptor. `select` now follows the same 11|rule: a file-local `HostPoll` builds `struct pollfd` and calls `::poll`, mirroring 12|`poll_nid_postfix` in `Socket/src/Udp.cpp`. 13| 14|Two behaviours changed with it: 15| 16|- Both `NotImplemented_nid_no_patch` calls are gone. A descriptor the socket poller reports as 17| `Unknown` is now `EBADF`, as is an invalid host descriptor (`POLLNVAL`). 18|- `POLLPRI` is not requested for host descriptors. A pipe or regular file has no out-of-band 19| data, so `exceptfds` cannot become ready for one. Requesting it reported a spurious 20| exceptional condition — macOS `poll` echoes `POLLPRI` back on a pipe when asked for it. 21| 22|New test `core/libs/tests/GuestSelect.cpp`, registered as `guest_select` beside `guest_poll`. 23| 24|One thing I did not change, because it looks like a design call: `SelectDescriptorLimit` is 1024 25|while `GuestSockets::FirstDescriptor` is `0x10000000`, and the guest `fd_set` is 1024 bits. So a 26|socket can never be represented in a guest `fd_set`, and an `nfds` large enough to include one is 27|rejected as `EINVAL` — the socket branch of `select` is unreachable. This change makes `select` 28|work for the descriptors it can be asked about, rather than changing that limit. 29| 30|Non-socket descriptors still throw on Windows, unchanged, because `WSAPoll` cannot poll pipes or 31|files. Worth a `TechnicalDebt.md` line if you agree. 32| 33|### Tested 34| 35|macOS 15.7.4 arm64. The library suite was **not** run locally: `libkernel` does not build on macOS 36|at all — `File/src/Stdio.cpp` uses the Linux `struct dirent64` — and `libc/src/JumpBuffer.cpp` is 37|x86-64 `setjmp`/`longjmp` assembly with no arm64 path, so the libs are Linux/Windows only. CI's 38|Linux job is the first real run of this test. 39| 40|Locally I linked `GuestSelect.cpp` against the real `Socket.cpp`, with `pipe`, `close`, `write`, 41|`read` and `__error` shimmed to the host syscalls, and ran it both ways: 42| 43|- this branch: 23/23 checks pass 44|- `main`: exits on the uncaught `select on descriptors that are not libSceNet sockets` exception 45| 46|Covered: an empty pipe waits out the timeout and clears the descriptor; a byte written makes the 47|read end ready and leaves `exceptfds` clear; the write end is writable; two descriptors ready at 48|once; `nfds` excludes a descriptor; `nfds` above the limit is `EINVAL`; a closed descriptor is 49|`EBADF` rather than a throw. 50| 51|### 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 (`tools/pr_overlap.py --branch` reports no overlap) - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes (DeepSeek). I reviewed and verified every line; the fd partition, the `POLLPRI` behaviour and the before/after runs above are my own checks.
oneandonlydean commented 2026-10-09 09:29:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:29:41Z.


Closing: no title is shown calling select on a non-socket descriptor on main, and the change hasn't been run. Reopen when it's a title's next stop on main, with the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078230172 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1706#issuecomment-6078230172). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:29:41Z. --- Closing: no title is shown calling `select` on a non-socket descriptor on main, and the change hasn't been run. Reopen when it's a title's next stop on main, with the log line.

Pull request closed

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!1706
No description provided.