fix(libc): build getifaddrs netmasks from the address family on macOS hosts #9619

Merged
rooklz merged 1 commit from upstream-pr/9619 into main 2026-10-25 21:20:50 +00:00
rooklz commented 2026-10-22 04:41:16 +00:00 (Migrated from github.com)

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


What

guest_interfaces fails on macOS hosts at its netmask check. macOS's getifaddrs returns IPv4 netmasks the way the routing socket stores them: cut down to their significant bytes, so sa_len is 5 to 8 instead of 16, and on some entries sa_family is 0. On my machine lo0's second IPv4 netmask is 05 00 00 00 ff and en0's is 07 02 00 00 ff ff ff, printed by a small probe that calls the host's getifaddrs.

HostEntries converted the netmask with GuestAddress, which picks the layout from the netmask's own family. A family of 0 took the IPv6 branch, so the guest got a 28-byte IPv6 netmask next to an IPv4 address, read from sin6_addr past the 5 bytes the host returned. With family 2 the four address bytes were still read past sa_len.

The netmask now goes through GuestNetmask: the layout comes from the address's family, and on macOS only the sa_len bytes the host returned are copied, with the rest of the mask left zero. Linux returns complete netmasks of the address's family, so the result there is the same as before. Windows builds its masks from prefix lengths and doesn't reach this code.

Tested

  • macOS 26.2, Apple M5, Apple clang 21, x86-64 build under Rosetta 2. The libraries don't link on macOS from main alone, so this ran on @mugurc's ci/macos-series-2 at 53db0badb with #7524, the code of #7937 and the libSceSsl change of #7332, the set @tkcm01 lists on #1. guest_interfaces without this change aborts at GuestInterfaces.cpp:262 (netmask[0] == address[0] && netmask[1] == address[1], found with lldb); with it, it passes.
  • Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release), this branch: guest_interfaces passes.
  • check_conventions.py passes. pr_overlap.py reports no conflicting lines; #6879 shares the file but conflicts with main, and it doesn't touch the netmask code.

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:4962205894 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T04:41:16Z. --- ### What `guest_interfaces` fails on macOS hosts at its netmask check. macOS's `getifaddrs` returns IPv4 netmasks the way the routing socket stores them: cut down to their significant bytes, so `sa_len` is 5 to 8 instead of 16, and on some entries `sa_family` is 0. On my machine `lo0`'s second IPv4 netmask is `05 00 00 00 ff` and `en0`'s is `07 02 00 00 ff ff ff`, printed by a small probe that calls the host's `getifaddrs`. `HostEntries` converted the netmask with `GuestAddress`, which picks the layout from the netmask's own family. A family of 0 took the IPv6 branch, so the guest got a 28-byte IPv6 netmask next to an IPv4 address, read from `sin6_addr` past the 5 bytes the host returned. With family 2 the four address bytes were still read past `sa_len`. The netmask now goes through `GuestNetmask`: the layout comes from the address's family, and on macOS only the `sa_len` bytes the host returned are copied, with the rest of the mask left zero. Linux returns complete netmasks of the address's family, so the result there is the same as before. Windows builds its masks from prefix lengths and doesn't reach this code. ### Tested - macOS 26.2, Apple M5, Apple clang 21, x86-64 build under Rosetta 2. The libraries don't link on macOS from `main` alone, so this ran on @mugurc's `ci/macos-series-2` at `53db0badb` with #7524, the code of #7937 and the libSceSsl change of #7332, the set @tkcm01 lists on #1. `guest_interfaces` without this change aborts at `GuestInterfaces.cpp:262` (`netmask[0] == address[0] && netmask[1] == address[1]`, found with lldb); with it, it passes. - Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release), this branch: `guest_interfaces` passes. - `check_conventions.py` passes. `pr_overlap.py` reports no conflicting lines; #6879 shares the file but conflicts with `main`, and it doesn't touch the netmask code. <!-- 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. --> ### 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: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-22 09:25:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T09:25:55Z.

Review: CHANGES_REQUESTED


