From a60256ea4d1a3b25b246b432b32e538bfad6c152 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 28 Sep 2026 07:47:12 +0000 Subject: [PATCH] fix(kernel): exit POSIX guest threads without forced unwinding On Linux scePthreadExit called pthread_exit, whose forced unwind runs the personality routine of every guest frame. Those resolve to libc.prx's __gxx_personality_v0, which cannot read libgcc's unwind context and aborts, so guest_thread_self crashed on Linux. Record a jump target in the thread start routine and longjmp back to it from scePthreadExit instead. Like _endthreadex on Windows, this skips the guest frames without unwinding them; the start routine still releases the thread normally. Threads not created by scePthreadCreate keep the pthread_exit fallback. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018emyZ4Rk8yoSJvxTFBe4DL --- core/libs/prx/libkernel/Pthread/src/Thread.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index 49378013e..bb6ea5acf 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -14,6 +14,7 @@ #include #ifndef _WIN32 +#include #include #endif @@ -43,6 +44,11 @@ static thread_local PthreadPrivate* currentThread = nullptr; static thread_local bool threadFinishing = false; #ifndef _WIN32 static thread_local std::unique_ptr adoptedThread; +// pthread_exit force-unwinds through guest frames, whose personality resolves to +// libc.prx's __gxx_personality_v0; that routine cannot read libgcc's unwind context +// and aborts. scePthreadExit instead jumps back to the thread start routine, which, +// like _endthreadex on Windows, skips the guest frames without unwinding them. +static thread_local std::jmp_buf* threadExitJump = nullptr; #endif void ThreadLifecycle::SetThreadDtors(thread_dtors_func_t callback) { @@ -238,7 +244,12 @@ int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, Pthread ReleaseThread(self); } } guard{self}; - RunThread(std::move(args)); + std::jmp_buf exitJump; + if (setjmp(exitJump) == 0) { + threadExitJump = &exitJump; + RunThread(std::move(args)); + } + threadExitJump = nullptr; }); try { if (detached) p->_thr.detach(); @@ -297,6 +308,8 @@ void APS5_VABI scePthreadExit(void* retval) { ReleaseThread(self); _endthreadex(0); #else + if (threadExitJump) + std::longjmp(*threadExitJump, 1); pthread_exit(retval); #endif throw std::runtime_error("Native thread exit returned");