1 #include <pthread.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <dlfcn.h>
6 #include <stddef.h>
7 #include <sched.h>
8 #include <stdarg.h>
9 #include "sanitizer_common/print_address.h"
10 
11 #include <sanitizer/tsan_interface.h>
12 
13 #ifdef __APPLE__
14 #include <mach/mach_time.h>
15 #endif
16 
17 // TSan-invisible barrier.
18 // Tests use it to establish necessary execution order in a way that does not
19 // interfere with tsan (does not establish synchronization between threads).
20 typedef unsigned long long invisible_barrier_t;
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 void __tsan_testonly_barrier_init(invisible_barrier_t *barrier,
26     unsigned count);
27 void __tsan_testonly_barrier_wait(invisible_barrier_t *barrier);
28 unsigned long __tsan_testonly_shadow_stack_current_size();
29 #ifdef __cplusplus
30 }
31 #endif
32 
barrier_init(invisible_barrier_t * barrier,unsigned count)33 static inline void barrier_init(invisible_barrier_t *barrier, unsigned count) {
34   __tsan_testonly_barrier_init(barrier, count);
35 }
36 
barrier_wait(invisible_barrier_t * barrier)37 static inline void barrier_wait(invisible_barrier_t *barrier) {
38   __tsan_testonly_barrier_wait(barrier);
39 }
40 
41 // Default instance of the barrier, but a test can declare more manually.
42 invisible_barrier_t barrier;
43 
44 #ifdef __APPLE__
monotonic_clock_ns()45 unsigned long long monotonic_clock_ns() {
46   static mach_timebase_info_data_t timebase_info;
47   if (timebase_info.denom == 0) mach_timebase_info(&timebase_info);
48   return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom;
49 }
50 #else
monotonic_clock_ns()51 unsigned long long monotonic_clock_ns() {
52   struct timespec t;
53   clock_gettime(CLOCK_MONOTONIC, &t);
54   return (unsigned long long)t.tv_sec * 1000000000ull + t.tv_nsec;
55 }
56 #endif
57 
58 //The const kPCInc must be in sync with StackTrace::GetPreviousInstructionPc
59 #if defined(__powerpc64__) || defined(__arm__) || defined(__aarch64__)
60 // PCs are always 4 byte aligned.
61 const int kPCInc = 4;
62 #elif defined(__sparc__) || defined(__mips__)
63 const int kPCInc = 8;
64 #else
65 const int kPCInc = 1;
66 #endif
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 void AnnotateThreadName(const char *f, int l, const char *name);
73 
74 void AnnotateRWLockCreate(const char *f, int l, void *m);
75 void AnnotateRWLockCreateStatic(const char *f, int l, void *m);
76 void AnnotateRWLockDestroy(const char *f, int l, void *m);
77 void AnnotateRWLockAcquired(const char *f, int l, void *m, long is_w);
78 void AnnotateRWLockReleased(const char *f, int l, void *m, long is_w);
79 
80 void AnnotateIgnoreReadsBegin(const char *f, int l);
81 void AnnotateIgnoreReadsEnd(const char *f, int l);
82 void AnnotateIgnoreWritesBegin(const char *f, int l);
83 void AnnotateIgnoreWritesEnd(const char *f, int l);
84 
85 void AnnotateIgnoreSyncBegin(const char *f, int l);
86 void AnnotateIgnoreSyncEnd(const char *f, int l);
87 
88 void AnnotateHappensBefore(const char *f, int l, void *addr);
89 void AnnotateHappensAfter(const char *f, int l, void *addr);
90 
91 void AnnotateBenignRaceSized(const char *f, int l, void *mem, unsigned int size, const char *desc);
92 void WTFAnnotateBenignRaceSized(const char *f, int l, void *mem, unsigned int size, const char *desc);
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #define ANNOTATE_RWLOCK_CREATE(m) \
99     AnnotateRWLockCreate(__FILE__, __LINE__, m)
100 #define ANNOTATE_RWLOCK_CREATE_STATIC(m) \
101     AnnotateRWLockCreateStatic(__FILE__, __LINE__, m)
102 #define ANNOTATE_RWLOCK_DESTROY(m) \
103     AnnotateRWLockDestroy(__FILE__, __LINE__, m)
104 #define ANNOTATE_RWLOCK_ACQUIRED(m, is_w) \
105     AnnotateRWLockAcquired(__FILE__, __LINE__, m, is_w)
106 #define ANNOTATE_RWLOCK_RELEASED(m, is_w) \
107     AnnotateRWLockReleased(__FILE__, __LINE__, m, is_w)
108 #define ANNOTATE_HAPPENS_BEFORE(addr) \
109   AnnotateHappensBefore(__FILE__, __LINE__, (void *)(addr))
110 #define ANNOTATE_HAPPENS_AFTER(addr) \
111   AnnotateHappensAfter(__FILE__, __LINE__, (void *)(addr))
112 #define ANNOTATE_BENIGN_RACE(var) \
113   AnnotateBenignRaceSized(__FILE__, __LINE__, &(var), sizeof(var), #var)
114 #define WTF_ANNOTATE_BENIGN_RACE(var) \
115   WTFAnnotateBenignRaceSized(__FILE__, __LINE__, &(var), sizeof(var), #var)
116 
117 #ifdef __APPLE__
118 #define ASM_SYMBOL(symbol) "_" #symbol
119 #else
120 #define ASM_SYMBOL(symbol) #symbol
121 #endif
122