fix(libkernel): return EPERM when unlocking an rwlock that is not held #3615

Closed
karim200003 wants to merge 1 commit from upstream-pr/3615 into main
karim200003 commented 2026-10-11 14:15:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karim200003 on 2026-10-11T14:15:35Z.


scePthreadRwlockUnlock and pthread_rwlock_unlock treated every caller that is not the write owner as a reader: they decremented the reader count and called unlock_shared on the host shared_timed_mutex. On a lock nobody holds, after a double unlock, or while another thread holds the write lock, that is undefined behaviour on the host, the call returned 0 and the lock was damaged afterwards (the reader count went negative and destroy reported EBUSY for an idle lock).

FreeBSD libthr's _pthread_rwlock_unlock returns EPERM when the write owner is another thread, and do_rw_unlock in kern_umtx.c returns EPERM when there is no write owner and the reader count is zero. Unlock now does the same: a write lock owned by another thread is refused, and a read unlock takes one count with a compare-exchange that fails at zero, before unlock_shared. As in libthr the count is per lock, so a thread that holds no read lock can still release one held by another.

guest_pthread_rwlock covers a never-locked rwlock, double unlocks after a write and after reads, unlocking while another thread holds the write lock, and that timed-out read attempts leave no count; it fails without the change. An 8 s stress of 12 readers and 4 writers mixing the blocking, try and timed calls (22 million lock operations) saw no false EPERM and no torn write.

Fixes #3096

AI-assisted: yes

What

Tested

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on:
  • AI-assisted:
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819696826 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3615). Originally posted by [karim200003](https://github.com/karim200003) on 2026-10-11T14:15:35Z. --- scePthreadRwlockUnlock and pthread_rwlock_unlock treated every caller that is not the write owner as a reader: they decremented the reader count and called unlock_shared on the host shared_timed_mutex. On a lock nobody holds, after a double unlock, or while another thread holds the write lock, that is undefined behaviour on the host, the call returned 0 and the lock was damaged afterwards (the reader count went negative and destroy reported EBUSY for an idle lock). FreeBSD libthr's _pthread_rwlock_unlock returns EPERM when the write owner is another thread, and do_rw_unlock in kern_umtx.c returns EPERM when there is no write owner and the reader count is zero. Unlock now does the same: a write lock owned by another thread is refused, and a read unlock takes one count with a compare-exchange that fails at zero, before unlock_shared. As in libthr the count is per lock, so a thread that holds no read lock can still release one held by another. guest_pthread_rwlock covers a never-locked rwlock, double unlocks after a write and after reads, unlocking while another thread holds the write lock, and that timed-out read attempts leave no count; it fails without the change. An 8 s stress of 12 readers and 4 writers mixing the blocking, try and timed calls (22 million lock operations) saw no false EPERM and no torn write. Fixes #3096 AI-assisted: yes ### What ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. --> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [ ] 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [ ] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [ ] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [ ] New third-party code is a submodule built from source - [ ] Generic behaviour, not specific to one title - [ ] Depends on: <!-- #PR, or none --> - [ ] AI-assisted: <!-- yes / no -->
github-actions[bot] commented 2026-10-11 14:15:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T14:15:46Z.


Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109941106 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3615#issuecomment-6109941106). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T14:15:46Z. --- Thank you for your contribution! Authors with no pull requests merged into `main` can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

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!3615
No description provided.