fix(libkernel): close guest sockets in sceKernelClose #4053

Merged
nikowolf123 merged 1 commit from upstream-pr/4053 into main 2026-10-12 12:20:47 +00:00
nikowolf123 commented 2026-10-11 22:15:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-11T22:15:10Z.


What

sceKernelClose passed every descriptor to the host close. Guest socket descriptors start at 0x10000000 (GuestSockets::FirstDescriptor) and are not host descriptors, so sceKernelClose on an open guest socket returned SCE_KERNEL_ERROR_EBADF, left the entry in the guest socket table and kept the host socket and its equeue registrations, which only GuestSockets::Close drops. The call neither closed the socket nor threw.

Guest sockets now go to GuestSockets::Close, as close does and as sceKernelRead and sceKernelWrite do since #1727. A closed or unknown socket still returns SCE_KERNEL_ERROR_EBADF. FreeBSD kern_close closes a descriptor of any type. Only the guest socket table hands out descriptors from 0x10000000 (socket, accept, socketpair, dup, dup2).

#1795 also routed guest sockets in sceKernelClose, returning -1 instead of the kernel error; it was closed as a duplicate of #1711 for its socketpair part. Open #3764 adds EqueueDescriptorClosed(d) as the first line of sceKernelClose and conflicts with this line; the socket check should stay first, since GuestSockets::Close already calls EqueueDescriptorClosed.

Tested

Ubuntu 26.04 on WSL2, GCC 15.2.0, CMake 4.2.3, Ninja, Release, BUILD_TESTING=ON, ANYPS5_ENABLE_SPIRV_TOOLS=ON, lavapipe with ANYPS5_REQUIRE_VULKAN=1, on main af0b52ebb. No title or homebrew.

  • guest_socket_read_write closes the listener with sceKernelClose, then checks that a second sceKernelClose returns SCE_KERNEL_ERROR_EBADF and close fails with EBADF. Without the fix it aborts at the first new check (0x80020009); with it, it passes.
  • A probe outside the tree counted /proc/self/fd around sceKernelClose on a new TCP socket: 6, 7, 7 without the fix; 6, 7, 6 with it.
  • Full ctest, 909 tests. Three GPU tests behave the same in 3 of 3 runs on af0b52ebb with and without this change: agc_vector_window and agc_driver_prepared_shaders_registered_workgroup_sync fail, and agc_driver_packed_float16_native skips for a reason tools/check_skips.py does not expect. Every other test passes or skips as expected.
  • python3 tools/check_conventions.py --base origin/main: all checks passed.
  • Windows: not tested. The added line has no platform-specific code.

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. Written with Claude Code; I reviewed every line and ran the tests above.

Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4822787915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4053). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-11T22:15:10Z. --- ### What `sceKernelClose` passed every descriptor to the host `close`. Guest socket descriptors start at `0x10000000` (`GuestSockets::FirstDescriptor`) and are not host descriptors, so `sceKernelClose` on an open guest socket returned `SCE_KERNEL_ERROR_EBADF`, left the entry in the guest socket table and kept the host socket and its equeue registrations, which only `GuestSockets::Close` drops. The call neither closed the socket nor threw. Guest sockets now go to `GuestSockets::Close`, as `close` does and as `sceKernelRead` and `sceKernelWrite` do since #1727. A closed or unknown socket still returns `SCE_KERNEL_ERROR_EBADF`. FreeBSD [`kern_close`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_descrip.c) closes a descriptor of any type. Only the guest socket table hands out descriptors from `0x10000000` (`socket`, `accept`, `socketpair`, `dup`, `dup2`). #1795 also routed guest sockets in `sceKernelClose`, returning `-1` instead of the kernel error; it was closed as a duplicate of #1711 for its socketpair part. Open #3764 adds `EqueueDescriptorClosed(d)` as the first line of `sceKernelClose` and conflicts with this line; the socket check should stay first, since `GuestSockets::Close` already calls `EqueueDescriptorClosed`. ### Tested Ubuntu 26.04 on WSL2, GCC 15.2.0, CMake 4.2.3, Ninja, Release, `BUILD_TESTING=ON`, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, lavapipe with `ANYPS5_REQUIRE_VULKAN=1`, on `main` af0b52ebb. No title or homebrew. - `guest_socket_read_write` closes the listener with `sceKernelClose`, then checks that a second `sceKernelClose` returns `SCE_KERNEL_ERROR_EBADF` and `close` fails with `EBADF`. Without the fix it aborts at the first new check (`0x80020009`); with it, it passes. - A probe outside the tree counted `/proc/self/fd` around `sceKernelClose` on a new TCP socket: 6, 7, 7 without the fix; 6, 7, 6 with it. - Full `ctest`, 909 tests. Three GPU tests behave the same in 3 of 3 runs on af0b52ebb with and without this change: `agc_vector_window` and `agc_driver_prepared_shaders_registered_workgroup_sync` fail, and `agc_driver_packed_float16_native` skips for a reason `tools/check_skips.py` does not expect. Every other test passes or skips as expected. - `python3 tools/check_conventions.py --base origin/main`: all checks passed. - Windows: not tested. The added line has no platform-specific code. ### 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 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. Written with Claude Code; I reviewed every line and ran the tests above. Generated with Claude Code
Dextroid17 commented 2026-10-12 01:29:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:29:18Z.


