fix(libkernel): return errors for waits with an unowned mutex #3011
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3011"
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 MarcZX10 on 2026-10-10T20:27:40Z.
What
pthread_cond_waitandpthread_cond_timedwaitcurrently throw or can dereference an invalid static initializer when the supplied mutex is not owned by the calling thread. FreeBSD libthr'scond_wait_commonvalidates ownership before waiting: an unowned mutex returnsEPERM, and its destroyed marker returnsEINVAL. The adaptive static-initializer value 1 is not the destroyed marker, so it also returnsEPERM; 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
guest_cond_unowned_mutex_tests;guest_cond_unowned_mutexpassed on the original implementation head.mainate4045bbb. A fresh configure could not run because third-party submodule checkouts are absent in this worktree.python3 tools/check_conventions.py --base upstream/mainandgit diff upstream/main...HEAD --checkpassed on the current head.Checklist
main; no open PR implements the same behaviorImported 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 requiresEPERMfrom 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.
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.mdand linked the FreeBSD implementation:cond_wait_commonvalidates mutex ownership before waiting._mutex_ownedreturnsEPERMfor an unowned slot andEINVALfor the destroyed marker. The adaptive initializer is value1, so it follows theEPERMpath; marker2is the destroyed case.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.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 newscePthreadRwlockDestroyline; both kept).Checked:
mutexOwnershipmatches FreeBSD_mutex_owned(NULL and the adaptive initializer 1 giveEPERM, the destroyed marker 2 givesEINVAL);guest_cond_unowned_mutexpasses with the change and fails without it; the other cond/mutex tests still pass. Merging once CI is green.