[llvm-commits] [compiler-rt] r172393 - in /compiler-rt/trunk/lib/tsan: lit_tests/mutex_destroy_locked.cc lit_tests/stack_race.cc lit_tests/stack_race2.cc lit_tests/tls_race.cc lit_tests/tls_race2.cc rtl/tsan_interceptors.cc rtl/tsan_mman.cc rtl/tsan_report.cc rtl/tsan_report.h rtl/tsan_rtl_mutex.cc rtl/tsan_rtl_report.cc

Dmitry Vyukov dvyukov at google.com
Mon Jan 14 02:00:03 PST 2013


Author: dvyukov
Date: Mon Jan 14 04:00:03 2013
New Revision: 172393

URL: http://llvm.org/viewvc/llvm-project?rev=172393&view=rev
Log:
tsan: describe stack and TLS addresses

Added:
    compiler-rt/trunk/lib/tsan/lit_tests/stack_race.cc
    compiler-rt/trunk/lib/tsan/lit_tests/stack_race2.cc
    compiler-rt/trunk/lib/tsan/lit_tests/tls_race.cc
    compiler-rt/trunk/lib/tsan/lit_tests/tls_race2.cc
Modified:
    compiler-rt/trunk/lib/tsan/lit_tests/mutex_destroy_locked.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_report.h
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc

Modified: compiler-rt/trunk/lib/tsan/lit_tests/mutex_destroy_locked.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/mutex_destroy_locked.cc?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/mutex_destroy_locked.cc (original)
+++ compiler-rt/trunk/lib/tsan/lit_tests/mutex_destroy_locked.cc Mon Jan 14 04:00:03 2013
@@ -2,19 +2,11 @@
 #include <pthread.h>
 #include <unistd.h>
 
-void *Thread(void *p) {
-  pthread_mutex_lock((pthread_mutex_t*)p);
-  return 0;
-}
-
 int main() {
   pthread_mutex_t m;
   pthread_mutex_init(&m, 0);
-  pthread_t t;
-  pthread_create(&t, 0, Thread, &m);
-  sleep(1);
+  pthread_mutex_lock(&m);
   pthread_mutex_destroy(&m);
-  pthread_join(t, 0);
   return 0;
 }
 
@@ -23,7 +15,7 @@
 // CHECK:     #1 main
 // CHECK:   and:
 // CHECK:     #0 pthread_mutex_lock
-// CHECK:     #1 Thread
+// CHECK:     #1 main
 // CHECK:   Mutex {{.*}} created at:
 // CHECK:     #0 pthread_mutex_init
 // CHECK:     #1 main

Added: compiler-rt/trunk/lib/tsan/lit_tests/stack_race.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/stack_race.cc?rev=172393&view=auto
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/stack_race.cc (added)
+++ compiler-rt/trunk/lib/tsan/lit_tests/stack_race.cc Mon Jan 14 04:00:03 2013
@@ -0,0 +1,20 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
+#include <pthread.h>
+#include <stddef.h>
+
+void *Thread(void *a) {
+  *(int*)a = 43;
+  return 0;
+}
+
+int main() {
+  int Var = 42;
+  pthread_t t;
+  pthread_create(&t, 0, Thread, &Var);
+  Var = 43;
+  pthread_join(t, 0);
+}
+
+// CHECK: WARNING: ThreadSanitizer: data race
+// CHECK:   Location is stack of main thread.
+

Added: compiler-rt/trunk/lib/tsan/lit_tests/stack_race2.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/stack_race2.cc?rev=172393&view=auto
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/stack_race2.cc (added)
+++ compiler-rt/trunk/lib/tsan/lit_tests/stack_race2.cc Mon Jan 14 04:00:03 2013
@@ -0,0 +1,28 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
+#include <pthread.h>
+#include <stddef.h>
+#include <unistd.h>
+
+void *Thread2(void *a) {
+  *(int*)a = 43;
+  return 0;
+}
+
+void *Thread(void *a) {
+  int Var = 42;
+  pthread_t t;
+  pthread_create(&t, 0, Thread2, &Var);
+  Var = 42;
+  pthread_join(t, 0);
+  return 0;
+}
+
+int main() {
+  pthread_t t;
+  pthread_create(&t, 0, Thread, 0);
+  pthread_join(t, 0);
+}
+
+// CHECK: WARNING: ThreadSanitizer: data race
+// CHECK:   Location is stack of thread T1.
+

Added: compiler-rt/trunk/lib/tsan/lit_tests/tls_race.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/tls_race.cc?rev=172393&view=auto
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/tls_race.cc (added)
+++ compiler-rt/trunk/lib/tsan/lit_tests/tls_race.cc Mon Jan 14 04:00:03 2013
@@ -0,0 +1,19 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
+#include <pthread.h>
+#include <stddef.h>
+
+void *Thread(void *a) {
+  *(int*)a = 43;
+  return 0;
+}
+
+int main() {
+  static __thread int Var = 42;
+  pthread_t t;
+  pthread_create(&t, 0, Thread, &Var);
+  Var = 43;
+  pthread_join(t, 0);
+}
+
+// CHECK: WARNING: ThreadSanitizer: data race
+// CHECK:   Location is TLS of main thread.

