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