#4053: the fix applies a pattern that's already working elsewhere in this file rather than inventing new logic — close_nid_postfix, sceKernelRead, and sceKernelWrite already route guest-socket-range descriptors to GuestSockets::Close the same way this adds for sceKernelClose, and a closed or unknown socket still returns the same EBADF before and after, so there's no behavior change for anything that already worked.

The conflict with open #3764 is real but lower-severity than it might read: #3764 does add EqueueDescriptorClosed(d) as the very first line, at the same position this PR's socket check would occupy, so the two will textually conflict on merge. But tracing what EqueueDescriptorClosed actually does, calling it twice (once from #3764's line, once again inside GuestSockets::Close) is harmless — it's an idempotent erase-if over the equeue's event list, not a refcount decrement or a free. So whichever lands second just needs the socket check to run before the redundant equeue call, which is a one-line reordering, not a functional fix.

#4154: the no-op treatment is safe, and it's consistent with how this function already treats every other advisory-only hint in range — MADV_DONTNEED is already accepted as a pure no-op, and MADV_PROTECT's real FreeBSD semantics (don't kill this process under swap pressure) is exactly the kind of hint a host can't grant anyway, so ignoring it only forgoes a hint, not a requirement. The value 10 matches FreeBSD's own ordering for MADV_PROTECT. One cosmetic note: removing this throw leaves the GuestAdviceCore constant with no remaining reference — dead but harmless, since nothing in this build is -Werror=unused.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116140024 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4053#issuecomment-6116140024). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T01:29:18Z. --- #4053: the fix applies a pattern that's already working elsewhere in this file rather than inventing new logic — `close_nid_postfix`, `sceKernelRead`, and `sceKernelWrite` already route guest-socket-range descriptors to `GuestSockets::Close` the same way this adds for `sceKernelClose`, and a closed or unknown socket still returns the same `EBADF` before and after, so there's no behavior change for anything that already worked. The conflict with open #3764 is real but lower-severity than it might read: #3764 does add `EqueueDescriptorClosed(d)` as the very first line, at the same position this PR's socket check would occupy, so the two will textually conflict on merge. But tracing what `EqueueDescriptorClosed` actually does, calling it twice (once from #3764's line, once again inside `GuestSockets::Close`) is harmless — it's an idempotent erase-if over the equeue's event list, not a refcount decrement or a free. So whichever lands second just needs the socket check to run before the redundant equeue call, which is a one-line reordering, not a functional fix. #4154: the no-op treatment is safe, and it's consistent with how this function already treats every other advisory-only hint in range — `MADV_DONTNEED` is already accepted as a pure no-op, and `MADV_PROTECT`'s real FreeBSD semantics (don't kill this process under swap pressure) is exactly the kind of hint a host can't grant anyway, so ignoring it only forgoes a hint, not a requirement. The value 10 matches FreeBSD's own ordering for `MADV_PROTECT`. One cosmetic note: removing this throw leaves the `GuestAdviceCore` constant with no remaining reference — dead but harmless, since nothing in this build is `-Werror=unused`. Written with AI assistance.
github-actions[bot] commented 2026-10-12 02:56:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T02:56:11Z.


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
#3295 core/libs/prx/libkernel/File/src/Open.cpp L214
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116920118 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4053#issuecomment-6116920118). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T02:56:11Z. --- <!-- 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 | |---|---|---|---| | #3295 | `core/libs/prx/libkernel/File/src/Open.cpp` [L214](https://github.com/boykopovar/AnyPS5/blob/ffa198407e70296c50d0011a4157a430408dd00e/core/libs/prx/libkernel/File/src/Open.cpp#L214) | | |
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!4053
No description provided.