fix(libkernel): return errors for waits with an unowned mutex #3011

Merged
MarcZX10 merged 2 commits from upstream-pr/3011 into main 2026-10-10 23:59:13 +00:00
MarcZX10 commented 2026-10-10 20:27:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T20:27:40Z.


What

pthread_cond_wait and pthread_cond_timedwait currently throw or can dereference an invalid static initializer when the supplied mutex is not owned by the calling thread. FreeBSD libthr's cond_wait_common validates ownership before waiting: an unowned mutex returns EPERM, and its destroyed marker returns EINVAL. The adaptive static-initializer value 1 is not the destroyed marker, so it also returns EPERM; the regression test pins that exact case.

This change validates ownership before the wait mutates mutex state and adds coverage for untimed and timed waits, including unlocked, uninitialized, adaptively initialized, destroyed, and other-thread-owned mutexes. The implementation and tests are unchanged by the current-main rebases.

Carries Hussein Kadhim's tested fix from issue #2988, preserving the original commit author.

Tested

  • Linux x86_64, GCC 12.2, Release: built guest_cond_unowned_mutex_tests; guest_cond_unowned_mutex passed on the original implementation head.
  • Current head is rebased on main at e4045bbb. A fresh configure could not run because third-party submodule checkouts are absent in this worktree.
  • python3 tools/check_conventions.py --base upstream/main and git diff upstream/main...HEAD --check passed on the current head.
  • Windows 11 stress validation is reported by the source contributor in issue #2988; it was not rerun here.

Checklist

  • Based on current main; no open PR implements the same behavior
  • One topic per PR; follow-ups go in a new PR
  • No source comments or unrelated files added
  • Generic behavior, not title-specific
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4814215922 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3011). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T20:27:40Z. --- ### What `pthread_cond_wait` and `pthread_cond_timedwait` currently throw or can dereference an invalid static initializer when the supplied mutex is not owned by the calling thread. FreeBSD libthr's [`cond_wait_common`](https://github.com/freebsd/freebsd-src/blob/main/lib/libthr/thread/thr_cond.c#L1992-L2029) validates ownership before waiting: an unowned mutex returns `EPERM`, and its destroyed marker returns `EINVAL`. The adaptive static-initializer value 1 is not the destroyed marker, so it also returns `EPERM`; the regression test pins that exact case. This change validates ownership before the wait mutates mutex state and adds coverage for untimed and timed waits, including unlocked, uninitialized, adaptively initialized, destroyed, and other-thread-owned mutexes. The implementation and tests are unchanged by the current-main rebases. Carries Hussein Kadhim's tested fix from [issue #2988](https://github.com/boykopovar/AnyPS5/issues/2988), preserving the original commit author. ### Tested - Linux x86_64, GCC 12.2, Release: built `guest_cond_unowned_mutex_tests`; `guest_cond_unowned_mutex` passed on the original implementation head. - Current head is rebased on `main` at `e4045bbb`. A fresh configure could not run because third-party submodule checkouts are absent in this worktree. - `python3 tools/check_conventions.py --base upstream/main` and `git diff upstream/main...HEAD --check` passed on the current head. - Windows 11 stress validation is reported by the source contributor in issue #2988; it was not rerun here. ### Checklist - [x] Based on current `main`; no open PR implements the same behavior - [x] One topic per PR; follow-ups go in a new PR - [x] No source comments or unrelated files added - [x] Generic behavior, not title-specific - [x] Depends on: none - [x] AI-assisted: yes
Dextroid17 commented 2026-10-10 22:23:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-10T22:23:03Z.

Review: COMMENTED


The behaviour this pins down is worth having, and it is carried with clear attribution.

The EPERM/EINVAL split is the part I would record in the change itself. FreeBSD distinguishes an unowned mutex from a destroyed one, and that distinction is the entire content of the fix. The description says where the behaviour comes from; the code and the test do not. A line naming the reference — as the other POSIX likenesses in this tree do — would let the next reader check the split instead of trusting it.

And the static-initializer case is the most interesting line in the change — it is also the one the test already spells out, which is worth saying plainly. An untouched PTHREAD_MUTEX_INITIALIZER-shaped value is neither "destroyed" nor obviously "owned", and the test constructs exactly that value (reinterpret_cast<PthreadMutex>(1)) and requires EPERM from the wait, because the calling thread never locked it. Recording that expectation in the change itself, beside the EPERM/EINVAL split, would spare the next reader deriving it.

