fix(libSceNet): match FreeBSD's inet_pton and socket errors on macOS #9231

Merged
Gonic0 merged 3 commits from upstream-pr/9231 into main 2026-10-27 23:59:53 +00:00
Gonic0 commented 2026-10-21 15:54:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-21T15:54:09Z.


What

guest_sce_net aborts on macOS at two checks where XNU's sockets library differs from FreeBSD's:

  • sceNetInetPton(AF_INET, "01.2.3.4") must return 0 (GuestSceNet.cpp:786). FreeBSD's inet_pton refuses an IPv4 component with a leading zero; macOS's accepts it and returns 1.2.3.4. On macOS sceNetInetPton now refuses such a component before calling the host.
  • sceNetSocket(AF_INET, SOCK_STREAM, IPPROTO_UDP) must fail with EPROTONOSUPPORT (GuestSceNet.cpp:151). FreeBSD's socket reports EPROTONOSUPPORT for a protocol of another socket type; XNU reports EPROTOTYPE. sceNetSocket only passes the stream, datagram and raw types, which XNU supports with protocol 0, so on macOS a host EPROTOTYPE there becomes EPROTONOSUPPORT.

Both changes are inside #ifdef __APPLE__; Linux and Windows are unchanged.

Commits:

  1. The leading-zero check in sceNetInetPton.
  2. The EPROTOTYPE mapping in sceNetSocket.

No debt entry: the guest sees FreeBSD's results.

Precedents: libkernel already follows FreeBSD here on every host. Its inet_pton parses with FreeBSD's text rules (#3530), and its socket reports EPROTOTYPE only for a type inet does not have (#7151). sceNetInetPton and sceNetSocket still call the host, so this PR only covers the two places where macOS's host differs.

Overlap: #8658 carries the same two hunks among its 100 commits. This PR takes them on their own so they can be reviewed and merged separately; whichever lands second drops or rebases them.

Tested

macOS 27.2, Apple M5 Pro, Apple clang 21.0.0, x86-64 build under Rosetta 2, Release.

main does not build libSceNet's dependencies on macOS, so the tests ran in a local tree of main 53a0a27bb with the community macOS stack merged, with this branch's Export.cpp changes applied on top (the same diff as this branch on main cecb540a6).

  • Host behaviour, from a small C program on the same machine: inet_pton(AF_INET, "01.2.3.4") returns 1; socket(AF_INET, SOCK_STREAM, IPPROTO_UDP), socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP) and socket(AF_INET6, SOCK_STREAM, IPPROTO_UDP) fail with EPROTOTYPE (41); socket(AF_INET, SOCK_DGRAM, 99) fails with EPROTONOSUPPORT (43).
  • Without commit 1: guest_sce_net fails at GuestSceNet.cpp:786 and :787 for "01.2.3.4".
  • Without commit 2: guest_sce_net fails at GuestSceNet.cpp:151.
  • With both: no other check in guest_sce_net fails (checked with a non-aborting Require that prints the line), and ctest -R '^guest_sce_net$' passes 5 runs of 5.
  • Independently on an Apple M1, macOS 27.0.1, by @Mauro42K on #8658's e0a3ccec0 (comment): guest_sce_net passes 5/5 with both blocks and aborts with either one reverted.
  • tools/check_conventions.py --base upstream/main and git diff --check pass.

Not built on Linux or Windows; the changes are preprocessed out there.

Checklist

  • Based on current main; no other open PR implements the same functions (see Overlap above: #8658)
  • 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4954442589 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9231). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-21T15:54:09Z. --- ### What `guest_sce_net` aborts on macOS at two checks where XNU's sockets library differs from FreeBSD's: - `sceNetInetPton(AF_INET, "01.2.3.4")` must return 0 (`GuestSceNet.cpp:786`). FreeBSD's `inet_pton` refuses an IPv4 component with a leading zero; macOS's accepts it and returns 1.2.3.4. On macOS `sceNetInetPton` now refuses such a component before calling the host. - `sceNetSocket(AF_INET, SOCK_STREAM, IPPROTO_UDP)` must fail with `EPROTONOSUPPORT` (`GuestSceNet.cpp:151`). FreeBSD's `socket` reports `EPROTONOSUPPORT` for a protocol of another socket type; XNU reports `EPROTOTYPE`. `sceNetSocket` only passes the stream, datagram and raw types, which XNU supports with protocol 0, so on macOS a host `EPROTOTYPE` there becomes `EPROTONOSUPPORT`. Both changes are inside `#ifdef __APPLE__`; Linux and Windows are unchanged. Commits: 1. The leading-zero check in `sceNetInetPton`. 2. The `EPROTOTYPE` mapping in `sceNetSocket`. No debt entry: the guest sees FreeBSD's results. Precedents: libkernel already follows FreeBSD here on every host. Its `inet_pton` parses with FreeBSD's text rules (#3530), and its `socket` reports `EPROTOTYPE` only for a type inet does not have (#7151). `sceNetInetPton` and `sceNetSocket` still call the host, so this PR only covers the two places where macOS's host differs. Overlap: #8658 carries the same two hunks among its 100 commits. This PR takes them on their own so they can be reviewed and merged separately; whichever lands second drops or rebases them. ### Tested macOS 27.2, Apple M5 Pro, Apple clang 21.0.0, x86-64 build under Rosetta 2, Release. `main` does not build libSceNet's dependencies on macOS, so the tests ran in a local tree of `main` 53a0a27bb with the community macOS stack merged, with this branch's `Export.cpp` changes applied on top (the same diff as this branch on `main` cecb540a6). - Host behaviour, from a small C program on the same machine: `inet_pton(AF_INET, "01.2.3.4")` returns 1; `socket(AF_INET, SOCK_STREAM, IPPROTO_UDP)`, `socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP)` and `socket(AF_INET6, SOCK_STREAM, IPPROTO_UDP)` fail with `EPROTOTYPE` (41); `socket(AF_INET, SOCK_DGRAM, 99)` fails with `EPROTONOSUPPORT` (43). - Without commit 1: `guest_sce_net` fails at `GuestSceNet.cpp:786` and `:787` for `"01.2.3.4"`. - Without commit 2: `guest_sce_net` fails at `GuestSceNet.cpp:151`. - With both: no other check in `guest_sce_net` fails (checked with a non-aborting `Require` that prints the line), and `ctest -R '^guest_sce_net$'` passes 5 runs of 5. - Independently on an Apple M1, macOS 27.0.1, by @Mauro42K on #8658's `e0a3ccec0` ([comment](https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6340916181)): `guest_sce_net` passes 5/5 with both blocks and aborts with either one reverted. - `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. Not built on Linux or Windows; the changes are preprocessed out there. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (see Overlap above: #8658) - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
