tsan: declare annotations in test.h
authorDmitry Vyukov <dvyukov@google.com>
Mon, 10 May 2021 07:04:20 +0000 (09:04 +0200)
committerDmitry Vyukov <dvyukov@google.com>
Wed, 12 May 2021 05:22:39 +0000 (07:22 +0200)
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

14 files changed:
compiler-rt/test/tsan/annotate_happens_before.cpp
compiler-rt/test/tsan/benign_race.cpp
compiler-rt/test/tsan/ignore_sync.cpp
compiler-rt/test/tsan/mutex_bad_read_lock.cpp
compiler-rt/test/tsan/mutex_bad_read_unlock.cpp
compiler-rt/test/tsan/mutex_bad_unlock.cpp
compiler-rt/test/tsan/mutex_double_lock.cpp
compiler-rt/test/tsan/mutexset5.cpp
compiler-rt/test/tsan/signal_sync2.cpp
compiler-rt/test/tsan/test.h
compiler-rt/test/tsan/thread_end_with_ignore.cpp
compiler-rt/test/tsan/thread_end_with_ignore2.cpp
compiler-rt/test/tsan/thread_end_with_ignore3.cpp
compiler-rt/test/tsan/thread_name.cpp

index 0116616..86a8669 100644 (file)
@@ -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) {
index 90722aa..53e820d 100644 (file)
@@ -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);
index ae24a8c..7a129fb 100644 (file)
@@ -1,9 +1,5 @@
 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-
-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;
index 84a2976..8f1fe77 100644 (file)
@@ -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;
index dcee515..6a88261 100644 (file)
@@ -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;
index 6b483cf..a31485a 100644 (file)
@@ -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;
index cd1e87d..2bef93a 100644 (file)
@@ -1,8 +1,5 @@
 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
-
-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);
index 16da605..ad5a767 100644 (file)
@@ -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];
index 163f206..7d00296 100644 (file)
@@ -1,19 +1,13 @@
 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
 // UNSUPPORTED: darwin
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "test.h"
+#include <errno.h>
 #include <signal.h>
-#include <sys/types.h>
 #include <sys/time.h>
-#include <unistd.h>
-#include <errno.h>
+#include <sys/types.h>
 
 // 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;
index 4c75572..16d7a12 100644 (file)
@@ -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
index 79bb08d..8bcf298 100644 (file)
@@ -1,8 +1,5 @@
 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-
-extern "C" void AnnotateIgnoreReadsBegin(const char *f, int l);
+#include "test.h"
 
 void *Thread(void *x) {
   AnnotateIgnoreReadsBegin("", 0);
index 9387ea4..4bdc560 100644 (file)
@@ -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);
index aa93da4..68b042b 100644 (file)
@@ -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
index 1fa0555..bc65421 100644 (file)
@@ -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) {