[asan] make sure the AsanThread object is destroyed if pthread_exit is called

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@149940 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_thread.cc b/lib/asan/asan_thread.cc
index 66bb01e..6e6436b 100644
--- a/lib/asan/asan_thread.cc
+++ b/lib/asan/asan_thread.cc
@@ -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;