fix(libkernel): read and write the detached state of a guest thread atomically #10810

Merged
MarcZX10 merged 5 commits from upstream-pr/10810 into main 2026-11-02 10:24:31 +00:00
MarcZX10 commented 2026-10-25 02:25:56 +00:00 (Migrated from github.com)

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


What

PthreadPrivate::_detached was a plain bool. scePthreadDetach writes it under the thread's join mutex, while scePthreadAttrGet, pthread_attr_get_np and the first check of the join path read it with no lock or under a different one. ThreadSanitizer reports this as a data race between scePthreadDetach and scePthreadAttrGet (and between detach and join).

The field is now std::atomic<bool>. All existing reads and writes compile unchanged and behave as before; no other change.

Tested

Linux x86-64, ThreadSanitizer build (-fsanitize=thread, RelWithDebInfo, dynamic libstdc++ because TSan cannot link statically). The host CPU gate was bypassed locally for the run only (not committed). No Windows, no ASan/UBSan run for this change, no console hardware.

  • The existing guest_pthread_stack test already reported the race at Attr.cpp:97 (read) against Thread.cpp:1108 (write) on current main.
  • New guest_pthread_detach_attr queries the attributes of a blocked thread from a second thread while the first detaches it, 50 rounds, and checks the attributes report the detached state afterwards. On current main under TSan it reports data race ... in scePthreadDetach; with the fix TSan reports nothing in three runs, and guest_pthread_stack no longer reports it. guest_pthread_join_detach passes.
  • The new test passes without a sanitizer both before and after the fix: the defect is only observable with a race detector, so it is a regression harness for TSan runs.

This PR is stacked on #10816. Rebase follow-up (2026-10-25): it now follows #10816 head 8eaa2bbad58d... on upstream main a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762, without conflicts. Both dependency test registrations remain in libkernel/CMakeLists.txt. Strict GCC C++20 syntax-only checks with -Wall -Wextra -Werror passed for Thread.cpp (suppressing its existing missing-field-initializer warning) and GuestPthreadDetachAttr.cpp. git diff --check upstream/main...HEAD and python3 tools/check_conventions.py --base upstream/main --head HEAD passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime or ThreadSanitizer result is claimed. The reported runtime evidence is from the prior base.

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: #10816
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Rebase validation (2026-10-25)

This PR is stacked on updated #10816 head 8eaa2bbad5; #10816 is based on upstream main a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762. The rebase completed without conflicts, and both dependency test registrations remain present. git diff --check upstream/main...HEAD and python3 tools/check_conventions.py --base upstream/main --head HEAD passed. Full local CMake configuration could not proceed because the SDL2 package is unavailable; no post-rebase runtime or ThreadSanitizer result is claimed.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5001129655 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10810). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:25:56Z. --- ### What `PthreadPrivate::_detached` was a plain `bool`. `scePthreadDetach` writes it under the thread's join mutex, while `scePthreadAttrGet`, `pthread_attr_get_np` and the first check of the join path read it with no lock or under a different one. ThreadSanitizer reports this as a data race between `scePthreadDetach` and `scePthreadAttrGet` (and between detach and join). The field is now `std::atomic<bool>`. All existing reads and writes compile unchanged and behave as before; no other change. ### Tested Linux x86-64, ThreadSanitizer build (`-fsanitize=thread`, RelWithDebInfo, dynamic libstdc++ because TSan cannot link statically). The host CPU gate was bypassed locally for the run only (not committed). No Windows, no ASan/UBSan run for this change, no console hardware. - The existing `guest_pthread_stack` test already reported the race at `Attr.cpp:97` (read) against `Thread.cpp:1108` (write) on current `main`. - New `guest_pthread_detach_attr` queries the attributes of a blocked thread from a second thread while the first detaches it, 50 rounds, and checks the attributes report the detached state afterwards. On current `main` under TSan it reports `data race ... in scePthreadDetach`; with the fix TSan reports nothing in three runs, and `guest_pthread_stack` no longer reports it. `guest_pthread_join_detach` passes. - The new test passes without a sanitizer both before and after the fix: the defect is only observable with a race detector, so it is a regression harness for TSan runs. This PR is stacked on #10816. Rebase follow-up (2026-10-25): it now follows #10816 head `8eaa2bbad58d...` on upstream `main` `a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762`, without conflicts. Both dependency test registrations remain in `libkernel/CMakeLists.txt`. Strict GCC C++20 syntax-only checks with `-Wall -Wextra -Werror` passed for `Thread.cpp` (suppressing its existing missing-field-initializer warning) and `GuestPthreadDetachAttr.cpp`. `git diff --check upstream/main...HEAD` and `python3 tools/check_conventions.py --base upstream/main --head HEAD` passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime or ThreadSanitizer result is claimed. The reported runtime evidence is from the prior base. ### 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: #10816 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes ### Rebase validation (2026-10-25) This PR is stacked on updated #10816 head `8eaa2bbad5`; #10816 is based on upstream `main` `a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762`. The rebase completed without conflicts, and both dependency test registrations remain present. `git diff --check upstream/main...HEAD` and `python3 tools/check_conventions.py --base upstream/main --head HEAD` passed. Full local CMake configuration could not proceed because the SDL2 package is unavailable; no post-rebase runtime or ThreadSanitizer result is claimed.
jotitadev commented 2026-10-27 05:37:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T05:37:47Z.

