feat(libkernel): implement socketpair #1711

Merged
SleglJan merged 1 commit from upstream-pr/1711 into main 2026-10-09 09:55:54 +00:00
SleglJan commented 2026-10-08 17:21:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-08T17:21:17Z.


What

Adds socketpair to libkernel, in Socket/src/Udp.cpp beside socket and accept, with the semantics of FreeBSD 11's kern_socketpair and socreate:

  • AF_UNIX with SOCK_STREAM, SOCK_DGRAM or SOCK_SEQPACKET and protocol 0. SOCK_NONBLOCK (0x20000000) puts both ends in non-blocking mode; SOCK_CLOEXEC (0x10000000) is accepted and has nothing to do.
  • Errors as FreeBSD 11's socreate and kern_socketpair give them: EAFNOSUPPORT for an unknown family; EPROTONOSUPPORT for type 0 or a non-zero protocol; EPROTOTYPE for another type; EOPNOTSUPP for AF_INET and AF_INET6 with a type and protocol the inet domain has, whose protocols have no connect2; EFAULT for a null output. Nothing is written to sv on failure.
  • The ends are registered as guest sockets, so send, recv (with MSG_NOSIGNAL and MSG_PEEK), recvfrom, recvmsg, shutdown, fcntl, poll and close work on them as on any guest socket, with the existing errno mapping (EAGAIN 35, EPIPE 32 on Linux). On a pair end getsockname and getpeername report FreeBSD's unnamed AF_UNIX address (16 bytes, family 1, truncated to the caller's length as kern_getsockname does), and recvfrom/recvmsg report length 0 for a stream and the unnamed address for a datagram, instead of converting the host's address as an IP one.
  • On Linux the pair is the host's socketpair(AF_UNIX, ...). On Windows, which has none, a SOCK_STREAM pair is a loopback TCP connection (listener on 127.0.0.1:0 with SO_EXCLUSIVEADDRUSE, connect, accept, peer port checked against the client, TCP_NODELAY), the shape OpenSSL's rio_notifier.c and curl's socketpair.c use on Windows, and a SOCK_DGRAM pair is two UDP sockets bound to 127.0.0.1:0 and connected to each other; SOCK_SEQPACKET throws there. The differences from the console that remain (full-receiver datagrams, getpeername after the peer closed, connect/bind/sendto with an address on a pair end, ECONNRESET on Windows) are recorded in TechnicalDebt.

