libkernel: scePthreadRwlockUnlock on a lock that is not held is undefined behaviour and returns success, where FreeBSD returns EPERM #3096

Closed
opened 2026-10-10 22:02:01 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-10 22:02:01 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:02:01Z.


What

scePthreadRwlockUnlock and pthread_rwlock_unlock (core/libs/prx/libkernel/Pthread/src/Rwlock.cpp) call unlock_shared() on the host shared_timed_mutex for any caller that is not the write owner. When the rwlock is not held at all, or is write-held by another thread, that is undefined behaviour on the host (it can corrupt the lock for the threads that do hold it), and the call returns 0.

FreeBSD libthr returns EPERM there (_Tthr_rwlock_unlock and do_rw_unlock in sys/kern/kern_umtx.c, releng/11.4): when the lock has no write owner and a zero reader count, and when the write owner is another thread. pthread_rwlock_unlock(3) lists [EPERM] "The current thread does not own the read/write lock". Like libthr, the reader count is per lock, not per thread, so a thread that holds no read lock can still unlock while another thread holds one; what is catchable is unlocking a lock nobody holds and unlocking a write lock owned by another thread. scePthreadMutexUnlock in this repo already returns EPERM for a non-owner.

Reproduce

Windows 11, WinLibs GCC 15.2.0 r7, main at the time of writing: create a rwlock and call pthread_rwlock_unlock without locking it, or lock for read, unlock twice (the second returns 0), or have a thread hold the write lock while another unlocks: all return 0, and the lock is damaged afterwards (a later timedwrlock no longer times out).

Fix

A tested fix is on my fork: husseinkad:fix/pthread-rwlock-unlock-unowned (two commits on main; PR #2679, closed by the intake check): PthreadRwlockPrivate keeps an atomic reader count, incremented after every successful read acquisition (rdlock, tryrdlock, timedrdlock; failed or timed-out attempts do not count) and decremented by a compare-exchange loop in unlock, which returns EPERM at 0 or when another thread owns the write lock. guest_pthread_rwlock covers a never-locked rwlock, a double unlock, unlock after trywrlock, an unlock while another thread holds the write lock, and that timed-out reads leave no count; it fails without the change.

A 5 to 15 second stress of the guest exports with up to 24 reader and 8 writer threads mixing rdlock/tryrdlock/timedrdlock/wrlock/trywrlock/timedwrlock (about 100 million lock operations over nine thread-count configurations) found no false EPERM, no leaked or negative count, no torn write and no hang. Open PR #2051 edits the lock-acquire lines of the same file, so whichever lands second needs a rebase; the increment has to stay after the acquire. Linux not run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795667735 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3096). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T22:02:01Z. --- ### What `scePthreadRwlockUnlock` and `pthread_rwlock_unlock` (`core/libs/prx/libkernel/Pthread/src/Rwlock.cpp`) call `unlock_shared()` on the host `shared_timed_mutex` for any caller that is not the write owner. When the rwlock is not held at all, or is write-held by another thread, that is undefined behaviour on the host (it can corrupt the lock for the threads that do hold it), and the call returns 0. FreeBSD libthr returns `EPERM` there ([`_Tthr_rwlock_unlock`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_rwlock.c) and `do_rw_unlock` in `sys/kern/kern_umtx.c`, releng/11.4): when the lock has no write owner and a zero reader count, and when the write owner is another thread. `pthread_rwlock_unlock(3)` lists `[EPERM]` "The current thread does not own the read/write lock". Like libthr, the reader count is per lock, not per thread, so a thread that holds no read lock can still unlock while another thread holds one; what is catchable is unlocking a lock nobody holds and unlocking a write lock owned by another thread. `scePthreadMutexUnlock` in this repo already returns `EPERM` for a non-owner. ### Reproduce Windows 11, WinLibs GCC 15.2.0 r7, `main` at the time of writing: create a rwlock and call `pthread_rwlock_unlock` without locking it, or lock for read, unlock twice (the second returns 0), or have a thread hold the write lock while another unlocks: all return 0, and the lock is damaged afterwards (a later `timedwrlock` no longer times out). ### Fix A tested fix is on my fork: [`husseinkad:fix/pthread-rwlock-unlock-unowned`](https://github.com/husseinkad/AnyPS5/tree/fix/pthread-rwlock-unlock-unowned) (two commits on `main`; PR #2679, closed by the intake check): `PthreadRwlockPrivate` keeps an atomic reader count, incremented after every successful read acquisition (`rdlock`, `tryrdlock`, `timedrdlock`; failed or timed-out attempts do not count) and decremented by a compare-exchange loop in unlock, which returns `EPERM` at 0 or when another thread owns the write lock. `guest_pthread_rwlock` covers a never-locked rwlock, a double unlock, unlock after `trywrlock`, an unlock while another thread holds the write lock, and that timed-out reads leave no count; it fails without the change. A 5 to 15 second stress of the guest exports with up to 24 reader and 8 writer threads mixing `rdlock`/`tryrdlock`/`timedrdlock`/`wrlock`/`trywrlock`/`timedwrlock` (about 100 million lock operations over nine thread-count configurations) found no false `EPERM`, no leaked or negative count, no torn write and no hang. Open PR #2051 edits the lock-acquire lines of the same file, so whichever lands second needs a rebase; the increment has to stay after the acquire. Linux not run. AI-assisted: yes.
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#3096
No description provided.