diff --git a/compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp b/compiler-rt/lib/tsan/tests/rtl/tsan_mop.cpp --- 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 #include -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 @@ t2.Read1(l, true); } -TEST(ThreadSanitizer, WriteThenLockedRead) { +TEST_F(ThreadSanitizer, WriteThenLockedRead) { UserMutex m(UserMutex::RW); MainThread t0; t0.Create(m); @@ -83,7 +83,7 @@ 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, RaceWithOffset) { +TEST_F(ThreadSanitizer, RaceWithOffset) { ScopedThread t1, t2; { MemLoc l; @@ -137,7 +137,7 @@ } } -TEST(ThreadSanitizer, RaceWithOffset2) { +TEST_F(ThreadSanitizer, RaceWithOffset2) { ScopedThread t1, t2; { MemLoc l; @@ -151,7 +151,7 @@ } } -TEST(ThreadSanitizer, NoRaceWithOffset) { +TEST_F(ThreadSanitizer, NoRaceWithOffset) { ScopedThread t1, t2; { MemLoc l; @@ -166,14 +166,14 @@ } } -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 @@ 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 @@ (void)x2; } -TEST(ThreadSanitizer, ReportDeadThread) { +TEST_F(ThreadSanitizer, ReportDeadThread) { MemLoc l; ScopedThread t1; { @@ -223,7 +223,7 @@ 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 --- 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 @@ t.Destroy(m); } -TEST(ThreadSanitizer, BasicSpinMutex) { +TEST_F(ThreadSanitizer, BasicSpinMutex) { ScopedThread t; UserMutex m(UserMutex::Spin); t.Create(m); @@ -54,7 +54,7 @@ t.Destroy(m); } -TEST(ThreadSanitizer, BasicRwMutex) { +TEST_F(ThreadSanitizer, BasicRwMutex) { ScopedThread t; UserMutex m(UserMutex::RW); t.Create(m); @@ -91,7 +91,7 @@ t.Destroy(m); } -TEST(ThreadSanitizer, Mutex) { +TEST_F(ThreadSanitizer, Mutex) { UserMutex m; MainThread t0; t0.Create(m); @@ -107,7 +107,7 @@ t2.Destroy(m); } -TEST(ThreadSanitizer, SpinMutex) { +TEST_F(ThreadSanitizer, SpinMutex) { UserMutex m(UserMutex::Spin); MainThread t0; t0.Create(m); @@ -123,7 +123,7 @@ t2.Destroy(m); } -TEST(ThreadSanitizer, RwMutex) { +TEST_F(ThreadSanitizer, RwMutex) { UserMutex m(UserMutex::RW); MainThread t0; t0.Create(m); @@ -148,7 +148,7 @@ 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 --- 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 --- 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 --- 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 --- 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 #include #include @@ -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 @@ 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 @@ 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 @@ expect_report(), report_type() {} - void ExpectReport(ReportType type) { + void ExpectReport(__tsan::ReportType type) { expect_report = true; report_type = type; } @@ -231,7 +232,7 @@ 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 @@ __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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 --- 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 @@ 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);