[ASan] Refactor thread starting code.
Move thread context creation into AsanThread::Create(). llvm-svn: 223483
This commit is contained in:
parent
01785bb063
commit
eba518be94
|
@ -197,10 +197,8 @@ INTERCEPTOR(int, pthread_create, void *thread,
|
||||||
int result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m);
|
int result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m);
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
u32 current_tid = GetCurrentTidOrInvalid();
|
u32 current_tid = GetCurrentTidOrInvalid();
|
||||||
AsanThread *t = AsanThread::Create(start_routine, arg);
|
AsanThread *t =
|
||||||
CreateThreadContextArgs args = { t, &stack };
|
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
|
||||||
asanThreadRegistry().CreateThread(*reinterpret_cast<uptr *>(t), detached,
|
|
||||||
current_tid, &args);
|
|
||||||
atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
|
atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
|
||||||
// Wait until the AsanThread object is initialized and the ThreadRegistry
|
// Wait until the AsanThread object is initialized and the ThreadRegistry
|
||||||
// entry is in "started" state. One reason for this is that after this
|
// entry is in "started" state. One reason for this is that after this
|
||||||
|
@ -739,10 +737,8 @@ INTERCEPTOR_WINAPI(DWORD, CreateThread,
|
||||||
¶m, thr_flags, tid);
|
¶m, thr_flags, tid);
|
||||||
if (result) {
|
if (result) {
|
||||||
u32 current_tid = GetCurrentTidOrInvalid();
|
u32 current_tid = GetCurrentTidOrInvalid();
|
||||||
AsanThread *t = AsanThread::Create(start_routine, arg);
|
AsanThread *t =
|
||||||
CreateThreadContextArgs args = { t, &stack };
|
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
|
||||||
asanThreadRegistry().CreateThread(*reinterpret_cast<uptr *>(t), detached,
|
|
||||||
current_tid, &args);
|
|
||||||
atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
|
atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
|
||||||
// The pthread_create interceptor waits here, so we do the same for
|
// The pthread_create interceptor waits here, so we do the same for
|
||||||
// consistency.
|
// consistency.
|
||||||
|
|
|
@ -264,9 +264,8 @@ ALWAYS_INLINE
|
||||||
void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
|
void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
|
||||||
AsanThread *t = GetCurrentThread();
|
AsanThread *t = GetCurrentThread();
|
||||||
if (!t) {
|
if (!t) {
|
||||||
t = AsanThread::Create(0, 0);
|
t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
|
||||||
CreateThreadContextArgs args = { t, stack };
|
parent_tid, stack, /* detached */ true);
|
||||||
asanThreadRegistry().CreateThread(*(uptr*)t, true, parent_tid, &args);
|
|
||||||
t->Init();
|
t->Init();
|
||||||
asanThreadRegistry().StartThread(t->tid(), 0, 0);
|
asanThreadRegistry().StartThread(t->tid(), 0, 0);
|
||||||
SetCurrentThread(t);
|
SetCurrentThread(t);
|
||||||
|
|
|
@ -674,11 +674,10 @@ static void AsanInitInternal() {
|
||||||
InitTlsSize();
|
InitTlsSize();
|
||||||
|
|
||||||
// Create main thread.
|
// Create main thread.
|
||||||
AsanThread *main_thread = AsanThread::Create(0, 0);
|
AsanThread *main_thread = AsanThread::Create(
|
||||||
CreateThreadContextArgs create_main_args = { main_thread, 0 };
|
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ 0,
|
||||||
u32 main_tid = asanThreadRegistry().CreateThread(
|
/* stack */ nullptr, /* detached */ true);
|
||||||
0, true, 0, &create_main_args);
|
CHECK_EQ(0, main_thread->tid());
|
||||||
CHECK_EQ(0, main_tid);
|
|
||||||
SetCurrentThread(main_thread);
|
SetCurrentThread(main_thread);
|
||||||
main_thread->ThreadStart(internal_getpid(),
|
main_thread->ThreadStart(internal_getpid(),
|
||||||
/* signal_thread_is_registered */ nullptr);
|
/* signal_thread_is_registered */ nullptr);
|
||||||
|
|
|
@ -27,6 +27,11 @@ namespace __asan {
|
||||||
|
|
||||||
// AsanThreadContext implementation.
|
// AsanThreadContext implementation.
|
||||||
|
|
||||||
|
struct CreateThreadContextArgs {
|
||||||
|
AsanThread *thread;
|
||||||
|
StackTrace *stack;
|
||||||
|
};
|
||||||
|
|
||||||
void AsanThreadContext::OnCreated(void *arg) {
|
void AsanThreadContext::OnCreated(void *arg) {
|
||||||
CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
|
CreateThreadContextArgs *args = static_cast<CreateThreadContextArgs*>(arg);
|
||||||
if (args->stack)
|
if (args->stack)
|
||||||
|
@ -75,13 +80,17 @@ AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
|
||||||
|
|
||||||
// AsanThread implementation.
|
// AsanThread implementation.
|
||||||
|
|
||||||
AsanThread *AsanThread::Create(thread_callback_t start_routine,
|
AsanThread *AsanThread::Create(thread_callback_t start_routine, void *arg,
|
||||||
void *arg) {
|
u32 parent_tid, StackTrace *stack,
|
||||||
|
bool detached) {
|
||||||
uptr PageSize = GetPageSizeCached();
|
uptr PageSize = GetPageSizeCached();
|
||||||
uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
|
uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
|
||||||
AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__);
|
AsanThread *thread = (AsanThread*)MmapOrDie(size, __func__);
|
||||||
thread->start_routine_ = start_routine;
|
thread->start_routine_ = start_routine;
|
||||||
thread->arg_ = arg;
|
thread->arg_ = arg;
|
||||||
|
CreateThreadContextArgs args = { thread, stack };
|
||||||
|
asanThreadRegistry().CreateThread(*reinterpret_cast<uptr *>(thread), detached,
|
||||||
|
parent_tid, &args);
|
||||||
|
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,8 @@ COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
|
||||||
// AsanThread are stored in TSD and destroyed when the thread dies.
|
// AsanThread are stored in TSD and destroyed when the thread dies.
|
||||||
class AsanThread {
|
class AsanThread {
|
||||||
public:
|
public:
|
||||||
static AsanThread *Create(thread_callback_t start_routine, void *arg);
|
static AsanThread *Create(thread_callback_t start_routine, void *arg,
|
||||||
|
u32 parent_tid, StackTrace *stack, bool detached);
|
||||||
static void TSDDtor(void *tsd);
|
static void TSDDtor(void *tsd);
|
||||||
void Destroy();
|
void Destroy();
|
||||||
|
|
||||||
|
@ -167,11 +168,6 @@ class ScopedDeadlySignal {
|
||||||
AsanThread *thread;
|
AsanThread *thread;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CreateThreadContextArgs {
|
|
||||||
AsanThread *thread;
|
|
||||||
StackTrace *stack;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Returns a single instance of registry.
|
// Returns a single instance of registry.
|
||||||
ThreadRegistry &asanThreadRegistry();
|
ThreadRegistry &asanThreadRegistry();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue