feat(libkernel): UDP sockets and name resolution on macOS hosts #7850

Merged
mugurc merged 1 commit from upstream-pr/7850 into main 2026-10-21 11:36:21 +00:00
mugurc commented 2026-10-19 10:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-19T10:57:55Z.


What

UDP sockets and name resolution on macOS hosts. The socket layer calls ppoll and asks for POLLRDHUP, and the resolver calls gethostbyname_r; macOS has none of them, so libkernel and libScePosixForWebKit do not compile on macOS without this. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298).

  • The equeue wait uses poll with the timeout rounded up to a millisecond.
  • A readable stream socket whose peer closed is reported with POLLHUP by peeking one byte.
  • FIONREAD on a datagram socket, and the byte count that fstat and kevent report for it, come from SO_NREAD, the size of the next datagram. macOS's FIONREAD counts every queued datagram together with its source address. One helper, QueuedBytes, serves the three; on Linux it makes the same ioctl(FIONREAD) call as before.
  • socketpair throws for SOCK_SEQPACKET, which has no AF_UNIX pair on macOS, and guest_socket_pair expects that throw there.
  • gethostbyname replaces gethostbyname_r; macOS keeps its result in thread-specific storage.
  • guest_udp sends on its connected socket without an address. macOS, like FreeBSD, fails sendto with an address on a connected socket with EISCONN, where Linux accepts it. The two changed lines are the same as in #7475, which makes the guest return EISCONN there on every host and checks it, so the two pull requests merge in either order.
  • log_macros captures its writes through a datagram pair with 1 MiB buffers instead of SOCK_SEQPACKET.

The whole-port PRs #7201 and #7332 contain these functions too. #7332 carries the first version of this commit from the series and also reads SO_NREAD for fstat and kevent.

Tested

macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2. main 9127ea36d with the rest of the macOS series (ci/macos-series-2 on my fork), the open macOS fix #7524, the JIT shared memory change of the closed #6928 (#7937 carries it now) and three changes from #7332. The libraries do not link on macOS without the series.

  • Without this PR, libkernel and libScePosixForWebKit do not compile on macOS (ppoll, POLLRDHUP, gethostbyname_r). With it, guest_udp, guest_tcp, guest_kqueue, guest_equeue_events, guest_resolver, log_macros and log_macros_timestamps pass.

  • With SO_NREAD in ioctl only (the first version of this commit), guest_kqueue aborts where it expects a kevent byte count of 1 for a 1-byte datagram on an AF_UNIX pair. A probe, run under Rosetta and natively, shows why. FIONREAD reads 17 for that datagram, 35 after a second 2-byte datagram and 21 for a 5-byte UDP datagram on loopback. SO_NREAD reads 1, 1 and 5.

  • guest_socket_pair, guest_poll and guest_sce_net still fail on macOS, at checks this PR does not touch:

    • shutdown(SHUT_RD) fails with ENOTCONN on a stream end whose peer shut its writing side.
    • poll does not report urgent data with POLLPRI.
    • sceNetShutdown does not wake a recvfrom blocked on an unbound UDP socket.

    #7332 handles all three. With the failing shutdown check reported instead of required, the rest of guest_socket_pair passes, including the SOCK_SEQPACKET check this PR adds.

  • guest_udp and guest_kqueue pass 5 runs of 5. With #7475 merged on top, guest_udp passes 5 runs of 5 as well.

  • ninja, ninja libs and ctest -j4: 1624 tests, 991 passed, 23 failed, the rest skipped (no Vulkan loader in the environment). 17 failures are the same as in the run before this update, all among the failures #7631 lists. Five come from tests that main added or changed today, in #6985, #6916, #6851 and #7872. The last is guest_socket_read_write, which fails only some runs; it passed when run alone afterwards.

  • Linux and Windows: not run. On Linux, QueuedBytes makes the same ioctl call as before and gethostbyname_r runs as before. The two guest_udp lines send on a connected socket without an address, which every host accepts; #7475 ran the same lines on Windows. Every other change is in an __APPLE__ branch.

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4919853971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7850). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-19T10:57:55Z. --- ### What UDP sockets and name resolution on macOS hosts. The socket layer calls `ppoll` and asks for `POLLRDHUP`, and the resolver calls `gethostbyname_r`; macOS has none of them, so `libkernel` and `libScePosixForWebKit` do not compile on macOS without this. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298). - The equeue wait uses `poll` with the timeout rounded up to a millisecond. - A readable stream socket whose peer closed is reported with `POLLHUP` by peeking one byte. - `FIONREAD` on a datagram socket, and the byte count that `fstat` and `kevent` report for it, come from `SO_NREAD`, the size of the next datagram. macOS's `FIONREAD` counts every queued datagram together with its source address. One helper, `QueuedBytes`, serves the three; on Linux it makes the same `ioctl(FIONREAD)` call as before. - `socketpair` throws for `SOCK_SEQPACKET`, which has no `AF_UNIX` pair on macOS, and `guest_socket_pair` expects that throw there. - `gethostbyname` replaces `gethostbyname_r`; macOS keeps its result in thread-specific storage. - `guest_udp` sends on its connected socket without an address. macOS, like FreeBSD, fails `sendto` with an address on a connected socket with `EISCONN`, where Linux accepts it. The two changed lines are the same as in #7475, which makes the guest return `EISCONN` there on every host and checks it, so the two pull requests merge in either order. - `log_macros` captures its writes through a datagram pair with 1 MiB buffers instead of `SOCK_SEQPACKET`. The whole-port PRs #7201 and #7332 contain these functions too. #7332 carries the first version of this commit from the series and also reads `SO_NREAD` for `fstat` and `kevent`. ### Tested macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2. `main` 9127ea36d with the rest of the macOS series (`ci/macos-series-2` on my fork), the open macOS fix #7524, the JIT shared memory change of the closed #6928 (#7937 carries it now) and three changes from #7332. The libraries do not link on macOS without the series. - Without this PR, `libkernel` and `libScePosixForWebKit` do not compile on macOS (`ppoll`, `POLLRDHUP`, `gethostbyname_r`). With it, `guest_udp`, `guest_tcp`, `guest_kqueue`, `guest_equeue_events`, `guest_resolver`, `log_macros` and `log_macros_timestamps` pass. - With `SO_NREAD` in `ioctl` only (the first version of this commit), `guest_kqueue` aborts where it expects a kevent byte count of 1 for a 1-byte datagram on an `AF_UNIX` pair. A probe, run under Rosetta and natively, shows why. `FIONREAD` reads 17 for that datagram, 35 after a second 2-byte datagram and 21 for a 5-byte UDP datagram on loopback. `SO_NREAD` reads 1, 1 and 5. - `guest_socket_pair`, `guest_poll` and `guest_sce_net` still fail on macOS, at checks this PR does not touch: - `shutdown(SHUT_RD)` fails with `ENOTCONN` on a stream end whose peer shut its writing side. - `poll` does not report urgent data with `POLLPRI`. - `sceNetShutdown` does not wake a `recvfrom` blocked on an unbound UDP socket. #7332 handles all three. With the failing `shutdown` check reported instead of required, the rest of `guest_socket_pair` passes, including the `SOCK_SEQPACKET` check this PR adds. - `guest_udp` and `guest_kqueue` pass 5 runs of 5. With #7475 merged on top, `guest_udp` passes 5 runs of 5 as well. - `ninja`, `ninja libs` and `ctest -j4`: 1624 tests, 991 passed, 23 failed, the rest skipped (no Vulkan loader in the environment). 17 failures are the same as in the run before this update, all among the failures #7631 lists. Five come from tests that `main` added or changed today, in #6985, #6916, #6851 and #7872. The last is `guest_socket_read_write`, which fails only some runs; it passed when run alone afterwards. - Linux and Windows: not run. On Linux, `QueuedBytes` makes the same `ioctl` call as before and `gethostbyname_r` runs as before. The two `guest_udp` lines send on a connected socket without an address, which every host accepts; #7475 ran the same lines on Windows. Every other change is in an `__APPLE__` branch. ### 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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-19 11:09:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T11:09:47Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8344 docs/dev/debt/libkernel.md L186
PR 8483 docs/dev/debt/libkernel.md L186
PR 8658 core/libs/prx/libkernel/Socket/src/Udp.cpp L439, L764-765, L801-802, L844, L867-868, L1304
core/libs/tests/LogMacros.cpp L40-41
PR 8670 docs/dev/debt/libkernel.md L186

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libScePosixForWebKit/Resolver.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/tests/GuestSocketPair.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestSocketPair.cpp
core/libs/tests/GuestUdp.cpp
core/libs/tests/LogMacros.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6879 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 6977 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libScePosixForWebKit/Resolver.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/tests/LogMacros.cpp
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 8520 docs/dev/debt/libkernel.md
PR 8553 docs/dev/debt/libkernel.md
PR 8726 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6265818657 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7850#issuecomment-6265818657). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T11:09:47Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 8344](https://www.github.com/boykopovar/AnyPS5/pull/8344) | `docs/dev/debt/libkernel.md` [L186](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/docs/dev/debt/libkernel.md#L186) | | | | [PR 8483](https://www.github.com/boykopovar/AnyPS5/pull/8483) | `docs/dev/debt/libkernel.md` [L186](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/docs/dev/debt/libkernel.md#L186) | | | | [PR 8658](https://www.github.com/boykopovar/AnyPS5/pull/8658) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` [L439](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/prx/libkernel/Socket/src/Udp.cpp#L439), [L764-765](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/prx/libkernel/Socket/src/Udp.cpp#L764-L765), [L801-802](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/prx/libkernel/Socket/src/Udp.cpp#L801-L802), [L844](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/prx/libkernel/Socket/src/Udp.cpp#L844), [L867-868](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/prx/libkernel/Socket/src/Udp.cpp#L867-L868), [L1304](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/prx/libkernel/Socket/src/Udp.cpp#L1304)<br>`core/libs/tests/LogMacros.cpp` [L40-41](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/core/libs/tests/LogMacros.cpp#L40-L41) | | | | [PR 8670](https://www.github.com/boykopovar/AnyPS5/pull/8670) | `docs/dev/debt/libkernel.md` [L186](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf57bdf51671db0063115e38ab2ddc85e/docs/dev/debt/libkernel.md#L186) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libScePosixForWebKit/Resolver.cpp`<br>`core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`core/libs/tests/GuestSocketPair.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestSocketPair.cpp`<br>`core/libs/tests/GuestUdp.cpp`<br>`core/libs/tests/LogMacros.cpp` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `docs/dev/debt/libkernel.md` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `docs/dev/debt/libkernel.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `docs/dev/debt/libkernel.md` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libScePosixForWebKit/Resolver.cpp`<br>`core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`core/libs/tests/LogMacros.cpp` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `docs/dev/debt/libkernel.md` | | [PR 8553](https://www.github.com/boykopovar/AnyPS5/pull/8553) | `docs/dev/debt/libkernel.md` | | [PR 8726](https://www.github.com/boykopovar/AnyPS5/pull/8726) | `docs/dev/debt/libkernel.md` |
fogydes commented 2026-10-19 12:08:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-19T12:08:23Z.

