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