Of the 34 relinked titles in the homebrew catalog (#1578), 19 import socketpair and 2 stop at it on main b858fdc4, PPSA99810 PSBox Cloud Gaming and PPSA99995 Prospero Win: each dies in the host loader with app.elf: symbol lookup error: app.elf: undefined symbol: MZb0GKT3mo8 when run for 8 s in a bubblewrap sandbox on Linux. A third, PPSA74126 Stremio Plus, reaches it once sceKernelGetFsSandboxRandomWord is in.

Overlap: #1091 (socket events) inserts its test block at the same place in libkernel/CMakeLists.txt and touches Udp.cpp above the new code; #1459 and #1708 insert a TechnicalDebt line at the same place; #1128 (clang-format) reformats the whole file. Whichever merges second rebases. #1795 (opened after this PR) adds a Linux-only socketpair together with lstat in the same files; this PR covers both platforms and registers its test in ctest.

Tested

  • Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), full build with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON on this branch (based on main 4b005b22): ctest --repeat until-fail:3 467/467; tools/check_test_registration.py and tools/check_conventions.py --base upstream/main pass.
  • New guest_socket_pair test: a stream pair carrying data both ways, poll idle and readable, MSG_PEEK, recvfrom with length 0, the unnamed address from getsockname and getpeername with full and truncated buffers, fcntl F_GETFL/F_SETFL with EAGAIN on an empty non-blocking end, shutdown(SHUT_WR) giving the peer EOF while the other direction stays open, close giving EOF and (Linux) EPIPE on send (the readiness wait before each EOF accepts POLLHUP as well as POLLIN, since WSAPoll reports the peer's FIN as POLLHUP alone, recorded in TechnicalDebt), EBADF on a second close; a SOCK_NONBLOCK | SOCK_CLOEXEC pair non-blocking on both ends; a datagram pair keeping message boundaries with the unnamed sender from recvfrom; a SOCK_SEQPACKET pair (Linux); and the errno for AF_INET, AF_INET6, family 3, type 0, type 3 with and without a protocol, a stray type bit, protocol 6 and a null output, with sv left untouched. Every receive after a send, shutdown or close waits with poll first, since loopback TCP on Windows does not deliver synchronously. Written before the implementation; it failed on the unresolved export and passes with it.
  • The patched libkernel.prx exports the NID (MZb0GKT3mo8).
  • tools/import_audit.py on five of the 19 catalog titles, absent imports before → after (before: the #1578 audit on main 4b005b22 + #1529; after: this branch): PPSA99039 EVO Player 24 → 23, PPSA99082 OpenNOW 36 → 35, PPSA99810 PSBox 64 → 63, PPSA99420 PS5MC 121 → 120, PPSA99003 ProsperoTV 49 → 48. None of them loads yet; the remaining imports are listed in #1578.
  • Windows: the same Build workflow run on this commit in the fork's Actions (merged onto main 6b56175a), run 37828080042: guest_socket_pair passes its three repeats with the SOCK_SEQPACKET and EPIPE cases excluded, full suite 474/474. The run of the previous commit (210ea0cf) aborted at the readiness wait after the peer's shutdown, which accepted POLLIN only. hikazey ran 29a11db3 merged onto main b858fdc4 on Windows 11 with MinGW-w64 GCC 15.2.0: guest_socket_pair 5/5, the full suite 478/478, and 5/5 failures with the connect(first → second) removed from the Windows datagram path (comment); their note that a stream send after the peer closed reports ECONNABORTED on Windows is added to the TechnicalDebt line.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4790414715 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1711). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-08T17:21:17Z. --- ### What Adds `socketpair` to libkernel, in `Socket/src/Udp.cpp` beside `socket` and `accept`, with the semantics of FreeBSD 11's [`kern_socketpair`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/uipc_syscalls.c) and [`socreate`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/uipc_socket.c): - `AF_UNIX` with `SOCK_STREAM`, `SOCK_DGRAM` or `SOCK_SEQPACKET` and protocol 0. `SOCK_NONBLOCK` (0x20000000) puts both ends in non-blocking mode; `SOCK_CLOEXEC` (0x10000000) is accepted and has nothing to do. - Errors as FreeBSD 11's `socreate` and `kern_socketpair` give them: `EAFNOSUPPORT` for an unknown family; `EPROTONOSUPPORT` for type 0 or a non-zero protocol; `EPROTOTYPE` for another type; `EOPNOTSUPP` for `AF_INET` and `AF_INET6` with a type and protocol the inet domain has, whose protocols have no `connect2`; `EFAULT` for a null output. Nothing is written to `sv` on failure. - The ends are registered as guest sockets, so `send`, `recv` (with `MSG_NOSIGNAL` and `MSG_PEEK`), `recvfrom`, `recvmsg`, `shutdown`, `fcntl`, `poll` and `close` work on them as on any guest socket, with the existing errno mapping (`EAGAIN` 35, `EPIPE` 32 on Linux). On a pair end `getsockname` and `getpeername` report FreeBSD's unnamed `AF_UNIX` address (16 bytes, family 1, truncated to the caller's length as `kern_getsockname` does), and `recvfrom`/`recvmsg` report length 0 for a stream and the unnamed address for a datagram, instead of converting the host's address as an IP one. - On Linux the pair is the host's `socketpair(AF_UNIX, ...)`. On Windows, which has none, a `SOCK_STREAM` pair is a loopback TCP connection (listener on 127.0.0.1:0 with `SO_EXCLUSIVEADDRUSE`, connect, accept, peer port checked against the client, `TCP_NODELAY`), the shape OpenSSL's `rio_notifier.c` and curl's `socketpair.c` use on Windows, and a `SOCK_DGRAM` pair is two UDP sockets bound to 127.0.0.1:0 and connected to each other; `SOCK_SEQPACKET` throws there. The differences from the console that remain (full-receiver datagrams, `getpeername` after the peer closed, `connect`/`bind`/`sendto` with an address on a pair end, `ECONNRESET` on Windows) are recorded in TechnicalDebt. Of the 34 relinked titles in the homebrew catalog (#1578), 19 import `socketpair` and 2 stop at it on `main` b858fdc4, PPSA99810 PSBox Cloud Gaming and PPSA99995 Prospero Win: each dies in the host loader with `app.elf: symbol lookup error: app.elf: undefined symbol: MZb0GKT3mo8` when run for 8 s in a bubblewrap sandbox on Linux. A third, PPSA74126 Stremio Plus, reaches it once `sceKernelGetFsSandboxRandomWord` is in. Overlap: #1091 (socket events) inserts its test block at the same place in `libkernel/CMakeLists.txt` and touches `Udp.cpp` above the new code; #1459 and #1708 insert a TechnicalDebt line at the same place; #1128 (clang-format) reformats the whole file. Whichever merges second rebases. #1795 (opened after this PR) adds a Linux-only `socketpair` together with `lstat` in the same files; this PR covers both platforms and registers its test in ctest. ### Tested - Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), full build with `BUILD_TESTING=ON` and `ANYPS5_ENABLE_SPIRV_TOOLS=ON` on this branch (based on `main` 4b005b22): `ctest --repeat until-fail:3` 467/467; `tools/check_test_registration.py` and `tools/check_conventions.py --base upstream/main` pass. - New `guest_socket_pair` test: a stream pair carrying data both ways, `poll` idle and readable, `MSG_PEEK`, `recvfrom` with length 0, the unnamed address from `getsockname` and `getpeername` with full and truncated buffers, `fcntl` `F_GETFL`/`F_SETFL` with `EAGAIN` on an empty non-blocking end, `shutdown(SHUT_WR)` giving the peer EOF while the other direction stays open, `close` giving EOF and (Linux) `EPIPE` on `send` (the readiness wait before each EOF accepts `POLLHUP` as well as `POLLIN`, since `WSAPoll` reports the peer's FIN as `POLLHUP` alone, recorded in TechnicalDebt), `EBADF` on a second close; a `SOCK_NONBLOCK | SOCK_CLOEXEC` pair non-blocking on both ends; a datagram pair keeping message boundaries with the unnamed sender from `recvfrom`; a `SOCK_SEQPACKET` pair (Linux); and the errno for `AF_INET`, `AF_INET6`, family 3, type 0, type 3 with and without a protocol, a stray type bit, protocol 6 and a null output, with `sv` left untouched. Every receive after a send, shutdown or close waits with `poll` first, since loopback TCP on Windows does not deliver synchronously. Written before the implementation; it failed on the unresolved export and passes with it. - The patched `libkernel.prx` exports the NID (`MZb0GKT3mo8`). - `tools/import_audit.py` on five of the 19 catalog titles, absent imports before → after (before: the #1578 audit on `main` 4b005b22 + #1529; after: this branch): PPSA99039 EVO Player 24 → 23, PPSA99082 OpenNOW 36 → 35, PPSA99810 PSBox 64 → 63, PPSA99420 PS5MC 121 → 120, PPSA99003 ProsperoTV 49 → 48. None of them loads yet; the remaining imports are listed in #1578. - Windows: the same Build workflow run on this commit in the fork's Actions (merged onto `main` 6b56175a), [run 37828080042](https://github.com/SleglJan/AnyPS5/actions/runs/37828080042): `guest_socket_pair` passes its three repeats with the `SOCK_SEQPACKET` and `EPIPE` cases excluded, full suite 474/474. The run of the previous commit (210ea0cf) aborted at the readiness wait after the peer's `shutdown`, which accepted `POLLIN` only. hikazey ran 29a11db3 merged onto `main` b858fdc4 on Windows 11 with MinGW-w64 GCC 15.2.0: `guest_socket_pair` 5/5, the full suite 478/478, and 5/5 failures with the `connect(first → second)` removed from the Windows datagram path ([comment](https://github.com/boykopovar/AnyPS5/pull/1711#issuecomment-6067618065)); their note that a stream `send` after the peer closed reports `ECONNABORTED` on Windows is added to the TechnicalDebt line. ### 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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-08 17:26:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T17:26: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
#1832 docs/dev/TechnicalDebt.md L347-348
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065395169 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1711#issuecomment-6065395169). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T17:26: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 | |---|---|---|---| | #1832 | `docs/dev/TechnicalDebt.md` [L347-348](https://github.com/boykopovar/AnyPS5/blob/3bd74c155db06c4dba07441b28f570121475b9b1/docs/dev/TechnicalDebt.md#L347-L348) | | |
hikazey commented 2026-10-08 19:37:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-08T19:37:37Z.


Windows run of the loopback emulation, since the description lists Linux only: Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7, the CI build), Release, configured like CI, Ryzen 9 5900X. This commit merged onto main b858fdc4; only docs/dev/TechnicalDebt.md needed an auto-merge.

