1 //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 //===----------------------------------------------------------------------===//
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"
22 // Setup defaults for compile definitions.
23 #ifndef TSAN_NO_HISTORY
24 # define TSAN_NO_HISTORY 0
27 #ifndef TSAN_COLLECT_STATS
28 # define TSAN_COLLECT_STATS 0
31 #ifndef TSAN_CONTAINS_UBSAN
32 # define TSAN_CONTAINS_UBSAN (CAN_SANITIZE_UB && !defined(SANITIZER_GO))
38 const bool kGoMode = true;
39 const bool kCppMode = false;
40 const char *const kTsanOptionsEnv = "GORACE";
41 // Go linker does not support weak symbols.
44 const bool kGoMode = false;
45 const bool kCppMode = true;
46 const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
50 const int kTidBits = 13;
51 const unsigned kMaxTid = 1 << kTidBits;
53 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
55 const unsigned kMaxTidInClock = kMaxTid; // Go does not track freed memory.
57 const int kClkBits = 42;
58 const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
59 const uptr kShadowStackSize = 64 * 1024;
61 // Count of shadow values in a shadow cell.
62 const uptr kShadowCnt = 4;
64 // That many user bytes are mapped onto a single shadow cell.
65 const uptr kShadowCell = 8;
67 // Size of a single shadow value (u64).
68 const uptr kShadowSize = 8;
70 // Shadow memory is kShadowMultiplier times larger than user memory.
71 const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
73 // That many user bytes are mapped onto a single meta shadow cell.
74 // Must be less or equal to minimal memory allocator alignment.
75 const uptr kMetaShadowCell = 8;
77 // Size of a single meta shadow value (u32).
78 const uptr kMetaShadowSize = 4;
81 const bool kCollectHistory = false;
83 const bool kCollectHistory = true;
86 // The following "build consistency" machinery ensures that all source files
87 // are built in the same configuration. Inconsistent builds lead to
88 // hard to debug crashes.
90 void build_consistency_debug();
92 void build_consistency_release();
95 #if TSAN_COLLECT_STATS
96 void build_consistency_stats();
98 void build_consistency_nostats();
101 static inline void USED build_consistency() {
103 build_consistency_debug();
105 build_consistency_release();
107 #if TSAN_COLLECT_STATS
108 build_consistency_stats();
110 build_consistency_nostats();
116 return a < b ? a : b;
121 return a > b ? a : b;
125 T RoundUp(T p, u64 align) {
126 DCHECK_EQ(align & (align - 1), 0);
127 return (T)(((u64)p + align - 1) & ~(align - 1));
131 T RoundDown(T p, u64 align) {
132 DCHECK_EQ(align & (align - 1), 0);
133 return (T)((u64)p & ~(align - 1));
136 // Zeroizes high part, returns 'bits' lsb bits.
138 T GetLsb(T v, int bits) {
139 return (T)((u64)v & ((1ull << bits) - 1));
144 bool operator==(const MD5Hash &other) const;
147 MD5Hash md5_hash(const void *data, uptr size);
156 // Descriptor of user's memory block.
163 COMPILER_CHECK(sizeof(MBlock) == 16);
165 } // namespace __tsan
167 #endif // TSAN_DEFS_H