fix(libkernel): return EINVAL for pthread mutex and cond operations on a destroyed object #4971

Closed
xXPBSXx wants to merge 2 commits from upstream-pr/4971 into main
xXPBSXx commented 2026-10-13 06:52:54 +00:00 (Migrated from github.com)

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 returns EINVAL in every one of these cases:

  • thr_mutex.c: check_and_init_mutex returns EINVAL when *m == THR_MUTEX_DESTROYED (lock, timedlock, trylock); mutex_unlock_common returns EINVAL for it; _pthread_mutex_destroy returns EINVAL for an already destroyed mutex.
  • thr_cond.c: CHECK_AND_INIT_COND returns EINVAL for THR_COND_DESTROYED (signal, broadcast, wait); _pthread_cond_destroy sets EINVAL for an already destroyed condition variable.

The change makes resolveMutex and resolveCond report the destroyed state and the callers return SCE_KERNEL_ERROR_EINVAL (the POSIX wrappers map it to EINVAL). The mutex destroy path and the condition wait path (mutexOwnership) already returned EINVAL for a destroyed mutex; this makes the rest consistent. The sce* 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 includes scePthreadCondSignalto: it now returns SCE_KERNEL_ERROR_EINVAL for 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 (resolveCond returns null for a destroyed condition variable); the new test does not call scePthreadCondSignalto.

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 in Mutex.cpp; the branch now contains a merge commit of main (9284312ac) that resolves the conflict there: the three lock paths (pthread_mutex_timedlock's MutexOperations::Timedlock, scePthreadMutexLock, scePthreadMutexTimedlock) keep acquireResolved (a destroyed mutex answers EINVAL) and hand the resolved mutex to #5429's parking calls (TimedWait::ParkAcquire, TimedWait::AcquireUntil). Cond.cpp and the libkernel CMakeLists.txt merged without a conflict.

Tested

  • On the merge head 9284312ac (merge of main d1670009f into the previous head 6ddb7e5c5), Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -j4: the 27 test executables of the targets guest_cond_*, guest_mutex_*, guest_posix_sem, guest_pthread_* and guest_semaphore (built with the four files of this PR on top of main ce5c83953) all exit with code 0, read directly from the executables: guest_pthread_destroyed_objects_tests 0, guest_mutex_raise_exception_tests 0, guest_mutex_contention_tests 0, guest_pthread_mutex_errors_tests 0, guest_cond_timedwait_tests 0 and the other 22 likewise.
  • Earlier, on the previous head 6ddb7e5c5 (based on ea2bbefb2): guest_pthread_destroyed_objects aborted on main (uncaught std::runtime_error: Mutex has been destroyed) and passed with the change. That without-fix run was not repeated on ce5c83953.
  • python tools/check_conventions.py --base upstream/main passes on the merge head.
  • Not tested: other ctest tests, Linux, a console. The expected values come from the FreeBSD 11.4 sources above, read from release/11.4.0 on GitHub.

Checklist

  • Merges onto current main (ce5c83953) without conflict (the branch contains a merge of main; the conflict with the merged #5429 is resolved, see Overlap); #4953 touches condition clocks only
  • 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 (n/a, none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes (Claude Code)

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839529244 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971). Originally posted by [xXPBSXx](https://github.com/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 returns `EINVAL` in every one of these cases: - `thr_mutex.c`: `check_and_init_mutex` returns `EINVAL` when `*m == THR_MUTEX_DESTROYED` (lock, timedlock, trylock); `mutex_unlock_common` returns `EINVAL` for it; `_pthread_mutex_destroy` returns `EINVAL` for an already destroyed mutex. - `thr_cond.c`: `CHECK_AND_INIT_COND` returns `EINVAL` for `THR_COND_DESTROYED` (signal, broadcast, wait); `_pthread_cond_destroy` sets `EINVAL` for an already destroyed condition variable. The change makes `resolveMutex` and `resolveCond` report the destroyed state and the callers return `SCE_KERNEL_ERROR_EINVAL` (the POSIX wrappers map it to `EINVAL`). The mutex destroy path and the condition wait path (`mutexOwnership`) already returned `EINVAL` for a destroyed mutex; this makes the rest consistent. The `sce*` 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 includes `scePthreadCondSignalto`: it now returns `SCE_KERNEL_ERROR_EINVAL` for 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 (`resolveCond` returns null for a destroyed condition variable); the new test does not call `scePthreadCondSignalto`. 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 in `Mutex.cpp`; the branch now contains a merge commit of `main` (`9284312ac`) that resolves the conflict there: the three lock paths (`pthread_mutex_timedlock`'s `MutexOperations::Timedlock`, `scePthreadMutexLock`, `scePthreadMutexTimedlock`) keep `acquireResolved` (a destroyed mutex answers `EINVAL`) and hand the resolved mutex to #5429's parking calls (`TimedWait::ParkAcquire`, `TimedWait::AcquireUntil`). `Cond.cpp` and the libkernel `CMakeLists.txt` merged without a conflict. ### Tested - On the merge head `9284312ac` (merge of `main` `d1670009f` into the previous head `6ddb7e5c5`), Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `-j4`: the 27 test executables of the targets `guest_cond_*`, `guest_mutex_*`, `guest_posix_sem`, `guest_pthread_*` and `guest_semaphore` (built with the four files of this PR on top of `main` `ce5c83953`) all exit with code 0, read directly from the executables: `guest_pthread_destroyed_objects_tests` 0, `guest_mutex_raise_exception_tests` 0, `guest_mutex_contention_tests` 0, `guest_pthread_mutex_errors_tests` 0, `guest_cond_timedwait_tests` 0 and the other 22 likewise. - Earlier, on the previous head `6ddb7e5c5` (based on `ea2bbefb2`): `guest_pthread_destroyed_objects` aborted on `main` (uncaught `std::runtime_error: Mutex has been destroyed`) and passed with the change. That without-fix run was not repeated on `ce5c83953`. - `python tools/check_conventions.py --base upstream/main` passes on the merge head. - Not tested: other ctest tests, Linux, a console. The expected values come from the FreeBSD 11.4 sources above, read from `release/11.4.0` on GitHub. ### Checklist - [x] Merges onto current `main` (`ce5c83953`) without conflict (the branch contains a merge of `main`; the conflict with the merged #5429 is resolved, see Overlap); #4953 touches condition clocks only - [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 (n/a, none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes (Claude Code) AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-13 23:32:39 +00:00 (Migrated from github.com)

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 with main now; please rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157426468 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6157426468). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T23:32:39Z. --- #5429 is in and changes the same lock sites in `Mutex.cpp`, so this conflicts with `main` now; please rebase.
Dripl1x commented 2026-10-14 00:11:03 +00:00 (Migrated from github.com)

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.cpp only, at the three lock call sites: main now resolves the mutex into current first and passes current to TimedWait::ParkAcquire and TimedWait::AcquireUntil as the parking key, and scePthreadMutexUnlock calls TimedWait::Unpark(current). Keeping your EINVAL check ahead of the first use of current and both sides otherwise resolves it. If it helps, I can post the resolved hunks here.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157899830 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6157899830). Originally posted by [Dripl1x](https://github.com/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.cpp` only, at the three lock call sites: `main` now resolves the mutex into `current` first and passes `current` to `TimedWait::ParkAcquire` and `TimedWait::AcquireUntil` as the parking key, and `scePthreadMutexUnlock` calls `TimedWait::Unpark(current)`. Keeping your `EINVAL` check ahead of the first use of `current` and both sides otherwise resolves it. If it helps, I can post the resolved hunks here. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-14 00:44:16 +00:00 (Migrated from github.com)

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, merging d1670009f into the previous head 6ddb7e5c5) that resolves it in Mutex.cpp: the three lock paths keep acquireResolved (so a destroyed mutex still returns EINVAL) and pass the resolved mutex to the #5429 parking calls. The PR is mergeable again (git merge-tree against main feddf7f4c is clean). On Windows I built and ran guest_pthread_destroyed_objects_tests, guest_mutex_raise_exception_tests and guest_mutex_contention_tests on the merge head, all exit code 0; the full pthread|mutex|cond|sem|rwlock|barrier group 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158304160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6158304160). Originally posted by [xXPBSXx](https://github.com/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`, merging `d1670009f` into the previous head `6ddb7e5c5`) that resolves it in `Mutex.cpp`: the three lock paths keep `acquireResolved` (so a destroyed mutex still returns `EINVAL`) and pass the resolved mutex to the `#5429` parking calls. The PR is mergeable again (`git merge-tree` against `main` `feddf7f4c` is clean). On Windows I built and ran `guest_pthread_destroyed_objects_tests`, `guest_mutex_raise_exception_tests` and `guest_mutex_contention_tests` on the merge head, all exit code 0; the full `pthread|mutex|cond|sem|rwlock|barrier` group 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).
oneandonlydean commented 2026-10-14 04:41:50 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161054391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6161054391). Originally posted by [oneandonlydean](https://github.com/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.
milescoviello commented 2026-10-15 04:24:43 +00:00 (Migrated from github.com)

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_fast first. Linux (Ubuntu 24.04, GCC 13.3, Release, RTX 4070 Ti), main a077a88df.

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.
    [libc] unhandled exception 'St13runtime_error' thrown from ...
    terminate called after throwing an instance of 'std::runtime_error'
      what():  Mutex has been destroyed
    
    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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6184377940 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6184377940). Originally posted by [milescoviello](https://github.com/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_fast` first. Linux (Ubuntu 24.04, GCC 13.3, Release, RTX 4070 Ti), `main` a077a88df. 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. ``` [libc] unhandled exception 'St13runtime_error' thrown from ... terminate called after throwing an instance of 'std::runtime_error' what(): Mutex has been destroyed ``` 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
xXPBSXx commented 2026-10-15 04:45:49 +00:00 (Migrated from github.com)

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, main a077a88df + #6217): the title's quit path stops with what(): 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: head 9284312ac merged into main d92a8dad390af1807cc91622b924252141816a5a (clean merge, tree db30968fb). guest_pthread_destroyed_objects (Windows 10, MinGW-w64 GCC 15.2.0, Release, ctest) passes with the change, ctest exit 0; without it (Cond.cpp and Mutex.cpp taken from main, the new test kept, libkernel rebuilt) it fails with test exit code 0xc0000409 and what(): Mutex has been destroyed in the test output, ctest exit 8. One machine, only this test and not the whole suite. python tools/check_conventions.py --base upstream/main passes for this head.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6184622823 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6184622823). Originally posted by [xXPBSXx](https://github.com/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, `main` `a077a88df` + #6217): the title's quit path stops with `what(): 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`: head `9284312ac` merged into `main` `d92a8dad390af1807cc91622b924252141816a5a` (clean merge, tree `db30968fb`). `guest_pthread_destroyed_objects` (Windows 10, MinGW-w64 GCC 15.2.0, Release, ctest) passes with the change, ctest exit 0; without it (`Cond.cpp` and `Mutex.cpp` taken from `main`, the new test kept, libkernel rebuilt) it fails with test exit code `0xc0000409` and `what(): Mutex has been destroyed` in the test output, ctest exit 8. One machine, only this test and not the whole suite. `python tools/check_conventions.py --base upstream/main` passes for this head. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-15 04:46:09 +00:00 (Migrated from github.com)

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, main a077a88df + #6217): the title's quit path stops with what(): 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: head 9284312ac merged into main d92a8dad390af1807cc91622b924252141816a5a (clean merge, tree db30968fb). guest_pthread_destroyed_objects (Windows 10, MinGW-w64 GCC 15.2.0, Release, ctest) passes with the change, ctest exit 0; without it (Cond.cpp and Mutex.cpp taken from main, the new test kept, libkernel rebuilt) it fails with test exit code 0xc0000409 and what(): Mutex has been destroyed in the test output, ctest exit 8. One machine, only this test and not the whole suite. python tools/check_conventions.py --base upstream/main passes for this head.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6184626722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4971#issuecomment-6184626722). Originally posted by [xXPBSXx](https://github.com/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, `main` `a077a88df` + #6217): the title's quit path stops with `what(): 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`: head `9284312ac` merged into `main` `d92a8dad390af1807cc91622b924252141816a5a` (clean merge, tree `db30968fb`). `guest_pthread_destroyed_objects` (Windows 10, MinGW-w64 GCC 15.2.0, Release, ctest) passes with the change, ctest exit 0; without it (`Cond.cpp` and `Mutex.cpp` taken from `main`, the new test kept, libkernel rebuilt) it fails with test exit code `0xc0000409` and `what(): Mutex has been destroyed` in the test output, ctest exit 8. One machine, only this test and not the whole suite. `python tools/check_conventions.py --base upstream/main` passes for this head. AI-assisted (Claude Code).

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