fix(libkernel): answer pthread_getaffinity_np for the console's 8- and 16-byte sets #11431

Merged
p3n1cillin merged 4 commits from upstream-pr/11431 into main 2026-10-30 00:20:44 +00:00
p3n1cillin commented 2026-10-26 13:11:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by p3n1cillin on 2026-10-26T13:11:32Z.


What

pthread_getaffinity_np accepted exactly 32 bytes, FreeBSD's cpuset_t, and returned ERANGE for every other size. The console does the opposite.

PS5_PayloadSDK measured it in a title, the RetroArch title on firmware 12.09 (PROBE.md, "What a title reads about its CPUs"): pthread_getaffinity_np "answers for set sizes of 8 and 16 bytes and returns ERANGE (34) for 32 bytes and more. FreeBSD's cpuset_t is 32 bytes, so code built on FreeBSD's headers always gets ERANGE." That fits FreeBSD's bounds check in kern_cpuset_getaffinity with a 64-bit kernel set, which Prospero Win also measured for cpuset_getaffinity.

The change:

  • 8 and 16 bytes (measured): answered, with the mask in the first 8 bytes and the rest of the set zeroed.
  • 32 bytes or more (measured): ERANGE.
  • Every other size (under 8, 9 to 15, 17 to 31): throws, since none was measured.
  • Unchanged: pthread_setaffinity_np and cpuset_getaffinity/cpuset_setaffinity keep FreeBSD's 32-byte limit. Neither is measured, so this PR leaves them as they are.
  • Debt: a new libkernel entry records which sizes are measured.

guest_available_cpumask now reads the affinity with 16 bytes and with 8 bytes. It checks that bytes 8 to 15 are zeroed and the bytes after the set untouched, and expects ERANGE for 32, 33 and 64 bytes. It doesn't assert the throw for unmeasured sizes, which is an unimplemented path.

Tested

Windows 11 Pro (26300), WinLibs GCC 15.2.0, Release, full build on main f759e165d9 (head 570ccce535):

  • libc and libkernel tests: all 433 that they register pass.
  • Without the Thread.cpp change: guest_available_cpumask fails.
  • Checks: tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py reports no line conflicts; #7135, which edits cpuset_getaffinity in the same file, doesn't touch pthread_getaffinity_np. No title run: none in the catalog reaches this call.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5017996209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11431). Originally posted by [p3n1cillin](https://github.com/p3n1cillin) on 2026-10-26T13:11:32Z. --- ### What `pthread_getaffinity_np` accepted exactly 32 bytes, FreeBSD's `cpuset_t`, and returned `ERANGE` for every other size. The console does the opposite. PS5_PayloadSDK measured it in a title, the RetroArch title on firmware 12.09 ([PROBE.md, "What a title reads about its CPUs"](https://github.com/mihawk-99/PS5_PayloadSDK/blob/3e31e0b1b68ada28beca68adc480cf0c3d17b9df/platform/docs/PROBE.md#L287-L297)): `pthread_getaffinity_np` "answers for set sizes of 8 and 16 bytes and returns ERANGE (34) for 32 bytes and more. FreeBSD's `cpuset_t` is 32 bytes, so code built on FreeBSD's headers always gets ERANGE." That fits FreeBSD's bounds check in `kern_cpuset_getaffinity` with a 64-bit kernel set, which Prospero Win also [measured](https://github.com/mpereiraesaa/prospero-win/blob/59c48117119fd57cd33e7bc91c11fa0c8ebf8b3d/wine/patches/0881-ntdll-ps5-report-the-cpus-a-game-runs-on.patch) for `cpuset_getaffinity`. The change: - **8 and 16 bytes** (measured): answered, with the mask in the first 8 bytes and the rest of the set zeroed. - **32 bytes or more** (measured): `ERANGE`. - **Every other size** (under 8, 9 to 15, 17 to 31): throws, since none was measured. - **Unchanged:** `pthread_setaffinity_np` and `cpuset_getaffinity`/`cpuset_setaffinity` keep FreeBSD's 32-byte limit. Neither is measured, so this PR leaves them as they are. - **Debt:** a new libkernel entry records which sizes are measured. `guest_available_cpumask` now reads the affinity with 16 bytes and with 8 bytes. It checks that bytes 8 to 15 are zeroed and the bytes after the set untouched, and expects `ERANGE` for 32, 33 and 64 bytes. It doesn't assert the throw for unmeasured sizes, which is an unimplemented path. ### Tested Windows 11 Pro (26300), WinLibs GCC 15.2.0, Release, full build on `main` f759e165d9 (head `570ccce535`): - **libc and libkernel tests:** all 433 that they register pass. - **Without the `Thread.cpp` change:** `guest_available_cpumask` fails. - **Checks:** `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` reports no line conflicts; #7135, which edits `cpuset_getaffinity` in the same file, doesn't touch `pthread_getaffinity_np`. No title run: none in the catalog reaches this call. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-26 13:50:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T13:50:20Z.

