fix(libkernel): prevent TLS nullptr crash in scePthreadCreate #1746
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1746"
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 Kikoarl on 2026-10-08T18:21:25Z.
What
This PR addresses two concurrency and stability issues in
core/libs/prx/libkernel/Pthread/src/Thread.cpp:Prevent TLS nullptr crash in
scePthreadCreate:scePthreadCreateon Linux (#elsebranch) previously usedstd::promise<bool> startandstart.set_value(true)to synchronize the creator and newly spawned threads.Under Linux GCC/glibc,
std::promise::set_valueinvokesstd::call_once, which relies on libstdc++ thread-local storage (_ZSt15__once_callable). When host tests or client code run across the boundary between the PRX shared library and test executables, the trampoline function executed viapthread_once/__once_proxycan dereference a null TLS pointer (_ZSt15__once_callableis0x0due to dual runtime / TLS model mismatch), resulting in an immediateSIGSEGVinstd::once_flag::_Prepare_execution::_FUNatmov (%rax), %rdx.This change replaces
std::promise<bool>on Linux with C++20std::atomic<int>wait/notify synchronization (wait(0), release store +notify_one()), avoiding any call tostd::call_onceor libstdc++ TLS.Preserve configured thread stack size and bounds in
SetStackFromHost:When a thread was created via
scePthreadCreatewith specific stack attributes, the child thread'sRunThreadexecutedSetStackFromHost(self)which unconditionally overwrotethread->stackSizewith the host thread stack size (typically 8 MB).SetStackFromHostnow preserves the configured stack size when non-zero. In addition, because x86-64 stacks grow downwards fromhightolow, it setsthread->stackAddress = reinterpret_cast<void*>(high - thread->stackSize)(matching the Windows behavior inStartNativeThread), ensuring guest thread local variables fall cleanly within the reported stack bounds.Tested
libs: 0 errors.ctest --test-dir /tmp/anyps5_build -R "guest_" -V: all 152 guest tests pass 100%.guest_kernel_is_stack,guest_thread_self, andguest_pthread_attrwithout failure.python3 tools/check_conventions.py --base origin/main: all convention checks passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T18:41:24Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libkernel/Pthread/src/Thread.cppL317-335, L339-340Imported from boykopovar/AnyPS5. Originally posted by WifeInLantern on 2026-10-09T07:28:54Z.
Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON, #1746 merged onto current main (clean). All 146guest_*tests pass (excluding the relinker Python tests, which I didn't build).guest_kernel_is_stack,guest_thread_self,guest_pthread_attrandguest_web_browser_dialogpass 30 repeated runs.The Windows CI failure is
guest_web_browser_dialogwithreserve the guest arena range: Attempt to access invalid address; it passes here and the error is in address-space reservation, not threading, so it looks unrelated.The change does not affect Windows at runtime: the promise removal is in the Linux branch, and
SetStackFromHostis skipped on Windows becauseStartNativeThreadalready setsstackAddress. So this only confirms nothing breaks on Windows; the Linux fixes are not verified by me.Two notes: the stack-bounds fix is separate from the
scePthreadCreateTLS fix, and CONTRIBUTING asks for one topic per PR. The overlap bot also lists #1802 and #1988 on the same lines.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:22:32Z.
Which build crashes here on current main? Linux CI runs
guest_pthread_attrgreen, so the null__once_callablepoints at two libstdc++ copies in your test build rather than atstd::promise, andstd::call_onceelsewhere inThread.cppwould hit the same thing. Please also move the stack-size commit to its own PR.Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T12:53:45Z.
Thanks for testing and review @WifeInLantern and @oneandonlydean!
As requested, I have removed the
SetStackFromHoststack bounds commit from this PR and kept it in a separate branch so this PR remains strictly focused on a single topic.Regarding the crash on Linux: running
guest_pthread_attr_testsagainst unpatchedlibkernel.prxreproduces aSIGSEGVinsidepthread_once()invoked bystd::promise::set_value():The crash occurs because
std::call_onceinternally relies on__once_callablein thread-local storage (TLS), which dereferences null across the shared library / test executable DSO boundary. Replacingstd::promisewithstd::atomic<int>synchronization avoidsstd::call_onceand cross-DSO TLS lifetime issues inscePthreadCreate, passing allguest_pthread_*tests cleanly.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:08:16Z.
Closing: Linux CI runs
guest_pthread_attrgreen on main, so the null__once_callablecomes from how your build links libstdc++ rather than fromstd::promise, and thestd::call_oncecalls elsewhere inThread.cppwould crash the same way there. If a CI configuration or a title hits it, please reopen with that.Pull request closed