2016-10-21 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_defs.h
blob259b30bee5db88f03863ae8052df19c65e84ba7b
1 //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
12 #ifndef TSAN_DEFS_H
13 #define TSAN_DEFS_H
15 #include "sanitizer_common/sanitizer_internal_defs.h"
16 #include "sanitizer_common/sanitizer_libc.h"
17 #include "tsan_stat.h"
18 #include "ubsan/ubsan_platform.h"
20 // Setup defaults for compile definitions.
21 #ifndef TSAN_NO_HISTORY
22 # define TSAN_NO_HISTORY 0
23 #endif
25 #ifndef TSAN_COLLECT_STATS
26 # define TSAN_COLLECT_STATS 0
27 #endif
29 #ifndef TSAN_CONTAINS_UBSAN
30 # define TSAN_CONTAINS_UBSAN (CAN_SANITIZE_UB && !defined(SANITIZER_GO))
31 #endif
33 namespace __tsan {
35 #ifdef SANITIZER_GO
36 const bool kGoMode = true;
37 const bool kCppMode = false;
38 const char *const kTsanOptionsEnv = "GORACE";
39 // Go linker does not support weak symbols.
40 #define CPP_WEAK
41 #else
42 const bool kGoMode = false;
43 const bool kCppMode = true;
44 const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
45 #define CPP_WEAK WEAK
46 #endif
48 const int kTidBits = 13;
49 const unsigned kMaxTid = 1 << kTidBits;
50 #ifndef SANITIZER_GO
51 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
52 #else
53 const unsigned kMaxTidInClock = kMaxTid; // Go does not track freed memory.
54 #endif
55 const int kClkBits = 42;
56 const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
57 const uptr kShadowStackSize = 64 * 1024;
59 // Count of shadow values in a shadow cell.
60 const uptr kShadowCnt = 4;
62 // That many user bytes are mapped onto a single shadow cell.
63 const uptr kShadowCell = 8;
65 // Size of a single shadow value (u64).
66 const uptr kShadowSize = 8;
68 // Shadow memory is kShadowMultiplier times larger than user memory.
69 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
71 // That many user bytes are mapped onto a single meta shadow cell.
72 // Must be less or equal to minimal memory allocator alignment.
73 const uptr kMetaShadowCell = 8;
75 // Size of a single meta shadow value (u32).
76 const uptr kMetaShadowSize = 4;
78 #if TSAN_NO_HISTORY
79 const bool kCollectHistory = false;
80 #else
81 const bool kCollectHistory = true;
82 #endif
84 const unsigned kInvalidTid = (unsigned)-1;
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
95 #if TSAN_COLLECT_STATS
96 void build_consistency_stats();
97 #else
98 void build_consistency_nostats();
99 #endif
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
114 template<typename T>
115 T min(T a, T b) {
116 return a < b ? a : b;
119 template<typename T>
120 T max(T a, T b) {
121 return a > b ? a : b;
124 template<typename T>
125 T RoundUp(T p, u64 align) {
126 DCHECK_EQ(align & (align - 1), 0);
127 return (T)(((u64)p + align - 1) & ~(align - 1));
130 template<typename T>
131 T RoundDown(T p, u64 align) {
132 DCHECK_EQ(align & (align - 1), 0);
133 return (T)((u64)p & ~(align - 1));
136 // Zeroizes high part, returns 'bits' lsb bits.
137 template<typename T>
138 T GetLsb(T v, int bits) {
139 return (T)((u64)v & ((1ull << bits) - 1));
142 struct MD5Hash {
143 u64 hash[2];
144 bool operator==(const MD5Hash &other) const;
147 MD5Hash md5_hash(const void *data, uptr size);
149 struct ThreadState;
150 class ThreadContext;
151 struct Context;
152 struct ReportStack;
153 class ReportDesc;
154 class RegionAlloc;
156 // Descriptor of user's memory block.
157 struct MBlock {
158 u64 siz;
159 u32 stk;
160 u16 tid;
163 COMPILER_CHECK(sizeof(MBlock) == 16);
165 } // namespace __tsan
167 #endif // TSAN_DEFS_H