[compiler-rt] r286211 - tsan: more precise handling of atomic stores

Dmitry Vyukov via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 7 21:34:51 PST 2016


Author: dvyukov
Date: Mon Nov  7 23:34:50 2016
New Revision: 286211

URL: http://llvm.org/viewvc/llvm-project?rev=286211&view=rev
Log:
tsan: more precise handling of atomic stores

Atomic stores terminate release sequences on the atomic variable,
and must use ReleaseStore primitive instead of Release.
This was broken in r192355 during a refactoring.
Restore correct behavior and add a test.


Added:
    compiler-rt/trunk/test/tsan/atomic_store.cc
Modified:
    compiler-rt/trunk/lib/tsan/rtl/tsan_interface_atomic.cc

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interface_atomic.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interface_atomic.cc?rev=286211&r1=286210&r2=286211&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interface_atomic.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interface_atomic.cc Mon Nov  7 23:34:50 2016
@@ -267,7 +267,7 @@ static void AtomicStore(ThreadState *thr
   thr->fast_state.IncrementEpoch();
   // Can't increment epoch w/o writing to the trace as well.
   TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
-  ReleaseImpl(thr, pc, &s->clock);
+  ReleaseStoreImpl(thr, pc, &s->clock);
   NoTsanAtomicStore(a, v, mo);
   s->mtx.Unlock();
 }

Added: compiler-rt/trunk/test/tsan/atomic_store.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/atomic_store.cc?rev=286211&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/atomic_store.cc (added)
+++ compiler-rt/trunk/test/tsan/atomic_store.cc Mon Nov  7 23:34:50 2016
@@ -0,0 +1,49 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
+#include "test.h"
+
+long long Data;
+long long Sync;
+
+void *Thread1(void *x) {
+  Data++;
+  __atomic_store_n(&Sync, 1, __ATOMIC_RELEASE);
+  barrier_wait(&barrier);
+  barrier_wait(&barrier);
+  return NULL;
+}
+
+void *Thread2(void *x) {
+  barrier_wait(&barrier);
+  if (__atomic_load_n(&Sync, __ATOMIC_RELAXED) != 1)
+    exit(0);
+  // This store must terminate release sequence of the store in Thread1,
+  // thus tsan must detect race between Thread1 and main on Data.
+  __atomic_store_n(&Sync, 2, __ATOMIC_RELEASE);
+  barrier_wait(&barrier);
+  return NULL;
+}
+
+int main() {
+  barrier_init(&barrier, 3);
+  pthread_t t[2];
+  pthread_create(&t[0], NULL, Thread1, NULL);
+  pthread_create(&t[1], NULL, Thread2, NULL);
+  barrier_wait(&barrier);
+  barrier_wait(&barrier);
+  if (__atomic_load_n(&Sync, __ATOMIC_ACQUIRE) != 2)
+    exit(0);
+  if (Data != 1)
+    exit(0);
+  pthread_join(t[0], NULL);
+  pthread_join(t[1], NULL);
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: WARNING: ThreadSanitizer: data race
+// CHECK:   Read
+// CHECK:     #0 main
+// CHECK:   Previous write
+// CHECK:     #0 Thread1
+// CHECK:   Location is global 'Data'
+// CHECK: DONE




More information about the llvm-commits mailing list