Review: CHANGES_REQUESTED


The updated head now throws for 17–31 bytes, which addresses half of my earlier request. The 9–15 byte sizes remain successful, though the PR and debt entry both say those sizes are unmeasured. Please either provide console evidence for this interval or throw for it so the implementation stays within measured behavior.

The branch is based on 4b1a9a3b2f51, while current main is 9656d366be90. The tree merges cleanly, and the relevant Thread.cpp and GuestAvailableCpumask.cpp sources have not changed, but main has advanced and also edited the shared libkernel debt file. Please sync the branch, rerun conventions, and refresh the focused test report against the current base before final review. My local tools/check_conventions.py --base upstream/main --head upstream/pr-11431 and git diff --check upstream/main...upstream/pr-11431 pass on the current fetched refs. I am requesting changes for the remaining unmeasured successful input range and the stale base/report.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5653000739 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11431#pullrequestreview-5653000739). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T13:50:20Z. **Review:** CHANGES_REQUESTED --- The updated head now throws for 17–31 bytes, which addresses half of my earlier request. The 9–15 byte sizes remain successful, though the PR and debt entry both say those sizes are unmeasured. Please either provide console evidence for this interval or throw for it so the implementation stays within measured behavior. The branch is based on `4b1a9a3b2f51`, while current `main` is `9656d366be90`. The tree merges cleanly, and the relevant `Thread.cpp` and `GuestAvailableCpumask.cpp` sources have not changed, but main has advanced and also edited the shared libkernel debt file. Please sync the branch, rerun conventions, and refresh the focused test report against the current base before final review. My local `tools/check_conventions.py --base upstream/main --head upstream/pr-11431` and `git diff --check upstream/main...upstream/pr-11431` pass on the current fetched refs. I am requesting changes for the remaining unmeasured successful input range and the stale base/report.
MarcZX10 commented 2026-10-26 15:44:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T15:44:19Z.

Review: DISMISSED


I re-reviewed exact head 32d7260ad7db057bf060363dc763a4cdaa13e16e. The implementation now returns success only for the measured 8- and 16-byte masks, returns ERANGE for sizes of 32 bytes or more, and throws for the unmeasured sizes below 32. The regression test and debt entry match that behavior. I withdraw my earlier request about the 9–15 byte range. The author reports all 406 registered libc/libkernel tests passing on Windows on this head.