build guest_socket_pair
this PR 5/5 passed
connect(first → second) removed from the Windows SOCK_DGRAM path, test kept 5/5 failed
full suite with this PR 478/478 passed

So NativePair works on Windows for both shapes: the stream pair (listener on 127.0.0.1:0 with SO_EXCLUSIVEADDRUSE, connect, accept, peer-port check, TCP_NODELAY) and the connected UDP pair. That covers data both ways, SOCK_NONBLOCK via FIONBIO (EAGAIN 35 on an empty end), getsockname/getpeername reporting the unnamed AF_UNIX address, and the error cases. The test's two #ifndef _WIN32 blocks are skipped here, as intended.

Related, from #1727: on Windows a write after the peer closed a TCP socket gives errno 53 (ECONNABORTED) rather than EPIPE. That applies to the stream pair too, next to the ECONNRESET line this adds to TechnicalDebt.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067618065 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1711#issuecomment-6067618065). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-08T19:37:37Z. --- Windows run of the loopback emulation, since the description lists Linux only: Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7, the CI build), Release, configured like CI, Ryzen 9 5900X. This commit merged onto `main` `b858fdc4`; only `docs/dev/TechnicalDebt.md` needed an auto-merge. | build | `guest_socket_pair` | | --- | --- | | this PR | 5/5 passed | | `connect(first → second)` removed from the Windows `SOCK_DGRAM` path, test kept | 5/5 failed | | full suite with this PR | 478/478 passed | So `NativePair` works on Windows for both shapes: the stream pair (listener on 127.0.0.1:0 with `SO_EXCLUSIVEADDRUSE`, connect, accept, peer-port check, `TCP_NODELAY`) and the connected UDP pair. That covers data both ways, `SOCK_NONBLOCK` via `FIONBIO` (`EAGAIN` 35 on an empty end), `getsockname`/`getpeername` reporting the unnamed `AF_UNIX` address, and the error cases. The test's two `#ifndef _WIN32` blocks are skipped here, as intended. Related, from #1727: on Windows a `write` after the peer closed a TCP socket gives errno 53 (`ECONNABORTED`) rather than `EPIPE`. That applies to the stream pair too, next to the `ECONNRESET` line this adds to TechnicalDebt. AI-assisted: yes (Claude Code).
SleglJan commented 2026-10-08 19:54:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-08T19:54:35Z.


Thanks for the Windows run and the mutation check on the datagram pair. Added to the Tested section, and the ECONNABORTED after the peer's close is now in the TechnicalDebt line (8f316964, docs only).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067897336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1711#issuecomment-6067897336). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-08T19:54:35Z. --- Thanks for the Windows run and the mutation check on the datagram pair. Added to the Tested section, and the `ECONNABORTED` after the peer's close is now in the TechnicalDebt line (8f316964, docs only).
github-actions[bot] commented 2026-10-09 09:36:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T09:36:04Z.


Progress report (4b6ab6b → 8f31696)

➖ System libraries: 84.76% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel socketpair_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078322507 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1711#issuecomment-6078322507). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T09:36:04Z. --- <!-- progress-report --> ### Progress report (4b6ab6b → 8f31696) ➖ **System libraries**: 84.76% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `socketpair_nid_postfix` | </details>
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!1711
No description provided.