fix(libkernel): return EPERM or EINVAL when a condition wait gets a mutex the caller does not hold #2752

Closed
husseinkad wants to merge 1 commit from upstream-pr/2752 into main
husseinkad commented 2026-10-10 12:54:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T12:54:35Z.


What

pthread_cond_wait, pthread_cond_timedwait, scePthreadCondWait and scePthreadCondTimedwait threw a host std::runtime_error or std::invalid_argument (ending the process) when the mutex was not held by the calling thread: an unlocked mutex, one locked by another thread, a statically initialised (null) one, and the adaptive static initialiser (pointer value 1) was dereferenced outright (a crash). FreeBSD libthr's cond_wait_common calls _mutex_owned before touching any state (thr_cond.c, thr_mutex.c at releng/11.4), which returns EPERM when the calling thread is not the owner (including the NULL and adaptive static initialisers, which it does not initialise on demand) and EINVAL for a destroyed mutex; pthread_cond_wait(3) lists [EPERM] for a mutex the calling thread does not hold. The mutex unlock and rwlock paths in this repo already return EPERM the same way.

The ownership check (mutexOwnership, replacing lockedMutex) runs before the trace, the owner clear, the unlock and the wait, so no waiter is registered and the mutex and the condition variable are left exactly as they were. It returns SCE_KERNEL_ERROR_EPERM or SCE_KERNEL_ERROR_EINVAL (the POSIX wrappers 1 and 22); a null mutex or condition pointer still throws.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • New guest_cond_unowned_mutex (registered in the libkernel CMakeLists.txt, no sleeps) covers the four entry points, plain and timed, for an unlocked mutex, one held by another thread, a zeroed one, the adaptive static initialiser and a destroyed one; it fails without the change (exit 0xc0000409) and passes with it. ctest -R "guest_cond|guest_pthread|guest_semaphore|guest_once|guest_thread|guest_kernel_sync" passes.
  • Outside the repo, main's Cond.cpp and this branch's side by side over the same libkernel: 40 producer/consumer stress scenarios of 3 s each (5 mutex types x untimed and timed waiters x lock/trylock/timedlock x two thread-count shapes): 0 false EPERM, no lost wakeup, no hang, all identical to main; a 96-case matrix of legitimate holds is byte-identical; 116 error-path cases where main threw or segfaulted now return EPERM/EINVAL and leave the mutex (another thread can lock it) and the condition variable (a normal wait/signal pair works) intact; timeouts, the tv_nsec and null-abstime EINVAL and a recursive mutex locked twice behave as before; per-call cost unchanged (0.685 s against 0.691 s per million calls).
  • tools/check_conventions.py --base origin/main and tools/check_test_registration.py build pass; tools/pr_overlap.py --branch reports no overlap.
  • Linux not run.

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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4811347343 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2752). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T12:54:35Z. --- ### What `pthread_cond_wait`, `pthread_cond_timedwait`, `scePthreadCondWait` and `scePthreadCondTimedwait` threw a host `std::runtime_error` or `std::invalid_argument` (ending the process) when the mutex was not held by the calling thread: an unlocked mutex, one locked by another thread, a statically initialised (null) one, and the adaptive static initialiser (pointer value 1) was dereferenced outright (a crash). FreeBSD libthr's `cond_wait_common` calls `_mutex_owned` before touching any state ([thr_cond.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_cond.c), [thr_mutex.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_mutex.c) at releng/11.4), which returns `EPERM` when the calling thread is not the owner (including the `NULL` and adaptive static initialisers, which it does not initialise on demand) and `EINVAL` for a destroyed mutex; `pthread_cond_wait(3)` lists `[EPERM]` for a mutex the calling thread does not hold. The mutex unlock and rwlock paths in this repo already return `EPERM` the same way. The ownership check (`mutexOwnership`, replacing `lockedMutex`) runs before the trace, the owner clear, the unlock and the wait, so no waiter is registered and the mutex and the condition variable are left exactly as they were. It returns `SCE_KERNEL_ERROR_EPERM` or `SCE_KERNEL_ERROR_EINVAL` (the POSIX wrappers 1 and 22); a null mutex or condition pointer still throws. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - New `guest_cond_unowned_mutex` (registered in the libkernel `CMakeLists.txt`, no sleeps) covers the four entry points, plain and timed, for an unlocked mutex, one held by another thread, a zeroed one, the adaptive static initialiser and a destroyed one; it fails without the change (exit `0xc0000409`) and passes with it. `ctest -R "guest_cond|guest_pthread|guest_semaphore|guest_once|guest_thread|guest_kernel_sync"` passes. - Outside the repo, `main`'s `Cond.cpp` and this branch's side by side over the same `libkernel`: 40 producer/consumer stress scenarios of 3 s each (5 mutex types x untimed and timed waiters x lock/trylock/timedlock x two thread-count shapes): 0 false `EPERM`, no lost wakeup, no hang, all identical to `main`; a 96-case matrix of legitimate holds is byte-identical; 116 error-path cases where `main` threw or segfaulted now return `EPERM`/`EINVAL` and leave the mutex (another thread can lock it) and the condition variable (a normal wait/signal pair works) intact; timeouts, the `tv_nsec` and null-abstime `EINVAL` and a recursive mutex locked twice behave as before; per-call cost unchanged (0.685 s against 0.691 s per million calls). - `tools/check_conventions.py --base origin/main` and `tools/check_test_registration.py build` pass; `tools/pr_overlap.py --branch` reports no overlap. - Linux not run. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-10 12:54:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T12:54:44Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097715900 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2752#issuecomment-6097715900). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T12:54:44Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

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!2752
No description provided.