fix(libkernel): report SCM_BINTIME and SCM_TIMESTAMP from recvmsg as measured on a PS5 #11141

Merged
orezraey merged 3 commits from upstream-pr/11141 into main 2026-10-26 22:01:32 +00:00
orezraey commented 2026-10-25 19:30:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-25T19:30:00Z.


What

recvmsg on a UDP socket now returns the SCM_BINTIME and SCM_TIMESTAMP control messages while SO_BINTIME or SO_TIMESTAMP is set, as a retail PS5 (firmware 13.60) does. Until now controlLength was always 0, and the options were only kept as bits. Measured with a homebrew probe from a payload process; the source and console log are in this gist (ptstamp).

On the console, over loopback:

  • With no option set, msg_controllen is 0.
  • SO_TIMESTAMP on IPv4 gives one 32-byte record: cmsg_len 32, level 0xffff (SOL_SOCKET), type 2 (SCM_TIMESTAMP), then a 16-byte timeval holding the real time of arrival.
  • SO_BINTIME gives {32, 0xffff, 4} (SCM_BINTIME) and a 16-byte bintime: seconds, then a 64-bit fraction.
  • With both set, SCM_BINTIME comes first, then SCM_TIMESTAMP, and msg_controllen is 64.
  • A 16-byte or 8-byte control buffer gets the first 16 or 8 bytes of the first record, msg_controllen 16 or 8, and MSG_CTRUNC (0x20). The data is intact.
  • On IPv6 only SCM_TIMESTAMP is produced; SO_BINTIME adds nothing, as in FreeBSD 11's ip6_savecontrol.

The records are built in a new ArrivalTimestamps helper. The fraction is computed from nanoseconds as FreeBSD's timespec2bintime does. recvmsg copies as many bytes as the guest buffer holds and sets MSG_CTRUNC when some are cut off, as the console did; FreeBSD 11.4's kern_recvit copies whole messages only, so this partial copy is a measured difference from it. Four cases were not measured and follow 11.4: an empty control buffer with a stamp pending (nothing copied, msg_controllen 0, MSG_CTRUNC), a null msg_control (nothing, no flag), and TCP and Unix sockets (no message).

The time is guest CLOCK_REALTIME, read when the host hands the datagram over. That is later than the moment it reached the socket, and the debt entry says so. Sockets created through libSceNet have their own receive path and are unchanged.

Before and after

recvmsg on a UDP socket PS5 AnyPS5 before AnyPS5 now
no option set msg_controllen 0 0 0
SO_TIMESTAMP one 32-byte SCM_TIMESTAMP record msg_controllen 0 one 32-byte SCM_TIMESTAMP record
SO_BINTIME (IPv4) one 32-byte SCM_BINTIME record msg_controllen 0 one 32-byte SCM_BINTIME record
both options SCM_BINTIME then SCM_TIMESTAMP, 64 bytes msg_controllen 0 SCM_BINTIME then SCM_TIMESTAMP, 64 bytes
8- or 16-byte control buffer first bytes copied, MSG_CTRUNC msg_controllen 0 first bytes copied, MSG_CTRUNC
SO_BINTIME on IPv6 nothing added msg_controllen 0 nothing added

Tested

Windows 11, WinLibs GCC 15.2, full build.

  • guest_udp passes 3 times out of 3. On IPv4 and IPv6 it covers no option, each option and both, with control buffers of 0, 8, 16, 40, 64 and 80 bytes. It checks the record layout and order, msg_controllen, MSG_CTRUNC, that bytes past the copied length stay untouched, and that the seconds fall between CLOCK_REALTIME readings taken before and after the call.
  • The 90 tests matching socket|udp|tcp|net_|recv|send|underscore pass.
  • tools/check_conventions.py passes.

Head 49bcd7549e on main aeff26bd97.

