fix(libkernel): fail a socket call on an open non-socket descriptor with ENOTSOCK #4922

Closed
xXPBSXx wants to merge 1 commit from upstream-pr/4922 into main
xXPBSXx commented 2026-10-13 04:17:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T04:17:30Z.


What

bind, listen, accept, connect, shutdown, getsockname, getpeername, getsockopt, setsockopt, send, recv and the other libkernel calls that look up a guest socket (Lookup() in Udp.cpp) answered EBADF (9) for every descriptor that is not a guest socket. For a descriptor that is open but not a socket, for example a pipe end, FreeBSD 11.4 answers ENOTSOCK (38): getsock_cap returns ENOTSOCK when fp->f_type != DTYPE_SOCKET and keeps EBADF for a closed or invalid descriptor.

Lookup() now fails with ENOTSOCK when the descriptor is open (DescriptorIsOpen_nid_no_patch); guest sockets live in the descriptor table since #4391, so Lookup() reaches this case only for a descriptor that is not a socket, and with EBADF otherwise. shutdown no longer overwrites the error of Lookup() with EBADF. Not changed: sendfile still answers EBADF for a destination that is not a socket (a separate path in Udp.cpp, finish(9)).

No title evidence: found by writing failure-path tests from the FreeBSD source, no title I ran passes a non-socket descriptor to a socket call. This is FreeBSD conformance.

Overlap: the overlap bot lists no pull request for this one, and git merge-tree of the head 63dfc37d6 against main bf8dc610e is clean (checked, not rebuilt). The test registration sits before guest_dup in core/libs/prx/libkernel/CMakeLists.txt, away from the other socket PRs; docs/dev/debt/libkernel.md is not touched.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, on main c2565f014; moved since to a newer main (git merge-tree against main bf8dc610e is clean; not rebuilt there) with its test registration at its own place:

  • guest_socket_on_non_socket (new) passes (the test uses pipe ends as the open non-socket descriptors; getpeername, sendto, recvfrom, sendmsg and recvmsg use the same Lookup() but have no case of their own): bind, listen, accept, connect, shutdown, getsockname, getsockopt, setsockopt, send and recv on an open pipe fail with ENOTSOCK; the same calls on a closed pipe end, on -1 and on a descriptor that was never open keep EBADF.
  • It fails without the change to Udp.cpp (bind on a pipe must fail with ENOTSOCK (38) (errno 9)).
  • The existing socket tests (guest_udp, guest_tcp, guest_sce_net, guest_poll, guest_dup, guest_select, guest_socket*): pass together with the new one.
  • python tools/check_conventions.py --base upstream/main passes.
  • Measured on a combined build of the twelve open branches of this series (this PR and the others) merged onto main 0f2683cb7: guest_socket_on_non_socket takes 0.25 s (ctest, serial, Windows). The full guest_* / libc / libkernel group passes (391 of 391, 0 failed; the build also contains the other branches of this series).
  • Not tested: Linux (one source for both hosts, only Windows was built), a console, a title run.

Checklist

  • Based on current main; no other open PR implements the same functions (overlaps described above)
  • 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 (docs/dev/debt/libkernel.md)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4838288648 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4922). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T04:17:30Z. --- ### What `bind`, `listen`, `accept`, `connect`, `shutdown`, `getsockname`, `getpeername`, `getsockopt`, `setsockopt`, `send`, `recv` and the other libkernel calls that look up a guest socket (`Lookup()` in `Udp.cpp`) answered `EBADF` (9) for every descriptor that is not a guest socket. For a descriptor that is open but not a socket, for example a pipe end, FreeBSD 11.4 answers `ENOTSOCK` (38): [`getsock_cap`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c) returns `ENOTSOCK` when `fp->f_type != DTYPE_SOCKET` and keeps `EBADF` for a closed or invalid descriptor. `Lookup()` now fails with `ENOTSOCK` when the descriptor is open (`DescriptorIsOpen_nid_no_patch`); guest sockets live in the descriptor table since #4391, so `Lookup()` reaches this case only for a descriptor that is not a socket, and with `EBADF` otherwise. `shutdown` no longer overwrites the error of `Lookup()` with `EBADF`. Not changed: `sendfile` still answers `EBADF` for a destination that is not a socket (a separate path in `Udp.cpp`, `finish(9)`). No title evidence: found by writing failure-path tests from the FreeBSD source, no title I ran passes a non-socket descriptor to a socket call. This is FreeBSD conformance. Overlap: the overlap bot lists no pull request for this one, and `git merge-tree` of the head `63dfc37d6` against `main` `bf8dc610e` is clean (checked, not rebuilt). The test registration sits before `guest_dup` in `core/libs/prx/libkernel/CMakeLists.txt`, away from the other socket PRs; `docs/dev/debt/libkernel.md` is not touched. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, on `main` `c2565f014`; moved since to a newer `main` (`git merge-tree` against `main` `bf8dc610e` is clean; not rebuilt there) with its test registration at its own place: - `guest_socket_on_non_socket` (new) passes (the test uses pipe ends as the open non-socket descriptors; `getpeername`, `sendto`, `recvfrom`, `sendmsg` and `recvmsg` use the same `Lookup()` but have no case of their own): `bind`, `listen`, `accept`, `connect`, `shutdown`, `getsockname`, `getsockopt`, `setsockopt`, `send` and `recv` on an open pipe fail with `ENOTSOCK`; the same calls on a closed pipe end, on -1 and on a descriptor that was never open keep `EBADF`. - It fails without the change to `Udp.cpp` (`bind on a pipe must fail with ENOTSOCK (38) (errno 9)`). - The existing socket tests (`guest_udp`, `guest_tcp`, `guest_sce_net`, `guest_poll`, `guest_dup`, `guest_select`, `guest_socket*`): pass together with the new one. - `python tools/check_conventions.py --base upstream/main` passes. - Measured on a combined build of the twelve open branches of this series (this PR and the others) merged onto `main` `0f2683cb7`: `guest_socket_on_non_socket` takes 0.25 s (ctest, serial, Windows). The full `guest_*` / libc / libkernel group passes (391 of 391, 0 failed; the build also contains the other branches of this series). - Not tested: Linux (one source for both hosts, only Windows was built), a console, a title run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (overlaps described above) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt (`docs/dev/debt/libkernel.md`) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-14 02:36:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:36:13Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159626088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4922#issuecomment-6159626088). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:36:13Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and 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!4922
No description provided.