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