fix(libkernel): keep the thread stack lock alive through static destruction #9960

Merged
tkcm01 merged 1 commit from upstream-pr/9960 into main 2026-10-23 01:04:49 +00:00
tkcm01 commented 2026-10-23 00:22:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tkcm01 on 2026-10-23T00:22:17Z.


What

#9272 keeps liveStacks and protectedStackPages in Pthread/src/Thread.cpp alive through static destruction, so that an mprotect from an atexit handler can still look up guest thread stacks. The stackLock guarding them is still a plain static std::mutex. libstdc++'s std::mutex has a trivial destructor, so it keeps working on Linux after static destruction; libc++'s destructor calls pthread_mutex_destroy, and a later lock throws std::system_error with EINVAL. On macOS, guest_memory_lifetime fails in its exit probe:

memory lifetime exit probe (mapping protection): mutex lock failed: Invalid argument
memory lifetime: child failed during native exit (status=256)

stackLock is now allocated and never destroyed, like the two maps (std::mutex& stackLock = *new std::mutex();, the same form as g_filesLock in Apr.cpp and g_lock in Ajm.cpp). Behaviour on Linux and Windows is unchanged.

The same line is part of the macOS ports in #7201 (@marion909) and #8658 (@iamthehimansh), both credited as co-authors; this splits it out as a small change, as for the rest of the macOS series on #1.

Tested

macOS 27.0.1, Apple M4, Apple clang 21, x86-64 build under Rosetta 2, no Vulkan loader. The libraries do not link on macOS from main alone, so the run is on mugurc's ci/macos-series-2 (53db0badb, main 3fd1cc393, which predates #9272) with #7524, the code commits of #7937 and the libSceSsl change from #7332, as described on #1, plus #9272's two map changes so the tree matches main in this spot.

  • With the maps kept alive and stackLock still a static std::mutex: guest_memory_lifetime fails with the output above.
  • With this change: guest_memory_lifetime passes.
  • Full ctest -j4: 1648 tests, 12 failed, all known macOS failures from my list on #1 (that list minus guest_filesystem, guest_standard_streams and guest_memory_lifetime, which have fixes in this tree); no new failure.
  • python3 tools/check_conventions.py --base upstream/main: passed. tools/pr_overlap.py lists #7201, #7565, #8592, #8658 and #9573 for Thread.cpp; only #7201 and #8658 touch stackLock, with this same change.

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
  • 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: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4975947481 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9960). Originally posted by [tkcm01](https://github.com/tkcm01) on 2026-10-23T00:22:17Z. --- ### What #9272 keeps `liveStacks` and `protectedStackPages` in `Pthread/src/Thread.cpp` alive through static destruction, so that an `mprotect` from an `atexit` handler can still look up guest thread stacks. The `stackLock` guarding them is still a plain `static std::mutex`. libstdc++'s `std::mutex` has a trivial destructor, so it keeps working on Linux after static destruction; libc++'s destructor calls `pthread_mutex_destroy`, and a later `lock` throws `std::system_error` with `EINVAL`. On macOS, `guest_memory_lifetime` fails in its exit probe: ```text memory lifetime exit probe (mapping protection): mutex lock failed: Invalid argument memory lifetime: child failed during native exit (status=256) ``` `stackLock` is now allocated and never destroyed, like the two maps (`std::mutex& stackLock = *new std::mutex();`, the same form as `g_filesLock` in `Apr.cpp` and `g_lock` in `Ajm.cpp`). Behaviour on Linux and Windows is unchanged. The same line is part of the macOS ports in #7201 (@marion909) and #8658 (@iamthehimansh), both credited as co-authors; this splits it out as a small change, as for the rest of the macOS series on #1. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> macOS 27.0.1, Apple M4, Apple clang 21, x86-64 build under Rosetta 2, no Vulkan loader. The libraries do not link on macOS from `main` alone, so the run is on mugurc's `ci/macos-series-2` (53db0badb, `main` 3fd1cc393, which predates #9272) with #7524, the code commits of #7937 and the libSceSsl change from #7332, as described on #1, plus #9272's two map changes so the tree matches `main` in this spot. - With the maps kept alive and `stackLock` still a static `std::mutex`: `guest_memory_lifetime` fails with the output above. - With this change: `guest_memory_lifetime` passes. - Full `ctest -j4`: 1648 tests, 12 failed, all known macOS failures from my list on #1 (that list minus `guest_filesystem`, `guest_standard_streams` and `guest_memory_lifetime`, which have fixes in this tree); no new failure. - `python3 tools/check_conventions.py --base upstream/main`: passed. `tools/pr_overlap.py` lists #7201, #7565, #8592, #8658 and #9573 for `Thread.cpp`; only #7201 and #8658 touch `stackLock`, with this same change. ### 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 - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 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!9960
No description provided.