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