fix(libkernel): close() and setsockopt() reach libSceNet sockets #4111

Merged
jdmagic merged 2 commits from upstream-pr/4111 into main 2026-10-12 07:28:01 +00:00
jdmagic commented 2026-10-11 23:29:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jdmagic on 2026-10-11T23:29:56Z.


What

On the console a libSceNet socket is a kernel descriptor, so titles close it with close() and set its options with setsockopt(). libSceNet keeps its sockets in its own table, numbered from 32, and libkernel only looked at its own guest sockets:

  • close() passed the number to the CRT, which reported an invalid parameter and left the socket open.
  • setsockopt() returned EBADF.

libSceNet now registers a closer and an option setter with libkernel, as it already registers its poller for select. sceNetSetsockopt, which the option setter calls, also accepts SO_REUSEADDR, SO_BROADCAST, SO_SNDBUF and SO_RCVBUF, as libkernel's own sockets do. Because the two tables number independently, a CRT descriptor and a libSceNet socket can share a number, and close() then closes the socket; this is recorded in TechnicalDebt.

Sackboy: A Big Adventure (PPSA01289) sets SO_BROADCAST on its UDP P2P socket with setsockopt() in UIpNetDriver::InitBase. The EBADF made every listen fail, and the engine travelled back to its default map with ?closed, reloading it about every two seconds behind the loading screen. Each time it tore the session down it also closed the socket with close(), leaking one socket per attempt.

  • guest_sce_net: a socket created with sceNetSocket takes SO_BROADCAST through setsockopt() and sceNetSetsockopt, and SO_REUSEADDR through sceNetSetsockopt; close() closes it, and sceNetSocketClose on it afterwards fails with EBADF.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), on main bca7a29d.

  • guest_sce_net, guest_socket_events, guest_socket_pair, guest_socket_read_write, guest_kernel_errors, guest_filesystem, guest_file_vector: 7/7 passed. The full ctest was not run on this branch.
  • The new guest_sce_net checks fail on main without the source changes.
  • PPSA01289 (own dump, converted with the relinker): the listen succeeds and the reload loop is gone. With the other libSceNet fixes (the epoll and errno PRs) the title reaches its first-boot gamma screen; getting there also needs changes outside this PR.

This PR and the two other libSceNet PRs from this branch set are independent and merge cleanly with each other.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823207156 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4111). Originally posted by [jdmagic](https://github.com/jdmagic) on 2026-10-11T23:29:56Z. --- ### What On the console a libSceNet socket is a kernel descriptor, so titles close it with `close()` and set its options with `setsockopt()`. libSceNet keeps its sockets in its own table, numbered from 32, and libkernel only looked at its own guest sockets: - `close()` passed the number to the CRT, which reported an invalid parameter and left the socket open. - `setsockopt()` returned `EBADF`. libSceNet now registers a closer and an option setter with libkernel, as it already registers its poller for `select`. `sceNetSetsockopt`, which the option setter calls, also accepts `SO_REUSEADDR`, `SO_BROADCAST`, `SO_SNDBUF` and `SO_RCVBUF`, as libkernel's own sockets do. Because the two tables number independently, a CRT descriptor and a libSceNet socket can share a number, and `close()` then closes the socket; this is recorded in TechnicalDebt. Sackboy: A Big Adventure (PPSA01289) sets `SO_BROADCAST` on its UDP P2P socket with `setsockopt()` in `UIpNetDriver::InitBase`. The `EBADF` made every listen fail, and the engine travelled back to its default map with `?closed`, reloading it about every two seconds behind the loading screen. Each time it tore the session down it also closed the socket with `close()`, leaking one socket per attempt. - `guest_sce_net`: a socket created with `sceNetSocket` takes `SO_BROADCAST` through `setsockopt()` and `sceNetSetsockopt`, and `SO_REUSEADDR` through `sceNetSetsockopt`; `close()` closes it, and `sceNetSocketClose` on it afterwards fails with `EBADF`. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), on main bca7a29d. - `guest_sce_net`, `guest_socket_events`, `guest_socket_pair`, `guest_socket_read_write`, `guest_kernel_errors`, `guest_filesystem`, `guest_file_vector`: 7/7 passed. The full `ctest` was not run on this branch. - The new `guest_sce_net` checks fail on main without the source changes. - PPSA01289 (own dump, converted with the relinker): the listen succeeds and the reload loop is gone. With the other libSceNet fixes (the epoll and errno PRs) the title reaches its first-boot gamma screen; getting there also needs changes outside this PR. This PR and the two other libSceNet PRs from this branch set are independent and merge cleanly with each other. ### 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 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-11 23:35:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T23:35:34Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4078 docs/dev/TechnicalDebt.md L430
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115166043 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4111#issuecomment-6115166043). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T23:35:34Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4078 | `docs/dev/TechnicalDebt.md` [L430](https://github.com/boykopovar/AnyPS5/blob/46f5b72e8ebfd73177e01709196c53aa697d09a7/docs/dev/TechnicalDebt.md#L430) | | |
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!4111
No description provided.