Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 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 | |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Alexey Samsonov | f7667cc | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 19 | #include "tsan_stat.h" |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 20 | #include "ubsan/ubsan_platform.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 21 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 30 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 31 | #ifndef TSAN_CONTAINS_UBSAN |
| 32 | # define TSAN_CONTAINS_UBSAN (CAN_SANITIZE_UB && !defined(SANITIZER_GO)) |
| 33 | #endif |
| 34 | |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 35 | namespace __tsan { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 36 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 37 | #ifdef SANITIZER_GO |
Dmitry Vyukov | 3fb70e3 | 2012-11-28 13:01:32 +0000 | [diff] [blame] | 38 | const bool kGoMode = true; |
| 39 | const bool kCppMode = false; |
Dmitry Vyukov | 79c9836 | 2012-11-08 11:32:40 +0000 | [diff] [blame] | 40 | const char *const kTsanOptionsEnv = "GORACE"; |
| 41 | #else |
Dmitry Vyukov | 3fb70e3 | 2012-11-28 13:01:32 +0000 | [diff] [blame] | 42 | const bool kGoMode = false; |
| 43 | const bool kCppMode = true; |
Dmitry Vyukov | 79c9836 | 2012-11-08 11:32:40 +0000 | [diff] [blame] | 44 | const char *const kTsanOptionsEnv = "TSAN_OPTIONS"; |
| 45 | #endif |
| 46 | |
Dmitry Vyukov | 9d2ffc2 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 47 | const int kTidBits = 13; |
Kostya Serebryany | 3d6ae15 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 48 | const unsigned kMaxTid = 1 << kTidBits; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 49 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 069ce82 | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 50 | const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 51 | #else |
| 52 | const unsigned kMaxTidInClock = kMaxTid; // Go does not track freed memory. |
| 53 | #endif |
Dmitry Vyukov | 334553e | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 54 | const int kClkBits = 42; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 55 | const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1; |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 56 | const uptr kShadowStackSize = 64 * 1024; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 57 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | // Count of shadow values in a shadow cell. |
Dmitry Vyukov | 993e2e0 | 2012-11-15 18:44:22 +0000 | [diff] [blame] | 59 | const uptr kShadowCnt = 4; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 60 | |
| 61 | // That many user bytes are mapped onto a single shadow cell. |
Dmitry Vyukov | 6c51d6e | 2012-11-06 13:21:06 +0000 | [diff] [blame] | 62 | const uptr kShadowCell = 8; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 63 | |
| 64 | // Size of a single shadow value (u64). |
Dmitry Vyukov | 6c51d6e | 2012-11-06 13:21:06 +0000 | [diff] [blame] | 65 | const uptr kShadowSize = 8; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 66 | |
Dmitry Vyukov | a05fcc1 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 67 | // Shadow memory is kShadowMultiplier times larger than user memory. |
| 68 | const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell; |
| 69 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 70 | // That many user bytes are mapped onto a single meta shadow cell. |
| 71 | // Must be less or equal to minimal memory allocator alignment. |
| 72 | const uptr kMetaShadowCell = 8; |
| 73 | |
| 74 | // Size of a single meta shadow value (u32). |
| 75 | const uptr kMetaShadowSize = 4; |
| 76 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 77 | #if TSAN_NO_HISTORY |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 78 | const bool kCollectHistory = false; |
| 79 | #else |
| 80 | const bool kCollectHistory = true; |
| 81 | #endif |
| 82 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 83 | const unsigned kInvalidTid = (unsigned)-1; |
| 84 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 85 | // The following "build consistency" machinery ensures that all source files |
| 86 | // are built in the same configuration. Inconsistent builds lead to |
| 87 | // hard to debug crashes. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 88 | #if SANITIZER_DEBUG |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 89 | void build_consistency_debug(); |
| 90 | #else |
| 91 | void build_consistency_release(); |
| 92 | #endif |
| 93 | |
| 94 | #if TSAN_COLLECT_STATS |
| 95 | void build_consistency_stats(); |
| 96 | #else |
| 97 | void build_consistency_nostats(); |
| 98 | #endif |
| 99 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 100 | static inline void USED build_consistency() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 101 | #if SANITIZER_DEBUG |
Dmitry Vyukov | 5aa3f22 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 102 | build_consistency_debug(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 103 | #else |
Dmitry Vyukov | 5aa3f22 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 104 | build_consistency_release(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 105 | #endif |
| 106 | #if TSAN_COLLECT_STATS |
Dmitry Vyukov | 5aa3f22 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 107 | build_consistency_stats(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 108 | #else |
Dmitry Vyukov | 5aa3f22 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 109 | build_consistency_nostats(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 110 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | template<typename T> |
| 114 | T min(T a, T b) { |
| 115 | return a < b ? a : b; |
| 116 | } |
| 117 | |
| 118 | template<typename T> |
| 119 | T max(T a, T b) { |
| 120 | return a > b ? a : b; |
| 121 | } |
| 122 | |
| 123 | template<typename T> |
Dmitry Vyukov | 0415ac0 | 2012-12-04 12:19:53 +0000 | [diff] [blame] | 124 | T RoundUp(T p, u64 align) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 125 | DCHECK_EQ(align & (align - 1), 0); |
| 126 | return (T)(((u64)p + align - 1) & ~(align - 1)); |
| 127 | } |
| 128 | |
Dmitry Vyukov | 0415ac0 | 2012-12-04 12:19:53 +0000 | [diff] [blame] | 129 | template<typename T> |
| 130 | T RoundDown(T p, u64 align) { |
| 131 | DCHECK_EQ(align & (align - 1), 0); |
| 132 | return (T)((u64)p & ~(align - 1)); |
| 133 | } |
| 134 | |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 135 | // Zeroizes high part, returns 'bits' lsb bits. |
| 136 | template<typename T> |
| 137 | T GetLsb(T v, int bits) { |
| 138 | return (T)((u64)v & ((1ull << bits) - 1)); |
| 139 | } |
| 140 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 141 | struct MD5Hash { |
| 142 | u64 hash[2]; |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 143 | bool operator==(const MD5Hash &other) const; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | MD5Hash md5_hash(const void *data, uptr size); |
| 147 | |
| 148 | struct ThreadState; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 149 | class ThreadContext; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 150 | struct Context; |
| 151 | struct ReportStack; |
| 152 | class ReportDesc; |
| 153 | class RegionAlloc; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 154 | |
| 155 | // Descriptor of user's memory block. |
| 156 | struct MBlock { |
| 157 | u64 siz; |
| 158 | u32 stk; |
| 159 | u16 tid; |
| 160 | }; |
| 161 | |
| 162 | COMPILER_CHECK(sizeof(MBlock) == 16); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 163 | |
| 164 | } // namespace __tsan |
| 165 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 166 | #endif // TSAN_DEFS_H |