fix(libkernel): fail condition variable init, destroy and setname with EFAULT on an unusable pointer #17220

Merged
Gruzzberry merged 2 commits from upstream-pr/17220 into main 2026-11-21 10:59:56 +00:00
Gruzzberry commented 2026-11-08 15:03:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-08T15:03:01Z.


What

pthread_cond_init, pthread_cond_destroy, pthread_cond_setname_np, scePthreadCondInit and scePthreadCondDestroy read and wrote the guest condition variable pointer, and the init calls also read the guest attribute pointer, without checking them, so an unusable pointer such as 0x10 or a read-only page crashed the host. FreeBSD 11.4's _pthread_cond_init and _pthread_cond_destroy store through the pointer the same way and the process faults, so libthr has no error to copy here; this is a host-safety fallback, and pthread_cond_setname_np has no libthr counterpart. A condition variable pointer that cannot be read, or cannot be written where the call stores, now returns EFAULT (SCE_KERNEL_ERROR_EFAULT from the sce calls), as does an attribute pointer that cannot be read, using GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch. Destroy reads the slot first and writes only where it stores, so the EINVAL for a destroyed condition variable and the 0 for a null static initializer are unchanged even in a read-only page; setname writes only when it creates the condition variable for a null slot. A null condition variable pointer keeps throwing (or EINVAL from pthread_cond_setname_np) and a null attribute still means the default. pthread_cond_signal, broadcast, wait, timedwait and scePthreadCondSignalto are untouched: they are hot paths and a copy helper would cost them on every call. 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 f5fe807353.

  • guest_pthread_cond_setname: new checks for the condition variable pointer 0x10, a read-only page and an unreadable page (EFAULT from pthread_cond_init, pthread_cond_setname_np and, for 0x10 and the unreadable page, pthread_cond_destroy; SCE_KERNEL_ERROR_EFAULT from scePthreadCondInit and scePthreadCondDestroy; pthread_cond_destroy of the zeroed read-only page gives 0), an attribute pointer 0x10 and an unreadable page (EFAULT from both init calls, the slot stays null), and the success path afterwards. With Pthread/src/Cond.cpp reverted to main the test crashes (SegFault); with this change it passes, three runs out of three.
  • The other condition variable, cancellation and destroyed-object tests (guest_pthread_cond, guest_pthread_condattr, guest_cond_timedwait, guest_cond_unowned_mutex, guest_cond_recursive_mutex, guest_posix_cond_broadcast, guest_pthread_cancel, guest_pthread_cancel_recovery, guest_pthread_destroyed_objects and the coarse variants) pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run, the full suite.

Overlaps

  • pr_overlap.py lists #13582, #15524, #15567, #17212 and #17216, and every row is docs/dev/debt/libkernel.md at the same appended place as this PR's entry, which keeps both lines; no other PR touches Pthread/src/Cond.cpp or GuestPthreadCondSetname.cpp.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5212377931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17220). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-08T15:03:01Z. --- ### What `pthread_cond_init`, `pthread_cond_destroy`, `pthread_cond_setname_np`, `scePthreadCondInit` and `scePthreadCondDestroy` read and wrote the guest condition variable pointer, and the init calls also read the guest attribute pointer, without checking them, so an unusable pointer such as `0x10` or a read-only page crashed the host. FreeBSD 11.4's [`_pthread_cond_init` and `_pthread_cond_destroy`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_cond.c) store through the pointer the same way and the process faults, so libthr has no error to copy here; this is a host-safety fallback, and `pthread_cond_setname_np` has no libthr counterpart. A condition variable pointer that cannot be read, or cannot be written where the call stores, now returns `EFAULT` (`SCE_KERNEL_ERROR_EFAULT` from the `sce` calls), as does an attribute pointer that cannot be read, using `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch`. Destroy reads the slot first and writes only where it stores, so the `EINVAL` for a destroyed condition variable and the 0 for a null static initializer are unchanged even in a read-only page; setname writes only when it creates the condition variable for a null slot. A null condition variable pointer keeps throwing (or `EINVAL` from `pthread_cond_setname_np`) and a null attribute still means the default. `pthread_cond_signal`, `broadcast`, `wait`, `timedwait` and `scePthreadCondSignalto` are untouched: they are hot paths and a copy helper would cost them on every call. 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` f5fe807353. - `guest_pthread_cond_setname`: new checks for the condition variable pointer `0x10`, a read-only page and an unreadable page (`EFAULT` from `pthread_cond_init`, `pthread_cond_setname_np` and, for `0x10` and the unreadable page, `pthread_cond_destroy`; `SCE_KERNEL_ERROR_EFAULT` from `scePthreadCondInit` and `scePthreadCondDestroy`; `pthread_cond_destroy` of the zeroed read-only page gives 0), an attribute pointer `0x10` and an unreadable page (`EFAULT` from both init calls, the slot stays null), and the success path afterwards. With `Pthread/src/Cond.cpp` reverted to `main` the test crashes (`SegFault`); with this change it passes, three runs out of three. - The other condition variable, cancellation and destroyed-object tests (`guest_pthread_cond`, `guest_pthread_condattr`, `guest_cond_timedwait`, `guest_cond_unowned_mutex`, `guest_cond_recursive_mutex`, `guest_posix_cond_broadcast`, `guest_pthread_cancel`, `guest_pthread_cancel_recovery`, `guest_pthread_destroyed_objects` and the coarse variants) pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run, the full suite. ### Overlaps - `pr_overlap.py` lists #13582, #15524, #15567, #17212 and #17216, and every row is `docs/dev/debt/libkernel.md` at the same appended place as this PR's entry, which keeps both lines; no other PR touches `Pthread/src/Cond.cpp` or `GuestPthreadCondSetname.cpp`. ### 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: none - [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)
