fix(libkernel): return EPERM from scePthreadRwlockUnlock when the lock is not held #2679

Closed
husseinkad wants to merge 2 commits from upstream-pr/2679 into main
husseinkad commented 2026-10-10 10:49:29 +00:00 (Migrated from github.com)

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


What

scePthreadRwlockUnlock (and pthread_rwlock_unlock) called unlock_shared() on the host shared_timed_mutex for any caller that was not the write owner. When the rwlock is not held at all, or is write-held by another thread, that is undefined behaviour, and the call returned 0.

FreeBSD libthr (_Tthr_rwlock_unlock in lib/libthr/thread/thr_rwlock.c, do_rw_unlock in sys/kern/kern_umtx.c) returns EPERM 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".

PthreadRwlockPrivate now 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 that returns EPERM at 0. Like libthr, the count is per lock, not per thread, so a thread that holds no read lock can still unlock while another thread holds one; what this catches is unlocking a lock that nobody holds and unlocking a write lock owned by another thread.

Tested

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

  • guest_pthread_rwlock: new cases unlock of a never-locked rwlock, double unlock after tryrdlock, unlock after trywrlock, unlock from the main thread while another thread holds the write lock, and that timed-out reads leave no count; all expect EPERM. The test fails without the change (exit 0xc0000409) and passes with it.
  • Outside the repo: a 5-15 s stress probe of the guest exports with up to 24 reader and 8 writer threads mixing rdlock/tryrdlock/timedrdlock/wrlock/trywrlock/timedwrlock found no false EPERM, no leaked or negative reader count, no torn write and no hang; on main the same probe confirms the double unlock returns 0.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap. 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.

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:4810635624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2679). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T10:49:29Z. --- ### What `scePthreadRwlockUnlock` (and `pthread_rwlock_unlock`) called `unlock_shared()` on the host `shared_timed_mutex` for any caller that was not the write owner. When the rwlock is not held at all, or is write-held by another thread, that is undefined behaviour, and the call returned 0. FreeBSD libthr (`_Tthr_rwlock_unlock` in `lib/libthr/thread/thr_rwlock.c`, `do_rw_unlock` in `sys/kern/kern_umtx.c`) returns `EPERM` 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". `PthreadRwlockPrivate` now 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 that returns `EPERM` at 0. Like libthr, the count is per lock, not per thread, so a thread that holds no read lock can still unlock while another thread holds one; what this catches is unlocking a lock that nobody holds and unlocking a write lock owned by another thread. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - `guest_pthread_rwlock`: new cases unlock of a never-locked rwlock, double unlock after `tryrdlock`, unlock after `trywrlock`, unlock from the main thread while another thread holds the write lock, and that timed-out reads leave no count; all expect `EPERM`. The test fails without the change (exit `0xc0000409`) and passes with it. - Outside the repo: a 5-15 s stress probe of the guest exports with up to 24 reader and 8 writer threads mixing `rdlock`/`tryrdlock`/`timedrdlock`/`wrlock`/`trywrlock`/`timedwrlock` found no false `EPERM`, no leaked or negative reader count, no torn write and no hang; on `main` the same probe confirms the double unlock returns 0. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. 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. ### 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 10:50:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T10:50:08Z.


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:6096724345 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2679#issuecomment-6096724345). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T10:50:08Z. --- 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!2679
No description provided.