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