[compiler-rt] r341428 - [hwasan] add a unique id to a thread and add debug prints for thread creation/destruction

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 4 16:26:08 PDT 2018


Author: kcc
Date: Tue Sep  4 16:26:08 2018
New Revision: 341428

URL: http://llvm.org/viewvc/llvm-project?rev=341428&view=rev
Log:
[hwasan] add a unique id to a thread and add debug prints for thread creation/destruction

Modified:
    compiler-rt/trunk/lib/hwasan/hwasan_flags.inc
    compiler-rt/trunk/lib/hwasan/hwasan_thread.cc
    compiler-rt/trunk/lib/hwasan/hwasan_thread.h
    compiler-rt/trunk/test/hwasan/TestCases/many-threads-uaf.c

Modified: compiler-rt/trunk/lib/hwasan/hwasan_flags.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_flags.inc?rev=341428&r1=341427&r2=341428&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_flags.inc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_flags.inc Tue Sep  4 16:26:08 2018
@@ -17,6 +17,8 @@
 // HWASAN_FLAG(Type, Name, DefaultValue, Description)
 // See COMMON_FLAG in sanitizer_flags.inc for more details.
 
+HWASAN_FLAG(bool, verbose_threads, false,
+            "inform on thread creation/destruction")
 HWASAN_FLAG(bool, tag_in_malloc, true, "")
 HWASAN_FLAG(bool, tag_in_free, true, "")
 HWASAN_FLAG(bool, print_stats, false, "")

Modified: compiler-rt/trunk/lib/hwasan/hwasan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_thread.cc?rev=341428&r1=341427&r2=341428&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_thread.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_thread.cc Tue Sep  4 16:26:08 2018
@@ -59,6 +59,7 @@ void Thread::RemoveFromThreadList(Thread
 
 Thread *Thread::Create(thread_callback_t start_routine,
                                void *arg) {
+  static u64 unique_id;
   uptr PageSize = GetPageSizeCached();
   uptr size = RoundUpTo(sizeof(Thread), PageSize);
   Thread *thread = (Thread*)MmapOrDie(size, __func__);
@@ -68,6 +69,7 @@ Thread *Thread::Create(thread_callback_t
   thread->random_state_ = flags()->random_tags ? RandomSeed() : 0;
   if (auto sz = flags()->heap_history_size)
     thread->heap_allocations_ = RingBuffer<HeapAllocationRecord>::New(sz);
+  thread->unique_id_ = unique_id++;
   InsertIntoThreadList(thread);
   return thread;
 }
@@ -99,6 +101,8 @@ void Thread::Init() {
     CHECK(MemIsApp(stack_bottom_));
     CHECK(MemIsApp(stack_top_ - 1));
   }
+  if (flags()->verbose_threads)
+    Print("Creating  ");
 }
 
 void Thread::ClearShadowForThreadStackAndTLS() {
@@ -109,6 +113,8 @@ void Thread::ClearShadowForThreadStackAn
 }
 
 void Thread::Destroy() {
+  if (flags()->verbose_threads)
+    Print("Destroying");
   malloc_storage().CommitBack();
   ClearShadowForThreadStackAndTLS();
   RemoveFromThreadList(this);
@@ -119,6 +125,11 @@ void Thread::Destroy() {
   DTLS_Destroy();
 }
 
+void Thread::Print(const char *Prefix) {
+  Printf("%s: thread %p id: %zd stack: [%p,%p) tls: [%p,%p)\n", Prefix, this,
+         unique_id_, stack_bottom(), stack_top(), tls_begin(), tls_end());
+}
+
 static u32 xorshift(u32 state) {
   state ^= state << 13;
   state ^= state >> 17;

Modified: compiler-rt/trunk/lib/hwasan/hwasan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_thread.h?rev=341428&r1=341427&r2=341428&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_thread.h (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_thread.h Tue Sep  4 16:26:08 2018
@@ -84,6 +84,7 @@ class Thread {
   // via mmap() and *must* be valid in zero-initialized state.
   void SetThreadStackAndTls();
   void ClearShadowForThreadStackAndTLS();
+  void Print(const char *prefix);
   thread_callback_t start_routine_;
   void *arg_;
   uptr stack_top_;
@@ -107,6 +108,8 @@ class Thread {
   static SpinMutex thread_list_mutex;
   static Thread *main_thread;
 
+  u64 unique_id_;  // counting from zero.
+
   u32 tagging_disabled_;  // if non-zero, malloc uses zero tag in this thread.
 
   ThreadStartArg thread_start_arg_;

Modified: compiler-rt/trunk/test/hwasan/TestCases/many-threads-uaf.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/hwasan/TestCases/many-threads-uaf.c?rev=341428&r1=341427&r2=341428&view=diff
==============================================================================
--- compiler-rt/trunk/test/hwasan/TestCases/many-threads-uaf.c (original)
+++ compiler-rt/trunk/test/hwasan/TestCases/many-threads-uaf.c Tue Sep  4 16:26:08 2018
@@ -1,4 +1,4 @@
-// RUN: %clang_hwasan %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hwasan %s -o %t && not %env_hwasan_opts=verbose_threads=1 %run %t 2>&1 | FileCheck %s
 // REQUIRES: stable-runtime
 
 #include <pthread.h>
@@ -14,6 +14,13 @@ void *BoringThread(void *arg) {
   return NULL;
 }
 
+// CHECK: Creating  : thread {{.*}} id: 0
+// CHECK: Creating  : thread {{.*}} id: 1
+// CHECK: Destroying: thread {{.*}} id: 1
+// CHECK: Creating  : thread {{.*}} id: 1100
+// CHECK: Destroying: thread {{.*}} id: 1100
+// CHECK: Creating  : thread {{.*}} id: 1101
+
 void *UAFThread(void *arg) {
   char * volatile x = (char*)malloc(10);
   fprintf(stderr, "ZZZ %p\n", x);




More information about the llvm-commits mailing list