blob: 2e6be3ff06520f88dcdb64df4e479ff984c0a051 [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
Dmitry Vyukove9a5f032014-10-24 17:07:29 +000069typedef SizeClassAllocator64<kHeapMemBeg, kHeapMemEnd - kHeapMemBeg, 0,
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000070 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +000071#endif
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
Dmitry Vyukov79915de2013-03-20 10:31:53 +000082const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
83
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000084// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000085// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000086// tid : kTidBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000087// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000088// history_size : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000089// epoch : kClkBits
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000090class FastState {
91 public:
92 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000093 x_ = tid << kTidShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000094 x_ |= epoch;
Dmitry Vyukov00e46042012-11-28 10:49:27 +000095 DCHECK_EQ(tid, this->tid());
96 DCHECK_EQ(epoch, this->epoch());
97 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000098 }
99
100 explicit FastState(u64 x)
101 : x_(x) {
102 }
103
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000104 u64 raw() const {
105 return x_;
106 }
107
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000108 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000109 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
110 return res;
111 }
112
113 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000114 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000115 return res;
116 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000117
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000118 u64 epoch() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000119 u64 res = x_ & ((1ull << kClkBits) - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000120 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000121 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000122
123 void IncrementEpoch() {
124 u64 old_epoch = epoch();
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000125 x_ += 1;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000126 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000127 (void)old_epoch;
128 }
129
130 void SetIgnoreBit() { x_ |= kIgnoreBit; }
131 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000132 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000133
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000134 void SetHistorySize(int hs) {
135 CHECK_GE(hs, 0);
136 CHECK_LE(hs, 7);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000137 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000138 }
139
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000140 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000141 int GetHistorySize() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000142 return (int)((x_ >> kHistoryShift) & kHistoryMask);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000143 }
144
145 void ClearHistorySize() {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000146 SetHistorySize(0);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000147 }
148
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000149 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000150 u64 GetTracePos() const {
151 const int hs = GetHistorySize();
152 // When hs == 0, the trace consists of 2 parts.
153 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
154 return epoch() & mask;
155 }
156
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000157 private:
158 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000159 static const int kTidShift = 64 - kTidBits - 1;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000160 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000161 static const u64 kFreedBit = 1ull << 63;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000162 static const u64 kHistoryShift = kClkBits;
163 static const u64 kHistoryMask = 7;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000164 u64 x_;
165};
166
167// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000168// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000169// tid : kTidBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000170// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000171// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000172// size_log : 2
173// addr0 : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000174// epoch : kClkBits
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000175class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000176 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000177 explicit Shadow(u64 x)
178 : FastState(x) {
179 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000180
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000181 explicit Shadow(const FastState &s)
182 : FastState(s.x_) {
183 ClearHistorySize();
184 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000185
186 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000187 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000188 DCHECK_LE(addr0, 7);
189 DCHECK_LE(kAccessSizeLog, 3);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000190 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191 DCHECK_EQ(kAccessSizeLog, size_log());
192 DCHECK_EQ(addr0, this->addr0());
193 }
194
195 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000196 DCHECK_EQ(x_ & kReadBit, 0);
197 if (!kAccessIsWrite)
198 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000199 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000200 }
201
Dmitry Vyukovba429142013-02-01 09:42:06 +0000202 void SetAtomic(bool kIsAtomic) {
203 DCHECK(!IsAtomic());
204 if (kIsAtomic)
205 x_ |= kAtomicBit;
206 DCHECK_EQ(IsAtomic(), kIsAtomic);
207 }
208
209 bool IsAtomic() const {
210 return x_ & kAtomicBit;
211 }
212
213 bool IsZero() const {
214 return x_ == 0;
215 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000216
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000217 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000218 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000219 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000220 return shifted_xor == 0;
221 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000222
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000223 static ALWAYS_INLINE
224 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
225 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 return masked_xor == 0;
227 }
228
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000229 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000230 unsigned kS2AccessSize) {
231 bool res = false;
232 u64 diff = s1.addr0() - s2.addr0();
233 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
234 // if (s1.addr0() + size1) > s2.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000235 if (s1.size() > -diff)
236 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237 } else {
238 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000239 if (kS2AccessSize > diff)
240 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000241 }
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000242 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
243 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000244 return res;
245 }
246
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000247 u64 ALWAYS_INLINE addr0() const { return (x_ >> kClkBits) & 7; }
248 u64 ALWAYS_INLINE size() const { return 1ull << size_log(); }
249 bool ALWAYS_INLINE IsWrite() const { return !IsRead(); }
250 bool ALWAYS_INLINE IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000251
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000252 // The idea behind the freed bit is as follows.
253 // When the memory is freed (or otherwise unaccessible) we write to the shadow
254 // values with tid/epoch related to the free and the freed bit set.
255 // During memory accesses processing the freed bit is considered
256 // as msb of tid. So any access races with shadow with freed bit set
257 // (it is as if write from a thread with which we never synchronized before).
258 // This allows us to detect accesses to freed memory w/o additional
259 // overheads in memory access processing and at the same time restore
260 // tid/epoch of free.
261 void MarkAsFreed() {
262 x_ |= kFreedBit;
263 }
264
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000265 bool IsFreed() const {
266 return x_ & kFreedBit;
267 }
268
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000269 bool GetFreedAndReset() {
270 bool res = x_ & kFreedBit;
271 x_ &= ~kFreedBit;
272 return res;
273 }
274
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000275 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
276 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
277 | (u64(kIsAtomic) << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000278 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
279 return v;
280 }
281
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000282 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000283 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000284 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
285 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
286 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
287 return v;
288 }
289
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000290 bool ALWAYS_INLINE IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000291 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000292 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
293 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
294 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
295 return v;
296 }
297
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298 private:
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000299 static const u64 kReadShift = 5 + kClkBits;
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000300 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000301 static const u64 kAtomicShift = 6 + kClkBits;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000302 static const u64 kAtomicBit = 1ull << kAtomicShift;
303
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000304 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000305
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000306 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000307 if (s1.addr0() == s2.addr0()) return true;
308 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
309 return true;
310 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
311 return true;
312 return false;
313 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000314};
315
Dmitry Vyukovb79ac882015-03-02 17:36:02 +0000316struct ThreadSignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000317
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000318struct JmpBuf {
319 uptr sp;
320 uptr mangled_sp;
Dmitry Vyukov69c4d372014-09-16 21:48:22 +0000321 int int_signal_send;
322 bool in_blocking_func;
323 uptr in_signal_handler;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000324 uptr *shadow_stack_pos;
325};
326
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000327// 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;
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000346 int ignore_sync;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000347 // Go does not support ignores.
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000348#ifndef SANITIZER_GO
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000349 IgnoreSet mop_ignore_set;
350 IgnoreSet sync_ignore_set;
351#endif
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000352 // C/C++ uses fixed size shadow stack embed into Trace.
353 // Go uses malloc-allocated shadow stack with dynamic size.
354 uptr *shadow_stack;
355 uptr *shadow_stack_end;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000356 uptr *shadow_stack_pos;
357 u64 *racy_shadow_addr;
358 u64 racy_state[2];
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000359 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000360 ThreadClock clock;
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000361#ifndef SANITIZER_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000362 AllocatorCache alloc_cache;
Alexey Samsonovc30e2d62013-05-29 09:15:39 +0000363 InternalAllocatorCache internal_alloc_cache;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000364 Vector<JmpBuf> jmp_bufs;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000365 int ignore_interceptors;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000366#endif
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000367#if TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000368 u64 stat[StatCnt];
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000369#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000370 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000371 const int unique_id;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000372 bool in_symbolizer;
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000373 bool in_ignored_lib;
Viktor Kutuzova7d323e2015-03-16 14:42:21 +0000374 bool is_inited;
Dmitry Vyukovf8cfdd92014-09-03 12:25:22 +0000375 bool is_dead;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000376 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000377 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000378 const uptr stk_addr;
379 const uptr stk_size;
380 const uptr tls_addr;
381 const uptr tls_size;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000382 ThreadContext *tctx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000383
Dmitry Vyukova22870e2015-02-13 15:37:11 +0000384#if SANITIZER_DEBUG && !SANITIZER_GO
Kostya Serebryanya63632a2014-02-14 12:20:42 +0000385 InternalDeadlockDetector internal_deadlock_detector;
Dmitry Vyukova22870e2015-02-13 15:37:11 +0000386#endif
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000387 DDPhysicalThread *dd_pt;
388 DDLogicalThread *dd_lt;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000389
Dmitry Vyukov18412192014-09-02 12:27:45 +0000390 atomic_uintptr_t in_signal_handler;
Dmitry Vyukovb79ac882015-03-02 17:36:02 +0000391 ThreadSignalContext *signal_ctx;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000392
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000393 DenseSlabAllocCache block_cache;
394 DenseSlabAllocCache sync_cache;
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000395 DenseSlabAllocCache clock_cache;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000396
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000397#ifndef SANITIZER_GO
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000398 u32 last_sleep_stack_id;
399 ThreadClock last_sleep_clock;
400#endif
401
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000402 // Set in regions of runtime that must be signal-safe and fork-safe.
403 // If set, malloc must not be called.
404 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000405
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000406 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +0000407 unsigned reuse_count,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000408 uptr stk_addr, uptr stk_size,
409 uptr tls_addr, uptr tls_size);
410};
411
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000412#ifndef SANITIZER_GO
Kuba Breckacd18f282015-11-05 13:54:50 +0000413#if SANITIZER_MAC
414ThreadState *cur_thread();
415void cur_thread_finalize();
416#else
Kostya Serebryanye61f4d52014-05-12 10:40:33 +0000417__attribute__((tls_model("initial-exec")))
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000418extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000419INLINE ThreadState *cur_thread() {
420 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
421}
Kuba Breckacd18f282015-11-05 13:54:50 +0000422INLINE void cur_thread_finalize() { }
423#endif // SANITIZER_MAC
424#endif // SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000425
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000426class ThreadContext : public ThreadContextBase {
427 public:
428 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000429 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000430 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000431 u32 creation_stack_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000432 SyncClock sync;
433 // Epoch at which the thread had started.
434 // If we see an event from the thread stamped by an older epoch,
435 // the event is from a dead thread that shared tid with this thread.
436 u64 epoch0;
437 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000438
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000439 // Override superclass callbacks.
Alexander Kornienkod8298122015-04-11 02:44:24 +0000440 void OnDead() override;
441 void OnJoined(void *arg) override;
442 void OnFinished() override;
443 void OnStarted(void *arg) override;
444 void OnCreated(void *arg) override;
445 void OnReset() override;
446 void OnDetached(void *arg) override;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000447};
448
449struct RacyStacks {
450 MD5Hash hash[2];
451 bool operator==(const RacyStacks &other) const {
452 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
453 return true;
454 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
455 return true;
456 return false;
457 }
458};
459
460struct RacyAddress {
461 uptr addr_min;
462 uptr addr_max;
463};
464
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000465struct FiredSuppression {
466 ReportType type;
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000467 uptr pc_or_addr;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000468 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000469};
470
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000471struct Context {
472 Context();
473
474 bool initialized;
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000475 bool after_multithreaded_fork;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000476
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000477 MetaMap metamap;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000478
479 Mutex report_mtx;
480 int nreported;
481 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000482 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000483
Dmitry Vyukovb7ebc532014-04-24 13:09:17 +0000484 void *background_thread;
485 atomic_uint32_t stop_background_thread;
486
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000487 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000488
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000489 Mutex racy_mtx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000490 Vector<RacyStacks> racy_stacks;
491 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000492 // Number of fired suppressions may be large enough.
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000493 Mutex fired_suppressions_mtx;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000494 InternalMmapVector<FiredSuppression> fired_suppressions;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000495 DDetector *dd;
Kostya Serebryany0548c792014-02-21 15:07:18 +0000496
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000497 ClockAlloc clock_alloc;
498
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000499 Flags flags;
500
501 u64 stat[StatCnt];
502 u64 int_alloc_cnt[MBlockTypeCount];
503 u64 int_alloc_siz[MBlockTypeCount];
504};
505
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000506extern Context *ctx; // The one and the only global runtime context.
507
Dmitry Vyukovce372102013-12-24 12:55:56 +0000508struct ScopedIgnoreInterceptors {
509 ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000510#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000511 cur_thread()->ignore_interceptors++;
512#endif
513 }
514
515 ~ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000516#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000517 cur_thread()->ignore_interceptors--;
518#endif
519 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000520};
521
522class ScopedReport {
523 public:
524 explicit ScopedReport(ReportType typ);
525 ~ScopedReport();
526
Alexey Samsonov40733a82014-11-03 22:23:44 +0000527 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000528 const MutexSet *mset);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000529 void AddStack(StackTrace stack, bool suppressable = false);
Dmitry Vyukova43e98c2014-05-28 18:03:32 +0000530 void AddThread(const ThreadContext *tctx, bool suppressable = false);
531 void AddThread(int unique_tid, bool suppressable = false);
Kostya Serebryany3df5d872014-03-21 13:00:18 +0000532 void AddUniqueTid(int unique_tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000533 void AddMutex(const SyncVar *s);
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000534 u64 AddMutex(u64 id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000535 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000536 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000537 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000538
539 const ReportDesc *GetReport() const;
540
541 private:
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000542 ReportDesc *rep_;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000543 // Symbolizer makes lots of intercepted calls. If we try to process them,
544 // at best it will cause deadlocks on internal mutexes.
545 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000546
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000547 void AddDeadMutex(u64 id);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000548
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000549 ScopedReport(const ScopedReport&);
550 void operator = (const ScopedReport&);
551};
552
Alexey Samsonov40733a82014-11-03 22:23:44 +0000553void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
554 MutexSet *mset);
555
556template<typename StackTraceTy>
557void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
558 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
559 uptr start = 0;
560 if (size + !!toppc > kStackTraceMax) {
561 start = size + !!toppc - kStackTraceMax;
562 size = kStackTraceMax - !!toppc;
563 }
564 stack->Init(&thr->shadow_stack[start], size, toppc);
565}
566
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000567
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000568#if TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000569void StatAggregate(u64 *dst, u64 *src);
570void StatOutput(u64 *stat);
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000571#endif
572
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000573void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000574#if TSAN_COLLECT_STATS
Dmitry Vyukovb57e39e2015-02-13 16:08:43 +0000575 thr->stat[typ] += n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000576#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000577}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000578void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000579#if TSAN_COLLECT_STATS
Dmitry Vyukovb57e39e2015-02-13 16:08:43 +0000580 thr->stat[typ] = n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000581#endif
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000582}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000583
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000584void MapShadow(uptr addr, uptr size);
Evgeniy Stepanov8e9c70b2015-05-29 22:31:28 +0000585void MapThreadTrace(uptr addr, uptr size, const char *name);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000586void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000587void InitializeShadowMemory();
588void InitializeInterceptors();
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000589void InitializeLibIgnore();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000590void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000591
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000592void ForkBefore(ThreadState *thr, uptr pc);
593void ForkParentAfter(ThreadState *thr, uptr pc);
594void ForkChildAfter(ThreadState *thr, uptr pc);
595
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000596void ReportRace(ThreadState *thr);
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000597bool OutputReport(ThreadState *thr, const ScopedReport &srep);
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000598bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000599bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000600void PrintMatchedBenignRaces();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000601
602#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000603# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000604#else
605# define DPrintf(...)
606#endif
607
608#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000609# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000610#else
611# define DPrintf2(...)
612#endif
613
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000614u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000615ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000616void PrintCurrentStack(ThreadState *thr, uptr pc);
Alexey Samsonovfbaaed62014-11-06 18:43:45 +0000617void PrintCurrentStackSlow(uptr pc); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000618
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000619void Initialize(ThreadState *thr);
620int Finalize(ThreadState *thr);
621
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000622void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
623void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000624
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000625void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000626 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000627void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000628 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000629 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000630void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000631 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000632void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
633 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000634void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
635 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000636
637const int kSizeLog1 = 0;
638const int kSizeLog2 = 1;
639const int kSizeLog4 = 2;
640const int kSizeLog8 = 3;
641
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000642void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000643 uptr addr, int kAccessSizeLog) {
644 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
645}
646
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000647void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000648 uptr addr, int kAccessSizeLog) {
649 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
650}
651
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000652void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000653 uptr addr, int kAccessSizeLog) {
654 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
655}
656
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000657void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000658 uptr addr, int kAccessSizeLog) {
659 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
660}
661
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000662void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
663void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000664void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000665
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000666void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
667void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
668void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
669void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000670
671void FuncEntry(ThreadState *thr, uptr pc);
672void FuncExit(ThreadState *thr);
673
674int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000675void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000676void ThreadFinish(ThreadState *thr);
677int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
678void ThreadJoin(ThreadState *thr, uptr pc, int tid);
679void ThreadDetach(ThreadState *thr, uptr pc, int tid);
680void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000681void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000682int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000683void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000684
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000685void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
686 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000687void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany11f4f302014-02-25 08:24:15 +0000688void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
689 bool try_lock = false);
Dmitry Vyukovc9af8182013-05-17 12:03:46 +0000690int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Kostya Serebryany01be2962014-02-25 10:33:37 +0000691void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000692void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
693void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov4bbe6dc2013-11-15 16:58:12 +0000694void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000695
696void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukovbd167972014-11-18 06:44:43 +0000697// AcquireGlobal synchronizes the current thread with all other threads.
698// In terms of happens-before relation, it draws a HB edge from all threads
699// (where they happen to execute right now) to the current thread. We use it to
700// handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
701// right before executing finalizers. This provides a coarse, but simple
702// approximation of the actual required synchronization.
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000703void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000704void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000705void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000706void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000707void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
708void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
709void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
710void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000711
712// The hacky call uses custom calling convention and an assembly thunk.
713// It is considerably faster that a normal call for the caller
714// if it is not executed (it is intended for slow paths from hot functions).
715// The trick is that the call preserves all registers and the compiler
716// does not treat it as a call.
717// If it does not work for you, use normal call.
Kuba Breckacf41c0c2015-11-03 14:33:39 +0000718#if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000719// The caller may not create the stack frame for itself at all,
720// so we create a reserve stack frame for it (1024b must be enough).
721#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000722 __asm__ __volatile__("sub $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000723 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000724 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000725 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000726 "add $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000727 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000728 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000729#else
730#define HACKY_CALL(f) f()
731#endif
732
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000733void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000734uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000735uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000736uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000737Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000738
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000739extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000740void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000741 EventType typ, u64 addr) {
Dmitry Vyukov547089e2014-05-15 12:51:48 +0000742 if (!kCollectHistory)
743 return;
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000744 DCHECK_GE((int)typ, 0);
745 DCHECK_LE((int)typ, 7);
746 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000747 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000748 u64 pos = fs.GetTracePos();
749 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000750#ifndef SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000751 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000752#else
753 TraceSwitch(thr);
754#endif
755 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000756 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000757 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000758 Event ev = (u64)addr | ((u64)typ << 61);
759 *evp = ev;
760}
761
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000762#ifndef SANITIZER_GO
763uptr ALWAYS_INLINE HeapEnd() {
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000764 return kHeapMemEnd + PrimaryAllocator::AdditionalSize();
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000765}
766#endif
767
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000768} // namespace __tsan
769
770#endif // TSAN_RTL_H