[compiler-rt] r305281 - tsan: fix reading of mutex flags

Dmitry Vyukov via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 13 02:37:52 PDT 2017


Author: dvyukov
Date: Tue Jun 13 04:37:51 2017
New Revision: 305281

URL: http://llvm.org/viewvc/llvm-project?rev=305281&view=rev
Log:
tsan: fix reading of mutex flags

SyncVar::IsFlagSet returns true if any flag is set.
This is wrong. Check the actual requested flag.


Added:
    compiler-rt/trunk/test/tsan/custom_mutex3.cc
Modified:
    compiler-rt/trunk/lib/tsan/rtl/tsan_sync.h
    compiler-rt/trunk/test/tsan/custom_mutex.h
    compiler-rt/trunk/test/tsan/custom_mutex0.cc
    compiler-rt/trunk/test/tsan/custom_mutex1.cc
    compiler-rt/trunk/test/tsan/custom_mutex2.cc

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_sync.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_sync.h?rev=305281&r1=305280&r2=305281&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_sync.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_sync.h Tue Jun 13 04:37:51 2017
@@ -83,7 +83,7 @@ struct SyncVar {
   }
 
   bool IsFlagSet(u32 f) const {
-    return atomic_load_relaxed(&flags);
+    return atomic_load_relaxed(&flags) & f;
   }
 
   void SetFlags(u32 f) {

Modified: compiler-rt/trunk/test/tsan/custom_mutex.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/custom_mutex.h?rev=305281&r1=305280&r2=305281&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/custom_mutex.h (original)
+++ compiler-rt/trunk/test/tsan/custom_mutex.h Tue Jun 13 04:37:51 2017
@@ -6,11 +6,11 @@
 // A very primitive mutex annotated with tsan annotations.
 class Mutex {
  public:
-  Mutex(bool prof = true)
+  Mutex(bool prof, unsigned flags)
       : prof_(prof)
       , locked_(false)
       , seq_(0) {
-    __tsan_mutex_create(this, 0);
+    __tsan_mutex_create(this, flags);
   }
 
   ~Mutex() {
@@ -87,5 +87,5 @@ class Mutex {
   }
 };
 
-Mutex Mutex::prof_mu_(false);
+Mutex Mutex::prof_mu_(false, __tsan_mutex_linker_init);
 int Mutex::prof_data_;

Modified: compiler-rt/trunk/test/tsan/custom_mutex0.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/custom_mutex0.cc?rev=305281&r1=305280&r2=305281&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/custom_mutex0.cc (original)
+++ compiler-rt/trunk/test/tsan/custom_mutex0.cc Tue Jun 13 04:37:51 2017
@@ -4,7 +4,7 @@
 // Test that custom annoations provide normal mutex synchronization
 // (no race reports for properly protected critical sections).
 
-Mutex mu;
+Mutex mu(true, 0);
 long data;
 
 void *thr(void *arg) {

Modified: compiler-rt/trunk/test/tsan/custom_mutex1.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/custom_mutex1.cc?rev=305281&r1=305280&r2=305281&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/custom_mutex1.cc (original)
+++ compiler-rt/trunk/test/tsan/custom_mutex1.cc Tue Jun 13 04:37:51 2017
@@ -3,7 +3,7 @@
 
 // Test that failed TryLock does not induce parasitic synchronization.
 
-Mutex mu;
+Mutex mu(true, 0);
 long data;
 
 void *thr(void *arg) {

Modified: compiler-rt/trunk/test/tsan/custom_mutex2.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/custom_mutex2.cc?rev=305281&r1=305280&r2=305281&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/custom_mutex2.cc (original)
+++ compiler-rt/trunk/test/tsan/custom_mutex2.cc Tue Jun 13 04:37:51 2017
@@ -3,7 +3,7 @@
 
 // Test that Broadcast does not induce parasitic synchronization.
 
-Mutex mu;
+Mutex mu(true, 0);
 long data;
 
 void *thr(void *arg) {

Added: compiler-rt/trunk/test/tsan/custom_mutex3.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/custom_mutex3.cc?rev=305281&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/custom_mutex3.cc (added)
+++ compiler-rt/trunk/test/tsan/custom_mutex3.cc Tue Jun 13 04:37:51 2017
@@ -0,0 +1,46 @@
+// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t
+// RUN: %env_tsan_opts=report_destroy_locked=0 %run %t 2>&1 | FileCheck %s
+#include "custom_mutex.h"
+
+// Regression test for a bug.
+// Thr1 destroys a locked mutex, previously such mutex was not removed from
+// sync map and as the result subsequent uses of a mutex located at the same
+// address caused false race reports.
+
+Mutex mu(false, __tsan_mutex_write_reentrant);
+long data;
+
+void *thr1(void *arg) {
+  mu.Lock();
+  mu.~Mutex();
+  new(&mu) Mutex(true, __tsan_mutex_write_reentrant);
+  return 0;
+}
+
+void *thr2(void *arg) {
+  barrier_wait(&barrier);
+  mu.Lock();
+  data++;
+  mu.Unlock();
+  return 0;
+}
+
+int main() {
+  barrier_init(&barrier, 2);
+  pthread_t th;
+  pthread_create(&th, 0, thr1, 0);
+  pthread_join(th, 0);
+
+  barrier_init(&barrier, 2);
+  pthread_create(&th, 0, thr2, 0);
+  mu.Lock();
+  data++;
+  mu.Unlock();
+  barrier_wait(&barrier);
+  pthread_join(th, 0);
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer: data race
+// CHECK: DONE




More information about the llvm-commits mailing list