fix(libSceNet): count only datagram bytes in sceNetIoctl FIONREAD on macOS #10628

Merged
maximilianfv merged 1 commit from upstream-pr/10628 into main 2026-10-27 04:38:46 +00:00
maximilianfv commented 2026-10-24 13:32:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-24T13:32:50Z.


What

On macOS, FIONREAD on a datagram socket counts more than the queued payload, so sceNetIoctl(socket, FIONREAD) on a sceNetSocket UDP socket reported 37 after one 5-byte datagram, where guest_sce_net expects 5, as Linux reports. On macOS, sceNetIoctl now asks a non-stream socket for SO_NREAD, the size of the next datagram, and keeps ioctl(FIONREAD) for stream sockets. libkernel's own sockets already do this on macOS (QueuedBytes in libkernel/Socket/src/Udp.cpp, from #7850 by mugurc), and docs/dev/debt/libkernel.md records it. The sceNetIoctl entry in docs/dev/debt/libSceNet.md now says the same for macOS. Linux and Windows are unchanged.

Prior art: iamthehimansh made the same change to sceNetIoctl in commit b2c0e64753 (2026-10-23) of the closed macOS port #8658, so he is credited as co-author on the commit. That commit is not in the PRs #8658 was split into (#10362, #10419). The two differ in small ways: that version sends only NET_SOCK_DGRAM sockets to SO_NREAD and has a code comment and no debt entry; this one sends every non-stream socket and updates the debt entry. #7201 (marion909) and #7332 (ycarogabriel-lgtm) use SO_NREAD only in libkernel's ioctl, not in libSceNet. #9932 and #9948 (ntamas94) read FIONREAD in sceNetGetSockInfo, a different function.

What the console reports is a separate question: #9932 (ntamas94) measured on a PS4 Pro that sceNetGetSockInfo's receive queue length counts each queued datagram's payload plus 16 bytes, as FreeBSD 11.4's receive buffer does. So this change matches neither the console nor plain macOS; it follows libkernel's sockets and the test. The debt entry now says so.

Tested

Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2. main alone does not build on macOS, so I tested in a tree made of mugurc's macOS series (ci/macos-series-2, 27e968974) merged with main at 1e20650e84, plus other macOS fixes that are not yet open PRs and #9231 (7c5d97324). Only this change differed between the two runs.

ctest -R '^(guest_sce_net|guest_inet_address|guest_net_.*|guest_datagram_.*|net_dontwait_.*|guest_socket_error|guest_udp)$' (21 tests): 20 pass both with and without this change. guest_sce_net aborts in both runs, at a different check each time:

  • Without this change it stops at GuestSceNet.cpp:845. After one 5-byte sceNetSendto to itself and a readable poll, sceNetIoctl(socket, 0x4004667f, ...) gives 37 where the test expects 5 (value read with lldb at the abort).
  • With it, that check passes and the test stops later at GuestSceNet.cpp:1930: sceNetResolverStartNtoa6 of "127.0.0.1" must fail with 0x804101E1. This is a separate resolver gap.

I found both stops with lldb on a -O0 rebuild. The test reaches the FIONREAD check only with #9231 (Gonic0) applied, because #9231 fixes an earlier stop at GuestSceNet.cpp:175. This change does not need #9231's code; only the test needs it to get that far.

Not tested on Linux or Windows; the code there is unchanged.

