fix(libkernel): return EFAULT for an unwritable pthread_key_create key pointer #15748

Merged
Gruzzberry merged 3 commits from upstream-pr/15748 into main 2026-11-08 05:15:23 +00:00
Gruzzberry commented 2026-11-04 12:05:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-04T12:05:43Z.


What

pthread_key_create stored the new key through the guest pointer with a raw write, so an unusable pointer such as 0x10 or a read-only page crashed the host. FreeBSD 11.4's _pthread_key_create does the same unchecked *key = i + 1 and the process faults, so there is no libthr error to copy; this is a host-safety fallback. The key is now taken, written with GuestCopyOut_nid_no_patch, and given back with scePthreadKeyDelete if the write fails, so a failed call returns EFAULT and leaves the key number free. A NULL key pointer keeps returning EINVAL as before, and scePthreadKeyCreate is not touched (#14128 changes its null check). pthread_key_delete takes no pointer and already matches _pthread_key_delete (EINVAL for key 0, a key past the table and a key that is not allocated), so it is unchanged; the test pins it. pthread_getspecific and pthread_setspecific are hot paths and are unchanged. The new debt entry records that the EFAULT is unmeasured.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on main b11c7bf84f.

  • guest_posix_specific: new checks for 0x10 and a read-only page as the key pointer (EFAULT, and the next pthread_key_create returns the same key number, so nothing leaked), NULL (EINVAL), and pthread_key_delete of a deleted key, key 0 and key 0x7fffffff (EINVAL). With Posix/Tsd.cpp reverted to main the test crashes (SegFault); with this change it passes, three runs out of three.
  • Full suite on this branch: the only failures are vulkan_validation_gate_reports, check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted and agc_driver_direct_write_completion, which are environment failures here (Python and GPU tests); agc_driver_direct_write_completion also fails with the changed source reverted to main.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run.

Overlaps

tools/pr_overlap.py reports no conflicting source or test file. The PRs it lists only append to docs/dev/debt/libkernel.md at the same place as this one, which keeps both lines. #14128 changes scePthreadKeyCreate in Pthread/src/Tsd.cpp and GuestPthreadKey.cpp, neither of which this PR edits.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none; merge order: no source overlap (only the shared debt-file append)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5153910440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15748). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-04T12:05:43Z. --- ### What `pthread_key_create` stored the new key through the guest pointer with a raw write, so an unusable pointer such as `0x10` or a read-only page crashed the host. FreeBSD 11.4's [`_pthread_key_create`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_spec.c#L53-L79) does the same unchecked `*key = i + 1` and the process faults, so there is no libthr error to copy; this is a host-safety fallback. The key is now taken, written with `GuestCopyOut_nid_no_patch`, and given back with `scePthreadKeyDelete` if the write fails, so a failed call returns `EFAULT` and leaves the key number free. A NULL key pointer keeps returning `EINVAL` as before, and `scePthreadKeyCreate` is not touched (#14128 changes its null check). `pthread_key_delete` takes no pointer and already matches [`_pthread_key_delete`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_spec.c#L81-L100) (`EINVAL` for key 0, a key past the table and a key that is not allocated), so it is unchanged; the test pins it. `pthread_getspecific` and `pthread_setspecific` are hot paths and are unchanged. The new debt entry records that the `EFAULT` is unmeasured. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on `main` b11c7bf84f. - `guest_posix_specific`: new checks for `0x10` and a read-only page as the key pointer (`EFAULT`, and the next `pthread_key_create` returns the same key number, so nothing leaked), NULL (`EINVAL`), and `pthread_key_delete` of a deleted key, key 0 and key `0x7fffffff` (`EINVAL`). With `Posix/Tsd.cpp` reverted to `main` the test crashes (`SegFault`); with this change it passes, three runs out of three. - Full suite on this branch: the only failures are `vulkan_validation_gate_reports`, `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted` and `agc_driver_direct_write_completion`, which are environment failures here (Python and GPU tests); `agc_driver_direct_write_completion` also fails with the changed source reverted to `main`. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. ### Overlaps `tools/pr_overlap.py` reports no conflicting source or test file. The PRs it lists only append to `docs/dev/debt/libkernel.md` at the same place as this one, which keeps both lines. #14128 changes `scePthreadKeyCreate` in `Pthread/src/Tsd.cpp` and `GuestPthreadKey.cpp`, neither of which this PR edits. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [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; merge order: no source overlap (only the shared debt-file append) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ahmadmuj commented 2026-11-06 11:05:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T11:05:57Z.

