From 8214764f35e1b764fb939e18f16e11aa43073469 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 10 May 2021 09:04:20 +0200 Subject: [PATCH] tsan: declare annotations in test.h We already declare subset of annotations in test.h. But some are duplicated and declared in tests. Move all annotation declarations to test.h. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D102152 --- compiler-rt/test/tsan/annotate_happens_before.cpp | 16 ---------------- compiler-rt/test/tsan/benign_race.cpp | 16 ++-------------- compiler-rt/test/tsan/ignore_sync.cpp | 6 +----- compiler-rt/test/tsan/mutex_bad_read_lock.cpp | 2 +- compiler-rt/test/tsan/mutex_bad_read_unlock.cpp | 3 +-- compiler-rt/test/tsan/mutex_bad_unlock.cpp | 2 +- compiler-rt/test/tsan/mutex_double_lock.cpp | 5 +---- compiler-rt/test/tsan/mutexset5.cpp | 2 +- compiler-rt/test/tsan/signal_sync2.cpp | 12 +++--------- compiler-rt/test/tsan/test.h | 19 +++++++++++++++++++ compiler-rt/test/tsan/thread_end_with_ignore.cpp | 5 +---- compiler-rt/test/tsan/thread_end_with_ignore2.cpp | 2 +- compiler-rt/test/tsan/thread_end_with_ignore3.cpp | 8 +++----- compiler-rt/test/tsan/thread_name.cpp | 2 -- 14 files changed, 35 insertions(+), 65 deletions(-) diff --git a/compiler-rt/test/tsan/annotate_happens_before.cpp b/compiler-rt/test/tsan/annotate_happens_before.cpp index 0116616..86a8669 100644 --- a/compiler-rt/test/tsan/annotate_happens_before.cpp +++ b/compiler-rt/test/tsan/annotate_happens_before.cpp @@ -7,24 +7,8 @@ Annotations usage example. Tsan does not see synchronization in barrier_wait. ANNOTATE_HAPPENS_BEFORE/AFTER communicate the synchronization to tsan and prevent the race report. - -If the compiler does not support __has_feature macro, then you can build with -CFLAGS="-fsanitize=thread -DTHREAD_SANITIZER" and then use -#ifdef THREAD_SANITIZER to enabled annotations. */ -#if defined(__has_feature) && __has_feature(thread_sanitizer) -# define ANNOTATE_HAPPENS_BEFORE(addr) \ - AnnotateHappensBefore(__FILE__, __LINE__, (void*)(addr)) -# define ANNOTATE_HAPPENS_AFTER(addr) \ - AnnotateHappensAfter(__FILE__, __LINE__, (void*)(addr)) -extern "C" void AnnotateHappensBefore(const char *f, int l, void *addr); -extern "C" void AnnotateHappensAfter(const char *f, int l, void *addr); -#else -# define ANNOTATE_HAPPENS_BEFORE(addr) -# define ANNOTATE_HAPPENS_AFTER(addr) -#endif - int Global; void *Thread1(void *x) { diff --git a/compiler-rt/test/tsan/benign_race.cpp b/compiler-rt/test/tsan/benign_race.cpp index 90722aa..53e820d 100644 --- a/compiler-rt/test/tsan/benign_race.cpp +++ b/compiler-rt/test/tsan/benign_race.cpp @@ -4,15 +4,6 @@ int Global; int WTFGlobal; -extern "C" { -void AnnotateBenignRaceSized(const char *f, int l, - void *mem, unsigned int size, const char *desc); -void WTFAnnotateBenignRaceSized(const char *f, int l, - void *mem, unsigned int size, - const char *desc); -} - - void *Thread(void *x) { Global = 42; WTFGlobal = 142; @@ -22,11 +13,8 @@ void *Thread(void *x) { int main() { barrier_init(&barrier, 2); - AnnotateBenignRaceSized(__FILE__, __LINE__, - &Global, sizeof(Global), "Race on Global"); - WTFAnnotateBenignRaceSized(__FILE__, __LINE__, - &WTFGlobal, sizeof(WTFGlobal), - "Race on WTFGlobal"); + ANNOTATE_BENIGN_RACE(Global); + WTF_ANNOTATE_BENIGN_RACE(WTFGlobal); pthread_t t; pthread_create(&t, 0, Thread, 0); barrier_wait(&barrier); diff --git a/compiler-rt/test/tsan/ignore_sync.cpp b/compiler-rt/test/tsan/ignore_sync.cpp index ae24a8c..7a129fb 100644 --- a/compiler-rt/test/tsan/ignore_sync.cpp +++ b/compiler-rt/test/tsan/ignore_sync.cpp @@ -1,9 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -#include -#include - -extern "C" void AnnotateIgnoreSyncBegin(const char*, int); -extern "C" void AnnotateIgnoreSyncEnd(const char*, int); +#include "test.h" int Global; pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER; diff --git a/compiler-rt/test/tsan/mutex_bad_read_lock.cpp b/compiler-rt/test/tsan/mutex_bad_read_lock.cpp index 84a2976..8f1fe77 100644 --- a/compiler-rt/test/tsan/mutex_bad_read_lock.cpp +++ b/compiler-rt/test/tsan/mutex_bad_read_lock.cpp @@ -1,5 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -extern "C" void AnnotateRWLockAcquired(const char *f, int l, void *m, long rw); +#include "test.h" int main() { int m = 0; diff --git a/compiler-rt/test/tsan/mutex_bad_read_unlock.cpp b/compiler-rt/test/tsan/mutex_bad_read_unlock.cpp index dcee515..6a88261 100644 --- a/compiler-rt/test/tsan/mutex_bad_read_unlock.cpp +++ b/compiler-rt/test/tsan/mutex_bad_read_unlock.cpp @@ -1,6 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | 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); +#include "test.h" int main() { int m = 0; diff --git a/compiler-rt/test/tsan/mutex_bad_unlock.cpp b/compiler-rt/test/tsan/mutex_bad_unlock.cpp index 6b483cf..a31485a 100644 --- a/compiler-rt/test/tsan/mutex_bad_unlock.cpp +++ b/compiler-rt/test/tsan/mutex_bad_unlock.cpp @@ -1,5 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -extern "C" void AnnotateRWLockReleased(const char *f, int l, void *m, long rw); +#include "test.h" int main() { int m = 0; diff --git a/compiler-rt/test/tsan/mutex_double_lock.cpp b/compiler-rt/test/tsan/mutex_double_lock.cpp index cd1e87d..2bef93a 100644 --- a/compiler-rt/test/tsan/mutex_double_lock.cpp +++ b/compiler-rt/test/tsan/mutex_double_lock.cpp @@ -1,8 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -#include -#include - -extern "C" void AnnotateRWLockAcquired(const char *f, int l, void *m, long rw); +#include "test.h" void *ThreadFunc(void *m) { AnnotateRWLockAcquired(__FILE__, __LINE__, m, 1); diff --git a/compiler-rt/test/tsan/mutexset5.cpp b/compiler-rt/test/tsan/mutexset5.cpp index 16da605..ad5a767 100644 --- a/compiler-rt/test/tsan/mutexset5.cpp +++ b/compiler-rt/test/tsan/mutexset5.cpp @@ -33,7 +33,7 @@ int main() { // CHECK: #1 main {{.*}}mutexset5.cpp:[[@LINE+4]] // CHECK: Mutex [[M2]] (0x{{.*}}) created at: // CHECK: #0 pthread_mutex_init - // CHECK: #1 main {{.*}}mutexset5.cpp:[[@LINE+5]] + // CHECK: #1 main {{.*}}mutexset5.cpp:[[@LINE+2]] pthread_mutex_init(&mtx1, 0); pthread_mutex_init(&mtx2, 0); pthread_t t[2]; diff --git a/compiler-rt/test/tsan/signal_sync2.cpp b/compiler-rt/test/tsan/signal_sync2.cpp index 163f206..7d00296 100644 --- a/compiler-rt/test/tsan/signal_sync2.cpp +++ b/compiler-rt/test/tsan/signal_sync2.cpp @@ -1,19 +1,13 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s // UNSUPPORTED: darwin -#include -#include -#include +#include "test.h" +#include #include -#include #include -#include -#include +#include // Test synchronization in signal handled within IgnoreSync region. -extern "C" void AnnotateIgnoreSyncBegin(const char *f, int l); -extern "C" void AnnotateIgnoreSyncEnd(const char *f, int l); - const int kSignalCount = 500; __thread int process_signals; diff --git a/compiler-rt/test/tsan/test.h b/compiler-rt/test/tsan/test.h index 4c75572..16d7a12 100644 --- a/compiler-rt/test/tsan/test.h +++ b/compiler-rt/test/tsan/test.h @@ -69,6 +69,8 @@ const int kPCInc = 1; extern "C" { #endif +void AnnotateThreadName(const char *f, int l, const char *name); + void AnnotateRWLockCreate(const char *f, int l, void *m); void AnnotateRWLockCreateStatic(const char *f, int l, void *m); void AnnotateRWLockDestroy(const char *f, int l, void *m); @@ -80,6 +82,15 @@ void AnnotateIgnoreReadsEnd(const char *f, int l); void AnnotateIgnoreWritesBegin(const char *f, int l); void AnnotateIgnoreWritesEnd(const char *f, int l); +void AnnotateIgnoreSyncBegin(const char *f, int l); +void AnnotateIgnoreSyncEnd(const char *f, int l); + +void AnnotateHappensBefore(const char *f, int l, void *addr); +void AnnotateHappensAfter(const char *f, int l, void *addr); + +void AnnotateBenignRaceSized(const char *f, int l, void *mem, unsigned int size, const char *desc); +void WTFAnnotateBenignRaceSized(const char *f, int l, void *mem, unsigned int size, const char *desc); + #ifdef __cplusplus } #endif @@ -94,6 +105,14 @@ void AnnotateIgnoreWritesEnd(const char *f, int l); AnnotateRWLockAcquired(__FILE__, __LINE__, m, is_w) #define ANNOTATE_RWLOCK_RELEASED(m, is_w) \ AnnotateRWLockReleased(__FILE__, __LINE__, m, is_w) +#define ANNOTATE_HAPPENS_BEFORE(addr) \ + AnnotateHappensBefore(__FILE__, __LINE__, (void *)(addr)) +#define ANNOTATE_HAPPENS_AFTER(addr) \ + AnnotateHappensAfter(__FILE__, __LINE__, (void *)(addr)) +#define ANNOTATE_BENIGN_RACE(var) \ + AnnotateBenignRaceSized(__FILE__, __LINE__, &(var), sizeof(var), #var) +#define WTF_ANNOTATE_BENIGN_RACE(var) \ + WTFAnnotateBenignRaceSized(__FILE__, __LINE__, &(var), sizeof(var), #var) #ifdef __APPLE__ #define ASM_SYMBOL(symbol) "_" #symbol diff --git a/compiler-rt/test/tsan/thread_end_with_ignore.cpp b/compiler-rt/test/tsan/thread_end_with_ignore.cpp index 79bb08d..8bcf298 100644 --- a/compiler-rt/test/tsan/thread_end_with_ignore.cpp +++ b/compiler-rt/test/tsan/thread_end_with_ignore.cpp @@ -1,8 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -#include -#include - -extern "C" void AnnotateIgnoreReadsBegin(const char *f, int l); +#include "test.h" void *Thread(void *x) { AnnotateIgnoreReadsBegin("", 0); diff --git a/compiler-rt/test/tsan/thread_end_with_ignore2.cpp b/compiler-rt/test/tsan/thread_end_with_ignore2.cpp index 9387ea4..4bdc560 100644 --- a/compiler-rt/test/tsan/thread_end_with_ignore2.cpp +++ b/compiler-rt/test/tsan/thread_end_with_ignore2.cpp @@ -1,5 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -extern "C" void AnnotateIgnoreWritesBegin(const char *f, int l); +#include "test.h" int main() { AnnotateIgnoreWritesBegin("", 0); diff --git a/compiler-rt/test/tsan/thread_end_with_ignore3.cpp b/compiler-rt/test/tsan/thread_end_with_ignore3.cpp index aa93da4..68b042b 100644 --- a/compiler-rt/test/tsan/thread_end_with_ignore3.cpp +++ b/compiler-rt/test/tsan/thread_end_with_ignore3.cpp @@ -1,6 +1,5 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s -extern "C" void AnnotateIgnoreReadsBegin(const char *f, int l); -extern "C" void AnnotateIgnoreReadsEnd(const char *f, int l); +#include "test.h" int main() { AnnotateIgnoreReadsBegin("", 0); @@ -15,8 +14,7 @@ int main() { // CHECK: ThreadSanitizer: main thread finished with ignores enabled // CHECK: Ignore was enabled at: // CHECK: #0 AnnotateIgnoreReadsBegin -// CHECK: #1 main {{.*}}thread_end_with_ignore3.cpp:10 +// CHECK: #1 main {{.*}}thread_end_with_ignore3.cpp:9 // CHECK: Ignore was enabled at: // CHECK: #0 AnnotateIgnoreReadsBegin -// CHECK: #1 main {{.*}}thread_end_with_ignore3.cpp:11 - +// CHECK: #1 main {{.*}}thread_end_with_ignore3.cpp:10 diff --git a/compiler-rt/test/tsan/thread_name.cpp b/compiler-rt/test/tsan/thread_name.cpp index 1fa0555..bc65421 100644 --- a/compiler-rt/test/tsan/thread_name.cpp +++ b/compiler-rt/test/tsan/thread_name.cpp @@ -15,8 +15,6 @@ #define USE_PTHREAD_SETNAME_NP 0 #endif -extern "C" void AnnotateThreadName(const char *f, int l, const char *name); - int Global; void *Thread1(void *x) { -- 2.7.4