fix(libkernel): drop the datagram whose recv faulted on macOS, as recvfrom does #25134

Open
Gonic0 wants to merge 2 commits from upstream-pr/25134 into main
Gonic0 commented 2026-11-21 17:32:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-11-21T17:32:42Z.


What

guest_udp fails on macOS since 2c7575b2ea (fix(libkernel): check the send and recv buffers as sendto and recvfrom do), at GuestUdp.cpp:669: after a recv on a datagram socket fails with EFAULT, the next recv must return the next datagram, as FreeBSD drops the one whose copy out faulted.

On macOS the faulted datagram stays queued. 6ad2973348 (fix(libkernel): drop a datagram whose receive faulted on macOS) handles that in recvfrom with DropFaultedDatagram, but recv takes its own path to ::recv and returned EFAULT without the drop, so the next recv got the faulted datagram again. recv now calls DropFaultedDatagram after an EFAULT on macOS, as recvfrom does. recvmsg receives into a host buffer before copying to the guest, so it already consumes the datagram. Linux and Windows are unchanged.

Tested

macOS 27.3, Apple M5 Pro, Apple clang, x86-64 Release under Rosetta 2, BUILD_TESTING=ON. Tree: main 6d55d9b269 plus #17425, #11049 and #25116, which main still needs to build on macOS (#24117). main has since moved to 0e001115a9 without changes to Udp.cpp or GuestUdp.cpp; this branch is rebased onto it.

  • guest_udp: fails without this change (Require at line 669, found by temporarily printing the failing line); passes 5 of 5 runs with it.
  • ctest -R '(udp|socket|tcp|sce_net|poll|recv|send|net_)' -j6, 126 tests: 5 fail without this change, 4 with it. The remaining four (guest_sce_net, guest_socket_options, guest_poll, guest_socket_port_zero) fail the same way with and without it and are listed in #24117.
  • Linux and Windows: not built; the change is inside #ifdef __APPLE__.
  • tools/check_conventions.py passes.

tools/pr_overlap.py --branch could not compare five open pull requests that touch Udp.cpp because they conflict with main; compared by hand:

  • #25017 changes the lines next to this one in recv_nid_postfix (ReceiveEnded, the WSAEMSGSIZE line), #11896 replaces the end of recv_nid_postfix with a Receive helper, and #18190 moves recv_nid_postfix. Whichever merges later rebases; neither change depends on the other.
  • #8605 and #23341 do not touch this part of recv_nid_postfix.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • On macOS, UDP receives that encounter a guest-memory fault now discard a datagram before reporting the receive error. Other receive errors continue to be reported unchanged.
  • Documentation
    • Added a note explaining that this handling may discard a subsequent datagram if another reader takes the original datagram first.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5430572812 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25134). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-11-21T17:32:42Z. --- ### What `guest_udp` fails on macOS since 2c7575b2ea (`fix(libkernel): check the send and recv buffers as sendto and recvfrom do`), at `GuestUdp.cpp:669`: after a `recv` on a datagram socket fails with `EFAULT`, the next `recv` must return the next datagram, as FreeBSD drops the one whose copy out faulted. On macOS the faulted datagram stays queued. 6ad2973348 (`fix(libkernel): drop a datagram whose receive faulted on macOS`) handles that in `recvfrom` with `DropFaultedDatagram`, but `recv` takes its own path to `::recv` and returned `EFAULT` without the drop, so the next `recv` got the faulted datagram again. `recv` now calls `DropFaultedDatagram` after an `EFAULT` on macOS, as `recvfrom` does. `recvmsg` receives into a host buffer before copying to the guest, so it already consumes the datagram. Linux and Windows are unchanged. ### 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. --> macOS 27.3, Apple M5 Pro, Apple clang, x86-64 Release under Rosetta 2, `BUILD_TESTING=ON`. Tree: `main` `6d55d9b269` plus #17425, #11049 and #25116, which `main` still needs to build on macOS (#24117). `main` has since moved to `0e001115a9` without changes to `Udp.cpp` or `GuestUdp.cpp`; this branch is rebased onto it. - `guest_udp`: fails without this change (`Require` at line 669, found by temporarily printing the failing line); passes 5 of 5 runs with it. - `ctest -R '(udp|socket|tcp|sce_net|poll|recv|send|net_)' -j6`, 126 tests: 5 fail without this change, 4 with it. The remaining four (`guest_sce_net`, `guest_socket_options`, `guest_poll`, `guest_socket_port_zero`) fail the same way with and without it and are listed in #24117. - Linux and Windows: not built; the change is inside `#ifdef __APPLE__`. - `tools/check_conventions.py` passes. `tools/pr_overlap.py --branch` could not compare five open pull requests that touch `Udp.cpp` because they conflict with `main`; compared by hand: - #25017 changes the lines next to this one in `recv_nid_postfix` (`ReceiveEnded`, the `WSAEMSGSIZE` line), #11896 replaces the end of `recv_nid_postfix` with a `Receive` helper, and #18190 moves `recv_nid_postfix`. Whichever merges later rebases; neither change depends on the other. - #8605 and #23341 do not touch this part of `recv_nid_postfix`. ### 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 (none added) - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * On macOS, UDP receives that encounter a guest-memory fault now discard a datagram before reporting the receive error. Other receive errors continue to be reported unchanged. * **Documentation** * Added a note explaining that this handling may discard a subsequent datagram if another reader takes the original datagram first. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-23 00:34:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T00:34:05Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected code follows the existing macOS recvfrom handling and preserves the mapped error return.

