[compiler-rt] r207208 - tsan: improve "read unlock of a write locked mutex" report

Dmitry Vyukov dvyukov at google.com
Fri Apr 25 01:21:31 PDT 2014


Author: dvyukov
Date: Fri Apr 25 03:21:30 2014
New Revision: 207208

URL: http://llvm.org/viewvc/llvm-project?rev=207208&view=rev
Log:
tsan: improve "read unlock of a write locked mutex" report


Added:
    compiler-rt/trunk/test/tsan/mutex_bad_read_unlock.cc
Modified:
    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

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=207208&r1=207207&r2=207208&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_report.cc Fri Apr 25 03:21:30 2014
@@ -78,6 +78,8 @@ static const char *ReportTypeString(Repo
     return "double lock of a mutex";
   if (typ == ReportTypeMutexBadUnlock)
     return "unlock of an unlocked mutex (or by a wrong thread)";
+  if (typ == ReportTypeMutexBadReadUnlock)
+    return "read unlock of a write locked mutex";
   if (typ == ReportTypeSignalUnsafe)
     return "signal-unsafe call inside of a signal";
   if (typ == ReportTypeErrnoInSignal)

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=207208&r1=207207&r2=207208&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_report.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_report.h Fri Apr 25 03:21:30 2014
@@ -26,6 +26,7 @@ enum ReportType {
   ReportTypeMutexDestroyLocked,
   ReportTypeMutexDoubleLock,
   ReportTypeMutexBadUnlock,
+  ReportTypeMutexBadReadUnlock,
   ReportTypeSignalUnsafe,
   ReportTypeErrnoInSignal,
   ReportTypeDeadlock

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=207208&r1=207207&r2=207208&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_mutex.cc Fri Apr 25 03:21:30 2014
@@ -249,18 +249,32 @@ void MutexReadUnlock(ThreadState *thr, u
   SyncVar *s = ctx->synctab.GetOrCreateAndLock(thr, pc, addr, true);
   thr->fast_state.IncrementEpoch();
   TraceAddEvent(thr, thr->fast_state, EventTypeRUnlock, s->GetId());
+  bool report_bad_unlock = false;
   if (s->owner_tid != SyncVar::kInvalidTid) {
-    Printf("ThreadSanitizer WARNING: read unlock of a write locked mutex %p\n",
-           addr);
-    PrintCurrentStack(thr, pc);
+    if (flags()->report_mutex_bugs && !s->is_broken) {
+      s->is_broken = true;
+      report_bad_unlock = true;
+    }
   }
   ReleaseImpl(thr, pc, &s->read_clock);
   if (flags()->detect_deadlocks && s->recursion == 0) {
     Callback cb(thr, pc);
     ctx->dd->MutexBeforeUnlock(&cb, &s->dd, false);
   }
+  u64 mid = s->GetId();
   s->mtx.Unlock();
-  thr->mset.Del(s->GetId(), false);
+  // Can't touch s after this point.
+  thr->mset.Del(mid, false);
+  if (report_bad_unlock) {
+    ThreadRegistryLock l(ctx->thread_registry);
+    ScopedReport rep(ReportTypeMutexBadReadUnlock);
+    rep.AddMutex(mid);
+    StackTrace trace;
+    trace.ObtainCurrent(thr, pc);
+    rep.AddStack(&trace);
+    rep.AddLocation(addr, 1);
+    OutputReport(ctx, rep);
+  }
   if (flags()->detect_deadlocks) {
     Callback cb(thr, pc);
     ReportDeadlock(thr, pc, ctx->dd->GetReport(&cb));

Added: compiler-rt/trunk/test/tsan/mutex_bad_read_unlock.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/mutex_bad_read_unlock.cc?rev=207208&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/mutex_bad_read_unlock.cc (added)
+++ compiler-rt/trunk/test/tsan/mutex_bad_read_unlock.cc Fri Apr 25 03:21:30 2014
@@ -0,0 +1,20 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && not %t 2>&1 | FileCheck %s
+extern "C" void AnnotateRWLockAcquired(const char *f, int l, void *m, long rw);
+extern "C" void AnnotateRWLockReleased(const char *f, int l, void *m, long rw);
+
+int main() {
+  int m = 0;
+  AnnotateRWLockAcquired(__FILE__, __LINE__, &m, 1);
+  AnnotateRWLockReleased(__FILE__, __LINE__, &m, 0);
+  return 0;
+}
+
+// CHECK: WARNING: ThreadSanitizer: read unlock of a write locked mutex
+// CHECK:     #0 AnnotateRWLockReleased
+// CHECK:     #1 main
+// CHECK: Location is stack of main thread.
+// CHECK:   Mutex {{.*}}) created at:
+// CHECK:     #0 AnnotateRWLockAcquired
+// CHECK:     #1 main
+// CHECK: SUMMARY: ThreadSanitizer: read unlock of a write locked mutex
+





More information about the llvm-commits mailing list