fix(libkernel): return EINVAL for pthread mutex and cond operations on a destroyed object #4971
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4971"
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 xXPBSXx on 2026-10-13T06:52:54Z.
What
Locking, trylocking and unlocking a destroyed mutex, and signalling, broadcasting, waiting on or destroying a destroyed condition variable threw a C++ exception into the guest (
Mutex has been destroyed,Condition variable has been destroyed,Condition variable has already been destroyed), which terminates the process. FreeBSD 11.4 libthr returnsEINVALin every one of these cases:thr_mutex.c:check_and_init_mutexreturnsEINVALwhen*m == THR_MUTEX_DESTROYED(lock, timedlock, trylock);mutex_unlock_commonreturnsEINVALfor it;_pthread_mutex_destroyreturnsEINVALfor an already destroyed mutex.thr_cond.c:CHECK_AND_INIT_CONDreturnsEINVALforTHR_COND_DESTROYED(signal, broadcast, wait);_pthread_cond_destroysetsEINVALfor an already destroyed condition variable.The change makes
resolveMutexandresolveCondreport the destroyed state and the callers returnSCE_KERNEL_ERROR_EINVAL(the POSIX wrappers map it toEINVAL). The mutex destroy path and the condition wait path (mutexOwnership) already returnedEINVALfor a destroyed mutex; this makes the rest consistent. Thesce*entry points share these functions, so they change too; I did not check what a PS5 returns there, the choice follows the shared libthr behavior. This includesscePthreadCondSignalto: it now returnsSCE_KERNEL_ERROR_EINVALfor a destroyed condition variable instead of throwing; for a live one it still wakes all waiters (NotifyAll) and still ignores its thread argument, as before. This follows from the code (resolveCondreturns null for a destroyed condition variable); the new test does not callscePthreadCondSignalto.Left as is: NULL pointer arguments still throw (FreeBSD would fault there), as does the use of an uninitialized condition attribute in
scePthreadCondInit.Overlap: #5429 (wake guest mutex waiters on release) is merged (main
d1670009f). It rewrote the same lock call sites inMutex.cpp; the branch now contains a merge commit ofmain(9284312ac) that resolves the conflict there: the three lock paths (pthread_mutex_timedlock'sMutexOperations::Timedlock,scePthreadMutexLock,scePthreadMutexTimedlock) keepacquireResolved(a destroyed mutex answersEINVAL) and hand the resolved mutex to #5429's parking calls (TimedWait::ParkAcquire,TimedWait::AcquireUntil).Cond.cppand the libkernelCMakeLists.txtmerged without a conflict.Tested
9284312ac(merge ofmaind1670009finto the previous head6ddb7e5c5), Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,-j4: the 27 test executables of the targetsguest_cond_*,guest_mutex_*,guest_posix_sem,guest_pthread_*andguest_semaphore(built with the four files of this PR on top ofmaince5c83953) all exit with code 0, read directly from the executables:guest_pthread_destroyed_objects_tests0,guest_mutex_raise_exception_tests0,guest_mutex_contention_tests0,guest_pthread_mutex_errors_tests0,guest_cond_timedwait_tests0 and the other 22 likewise.6ddb7e5c5(based onea2bbefb2):guest_pthread_destroyed_objectsaborted onmain(uncaughtstd::runtime_error: Mutex has been destroyed) and passed with the change. That without-fix run was not repeated once5c83953.python tools/check_conventions.py --base upstream/mainpasses on the merge head.release/11.4.0on GitHub.Checklist
main(ce5c83953) without conflict (the branch contains a merge ofmain; the conflict with the merged #5429 is resolved, see Overlap); #4953 touches condition clocks onlyNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T23:32:39Z.
#5429 is in and changes the same lock sites in
Mutex.cpp, so this conflicts withmainnow; please rebase.Imported from boykopovar/AnyPS5. Originally posted by Dripl1x on 2026-10-14T00:11:03Z.
@xXPBSXx #5429 was merged before this one, so the order I proposed there is reversed and the rebase falls to this PR; sorry for the extra step. The conflict is in
Pthread/src/Mutex.cpponly, at the three lock call sites:mainnow resolves the mutex intocurrentfirst and passescurrenttoTimedWait::ParkAcquireandTimedWait::AcquireUntilas the parking key, andscePthreadMutexUnlockcallsTimedWait::Unpark(current). Keeping yourEINVALcheck ahead of the first use ofcurrentand both sides otherwise resolves it. If it helps, I can post the resolved hunks here.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T00:44:16Z.
Thanks for the notes on the conflict with #5429. The branch now contains a merge commit of
main(9284312ac, mergingd1670009finto the previous head6ddb7e5c5) that resolves it inMutex.cpp: the three lock paths keepacquireResolved(so a destroyed mutex still returnsEINVAL) and pass the resolved mutex to the#5429parking calls. The PR is mergeable again (git merge-treeagainstmainfeddf7f4cis clean). On Windows I built and ranguest_pthread_destroyed_objects_tests,guest_mutex_raise_exception_testsandguest_mutex_contention_testson the merge head, all exit code 0; the fullpthread|mutex|cond|sem|rwlock|barriergroup has not been run on this head yet. I used a merge commit instead of a rebase so that the branch history does not change under the reviews.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T04:41:50Z.
Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-15T04:24:43Z.
@xXPBSXx a title that reaches this: Ship of Harkinian (PPSA99320, catalog release). It needs #6217 to get past
malloc_stats_fastfirst. Linux (Ubuntu 24.04, GCC 13.3, Release, RTX 4070 Ti),maina077a88df.With no ROM installed, the title shows its "Game files needed" dialog. Pressing Cross on its OK button (pad input 8 s in) quits the title:
main+ #6217: the quit stops on the exit path. A run that is ended by its 75 s timeout stops the same way.main+ #6217 + this PR's head: the same quit exits with code 0.So per @oneandonlydean's note, this may be worth reopening with PPSA99320 as its title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-15T04:45:49Z.
Reopening with a title and its log line, as asked in the closing note.
Title: Ship of Harkinian (PPSA99320), reported by @milescoviello above (Linux,
maina077a88df+ #6217): the title's quit path stops withwhat(): Mutex has been destroyed; with this head the same quit exits with code 0. I have not run that title myself.Windows check of this head on current
main: head9284312acmerged intomaind92a8dad390af1807cc91622b924252141816a5a(clean merge, treedb30968fb).guest_pthread_destroyed_objects(Windows 10, MinGW-w64 GCC 15.2.0, Release, ctest) passes with the change, ctest exit 0; without it (Cond.cppandMutex.cpptaken frommain, the new test kept, libkernel rebuilt) it fails with test exit code0xc0000409andwhat(): Mutex has been destroyedin the test output, ctest exit 8. One machine, only this test and not the whole suite.python tools/check_conventions.py --base upstream/mainpasses for this head.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-15T04:46:09Z.
Request to reopen, with a title and its log line as asked in the closing note (GitHub does not let me reopen it myself).
Title: Ship of Harkinian (PPSA99320), reported by @milescoviello above (Linux,
maina077a88df+ #6217): the title's quit path stops withwhat(): Mutex has been destroyed; with this head the same quit exits with code 0. I have not run that title myself.Windows check of this head on current
main: head9284312acmerged intomaind92a8dad390af1807cc91622b924252141816a5a(clean merge, treedb30968fb).guest_pthread_destroyed_objects(Windows 10, MinGW-w64 GCC 15.2.0, Release, ctest) passes with the change, ctest exit 0; without it (Cond.cppandMutex.cpptaken frommain, the new test kept, libkernel rebuilt) it fails with test exit code0xc0000409andwhat(): Mutex has been destroyedin the test output, ctest exit 8. One machine, only this test and not the whole suite.python tools/check_conventions.py --base upstream/mainpasses for this head.AI-assisted (Claude Code).
Pull request closed