blob: a13e4b6379f015a3a69363e7a05f0d60ab8f5393 [file] [log] [blame]
Kostya Serebryany7ac41482012-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 Serebryany72166ca2012-12-05 10:09:15 +000029#include "sanitizer_common/sanitizer_allocator.h"
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +000030#include "sanitizer_common/sanitizer_allocator_internal.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070031#include "sanitizer_common/sanitizer_asm.h"
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000032#include "sanitizer_common/sanitizer_common.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070033#include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
Dmitry Vyukov4af0f212013-10-03 13:37:17 +000034#include "sanitizer_common/sanitizer_libignore.h"
Sergey Matveeva52e5c62013-06-26 15:37:14 +000035#include "sanitizer_common/sanitizer_suppressions.h"
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000036#include "sanitizer_common/sanitizer_thread_registry.h"
Kostya Serebryany7ac41482012-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 Vyukov385542a2012-11-28 10:35:31 +000044#include "tsan_platform.h"
Dmitry Vyukovad9da372012-12-06 12:16:15 +000045#include "tsan_mutexset.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070046#include "tsan_ignoreset.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070047#include "tsan_stack_trace.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000048
Kostya Serebryany503a3af2012-12-04 15:13:30 +000049#if SANITIZER_WORDSIZE != 64
50# error "ThreadSanitizer is supported only on 64-bit platforms"
51#endif
52
Kostya Serebryany7ac41482012-05-10 13:48:04 +000053namespace __tsan {
54
Stephen Hines86277eb2015-03-23 12:06:32 -070055#ifndef SANITIZER_GO
Dmitry Vyukove93e5052013-03-18 10:32:21 +000056struct MapUnmapCallback;
Stephen Hines86277eb2015-03-23 12:06:32 -070057#ifdef __mips64
58static 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
Stephen Hines6d186232014-11-26 17:56:19 -080069typedef SizeClassAllocator64<kHeapMemBeg, kHeapMemEnd - kHeapMemBeg, 0,
Dmitry Vyukove93e5052013-03-18 10:32:21 +000070 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
Stephen Hines86277eb2015-03-23 12:06:32 -070071#endif
Kostya Serebryany82de9422012-12-04 14:15:17 +000072typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukove93e5052013-03-18 10:32:21 +000073typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov2e870512012-08-15 15:35:15 +000074typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
75 SecondaryAllocator> Allocator;
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +000076Allocator *allocator();
Dmitry Vyukov2e870512012-08-15 15:35:15 +000077#endif
78
Alexey Samsonov591616d2012-09-11 09:44:48 +000079void TsanCheckFailed(const char *file, int line, const char *cond,
80 u64 v1, u64 v2);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000081
Dmitry Vyukov9743d742013-03-20 10:31:53 +000082const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
83
Kostya Serebryany7ac41482012-05-10 13:48:04 +000084// FastState (from most significant bit):
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +000085// ignore : 1
Kostya Serebryany7ac41482012-05-10 13:48:04 +000086// tid : kTidBits
Dmitry Vyukov069ce822012-05-17 14:17:51 +000087// unused : -
Dmitry Vyukovd698edc2012-11-28 12:19:50 +000088// history_size : 3
Stephen Hines6a211c52014-07-21 00:49:56 -070089// epoch : kClkBits
Kostya Serebryany7ac41482012-05-10 13:48:04 +000090class FastState {
91 public:
92 FastState(u64 tid, u64 epoch) {
Dmitry Vyukov069ce822012-05-17 14:17:51 +000093 x_ = tid << kTidShift;
Stephen Hines6a211c52014-07-21 00:49:56 -070094 x_ |= epoch;
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +000095 DCHECK_EQ(tid, this->tid());
96 DCHECK_EQ(epoch, this->epoch());
97 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000098 }
99
100 explicit FastState(u64 x)
101 : x_(x) {
102 }
103
Dmitry Vyukov332c62b2012-08-16 15:08:49 +0000104 u64 raw() const {
105 return x_;
106 }
107
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000108 u64 tid() const {
Dmitry Vyukovc8f0a002012-11-30 20:02:11 +0000109 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
110 return res;
111 }
112
113 u64 TidWithIgnore() const {
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000114 u64 res = x_ >> kTidShift;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000115 return res;
116 }
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000117
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000118 u64 epoch() const {
Stephen Hines6a211c52014-07-21 00:49:56 -0700119 u64 res = x_ & ((1ull << kClkBits) - 1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000120 return res;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000121 }
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000122
123 void IncrementEpoch() {
124 u64 old_epoch = epoch();
Stephen Hines6a211c52014-07-21 00:49:56 -0700125 x_ += 1;
Dmitry Vyukove784ad42012-05-21 10:20:53 +0000126 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000127 (void)old_epoch;
128 }
129
130 void SetIgnoreBit() { x_ |= kIgnoreBit; }
131 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +0000132 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000133
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000134 void SetHistorySize(int hs) {
135 CHECK_GE(hs, 0);
136 CHECK_LE(hs, 7);
Stephen Hines6a211c52014-07-21 00:49:56 -0700137 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000138 }
139
Stephen Hines6a211c52014-07-21 00:49:56 -0700140 ALWAYS_INLINE
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000141 int GetHistorySize() const {
Stephen Hines6a211c52014-07-21 00:49:56 -0700142 return (int)((x_ >> kHistoryShift) & kHistoryMask);
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000143 }
144
145 void ClearHistorySize() {
Stephen Hines6a211c52014-07-21 00:49:56 -0700146 SetHistorySize(0);
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000147 }
148
Stephen Hines6a211c52014-07-21 00:49:56 -0700149 ALWAYS_INLINE
Dmitry Vyukovd698edc2012-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 Serebryany7ac41482012-05-10 13:48:04 +0000157 private:
158 friend class Shadow;
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000159 static const int kTidShift = 64 - kTidBits - 1;
Dmitry Vyukov0d35d9d2012-11-28 10:49:27 +0000160 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000161 static const u64 kFreedBit = 1ull << 63;
Stephen Hines6a211c52014-07-21 00:49:56 -0700162 static const u64 kHistoryShift = kClkBits;
163 static const u64 kHistoryMask = 7;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000164 u64 x_;
165};
166
167// Shadow (from most significant bit):
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000168// freed : 1
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000169// tid : kTidBits
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000170// is_atomic : 1
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000171// is_read : 1
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000172// size_log : 2
173// addr0 : 3
Stephen Hines6a211c52014-07-21 00:49:56 -0700174// epoch : kClkBits
Dmitry Vyukove9636662012-06-27 16:05:06 +0000175class Shadow : public FastState {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000176 public:
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000177 explicit Shadow(u64 x)
178 : FastState(x) {
179 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000180
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000181 explicit Shadow(const FastState &s)
182 : FastState(s.x_) {
183 ClearHistorySize();
184 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000185
186 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
Stephen Hines6a211c52014-07-21 00:49:56 -0700187 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000188 DCHECK_LE(addr0, 7);
189 DCHECK_LE(kAccessSizeLog, 3);
Stephen Hines6a211c52014-07-21 00:49:56 -0700190 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
Kostya Serebryany7ac41482012-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 Vyukov33a040a2013-02-01 10:02:55 +0000196 DCHECK_EQ(x_ & kReadBit, 0);
197 if (!kAccessIsWrite)
198 x_ |= kReadBit;
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000199 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000200 }
201
Dmitry Vyukov334553e2013-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 Serebryany7ac41482012-05-10 13:48:04 +0000216
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000217 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000218 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukovc8f0a002012-11-30 20:02:11 +0000219 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000220 return shifted_xor == 0;
221 }
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000222
Stephen Hines6a211c52014-07-21 00:49:56 -0700223 static ALWAYS_INLINE
224 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
225 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000226 return masked_xor == 0;
227 }
228
Stephen Hines6a211c52014-07-21 00:49:56 -0700229 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
Kostya Serebryany7ac41482012-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;
Stephen Hines6a211c52014-07-21 00:49:56 -0700235 if (s1.size() > -diff)
236 res = true;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000237 } else {
238 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
Stephen Hines6a211c52014-07-21 00:49:56 -0700239 if (kS2AccessSize > diff)
240 res = true;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000241 }
Stephen Hines6a211c52014-07-21 00:49:56 -0700242 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
243 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000244 return res;
245 }
246
Stephen Hines6a211c52014-07-21 00:49:56 -0700247 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 Serebryany7ac41482012-05-10 13:48:04 +0000251
Dmitry Vyukov069ce822012-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 Vyukov32858662013-02-01 14:41:58 +0000265 bool IsFreed() const {
266 return x_ & kFreedBit;
267 }
268
Dmitry Vyukov069ce822012-05-17 14:17:51 +0000269 bool GetFreedAndReset() {
270 bool res = x_ & kFreedBit;
271 x_ &= ~kFreedBit;
272 return res;
273 }
274
Stephen Hines6a211c52014-07-21 00:49:56 -0700275 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
276 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
277 | (u64(kIsAtomic) << kAtomicShift));
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000278 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
279 return v;
280 }
281
Stephen Hines6a211c52014-07-21 00:49:56 -0700282 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000283 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukov334553e2013-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
Stephen Hines6a211c52014-07-21 00:49:56 -0700290 bool ALWAYS_INLINE IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000291 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukov334553e2013-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 Serebryany7ac41482012-05-10 13:48:04 +0000298 private:
Stephen Hines6a211c52014-07-21 00:49:56 -0700299 static const u64 kReadShift = 5 + kClkBits;
Dmitry Vyukov33a040a2013-02-01 10:02:55 +0000300 static const u64 kReadBit = 1ull << kReadShift;
Stephen Hines6a211c52014-07-21 00:49:56 -0700301 static const u64 kAtomicShift = 6 + kClkBits;
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000302 static const u64 kAtomicBit = 1ull << kAtomicShift;
303
Stephen Hines6a211c52014-07-21 00:49:56 -0700304 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000305
Stephen Hines6a211c52014-07-21 00:49:56 -0700306 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
Dmitry Vyukovadfb6502012-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 Serebryany7ac41482012-05-10 13:48:04 +0000314};
315
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700316struct ThreadSignalContext;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000317
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000318struct JmpBuf {
319 uptr sp;
320 uptr mangled_sp;
Stephen Hines6d186232014-11-26 17:56:19 -0800321 int int_signal_send;
322 bool in_blocking_func;
323 uptr in_signal_handler;
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000324 uptr *shadow_stack_pos;
325};
326
Kostya Serebryany7ac41482012-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 Vyukove1ddbf92013-10-10 15:58:12 +0000346 int ignore_sync;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700347 // Go does not support ignores.
Stephen Hines86277eb2015-03-23 12:06:32 -0700348#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700349 IgnoreSet mop_ignore_set;
350 IgnoreSet sync_ignore_set;
351#endif
Dmitry Vyukov01a7ce82013-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 Serebryany7ac41482012-05-10 13:48:04 +0000356 uptr *shadow_stack_pos;
357 u64 *racy_shadow_addr;
358 u64 racy_state[2];
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000359 MutexSet mset;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000360 ThreadClock clock;
Stephen Hines86277eb2015-03-23 12:06:32 -0700361#ifndef SANITIZER_GO
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000362 AllocatorCache alloc_cache;
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +0000363 InternalAllocatorCache internal_alloc_cache;
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000364 Vector<JmpBuf> jmp_bufs;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700365 int ignore_interceptors;
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000366#endif
Stephen Hines86277eb2015-03-23 12:06:32 -0700367#if TSAN_COLLECT_STATS
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000368 u64 stat[StatCnt];
Stephen Hines86277eb2015-03-23 12:06:32 -0700369#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000370 const int tid;
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +0000371 const int unique_id;
Dmitry Vyukov4e81d0e2013-01-29 13:03:07 +0000372 bool in_symbolizer;
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000373 bool in_ignored_lib;
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700374 bool is_inited;
Stephen Hines6d186232014-11-26 17:56:19 -0800375 bool is_dead;
Dmitry Vyukov32858662013-02-01 14:41:58 +0000376 bool is_freeing;
Dmitry Vyukov0dc47b62013-03-21 15:37:39 +0000377 bool is_vptr_access;
Kostya Serebryany7ac41482012-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;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700382 ThreadContext *tctx;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000383
Stephen Hines86277eb2015-03-23 12:06:32 -0700384#if SANITIZER_DEBUG && !SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700385 InternalDeadlockDetector internal_deadlock_detector;
Stephen Hines86277eb2015-03-23 12:06:32 -0700386#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700387 DDPhysicalThread *dd_pt;
388 DDLogicalThread *dd_lt;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000389
Stephen Hines6d186232014-11-26 17:56:19 -0800390 atomic_uintptr_t in_signal_handler;
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700391 ThreadSignalContext *signal_ctx;
Dmitry Vyukove9636662012-06-27 16:05:06 +0000392
Stephen Hines6a211c52014-07-21 00:49:56 -0700393 DenseSlabAllocCache block_cache;
394 DenseSlabAllocCache sync_cache;
Stephen Hines6d186232014-11-26 17:56:19 -0800395 DenseSlabAllocCache clock_cache;
Stephen Hines6a211c52014-07-21 00:49:56 -0700396
Stephen Hines86277eb2015-03-23 12:06:32 -0700397#ifndef SANITIZER_GO
Dmitry Vyukov84853112012-08-31 17:27:49 +0000398 u32 last_sleep_stack_id;
399 ThreadClock last_sleep_clock;
400#endif
401
Dmitry Vyukov9ad7c322012-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 Serebryany7ac41482012-05-10 13:48:04 +0000405
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +0000406 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700407 unsigned reuse_count,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000408 uptr stk_addr, uptr stk_size,
409 uptr tls_addr, uptr tls_size);
410};
411
Stephen Hines86277eb2015-03-23 12:06:32 -0700412#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700413__attribute__((tls_model("initial-exec")))
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000414extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000415INLINE ThreadState *cur_thread() {
416 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
417}
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000418#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000419
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000420class ThreadContext : public ThreadContextBase {
421 public:
422 explicit ThreadContext(int tid);
Dmitry Vyukov6af642e2013-03-18 10:10:15 +0000423 ~ThreadContext();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000424 ThreadState *thr;
Dmitry Vyukov2c5284e2013-03-18 09:02:27 +0000425 u32 creation_stack_id;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000426 SyncClock sync;
427 // Epoch at which the thread had started.
428 // If we see an event from the thread stamped by an older epoch,
429 // the event is from a dead thread that shared tid with this thread.
430 u64 epoch0;
431 u64 epoch1;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000432
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000433 // Override superclass callbacks.
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700434 void OnDead() override;
435 void OnJoined(void *arg) override;
436 void OnFinished() override;
437 void OnStarted(void *arg) override;
438 void OnCreated(void *arg) override;
439 void OnReset() override;
440 void OnDetached(void *arg) override;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000441};
442
443struct RacyStacks {
444 MD5Hash hash[2];
445 bool operator==(const RacyStacks &other) const {
446 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
447 return true;
448 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
449 return true;
450 return false;
451 }
452};
453
454struct RacyAddress {
455 uptr addr_min;
456 uptr addr_max;
457};
458
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +0000459struct FiredSuppression {
460 ReportType type;
461 uptr pc;
Dmitry Vyukovf754eb52013-03-27 17:59:57 +0000462 Suppression *supp;
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +0000463};
464
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000465struct Context {
466 Context();
467
468 bool initialized;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700469 bool after_multithreaded_fork;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000470
Stephen Hines6a211c52014-07-21 00:49:56 -0700471 MetaMap metamap;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000472
473 Mutex report_mtx;
474 int nreported;
475 int nmissed_expected;
Dmitry Vyukova38e40f2013-03-21 07:02:36 +0000476 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000477
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700478 void *background_thread;
479 atomic_uint32_t stop_background_thread;
480
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000481 ThreadRegistry *thread_registry;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000482
483 Vector<RacyStacks> racy_stacks;
484 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0a05e5f2013-06-14 11:18:58 +0000485 // Number of fired suppressions may be large enough.
486 InternalMmapVector<FiredSuppression> fired_suppressions;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700487 DDetector *dd;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000488
Stephen Hines6d186232014-11-26 17:56:19 -0800489 ClockAlloc clock_alloc;
490
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000491 Flags flags;
492
493 u64 stat[StatCnt];
494 u64 int_alloc_cnt[MBlockTypeCount];
495 u64 int_alloc_siz[MBlockTypeCount];
496};
497
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700498extern Context *ctx; // The one and the only global runtime context.
499
500struct ScopedIgnoreInterceptors {
501 ScopedIgnoreInterceptors() {
Stephen Hines86277eb2015-03-23 12:06:32 -0700502#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700503 cur_thread()->ignore_interceptors++;
504#endif
505 }
506
507 ~ScopedIgnoreInterceptors() {
Stephen Hines86277eb2015-03-23 12:06:32 -0700508#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700509 cur_thread()->ignore_interceptors--;
510#endif
511 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000512};
513
514class ScopedReport {
515 public:
516 explicit ScopedReport(ReportType typ);
517 ~ScopedReport();
518
Stephen Hines6d186232014-11-26 17:56:19 -0800519 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000520 const MutexSet *mset);
Stephen Hines6d186232014-11-26 17:56:19 -0800521 void AddStack(StackTrace stack, bool suppressable = false);
Stephen Hines6a211c52014-07-21 00:49:56 -0700522 void AddThread(const ThreadContext *tctx, bool suppressable = false);
523 void AddThread(int unique_tid, bool suppressable = false);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700524 void AddUniqueTid(int unique_tid);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000525 void AddMutex(const SyncVar *s);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700526 u64 AddMutex(u64 id);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000527 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000528 void AddSleep(u32 stack_id);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000529 void SetCount(int count);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000530
531 const ReportDesc *GetReport() const;
532
533 private:
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000534 ReportDesc *rep_;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700535 // Symbolizer makes lots of intercepted calls. If we try to process them,
536 // at best it will cause deadlocks on internal mutexes.
537 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000538
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700539 void AddDeadMutex(u64 id);
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000540
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000541 ScopedReport(const ScopedReport&);
542 void operator = (const ScopedReport&);
543};
544
Stephen Hines6d186232014-11-26 17:56:19 -0800545void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
546 MutexSet *mset);
547
548template<typename StackTraceTy>
549void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
550 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
551 uptr start = 0;
552 if (size + !!toppc > kStackTraceMax) {
553 start = size + !!toppc - kStackTraceMax;
554 size = kStackTraceMax - !!toppc;
555 }
556 stack->Init(&thr->shadow_stack[start], size, toppc);
557}
558
Dmitry Vyukov332c62b2012-08-16 15:08:49 +0000559
Stephen Hines86277eb2015-03-23 12:06:32 -0700560#if TSAN_COLLECT_STATS
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000561void StatAggregate(u64 *dst, u64 *src);
562void StatOutput(u64 *stat);
Stephen Hines86277eb2015-03-23 12:06:32 -0700563#endif
564
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000565void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700566#if TSAN_COLLECT_STATS
567 thr->stat[typ] += n;
568#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000569}
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000570void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700571#if TSAN_COLLECT_STATS
572 thr->stat[typ] = n;
573#endif
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000574}
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000575
Dmitry Vyukova05fcc12012-11-06 16:00:16 +0000576void MapShadow(uptr addr, uptr size);
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700577void MapThreadTrace(uptr addr, uptr size, const char *name);
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +0000578void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000579void InitializeShadowMemory();
580void InitializeInterceptors();
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000581void InitializeLibIgnore();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000582void InitializeDynamicAnnotations();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000583
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700584void ForkBefore(ThreadState *thr, uptr pc);
585void ForkParentAfter(ThreadState *thr, uptr pc);
586void ForkChildAfter(ThreadState *thr, uptr pc);
587
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000588void ReportRace(ThreadState *thr);
Stephen Hines6a211c52014-07-21 00:49:56 -0700589bool OutputReport(ThreadState *thr, const ScopedReport &srep);
Stephen Hines6d186232014-11-26 17:56:19 -0800590bool IsFiredSuppression(Context *ctx, const ScopedReport &srep,
591 StackTrace trace);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000592bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukov0fd908c2013-03-28 16:21:19 +0000593void PrintMatchedBenignRaces();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000594
595#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000596# define DPrintf Printf
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000597#else
598# define DPrintf(...)
599#endif
600
601#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000602# define DPrintf2 Printf
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000603#else
604# define DPrintf2(...)
605#endif
606
Dmitry Vyukov84853112012-08-31 17:27:49 +0000607u32 CurrentStackId(ThreadState *thr, uptr pc);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700608ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov1da10562012-09-01 12:13:18 +0000609void PrintCurrentStack(ThreadState *thr, uptr pc);
Stephen Hines6d186232014-11-26 17:56:19 -0800610void PrintCurrentStackSlow(uptr pc); // uses libunwind
Dmitry Vyukov84853112012-08-31 17:27:49 +0000611
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000612void Initialize(ThreadState *thr);
613int Finalize(ThreadState *thr);
614
Stephen Hines6a211c52014-07-21 00:49:56 -0700615void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
616void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
Dmitry Vyukov21cc85d2012-12-20 17:29:34 +0000617
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000618void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000619 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000620void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000621 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000622 u64 *shadow_mem, Shadow cur);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000623void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000624 uptr size, bool is_write);
Dmitry Vyukoveaa01902013-02-13 13:05:36 +0000625void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
626 uptr size, uptr step, bool is_write);
Dmitry Vyukov8ecd0e52013-04-30 11:56:56 +0000627void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
628 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000629
630const int kSizeLog1 = 0;
631const int kSizeLog2 = 1;
632const int kSizeLog4 = 2;
633const int kSizeLog8 = 3;
634
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000635void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000636 uptr addr, int kAccessSizeLog) {
637 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
638}
639
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000640void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000641 uptr addr, int kAccessSizeLog) {
642 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
643}
644
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000645void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000646 uptr addr, int kAccessSizeLog) {
647 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
648}
649
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000650void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000651 uptr addr, int kAccessSizeLog) {
652 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
653}
654
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000655void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
656void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov26af8932012-08-15 16:52:19 +0000657void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000658
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700659void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
660void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
661void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
662void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000663
664void FuncEntry(ThreadState *thr, uptr pc);
665void FuncExit(ThreadState *thr);
666
667int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukove0023f72012-10-02 12:58:14 +0000668void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000669void ThreadFinish(ThreadState *thr);
670int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
671void ThreadJoin(ThreadState *thr, uptr pc, int tid);
672void ThreadDetach(ThreadState *thr, uptr pc, int tid);
673void ThreadFinalize(ThreadState *thr);
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +0000674void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov54e0a9a2012-11-07 16:41:57 +0000675int ThreadCount(ThreadState *thr);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000676void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000677
Dmitry Vyukovc20e9ba2012-08-16 13:29:41 +0000678void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
679 bool rw, bool recursive, bool linker_init);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000680void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700681void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
682 bool try_lock = false);
Dmitry Vyukov8354fae2013-05-17 12:03:46 +0000683int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700684void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000685void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
686void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov11f53092013-11-15 16:58:12 +0000687void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000688
689void Acquire(ThreadState *thr, uptr pc, uptr addr);
Stephen Hines6d186232014-11-26 17:56:19 -0800690// AcquireGlobal synchronizes the current thread with all other threads.
691// In terms of happens-before relation, it draws a HB edge from all threads
692// (where they happen to execute right now) to the current thread. We use it to
693// handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
694// right before executing finalizers. This provides a coarse, but simple
695// approximation of the actual required synchronization.
Dmitry Vyukov538f1ba2012-11-07 15:08:20 +0000696void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000697void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov9d150bd2012-07-28 15:27:41 +0000698void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000699void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000700void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
701void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
702void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
703void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000704
705// The hacky call uses custom calling convention and an assembly thunk.
706// It is considerably faster that a normal call for the caller
707// if it is not executed (it is intended for slow paths from hot functions).
708// The trick is that the call preserves all registers and the compiler
709// does not treat it as a call.
710// If it does not work for you, use normal call.
Stephen Hines86277eb2015-03-23 12:06:32 -0700711#if !SANITIZER_DEBUG && defined(__x86_64__)
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000712// The caller may not create the stack frame for itself at all,
713// so we create a reserve stack frame for it (1024b must be enough).
714#define HACKY_CALL(f) \
Dmitry Vyukov41e81532012-09-02 11:24:07 +0000715 __asm__ __volatile__("sub $1024, %%rsp;" \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700716 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukovf5d526f2012-11-26 14:20:26 +0000717 ".hidden " #f "_thunk;" \
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000718 "call " #f "_thunk;" \
Dmitry Vyukov41e81532012-09-02 11:24:07 +0000719 "add $1024, %%rsp;" \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700720 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukov41e81532012-09-02 11:24:07 +0000721 ::: "memory", "cc");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000722#else
723#define HACKY_CALL(f) f()
724#endif
725
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000726void TraceSwitch(ThreadState *thr);
Dmitry Vyukov385542a2012-11-28 10:35:31 +0000727uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000728uptr TraceSize();
Dmitry Vyukov0415ac02012-12-04 12:19:53 +0000729uptr TraceParts();
Dmitry Vyukov9743d742013-03-20 10:31:53 +0000730Trace *ThreadTrace(int tid);
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000731
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000732extern "C" void __tsan_trace_switch();
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +0000733void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000734 EventType typ, u64 addr) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700735 if (!kCollectHistory)
736 return;
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000737 DCHECK_GE((int)typ, 0);
738 DCHECK_LE((int)typ, 7);
739 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000740 StatInc(thr, StatEvents);
Dmitry Vyukov3fb70e32012-11-28 13:01:32 +0000741 u64 pos = fs.GetTracePos();
742 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700743#ifndef SANITIZER_GO
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000744 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000745#else
746 TraceSwitch(thr);
747#endif
748 }
Dmitry Vyukov385542a2012-11-28 10:35:31 +0000749 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukov3fb70e32012-11-28 13:01:32 +0000750 Event *evp = &trace[pos];
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000751 Event ev = (u64)addr | ((u64)typ << 61);
752 *evp = ev;
753}
754
Stephen Hines86277eb2015-03-23 12:06:32 -0700755#ifndef SANITIZER_GO
756uptr ALWAYS_INLINE HeapEnd() {
757#if SANITIZER_CAN_USE_ALLOCATOR64
758 return kHeapMemEnd + PrimaryAllocator::AdditionalSize();
759#else
760 return kHeapMemEnd;
761#endif
762}
763#endif
764
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000765} // namespace __tsan
766
767#endif // TSAN_RTL_H