blob: 5445f575a4bf9b2127b6fb72719229e16d235098 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- 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 Serebryany571232b2012-12-05 10:09:15 +000029#include "sanitizer_common/sanitizer_allocator.h"
Alexey Samsonovc30e2d62013-05-29 09:15:39 +000030#include "sanitizer_common/sanitizer_allocator_internal.h"
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000031#include "sanitizer_common/sanitizer_common.h"
Sergey Matveevd109eb02013-06-26 15:37:14 +000032#include "sanitizer_common/sanitizer_suppressions.h"
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000033#include "sanitizer_common/sanitizer_thread_registry.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000034#include "tsan_clock.h"
35#include "tsan_defs.h"
36#include "tsan_flags.h"
37#include "tsan_sync.h"
38#include "tsan_trace.h"
39#include "tsan_vector.h"
40#include "tsan_report.h"
Dmitry Vyukov2429b022012-11-28 10:35:31 +000041#include "tsan_platform.h"
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000042#include "tsan_mutexset.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000043
Kostya Serebryany242b6302012-12-04 15:13:30 +000044#if SANITIZER_WORDSIZE != 64
45# error "ThreadSanitizer is supported only on 64-bit platforms"
46#endif
47
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048namespace __tsan {
49
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000050// Descriptor of user's memory block.
51struct MBlock {
Dmitry Vyukov4ddd37b2013-03-18 19:47:36 +000052 /*
53 u64 mtx : 1; // must be first
54 u64 lst : 44;
55 u64 stk : 31; // on word boundary
56 u64 tid : kTidBits;
57 u64 siz : 128 - 1 - 31 - 44 - kTidBits; // 39
58 */
59 u64 raw[2];
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000060
Dmitry Vyukov4ddd37b2013-03-18 19:47:36 +000061 void Init(uptr siz, u32 tid, u32 stk) {
62 raw[0] = raw[1] = 0;
63 raw[1] |= (u64)siz << ((1 + 44 + 31 + kTidBits) % 64);
64 raw[1] |= (u64)tid << ((1 + 44 + 31) % 64);
65 raw[0] |= (u64)stk << (1 + 44);
66 raw[1] |= (u64)stk >> (64 - 44 - 1);
67 DCHECK_EQ(Size(), siz);
68 DCHECK_EQ(Tid(), tid);
69 DCHECK_EQ(StackId(), stk);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000070 }
Dmitry Vyukov4ddd37b2013-03-18 19:47:36 +000071
72 u32 Tid() const {
73 return GetLsb(raw[1] >> ((1 + 44 + 31) % 64), kTidBits);
74 }
75
76 uptr Size() const {
77 return raw[1] >> ((1 + 31 + 44 + kTidBits) % 64);
78 }
79
80 u32 StackId() const {
81 return (raw[0] >> (1 + 44)) | GetLsb(raw[1] << (64 - 44 - 1), 31);
82 }
83
84 SyncVar *ListHead() const {
85 return (SyncVar*)(GetLsb(raw[0] >> 1, 44) << 3);
86 }
87
88 void ListPush(SyncVar *v) {
89 SyncVar *lst = ListHead();
90 v->next = lst;
91 u64 x = (u64)v ^ (u64)lst;
92 x = (x >> 3) << 1;
93 raw[0] ^= x;
94 DCHECK_EQ(ListHead(), v);
95 }
96
97 SyncVar *ListPop() {
98 SyncVar *lst = ListHead();
99 SyncVar *nxt = lst->next;
100 lst->next = 0;
101 u64 x = (u64)lst ^ (u64)nxt;
102 x = (x >> 3) << 1;
103 raw[0] ^= x;
104 DCHECK_EQ(ListHead(), nxt);
105 return lst;
106 }
107
108 void ListReset() {
109 SyncVar *lst = ListHead();
110 u64 x = (u64)lst;
111 x = (x >> 3) << 1;
112 raw[0] ^= x;
113 DCHECK_EQ(ListHead(), 0);
114 }
115
116 void Lock();
117 void Unlock();
118 typedef GenericScopedLock<MBlock> ScopedLock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000119};
120
121#ifndef TSAN_GO
122#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukovf77c6ea2012-08-16 13:27:25 +0000123const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000124#else
125const uptr kAllocatorSpace = 0x7d0000000000ULL;
126#endif
127const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
128
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +0000129struct MapUnmapCallback;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000130typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +0000131 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Kostya Serebryanyf2992882012-12-04 14:15:17 +0000132typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +0000133typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000134typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
135 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000136Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000137#endif
138
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +0000139void TsanCheckFailed(const char *file, int line, const char *cond,
140 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000141
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000142const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
143
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000144// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000145// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000146// tid : kTidBits
147// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000148// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000149// history_size : 3
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000150class FastState {
151 public:
152 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000153 x_ = tid << kTidShift;
154 x_ |= epoch << kClkShift;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000155 DCHECK_EQ(tid, this->tid());
156 DCHECK_EQ(epoch, this->epoch());
157 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000158 }
159
160 explicit FastState(u64 x)
161 : x_(x) {
162 }
163
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000164 u64 raw() const {
165 return x_;
166 }
167
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000168 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000169 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
170 return res;
171 }
172
173 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000174 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000175 return res;
176 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000177
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000178 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000179 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000180 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000181 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000182
183 void IncrementEpoch() {
184 u64 old_epoch = epoch();
185 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000186 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000187 (void)old_epoch;
188 }
189
190 void SetIgnoreBit() { x_ |= kIgnoreBit; }
191 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000192 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000193
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000194 void SetHistorySize(int hs) {
195 CHECK_GE(hs, 0);
196 CHECK_LE(hs, 7);
197 x_ = (x_ & ~7) | hs;
198 }
199
200 int GetHistorySize() const {
201 return (int)(x_ & 7);
202 }
203
204 void ClearHistorySize() {
205 x_ &= ~7;
206 }
207
208 u64 GetTracePos() const {
209 const int hs = GetHistorySize();
210 // When hs == 0, the trace consists of 2 parts.
211 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
212 return epoch() & mask;
213 }
214
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000215 private:
216 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000217 static const int kTidShift = 64 - kTidBits - 1;
218 static const int kClkShift = kTidShift - kClkBits;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000219 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000220 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000221 u64 x_;
222};
223
224// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000225// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226// tid : kTidBits
227// epoch : kClkBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000228// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000229// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000230// size_log : 2
231// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000232class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000233 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000234 explicit Shadow(u64 x)
235 : FastState(x) {
236 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000238 explicit Shadow(const FastState &s)
239 : FastState(s.x_) {
240 ClearHistorySize();
241 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000242
243 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
244 DCHECK_EQ(x_ & 31, 0);
245 DCHECK_LE(addr0, 7);
246 DCHECK_LE(kAccessSizeLog, 3);
247 x_ |= (kAccessSizeLog << 3) | addr0;
248 DCHECK_EQ(kAccessSizeLog, size_log());
249 DCHECK_EQ(addr0, this->addr0());
250 }
251
252 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000253 DCHECK_EQ(x_ & kReadBit, 0);
254 if (!kAccessIsWrite)
255 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000256 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000257 }
258
Dmitry Vyukovba429142013-02-01 09:42:06 +0000259 void SetAtomic(bool kIsAtomic) {
260 DCHECK(!IsAtomic());
261 if (kIsAtomic)
262 x_ |= kAtomicBit;
263 DCHECK_EQ(IsAtomic(), kIsAtomic);
264 }
265
266 bool IsAtomic() const {
267 return x_ & kAtomicBit;
268 }
269
270 bool IsZero() const {
271 return x_ == 0;
272 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000273
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000274 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000275 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000276 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000277 return shifted_xor == 0;
278 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000279
280 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000281 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
282 return masked_xor == 0;
283 }
284
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000285 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
286 unsigned kS2AccessSize) {
287 bool res = false;
288 u64 diff = s1.addr0() - s2.addr0();
289 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
290 // if (s1.addr0() + size1) > s2.addr0()) return true;
291 if (s1.size() > -diff) res = true;
292 } else {
293 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
294 if (kS2AccessSize > diff) res = true;
295 }
296 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
297 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
298 return res;
299 }
300
301 // The idea behind the offset is as follows.
302 // Consider that we have 8 bool's contained within a single 8-byte block
303 // (mapped to a single shadow "cell"). Now consider that we write to the bools
304 // from a single thread (which we consider the common case).
305 // W/o offsetting each access will have to scan 4 shadow values at average
306 // to find the corresponding shadow value for the bool.
307 // With offsetting we start scanning shadow with the offset so that
308 // each access hits necessary shadow straight off (at least in an expected
309 // optimistic case).
310 // This logic works seamlessly for any layout of user data. For example,
311 // if user data is {int, short, char, char}, then accesses to the int are
312 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
313 // from a single thread won't need to scan all 8 shadow values.
314 unsigned ComputeSearchOffset() {
315 return x_ & 7;
316 }
317 u64 addr0() const { return x_ & 7; }
318 u64 size() const { return 1ull << size_log(); }
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000319 bool IsWrite() const { return !IsRead(); }
320 bool IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000321
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000322 // The idea behind the freed bit is as follows.
323 // When the memory is freed (or otherwise unaccessible) we write to the shadow
324 // values with tid/epoch related to the free and the freed bit set.
325 // During memory accesses processing the freed bit is considered
326 // as msb of tid. So any access races with shadow with freed bit set
327 // (it is as if write from a thread with which we never synchronized before).
328 // This allows us to detect accesses to freed memory w/o additional
329 // overheads in memory access processing and at the same time restore
330 // tid/epoch of free.
331 void MarkAsFreed() {
332 x_ |= kFreedBit;
333 }
334
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000335 bool IsFreed() const {
336 return x_ & kFreedBit;
337 }
338
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000339 bool GetFreedAndReset() {
340 bool res = x_ & kFreedBit;
341 x_ &= ~kFreedBit;
342 return res;
343 }
344
Dmitry Vyukovba429142013-02-01 09:42:06 +0000345 bool IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000346 // analyzes 5-th bit (is_read) and 6-th bit (is_atomic)
347 bool v = x_ & u64(((kIsWrite ^ 1) << kReadShift)
348 | (kIsAtomic << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000349 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
350 return v;
351 }
352
353 bool IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000354 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000355 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
356 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
357 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
358 return v;
359 }
360
361 bool IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000362 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000363 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
364 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
365 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
366 return v;
367 }
368
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000369 private:
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000370 static const u64 kReadShift = 5;
371 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000372 static const u64 kAtomicShift = 6;
373 static const u64 kAtomicBit = 1ull << kAtomicShift;
374
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000375 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000376
377 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
378 if (s1.addr0() == s2.addr0()) return true;
379 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
380 return true;
381 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
382 return true;
383 return false;
384 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000385};
386
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000387struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000388
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000389struct JmpBuf {
390 uptr sp;
391 uptr mangled_sp;
392 uptr *shadow_stack_pos;
393};
394
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000395// This struct is stored in TLS.
396struct ThreadState {
397 FastState fast_state;
398 // Synch epoch represents the threads's epoch before the last synchronization
399 // action. It allows to reduce number of shadow state updates.
400 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
401 // if we are processing write to X from the same thread at epoch=200,
402 // we do nothing, because both writes happen in the same 'synch epoch'.
403 // That is, if another memory access does not race with the former write,
404 // it does not race with the latter as well.
405 // QUESTION: can we can squeeze this into ThreadState::Fast?
406 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
407 // taken by epoch between synchs.
408 // This way we can save one load from tls.
409 u64 fast_synch_epoch;
410 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
411 // We do not distinguish beteween ignoring reads and writes
412 // for better performance.
413 int ignore_reads_and_writes;
414 uptr *shadow_stack_pos;
415 u64 *racy_shadow_addr;
416 u64 racy_state[2];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000417#ifndef TSAN_GO
418 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000419 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000420#else
421 // Go uses satellite shadow stack with dynamic size.
422 uptr *shadow_stack;
423 uptr *shadow_stack_end;
424#endif
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000425 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000426 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000427#ifndef TSAN_GO
428 AllocatorCache alloc_cache;
Alexey Samsonovc30e2d62013-05-29 09:15:39 +0000429 InternalAllocatorCache internal_alloc_cache;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000430 Vector<JmpBuf> jmp_bufs;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000431#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000432 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000433 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000434 const int unique_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000435 int in_rtl;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000436 bool in_symbolizer;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000437 bool is_alive;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000438 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000439 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000440 const uptr stk_addr;
441 const uptr stk_size;
442 const uptr tls_addr;
443 const uptr tls_size;
444
445 DeadlockDetector deadlock_detector;
446
447 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000448 SignalContext *signal_ctx;
449
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000450#ifndef TSAN_GO
451 u32 last_sleep_stack_id;
452 ThreadClock last_sleep_clock;
453#endif
454
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000455 // Set in regions of runtime that must be signal-safe and fork-safe.
456 // If set, malloc must not be called.
457 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000458
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000459 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000460 uptr stk_addr, uptr stk_size,
461 uptr tls_addr, uptr tls_size);
462};
463
464Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000465
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000466#ifndef TSAN_GO
467extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000468INLINE ThreadState *cur_thread() {
469 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
470}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000471#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000472
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000473class ThreadContext : public ThreadContextBase {
474 public:
475 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000476 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000477 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000478#ifdef TSAN_GO
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000479 StackTrace creation_stack;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000480#else
481 u32 creation_stack_id;
482#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000483 SyncClock sync;
484 // Epoch at which the thread had started.
485 // If we see an event from the thread stamped by an older epoch,
486 // the event is from a dead thread that shared tid with this thread.
487 u64 epoch0;
488 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000489
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000490 // Override superclass callbacks.
491 void OnDead();
492 void OnJoined(void *arg);
493 void OnFinished();
494 void OnStarted(void *arg);
495 void OnCreated(void *arg);
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +0000496 void OnReset();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000497};
498
499struct RacyStacks {
500 MD5Hash hash[2];
501 bool operator==(const RacyStacks &other) const {
502 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
503 return true;
504 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
505 return true;
506 return false;
507 }
508};
509
510struct RacyAddress {
511 uptr addr_min;
512 uptr addr_max;
513};
514
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000515struct FiredSuppression {
516 ReportType type;
517 uptr pc;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000518 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000519};
520
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000521struct Context {
522 Context();
523
524 bool initialized;
525
526 SyncTab synctab;
527
528 Mutex report_mtx;
529 int nreported;
530 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000531 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000532
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000533 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000534
535 Vector<RacyStacks> racy_stacks;
536 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000537 // Number of fired suppressions may be large enough.
538 InternalMmapVector<FiredSuppression> fired_suppressions;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000539
540 Flags flags;
541
542 u64 stat[StatCnt];
543 u64 int_alloc_cnt[MBlockTypeCount];
544 u64 int_alloc_siz[MBlockTypeCount];
545};
546
547class ScopedInRtl {
548 public:
549 ScopedInRtl();
550 ~ScopedInRtl();
551 private:
552 ThreadState*thr_;
553 int in_rtl_;
554 int errno_;
555};
556
557class ScopedReport {
558 public:
559 explicit ScopedReport(ReportType typ);
560 ~ScopedReport();
561
562 void AddStack(const StackTrace *stack);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000563 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack,
564 const MutexSet *mset);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000565 void AddThread(const ThreadContext *tctx);
566 void AddMutex(const SyncVar *s);
567 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000568 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000569 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000570
571 const ReportDesc *GetReport() const;
572
573 private:
574 Context *ctx_;
575 ReportDesc *rep_;
576
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000577 void AddMutex(u64 id);
578
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000579 ScopedReport(const ScopedReport&);
580 void operator = (const ScopedReport&);
581};
582
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000583void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset);
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000584
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000585void StatAggregate(u64 *dst, u64 *src);
586void StatOutput(u64 *stat);
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000587void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000588 if (kCollectStats)
589 thr->stat[typ] += n;
590}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000591void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000592 if (kCollectStats)
593 thr->stat[typ] = n;
594}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000595
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000596void MapShadow(uptr addr, uptr size);
Dmitry Vyukov3e7ede22012-12-13 08:14:02 +0000597void MapThreadTrace(uptr addr, uptr size);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000598void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000599void InitializeShadowMemory();
600void InitializeInterceptors();
601void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000602
603void ReportRace(ThreadState *thr);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000604bool OutputReport(Context *ctx,
605 const ScopedReport &srep,
Dmitry Vyukovf4f76b12013-01-24 13:50:32 +0000606 const ReportStack *suppress_stack1 = 0,
Dmitry Vyukov315bb0e2013-06-10 15:38:44 +0000607 const ReportStack *suppress_stack2 = 0,
608 const ReportLocation *suppress_loc = 0);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000609bool IsFiredSuppression(Context *ctx,
610 const ScopedReport &srep,
611 const StackTrace &trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000612bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000613void PrintMatchedBenignRaces();
Kostya Serebryany4fb340d2013-02-06 14:24:00 +0000614bool FrameIsInternal(const ReportStack *frame);
Alexey Samsonov85cc9b62013-02-06 16:28:05 +0000615ReportStack *SkipTsanInternalFrames(ReportStack *ent);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000616
617#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000618# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000619#else
620# define DPrintf(...)
621#endif
622
623#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000624# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000625#else
626# define DPrintf2(...)
627#endif
628
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000629u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000630void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov019ef672013-01-29 14:20:12 +0000631void PrintCurrentStackSlow(); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000632
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000633void Initialize(ThreadState *thr);
634int Finalize(ThreadState *thr);
635
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000636SyncVar* GetJavaSync(ThreadState *thr, uptr pc, uptr addr,
637 bool write_lock, bool create);
638SyncVar* GetAndRemoveJavaSync(ThreadState *thr, uptr pc, uptr addr);
639
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000640void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000641 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000642void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000643 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000644 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000645void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000646 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000647void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
648 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000649void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
650 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000651
652const int kSizeLog1 = 0;
653const int kSizeLog2 = 1;
654const int kSizeLog4 = 2;
655const int kSizeLog8 = 3;
656
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000657void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000658 uptr addr, int kAccessSizeLog) {
659 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
660}
661
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000662void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000663 uptr addr, int kAccessSizeLog) {
664 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
665}
666
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000667void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000668 uptr addr, int kAccessSizeLog) {
669 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
670}
671
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000672void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000673 uptr addr, int kAccessSizeLog) {
674 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
675}
676
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000677void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
678void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000679void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000680void IgnoreCtl(ThreadState *thr, bool write, bool begin);
681
682void FuncEntry(ThreadState *thr, uptr pc);
683void FuncExit(ThreadState *thr);
684
685int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000686void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000687void ThreadFinish(ThreadState *thr);
688int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
689void ThreadJoin(ThreadState *thr, uptr pc, int tid);
690void ThreadDetach(ThreadState *thr, uptr pc, int tid);
691void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000692void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000693int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000694void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000695
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000696void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
697 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000698void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukovc9af8182013-05-17 12:03:46 +0000699void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1);
700int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000701void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
702void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
703void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
704
705void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000706void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000707void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000708void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000709void AfterSleep(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000710
711// The hacky call uses custom calling convention and an assembly thunk.
712// It is considerably faster that a normal call for the caller
713// if it is not executed (it is intended for slow paths from hot functions).
714// The trick is that the call preserves all registers and the compiler
715// does not treat it as a call.
716// If it does not work for you, use normal call.
717#if TSAN_DEBUG == 0
718// The caller may not create the stack frame for itself at all,
719// so we create a reserve stack frame for it (1024b must be enough).
720#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000721 __asm__ __volatile__("sub $1024, %%rsp;" \
722 "/*.cfi_adjust_cfa_offset 1024;*/" \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000723 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000724 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000725 "add $1024, %%rsp;" \
726 "/*.cfi_adjust_cfa_offset -1024;*/" \
727 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000728#else
729#define HACKY_CALL(f) f()
730#endif
731
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000732void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000733uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000734uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000735uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000736Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000737
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000738extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000739void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000740 EventType typ, u64 addr) {
741 DCHECK_GE((int)typ, 0);
742 DCHECK_LE((int)typ, 7);
743 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000744 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000745 u64 pos = fs.GetTracePos();
746 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000747#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000748 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000749#else
750 TraceSwitch(thr);
751#endif
752 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000753 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000754 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000755 Event ev = (u64)addr | ((u64)typ << 61);
756 *evp = ev;
757}
758
759} // namespace __tsan
760
761#endif // TSAN_RTL_H