fix(libkernel): keep the thread stack maps alive through static destruction #9272

Merged
milescoviello merged 1 commit from upstream-pr/9272 into main 2026-10-23 00:05:32 +00:00
milescoviello commented 2026-10-21 17:07:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T17:07:03Z.


What

libkernel keeps three maps of thread stack ranges in Pthread/src/Thread.cpp: liveStacks, protectedStackPages and guestStackMappings. They were plain namespace-scope globals, so libkernel's static destructors free them at exit. A detached thread can still be running then, and it unregisters its stack (UnregisterStack) or looks one up (GuestThreadStack) in a map that has been destroyed.

ThreadSanitizer reports this in guest_pthread_join_detach. The main thread's static destruction of liveStacks (Thread.cpp:79) races with a detached thread's UnregisterStack (Thread.cpp:329, holding stackLock):

WARNING: ThreadSanitizer: data race
  Read of size 8 ... by main thread:
    ... std::_Rb_tree<PthreadPrivate*, ...>  (__static_initialization_and_destruction_0, Thread.cpp:79)
  Previous write of size 8 ... by thread T4 (mutexes: write M0):
    #0 std::_Rb_tree_header::_M_reset()
    ... UnregisterStack Thread.cpp:329
  Location is global 'liveStacks' of size 48

The three maps are now created once and never destroyed (static auto& liveStacks = *new std::map<...>()), the way Apr.cpp keeps its file tables. Their mutexes stay as they are, since std::mutex needs no destructor. Nothing else changes.

Overlap: #8658 (macOS host support) makes the same change to liveStacks and protectedStackPages as part of a much larger PR. This PR is that piece on its own, plus guestStackMappings, for the Linux exit race. Whichever lands second drops its copy.

Tested

Ubuntu 24.04, GCC 13.3, on main cecb540a63.

  • ThreadSanitizer build (RelWithDebInfo, -fsanitize=thread, run with ASLR off), guest_pthread_join_detach:
    • main: fails 3 of 3, each with the race on liveStacks above.
    • With this change: passes 3 of 3, with no ThreadSanitizer report.
  • Normal test build: the 38 tests matching ^guest_pthread|stack|^guest_thread pass 3 of 3.

It came from a ThreadSanitizer run of the CPU-side tests on main. Its other reports are in third-party code (FFmpeg, PulseAudio), in code xXPBSXx's #9147 and #9223 already fix, or not yet analysed.

  • Windows 11 Enterprise 26300, WinLibs GCC 15.2.0 r7 (UCRT), Release: guest_pthread_join_detach passes 3 of 3 on main and 3 of 3 with this change.

Not tested: macOS.

Checklist

  • Based on current main; no other open PR implements the same functions (#8658 includes the same change for two of the three maps; see above)
  • 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
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: none needed (libkernel only)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4955387540 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9272). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-21T17:07:03Z. --- ### What libkernel keeps three maps of thread stack ranges in `Pthread/src/Thread.cpp`: `liveStacks`, `protectedStackPages` and `guestStackMappings`. They were plain namespace-scope globals, so libkernel's static destructors free them at exit. A detached thread can still be running then, and it unregisters its stack (`UnregisterStack`) or looks one up (`GuestThreadStack`) in a map that has been destroyed. ThreadSanitizer reports this in `guest_pthread_join_detach`. The main thread's static destruction of `liveStacks` (`Thread.cpp:79`) races with a detached thread's `UnregisterStack` (`Thread.cpp:329`, holding `stackLock`): ``` WARNING: ThreadSanitizer: data race Read of size 8 ... by main thread: ... std::_Rb_tree<PthreadPrivate*, ...> (__static_initialization_and_destruction_0, Thread.cpp:79) Previous write of size 8 ... by thread T4 (mutexes: write M0): #0 std::_Rb_tree_header::_M_reset() ... UnregisterStack Thread.cpp:329 Location is global 'liveStacks' of size 48 ``` The three maps are now created once and never destroyed (`static auto& liveStacks = *new std::map<...>()`), the way `Apr.cpp` keeps its file tables. Their mutexes stay as they are, since `std::mutex` needs no destructor. Nothing else changes. **Overlap:** #8658 (macOS host support) makes the same change to `liveStacks` and `protectedStackPages` as part of a much larger PR. This PR is that piece on its own, plus `guestStackMappings`, for the Linux exit race. Whichever lands second drops its copy. ### Tested Ubuntu 24.04, GCC 13.3, on `main` cecb540a63. - ThreadSanitizer build (RelWithDebInfo, `-fsanitize=thread`, run with ASLR off), `guest_pthread_join_detach`: - `main`: fails 3 of 3, each with the race on `liveStacks` above. - With this change: passes 3 of 3, with no ThreadSanitizer report. - Normal test build: the 38 tests matching `^guest_pthread|stack|^guest_thread` pass 3 of 3. It came from a ThreadSanitizer run of the CPU-side tests on `main`. Its other reports are in third-party code (FFmpeg, PulseAudio), in code xXPBSXx's #9147 and #9223 already fix, or not yet analysed. - Windows 11 Enterprise 26300, WinLibs GCC 15.2.0 r7 (UCRT), Release: `guest_pthread_join_detach` passes 3 of 3 on `main` and 3 of 3 with this change. Not tested: macOS. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions (#8658 includes the same change for two of the three maps; see above) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: none needed (libkernel only) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!9272
No description provided.