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 21 #ifndef TSAN_DEBUG 22 #define TSAN_DEBUG 0 23 #endif // TSAN_DEBUG 24 25 namespace __tsan { 26 27 const int kTidBits = 13; 28 const unsigned kMaxTid = 1 << kTidBits; 29 const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit. 30 const int kClkBits = 43; 31 #ifndef TSAN_GO 32 const int kShadowStackSize = 4 * 1024; 33 const int kTraceStackSize = 256; 34 #endif 35 36 #ifdef TSAN_SHADOW_COUNT 37 # if TSAN_SHADOW_COUNT == 2 \ 38 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8 39 const unsigned kShadowCnt = TSAN_SHADOW_COUNT; 40 # else 41 # error "TSAN_SHADOW_COUNT must be one of 2,4,8" 42 # endif 43 #else 44 // Count of shadow values in a shadow cell. 45 const unsigned kShadowCnt = 8; 46 #endif 47 48 // That many user bytes are mapped onto a single shadow cell. 49 const unsigned kShadowCell = 8; 50 51 // Size of a single shadow value (u64). 52 const unsigned kShadowSize = 8; 53 54 #if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS 55 const bool kCollectStats = true; 56 #else 57 const bool kCollectStats = false; 58 #endif 59 60 // The following "build consistency" machinery ensures that all source files 61 // are built in the same configuration. Inconsistent builds lead to 62 // hard to debug crashes. 63 #if TSAN_DEBUG 64 void build_consistency_debug(); 65 #else 66 void build_consistency_release(); 67 #endif 68 69 #if TSAN_COLLECT_STATS 70 void build_consistency_stats(); 71 #else 72 void build_consistency_nostats(); 73 #endif 74 75 #if TSAN_SHADOW_COUNT == 1 76 void build_consistency_shadow1(); 77 #elif TSAN_SHADOW_COUNT == 2 78 void build_consistency_shadow2(); 79 #elif TSAN_SHADOW_COUNT == 4 80 void build_consistency_shadow4(); 81 #else 82 void build_consistency_shadow8(); 83 #endif 84 85 static inline void USED build_consistency() { 86 #if TSAN_DEBUG 87 build_consistency_debug(); 88 #else 89 build_consistency_release(); 90 #endif 91 #if TSAN_COLLECT_STATS 92 build_consistency_stats(); 93 #else 94 build_consistency_nostats(); 95 #endif 96 #if TSAN_SHADOW_COUNT == 1 97 build_consistency_shadow1(); 98 #elif TSAN_SHADOW_COUNT == 2 99 build_consistency_shadow2(); 100 #elif TSAN_SHADOW_COUNT == 4 101 build_consistency_shadow4(); 102 #else 103 build_consistency_shadow8(); 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, int align) { 119 DCHECK_EQ(align & (align - 1), 0); 120 return (T)(((u64)p + align - 1) & ~(align - 1)); 121 } 122 123 struct MD5Hash { 124 u64 hash[2]; 125 bool operator==(const MD5Hash &other) const; 126 }; 127 128 MD5Hash md5_hash(const void *data, uptr size); 129 130 struct ThreadState; 131 struct ThreadContext; 132 struct Context; 133 struct ReportStack; 134 class ReportDesc; 135 class RegionAlloc; 136 class StackTrace; 137 struct MBlock; 138 139 } // namespace __tsan 140 141 #endif // TSAN_DEFS_H 142