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 <noreply@anthropic.com>
This commit is contained in:
Merijn
2026-10-05 23:00:41 +02:00
parent 2707b3a3c8
commit fd37c58391
2 changed files with 47 additions and 0 deletions
@@ -42,6 +42,27 @@ static std::atomic<get_thread_atexit_count_func_t> threadAtexitCount{nullptr};
static std::atomic<thread_atexit_report_func_t> 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<void*>(low);
thread->stackSize = static_cast<std::size_t>(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<PthreadPrivate> adoptedThread;
// pthread_exit force-unwinds through guest frames, whose personality resolves to
@@ -96,6 +117,7 @@ static void RunThread(std::unique_ptr<ThreadArgs> 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
+23
View File
@@ -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<std::uintptr_t>(stack);
const auto value = reinterpret_cast<std::uintptr_t>(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<WorkerContext*>(arg);
context.selfFromWorker = scePthreadSelf();
int local = 0;
context.workerStackReported = StackContains(context.selfFromWorker, &local);
context.unlockResult = scePthreadMutexUnlock(context.mutex);
scePthreadExit(reinterpret_cast<void*>(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);