Added: compiler-rt/trunk/lib/tsan/lit_tests/tls_race2.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/tls_race2.cc?rev=172393&view=auto
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/tls_race2.cc (added)
+++ compiler-rt/trunk/lib/tsan/lit_tests/tls_race2.cc Mon Jan 14 04:00:03 2013
@@ -0,0 +1,28 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
+#include <pthread.h>
+#include <stddef.h>
+#include <unistd.h>
+
+void *Thread2(void *a) {
+  *(int*)a = 43;
+  return 0;
+}
+
+void *Thread(void *a) {
+  static __thread int Var = 42;
+  pthread_t t;
+  pthread_create(&t, 0, Thread2, &Var);
+  Var = 42;
+  pthread_join(t, 0);
+  return 0;
+}
+
+int main() {
+  pthread_t t;
+  pthread_create(&t, 0, Thread, 0);
+  pthread_join(t, 0);
+}
+
+// CHECK: WARNING: ThreadSanitizer: data race
+// CHECK:   Location is TLS of thread T1.
+

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Mon Jan 14 04:00:03 2013
@@ -1679,6 +1679,7 @@
               (uptr)sigactions[sig].sa_sigaction :
               (uptr)sigactions[sig].sa_handler;
           stack.Init(&pc, 1);
+          Lock l(&ctx->thread_mtx);
           ScopedReport rep(ReportTypeErrnoInSignal);
           if (!IsFiredSuppression(ctx, rep, stack)) {
             rep.AddStack(&stack);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc Mon Jan 14 04:00:03 2013
@@ -48,6 +48,7 @@
   Context *ctx = CTX();
   StackTrace stack;
   stack.ObtainCurrent(thr, pc);
+  Lock l(&ctx->thread_mtx);
   ScopedReport rep(ReportTypeSignalUnsafe);
   if (!IsFiredSuppression(ctx, rep, stack)) {
     rep.AddStack(&stack);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc Mon Jan 14 04:00:03 2013
@@ -112,7 +112,9 @@
         loc->size, loc->addr, thread_name(thrbuf, loc->tid));
     PrintStack(loc->stack);
   } else if (loc->type == ReportLocationStack) {
-    Printf("  Location is stack of %s\n\n", thread_name(thrbuf, loc->tid));
+    Printf("  Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
+  } else if (loc->type == ReportLocationTLS) {
+    Printf("  Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
   } else if (loc->type == ReportLocationFD) {
     Printf("  Location is file descriptor %d created by %s at:\n",
         loc->fd, thread_name(thrbuf, loc->tid));

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_report.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_report.h?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_report.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_report.h Mon Jan 14 04:00:03 2013
@@ -58,6 +58,7 @@
   ReportLocationGlobal,
   ReportLocationHeap,
   ReportLocationStack,
+  ReportLocationTLS,
   ReportLocationFD
 };
 

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc Mon Jan 14 04:00:03 2013
@@ -55,6 +55,7 @@
       && s->owner_tid != SyncVar::kInvalidTid
       && !s->is_broken) {
     s->is_broken = true;
+    Lock l(&ctx->thread_mtx);
     ScopedReport rep(ReportTypeMutexDestroyLocked);
     rep.AddMutex(s);
     StackTrace trace;

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc?rev=172393&r1=172392&r2=172393&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_report.cc Mon Jan 14 04:00:03 2013
@@ -121,6 +121,7 @@
 
 ScopedReport::ScopedReport(ReportType typ) {
   ctx_ = CTX();
+  ctx_->thread_mtx.CheckLocked();
   void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
   rep_ = new(mem) ReportDesc;
   rep_->typ = typ;
@@ -187,15 +188,37 @@
 
 #ifndef TSAN_GO
 static ThreadContext *FindThread(int unique_id) {
-  CTX()->thread_mtx.CheckLocked();
+  Context *ctx = CTX();
+  ctx->thread_mtx.CheckLocked();
   for (unsigned i = 0; i < kMaxTid; i++) {
-    ThreadContext *tctx = CTX()->threads[i];
+    ThreadContext *tctx = ctx->threads[i];
     if (tctx && tctx->unique_id == unique_id) {
       return tctx;
     }
   }
   return 0;
 }
+
+ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
+  Context *ctx = CTX();
+  ctx->thread_mtx.CheckLocked();
+  for (unsigned i = 0; i < kMaxTid; i++) {
+    ThreadContext *tctx = ctx->threads[i];
+    if (tctx == 0 || tctx->status != ThreadStatusRunning)
+      continue;
+    ThreadState *thr = tctx->thr;
+    CHECK(thr);
+    if (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) {
+      *is_stack = true;
+      return tctx;
+    }
+    if (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size) {
+      *is_stack = false;
+      return tctx;
+    }
+  }
+  return 0;
+}
 #endif
 
 void ScopedReport::AddMutex(const SyncVar *s) {
@@ -276,12 +299,21 @@
       AddThread(tctx);
     return;
   }
-#endif
+  bool is_stack = false;
+  if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
+    void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
+    ReportLocation *loc = new(mem) ReportLocation();
+    rep_->locs.PushBack(loc);
+    loc->type = is_stack ? ReportLocationStack : ReportLocationTLS;
+    loc->tid = tctx->tid;
+    AddThread(tctx);
+  }
   ReportLocation *loc = SymbolizeData(addr);
   if (loc) {
     rep_->locs.PushBack(loc);
     return;
   }
+#endif
 }
 
 #ifndef TSAN_GO





More information about the llvm-commits mailing list