blob: 3db1a89c89c87d110e5d2ec5eff68be41186b7c6 [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
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000069struct MapUnmapCallback;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000070typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000071 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Kostya Serebryanyf2992882012-12-04 14:15:17 +000072typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000073typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000074typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
75 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000076Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000077#endif
78
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000079void TsanCheckFailed(const char *file, int line, const char *cond,
80 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000081
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000082// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000083// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000084// tid : kTidBits
85// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000086// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000087// history_size : 3
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000088class FastState {
89 public:
90 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000091 x_ = tid << kTidShift;
92 x_ |= epoch << kClkShift;
Dmitry Vyukov00e46042012-11-28 10:49:27 +000093 DCHECK_EQ(tid, this->tid());
94 DCHECK_EQ(epoch, this->epoch());
95 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000096 }
97
98 explicit FastState(u64 x)
99 : x_(x) {
100 }
101
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000102 u64 raw() const {
103 return x_;
104 }
105
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000106 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000107 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
108 return res;
109 }
110
111 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000112 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000113 return res;
114 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000115
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000116 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000117 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000118 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000119 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000120
121 void IncrementEpoch() {
122 u64 old_epoch = epoch();
123 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000124 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000125 (void)old_epoch;
126 }
127
128 void SetIgnoreBit() { x_ |= kIgnoreBit; }
129 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000130 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000131
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000132 void SetHistorySize(int hs) {
133 CHECK_GE(hs, 0);
134 CHECK_LE(hs, 7);
135 x_ = (x_ & ~7) | hs;
136 }
137
138 int GetHistorySize() const {
139 return (int)(x_ & 7);
140 }
141
142 void ClearHistorySize() {
143 x_ &= ~7;
144 }
145
146 u64 GetTracePos() const {
147 const int hs = GetHistorySize();
148 // When hs == 0, the trace consists of 2 parts.
149 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
150 return epoch() & mask;
151 }
152
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000153 private:
154 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000155 static const int kTidShift = 64 - kTidBits - 1;
156 static const int kClkShift = kTidShift - kClkBits;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000157 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000158 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159 u64 x_;
160};
161
162// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000163// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000164// tid : kTidBits
165// epoch : kClkBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000166// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000167// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000168// size_log : 2
169// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000170class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000171 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000172 explicit Shadow(u64 x)
173 : FastState(x) {
174 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000175
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000176 explicit Shadow(const FastState &s)
177 : FastState(s.x_) {
178 ClearHistorySize();
179 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000180
181 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
182 DCHECK_EQ(x_ & 31, 0);
183 DCHECK_LE(addr0, 7);
184 DCHECK_LE(kAccessSizeLog, 3);
185 x_ |= (kAccessSizeLog << 3) | addr0;
186 DCHECK_EQ(kAccessSizeLog, size_log());
187 DCHECK_EQ(addr0, this->addr0());
188 }
189
190 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000191 DCHECK_EQ(x_ & kReadBit, 0);
192 if (!kAccessIsWrite)
193 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000194 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000195 }
196
Dmitry Vyukovba429142013-02-01 09:42:06 +0000197 void SetAtomic(bool kIsAtomic) {
198 DCHECK(!IsAtomic());
199 if (kIsAtomic)
200 x_ |= kAtomicBit;
201 DCHECK_EQ(IsAtomic(), kIsAtomic);
202 }
203
204 bool IsAtomic() const {
205 return x_ & kAtomicBit;
206 }
207
208 bool IsZero() const {
209 return x_ == 0;
210 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000211
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000212 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000213 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000214 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000215 return shifted_xor == 0;
216 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000217
218 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000219 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
220 return masked_xor == 0;
221 }
222
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000223 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
224 unsigned kS2AccessSize) {
225 bool res = false;
226 u64 diff = s1.addr0() - s2.addr0();
227 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
228 // if (s1.addr0() + size1) > s2.addr0()) return true;
229 if (s1.size() > -diff) res = true;
230 } else {
231 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
232 if (kS2AccessSize > diff) res = true;
233 }
234 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
235 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
236 return res;
237 }
238
239 // The idea behind the offset is as follows.
240 // Consider that we have 8 bool's contained within a single 8-byte block
241 // (mapped to a single shadow "cell"). Now consider that we write to the bools
242 // from a single thread (which we consider the common case).
243 // W/o offsetting each access will have to scan 4 shadow values at average
244 // to find the corresponding shadow value for the bool.
245 // With offsetting we start scanning shadow with the offset so that
246 // each access hits necessary shadow straight off (at least in an expected
247 // optimistic case).
248 // This logic works seamlessly for any layout of user data. For example,
249 // if user data is {int, short, char, char}, then accesses to the int are
250 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
251 // from a single thread won't need to scan all 8 shadow values.
252 unsigned ComputeSearchOffset() {
253 return x_ & 7;
254 }
255 u64 addr0() const { return x_ & 7; }
256 u64 size() const { return 1ull << size_log(); }
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000257 bool IsWrite() const { return !IsRead(); }
258 bool IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000259
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000260 // The idea behind the freed bit is as follows.
261 // When the memory is freed (or otherwise unaccessible) we write to the shadow
262 // values with tid/epoch related to the free and the freed bit set.
263 // During memory accesses processing the freed bit is considered
264 // as msb of tid. So any access races with shadow with freed bit set
265 // (it is as if write from a thread with which we never synchronized before).
266 // This allows us to detect accesses to freed memory w/o additional
267 // overheads in memory access processing and at the same time restore
268 // tid/epoch of free.
269 void MarkAsFreed() {
270 x_ |= kFreedBit;
271 }
272
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000273 bool IsFreed() const {
274 return x_ & kFreedBit;
275 }
276
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000277 bool GetFreedAndReset() {
278 bool res = x_ & kFreedBit;
279 x_ &= ~kFreedBit;
280 return res;
281 }
282
Dmitry Vyukovba429142013-02-01 09:42:06 +0000283 bool IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000284 // analyzes 5-th bit (is_read) and 6-th bit (is_atomic)
285 bool v = x_ & u64(((kIsWrite ^ 1) << kReadShift)
286 | (kIsAtomic << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000287 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
288 return v;
289 }
290
291 bool IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000292 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000293 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
294 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
295 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
296 return v;
297 }
298
299 bool IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000300 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000301 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
302 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
303 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
304 return v;
305 }
306
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000307 private:
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000308 static const u64 kReadShift = 5;
309 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000310 static const u64 kAtomicShift = 6;
311 static const u64 kAtomicBit = 1ull << kAtomicShift;
312
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000313 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000314
315 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
316 if (s1.addr0() == s2.addr0()) return true;
317 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
318 return true;
319 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
320 return true;
321 return false;
322 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000323};
324
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000325struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000326
327// This struct is stored in TLS.
328struct ThreadState {
329 FastState fast_state;
330 // Synch epoch represents the threads's epoch before the last synchronization
331 // action. It allows to reduce number of shadow state updates.
332 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
333 // if we are processing write to X from the same thread at epoch=200,
334 // we do nothing, because both writes happen in the same 'synch epoch'.
335 // That is, if another memory access does not race with the former write,
336 // it does not race with the latter as well.
337 // QUESTION: can we can squeeze this into ThreadState::Fast?
338 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
339 // taken by epoch between synchs.
340 // This way we can save one load from tls.
341 u64 fast_synch_epoch;
342 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
343 // We do not distinguish beteween ignoring reads and writes
344 // for better performance.
345 int ignore_reads_and_writes;
346 uptr *shadow_stack_pos;
347 u64 *racy_shadow_addr;
348 u64 racy_state[2];
349 Trace trace;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000350#ifndef TSAN_GO
351 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000352 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000353#else
354 // Go uses satellite shadow stack with dynamic size.
355 uptr *shadow_stack;
356 uptr *shadow_stack_end;
357#endif
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000358 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000359 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000360#ifndef TSAN_GO
361 AllocatorCache alloc_cache;
362#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000363 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000365 const int unique_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000366 int in_rtl;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000367 bool in_symbolizer;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000368 bool is_alive;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000369 bool is_freeing;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000370 const uptr stk_addr;
371 const uptr stk_size;
372 const uptr tls_addr;
373 const uptr tls_size;
374
375 DeadlockDetector deadlock_detector;
376
377 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000378 SignalContext *signal_ctx;
379
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000380#ifndef TSAN_GO
381 u32 last_sleep_stack_id;
382 ThreadClock last_sleep_clock;
383#endif
384
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000385 // Set in regions of runtime that must be signal-safe and fork-safe.
386 // If set, malloc must not be called.
387 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000388
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000389 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000390 uptr stk_addr, uptr stk_size,
391 uptr tls_addr, uptr tls_size);
392};
393
394Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000395
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000396#ifndef TSAN_GO
397extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000398INLINE ThreadState *cur_thread() {
399 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
400}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000401#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000402
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000403// An info about a thread that is hold for some time after its termination.
404struct ThreadDeadInfo {
405 Trace trace;
406};
407
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000408class ThreadContext : public ThreadContextBase {
409 public:
410 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000411 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000412 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000413#ifdef TSAN_GO
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000414 StackTrace creation_stack;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000415#else
416 u32 creation_stack_id;
417#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000418 SyncClock sync;
419 // Epoch at which the thread had started.
420 // If we see an event from the thread stamped by an older epoch,
421 // the event is from a dead thread that shared tid with this thread.
422 u64 epoch0;
423 u64 epoch1;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000424 ThreadDeadInfo *dead_info;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000425
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000426 // Override superclass callbacks.
427 void OnDead();
428 void OnJoined(void *arg);
429 void OnFinished();
430 void OnStarted(void *arg);
431 void OnCreated(void *arg);
432 void OnReset(void *arg);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000433};
434
435struct RacyStacks {
436 MD5Hash hash[2];
437 bool operator==(const RacyStacks &other) const {
438 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
439 return true;
440 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
441 return true;
442 return false;
443 }
444};
445
446struct RacyAddress {
447 uptr addr_min;
448 uptr addr_max;
449};
450
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000451struct FiredSuppression {
452 ReportType type;
453 uptr pc;
454};
455
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000456struct Context {
457 Context();
458
459 bool initialized;
460
461 SyncTab synctab;
462
463 Mutex report_mtx;
464 int nreported;
465 int nmissed_expected;
466
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000467 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000468
469 Vector<RacyStacks> racy_stacks;
470 Vector<RacyAddress> racy_addresses;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000471 Vector<FiredSuppression> fired_suppressions;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000472
473 Flags flags;
474
475 u64 stat[StatCnt];
476 u64 int_alloc_cnt[MBlockTypeCount];
477 u64 int_alloc_siz[MBlockTypeCount];
478};
479
480class ScopedInRtl {
481 public:
482 ScopedInRtl();
483 ~ScopedInRtl();
484 private:
485 ThreadState*thr_;
486 int in_rtl_;
487 int errno_;
488};
489
490class ScopedReport {
491 public:
492 explicit ScopedReport(ReportType typ);
493 ~ScopedReport();
494
495 void AddStack(const StackTrace *stack);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000496 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack,
497 const MutexSet *mset);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000498 void AddThread(const ThreadContext *tctx);
499 void AddMutex(const SyncVar *s);
500 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000501 void AddSleep(u32 stack_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000502
503 const ReportDesc *GetReport() const;
504
505 private:
506 Context *ctx_;
507 ReportDesc *rep_;
508
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000509 void AddMutex(u64 id);
510
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000511 ScopedReport(const ScopedReport&);
512 void operator = (const ScopedReport&);
513};
514
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000515void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset);
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000516
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000517void StatAggregate(u64 *dst, u64 *src);
518void StatOutput(u64 *stat);
519void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
520 if (kCollectStats)
521 thr->stat[typ] += n;
522}
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000523void ALWAYS_INLINE INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
524 if (kCollectStats)
525 thr->stat[typ] = n;
526}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000527
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000528void MapShadow(uptr addr, uptr size);
Dmitry Vyukov3e7ede22012-12-13 08:14:02 +0000529void MapThreadTrace(uptr addr, uptr size);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000530void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000531void InitializeShadowMemory();
532void InitializeInterceptors();
533void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000534
535void ReportRace(ThreadState *thr);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000536bool OutputReport(Context *ctx,
537 const ScopedReport &srep,
Dmitry Vyukovf4f76b12013-01-24 13:50:32 +0000538 const ReportStack *suppress_stack1 = 0,
539 const ReportStack *suppress_stack2 = 0);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000540bool IsFiredSuppression(Context *ctx,
541 const ScopedReport &srep,
542 const StackTrace &trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000543bool IsExpectedReport(uptr addr, uptr size);
Kostya Serebryany4fb340d2013-02-06 14:24:00 +0000544bool FrameIsInternal(const ReportStack *frame);
Alexey Samsonov85cc9b62013-02-06 16:28:05 +0000545ReportStack *SkipTsanInternalFrames(ReportStack *ent);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000546
547#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000548# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000549#else
550# define DPrintf(...)
551#endif
552
553#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000554# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000555#else
556# define DPrintf2(...)
557#endif
558
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000559u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000560void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov019ef672013-01-29 14:20:12 +0000561void PrintCurrentStackSlow(); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000562
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000563void Initialize(ThreadState *thr);
564int Finalize(ThreadState *thr);
565
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000566SyncVar* GetJavaSync(ThreadState *thr, uptr pc, uptr addr,
567 bool write_lock, bool create);
568SyncVar* GetAndRemoveJavaSync(ThreadState *thr, uptr pc, uptr addr);
569
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000570void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000571 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000572void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000573 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000574 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000575void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000576 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000577void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
578 uptr size, uptr step, bool is_write);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000579
580const int kSizeLog1 = 0;
581const int kSizeLog2 = 1;
582const int kSizeLog4 = 2;
583const int kSizeLog8 = 3;
584
585void ALWAYS_INLINE INLINE MemoryRead(ThreadState *thr, uptr pc,
586 uptr addr, int kAccessSizeLog) {
587 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
588}
589
590void ALWAYS_INLINE INLINE MemoryWrite(ThreadState *thr, uptr pc,
591 uptr addr, int kAccessSizeLog) {
592 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
593}
594
595void ALWAYS_INLINE INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
596 uptr addr, int kAccessSizeLog) {
597 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
598}
599
600void ALWAYS_INLINE INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
601 uptr addr, int kAccessSizeLog) {
602 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
603}
604
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000605void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
606void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000607void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000608void IgnoreCtl(ThreadState *thr, bool write, bool begin);
609
610void FuncEntry(ThreadState *thr, uptr pc);
611void FuncExit(ThreadState *thr);
612
613int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000614void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000615void ThreadFinish(ThreadState *thr);
616int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
617void ThreadJoin(ThreadState *thr, uptr pc, int tid);
618void ThreadDetach(ThreadState *thr, uptr pc, int tid);
619void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000620void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000621int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000622void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000623
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000624void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
625 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000626void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
627void MutexLock(ThreadState *thr, uptr pc, uptr addr);
628void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
629void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
630void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
631void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
632
633void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000634void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000635void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000636void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000637void AfterSleep(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000638
639// The hacky call uses custom calling convention and an assembly thunk.
640// It is considerably faster that a normal call for the caller
641// if it is not executed (it is intended for slow paths from hot functions).
642// The trick is that the call preserves all registers and the compiler
643// does not treat it as a call.
644// If it does not work for you, use normal call.
645#if TSAN_DEBUG == 0
646// The caller may not create the stack frame for itself at all,
647// so we create a reserve stack frame for it (1024b must be enough).
648#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000649 __asm__ __volatile__("sub $1024, %%rsp;" \
650 "/*.cfi_adjust_cfa_offset 1024;*/" \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000651 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000652 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000653 "add $1024, %%rsp;" \
654 "/*.cfi_adjust_cfa_offset -1024;*/" \
655 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000656#else
657#define HACKY_CALL(f) f()
658#endif
659
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000660void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000661uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000662uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000663uptr TraceParts();
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000664
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000665extern "C" void __tsan_trace_switch();
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000666void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000667 EventType typ, u64 addr) {
668 DCHECK_GE((int)typ, 0);
669 DCHECK_LE((int)typ, 7);
670 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000671 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000672 u64 pos = fs.GetTracePos();
673 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000674#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000675 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000676#else
677 TraceSwitch(thr);
678#endif
679 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000680 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000681 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000682 Event ev = (u64)addr | ((u64)typ << 61);
683 *evp = ev;
684}
685
686} // namespace __tsan
687
688#endif // TSAN_RTL_H