blob: a709b4f8f6bd4977fb2fb7fbbb39ce2c4d6ac256 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_rtl.h ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12// Main internal TSan header file.
13//
14// Ground rules:
15// - C++ run-time should not be used (static CTORs, RTTI, exceptions, static
16// function-scope locals)
17// - All functions/classes/etc reside in namespace __tsan, except for those
18// declared in tsan_interface.h.
19// - Platform-specific files should be used instead of ifdefs (*).
20// - No system headers included in header files (*).
21// - Platform specific headres included only into platform-specific files (*).
22//
23// (*) Except when inlining is critical for performance.
24//===----------------------------------------------------------------------===//
25
26#ifndef TSAN_RTL_H
27#define TSAN_RTL_H
28
Kostya Serebryany571232b2012-12-05 10:09:15 +000029#include "sanitizer_common/sanitizer_allocator.h"
Alexey Samsonovc30e2d62013-05-29 09:15:39 +000030#include "sanitizer_common/sanitizer_allocator_internal.h"
Kostya Serebryany14e92c22013-12-05 07:44:35 +000031#include "sanitizer_common/sanitizer_asm.h"
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000032#include "sanitizer_common/sanitizer_common.h"
Dmitry Vyukov6cfab722014-02-28 10:48:13 +000033#include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
Dmitry Vyukov5ba73642013-10-03 13:37:17 +000034#include "sanitizer_common/sanitizer_libignore.h"
Sergey Matveevd109eb02013-06-26 15:37:14 +000035#include "sanitizer_common/sanitizer_suppressions.h"
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000036#include "sanitizer_common/sanitizer_thread_registry.h"
Kostya Serebryany4ad375f2012-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 Vyukov2429b022012-11-28 10:35:31 +000044#include "tsan_platform.h"
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000045#include "tsan_mutexset.h"
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +000046#include "tsan_ignoreset.h"
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +000047#include "tsan_stack_trace.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048
Kostya Serebryany242b6302012-12-04 15:13:30 +000049#if SANITIZER_WORDSIZE != 64
50# error "ThreadSanitizer is supported only on 64-bit platforms"
51#endif
52
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000053namespace __tsan {
54
Kostya Serebryany83ed8892014-12-09 01:31:14 +000055#ifndef SANITIZER_GO
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000056struct MapUnmapCallback;
Adhemerval Zanellad7984712015-08-05 15:17:59 +000057#if defined(__mips64) || defined(__aarch64__)
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +000058static const uptr kAllocatorSpace = 0;
59static const uptr kAllocatorSize = SANITIZER_MMAP_RANGE_SIZE;
60static const uptr kAllocatorRegionSizeLog = 20;
61static const uptr kAllocatorNumRegions =
62 kAllocatorSize >> kAllocatorRegionSizeLog;
63typedef TwoLevelByteMap<(kAllocatorNumRegions >> 12), 1 << 12,
64 MapUnmapCallback> ByteMap;
65typedef SizeClassAllocator32<kAllocatorSpace, kAllocatorSize, 0,
66 CompactSizeClassMap, kAllocatorRegionSizeLog, ByteMap,
67 MapUnmapCallback> PrimaryAllocator;
68#else
Adhemerval Zanella066c5f02015-11-26 13:10:47 +000069typedef SizeClassAllocator64<Mapping::kHeapMemBeg,
70 Mapping::kHeapMemEnd - Mapping::kHeapMemBeg, 0,
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000071 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +000072#endif
Kostya Serebryanyf2992882012-12-04 14:15:17 +000073typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000074typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000075typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
76 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000077Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000078#endif
79
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000080void TsanCheckFailed(const char *file, int line, const char *cond,
81 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000082
Dmitry Vyukov79915de2013-03-20 10:31:53 +000083const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
84
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000085// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000086// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000087// tid : kTidBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000088// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000089// history_size : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000090// epoch : kClkBits
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000091class FastState {
92 public:
93 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000094 x_ = tid << kTidShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000095 x_ |= epoch;
Dmitry Vyukov00e46042012-11-28 10:49:27 +000096 DCHECK_EQ(tid, this->tid());
97 DCHECK_EQ(epoch, this->epoch());
98 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000099 }
100
101 explicit FastState(u64 x)
102 : x_(x) {
103 }
104
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000105 u64 raw() const {
106 return x_;
107 }
108
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000109 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000110 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
111 return res;
112 }
113
114 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000115 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000116 return res;
117 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000118
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000119 u64 epoch() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000120 u64 res = x_ & ((1ull << kClkBits) - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000121 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000122 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000123
124 void IncrementEpoch() {
125 u64 old_epoch = epoch();
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000126 x_ += 1;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000127 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000128 (void)old_epoch;
129 }
130
131 void SetIgnoreBit() { x_ |= kIgnoreBit; }
132 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000133 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000134
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000135 void SetHistorySize(int hs) {
136 CHECK_GE(hs, 0);
137 CHECK_LE(hs, 7);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000138 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000139 }
140
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000141 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000142 int GetHistorySize() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000143 return (int)((x_ >> kHistoryShift) & kHistoryMask);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000144 }
145
146 void ClearHistorySize() {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000147 SetHistorySize(0);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000148 }
149
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000150 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000151 u64 GetTracePos() const {
152 const int hs = GetHistorySize();
153 // When hs == 0, the trace consists of 2 parts.
154 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
155 return epoch() & mask;
156 }
157
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000158 private:
159 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000160 static const int kTidShift = 64 - kTidBits - 1;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000161 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000162 static const u64 kFreedBit = 1ull << 63;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000163 static const u64 kHistoryShift = kClkBits;
164 static const u64 kHistoryMask = 7;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000165 u64 x_;
166};
167
168// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000169// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000170// tid : kTidBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000171// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000172// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000173// size_log : 2
174// addr0 : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000175// epoch : kClkBits
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000176class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000177 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000178 explicit Shadow(u64 x)
179 : FastState(x) {
180 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000181
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000182 explicit Shadow(const FastState &s)
183 : FastState(s.x_) {
184 ClearHistorySize();
185 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000186
187 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000188 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000189 DCHECK_LE(addr0, 7);
190 DCHECK_LE(kAccessSizeLog, 3);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000191 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000192 DCHECK_EQ(kAccessSizeLog, size_log());
193 DCHECK_EQ(addr0, this->addr0());
194 }
195
196 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000197 DCHECK_EQ(x_ & kReadBit, 0);
198 if (!kAccessIsWrite)
199 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000200 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000201 }
202
Dmitry Vyukovba429142013-02-01 09:42:06 +0000203 void SetAtomic(bool kIsAtomic) {
204 DCHECK(!IsAtomic());
205 if (kIsAtomic)
206 x_ |= kAtomicBit;
207 DCHECK_EQ(IsAtomic(), kIsAtomic);
208 }
209
210 bool IsAtomic() const {
211 return x_ & kAtomicBit;
212 }
213
214 bool IsZero() const {
215 return x_ == 0;
216 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000217
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000218 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000219 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000220 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000221 return shifted_xor == 0;
222 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000223
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000224 static ALWAYS_INLINE
225 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
226 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000227 return masked_xor == 0;
228 }
229
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000230 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231 unsigned kS2AccessSize) {
232 bool res = false;
233 u64 diff = s1.addr0() - s2.addr0();
234 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
235 // if (s1.addr0() + size1) > s2.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000236 if (s1.size() > -diff)
237 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000238 } else {
239 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000240 if (kS2AccessSize > diff)
241 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000242 }
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000243 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
244 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000245 return res;
246 }
247
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000248 u64 ALWAYS_INLINE addr0() const { return (x_ >> kClkBits) & 7; }
249 u64 ALWAYS_INLINE size() const { return 1ull << size_log(); }
250 bool ALWAYS_INLINE IsWrite() const { return !IsRead(); }
251 bool ALWAYS_INLINE IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000252
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000253 // The idea behind the freed bit is as follows.
254 // When the memory is freed (or otherwise unaccessible) we write to the shadow
255 // values with tid/epoch related to the free and the freed bit set.
256 // During memory accesses processing the freed bit is considered
257 // as msb of tid. So any access races with shadow with freed bit set
258 // (it is as if write from a thread with which we never synchronized before).
259 // This allows us to detect accesses to freed memory w/o additional
260 // overheads in memory access processing and at the same time restore
261 // tid/epoch of free.
262 void MarkAsFreed() {
263 x_ |= kFreedBit;
264 }
265
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000266 bool IsFreed() const {
267 return x_ & kFreedBit;
268 }
269
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000270 bool GetFreedAndReset() {
271 bool res = x_ & kFreedBit;
272 x_ &= ~kFreedBit;
273 return res;
274 }
275
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000276 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
277 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
278 | (u64(kIsAtomic) << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000279 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
280 return v;
281 }
282
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000283 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000284 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000285 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
286 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
287 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
288 return v;
289 }
290
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000291 bool ALWAYS_INLINE IsRWWeakerOrEqual(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
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000299 private:
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000300 static const u64 kReadShift = 5 + kClkBits;
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000301 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000302 static const u64 kAtomicShift = 6 + kClkBits;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000303 static const u64 kAtomicBit = 1ull << kAtomicShift;
304
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000305 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000306
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000307 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000308 if (s1.addr0() == s2.addr0()) return true;
309 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
310 return true;
311 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
312 return true;
313 return false;
314 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000315};
316
Dmitry Vyukovb79ac882015-03-02 17:36:02 +0000317struct ThreadSignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000318
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000319struct JmpBuf {
320 uptr sp;
321 uptr mangled_sp;
Dmitry Vyukov69c4d372014-09-16 21:48:22 +0000322 int int_signal_send;
323 bool in_blocking_func;
324 uptr in_signal_handler;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000325 uptr *shadow_stack_pos;
326};
327
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000328// This struct is stored in TLS.
329struct ThreadState {
330 FastState fast_state;
331 // Synch epoch represents the threads's epoch before the last synchronization
332 // action. It allows to reduce number of shadow state updates.
333 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
334 // if we are processing write to X from the same thread at epoch=200,
335 // we do nothing, because both writes happen in the same 'synch epoch'.
336 // That is, if another memory access does not race with the former write,
337 // it does not race with the latter as well.
338 // QUESTION: can we can squeeze this into ThreadState::Fast?
339 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
340 // taken by epoch between synchs.
341 // This way we can save one load from tls.
342 u64 fast_synch_epoch;
343 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
344 // We do not distinguish beteween ignoring reads and writes
345 // for better performance.
346 int ignore_reads_and_writes;
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000347 int ignore_sync;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000348 // Go does not support ignores.
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000349#ifndef SANITIZER_GO
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000350 IgnoreSet mop_ignore_set;
351 IgnoreSet sync_ignore_set;
352#endif
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000353 // C/C++ uses fixed size shadow stack embed into Trace.
354 // Go uses malloc-allocated shadow stack with dynamic size.
355 uptr *shadow_stack;
356 uptr *shadow_stack_end;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000357 uptr *shadow_stack_pos;
358 u64 *racy_shadow_addr;
359 u64 racy_state[2];
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000360 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000361 ThreadClock clock;
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000362#ifndef SANITIZER_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000363 AllocatorCache alloc_cache;
Alexey Samsonovc30e2d62013-05-29 09:15:39 +0000364 InternalAllocatorCache internal_alloc_cache;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000365 Vector<JmpBuf> jmp_bufs;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000366 int ignore_interceptors;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000367#endif
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000368#if TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000369 u64 stat[StatCnt];
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000370#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000371 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000372 const int unique_id;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000373 bool in_symbolizer;
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000374 bool in_ignored_lib;
Viktor Kutuzova7d323e2015-03-16 14:42:21 +0000375 bool is_inited;
Dmitry Vyukovf8cfdd92014-09-03 12:25:22 +0000376 bool is_dead;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000377 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000378 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000379 const uptr stk_addr;
380 const uptr stk_size;
381 const uptr tls_addr;
382 const uptr tls_size;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000383 ThreadContext *tctx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000384
Dmitry Vyukova22870e2015-02-13 15:37:11 +0000385#if SANITIZER_DEBUG && !SANITIZER_GO
Kostya Serebryanya63632a2014-02-14 12:20:42 +0000386 InternalDeadlockDetector internal_deadlock_detector;
Dmitry Vyukova22870e2015-02-13 15:37:11 +0000387#endif
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000388 DDPhysicalThread *dd_pt;
389 DDLogicalThread *dd_lt;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000390
Dmitry Vyukov18412192014-09-02 12:27:45 +0000391 atomic_uintptr_t in_signal_handler;
Dmitry Vyukovb79ac882015-03-02 17:36:02 +0000392 ThreadSignalContext *signal_ctx;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000393
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000394 DenseSlabAllocCache block_cache;
395 DenseSlabAllocCache sync_cache;
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000396 DenseSlabAllocCache clock_cache;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000397
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000398#ifndef SANITIZER_GO
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000399 u32 last_sleep_stack_id;
400 ThreadClock last_sleep_clock;
401#endif
402
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000403 // Set in regions of runtime that must be signal-safe and fork-safe.
404 // If set, malloc must not be called.
405 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000406
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000407 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +0000408 unsigned reuse_count,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000409 uptr stk_addr, uptr stk_size,
410 uptr tls_addr, uptr tls_size);
411};
412
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000413#ifndef SANITIZER_GO
Kuba Breckacd18f282015-11-05 13:54:50 +0000414#if SANITIZER_MAC
415ThreadState *cur_thread();
416void cur_thread_finalize();
417#else
Kostya Serebryanye61f4d52014-05-12 10:40:33 +0000418__attribute__((tls_model("initial-exec")))
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000419extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000420INLINE ThreadState *cur_thread() {
421 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
422}
Kuba Breckacd18f282015-11-05 13:54:50 +0000423INLINE void cur_thread_finalize() { }
424#endif // SANITIZER_MAC
425#endif // SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000426
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000427class ThreadContext : public ThreadContextBase {
428 public:
429 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000430 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000431 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000432 u32 creation_stack_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000433 SyncClock sync;
434 // Epoch at which the thread had started.
435 // If we see an event from the thread stamped by an older epoch,
436 // the event is from a dead thread that shared tid with this thread.
437 u64 epoch0;
438 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000439
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000440 // Override superclass callbacks.
Alexander Kornienkod8298122015-04-11 02:44:24 +0000441 void OnDead() override;
442 void OnJoined(void *arg) override;
443 void OnFinished() override;
444 void OnStarted(void *arg) override;
445 void OnCreated(void *arg) override;
446 void OnReset() override;
447 void OnDetached(void *arg) override;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000448};
449
450struct RacyStacks {
451 MD5Hash hash[2];
452 bool operator==(const RacyStacks &other) const {
453 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
454 return true;
455 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
456 return true;
457 return false;
458 }
459};
460
461struct RacyAddress {
462 uptr addr_min;
463 uptr addr_max;
464};
465
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000466struct FiredSuppression {
467 ReportType type;
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000468 uptr pc_or_addr;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000469 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000470};
471
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000472struct Context {
473 Context();
474
475 bool initialized;
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000476 bool after_multithreaded_fork;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000477
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000478 MetaMap metamap;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000479
480 Mutex report_mtx;
481 int nreported;
482 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000483 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000484
Dmitry Vyukovb7ebc532014-04-24 13:09:17 +0000485 void *background_thread;
486 atomic_uint32_t stop_background_thread;
487
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000488 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000489
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000490 Mutex racy_mtx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000491 Vector<RacyStacks> racy_stacks;
492 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000493 // Number of fired suppressions may be large enough.
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000494 Mutex fired_suppressions_mtx;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000495 InternalMmapVector<FiredSuppression> fired_suppressions;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000496 DDetector *dd;
Kostya Serebryany0548c792014-02-21 15:07:18 +0000497
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000498 ClockAlloc clock_alloc;
499
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000500 Flags flags;
501
502 u64 stat[StatCnt];
503 u64 int_alloc_cnt[MBlockTypeCount];
504 u64 int_alloc_siz[MBlockTypeCount];
505};
506
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000507extern Context *ctx; // The one and the only global runtime context.
508
Dmitry Vyukovce372102013-12-24 12:55:56 +0000509struct ScopedIgnoreInterceptors {
510 ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000511#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000512 cur_thread()->ignore_interceptors++;
513#endif
514 }
515
516 ~ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000517#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000518 cur_thread()->ignore_interceptors--;
519#endif
520 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000521};
522
523class ScopedReport {
524 public:
525 explicit ScopedReport(ReportType typ);
526 ~ScopedReport();
527
Alexey Samsonov40733a82014-11-03 22:23:44 +0000528 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000529 const MutexSet *mset);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000530 void AddStack(StackTrace stack, bool suppressable = false);
Dmitry Vyukova43e98c2014-05-28 18:03:32 +0000531 void AddThread(const ThreadContext *tctx, bool suppressable = false);
532 void AddThread(int unique_tid, bool suppressable = false);
Kostya Serebryany3df5d872014-03-21 13:00:18 +0000533 void AddUniqueTid(int unique_tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000534 void AddMutex(const SyncVar *s);
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000535 u64 AddMutex(u64 id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000536 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000537 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000538 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000539
540 const ReportDesc *GetReport() const;
541
542 private:
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000543 ReportDesc *rep_;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000544 // Symbolizer makes lots of intercepted calls. If we try to process them,
545 // at best it will cause deadlocks on internal mutexes.
546 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000547
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000548 void AddDeadMutex(u64 id);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000549
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000550 ScopedReport(const ScopedReport&);
551 void operator = (const ScopedReport&);
552};
553
Alexey Samsonov40733a82014-11-03 22:23:44 +0000554void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
555 MutexSet *mset);
556
557template<typename StackTraceTy>
558void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
559 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
560 uptr start = 0;
561 if (size + !!toppc > kStackTraceMax) {
562 start = size + !!toppc - kStackTraceMax;
563 size = kStackTraceMax - !!toppc;
564 }
565 stack->Init(&thr->shadow_stack[start], size, toppc);
566}
567
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000568
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000569#if TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000570void StatAggregate(u64 *dst, u64 *src);
571void StatOutput(u64 *stat);
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000572#endif
573
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000574void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000575#if TSAN_COLLECT_STATS
Dmitry Vyukovb57e39e2015-02-13 16:08:43 +0000576 thr->stat[typ] += n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000577#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000578}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000579void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000580#if TSAN_COLLECT_STATS
Dmitry Vyukovb57e39e2015-02-13 16:08:43 +0000581 thr->stat[typ] = n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000582#endif
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000583}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000584
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000585void MapShadow(uptr addr, uptr size);
Evgeniy Stepanov8e9c70b2015-05-29 22:31:28 +0000586void MapThreadTrace(uptr addr, uptr size, const char *name);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000587void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000588void InitializeShadowMemory();
589void InitializeInterceptors();
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000590void InitializeLibIgnore();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000591void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000592
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000593void ForkBefore(ThreadState *thr, uptr pc);
594void ForkParentAfter(ThreadState *thr, uptr pc);
595void ForkChildAfter(ThreadState *thr, uptr pc);
596
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000597void ReportRace(ThreadState *thr);
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000598bool OutputReport(ThreadState *thr, const ScopedReport &srep);
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000599bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000600bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000601void PrintMatchedBenignRaces();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000602
603#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000604# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000605#else
606# define DPrintf(...)
607#endif
608
609#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000610# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000611#else
612# define DPrintf2(...)
613#endif
614
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000615u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000616ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000617void PrintCurrentStack(ThreadState *thr, uptr pc);
Alexey Samsonovfbaaed62014-11-06 18:43:45 +0000618void PrintCurrentStackSlow(uptr pc); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000619
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000620void Initialize(ThreadState *thr);
621int Finalize(ThreadState *thr);
622
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000623void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
624void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000625
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000626void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000627 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000628void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000629 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000630 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000631void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000632 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000633void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
634 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000635void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
636 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000637
638const int kSizeLog1 = 0;
639const int kSizeLog2 = 1;
640const int kSizeLog4 = 2;
641const int kSizeLog8 = 3;
642
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000643void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000644 uptr addr, int kAccessSizeLog) {
645 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
646}
647
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000648void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000649 uptr addr, int kAccessSizeLog) {
650 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
651}
652
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000653void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000654 uptr addr, int kAccessSizeLog) {
655 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
656}
657
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000658void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000659 uptr addr, int kAccessSizeLog) {
660 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
661}
662
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000663void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
664void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000665void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000666
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000667void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
668void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
669void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
670void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000671
672void FuncEntry(ThreadState *thr, uptr pc);
673void FuncExit(ThreadState *thr);
674
675int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000676void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000677void ThreadFinish(ThreadState *thr);
678int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
679void ThreadJoin(ThreadState *thr, uptr pc, int tid);
680void ThreadDetach(ThreadState *thr, uptr pc, int tid);
681void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000682void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000683int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000684void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000685
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000686void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
687 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000688void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany11f4f302014-02-25 08:24:15 +0000689void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
690 bool try_lock = false);
Dmitry Vyukovc9af8182013-05-17 12:03:46 +0000691int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Kostya Serebryany01be2962014-02-25 10:33:37 +0000692void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000693void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
694void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov4bbe6dc2013-11-15 16:58:12 +0000695void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000696
697void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukovbd167972014-11-18 06:44:43 +0000698// AcquireGlobal synchronizes the current thread with all other threads.
699// In terms of happens-before relation, it draws a HB edge from all threads
700// (where they happen to execute right now) to the current thread. We use it to
701// handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
702// right before executing finalizers. This provides a coarse, but simple
703// approximation of the actual required synchronization.
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000704void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000705void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000706void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000707void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000708void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
709void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
710void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
711void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000712
713// The hacky call uses custom calling convention and an assembly thunk.
714// It is considerably faster that a normal call for the caller
715// if it is not executed (it is intended for slow paths from hot functions).
716// The trick is that the call preserves all registers and the compiler
717// does not treat it as a call.
718// If it does not work for you, use normal call.
Kuba Breckacf41c0c2015-11-03 14:33:39 +0000719#if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000720// The caller may not create the stack frame for itself at all,
721// so we create a reserve stack frame for it (1024b must be enough).
722#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000723 __asm__ __volatile__("sub $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000724 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000725 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000726 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000727 "add $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000728 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000729 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000730#else
731#define HACKY_CALL(f) f()
732#endif
733
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000734void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000735uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000736uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000737uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000738Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000739
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000740extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000741void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000742 EventType typ, u64 addr) {
Dmitry Vyukov547089e2014-05-15 12:51:48 +0000743 if (!kCollectHistory)
744 return;
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000745 DCHECK_GE((int)typ, 0);
746 DCHECK_LE((int)typ, 7);
747 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000748 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000749 u64 pos = fs.GetTracePos();
750 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000751#ifndef SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000752 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000753#else
754 TraceSwitch(thr);
755#endif
756 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000757 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000758 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000759 Event ev = (u64)addr | ((u64)typ << 61);
760 *evp = ev;
761}
762
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000763#ifndef SANITIZER_GO
764uptr ALWAYS_INLINE HeapEnd() {
Adhemerval Zanella066c5f02015-11-26 13:10:47 +0000765 return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000766}
767#endif
768
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000769} // namespace __tsan
770
771#endif // TSAN_RTL_H