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