[llvm-commits] [compiler-rt] r149940 - in /compiler-rt/trunk/lib/asan: asan_internal.h asan_posix.cc asan_thread.cc asan_thread.h asan_thread_registry.cc tests/asan_test.cc

Kostya Serebryany kcc at google.com
Mon Feb 6 16:27:16 PST 2012


Author: kcc
Date: Mon Feb  6 18:27:15 2012
New Revision: 149940

URL: http://llvm.org/viewvc/llvm-project?rev=149940&view=rev
Log:
[asan] make sure the AsanThread object is destroyed if pthread_exit is called

Modified:
    compiler-rt/trunk/lib/asan/asan_internal.h
    compiler-rt/trunk/lib/asan/asan_posix.cc
    compiler-rt/trunk/lib/asan/asan_thread.cc
    compiler-rt/trunk/lib/asan/asan_thread.h
    compiler-rt/trunk/lib/asan/asan_thread_registry.cc
    compiler-rt/trunk/lib/asan/tests/asan_test.cc

Modified: compiler-rt/trunk/lib/asan/asan_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=149940&r1=149939&r2=149940&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_internal.h Mon Feb  6 18:27:15 2012
@@ -129,7 +129,7 @@
 int AtomicInc(int *a);
 
 // Wrapper for TLS/TSD.
-void AsanTSDInit();
+void AsanTSDInit(void (*destructor)(void *tsd));
 void *AsanTSDGet();
 void AsanTSDSet(void *tsd);
 

Modified: compiler-rt/trunk/lib/asan/asan_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_posix.cc?rev=149940&r1=149939&r2=149940&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_posix.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_posix.cc Mon Feb  6 18:27:15 2012
@@ -97,10 +97,10 @@
 
 static pthread_key_t tsd_key;
 static bool tsd_key_inited = false;
-void AsanTSDInit() {
+void AsanTSDInit(void (*destructor)(void *tsd)) {
   CHECK(!tsd_key_inited);
   tsd_key_inited = true;
-  CHECK(0 == pthread_key_create(&tsd_key, 0));
+  CHECK(0 == pthread_key_create(&tsd_key, destructor));
 }
 
 void *AsanTSDGet() {

Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=149940&r1=149939&r2=149940&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Mon Feb  6 18:27:15 2012
@@ -40,7 +40,23 @@
   return thread;
 }
 
+void AsanThreadSummary::TSDDtor(void *tsd) {
+  AsanThreadSummary *summary = (AsanThreadSummary*)tsd;
+  if (FLAG_v >= 1) {
+    Report("T%d TSDDtor\n", summary->tid());
+  }
+  if (summary->thread()) {
+    summary->thread()->Destroy();
+  }
+}
+
 void AsanThread::Destroy() {
+  if (FLAG_v >= 1) {
+    Report("T%d exited\n", tid());
+  }
+
+  asanThreadRegistry().UnregisterThread(this);
+  CHECK(summary()->thread() == NULL);
   // We also clear the shadow on thread destruction because
   // some code may still be executing in later TSD destructors
   // and we don't want it to have any poisoned stack.
@@ -78,11 +94,6 @@
   void *res = start_routine_(arg_);
   malloc_storage().CommitBack();
 
-  if (FLAG_v >= 1) {
-    Report("T%d exited\n", tid());
-  }
-
-  asanThreadRegistry().UnregisterThread(this);
   this->Destroy();
 
   return res;

Modified: compiler-rt/trunk/lib/asan/asan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.h?rev=149940&r1=149939&r2=149940&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.h (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.h Mon Feb  6 18:27:15 2012
@@ -51,6 +51,8 @@
   void set_tid(int tid) { tid_ = tid; }
   AsanThread *thread() { return thread_; }
   void set_thread(AsanThread *thread) { thread_ = thread; }
+  static void TSDDtor(void *tsd);
+
  private:
   int tid_;
   int parent_tid_;

Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.cc?rev=149940&r1=149939&r2=149940&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.cc Mon Feb  6 18:27:15 2012
@@ -32,7 +32,7 @@
       mu_(x) { }
 
 void AsanThreadRegistry::Init() {
-  AsanTSDInit();
+  AsanTSDInit(AsanThreadSummary::TSDDtor);
   main_thread_.set_summary(&main_thread_summary_);
   main_thread_summary_.set_thread(&main_thread_);
   RegisterThread(&main_thread_);

Modified: compiler-rt/trunk/lib/asan/tests/asan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_test.cc?rev=149940&r1=149939&r2=149940&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_test.cc Mon Feb  6 18:27:15 2012
@@ -1637,6 +1637,18 @@
   }
 }
 
+static void *PthreadExit(void *a) {
+  pthread_exit(0);
+}
+
+TEST(AddressSanitizer, PthreadExitTest) {
+  pthread_t t;
+  for (int i = 0; i < 1000; i++) {
+    pthread_create(&t, 0, PthreadExit, 0);
+    pthread_join(t, 0);
+  }
+}
+
 #ifdef __EXCEPTIONS
 __attribute__((noinline))
 static void StackReuseAndException() {





More information about the llvm-commits mailing list