The ordering claim is the right shape: validating ownership before mutating either the mutex or the condition-variable state is exactly what a thrown exception could not do, and it is why the explicit error codes are the fix rather than a side effect.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481017227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3011#pullrequestreview-5481017227). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-10T22:23:03Z. **Review:** COMMENTED --- The behaviour this pins down is worth having, and it is carried with clear attribution. **The EPERM/EINVAL split is the part I would record in the change itself.** FreeBSD distinguishes an unowned mutex from a destroyed one, and that distinction is the entire content of the fix. The description says where the behaviour comes from; the code and the test do not. A line naming the reference — as the other POSIX likenesses in this tree do — would let the next reader check the split instead of trusting it. **And the static-initializer case is the most interesting line in the change** — it is also the one the test already spells out, which is worth saying plainly. An untouched `PTHREAD_MUTEX_INITIALIZER`-shaped value is neither "destroyed" nor obviously "owned", and the test constructs exactly that value (`reinterpret_cast<PthreadMutex>(1)`) and requires `EPERM` from the wait, because the calling thread never locked it. Recording that expectation in the change itself, beside the EPERM/EINVAL split, would spare the next reader deriving it. The ordering claim is the right shape: validating ownership before mutating either the mutex or the condition-variable state is exactly what a thrown exception could not do, and it is why the explicit error codes are the fix rather than a side effect. Written with AI assistance.
MarcZX10 commented 2026-10-10 22:58:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:58:18Z.


Thanks for calling out both the error distinction and the static initializer. I recorded them in the existing condition-wait entry in TechnicalDebt.md and linked the FreeBSD implementation:

  • cond_wait_common validates mutex ownership before waiting.
  • _mutex_owned returns EPERM for an unowned slot and EINVAL for the destroyed marker. The adaptive initializer is value 1, so it follows the EPERM path; marker 2 is the destroyed case.
  • The test exercises both values, along with an uninitialized mutex and one owned by another thread, through timed and untimed sce and POSIX waits.

The note now says these FreeBSD-derived results are still unverified on console hardware. I rebased the PR onto current main; Hussein Kadhim's implementation commit remains the original authored commit. The prior Linux Release focused test passed on the original head. I could not repeat the build after rebasing because the current worktree's third-party submodule checkouts are absent; the current-head conventions and diff checks pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103088319 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3011#issuecomment-6103088319). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T22:58:18Z. --- Thanks for calling out both the error distinction and the static initializer. I recorded them in the existing condition-wait entry in `TechnicalDebt.md` and linked the FreeBSD implementation: - [`cond_wait_common`](https://github.com/freebsd/freebsd-src/blob/main/lib/libthr/thread/thr_cond.c#L1992-L2029) validates mutex ownership before waiting. - [`_mutex_owned`](https://github.com/freebsd/freebsd-src/blob/main/lib/libthr/thread/thr_mutex.c#L1166-L1178) returns `EPERM` for an unowned slot and `EINVAL` for the destroyed marker. The adaptive initializer is value `1`, so it follows the `EPERM` path; marker `2` is the destroyed case. - The test exercises both values, along with an uninitialized mutex and one owned by another thread, through timed and untimed sce and POSIX waits. The note now says these FreeBSD-derived results are still unverified on console hardware. I rebased the PR onto current `main`; Hussein Kadhim's implementation commit remains the original authored commit. The prior Linux Release focused test passed on the original head. I could not repeat the build after rebasing because the current worktree's third-party submodule checkouts are absent; the current-head conventions and diff checks pass.
adriadam10 commented 2026-10-10 23:56:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:56:03Z.


Rebased on current main (only a TechnicalDebt.md adjacency conflict with the new scePthreadRwlockDestroy line; both kept).

Checked: mutexOwnership matches FreeBSD _mutex_owned (NULL and the adaptive initializer 1 give EPERM, the destroyed marker 2 gives EINVAL); guest_cond_unowned_mutex passes with the change and fails without it; the other cond/mutex tests still pass. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103514572 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3011#issuecomment-6103514572). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:56:03Z. --- Rebased on current `main` (only a TechnicalDebt.md adjacency conflict with the new `scePthreadRwlockDestroy` line; both kept). Checked: `mutexOwnership` matches FreeBSD `_mutex_owned` (NULL and the adaptive initializer 1 give `EPERM`, the destroyed marker 2 gives `EINVAL`); `guest_cond_unowned_mutex` passes with the change and fails without it; the other cond/mutex tests still pass. Merging once CI is green.
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!3011
No description provided.