Review: COMMENTED


Tested and reviewed the diff:

The macOS host socket adaptations (SO_NREAD for datagram size on FIONREAD/fstat/kevent, millisecond rounding for poll timeouts in absence of ppoll, and gethostbyname fallback) are well isolated under #ifdef __APPLE__.

One overlap coordination note:
The modifications in core/libs/tests/GuestUdp.cpp (lines 154, 202, 222-223) overlap with open PR #7475 (fix(libkernel): return EISCONN from sendto with an address on a connected datagram socket).

PR #7475 adds explicit EISCONN (56) validation in Udp.cpp for sendto calls with an address on connected sockets to match FreeBSD kernel behavior, and updates GuestUdp.cpp accordingly. Your separation of sender (connected) and addressed (unconnected) in GuestUdp.cpp aligns cleanly with that change.

To help maintainers sequence them without merge conflicts, consider adding Depends on: #7475 (or noting how the two interact in the PR description).

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5571302802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7850#pullrequestreview-5571302802). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-19T12:08:23Z. **Review:** COMMENTED --- Tested and reviewed the diff: The macOS host socket adaptations (`SO_NREAD` for datagram size on `FIONREAD`/`fstat`/`kevent`, millisecond rounding for `poll` timeouts in absence of `ppoll`, and `gethostbyname` fallback) are well isolated under `#ifdef __APPLE__`. One overlap coordination note: The modifications in `core/libs/tests/GuestUdp.cpp` (lines 154, 202, 222-223) overlap with open PR #7475 (`fix(libkernel): return EISCONN from sendto with an address on a connected datagram socket`). PR #7475 adds explicit `EISCONN` (56) validation in `Udp.cpp` for `sendto` calls with an address on connected sockets to match FreeBSD kernel behavior, and updates `GuestUdp.cpp` accordingly. Your separation of `sender` (connected) and `addressed` (unconnected) in `GuestUdp.cpp` aligns cleanly with that change. To help maintainers sequence them without merge conflicts, consider adding `Depends on: #7475` (or noting how the two interact in the PR description). AI-assisted: yes
mugurc commented 2026-10-19 20:55:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-19T20:55:55Z.


Thanks @fogydes. The two lines of guest_udp that #7475 changes are now changed the same way here: the connected sender sends without an address, and the sendmsg header has no name. With identical lines the two pull requests merge in either order without a conflict, so this one needs no Depends on. git merge-tree agrees for both orders on main 9127ea36d.

This drops the second, unconnected socket and the macOS-only EISCONN check from this pull request. #7475 adds that check for every host, and on macOS the host already returns EISCONN there. On macOS guest_udp passes with this head alone and with #7475 merged on top. The description is updated.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6277132286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7850#issuecomment-6277132286). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-19T20:55:55Z. --- Thanks @fogydes. The two lines of `guest_udp` that #7475 changes are now changed the same way here: the connected sender sends without an address, and the `sendmsg` header has no name. With identical lines the two pull requests merge in either order without a conflict, so this one needs no **Depends on**. `git merge-tree` agrees for both orders on `main` 9127ea36d. This drops the second, unconnected socket and the macOS-only `EISCONN` check from this pull request. #7475 adds that check for every host, and on macOS the host already returns `EISCONN` there. On macOS `guest_udp` passes with this head alone and with #7475 merged on top. The description is updated.
adriadam10 commented 2026-10-21 11:37:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T11:37:29Z.


Checked the diff: every macOS change sits behind __APPLE__, QueuedBytes keeps the ioctl(FIONREAD) call on Linux, POLLRDHUP stays in the non-Apple branch, the guest_udp lines match #7475, and the debt entries match the code; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6314957195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7850#issuecomment-6314957195). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T11:37:29Z. --- Checked the diff: every macOS change sits behind `__APPLE__`, `QueuedBytes` keeps the `ioctl(FIONREAD)` call on Linux, `POLLRDHUP` stays in the non-Apple branch, the `guest_udp` lines match #7475, and the debt entries match the code; merged.
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!7850
No description provided.