fix(libkernel): return ESRCH from join, detach and cancel of a released thread handle #10817

Merged
MarcZX10 merged 9 commits from upstream-pr/10817 into main 2026-11-02 10:24:37 +00:00
MarcZX10 commented 2026-10-25 02:48:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:48:20Z.


What

scePthreadJoin (and the timed join that shares its code), scePthreadDetach and scePthreadCancel dereferenced the Pthread handle before checking that the thread object still exists. A detached thread frees its PthreadPrivate when it ends, so a join, detach or cancel on that handle could read freed memory. The original guest_pthread_released_handle test reproduced a heap-use-after-free in JoinThread_nid_no_patch after the object was freed, and returned EINVAL during a destruction race instead of ESRCH.

Each operation now checks the guest thread registry while the object is protected. Join, detach and cancel retain a temporary reference while they release the registry lock and continue using the object; join releases it before its blocking wait. The short getters and setters keep the registry lock for the duration of their access. Calls on an already released handle return SCE_KERNEL_ERROR_ESRCH, consistent with a second join (#4481), scePthreadAttrGet (#7957) and the name, priority and affinity calls (#10816).

Joined threads that are still retained and detached threads that have not ended behave as before.

Tested

Before this follow-up, the author reported a Linux x86-64 AddressSanitizer + UBSan build (ANYPS5_ENABLE_ASAN=ON with -fsanitize=undefined, RelWithDebInfo), with the host CPU gate bypassed locally for the run only. The author reported guest_pthread_released_handle passing in five runs and the guest_pthread_*, guest_cond_*, guest_rwlock_*, guest_signal*, guest_semaphore and guest_kernel_sys* tests giving the same results as before. The author reported guest_pthread_stack aborting on main under ASan because its 4 KiB frame-distance assertion is exceeded by ASan frames; guest_semaphore_trace was not built. No Windows, ThreadSanitizer or console hardware run was reported.

For earlier follow-up work, local checks included GCC C++20 syntax-only compilation for Thread.cpp and GuestPthreadReleasedHandle.cpp, MinGW C++20 syntax-only compilation for GuestPthreadReleasedHandle.cpp, and git diff --check. The installed MinGW headers do not provide PROCESSOR_RELATIONSHIP::EfficiencyClass or SetThreadDescription, which existing Thread.cpp code uses. Full CMake configuration could not proceed because SDL2 and required host development packages were unavailable. The pthread runtime tests were not run locally for that follow-up.

Rebase validation (2026-10-25)

This PR's four commits are now stacked on #10810 head b79959c5e3; #10810 is stacked on #10816 head 8eaa2bbad5, and that stack is based on upstream main a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762. The rebase completed without conflicts. I retained the test registrations from all three PRs.

After updating the complete stack to the latest upstream main, strict GCC C++20 syntax-only checks with -Wall -Wextra -Werror passed for Thread.cpp (suppressing its existing missing-field-initializer warning) and GuestPthreadFinalReleaseRace.cpp. git diff --check upstream/main...HEAD and python3 tools/check_conventions.py --base upstream/main --head HEAD also passed. The runtime/ASan test was not rerun, and full local CMake configuration remains unavailable because the SDL2 submodule/development dependencies are missing. No post-rebase runtime result is claimed.

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 under Silent stubs in their library's technical debt file (none added)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: #10810
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5001260630 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10817). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:48:20Z. --- ### What `scePthreadJoin` (and the timed join that shares its code), `scePthreadDetach` and `scePthreadCancel` dereferenced the `Pthread` handle before checking that the thread object still exists. A detached thread frees its `PthreadPrivate` when it ends, so a join, detach or cancel on that handle could read freed memory. The original `guest_pthread_released_handle` test reproduced a heap-use-after-free in `JoinThread_nid_no_patch` after the object was freed, and returned EINVAL during a destruction race instead of ESRCH. Each operation now checks the guest thread registry while the object is protected. Join, detach and cancel retain a temporary reference while they release the registry lock and continue using the object; join releases it before its blocking wait. The short getters and setters keep the registry lock for the duration of their access. Calls on an already released handle return `SCE_KERNEL_ERROR_ESRCH`, consistent with a second join (#4481), `scePthreadAttrGet` (#7957) and the name, priority and affinity calls (#10816). Joined threads that are still retained and detached threads that have not ended behave as before. ### Tested Before this follow-up, the author reported a Linux x86-64 AddressSanitizer + UBSan build (`ANYPS5_ENABLE_ASAN=ON` with `-fsanitize=undefined`, RelWithDebInfo), with the host CPU gate bypassed locally for the run only. The author reported `guest_pthread_released_handle` passing in five runs and the `guest_pthread_*`, `guest_cond_*`, `guest_rwlock_*`, `guest_signal*`, `guest_semaphore` and `guest_kernel_sys*` tests giving the same results as before. The author reported `guest_pthread_stack` aborting on `main` under ASan because its 4 KiB frame-distance assertion is exceeded by ASan frames; `guest_semaphore_trace` was not built. No Windows, ThreadSanitizer or console hardware run was reported. For earlier follow-up work, local checks included GCC C++20 syntax-only compilation for `Thread.cpp` and `GuestPthreadReleasedHandle.cpp`, MinGW C++20 syntax-only compilation for `GuestPthreadReleasedHandle.cpp`, and `git diff --check`. The installed MinGW headers do not provide `PROCESSOR_RELATIONSHIP::EfficiencyClass` or `SetThreadDescription`, which existing `Thread.cpp` code uses. Full CMake configuration could not proceed because SDL2 and required host development packages were unavailable. The pthread runtime tests were not run locally for that follow-up. ### Rebase validation (2026-10-25) This PR's four commits are now stacked on #10810 head `b79959c5e3`; #10810 is stacked on #10816 head `8eaa2bbad5`, and that stack is based on upstream `main` `a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762`. The rebase completed without conflicts. I retained the test registrations from all three PRs. After updating the complete stack to the latest upstream `main`, strict GCC C++20 syntax-only checks with `-Wall -Wextra -Werror` passed for `Thread.cpp` (suppressing its existing missing-field-initializer warning) and `GuestPthreadFinalReleaseRace.cpp`. `git diff --check upstream/main...HEAD` and `python3 tools/check_conventions.py --base upstream/main --head HEAD` also passed. The runtime/ASan test was not rerun, and full local CMake configuration remains unavailable because the SDL2 submodule/development dependencies are missing. No post-rebase runtime result is claimed. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [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 under Silent stubs in their library's technical debt file (none added) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: #10810 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!10817
No description provided.