What I ran

Reviewed head d37121dfbea137493e75c3c29668fc7907df0b17. No commands or tests were run. The author reports macOS tests and convention checks; these were not run by me.

Results

Check Result Note
Diff and changed code passed One changed file inspected
Tests not run Author-reported results only
Platform builds not run Linux and Windows unverified

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests, builds, and runtime behavior were not independently verified. The only changed file, core/libs/prx/libkernel/Socket/src/Udp.cpp, was read at the changed function, helper, and related recvfrom path. No files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6015388272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25134#pullrequestreview-6015388272). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T00:34:05Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected code follows the existing macOS `recvfrom` handling and preserves the mapped error return. ## What I ran Reviewed head `d37121dfbea137493e75c3c29668fc7907df0b17`. No commands or tests were run. The author reports macOS tests and convention checks; these were not run by me. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | One changed file inspected | | Tests | not run | Author-reported results only | | Platform builds | not run | Linux and Windows unverified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Tests, builds, and runtime behavior were not independently verified. The only changed file, `core/libs/prx/libkernel/Socket/src/Udp.cpp`, was read at the changed function, helper, and related `recvfrom` path. No files were excluded by the manifest. ## To get this to a good state Nothing is required.
NobleBrains commented 2026-12-01 00:21:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NobleBrains on 2026-12-01T00:21:59Z.

Review: APPROVED


Confirmed on macOS 15.

