1 //===-- tsan_rtl.h ----------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 // Main internal TSan header file.
13 // - C++ run-time should not be used (static CTORs, RTTI, exceptions, static
14 // function-scope locals)
15 // - All functions/classes/etc reside in namespace __tsan, except for those
16 // declared in tsan_interface.h.
17 // - Platform-specific files should be used instead of ifdefs (*).
18 // - No system headers included in header files (*).
19 // - Platform specific headres included only into platform-specific files (*).
21 // (*) Except when inlining is critical for performance.
22 //===----------------------------------------------------------------------===//
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"
47 #if SANITIZER_WORDSIZE != 64
48 # error "ThreadSanitizer is supported only on 64-bit platforms"
54 struct MapUnmapCallback
;
55 #if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__)
56 static const uptr kAllocatorRegionSizeLog
= 20;
57 static const uptr kAllocatorNumRegions
=
58 SANITIZER_MMAP_RANGE_SIZE
>> kAllocatorRegionSizeLog
;
59 typedef TwoLevelByteMap
<(kAllocatorNumRegions
>> 12), 1 << 12,
60 MapUnmapCallback
> ByteMap
;
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
= kAllocatorRegionSizeLog
;
67 typedef __tsan::ByteMap ByteMap
;
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 static const uptr kSpaceBeg
= Mapping::kHeapMemBeg
;
75 static const uptr kSpaceSize
= Mapping::kHeapMemEnd
- Mapping::kHeapMemBeg
;
76 static const uptr kMetadataSize
= 0;
77 typedef DefaultSizeClassMap SizeClassMap
;
78 typedef __tsan::MapUnmapCallback MapUnmapCallback
;
79 static const uptr kFlags
= 0;
81 typedef SizeClassAllocator64
<AP64
> PrimaryAllocator
;
83 typedef SizeClassAllocatorLocalCache
<PrimaryAllocator
> AllocatorCache
;
84 typedef LargeMmapAllocator
<MapUnmapCallback
> SecondaryAllocator
;
85 typedef CombinedAllocator
<PrimaryAllocator
, AllocatorCache
,
86 SecondaryAllocator
> Allocator
;
87 Allocator
*allocator();
90 void TsanCheckFailed(const char *file
, int line
, const char *cond
,
93 const u64 kShadowRodata
= (u64
)-1; // .rodata shadow marker
95 // FastState (from most significant bit):
103 FastState(u64 tid
, u64 epoch
) {
104 x_
= tid
<< kTidShift
;
106 DCHECK_EQ(tid
, this->tid());
107 DCHECK_EQ(epoch
, this->epoch());
108 DCHECK_EQ(GetIgnoreBit(), false);
111 explicit FastState(u64 x
)
120 u64 res
= (x_
& ~kIgnoreBit
) >> kTidShift
;
124 u64
TidWithIgnore() const {
125 u64 res
= x_
>> kTidShift
;
130 u64 res
= x_
& ((1ull << kClkBits
) - 1);
134 void IncrementEpoch() {
135 u64 old_epoch
= epoch();
137 DCHECK_EQ(old_epoch
+ 1, epoch());
141 void SetIgnoreBit() { x_
|= kIgnoreBit
; }
142 void ClearIgnoreBit() { x_
&= ~kIgnoreBit
; }
143 bool GetIgnoreBit() const { return (s64
)x_
< 0; }
145 void SetHistorySize(int hs
) {
148 x_
= (x_
& ~(kHistoryMask
<< kHistoryShift
)) | (u64(hs
) << kHistoryShift
);
152 int GetHistorySize() const {
153 return (int)((x_
>> kHistoryShift
) & kHistoryMask
);
156 void ClearHistorySize() {
161 u64
GetTracePos() const {
162 const int hs
= GetHistorySize();
163 // When hs == 0, the trace consists of 2 parts.
164 const u64 mask
= (1ull << (kTracePartSizeBits
+ hs
+ 1)) - 1;
165 return epoch() & mask
;
170 static const int kTidShift
= 64 - kTidBits
- 1;
171 static const u64 kIgnoreBit
= 1ull << 63;
172 static const u64 kFreedBit
= 1ull << 63;
173 static const u64 kHistoryShift
= kClkBits
;
174 static const u64 kHistoryMask
= 7;
178 // Shadow (from most significant bit):
186 class Shadow
: public FastState
{
188 explicit Shadow(u64 x
)
192 explicit Shadow(const FastState
&s
)
197 void SetAddr0AndSizeLog(u64 addr0
, unsigned kAccessSizeLog
) {
198 DCHECK_EQ((x_
>> kClkBits
) & 31, 0);
200 DCHECK_LE(kAccessSizeLog
, 3);
201 x_
|= ((kAccessSizeLog
<< 3) | addr0
) << kClkBits
;
202 DCHECK_EQ(kAccessSizeLog
, size_log());
203 DCHECK_EQ(addr0
, this->addr0());
206 void SetWrite(unsigned kAccessIsWrite
) {
207 DCHECK_EQ(x_
& kReadBit
, 0);
210 DCHECK_EQ(kAccessIsWrite
, IsWrite());
213 void SetAtomic(bool kIsAtomic
) {
217 DCHECK_EQ(IsAtomic(), kIsAtomic
);
220 bool IsAtomic() const {
221 return x_
& kAtomicBit
;
224 bool IsZero() const {
228 static inline bool TidsAreEqual(const Shadow s1
, const Shadow s2
) {
229 u64 shifted_xor
= (s1
.x_
^ s2
.x_
) >> kTidShift
;
230 DCHECK_EQ(shifted_xor
== 0, s1
.TidWithIgnore() == s2
.TidWithIgnore());
231 return shifted_xor
== 0;
235 bool Addr0AndSizeAreEqual(const Shadow s1
, const Shadow s2
) {
236 u64 masked_xor
= ((s1
.x_
^ s2
.x_
) >> kClkBits
) & 31;
237 return masked_xor
== 0;
240 static ALWAYS_INLINE
bool TwoRangesIntersect(Shadow s1
, Shadow s2
,
241 unsigned kS2AccessSize
) {
243 u64 diff
= s1
.addr0() - s2
.addr0();
244 if ((s64
)diff
< 0) { // s1.addr0 < s2.addr0 // NOLINT
245 // if (s1.addr0() + size1) > s2.addr0()) return true;
246 if (s1
.size() > -diff
)
249 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
250 if (kS2AccessSize
> diff
)
253 DCHECK_EQ(res
, TwoRangesIntersectSlow(s1
, s2
));
254 DCHECK_EQ(res
, TwoRangesIntersectSlow(s2
, s1
));
258 u64 ALWAYS_INLINE
addr0() const { return (x_
>> kClkBits
) & 7; }
259 u64 ALWAYS_INLINE
size() const { return 1ull << size_log(); }
260 bool ALWAYS_INLINE
IsWrite() const { return !IsRead(); }
261 bool ALWAYS_INLINE
IsRead() const { return x_
& kReadBit
; }
263 // The idea behind the freed bit is as follows.
264 // When the memory is freed (or otherwise unaccessible) we write to the shadow
265 // values with tid/epoch related to the free and the freed bit set.
266 // During memory accesses processing the freed bit is considered
267 // as msb of tid. So any access races with shadow with freed bit set
268 // (it is as if write from a thread with which we never synchronized before).
269 // This allows us to detect accesses to freed memory w/o additional
270 // overheads in memory access processing and at the same time restore
271 // tid/epoch of free.
276 bool IsFreed() const {
277 return x_
& kFreedBit
;
280 bool GetFreedAndReset() {
281 bool res
= x_
& kFreedBit
;
286 bool ALWAYS_INLINE
IsBothReadsOrAtomic(bool kIsWrite
, bool kIsAtomic
) const {
287 bool v
= x_
& ((u64(kIsWrite
^ 1) << kReadShift
)
288 | (u64(kIsAtomic
) << kAtomicShift
));
289 DCHECK_EQ(v
, (!IsWrite() && !kIsWrite
) || (IsAtomic() && kIsAtomic
));
293 bool ALWAYS_INLINE
IsRWNotWeaker(bool kIsWrite
, bool kIsAtomic
) const {
294 bool v
= ((x_
>> kReadShift
) & 3)
295 <= u64((kIsWrite
^ 1) | (kIsAtomic
<< 1));
296 DCHECK_EQ(v
, (IsAtomic() < kIsAtomic
) ||
297 (IsAtomic() == kIsAtomic
&& !IsWrite() <= !kIsWrite
));
301 bool ALWAYS_INLINE
IsRWWeakerOrEqual(bool kIsWrite
, bool kIsAtomic
) const {
302 bool v
= ((x_
>> kReadShift
) & 3)
303 >= u64((kIsWrite
^ 1) | (kIsAtomic
<< 1));
304 DCHECK_EQ(v
, (IsAtomic() > kIsAtomic
) ||
305 (IsAtomic() == kIsAtomic
&& !IsWrite() >= !kIsWrite
));
310 static const u64 kReadShift
= 5 + kClkBits
;
311 static const u64 kReadBit
= 1ull << kReadShift
;
312 static const u64 kAtomicShift
= 6 + kClkBits
;
313 static const u64 kAtomicBit
= 1ull << kAtomicShift
;
315 u64
size_log() const { return (x_
>> (3 + kClkBits
)) & 3; }
317 static bool TwoRangesIntersectSlow(const Shadow s1
, const Shadow s2
) {
318 if (s1
.addr0() == s2
.addr0()) return true;
319 if (s1
.addr0() < s2
.addr0() && s1
.addr0() + s1
.size() > s2
.addr0())
321 if (s2
.addr0() < s1
.addr0() && s2
.addr0() + s2
.size() > s1
.addr0())
327 struct ThreadSignalContext
;
333 bool in_blocking_func
;
334 uptr in_signal_handler
;
335 uptr
*shadow_stack_pos
;
338 // A Processor represents a physical thread, or a P for Go.
339 // It is used to store internal resources like allocate cache, and does not
340 // participate in race-detection logic (invisible to end user).
341 // In C++ it is tied to an OS thread just like ThreadState, however ideally
342 // it should be tied to a CPU (this way we will have fewer allocator caches).
343 // In Go it is tied to a P, so there are significantly fewer Processor's than
344 // ThreadState's (which are tied to Gs).
345 // A ThreadState must be wired with a Processor to handle events.
347 ThreadState
*thr
; // currently wired thread, or nullptr
349 AllocatorCache alloc_cache
;
350 InternalAllocatorCache internal_alloc_cache
;
352 DenseSlabAllocCache block_cache
;
353 DenseSlabAllocCache sync_cache
;
354 DenseSlabAllocCache clock_cache
;
355 DDPhysicalThread
*dd_pt
;
359 // ScopedGlobalProcessor temporary setups a global processor for the current
360 // thread, if it does not have one. Intended for interceptors that can run
361 // at the very thread end, when we already destroyed the thread processor.
362 struct ScopedGlobalProcessor
{
363 ScopedGlobalProcessor();
364 ~ScopedGlobalProcessor();
368 // This struct is stored in TLS.
370 FastState fast_state
;
371 // Synch epoch represents the threads's epoch before the last synchronization
372 // action. It allows to reduce number of shadow state updates.
373 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
374 // if we are processing write to X from the same thread at epoch=200,
375 // we do nothing, because both writes happen in the same 'synch epoch'.
376 // That is, if another memory access does not race with the former write,
377 // it does not race with the latter as well.
378 // QUESTION: can we can squeeze this into ThreadState::Fast?
379 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
380 // taken by epoch between synchs.
381 // This way we can save one load from tls.
382 u64 fast_synch_epoch
;
383 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
384 // We do not distinguish beteween ignoring reads and writes
385 // for better performance.
386 int ignore_reads_and_writes
;
388 int suppress_reports
;
389 // Go does not support ignores.
391 IgnoreSet mop_ignore_set
;
392 IgnoreSet sync_ignore_set
;
394 // C/C++ uses fixed size shadow stack embed into Trace.
395 // Go uses malloc-allocated shadow stack with dynamic size.
397 uptr
*shadow_stack_end
;
398 uptr
*shadow_stack_pos
;
399 u64
*racy_shadow_addr
;
404 Vector
<JmpBuf
> jmp_bufs
;
405 int ignore_interceptors
;
407 #if TSAN_COLLECT_STATS
424 #if SANITIZER_DEBUG && !SANITIZER_GO
425 InternalDeadlockDetector internal_deadlock_detector
;
427 DDLogicalThread
*dd_lt
;
429 // Current wired Processor, or nullptr. Required to handle any events.
432 Processor
*proc() { return proc1
; }
437 atomic_uintptr_t in_signal_handler
;
438 ThreadSignalContext
*signal_ctx
;
441 u32 last_sleep_stack_id
;
442 ThreadClock last_sleep_clock
;
445 // Set in regions of runtime that must be signal-safe and fork-safe.
446 // If set, malloc must not be called.
449 const ReportDesc
*current_report
;
451 explicit ThreadState(Context
*ctx
, int tid
, int unique_id
, u64 epoch
,
452 unsigned reuse_count
,
453 uptr stk_addr
, uptr stk_size
,
454 uptr tls_addr
, uptr tls_size
);
458 #if SANITIZER_MAC || SANITIZER_ANDROID
459 ThreadState
*cur_thread();
460 void cur_thread_finalize();
462 __attribute__((tls_model("initial-exec")))
463 extern THREADLOCAL
char cur_thread_placeholder
[];
464 INLINE ThreadState
*cur_thread() {
465 return reinterpret_cast<ThreadState
*>(&cur_thread_placeholder
);
467 INLINE
void cur_thread_finalize() { }
468 #endif // SANITIZER_MAC || SANITIZER_ANDROID
469 #endif // SANITIZER_GO
471 class ThreadContext
: public ThreadContextBase
{
473 explicit ThreadContext(int tid
);
476 u32 creation_stack_id
;
478 // Epoch at which the thread had started.
479 // If we see an event from the thread stamped by an older epoch,
480 // the event is from a dead thread that shared tid with this thread.
484 // Override superclass callbacks.
485 void OnDead() override
;
486 void OnJoined(void *arg
) override
;
487 void OnFinished() override
;
488 void OnStarted(void *arg
) override
;
489 void OnCreated(void *arg
) override
;
490 void OnReset() override
;
491 void OnDetached(void *arg
) override
;
496 bool operator==(const RacyStacks
&other
) const {
497 if (hash
[0] == other
.hash
[0] && hash
[1] == other
.hash
[1])
499 if (hash
[0] == other
.hash
[1] && hash
[1] == other
.hash
[0])
510 struct FiredSuppression
{
520 bool after_multithreaded_fork
;
526 int nmissed_expected
;
527 atomic_uint64_t last_symbolize_time_ns
;
529 void *background_thread
;
530 atomic_uint32_t stop_background_thread
;
532 ThreadRegistry
*thread_registry
;
535 Vector
<RacyStacks
> racy_stacks
;
536 Vector
<RacyAddress
> racy_addresses
;
537 // Number of fired suppressions may be large enough.
538 Mutex fired_suppressions_mtx
;
539 InternalMmapVector
<FiredSuppression
> fired_suppressions
;
542 ClockAlloc clock_alloc
;
547 u64 int_alloc_cnt
[MBlockTypeCount
];
548 u64 int_alloc_siz
[MBlockTypeCount
];
551 extern Context
*ctx
; // The one and the only global runtime context.
553 ALWAYS_INLINE Flags
*flags() {
557 struct ScopedIgnoreInterceptors
{
558 ScopedIgnoreInterceptors() {
560 cur_thread()->ignore_interceptors
++;
564 ~ScopedIgnoreInterceptors() {
566 cur_thread()->ignore_interceptors
--;
571 const char *GetObjectTypeFromTag(uptr tag
);
572 const char *GetReportHeaderFromTag(uptr tag
);
573 uptr
TagFromShadowStackFrame(uptr pc
);
577 explicit ScopedReport(ReportType typ
, uptr tag
= kExternalTagNone
);
580 void AddMemoryAccess(uptr addr
, uptr external_tag
, Shadow s
, StackTrace stack
,
581 const MutexSet
*mset
);
582 void AddStack(StackTrace stack
, bool suppressable
= false);
583 void AddThread(const ThreadContext
*tctx
, bool suppressable
= false);
584 void AddThread(int unique_tid
, bool suppressable
= false);
585 void AddUniqueTid(int unique_tid
);
586 void AddMutex(const SyncVar
*s
);
587 u64
AddMutex(u64 id
);
588 void AddLocation(uptr addr
, uptr size
);
589 void AddSleep(u32 stack_id
);
590 void SetCount(int count
);
592 const ReportDesc
*GetReport() const;
596 // Symbolizer makes lots of intercepted calls. If we try to process them,
597 // at best it will cause deadlocks on internal mutexes.
598 ScopedIgnoreInterceptors ignore_interceptors_
;
600 void AddDeadMutex(u64 id
);
602 ScopedReport(const ScopedReport
&);
603 void operator = (const ScopedReport
&);
606 ThreadContext
*IsThreadStackOrTls(uptr addr
, bool *is_stack
);
607 void RestoreStack(int tid
, const u64 epoch
, VarSizeStackTrace
*stk
,
608 MutexSet
*mset
, uptr
*tag
= nullptr);
610 // The stack could look like:
611 // <start> | <main> | <foo> | tag | <bar>
612 // This will extract the tag and keep:
613 // <start> | <main> | <foo> | <bar>
614 template<typename StackTraceTy
>
615 void ExtractTagFromStack(StackTraceTy
*stack
, uptr
*tag
= nullptr) {
616 if (stack
->size
< 2) return;
617 uptr possible_tag_pc
= stack
->trace
[stack
->size
- 2];
618 uptr possible_tag
= TagFromShadowStackFrame(possible_tag_pc
);
619 if (possible_tag
== kExternalTagNone
) return;
620 stack
->trace_buffer
[stack
->size
- 2] = stack
->trace_buffer
[stack
->size
- 1];
622 if (tag
) *tag
= possible_tag
;
625 template<typename StackTraceTy
>
626 void ObtainCurrentStack(ThreadState
*thr
, uptr toppc
, StackTraceTy
*stack
,
627 uptr
*tag
= nullptr) {
628 uptr size
= thr
->shadow_stack_pos
- thr
->shadow_stack
;
630 if (size
+ !!toppc
> kStackTraceMax
) {
631 start
= size
+ !!toppc
- kStackTraceMax
;
632 size
= kStackTraceMax
- !!toppc
;
634 stack
->Init(&thr
->shadow_stack
[start
], size
, toppc
);
635 ExtractTagFromStack(stack
, tag
);
639 #if TSAN_COLLECT_STATS
640 void StatAggregate(u64
*dst
, u64
*src
);
641 void StatOutput(u64
*stat
);
644 void ALWAYS_INLINE
StatInc(ThreadState
*thr
, StatType typ
, u64 n
= 1) {
645 #if TSAN_COLLECT_STATS
649 void ALWAYS_INLINE
StatSet(ThreadState
*thr
, StatType typ
, u64 n
) {
650 #if TSAN_COLLECT_STATS
655 void MapShadow(uptr addr
, uptr size
);
656 void MapThreadTrace(uptr addr
, uptr size
, const char *name
);
657 void DontNeedShadowFor(uptr addr
, uptr size
);
658 void InitializeShadowMemory();
659 void InitializeInterceptors();
660 void InitializeLibIgnore();
661 void InitializeDynamicAnnotations();
663 void ForkBefore(ThreadState
*thr
, uptr pc
);
664 void ForkParentAfter(ThreadState
*thr
, uptr pc
);
665 void ForkChildAfter(ThreadState
*thr
, uptr pc
);
667 void ReportRace(ThreadState
*thr
);
668 bool OutputReport(ThreadState
*thr
, const ScopedReport
&srep
);
669 bool IsFiredSuppression(Context
*ctx
, ReportType type
, StackTrace trace
);
670 bool IsExpectedReport(uptr addr
, uptr size
);
671 void PrintMatchedBenignRaces();
673 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
674 # define DPrintf Printf
676 # define DPrintf(...)
679 #if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
680 # define DPrintf2 Printf
682 # define DPrintf2(...)
685 u32
CurrentStackId(ThreadState
*thr
, uptr pc
);
686 ReportStack
*SymbolizeStackId(u32 stack_id
);
687 void PrintCurrentStack(ThreadState
*thr
, uptr pc
);
688 void PrintCurrentStackSlow(uptr pc
); // uses libunwind
690 void Initialize(ThreadState
*thr
);
691 int Finalize(ThreadState
*thr
);
693 void OnUserAlloc(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
, bool write
);
694 void OnUserFree(ThreadState
*thr
, uptr pc
, uptr p
, bool write
);
696 void MemoryAccess(ThreadState
*thr
, uptr pc
, uptr addr
,
697 int kAccessSizeLog
, bool kAccessIsWrite
, bool kIsAtomic
);
698 void MemoryAccessImpl(ThreadState
*thr
, uptr addr
,
699 int kAccessSizeLog
, bool kAccessIsWrite
, bool kIsAtomic
,
700 u64
*shadow_mem
, Shadow cur
);
701 void MemoryAccessRange(ThreadState
*thr
, uptr pc
, uptr addr
,
702 uptr size
, bool is_write
);
703 void MemoryAccessRangeStep(ThreadState
*thr
, uptr pc
, uptr addr
,
704 uptr size
, uptr step
, bool is_write
);
705 void UnalignedMemoryAccess(ThreadState
*thr
, uptr pc
, uptr addr
,
706 int size
, bool kAccessIsWrite
, bool kIsAtomic
);
708 const int kSizeLog1
= 0;
709 const int kSizeLog2
= 1;
710 const int kSizeLog4
= 2;
711 const int kSizeLog8
= 3;
713 void ALWAYS_INLINE
MemoryRead(ThreadState
*thr
, uptr pc
,
714 uptr addr
, int kAccessSizeLog
) {
715 MemoryAccess(thr
, pc
, addr
, kAccessSizeLog
, false, false);
718 void ALWAYS_INLINE
MemoryWrite(ThreadState
*thr
, uptr pc
,
719 uptr addr
, int kAccessSizeLog
) {
720 MemoryAccess(thr
, pc
, addr
, kAccessSizeLog
, true, false);
723 void ALWAYS_INLINE
MemoryReadAtomic(ThreadState
*thr
, uptr pc
,
724 uptr addr
, int kAccessSizeLog
) {
725 MemoryAccess(thr
, pc
, addr
, kAccessSizeLog
, false, true);
728 void ALWAYS_INLINE
MemoryWriteAtomic(ThreadState
*thr
, uptr pc
,
729 uptr addr
, int kAccessSizeLog
) {
730 MemoryAccess(thr
, pc
, addr
, kAccessSizeLog
, true, true);
733 void MemoryResetRange(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
734 void MemoryRangeFreed(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
735 void MemoryRangeImitateWrite(ThreadState
*thr
, uptr pc
, uptr addr
, uptr size
);
737 void ThreadIgnoreBegin(ThreadState
*thr
, uptr pc
, bool save_stack
= true);
738 void ThreadIgnoreEnd(ThreadState
*thr
, uptr pc
);
739 void ThreadIgnoreSyncBegin(ThreadState
*thr
, uptr pc
, bool save_stack
= true);
740 void ThreadIgnoreSyncEnd(ThreadState
*thr
, uptr pc
);
742 void FuncEntry(ThreadState
*thr
, uptr pc
);
743 void FuncExit(ThreadState
*thr
);
745 int ThreadCreate(ThreadState
*thr
, uptr pc
, uptr uid
, bool detached
);
746 void ThreadStart(ThreadState
*thr
, int tid
, tid_t os_id
, bool workerthread
);
747 void ThreadFinish(ThreadState
*thr
);
748 int ThreadTid(ThreadState
*thr
, uptr pc
, uptr uid
);
749 void ThreadJoin(ThreadState
*thr
, uptr pc
, int tid
);
750 void ThreadDetach(ThreadState
*thr
, uptr pc
, int tid
);
751 void ThreadFinalize(ThreadState
*thr
);
752 void ThreadSetName(ThreadState
*thr
, const char *name
);
753 int ThreadCount(ThreadState
*thr
);
754 void ProcessPendingSignals(ThreadState
*thr
);
756 Processor
*ProcCreate();
757 void ProcDestroy(Processor
*proc
);
758 void ProcWire(Processor
*proc
, ThreadState
*thr
);
759 void ProcUnwire(Processor
*proc
, ThreadState
*thr
);
761 // Note: the parameter is called flagz, because flags is already taken
762 // by the global function that returns flags.
763 void MutexCreate(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
764 void MutexDestroy(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
765 void MutexPreLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
766 void MutexPostLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0,
768 int MutexUnlock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
769 void MutexPreReadLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
770 void MutexPostReadLock(ThreadState
*thr
, uptr pc
, uptr addr
, u32 flagz
= 0);
771 void MutexReadUnlock(ThreadState
*thr
, uptr pc
, uptr addr
);
772 void MutexReadOrWriteUnlock(ThreadState
*thr
, uptr pc
, uptr addr
);
773 void MutexRepair(ThreadState
*thr
, uptr pc
, uptr addr
); // call on EOWNERDEAD
774 void MutexInvalidAccess(ThreadState
*thr
, uptr pc
, uptr addr
);
776 void Acquire(ThreadState
*thr
, uptr pc
, uptr addr
);
777 // AcquireGlobal synchronizes the current thread with all other threads.
778 // In terms of happens-before relation, it draws a HB edge from all threads
779 // (where they happen to execute right now) to the current thread. We use it to
780 // handle Go finalizers. Namely, finalizer goroutine executes AcquireGlobal
781 // right before executing finalizers. This provides a coarse, but simple
782 // approximation of the actual required synchronization.
783 void AcquireGlobal(ThreadState
*thr
, uptr pc
);
784 void Release(ThreadState
*thr
, uptr pc
, uptr addr
);
785 void ReleaseStore(ThreadState
*thr
, uptr pc
, uptr addr
);
786 void AfterSleep(ThreadState
*thr
, uptr pc
);
787 void AcquireImpl(ThreadState
*thr
, uptr pc
, SyncClock
*c
);
788 void ReleaseImpl(ThreadState
*thr
, uptr pc
, SyncClock
*c
);
789 void ReleaseStoreImpl(ThreadState
*thr
, uptr pc
, SyncClock
*c
);
790 void AcquireReleaseImpl(ThreadState
*thr
, uptr pc
, SyncClock
*c
);
792 // The hacky call uses custom calling convention and an assembly thunk.
793 // It is considerably faster that a normal call for the caller
794 // if it is not executed (it is intended for slow paths from hot functions).
795 // The trick is that the call preserves all registers and the compiler
796 // does not treat it as a call.
797 // If it does not work for you, use normal call.
798 #if !SANITIZER_DEBUG && defined(__x86_64__) && !SANITIZER_MAC
799 // The caller may not create the stack frame for itself at all,
800 // so we create a reserve stack frame for it (1024b must be enough).
801 #define HACKY_CALL(f) \
802 __asm__ __volatile__("sub $1024, %%rsp;" \
803 CFI_INL_ADJUST_CFA_OFFSET(1024) \
804 ".hidden " #f "_thunk;" \
805 "call " #f "_thunk;" \
806 "add $1024, %%rsp;" \
807 CFI_INL_ADJUST_CFA_OFFSET(-1024) \
810 #define HACKY_CALL(f) f()
813 void TraceSwitch(ThreadState
*thr
);
814 uptr
TraceTopPC(ThreadState
*thr
);
817 Trace
*ThreadTrace(int tid
);
819 extern "C" void __tsan_trace_switch();
820 void ALWAYS_INLINE
TraceAddEvent(ThreadState
*thr
, FastState fs
,
821 EventType typ
, u64 addr
) {
822 if (!kCollectHistory
)
824 DCHECK_GE((int)typ
, 0);
825 DCHECK_LE((int)typ
, 7);
826 DCHECK_EQ(GetLsb(addr
, kEventPCBits
), addr
);
827 StatInc(thr
, StatEvents
);
828 u64 pos
= fs
.GetTracePos();
829 if (UNLIKELY((pos
% kTracePartSize
) == 0)) {
831 HACKY_CALL(__tsan_trace_switch
);
836 Event
*trace
= (Event
*)GetThreadTrace(fs
.tid());
837 Event
*evp
= &trace
[pos
];
838 Event ev
= (u64
)addr
| ((u64
)typ
<< kEventPCBits
);
843 uptr ALWAYS_INLINE
HeapEnd() {
844 return HeapMemEnd() + PrimaryAllocator::AdditionalSize();
848 } // namespace __tsan