After #10572 merged I rebased onto main fccc63319d. Only the debt-file context changed, the code is byte-identical, and I did not re-run the tests.

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 (#10572 has merged and this branch is rebased onto it)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4995819578 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10628). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-24T13:32:50Z. --- ### What On macOS, `FIONREAD` on a datagram socket counts more than the queued payload, so `sceNetIoctl(socket, FIONREAD)` on a `sceNetSocket` UDP socket reported 37 after one 5-byte datagram, where `guest_sce_net` expects 5, as Linux reports. On macOS, `sceNetIoctl` now asks a non-stream socket for `SO_NREAD`, the size of the next datagram, and keeps `ioctl(FIONREAD)` for stream sockets. libkernel's own sockets already do this on macOS (`QueuedBytes` in `libkernel/Socket/src/Udp.cpp`, from #7850 by mugurc), and `docs/dev/debt/libkernel.md` records it. The `sceNetIoctl` entry in `docs/dev/debt/libSceNet.md` now says the same for macOS. Linux and Windows are unchanged. Prior art: iamthehimansh made the same change to `sceNetIoctl` in commit b2c0e64753 (2026-10-23) of the closed macOS port #8658, so he is credited as co-author on the commit. That commit is not in the PRs #8658 was split into (#10362, #10419). The two differ in small ways: that version sends only `NET_SOCK_DGRAM` sockets to `SO_NREAD` and has a code comment and no debt entry; this one sends every non-stream socket and updates the debt entry. #7201 (marion909) and #7332 (ycarogabriel-lgtm) use `SO_NREAD` only in libkernel's `ioctl`, not in libSceNet. #9932 and #9948 (ntamas94) read `FIONREAD` in `sceNetGetSockInfo`, a different function. What the console reports is a separate question: #9932 (ntamas94) measured on a PS4 Pro that `sceNetGetSockInfo`'s receive queue length counts each queued datagram's payload plus 16 bytes, as FreeBSD 11.4's receive buffer does. So this change matches neither the console nor plain macOS; it follows libkernel's sockets and the test. The debt entry now says so. ### 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. --> Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2. `main` alone does not build on macOS, so I tested in a tree made of mugurc's macOS series (`ci/macos-series-2`, 27e968974) merged with `main` at 1e20650e84, plus other macOS fixes that are not yet open PRs and #9231 (7c5d97324). Only this change differed between the two runs. `ctest -R '^(guest_sce_net|guest_inet_address|guest_net_.*|guest_datagram_.*|net_dontwait_.*|guest_socket_error|guest_udp)$'` (21 tests): 20 pass both with and without this change. `guest_sce_net` aborts in both runs, at a different check each time: - Without this change it stops at `GuestSceNet.cpp:845`. After one 5-byte `sceNetSendto` to itself and a readable poll, `sceNetIoctl(socket, 0x4004667f, ...)` gives 37 where the test expects 5 (value read with lldb at the abort). - With it, that check passes and the test stops later at `GuestSceNet.cpp:1930`: `sceNetResolverStartNtoa6` of `"127.0.0.1"` must fail with `0x804101E1`. This is a separate resolver gap. I found both stops with lldb on a `-O0` rebuild. The test reaches the `FIONREAD` check only with #9231 (Gonic0) applied, because #9231 fixes an earlier stop at `GuestSceNet.cpp:175`. This change does not need #9231's code; only the test needs it to get that far. Not tested on Linux or Windows; the code there is unchanged. After #10572 merged I rebased onto `main` `fccc63319d`. Only the debt-file context changed, the code is byte-identical, and I did not re-run the tests. ### 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: <!-- #PR, or none --> none (#10572 has merged and this branch is rebased onto it) - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 13:39:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:39:24Z.

Review: APPROVED


Reviewed exact head 57333d34904a5f01b8f2b0d480265960cf3ea542. The macOS-only branch preserves stream FIONREAD behavior and uses SO_NREAD for non-stream sockets, matching the platform-specific mismatch reproduced in the PR body and the existing guest assertion for a 5-byte datagram. Linux and Windows code paths are unchanged. The debt text now records the platform behavior and distinguishes the PS4 Pro sceNetGetSockInfo measurement from this sceNetIoctl call.

git diff --check upstream/main...HEAD and python3 tools/check_conventions.py --base upstream/main --head HEAD pass on this exact head. Hosted eligibility, conventions, intake, and labels pass; product test jobs were skipped. The macOS 21-test run and before/after debugger evidence are author-reported; I did not independently run macOS. I found no blocking issue in this focused platform fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638442401 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10628#pullrequestreview-5638442401). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T13:39:24Z. **Review:** APPROVED --- Reviewed exact head `57333d34904a5f01b8f2b0d480265960cf3ea542`. The macOS-only branch preserves stream `FIONREAD` behavior and uses `SO_NREAD` for non-stream sockets, matching the platform-specific mismatch reproduced in the PR body and the existing guest assertion for a 5-byte datagram. Linux and Windows code paths are unchanged. The debt text now records the platform behavior and distinguishes the PS4 Pro `sceNetGetSockInfo` measurement from this `sceNetIoctl` call. `git diff --check upstream/main...HEAD` and `python3 tools/check_conventions.py --base upstream/main --head HEAD` pass on this exact head. Hosted eligibility, conventions, intake, and labels pass; product test jobs were skipped. The macOS 21-test run and before/after debugger evidence are author-reported; I did not independently run macOS. I found no blocking issue in this focused platform fix.
MarcZX10 commented 2026-10-24 13:48:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:48:53Z.

Review: CHANGES_REQUESTED


Follow-up on the new overlap check at exact head 57333d34904a5f01b8b2d0b480265960cf3ea542: I manually compared the shared docs/dev/debt/libSceNet.md hunk with #10572. git merge-tree --write-tree upstream/pr/10572 upstream/pr/10628 reports a content conflict in that file: both PRs edit the adjacent sceNetUsleep / sceNetIoctl entries in one hunk. The two code changes are distinct, and the conflict is straightforward to resolve by retaining both updated entries, but the PRs still need an agreed merge order and the dependent PR should record Depends on: #N as required by CONTRIBUTING. The current checklist says Depends on: none.

Please coordinate with #10572's author and record the agreed order/dependency. My earlier approval covered the implementation; this review requests changes only for the unresolved documentation merge coordination. The newly posted macOS measurements on #10628 are useful additional host evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638472915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10628#pullrequestreview-5638472915). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T13:48:53Z. **Review:** CHANGES_REQUESTED --- Follow-up on the new overlap check at exact head `57333d34904a5f01b8b2d0b480265960cf3ea542`: I manually compared the shared `docs/dev/debt/libSceNet.md` hunk with #10572. `git merge-tree --write-tree upstream/pr/10572 upstream/pr/10628` reports a content conflict in that file: both PRs edit the adjacent `sceNetUsleep` / `sceNetIoctl` entries in one hunk. The two code changes are distinct, and the conflict is straightforward to resolve by retaining both updated entries, but the PRs still need an agreed merge order and the dependent PR should record `Depends on: #N` as required by CONTRIBUTING. The current checklist says `Depends on: none`. Please coordinate with #10572's author and record the agreed order/dependency. My earlier approval covered the implementation; this review requests changes only for the unresolved documentation merge coordination. The newly posted macOS measurements on #10628 are useful additional host evidence.
MarcZX10 commented 2026-10-24 14:12:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:12:43Z.

Review: APPROVED


Follow-up on exact head 57333d34904a5f01b8f2b0d480265960cf3ea542. The merge-order blocker is resolved: #10572's author confirms it lands first, and this PR now records Depends on: #10572 and will rebase to retain both debt entries. My prior code review remains applicable: the macOS-only path uses SO_NREAD for non-stream sockets while preserving stream FIONREAD; Linux and Windows are unchanged. The author reports 20 of 21 focused tests pass in both runs; the one guest_sce_net stop is a separate resolver expectation, and the target FIONREAD assertion passes only with this change. Hosted policy checks pass and product builds were skipped. I did not independently run the macOS test suite. No blocker remains.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638552541 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10628#pullrequestreview-5638552541). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T14:12:43Z. **Review:** APPROVED --- Follow-up on exact head `57333d34904a5f01b8f2b0d480265960cf3ea542`. The merge-order blocker is resolved: #10572's author confirms it lands first, and this PR now records `Depends on: #10572` and will rebase to retain both debt entries. My prior code review remains applicable: the macOS-only path uses `SO_NREAD` for non-stream sockets while preserving stream `FIONREAD`; Linux and Windows are unchanged. The author reports 20 of 21 focused tests pass in both runs; the one `guest_sce_net` stop is a separate resolver expectation, and the target `FIONREAD` assertion passes only with this change. Hosted policy checks pass and product builds were skipped. I did not independently run the macOS test suite. No blocker remains.
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!10628
No description provided.