1 //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
16 #include "sanitizer_common/sanitizer_internal_defs.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "tsan_stat.h"
19 #include "ubsan/ubsan_platform.h"
21 // Setup defaults for compile definitions.
22 #ifndef TSAN_NO_HISTORY
23 # define TSAN_NO_HISTORY 0
26 #ifndef TSAN_COLLECT_STATS
27 # define TSAN_COLLECT_STATS 0
30 #ifndef TSAN_CONTAINS_UBSAN
31 # if CAN_SANITIZE_UB && !SANITIZER_GO
32 # define TSAN_CONTAINS_UBSAN 1
34 # define TSAN_CONTAINS_UBSAN 0
40 const int kClkBits = 42;
41 const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
45 u64 reused : 64 - kClkBits; // tid reuse count
49 static const uptr kSize = 512;
50 static const uptr kTableSize = kSize / sizeof(u32);
51 static const uptr kClockCount = kSize / sizeof(ClockElem);
52 static const uptr kRefIdx = kTableSize - 1;
53 static const uptr kBlockIdx = kTableSize - 2;
56 u32 table[kTableSize];
57 ClockElem clock[kClockCount];
64 const int kTidBits = 13;
65 // Reduce kMaxTid by kClockCount because one slot in ClockBlock table is
66 // occupied by reference counter, so total number of elements we can store
67 // in SyncClock is kClockCount * (kTableSize - 1).
68 const unsigned kMaxTid = (1 << kTidBits) - ClockBlock::kClockCount;
70 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
72 const unsigned kMaxTidInClock = kMaxTid; // Go does not track freed memory.
74 const uptr kShadowStackSize = 64 * 1024;
76 // Count of shadow values in a shadow cell.
77 const uptr kShadowCnt = 4;
79 // That many user bytes are mapped onto a single shadow cell.
80 const uptr kShadowCell = 8;
82 // Size of a single shadow value (u64).
83 const uptr kShadowSize = 8;
85 // Shadow memory is kShadowMultiplier times larger than user memory.
86 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
88 // That many user bytes are mapped onto a single meta shadow cell.
89 // Must be less or equal to minimal memory allocator alignment.
90 const uptr kMetaShadowCell = 8;
92 // Size of a single meta shadow value (u32).
93 const uptr kMetaShadowSize = 4;
96 const bool kCollectHistory = false;
98 const bool kCollectHistory = true;
101 const u16 kInvalidTid = kMaxTid + 1;
103 // The following "build consistency" machinery ensures that all source files
104 // are built in the same configuration. Inconsistent builds lead to
105 // hard to debug crashes.
107 void build_consistency_debug();
109 void build_consistency_release();
112 #if TSAN_COLLECT_STATS
113 void build_consistency_stats();
115 void build_consistency_nostats();
118 static inline void USED build_consistency() {
120 build_consistency_debug();
122 build_consistency_release();
124 #if TSAN_COLLECT_STATS
125 build_consistency_stats();
127 build_consistency_nostats();
133 return a < b ? a : b;
138 return a > b ? a : b;
142 T RoundUp(T p, u64 align) {
143 DCHECK_EQ(align & (align - 1), 0);
144 return (T)(((u64)p + align - 1) & ~(align - 1));
148 T RoundDown(T p, u64 align) {
149 DCHECK_EQ(align & (align - 1), 0);
150 return (T)((u64)p & ~(align - 1));
153 // Zeroizes high part, returns 'bits' lsb bits.
155 T GetLsb(T v, int bits) {
156 return (T)((u64)v & ((1ull << bits) - 1));
161 bool operator==(const MD5Hash &other) const;
164 MD5Hash md5_hash(const void *data, uptr size);
174 // Descriptor of user's memory block.
182 COMPILER_CHECK(sizeof(MBlock) == 16);
184 enum ExternalTag : uptr {
185 kExternalTagNone = 0,
186 kExternalTagSwiftModifyingAccess = 1,
187 kExternalTagFirstUserAvailable = 2,
188 kExternalTagMax = 1024,
189 // Don't set kExternalTagMax over 65,536, since MBlock only stores tags
190 // as 16-bit values, see tsan_defs.h.
193 } // namespace __tsan
195 #endif // TSAN_DEFS_H