1 //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef TSAN_DEFS_H
15 #define TSAN_DEFS_H
16
17 #include "sanitizer_common/sanitizer_internal_defs.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19 #include "tsan_stat.h"
20
21 // Setup defaults for compile definitions.
22 #ifndef TSAN_NO_HISTORY
23 # define TSAN_NO_HISTORY 0
24 #endif
25
26 #ifndef TSAN_COLLECT_STATS
27 # define TSAN_COLLECT_STATS 0
28 #endif
29
30 namespace __tsan {
31
32 #ifdef SANITIZER_GO
33 const bool kGoMode = true;
34 const bool kCppMode = false;
35 const char *const kTsanOptionsEnv = "GORACE";
36 // Go linker does not support weak symbols.
37 #define CPP_WEAK
38 #else
39 const bool kGoMode = false;
40 const bool kCppMode = true;
41 const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
42 #define CPP_WEAK WEAK
43 #endif
44
45 const int kTidBits = 13;
46 const unsigned kMaxTid = 1 << kTidBits;
47 #ifndef SANITIZER_GO
48 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
49 #else
50 const unsigned kMaxTidInClock = kMaxTid; // Go does not track freed memory.
51 #endif
52 const int kClkBits = 42;
53 const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
54 const uptr kShadowStackSize = 64 * 1024;
55
56 // Count of shadow values in a shadow cell.
57 const uptr kShadowCnt = 4;
58
59 // That many user bytes are mapped onto a single shadow cell.
60 const uptr kShadowCell = 8;
61
62 // Size of a single shadow value (u64).
63 const uptr kShadowSize = 8;
64
65 // Shadow memory is kShadowMultiplier times larger than user memory.
66 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
67
68 // That many user bytes are mapped onto a single meta shadow cell.
69 // Must be less or equal to minimal memory allocator alignment.
70 const uptr kMetaShadowCell = 8;
71
72 // Size of a single meta shadow value (u32).
73 const uptr kMetaShadowSize = 4;
74
75 #if TSAN_NO_HISTORY
76 const bool kCollectHistory = false;
77 #else
78 const bool kCollectHistory = true;
79 #endif
80
81 // The following "build consistency" machinery ensures that all source files
82 // are built in the same configuration. Inconsistent builds lead to
83 // hard to debug crashes.
84 #if SANITIZER_DEBUG
85 void build_consistency_debug();
86 #else
87 void build_consistency_release();
88 #endif
89
90 #if TSAN_COLLECT_STATS
91 void build_consistency_stats();
92 #else
93 void build_consistency_nostats();
94 #endif
95
build_consistency()96 static inline void USED build_consistency() {
97 #if SANITIZER_DEBUG
98 build_consistency_debug();
99 #else
100 build_consistency_release();
101 #endif
102 #if TSAN_COLLECT_STATS
103 build_consistency_stats();
104 #else
105 build_consistency_nostats();
106 #endif
107 }
108
109 template<typename T>
min(T a,T b)110 T min(T a, T b) {
111 return a < b ? a : b;
112 }
113
114 template<typename T>
max(T a,T b)115 T max(T a, T b) {
116 return a > b ? a : b;
117 }
118
119 template<typename T>
RoundUp(T p,u64 align)120 T RoundUp(T p, u64 align) {
121 DCHECK_EQ(align & (align - 1), 0);
122 return (T)(((u64)p + align - 1) & ~(align - 1));
123 }
124
125 template<typename T>
RoundDown(T p,u64 align)126 T RoundDown(T p, u64 align) {
127 DCHECK_EQ(align & (align - 1), 0);
128 return (T)((u64)p & ~(align - 1));
129 }
130
131 // Zeroizes high part, returns 'bits' lsb bits.
132 template<typename T>
GetLsb(T v,int bits)133 T GetLsb(T v, int bits) {
134 return (T)((u64)v & ((1ull << bits) - 1));
135 }
136
137 struct MD5Hash {
138 u64 hash[2];
139 bool operator==(const MD5Hash &other) const;
140 };
141
142 MD5Hash md5_hash(const void *data, uptr size);
143
144 struct ThreadState;
145 class ThreadContext;
146 struct Context;
147 struct ReportStack;
148 class ReportDesc;
149 class RegionAlloc;
150
151 // Descriptor of user's memory block.
152 struct MBlock {
153 u64 siz;
154 u32 stk;
155 u16 tid;
156 };
157
158 COMPILER_CHECK(sizeof(MBlock) == 16);
159
160 } // namespace __tsan
161
162 #endif // TSAN_DEFS_H
163