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