Review: APPROVED


Checked head 979af971268f237766b452b13eb15fa1d8683944, PR base e64c194c4343f473e815f00321fcd03910381949 and pinned main 5299939ecfa9e186e1eb457c6f4bd133bdb087c7.

On Ubuntu 24.04.2/WSL2, GCC 13.3 Release, a separate probe links the actual libkernel/libc modules with the existing fixture's flags. Loaded paths and symbols match the build.

  • All 512 keys are available at startup. At capacity, valid and protected outputs return 35; valid output stays unchanged.
  • With one slot free, read-only output returns14. After 1, 8, 8 failed writes, valid creation reclaims key 257 each time.
  • Retained keys keep exact independent values. Cleanup deletes each owned live key and gets 22 on a second delete.
  • Removing rollback makes exact-slot reuse fail with 35. Moving checked output before allocation returns14 at full protected output instead of 35.
  • On pinned main, the existing fixture passes. The same capacity probe reaches the first read-only copy with one slot free and receives SIGSEGV.
  • Restored production and guest_posix_specific pass. python3 tools/check_conventions.py --base origin/main passes.

Commands: cmake --build <build> --target guest_posix_specific_tests -j 8, ctest --test-dir <build> -R '^guest_posix_specific$' --output-on-failure, and timeout 30s <build>/tests/key_capacity_rollback_probe. Both revisions use separate builds. The final runner uses an environment without credentials. Each passing probe finishes below 0.02 seconds.

The pinned FreeBSD source checks allocation before output. EFAULT rollback remains the documented host fallback; this does not establish console behavior.

No scoped correctness finding. CPU-only evidence; no Windows, macOS, console, game or GPU execution in this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5799234354 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15748#pullrequestreview-5799234354). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T11:05:57Z. **Review:** APPROVED --- Checked head `979af971268f237766b452b13eb15fa1d8683944`, PR base `e64c194c4343f473e815f00321fcd03910381949` and pinned main `5299939ecfa9e186e1eb457c6f4bd133bdb087c7`. On Ubuntu 24.04.2/WSL2, GCC 13.3 Release, a separate probe links the actual libkernel/libc modules with the existing fixture's flags. Loaded paths and symbols match the build. - All 512 keys are available at startup. At capacity, valid and protected outputs return 35; valid output stays unchanged. - With one slot free, read-only output returns14. After 1, 8, 8 failed writes, valid creation reclaims key 257 each time. - Retained keys keep exact independent values. Cleanup deletes each owned live key and gets 22 on a second delete. - Removing rollback makes exact-slot reuse fail with 35. Moving checked output before allocation returns14 at full protected output instead of 35. - On pinned main, the existing fixture passes. The same capacity probe reaches the first read-only copy with one slot free and receives SIGSEGV. - Restored production and `guest_posix_specific` pass. `python3 tools/check_conventions.py --base origin/main` passes. Commands: `cmake --build <build> --target guest_posix_specific_tests -j 8`, `ctest --test-dir <build> -R '^guest_posix_specific$' --output-on-failure`, and `timeout 30s <build>/tests/key_capacity_rollback_probe`. Both revisions use separate builds. The final runner uses an environment without credentials. Each passing probe finishes below 0.02 seconds. The [pinned FreeBSD source](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_spec.c#L53-L79) checks allocation before output. EFAULT rollback remains the documented host fallback; this does not establish console behavior. No scoped correctness finding. CPU-only evidence; no Windows, macOS, console, game or GPU execution in 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!15748
No description provided.