Current-main conventions and git diff --check pass, and the merge-tree preview is clean. The branch is based on e38fcb00dc76, while current main is b0f1183ca12b; the only intervening change in this PR's paths is another edit to docs/dev/debt/libkernel.md. Please sync to current main and rerun conventions so the debt entry is integrated on the latest base. I did not independently run the Windows suite.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5654798173 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11431#pullrequestreview-5654798173). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T15:44:19Z. **Review:** DISMISSED --- I re-reviewed exact head `32d7260ad7db057bf060363dc763a4cdaa13e16e`. The implementation now returns success only for the measured 8- and 16-byte masks, returns `ERANGE` for sizes of 32 bytes or more, and throws for the unmeasured sizes below 32. The regression test and debt entry match that behavior. I withdraw my earlier request about the 9–15 byte range. The author reports all 406 registered libc/libkernel tests passing on Windows on this head. Current-main conventions and `git diff --check` pass, and the merge-tree preview is clean. The branch is based on `e38fcb00dc76`, while current `main` is `b0f1183ca12b`; the only intervening change in this PR's paths is another edit to `docs/dev/debt/libkernel.md`. Please sync to current main and rerun conventions so the debt entry is integrated on the latest base. I did not independently run the Windows suite.
THEMANnt commented 2026-10-26 16:07:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-26T16:07:11Z.

Review: APPROVED


Reviewed 32d7260ad7db against 2dbe9b99f577. No actionable findings.

pthread_getaffinity_np now succeeds only for measured 8- and 16-byte sets, returns ERANGE at 32 bytes or above, and throws for other smaller unmeasured sizes. The regression checks both supported sizes, rejected-size boundaries, null arguments, and output-buffer bounds.

Validation: The Release target guest_available_cpumask_tests built successfully and focused CTest passed 1/1. An independent 0–64 size sweep confirmed only 8 and 16 succeed, with ERANGE at and above 32 and buffer preservation on rejected inputs. Hosted conventions and labels passed; all hosted product build/test jobs were skipped. The patch also applies cleanly to local main 5bdda3661e2c; its intervening change is in the debt file, while the implementation and test are unchanged.

The focused target was not rebuilt on that newer main, and no console/title test was run.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5655142261 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11431#pullrequestreview-5655142261). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-26T16:07:11Z. **Review:** APPROVED --- Reviewed `32d7260ad7db` against `2dbe9b99f577`. No actionable findings. `pthread_getaffinity_np` now succeeds only for measured 8- and 16-byte sets, returns `ERANGE` at 32 bytes or above, and throws for other smaller unmeasured sizes. The regression checks both supported sizes, rejected-size boundaries, null arguments, and output-buffer bounds. **Validation:** The Release target `guest_available_cpumask_tests` built successfully and focused CTest passed 1/1. An independent 0–64 size sweep confirmed only 8 and 16 succeed, with `ERANGE` at and above 32 and buffer preservation on rejected inputs. Hosted conventions and labels passed; all hosted product build/test jobs were skipped. The patch also applies cleanly to local `main` `5bdda3661e2c`; its intervening change is in the debt file, while the implementation and test are unchanged. The focused target was not rebuilt on that newer main, and no console/title test was run.
MarcZX10 commented 2026-10-29 09:02:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:02:56Z.

Review: CHANGES_REQUESTED


Re-check — changes requested\n\nThe new report resolves my previous request against base 4c7d275bef: guest_available_cpumask passes 3/3 and conventions pass; the single guest_freebsd_clocks failure is reported on main, and guest_filesystem passed 5/5 when isolated after one parallel failure.\n\nCurrent upstream main is now aaccd0ba39, and this head is 44 commits behind it. Please update to current main and rerun guest_available_cpumask plus conventions on the exact resulting head. I did not independently run the Windows tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695177364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11431#pullrequestreview-5695177364). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:02:56Z. **Review:** CHANGES_REQUESTED --- ## Re-check — changes requested\n\nThe new report resolves my previous request against base 4c7d275bef: guest_available_cpumask passes 3/3 and conventions pass; the single guest_freebsd_clocks failure is reported on main, and guest_filesystem passed 5/5 when isolated after one parallel failure.\n\nCurrent upstream main is now aaccd0ba39, and this head is 44 commits behind it. Please update to current main and rerun guest_available_cpumask plus conventions on the exact resulting head. I did not independently run the Windows tests.
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!11431
No description provided.