blob: 88f98e2b264319264b11cb4ca3b70d43e02bf940 [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
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000354#ifdef TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000355 u64 stat[StatCnt];
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000356#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000357 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000358 const int unique_id;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000359 bool in_symbolizer;
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000360 bool in_ignored_lib;
Dmitry Vyukovf8cfdd92014-09-03 12:25:22 +0000361 bool is_dead;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000362 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000363 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364 const uptr stk_addr;
365 const uptr stk_size;
366 const uptr tls_addr;
367 const uptr tls_size;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000368 ThreadContext *tctx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000369
Kostya Serebryanya63632a2014-02-14 12:20:42 +0000370 InternalDeadlockDetector internal_deadlock_detector;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000371 DDPhysicalThread *dd_pt;
372 DDLogicalThread *dd_lt;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000373
Dmitry Vyukov18412192014-09-02 12:27:45 +0000374 atomic_uintptr_t in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000375 SignalContext *signal_ctx;
376
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000377 DenseSlabAllocCache block_cache;
378 DenseSlabAllocCache sync_cache;
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000379 DenseSlabAllocCache clock_cache;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000380
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000381#ifndef SANITIZER_GO
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000382 u32 last_sleep_stack_id;
383 ThreadClock last_sleep_clock;
384#endif
385
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000386 // Set in regions of runtime that must be signal-safe and fork-safe.
387 // If set, malloc must not be called.
388 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000389
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000390 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +0000391 unsigned reuse_count,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000392 uptr stk_addr, uptr stk_size,
393 uptr tls_addr, uptr tls_size);
394};
395
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000396#ifndef SANITIZER_GO
Kostya Serebryanye61f4d52014-05-12 10:40:33 +0000397__attribute__((tls_model("initial-exec")))
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000398extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000399INLINE ThreadState *cur_thread() {
400 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
401}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000402#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000403
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000404class ThreadContext : public ThreadContextBase {
405 public:
406 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000407 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000408 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000409 u32 creation_stack_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000410 SyncClock sync;
411 // Epoch at which the thread had started.
412 // If we see an event from the thread stamped by an older epoch,
413 // the event is from a dead thread that shared tid with this thread.
414 u64 epoch0;
415 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000416
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000417 // Override superclass callbacks.
418 void OnDead();
419 void OnJoined(void *arg);
420 void OnFinished();
421 void OnStarted(void *arg);
422 void OnCreated(void *arg);
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +0000423 void OnReset();
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000424 void OnDetached(void *arg);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000425};
426
427struct RacyStacks {
428 MD5Hash hash[2];
429 bool operator==(const RacyStacks &other) const {
430 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
431 return true;
432 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
433 return true;
434 return false;
435 }
436};
437
438struct RacyAddress {
439 uptr addr_min;
440 uptr addr_max;
441};
442
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000443struct FiredSuppression {
444 ReportType type;
445 uptr pc;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000446 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000447};
448
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000449struct Context {
450 Context();
451
452 bool initialized;
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000453 bool after_multithreaded_fork;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000454
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000455 MetaMap metamap;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000456
457 Mutex report_mtx;
458 int nreported;
459 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000460 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000461
Dmitry Vyukovb7ebc532014-04-24 13:09:17 +0000462 void *background_thread;
463 atomic_uint32_t stop_background_thread;
464
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000465 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000466
467 Vector<RacyStacks> racy_stacks;
468 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000469 // Number of fired suppressions may be large enough.
470 InternalMmapVector<FiredSuppression> fired_suppressions;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000471 DDetector *dd;
Kostya Serebryany0548c792014-02-21 15:07:18 +0000472
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000473 ClockAlloc clock_alloc;
474
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000475 Flags flags;
476
477 u64 stat[StatCnt];
478 u64 int_alloc_cnt[MBlockTypeCount];
479 u64 int_alloc_siz[MBlockTypeCount];
480};
481
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000482extern Context *ctx; // The one and the only global runtime context.
483
Dmitry Vyukovce372102013-12-24 12:55:56 +0000484struct ScopedIgnoreInterceptors {
485 ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000486#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000487 cur_thread()->ignore_interceptors++;
488#endif
489 }
490
491 ~ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000492#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000493 cur_thread()->ignore_interceptors--;
494#endif
495 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000496};
497
498class ScopedReport {
499 public:
500 explicit ScopedReport(ReportType typ);
501 ~ScopedReport();
502
Alexey Samsonov40733a82014-11-03 22:23:44 +0000503 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000504 const MutexSet *mset);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000505 void AddStack(StackTrace stack, bool suppressable = false);
Dmitry Vyukova43e98c2014-05-28 18:03:32 +0000506 void AddThread(const ThreadContext *tctx, bool suppressable = false);
507 void AddThread(int unique_tid, bool suppressable = false);
Kostya Serebryany3df5d872014-03-21 13:00:18 +0000508 void AddUniqueTid(int unique_tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000509 void AddMutex(const SyncVar *s);
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000510 u64 AddMutex(u64 id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000511 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000512 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000513 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000514
515 const ReportDesc *GetReport() const;
516
517 private:
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000518 ReportDesc *rep_;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000519 // Symbolizer makes lots of intercepted calls. If we try to process them,
520 // at best it will cause deadlocks on internal mutexes.
521 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000522
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000523 void AddDeadMutex(u64 id);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000524
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000525 ScopedReport(const ScopedReport&);
526 void operator = (const ScopedReport&);
527};
528
Alexey Samsonov40733a82014-11-03 22:23:44 +0000529void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
530 MutexSet *mset);
531
532template<typename StackTraceTy>
533void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
534 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
535 uptr start = 0;
536 if (size + !!toppc > kStackTraceMax) {
537 start = size + !!toppc - kStackTraceMax;
538 size = kStackTraceMax - !!toppc;
539 }
540 stack->Init(&thr->shadow_stack[start], size, toppc);
541}
542
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000543
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000544#ifdef TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000545void StatAggregate(u64 *dst, u64 *src);
546void StatOutput(u64 *stat);
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000547#endif
548
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000549void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000550#ifdef TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000551 if (kCollectStats)
552 thr->stat[typ] += n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000553#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000554}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000555void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000556#ifdef TSAN_COLLECT_STATS
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000557 if (kCollectStats)
558 thr->stat[typ] = n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000559#endif
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000560}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000561
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000562void MapShadow(uptr addr, uptr size);
Dmitry Vyukov3e7ede22012-12-13 08:14:02 +0000563void MapThreadTrace(uptr addr, uptr size);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000564void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000565void InitializeShadowMemory();
566void InitializeInterceptors();
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000567void InitializeLibIgnore();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000568void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000569
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000570void ForkBefore(ThreadState *thr, uptr pc);
571void ForkParentAfter(ThreadState *thr, uptr pc);
572void ForkChildAfter(ThreadState *thr, uptr pc);
573
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000574void ReportRace(ThreadState *thr);
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000575bool OutputReport(ThreadState *thr, const ScopedReport &srep);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000576bool IsFiredSuppression(Context *ctx, const ScopedReport &srep,
577 StackTrace trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000578bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000579void PrintMatchedBenignRaces();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000580
581#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000582# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000583#else
584# define DPrintf(...)
585#endif
586
587#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000588# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000589#else
590# define DPrintf2(...)
591#endif
592
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000593u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000594ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000595void PrintCurrentStack(ThreadState *thr, uptr pc);
Alexey Samsonovfbaaed62014-11-06 18:43:45 +0000596void PrintCurrentStackSlow(uptr pc); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000597
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000598void Initialize(ThreadState *thr);
599int Finalize(ThreadState *thr);
600
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000601void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
602void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000603
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000604void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000605 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000606void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000607 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000608 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000609void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000610 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000611void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
612 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000613void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
614 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000615
616const int kSizeLog1 = 0;
617const int kSizeLog2 = 1;
618const int kSizeLog4 = 2;
619const int kSizeLog8 = 3;
620
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000621void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000622 uptr addr, int kAccessSizeLog) {
623 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
624}
625
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000626void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000627 uptr addr, int kAccessSizeLog) {
628 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
629}
630
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000631void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000632 uptr addr, int kAccessSizeLog) {
633 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
634}
635
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000636void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000637 uptr addr, int kAccessSizeLog) {
638 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
639}
640
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000641void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
642void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000643void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000644
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000645void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
646void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
647void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
648void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000649
650void FuncEntry(ThreadState *thr, uptr pc);
651void FuncExit(ThreadState *thr);
652
653int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000654void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000655void ThreadFinish(ThreadState *thr);
656int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
657void ThreadJoin(ThreadState *thr, uptr pc, int tid);
658void ThreadDetach(ThreadState *thr, uptr pc, int tid);
659void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000660void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000661int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000662void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000663
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000664void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
665 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000666void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany11f4f302014-02-25 08:24:15 +0000667void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
668 bool try_lock = false);
Dmitry Vyukovc9af8182013-05-17 12:03:46 +0000669int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Kostya Serebryany01be2962014-02-25 10:33:37 +0000670void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000671void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
672void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov4bbe6dc2013-11-15 16:58:12 +0000673void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000674
675void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukovbd167972014-11-18 06:44:43 +0000676// AcquireGlobal synchronizes the current thread with all other threads.
677// In terms of happens-before relation, it draws a HB edge from all threads
678// (where they happen to execute right now) to the current thread. We use it to
679// handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
680// right before executing finalizers. This provides a coarse, but simple
681// approximation of the actual required synchronization.
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000682void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000683void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000684void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000685void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000686void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
687void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
688void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
689void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000690
691// The hacky call uses custom calling convention and an assembly thunk.
692// It is considerably faster that a normal call for the caller
693// if it is not executed (it is intended for slow paths from hot functions).
694// The trick is that the call preserves all registers and the compiler
695// does not treat it as a call.
696// If it does not work for you, use normal call.
Dmitry Vyukov48846ba2015-01-22 14:13:56 +0000697#if !SANITIZER_DEBUG && defined(__x86_64__)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000698// The caller may not create the stack frame for itself at all,
699// so we create a reserve stack frame for it (1024b must be enough).
700#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000701 __asm__ __volatile__("sub $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000702 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000703 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000704 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000705 "add $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000706 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000707 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000708#else
709#define HACKY_CALL(f) f()
710#endif
711
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000712void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000713uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000714uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000715uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000716Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000717
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000718extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000719void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000720 EventType typ, u64 addr) {
Dmitry Vyukov547089e2014-05-15 12:51:48 +0000721 if (!kCollectHistory)
722 return;
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000723 DCHECK_GE((int)typ, 0);
724 DCHECK_LE((int)typ, 7);
725 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000726 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000727 u64 pos = fs.GetTracePos();
728 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000729#ifndef SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000730 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000731#else
732 TraceSwitch(thr);
733#endif
734 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000735 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000736 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000737 Event ev = (u64)addr | ((u64)typ << 61);
738 *evp = ev;
739}
740
741} // namespace __tsan
742
743#endif // TSAN_RTL_H