[compiler-rt] a61c8e1 - tsan: for unittests, change to use test fixtures to clear racy stacks

Yuanfang Chen via llvm-commits llvm-commits at lists.llvm.org
Tue May 3 10:18:15 PDT 2022


Author: Yuanfang Chen
Date: 2022-05-03T10:18:00-07:00
New Revision: a61c8e1ebdff2111a394feac168426e0d962a134

URL: https://github.com/llvm/llvm-project/commit/a61c8e1ebdff2111a394feac168426e0d962a134
DIFF: https://github.com/llvm/llvm-project/commit/a61c8e1ebdff2111a394feac168426e0d962a134.diff

LOG: tsan: for unittests, change to use test fixtures to clear racy stacks

After cd0a5889d71, unittest would run in shard mode where many tests
share a single process. Need to clear some global state to make the test
results stable.

Reviewed By: thetruestblue, rsundahl

Differential Revision: https://reviews.llvm.org/D124591

Added: 
    

Modified: 
    compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp
    compiler-rt/lib/tsan/tests/rtl/tsan_mutex.cpp
    compiler-rt/lib/tsan/tests/rtl/tsan_string.cpp
    compiler-rt/lib/tsan/tests/rtl/tsan_test.cpp
    compiler-rt/lib/tsan/tests/rtl/tsan_test_util.h
    compiler-rt/lib/tsan/tests/rtl/tsan_test_util_posix.cpp
    compiler-rt/lib/tsan/tests/rtl/tsan_thread.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp
index b91aead3dd74e..29f0964197ba3 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp
@@ -15,48 +15,48 @@
 #include <stddef.h>
 #include <stdint.h>
 