EagleBearerYS commented 2026-10-22 04:39:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T04:39:22Z.


@Gonic0 a merge-order note: #9578 changes sceNetInetPton in the same Export.cpp. An AF_INET6 address now goes to libkernel's __inet_pton_nid_postfix, and an AF_INET address still goes to the host's inet_pton (InetPtonA on Windows). This PR's macOS check of an AF_INET address's leading zeros runs before that call, so with #9578 it still comes before the host parser. The two merge without a conflict in either order (git merge-tree of the two heads), so #9578 proposes that neither waits for the other. If you'd rather this one went first, say so and #9578 will list it under Depends on.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332849913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9231#issuecomment-6332849913). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T04:39:22Z. --- @Gonic0 a merge-order note: #9578 changes `sceNetInetPton` in the same `Export.cpp`. An `AF_INET6` address now goes to libkernel's `__inet_pton_nid_postfix`, and an `AF_INET` address still goes to the host's `inet_pton` (`InetPtonA` on Windows). This PR's macOS check of an `AF_INET` address's leading zeros runs before that call, so with #9578 it still comes before the host parser. The two merge without a conflict in either order (`git merge-tree` of the two heads), so #9578 proposes that neither waits for the other. If you'd rather this one went first, say so and #9578 will list it under Depends on.
Gonic0 commented 2026-10-22 04:41:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-22T04:41:37Z.


Thanks @EagleBearerYS, agreed. I ran the same git merge-tree check against #9494 and #9578 and got the same result: no conflicts, and both #ifdef __APPLE__ blocks stay in place. Neither needs to wait, so no Depends on needed on either side.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332879742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9231#issuecomment-6332879742). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-22T04:41:37Z. --- Thanks @EagleBearerYS, agreed. I ran the same `git merge-tree` check against #9494 and #9578 and got the same result: no conflicts, and both `#ifdef __APPLE__` blocks stay in place. Neither needs to wait, so no Depends on needed on either side. AI-assisted: yes
harisibrar1 commented 2026-10-24 13:36:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:36:45Z.

Review: APPROVED


Checked against main 522e32e3b6 and FreeBSD 11.4 lib/libc/inet/inet_pton.c and sys/kern/uipc_socket.c (socreate):

  • The leading-zero check rejects exactly when FreeBSD's inet_pton4 would reach if (saw_digit && *tp == 0) return (0); (inet_pton.c:94), so it never rejects anything FreeBSD accepts. Because the check is inside convert, it also covers the IPv6 dotted tail (::ffff:01.2.3.4, GuestSceNet.cpp:931), the same way FreeBSD's inet_pton6 hands that tail to inet_pton4.
  • In FreeBSD 11.4, socreate returns EPROTONOSUPPORT when a nonzero protocol doesn't match the type (uipc_socket.c:487-498), so mapping macOS's EPROTOTYPE to that error matches the reference for the SOCK_STREAM/IPPROTO_UDP case tested at GuestSceNet.cpp:176. I couldn't run this on macOS; that part rests on your and maximilianfv's host runs.
  • Main hasn't picked this up yet (no EPROTOTYPE handling in libSceNet/Export.cpp), and a 3-way git merge-tree against current main shows no conflicts. Looks good to me.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638435033 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9231#pullrequestreview-5638435033). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:36:45Z. **Review:** APPROVED --- Checked against main 522e32e3b6 and FreeBSD 11.4 lib/libc/inet/inet_pton.c and sys/kern/uipc_socket.c (socreate): - The leading-zero check rejects exactly when FreeBSD's `inet_pton4` would reach `if (saw_digit && *tp == 0) return (0);` (inet_pton.c:94), so it never rejects anything FreeBSD accepts. Because the check is inside `convert`, it also covers the IPv6 dotted tail (`::ffff:01.2.3.4`, GuestSceNet.cpp:931), the same way FreeBSD's `inet_pton6` hands that tail to `inet_pton4`. - In FreeBSD 11.4, `socreate` returns EPROTONOSUPPORT when a nonzero protocol doesn't match the type (uipc_socket.c:487-498), so mapping macOS's EPROTOTYPE to that error matches the reference for the SOCK_STREAM/IPPROTO_UDP case tested at GuestSceNet.cpp:176. I couldn't run this on macOS; that part rests on your and maximilianfv's host runs. - Main hasn't picked this up yet (no EPROTOTYPE handling in libSceNet/Export.cpp), and a 3-way `git merge-tree` against current main shows no conflicts. Looks good to me. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9231
No description provided.