[Ada] Fix assertion failure on pragma Compile_Time_Error in generic unit
[official-gcc.git] / libsanitizer / tsan / tsan_rtl.h
blobf97b583c48b9d8b84f61ad4d4f0ba66a0c7275c0
1 //===-- tsan_rtl.h ----------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 // Main internal TSan header file.
12 // Ground rules:
13 // - C++ run-time should not be used (static CTORs, RTTI, exceptions, static
14 // function-scope locals)
15 // - All functions/classes/etc reside in namespace __tsan, except for those
16 // declared in tsan_interface.h.
17 // - Platform-specific files should be used instead of ifdefs (*).
18 // - No system headers included in header files (*).
19 // - Platform specific headres included only into platform-specific files (*).
21 // (*) Except when inlining is critical for performance.
22 //===----------------------------------------------------------------------===//
24 #ifndef TSAN_RTL_H
25 #define TSAN_RTL_H
27 #include "sanitizer_common/sanitizer_allocator.h"
28 #include "sanitizer_common/sanitizer_allocator_internal.h"
29 #include "sanitizer_common/sanitizer_asm.h"
30 #include "sanitizer_common/sanitizer_common.h"
31 #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
32 #include "sanitizer_common/sanitizer_libignore.h"
33 #include "sanitizer_common/sanitizer_suppressions.h"
34 #include "sanitizer_common/sanitizer_thread_registry.h"
35 #include "sanitizer_common/sanitizer_vector.h"
36 #include "tsan_clock.h"
37 #include "tsan_defs.h"
38 #include "tsan_flags.h"
39 #include "tsan_mman.h"
40 #include "tsan_sync.h"
41 #include "tsan_trace.h"
42 #include "tsan_report.h"
43 #include "tsan_platform.h"
44 #include "tsan_mutexset.h"
45 #include "tsan_ignoreset.h"
46 #include "tsan_stack_trace.h"
48 #if SANITIZER_WORDSIZE != 64
49 # error "ThreadSanitizer is supported only on 64-bit platforms"
50 #endif
52 namespace __tsan {
54 #if !SANITIZER_GO
55 struct MapUnmapCallback;
56 #if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__)
57 static const uptr kAllocatorRegionSizeLog = 20;
58 static const uptr kAllocatorNumRegions =
59 SANITIZER_MMAP_RANGE_SIZE >> kAllocatorRegionSizeLog;
60 typedef TwoLevelByteMap<(kAllocatorNumRegions >> 12), 1 << 12,
61 MapUnmapCallback> ByteMap;
62 struct AP32 {
63 static const uptr kSpaceBeg = 0;
64 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
65 static const uptr kMetadataSize = 0;
66 typedef __sanitizer::CompactSizeClassMap SizeClassMap;
67 static const uptr kRegionSizeLog = kAllocatorRegionSizeLog;
68 typedef __tsan::ByteMap ByteMap;
69 typedef __tsan::MapUnmapCallback MapUnmapCallback;
70 static const uptr kFlags = 0;
72 typedef SizeClassAllocator32<AP32> PrimaryAllocator;
73 #else
74 struct AP64 { // Allocator64 parameters. Deliberately using a short name.
75 static const uptr kSpaceBeg = Mapping::kHeapMemBeg;
76 static const uptr kSpaceSize = Mapping::kHeapMemEnd - Mapping::kHeapMemBeg;
77 static const uptr kMetadataSize = 0;
78 typedef DefaultSizeClassMap SizeClassMap;
79 typedef __tsan::MapUnmapCallback MapUnmapCallback;
80 static const uptr kFlags = 0;
82 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
83 #endif
84 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
85 typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
86 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
87 SecondaryAllocator> Allocator;
88 Allocator *allocator();
89 #endif
91 void TsanCheckFailed(const char *file, int line, const char *cond,
92 u64 v1, u64 v2);
94 const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
96 // FastState (from most significant bit):
97 // ignore : 1
98 // tid : kTidBits
99 // unused : -
100 // history_size : 3
101 // epoch : kClkBits
102 class FastState {
103 public:
104 FastState(u64 tid, u64 epoch) {
105 x_ = tid << kTidShift;
106 x_ |= epoch;
107 DCHECK_EQ(tid, this->tid());
108 DCHECK_EQ(epoch, this->epoch());
109 DCHECK_EQ(GetIgnoreBit(), false);
112 explicit FastState(u64 x)
113 : x_(x) {
116 u64 raw() const {
117 return x_;
120 u64 tid() const {
121 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
122 return res;
125 u64 TidWithIgnore() const {
126 u64 res = x_ >> kTidShift;
127 return res;
130 u64 epoch() const {
131 u64 res = x_ & ((1ull << kClkBits) - 1);
132 return res;
135 void IncrementEpoch() {
136 u64 old_epoch = epoch();
137 x_ += 1;
138 DCHECK_EQ(old_epoch + 1, epoch());
139 (void)old_epoch;
142 void SetIgnoreBit() { x_ |= kIgnoreBit; }
143 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
144 bool GetIgnoreBit() const { return (s64)x_ < 0; }
146 void SetHistorySize(int hs) {
147 CHECK_GE(hs, 0);
148 CHECK_LE(hs, 7);
149 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
152 ALWAYS_INLINE
153 int GetHistorySize() const {
154 return (int)((x_ >> kHistoryShift) & kHistoryMask);
157 void ClearHistorySize() {
158 SetHistorySize(0);
161 ALWAYS_INLINE
162 u64 GetTracePos() const {
163 const int hs = GetHistorySize();
164 // When hs == 0, the trace consists of 2 parts.
165 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
166 return epoch() & mask;
169 private:
170 friend class Shadow;
171 static const int kTidShift = 64 - kTidBits - 1;
172 static const u64 kIgnoreBit = 1ull << 63;
173 static const u64 kFreedBit = 1ull << 63;
174 static const u64 kHistoryShift = kClkBits;
175 static const u64 kHistoryMask = 7;
176 u64 x_;
179 // Shadow (from most significant bit):
180 // freed : 1
181 // tid : kTidBits
182 // is_atomic : 1
183 // is_read : 1
184 // size_log : 2
185 // addr0 : 3
186 // epoch : kClkBits
187 class Shadow : public FastState {
188 public:
189 explicit Shadow(u64 x)
190 : FastState(x) {
193 explicit Shadow(const FastState &s)
194 : FastState(s.x_) {
195 ClearHistorySize();
198 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
199 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
200 DCHECK_LE(addr0, 7);
201 DCHECK_LE(kAccessSizeLog, 3);
202 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
203 DCHECK_EQ(kAccessSizeLog, size_log());
204 DCHECK_EQ(addr0, this->addr0());
207 void SetWrite(unsigned kAccessIsWrite) {
208 DCHECK_EQ(x_ & kReadBit, 0);
209 if (!kAccessIsWrite)
210 x_ |= kReadBit;
211 DCHECK_EQ(kAccessIsWrite, IsWrite());
214 void SetAtomic(bool kIsAtomic) {
215 DCHECK(!IsAtomic());
216 if (kIsAtomic)
217 x_ |= kAtomicBit;
218 DCHECK_EQ(IsAtomic(), kIsAtomic);
221 bool IsAtomic() const {
222 return x_ & kAtomicBit;
225 bool IsZero() const {
226 return x_ == 0;
229 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
230 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
231 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
232 return shifted_xor == 0;
235 static ALWAYS_INLINE
236 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
237 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
238 return masked_xor == 0;
241 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
242 unsigned kS2AccessSize) {
243 bool res = false;
244 u64 diff = s1.addr0() - s2.addr0();
245 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
246 // if (s1.addr0() + size1) > s2.addr0()) return true;
247 if (s1.size() > -diff)
248 res = true;
249 } else {
250 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
251 if (kS2AccessSize > diff)
252 res = true;
254 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
255 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
256 return res;
259 u64 ALWAYS_INLINE addr0() const { return (x_ >> kClkBits) & 7; }
260 u64 ALWAYS_INLINE size() const { return 1ull << size_log(); }
261 bool ALWAYS_INLINE IsWrite() const { return !IsRead(); }
262 bool ALWAYS_INLINE IsRead() const { return x_ & kReadBit; }
264 // The idea behind the freed bit is as follows.
265 // When the memory is freed (or otherwise unaccessible) we write to the shadow
266 // values with tid/epoch related to the free and the freed bit set.
267 // During memory accesses processing the freed bit is considered
268 // as msb of tid. So any access races with shadow with freed bit set
269 // (it is as if write from a thread with which we never synchronized before).
270 // This allows us to detect accesses to freed memory w/o additional
271 // overheads in memory access processing and at the same time restore
272 // tid/epoch of free.
273 void MarkAsFreed() {
274 x_ |= kFreedBit;
277 bool IsFreed() const {
278 return x_ & kFreedBit;
281 bool GetFreedAndReset() {
282 bool res = x_ & kFreedBit;
283 x_ &= ~kFreedBit;
284 return res;
287 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
288 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
289 | (u64(kIsAtomic) << kAtomicShift));
290 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
291 return v;
294 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
295 bool v = ((x_ >> kReadShift) & 3)
296 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
297 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
298 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
299 return v;
302 bool ALWAYS_INLINE IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
303 bool v = ((x_ >> kReadShift) & 3)
304 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
305 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
306 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
307 return v;
310 private:
311 static const u64 kReadShift = 5 + kClkBits;
312 static const u64 kReadBit = 1ull << kReadShift;
313 static const u64 kAtomicShift = 6 + kClkBits;
314 static const u64 kAtomicBit = 1ull << kAtomicShift;
316 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
318 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
319 if (s1.addr0() == s2.addr0()) return true;
320 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
321 return true;
322 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
323 return true;
324 return false;
328 struct ThreadSignalContext;
330 struct JmpBuf {
331 uptr sp;
332 uptr mangled_sp;
333 int int_signal_send;
334 bool in_blocking_func;
335 uptr in_signal_handler;
336 uptr *shadow_stack_pos;
339 // A Processor represents a physical thread, or a P for Go.
340 // It is used to store internal resources like allocate cache, and does not
341 // participate in race-detection logic (invisible to end user).
342 // In C++ it is tied to an OS thread just like ThreadState, however ideally
343 // it should be tied to a CPU (this way we will have fewer allocator caches).
344 // In Go it is tied to a P, so there are significantly fewer Processor's than
345 // ThreadState's (which are tied to Gs).
346 // A ThreadState must be wired with a Processor to handle events.
347 struct Processor {
348 ThreadState *thr; // currently wired thread, or nullptr
349 #if !SANITIZER_GO
350 AllocatorCache alloc_cache;
351 InternalAllocatorCache internal_alloc_cache;
352 #endif
353 DenseSlabAllocCache block_cache;
354 DenseSlabAllocCache sync_cache;
355 DenseSlabAllocCache clock_cache;
356 DDPhysicalThread *dd_pt;
359 #if !SANITIZER_GO
360 // ScopedGlobalProcessor temporary setups a global processor for the current
361 // thread, if it does not have one. Intended for interceptors that can run
362 // at the very thread end, when we already destroyed the thread processor.
363 struct ScopedGlobalProcessor {
364 ScopedGlobalProcessor();
365 ~ScopedGlobalProcessor();
367 #endif
369 // This struct is stored in TLS.
370 struct ThreadState {
371 FastState fast_state;
372 // Synch epoch represents the threads's epoch before the last synchronization
373 // action. It allows to reduce number of shadow state updates.
374 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
375 // if we are processing write to X from the same thread at epoch=200,
376 // we do nothing, because both writes happen in the same 'synch epoch'.
377 // That is, if another memory access does not race with the former write,
378 // it does not race with the latter as well.
379 // QUESTION: can we can squeeze this into ThreadState::Fast?
380 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
381 // taken by epoch between synchs.
382 // This way we can save one load from tls.
383 u64 fast_synch_epoch;
384 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
385 // We do not distinguish beteween ignoring reads and writes
386 // for better performance.
387 int ignore_reads_and_writes;
388 int ignore_sync;
389 int suppress_reports;
390 // Go does not support ignores.
391 #if !SANITIZER_GO
392 IgnoreSet mop_ignore_set;
393 IgnoreSet sync_ignore_set;
394 #endif
395 // C/C++ uses fixed size shadow stack embed into Trace.
396 // Go uses malloc-allocated shadow stack with dynamic size.
397 uptr *shadow_stack;
398 uptr *shadow_stack_end;
399 uptr *shadow_stack_pos;
400 u64 *racy_shadow_addr;
401 u64 racy_state[2];
402 MutexSet mset;
403 ThreadClock clock;
404 #if !SANITIZER_GO
405 Vector<JmpBuf> jmp_bufs;
406 int ignore_interceptors;
407 #endif
408 #if TSAN_COLLECT_STATS
409 u64 stat[StatCnt];
410 #endif
411 const int tid;
412 const int unique_id;
413 bool in_symbolizer;
414 bool in_ignored_lib;
415 bool is_inited;
416 bool is_dead;
417 bool is_freeing;
418 bool is_vptr_access;
419 const uptr stk_addr;
420 const uptr stk_size;
421 const uptr tls_addr;
422 const uptr tls_size;
423 ThreadContext *tctx;
425 #if SANITIZER_DEBUG && !SANITIZER_GO
426 InternalDeadlockDetector internal_deadlock_detector;
427 #endif
428 DDLogicalThread *dd_lt;
430 // Current wired Processor, or nullptr. Required to handle any events.
431 Processor *proc1;
432 #if !SANITIZER_GO
433 Processor *proc() { return proc1; }
434 #else
435 Processor *proc();
436 #endif
438 atomic_uintptr_t in_signal_handler;
439 ThreadSignalContext *signal_ctx;
441 #if !SANITIZER_GO
442 u32 last_sleep_stack_id;
443 ThreadClock last_sleep_clock;
444 #endif
446 // Set in regions of runtime that must be signal-safe and fork-safe.
447 // If set, malloc must not be called.
448 int nomalloc;
450 const ReportDesc *current_report;
452 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
453 unsigned reuse_count,
454 uptr stk_addr, uptr stk_size,
455 uptr tls_addr, uptr tls_size);
458 #if !SANITIZER_GO
459 #if SANITIZER_MAC || SANITIZER_ANDROID
460 ThreadState *cur_thread();
461 void cur_thread_finalize();
462 #else
463 __attribute__((tls_model("initial-exec")))
464 extern THREADLOCAL char cur_thread_placeholder[];
465 INLINE ThreadState *cur_thread() {
466 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
468 INLINE void cur_thread_finalize() { }
469 #endif // SANITIZER_MAC || SANITIZER_ANDROID
470 #endif // SANITIZER_GO
472 class ThreadContext : public ThreadContextBase {
473 public:
474 explicit ThreadContext(int tid);
475 ~ThreadContext();
476 ThreadState *thr;
477 u32 creation_stack_id;
478 SyncClock sync;
479 // Epoch at which the thread had started.
480 // If we see an event from the thread stamped by an older epoch,
481 // the event is from a dead thread that shared tid with this thread.
482 u64 epoch0;
483 u64 epoch1;
485 // Override superclass callbacks.
486 void OnDead() override;
487 void OnJoined(void *arg) override;
488 void OnFinished() override;
489 void OnStarted(void *arg) override;
490 void OnCreated(void *arg) override;
491 void OnReset() override;
492 void OnDetached(void *arg) override;
495 struct RacyStacks {
496 MD5Hash hash[2];
497 bool operator==(const RacyStacks &other) const {
498 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
499 return true;
500 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
501 return true;
502 return false;
506 struct RacyAddress {
507 uptr addr_min;
508 uptr addr_max;
511 struct FiredSuppression {
512 ReportType type;
513 uptr pc_or_addr;
514 Suppression *supp;
517 struct Context {
518 Context();
520 bool initialized;
521 #if !SANITIZER_GO
522 bool after_multithreaded_fork;
523 #endif
525 MetaMap metamap;
527 Mutex report_mtx;
528 int nreported;
529 int nmissed_expected;
530 atomic_uint64_t last_symbolize_time_ns;
532 void *background_thread;
533 atomic_uint32_t stop_background_thread;
535 ThreadRegistry *thread_registry;
537 Mutex racy_mtx;
538 Vector<RacyStacks> racy_stacks;
539 Vector<RacyAddress> racy_addresses;
540 // Number of fired suppressions may be large enough.
541 Mutex fired_suppressions_mtx;
542 InternalMmapVector<FiredSuppression> fired_suppressions;
543 DDetector *dd;
545 ClockAlloc clock_alloc;
547 Flags flags;
549 u64 stat[StatCnt];
550 u64 int_alloc_cnt[MBlockTypeCount];
551 u64 int_alloc_siz[MBlockTypeCount];
554 extern Context *ctx; // The one and the only global runtime context.
556 ALWAYS_INLINE Flags *flags() {
557 return &ctx->flags;
560 struct ScopedIgnoreInterceptors {
561 ScopedIgnoreInterceptors() {
562 #if !SANITIZER_GO
563 cur_thread()->ignore_interceptors++;
564 #endif
567 ~ScopedIgnoreInterceptors() {
568 #if !SANITIZER_GO
569 cur_thread()->ignore_interceptors--;
570 #endif
574 const char *GetObjectTypeFromTag(uptr tag);
575 const char *GetReportHeaderFromTag(uptr tag);
576 uptr TagFromShadowStackFrame(uptr pc);
578 class ScopedReportBase {
579 public:
580 void AddMemoryAccess(uptr addr, uptr external_tag, Shadow s, StackTrace stack,
581 const MutexSet *mset);
582 void AddStack(StackTrace stack, bool suppressable = false);
583 void AddThread(const ThreadContext *tctx, bool suppressable = false);
584 void AddThread(int unique_tid, bool suppressable = false);
585 void AddUniqueTid(int unique_tid);
586 void AddMutex(const SyncVar *s);
587 u64 AddMutex(u64 id);
588 void AddLocation(uptr addr, uptr size);
589 void AddSleep(u32 stack_id);
590 void SetCount(int count);
592 const ReportDesc *GetReport() const;
594 protected:
595 ScopedReportBase(ReportType typ, uptr tag);
596 ~ScopedReportBase();
598 private:
599 ReportDesc *rep_;
600 // Symbolizer makes lots of intercepted calls. If we try to process them,
601 // at best it will cause deadlocks on internal mutexes.
602 ScopedIgnoreInterceptors ignore_interceptors_;
604 void AddDeadMutex(u64 id);
606 ScopedReportBase(const ScopedReportBase &) = delete;
607 void operator=(const ScopedReportBase &) = delete;
610 class ScopedReport : public ScopedReportBase {
611 public:
612 explicit ScopedReport(ReportType typ, uptr tag = kExternalTagNone);
613 ~ScopedReport();
615 private:
616 ScopedErrorReportLock lock_;
619 ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack);
620 void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
621 MutexSet *mset, uptr *tag = nullptr);
623 // The stack could look like:
624 // <start> | <main> | <foo> | tag | <bar>
625 // This will extract the tag and keep:
626 // <start> | <main> | <foo> | <bar>
627 template<typename StackTraceTy>
628 void ExtractTagFromStack(StackTraceTy *stack, uptr *tag = nullptr) {
629 if (stack->size < 2) return;
630 uptr possible_tag_pc = stack->trace[stack->size - 2];
631 uptr possible_tag = TagFromShadowStackFrame(possible_tag_pc);
632 if (possible_tag == kExternalTagNone) return;
633 stack->trace_buffer[stack->size - 2] = stack->trace_buffer[stack->size - 1];
634 stack->size -= 1;
635 if (tag) *tag = possible_tag;
638 template<typename StackTraceTy>
639 void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack,
640 uptr *tag = nullptr) {
641 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
642 uptr start = 0;
643 if (size + !!toppc > kStackTraceMax) {
644 start = size + !!toppc - kStackTraceMax;
645 size = kStackTraceMax - !!toppc;
647 stack->Init(&thr->shadow_stack[start], size, toppc);
648 ExtractTagFromStack(stack, tag);
651 #define GET_STACK_TRACE_FATAL(thr, pc) \
652 VarSizeStackTrace stack; \
653 ObtainCurrentStack(thr, pc, &stack); \
654 stack.ReverseOrder();
656 #if TSAN_COLLECT_STATS
657 void StatAggregate(u64 *dst, u64 *src);
658 void StatOutput(u64 *stat);
659 #endif
661 void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
662 #if TSAN_COLLECT_STATS
663 thr->stat[typ] += n;
664 #endif
666 void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
667 #if TSAN_COLLECT_STATS
668 thr->stat[typ] = n;
669 #endif
672 void MapShadow(uptr addr, uptr size);
673 void MapThreadTrace(uptr addr, uptr size, const char *name);
674 void DontNeedShadowFor(uptr addr, uptr size);
675 void InitializeShadowMemory();
676 void InitializeInterceptors();
677 void InitializeLibIgnore();
678 void InitializeDynamicAnnotations();
680 void ForkBefore(ThreadState *thr, uptr pc);
681 void ForkParentAfter(ThreadState *thr, uptr pc);
682 void ForkChildAfter(ThreadState *thr, uptr pc);
684 void ReportRace(ThreadState *thr);
685 bool OutputReport(ThreadState *thr, const ScopedReport &srep);
686 bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
687 bool IsExpectedReport(uptr addr, uptr size);
688 void PrintMatchedBenignRaces();
690 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
691 # define DPrintf Printf
692 #else
693 # define DPrintf(...)
694 #endif
696 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
697 # define DPrintf2 Printf
698 #else
699 # define DPrintf2(...)
700 #endif
702 u32 CurrentStackId(ThreadState *thr, uptr pc);
703 ReportStack *SymbolizeStackId(u32 stack_id);
704 void PrintCurrentStack(ThreadState *thr, uptr pc);
705 void PrintCurrentStackSlow(uptr pc); // uses libunwind
707 void Initialize(ThreadState *thr);
708 void MaybeSpawnBackgroundThread();
709 int Finalize(ThreadState *thr);
711 void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
712 void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
714 void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
715 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
716 void MemoryAccessImpl(ThreadState *thr, uptr addr,
717 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
718 u64 *shadow_mem, Shadow cur);
719 void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
720 uptr size, bool is_write);
721 void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
722 uptr size, uptr step, bool is_write);
723 void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
724 int size, bool kAccessIsWrite, bool kIsAtomic);
726 const int kSizeLog1 = 0;
727 const int kSizeLog2 = 1;
728 const int kSizeLog4 = 2;
729 const int kSizeLog8 = 3;
731 void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
732 uptr addr, int kAccessSizeLog) {
733 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
736 void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
737 uptr addr, int kAccessSizeLog) {
738 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
741 void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
742 uptr addr, int kAccessSizeLog) {
743 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
746 void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
747 uptr addr, int kAccessSizeLog) {
748 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
751 void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
752 void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
753 void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
755 void ThreadIgnoreBegin(ThreadState *thr, uptr pc, bool save_stack = true);
756 void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
757 void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc, bool save_stack = true);
758 void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
760 void FuncEntry(ThreadState *thr, uptr pc);
761 void FuncExit(ThreadState *thr);
763 int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
764 void ThreadStart(ThreadState *thr, int tid, tid_t os_id, bool workerthread);
765 void ThreadFinish(ThreadState *thr);
766 int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
767 void ThreadJoin(ThreadState *thr, uptr pc, int tid);
768 void ThreadDetach(ThreadState *thr, uptr pc, int tid);
769 void ThreadFinalize(ThreadState *thr);
770 void ThreadSetName(ThreadState *thr, const char *name);
771 int ThreadCount(ThreadState *thr);
772 void ProcessPendingSignals(ThreadState *thr);
774 Processor *ProcCreate();
775 void ProcDestroy(Processor *proc);
776 void ProcWire(Processor *proc, ThreadState *thr);
777 void ProcUnwire(Processor *proc, ThreadState *thr);
779 // Note: the parameter is called flagz, because flags is already taken
780 // by the global function that returns flags.
781 void MutexCreate(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
782 void MutexDestroy(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
783 void MutexPreLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
784 void MutexPostLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0,
785 int rec = 1);
786 int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
787 void MutexPreReadLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
788 void MutexPostReadLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
789 void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
790 void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
791 void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
792 void MutexInvalidAccess(ThreadState *thr, uptr pc, uptr addr);
794 void Acquire(ThreadState *thr, uptr pc, uptr addr);
795 // AcquireGlobal synchronizes the current thread with all other threads.
796 // In terms of happens-before relation, it draws a HB edge from all threads
797 // (where they happen to execute right now) to the current thread. We use it to
798 // handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
799 // right before executing finalizers. This provides a coarse, but simple
800 // approximation of the actual required synchronization.
801 void AcquireGlobal(ThreadState *thr, uptr pc);
802 void Release(ThreadState *thr, uptr pc, uptr addr);
803 void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
804 void AfterSleep(ThreadState *thr, uptr pc);
805 void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
806 void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
807 void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
808 void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
810 // The hacky call uses custom calling convention and an assembly thunk.
811 // It is considerably faster that a normal call for the caller
812 // if it is not executed (it is intended for slow paths from hot functions).
813 // The trick is that the call preserves all registers and the compiler
814 // does not treat it as a call.
815 // If it does not work for you, use normal call.
816 #if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
817 // The caller may not create the stack frame for itself at all,
818 // so we create a reserve stack frame for it (1024b must be enough).
819 #define HACKY_CALL(f) \
820 __asm__ __volatile__("sub $1024, %%rsp;" \
821 CFI_INL_ADJUST_CFA_OFFSET(1024) \
822 ".hidden " #f "_thunk;" \
823 "call " #f "_thunk;" \
824 "add $1024, %%rsp;" \
825 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
826 ::: "memory", "cc");
827 #else
828 #define HACKY_CALL(f) f()
829 #endif
831 void TraceSwitch(ThreadState *thr);
832 uptr TraceTopPC(ThreadState *thr);
833 uptr TraceSize();
834 uptr TraceParts();
835 Trace *ThreadTrace(int tid);
837 extern "C" void __tsan_trace_switch();
838 void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
839 EventType typ, u64 addr) {
840 if (!kCollectHistory)
841 return;
842 DCHECK_GE((int)typ, 0);
843 DCHECK_LE((int)typ, 7);
844 DCHECK_EQ(GetLsb(addr, kEventPCBits), addr);
845 StatInc(thr, StatEvents);
846 u64 pos = fs.GetTracePos();
847 if (UNLIKELY((pos % kTracePartSize) == 0)) {
848 #if !SANITIZER_GO
849 HACKY_CALL(__tsan_trace_switch);
850 #else
851 TraceSwitch(thr);
852 #endif
854 Event *trace = (Event*)GetThreadTrace(fs.tid());
855 Event *evp = &trace[pos];
856 Event ev = (u64)addr | ((u64)typ << kEventPCBits);
857 *evp = ev;
860 #if !SANITIZER_GO
861 uptr ALWAYS_INLINE HeapEnd() {
862 return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
864 #endif
866 } // namespace __tsan
868 #endif // TSAN_RTL_H