fix(libkernel): drop a datagram whose receive faulted on macOS #19520

Merged
imellag merged 1 commit from upstream-pr/19520 into main 2026-11-13 15:38:29 +00:00
imellag commented 2026-11-13 13:04:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-11-13T13:04:48Z.


What

On macOS a datagram whose receive faults loses its source address but stays queued. When the guest's recvfrom buffer is not writable, the host recvfrom fails with EFAULT. XNU then leaves the datagram's data at the head of the queue without its address record, so the next recvfrom returns that data with an address length of 0. A title that asks who sent a packet gets an empty address, and AnyPS5 writes nothing to the address output (so a bad address pointer is not even reported). This is why guest_udp fails on macOS at recvfrom with address 0x10, which returns 2 instead of failing with EFAULT.

FreeBSD 11.4's soreceive_dgram takes the whole record off the socket buffer before copying it out, so a faulted receive loses that datagram, and Linux's udp_recvmsg drops it as well. The Windows path of recvfrom already does the same through DropDatagram.

Measured on macOS 27.2 (x86-64 under Rosetta 2) with a native program on a loopback UDP socket:

step result
recvfrom into a PROT_READ page -1, EFAULT
next recvfrom with an address 2 bytes, address length 0
same with MSG_PEEK faulting address kept (nothing dequeued)
recvmsg into a PROT_READ page, then recvfrom 2 bytes, address length 0

On macOS, after the host recvfrom of a datagram socket fails with EFAULT and the guest did not ask for MSG_PEEK, the remnant is dropped with a 1-byte non-blocking recv, so the next receive gets the next datagram with its address. Linux, whose kernel already dropped the datagram, and Windows are unchanged. recvmsg needs nothing: it receives into a host buffer and copies out afterwards, so the host never faults there.

The open #12682, #12688 and #12700 restructure this recvfrom; whichever lands second carries the four lines over.

Tested

macOS 27.2, Apple M4 Pro, x86-64 build under Rosetta 2, main d4556df8a8:

  • main: guest_udp aborts; with a print in the test, the recvfrom with address 0x10 returns 2 with address length 0, and with a print in recvfrom the host reports family 0, size 0 for that datagram.
  • this PR: guest_udp passes.
  • ctest -R "^(guest_(udp|socket|sce_net|net|tcp|kqueue|poll|recv|send|http|ssl|unix|select|equeue)|net_)" -j6 after a full incremental build: 120 of 128 pass. The failures are the ones main already has on macOS (guest_sce_net_linger #19488, guest_kqueue #19490, guest_socket_options #17879, guest_socket_port_zero #17878, guest_sce_net, guest_net_ctl, net_reuseport_patched not run), plus guest_socket_read_write, which failed once under -j6 and passes 5 of 5 runs on its own.

Not run on Linux or Windows.

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:5295976403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19520). Originally posted by [imellag](https://github.com/imellag) on 2026-11-13T13:04:48Z. --- ### What On macOS a datagram whose receive faults loses its source address but stays queued. When the guest's `recvfrom` buffer is not writable, the host `recvfrom` fails with `EFAULT`. XNU then leaves the datagram's data at the head of the queue without its address record, so the next `recvfrom` returns that data with an address length of 0. A title that asks who sent a packet gets an empty address, and AnyPS5 writes nothing to the address output (so a bad address pointer is not even reported). This is why `guest_udp` fails on macOS at `recvfrom` with address `0x10`, which returns 2 instead of failing with `EFAULT`. FreeBSD 11.4's `soreceive_dgram` takes the whole record off the socket buffer before copying it out, so a faulted receive loses that datagram, and Linux's `udp_recvmsg` drops it as well. The Windows path of `recvfrom` already does the same through `DropDatagram`. Measured on macOS 27.2 (x86-64 under Rosetta 2) with a native program on a loopback UDP socket: | step | result | |---|---| | `recvfrom` into a `PROT_READ` page | -1, `EFAULT` | | next `recvfrom` with an address | 2 bytes, address length 0 | | same with `MSG_PEEK` faulting | address kept (nothing dequeued) | | `recvmsg` into a `PROT_READ` page, then `recvfrom` | 2 bytes, address length 0 | On macOS, after the host `recvfrom` of a datagram socket fails with `EFAULT` and the guest did not ask for `MSG_PEEK`, the remnant is dropped with a 1-byte non-blocking `recv`, so the next receive gets the next datagram with its address. Linux, whose kernel already dropped the datagram, and Windows are unchanged. `recvmsg` needs nothing: it receives into a host buffer and copies out afterwards, so the host never faults there. The open #12682, #12688 and #12700 restructure this `recvfrom`; whichever lands second carries the four lines over. ### Tested macOS 27.2, Apple M4 Pro, x86-64 build under Rosetta 2, `main` d4556df8a8: - `main`: `guest_udp` aborts; with a print in the test, the `recvfrom` with address `0x10` returns 2 with address length 0, and with a print in `recvfrom` the host reports family 0, size 0 for that datagram. - this PR: `guest_udp` passes. - `ctest -R "^(guest_(udp|socket|sce_net|net|tcp|kqueue|poll|recv|send|http|ssl|unix|select|equeue)|net_)" -j6` after a full incremental build: 120 of 128 pass. The failures are the ones `main` already has on macOS (`guest_sce_net_linger` #19488, `guest_kqueue` #19490, `guest_socket_options` #17879, `guest_socket_port_zero` #17878, `guest_sce_net`, `guest_net_ctl`, `net_reuseport_patched` not run), plus `guest_socket_read_write`, which failed once under `-j6` and passes 5 of 5 runs on its own. Not run on Linux or Windows. ### 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 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
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!19520
No description provided.