2012-11-28 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libsanitizer / tsan / tsan_defs.h
blob6f3fd21e246d9d0733e9e9de7168261de71a1962
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 char *const kTsanOptionsEnv = "GORACE";
27 #else
28 const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
29 #endif
31 const int kTidBits = 13;
32 const unsigned kMaxTid = 1 << kTidBits;
33 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
34 const int kClkBits = 43;
35 #ifndef TSAN_GO
36 const int kShadowStackSize = 4 * 1024;
37 const int kTraceStackSize = 256;
38 #endif
40 #ifdef TSAN_SHADOW_COUNT
41 # if TSAN_SHADOW_COUNT == 2 \
42 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
43 const uptr kShadowCnt = TSAN_SHADOW_COUNT;
44 # else
45 # error "TSAN_SHADOW_COUNT must be one of 2,4,8"
46 # endif
47 #else
48 // Count of shadow values in a shadow cell.
49 const uptr kShadowCnt = 4;
50 #endif
52 // That many user bytes are mapped onto a single shadow cell.
53 const uptr kShadowCell = 8;
55 // Size of a single shadow value (u64).
56 const uptr kShadowSize = 8;
58 // Shadow memory is kShadowMultiplier times larger than user memory.
59 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
61 #if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
62 const bool kCollectStats = true;
63 #else
64 const bool kCollectStats = false;
65 #endif
67 // The following "build consistency" machinery ensures that all source files
68 // are built in the same configuration. Inconsistent builds lead to
69 // hard to debug crashes.
70 #if TSAN_DEBUG
71 void build_consistency_debug();
72 #else
73 void build_consistency_release();
74 #endif
76 #if TSAN_COLLECT_STATS
77 void build_consistency_stats();
78 #else
79 void build_consistency_nostats();
80 #endif
82 #if TSAN_SHADOW_COUNT == 1
83 void build_consistency_shadow1();
84 #elif TSAN_SHADOW_COUNT == 2
85 void build_consistency_shadow2();
86 #elif TSAN_SHADOW_COUNT == 4
87 void build_consistency_shadow4();
88 #else
89 void build_consistency_shadow8();
90 #endif
92 static inline void USED build_consistency() {
93 #if TSAN_DEBUG
94 build_consistency_debug();
95 #else
96 build_consistency_release();
97 #endif
98 #if TSAN_COLLECT_STATS
99 build_consistency_stats();
100 #else
101 build_consistency_nostats();
102 #endif
103 #if TSAN_SHADOW_COUNT == 1
104 build_consistency_shadow1();
105 #elif TSAN_SHADOW_COUNT == 2
106 build_consistency_shadow2();
107 #elif TSAN_SHADOW_COUNT == 4
108 build_consistency_shadow4();
109 #else
110 build_consistency_shadow8();
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, int align) {
126 DCHECK_EQ(align & (align - 1), 0);
127 return (T)(((u64)p + align - 1) & ~(align - 1));
130 struct MD5Hash {
131 u64 hash[2];
132 bool operator==(const MD5Hash &other) const;
135 MD5Hash md5_hash(const void *data, uptr size);
137 struct ThreadState;
138 struct ThreadContext;
139 struct Context;
140 struct ReportStack;
141 class ReportDesc;
142 class RegionAlloc;
143 class StackTrace;
144 struct MBlock;
146 } // namespace __tsan
148 #endif // TSAN_DEFS_H