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