blob: 4364ef803f8faa753d9160f0d2611f91902ceb54 [file] [log] [blame]
Kostya Serebryany7ac41482012-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 Serebryany72166ca2012-12-05 10:09:15 +000029#include "sanitizer_common/sanitizer_allocator.h"
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +000030#include "sanitizer_common/sanitizer_allocator_internal.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070031#include "sanitizer_common/sanitizer_asm.h"
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000032#include "sanitizer_common/sanitizer_common.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070033#include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
Dmitry Vyukov4af0f212013-10-03 13:37:17 +000034#include "sanitizer_common/sanitizer_libignore.h"
Sergey Matveeva52e5c62013-06-26 15:37:14 +000035#include "sanitizer_common/sanitizer_suppressions.h"
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000036#include "sanitizer_common/sanitizer_thread_registry.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000037#include "tsan_clock.h"
38#include "tsan_defs.h"
39#include "tsan_flags.h"
40#include "tsan_sync.h"
41#include "tsan_trace.h"
42#include "tsan_vector.h"
43#include "tsan_report.h"
Dmitry Vyukov385542a2012-11-28 10:35:31 +000044#include "tsan_platform.h"
Dmitry Vyukovad9da372012-12-06 12:16:15 +000045#include "tsan_mutexset.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070046#include "tsan_ignoreset.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000047
Kostya Serebryany503a3af2012-12-04 15:13:30 +000048#if SANITIZER_WORDSIZE != 64
49# error "ThreadSanitizer is supported only on 64-bit platforms"
50#endif
51
Kostya Serebryany7ac41482012-05-10 13:48:04 +000052namespace __tsan {
53
Dmitry Vyukov2e870512012-08-15 15:35:15 +000054// Descriptor of user's memory block.
55struct MBlock {
Dmitry Vyukovf51c3862013-03-18 19:47:36 +000056 /*
57 u64 mtx : 1; // must be first
58 u64 lst : 44;
59 u64 stk : 31; // on word boundary
60 u64 tid : kTidBits;
61 u64 siz : 128 - 1 - 31 - 44 - kTidBits; // 39
62 */
63 u64 raw[2];
Dmitry Vyukovad9da372012-12-06 12:16:15 +000064
Dmitry Vyukovf51c3862013-03-18 19:47:36 +000065 void Init(uptr siz, u32 tid, u32 stk) {
66 raw[0] = raw[1] = 0;
67 raw[1] |= (u64)siz << ((1 + 44 + 31 + kTidBits) % 64);
68 raw[1] |= (u64)tid << ((1 + 44 + 31) % 64);
69 raw[0] |= (u64)stk << (1 + 44);
70 raw[1] |= (u64)stk >> (64 - 44 - 1);
71 DCHECK_EQ(Size(), siz);
72 DCHECK_EQ(Tid(), tid);
73 DCHECK_EQ(StackId(), stk);
Dmitry Vyukovad9da372012-12-06 12:16:15 +000074 }
Dmitry Vyukovf51c3862013-03-18 19:47:36 +000075
76 u32 Tid() const {
77 return GetLsb(raw[1] >> ((1 + 44 + 31) % 64), kTidBits);
78 }
79
80 uptr Size() const {
81 return raw[1] >> ((1 + 31 + 44 + kTidBits) % 64);
82 }
83
84 u32 StackId() const {
85 return (raw[0] >> (1 + 44)) | GetLsb(raw[1] << (64 - 44 - 1), 31);
86 }
87
88 SyncVar *ListHead() const {
89 return (SyncVar*)(GetLsb(raw[0] >> 1, 44) << 3);
90 }
91
92 void ListPush(SyncVar *v) {
93 SyncVar *lst = ListHead();
94 v->next = lst;
95 u64 x = (u64)v ^ (u64)lst;
96 x = (x >> 3) << 1;
97 raw[0] ^= x;
98 DCHECK_EQ(ListHead(), v);
99 }
100
101 SyncVar *ListPop() {
102 SyncVar *lst = ListHead();
103 SyncVar *nxt = lst->next;
104 lst->next = 0;
105 u64 x = (u64)lst ^ (u64)nxt;
106 x = (x >> 3) << 1;
107 raw[0] ^= x;
108 DCHECK_EQ(ListHead(), nxt);
109 return lst;
110 }
111
112 void ListReset() {
113 SyncVar *lst = ListHead();
114 u64 x = (u64)lst;
115 x = (x >> 3) << 1;
116 raw[0] ^= x;
117 DCHECK_EQ(ListHead(), 0);
118 }
119
120 void Lock();
121 void Unlock();
122 typedef GenericScopedLock<MBlock> ScopedLock;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000123};
124
125#ifndef TSAN_GO
126#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukoveee7f732012-08-16 13:27:25 +0000127const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000128#else
129const uptr kAllocatorSpace = 0x7d0000000000ULL;
130#endif
131const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
132
Dmitry Vyukove93e5052013-03-18 10:32:21 +0000133struct MapUnmapCallback;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000134typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
Dmitry Vyukove93e5052013-03-18 10:32:21 +0000135 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Kostya Serebryany82de9422012-12-04 14:15:17 +0000136typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukove93e5052013-03-18 10:32:21 +0000137typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000138typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
139 SecondaryAllocator> Allocator;
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +0000140Allocator *allocator();
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000141#endif
142
Alexey Samsonov591616d2012-09-11 09:44:48 +0000143void TsanCheckFailed(const char *file, int line, const char *cond,
144 u64 v1, u64 v2);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000145
Dmitry Vyukov9743d742013-03-20 10:31:53 +0000146const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
147
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000148// FastState (from most significant bit):
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +0000149// ignore : 1
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000150// tid : kTidBits
151// epoch : kClkBits
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000152// unused : -
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000153// history_size : 3
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000154class FastState {
155 public:
156 FastState(u64 tid, u64 epoch) {
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000157 x_ = tid << kTidShift;
158 x_ |= epoch << kClkShift;
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +0000159 DCHECK_EQ(tid, this->tid());
160 DCHECK_EQ(epoch, this->epoch());
161 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000162 }
163
164 explicit FastState(u64 x)
165 : x_(x) {
166 }
167
Dmitry Vyukov332c62b2012-08-16 15:08:49 +0000168 u64 raw() const {
169 return x_;
170 }
171
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000172 u64 tid() const {
Dmitry Vyukovc8f0a002012-11-30 20:02:11 +0000173 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
174 return res;
175 }
176
177 u64 TidWithIgnore() const {
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000178 u64 res = x_ >> kTidShift;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000179 return res;
180 }
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000181
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000182 u64 epoch() const {
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000183 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000184 return res;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000185 }
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000186
187 void IncrementEpoch() {
188 u64 old_epoch = epoch();
189 x_ += 1 << kClkShift;
Dmitry Vyukove784ad42012-05-21 10:20:53 +0000190 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000191 (void)old_epoch;
192 }
193
194 void SetIgnoreBit() { x_ |= kIgnoreBit; }
195 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +0000196 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000197
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000198 void SetHistorySize(int hs) {
199 CHECK_GE(hs, 0);
200 CHECK_LE(hs, 7);
201 x_ = (x_ & ~7) | hs;
202 }
203
204 int GetHistorySize() const {
205 return (int)(x_ & 7);
206 }
207
208 void ClearHistorySize() {
209 x_ &= ~7;
210 }
211
212 u64 GetTracePos() const {
213 const int hs = GetHistorySize();
214 // When hs == 0, the trace consists of 2 parts.
215 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
216 return epoch() & mask;
217 }
218
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000219 private:
220 friend class Shadow;
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000221 static const int kTidShift = 64 - kTidBits - 1;
222 static const int kClkShift = kTidShift - kClkBits;
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +0000223 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000224 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000225 u64 x_;
226};
227
228// Shadow (from most significant bit):
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000229// freed : 1
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000230// tid : kTidBits
231// epoch : kClkBits
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000232// is_atomic : 1
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000233// is_read : 1
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000234// size_log : 2
235// addr0 : 3
Dmitry Vyukove9636662012-06-27 16:05:06 +0000236class Shadow : public FastState {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000237 public:
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000238 explicit Shadow(u64 x)
239 : FastState(x) {
240 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000241
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000242 explicit Shadow(const FastState &s)
243 : FastState(s.x_) {
244 ClearHistorySize();
245 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000246
247 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
248 DCHECK_EQ(x_ & 31, 0);
249 DCHECK_LE(addr0, 7);
250 DCHECK_LE(kAccessSizeLog, 3);
251 x_ |= (kAccessSizeLog << 3) | addr0;
252 DCHECK_EQ(kAccessSizeLog, size_log());
253 DCHECK_EQ(addr0, this->addr0());
254 }
255
256 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000257 DCHECK_EQ(x_ & kReadBit, 0);
258 if (!kAccessIsWrite)
259 x_ |= kReadBit;
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000260 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000261 }
262
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000263 void SetAtomic(bool kIsAtomic) {
264 DCHECK(!IsAtomic());
265 if (kIsAtomic)
266 x_ |= kAtomicBit;
267 DCHECK_EQ(IsAtomic(), kIsAtomic);
268 }
269
270 bool IsAtomic() const {
271 return x_ & kAtomicBit;
272 }
273
274 bool IsZero() const {
275 return x_ == 0;
276 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000277
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000278 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000279 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukovc8f0a002012-11-30 20:02:11 +0000280 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000281 return shifted_xor == 0;
282 }
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000283
284 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000285 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
286 return masked_xor == 0;
287 }
288
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000289 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
290 unsigned kS2AccessSize) {
291 bool res = false;
292 u64 diff = s1.addr0() - s2.addr0();
293 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
294 // if (s1.addr0() + size1) > s2.addr0()) return true;
295 if (s1.size() > -diff) res = true;
296 } else {
297 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
298 if (kS2AccessSize > diff) res = true;
299 }
300 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
301 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
302 return res;
303 }
304
305 // The idea behind the offset is as follows.
306 // Consider that we have 8 bool's contained within a single 8-byte block
307 // (mapped to a single shadow "cell"). Now consider that we write to the bools
308 // from a single thread (which we consider the common case).
309 // W/o offsetting each access will have to scan 4 shadow values at average
310 // to find the corresponding shadow value for the bool.
311 // With offsetting we start scanning shadow with the offset so that
312 // each access hits necessary shadow straight off (at least in an expected
313 // optimistic case).
314 // This logic works seamlessly for any layout of user data. For example,
315 // if user data is {int, short, char, char}, then accesses to the int are
316 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
317 // from a single thread won't need to scan all 8 shadow values.
318 unsigned ComputeSearchOffset() {
319 return x_ & 7;
320 }
321 u64 addr0() const { return x_ & 7; }
322 u64 size() const { return 1ull << size_log(); }
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000323 bool IsWrite() const { return !IsRead(); }
324 bool IsRead() const { return x_ & kReadBit; }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000325
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000326 // The idea behind the freed bit is as follows.
327 // When the memory is freed (or otherwise unaccessible) we write to the shadow
328 // values with tid/epoch related to the free and the freed bit set.
329 // During memory accesses processing the freed bit is considered
330 // as msb of tid. So any access races with shadow with freed bit set
331 // (it is as if write from a thread with which we never synchronized before).
332 // This allows us to detect accesses to freed memory w/o additional
333 // overheads in memory access processing and at the same time restore
334 // tid/epoch of free.
335 void MarkAsFreed() {
336 x_ |= kFreedBit;
337 }
338
Dmitry Vyukov32858662013-02-01 14:41:58 +0000339 bool IsFreed() const {
340 return x_ & kFreedBit;
341 }
342
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000343 bool GetFreedAndReset() {
344 bool res = x_ & kFreedBit;
345 x_ &= ~kFreedBit;
346 return res;
347 }
348
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000349 bool IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000350 // analyzes 5-th bit (is_read) and 6-th bit (is_atomic)
351 bool v = x_ & u64(((kIsWrite ^ 1) << kReadShift)
352 | (kIsAtomic << kAtomicShift));
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000353 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
354 return v;
355 }
356
357 bool IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000358 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000359 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
360 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
361 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
362 return v;
363 }
364
365 bool IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000366 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000367 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
368 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
369 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
370 return v;
371 }
372
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000373 private:
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000374 static const u64 kReadShift = 5;
375 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000376 static const u64 kAtomicShift = 6;
377 static const u64 kAtomicBit = 1ull << kAtomicShift;
378
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000379 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000380
381 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
382 if (s1.addr0() == s2.addr0()) return true;
383 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
384 return true;
385 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
386 return true;
387 return false;
388 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000389};
390
Dmitry Vyukove9636662012-06-27 16:05:06 +0000391struct SignalContext;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000392
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000393struct JmpBuf {
394 uptr sp;
395 uptr mangled_sp;
396 uptr *shadow_stack_pos;
397};
398
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000399// This struct is stored in TLS.
400struct ThreadState {
401 FastState fast_state;
402 // Synch epoch represents the threads's epoch before the last synchronization
403 // action. It allows to reduce number of shadow state updates.
404 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
405 // if we are processing write to X from the same thread at epoch=200,
406 // we do nothing, because both writes happen in the same 'synch epoch'.
407 // That is, if another memory access does not race with the former write,
408 // it does not race with the latter as well.
409 // QUESTION: can we can squeeze this into ThreadState::Fast?
410 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
411 // taken by epoch between synchs.
412 // This way we can save one load from tls.
413 u64 fast_synch_epoch;
414 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
415 // We do not distinguish beteween ignoring reads and writes
416 // for better performance.
417 int ignore_reads_and_writes;
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000418 int ignore_sync;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700419 // Go does not support ignores.
420#ifndef TSAN_GO
421 IgnoreSet mop_ignore_set;
422 IgnoreSet sync_ignore_set;
423#endif
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000424 // C/C++ uses fixed size shadow stack embed into Trace.
425 // Go uses malloc-allocated shadow stack with dynamic size.
426 uptr *shadow_stack;
427 uptr *shadow_stack_end;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000428 uptr *shadow_stack_pos;
429 u64 *racy_shadow_addr;
430 u64 racy_state[2];
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000431 MutexSet mset;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000432 ThreadClock clock;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000433#ifndef TSAN_GO
434 AllocatorCache alloc_cache;
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +0000435 InternalAllocatorCache internal_alloc_cache;
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000436 Vector<JmpBuf> jmp_bufs;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700437 int ignore_interceptors;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000438#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000439 u64 stat[StatCnt];
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000440 const int tid;
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +0000441 const int unique_id;
Dmitry Vyukov4e81d0e2013-01-29 13:03:07 +0000442 bool in_symbolizer;
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000443 bool in_ignored_lib;
Dmitry Vyukov1fc03d52012-06-28 18:07:46 +0000444 bool is_alive;
Dmitry Vyukov32858662013-02-01 14:41:58 +0000445 bool is_freeing;
Dmitry Vyukov0dc47b62013-03-21 15:37:39 +0000446 bool is_vptr_access;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000447 const uptr stk_addr;
448 const uptr stk_size;
449 const uptr tls_addr;
450 const uptr tls_size;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700451 ThreadContext *tctx;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000452
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700453 InternalDeadlockDetector internal_deadlock_detector;
454 DDPhysicalThread *dd_pt;
455 DDLogicalThread *dd_lt;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000456
457 bool in_signal_handler;
Dmitry Vyukove9636662012-06-27 16:05:06 +0000458 SignalContext *signal_ctx;
459
Dmitry Vyukov84853112012-08-31 17:27:49 +0000460#ifndef TSAN_GO
461 u32 last_sleep_stack_id;
462 ThreadClock last_sleep_clock;
463#endif
464
Dmitry Vyukov9ad7c322012-06-22 11:08:55 +0000465 // Set in regions of runtime that must be signal-safe and fork-safe.
466 // If set, malloc must not be called.
467 int nomalloc;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000468
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +0000469 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700470 unsigned reuse_count,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000471 uptr stk_addr, uptr stk_size,
472 uptr tls_addr, uptr tls_size);
473};
474
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000475#ifndef TSAN_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700476__attribute__((tls_model("initial-exec")))
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000477extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000478INLINE ThreadState *cur_thread() {
479 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
480}
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000481#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000482
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000483class ThreadContext : public ThreadContextBase {
484 public:
485 explicit ThreadContext(int tid);
Dmitry Vyukov6af642e2013-03-18 10:10:15 +0000486 ~ThreadContext();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000487 ThreadState *thr;
Dmitry Vyukov2c5284e2013-03-18 09:02:27 +0000488 u32 creation_stack_id;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000489 SyncClock sync;
490 // Epoch at which the thread had started.
491 // If we see an event from the thread stamped by an older epoch,
492 // the event is from a dead thread that shared tid with this thread.
493 u64 epoch0;
494 u64 epoch1;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000495
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000496 // Override superclass callbacks.
497 void OnDead();
498 void OnJoined(void *arg);
499 void OnFinished();
500 void OnStarted(void *arg);
501 void OnCreated(void *arg);
Dmitry Vyukovce85e032013-03-19 12:25:48 +0000502 void OnReset();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000503};
504
505struct RacyStacks {
506 MD5Hash hash[2];
507 bool operator==(const RacyStacks &other) const {
508 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
509 return true;
510 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
511 return true;
512 return false;
513 }
514};
515
516struct RacyAddress {
517 uptr addr_min;
518 uptr addr_max;
519};
520
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +0000521struct FiredSuppression {
522 ReportType type;
523 uptr pc;
Dmitry Vyukovf754eb52013-03-27 17:59:57 +0000524 Suppression *supp;
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +0000525};
526
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000527struct Context {
528 Context();
529
530 bool initialized;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700531 bool after_multithreaded_fork;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000532
533 SyncTab synctab;
534
535 Mutex report_mtx;
536 int nreported;
537 int nmissed_expected;
Dmitry Vyukova38e40f2013-03-21 07:02:36 +0000538 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000539
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700540 void *background_thread;
541 atomic_uint32_t stop_background_thread;
542
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000543 ThreadRegistry *thread_registry;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000544
545 Vector<RacyStacks> racy_stacks;
546 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0a05e5f2013-06-14 11:18:58 +0000547 // Number of fired suppressions may be large enough.
548 InternalMmapVector<FiredSuppression> fired_suppressions;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700549 DDetector *dd;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000550
551 Flags flags;
552
553 u64 stat[StatCnt];
554 u64 int_alloc_cnt[MBlockTypeCount];
555 u64 int_alloc_siz[MBlockTypeCount];
556};
557
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700558extern Context *ctx; // The one and the only global runtime context.
559
560struct ScopedIgnoreInterceptors {
561 ScopedIgnoreInterceptors() {
562#ifndef TSAN_GO
563 cur_thread()->ignore_interceptors++;
564#endif
565 }
566
567 ~ScopedIgnoreInterceptors() {
568#ifndef TSAN_GO
569 cur_thread()->ignore_interceptors--;
570#endif
571 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000572};
573
574class ScopedReport {
575 public:
576 explicit ScopedReport(ReportType typ);
577 ~ScopedReport();
578
579 void AddStack(const StackTrace *stack);
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000580 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack,
581 const MutexSet *mset);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000582 void AddThread(const ThreadContext *tctx);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700583 void AddThread(int unique_tid);
584 void AddUniqueTid(int unique_tid);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000585 void AddMutex(const SyncVar *s);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700586 u64 AddMutex(u64 id);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000587 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000588 void AddSleep(u32 stack_id);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000589 void SetCount(int count);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000590
591 const ReportDesc *GetReport() const;
592
593 private:
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000594 ReportDesc *rep_;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700595 // Symbolizer makes lots of intercepted calls. If we try to process them,
596 // at best it will cause deadlocks on internal mutexes.
597 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000598
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700599 void AddDeadMutex(u64 id);
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000600
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000601 ScopedReport(const ScopedReport&);
602 void operator = (const ScopedReport&);
603};
604
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000605void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset);
Dmitry Vyukov332c62b2012-08-16 15:08:49 +0000606
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000607void StatAggregate(u64 *dst, u64 *src);
608void StatOutput(u64 *stat);
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000609void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000610 if (kCollectStats)
611 thr->stat[typ] += n;
612}
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000613void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000614 if (kCollectStats)
615 thr->stat[typ] = n;
616}
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000617
Dmitry Vyukova05fcc12012-11-06 16:00:16 +0000618void MapShadow(uptr addr, uptr size);
Dmitry Vyukov6535c312012-12-13 08:14:02 +0000619void MapThreadTrace(uptr addr, uptr size);
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +0000620void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000621void InitializeShadowMemory();
622void InitializeInterceptors();
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000623void InitializeLibIgnore();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000624void InitializeDynamicAnnotations();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000625
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700626void ForkBefore(ThreadState *thr, uptr pc);
627void ForkParentAfter(ThreadState *thr, uptr pc);
628void ForkChildAfter(ThreadState *thr, uptr pc);
629
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000630void ReportRace(ThreadState *thr);
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +0000631bool OutputReport(Context *ctx,
632 const ScopedReport &srep,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700633 const ReportStack *suppress_stack1,
Dmitry Vyukov39968332013-06-10 15:38:44 +0000634 const ReportStack *suppress_stack2 = 0,
635 const ReportLocation *suppress_loc = 0);
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +0000636bool IsFiredSuppression(Context *ctx,
637 const ScopedReport &srep,
638 const StackTrace &trace);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000639bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukov0fd908c2013-03-28 16:21:19 +0000640void PrintMatchedBenignRaces();
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000641bool FrameIsInternal(const ReportStack *frame);
Alexey Samsonov5ba301d2013-02-06 16:28:05 +0000642ReportStack *SkipTsanInternalFrames(ReportStack *ent);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000643
644#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000645# define DPrintf Printf
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000646#else
647# define DPrintf(...)
648#endif
649
650#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000651# define DPrintf2 Printf
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000652#else
653# define DPrintf2(...)
654#endif
655
Dmitry Vyukov84853112012-08-31 17:27:49 +0000656u32 CurrentStackId(ThreadState *thr, uptr pc);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700657ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov1da10562012-09-01 12:13:18 +0000658void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov793e7612013-01-29 14:20:12 +0000659void PrintCurrentStackSlow(); // uses libunwind
Dmitry Vyukov84853112012-08-31 17:27:49 +0000660
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000661void Initialize(ThreadState *thr);
662int Finalize(ThreadState *thr);
663
Dmitry Vyukov21cc85d2012-12-20 17:29:34 +0000664SyncVar* GetJavaSync(ThreadState *thr, uptr pc, uptr addr,
665 bool write_lock, bool create);
666SyncVar* GetAndRemoveJavaSync(ThreadState *thr, uptr pc, uptr addr);
667
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000668void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000669 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000670void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000671 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000672 u64 *shadow_mem, Shadow cur);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000673void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000674 uptr size, bool is_write);
Dmitry Vyukoveaa01902013-02-13 13:05:36 +0000675void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
676 uptr size, uptr step, bool is_write);
Dmitry Vyukov8ecd0e52013-04-30 11:56:56 +0000677void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
678 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000679
680const int kSizeLog1 = 0;
681const int kSizeLog2 = 1;
682const int kSizeLog4 = 2;
683const int kSizeLog8 = 3;
684
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000685void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000686 uptr addr, int kAccessSizeLog) {
687 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
688}
689
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000690void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000691 uptr addr, int kAccessSizeLog) {
692 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
693}
694
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000695void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000696 uptr addr, int kAccessSizeLog) {
697 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
698}
699
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000700void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000701 uptr addr, int kAccessSizeLog) {
702 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
703}
704
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000705void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
706void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov26af8932012-08-15 16:52:19 +0000707void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000708
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700709void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
710void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
711void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
712void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000713
714void FuncEntry(ThreadState *thr, uptr pc);
715void FuncExit(ThreadState *thr);
716
717int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukove0023f72012-10-02 12:58:14 +0000718void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000719void ThreadFinish(ThreadState *thr);
720int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
721void ThreadJoin(ThreadState *thr, uptr pc, int tid);
722void ThreadDetach(ThreadState *thr, uptr pc, int tid);
723void ThreadFinalize(ThreadState *thr);
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +0000724void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov54e0a9a2012-11-07 16:41:57 +0000725int ThreadCount(ThreadState *thr);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000726void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000727
Dmitry Vyukovc20e9ba2012-08-16 13:29:41 +0000728void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
729 bool rw, bool recursive, bool linker_init);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000730void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700731void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
732 bool try_lock = false);
Dmitry Vyukov8354fae2013-05-17 12:03:46 +0000733int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700734void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000735void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
736void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov11f53092013-11-15 16:58:12 +0000737void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000738
739void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov538f1ba2012-11-07 15:08:20 +0000740void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000741void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov9d150bd2012-07-28 15:27:41 +0000742void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000743void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000744void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
745void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
746void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
747void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000748
749// The hacky call uses custom calling convention and an assembly thunk.
750// It is considerably faster that a normal call for the caller
751// if it is not executed (it is intended for slow paths from hot functions).
752// The trick is that the call preserves all registers and the compiler
753// does not treat it as a call.
754// If it does not work for you, use normal call.
755#if TSAN_DEBUG == 0
756// The caller may not create the stack frame for itself at all,
757// so we create a reserve stack frame for it (1024b must be enough).
758#define HACKY_CALL(f) \
Dmitry Vyukov41e81532012-09-02 11:24:07 +0000759 __asm__ __volatile__("sub $1024, %%rsp;" \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700760 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukovf5d526f2012-11-26 14:20:26 +0000761 ".hidden " #f "_thunk;" \
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000762 "call " #f "_thunk;" \
Dmitry Vyukov41e81532012-09-02 11:24:07 +0000763 "add $1024, %%rsp;" \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700764 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukov41e81532012-09-02 11:24:07 +0000765 ::: "memory", "cc");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000766#else
767#define HACKY_CALL(f) f()
768#endif
769
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000770void TraceSwitch(ThreadState *thr);
Dmitry Vyukov385542a2012-11-28 10:35:31 +0000771uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000772uptr TraceSize();
Dmitry Vyukov0415ac02012-12-04 12:19:53 +0000773uptr TraceParts();
Dmitry Vyukov9743d742013-03-20 10:31:53 +0000774Trace *ThreadTrace(int tid);
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000775
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000776extern "C" void __tsan_trace_switch();
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000777void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000778 EventType typ, u64 addr) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700779 if (!kCollectHistory)
780 return;
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000781 DCHECK_GE((int)typ, 0);
782 DCHECK_LE((int)typ, 7);
783 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000784 StatInc(thr, StatEvents);
Dmitry Vyukov3fb70e32012-11-28 13:01:32 +0000785 u64 pos = fs.GetTracePos();
786 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000787#ifndef TSAN_GO
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000788 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000789#else
790 TraceSwitch(thr);
791#endif
792 }
Dmitry Vyukov385542a2012-11-28 10:35:31 +0000793 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukov3fb70e32012-11-28 13:01:32 +0000794 Event *evp = &trace[pos];
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000795 Event ev = (u64)addr | ((u64)typ << 61);
796 *evp = ev;
797}
798
799} // namespace __tsan
800
801#endif // TSAN_RTL_H