fix(libkernel): read and write guest sockets #1727

Merged
SleglJan merged 1 commit from upstream-pr/1727 into main 2026-10-10 04:41:04 +00:00
SleglJan commented 2026-10-08 17:40:45 +00:00 (Migrated from github.com)

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


What

read, write, _read and _write on a guest socket descriptor (0x10000000 and up) went to the host as if the number were a host descriptor, so sceKernelRead and sceKernelWrite threw (sceKernelWrite: write failed, fd=…, errno=9) on the first byte a title moved through a socket that way. On the console both are the socket's recv and send without flags (soo_read/soo_write).

  • GuestSockets::Read and GuestSockets::Write in the socket runtime forward to recv and send with flags 0, so the socket layer's errno applies: EAGAIN (35) on an empty non-blocking socket, EPIPE (32) after the peer closed, EBADF (9) on a closed descriptor. A zero-length read returns 0 without touching the socket, as dofileread does, and a read longer than INT_MAX is clamped.
  • read, write, _read and _write route descriptors at or above GuestSockets::FirstDescriptor to them; sceKernelRead and sceKernelWrite do the same, before their null-buffer check, and return the kernel error (0x80020023 for EAGAIN, 0x80020009 for EBADF) instead of throwing.
  • Recorded in TechnicalDebt: EPIPE raises the host's SIGPIPE on Linux, which the guest can neither ignore nor suppress, and nothing on Windows (the console raises it unless SO_NOSIGPIPE); a write above INT_MAX bytes is EMSGSIZE; readv, writev, pread, pwrite, their sceKernel forms and AIO on a guest socket still go to the host.

The 15 catalog titles whose OpenSSL QUIC notifier creates a socketpair (#1711, open) signal it with write and drain it with read (rio_notifier.c through writesocket/readsocket); the same applies to any TCP socket a title reads with read.

Overlap: #1307 rewrites the four wrappers in Stdio.cpp (the socket branch has to stay first); #1711 and #1708 insert a TechnicalDebt line at the same place; #1091 and #1738 add declarations to SocketRuntime.hpp, and #1091 code to Udp.cpp near the table, where this adds two declarations and two functions at the end. Whichever merges second rebases. The pr-overlap report's __error_nid_postfix rows (#1613, #1616, #1627, #1675) are the shared extern "C" declaration of the errno accessor that Open.cpp now adds, not an export.

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 b83e849f): ctest --repeat until-fail:3 467/467; tools/check_test_registration.py and tools/check_conventions.py --base upstream/main pass.
  • New guest_socket_read_write test over a TCP loopback pair: write/read, _write/_read and sceKernelWrite/sceKernelRead carrying data both ways; EAGAIN and 0x80020023 on an empty non-blocking socket; zero-length reads returning 0; 0 from both forms after the peer closed; (Linux) EPIPE on write after the peer closed, with the host's SIGPIPE ignored by the test since a title cannot; EBADF and 0x80020009 from every entry point on a closed descriptor. Each receive waits with poll first, accepting POLLHUP after the close. Written before the change: on main it aborts with the sceKernelWrite throw above; it passes with the change.
  • Windows: the same Build workflow run on this commit in the fork's Actions (merged onto main 6b56175a), run 37822108157: guest_socket_read_write passes its three repeats, full suite 474/474; the test excludes only the EPIPE case there, like guest_tcp.
  • Rebased onto main db06355f (head 9d0b1057; git range-diff shows only context lines changed). Local full ctest: 541/542; the failure, agc_driver_dcc_keys_first_write, fails on main too (#2188). The fork's Build workflow on this head merged into main 8b393b6e, run 37996682292: the relinker jobs pass and guest_socket_read_write passes on Linux; Linux fails only guest_video_out_resolution_status (a main test that aborts on the runner's offscreen SDL driver), and Windows stops building libSceVideoOut.prx on the duplicate export from #2462, both also on main.

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:4790626489 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1727). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-08T17:40:45Z. --- ### What `read`, `write`, `_read` and `_write` on a guest socket descriptor (0x10000000 and up) went to the host as if the number were a host descriptor, so `sceKernelRead` and `sceKernelWrite` threw (`sceKernelWrite: write failed, fd=…, errno=9`) on the first byte a title moved through a socket that way. On the console both are the socket's `recv` and `send` without flags ([`soo_read`/`soo_write`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/sys_socket.c)). - `GuestSockets::Read` and `GuestSockets::Write` in the socket runtime forward to `recv` and `send` with flags 0, so the socket layer's errno applies: `EAGAIN` (35) on an empty non-blocking socket, `EPIPE` (32) after the peer closed, `EBADF` (9) on a closed descriptor. A zero-length `read` returns 0 without touching the socket, as `dofileread` does, and a read longer than `INT_MAX` is clamped. - `read`, `write`, `_read` and `_write` route descriptors at or above `GuestSockets::FirstDescriptor` to them; `sceKernelRead` and `sceKernelWrite` do the same, before their null-buffer check, and return the kernel error (`0x80020023` for `EAGAIN`, `0x80020009` for `EBADF`) instead of throwing. - Recorded in TechnicalDebt: `EPIPE` raises the host's `SIGPIPE` on Linux, which the guest can neither ignore nor suppress, and nothing on Windows (the console raises it unless `SO_NOSIGPIPE`); a `write` above `INT_MAX` bytes is `EMSGSIZE`; `readv`, `writev`, `pread`, `pwrite`, their `sceKernel` forms and AIO on a guest socket still go to the host. The 15 catalog titles whose OpenSSL QUIC notifier creates a `socketpair` (#1711, open) signal it with `write` and drain it with `read` (`rio_notifier.c` through `writesocket`/`readsocket`); the same applies to any TCP socket a title reads with `read`. Overlap: #1307 rewrites the four wrappers in `Stdio.cpp` (the socket branch has to stay first); #1711 and #1708 insert a TechnicalDebt line at the same place; #1091 and #1738 add declarations to `SocketRuntime.hpp`, and #1091 code to `Udp.cpp` near the table, where this adds two declarations and two functions at the end. Whichever merges second rebases. The pr-overlap report's `__error_nid_postfix` rows (#1613, #1616, #1627, #1675) are the shared `extern "C"` declaration of the errno accessor that `Open.cpp` now adds, not an export. ### 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` b83e849f): `ctest --repeat until-fail:3` 467/467; `tools/check_test_registration.py` and `tools/check_conventions.py --base upstream/main` pass. - New `guest_socket_read_write` test over a TCP loopback pair: `write`/`read`, `_write`/`_read` and `sceKernelWrite`/`sceKernelRead` carrying data both ways; `EAGAIN` and `0x80020023` on an empty non-blocking socket; zero-length reads returning 0; 0 from both forms after the peer closed; (Linux) `EPIPE` on `write` after the peer closed, with the host's `SIGPIPE` ignored by the test since a title cannot; `EBADF` and `0x80020009` from every entry point on a closed descriptor. Each receive waits with `poll` first, accepting `POLLHUP` after the close. Written before the change: on `main` it aborts with the `sceKernelWrite` throw above; it passes with the change. - Windows: the same Build workflow run on this commit in the fork's Actions (merged onto `main` 6b56175a), [run 37822108157](https://github.com/SleglJan/AnyPS5/actions/runs/37822108157): `guest_socket_read_write` passes its three repeats, full suite 474/474; the test excludes only the `EPIPE` case there, like `guest_tcp`. - Rebased onto `main` db06355f (head 9d0b1057; `git range-diff` shows only context lines changed). Local full ctest: 541/542; the failure, `agc_driver_dcc_keys_first_write`, fails on `main` too (#2188). The fork's Build workflow on this head merged into `main` 8b393b6e, [run 37996682292](https://github.com/SleglJan/AnyPS5/actions/runs/37996682292): the relinker jobs pass and `guest_socket_read_write` passes on Linux; Linux fails only `guest_video_out_resolution_status` (a `main` test that aborts on the runner's offscreen SDL driver), and Windows stops building `libSceVideoOut.prx` on the duplicate export from #2462, both also on `main`. ### 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:48:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T17:48:32Z.


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
#1755 core/libs/prx/libkernel/File/src/Open.cpp L6
#1764 core/libs/prx/libkernel/File/src/Open.cpp L6
#2144 core/libs/prx/libkernel/File/src/Open.cpp L135-138, L148-156
#2545 core/libs/tests/GuestSocketReadWrite.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065769039 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1727#issuecomment-6065769039). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T17:48:32Z. --- <!-- 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 | |---|---|---|---| | #1755 | `core/libs/prx/libkernel/File/src/Open.cpp` [L6](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libkernel/File/src/Open.cpp#L6) | | | | #1764 | `core/libs/prx/libkernel/File/src/Open.cpp` [L6](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libkernel/File/src/Open.cpp#L6) | | | | #2144 | `core/libs/prx/libkernel/File/src/Open.cpp` [L135-138](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libkernel/File/src/Open.cpp#L135-L138), [L148-156](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libkernel/File/src/Open.cpp#L148-L156) | | | | #2545 | | | `core/libs/tests/GuestSocketReadWrite.cpp` |
hikazey commented 2026-10-08 19:14:54 +00:00 (Migrated from github.com)

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


Windows run, 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_read_write
this PR 5/5 passed
sceKernelWrite's socket branch removed, test kept 5/5 failed: sceKernelWrite: write failed, fd=268435457, errno=9, the original throw
full suite with this PR 478/478 passed

One Windows difference the test doesn't cover, since the peer-closed write case is #ifndef _WIN32. I added a local probe there for Windows: close(client), then write_nid_postfix(server, …) in a loop. In all 5 runs the second write returns -1 with errno 53 (ECONNABORTED): Winsock's WSAECONNABORTED, mapped by NativeError. On Linux, and in FreeBSD's sosend, the same sequence gives 32 (EPIPE). A title that checks for EPIPE after the peer closed would see a different errno on Windows. That may be worth a line next to the SIGPIPE entry in TechnicalDebt, or a follow-up that maps it for send/write on a connected stream socket. I haven't checked how the console reports a reset (as opposed to an orderly close) there.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067232145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1727#issuecomment-6067232145). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-08T19:14:54Z. --- Windows run, 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_read_write` | | --- | --- | | this PR | 5/5 passed | | `sceKernelWrite`'s socket branch removed, test kept | 5/5 failed: `sceKernelWrite: write failed, fd=268435457, errno=9`, the original throw | | full suite with this PR | 478/478 passed | One Windows difference the test doesn't cover, since the peer-closed `write` case is `#ifndef _WIN32`. I added a local probe there for Windows: `close(client)`, then `write_nid_postfix(server, …)` in a loop. In all 5 runs the second write returns -1 with **errno 53 (`ECONNABORTED`)**: Winsock's `WSAECONNABORTED`, mapped by `NativeError`. On Linux, and in FreeBSD's `sosend`, the same sequence gives 32 (`EPIPE`). A title that checks for `EPIPE` after the peer closed would see a different errno on Windows. That may be worth a line next to the `SIGPIPE` entry in TechnicalDebt, or a follow-up that maps it for `send`/`write` on a connected stream socket. I haven't checked how the console reports a reset (as opposed to an orderly close) there. AI-assisted: yes (Claude Code).
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!1727
No description provided.