Review: COMMENTED


ThreadSanitizer run of head 256782621 (the #10816 commits plus the two #10810 commits) on bare-metal Linux.

Setup: Ubuntu 26.04 x86-64 (i7-8700, not a VM), GCC 15.2, -DANYPS5_ENABLE_TSAN=ON (the option from #11179), RelWithDebInfo, TSAN_OPTIONS=suppressions=tools/tsan-suppressions.txt. I did not bypass the host CPU gate: this CPU passes it. Base: main c3ee5a425; the head merges cleanly. I merged it together with #10100 and #11125 for the run. Those two touch other files and other reports.

Baseline (main c3ee5a425): guest_pthread_stack reports the race in 10 of 10 runs. The read is scePthreadAttrGet (Attr.cpp:97, from RetainedStackWorker). The previous write is scePthreadDetach (Thread.cpp:1137, from main in GuestPthreadStack.cpp:219).

With this PR:

  • guest_pthread_stack and the new guest_pthread_detach_attr, 10 repeated runs: 0 reports, both pass.
  • guest_pthread_handle_after_exit and guest_pthread_join_detach pass with no report.
  • Full non-GPU suite: no report from Pthread/ code and no new report in any test.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5662691663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10810#pullrequestreview-5662691663). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T05:37:47Z. **Review:** COMMENTED --- ThreadSanitizer run of head `256782621` (the #10816 commits plus the two #10810 commits) on bare-metal Linux. **Setup:** Ubuntu 26.04 x86-64 (i7-8700, not a VM), GCC 15.2, `-DANYPS5_ENABLE_TSAN=ON` (the option from #11179), RelWithDebInfo, `TSAN_OPTIONS=suppressions=tools/tsan-suppressions.txt`. I did not bypass the host CPU gate: this CPU passes it. Base: `main` `c3ee5a425`; the head merges cleanly. I merged it together with #10100 and #11125 for the run. Those two touch other files and other reports. **Baseline (`main` `c3ee5a425`):** `guest_pthread_stack` reports the race in 10 of 10 runs. The read is `scePthreadAttrGet` (Attr.cpp:97, from `RetainedStackWorker`). The previous write is `scePthreadDetach` (Thread.cpp:1137, from `main` in GuestPthreadStack.cpp:219). **With this PR:** - `guest_pthread_stack` and the new `guest_pthread_detach_attr`, 10 repeated runs: 0 reports, both pass. - `guest_pthread_handle_after_exit` and `guest_pthread_join_detach` pass with no report. - Full non-GPU suite: no report from `Pthread/` code and no new report in any test. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!10810
No description provided.