1 //===-- tsan_rtl.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 // Main internal TSan header file.
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 (*).
22 // (*) Except when inlining is critical for performance.
23 //===----------------------------------------------------------------------===//
28 #include "sanitizer_common/sanitizer_allocator.h"
29 #include "sanitizer_common/sanitizer_allocator_internal.h"
30 #include "sanitizer_common/sanitizer_asm.h"
31 #include "sanitizer_common/sanitizer_common.h"
32 #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
33 #include "sanitizer_common/sanitizer_libignore.h"
34 #include "sanitizer_common/sanitizer_suppressions.h"
35 #include "sanitizer_common/sanitizer_thread_registry.h"
36 #include "sanitizer_common/sanitizer_vector.h"
37 #include "tsan_defs.h"
38 #include "tsan_flags.h"
39 #include "tsan_ignoreset.h"
40 #include "tsan_ilist.h"
41 #include "tsan_mman.h"
42 #include "tsan_mutexset.h"
43 #include "tsan_platform.h"
44 #include "tsan_report.h"
45 #include "tsan_shadow.h"
46 #include "tsan_stack_trace.h"
47 #include "tsan_sync.h"
48 #include "tsan_trace.h"
49 #include "tsan_vector_clock.h"
51 #if SANITIZER_WORDSIZE != 64
52 # error "ThreadSanitizer is supported only on 64-bit platforms"
58 struct MapUnmapCallback
;
59 #if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__)
62 static const uptr kSpaceBeg
= 0;
63 static const u64 kSpaceSize
= SANITIZER_MMAP_RANGE_SIZE
;
64 static const uptr kMetadataSize
= 0;
65 typedef __sanitizer::CompactSizeClassMap SizeClassMap
;
66 static const uptr kRegionSizeLog
= 20;
67 using AddressSpaceView
= LocalAddressSpaceView
;
68 typedef __tsan::MapUnmapCallback MapUnmapCallback
;
69 static const uptr kFlags
= 0;
71 typedef SizeClassAllocator32
<AP32
> PrimaryAllocator
;
73 struct AP64
{ // Allocator64 parameters. Deliberately using a short name.
74 # if defined(__s390x__)
75 typedef MappingS390x Mapping
;
77 typedef Mapping48AddressSpace Mapping
;
79 static const uptr kSpaceBeg
= Mapping::kHeapMemBeg
;
80 static const uptr kSpaceSize
= Mapping::kHeapMemEnd
- Mapping::kHeapMemBeg
;
81 static const uptr kMetadataSize
= 0;
82 typedef DefaultSizeClassMap SizeClassMap
;
83 typedef __tsan::MapUnmapCallback MapUnmapCallback
;
84 static const uptr kFlags
= 0;
85 using AddressSpaceView
= LocalAddressSpaceView
;
87 typedef SizeClassAllocator64
<AP64
> PrimaryAllocator
;
89 typedef CombinedAllocator
<PrimaryAllocator
> Allocator
;
90 typedef Allocator::AllocatorCache AllocatorCache
;
91 Allocator
*allocator();
94 struct ThreadSignalContext
;
99 bool in_blocking_func
;
100 uptr in_signal_handler
;
101 uptr
*shadow_stack_pos
;
104 // A Processor represents a physical thread, or a P for Go.
105 // It is used to store internal resources like allocate cache, and does not
106 // participate in race-detection logic (invisible to end user).
107 // In C++ it is tied to an OS thread just like ThreadState, however ideally
108 // it should be tied to a CPU (this way we will have fewer allocator caches).
109 // In Go it is tied to a P, so there are significantly fewer Processor's than
110 // ThreadState's (which are tied to Gs).
111 // A ThreadState must be wired with a Processor to handle events.
113 ThreadState
*thr
; // currently wired thread, or nullptr
115 AllocatorCache alloc_cache
;
116 InternalAllocatorCache internal_alloc_cache
;
118 DenseSlabAllocCache block_cache
;
119 DenseSlabAllocCache sync_cache
;
120 DDPhysicalThread
*dd_pt
;
124 // ScopedGlobalProcessor temporary setups a global processor for the current
125 // thread, if it does not have one. Intended for interceptors that can run
126 // at the very thread end, when we already destroyed the thread processor.
127 struct ScopedGlobalProcessor
{
128 ScopedGlobalProcessor();
129 ~ScopedGlobalProcessor();
141 atomic_uint32_t raw_epoch
;
143 Vector
<TidEpoch
> journal
;
146 Epoch
epoch() const {
147 return static_cast<Epoch
>(atomic_load(&raw_epoch
, memory_order_relaxed
));
150 void SetEpoch(Epoch v
) {
151 atomic_store(&raw_epoch
, static_cast<u32
>(v
), memory_order_relaxed
);
155 } ALIGNED(SANITIZER_CACHE_LINE_SIZE
);
157 // This struct is stored in TLS.
159 FastState fast_state
;
162 int ignore_interceptors
;
164 uptr
*shadow_stack_pos
;
166 // Current position in tctx->trace.Back()->events (Event*).
167 atomic_uintptr_t trace_pos
;
168 // PC of the last memory access, used to compute PC deltas in the trace.
171 // Technically `current` should be a separate THREADLOCAL variable;
172 // but it is placed here in order to share cache line with previous fields.
173 ThreadState
* current
;
175 atomic_sint32_t pending_signals
;
179 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
180 // We do not distinguish beteween ignoring reads and writes
181 // for better performance.
182 int ignore_reads_and_writes
;
183 int suppress_reports
;
184 // Go does not support ignores.
186 IgnoreSet mop_ignore_set
;
187 IgnoreSet sync_ignore_set
;
190 uptr
*shadow_stack_end
;
192 Vector
<JmpBuf
> jmp_bufs
;
206 DDLogicalThread
*dd_lt
;
212 // Current wired Processor, or nullptr. Required to handle any events.
215 Processor
*proc() { return proc1
; }
220 atomic_uintptr_t in_signal_handler
;
221 ThreadSignalContext
*signal_ctx
;
224 StackID last_sleep_stack_id
;
225 VectorClock last_sleep_clock
;
228 // Set in regions of runtime that must be signal-safe and fork-safe.
229 // If set, malloc must not be called.
232 const ReportDesc
*current_report
;
234 explicit ThreadState(Tid tid
);
235 } ALIGNED(SANITIZER_CACHE_LINE_SIZE
);
238 #if SANITIZER_MAC || SANITIZER_ANDROID
239 ThreadState
*cur_thread();
240 void set_cur_thread(ThreadState
*thr
);
241 void cur_thread_finalize();
242 inline ThreadState
*cur_thread_init() { return cur_thread(); }
244 __attribute__((tls_model("initial-exec")))
245 extern THREADLOCAL
char cur_thread_placeholder
[];
246 inline ThreadState
*cur_thread() {
247 return reinterpret_cast<ThreadState
*>(cur_thread_placeholder
)->current
;
249 inline ThreadState
*cur_thread_init() {
250 ThreadState
*thr
= reinterpret_cast<ThreadState
*>(cur_thread_placeholder
);
251 if (UNLIKELY(!thr
->current
))
255 inline void set_cur_thread(ThreadState
*thr
) {
256 reinterpret_cast<ThreadState
*>(cur_thread_placeholder
)->current
= thr
;
258 inline void cur_thread_finalize() { }
259 # endif // SANITIZER_MAC || SANITIZER_ANDROID
260 #endif // SANITIZER_GO
262 class ThreadContext final
: public ThreadContextBase
{
264 explicit ThreadContext(Tid tid
);
267 StackID creation_stack_id
;
272 // Override superclass callbacks.
273 void OnDead() override
;
274 void OnJoined(void *arg
) override
;
275 void OnFinished() override
;
276 void OnStarted(void *arg
) override
;
277 void OnCreated(void *arg
) override
;
278 void OnReset() override
;
279 void OnDetached(void *arg
) override
;
284 bool operator==(const RacyStacks
&other
) const;
292 struct FiredSuppression
{
303 bool after_multithreaded_fork
;
310 atomic_uint64_t last_symbolize_time_ns
;
312 void *background_thread
;
313 atomic_uint32_t stop_background_thread
;
315 ThreadRegistry thread_registry
;
318 Vector
<RacyStacks
> racy_stacks
;
319 Vector
<RacyAddress
> racy_addresses
;
320 // Number of fired suppressions may be large enough.
321 Mutex fired_suppressions_mtx
;
322 InternalMmapVector
<FiredSuppression
> fired_suppressions
;
328 // The last slot index (kFreeSid) is used to denote freed memory.
329 TidSlot slots
[kThreadSlotCount
- 1];
331 // Protects global_epoch, slot_queue, trace_part_recycle.
333 uptr global_epoch
; // guarded by slot_mtx and by all slot mutexes
334 bool resetting
; // global reset is in progress
335 IList
<TidSlot
, &TidSlot::node
> slot_queue
SANITIZER_GUARDED_BY(slot_mtx
);
336 IList
<TraceHeader
, &TraceHeader::global
, TracePart
> trace_part_recycle
337 SANITIZER_GUARDED_BY(slot_mtx
);
338 uptr trace_part_total_allocated
SANITIZER_GUARDED_BY(slot_mtx
);
339 uptr trace_part_recycle_finished
SANITIZER_GUARDED_BY(slot_mtx
);
340 uptr trace_part_finished_excess
SANITIZER_GUARDED_BY(slot_mtx
);
343 extern Context
*ctx
; // The one and the only global runtime context.
345 ALWAYS_INLINE Flags
*flags() {
349 struct ScopedIgnoreInterceptors
{
350 ScopedIgnoreInterceptors() {
352 cur_thread()->ignore_interceptors
++;
356 ~ScopedIgnoreInterceptors() {
358 cur_thread()->ignore_interceptors
--;
363 const char *GetObjectTypeFromTag(uptr tag
);
364 const char *GetReportHeaderFromTag(uptr tag
);
365 uptr
TagFromShadowStackFrame(uptr pc
);
367 class ScopedReportBase
{
369 void AddMemoryAccess(uptr addr
, uptr external_tag
, Shadow s
, Tid tid
,
370 StackTrace stack
, const MutexSet
*mset
);
371 void AddStack(StackTrace stack
, bool suppressable
= false);
372 void AddThread(const ThreadContext
*tctx
, bool suppressable
= false);
373 void AddThread(Tid tid
, bool suppressable
= false);
374 void AddUniqueTid(Tid unique_tid
);
375 int AddMutex(uptr addr
, StackID creation_stack_id
);
376 void AddLocation(uptr addr
, uptr size
);
377 void AddSleep(StackID stack_id
);
378 void SetCount(int count
);
379 void SetSigNum(int sig
);
381 const ReportDesc
*GetReport() const;
384 ScopedReportBase(ReportType typ
, uptr tag
);
389 // Symbolizer makes lots of intercepted calls. If we try to process them,
390 // at best it will cause deadlocks on internal mutexes.
391 ScopedIgnoreInterceptors ignore_interceptors_
;
393 ScopedReportBase(const ScopedReportBase
&) = delete;
394 void operator=(const ScopedReportBase
&) = delete;
397 class ScopedReport
: public ScopedReportBase
{
399 explicit ScopedReport(ReportType typ
, uptr tag
= kExternalTagNone
);
403 ScopedErrorReportLock lock_
;
406 bool ShouldReport(ThreadState
*thr
, ReportType typ
);
407 ThreadContext
*IsThreadStackOrTls(uptr addr
, bool *is_stack
);
409 // The stack could look like:
410 // <start> | <main> | <foo> | tag | <bar>
411 // This will extract the tag and keep:
412 // <start> | <main> | <foo> | <bar>
413 template<typename StackTraceTy
>
414 void ExtractTagFromStack(StackTraceTy
*stack
, uptr
*tag
= nullptr) {
415 if (stack
->size
< 2) return;
416 uptr possible_tag_pc
= stack
->trace
[stack
->size
- 2];
417 uptr possible_tag
= TagFromShadowStackFrame(possible_tag_pc
);
418 if (possible_tag
== kExternalTagNone
) return;
419 stack
->trace_buffer
[stack
->size
- 2] = stack
->trace_buffer
[stack
->size
- 1];
421 if (tag
) *tag
= possible_tag
;
424 template<typename StackTraceTy
>
425 void ObtainCurrentStack(ThreadState
*thr
, uptr toppc
, StackTraceTy
*stack
,
426 uptr
*tag
= nullptr) {
427 uptr size
= thr
->shadow_stack_pos
- thr
->shadow_stack
;
429 if (size
+ !!toppc
> kStackTraceMax
) {
430 start
= size
+ !!toppc
- kStackTraceMax
;
431 size
= kStackTraceMax
- !!toppc
;
433 stack
->Init(&thr
->shadow_stack
[start
], size
, toppc
);
434 ExtractTagFromStack(stack
, tag
);
437 #define GET_STACK_TRACE_FATAL(thr, pc) \
438 VarSizeStackTrace stack; \
439 ObtainCurrentStack(thr, pc, &stack); \
440 stack.ReverseOrder();
442 void MapShadow(uptr addr
, uptr size
);
443 void MapThreadTrace(uptr addr
, uptr size
, const char *name
);
444 void DontNeedShadowFor(uptr addr
, uptr size
);
445 void UnmapShadow(ThreadState
*thr
, uptr addr
, uptr size
);
446 void InitializeShadowMemory();
447 void InitializeInterceptors();
448 void InitializeLibIgnore();
449 void InitializeDynamicAnnotations();
451 void ForkBefore(ThreadState
*thr
, uptr pc
);
452 void ForkParentAfter(ThreadState
*thr
, uptr pc
);
453 void ForkChildAfter(ThreadState
*thr
, uptr pc
, bool start_thread
);
455 void ReportRace(ThreadState
*thr
, RawShadow
*shadow_mem
, Shadow cur
, Shadow old
,
457 bool OutputReport(ThreadState
*thr
, const ScopedReport
&srep
);
458 bool IsFiredSuppression(Context
*ctx
, ReportType type
, StackTrace trace
);
459 bool IsExpectedReport(uptr addr
, uptr size
);
461 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
462 # define DPrintf Printf
464 # define DPrintf(...)
467 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
468 # define DPrintf2 Printf
470 # define DPrintf2(...)
473 StackID
CurrentStackId(ThreadState
*thr
, uptr pc
);
474 ReportStack
*SymbolizeStackId(StackID stack_id
);
475 void PrintCurrentStack(ThreadState
*thr
, uptr pc
);
476 void PrintCurrentStackSlow(uptr pc
); // uses libunwind
477 MBlock
*JavaHeapBlock(uptr addr
, uptr
*start
);
479 void Initialize(ThreadState
*thr
);
480 void MaybeSpawnBackgroundThread();
481 int Finalize(ThreadState
*thr
);
483 void OnUserAlloc(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
, bool write
);
484 void OnUserFree(ThreadState
*thr
, uptr pc
, uptr p
, bool write
);
486 void MemoryAccess(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
,
488 void UnalignedMemoryAccess(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
,
490 // This creates 2 non-inlined specialized versions of MemoryAccessRange.
491 template <bool is_read
>
492 void MemoryAccessRangeT(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
495 void MemoryAccessRange(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
,
500 MemoryAccessRangeT
<false>(thr
, pc
, addr
, size
);
502 MemoryAccessRangeT
<true>(thr
, pc
, addr
, size
);
505 void ShadowSet(RawShadow
*p
, RawShadow
*end
, RawShadow v
);
506 void MemoryRangeFreed(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
507 void MemoryResetRange(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
508 void MemoryRangeImitateWrite(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
509 void MemoryRangeImitateWriteOrResetRange(ThreadState
*thr
, uptr pc
, uptr addr
,
512 void ThreadIgnoreBegin(ThreadState
*thr
, uptr pc
);
513 void ThreadIgnoreEnd(ThreadState
*thr
);
514 void ThreadIgnoreSyncBegin(ThreadState
*thr
, uptr pc
);
515 void ThreadIgnoreSyncEnd(ThreadState
*thr
);
517 Tid
ThreadCreate(ThreadState
*thr
, uptr pc
, uptr uid
, bool detached
);
518 void ThreadStart(ThreadState
*thr
, Tid tid
, tid_t os_id
,
519 ThreadType thread_type
);
520 void ThreadFinish(ThreadState
*thr
);
521 Tid
ThreadConsumeTid(ThreadState
*thr
, uptr pc
, uptr uid
);
522 void ThreadJoin(ThreadState
*thr
, uptr pc
, Tid tid
);
523 void ThreadDetach(ThreadState
*thr
, uptr pc
, Tid tid
);
524 void ThreadFinalize(ThreadState
*thr
);
525 void ThreadSetName(ThreadState
*thr
, const char *name
);
526 int ThreadCount(ThreadState
*thr
);
527 void ProcessPendingSignalsImpl(ThreadState
*thr
);
528 void ThreadNotJoined(ThreadState
*thr
, uptr pc
, Tid tid
, uptr uid
);
530 Processor
*ProcCreate();
531 void ProcDestroy(Processor
*proc
);
532 void ProcWire(Processor
*proc
, ThreadState
*thr
);
533 void ProcUnwire(Processor
*proc
, ThreadState
*thr
);
535 // Note: the parameter is called flagz, because flags is already taken
536 // by the global function that returns flags.
537 void MutexCreate(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
538 void MutexDestroy(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
539 void MutexPreLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
540 void MutexPostLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0,
542 int MutexUnlock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
543 void MutexPreReadLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
544 void MutexPostReadLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
545 void MutexReadUnlock(ThreadState
*thr
, uptr pc
, uptr addr
);
546 void MutexReadOrWriteUnlock(ThreadState
*thr
, uptr pc
, uptr addr
);
547 void MutexRepair(ThreadState
*thr
, uptr pc
, uptr addr
); // call on EOWNERDEAD
548 void MutexInvalidAccess(ThreadState
*thr
, uptr pc
, uptr addr
);
550 void Acquire(ThreadState
*thr
, uptr pc
, uptr addr
);
551 // AcquireGlobal synchronizes the current thread with all other threads.
552 // In terms of happens-before relation, it draws a HB edge from all threads
553 // (where they happen to execute right now) to the current thread. We use it to
554 // handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
555 // right before executing finalizers. This provides a coarse, but simple
556 // approximation of the actual required synchronization.
557 void AcquireGlobal(ThreadState
*thr
);
558 void Release(ThreadState
*thr
, uptr pc
, uptr addr
);
559 void ReleaseStoreAcquire(ThreadState
*thr
, uptr pc
, uptr addr
);
560 void ReleaseStore(ThreadState
*thr
, uptr pc
, uptr addr
);
561 void AfterSleep(ThreadState
*thr
, uptr pc
);
562 void IncrementEpoch(ThreadState
*thr
);
565 uptr ALWAYS_INLINE
HeapEnd() {
566 return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
570 void SlotAttachAndLock(ThreadState
*thr
) SANITIZER_ACQUIRE(thr
->slot
->mtx
);
571 void SlotDetach(ThreadState
*thr
);
572 void SlotLock(ThreadState
*thr
) SANITIZER_ACQUIRE(thr
->slot
->mtx
);
573 void SlotUnlock(ThreadState
*thr
) SANITIZER_RELEASE(thr
->slot
->mtx
);
574 void DoReset(ThreadState
*thr
, uptr epoch
);
575 void FlushShadowMemory();
577 ThreadState
*FiberCreate(ThreadState
*thr
, uptr pc
, unsigned flags
);
578 void FiberDestroy(ThreadState
*thr
, uptr pc
, ThreadState
*fiber
);
579 void FiberSwitch(ThreadState
*thr
, uptr pc
, ThreadState
*fiber
, unsigned flags
);
581 // These need to match __tsan_switch_to_fiber_* flags defined in
582 // tsan_interface.h. See documentation there as well.
583 enum FiberSwitchFlags
{
584 FiberSwitchFlagNoSync
= 1 << 0, // __tsan_switch_to_fiber_no_sync
590 SlotLocker(ThreadState
*thr
, bool recursive
= false)
591 : thr_(thr
), locked_(recursive
? thr
->slot_locked
: false) {
609 SlotUnlocker(ThreadState
*thr
) : thr_(thr
), locked_(thr
->slot_locked
) {
624 ALWAYS_INLINE
void ProcessPendingSignals(ThreadState
*thr
) {
625 if (UNLIKELY(atomic_load_relaxed(&thr
->pending_signals
)))
626 ProcessPendingSignalsImpl(thr
);
629 extern bool is_initialized
;
632 void LazyInitialize(ThreadState
*thr
) {
633 // If we can use .preinit_array, assume that __tsan_init
634 // called from .preinit_array initializes runtime before
635 // any instrumented code.
636 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
637 if (UNLIKELY(!is_initialized
))
642 void TraceResetForTesting();
643 void TraceSwitchPart(ThreadState
*thr
);
644 void TraceSwitchPartImpl(ThreadState
*thr
);
645 bool RestoreStack(EventType type
, Sid sid
, Epoch epoch
, uptr addr
, uptr size
,
646 AccessType typ
, Tid
*ptid
, VarSizeStackTrace
*pstk
,
647 MutexSet
*pmset
, uptr
*ptag
);
649 template <typename EventT
>
650 ALWAYS_INLINE WARN_UNUSED_RESULT
bool TraceAcquire(ThreadState
*thr
,
652 // TraceSwitchPart accesses shadow_stack, but it's called infrequently,
653 // so we check it here proactively.
654 DCHECK(thr
->shadow_stack
);
655 Event
*pos
= reinterpret_cast<Event
*>(atomic_load_relaxed(&thr
->trace_pos
));
657 // TraceSwitch acquires these mutexes,
658 // so we lock them here to detect deadlocks more reliably.
659 { Lock
lock(&ctx
->slot_mtx
); }
660 { Lock
lock(&thr
->tctx
->trace
.mtx
); }
661 TracePart
*current
= thr
->tctx
->trace
.parts
.Back();
663 DCHECK_GE(pos
, ¤t
->events
[0]);
664 DCHECK_LE(pos
, ¤t
->events
[TracePart::kSize
]);
666 DCHECK_EQ(pos
, nullptr);
669 // TracePart is allocated with mmap and is at least 4K aligned.
670 // So the following check is a faster way to check for part end.
671 // It may have false positives in the middle of the trace,
672 // they are filtered out in TraceSwitch.
673 if (UNLIKELY(((uptr
)(pos
+ 1) & TracePart::kAlignment
) == 0))
675 *ev
= reinterpret_cast<EventT
*>(pos
);
679 template <typename EventT
>
680 ALWAYS_INLINE
void TraceRelease(ThreadState
*thr
, EventT
*evp
) {
681 DCHECK_LE(evp
+ 1, &thr
->tctx
->trace
.parts
.Back()->events
[TracePart::kSize
]);
682 atomic_store_relaxed(&thr
->trace_pos
, (uptr
)(evp
+ 1));
685 template <typename EventT
>
686 void TraceEvent(ThreadState
*thr
, EventT ev
) {
688 if (!TraceAcquire(thr
, &evp
)) {
689 TraceSwitchPart(thr
);
690 UNUSED
bool res
= TraceAcquire(thr
, &evp
);
694 TraceRelease(thr
, evp
);
697 ALWAYS_INLINE WARN_UNUSED_RESULT
bool TryTraceFunc(ThreadState
*thr
,
699 if (!kCollectHistory
)
702 if (UNLIKELY(!TraceAcquire(thr
, &ev
)))
707 TraceRelease(thr
, ev
);
712 bool TryTraceMemoryAccess(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
,
715 bool TryTraceMemoryAccessRange(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
,
717 void TraceMemoryAccessRange(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
,
719 void TraceFunc(ThreadState
*thr
, uptr pc
= 0);
720 void TraceMutexLock(ThreadState
*thr
, EventType type
, uptr pc
, uptr addr
,
722 void TraceMutexUnlock(ThreadState
*thr
, uptr addr
);
723 void TraceTime(ThreadState
*thr
);
725 void TraceRestartFuncExit(ThreadState
*thr
);
726 void TraceRestartFuncEntry(ThreadState
*thr
, uptr pc
);
728 void GrowShadowStack(ThreadState
*thr
);
731 void FuncEntry(ThreadState
*thr
, uptr pc
) {
732 DPrintf2("#%d: FuncEntry %p\n", (int)thr
->fast_state
.sid(), (void *)pc
);
733 if (UNLIKELY(!TryTraceFunc(thr
, pc
)))
734 return TraceRestartFuncEntry(thr
, pc
);
735 DCHECK_GE(thr
->shadow_stack_pos
, thr
->shadow_stack
);
737 DCHECK_LT(thr
->shadow_stack_pos
, thr
->shadow_stack_end
);
739 if (thr
->shadow_stack_pos
== thr
->shadow_stack_end
)
740 GrowShadowStack(thr
);
742 thr
->shadow_stack_pos
[0] = pc
;
743 thr
->shadow_stack_pos
++;
747 void FuncExit(ThreadState
*thr
) {
748 DPrintf2("#%d: FuncExit\n", (int)thr
->fast_state
.sid());
749 if (UNLIKELY(!TryTraceFunc(thr
, 0)))
750 return TraceRestartFuncExit(thr
);
751 DCHECK_GT(thr
->shadow_stack_pos
, thr
->shadow_stack
);
753 DCHECK_LT(thr
->shadow_stack_pos
, thr
->shadow_stack_end
);
755 thr
->shadow_stack_pos
--;
759 extern void (*on_initialize
)(void);
760 extern int (*on_finalize
)(int);
762 } // namespace __tsan