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 @@ -28,6 +28,9 @@ void Thread::InitRandomState() { random_state_ = flags()->random_tags ? RandomSeed() : unique_id_; + // Now that random_state_ is initialized, we can call GenerateRandomTag(). + EnableTagging(); + // Push a random number of zeros onto the ring buffer so that the first stack // tag base will be random. for (tag_t i = 0, e = GenerateRandomTag(); i != e; ++i) @@ -40,6 +43,9 @@ CHECK_EQ(0, stack_top_); CHECK_EQ(0, stack_bottom_); + // Disable tagging until InitRandomState() is called. + DisableTagging(); + static u64 unique_id; unique_id_ = unique_id++; if (auto sz = flags()->heap_history_size) 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; }