fix(libkernel): answer zero-length datagram receives and recvmsg flags as measured on a PS5 #10308

Merged
orezraey merged 4 commits from upstream-pr/10308 into main 2026-10-24 07:46:45 +00:00
orezraey commented 2026-10-23 17:13:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T17:13:35Z.


What

Socket receives measured on a retail PS5 (firmware 13.60) through libkernel_sys.sprx from a payload process. The sources and console logs are in this gist (precv, precv2).

The console:

  • returns 0 at once for a zero-length recvfrom with MSG_DONTWAIT on a datagram socket with nothing queued, and a zero-length receive dequeues the next datagram when one is queued;
  • returns the flags passed to recvmsg in msg_flags, with MSG_TRUNC added for a truncated datagram (0x12 with MSG_PEEK, 0x90 with MSG_DONTWAIT);
  • truncates a datagram to the buffer, keeps it on MSG_PEEK and reports the sender, as AnyPS5 already does, so the libkernel half of that debt entry is removed;
  • answers a zero-length recv on a stream socket with 0, or ENOTCONN when unconnected, a read of 0 bytes with 0 even when unconnected, pread and lseek with ESPIPE, and discards data buffered before shutdown(SHUT_RD), all as AnyPS5 already does on Windows.

AnyPS5 asked the host for a zero-length datagram receive, which blocks on an empty queue, and reported only MSG_TRUNC in msg_flags. A zero-length receive on a socket that is not a stream now polls the host and returns 0 when nothing is queued; otherwise the host receive dequeues the datagram as before. recvmsg now keeps the caller's flags, and a zero-length recvmsg on an empty datagram socket also returns 0 at once. Three cases follow FreeBSD 11.4 rather than a console measurement: the blocking zero-length recv on an empty datagram socket (soreceive_dgram returns 0 for uio_resid == 0 before its SS_NBIO and sbwait checks; the blocking zero-length call in the logs is read, which dofileread finishes before the socket layer), a zero-length MSG_PEEK keeping the datagram (soreceive_generic), and the empty recvmsg, since recvmsg was measured only with a datagram queued. The debt entry says so, and the libSceNet sceNetRecv/sceNetRecvfrom half of the removed entry stays, since only libkernel_sys was measured.

Tested

Windows 11, MinGW (winlibs).

  • guest_datagram_recv and guest_datagram_recvfrom check the zero-length receives (the blocking one hangs until the test times out without this change), and guest_udp checks the recvmsg flags. The socket tests pass three runs in a row.
  • The full suite (-E "^agc_|^select_tests$") passes apart from guest_kqueue, which fails only under -j 14, as on main.
  • tools/check_conventions.py passes.
  • tools/pr_overlap.py: #7090 touch nearby lines; whichever lands second keeps both.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986545368 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10308). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T17:13:35Z. --- ### What Socket receives measured on a retail PS5 (firmware 13.60) through `libkernel_sys.sprx` from a payload process. The sources and console logs are in [this gist](https://gist.github.com/orezraey/4c2c8c0c210d1329bbe6ed71ea9c32f4) (`precv`, `precv2`). The console: - returns 0 at once for a zero-length `recvfrom` with `MSG_DONTWAIT` on a datagram socket with nothing queued, and a zero-length receive dequeues the next datagram when one is queued; - returns the flags passed to `recvmsg` in `msg_flags`, with `MSG_TRUNC` added for a truncated datagram (0x12 with `MSG_PEEK`, 0x90 with `MSG_DONTWAIT`); - truncates a datagram to the buffer, keeps it on `MSG_PEEK` and reports the sender, as AnyPS5 already does, so the libkernel half of that debt entry is removed; - answers a zero-length `recv` on a stream socket with 0, or `ENOTCONN` when unconnected, a `read` of 0 bytes with 0 even when unconnected, `pread` and `lseek` with `ESPIPE`, and discards data buffered before `shutdown(SHUT_RD)`, all as AnyPS5 already does on Windows. AnyPS5 asked the host for a zero-length datagram receive, which blocks on an empty queue, and reported only `MSG_TRUNC` in `msg_flags`. A zero-length receive on a socket that is not a stream now polls the host and returns 0 when nothing is queued; otherwise the host receive dequeues the datagram as before. `recvmsg` now keeps the caller's flags, and a zero-length `recvmsg` on an empty datagram socket also returns 0 at once. Three cases follow FreeBSD 11.4 rather than a console measurement: the blocking zero-length `recv` on an empty datagram socket (`soreceive_dgram` returns 0 for `uio_resid == 0` before its `SS_NBIO` and `sbwait` checks; the blocking zero-length call in the logs is `read`, which `dofileread` finishes before the socket layer), a zero-length `MSG_PEEK` keeping the datagram (`soreceive_generic`), and the empty `recvmsg`, since `recvmsg` was measured only with a datagram queued. The debt entry says so, and the libSceNet `sceNetRecv`/`sceNetRecvfrom` half of the removed entry stays, since only libkernel_sys was measured. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, MinGW (winlibs). - `guest_datagram_recv` and `guest_datagram_recvfrom` check the zero-length receives (the blocking one hangs until the test times out without this change), and `guest_udp` checks the `recvmsg` flags. The socket tests pass three runs in a row. - The full suite (`-E "^agc_|^select_tests$"`) passes apart from `guest_kqueue`, which fails only under `-j 14`, as on `main`. - `tools/check_conventions.py` passes. - `tools/pr_overlap.py`: #7090 touch nearby lines; whichever lands second keeps both. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 05:19:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:19:30Z.

