[PATCH] D18496: [tsan] Fix a crash when exiting the main thread (e.g. dispatch_main)

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 27 00:34:28 PDT 2016


kubabrecka created this revision.
kubabrecka added reviewers: dvyukov, samsonov, kcc, glider.
kubabrecka added subscribers: llvm-commits, zaks.anna, dcoughlin.

This patch fixes the custom ThreadState destruction on OS X to avoid crashing when dispatch_main calls pthread_exit which quits the main thread.

http://reviews.llvm.org/D18496

Files:
  lib/tsan/rtl/tsan_platform_mac.cc
  test/tsan/Darwin/dispatch_main.mm

Index: test/tsan/Darwin/dispatch_main.mm
===================================================================
--- test/tsan/Darwin/dispatch_main.mm
+++ test/tsan/Darwin/dispatch_main.mm
@@ -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(3);
+      exit(0);
+    });
+  });
+
+  dispatch_main();
+}
+
+// CHECK: Hello world
+// CHECK: Done.
+// CHECK-NOT: WARNING: ThreadSanitizer
+// CHECK-NOT: CHECK failed
Index: lib/tsan/rtl/tsan_platform_mac.cc
===================================================================
--- lib/tsan/rtl/tsan_platform_mac.cc
+++ lib/tsan/rtl/tsan_platform_mac.cc
@@ -91,7 +91,10 @@
 // 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) {
+    // Keep the main thread's ThreadState.
+    return;
+  }
   ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
   internal_munmap(*fake_tls, sizeof(ThreadState));
   *fake_tls = nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18496.51729.patch
Type: text/x-patch
Size: 1662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160327/c842d756/attachment.bin>


More information about the llvm-commits mailing list