]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/test/tsan/test.h
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / test / tsan / test.h
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 #ifdef __APPLE__
12 #include <mach/mach_time.h>
13 #endif
14
15 // TSan-invisible barrier.
16 // Tests use it to establish necessary execution order in a way that does not
17 // interfere with tsan (does not establish synchronization between threads).
18 typedef unsigned long long invisible_barrier_t;
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 void __tsan_testonly_barrier_init(invisible_barrier_t *barrier,
24 unsigned count);
25 void __tsan_testonly_barrier_wait(invisible_barrier_t *barrier);
26 unsigned long __tsan_testonly_shadow_stack_current_size();
27 #ifdef __cplusplus
28 }
29 #endif
30
31 static inline void barrier_init(invisible_barrier_t *barrier, unsigned count) {
32 __tsan_testonly_barrier_init(barrier, count);
33 }
34
35 static inline void barrier_wait(invisible_barrier_t *barrier) {
36 __tsan_testonly_barrier_wait(barrier);
37 }
38
39 // Default instance of the barrier, but a test can declare more manually.
40 invisible_barrier_t barrier;
41
42 #ifdef __APPLE__
43 unsigned long long monotonic_clock_ns() {
44 static mach_timebase_info_data_t timebase_info;
45 if (timebase_info.denom == 0) mach_timebase_info(&timebase_info);
46 return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom;
47 }
48 #else
49 unsigned long long monotonic_clock_ns() {
50 struct timespec t;
51 clock_gettime(CLOCK_MONOTONIC, &t);
52 return (unsigned long long)t.tv_sec * 1000000000ull + t.tv_nsec;
53 }
54 #endif
55
56 //The const kPCInc must be in sync with StackTrace::GetPreviousInstructionPc
57 #if defined(__powerpc64__)
58 // PCs are always 4 byte aligned.
59 const int kPCInc = 4;
60 #elif defined(__sparc__) || defined(__mips__)
61 const int kPCInc = 8;
62 #else
63 const int kPCInc = 1;
64 #endif
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 void AnnotateRWLockCreate(const char *f, int l, void *m);
71 void AnnotateRWLockCreateStatic(const char *f, int l, void *m);
72 void AnnotateRWLockDestroy(const char *f, int l, void *m);
73 void AnnotateRWLockAcquired(const char *f, int l, void *m, long is_w);
74 void AnnotateRWLockReleased(const char *f, int l, void *m, long is_w);
75
76 #ifdef __cplusplus
77 }
78 #endif
79
80 #define ANNOTATE_RWLOCK_CREATE(m) \
81 AnnotateRWLockCreate(__FILE__, __LINE__, m)
82 #define ANNOTATE_RWLOCK_CREATE_STATIC(m) \
83 AnnotateRWLockCreateStatic(__FILE__, __LINE__, m)
84 #define ANNOTATE_RWLOCK_DESTROY(m) \
85 AnnotateRWLockDestroy(__FILE__, __LINE__, m)
86 #define ANNOTATE_RWLOCK_ACQUIRED(m, is_w) \
87 AnnotateRWLockAcquired(__FILE__, __LINE__, m, is_w)
88 #define ANNOTATE_RWLOCK_RELEASED(m, is_w) \
89 AnnotateRWLockReleased(__FILE__, __LINE__, m, is_w)