libkernel: scePthreadRwlockUnlock on a lock that is not held is undefined behaviour and returns success, where FreeBSD returns EPERM #3096
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-10T22:02:01Z.
What
scePthreadRwlockUnlockandpthread_rwlock_unlock(core/libs/prx/libkernel/Pthread/src/Rwlock.cpp) callunlock_shared()on the hostshared_timed_mutexfor 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
EPERMthere (_Tthr_rwlock_unlockanddo_rw_unlockinsys/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.scePthreadMutexUnlockin this repo already returnsEPERMfor a non-owner.Reproduce
Windows 11, WinLibs GCC 15.2.0 r7,
mainat the time of writing: create a rwlock and callpthread_rwlock_unlockwithout 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 latertimedwrlockno longer times out).Fix
A tested fix is on my fork:
husseinkad:fix/pthread-rwlock-unlock-unowned(two commits onmain; PR #2679, closed by the intake check):PthreadRwlockPrivatekeeps 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 returnsEPERMat 0 or when another thread owns the write lock.guest_pthread_rwlockcovers a never-locked rwlock, a double unlock, unlock aftertrywrlock, 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 falseEPERM, 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.