PrivacyOnion commented 2026-11-08 15:34:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-08T15:34:36Z.

Review: APPROVED


What I ran

  • Head commit: 53feebc4c79805d76d4948a3d5ec7b653457a3e3
  • Merge-base: f5fe8073533b671946ab8999ebbee85f4f78c597 (merges cleanly with current main).
  • Environment: Windows 11 x86-64, MinGW-w64 GCC 15.2.0 (WinLibs UCRT POSIX), CMake 3.31, Ninja 1.13.

Tools and Checks

  • python tools/check_conventions.py --base upstream/main --head pr-17220: 0 errors.
  • git diff --check upstream/main...pr-17220: clean.

Windows Test Suite & Negative Control

Test Status Duration
guest_pthread_cond_setname Passed 0.02 s
guest_pthread_cond Passed 0.04 s
guest_pthread_condattr Passed 0.04 s
guest_cond_timedwait Passed 0.04 s
guest_cond_recursive_mutex Passed 0.04 s
guest_cond_unowned_mutex Passed 0.04 s
guest_posix_cond_broadcast Passed 0.05 s
  • Negative control: Restoring core/libs/prx/libkernel/Pthread/src/Cond.cpp from main fails guest_pthread_cond_setname with SIGSEGV on write of 0x0000000000000010 (0xc0000005). With this PR, all invalid slot and attribute checks cleanly return EFAULT / SCE_KERNEL_ERROR_EFAULT.
  • Memory safety: std::unique_ptr release is guarded by successful GuestCopyOut_nid_no_patch, preventing leaks on write failures. In scePthreadCondDestroy, the handle is deleted only if GuestCopyOut successfully overwrote the slot with destroyedCond(), avoiding dangling pointers in the guest address space.

Please remove the generated attribution footer below the checklist; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5818472269 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17220#pullrequestreview-5818472269). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-08T15:34:36Z. **Review:** APPROVED --- ### What I ran - **Head commit:** `53feebc4c79805d76d4948a3d5ec7b653457a3e3` - **Merge-base:** `f5fe8073533b671946ab8999ebbee85f4f78c597` (merges cleanly with current `main`). - **Environment:** Windows 11 x86-64, MinGW-w64 GCC 15.2.0 (WinLibs UCRT POSIX), CMake 3.31, Ninja 1.13. ### Tools and Checks - `python tools/check_conventions.py --base upstream/main --head pr-17220`: 0 errors. - `git diff --check upstream/main...pr-17220`: clean. ### Windows Test Suite & Negative Control | Test | Status | Duration | |---|---|---| | `guest_pthread_cond_setname` | Passed | 0.02 s | | `guest_pthread_cond` | Passed | 0.04 s | | `guest_pthread_condattr` | Passed | 0.04 s | | `guest_cond_timedwait` | Passed | 0.04 s | | `guest_cond_recursive_mutex` | Passed | 0.04 s | | `guest_cond_unowned_mutex` | Passed | 0.04 s | | `guest_posix_cond_broadcast` | Passed | 0.05 s | - **Negative control:** Restoring `core/libs/prx/libkernel/Pthread/src/Cond.cpp` from `main` fails `guest_pthread_cond_setname` with `SIGSEGV` on `write of 0x0000000000000010` (`0xc0000005`). With this PR, all invalid slot and attribute checks cleanly return `EFAULT` / `SCE_KERNEL_ERROR_EFAULT`. - **Memory safety:** `std::unique_ptr` release is guarded by successful `GuestCopyOut_nid_no_patch`, preventing leaks on write failures. In `scePthreadCondDestroy`, the handle is deleted only if `GuestCopyOut` successfully overwrote the slot with `destroyedCond()`, avoiding dangling pointers in the guest address space. Please remove the generated attribution footer below the checklist; the AI-assisted checkbox can stay. Looks Good To Me
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!17220
No description provided.