Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libsanitizer / tsan / tsan_defs.h
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 //===----------------------------------------------------------------------===//
11
12 #ifndef TSAN_DEFS_H
13 #define TSAN_DEFS_H
14
15 #include "sanitizer_common/sanitizer_internal_defs.h"
16 #include "sanitizer_common/sanitizer_libc.h"
17 #include "tsan_stat.h"
18
19 #ifndef TSAN_DEBUG
20 #define TSAN_DEBUG 0
21 #endif  // TSAN_DEBUG
22
23 namespace __tsan {
24
25 #ifdef TSAN_GO
26 const bool kGoMode = true;
27 const bool kCppMode = false;
28 const char *const kTsanOptionsEnv = "GORACE";
29 // Go linker does not support weak symbols.
30 #define CPP_WEAK
31 #else
32 const bool kGoMode = false;
33 const bool kCppMode = true;
34 const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
35 #define CPP_WEAK WEAK
36 #endif
37
38 const int kTidBits = 13;
39 const unsigned kMaxTid = 1 << kTidBits;
40 const unsigned kMaxTidInClock = kMaxTid * 2;  // This includes msb 'freed' bit.
41 const int kClkBits = 42;
42 #ifndef TSAN_GO
43 const int kShadowStackSize = 4 * 1024;
44 const int kTraceStackSize = 256;
45 #endif
46
47 #ifdef TSAN_SHADOW_COUNT
48 # if TSAN_SHADOW_COUNT == 2 \
49   || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
50 const uptr kShadowCnt = TSAN_SHADOW_COUNT;
51 # else
52 #   error "TSAN_SHADOW_COUNT must be one of 2,4,8"
53 # endif
54 #else
55 // Count of shadow values in a shadow cell.
56 const uptr kShadowCnt = 4;
57 #endif
58
59 // That many user bytes are mapped onto a single shadow cell.
60 const uptr kShadowCell = 8;
61
62 // Size of a single shadow value (u64).
63 const uptr kShadowSize = 8;
64
65 // Shadow memory is kShadowMultiplier times larger than user memory.
66 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
67
68 #if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
69 const bool kCollectStats = true;
70 #else
71 const bool kCollectStats = false;
72 #endif
73
74 // The following "build consistency" machinery ensures that all source files
75 // are built in the same configuration. Inconsistent builds lead to
76 // hard to debug crashes.
77 #if TSAN_DEBUG
78 void build_consistency_debug();
79 #else
80 void build_consistency_release();
81 #endif
82
83 #if TSAN_COLLECT_STATS
84 void build_consistency_stats();
85 #else
86 void build_consistency_nostats();
87 #endif
88
89 #if TSAN_SHADOW_COUNT == 1
90 void build_consistency_shadow1();
91 #elif TSAN_SHADOW_COUNT == 2
92 void build_consistency_shadow2();
93 #elif TSAN_SHADOW_COUNT == 4
94 void build_consistency_shadow4();
95 #else
96 void build_consistency_shadow8();
97 #endif
98
99 static inline void USED build_consistency() {
100 #if TSAN_DEBUG
101   build_consistency_debug();
102 #else
103   build_consistency_release();
104 #endif
105 #if TSAN_COLLECT_STATS
106   build_consistency_stats();
107 #else
108   build_consistency_nostats();
109 #endif
110 #if TSAN_SHADOW_COUNT == 1
111   build_consistency_shadow1();
112 #elif TSAN_SHADOW_COUNT == 2
113   build_consistency_shadow2();
114 #elif TSAN_SHADOW_COUNT == 4
115   build_consistency_shadow4();
116 #else
117   build_consistency_shadow8();
118 #endif
119 }
120
121 template<typename T>
122 T min(T a, T b) {
123   return a < b ? a : b;
124 }
125
126 template<typename T>
127 T max(T a, T b) {
128   return a > b ? a : b;
129 }
130
131 template<typename T>
132 T RoundUp(T p, u64 align) {
133   DCHECK_EQ(align & (align - 1), 0);
134   return (T)(((u64)p + align - 1) & ~(align - 1));
135 }
136
137 template<typename T>
138 T RoundDown(T p, u64 align) {
139   DCHECK_EQ(align & (align - 1), 0);
140   return (T)((u64)p & ~(align - 1));
141 }
142
143 // Zeroizes high part, returns 'bits' lsb bits.
144 template<typename T>
145 T GetLsb(T v, int bits) {
146   return (T)((u64)v & ((1ull << bits) - 1));
147 }
148
149 struct MD5Hash {
150   u64 hash[2];
151   bool operator==(const MD5Hash &other) const;
152 };
153
154 MD5Hash md5_hash(const void *data, uptr size);
155
156 struct ThreadState;
157 struct ThreadContext;
158 struct Context;
159 struct ReportStack;
160 class ReportDesc;
161 class RegionAlloc;
162 class StackTrace;
163 struct MBlock;
164
165 }  // namespace __tsan
166
167 #endif  // TSAN_DEFS_H