blob: efd6b140e49e2c40be2462e54329045b0aee8d17 [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 Samsonov9aecdfe2013-03-15 13:48:44 +000030#include "sanitizer_common/sanitizer_common.h"
31#include "sanitizer_common/sanitizer_thread_registry.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000032#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 Vyukov2429b022012-11-28 10:35:31 +000039#include "tsan_platform.h"
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000040#include "tsan_mutexset.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000041
Kostya Serebryany242b6302012-12-04 15:13:30 +000042#if SANITIZER_WORDSIZE != 64
43# error "ThreadSanitizer is supported only on 64-bit platforms"
44#endif
45
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000046namespace __tsan {
47
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000048// Descriptor of user's memory block.
49struct MBlock {
Dmitry Vyukov4ddd37b2013-03-18 19:47:36 +000050 /*
51 u64 mtx : 1; // must be first
52 u64 lst : 44;
53 u64 stk : 31; // on word boundary
54 u64 tid : kTidBits;
55 u64 siz : 128 - 1 - 31 - 44 - kTidBits; // 39
56 */
57 u64 raw[2];
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000058
Dmitry Vyukov4ddd37b2013-03-18 19:47:36 +000059 void Init(uptr siz, u32 tid, u32 stk) {
60 raw[0] = raw[1] = 0;
61 raw[1] |= (u64)siz << ((1 + 44 + 31 + kTidBits) % 64);
62 raw[1] |= (u64)tid << ((1 + 44 + 31) % 64);
63 raw[0] |= (u64)stk << (1 + 44);
64 raw[1] |= (u64)stk >> (64 - 44 - 1);
65 DCHECK_EQ(Size(), siz);
66 DCHECK_EQ(Tid(), tid);
67 DCHECK_EQ(StackId(), stk);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000068 }
Dmitry Vyukov4ddd37b2013-03-18 19:47:36 +000069
70 u32 Tid() const {
71 return GetLsb(raw[1] >> ((1 + 44 + 31) % 64), kTidBits);
72 }
73
74 uptr Size() const {
75 return raw[1] >> ((1 + 31 + 44 + kTidBits) % 64);
76 }
77
78 u32 StackId() const {
79 return (raw[0] >> (1 + 44)) | GetLsb(raw[1] << (64 - 44 - 1), 31);
80 }
81
82 SyncVar *ListHead() const {
83 return (SyncVar*)(GetLsb(raw[0] >> 1, 44) << 3);
84 }
85
86 void ListPush(SyncVar *v) {
87 SyncVar *lst = ListHead();
88 v->next = lst;
89 u64 x = (u64)v ^ (u64)lst;
90 x = (x >> 3) << 1;
91 raw[0] ^= x;
92 DCHECK_EQ(ListHead(), v);
93 }
94
95 SyncVar *ListPop() {
96 SyncVar *lst = ListHead();
97 SyncVar *nxt = lst->next;
98 lst->next = 0;
99 u64 x = (u64)lst ^ (u64)nxt;
100 x = (x >> 3) << 1;
101 raw[0] ^= x;
102 DCHECK_EQ(ListHead(), nxt);
103 return lst;
104 }
105
106 void ListReset() {
107 SyncVar *lst = ListHead();
108 u64 x = (u64)lst;
109 x = (x >> 3) << 1;
110 raw[0] ^= x;
111 DCHECK_EQ(ListHead(), 0);
112 }
113
114 void Lock();
115 void Unlock();
116 typedef GenericScopedLock<MBlock> ScopedLock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000117};
118
119#ifndef TSAN_GO
120#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukovf77c6ea2012-08-16 13:27:25 +0000121const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000122#else
123const uptr kAllocatorSpace = 0x7d0000000000ULL;
124#endif
125const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
126
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +0000127struct MapUnmapCallback;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000128typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +0000129 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Kostya Serebryanyf2992882012-12-04 14:15:17 +0000130typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +0000131typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000132typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
133 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000134Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000135#endif
136
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +0000137void TsanCheckFailed(const char *file, int line, const char *cond,
138 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000139
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000140const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
141
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000142// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000143// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000144// tid : kTidBits
145// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000146// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000147// history_size : 3
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000148class FastState {
149 public:
150 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000151 x_ = tid << kTidShift;
152 x_ |= epoch << kClkShift;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000153 DCHECK_EQ(tid, this->tid());
154 DCHECK_EQ(epoch, this->epoch());
155 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000156 }
157
158 explicit FastState(u64 x)
159 : x_(x) {
160 }
161
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000162 u64 raw() const {
163 return x_;
164 }
165
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000167 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
168 return res;
169 }
170
171 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000172 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000173 return res;
174 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000175
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000176 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000177 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000178 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000179 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000180
181 void IncrementEpoch() {
182 u64 old_epoch = epoch();
183 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000184 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000185 (void)old_epoch;
186 }
187
188 void SetIgnoreBit() { x_ |= kIgnoreBit; }
189 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000190 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000192 void SetHistorySize(int hs) {
193 CHECK_GE(hs, 0);
194 CHECK_LE(hs, 7);
195 x_ = (x_ & ~7) | hs;
196 }
197
198 int GetHistorySize() const {
199 return (int)(x_ & 7);
200 }
201
202 void ClearHistorySize() {
203 x_ &= ~7;
204 }
205
206 u64 GetTracePos() const {
207 const int hs = GetHistorySize();
208 // When hs == 0, the trace consists of 2 parts.
209 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
210 return epoch() & mask;
211 }
212
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000213 private:
214 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000215 static const int kTidShift = 64 - kTidBits - 1;
216 static const int kClkShift = kTidShift - kClkBits;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000217 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000218 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000219 u64 x_;
220};
221
222// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000223// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000224// tid : kTidBits
225// epoch : kClkBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000226// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000227// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228// size_log : 2
229// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000230class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000232 explicit Shadow(u64 x)
233 : FastState(x) {
234 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000235
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000236 explicit Shadow(const FastState &s)
237 : FastState(s.x_) {
238 ClearHistorySize();
239 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000240
241 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
242 DCHECK_EQ(x_ & 31, 0);
243 DCHECK_LE(addr0, 7);
244 DCHECK_LE(kAccessSizeLog, 3);
245 x_ |= (kAccessSizeLog << 3) | addr0;
246 DCHECK_EQ(kAccessSizeLog, size_log());
247 DCHECK_EQ(addr0, this->addr0());
248 }
249
250 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000251 DCHECK_EQ(x_ & kReadBit, 0);
252 if (!kAccessIsWrite)
253 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000254 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000255 }
256
Dmitry Vyukovba429142013-02-01 09:42:06 +0000257 void SetAtomic(bool kIsAtomic) {
258 DCHECK(!IsAtomic());
259 if (kIsAtomic)
260 x_ |= kAtomicBit;
261 DCHECK_EQ(IsAtomic(), kIsAtomic);
262 }
263
264 bool IsAtomic() const {
265 return x_ & kAtomicBit;
266 }
267
268 bool IsZero() const {
269 return x_ == 0;
270 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000271
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000272 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000273 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000274 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000275 return shifted_xor == 0;
276 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000277
278 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000279 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
280 return masked_xor == 0;
281 }
282
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000283 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
284 unsigned kS2AccessSize) {
285 bool res = false;
286 u64 diff = s1.addr0() - s2.addr0();
287 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
288 // if (s1.addr0() + size1) > s2.addr0()) return true;
289 if (s1.size() > -diff) res = true;
290 } else {
291 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
292 if (kS2AccessSize > diff) res = true;
293 }
294 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
295 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
296 return res;
297 }
298
299 // The idea behind the offset is as follows.
300 // Consider that we have 8 bool's contained within a single 8-byte block
301 // (mapped to a single shadow "cell"). Now consider that we write to the bools
302 // from a single thread (which we consider the common case).
303 // W/o offsetting each access will have to scan 4 shadow values at average
304 // to find the corresponding shadow value for the bool.
305 // With offsetting we start scanning shadow with the offset so that
306 // each access hits necessary shadow straight off (at least in an expected
307 // optimistic case).
308 // This logic works seamlessly for any layout of user data. For example,
309 // if user data is {int, short, char, char}, then accesses to the int are
310 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
311 // from a single thread won't need to scan all 8 shadow values.
312 unsigned ComputeSearchOffset() {
313 return x_ & 7;
314 }
315 u64 addr0() const { return x_ & 7; }
316 u64 size() const { return 1ull << size_log(); }
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000317 bool IsWrite() const { return !IsRead(); }
318 bool IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000319
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000320 // The idea behind the freed bit is as follows.
321 // When the memory is freed (or otherwise unaccessible) we write to the shadow
322 // values with tid/epoch related to the free and the freed bit set.
323 // During memory accesses processing the freed bit is considered
324 // as msb of tid. So any access races with shadow with freed bit set
325 // (it is as if write from a thread with which we never synchronized before).
326 // This allows us to detect accesses to freed memory w/o additional
327 // overheads in memory access processing and at the same time restore
328 // tid/epoch of free.
329 void MarkAsFreed() {
330 x_ |= kFreedBit;
331 }
332
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000333 bool IsFreed() const {
334 return x_ & kFreedBit;
335 }
336
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000337 bool GetFreedAndReset() {
338 bool res = x_ & kFreedBit;
339 x_ &= ~kFreedBit;
340 return res;
341 }
342
Dmitry Vyukovba429142013-02-01 09:42:06 +0000343 bool IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000344 // analyzes 5-th bit (is_read) and 6-th bit (is_atomic)
345 bool v = x_ & u64(((kIsWrite ^ 1) << kReadShift)
346 | (kIsAtomic << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000347 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
348 return v;
349 }
350
351 bool IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000352 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000353 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
354 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
355 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
356 return v;
357 }
358
359 bool IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000360 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000361 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
362 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
363 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
364 return v;
365 }
366
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000367 private:
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000368 static const u64 kReadShift = 5;
369 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000370 static const u64 kAtomicShift = 6;
371 static const u64 kAtomicBit = 1ull << kAtomicShift;
372
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000373 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000374
375 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
376 if (s1.addr0() == s2.addr0()) return true;
377 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
378 return true;
379 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
380 return true;
381 return false;
382 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000383};
384
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000385struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000386
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000387struct JmpBuf {
388 uptr sp;
389 uptr mangled_sp;
390 uptr *shadow_stack_pos;
391};
392
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000393// This struct is stored in TLS.
394struct ThreadState {
395 FastState fast_state;
396 // Synch epoch represents the threads's epoch before the last synchronization
397 // action. It allows to reduce number of shadow state updates.
398 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
399 // if we are processing write to X from the same thread at epoch=200,
400 // we do nothing, because both writes happen in the same 'synch epoch'.
401 // That is, if another memory access does not race with the former write,
402 // it does not race with the latter as well.
403 // QUESTION: can we can squeeze this into ThreadState::Fast?
404 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
405 // taken by epoch between synchs.
406 // This way we can save one load from tls.
407 u64 fast_synch_epoch;
408 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
409 // We do not distinguish beteween ignoring reads and writes
410 // for better performance.
411 int ignore_reads_and_writes;
412 uptr *shadow_stack_pos;
413 u64 *racy_shadow_addr;
414 u64 racy_state[2];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000415#ifndef TSAN_GO
416 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000417 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000418#else
419 // Go uses satellite shadow stack with dynamic size.
420 uptr *shadow_stack;
421 uptr *shadow_stack_end;
422#endif
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000423 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000424 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000425#ifndef TSAN_GO
426 AllocatorCache alloc_cache;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000427 Vector<JmpBuf> jmp_bufs;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000428#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000429 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000430 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000431 const int unique_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000432 int in_rtl;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000433 bool in_symbolizer;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000434 bool is_alive;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000435 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000436 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000437 const uptr stk_addr;
438 const uptr stk_size;
439 const uptr tls_addr;
440 const uptr tls_size;
441
442 DeadlockDetector deadlock_detector;
443
444 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000445 SignalContext *signal_ctx;
446
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000447#ifndef TSAN_GO
448 u32 last_sleep_stack_id;
449 ThreadClock last_sleep_clock;
450#endif
451
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000452 // Set in regions of runtime that must be signal-safe and fork-safe.
453 // If set, malloc must not be called.
454 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000455
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000456 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000457 uptr stk_addr, uptr stk_size,
458 uptr tls_addr, uptr tls_size);
459};
460
461Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000462
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000463#ifndef TSAN_GO
464extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000465INLINE ThreadState *cur_thread() {
466 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
467}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000468#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000469
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000470class ThreadContext : public ThreadContextBase {
471 public:
472 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000473 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000474 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000475#ifdef TSAN_GO
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000476 StackTrace creation_stack;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000477#else
478 u32 creation_stack_id;
479#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000480 SyncClock sync;
481 // Epoch at which the thread had started.
482 // If we see an event from the thread stamped by an older epoch,
483 // the event is from a dead thread that shared tid with this thread.
484 u64 epoch0;
485 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000486
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000487 // Override superclass callbacks.
488 void OnDead();
489 void OnJoined(void *arg);
490 void OnFinished();
491 void OnStarted(void *arg);
492 void OnCreated(void *arg);
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +0000493 void OnReset();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000494};
495
496struct RacyStacks {
497 MD5Hash hash[2];
498 bool operator==(const RacyStacks &other) const {
499 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
500 return true;
501 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
502 return true;
503 return false;
504 }
505};
506
507struct RacyAddress {
508 uptr addr_min;
509 uptr addr_max;
510};
511
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000512struct FiredSuppression {
513 ReportType type;
514 uptr pc;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000515 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000516};
517
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000518struct Context {
519 Context();
520
521 bool initialized;
522
523 SyncTab synctab;
524
525 Mutex report_mtx;
526 int nreported;
527 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000528 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000529
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000530 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000531
532 Vector<RacyStacks> racy_stacks;
533 Vector<RacyAddress> racy_addresses;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000534 Vector<FiredSuppression> fired_suppressions;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000535
536 Flags flags;
537
538 u64 stat[StatCnt];
539 u64 int_alloc_cnt[MBlockTypeCount];
540 u64 int_alloc_siz[MBlockTypeCount];
541};
542
543class ScopedInRtl {
544 public:
545 ScopedInRtl();
546 ~ScopedInRtl();
547 private:
548 ThreadState*thr_;
549 int in_rtl_;
550 int errno_;
551};
552
553class ScopedReport {
554 public:
555 explicit ScopedReport(ReportType typ);
556 ~ScopedReport();
557
558 void AddStack(const StackTrace *stack);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000559 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack,
560 const MutexSet *mset);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000561 void AddThread(const ThreadContext *tctx);
562 void AddMutex(const SyncVar *s);
563 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000564 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000565 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000566
567 const ReportDesc *GetReport() const;
568
569 private:
570 Context *ctx_;
571 ReportDesc *rep_;
572
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000573 void AddMutex(u64 id);
574
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000575 ScopedReport(const ScopedReport&);
576 void operator = (const ScopedReport&);
577};
578
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000579void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset);
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000580
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000581void StatAggregate(u64 *dst, u64 *src);
582void StatOutput(u64 *stat);
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000583void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000584 if (kCollectStats)
585 thr->stat[typ] += n;
586}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000587void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000588 if (kCollectStats)
589 thr->stat[typ] = n;
590}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000591
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000592void MapShadow(uptr addr, uptr size);
Dmitry Vyukov3e7ede22012-12-13 08:14:02 +0000593void MapThreadTrace(uptr addr, uptr size);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000594void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000595void InitializeShadowMemory();
596void InitializeInterceptors();
597void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000598
599void ReportRace(ThreadState *thr);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000600bool OutputReport(Context *ctx,
601 const ScopedReport &srep,
Dmitry Vyukovf4f76b12013-01-24 13:50:32 +0000602 const ReportStack *suppress_stack1 = 0,
603 const ReportStack *suppress_stack2 = 0);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000604bool IsFiredSuppression(Context *ctx,
605 const ScopedReport &srep,
606 const StackTrace &trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000607bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000608void PrintMatchedBenignRaces();
Kostya Serebryany4fb340d2013-02-06 14:24:00 +0000609bool FrameIsInternal(const ReportStack *frame);
Alexey Samsonov85cc9b62013-02-06 16:28:05 +0000610ReportStack *SkipTsanInternalFrames(ReportStack *ent);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000611
612#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000613# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000614#else
615# define DPrintf(...)
616#endif
617
618#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000619# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000620#else
621# define DPrintf2(...)
622#endif
623
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000624u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000625void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov019ef672013-01-29 14:20:12 +0000626void PrintCurrentStackSlow(); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000627
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000628void Initialize(ThreadState *thr);
629int Finalize(ThreadState *thr);
630
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000631SyncVar* GetJavaSync(ThreadState *thr, uptr pc, uptr addr,
632 bool write_lock, bool create);
633SyncVar* GetAndRemoveJavaSync(ThreadState *thr, uptr pc, uptr addr);
634
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000635void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000636 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000637void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000638 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000639 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000640void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000641 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000642void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
643 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000644void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
645 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000646
647const int kSizeLog1 = 0;
648const int kSizeLog2 = 1;
649const int kSizeLog4 = 2;
650const int kSizeLog8 = 3;
651
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000652void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000653 uptr addr, int kAccessSizeLog) {
654 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
655}
656
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000657void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000658 uptr addr, int kAccessSizeLog) {
659 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
660}
661
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000662void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000663 uptr addr, int kAccessSizeLog) {
664 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
665}
666
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000667void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000668 uptr addr, int kAccessSizeLog) {
669 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
670}
671
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000672void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
673void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000674void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000675void IgnoreCtl(ThreadState *thr, bool write, bool begin);
676
677void FuncEntry(ThreadState *thr, uptr pc);
678void FuncExit(ThreadState *thr);
679
680int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000681void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000682void ThreadFinish(ThreadState *thr);
683int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
684void ThreadJoin(ThreadState *thr, uptr pc, int tid);
685void ThreadDetach(ThreadState *thr, uptr pc, int tid);
686void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000687void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000688int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000689void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000690
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000691void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
692 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000693void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
694void MutexLock(ThreadState *thr, uptr pc, uptr addr);
695void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
696void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
697void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
698void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
699
700void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000701void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000702void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000703void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000704void AfterSleep(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000705
706// The hacky call uses custom calling convention and an assembly thunk.
707// It is considerably faster that a normal call for the caller
708// if it is not executed (it is intended for slow paths from hot functions).
709// The trick is that the call preserves all registers and the compiler
710// does not treat it as a call.
711// If it does not work for you, use normal call.
712#if TSAN_DEBUG == 0
713// The caller may not create the stack frame for itself at all,
714// so we create a reserve stack frame for it (1024b must be enough).
715#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000716 __asm__ __volatile__("sub $1024, %%rsp;" \
717 "/*.cfi_adjust_cfa_offset 1024;*/" \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000718 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000719 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000720 "add $1024, %%rsp;" \
721 "/*.cfi_adjust_cfa_offset -1024;*/" \
722 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000723#else
724#define HACKY_CALL(f) f()
725#endif
726
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000727void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000728uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000729uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000730uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000731Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000732
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000733extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000734void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000735 EventType typ, u64 addr) {
736 DCHECK_GE((int)typ, 0);
737 DCHECK_LE((int)typ, 7);
738 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000739 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000740 u64 pos = fs.GetTracePos();
741 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000742#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000743 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000744#else
745 TraceSwitch(thr);
746#endif
747 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000748 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000749 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000750 Event ev = (u64)addr | ((u64)typ << 61);
751 *evp = ev;
752}
753
754} // namespace __tsan
755
756#endif // TSAN_RTL_H