blob: 52ecf7668fc3ebfeee36913405a93c4dd95b2968 [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 Vyukov9f1509f2012-08-15 16:52:19 +000050 Mutex mtx;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000051 uptr size;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000052 u32 alloc_tid;
53 u32 alloc_stack_id;
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +000054 SyncVar *head;
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000055
56 MBlock()
57 : mtx(MutexTypeMBlock, StatMtxMBlock) {
58 }
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000059};
60
61#ifndef TSAN_GO
62#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukovf77c6ea2012-08-16 13:27:25 +000063const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000064#else
65const uptr kAllocatorSpace = 0x7d0000000000ULL;
66#endif
67const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
68
Kostya Serebryany6f604b52012-12-27 07:37:24 +000069struct TsanMapUnmapCallback {
70 void OnMap(uptr p, uptr size) const { }
71 void OnUnmap(uptr p, uptr size) const {
72 // We are about to unmap a chunk of user memory.
73 // Mark the corresponding shadow memory as not needed.
74 uptr shadow_beg = MemToShadow(p);
75 uptr shadow_end = MemToShadow(p + size);
76 CHECK(IsAligned(shadow_end|shadow_beg, GetPageSizeCached()));
77 FlushUnneededShadowMemory(shadow_beg, shadow_end - shadow_beg);
78 }
79};
80
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000081typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
82 DefaultSizeClassMap> PrimaryAllocator;
Kostya Serebryanyf2992882012-12-04 14:15:17 +000083typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Kostya Serebryany6f604b52012-12-27 07:37:24 +000084typedef LargeMmapAllocator<TsanMapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000085typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
86 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000087Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000088#endif
89
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000090void TsanCheckFailed(const char *file, int line, const char *cond,
91 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000092
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000093// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000094// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000095// tid : kTidBits
96// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000097// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000098// history_size : 3
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000099class FastState {
100 public:
101 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000102 x_ = tid << kTidShift;
103 x_ |= epoch << kClkShift;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000104 DCHECK_EQ(tid, this->tid());
105 DCHECK_EQ(epoch, this->epoch());
106 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000107 }
108
109 explicit FastState(u64 x)
110 : x_(x) {
111 }
112
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000113 u64 raw() const {
114 return x_;
115 }
116
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000117 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000118 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
119 return res;
120 }
121
122 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000123 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000124 return res;
125 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000126
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000127 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000128 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000129 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000130 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000131
132 void IncrementEpoch() {
133 u64 old_epoch = epoch();
134 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000135 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000136 (void)old_epoch;
137 }
138
139 void SetIgnoreBit() { x_ |= kIgnoreBit; }
140 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000141 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000142
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000143 void SetHistorySize(int hs) {
144 CHECK_GE(hs, 0);
145 CHECK_LE(hs, 7);
146 x_ = (x_ & ~7) | hs;
147 }
148
149 int GetHistorySize() const {
150 return (int)(x_ & 7);
151 }
152
153 void ClearHistorySize() {
154 x_ &= ~7;
155 }
156
157 u64 GetTracePos() const {
158 const int hs = GetHistorySize();
159 // When hs == 0, the trace consists of 2 parts.
160 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
161 return epoch() & mask;
162 }
163
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000164 private:
165 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000166 static const int kTidShift = 64 - kTidBits - 1;
167 static const int kClkShift = kTidShift - kClkBits;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000168 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000169 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000170 u64 x_;
171};
172
173// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000174// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000175// tid : kTidBits
176// epoch : kClkBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000177// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000178// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000179// size_log : 2
180// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000181class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000182 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000183 explicit Shadow(u64 x)
184 : FastState(x) {
185 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000186
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000187 explicit Shadow(const FastState &s)
188 : FastState(s.x_) {
189 ClearHistorySize();
190 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191
192 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
193 DCHECK_EQ(x_ & 31, 0);
194 DCHECK_LE(addr0, 7);
195 DCHECK_LE(kAccessSizeLog, 3);
196 x_ |= (kAccessSizeLog << 3) | addr0;
197 DCHECK_EQ(kAccessSizeLog, size_log());
198 DCHECK_EQ(addr0, this->addr0());
199 }
200
201 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000202 DCHECK_EQ(x_ & kReadBit, 0);
203 if (!kAccessIsWrite)
204 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000205 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000206 }
207
Dmitry Vyukovba429142013-02-01 09:42:06 +0000208 void SetAtomic(bool kIsAtomic) {
209 DCHECK(!IsAtomic());
210 if (kIsAtomic)
211 x_ |= kAtomicBit;
212 DCHECK_EQ(IsAtomic(), kIsAtomic);
213 }
214
215 bool IsAtomic() const {
216 return x_ & kAtomicBit;
217 }
218
219 bool IsZero() const {
220 return x_ == 0;
221 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000222
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000223 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000224 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000225 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 return shifted_xor == 0;
227 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000228
229 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000230 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
231 return masked_xor == 0;
232 }
233
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000234 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
235 unsigned kS2AccessSize) {
236 bool res = false;
237 u64 diff = s1.addr0() - s2.addr0();
238 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
239 // if (s1.addr0() + size1) > s2.addr0()) return true;
240 if (s1.size() > -diff) res = true;
241 } else {
242 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
243 if (kS2AccessSize > diff) res = true;
244 }
245 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
246 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
247 return res;
248 }
249
250 // The idea behind the offset is as follows.
251 // Consider that we have 8 bool's contained within a single 8-byte block
252 // (mapped to a single shadow "cell"). Now consider that we write to the bools
253 // from a single thread (which we consider the common case).
254 // W/o offsetting each access will have to scan 4 shadow values at average
255 // to find the corresponding shadow value for the bool.
256 // With offsetting we start scanning shadow with the offset so that
257 // each access hits necessary shadow straight off (at least in an expected
258 // optimistic case).
259 // This logic works seamlessly for any layout of user data. For example,
260 // if user data is {int, short, char, char}, then accesses to the int are
261 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
262 // from a single thread won't need to scan all 8 shadow values.
263 unsigned ComputeSearchOffset() {
264 return x_ & 7;
265 }
266 u64 addr0() const { return x_ & 7; }
267 u64 size() const { return 1ull << size_log(); }
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000268 bool IsWrite() const { return !IsRead(); }
269 bool IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000271 // The idea behind the freed bit is as follows.
272 // When the memory is freed (or otherwise unaccessible) we write to the shadow
273 // values with tid/epoch related to the free and the freed bit set.
274 // During memory accesses processing the freed bit is considered
275 // as msb of tid. So any access races with shadow with freed bit set
276 // (it is as if write from a thread with which we never synchronized before).
277 // This allows us to detect accesses to freed memory w/o additional
278 // overheads in memory access processing and at the same time restore
279 // tid/epoch of free.
280 void MarkAsFreed() {
281 x_ |= kFreedBit;
282 }
283
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000284 bool IsFreed() const {
285 return x_ & kFreedBit;
286 }
287
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000288 bool GetFreedAndReset() {
289 bool res = x_ & kFreedBit;
290 x_ &= ~kFreedBit;
291 return res;
292 }
293
Dmitry Vyukovba429142013-02-01 09:42:06 +0000294 bool IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000295 // analyzes 5-th bit (is_read) and 6-th bit (is_atomic)
296 bool v = x_ & u64(((kIsWrite ^ 1) << kReadShift)
297 | (kIsAtomic << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000298 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
299 return v;
300 }
301
302 bool IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000303 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000304 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
305 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
306 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
307 return v;
308 }
309
310 bool IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000311 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000312 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
313 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
314 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
315 return v;
316 }
317
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000318 private:
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000319 static const u64 kReadShift = 5;
320 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000321 static const u64 kAtomicShift = 6;
322 static const u64 kAtomicBit = 1ull << kAtomicShift;
323
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000324 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000325
326 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
327 if (s1.addr0() == s2.addr0()) return true;
328 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
329 return true;
330 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
331 return true;
332 return false;
333 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000334};
335
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000336struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000337
338// This struct is stored in TLS.
339struct ThreadState {
340 FastState fast_state;
341 // Synch epoch represents the threads's epoch before the last synchronization
342 // action. It allows to reduce number of shadow state updates.
343 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
344 // if we are processing write to X from the same thread at epoch=200,
345 // we do nothing, because both writes happen in the same 'synch epoch'.
346 // That is, if another memory access does not race with the former write,
347 // it does not race with the latter as well.
348 // QUESTION: can we can squeeze this into ThreadState::Fast?
349 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
350 // taken by epoch between synchs.
351 // This way we can save one load from tls.
352 u64 fast_synch_epoch;
353 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
354 // We do not distinguish beteween ignoring reads and writes
355 // for better performance.
356 int ignore_reads_and_writes;
357 uptr *shadow_stack_pos;
358 u64 *racy_shadow_addr;
359 u64 racy_state[2];
360 Trace trace;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000361#ifndef TSAN_GO
362 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000363 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000364#else
365 // Go uses satellite shadow stack with dynamic size.
366 uptr *shadow_stack;
367 uptr *shadow_stack_end;
368#endif
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000369 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000370 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000371#ifndef TSAN_GO
372 AllocatorCache alloc_cache;
373#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000374 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000375 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000376 const int unique_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000377 int in_rtl;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000378 bool in_symbolizer;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000379 bool is_alive;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000380 bool is_freeing;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000381 const uptr stk_addr;
382 const uptr stk_size;
383 const uptr tls_addr;
384 const uptr tls_size;
385
386 DeadlockDetector deadlock_detector;
387
388 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000389 SignalContext *signal_ctx;
390
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000391#ifndef TSAN_GO
392 u32 last_sleep_stack_id;
393 ThreadClock last_sleep_clock;
394#endif
395
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000396 // Set in regions of runtime that must be signal-safe and fork-safe.
397 // If set, malloc must not be called.
398 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000399
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000400 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000401 uptr stk_addr, uptr stk_size,
402 uptr tls_addr, uptr tls_size);
403};
404
405Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000406
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000407#ifndef TSAN_GO
408extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000409INLINE ThreadState *cur_thread() {
410 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
411}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000412#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000413
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000414// An info about a thread that is hold for some time after its termination.
415struct ThreadDeadInfo {
416 Trace trace;
417};
418
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000419class ThreadContext : public ThreadContextBase {
420 public:
421 explicit ThreadContext(int tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000422 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000423#ifdef TSAN_GO
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000424 StackTrace creation_stack;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000425#else
426 u32 creation_stack_id;
427#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000428 SyncClock sync;
429 // Epoch at which the thread had started.
430 // If we see an event from the thread stamped by an older epoch,
431 // the event is from a dead thread that shared tid with this thread.
432 u64 epoch0;
433 u64 epoch1;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000434 ThreadDeadInfo *dead_info;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000435
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000436 // Override superclass callbacks.
437 void OnDead();
438 void OnJoined(void *arg);
439 void OnFinished();
440 void OnStarted(void *arg);
441 void OnCreated(void *arg);
442 void OnReset(void *arg);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000443};
444
445struct RacyStacks {
446 MD5Hash hash[2];
447 bool operator==(const RacyStacks &other) const {
448 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
449 return true;
450 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
451 return true;
452 return false;
453 }
454};
455
456struct RacyAddress {
457 uptr addr_min;
458 uptr addr_max;
459};
460
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000461struct FiredSuppression {
462 ReportType type;
463 uptr pc;
464};
465
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000466struct Context {
467 Context();
468
469 bool initialized;
470
471 SyncTab synctab;
472
473 Mutex report_mtx;
474 int nreported;
475 int nmissed_expected;
476
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000477 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000478
479 Vector<RacyStacks> racy_stacks;
480 Vector<RacyAddress> racy_addresses;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000481 Vector<FiredSuppression> fired_suppressions;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000482
483 Flags flags;
484
485 u64 stat[StatCnt];
486 u64 int_alloc_cnt[MBlockTypeCount];
487 u64 int_alloc_siz[MBlockTypeCount];
488};
489
490class ScopedInRtl {
491 public:
492 ScopedInRtl();
493 ~ScopedInRtl();
494 private:
495 ThreadState*thr_;
496 int in_rtl_;
497 int errno_;
498};
499
500class ScopedReport {
501 public:
502 explicit ScopedReport(ReportType typ);
503 ~ScopedReport();
504
505 void AddStack(const StackTrace *stack);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000506 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack,
507 const MutexSet *mset);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000508 void AddThread(const ThreadContext *tctx);
509 void AddMutex(const SyncVar *s);
510 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000511 void AddSleep(u32 stack_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000512
513 const ReportDesc *GetReport() const;
514
515 private:
516 Context *ctx_;
517 ReportDesc *rep_;
518
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000519 void AddMutex(u64 id);
520
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000521 ScopedReport(const ScopedReport&);
522 void operator = (const ScopedReport&);
523};
524
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000525void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset);
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000526
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000527void StatAggregate(u64 *dst, u64 *src);
528void StatOutput(u64 *stat);
529void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
530 if (kCollectStats)
531 thr->stat[typ] += n;
532}
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000533void ALWAYS_INLINE INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
534 if (kCollectStats)
535 thr->stat[typ] = n;
536}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000537
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000538void MapShadow(uptr addr, uptr size);
Dmitry Vyukov3e7ede22012-12-13 08:14:02 +0000539void MapThreadTrace(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000540void InitializeShadowMemory();
541void InitializeInterceptors();
542void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000543
544void ReportRace(ThreadState *thr);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000545bool OutputReport(Context *ctx,
546 const ScopedReport &srep,
Dmitry Vyukovf4f76b12013-01-24 13:50:32 +0000547 const ReportStack *suppress_stack1 = 0,
548 const ReportStack *suppress_stack2 = 0);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000549bool IsFiredSuppression(Context *ctx,
550 const ScopedReport &srep,
551 const StackTrace &trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000552bool IsExpectedReport(uptr addr, uptr size);
Kostya Serebryany4fb340d2013-02-06 14:24:00 +0000553bool FrameIsInternal(const ReportStack *frame);
Alexey Samsonov85cc9b62013-02-06 16:28:05 +0000554ReportStack *SkipTsanInternalFrames(ReportStack *ent);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000555
556#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000557# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000558#else
559# define DPrintf(...)
560#endif
561
562#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000563# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000564#else
565# define DPrintf2(...)
566#endif
567
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000568u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000569void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov019ef672013-01-29 14:20:12 +0000570void PrintCurrentStackSlow(); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000571
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000572void Initialize(ThreadState *thr);
573int Finalize(ThreadState *thr);
574
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000575SyncVar* GetJavaSync(ThreadState *thr, uptr pc, uptr addr,
576 bool write_lock, bool create);
577SyncVar* GetAndRemoveJavaSync(ThreadState *thr, uptr pc, uptr addr);
578
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000579void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000580 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000581void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000582 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000583 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000584void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000585 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000586void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
587 uptr size, uptr step, bool is_write);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000588
589const int kSizeLog1 = 0;
590const int kSizeLog2 = 1;
591const int kSizeLog4 = 2;
592const int kSizeLog8 = 3;
593
594void ALWAYS_INLINE INLINE MemoryRead(ThreadState *thr, uptr pc,
595 uptr addr, int kAccessSizeLog) {
596 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
597}
598
599void ALWAYS_INLINE INLINE MemoryWrite(ThreadState *thr, uptr pc,
600 uptr addr, int kAccessSizeLog) {
601 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
602}
603
604void ALWAYS_INLINE INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
605 uptr addr, int kAccessSizeLog) {
606 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
607}
608
609void ALWAYS_INLINE INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
610 uptr addr, int kAccessSizeLog) {
611 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
612}
613
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000614void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
615void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000616void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000617void IgnoreCtl(ThreadState *thr, bool write, bool begin);
618
619void FuncEntry(ThreadState *thr, uptr pc);
620void FuncExit(ThreadState *thr);
621
622int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000623void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000624void ThreadFinish(ThreadState *thr);
625int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
626void ThreadJoin(ThreadState *thr, uptr pc, int tid);
627void ThreadDetach(ThreadState *thr, uptr pc, int tid);
628void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000629void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000630int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000631void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000632
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000633void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
634 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000635void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
636void MutexLock(ThreadState *thr, uptr pc, uptr addr);
637void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
638void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
639void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
640void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
641
642void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000643void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000644void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000645void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000646void AfterSleep(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000647
648// The hacky call uses custom calling convention and an assembly thunk.
649// It is considerably faster that a normal call for the caller
650// if it is not executed (it is intended for slow paths from hot functions).
651// The trick is that the call preserves all registers and the compiler
652// does not treat it as a call.
653// If it does not work for you, use normal call.
654#if TSAN_DEBUG == 0
655// The caller may not create the stack frame for itself at all,
656// so we create a reserve stack frame for it (1024b must be enough).
657#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000658 __asm__ __volatile__("sub $1024, %%rsp;" \
659 "/*.cfi_adjust_cfa_offset 1024;*/" \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000660 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000661 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000662 "add $1024, %%rsp;" \
663 "/*.cfi_adjust_cfa_offset -1024;*/" \
664 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000665#else
666#define HACKY_CALL(f) f()
667#endif
668
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000669void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000670uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000671uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000672uptr TraceParts();
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000673
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000674extern "C" void __tsan_trace_switch();
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000675void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000676 EventType typ, u64 addr) {
677 DCHECK_GE((int)typ, 0);
678 DCHECK_LE((int)typ, 7);
679 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000680 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000681 u64 pos = fs.GetTracePos();
682 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000683#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000684 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000685#else
686 TraceSwitch(thr);
687#endif
688 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000689 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000690 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000691 Event ev = (u64)addr | ((u64)typ << 61);
692 *evp = ev;
693}
694
695} // namespace __tsan
696
697#endif // TSAN_RTL_H