Review: COMMENTED


Checked against main 7966e590ef, FreeBSD 11.4 sys/kern/uipc_socket.c / uipc_syscalls.c / sys_generic.c (release/11.4.0) and the precv/precv2 logs in the gist:

  • This merges cleanly into current main, and main's receive paths haven't changed since your base. The msg_flags change matches both the logs (0x10, 0x12, 0x90) and FreeBSD: sys_recvmsg seeds msg_flags with the call flags (L1325), and soreceive_dgram ORs MSG_TRUNC back in (L2321, L2325).
  • Small description fix: I couldn't find a zero-length MSG_PEEK call in precv.c or precv2.c, so "a zero-length MSG_PEEK keeps it" looks FreeBSD-derived (soreceive_generic, uio_resid == 0 at L1575) rather than measured.
  • Likewise for "blocking or not": the console's zero-length recvfrom on an empty queue was measured only with MSG_DONTWAIT. The blocking zero-length case in the logs is read, which FreeBSD finishes in dofileread before the socket layer (sys_generic.c L367-371), which is also why udp_read0_with_data doesn't dequeue. The blocking recv behaviour does follow FreeBSD, since soreceive_dgram returns 0 for uio_resid == 0 before its SS_NBIO and sbwait checks (L2199-2202). It might be worth saying so in the debt entry.
  • The removed debt entry also covered sceNetRecv/sceNetRecvfrom in libSceNet, but the gist only measures libkernel_sys. Could you keep the libSceNet half, or note why the measurement carries over?
  • I couldn't run the Windows paths myself; my reading of the Winsock docs suggests the WSAEMSGSIZE handling still matches.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636984703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10308#pullrequestreview-5636984703). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T05:19:30Z. **Review:** COMMENTED --- Checked against main 7966e590ef, FreeBSD 11.4 `sys/kern/uipc_socket.c` / `uipc_syscalls.c` / `sys_generic.c` (release/11.4.0) and the precv/precv2 logs in the gist: - This merges cleanly into current main, and main's receive paths haven't changed since your base. The `msg_flags` change matches both the logs (0x10, 0x12, 0x90) and FreeBSD: `sys_recvmsg` seeds `msg_flags` with the call flags (L1325), and `soreceive_dgram` ORs `MSG_TRUNC` back in (L2321, L2325). - Small description fix: I couldn't find a zero-length `MSG_PEEK` call in precv.c or precv2.c, so "a zero-length `MSG_PEEK` keeps it" looks FreeBSD-derived (`soreceive_generic`, `uio_resid == 0` at L1575) rather than measured. - Likewise for "blocking or not": the console's zero-length `recvfrom` on an empty queue was measured only with `MSG_DONTWAIT`. The blocking zero-length case in the logs is `read`, which FreeBSD finishes in `dofileread` before the socket layer (sys_generic.c L367-371), which is also why `udp_read0_with_data` doesn't dequeue. The blocking `recv` behaviour does follow FreeBSD, since `soreceive_dgram` returns 0 for `uio_resid == 0` before its SS_NBIO and sbwait checks (L2199-2202). It might be worth saying so in the debt entry. - The removed debt entry also covered `sceNetRecv`/`sceNetRecvfrom` in libSceNet, but the gist only measures libkernel_sys. Could you keep the libSceNet half, or note why the measurement carries over? - I couldn't run the Windows paths myself; my reading of the Winsock docs suggests the WSAEMSGSIZE handling still matches. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!10308
No description provided.