1 //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef TSAN_DEFS_H
14 #define TSAN_DEFS_H
15 
16 #include "sanitizer_common/sanitizer_internal_defs.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "sanitizer_common/sanitizer_mutex.h"
19 #include "ubsan/ubsan_platform.h"
20 
21 #ifndef TSAN_VECTORIZE
22 #  define TSAN_VECTORIZE __SSE4_2__
23 #endif
24 
25 #if TSAN_VECTORIZE
26 // <emmintrin.h> transitively includes <stdlib.h>,
27 // and it's prohibited to include std headers into tsan runtime.
28 // So we do this dirty trick.
29 #  define _MM_MALLOC_H_INCLUDED
30 #  define __MM_MALLOC_H
31 #  include <emmintrin.h>
32 #  include <smmintrin.h>
33 #  define VECTOR_ALIGNED ALIGNED(16)
34 typedef __m128i m128;
35 #else
36 #  define VECTOR_ALIGNED
37 #endif
38 
39 // Setup defaults for compile definitions.
40 #ifndef TSAN_NO_HISTORY
41 # define TSAN_NO_HISTORY 0
42 #endif
43 
44 #ifndef TSAN_CONTAINS_UBSAN
45 # if CAN_SANITIZE_UB && !SANITIZER_GO
46 #  define TSAN_CONTAINS_UBSAN 1
47 # else
48 #  define TSAN_CONTAINS_UBSAN 0
49 # endif
50 #endif
51 
52 namespace __tsan {
53 
54 constexpr uptr kByteBits = 8;
55 
56 // Thread slot ID.
57 enum class Sid : u8 {};
58 constexpr uptr kThreadSlotCount = 256;
59 constexpr Sid kFreeSid = static_cast<Sid>(255);
60 
61 // Abstract time unit, vector clock element.
62 enum class Epoch : u16 {};
63 constexpr uptr kEpochBits = 14;
64 constexpr Epoch kEpochZero = static_cast<Epoch>(0);
65 constexpr Epoch kEpochOver = static_cast<Epoch>(1 << kEpochBits);
66 constexpr Epoch kEpochLast = static_cast<Epoch>((1 << kEpochBits) - 1);
67 
68 inline Epoch EpochInc(Epoch epoch) {
69   return static_cast<Epoch>(static_cast<u16>(epoch) + 1);
70 }
71 
72 inline bool EpochOverflow(Epoch epoch) { return epoch == kEpochOver; }
73 
74 const uptr kShadowStackSize = 64 * 1024;
75 
76 // Count of shadow values in a shadow cell.
77 const uptr kShadowCnt = 4;
78 
79 // That many user bytes are mapped onto a single shadow cell.
80 const uptr kShadowCell = 8;
81 
82 // Single shadow value.
83 enum class RawShadow : u32 {};
84 const uptr kShadowSize = sizeof(RawShadow);
85 
86 // Shadow memory is kShadowMultiplier times larger than user memory.
87 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
88 
89 // That many user bytes are mapped onto a single meta shadow cell.
90 // Must be less or equal to minimal memory allocator alignment.
91 const uptr kMetaShadowCell = 8;
92 
93 // Size of a single meta shadow value (u32).
94 const uptr kMetaShadowSize = 4;
95 
96 // All addresses and PCs are assumed to be compressable to that many bits.
97 const uptr kCompressedAddrBits = 44;
98 
99 #if TSAN_NO_HISTORY
100 const bool kCollectHistory = false;
101 #else
102 const bool kCollectHistory = true;
103 #endif
104 
105 // The following "build consistency" machinery ensures that all source files
106 // are built in the same configuration. Inconsistent builds lead to
107 // hard to debug crashes.
108 #if SANITIZER_DEBUG
109 void build_consistency_debug();
110 #else
111 void build_consistency_release();
112 #endif
113 
114 static inline void USED build_consistency() {
115 #if SANITIZER_DEBUG
116   build_consistency_debug();
117 #else
118   build_consistency_release();
119 #endif
120 }
121 
122 template<typename T>
123 T min(T a, T b) {
124   return a < b ? a : b;
125 }
126 
127 template<typename T>
128 T max(T a, T b) {
129   return a > b ? a : b;
130 }
131 
132 template<typename T>
133 T RoundUp(T p, u64 align) {
134   DCHECK_EQ(align & (align - 1), 0);
135   return (T)(((u64)p + align - 1) & ~(align - 1));
136 }
137 
138 template<typename T>
139 T RoundDown(T p, u64 align) {
140   DCHECK_EQ(align & (align - 1), 0);
141   return (T)((u64)p & ~(align - 1));
142 }
143 
144 // Zeroizes high part, returns 'bits' lsb bits.
145 template<typename T>
146 T GetLsb(T v, int bits) {
147   return (T)((u64)v & ((1ull << bits) - 1));
148 }
149 
150 struct MD5Hash {
151   u64 hash[2];
152   bool operator==(const MD5Hash &other) const;
153 };
154 
155 MD5Hash md5_hash(const void *data, uptr size);
156 
157 struct Processor;
158 struct ThreadState;
159 class ThreadContext;
160 struct TidSlot;
161 struct Context;
162 struct ReportStack;
163 class ReportDesc;
164 class RegionAlloc;
165 struct Trace;
166 struct TracePart;
167 
168 typedef uptr AccessType;
169 
170 enum : AccessType {
171   kAccessWrite = 0,
172   kAccessRead = 1 << 0,
173   kAccessAtomic = 1 << 1,
174   kAccessVptr = 1 << 2,  // read or write of an object virtual table pointer
175   kAccessFree = 1 << 3,  // synthetic memory access during memory freeing
176   kAccessExternalPC = 1 << 4,  // access PC can have kExternalPCBit set
177   kAccessCheckOnly = 1 << 5,   // check for races, but don't store
178   kAccessNoRodata = 1 << 6,    // don't check for .rodata marker
179 };
180 
181 // Descriptor of user's memory block.
182 struct MBlock {
183   u64  siz : 48;
184   u64  tag : 16;
185   StackID stk;
186   Tid tid;
187 };
188 
189 COMPILER_CHECK(sizeof(MBlock) == 16);
190 
191 enum ExternalTag : uptr {
192   kExternalTagNone = 0,
193   kExternalTagSwiftModifyingAccess = 1,
194   kExternalTagFirstUserAvailable = 2,
195   kExternalTagMax = 1024,
196   // Don't set kExternalTagMax over 65,536, since MBlock only stores tags
197   // as 16-bit values, see tsan_defs.h.
198 };
199 
200 enum {
201   MutexTypeReport = MutexLastCommon,
202   MutexTypeSyncVar,
203   MutexTypeAnnotations,
204   MutexTypeAtExit,
205   MutexTypeFired,
206   MutexTypeRacy,
207   MutexTypeGlobalProc,
208   MutexTypeInternalAlloc,
209   MutexTypeTrace,
210   MutexTypeSlot,
211   MutexTypeSlots,
212 };
213 
214 }  // namespace __tsan
215 
216 #endif  // TSAN_DEFS_H
217