tsan_rtl.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. //===-- tsan_rtl.h ----------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. // Main internal TSan header file.
  12. //
  13. // Ground rules:
  14. // - C++ run-time should not be used (static CTORs, RTTI, exceptions, static
  15. // function-scope locals)
  16. // - All functions/classes/etc reside in namespace __tsan, except for those
  17. // declared in tsan_interface.h.
  18. // - Platform-specific files should be used instead of ifdefs (*).
  19. // - No system headers included in header files (*).
  20. // - Platform specific headres included only into platform-specific files (*).
  21. //
  22. // (*) Except when inlining is critical for performance.
  23. //===----------------------------------------------------------------------===//
  24. #ifndef TSAN_RTL_H
  25. #define TSAN_RTL_H
  26. #include "sanitizer_common/sanitizer_allocator.h"
  27. #include "sanitizer_common/sanitizer_allocator_internal.h"
  28. #include "sanitizer_common/sanitizer_asm.h"
  29. #include "sanitizer_common/sanitizer_common.h"
  30. #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
  31. #include "sanitizer_common/sanitizer_libignore.h"
  32. #include "sanitizer_common/sanitizer_suppressions.h"
  33. #include "sanitizer_common/sanitizer_thread_registry.h"
  34. #include "sanitizer_common/sanitizer_vector.h"
  35. #include "tsan_clock.h"
  36. #include "tsan_defs.h"
  37. #include "tsan_flags.h"
  38. #include "tsan_ignoreset.h"
  39. #include "tsan_mman.h"
  40. #include "tsan_mutexset.h"
  41. #include "tsan_platform.h"
  42. #include "tsan_report.h"
  43. #include "tsan_shadow.h"
  44. #include "tsan_stack_trace.h"
  45. #include "tsan_sync.h"
  46. #include "tsan_trace.h"
  47. #if SANITIZER_WORDSIZE != 64
  48. # error "ThreadSanitizer is supported only on 64-bit platforms"
  49. #endif
  50. namespace __tsan {
  51. #if !SANITIZER_GO
  52. struct MapUnmapCallback;
  53. #if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__)
  54. struct AP32 {
  55. static const uptr kSpaceBeg = 0;
  56. static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
  57. static const uptr kMetadataSize = 0;
  58. typedef __sanitizer::CompactSizeClassMap SizeClassMap;
  59. static const uptr kRegionSizeLog = 20;
  60. using AddressSpaceView = LocalAddressSpaceView;
  61. typedef __tsan::MapUnmapCallback MapUnmapCallback;
  62. static const uptr kFlags = 0;
  63. };
  64. typedef SizeClassAllocator32<AP32> PrimaryAllocator;
  65. #else
  66. struct AP64 { // Allocator64 parameters. Deliberately using a short name.
  67. # if defined(__s390x__)
  68. typedef MappingS390x Mapping;
  69. # else
  70. typedef Mapping48AddressSpace Mapping;
  71. # endif
  72. static const uptr kSpaceBeg = Mapping::kHeapMemBeg;
  73. static const uptr kSpaceSize = Mapping::kHeapMemEnd - Mapping::kHeapMemBeg;
  74. static const uptr kMetadataSize = 0;
  75. typedef DefaultSizeClassMap SizeClassMap;
  76. typedef __tsan::MapUnmapCallback MapUnmapCallback;
  77. static const uptr kFlags = 0;
  78. using AddressSpaceView = LocalAddressSpaceView;
  79. };
  80. typedef SizeClassAllocator64<AP64> PrimaryAllocator;
  81. #endif
  82. typedef CombinedAllocator<PrimaryAllocator> Allocator;
  83. typedef Allocator::AllocatorCache AllocatorCache;
  84. Allocator *allocator();
  85. #endif
  86. struct ThreadSignalContext;
  87. struct JmpBuf {
  88. uptr sp;
  89. int int_signal_send;
  90. bool in_blocking_func;
  91. uptr in_signal_handler;
  92. uptr *shadow_stack_pos;
  93. };
  94. // A Processor represents a physical thread, or a P for Go.
  95. // It is used to store internal resources like allocate cache, and does not
  96. // participate in race-detection logic (invisible to end user).
  97. // In C++ it is tied to an OS thread just like ThreadState, however ideally
  98. // it should be tied to a CPU (this way we will have fewer allocator caches).
  99. // In Go it is tied to a P, so there are significantly fewer Processor's than
  100. // ThreadState's (which are tied to Gs).
  101. // A ThreadState must be wired with a Processor to handle events.
  102. struct Processor {
  103. ThreadState *thr; // currently wired thread, or nullptr
  104. #if !SANITIZER_GO
  105. AllocatorCache alloc_cache;
  106. InternalAllocatorCache internal_alloc_cache;
  107. #endif
  108. DenseSlabAllocCache block_cache;
  109. DenseSlabAllocCache sync_cache;
  110. DenseSlabAllocCache clock_cache;
  111. DDPhysicalThread *dd_pt;
  112. };
  113. #if !SANITIZER_GO
  114. // ScopedGlobalProcessor temporary setups a global processor for the current
  115. // thread, if it does not have one. Intended for interceptors that can run
  116. // at the very thread end, when we already destroyed the thread processor.
  117. struct ScopedGlobalProcessor {
  118. ScopedGlobalProcessor();
  119. ~ScopedGlobalProcessor();
  120. };
  121. #endif
  122. // This struct is stored in TLS.
  123. struct ThreadState {
  124. FastState fast_state;
  125. // Synch epoch represents the threads's epoch before the last synchronization
  126. // action. It allows to reduce number of shadow state updates.
  127. // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
  128. // if we are processing write to X from the same thread at epoch=200,
  129. // we do nothing, because both writes happen in the same 'synch epoch'.
  130. // That is, if another memory access does not race with the former write,
  131. // it does not race with the latter as well.
  132. // QUESTION: can we can squeeze this into ThreadState::Fast?
  133. // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
  134. // taken by epoch between synchs.
  135. // This way we can save one load from tls.
  136. u64 fast_synch_epoch;
  137. // Technically `current` should be a separate THREADLOCAL variable;
  138. // but it is placed here in order to share cache line with previous fields.
  139. ThreadState* current;
  140. // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
  141. // We do not distinguish beteween ignoring reads and writes
  142. // for better performance.
  143. int ignore_reads_and_writes;
  144. atomic_sint32_t pending_signals;
  145. int ignore_sync;
  146. int suppress_reports;
  147. // Go does not support ignores.
  148. #if !SANITIZER_GO
  149. IgnoreSet mop_ignore_set;
  150. IgnoreSet sync_ignore_set;
  151. // C/C++ uses fixed size shadow stack.
  152. uptr shadow_stack[kShadowStackSize];
  153. #else
  154. // Go uses malloc-allocated shadow stack with dynamic size.
  155. uptr *shadow_stack;
  156. #endif
  157. uptr *shadow_stack_end;
  158. uptr *shadow_stack_pos;
  159. RawShadow *racy_shadow_addr;
  160. RawShadow racy_state[2];
  161. MutexSet mset;
  162. ThreadClock clock;
  163. #if !SANITIZER_GO
  164. Vector<JmpBuf> jmp_bufs;
  165. int ignore_interceptors;
  166. #endif
  167. const Tid tid;
  168. const int unique_id;
  169. bool in_symbolizer;
  170. bool in_ignored_lib;
  171. bool is_inited;
  172. bool is_dead;
  173. bool is_freeing;
  174. bool is_vptr_access;
  175. const uptr stk_addr;
  176. const uptr stk_size;
  177. const uptr tls_addr;
  178. const uptr tls_size;
  179. ThreadContext *tctx;
  180. DDLogicalThread *dd_lt;
  181. // Current wired Processor, or nullptr. Required to handle any events.
  182. Processor *proc1;
  183. #if !SANITIZER_GO
  184. Processor *proc() { return proc1; }
  185. #else
  186. Processor *proc();
  187. #endif
  188. atomic_uintptr_t in_signal_handler;
  189. ThreadSignalContext *signal_ctx;
  190. #if !SANITIZER_GO
  191. StackID last_sleep_stack_id;
  192. ThreadClock last_sleep_clock;
  193. #endif
  194. // Set in regions of runtime that must be signal-safe and fork-safe.
  195. // If set, malloc must not be called.
  196. int nomalloc;
  197. const ReportDesc *current_report;
  198. // Current position in tctx->trace.Back()->events (Event*).
  199. atomic_uintptr_t trace_pos;
  200. // PC of the last memory access, used to compute PC deltas in the trace.
  201. uptr trace_prev_pc;
  202. Sid sid;
  203. Epoch epoch;
  204. explicit ThreadState(Context *ctx, Tid tid, int unique_id, u64 epoch,
  205. unsigned reuse_count, uptr stk_addr, uptr stk_size,
  206. uptr tls_addr, uptr tls_size);
  207. } ALIGNED(SANITIZER_CACHE_LINE_SIZE);
  208. #if !SANITIZER_GO
  209. #if SANITIZER_MAC || SANITIZER_ANDROID
  210. ThreadState *cur_thread();
  211. void set_cur_thread(ThreadState *thr);
  212. void cur_thread_finalize();
  213. inline ThreadState *cur_thread_init() { return cur_thread(); }
  214. # else
  215. __attribute__((tls_model("initial-exec")))
  216. extern THREADLOCAL char cur_thread_placeholder[];
  217. inline ThreadState *cur_thread() {
  218. return reinterpret_cast<ThreadState *>(cur_thread_placeholder)->current;
  219. }
  220. inline ThreadState *cur_thread_init() {
  221. ThreadState *thr = reinterpret_cast<ThreadState *>(cur_thread_placeholder);
  222. if (UNLIKELY(!thr->current))
  223. thr->current = thr;
  224. return thr->current;
  225. }
  226. inline void set_cur_thread(ThreadState *thr) {
  227. reinterpret_cast<ThreadState *>(cur_thread_placeholder)->current = thr;
  228. }
  229. inline void cur_thread_finalize() { }
  230. # endif // SANITIZER_MAC || SANITIZER_ANDROID
  231. #endif // SANITIZER_GO
  232. class ThreadContext final : public ThreadContextBase {
  233. public:
  234. explicit ThreadContext(Tid tid);
  235. ~ThreadContext();
  236. ThreadState *thr;
  237. StackID creation_stack_id;
  238. SyncClock sync;
  239. // Epoch at which the thread had started.
  240. // If we see an event from the thread stamped by an older epoch,
  241. // the event is from a dead thread that shared tid with this thread.
  242. u64 epoch0;
  243. u64 epoch1;
  244. v3::Trace trace;
  245. // Override superclass callbacks.
  246. void OnDead() override;
  247. void OnJoined(void *arg) override;
  248. void OnFinished() override;
  249. void OnStarted(void *arg) override;
  250. void OnCreated(void *arg) override;
  251. void OnReset() override;
  252. void OnDetached(void *arg) override;
  253. };
  254. struct RacyStacks {
  255. MD5Hash hash[2];
  256. bool operator==(const RacyStacks &other) const;
  257. };
  258. struct RacyAddress {
  259. uptr addr_min;
  260. uptr addr_max;
  261. };
  262. struct FiredSuppression {
  263. ReportType type;
  264. uptr pc_or_addr;
  265. Suppression *supp;
  266. };
  267. struct Context {
  268. Context();
  269. bool initialized;
  270. #if !SANITIZER_GO
  271. bool after_multithreaded_fork;
  272. #endif
  273. MetaMap metamap;
  274. Mutex report_mtx;
  275. int nreported;
  276. atomic_uint64_t last_symbolize_time_ns;
  277. void *background_thread;
  278. atomic_uint32_t stop_background_thread;
  279. ThreadRegistry thread_registry;
  280. Mutex racy_mtx;
  281. Vector<RacyStacks> racy_stacks;
  282. Vector<RacyAddress> racy_addresses;
  283. // Number of fired suppressions may be large enough.
  284. Mutex fired_suppressions_mtx;
  285. InternalMmapVector<FiredSuppression> fired_suppressions;
  286. DDetector *dd;
  287. ClockAlloc clock_alloc;
  288. Flags flags;
  289. fd_t memprof_fd;
  290. Mutex slot_mtx;
  291. };
  292. extern Context *ctx; // The one and the only global runtime context.
  293. ALWAYS_INLINE Flags *flags() {
  294. return &ctx->flags;
  295. }
  296. struct ScopedIgnoreInterceptors {
  297. ScopedIgnoreInterceptors() {
  298. #if !SANITIZER_GO
  299. cur_thread()->ignore_interceptors++;
  300. #endif
  301. }
  302. ~ScopedIgnoreInterceptors() {
  303. #if !SANITIZER_GO
  304. cur_thread()->ignore_interceptors--;
  305. #endif
  306. }
  307. };
  308. const char *GetObjectTypeFromTag(uptr tag);
  309. const char *GetReportHeaderFromTag(uptr tag);
  310. uptr TagFromShadowStackFrame(uptr pc);
  311. class ScopedReportBase {
  312. public:
  313. void AddMemoryAccess(uptr addr, uptr external_tag, Shadow s, StackTrace stack,
  314. const MutexSet *mset);
  315. void AddStack(StackTrace stack, bool suppressable = false);
  316. void AddThread(const ThreadContext *tctx, bool suppressable = false);
  317. void AddThread(Tid unique_tid, bool suppressable = false);
  318. void AddUniqueTid(Tid unique_tid);
  319. void AddMutex(const SyncVar *s);
  320. u64 AddMutex(u64 id);
  321. void AddLocation(uptr addr, uptr size);
  322. void AddSleep(StackID stack_id);
  323. void SetCount(int count);
  324. const ReportDesc *GetReport() const;
  325. protected:
  326. ScopedReportBase(ReportType typ, uptr tag);
  327. ~ScopedReportBase();
  328. private:
  329. ReportDesc *rep_;
  330. // Symbolizer makes lots of intercepted calls. If we try to process them,
  331. // at best it will cause deadlocks on internal mutexes.
  332. ScopedIgnoreInterceptors ignore_interceptors_;
  333. void AddDeadMutex(u64 id);
  334. ScopedReportBase(const ScopedReportBase &) = delete;
  335. void operator=(const ScopedReportBase &) = delete;
  336. };
  337. class ScopedReport : public ScopedReportBase {
  338. public:
  339. explicit ScopedReport(ReportType typ, uptr tag = kExternalTagNone);
  340. ~ScopedReport();
  341. private:
  342. ScopedErrorReportLock lock_;
  343. };
  344. bool ShouldReport(ThreadState *thr, ReportType typ);
  345. ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack);
  346. void RestoreStack(Tid tid, const u64 epoch, VarSizeStackTrace *stk,
  347. MutexSet *mset, uptr *tag = nullptr);
  348. // The stack could look like:
  349. // <start> | <main> | <foo> | tag | <bar>
  350. // This will extract the tag and keep:
  351. // <start> | <main> | <foo> | <bar>
  352. template<typename StackTraceTy>
  353. void ExtractTagFromStack(StackTraceTy *stack, uptr *tag = nullptr) {
  354. if (stack->size < 2) return;
  355. uptr possible_tag_pc = stack->trace[stack->size - 2];
  356. uptr possible_tag = TagFromShadowStackFrame(possible_tag_pc);
  357. if (possible_tag == kExternalTagNone) return;
  358. stack->trace_buffer[stack->size - 2] = stack->trace_buffer[stack->size - 1];
  359. stack->size -= 1;
  360. if (tag) *tag = possible_tag;
  361. }
  362. template<typename StackTraceTy>
  363. void ObtainCurrentStack(ThreadState *thr, uptr toppc, StackTraceTy *stack,
  364. uptr *tag = nullptr) {
  365. uptr size = thr->shadow_stack_pos - thr->shadow_stack;
  366. uptr start = 0;
  367. if (size + !!toppc > kStackTraceMax) {
  368. start = size + !!toppc - kStackTraceMax;
  369. size = kStackTraceMax - !!toppc;
  370. }
  371. stack->Init(&thr->shadow_stack[start], size, toppc);
  372. ExtractTagFromStack(stack, tag);
  373. }
  374. #define GET_STACK_TRACE_FATAL(thr, pc) \
  375. VarSizeStackTrace stack; \
  376. ObtainCurrentStack(thr, pc, &stack); \
  377. stack.ReverseOrder();
  378. void MapShadow(uptr addr, uptr size);
  379. void MapThreadTrace(uptr addr, uptr size, const char *name);
  380. void DontNeedShadowFor(uptr addr, uptr size);
  381. void UnmapShadow(ThreadState *thr, uptr addr, uptr size);
  382. void InitializeShadowMemory();
  383. void InitializeInterceptors();
  384. void InitializeLibIgnore();
  385. void InitializeDynamicAnnotations();
  386. void ForkBefore(ThreadState *thr, uptr pc);
  387. void ForkParentAfter(ThreadState *thr, uptr pc);
  388. void ForkChildAfter(ThreadState *thr, uptr pc, bool start_thread);
  389. void ReportRace(ThreadState *thr);
  390. bool OutputReport(ThreadState *thr, const ScopedReport &srep);
  391. bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
  392. bool IsExpectedReport(uptr addr, uptr size);
  393. #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
  394. # define DPrintf Printf
  395. #else
  396. # define DPrintf(...)
  397. #endif
  398. #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
  399. # define DPrintf2 Printf
  400. #else
  401. # define DPrintf2(...)
  402. #endif
  403. StackID CurrentStackId(ThreadState *thr, uptr pc);
  404. ReportStack *SymbolizeStackId(StackID stack_id);
  405. void PrintCurrentStack(ThreadState *thr, uptr pc);
  406. void PrintCurrentStackSlow(uptr pc); // uses libunwind
  407. MBlock *JavaHeapBlock(uptr addr, uptr *start);
  408. void Initialize(ThreadState *thr);
  409. void MaybeSpawnBackgroundThread();
  410. int Finalize(ThreadState *thr);
  411. void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write);
  412. void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write);
  413. void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
  414. int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic);
  415. void MemoryAccessImpl(ThreadState *thr, uptr addr,
  416. int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
  417. u64 *shadow_mem, Shadow cur);
  418. void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
  419. uptr size, bool is_write);
  420. void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr, uptr size,
  421. AccessType typ);
  422. const int kSizeLog1 = 0;
  423. const int kSizeLog2 = 1;
  424. const int kSizeLog4 = 2;
  425. const int kSizeLog8 = 3;
  426. ALWAYS_INLINE
  427. void MemoryAccess(ThreadState *thr, uptr pc, uptr addr, uptr size,
  428. AccessType typ) {
  429. int size_log;
  430. switch (size) {
  431. case 1:
  432. size_log = kSizeLog1;
  433. break;
  434. case 2:
  435. size_log = kSizeLog2;
  436. break;
  437. case 4:
  438. size_log = kSizeLog4;
  439. break;
  440. default:
  441. DCHECK_EQ(size, 8);
  442. size_log = kSizeLog8;
  443. break;
  444. }
  445. bool is_write = !(typ & kAccessRead);
  446. bool is_atomic = typ & kAccessAtomic;
  447. if (typ & kAccessVptr)
  448. thr->is_vptr_access = true;
  449. if (typ & kAccessFree)
  450. thr->is_freeing = true;
  451. MemoryAccess(thr, pc, addr, size_log, is_write, is_atomic);
  452. if (typ & kAccessVptr)
  453. thr->is_vptr_access = false;
  454. if (typ & kAccessFree)
  455. thr->is_freeing = false;
  456. }
  457. void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
  458. void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
  459. void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
  460. void MemoryRangeImitateWriteOrResetRange(ThreadState *thr, uptr pc, uptr addr,
  461. uptr size);
  462. void ThreadIgnoreBegin(ThreadState *thr, uptr pc);
  463. void ThreadIgnoreEnd(ThreadState *thr);
  464. void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc);
  465. void ThreadIgnoreSyncEnd(ThreadState *thr);
  466. void FuncEntry(ThreadState *thr, uptr pc);
  467. void FuncExit(ThreadState *thr);
  468. Tid ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
  469. void ThreadStart(ThreadState *thr, Tid tid, tid_t os_id,
  470. ThreadType thread_type);
  471. void ThreadFinish(ThreadState *thr);
  472. Tid ThreadConsumeTid(ThreadState *thr, uptr pc, uptr uid);
  473. void ThreadJoin(ThreadState *thr, uptr pc, Tid tid);
  474. void ThreadDetach(ThreadState *thr, uptr pc, Tid tid);
  475. void ThreadFinalize(ThreadState *thr);
  476. void ThreadSetName(ThreadState *thr, const char *name);
  477. int ThreadCount(ThreadState *thr);
  478. void ProcessPendingSignalsImpl(ThreadState *thr);
  479. void ThreadNotJoined(ThreadState *thr, uptr pc, Tid tid, uptr uid);
  480. Processor *ProcCreate();
  481. void ProcDestroy(Processor *proc);
  482. void ProcWire(Processor *proc, ThreadState *thr);
  483. void ProcUnwire(Processor *proc, ThreadState *thr);
  484. // Note: the parameter is called flagz, because flags is already taken
  485. // by the global function that returns flags.
  486. void MutexCreate(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
  487. void MutexDestroy(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
  488. void MutexPreLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
  489. void MutexPostLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0,
  490. int rec = 1);
  491. int MutexUnlock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
  492. void MutexPreReadLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
  493. void MutexPostReadLock(ThreadState *thr, uptr pc, uptr addr, u32 flagz = 0);
  494. void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
  495. void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
  496. void MutexRepair(ThreadState *thr, uptr pc, uptr addr); // call on EOWNERDEAD
  497. void MutexInvalidAccess(ThreadState *thr, uptr pc, uptr addr);
  498. void Acquire(ThreadState *thr, uptr pc, uptr addr);
  499. // AcquireGlobal synchronizes the current thread with all other threads.
  500. // In terms of happens-before relation, it draws a HB edge from all threads
  501. // (where they happen to execute right now) to the current thread. We use it to
  502. // handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
  503. // right before executing finalizers. This provides a coarse, but simple
  504. // approximation of the actual required synchronization.
  505. void AcquireGlobal(ThreadState *thr);
  506. void Release(ThreadState *thr, uptr pc, uptr addr);
  507. void ReleaseStoreAcquire(ThreadState *thr, uptr pc, uptr addr);
  508. void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
  509. void AfterSleep(ThreadState *thr, uptr pc);
  510. void AcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
  511. void ReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
  512. void ReleaseStoreAcquireImpl(ThreadState *thr, uptr pc, SyncClock *c);
  513. void ReleaseStoreImpl(ThreadState *thr, uptr pc, SyncClock *c);
  514. void AcquireReleaseImpl(ThreadState *thr, uptr pc, SyncClock *c);
  515. // The hacky call uses custom calling convention and an assembly thunk.
  516. // It is considerably faster that a normal call for the caller
  517. // if it is not executed (it is intended for slow paths from hot functions).
  518. // The trick is that the call preserves all registers and the compiler
  519. // does not treat it as a call.
  520. // If it does not work for you, use normal call.
  521. #if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
  522. // The caller may not create the stack frame for itself at all,
  523. // so we create a reserve stack frame for it (1024b must be enough).
  524. #define HACKY_CALL(f) \
  525. __asm__ __volatile__("sub $1024, %%rsp;" \
  526. CFI_INL_ADJUST_CFA_OFFSET(1024) \
  527. ".hidden " #f "_thunk;" \
  528. "call " #f "_thunk;" \
  529. "add $1024, %%rsp;" \
  530. CFI_INL_ADJUST_CFA_OFFSET(-1024) \
  531. ::: "memory", "cc");
  532. #else
  533. #define HACKY_CALL(f) f()
  534. #endif
  535. void TraceSwitch(ThreadState *thr);
  536. uptr TraceTopPC(ThreadState *thr);
  537. uptr TraceSize();
  538. uptr TraceParts();
  539. Trace *ThreadTrace(Tid tid);
  540. extern "C" void __tsan_trace_switch();
  541. void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
  542. EventType typ, u64 addr) {
  543. if (!kCollectHistory)
  544. return;
  545. DCHECK_GE((int)typ, 0);
  546. DCHECK_LE((int)typ, 7);
  547. DCHECK_EQ(GetLsb(addr, kEventPCBits), addr);
  548. u64 pos = fs.GetTracePos();
  549. if (UNLIKELY((pos % kTracePartSize) == 0)) {
  550. #if !SANITIZER_GO
  551. HACKY_CALL(__tsan_trace_switch);
  552. #else
  553. TraceSwitch(thr);
  554. #endif
  555. }
  556. Event *trace = (Event*)GetThreadTrace(fs.tid());
  557. Event *evp = &trace[pos];
  558. Event ev = (u64)addr | ((u64)typ << kEventPCBits);
  559. *evp = ev;
  560. }
  561. #if !SANITIZER_GO
  562. uptr ALWAYS_INLINE HeapEnd() {
  563. return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
  564. }
  565. #endif
  566. ThreadState *FiberCreate(ThreadState *thr, uptr pc, unsigned flags);
  567. void FiberDestroy(ThreadState *thr, uptr pc, ThreadState *fiber);
  568. void FiberSwitch(ThreadState *thr, uptr pc, ThreadState *fiber, unsigned flags);
  569. // These need to match __tsan_switch_to_fiber_* flags defined in
  570. // tsan_interface.h. See documentation there as well.
  571. enum FiberSwitchFlags {
  572. FiberSwitchFlagNoSync = 1 << 0, // __tsan_switch_to_fiber_no_sync
  573. };
  574. ALWAYS_INLINE void ProcessPendingSignals(ThreadState *thr) {
  575. if (UNLIKELY(atomic_load_relaxed(&thr->pending_signals)))
  576. ProcessPendingSignalsImpl(thr);
  577. }
  578. extern bool is_initialized;
  579. ALWAYS_INLINE
  580. void LazyInitialize(ThreadState *thr) {
  581. // If we can use .preinit_array, assume that __tsan_init
  582. // called from .preinit_array initializes runtime before
  583. // any instrumented code.
  584. #if !SANITIZER_CAN_USE_PREINIT_ARRAY
  585. if (UNLIKELY(!is_initialized))
  586. Initialize(thr);
  587. #endif
  588. }
  589. namespace v3 {
  590. void TraceSwitchPart(ThreadState *thr);
  591. bool RestoreStack(Tid tid, EventType type, Sid sid, Epoch epoch, uptr addr,
  592. uptr size, AccessType typ, VarSizeStackTrace *pstk,
  593. MutexSet *pmset, uptr *ptag);
  594. template <typename EventT>
  595. ALWAYS_INLINE WARN_UNUSED_RESULT bool TraceAcquire(ThreadState *thr,
  596. EventT **ev) {
  597. Event *pos = reinterpret_cast<Event *>(atomic_load_relaxed(&thr->trace_pos));
  598. #if SANITIZER_DEBUG
  599. // TraceSwitch acquires these mutexes,
  600. // so we lock them here to detect deadlocks more reliably.
  601. { Lock lock(&ctx->slot_mtx); }
  602. { Lock lock(&thr->tctx->trace.mtx); }
  603. TracePart *current = thr->tctx->trace.parts.Back();
  604. if (current) {
  605. DCHECK_GE(pos, &current->events[0]);
  606. DCHECK_LE(pos, &current->events[TracePart::kSize]);
  607. } else {
  608. DCHECK_EQ(pos, nullptr);
  609. }
  610. #endif
  611. // TracePart is allocated with mmap and is at least 4K aligned.
  612. // So the following check is a faster way to check for part end.
  613. // It may have false positives in the middle of the trace,
  614. // they are filtered out in TraceSwitch.
  615. if (UNLIKELY(((uptr)(pos + 1) & TracePart::kAlignment) == 0))
  616. return false;
  617. *ev = reinterpret_cast<EventT *>(pos);
  618. return true;
  619. }
  620. template <typename EventT>
  621. ALWAYS_INLINE void TraceRelease(ThreadState *thr, EventT *evp) {
  622. DCHECK_LE(evp + 1, &thr->tctx->trace.parts.Back()->events[TracePart::kSize]);
  623. atomic_store_relaxed(&thr->trace_pos, (uptr)(evp + 1));
  624. }
  625. template <typename EventT>
  626. void TraceEvent(ThreadState *thr, EventT ev) {
  627. EventT *evp;
  628. if (!TraceAcquire(thr, &evp)) {
  629. TraceSwitchPart(thr);
  630. UNUSED bool res = TraceAcquire(thr, &evp);
  631. DCHECK(res);
  632. }
  633. *evp = ev;
  634. TraceRelease(thr, evp);
  635. }
  636. ALWAYS_INLINE WARN_UNUSED_RESULT bool TryTraceFunc(ThreadState *thr,
  637. uptr pc = 0) {
  638. if (!kCollectHistory)
  639. return true;
  640. EventFunc *ev;
  641. if (UNLIKELY(!TraceAcquire(thr, &ev)))
  642. return false;
  643. ev->is_access = 0;
  644. ev->is_func = 1;
  645. ev->pc = pc;
  646. TraceRelease(thr, ev);
  647. return true;
  648. }
  649. WARN_UNUSED_RESULT
  650. bool TryTraceMemoryAccess(ThreadState *thr, uptr pc, uptr addr, uptr size,
  651. AccessType typ);
  652. WARN_UNUSED_RESULT
  653. bool TryTraceMemoryAccessRange(ThreadState *thr, uptr pc, uptr addr, uptr size,
  654. AccessType typ);
  655. void TraceMemoryAccessRange(ThreadState *thr, uptr pc, uptr addr, uptr size,
  656. AccessType typ);
  657. void TraceFunc(ThreadState *thr, uptr pc = 0);
  658. void TraceMutexLock(ThreadState *thr, EventType type, uptr pc, uptr addr,
  659. StackID stk);
  660. void TraceMutexUnlock(ThreadState *thr, uptr addr);
  661. void TraceTime(ThreadState *thr);
  662. } // namespace v3
  663. void GrowShadowStack(ThreadState *thr);
  664. ALWAYS_INLINE
  665. void FuncEntry(ThreadState *thr, uptr pc) {
  666. DPrintf2("#%d: FuncEntry %p\n", (int)thr->fast_state.tid(), (void *)pc);
  667. if (kCollectHistory) {
  668. thr->fast_state.IncrementEpoch();
  669. TraceAddEvent(thr, thr->fast_state, EventTypeFuncEnter, pc);
  670. }
  671. // Shadow stack maintenance can be replaced with
  672. // stack unwinding during trace switch (which presumably must be faster).
  673. DCHECK_GE(thr->shadow_stack_pos, thr->shadow_stack);
  674. #if !SANITIZER_GO
  675. DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end);
  676. #else
  677. if (thr->shadow_stack_pos == thr->shadow_stack_end)
  678. GrowShadowStack(thr);
  679. #endif
  680. thr->shadow_stack_pos[0] = pc;
  681. thr->shadow_stack_pos++;
  682. }
  683. ALWAYS_INLINE
  684. void FuncExit(ThreadState *thr) {
  685. DPrintf2("#%d: FuncExit\n", (int)thr->fast_state.tid());
  686. if (kCollectHistory) {
  687. thr->fast_state.IncrementEpoch();
  688. TraceAddEvent(thr, thr->fast_state, EventTypeFuncExit, 0);
  689. }
  690. DCHECK_GT(thr->shadow_stack_pos, thr->shadow_stack);
  691. #if !SANITIZER_GO
  692. DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end);
  693. #endif
  694. thr->shadow_stack_pos--;
  695. }
  696. #if !SANITIZER_GO
  697. extern void (*on_initialize)(void);
  698. extern int (*on_finalize)(int);
  699. #endif
  700. } // namespace __tsan
  701. #endif // TSAN_RTL_H