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