[tsan] Fix a crash when exiting the main thread (e.g. dispatch_main)
authorKuba Brecka <kuba.brecka@gmail.com>
Mon, 28 Mar 2016 19:36:25 +0000 (19:36 +0000)
committerKuba Brecka <kuba.brecka@gmail.com>
Mon, 28 Mar 2016 19:36:25 +0000 (19:36 +0000)
This patch fixes the custom ThreadState destruction on OS X to avoid crashing when dispatch_main calls pthread_exit which quits the main thread.

Differential Revision: http://reviews.llvm.org/D18496

llvm-svn: 264627

compiler-rt/lib/tsan/rtl/tsan_platform_mac.cc
compiler-rt/test/tsan/Darwin/dispatch_main.mm [new file with mode: 0644]

index 31caf37..6109ef2 100644 (file)
@@ -91,7 +91,11 @@ ThreadState *cur_thread() {
 // handler will try to access the unmapped ThreadState.
 void cur_thread_finalize() {
   uptr thread_identity = (uptr)pthread_self();
-  CHECK_NE(thread_identity, main_thread_identity);
+  if (thread_identity == main_thread_identity) {
+    // Calling dispatch_main() or xpc_main() actually invokes pthread_exit to
+    // exit the main thread. Let's keep the main thread's ThreadState.
+    return;
+  }
   ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
   internal_munmap(*fake_tls, sizeof(ThreadState));
   *fake_tls = nullptr;
diff --git a/compiler-rt/test/tsan/Darwin/dispatch_main.mm b/compiler-rt/test/tsan/Darwin/dispatch_main.mm
new file mode 100644 (file)
index 0000000..1bb611c
--- /dev/null
@@ -0,0 +1,38 @@
+// Check that we don't crash when dispatch_main calls pthread_exit which
+// quits the main thread.
+
+// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s
+
+#import <Foundation/Foundation.h>
+
+int main() {
+  NSLog(@"Hello world");
+
+  dispatch_queue_t q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL);
+
+  dispatch_async(q, ^{
+    NSLog(@"1");
+  });
+
+  dispatch_async(q, ^{
+    NSLog(@"2");
+  });
+
+  dispatch_async(q, ^{
+    NSLog(@"3");
+
+    dispatch_async(dispatch_get_main_queue(), ^{
+      NSLog(@"Done.");
+      sleep(1);
+      exit(0);
+    });
+  });
+
+  dispatch_main();
+}
+
+// CHECK: Hello world
+// CHECK: Done.
+// CHECK-NOT: WARNING: ThreadSanitizer
+// CHECK-NOT: CHECK failed