libkernel: pthread_cond_wait and pthread_cond_timedwait abort the process when the mutex is not held, where FreeBSD returns EPERM #2988
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T19:49:50Z.
What
pthread_cond_wait,pthread_cond_timedwait,scePthreadCondWaitandscePthreadCondTimedwait(core/libs/prx/libkernel/Pthread/src/Cond.cpp,lockedMutex) throw a hoststd::runtime_errororstd::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_commoncalls_mutex_ownedbefore touching any state (thr_cond.c, thr_mutex.c, releng/11.4), which returnsEPERMwhen the calling thread is not the owner (including theNULLand adaptive static initialisers, which it does not initialise on demand) andEINVALfor a destroyed mutex.pthread_cond_wait(3)lists[EPERM]for a mutex the calling thread does not hold.scePthreadMutexUnlockand the rwlock functions in this repo already returnEPERMfor the same kind of misuse.Reproduce
Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release,
mainat the time of writing: create a condition variable and a mutex, do not lock the mutex, callpthread_cond_wait(&cond, &mutex); the process terminates with exit code0xc0000409. The same with a mutex locked by another thread, and with a zeroedpthread_mutex_t.pthread_cond_timedwaitwith an unowned mutex behaves the same.Fix
A tested fix is on my fork:
husseinkad:fix/pthread-cond-wait-unowned-mutex(one commit onmain; 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 returnsSCE_KERNEL_ERROR_EPERMorEINVAL(the POSIX wrappers 1 and 22); a null mutex or condition pointer still throws. It addsguest_cond_unowned_mutex, which fails without the change and passes with it.Checked against
main'sCond.cppover the samelibkernel: 40 producer/consumer stress scenarios of 3 seconds each (five mutex types, untimed and timed waiters) with no falseEPERM, lost wakeup or hang; 96 legitimate-hold cases byte-identical; 116 error-path cases wheremainthrew or segfaulted now returnEPERM/EINVALwith the mutex and the condition variable left intact; per-call cost unchanged. Linux not run.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:44Z.
Fixed by #3011.