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

source code of compiler-rt/test/tsan/test.h