From 8410a8b31eddf6445800511242cc835e0d7a35a9 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Sat, 7 Oct 2023 01:47:41 -0700 Subject: [PATCH] ensure that native created threads will be passed the native flag (which appears to be intended) also make sure ThreadInfo constructions use the correct constructor (new ThreadInfo is analogous to malloc while new ThreadInfo is analogous to calloc; granted libtas overrides malloc to calloc so this probably doesn't matter in practice, but the compiler probably shouldn't be getting any funny ideas over UB here) --- src/library/checkpoint/ThreadManager.cpp | 2 +- src/library/pthreadwrappers.cpp | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/library/checkpoint/ThreadManager.cpp b/src/library/checkpoint/ThreadManager.cpp index 0bc009e8..cd978b5f 100644 --- a/src/library/checkpoint/ThreadManager.cpp +++ b/src/library/checkpoint/ThreadManager.cpp @@ -183,7 +183,7 @@ ThreadInfo* ThreadManager::getNewThread() /* No free thread, create a new one */ if (!thread) { - thread = new ThreadInfo; + thread = new ThreadInfo(); debuglogstdio(LCF_THREAD, "Allocate a new ThreadInfo struct"); threadListChanged = true; } diff --git a/src/library/pthreadwrappers.cpp b/src/library/pthreadwrappers.cpp index 2687c986..12b6f0ba 100644 --- a/src/library/pthreadwrappers.cpp +++ b/src/library/pthreadwrappers.cpp @@ -187,14 +187,29 @@ static void *pthread_start(void *arg) return thread->retval; } +static void *pthread_native_start(void *arg) +{ + ThreadInfo* thread = static_cast(arg); + ThreadManager::update(thread); + + auto thread_start = thread->start; + auto thread_arg = thread->arg; + delete thread; + + return thread_start(thread_arg); +} + /* Override */ int pthread_create (pthread_t * tid_p, const pthread_attr_t * attr, void * (* start_routine) (void *), void * arg) __THROW { LINK_NAMESPACE(pthread_create, "pthread"); LINK_NAMESPACE(pthread_detach, "pthread"); - if (GlobalState::isNative()) - return orig::pthread_create(tid_p, attr, start_routine, arg); + if (GlobalState::isNative()) { + ThreadInfo* thread = new ThreadInfo(); + ThreadManager::initThreadFromParent(thread, start_routine, arg, __builtin_return_address(0)); + return orig::pthread_create(tid_p, attr, pthread_native_start, thread); + } debuglogstdio(LCF_THREAD, "Thread is created with routine %p", (void*)start_routine);