| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_rtl.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 | // Main internal TSan header file. | 
|  | 13 | // | 
|  | 14 | // Ground rules: | 
|  | 15 | //   - C++ run-time should not be used (static CTORs, RTTI, exceptions, static | 
|  | 16 | //     function-scope locals) | 
|  | 17 | //   - All functions/classes/etc reside in namespace __tsan, except for those | 
|  | 18 | //     declared in tsan_interface.h. | 
|  | 19 | //   - Platform-specific files should be used instead of ifdefs (*). | 
|  | 20 | //   - No system headers included in header files (*). | 
|  | 21 | //   - Platform specific headres included only into platform-specific files (*). | 
|  | 22 | // | 
|  | 23 | //  (*) Except when inlining is critical for performance. | 
|  | 24 | //===----------------------------------------------------------------------===// | 
|  | 25 |  | 
|  | 26 | #ifndef TSAN_RTL_H | 
|  | 27 | #define TSAN_RTL_H | 
|  | 28 |  | 
| Alexey Samsonov | bc3a7e3 | 2012-06-06 06:47:26 +0000 | [diff] [blame] | 29 | #include "sanitizer_common/sanitizer_common.h" | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 30 | #include "sanitizer_common/sanitizer_allocator64.h" | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 31 | #include "tsan_clock.h" | 
|  | 32 | #include "tsan_defs.h" | 
|  | 33 | #include "tsan_flags.h" | 
|  | 34 | #include "tsan_sync.h" | 
|  | 35 | #include "tsan_trace.h" | 
|  | 36 | #include "tsan_vector.h" | 
|  | 37 | #include "tsan_report.h" | 
|  | 38 |  | 
|  | 39 | namespace __tsan { | 
|  | 40 |  | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 41 | // Descriptor of user's memory block. | 
|  | 42 | struct MBlock { | 
| Dmitry Vyukov | 9f1509f | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 43 | Mutex mtx; | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 44 | uptr size; | 
| Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 45 | u32 alloc_tid; | 
|  | 46 | u32 alloc_stack_id; | 
| Dmitry Vyukov | 9f1509f | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 47 | SyncVar *head; | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 48 | }; | 
|  | 49 |  | 
|  | 50 | #ifndef TSAN_GO | 
|  | 51 | #if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW | 
| Dmitry Vyukov | f77c6ea | 2012-08-16 13:27:25 +0000 | [diff] [blame] | 52 | const uptr kAllocatorSpace = 0x7d0000000000ULL; | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 53 | #else | 
|  | 54 | const uptr kAllocatorSpace = 0x7d0000000000ULL; | 
|  | 55 | #endif | 
|  | 56 | const uptr kAllocatorSize  =  0x10000000000ULL;  // 1T. | 
|  | 57 |  | 
|  | 58 | typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock), | 
|  | 59 | DefaultSizeClassMap> PrimaryAllocator; | 
|  | 60 | typedef SizeClassAllocatorLocalCache<PrimaryAllocator::kNumClasses, | 
|  | 61 | PrimaryAllocator> AllocatorCache; | 
|  | 62 | typedef LargeMmapAllocator SecondaryAllocator; | 
|  | 63 | typedef CombinedAllocator<PrimaryAllocator, AllocatorCache, | 
|  | 64 | SecondaryAllocator> Allocator; | 
| Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 65 | Allocator *allocator(); | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 66 | #endif | 
|  | 67 |  | 
| Alexey Samsonov | 5c6b93b | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 68 | void TsanCheckFailed(const char *file, int line, const char *cond, | 
|  | 69 | u64 v1, u64 v2); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 70 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 71 | // FastState (from most significant bit): | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 72 | //   unused          : 1 | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 73 | //   tid             : kTidBits | 
|  | 74 | //   epoch           : kClkBits | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 75 | //   unused          : - | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 76 | //   ignore_bit      : 1 | 
|  | 77 | class FastState { | 
|  | 78 | public: | 
|  | 79 | FastState(u64 tid, u64 epoch) { | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 80 | x_ = tid << kTidShift; | 
|  | 81 | x_ |= epoch << kClkShift; | 
|  | 82 | DCHECK(tid == this->tid()); | 
|  | 83 | DCHECK(epoch == this->epoch()); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | explicit FastState(u64 x) | 
|  | 87 | : x_(x) { | 
|  | 88 | } | 
|  | 89 |  | 
| Dmitry Vyukov | 3482ec3 | 2012-08-16 15:08:49 +0000 | [diff] [blame] | 90 | u64 raw() const { | 
|  | 91 | return x_; | 
|  | 92 | } | 
|  | 93 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 94 | u64 tid() const { | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 95 | u64 res = x_ >> kTidShift; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 96 | return res; | 
|  | 97 | } | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 98 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 99 | u64 epoch() const { | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 100 | u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 101 | return res; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 102 | } | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 103 |  | 
|  | 104 | void IncrementEpoch() { | 
|  | 105 | u64 old_epoch = epoch(); | 
|  | 106 | x_ += 1 << kClkShift; | 
| Dmitry Vyukov | 163a8338 | 2012-05-21 10:20:53 +0000 | [diff] [blame] | 107 | DCHECK_EQ(old_epoch + 1, epoch()); | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 108 | (void)old_epoch; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | void SetIgnoreBit() { x_ |= kIgnoreBit; } | 
|  | 112 | void ClearIgnoreBit() { x_ &= ~kIgnoreBit; } | 
| Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 113 | bool GetIgnoreBit() const { return x_ & kIgnoreBit; } | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 114 |  | 
|  | 115 | private: | 
|  | 116 | friend class Shadow; | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 117 | static const int kTidShift = 64 - kTidBits - 1; | 
|  | 118 | static const int kClkShift = kTidShift - kClkBits; | 
|  | 119 | static const u64 kIgnoreBit = 1ull; | 
|  | 120 | static const u64 kFreedBit = 1ull << 63; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 121 | u64 x_; | 
|  | 122 | }; | 
|  | 123 |  | 
|  | 124 | // Shadow (from most significant bit): | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 125 | //   freed           : 1 | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 126 | //   tid             : kTidBits | 
|  | 127 | //   epoch           : kClkBits | 
|  | 128 | //   is_write        : 1 | 
|  | 129 | //   size_log        : 2 | 
|  | 130 | //   addr0           : 3 | 
| Dmitry Vyukov | 97c26bd | 2012-06-27 16:05:06 +0000 | [diff] [blame] | 131 | class Shadow : public FastState { | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 132 | public: | 
|  | 133 | explicit Shadow(u64 x) : FastState(x) { } | 
|  | 134 |  | 
|  | 135 | explicit Shadow(const FastState &s) : FastState(s.x_) { } | 
|  | 136 |  | 
|  | 137 | void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) { | 
|  | 138 | DCHECK_EQ(x_ & 31, 0); | 
|  | 139 | DCHECK_LE(addr0, 7); | 
|  | 140 | DCHECK_LE(kAccessSizeLog, 3); | 
|  | 141 | x_ |= (kAccessSizeLog << 3) | addr0; | 
|  | 142 | DCHECK_EQ(kAccessSizeLog, size_log()); | 
|  | 143 | DCHECK_EQ(addr0, this->addr0()); | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | void SetWrite(unsigned kAccessIsWrite) { | 
|  | 147 | DCHECK_EQ(x_ & 32, 0); | 
|  | 148 | if (kAccessIsWrite) | 
|  | 149 | x_ |= 32; | 
|  | 150 | DCHECK_EQ(kAccessIsWrite, is_write()); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | bool IsZero() const { return x_ == 0; } | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 154 |  | 
| Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 155 | static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) { | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 156 | u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 157 | DCHECK_EQ(shifted_xor == 0, s1.tid() == s2.tid()); | 
|  | 158 | return shifted_xor == 0; | 
|  | 159 | } | 
| Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 160 |  | 
|  | 161 | static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) { | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 162 | u64 masked_xor = (s1.x_ ^ s2.x_) & 31; | 
|  | 163 | return masked_xor == 0; | 
|  | 164 | } | 
|  | 165 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 166 | static inline bool TwoRangesIntersect(Shadow s1, Shadow s2, | 
|  | 167 | unsigned kS2AccessSize) { | 
|  | 168 | bool res = false; | 
|  | 169 | u64 diff = s1.addr0() - s2.addr0(); | 
|  | 170 | if ((s64)diff < 0) {  // s1.addr0 < s2.addr0  // NOLINT | 
|  | 171 | // if (s1.addr0() + size1) > s2.addr0()) return true; | 
|  | 172 | if (s1.size() > -diff)  res = true; | 
|  | 173 | } else { | 
|  | 174 | // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true; | 
|  | 175 | if (kS2AccessSize > diff) res = true; | 
|  | 176 | } | 
|  | 177 | DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2)); | 
|  | 178 | DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1)); | 
|  | 179 | return res; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | // The idea behind the offset is as follows. | 
|  | 183 | // Consider that we have 8 bool's contained within a single 8-byte block | 
|  | 184 | // (mapped to a single shadow "cell"). Now consider that we write to the bools | 
|  | 185 | // from a single thread (which we consider the common case). | 
|  | 186 | // W/o offsetting each access will have to scan 4 shadow values at average | 
|  | 187 | // to find the corresponding shadow value for the bool. | 
|  | 188 | // With offsetting we start scanning shadow with the offset so that | 
|  | 189 | // each access hits necessary shadow straight off (at least in an expected | 
|  | 190 | // optimistic case). | 
|  | 191 | // This logic works seamlessly for any layout of user data. For example, | 
|  | 192 | // if user data is {int, short, char, char}, then accesses to the int are | 
|  | 193 | // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses | 
|  | 194 | // from a single thread won't need to scan all 8 shadow values. | 
|  | 195 | unsigned ComputeSearchOffset() { | 
|  | 196 | return x_ & 7; | 
|  | 197 | } | 
|  | 198 | u64 addr0() const { return x_ & 7; } | 
|  | 199 | u64 size() const { return 1ull << size_log(); } | 
|  | 200 | bool is_write() const { return x_ & 32; } | 
|  | 201 |  | 
| Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 202 | // The idea behind the freed bit is as follows. | 
|  | 203 | // When the memory is freed (or otherwise unaccessible) we write to the shadow | 
|  | 204 | // values with tid/epoch related to the free and the freed bit set. | 
|  | 205 | // During memory accesses processing the freed bit is considered | 
|  | 206 | // as msb of tid. So any access races with shadow with freed bit set | 
|  | 207 | // (it is as if write from a thread with which we never synchronized before). | 
|  | 208 | // This allows us to detect accesses to freed memory w/o additional | 
|  | 209 | // overheads in memory access processing and at the same time restore | 
|  | 210 | // tid/epoch of free. | 
|  | 211 | void MarkAsFreed() { | 
|  | 212 | x_ |= kFreedBit; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | bool GetFreedAndReset() { | 
|  | 216 | bool res = x_ & kFreedBit; | 
|  | 217 | x_ &= ~kFreedBit; | 
|  | 218 | return res; | 
|  | 219 | } | 
|  | 220 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 221 | private: | 
|  | 222 | u64 size_log() const { return (x_ >> 3) & 3; } | 
| Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 223 |  | 
|  | 224 | static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) { | 
|  | 225 | if (s1.addr0() == s2.addr0()) return true; | 
|  | 226 | if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0()) | 
|  | 227 | return true; | 
|  | 228 | if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0()) | 
|  | 229 | return true; | 
|  | 230 | return false; | 
|  | 231 | } | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 232 | }; | 
|  | 233 |  | 
|  | 234 | // Freed memory. | 
|  | 235 | // As if 8-byte write by thread 0xff..f at epoch 0xff..f, races with everything. | 
|  | 236 | const u64 kShadowFreed = 0xfffffffffffffff8ull; | 
|  | 237 |  | 
| Dmitry Vyukov | 97c26bd | 2012-06-27 16:05:06 +0000 | [diff] [blame] | 238 | struct SignalContext; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 239 |  | 
|  | 240 | // This struct is stored in TLS. | 
|  | 241 | struct ThreadState { | 
|  | 242 | FastState fast_state; | 
|  | 243 | // Synch epoch represents the threads's epoch before the last synchronization | 
|  | 244 | // action. It allows to reduce number of shadow state updates. | 
|  | 245 | // For example, fast_synch_epoch=100, last write to addr X was at epoch=150, | 
|  | 246 | // if we are processing write to X from the same thread at epoch=200, | 
|  | 247 | // we do nothing, because both writes happen in the same 'synch epoch'. | 
|  | 248 | // That is, if another memory access does not race with the former write, | 
|  | 249 | // it does not race with the latter as well. | 
|  | 250 | // QUESTION: can we can squeeze this into ThreadState::Fast? | 
|  | 251 | // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are | 
|  | 252 | // taken by epoch between synchs. | 
|  | 253 | // This way we can save one load from tls. | 
|  | 254 | u64 fast_synch_epoch; | 
|  | 255 | // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read. | 
|  | 256 | // We do not distinguish beteween ignoring reads and writes | 
|  | 257 | // for better performance. | 
|  | 258 | int ignore_reads_and_writes; | 
|  | 259 | uptr *shadow_stack_pos; | 
|  | 260 | u64 *racy_shadow_addr; | 
|  | 261 | u64 racy_state[2]; | 
|  | 262 | Trace trace; | 
| Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 263 | #ifndef TSAN_GO | 
|  | 264 | // C/C++ uses embed shadow stack of fixed size. | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 265 | uptr shadow_stack[kShadowStackSize]; | 
| Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 266 | #else | 
|  | 267 | // Go uses satellite shadow stack with dynamic size. | 
|  | 268 | uptr *shadow_stack; | 
|  | 269 | uptr *shadow_stack_end; | 
|  | 270 | #endif | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 271 | ThreadClock clock; | 
| Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 272 | #ifndef TSAN_GO | 
|  | 273 | AllocatorCache alloc_cache; | 
|  | 274 | #endif | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 275 | u64 stat[StatCnt]; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 276 | const int tid; | 
| Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 277 | const int unique_id; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 278 | int in_rtl; | 
| Dmitry Vyukov | fa985a0 | 2012-06-28 18:07:46 +0000 | [diff] [blame] | 279 | bool is_alive; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 280 | const uptr stk_addr; | 
|  | 281 | const uptr stk_size; | 
|  | 282 | const uptr tls_addr; | 
|  | 283 | const uptr tls_size; | 
|  | 284 |  | 
|  | 285 | DeadlockDetector deadlock_detector; | 
|  | 286 |  | 
|  | 287 | bool in_signal_handler; | 
| Dmitry Vyukov | 97c26bd | 2012-06-27 16:05:06 +0000 | [diff] [blame] | 288 | SignalContext *signal_ctx; | 
|  | 289 |  | 
| Dmitry Vyukov | 318f777 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 290 | #ifndef TSAN_GO | 
|  | 291 | u32 last_sleep_stack_id; | 
|  | 292 | ThreadClock last_sleep_clock; | 
|  | 293 | #endif | 
|  | 294 |  | 
| Dmitry Vyukov | de1fd1c | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 295 | // Set in regions of runtime that must be signal-safe and fork-safe. | 
|  | 296 | // If set, malloc must not be called. | 
|  | 297 | int nomalloc; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 298 |  | 
| Dmitry Vyukov | 191f2f7 | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 299 | explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch, | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 300 | uptr stk_addr, uptr stk_size, | 
|  | 301 | uptr tls_addr, uptr tls_size); | 
|  | 302 | }; | 
|  | 303 |  | 
|  | 304 | Context *CTX(); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 305 |  | 
| Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 306 | #ifndef TSAN_GO | 
|  | 307 | extern THREADLOCAL char cur_thread_placeholder[]; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 308 | INLINE ThreadState *cur_thread() { | 
|  | 309 | return reinterpret_cast<ThreadState *>(&cur_thread_placeholder); | 
|  | 310 | } | 
| Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 311 | #endif | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 312 |  | 
|  | 313 | enum ThreadStatus { | 
|  | 314 | ThreadStatusInvalid,   // Non-existent thread, data is invalid. | 
|  | 315 | ThreadStatusCreated,   // Created but not yet running. | 
|  | 316 | ThreadStatusRunning,   // The thread is currently running. | 
|  | 317 | ThreadStatusFinished,  // Joinable thread is finished but not yet joined. | 
| Alexey Samsonov | 046248c | 2012-09-13 11:54:41 +0000 | [diff] [blame] | 318 | ThreadStatusDead       // Joined, but some info (trace) is still alive. | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 319 | }; | 
|  | 320 |  | 
|  | 321 | // An info about a thread that is hold for some time after its termination. | 
|  | 322 | struct ThreadDeadInfo { | 
|  | 323 | Trace trace; | 
|  | 324 | }; | 
|  | 325 |  | 
|  | 326 | struct ThreadContext { | 
|  | 327 | const int tid; | 
|  | 328 | int unique_id;  // Non-rolling thread id. | 
| Dmitry Vyukov | 56faa55 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 329 | uptr os_id;  // pid | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 330 | uptr user_id;  // Some opaque user thread id (e.g. pthread_t). | 
|  | 331 | ThreadState *thr; | 
|  | 332 | ThreadStatus status; | 
|  | 333 | bool detached; | 
|  | 334 | int reuse_count; | 
|  | 335 | SyncClock sync; | 
|  | 336 | // Epoch at which the thread had started. | 
|  | 337 | // If we see an event from the thread stamped by an older epoch, | 
|  | 338 | // the event is from a dead thread that shared tid with this thread. | 
|  | 339 | u64 epoch0; | 
|  | 340 | u64 epoch1; | 
|  | 341 | StackTrace creation_stack; | 
| Dmitry Vyukov | f6985e3 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 342 | ThreadDeadInfo *dead_info; | 
|  | 343 | ThreadContext *dead_next;  // In dead thread list. | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 344 |  | 
|  | 345 | explicit ThreadContext(int tid); | 
|  | 346 | }; | 
|  | 347 |  | 
|  | 348 | struct RacyStacks { | 
|  | 349 | MD5Hash hash[2]; | 
|  | 350 | bool operator==(const RacyStacks &other) const { | 
|  | 351 | if (hash[0] == other.hash[0] && hash[1] == other.hash[1]) | 
|  | 352 | return true; | 
|  | 353 | if (hash[0] == other.hash[1] && hash[1] == other.hash[0]) | 
|  | 354 | return true; | 
|  | 355 | return false; | 
|  | 356 | } | 
|  | 357 | }; | 
|  | 358 |  | 
|  | 359 | struct RacyAddress { | 
|  | 360 | uptr addr_min; | 
|  | 361 | uptr addr_max; | 
|  | 362 | }; | 
|  | 363 |  | 
| Dmitry Vyukov | 90c9cbf | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 364 | struct FiredSuppression { | 
|  | 365 | ReportType type; | 
|  | 366 | uptr pc; | 
|  | 367 | }; | 
|  | 368 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 369 | struct Context { | 
|  | 370 | Context(); | 
|  | 371 |  | 
|  | 372 | bool initialized; | 
|  | 373 |  | 
|  | 374 | SyncTab synctab; | 
|  | 375 |  | 
|  | 376 | Mutex report_mtx; | 
|  | 377 | int nreported; | 
|  | 378 | int nmissed_expected; | 
|  | 379 |  | 
|  | 380 | Mutex thread_mtx; | 
| Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 381 | unsigned thread_seq; | 
|  | 382 | unsigned unique_thread_seq; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 383 | int alive_threads; | 
|  | 384 | int max_alive_threads; | 
|  | 385 | ThreadContext *threads[kMaxTid]; | 
|  | 386 | int dead_list_size; | 
|  | 387 | ThreadContext* dead_list_head; | 
|  | 388 | ThreadContext* dead_list_tail; | 
|  | 389 |  | 
|  | 390 | Vector<RacyStacks> racy_stacks; | 
|  | 391 | Vector<RacyAddress> racy_addresses; | 
| Dmitry Vyukov | 90c9cbf | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 392 | Vector<FiredSuppression> fired_suppressions; | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 393 |  | 
|  | 394 | Flags flags; | 
|  | 395 |  | 
|  | 396 | u64 stat[StatCnt]; | 
|  | 397 | u64 int_alloc_cnt[MBlockTypeCount]; | 
|  | 398 | u64 int_alloc_siz[MBlockTypeCount]; | 
|  | 399 | }; | 
|  | 400 |  | 
|  | 401 | class ScopedInRtl { | 
|  | 402 | public: | 
|  | 403 | ScopedInRtl(); | 
|  | 404 | ~ScopedInRtl(); | 
|  | 405 | private: | 
|  | 406 | ThreadState*thr_; | 
|  | 407 | int in_rtl_; | 
|  | 408 | int errno_; | 
|  | 409 | }; | 
|  | 410 |  | 
|  | 411 | class ScopedReport { | 
|  | 412 | public: | 
|  | 413 | explicit ScopedReport(ReportType typ); | 
|  | 414 | ~ScopedReport(); | 
|  | 415 |  | 
|  | 416 | void AddStack(const StackTrace *stack); | 
|  | 417 | void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack); | 
|  | 418 | void AddThread(const ThreadContext *tctx); | 
|  | 419 | void AddMutex(const SyncVar *s); | 
|  | 420 | void AddLocation(uptr addr, uptr size); | 
| Dmitry Vyukov | 318f777 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 421 | void AddSleep(u32 stack_id); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 422 |  | 
|  | 423 | const ReportDesc *GetReport() const; | 
|  | 424 |  | 
|  | 425 | private: | 
|  | 426 | Context *ctx_; | 
|  | 427 | ReportDesc *rep_; | 
|  | 428 |  | 
|  | 429 | ScopedReport(const ScopedReport&); | 
|  | 430 | void operator = (const ScopedReport&); | 
|  | 431 | }; | 
|  | 432 |  | 
| Dmitry Vyukov | 3482ec3 | 2012-08-16 15:08:49 +0000 | [diff] [blame] | 433 | void RestoreStack(int tid, const u64 epoch, StackTrace *stk); | 
|  | 434 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 435 | void StatAggregate(u64 *dst, u64 *src); | 
|  | 436 | void StatOutput(u64 *stat); | 
|  | 437 | void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) { | 
|  | 438 | if (kCollectStats) | 
|  | 439 | thr->stat[typ] += n; | 
|  | 440 | } | 
|  | 441 |  | 
| Dmitry Vyukov | c015712 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 442 | void MapShadow(uptr addr, uptr size); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 443 | void InitializeShadowMemory(); | 
|  | 444 | void InitializeInterceptors(); | 
|  | 445 | void InitializeDynamicAnnotations(); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 446 |  | 
|  | 447 | void ReportRace(ThreadState *thr); | 
| Dmitry Vyukov | 90c9cbf | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 448 | bool OutputReport(Context *ctx, | 
|  | 449 | const ScopedReport &srep, | 
| Dmitry Vyukov | 665ce2a | 2012-05-14 15:28:03 +0000 | [diff] [blame] | 450 | const ReportStack *suppress_stack = 0); | 
| Dmitry Vyukov | 90c9cbf | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 451 | bool IsFiredSuppression(Context *ctx, | 
|  | 452 | const ScopedReport &srep, | 
|  | 453 | const StackTrace &trace); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 454 | bool IsExpectedReport(uptr addr, uptr size); | 
|  | 455 |  | 
|  | 456 | #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1 | 
| Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 457 | # define DPrintf Printf | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 458 | #else | 
|  | 459 | # define DPrintf(...) | 
|  | 460 | #endif | 
|  | 461 |  | 
|  | 462 | #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2 | 
| Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 463 | # define DPrintf2 Printf | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 464 | #else | 
|  | 465 | # define DPrintf2(...) | 
|  | 466 | #endif | 
|  | 467 |  | 
| Dmitry Vyukov | 318f777 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 468 | u32 CurrentStackId(ThreadState *thr, uptr pc); | 
| Dmitry Vyukov | 46ca1fb | 2012-09-01 12:13:18 +0000 | [diff] [blame] | 469 | void PrintCurrentStack(ThreadState *thr, uptr pc); | 
| Dmitry Vyukov | 318f777 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 470 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 471 | void Initialize(ThreadState *thr); | 
|  | 472 | int Finalize(ThreadState *thr); | 
|  | 473 |  | 
|  | 474 | void MemoryAccess(ThreadState *thr, uptr pc, uptr addr, | 
|  | 475 | int kAccessSizeLog, bool kAccessIsWrite); | 
|  | 476 | void MemoryAccessImpl(ThreadState *thr, uptr addr, | 
|  | 477 | int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state, | 
|  | 478 | u64 *shadow_mem, Shadow cur); | 
|  | 479 | void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr); | 
|  | 480 | void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr); | 
|  | 481 | void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr); | 
|  | 482 | void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr); | 
|  | 483 | void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr, | 
|  | 484 | uptr size, bool is_write); | 
|  | 485 | void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size); | 
|  | 486 | void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size); | 
| Dmitry Vyukov | 9f1509f | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 487 | void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 488 | void IgnoreCtl(ThreadState *thr, bool write, bool begin); | 
|  | 489 |  | 
|  | 490 | void FuncEntry(ThreadState *thr, uptr pc); | 
|  | 491 | void FuncExit(ThreadState *thr); | 
|  | 492 |  | 
|  | 493 | int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached); | 
| Dmitry Vyukov | 56faa55 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 494 | void ThreadStart(ThreadState *thr, int tid, uptr os_id); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 495 | void ThreadFinish(ThreadState *thr); | 
|  | 496 | int ThreadTid(ThreadState *thr, uptr pc, uptr uid); | 
|  | 497 | void ThreadJoin(ThreadState *thr, uptr pc, int tid); | 
|  | 498 | void ThreadDetach(ThreadState *thr, uptr pc, int tid); | 
|  | 499 | void ThreadFinalize(ThreadState *thr); | 
|  | 500 |  | 
| Dmitry Vyukov | 4723e6b | 2012-08-16 13:29:41 +0000 | [diff] [blame] | 501 | void MutexCreate(ThreadState *thr, uptr pc, uptr addr, | 
|  | 502 | bool rw, bool recursive, bool linker_init); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 503 | void MutexDestroy(ThreadState *thr, uptr pc, uptr addr); | 
|  | 504 | void MutexLock(ThreadState *thr, uptr pc, uptr addr); | 
|  | 505 | void MutexUnlock(ThreadState *thr, uptr pc, uptr addr); | 
|  | 506 | void MutexReadLock(ThreadState *thr, uptr pc, uptr addr); | 
|  | 507 | void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr); | 
|  | 508 | void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr); | 
|  | 509 |  | 
|  | 510 | void Acquire(ThreadState *thr, uptr pc, uptr addr); | 
| Dmitry Vyukov | e11f292 | 2012-11-07 15:08:20 +0000 | [diff] [blame^] | 511 | void AcquireGlobal(ThreadState *thr, uptr pc); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 512 | void Release(ThreadState *thr, uptr pc, uptr addr); | 
| Dmitry Vyukov | 904d3f9 | 2012-07-28 15:27:41 +0000 | [diff] [blame] | 513 | void ReleaseStore(ThreadState *thr, uptr pc, uptr addr); | 
| Dmitry Vyukov | 318f777 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 514 | void AfterSleep(ThreadState *thr, uptr pc); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 515 |  | 
|  | 516 | // The hacky call uses custom calling convention and an assembly thunk. | 
|  | 517 | // It is considerably faster that a normal call for the caller | 
|  | 518 | // if it is not executed (it is intended for slow paths from hot functions). | 
|  | 519 | // The trick is that the call preserves all registers and the compiler | 
|  | 520 | // does not treat it as a call. | 
|  | 521 | // If it does not work for you, use normal call. | 
|  | 522 | #if TSAN_DEBUG == 0 | 
|  | 523 | // The caller may not create the stack frame for itself at all, | 
|  | 524 | // so we create a reserve stack frame for it (1024b must be enough). | 
|  | 525 | #define HACKY_CALL(f) \ | 
| Dmitry Vyukov | b7f1852 | 2012-09-02 11:24:07 +0000 | [diff] [blame] | 526 | __asm__ __volatile__("sub $1024, %%rsp;" \ | 
|  | 527 | "/*.cfi_adjust_cfa_offset 1024;*/" \ | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 528 | "call " #f "_thunk;" \ | 
| Dmitry Vyukov | b7f1852 | 2012-09-02 11:24:07 +0000 | [diff] [blame] | 529 | "add $1024, %%rsp;" \ | 
|  | 530 | "/*.cfi_adjust_cfa_offset -1024;*/" \ | 
|  | 531 | ::: "memory", "cc"); | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 532 | #else | 
|  | 533 | #define HACKY_CALL(f) f() | 
|  | 534 | #endif | 
|  | 535 |  | 
| Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 536 | void TraceSwitch(ThreadState *thr); | 
|  | 537 |  | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 538 | extern "C" void __tsan_trace_switch(); | 
|  | 539 | void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, u64 epoch, | 
|  | 540 | EventType typ, uptr addr) { | 
|  | 541 | StatInc(thr, StatEvents); | 
| Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 542 | if (UNLIKELY((epoch % kTracePartSize) == 0)) { | 
|  | 543 | #ifndef TSAN_GO | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 544 | HACKY_CALL(__tsan_trace_switch); | 
| Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 545 | #else | 
|  | 546 | TraceSwitch(thr); | 
|  | 547 | #endif | 
|  | 548 | } | 
| Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 549 | Event *evp = &thr->trace.events[epoch % kTraceSize]; | 
|  | 550 | Event ev = (u64)addr | ((u64)typ << 61); | 
|  | 551 | *evp = ev; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | }  // namespace __tsan | 
|  | 555 |  | 
|  | 556 | #endif  // TSAN_RTL_H |