-TEST(ThreadSanitizer, SimpleWrite) {
+TEST_F(ThreadSanitizer, SimpleWrite) {
   ScopedThread t;
   MemLoc l;
   t.Write1(l);
 }
 
-TEST(ThreadSanitizer, SimpleWriteWrite) {
+TEST_F(ThreadSanitizer, SimpleWriteWrite) {
   ScopedThread t1, t2;
   MemLoc l1, l2;
   t1.Write1(l1);
   t2.Write1(l2);
 }
 
-TEST(ThreadSanitizer, WriteWriteRace) {
+TEST_F(ThreadSanitizer, WriteWriteRace) {
   ScopedThread t1, t2;
   MemLoc l;
   t1.Write1(l);
   t2.Write1(l, true);
 }
 
-TEST(ThreadSanitizer, ReadWriteRace) {
+TEST_F(ThreadSanitizer, ReadWriteRace) {
   ScopedThread t1, t2;
   MemLoc l;
   t1.Read1(l);
   t2.Write1(l, true);
 }
 
-TEST(ThreadSanitizer, WriteReadRace) {
+TEST_F(ThreadSanitizer, WriteReadRace) {
   ScopedThread t1, t2;
   MemLoc l;
   t1.Write1(l);
   t2.Read1(l, true);
 }
 
-TEST(ThreadSanitizer, ReadReadNoRace) {
+TEST_F(ThreadSanitizer, ReadReadNoRace) {
   ScopedThread t1, t2;
   MemLoc l;
   t1.Read1(l);
   t2.Read1(l);
 }
 
-TEST(ThreadSanitizer, WriteThenRead) {
+TEST_F(ThreadSanitizer, WriteThenRead) {
   MemLoc l;
   ScopedThread t1, t2;
   t1.Write1(l);
@@ -64,7 +64,7 @@ TEST(ThreadSanitizer, WriteThenRead) {
   t2.Read1(l, true);
 }
 
-TEST(ThreadSanitizer, WriteThenLockedRead) {
+TEST_F(ThreadSanitizer, WriteThenLockedRead) {
   UserMutex m(UserMutex::RW);
   MainThread t0;
   t0.Create(m);
@@ -83,7 +83,7 @@ TEST(ThreadSanitizer, WriteThenLockedRead) {
   t0.Destroy(m);
 }
 
-TEST(ThreadSanitizer, LockedWriteThenRead) {
+TEST_F(ThreadSanitizer, LockedWriteThenRead) {
   UserMutex m(UserMutex::RW);
   MainThread t0;
   t0.Create(m);
@@ -103,7 +103,7 @@ TEST(ThreadSanitizer, LockedWriteThenRead) {
 }
 
 
-TEST(ThreadSanitizer, RaceWithOffset) {
+TEST_F(ThreadSanitizer, RaceWithOffset) {
   ScopedThread t1, t2;
   {
     MemLoc l;
@@ -137,7 +137,7 @@ TEST(ThreadSanitizer, RaceWithOffset) {
   }
 }
 
-TEST(ThreadSanitizer, RaceWithOffset2) {
+TEST_F(ThreadSanitizer, RaceWithOffset2) {
   ScopedThread t1, t2;
   {
     MemLoc l;
@@ -151,7 +151,7 @@ TEST(ThreadSanitizer, RaceWithOffset2) {
   }
 }
 
-TEST(ThreadSanitizer, NoRaceWithOffset) {
+TEST_F(ThreadSanitizer, NoRaceWithOffset) {
   ScopedThread t1, t2;
   {
     MemLoc l;
@@ -166,14 +166,14 @@ TEST(ThreadSanitizer, NoRaceWithOffset) {
   }
 }
 
-TEST(ThreadSanitizer, RaceWithDeadThread) {
+TEST_F(ThreadSanitizer, RaceWithDeadThread) {
   MemLoc l;
   ScopedThread t;
   ScopedThread().Write1(l);
   t.Write1(l, true);
 }
 
-TEST(ThreadSanitizer, BenignRaceOnVptr) {
+TEST_F(ThreadSanitizer, BenignRaceOnVptr) {
   void *vptr_storage;
   MemLoc vptr(&vptr_storage), val;
   vptr_storage = val.loc();
@@ -182,7 +182,7 @@ TEST(ThreadSanitizer, BenignRaceOnVptr) {
   t2.Read8(vptr);
 }
 
-TEST(ThreadSanitizer, HarmfulRaceOnVptr) {
+TEST_F(ThreadSanitizer, HarmfulRaceOnVptr) {
   void *vptr_storage;
   MemLoc vptr(&vptr_storage), val1, val2;
   vptr_storage = val1.loc();
@@ -203,7 +203,7 @@ static void bar() {
   (void)x2;
 }
 
-TEST(ThreadSanitizer, ReportDeadThread) {
+TEST_F(ThreadSanitizer, ReportDeadThread) {
   MemLoc l;
   ScopedThread t1;
   {
@@ -223,7 +223,7 @@ int ClassWithStatic::Data[4];
 
 static void foobarbaz() {}
 
-TEST(ThreadSanitizer, ReportRace) {
+TEST_F(ThreadSanitizer, ReportRace) {
   ScopedThread t1;
   MainThread().Access(&ClassWithStatic::Data, true, 4, false);
   t1.Call(&foobarbaz);

diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_mutex.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_mutex.cpp
index 663811ecd760c..d45014b2ec86a 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_mutex.cpp
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_mutex.cpp
@@ -18,7 +18,7 @@
 
 namespace __tsan {
 
-TEST(ThreadSanitizer, BasicMutex) {
+TEST_F(ThreadSanitizer, BasicMutex) {
   ScopedThread t;
   UserMutex m;
   t.Create(m);
@@ -36,7 +36,7 @@ TEST(ThreadSanitizer, BasicMutex) {
   t.Destroy(m);
 }
 
-TEST(ThreadSanitizer, BasicSpinMutex) {
+TEST_F(ThreadSanitizer, BasicSpinMutex) {
   ScopedThread t;
   UserMutex m(UserMutex::Spin);
   t.Create(m);
@@ -54,7 +54,7 @@ TEST(ThreadSanitizer, BasicSpinMutex) {
   t.Destroy(m);
 }
 
-TEST(ThreadSanitizer, BasicRwMutex) {
+TEST_F(ThreadSanitizer, BasicRwMutex) {
   ScopedThread t;
   UserMutex m(UserMutex::RW);
   t.Create(m);
@@ -91,7 +91,7 @@ TEST(ThreadSanitizer, BasicRwMutex) {
   t.Destroy(m);
 }
 
-TEST(ThreadSanitizer, Mutex) {
+TEST_F(ThreadSanitizer, Mutex) {
   UserMutex m;
   MainThread t0;
   t0.Create(m);
@@ -107,7 +107,7 @@ TEST(ThreadSanitizer, Mutex) {
   t2.Destroy(m);
 }
 
-TEST(ThreadSanitizer, SpinMutex) {
+TEST_F(ThreadSanitizer, SpinMutex) {
   UserMutex m(UserMutex::Spin);
   MainThread t0;
   t0.Create(m);
@@ -123,7 +123,7 @@ TEST(ThreadSanitizer, SpinMutex) {
   t2.Destroy(m);
 }
 
-TEST(ThreadSanitizer, RwMutex) {
+TEST_F(ThreadSanitizer, RwMutex) {
   UserMutex m(UserMutex::RW);
   MainThread t0;
   t0.Create(m);
@@ -148,7 +148,7 @@ TEST(ThreadSanitizer, RwMutex) {
   t2.Destroy(m);
 }
 
-TEST(ThreadSanitizer, StaticMutex) {
+TEST_F(ThreadSanitizer, StaticMutex) {
   // Emulates statically initialized mutex.
   UserMutex m;
   m.StaticInit();

diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_string.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_string.cpp
index 4c31389298a95..91e240fd05fec 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_string.cpp
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_string.cpp
@@ -15,7 +15,7 @@
 
 namespace __tsan {
 
-TEST(ThreadSanitizer, Memcpy) {
+TEST_F(ThreadSanitizer, Memcpy) {
   char data0[7] = {1, 2, 3, 4, 5, 6, 7};
   char data[7] = {42, 42, 42, 42, 42, 42, 42};
   MainThread().Memcpy(data+1, data0+1, 5);
@@ -36,7 +36,7 @@ TEST(ThreadSanitizer, Memcpy) {
   EXPECT_EQ(data[6], 42);
 }
 
-TEST(ThreadSanitizer, MemcpyRace1) {
+TEST_F(ThreadSanitizer, MemcpyRace1) {
   char *data = new char[10];
   char *data1 = new char[10];
   char *data2 = new char[10];
@@ -45,7 +45,7 @@ TEST(ThreadSanitizer, MemcpyRace1) {
   t2.Memcpy(data, data2, 10, true);
 }
 
-TEST(ThreadSanitizer, MemcpyRace2) {
+TEST_F(ThreadSanitizer, MemcpyRace2) {
   char *data = new char[10];
   char *data1 = new char[10];
   char *data2 = new char[10];
@@ -54,7 +54,7 @@ TEST(ThreadSanitizer, MemcpyRace2) {
   t2.Memcpy(data+3, data2, 4, true);
 }
 
-TEST(ThreadSanitizer, MemcpyRace3) {
+TEST_F(ThreadSanitizer, MemcpyRace3) {
   char *data = new char[10];
   char *data1 = new char[10];
   char *data2 = new char[10];
@@ -63,7 +63,7 @@ TEST(ThreadSanitizer, MemcpyRace3) {
   t2.Memcpy(data1, data2, 10, true);
 }
 
-TEST(ThreadSanitizer, MemcpyStack) {
+TEST_F(ThreadSanitizer, MemcpyStack) {
   char *data = new char[10];
   char *data1 = new char[10];
   ScopedThread t1, t2;
@@ -71,7 +71,7 @@ TEST(ThreadSanitizer, MemcpyStack) {
   t2.Memcpy(data, data1, 10, true);
 }
 
-TEST(ThreadSanitizer, MemsetRace1) {
+TEST_F(ThreadSanitizer, MemsetRace1) {
   char *data = new char[10];
   ScopedThread t1, t2;
   t1.Memset(data, 1, 10);

diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_test.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_test.cpp
index 84e6bbcfe4744..594c3ebfeb738 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_test.cpp
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_test.cpp
@@ -16,7 +16,7 @@
 static void foo() {}
 static void bar() {}
 
-TEST(ThreadSanitizer, FuncCall) {
+TEST_F(ThreadSanitizer, FuncCall) {
   ScopedThread t1, t2;
   MemLoc l;
   t1.Write1(l);

diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_test_util.h b/compiler-rt/lib/tsan/tests/rtl/tsan_test_util.h
index b2d766ad8e9ba..7b7aea2430a90 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_test_util.h
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_test_util.h
@@ -13,6 +13,13 @@
 #ifndef TSAN_TEST_UTIL_H
 #define TSAN_TEST_UTIL_H
 
+#include "gtest/gtest.h"
+
+class ThreadSanitizer : public ::testing::Test {
+ protected:
+  void TearDown() override;
+};
+
 void TestMutexBeforeInit();
 
 // A location of memory on which a race may be detected.

diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_test_util_posix.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_test_util_posix.cpp
index 91fed384475ee..8aa0813b11f0d 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_test_util_posix.cpp
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_test_util_posix.cpp
@@ -14,11 +14,10 @@
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "tsan_interface.h"
 #include "tsan_posix_util.h"
+#include "tsan_rtl.h"
 #include "tsan_test_util.h"
 #include "tsan_report.h"
 
-#include "gtest/gtest.h"
-
 #include <assert.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -29,11 +28,13 @@
 
 #define CALLERPC (__builtin_return_address(0))
 
-using namespace __tsan;
-
 static __thread bool expect_report;
 static __thread bool expect_report_reported;
-static __thread ReportType expect_report_type;
+static __thread __tsan::ReportType expect_report_type;
+
+void ThreadSanitizer::TearDown() {
+  __tsan::ctx->racy_stacks.Reset();
+}
 
 static void *BeforeInitThread(void *param) {
   (void)param;
@@ -75,11 +76,11 @@ bool OnReport(const ReportDesc *rep, bool suppressed) {
 
 static void* allocate_addr(int size, int offset_from_aligned = 0) {
   static uintptr_t foo;
-  static atomic_uintptr_t uniq = {(uintptr_t)&foo};  // Some real address.
+  static __tsan::atomic_uintptr_t uniq = {(uintptr_t)&foo}; // Some real address.
   const int kAlign = 16;
   CHECK(offset_from_aligned < kAlign);
   size = (size + 2 * kAlign) & ~(kAlign - 1);
-  uintptr_t addr = atomic_fetch_add(&uniq, size, memory_order_relaxed);
+  uintptr_t addr = atomic_fetch_add(&uniq, size, __tsan::memory_order_relaxed);
   return (void*)(addr + offset_from_aligned);
 }
 
@@ -210,7 +211,7 @@ struct Event {
   uptr arg2;
   bool res;
   bool expect_report;
-  ReportType report_type;
+  __tsan::ReportType report_type;
 
   explicit Event(Type type, const void *ptr = 0, uptr arg = 0, uptr arg2 = 0)
       : type(type),
@@ -221,7 +222,7 @@ struct Event {
         expect_report(),
         report_type() {}
 
-  void ExpectReport(ReportType type) {
+  void ExpectReport(__tsan::ReportType type) {
     expect_report = true;
     report_type = type;
   }
@@ -231,7 +232,7 @@ struct ScopedThread::Impl {
   pthread_t thread;
   bool main;
   bool detached;
-  atomic_uintptr_t event;  // Event*
+  __tsan::atomic_uintptr_t event;  // Event*
 
   static void *ScopedThreadCallback(void *arg);
   void send(Event *ev);
@@ -347,17 +348,18 @@ void *ScopedThread::Impl::ScopedThreadCallback(void *arg) {
   __tsan_func_entry(CALLERPC);
   Impl *impl = (Impl*)arg;
   for (;;) {
-    Event* ev = (Event*)atomic_load(&impl->event, memory_order_acquire);
+    Event *ev =
+        (Event *)atomic_load(&impl->event, __tsan::memory_order_acquire);
     if (ev == 0) {
       sched_yield();
       continue;
     }
     if (ev->type == Event::SHUTDOWN) {
-      atomic_store(&impl->event, 0, memory_order_release);
+      atomic_store(&impl->event, 0, __tsan::memory_order_release);
       break;
     }
     impl->HandleEvent(ev);
-    atomic_store(&impl->event, 0, memory_order_release);
+    atomic_store(&impl->event, 0, __tsan::memory_order_release);
   }
   __tsan_func_exit();
   return 0;
@@ -367,9 +369,9 @@ void ScopedThread::Impl::send(Event *e) {
   if (main) {
     HandleEvent(e);
   } else {
-    CHECK_EQ(atomic_load(&event, memory_order_relaxed), 0);
-    atomic_store(&event, (uintptr_t)e, memory_order_release);
-    while (atomic_load(&event, memory_order_acquire) != 0)
+    CHECK_EQ(atomic_load(&event, __tsan::memory_order_relaxed), 0);
+    atomic_store(&event, (uintptr_t)e, __tsan::memory_order_release);
+    while (atomic_load(&event, __tsan::memory_order_acquire) != 0)
       sched_yield();
   }
 }
@@ -378,7 +380,7 @@ ScopedThread::ScopedThread(bool detached, bool main) {
   impl_ = new Impl;
   impl_->main = main;
   impl_->detached = detached;
-  atomic_store(&impl_->event, 0, memory_order_relaxed);
+  atomic_store(&impl_->event, 0, __tsan::memory_order_relaxed);
   if (!main) {
     pthread_attr_t attr;
     pthread_attr_init(&attr);
@@ -412,7 +414,7 @@ void ScopedThread::Access(void *addr, bool is_write,
   Event event(is_write ? Event::WRITE : Event::READ, addr, size,
               (uptr)CALLERPC);
   if (expect_race)
-    event.ExpectReport(ReportTypeRace);
+    event.ExpectReport(__tsan::ReportTypeRace);
   impl_->send(&event);
 }
 
@@ -421,7 +423,7 @@ void ScopedThread::VptrUpdate(const MemLoc &vptr,
                               bool expect_race) {
   Event event(Event::VPTR_UPDATE, vptr.loc(), (uptr)new_val.loc());
   if (expect_race)
-    event.ExpectReport(ReportTypeRace);
+    event.ExpectReport(__tsan::ReportTypeRace);
   impl_->send(&event);
 }
 
@@ -481,7 +483,7 @@ void ScopedThread::Memcpy(void *dst, const void *src, int size,
                           bool expect_race) {
   Event event(Event::MEMCPY, dst, (uptr)src, size);
   if (expect_race)
-    event.ExpectReport(ReportTypeRace);
+    event.ExpectReport(__tsan::ReportTypeRace);
   impl_->send(&event);
 }
 
@@ -489,6 +491,6 @@ void ScopedThread::Memset(void *dst, int val, int size,
                           bool expect_race) {
   Event event(Event::MEMSET, dst, val, size);
   if (expect_race)
-    event.ExpectReport(ReportTypeRace);
+    event.ExpectReport(__tsan::ReportTypeRace);
   impl_->send(&event);
 }

diff  --git a/compiler-rt/lib/tsan/tests/rtl/tsan_thread.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_thread.cpp
index 9d79e0e9429e2..fcbeaeaf71cf4 100644
--- a/compiler-rt/lib/tsan/tests/rtl/tsan_thread.cpp
+++ b/compiler-rt/lib/tsan/tests/rtl/tsan_thread.cpp
@@ -12,7 +12,7 @@
 #include "tsan_test_util.h"
 #include "gtest/gtest.h"
 
-TEST(ThreadSanitizer, ThreadSync) {
+TEST_F(ThreadSanitizer, ThreadSync) {
   MainThread t0;
   MemLoc l;
   t0.Write1(l);
@@ -23,13 +23,13 @@ TEST(ThreadSanitizer, ThreadSync) {
   t0.Write1(l);
 }
 
-TEST(ThreadSanitizer, ThreadDetach1) {
+TEST_F(ThreadSanitizer, ThreadDetach1) {
   ScopedThread t1(true);
   MemLoc l;
   t1.Write1(l);
 }
 
-TEST(ThreadSanitizer, ThreadDetach2) {
+TEST_F(ThreadSanitizer, ThreadDetach2) {
   ScopedThread t1;
   MemLoc l;
   t1.Write1(l);


        


More information about the llvm-commits mailing list