The GuestNetmask helper chooses the output layout from the address family, initializes the mask storage to zero, and copies only the available sa_len payload bytes on macOS. That avoids treating sa_family == 0 as IPv6 and avoids reading beyond a short IPv4 mask. Exact-head conventions and git diff --check pass. The reported macOS integration-tree red/green and Linux guest_interfaces pass are useful; current hosted product jobs are skipped, and I did not rerun runtime tests locally. The PR is generic (Unblocks: none).

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5612028918 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619#pullrequestreview-5612028918). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T09:25:55Z. **Review:** CHANGES_REQUESTED --- The `GuestNetmask` helper chooses the output layout from the address family, initializes the mask storage to zero, and copies only the available `sa_len` payload bytes on macOS. That avoids treating `sa_family == 0` as IPv6 and avoids reading beyond a short IPv4 mask. Exact-head conventions and `git diff --check` pass. The reported macOS integration-tree red/green and Linux `guest_interfaces` pass are useful; current hosted product jobs are skipped, and I did not rerun runtime tests locally. The PR is generic (`Unblocks: none`). I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
rooklz commented 2026-10-22 11:31:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T11:31:05Z.


On the #5239 direction: this came from running the suite on the macOS port tree discussed on #1, where guest_interfaces fails without it, not from a title. I am fine holding it until the queue drops or a maintainer says otherwise. If the preference is to fold it into #8658 instead, say so and I will close this in favour of that.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339676857 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619#issuecomment-6339676857). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T11:31:05Z. --- On the #5239 direction: this came from running the suite on the macOS port tree discussed on #1, where `guest_interfaces` fails without it, not from a title. I am fine holding it until the queue drops or a maintainer says otherwise. If the preference is to fold it into #8658 instead, say so and I will close this in favour of that.
MarcZX10 commented 2026-10-22 13:12:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T13:12:09Z.

Review: DISMISSED


I withdraw the earlier #5239 intake-hold objection. I rechecked exact head b2cfd1d3fe74aa318fb36eaca952f210f783af8c against current main 3ca07d086d5f44fbe3c00370e7f03953ab46ca1b: whitespace and conventions pass, and Interfaces.cpp passes GCC 12.2 C++20 syntax checking with the existing missing-field-initializer warning suppressed. A three-way merge with current main is clean. The macOS integration-tree red/green and Linux guest_interfaces pass are author-reported; the hosted relinker/register-context/write-watch matrices pass, while full Linux/Windows product jobs are skipped. I did not run the platform runtime tests locally.

The branch base is 17f533556272e5f6833ed730172f12308eee08d2, behind current main. The repository requires branches to be kept up to date with main. Please rebase/update and refresh the focused guest_interfaces results on the resulting head. The overlap scanner also cannot compare #6879 because that PR conflicts with main; inspect that shared file manually as part of the refreshed overlap check. I reviewed #8658's Interfaces.cpp patch too: it changes macOS link-layer handling in separate hunks, and a direct merge of the two PR heads is clean. This request is for current-main and refreshed-overlap validation only, not the obsolete intake hold.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5614755921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619#pullrequestreview-5614755921). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T13:12:09Z. **Review:** DISMISSED --- I withdraw the earlier #5239 intake-hold objection. I rechecked exact head `b2cfd1d3fe74aa318fb36eaca952f210f783af8c` against current main `3ca07d086d5f44fbe3c00370e7f03953ab46ca1b`: whitespace and conventions pass, and `Interfaces.cpp` passes GCC 12.2 C++20 syntax checking with the existing missing-field-initializer warning suppressed. A three-way merge with current main is clean. The macOS integration-tree red/green and Linux `guest_interfaces` pass are author-reported; the hosted relinker/register-context/write-watch matrices pass, while full Linux/Windows product jobs are skipped. I did not run the platform runtime tests locally. The branch base is `17f533556272e5f6833ed730172f12308eee08d2`, behind current main. The repository requires branches to be kept up to date with main. Please rebase/update and refresh the focused `guest_interfaces` results on the resulting head. The overlap scanner also cannot compare #6879 because that PR conflicts with main; inspect that shared file manually as part of the refreshed overlap check. I reviewed #8658's `Interfaces.cpp` patch too: it changes macOS link-layer handling in separate hunks, and a direct merge of the two PR heads is clean. This request is for current-main and refreshed-overlap validation only, not the obsolete intake hold.
rooklz commented 2026-10-22 13:31:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:31:11Z.


