1 //===-- tsan_rtl.h ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 // Main internal TSan header file.
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 (*).
23 // (*) Except when inlining is critical for performance.
24 //===----------------------------------------------------------------------===//
29 #include "sanitizer_common/sanitizer_allocator.h"
30 #include "sanitizer_common/sanitizer_allocator_internal.h"
31 #include "sanitizer_common/sanitizer_asm.h"
32 #include "sanitizer_common/sanitizer_common.h"
33 #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
34 #include "sanitizer_common/sanitizer_libignore.h"
35 #include "sanitizer_common/sanitizer_suppressions.h"
36 #include "sanitizer_common/sanitizer_thread_registry.h"
37 #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"
44 #include "tsan_platform.h"
45 #include "tsan_mutexset.h"
46 #include "tsan_ignoreset.h"
47 #include "tsan_stack_trace.h"
49 #if SANITIZER_WORDSIZE != 64
50 # error "ThreadSanitizer is supported only on 64-bit platforms"
56 struct MapUnmapCallback;
57 #if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__)
58 static const uptr kAllocatorSpace = 0;
59 static const uptr kAllocatorSize = SANITIZER_MMAP_RANGE_SIZE;
60 static const uptr kAllocatorRegionSizeLog = 20;
61 static const uptr kAllocatorNumRegions =
62 kAllocatorSize >> kAllocatorRegionSizeLog;
63 typedef TwoLevelByteMap<(kAllocatorNumRegions >> 12), 1 << 12,
64 MapUnmapCallback> ByteMap;
65 typedef SizeClassAllocator32<kAllocatorSpace, kAllocatorSize, 0,
66 CompactSizeClassMap, kAllocatorRegionSizeLog, ByteMap,
67 MapUnmapCallback> PrimaryAllocator;
69 typedef SizeClassAllocator64<Mapping::kHeapMemBeg,
70 Mapping::kHeapMemEnd - Mapping::kHeapMemBeg, 0,
71 DefaultSizeClassMap, MapUnmapCallback> PrimaryAllocator;
73 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
74 typedef LargeMmapAllocator<MapUnmapCallback> SecondaryAllocator;
75 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
76 SecondaryAllocator> Allocator;
77 Allocator *allocator();
80 void TsanCheckFailed(const char *file, int line, const char *cond,
83 const u64 kShadowRodata = (u64)-1; // .rodata shadow marker
85 // FastState (from most significant bit):
93 FastState(u64 tid, u64 epoch) {
94 x_ = tid << kTidShift;
96 DCHECK_EQ(tid, this->tid());
97 DCHECK_EQ(epoch, this->epoch());
98 DCHECK_EQ(GetIgnoreBit(), false);
101 explicit FastState(u64 x)
110 u64 res = (x_ & ~kIgnoreBit) >> kTidShift;
114 u64 TidWithIgnore() const {
115 u64 res = x_ >> kTidShift;
120 u64 res = x_ & ((1ull << kClkBits) - 1);
124 void IncrementEpoch() {
125 u64 old_epoch = epoch();
127 DCHECK_EQ(old_epoch + 1, epoch());
131 void SetIgnoreBit() { x_ |= kIgnoreBit; }
132 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
133 bool GetIgnoreBit() const { return (s64)x_ < 0; }
135 void SetHistorySize(int hs) {
138 x_ = (x_ & ~(kHistoryMask << kHistoryShift)) | (u64(hs) << kHistoryShift);
142 int GetHistorySize() const {
143 return (int)((x_ >> kHistoryShift) & kHistoryMask);
146 void ClearHistorySize() {
151 u64 GetTracePos() const {
152 const int hs = GetHistorySize();
153 // When hs == 0, the trace consists of 2 parts.
154 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
155 return epoch() & mask;
160 static const int kTidShift = 64 - kTidBits - 1;
161 static const u64 kIgnoreBit = 1ull << 63;
162 static const u64 kFreedBit = 1ull << 63;
163 static const u64 kHistoryShift = kClkBits;
164 static const u64 kHistoryMask = 7;
168 // Shadow (from most significant bit):
176 class Shadow : public FastState {
178 explicit Shadow(u64 x)
182 explicit Shadow(const FastState &s)
187 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
188 DCHECK_EQ((x_ >> kClkBits) & 31, 0);
190 DCHECK_LE(kAccessSizeLog, 3);
191 x_ |= ((kAccessSizeLog << 3) | addr0) << kClkBits;
192 DCHECK_EQ(kAccessSizeLog, size_log());
193 DCHECK_EQ(addr0, this->addr0());
196 void SetWrite(unsigned kAccessIsWrite) {
197 DCHECK_EQ(x_ & kReadBit, 0);
200 DCHECK_EQ(kAccessIsWrite, IsWrite());
203 void SetAtomic(bool kIsAtomic) {
207 DCHECK_EQ(IsAtomic(), kIsAtomic);
210 bool IsAtomic() const {
211 return x_ & kAtomicBit;
214 bool IsZero() const {
218 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
219 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
220 DCHECK_EQ(shifted_xor == 0, s1.TidWithIgnore() == s2.TidWithIgnore());
221 return shifted_xor == 0;
225 bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
226 u64 masked_xor = ((s1.x_ ^ s2.x_) >> kClkBits) & 31;
227 return masked_xor == 0;
230 static ALWAYS_INLINE bool TwoRangesIntersect(Shadow s1, Shadow s2,
231 unsigned kS2AccessSize) {
233 u64 diff = s1.addr0() - s2.addr0();
234 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
235 // if (s1.addr0() + size1) > s2.addr0()) return true;
236 if (s1.size() > -diff)
239 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
240 if (kS2AccessSize > diff)
243 DCHECK_EQ(res, TwoRangesIntersectSlow(s1, s2));
244 DCHECK_EQ(res, TwoRangesIntersectSlow(s2, s1));
248 u64 ALWAYS_INLINE addr0() const { return (x_ >> kClkBits) & 7; }
249 u64 ALWAYS_INLINE size() const { return 1ull << size_log(); }
250 bool ALWAYS_INLINE IsWrite() const { return !IsRead(); }
251 bool ALWAYS_INLINE IsRead() const { return x_ & kReadBit; }
253 // The idea behind the freed bit is as follows.
254 // When the memory is freed (or otherwise unaccessible) we write to the shadow
255 // values with tid/epoch related to the free and the freed bit set.
256 // During memory accesses processing the freed bit is considered
257 // as msb of tid. So any access races with shadow with freed bit set
258 // (it is as if write from a thread with which we never synchronized before).
259 // This allows us to detect accesses to freed memory w/o additional
260 // overheads in memory access processing and at the same time restore
261 // tid/epoch of free.
266 bool IsFreed() const {
267 return x_ & kFreedBit;
270 bool GetFreedAndReset() {
271 bool res = x_ & kFreedBit;
276 bool ALWAYS_INLINE IsBothReadsOrAtomic(bool kIsWrite, bool kIsAtomic) const {
277 bool v = x_ & ((u64(kIsWrite ^ 1) << kReadShift)
278 | (u64(kIsAtomic) << kAtomicShift));
279 DCHECK_EQ(v, (!IsWrite() && !kIsWrite) || (IsAtomic() && kIsAtomic));
283 bool ALWAYS_INLINE IsRWNotWeaker(bool kIsWrite, bool kIsAtomic) const {
284 bool v = ((x_ >> kReadShift) & 3)
285 <= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
286 DCHECK_EQ(v, (IsAtomic() < kIsAtomic) ||
287 (IsAtomic() == kIsAtomic && !IsWrite() <= !kIsWrite));
291 bool ALWAYS_INLINE IsRWWeakerOrEqual(bool kIsWrite, bool kIsAtomic) const {
292 bool v = ((x_ >> kReadShift) & 3)
293 >= u64((kIsWrite ^ 1) | (kIsAtomic << 1));
294 DCHECK_EQ(v, (IsAtomic() > kIsAtomic) ||
295 (IsAtomic() == kIsAtomic && !IsWrite() >= !kIsWrite));
300 static const u64 kReadShift = 5 + kClkBits;
301 static const u64 kReadBit = 1ull << kReadShift;
302 static const u64 kAtomicShift = 6 + kClkBits;
303 static const u64 kAtomicBit = 1ull << kAtomicShift;
305 u64 size_log() const { return (x_ >> (3 + kClkBits)) & 3; }
307 static bool TwoRangesIntersectSlow(const Shadow s1, const Shadow s2) {
308 if (s1.addr0() == s2.addr0()) return true;
309 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
311 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
317 struct ThreadSignalContext;
323 bool in_blocking_func;
324 uptr in_signal_handler;
325 uptr *shadow_stack_pos;
328 // This struct is stored in TLS.
330 FastState fast_state;
331 // Synch epoch represents the threads's epoch before the last synchronization
332 // action. It allows to reduce number of shadow state updates.
333 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
334 // if we are processing write to X from the same thread at epoch=200,
335 // we do nothing, because both writes happen in the same 'synch epoch'.
336 // That is, if another memory access does not race with the former write,
337 // it does not race with the latter as well.
338 // QUESTION: can we can squeeze this into ThreadState::Fast?
339 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
340 // taken by epoch between synchs.
341 // This way we can save one load from tls.
342 u64 fast_synch_epoch;
343 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
344 // We do not distinguish beteween ignoring reads and writes
345 // for better performance.
346 int ignore_reads_and_writes;
348 // Go does not support ignores.
350 IgnoreSet mop_ignore_set;
351 IgnoreSet sync_ignore_set;
353 // C/C++ uses fixed size shadow stack embed into Trace.
354 // Go uses malloc-allocated shadow stack with dynamic size.
356 uptr *shadow_stack_end;
357 uptr *shadow_stack_pos;
358 u64 *racy_shadow_addr;
363 AllocatorCache alloc_cache;
364 InternalAllocatorCache internal_alloc_cache;
365 Vector<JmpBuf> jmp_bufs;
366 int ignore_interceptors;
368 #if TSAN_COLLECT_STATS
385 #if SANITIZER_DEBUG && !SANITIZER_GO
386 InternalDeadlockDetector internal_deadlock_detector;
388 DDPhysicalThread *dd_pt;
389 DDLogicalThread *dd_lt;
391 atomic_uintptr_t in_signal_handler;
392 ThreadSignalContext *signal_ctx;
394 DenseSlabAllocCache block_cache;
395 DenseSlabAllocCache sync_cache;
396 DenseSlabAllocCache clock_cache;
399 u32 last_sleep_stack_id;
400 ThreadClock last_sleep_clock;
403 // Set in regions of runtime that must be signal-safe and fork-safe.
404 // If set, malloc must not be called.
407 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
408 unsigned reuse_count,
409 uptr stk_addr, uptr stk_size,
410 uptr tls_addr, uptr tls_size);
415 ThreadState *cur_thread();
416 void cur_thread_finalize();
418 __attribute__((tls_model("initial-exec")))
419 extern THREADLOCAL char cur_thread_placeholder[];
420 INLINE ThreadState *cur_thread() {
421 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
423 INLINE void cur_thread_finalize() { }
424 #endif // SANITIZER_MAC
425 #endif // SANITIZER_GO
427 class ThreadContext : public ThreadContextBase {
429 explicit ThreadContext(int tid);
432 u32 creation_stack_id;
434 // Epoch at which the thread had started.
435 // If we see an event from the thread stamped by an older epoch,
436 // the event is from a dead thread that shared tid with this thread.
440 // Override superclass callbacks.
441 void OnDead() override;
442 void OnJoined(void *arg) override;
443 void OnFinished() override;
444 void OnStarted(void *arg) override;
445 void OnCreated(void *arg) override;
446 void OnReset() override;
447 void OnDetached(void *arg) override;
452 bool operator==(const RacyStacks &other) const {
453 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
455 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
466 struct FiredSuppression {
476 bool after_multithreaded_fork;
482 int nmissed_expected;
483 atomic_uint64_t last_symbolize_time_ns;
485 void *background_thread;
486 atomic_uint32_t stop_background_thread;
488 ThreadRegistry *thread_registry;
491 Vector<RacyStacks> racy_stacks;
492 Vector<RacyAddress> racy_addresses;
493 // Number of fired suppressions may be large enough.
494 Mutex fired_suppressions_mtx;
495 InternalMmapVector<FiredSuppression> fired_suppressions;
498 ClockAlloc clock_alloc;
503 u64 int_alloc_cnt[MBlockTypeCount];
504 u64 int_alloc_siz[MBlockTypeCount];
507 extern Context *ctx; // The one and the only global runtime context.
509 struct ScopedIgnoreInterceptors {
510 ScopedIgnoreInterceptors() {
512 cur_thread()->ignore_interceptors++;
516 ~ScopedIgnoreInterceptors() {
518 cur_thread()->ignore_interceptors--;
525 explicit ScopedReport(ReportType typ);
528 void AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
529 const MutexSet *mset);
530 void AddStack(StackTrace stack, bool suppressable = false);
531 void AddThread(const ThreadContext *tctx, bool suppressable = false);
532 void AddThread(int unique_tid, bool suppressable = false);
533 void AddUniqueTid(int unique_tid);
534 void AddMutex(const SyncVar *s);
535 u64 AddMutex(u64 id);
536 void AddLocation(uptr addr, uptr size);
537 void AddSleep(u32 stack_id);
538 void SetCount(int count);
540 const ReportDesc *GetReport() const;
544 // Symbolizer makes lots of intercepted calls. If we try to process them,
545 // at best it will cause deadlocks on internal mutexes.
546 ScopedIgnoreInterceptors ignore_interceptors_;
548 void AddDeadMutex(u64 id);
550 ScopedReport(const ScopedReport&);
551 void operator = (const ScopedReport&);
554 void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
557 template<typename StackTraceTy>
558 void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack) {
559 uptr size = thr->shadow_stack_pos - thr->shadow_stack;
561 if (size + !!toppc > kStackTraceMax) {
562 start = size + !!toppc - kStackTraceMax;
563 size = kStackTraceMax - !!toppc;
565 stack->Init(&thr->shadow_stack[start], size, toppc);
569 #if TSAN_COLLECT_STATS
570 void StatAggregate(u64 *dst, u64 *src);
571 void StatOutput(u64 *stat);
574 void ALWAYS_INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
575 #if TSAN_COLLECT_STATS
579 void ALWAYS_INLINE StatSet(ThreadState *thr, StatType typ, u64 n) {
580 #if TSAN_COLLECT_STATS
585 void MapShadow(uptr addr, uptr size);
586 void MapThreadTrace(uptr addr, uptr size, const char *name);
587 void DontNeedShadowFor(uptr addr, uptr size);
588 void InitializeShadowMemory();
589 void InitializeInterceptors();
590 void InitializeLibIgnore();
591 void InitializeDynamicAnnotations();
593 void ForkBefore(ThreadState *thr, uptr pc);
594 void ForkParentAfter(ThreadState *thr, uptr pc);
595 void ForkChildAfter(ThreadState *thr, uptr pc);
597 void ReportRace(ThreadState *thr);
598 bool OutputReport(ThreadState *thr, const ScopedReport &srep);
599 bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
600 bool IsExpectedReport(uptr addr, uptr size);
601 void PrintMatchedBenignRaces();
603 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
604 # define DPrintf Printf
606 # define DPrintf(...)
609 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
610 # define DPrintf2 Printf
612 # define DPrintf2(...)
615 u32 CurrentStackId(ThreadState *thr, uptr pc);
616 ReportStack *SymbolizeStackId(u32 stack_id);
617 void PrintCurrentStack(ThreadState *thr, uptr pc);
618 void PrintCurrentStackSlow(uptr pc); // uses libunwind
620 void Initialize(ThreadState *thr);
621 int Finalize(ThreadState *thr);
623 void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
624 void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
626 void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
627 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
628 void MemoryAccessImpl(ThreadState *thr, uptr addr,
629 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
630 u64 *shadow_mem, Shadow cur);
631 void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
632 uptr size, bool is_write);
633 void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
634 uptr size, uptr step, bool is_write);
635 void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
636 int size, bool kAccessIsWrite, bool kIsAtomic);
638 const int kSizeLog1 = 0;
639 const int kSizeLog2 = 1;
640 const int kSizeLog4 = 2;
641 const int kSizeLog8 = 3;
643 void ALWAYS_INLINE MemoryRead(ThreadState *thr, uptr pc,
644 uptr addr, int kAccessSizeLog) {
645 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, false);
648 void ALWAYS_INLINE MemoryWrite(ThreadState *thr, uptr pc,
649 uptr addr, int kAccessSizeLog) {
650 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, false);
653 void ALWAYS_INLINE MemoryReadAtomic(ThreadState *thr, uptr pc,
654 uptr addr, int kAccessSizeLog) {
655 MemoryAccess(thr, pc, addr, kAccessSizeLog, false, true);
658 void ALWAYS_INLINE MemoryWriteAtomic(ThreadState *thr, uptr pc,
659 uptr addr, int kAccessSizeLog) {
660 MemoryAccess(thr, pc, addr, kAccessSizeLog, true, true);
663 void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
664 void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
665 void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
667 void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
668 void ThreadIgnoreEnd(ThreadState *thr, uptr pc);
669 void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
670 void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc);
672 void FuncEntry(ThreadState *thr, uptr pc);
673 void FuncExit(ThreadState *thr);
675 int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
676 void ThreadStart(ThreadState *thr, int tid, uptr os_id);
677 void ThreadFinish(ThreadState *thr);
678 int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
679 void ThreadJoin(ThreadState *thr, uptr pc, int tid);
680 void ThreadDetach(ThreadState *thr, uptr pc, int tid);
681 void ThreadFinalize(ThreadState *thr);
682 void ThreadSetName(ThreadState *thr, const char *name);
683 int ThreadCount(ThreadState *thr);
684 void ProcessPendingSignals(ThreadState *thr);
686 void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
687 bool rw, bool recursive, bool linker_init);
688 void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
689 void MutexLock(ThreadState *thr, uptr pc, uptr addr, int rec = 1,
690 bool try_lock = false);
691 int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, bool all = false);
692 void MutexReadLock(ThreadState *thr, uptr pc, uptr addr, bool try_lock = false);
693 void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
694 void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
695 void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
697 void Acquire(ThreadState *thr, uptr pc, uptr addr);
698 // AcquireGlobal synchronizes the current thread with all other threads.
699 // In terms of happens-before relation, it draws a HB edge from all threads
700 // (where they happen to execute right now) to the current thread. We use it to
701 // handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
702 // right before executing finalizers. This provides a coarse, but simple
703 // approximation of the actual required synchronization.
704 void AcquireGlobal(ThreadState *thr, uptr pc);
705 void Release(ThreadState *thr, uptr pc, uptr addr);
706 void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
707 void AfterSleep(ThreadState *thr, uptr pc);
708 void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
709 void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
710 void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
711 void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
713 // The hacky call uses custom calling convention and an assembly thunk.
714 // It is considerably faster that a normal call for the caller
715 // if it is not executed (it is intended for slow paths from hot functions).
716 // The trick is that the call preserves all registers and the compiler
717 // does not treat it as a call.
718 // If it does not work for you, use normal call.
719 #if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
720 // The caller may not create the stack frame for itself at all,
721 // so we create a reserve stack frame for it (1024b must be enough).
722 #define HACKY_CALL(f) \
723 __asm__ __volatile__("sub $1024, %%rsp;" \
724 CFI_INL_ADJUST_CFA_OFFSET(1024) \
725 ".hidden " #f "_thunk;" \
726 "call " #f "_thunk;" \
727 "add $1024, %%rsp;" \
728 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
731 #define HACKY_CALL(f) f()
734 void TraceSwitch(ThreadState *thr);
735 uptr TraceTopPC(ThreadState *thr);
738 Trace *ThreadTrace(int tid);
740 extern "C" void __tsan_trace_switch();
741 void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
742 EventType typ, u64 addr) {
743 if (!kCollectHistory)
745 DCHECK_GE((int)typ, 0);
746 DCHECK_LE((int)typ, 7);
747 DCHECK_EQ(GetLsb(addr, 61), addr);
748 StatInc(thr, StatEvents);
749 u64 pos = fs.GetTracePos();
750 if (UNLIKELY((pos % kTracePartSize) == 0)) {
752 HACKY_CALL(__tsan_trace_switch);
757 Event *trace = (Event*)GetThreadTrace(fs.tid());
758 Event *evp = &trace[pos];
759 Event ev = (u64)addr | ((u64)typ << 61);
764 uptr ALWAYS_INLINE HeapEnd() {
765 return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
769 } // namespace __tsan