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).
20typedef unsigned long long invisible_barrier_t;
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25void __tsan_testonly_barrier_init(invisible_barrier_t *barrier,
26 unsigned count);
27void __tsan_testonly_barrier_wait(invisible_barrier_t *barrier);
28unsigned long __tsan_testonly_shadow_stack_current_size();
29#ifdef __cplusplus
30}
31#endif
32
33static inline void barrier_init(invisible_barrier_t *barrier, unsigned count) {
34 __tsan_testonly_barrier_init(barrier, count);
35}
36
37static 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.
42invisible_barrier_t barrier;
43
44#ifdef __APPLE__
45unsigned 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
51unsigned 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.
61const int kPCInc = 4;
62#elif defined(__sparc__) || defined(__mips__)
63const int kPCInc = 8;
64#else
65const int kPCInc = 1;
66#endif
67
68#ifdef __cplusplus
69extern "C" {
70#endif
71
72void AnnotateRWLockCreate(const char *f, int l, void *m);
73void AnnotateRWLockCreateStatic(const char *f, int l, void *m);
74void AnnotateRWLockDestroy(const char *f, int l, void *m);
75void AnnotateRWLockAcquired(const char *f, int l, void *m, long is_w);
76void AnnotateRWLockReleased(const char *f, int l, void *m, long is_w);
77
78void AnnotateIgnoreReadsBegin(const char *f, int l);
79void AnnotateIgnoreReadsEnd(const char *f, int l);
80void AnnotateIgnoreWritesBegin(const char *f, int l);
81void AnnotateIgnoreWritesEnd(const char *f, int l);
82
83#ifdef __cplusplus
84}
85#endif
86
87#define ANNOTATE_RWLOCK_CREATE(m) \
88 AnnotateRWLockCreate(__FILE__, __LINE__, m)
89#define ANNOTATE_RWLOCK_CREATE_STATIC(m) \
90 AnnotateRWLockCreateStatic(__FILE__, __LINE__, m)
91#define ANNOTATE_RWLOCK_DESTROY(m) \
92 AnnotateRWLockDestroy(__FILE__, __LINE__, m)
93#define ANNOTATE_RWLOCK_ACQUIRED(m, is_w) \
94 AnnotateRWLockAcquired(__FILE__, __LINE__, m, is_w)
95#define ANNOTATE_RWLOCK_RELEASED(m, is_w) \
96 AnnotateRWLockReleased(__FILE__, __LINE__, m, is_w)
97
98#ifdef __APPLE__
99#define ASM_SYMBOL(symbol) "_" #symbol
100#else
101#define ASM_SYMBOL(symbol) #symbol
102#endif
103