From fd37c583912eab3ada5013b4c487231fe758276b Mon Sep 17 00:00:00 2001 From: Merijn Date: Mon, 5 Oct 2026 23:00:41 +0200 Subject: [PATCH] fix(libkernel): report the stack of adopted threads scePthreadAttrGet reported a null stack address and a zero size for an adopted thread (the main thread), and for every guest thread on Linux, where the stack range was never recorded. A garbage collector that scans thread stacks through scePthreadAttrGetstackaddr and scePthreadAttrGetstacksize (Unity IL2CPP in PPSA12544) then skipped the main thread's stack and freed objects it still referenced. The range now comes from the host thread: GetCurrentThreadStackLimits on Windows, pthread_getattr_np on Linux. Co-Authored-By: Claude Opus 5.5 --- .../libs/prx/libkernel/Pthread/src/Thread.cpp | 24 +++++++++++++++++++ core/libs/tests/GuestThreadSelf.cpp | 23 ++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index 3dc69cbab..23b69cf56 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -42,6 +42,27 @@ static std::atomic threadAtexitCount{nullptr}; static std::atomic threadAtexitReport{nullptr}; static thread_local PthreadPrivate* currentThread = nullptr; static thread_local bool threadFinishing = false; + +static void SetStackFromHost(PthreadPrivate* thread) { +#ifdef _WIN32 + ULONG_PTR low = 0; + ULONG_PTR high = 0; + GetCurrentThreadStackLimits(&low, &high); + if (high <= low) throw std::runtime_error("Cannot query the host thread stack"); + thread->stackAddress = reinterpret_cast(low); + thread->stackSize = static_cast(high - low); +#else + pthread_attr_t attr; + if (pthread_getattr_np(pthread_self(), &attr) != 0) throw std::runtime_error("Cannot query the host thread stack"); + void* address = nullptr; + std::size_t size = 0; + const int result = pthread_attr_getstack(&attr, &address, &size); + pthread_attr_destroy(&attr); + if (result != 0 || size == 0) throw std::runtime_error("Cannot query the host thread stack"); + thread->stackAddress = address; + thread->stackSize = size; +#endif +} #ifndef _WIN32 static thread_local std::unique_ptr adoptedThread; // pthread_exit force-unwinds through guest frames, whose personality resolves to @@ -96,6 +117,7 @@ static void RunThread(std::unique_ptr args) { const auto entry = args->entry; void* arg = args->arg; PthreadPrivate* self = args->self; + if (!self->stackAddress) SetStackFromHost(self); currentThread = self; args.reset(); finishThread(self, entry(arg)); @@ -327,6 +349,7 @@ Pthread APS5_VABI scePthreadSelf() { adopted->threadId = std::this_thread::get_id(); adopted->_detached = true; adopted->references.store(1, std::memory_order_relaxed); + SetStackFromHost(adopted.get()); currentThread = adopted.release(); } #else @@ -335,6 +358,7 @@ Pthread APS5_VABI scePthreadSelf() { adoptedThread->_detached = true; adoptedThread->_adopted = true; adoptedThread->threadId = std::this_thread::get_id(); + SetStackFromHost(adoptedThread.get()); currentThread = adoptedThread.get(); } #endif diff --git a/core/libs/tests/GuestThreadSelf.cpp b/core/libs/tests/GuestThreadSelf.cpp index 585745cbd..4290ec703 100644 --- a/core/libs/tests/GuestThreadSelf.cpp +++ b/core/libs/tests/GuestThreadSelf.cpp @@ -15,6 +15,23 @@ int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* a int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex); int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex); int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex); +int APS5_VABI scePthreadAttrInit(PthreadAttr* attr); +int APS5_VABI scePthreadAttrDestroy(PthreadAttr* attr); +int APS5_VABI scePthreadAttrGet(Pthread thread, PthreadAttr* attr); +int APS5_VABI scePthreadAttrGetstackaddr(const PthreadAttr* attr, void** stack_addr); +int APS5_VABI scePthreadAttrGetstacksize(const PthreadAttr* attr, size_t* stack_size); +} + +static bool StackContains(Pthread thread, const void* address) { + PthreadAttr attr = nullptr; + if (scePthreadAttrInit(&attr) != 0 || scePthreadAttrGet(thread, &attr) != 0) return false; + void* stack = nullptr; + size_t size = 0; + const bool queried = scePthreadAttrGetstackaddr(&attr, &stack) == 0 && scePthreadAttrGetstacksize(&attr, &size) == 0; + scePthreadAttrDestroy(&attr); + const auto begin = reinterpret_cast(stack); + const auto value = reinterpret_cast(address); + return queried && stack != nullptr && size != 0 && value >= begin && value - begin < size; } static constexpr int SCE_OK = 0; @@ -28,6 +45,7 @@ static void Require(bool value) { if (!value) std::abort(); } struct WorkerContext { Pthread thread = nullptr; Pthread selfFromWorker = nullptr; + bool workerStackReported = false; PthreadMutex* mutex = nullptr; int unlockResult = 0; }; @@ -35,6 +53,8 @@ struct WorkerContext { static void* APS5_VABI Worker(void* arg) { auto& context = *static_cast(arg); context.selfFromWorker = scePthreadSelf(); + int local = 0; + context.workerStackReported = StackContains(context.selfFromWorker, &local); context.unlockResult = scePthreadMutexUnlock(context.mutex); scePthreadExit(reinterpret_cast(WorkerRetval)); return nullptr; @@ -44,6 +64,8 @@ int main() { const Pthread mainSelf = scePthreadSelf(); Require(mainSelf != nullptr); Require(scePthreadSelf() == mainSelf); + int local = 0; + Require(StackContains(mainSelf, &local)); Require(scePthreadJoin(mainSelf, nullptr) == SCE_KERNEL_ERROR_EINVAL); Require(scePthreadDetach(mainSelf) == SCE_KERNEL_ERROR_EINVAL); @@ -69,6 +91,7 @@ int main() { Require(context.selfFromWorker == context.thread); Require(context.selfFromWorker != mainSelf); Require(context.unlockResult == SCE_KERNEL_ERROR_EPERM); + Require(context.workerStackReported); Require(scePthreadMutexUnlock(&mutex) == SCE_OK); Require(scePthreadMutexDestroy(&mutex) == SCE_OK);