libkernel: pthread_cond_wait and pthread_cond_timedwait abort the process when the mutex is not held, where FreeBSD returns EPERM #2988

Closed
opened 2026-10-10 19:49:50 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-10 19:49:50 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T19:49:50Z.


What

pthread_cond_wait, pthread_cond_timedwait, scePthreadCondWait and scePthreadCondTimedwait (core/libs/prx/libkernel/Pthread/src/Cond.cpp, lockedMutex) throw a host std::runtime_error or std::invalid_argument, which ends the process, when the mutex passed in is not held by the calling thread. Cases: an unlocked mutex, a mutex locked by another thread, a statically initialised (zeroed) mutex, and the adaptive static initialiser (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, pointer value 1) which is dereferenced outright and crashes.

FreeBSD's libthr returns an error instead: cond_wait_common calls _mutex_owned before touching any state (thr_cond.c, thr_mutex.c, 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. scePthreadMutexUnlock and the rwlock functions in this repo already return EPERM for the same kind of misuse.

Reproduce

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release, main at the time of writing: create a condition variable and a mutex, do not lock the mutex, call pthread_cond_wait(&cond, &mutex); the process terminates with exit code 0xc0000409. The same with a mutex locked by another thread, and with a zeroed pthread_mutex_t. pthread_cond_timedwait with an unowned mutex behaves the same.

Fix

A tested fix is on my fork: husseinkad:fix/pthread-cond-wait-unowned-mutex (one commit on main; PR #2752, closed by the intake check). The ownership check runs before any state change (no waiter registered, the mutex and the condition variable untouched) and returns SCE_KERNEL_ERROR_EPERM or EINVAL (the POSIX wrappers 1 and 22); a null mutex or condition pointer still throws. It adds guest_cond_unowned_mutex, which fails without the change and passes with it.

Checked against main's Cond.cpp over the same libkernel: 40 producer/consumer stress scenarios of 3 seconds each (five mutex types, untimed and timed waiters) with no false EPERM, lost wakeup or hang; 96 legitimate-hold cases byte-identical; 116 error-path cases where main threw or segfaulted now return EPERM/EINVAL with the mutex and the condition variable left intact; per-call cost unchanged. Linux not run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5794712835 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2988). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T19:49:50Z. --- ### What `pthread_cond_wait`, `pthread_cond_timedwait`, `scePthreadCondWait` and `scePthreadCondTimedwait` (`core/libs/prx/libkernel/Pthread/src/Cond.cpp`, `lockedMutex`) throw a host `std::runtime_error` or `std::invalid_argument`, which ends the process, when the mutex passed in is not held by the calling thread. Cases: an unlocked mutex, a mutex locked by another thread, a statically initialised (zeroed) mutex, and the adaptive static initialiser (`PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP`, pointer value 1) which is dereferenced outright and crashes. FreeBSD's libthr returns an error instead: `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), 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. `scePthreadMutexUnlock` and the rwlock functions in this repo already return `EPERM` for the same kind of misuse. ### Reproduce Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release, `main` at the time of writing: create a condition variable and a mutex, do not lock the mutex, call `pthread_cond_wait(&cond, &mutex)`; the process terminates with exit code `0xc0000409`. The same with a mutex locked by another thread, and with a zeroed `pthread_mutex_t`. `pthread_cond_timedwait` with an unowned mutex behaves the same. ### Fix A tested fix is on my fork: [`husseinkad:fix/pthread-cond-wait-unowned-mutex`](https://github.com/husseinkad/AnyPS5/tree/fix/pthread-cond-wait-unowned-mutex) (one commit on `main`; PR #2752, closed by the intake check). The ownership check runs before any state change (no waiter registered, the mutex and the condition variable untouched) and returns `SCE_KERNEL_ERROR_EPERM` or `EINVAL` (the POSIX wrappers 1 and 22); a null mutex or condition pointer still throws. It adds `guest_cond_unowned_mutex`, which fails without the change and passes with it. Checked against `main`'s `Cond.cpp` over the same `libkernel`: 40 producer/consumer stress scenarios of 3 seconds each (five mutex types, untimed and timed waiters) with no false `EPERM`, lost wakeup or hang; 96 legitimate-hold cases byte-identical; 116 error-path cases where `main` threw or segfaulted now return `EPERM`/`EINVAL` with the mutex and the condition variable left intact; per-call cost unchanged. Linux not run. AI-assisted: yes.
oneandonlydean commented 2026-10-11 01:08:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:44Z.


Fixed by #3011.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104055547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2988#issuecomment-6104055547). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T01:08:44Z. --- Fixed by #3011.
Sign in to join this conversation.
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#2988
No description provided.