fix(libkernel): keep the thread stack maps alive through static destruction #9272
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9272"
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 milescoviello on 2026-10-21T17:07:03Z.
What
libkernel keeps three maps of thread stack ranges in
Pthread/src/Thread.cpp:liveStacks,protectedStackPagesandguestStackMappings. 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 ofliveStacks(Thread.cpp:79) races with a detached thread'sUnregisterStack(Thread.cpp:329, holdingstackLock):The three maps are now created once and never destroyed (
static auto& liveStacks = *new std::map<...>()), the wayApr.cppkeeps its file tables. Their mutexes stay as they are, sincestd::mutexneeds no destructor. Nothing else changes.Overlap: #8658 (macOS host support) makes the same change to
liveStacksandprotectedStackPagesas part of a much larger PR. This PR is that piece on its own, plusguestStackMappings, for the Linux exit race. Whichever lands second drops its copy.Tested
Ubuntu 24.04, GCC 13.3, on
maincecb540a63.-fsanitize=thread, run with ASLR off),guest_pthread_join_detach:main: fails 3 of 3, each with the race onliveStacksabove.^guest_pthread|stack|^guest_threadpass 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.guest_pthread_join_detachpasses 3 of 3 onmainand 3 of 3 with this change.Not tested: macOS.
Checklist
main; no other open PR implements the same functions (#8658 includes the same change for two of the three maps; see above)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code