2013-02-04 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_defs.h
blob6683a4e1abb0164a1e7560900bcb2808030b5c84
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"
19 #ifndef TSAN_DEBUG
20 #define TSAN_DEBUG 0
21 #endif // TSAN_DEBUG
23 namespace __tsan {
25 #ifdef TSAN_GO
26 const bool kGoMode = true;
27 const bool kCppMode = false;
28 const char *const kTsanOptionsEnv = "GORACE";
29 #else
30 const bool kGoMode = false;
31 const bool kCppMode = true;
32 const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
33 #endif
35 const int kTidBits = 13;
36 const unsigned kMaxTid = 1 << kTidBits;
37 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
38 const int kClkBits = 43;
39 #ifndef TSAN_GO
40 const int kShadowStackSize = 4 * 1024;
41 const int kTraceStackSize = 256;
42 #endif
44 #ifdef TSAN_SHADOW_COUNT
45 # if TSAN_SHADOW_COUNT == 2 \
46 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
47 const uptr kShadowCnt = TSAN_SHADOW_COUNT;
48 # else
49 # error "TSAN_SHADOW_COUNT must be one of 2,4,8"
50 # endif
51 #else
52 // Count of shadow values in a shadow cell.
53 const uptr kShadowCnt = 4;
54 #endif
56 // That many user bytes are mapped onto a single shadow cell.
57 const uptr kShadowCell = 8;
59 // Size of a single shadow value (u64).
60 const uptr kShadowSize = 8;
62 // Shadow memory is kShadowMultiplier times larger than user memory.
63 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
65 #if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
66 const bool kCollectStats = true;
67 #else
68 const bool kCollectStats = false;
69 #endif
71 // The following "build consistency" machinery ensures that all source files
72 // are built in the same configuration. Inconsistent builds lead to
73 // hard to debug crashes.
74 #if TSAN_DEBUG
75 void build_consistency_debug();
76 #else
77 void build_consistency_release();
78 #endif
80 #if TSAN_COLLECT_STATS
81 void build_consistency_stats();
82 #else
83 void build_consistency_nostats();
84 #endif
86 #if TSAN_SHADOW_COUNT == 1
87 void build_consistency_shadow1();
88 #elif TSAN_SHADOW_COUNT == 2
89 void build_consistency_shadow2();
90 #elif TSAN_SHADOW_COUNT == 4
91 void build_consistency_shadow4();
92 #else
93 void build_consistency_shadow8();
94 #endif
96 static inline void USED build_consistency() {
97 #if TSAN_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 #if TSAN_SHADOW_COUNT == 1
108 build_consistency_shadow1();
109 #elif TSAN_SHADOW_COUNT == 2
110 build_consistency_shadow2();
111 #elif TSAN_SHADOW_COUNT == 4
112 build_consistency_shadow4();
113 #else
114 build_consistency_shadow8();
115 #endif
118 template<typename T>
119 T min(T a, T b) {
120 return a < b ? a : b;
123 template<typename T>
124 T max(T a, T b) {
125 return a > b ? a : b;
128 template<typename T>
129 T RoundUp(T p, u64 align) {
130 DCHECK_EQ(align & (align - 1), 0);
131 return (T)(((u64)p + align - 1) & ~(align - 1));
134 template<typename T>
135 T RoundDown(T p, u64 align) {
136 DCHECK_EQ(align & (align - 1), 0);
137 return (T)((u64)p & ~(align - 1));
140 // Zeroizes high part, returns 'bits' lsb bits.
141 template<typename T>
142 T GetLsb(T v, int bits) {
143 return (T)((u64)v & ((1ull << bits) - 1));
146 struct MD5Hash {
147 u64 hash[2];
148 bool operator==(const MD5Hash &other) const;
151 MD5Hash md5_hash(const void *data, uptr size);
153 struct ThreadState;
154 struct ThreadContext;
155 struct Context;
156 struct ReportStack;
157 class ReportDesc;
158 class RegionAlloc;
159 class StackTrace;
160 struct MBlock;
162 } // namespace __tsan
164 #endif // TSAN_DEFS_H