Head 19566dbee8 sets MSG_CTRUNC for an empty control buffer with a stamp pending, as 11.4 does (wachi35676's review), and guest_udp now expects it and checks that a null msg_control gets no flag. guest_udp and guest_socket_options pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL, GCC 13.3); with the old Udp.cpp line guest_udp fails. tools/check_conventions.py passes.

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: #11223 (recvmsg's receive check), #7090 (Unix SCM_RIGHTS) and #10767 (SHUT_RD), which edit the end of recvmsg, and #11116 (mine), which edits the debt entry next to the SO_TIMESTAMP one; merge order only, and the control messages are disjoint (this PR only adds them on IP datagram sockets)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5007943550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11141). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-25T19:30:00Z. --- ### What `recvmsg` on a UDP socket now returns the `SCM_BINTIME` and `SCM_TIMESTAMP` control messages while `SO_BINTIME` or `SO_TIMESTAMP` is set, as a retail PS5 (firmware 13.60) does. Until now `controlLength` was always 0, and the options were only kept as bits. Measured with a homebrew probe from a payload process; the source and console log are in [this gist](https://gist.github.com/orezraey/2e21e60d23ab1ab0e178ddc061bd3dca) (`ptstamp`). On the console, over loopback: - With no option set, `msg_controllen` is 0. - `SO_TIMESTAMP` on IPv4 gives one 32-byte record: `cmsg_len` 32, level `0xffff` (`SOL_SOCKET`), type 2 (`SCM_TIMESTAMP`), then a 16-byte `timeval` holding the real time of arrival. - `SO_BINTIME` gives `{32, 0xffff, 4}` (`SCM_BINTIME`) and a 16-byte `bintime`: seconds, then a 64-bit fraction. - With both set, `SCM_BINTIME` comes first, then `SCM_TIMESTAMP`, and `msg_controllen` is 64. - A 16-byte or 8-byte control buffer gets the first 16 or 8 bytes of the first record, `msg_controllen` 16 or 8, and `MSG_CTRUNC` (0x20). The data is intact. - On IPv6 only `SCM_TIMESTAMP` is produced; `SO_BINTIME` adds nothing, as in FreeBSD 11's `ip6_savecontrol`. The records are built in a new `ArrivalTimestamps` helper. The fraction is computed from nanoseconds as FreeBSD's `timespec2bintime` does. `recvmsg` copies as many bytes as the guest buffer holds and sets `MSG_CTRUNC` when some are cut off, as the console did; FreeBSD 11.4's `kern_recvit` copies whole messages only, so this partial copy is a measured difference from it. Four cases were not measured and follow 11.4: an empty control buffer with a stamp pending (nothing copied, `msg_controllen` 0, `MSG_CTRUNC`), a null `msg_control` (nothing, no flag), and TCP and Unix sockets (no message). The time is guest `CLOCK_REALTIME`, read when the host hands the datagram over. That is later than the moment it reached the socket, and the debt entry says so. Sockets created through libSceNet have their own receive path and are unchanged. **Before and after** | `recvmsg` on a UDP socket | PS5 | AnyPS5 before | AnyPS5 now | |---|---|---|---| | no option set | `msg_controllen` 0 | 0 | 0 | | `SO_TIMESTAMP` | one 32-byte `SCM_TIMESTAMP` record | `msg_controllen` 0 | one 32-byte `SCM_TIMESTAMP` record | | `SO_BINTIME` (IPv4) | one 32-byte `SCM_BINTIME` record | `msg_controllen` 0 | one 32-byte `SCM_BINTIME` record | | both options | `SCM_BINTIME` then `SCM_TIMESTAMP`, 64 bytes | `msg_controllen` 0 | `SCM_BINTIME` then `SCM_TIMESTAMP`, 64 bytes | | 8- or 16-byte control buffer | first bytes copied, `MSG_CTRUNC` | `msg_controllen` 0 | first bytes copied, `MSG_CTRUNC` | | `SO_BINTIME` on IPv6 | nothing added | `msg_controllen` 0 | nothing added | ### 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, WinLibs GCC 15.2, full build. - `guest_udp` passes 3 times out of 3. On IPv4 and IPv6 it covers no option, each option and both, with control buffers of 0, 8, 16, 40, 64 and 80 bytes. It checks the record layout and order, `msg_controllen`, `MSG_CTRUNC`, that bytes past the copied length stay untouched, and that the seconds fall between `CLOCK_REALTIME` readings taken before and after the call. - The 90 tests matching `socket|udp|tcp|net_|recv|send|underscore` pass. - `tools/check_conventions.py` passes. Head `49bcd7549e` on `main` `aeff26bd97`. Head `19566dbee8` sets `MSG_CTRUNC` for an empty control buffer with a stamp pending, as 11.4 does (wachi35676's review), and `guest_udp` now expects it and checks that a null `msg_control` gets no flag. `guest_udp` and `guest_socket_options` pass 3 times out of 3 on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL, GCC 13.3); with the old `Udp.cpp` line `guest_udp` fails. `tools/check_conventions.py` passes. ### 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: #11223 (`recvmsg`'s receive check), #7090 (Unix `SCM_RIGHTS`) and #10767 (`SHUT_RD`), which edit the end of `recvmsg`, and #11116 (mine), which edits the debt entry next to the `SO_TIMESTAMP` one; merge order only, and the control messages are disjoint (this PR only adds them on IP datagram sockets) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 19:35:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:35:49Z.

Review: APPROVED


Reviewed exact head 49bcd7549e8a4baba5bb6cfdb7ca203013c2ca94. The IPv4-only SCM_BINTIME and IPv4/IPv6 SCM_TIMESTAMP records use the expected 32-byte control-message layout, order, arrival seconds/fraction encoding, and capacity-based truncation behavior. The helper is restricted to UDP IP sockets, and the debt entry records that the timestamp is sampled after host receive rather than at kernel packet arrival.

Local checks:

  • GCC 12.2 syntax-only compilation of Udp.cpp and GuestUdp.cpp: passed.
  • python3 tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main...HEAD: passed.

The author reports PS5 observations for both message types/order/truncation and Windows guest_udp coverage across IPv4/IPv6, option combinations and control-buffer sizes. Hosted eligibility, conventions, intake, labels and report pass; product build/test jobs are skipped. I did not independently run the PS5 probe or Windows runtime tests. Declared merge-order dependencies remain as documented. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644899706 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11141#pullrequestreview-5644899706). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T19:35:49Z. **Review:** APPROVED --- Reviewed exact head `49bcd7549e8a4baba5bb6cfdb7ca203013c2ca94`. The IPv4-only `SCM_BINTIME` and IPv4/IPv6 `SCM_TIMESTAMP` records use the expected 32-byte control-message layout, order, arrival seconds/fraction encoding, and capacity-based truncation behavior. The helper is restricted to UDP IP sockets, and the debt entry records that the timestamp is sampled after host receive rather than at kernel packet arrival. Local checks: - GCC 12.2 syntax-only compilation of `Udp.cpp` and `GuestUdp.cpp`: passed. - `python3 tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check upstream/main...HEAD`: passed. The author reports PS5 observations for both message types/order/truncation and Windows `guest_udp` coverage across IPv4/IPv6, option combinations and control-buffer sizes. Hosted eligibility, conventions, intake, labels and report pass; product build/test jobs are skipped. I did not independently run the PS5 probe or Windows runtime tests. Declared merge-order dependencies remain as documented. No blocking issue found.
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!11141
No description provided.