diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp --- a/compiler-rt/lib/hwasan/hwasan.cpp +++ b/compiler-rt/lib/hwasan/hwasan.cpp @@ -345,7 +345,7 @@ // Needs to be called here because flags()->random_tags might not have been // initialized when InitInstrumentation() was called. - GetCurrentThread()->InitRandomState(); + GetCurrentThread()->EnsureRandomStateInited(); SetPrintfAndReportCallback(AppendToErrorMessageBuffer); // This may call libc -> needs initialized shadow. diff --git a/compiler-rt/lib/hwasan/hwasan_fuchsia.cpp b/compiler-rt/lib/hwasan/hwasan_fuchsia.cpp --- a/compiler-rt/lib/hwasan/hwasan_fuchsia.cpp +++ b/compiler-rt/lib/hwasan/hwasan_fuchsia.cpp @@ -130,7 +130,7 @@ static void ThreadStartHook(void *hook, thrd_t self) { Thread *thread = static_cast(hook); FinishThreadInitialization(thread); - thread->InitRandomState(); + thread->EnsureRandomStateInited(); } // This is the function that sets up the stack ring buffer and enables us to use diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp --- a/compiler-rt/lib/hwasan/hwasan_linux.cpp +++ b/compiler-rt/lib/hwasan/hwasan_linux.cpp @@ -250,7 +250,7 @@ // ---------------------- TSD ---------------- {{{1 extern "C" void __hwasan_thread_enter() { - hwasanThreadList().CreateCurrentThread()->InitRandomState(); + hwasanThreadList().CreateCurrentThread()->EnsureRandomStateInited(); } extern "C" void __hwasan_thread_exit() { diff --git a/compiler-rt/lib/hwasan/hwasan_thread.h b/compiler-rt/lib/hwasan/hwasan_thread.h --- a/compiler-rt/lib/hwasan/hwasan_thread.h +++ b/compiler-rt/lib/hwasan/hwasan_thread.h @@ -28,12 +28,17 @@ void Init(uptr stack_buffer_start, uptr stack_buffer_size, const InitState *state = nullptr); - void InitRandomState(); + void InitStackAndTls(const InitState *state = nullptr); // Must be called from the thread itself. void InitStackRingBuffer(uptr stack_buffer_start, uptr stack_buffer_size); + inline void EnsureRandomStateInited() { + if (UNLIKELY(!random_state_inited_)) + InitRandomState(); + } + void Destroy(); uptr stack_top() { return stack_top_; } @@ -70,6 +75,7 @@ // via mmap() and *must* be valid in zero-initialized state. void ClearShadowForThreadStackAndTLS(); void Print(const char *prefix); + void InitRandomState(); uptr vfork_spill_; uptr stack_top_; uptr stack_bottom_; @@ -89,6 +95,8 @@ bool announced_; + bool random_state_inited_; // whether random_state_ has been initialized. + friend struct ThreadListHead; }; diff --git a/compiler-rt/lib/hwasan/hwasan_thread.cpp b/compiler-rt/lib/hwasan/hwasan_thread.cpp --- a/compiler-rt/lib/hwasan/hwasan_thread.cpp +++ b/compiler-rt/lib/hwasan/hwasan_thread.cpp @@ -27,6 +27,7 @@ void Thread::InitRandomState() { random_state_ = flags()->random_tags ? RandomSeed() : unique_id_; + random_state_inited_ = true; // Push a random number of zeros onto the ring buffer so that the first stack // tag base will be random. @@ -125,6 +126,7 @@ tag_t Thread::GenerateRandomTag(uptr num_bits) { DCHECK_GT(num_bits, 0); if (tagging_disabled_) return 0; + EnsureRandomStateInited(); tag_t tag; const uptr tag_mask = (1ULL << num_bits) - 1; do { diff --git a/compiler-rt/test/hwasan/TestCases/pthread_create.c b/compiler-rt/test/hwasan/TestCases/pthread_create.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/hwasan/TestCases/pthread_create.c @@ -0,0 +1,22 @@ +// Tests that our thread initialization hooks work properly with random_tags=1. +// RUN: %clang_hwasan %s -o %t +// RUN: %env_hwasan_opts=random_tags=1 %run %t +// REQUIRES: stable-runtime + +#include + +#include + +volatile int state; + +void *Increment(void *arg) { + ++state; + return NULL; +} + +int main() { + __hwasan_enable_allocator_tagging(); + pthread_t t1; + pthread_create(&t1, NULL, Increment, NULL); + pthread_join(t1, NULL); +} diff --git a/compiler-rt/test/hwasan/TestCases/thread-uaf.c b/compiler-rt/test/hwasan/TestCases/thread-uaf.c --- a/compiler-rt/test/hwasan/TestCases/thread-uaf.c +++ b/compiler-rt/test/hwasan/TestCases/thread-uaf.c @@ -1,8 +1,6 @@ // Tests UAF detection where Allocate/Deallocate/Use // happen in separate threads. -// RUN: %clang_hwasan %s -o %t && not %run %t > %t.out 2>&1 -// RUN: cat %t.out | FileCheck %s -// RUN: cat %t.out | FileCheck --check-prefix=CHECK-THREAD %s +// RUN: %clang_hwasan %s -o %t && not %run %t 2>&1 | FileCheck %s // REQUIRES: stable-runtime #include @@ -37,10 +35,10 @@ // CHECK: in Deallocate // CHECK: previously allocated here: // CHECK: in Allocate - // CHECK-THREAD-DAG: Thread: T2 0x - // CHECK-THREAD-DAG: Thread: T3 0x - // CHECK-THREAD-DAG: Thread: T0 0x - // CHECK-THREAD-DAG: Thread: T1 0x + // CHECK-DAG: Thread: T2 0x + // CHECK-DAG: Thread: T3 0x + // CHECK-DAG: Thread: T0 0x + // CHECK-DAG: Thread: T1 0x __sync_fetch_and_add(&state, 1); return NULL; }