Rebased onto current main (8b3e39832a): head 45f4739c8d, same diff. check_conventions.py passes on it.

Refreshed results on this head: guest_interface passes 3 of 3 on the macOS integration tree (the same ci/macos-series-2 tree as in the description, which carries exactly this Interfaces.cpp), and a hosted Linux run of guest_interfaces is at https://github.com/rooklz/AnyPS5/actions/runs/39787382912 (in progress as I write; I'll note the result).

#6879 by hand: its Interfaces.cpp hunks add link-layer handling on lines 160 to 182 and 253 to 307 of main, and one of them ends with an #endif three lines above the Entry construction this PR changes. The two don't touch the same line, so whichever lands second gets at most a textual conflict in that hunk, resolved by keeping both; neither changes what the other does. #8658's Interfaces.cpp hunks are separate too, as you found.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342009056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619#issuecomment-6342009056). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T13:31:11Z. --- Rebased onto current `main` (`8b3e39832a`): head `45f4739c8d`, same diff. `check_conventions.py` passes on it. Refreshed results on this head: `guest_interface` passes 3 of 3 on the macOS integration tree (the same `ci/macos-series-2` tree as in the description, which carries exactly this `Interfaces.cpp`), and a hosted Linux run of `guest_interfaces` is at https://github.com/rooklz/AnyPS5/actions/runs/39787382912 (in progress as I write; I'll note the result). #6879 by hand: its `Interfaces.cpp` hunks add link-layer handling on lines 160 to 182 and 253 to 307 of `main`, and one of them ends with an `#endif` three lines above the `Entry` construction this PR changes. The two don't touch the same line, so whichever lands second gets at most a textual conflict in that hunk, resolved by keeping both; neither changes what the other does. #8658's `Interfaces.cpp` hunks are separate too, as you found.
rooklz commented 2026-10-22 13:57:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:57:40Z.


The hosted Linux run on head 45f4739c8d finished: guest_interfaces passes (https://github.com/rooklz/AnyPS5/actions/runs/39787382912, Ubuntu 24.04, GCC, Release).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342571522 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619#issuecomment-6342571522). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T13:57:40Z. --- The hosted Linux run on head `45f4739c8d` finished: `guest_interfaces` passes (https://github.com/rooklz/AnyPS5/actions/runs/39787382912, Ubuntu 24.04, GCC, Release).
maximilianfv commented 2026-10-25 15:09:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T15:09:51Z.

Review: COMMENTED


The netmask handling reads correct and matches FreeBSD, but this Mac doesn't show the bug, so I can't confirm the fix here. Comment, not approval; the notes below are non-blocking.

Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied at 45f4739c8d to mugurc's macOS series (27e968974) merged with main at 2742004ed2, with other open macOS fixes, then toggled only this PR. 3 runs per side.

guest_interfaces
Without this PR Passed 3/3
With this PR Passed 3/3

On this host, getifaddrs already returns IPv4 netmasks with sa_family 2 and zero bytes after sa_len. A native probe prints lo0 05 02 00 00 ff | 00 00 00 and en0 07 02 00 00 ff ff fc | 00, where | marks sa_len. Your probe showed family 0 (05 00 00 00 ff), so the old code's read past sa_len happens to produce the right mask here, and the test passes both ways. The A/B therefore neither confirms nor contradicts the fix on this machine.

Code

  • GuestNetmask (core/libs/prx/libc/src/Interfaces.cpp:92-105) does what FreeBSD 11.4's rtsock_fix_netmask (sys/net/rtsock.c:1008, used by sysctl_iflist at :1773) does: zero a buffer the size of the address, copy only the sa_len bytes the host gave, then take the family from the address. A guest gets the console's layout, and Linux and Windows are unchanged.

Notes

  1. core/libs/tests/GuestInterfaces.cpp:260-264: for IPv4 the test checks bytes 0-3 and 8-15 of each netmask but never bytes 4-7, which hold the mask itself, so a copy at the wrong offset would still pass. A check that the IPv4 mask is a contiguous run of one-bits from the top, or netmask[4] == 0xff on the loopback entry, would cover it.
  2. core/libs/prx/libc/src/Interfaces.cpp:99-101: for IPv6 the copy reads up to 26 bytes through netmask->sa_data, which is declared char[14]. That is fine at run time, but stricter -Warray-bounds or fortify builds may flag it. std::memcpy(&mask, netmask, length); mask.ss_family = static_cast<sa_family_t>(family); copies the same bytes from the start of the struct, drops the offsetof guard, and has the same shape as rtsock_fix_netmask.
  3. The Tested section still describes #6879 as sharing the file and conflicting with main; #6879 is closed now. The branch is also still based on 8b3e39832a, from before #8279 guarded the Linux-only headers in this file, so the head doesn't build on macOS by itself, although the merge with main is clean. A rebase, as mentioned in the thread, would make the "Based on current main" box true again.

AI-assisted: yes (Claude Code applied the PR, ran the test both ways, probed getifaddrs natively, read the code and wrote this review).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643804054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9619#pullrequestreview-5643804054). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-25T15:09:51Z. **Review:** COMMENTED --- The netmask handling reads correct and matches FreeBSD, but this Mac doesn't show the bug, so I can't confirm the fix here. Comment, not approval; the notes below are non-blocking. ### Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2) Applied at `45f4739c8d` to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`, with other open macOS fixes, then toggled only this PR. 3 runs per side. | | `guest_interfaces` | |---|---| | Without this PR | Passed 3/3 | | With this PR | Passed 3/3 | On this host, `getifaddrs` already returns IPv4 netmasks with `sa_family` 2 and zero bytes after `sa_len`. A native probe prints `lo0 05 02 00 00 ff | 00 00 00` and `en0 07 02 00 00 ff ff fc | 00`, where `|` marks `sa_len`. Your probe showed family 0 (`05 00 00 00 ff`), so the old code's read past `sa_len` happens to produce the right mask here, and the test passes both ways. The A/B therefore neither confirms nor contradicts the fix on this machine. ### Code - `GuestNetmask` (`core/libs/prx/libc/src/Interfaces.cpp:92-105`) does what FreeBSD 11.4's `rtsock_fix_netmask` (`sys/net/rtsock.c:1008`, used by `sysctl_iflist` at :1773) does: zero a buffer the size of the address, copy only the `sa_len` bytes the host gave, then take the family from the address. A guest gets the console's layout, and Linux and Windows are unchanged. ### Notes 1. `core/libs/tests/GuestInterfaces.cpp:260-264`: for IPv4 the test checks bytes 0-3 and 8-15 of each netmask but never bytes 4-7, which hold the mask itself, so a copy at the wrong offset would still pass. A check that the IPv4 mask is a contiguous run of one-bits from the top, or `netmask[4] == 0xff` on the loopback entry, would cover it. 2. `core/libs/prx/libc/src/Interfaces.cpp:99-101`: for IPv6 the copy reads up to 26 bytes through `netmask->sa_data`, which is declared `char[14]`. That is fine at run time, but stricter `-Warray-bounds` or fortify builds may flag it. `std::memcpy(&mask, netmask, length); mask.ss_family = static_cast<sa_family_t>(family);` copies the same bytes from the start of the struct, drops the `offsetof` guard, and has the same shape as `rtsock_fix_netmask`. 3. The `Tested` section still describes #6879 as sharing the file and conflicting with `main`; #6879 is closed now. The branch is also still based on `8b3e39832a`, from before #8279 guarded the Linux-only headers in this file, so the head doesn't build on macOS by itself, although the merge with `main` is clean. A rebase, as mentioned in the thread, would make the "Based on current `main`" box true again. AI-assisted: yes (Claude Code applied the PR, ran the test both ways, probed `getifaddrs` natively, read the code and wrote this review).
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!9619
No description provided.