blob: 889a79da10bcf60cd9dbd4527a239646e671f189 [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;
Bill Schmidt29791622015-12-08 21:54:39 +000057#if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__)
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +000058static const uptr kAllocatorSpace = 0;
59static const uptr kAllocatorSize = SANITIZER_MMAP_RANGE_SIZE;
60static const uptr kAllocatorRegionSizeLog = 20;
61static const uptr kAllocatorNumRegions =
62 kAllocatorSize >> kAllocatorRegionSizeLog;
63typedef TwoLevelByteMap<(kAllocatorNumRegions >> 12), 1 << 12,
64 MapUnmapCallback> ByteMap;
65typedef SizeClassAllocator32<kAllocatorSpace, kAllocatorSize, 0,
66 CompactSizeClassMap, kAllocatorRegionSizeLog, ByteMap,
67 MapUnmapCallback> PrimaryAllocator;
68#else
Kostya Serebryany15647b12016-08-25 20:23:08 +000069struct AP64 { // Allocator64 parameters. Deliberately using a short name.
70 static const uptr kSpaceBeg = Mapping::kHeapMemBeg;
71 static const uptr kSpaceSize = Mapping::kHeapMemEnd - Mapping::kHeapMemBeg;
72 static const uptr kMetadataSize = 0;
73 typedef DefaultSizeClassMap SizeClassMap;
74 typedef __tsan::MapUnmapCallback MapUnmapCallback;
75};
76typedef SizeClassAllocator64<AP64> PrimaryAllocator;
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +000077#endif
Kostya Serebryanyf2992882012-12-04 14:15:17 +000078typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Dmitry Vyukov20bf8c72013-03-18 10:32:21 +000079typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000080typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
81 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000082Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000083#endif
84
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000085void TsanCheckFailed(const char *file, int line, const char *cond,
86 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000087
Dmitry Vyukov79915de2013-03-20 10:31:53 +000088const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
89
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000090// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000091// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000092// tid : kTidBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000093// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000094// history_size : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000095// epoch : kClkBits
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000096class FastState {
97 public:
98 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000099 x_ = tid << kTidShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000100 x_ |= epoch;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000101 DCHECK_EQ(tid, this->tid());
102 DCHECK_EQ(epoch, this->epoch());
103 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000104 }
105
106 explicit FastState(u64 x)
107 : x_(x) {
108 }
109
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000110 u64 raw() const {
111 return x_;
112 }
113
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000114 u64 tid() const {
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000115 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
116 return res;
117 }
118
119 u64 TidWithIgnore() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000120 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000121 return res;
122 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000123
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000124 u64 epoch() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000125 u64 res = x_ & ((1ull << kClkBits) - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000126 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000127 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000128
129 void IncrementEpoch() {
130 u64 old_epoch = epoch();
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000131 x_ += 1;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000132 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000133 (void)old_epoch;
134 }
135
136 void SetIgnoreBit() { x_ |= kIgnoreBit; }
137 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000138 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000139
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000140 void SetHistorySize(int hs) {
141 CHECK_GE(hs, 0);
142 CHECK_LE(hs, 7);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000143 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000144 }
145
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000146 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000147 int GetHistorySize() const {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000148 return (int)((x_ >> kHistoryShift) & kHistoryMask);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000149 }
150
151 void ClearHistorySize() {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000152 SetHistorySize(0);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000153 }
154
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000155 ALWAYS_INLINE
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000156 u64 GetTracePos() const {
157 const int hs = GetHistorySize();
158 // When hs == 0, the trace consists of 2 parts.
159 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
160 return epoch() & mask;
161 }
162
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000163 private:
164 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000165 static const int kTidShift = 64 - kTidBits - 1;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000166 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000167 static const u64 kFreedBit = 1ull << 63;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000168 static const u64 kHistoryShift = kClkBits;
169 static const u64 kHistoryMask = 7;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000170 u64 x_;
171};
172
173// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000174// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000175// tid : kTidBits
Dmitry Vyukovba429142013-02-01 09:42:06 +0000176// is_atomic : 1
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000177// is_read : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000178// size_log : 2
179// addr0 : 3
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000180// epoch : kClkBits
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000181class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000182 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000183 explicit Shadow(u64 x)
184 : FastState(x) {
185 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000186
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000187 explicit Shadow(const FastState &s)
188 : FastState(s.x_) {
189 ClearHistorySize();
190 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191
192 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000193 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000194 DCHECK_LE(addr0, 7);
195 DCHECK_LE(kAccessSizeLog, 3);
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000196 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000197 DCHECK_EQ(kAccessSizeLog, size_log());
198 DCHECK_EQ(addr0, this->addr0());
199 }
200
201 void SetWrite(unsigned kAccessIsWrite) {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000202 DCHECK_EQ(x_ & kReadBit, 0);
203 if (!kAccessIsWrite)
204 x_ |= kReadBit;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000205 DCHECK_EQ(kAccessIsWrite, IsWrite());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000206 }
207
Dmitry Vyukovba429142013-02-01 09:42:06 +0000208 void SetAtomic(bool kIsAtomic) {
209 DCHECK(!IsAtomic());
210 if (kIsAtomic)
211 x_ |= kAtomicBit;
212 DCHECK_EQ(IsAtomic(), kIsAtomic);
213 }
214
215 bool IsAtomic() const {
216 return x_ & kAtomicBit;
217 }
218
219 bool IsZero() const {
220 return x_ == 0;
221 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000222
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000223 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000224 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Dmitry Vyukove993dac22012-11-30 20:02:11 +0000225 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 return shifted_xor == 0;
227 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000228
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000229 static ALWAYS_INLINE
230 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
231 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000232 return masked_xor == 0;
233 }
234
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000235 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000236 unsigned kS2AccessSize) {
237 bool res = false;
238 u64 diff = s1.addr0() - s2.addr0();
239 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
240 // if (s1.addr0() + size1) > s2.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000241 if (s1.size() > -diff)
242 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000243 } else {
244 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000245 if (kS2AccessSize > diff)
246 res = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000247 }
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000248 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
249 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000250 return res;
251 }
252
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000253 u64 ALWAYS_INLINE addr0() const { return (x_ >> kClkBits) & 7; }
254 u64 ALWAYS_INLINE size() const { return 1ull << size_log(); }
255 bool ALWAYS_INLINE IsWrite() const { return !IsRead(); }
256 bool ALWAYS_INLINE IsRead() const { return x_ & kReadBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000257
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000258 // The idea behind the freed bit is as follows.
259 // When the memory is freed (or otherwise unaccessible) we write to the shadow
260 // values with tid/epoch related to the free and the freed bit set.
261 // During memory accesses processing the freed bit is considered
262 // as msb of tid. So any access races with shadow with freed bit set
263 // (it is as if write from a thread with which we never synchronized before).
264 // This allows us to detect accesses to freed memory w/o additional
265 // overheads in memory access processing and at the same time restore
266 // tid/epoch of free.
267 void MarkAsFreed() {
268 x_ |= kFreedBit;
269 }
270
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000271 bool IsFreed() const {
272 return x_ & kFreedBit;
273 }
274
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000275 bool GetFreedAndReset() {
276 bool res = x_ & kFreedBit;
277 x_ &= ~kFreedBit;
278 return res;
279 }
280
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000281 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
282 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
283 | (u64(kIsAtomic) << kAtomicShift));
Dmitry Vyukovba429142013-02-01 09:42:06 +0000284 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
285 return v;
286 }
287
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000288 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000289 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000290 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
291 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
292 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
293 return v;
294 }
295
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000296 bool ALWAYS_INLINE IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000297 bool v = ((x_ >> kReadShift) & 3)
Dmitry Vyukovba429142013-02-01 09:42:06 +0000298 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
299 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
300 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
301 return v;
302 }
303
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000304 private:
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000305 static const u64 kReadShift = 5 + kClkBits;
Dmitry Vyukov71242b02013-02-01 10:02:55 +0000306 static const u64 kReadBit = 1ull << kReadShift;
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000307 static const u64 kAtomicShift = 6 + kClkBits;
Dmitry Vyukovba429142013-02-01 09:42:06 +0000308 static const u64 kAtomicBit = 1ull << kAtomicShift;
309
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000310 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000311
Dmitry Vyukovafdcc962014-05-30 13:36:29 +0000312 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000313 if (s1.addr0() == s2.addr0()) return true;
314 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
315 return true;
316 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
317 return true;
318 return false;
319 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000320};
321
Dmitry Vyukovb79ac882015-03-02 17:36:02 +0000322struct ThreadSignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000323
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000324struct JmpBuf {
325 uptr sp;
326 uptr mangled_sp;
Dmitry Vyukov69c4d372014-09-16 21:48:22 +0000327 int int_signal_send;
328 bool in_blocking_func;
329 uptr in_signal_handler;
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000330 uptr *shadow_stack_pos;
331};
332
Dmitry Vyukovd87c7b32016-04-27 08:23:02 +0000333// A Processor represents a physical thread, or a P for Go.
334// It is used to store internal resources like allocate cache, and does not
335// participate in race-detection logic (invisible to end user).
336// In C++ it is tied to an OS thread just like ThreadState, however ideally
337// it should be tied to a CPU (this way we will have fewer allocator caches).
338// In Go it is tied to a P, so there are significantly fewer Processor's than
339// ThreadState's (which are tied to Gs).
340// A ThreadState must be wired with a Processor to handle events.
341struct Processor {
342 ThreadState *thr; // currently wired thread, or nullptr
343#ifndef SANITIZER_GO
344 AllocatorCache alloc_cache;
345 InternalAllocatorCache internal_alloc_cache;
346#endif
347 DenseSlabAllocCache block_cache;
348 DenseSlabAllocCache sync_cache;
349 DenseSlabAllocCache clock_cache;
350 DDPhysicalThread *dd_pt;
351};
352
Dmitry Vyukov24986292016-05-10 11:19:50 +0000353#ifndef SANITIZER_GO
354// ScopedGlobalProcessor temporary setups a global processor for the current
355// thread, if it does not have one. Intended for interceptors that can run
356// at the very thread end, when we already destroyed the thread processor.
357struct ScopedGlobalProcessor {
358 ScopedGlobalProcessor();
359 ~ScopedGlobalProcessor();
360};
361#endif
362
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000363// This struct is stored in TLS.
364struct ThreadState {
365 FastState fast_state;
366 // Synch epoch represents the threads's epoch before the last synchronization
367 // action. It allows to reduce number of shadow state updates.
368 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
369 // if we are processing write to X from the same thread at epoch=200,
370 // we do nothing, because both writes happen in the same 'synch epoch'.
371 // That is, if another memory access does not race with the former write,
372 // it does not race with the latter as well.
373 // QUESTION: can we can squeeze this into ThreadState::Fast?
374 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
375 // taken by epoch between synchs.
376 // This way we can save one load from tls.
377 u64 fast_synch_epoch;
378 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
379 // We do not distinguish beteween ignoring reads and writes
380 // for better performance.
381 int ignore_reads_and_writes;
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000382 int ignore_sync;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000383 // Go does not support ignores.
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000384#ifndef SANITIZER_GO
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000385 IgnoreSet mop_ignore_set;
386 IgnoreSet sync_ignore_set;
387#endif
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000388 // C/C++ uses fixed size shadow stack embed into Trace.
389 // Go uses malloc-allocated shadow stack with dynamic size.
390 uptr *shadow_stack;
391 uptr *shadow_stack_end;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000392 uptr *shadow_stack_pos;
393 u64 *racy_shadow_addr;
394 u64 racy_state[2];
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000395 MutexSet mset;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000396 ThreadClock clock;
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000397#ifndef SANITIZER_GO
Dmitry Vyukov4adf49d2013-03-25 10:10:44 +0000398 Vector<JmpBuf> jmp_bufs;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000399 int ignore_interceptors;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000400#endif
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000401#if TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000402 u64 stat[StatCnt];
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000403#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000404 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000405 const int unique_id;
Dmitry Vyukovb46930b2013-01-29 13:03:07 +0000406 bool in_symbolizer;
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000407 bool in_ignored_lib;
Viktor Kutuzova7d323e2015-03-16 14:42:21 +0000408 bool is_inited;
Dmitry Vyukovf8cfdd92014-09-03 12:25:22 +0000409 bool is_dead;
Dmitry Vyukov87c6bb92013-02-01 14:41:58 +0000410 bool is_freeing;
Dmitry Vyukov0851fa82013-03-21 15:37:39 +0000411 bool is_vptr_access;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000412 const uptr stk_addr;
413 const uptr stk_size;
414 const uptr tls_addr;
415 const uptr tls_size;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000416 ThreadContext *tctx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000417
Dmitry Vyukova22870e2015-02-13 15:37:11 +0000418#if SANITIZER_DEBUG && !SANITIZER_GO
Kostya Serebryanya63632a2014-02-14 12:20:42 +0000419 InternalDeadlockDetector internal_deadlock_detector;
Dmitry Vyukova22870e2015-02-13 15:37:11 +0000420#endif
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000421 DDLogicalThread *dd_lt;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000422
Dmitry Vyukovd87c7b32016-04-27 08:23:02 +0000423 // Current wired Processor, or nullptr. Required to handle any events.
Dmitry Vyukov3efe3952016-04-27 12:30:48 +0000424 Processor *proc1;
425#ifndef SANITIZER_GO
426 Processor *proc() { return proc1; }
427#else
428 Processor *proc();
429#endif
Dmitry Vyukovd87c7b32016-04-27 08:23:02 +0000430
Dmitry Vyukov18412192014-09-02 12:27:45 +0000431 atomic_uintptr_t in_signal_handler;
Dmitry Vyukovb79ac882015-03-02 17:36:02 +0000432 ThreadSignalContext *signal_ctx;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000433
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000434#ifndef SANITIZER_GO
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000435 u32 last_sleep_stack_id;
436 ThreadClock last_sleep_clock;
437#endif
438
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000439 // Set in regions of runtime that must be signal-safe and fork-safe.
440 // If set, malloc must not be called.
441 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000442
Kuba Breckaa1496f72016-03-10 17:00:29 +0000443 const ReportDesc *current_report;
444
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000445 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +0000446 unsigned reuse_count,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000447 uptr stk_addr, uptr stk_size,
448 uptr tls_addr, uptr tls_size);
449};
450
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000451#ifndef SANITIZER_GO
Yabin Cuibd3a7722016-01-15 03:39:04 +0000452#if SANITIZER_MAC || SANITIZER_ANDROID
Kuba Breckacd18f282015-11-05 13:54:50 +0000453ThreadState *cur_thread();
454void cur_thread_finalize();
455#else
Kostya Serebryanye61f4d52014-05-12 10:40:33 +0000456__attribute__((tls_model("initial-exec")))
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000457extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000458INLINE ThreadState *cur_thread() {
459 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
460}
Kuba Breckacd18f282015-11-05 13:54:50 +0000461INLINE void cur_thread_finalize() { }
Yabin Cuibd3a7722016-01-15 03:39:04 +0000462#endif // SANITIZER_MAC || SANITIZER_ANDROID
Kuba Breckacd18f282015-11-05 13:54:50 +0000463#endif // SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000464
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000465class ThreadContext : public ThreadContextBase {
466 public:
467 explicit ThreadContext(int tid);
Dmitry Vyukov49e462f2013-03-18 10:10:15 +0000468 ~ThreadContext();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000469 ThreadState *thr;
Dmitry Vyukov7cd20252013-03-18 09:02:27 +0000470 u32 creation_stack_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000471 SyncClock sync;
472 // Epoch at which the thread had started.
473 // If we see an event from the thread stamped by an older epoch,
474 // the event is from a dead thread that shared tid with this thread.
475 u64 epoch0;
476 u64 epoch1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000477
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000478 // Override superclass callbacks.
Alexander Kornienkod8298122015-04-11 02:44:24 +0000479 void OnDead() override;
480 void OnJoined(void *arg) override;
481 void OnFinished() override;
482 void OnStarted(void *arg) override;
483 void OnCreated(void *arg) override;
484 void OnReset() override;
485 void OnDetached(void *arg) override;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000486};
487
488struct RacyStacks {
489 MD5Hash hash[2];
490 bool operator==(const RacyStacks &other) const {
491 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
492 return true;
493 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
494 return true;
495 return false;
496 }
497};
498
499struct RacyAddress {
500 uptr addr_min;
501 uptr addr_max;
502};
503
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000504struct FiredSuppression {
505 ReportType type;
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000506 uptr pc_or_addr;
Dmitry Vyukovb365d402013-03-27 17:59:57 +0000507 Suppression *supp;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000508};
509
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000510struct Context {
511 Context();
512
513 bool initialized;
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000514 bool after_multithreaded_fork;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000515
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000516 MetaMap metamap;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000517
518 Mutex report_mtx;
519 int nreported;
520 int nmissed_expected;
Dmitry Vyukov48e5d4a2013-03-21 07:02:36 +0000521 atomic_uint64_t last_symbolize_time_ns;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000522
Dmitry Vyukovb7ebc532014-04-24 13:09:17 +0000523 void *background_thread;
524 atomic_uint32_t stop_background_thread;
525
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000526 ThreadRegistry *thread_registry;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000527
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000528 Mutex racy_mtx;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000529 Vector<RacyStacks> racy_stacks;
530 Vector<RacyAddress> racy_addresses;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000531 // Number of fired suppressions may be large enough.
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000532 Mutex fired_suppressions_mtx;
Alexey Samsonov0d7012d2013-06-14 11:18:58 +0000533 InternalMmapVector<FiredSuppression> fired_suppressions;
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000534 DDetector *dd;
Kostya Serebryany0548c792014-02-21 15:07:18 +0000535
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000536 ClockAlloc clock_alloc;
537
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000538 Flags flags;
539
540 u64 stat[StatCnt];
541 u64 int_alloc_cnt[MBlockTypeCount];
542 u64 int_alloc_siz[MBlockTypeCount];
543};
544
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000545extern Context *ctx; // The one and the only global runtime context.
546
Dmitry Vyukovce372102013-12-24 12:55:56 +0000547struct ScopedIgnoreInterceptors {
548 ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000549#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000550 cur_thread()->ignore_interceptors++;
551#endif
552 }
553
554 ~ScopedIgnoreInterceptors() {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000555#ifndef SANITIZER_GO
Dmitry Vyukovce372102013-12-24 12:55:56 +0000556 cur_thread()->ignore_interceptors--;
557#endif
558 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000559};
560
561class ScopedReport {
562 public:
563 explicit ScopedReport(ReportType typ);
564 ~ScopedReport();
565
Alexey Samsonov40733a82014-11-03 22:23:44 +0000566 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000567 const MutexSet *mset);
Alexey Samsonov40733a82014-11-03 22:23:44 +0000568 void AddStack(StackTrace stack, bool suppressable = false);
Dmitry Vyukova43e98c2014-05-28 18:03:32 +0000569 void AddThread(const ThreadContext *tctx, bool suppressable = false);
570 void AddThread(int unique_tid, bool suppressable = false);
Kostya Serebryany3df5d872014-03-21 13:00:18 +0000571 void AddUniqueTid(int unique_tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000572 void AddMutex(const SyncVar *s);
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000573 u64 AddMutex(u64 id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000574 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000575 void AddSleep(u32 stack_id);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000576 void SetCount(int count);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000577
578 const ReportDesc *GetReport() const;
579
580 private:
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000581 ReportDesc *rep_;
Dmitry Vyukovce372102013-12-24 12:55:56 +0000582 // Symbolizer makes lots of intercepted calls. If we try to process them,
583 // at best it will cause deadlocks on internal mutexes.
584 ScopedIgnoreInterceptors ignore_interceptors_;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000585
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000586 void AddDeadMutex(u64 id);
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000587
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000588 ScopedReport(const ScopedReport&);
589 void operator = (const ScopedReport&);
590};
591
Alexey Samsonov40733a82014-11-03 22:23:44 +0000592void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
593 MutexSet *mset);
594
595template<typename StackTraceTy>
596void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
597 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
598 uptr start = 0;
599 if (size + !!toppc > kStackTraceMax) {
600 start = size + !!toppc - kStackTraceMax;
601 size = kStackTraceMax - !!toppc;
602 }
603 stack->Init(&thr->shadow_stack[start], size, toppc);
604}
605
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000606
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000607#if TSAN_COLLECT_STATS
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000608void StatAggregate(u64 *dst, u64 *src);
609void StatOutput(u64 *stat);
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000610#endif
611
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000612void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000613#if TSAN_COLLECT_STATS
Dmitry Vyukovb57e39e2015-02-13 16:08:43 +0000614 thr->stat[typ] += n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000615#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000616}
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000617void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
Alexey Samsonove194dfa2015-02-17 23:23:10 +0000618#if TSAN_COLLECT_STATS
Dmitry Vyukovb57e39e2015-02-13 16:08:43 +0000619 thr->stat[typ] = n;
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000620#endif
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000621}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000622
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000623void MapShadow(uptr addr, uptr size);
Evgeniy Stepanov8e9c70b2015-05-29 22:31:28 +0000624void MapThreadTrace(uptr addr, uptr size, const char *name);
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000625void DontNeedShadowFor(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000626void InitializeShadowMemory();
627void InitializeInterceptors();
Dmitry Vyukov5ba73642013-10-03 13:37:17 +0000628void InitializeLibIgnore();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000629void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000630
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000631void ForkBefore(ThreadState *thr, uptr pc);
632void ForkParentAfter(ThreadState *thr, uptr pc);
633void ForkChildAfter(ThreadState *thr, uptr pc);
634
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000635void ReportRace(ThreadState *thr);
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000636bool OutputReport(ThreadState *thr, const ScopedReport &srep);
Dmitry Vyukov3464dac2015-09-03 11:20:46 +0000637bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000638bool IsExpectedReport(uptr addr, uptr size);
Dmitry Vyukovf2cbda42013-03-28 16:21:19 +0000639void PrintMatchedBenignRaces();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000640
641#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000642# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000643#else
644# define DPrintf(...)
645#endif
646
647#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000648# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000649#else
650# define DPrintf2(...)
651#endif
652
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000653u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000654ReportStack *SymbolizeStackId(u32 stack_id);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000655void PrintCurrentStack(ThreadState *thr, uptr pc);
Alexey Samsonovfbaaed62014-11-06 18:43:45 +0000656void PrintCurrentStackSlow(uptr pc); // uses libunwind
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000657
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000658void Initialize(ThreadState *thr);
659int Finalize(ThreadState *thr);
660
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000661void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
662void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
Dmitry Vyukov2547ac62012-12-20 17:29:34 +0000663
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000664void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000665 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000666void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000667 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000668 u64 *shadow_mem, Shadow cur);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000669void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000670 uptr size, bool is_write);
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000671void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
672 uptr size, uptr step, bool is_write);
Dmitry Vyukov3f7bf082013-04-30 11:56:56 +0000673void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
674 int size, bool kAccessIsWrite, bool kIsAtomic);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000675
676const int kSizeLog1 = 0;
677const int kSizeLog2 = 1;
678const int kSizeLog4 = 2;
679const int kSizeLog8 = 3;
680
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000681void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000682 uptr addr, int kAccessSizeLog) {
683 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
684}
685
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000686void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000687 uptr addr, int kAccessSizeLog) {
688 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
689}
690
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000691void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000692 uptr addr, int kAccessSizeLog) {
693 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
694}
695
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000696void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
Dmitry Vyukovba429142013-02-01 09:42:06 +0000697 uptr addr, int kAccessSizeLog) {
698 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
699}
700
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000701void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
702void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000703void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000704
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000705void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
706void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
707void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
708void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000709
710void FuncEntry(ThreadState *thr, uptr pc);
711void FuncExit(ThreadState *thr);
712
713int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000714void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000715void ThreadFinish(ThreadState *thr);
716int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
717void ThreadJoin(ThreadState *thr, uptr pc, int tid);
718void ThreadDetach(ThreadState *thr, uptr pc, int tid);
719void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000720void ThreadSetName(ThreadState *thr, const char *name);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000721int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000722void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000723
Dmitry Vyukovd87c7b32016-04-27 08:23:02 +0000724Processor *ProcCreate();
725void ProcDestroy(Processor *proc);
726void ProcWire(Processor *proc, ThreadState *thr);
727void ProcUnwire(Processor *proc, ThreadState *thr);
728
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000729void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
730 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000731void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany11f4f302014-02-25 08:24:15 +0000732void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
733 bool try_lock = false);
Dmitry Vyukovc9af8182013-05-17 12:03:46 +0000734int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
Kostya Serebryany01be2962014-02-25 10:33:37 +0000735void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000736void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
737void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov4bbe6dc2013-11-15 16:58:12 +0000738void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
Kuba Brecka46bf4542016-03-16 15:39:20 +0000739void MutexInvalidAccess(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000740
741void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukovbd167972014-11-18 06:44:43 +0000742// AcquireGlobal synchronizes the current thread with all other threads.
743// In terms of happens-before relation, it draws a HB edge from all threads
744// (where they happen to execute right now) to the current thread. We use it to
745// handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
746// right before executing finalizers. This provides a coarse, but simple
747// approximation of the actual required synchronization.
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000748void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000749void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000750void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000751void AfterSleep(ThreadState *thr, uptr pc);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000752void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
753void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
754void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
755void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000756
757// The hacky call uses custom calling convention and an assembly thunk.
758// It is considerably faster that a normal call for the caller
759// if it is not executed (it is intended for slow paths from hot functions).
760// The trick is that the call preserves all registers and the compiler
761// does not treat it as a call.
762// If it does not work for you, use normal call.
Kuba Breckacf41c0c2015-11-03 14:33:39 +0000763#if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000764// The caller may not create the stack frame for itself at all,
765// so we create a reserve stack frame for it (1024b must be enough).
766#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000767 __asm__ __volatile__("sub $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000768 CFI_INL_ADJUST_CFA_OFFSET(1024) \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000769 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000770 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000771 "add $1024, %%rsp;" \
Kostya Serebryany14e92c22013-12-05 07:44:35 +0000772 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000773 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000774#else
775#define HACKY_CALL(f) f()
776#endif
777
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000778void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000779uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000780uptr TraceSize();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000781uptr TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000782Trace *ThreadTrace(int tid);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000783
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000784extern "C" void __tsan_trace_switch();
Timur Iskhodzhanova6788322013-03-28 18:52:40 +0000785void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000786 EventType typ, u64 addr) {
Dmitry Vyukov547089e2014-05-15 12:51:48 +0000787 if (!kCollectHistory)
788 return;
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000789 DCHECK_GE((int)typ, 0);
790 DCHECK_LE((int)typ, 7);
791 DCHECK_EQ(GetLsb(addr, 61), addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000792 StatInc(thr, StatEvents);
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000793 u64 pos = fs.GetTracePos();
794 if (UNLIKELY((pos % kTracePartSize) == 0)) {
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000795#ifndef SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000796 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000797#else
798 TraceSwitch(thr);
799#endif
800 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000801 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +0000802 Event *evp = &trace[pos];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000803 Event ev = (u64)addr | ((u64)typ << 61);
804 *evp = ev;
805}
806
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000807#ifndef SANITIZER_GO
808uptr ALWAYS_INLINE HeapEnd() {
Adhemerval Zanella066c5f02015-11-26 13:10:47 +0000809 return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
Mohit K. Bhakkada46d5a72015-02-20 06:42:41 +0000810}
811#endif
812
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000813} // namespace __tsan
814
815#endif // TSAN_RTL_H