blob: 768e8307fcc84d62a7066daa6b2289d8ff896b02 [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;
Dmitry Vyukove9a5f032014-10-24 17:07:29 +000057typedef SizeClassAllocator64<kHeapMemBeg, kHeapMemEnd - kHeapMemBeg, 0,
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000058 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Kostya Serebryanyf2992882012-12-04 14:15:17 +000059typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000060typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000061typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
62 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000063Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000064#endif
65
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000066void TsanCheckFailed(const char *file, int line, const char *cond,
67 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000068
Dmitry Vyukov79915de2013-03-20 10:31:53 +000069const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
70
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000071// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000072// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000073// tid : kTidBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000074// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000075// history_size : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000076// epoch : kClkBits
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000077class FastState {
78 public:
79 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000080 x_ = tid << kTidShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000081 x_ |= epoch;
Dmitry Vyukov00e46042012-11-28 10:49:27 +000082 DCHECK_EQ(tid, this->tid());
83 DCHECK_EQ(epoch, this->epoch());
84 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000085 }
86
87 explicit FastState(u64 x)
88 : x_(x) {
89 }
90
Dmitry Vyukov3482ec32012-08-16 15:08:49 +000091 u64 raw() const {
92 return x_;
93 }
94
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000095 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +000096 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
97 return res;
98 }
99
100 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000101 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000102 return res;
103 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000104
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000105 u64 epoch() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000106 u64 res = x_ & ((1ull << kClkBits) - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000107 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000108 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000109
110 void IncrementEpoch() {
111 u64 old_epoch = epoch();
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000112 x_ += 1;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000113 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000114 (void)old_epoch;
115 }
116
117 void SetIgnoreBit() { x_ |= kIgnoreBit; }
118 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000119 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000120
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000121 void SetHistorySize(int hs) {
122 CHECK_GE(hs, 0);
123 CHECK_LE(hs, 7);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000124 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000125 }
126
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000127 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000128 int GetHistorySize() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000129 return (int)((x_ >> kHistoryShift) & kHistoryMask);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000130 }
131
132 void ClearHistorySize() {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000133 SetHistorySize(0);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000134 }
135
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000136 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000137 u64 GetTracePos() const {
138 const int hs = GetHistorySize();
139 // When hs == 0, the trace consists of 2 parts.
140 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
141 return epoch() & mask;
142 }
143
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000144 private:
145 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000146 static const int kTidShift = 64 - kTidBits - 1;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000147 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000148 static const u64 kFreedBit = 1ull << 63;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000149 static const u64 kHistoryShift = kClkBits;
150 static const u64 kHistoryMask = 7;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000151 u64 x_;
152};
153
154// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000155// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000156// tid : kTidBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000157// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000158// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159// size_log : 2
160// addr0 : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000161// epoch : kClkBits
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000162class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000163 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000164 explicit Shadow(u64 x)
165 : FastState(x) {
166 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000167
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000168 explicit Shadow(const FastState &s)
169 : FastState(s.x_) {
170 ClearHistorySize();
171 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000172
173 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000174 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000175 DCHECK_LE(addr0, 7);
176 DCHECK_LE(kAccessSizeLog, 3);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000177 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000178 DCHECK_EQ(kAccessSizeLog, size_log());
179 DCHECK_EQ(addr0, this->addr0());
180 }
181
182 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000183 DCHECK_EQ(x_ & kReadBit, 0);
184 if (!kAccessIsWrite)
185 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000186 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000187 }
188
Dmitry Vyukovba429142013-02-01 09:42:06 +0000189 void SetAtomic(bool kIsAtomic) {
190 DCHECK(!IsAtomic());
191 if (kIsAtomic)
192 x_ |= kAtomicBit;
193 DCHECK_EQ(IsAtomic(), kIsAtomic);
194 }
195
196 bool IsAtomic() const {
197 return x_ & kAtomicBit;
198 }
199
200 bool IsZero() const {
201 return x_ == 0;
202 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000203
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000204 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000205 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000206 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000207 return shifted_xor == 0;
208 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000209
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000210 static ALWAYS_INLINE
211 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
212 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000213 return masked_xor == 0;
214 }
215
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000216 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000217 unsigned kS2AccessSize) {
218 bool res = false;
219 u64 diff = s1.addr0() - s2.addr0();
220 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
221 // if (s1.addr0() + size1) > s2.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000222 if (s1.size() > -diff)
223 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000224 } else {
225 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000226 if (kS2AccessSize > diff)
227 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228 }
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000229 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
230 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231 return res;
232 }
233
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000234 u64 ALWAYS_INLINE addr0() const { return (x_ >> kClkBits) & 7; }
235 u64 ALWAYS_INLINE size() const { return 1ull << size_log(); }
236 bool ALWAYS_INLINE IsWrite() const { return !IsRead(); }
237 bool ALWAYS_INLINE IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000238
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000239 // The idea behind the freed bit is as follows.
240 // When the memory is freed (or otherwise unaccessible) we write to the shadow
241 // values with tid/epoch related to the free and the freed bit set.
242 // During memory accesses processing the freed bit is considered
243 // as msb of tid. So any access races with shadow with freed bit set
244 // (it is as if write from a thread with which we never synchronized before).
245 // This allows us to detect accesses to freed memory w/o additional
246 // overheads in memory access processing and at the same time restore
247 // tid/epoch of free.
248 void MarkAsFreed() {
249 x_ |= kFreedBit;
250 }
251
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000252 bool IsFreed() const {
253 return x_ & kFreedBit;
254 }
255
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000256 bool GetFreedAndReset() {
257 bool res = x_ & kFreedBit;
258 x_ &= ~kFreedBit;
259 return res;
260 }
261
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000262 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
263 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
264 | (u64(kIsAtomic) << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000265 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
266 return v;
267 }
268
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000269 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000270 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000271 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
272 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
273 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
274 return v;
275 }
276
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000277 bool ALWAYS_INLINE IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000278 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000279 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
280 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
281 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
282 return v;
283 }
284
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000285 private:
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000286 static const u64 kReadShift = 5 + kClkBits;
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000287 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000288 static const u64 kAtomicShift = 6 + kClkBits;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000289 static const u64 kAtomicBit = 1ull << kAtomicShift;
290
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000291 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000292
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000293 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000294 if (s1.addr0() == s2.addr0()) return true;
295 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
296 return true;
297 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
298 return true;
299 return false;
300 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000301};
302
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000303struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000304
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000305struct JmpBuf {
306 uptr sp;
307 uptr mangled_sp;
Dmitry Vyukov69c4d372014-09-16 21:48:22 +0000308 int int_signal_send;
309 bool in_blocking_func;
310 uptr in_signal_handler;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000311 uptr *shadow_stack_pos;
312};
313
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000314// This struct is stored in TLS.
315struct ThreadState {
316 FastState fast_state;
317 // Synch epoch represents the threads's epoch before the last synchronization
318 // action. It allows to reduce number of shadow state updates.
319 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
320 // if we are processing write to X from the same thread at epoch=200,
321 // we do nothing, because both writes happen in the same 'synch epoch'.
322 // That is, if another memory access does not race with the former write,
323 // it does not race with the latter as well.
324 // QUESTION: can we can squeeze this into ThreadState::Fast?
325 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
326 // taken by epoch between synchs.
327 // This way we can save one load from tls.
328 u64 fast_synch_epoch;
329 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
330 // We do not distinguish beteween ignoring reads and writes
331 // for better performance.
332 int ignore_reads_and_writes;
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000333 int ignore_sync;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000334 // Go does not support ignores.
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000335#ifndef SANITIZER_GO
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000336 IgnoreSet mop_ignore_set;
337 IgnoreSet sync_ignore_set;
338#endif
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000339 // C/C++ uses fixed size shadow stack embed into Trace.
340 // Go uses malloc-allocated shadow stack with dynamic size.
341 uptr *shadow_stack;
342 uptr *shadow_stack_end;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000343 uptr *shadow_stack_pos;
344 u64 *racy_shadow_addr;
345 u64 racy_state[2];
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000346 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000347 ThreadClock clock;
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000348#ifndef SANITIZER_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000349 AllocatorCache alloc_cache;
Alexey Samsonovc30e2d62013-05-29 09:15:39 +0000350 InternalAllocatorCache internal_alloc_cache;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000351 Vector<JmpBuf> jmp_bufs;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000352 int ignore_interceptors;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000353#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000354 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000355 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000356 const int unique_id;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000357 bool in_symbolizer;
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000358 bool in_ignored_lib;
Dmitry Vyukovf8cfdd92014-09-03 12:25:22 +0000359 bool is_dead;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000360 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000361 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000362 const uptr stk_addr;
363 const uptr stk_size;
364 const uptr tls_addr;
365 const uptr tls_size;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000366 ThreadContext *tctx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000367
Kostya Serebryanya63632a2014-02-14 12:20:42 +0000368 InternalDeadlockDetector internal_deadlock_detector;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000369 DDPhysicalThread *dd_pt;
370 DDLogicalThread *dd_lt;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000371
Dmitry Vyukov18412192014-09-02 12:27:45 +0000372 atomic_uintptr_t in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000373 SignalContext *signal_ctx;
374
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000375 DenseSlabAllocCache block_cache;
376 DenseSlabAllocCache sync_cache;
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000377 DenseSlabAllocCache clock_cache;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000378
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000379#ifndef SANITIZER_GO
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000380 u32 last_sleep_stack_id;
381 ThreadClock last_sleep_clock;
382#endif
383
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000384 // Set in regions of runtime that must be signal-safe and fork-safe.
385 // If set, malloc must not be called.
386 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000387
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000388 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +0000389 unsigned reuse_count,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000390 uptr stk_addr, uptr stk_size,
391 uptr tls_addr, uptr tls_size);
392};
393
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000394#ifndef SANITIZER_GO
Kostya Serebryanye61f4d52014-05-12 10:40:33 +0000395__attribute__((tls_model("initial-exec")))
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000396extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000397INLINE ThreadState *cur_thread() {
398 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
399}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000400#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000401
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000402class ThreadContext : public ThreadContextBase {
403 public:
404 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000405 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000406 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000407 u32 creation_stack_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000408 SyncClock sync;
409 // Epoch at which the thread had started.
410 // If we see an event from the thread stamped by an older epoch,
411 // the event is from a dead thread that shared tid with this thread.
412 u64 epoch0;
413 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000414
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000415 // Override superclass callbacks.
416 void OnDead();
417 void OnJoined(void *arg);
418 void OnFinished();
419 void OnStarted(void *arg);
420 void OnCreated(void *arg);
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +0000421 void OnReset();
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000422 void OnDetached(void *arg);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000423};
424
425struct RacyStacks {
426 MD5Hash hash[2];
427 bool operator==(const RacyStacks &other) const {
428 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
429 return true;
430 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
431 return true;
432 return false;
433 }
434};
435
436struct RacyAddress {
437 uptr addr_min;
438 uptr addr_max;
439};
440
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000441struct FiredSuppression {
442 ReportType type;
443 uptr pc;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000444 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000445};
446
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000447struct Context {
448 Context();
449
450 bool initialized;
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000451 bool after_multithreaded_fork;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000452
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000453 MetaMap metamap;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000454
455 Mutex report_mtx;
456 int nreported;
457 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000458 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000459
Dmitry Vyukovb7ebc532014-04-24 13:09:17 +0000460 void *background_thread;
461 atomic_uint32_t stop_background_thread;
462
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000463 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000464
465 Vector<RacyStacks> racy_stacks;
466 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000467 // Number of fired suppressions may be large enough.
468 InternalMmapVector<FiredSuppression> fired_suppressions;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000469 DDetector *dd;
Kostya Serebryany0548c792014-02-21 15:07:18 +0000470
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000471 ClockAlloc clock_alloc;
472
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000473 Flags flags;
474
475 u64 stat[StatCnt];
476 u64 int_alloc_cnt[MBlockTypeCount];
477 u64 int_alloc_siz[MBlockTypeCount];
478};
479
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000480extern Context *ctx; // The one and the only global runtime context.
481
Dmitry Vyukovce372102013-12-24 12:55:56 +0000482struct ScopedIgnoreInterceptors {
483 ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000484#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000485 cur_thread()->ignore_interceptors++;
486#endif
487 }
488
489 ~ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000490#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000491 cur_thread()->ignore_interceptors--;
492#endif
493 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000494};
495
496class ScopedReport {
497 public:
498 explicit ScopedReport(ReportType typ);
499 ~ScopedReport();
500
Alexey Samsonov40733a82014-11-03 22:23:44 +0000501 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000502 const MutexSet *mset);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000503 void AddStack(StackTrace stack, bool suppressable = false);
Dmitry Vyukova43e98c2014-05-28 18:03:32 +0000504 void AddThread(const ThreadContext *tctx, bool suppressable = false);
505 void AddThread(int unique_tid, bool suppressable = false);
Kostya Serebryany3df5d872014-03-21 13:00:18 +0000506 void AddUniqueTid(int unique_tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000507 void AddMutex(const SyncVar *s);
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000508 u64 AddMutex(u64 id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000509 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000510 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000511 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000512
513 const ReportDesc *GetReport() const;
514
515 private:
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000516 ReportDesc *rep_;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000517 // Symbolizer makes lots of intercepted calls. If we try to process them,
518 // at best it will cause deadlocks on internal mutexes.
519 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000520
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000521 void AddDeadMutex(u64 id);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000522
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000523 ScopedReport(const ScopedReport&);
524 void operator = (const ScopedReport&);
525};
526
Alexey Samsonov40733a82014-11-03 22:23:44 +0000527void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
528 MutexSet *mset);
529
530template<typename StackTraceTy>
531void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
532 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
533 uptr start = 0;
534 if (size + !!toppc > kStackTraceMax) {
535 start = size + !!toppc - kStackTraceMax;
536 size = kStackTraceMax - !!toppc;
537 }
538 stack->Init(&thr->shadow_stack[start], size, toppc);
539}
540
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000541
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000542void StatAggregate(u64 *dst, u64 *src);
543void StatOutput(u64 *stat);
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000544void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000545 if (kCollectStats)
546 thr->stat[typ] += n;
547}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000548void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000549 if (kCollectStats)
550 thr->stat[typ] = n;
551}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000552
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000553void MapShadow(uptr addr, uptr size);
Dmitry Vyukov3e7ede22012-12-13 08:14:02 +0000554void MapThreadTrace(uptr addr, uptr size);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000555void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000556void InitializeShadowMemory();
557void InitializeInterceptors();
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000558void InitializeLibIgnore();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000559void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000560
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000561void ForkBefore(ThreadState *thr, uptr pc);
562void ForkParentAfter(ThreadState *thr, uptr pc);
563void ForkChildAfter(ThreadState *thr, uptr pc);
564
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000565void ReportRace(ThreadState *thr);
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000566bool OutputReport(ThreadState *thr, const ScopedReport &srep);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000567bool IsFiredSuppression(Context *ctx, const ScopedReport &srep,
568 StackTrace trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000569bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000570void PrintMatchedBenignRaces();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000571
572#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000573# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000574#else
575# define DPrintf(...)
576#endif
577
578#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000579# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000580#else
581# define DPrintf2(...)
582#endif
583
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000584u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000585ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000586void PrintCurrentStack(ThreadState *thr, uptr pc);
Alexey Samsonovfbaaed62014-11-06 18:43:45 +0000587void PrintCurrentStackSlow(uptr pc); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000588
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000589void Initialize(ThreadState *thr);
590int Finalize(ThreadState *thr);
591
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000592void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
593void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000594
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000595void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000596 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000597void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000598 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000599 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000600void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000601 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000602void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
603 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000604void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
605 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000606
607const int kSizeLog1 = 0;
608const int kSizeLog2 = 1;
609const int kSizeLog4 = 2;
610const int kSizeLog8 = 3;
611
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000612void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000613 uptr addr, int kAccessSizeLog) {
614 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
615}
616
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000617void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000618 uptr addr, int kAccessSizeLog) {
619 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
620}
621
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000622void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000623 uptr addr, int kAccessSizeLog) {
624 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
625}
626
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000627void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000628 uptr addr, int kAccessSizeLog) {
629 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
630}
631
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000632void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
633void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000634void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000635
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000636void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
637void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
638void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
639void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000640
641void FuncEntry(ThreadState *thr, uptr pc);
642void FuncExit(ThreadState *thr);
643
644int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000645void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000646void ThreadFinish(ThreadState *thr);
647int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
648void ThreadJoin(ThreadState *thr, uptr pc, int tid);
649void ThreadDetach(ThreadState *thr, uptr pc, int tid);
650void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000651void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000652int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000653void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000654
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000655void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
656 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000657void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany11f4f302014-02-25 08:24:15 +0000658void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
659 bool try_lock = false);
Dmitry Vyukovc9af8182013-05-17 12:03:46 +0000660int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Kostya Serebryany01be2962014-02-25 10:33:37 +0000661void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000662void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
663void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov4bbe6dc2013-11-15 16:58:12 +0000664void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000665
666void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukovbd167972014-11-18 06:44:43 +0000667// AcquireGlobal synchronizes the current thread with all other threads.
668// In terms of happens-before relation, it draws a HB edge from all threads
669// (where they happen to execute right now) to the current thread. We use it to
670// handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
671// right before executing finalizers. This provides a coarse, but simple
672// approximation of the actual required synchronization.
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000673void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000674void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000675void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000676void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000677void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
678void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
679void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
680void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000681
682// The hacky call uses custom calling convention and an assembly thunk.
683// It is considerably faster that a normal call for the caller
684// if it is not executed (it is intended for slow paths from hot functions).
685// The trick is that the call preserves all registers and the compiler
686// does not treat it as a call.
687// If it does not work for you, use normal call.
Dmitry Vyukov48846ba2015-01-22 14:13:56 +0000688#if !SANITIZER_DEBUG && defined(__x86_64__)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000689// The caller may not create the stack frame for itself at all,
690// so we create a reserve stack frame for it (1024b must be enough).
691#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000692 __asm__ __volatile__("sub $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000693 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000694 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000695 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000696 "add $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000697 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000698 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000699#else
700#define HACKY_CALL(f) f()
701#endif
702
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000703void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000704uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000705uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000706uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000707Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000708
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000709extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000710void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000711 EventType typ, u64 addr) {
Dmitry Vyukov547089e2014-05-15 12:51:48 +0000712 if (!kCollectHistory)
713 return;
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000714 DCHECK_GE((int)typ, 0);
715 DCHECK_LE((int)typ, 7);
716 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000717 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000718 u64 pos = fs.GetTracePos();
719 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000720#ifndef SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000721 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000722#else
723 TraceSwitch(thr);
724#endif
725 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000726 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000727 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000728 Event ev = (u64)addr | ((u64)typ << 61);
729 *evp = ev;
730}
731
732} // namespace __tsan
733
734#endif // TSAN_RTL_H