Setup: Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, BUILD_TESTING=ON. These are the OS and compiler generation of the macos-15 runner.
Tree: main 93d64f2d1e + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (main does not build on macOS without them, see #28168), + #22091, #28369, #28075, #28076, #28284, then #17878, #20161, #25134, #26806, #22932, #28373, #27828, #27950, #27582, #24311 merged one after another. All merged cleanly.

guest_udp failures
same tree before this PR 3/3
with this PR at d37121dfbe 0/5

Full ctest -j 6 after all of them: 7047/7066 pass. Compared with the run before the second group of merges, no test fails that passed before.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6122949281 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25134#pullrequestreview-6122949281). Originally posted by [NobleBrains](https://github.com/NobleBrains) on 2026-12-01T00:21:59Z. **Review:** APPROVED --- Confirmed on macOS 15. **Setup:** Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, `BUILD_TESTING=ON`. These are the OS and compiler generation of the `macos-15` runner. **Tree:** `main` `93d64f2d1e` + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (`main` does not build on macOS without them, see #28168), + #22091, #28369, #28075, #28076, #28284, then #17878, #20161, #25134, #26806, #22932, #28373, #27828, #27950, #27582, #24311 merged one after another. All merged cleanly. | `guest_udp` | failures | |---|---| | same tree before this PR | 3/3 | | with this PR at `d37121dfbe` | 0/5 | **Full `ctest -j 6`** after all of them: 7047/7066 pass. Compared with the run before the second group of merges, no test fails that passed before. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 16:56:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:56:16Z.

Review: COMMENTED


The Darwin EFAULT path calls recv a second time to discard the faulted datagram. The original receive and the discard are separate operations, so another reader of the socket can consume that datagram before the discard and cause this call to drop the following one. Please keep a faulting call's packet consumption within one receive operation or coordinate the receive paths so they cannot discard another reader's packet.

The integrated Linux guest_socket_options and guest_socket_port_zero controls passed. The new receive behavior is specific to Darwin and still needs verification there.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6174410631 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25134#pullrequestreview-6174410631). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T16:56:16Z. **Review:** COMMENTED --- The Darwin `EFAULT` path calls `recv` a second time to discard the faulted datagram. The original receive and the discard are separate operations, so another reader of the socket can consume that datagram before the discard and cause this call to drop the following one. Please keep a faulting call's packet consumption within one receive operation or coordinate the receive paths so they cannot discard another reader's packet. The integrated Linux `guest_socket_options` and `guest_socket_port_zero` controls passed. The new receive behavior is specific to Darwin and still needs verification there. AI-assisted: yes.
MarcZX10 commented 2026-12-03 20:56:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-03T20:56:58Z.

Review: APPROVED


Reviewed — looks good

The change applies the existing macOS faulted-datagram handling to recv and records its documented race; I found no blocking defects in the reviewed files.

What I ran

Reviewed head a1ee7b1df4d631373867268fb2f4f7a2ecd1f7dd against base main; no merged-preview check. I used repo_read.py to inspect the changed lines, DropFaultedDatagram, and the corresponding recvfrom handling. No tests or builds were run. The author reports macOS test results in the PR description; these were not independently run.

python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 1985 2060
python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 520 560
python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 2338 2362

Results

Check Result Note
Changed source and documentation passed Both reviewed
Author-reported macOS checks not run Reported in PR description
Independent tests or builds not run No execution

Negative control

Not run; no test was executed.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

No tests or builds were run, and macOS, Linux, and Windows behavior was not independently exercised. I read both changed files listed under reviewed in files.json; that list contains no excluded files.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6178410447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25134#pullrequestreview-6178410447). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-12-03T20:56:58Z. **Review:** APPROVED --- # Reviewed — looks good The change applies the existing macOS faulted-datagram handling to `recv` and records its documented race; I found no blocking defects in the reviewed files. ## What I ran Reviewed head `a1ee7b1df4d631373867268fb2f4f7a2ecd1f7dd` against base `main`; no merged-preview check. I used `repo_read.py` to inspect the changed lines, `DropFaultedDatagram`, and the corresponding `recvfrom` handling. No tests or builds were run. The author reports macOS test results in the PR description; these were not independently run. ```text python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 1985 2060 python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 520 560 python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 2338 2362 ``` ## Results | Check | Result | Note | |---|---|---| | Changed source and documentation | passed | Both reviewed | | Author-reported macOS checks | not run | Reported in PR description | | Independent tests or builds | not run | No execution | ## Negative control Not run; no test was executed. ## Findings ### Blocking None. ### Non-blocking None. ## Sources - [`Udp.cpp` at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/a1ee7b1df4d631373867268fb2f4f7a2ecd1f7dd/core/libs/prx/libkernel/Socket/src/Udp.cpp#L2049-L2055) adds the macOS EFAULT handling; [`DropFaultedDatagram`](https://github.com/boykopovar/AnyPS5/blob/a1ee7b1df4d631373867268fb2f4f7a2ecd1f7dd/core/libs/prx/libkernel/Socket/src/Udp.cpp#L542-L547) skips non-datagram sockets and `MSG_PEEK` and uses a non-blocking receive. - [The added debt note](https://github.com/boykopovar/AnyPS5/blob/a1ee7b1df4d631373867268fb2f4f7a2ecd1f7dd/docs/dev/debt/changes/fix-libkernel-recv-drop-faulted-datagram-macos.md#L1-L5) documents the race between the failing receive and discard. ## Not verified No tests or builds were run, and macOS, Linux, and Windows behavior was not independently exercised. I read both changed files listed under `reviewed` in `files.json`; that list contains no excluded files. ## To get this to a good state Nothing is required.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/25134:upstream-pr/25134'
git switch 'upstream-pr/25134'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/25134'
git switch 'upstream-pr/25134'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/25134'
git switch 'upstream-pr/25134'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/25134'
git switch 'main'
git merge --squash 'upstream-pr/25134'
git switch 'main'
git merge --ff-only 'upstream-pr/25134'
git switch 'main'
git merge 'upstream-pr/25134'
git push origin 'main'
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!25134
No description provided.