1 //===-- tsan_rtl_report.cc ------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_libc.h"
13 #include "sanitizer_common/sanitizer_placement_new.h"
14 #include "sanitizer_common/sanitizer_stackdepot.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_stacktrace.h"
17 #include "tsan_platform.h"
19 #include "tsan_suppressions.h"
20 #include "tsan_symbolize.h"
21 #include "tsan_report.h"
22 #include "tsan_sync.h"
23 #include "tsan_mman.h"
24 #include "tsan_flags.h"
29 using namespace __sanitizer
; // NOLINT
31 static ReportStack
*SymbolizeStack(StackTrace trace
);
33 void TsanCheckFailed(const char *file
, int line
, const char *cond
,
35 // There is high probability that interceptors will check-fail as well,
36 // on the other hand there is no sense in processing interceptors
37 // since we are going to die soon.
38 ScopedIgnoreInterceptors ignore
;
40 cur_thread()->ignore_sync
++;
41 cur_thread()->ignore_reads_and_writes
++;
43 Printf("FATAL: ThreadSanitizer CHECK failed: "
44 "%s:%d \"%s\" (0x%zx, 0x%zx)\n",
45 file
, line
, cond
, (uptr
)v1
, (uptr
)v2
);
46 PrintCurrentStackSlow(StackTrace::GetCurrentPc());
50 // Can be overriden by an application/test to intercept reports.
51 #ifdef TSAN_EXTERNAL_HOOKS
52 bool OnReport(const ReportDesc
*rep
, bool suppressed
);
54 SANITIZER_WEAK_CXX_DEFAULT_IMPL
55 bool OnReport(const ReportDesc
*rep
, bool suppressed
) {
61 SANITIZER_WEAK_DEFAULT_IMPL
62 void __tsan_on_report(const ReportDesc
*rep
) {
66 static void StackStripMain(SymbolizedStack
*frames
) {
67 SymbolizedStack
*last_frame
= nullptr;
68 SymbolizedStack
*last_frame2
= nullptr;
69 for (SymbolizedStack
*cur
= frames
; cur
; cur
= cur
->next
) {
70 last_frame2
= last_frame
;
77 const char *last
= last_frame
->info
.function
;
78 const char *last2
= last_frame2
->info
.function
;
79 // Strip frame above 'main'
80 if (last2
&& 0 == internal_strcmp(last2
, "main")) {
81 last_frame
->ClearAll();
82 last_frame2
->next
= nullptr;
83 // Strip our internal thread start routine.
84 } else if (last
&& 0 == internal_strcmp(last
, "__tsan_thread_start_func")) {
85 last_frame
->ClearAll();
86 last_frame2
->next
= nullptr;
87 // Strip global ctors init.
88 } else if (last
&& 0 == internal_strcmp(last
, "__do_global_ctors_aux")) {
89 last_frame
->ClearAll();
90 last_frame2
->next
= nullptr;
91 // If both are 0, then we probably just failed to symbolize.
92 } else if (last
|| last2
) {
93 // Ensure that we recovered stack completely. Trimmed stack
94 // can actually happen if we do not instrument some code,
95 // so it's only a debug print. However we must try hard to not miss it
97 DPrintf("Bottom stack frame is missed\n");
100 // The last frame always point into runtime (gosched0, goexit0, runtime.main).
101 last_frame
->ClearAll();
102 last_frame2
->next
= nullptr;
106 ReportStack
*SymbolizeStackId(u32 stack_id
) {
109 StackTrace stack
= StackDepotGet(stack_id
);
110 if (stack
.trace
== nullptr)
112 return SymbolizeStack(stack
);
115 static ReportStack
*SymbolizeStack(StackTrace trace
) {
118 SymbolizedStack
*top
= nullptr;
119 for (uptr si
= 0; si
< trace
.size
; si
++) {
120 const uptr pc
= trace
.trace
[si
];
122 // We obtain the return address, but we're interested in the previous
124 if ((pc
& kExternalPCBit
) == 0)
125 pc1
= StackTrace::GetPreviousInstructionPc(pc
);
126 SymbolizedStack
*ent
= SymbolizeCode(pc1
);
128 SymbolizedStack
*last
= ent
;
130 last
->info
.address
= pc
; // restore original pc for report
133 last
->info
.address
= pc
; // restore original pc for report
139 ReportStack
*stack
= ReportStack::New();
144 ScopedReport::ScopedReport(ReportType typ
) {
145 ctx
->thread_registry
->CheckLocked();
146 void *mem
= internal_alloc(MBlockReport
, sizeof(ReportDesc
));
147 rep_
= new(mem
) ReportDesc
;
149 ctx
->report_mtx
.Lock();
150 CommonSanitizerReportMutex
.Lock();
153 ScopedReport::~ScopedReport() {
154 CommonSanitizerReportMutex
.Unlock();
155 ctx
->report_mtx
.Unlock();
156 DestroyAndFree(rep_
);
159 void ScopedReport::AddStack(StackTrace stack
, bool suppressable
) {
160 ReportStack
**rs
= rep_
->stacks
.PushBack();
161 *rs
= SymbolizeStack(stack
);
162 (*rs
)->suppressable
= suppressable
;
165 void ScopedReport::AddMemoryAccess(uptr addr
, Shadow s
, StackTrace stack
,
166 const MutexSet
*mset
) {
167 void *mem
= internal_alloc(MBlockReportMop
, sizeof(ReportMop
));
168 ReportMop
*mop
= new(mem
) ReportMop
;
169 rep_
->mops
.PushBack(mop
);
171 mop
->addr
= addr
+ s
.addr0();
172 mop
->size
= s
.size();
173 mop
->write
= s
.IsWrite();
174 mop
->atomic
= s
.IsAtomic();
175 mop
->stack
= SymbolizeStack(stack
);
177 mop
->stack
->suppressable
= true;
178 for (uptr i
= 0; i
< mset
->Size(); i
++) {
179 MutexSet::Desc d
= mset
->Get(i
);
180 u64 mid
= this->AddMutex(d
.id
);
181 ReportMopMutex mtx
= {mid
, d
.write
};
182 mop
->mset
.PushBack(mtx
);
186 void ScopedReport::AddUniqueTid(int unique_tid
) {
187 rep_
->unique_tids
.PushBack(unique_tid
);
190 void ScopedReport::AddThread(const ThreadContext
*tctx
, bool suppressable
) {
191 for (uptr i
= 0; i
< rep_
->threads
.Size(); i
++) {
192 if ((u32
)rep_
->threads
[i
]->id
== tctx
->tid
)
195 void *mem
= internal_alloc(MBlockReportThread
, sizeof(ReportThread
));
196 ReportThread
*rt
= new(mem
) ReportThread
;
197 rep_
->threads
.PushBack(rt
);
199 rt
->os_id
= tctx
->os_id
;
200 rt
->running
= (tctx
->status
== ThreadStatusRunning
);
201 rt
->name
= internal_strdup(tctx
->name
);
202 rt
->parent_tid
= tctx
->parent_tid
;
204 rt
->stack
= SymbolizeStackId(tctx
->creation_stack_id
);
206 rt
->stack
->suppressable
= suppressable
;
210 static bool FindThreadByUidLockedCallback(ThreadContextBase
*tctx
, void *arg
) {
211 int unique_id
= *(int *)arg
;
212 return tctx
->unique_id
== (u32
)unique_id
;
215 static ThreadContext
*FindThreadByUidLocked(int unique_id
) {
216 ctx
->thread_registry
->CheckLocked();
217 return static_cast<ThreadContext
*>(
218 ctx
->thread_registry
->FindThreadContextLocked(
219 FindThreadByUidLockedCallback
, &unique_id
));
222 static ThreadContext
*FindThreadByTidLocked(int tid
) {
223 ctx
->thread_registry
->CheckLocked();
224 return static_cast<ThreadContext
*>(
225 ctx
->thread_registry
->GetThreadLocked(tid
));
228 static bool IsInStackOrTls(ThreadContextBase
*tctx_base
, void *arg
) {
229 uptr addr
= (uptr
)arg
;
230 ThreadContext
*tctx
= static_cast<ThreadContext
*>(tctx_base
);
231 if (tctx
->status
!= ThreadStatusRunning
)
233 ThreadState
*thr
= tctx
->thr
;
235 return ((addr
>= thr
->stk_addr
&& addr
< thr
->stk_addr
+ thr
->stk_size
) ||
236 (addr
>= thr
->tls_addr
&& addr
< thr
->tls_addr
+ thr
->tls_size
));
239 ThreadContext
*IsThreadStackOrTls(uptr addr
, bool *is_stack
) {
240 ctx
->thread_registry
->CheckLocked();
241 ThreadContext
*tctx
= static_cast<ThreadContext
*>(
242 ctx
->thread_registry
->FindThreadContextLocked(IsInStackOrTls
,
246 ThreadState
*thr
= tctx
->thr
;
248 *is_stack
= (addr
>= thr
->stk_addr
&& addr
< thr
->stk_addr
+ thr
->stk_size
);
253 void ScopedReport::AddThread(int unique_tid
, bool suppressable
) {
255 if (const ThreadContext
*tctx
= FindThreadByUidLocked(unique_tid
))
256 AddThread(tctx
, suppressable
);
260 void ScopedReport::AddMutex(const SyncVar
*s
) {
261 for (uptr i
= 0; i
< rep_
->mutexes
.Size(); i
++) {
262 if (rep_
->mutexes
[i
]->id
== s
->uid
)
265 void *mem
= internal_alloc(MBlockReportMutex
, sizeof(ReportMutex
));
266 ReportMutex
*rm
= new(mem
) ReportMutex
;
267 rep_
->mutexes
.PushBack(rm
);
270 rm
->destroyed
= false;
271 rm
->stack
= SymbolizeStackId(s
->creation_stack_id
);
274 u64
ScopedReport::AddMutex(u64 id
) {
277 uptr addr
= SyncVar::SplitId(id
, &uid
);
278 SyncVar
*s
= ctx
->metamap
.GetIfExistsAndLock(addr
, true);
279 // Check that the mutex is still alive.
280 // Another mutex can be created at the same address,
281 // so check uid as well.
282 if (s
&& s
->CheckId(uid
)) {
293 void ScopedReport::AddDeadMutex(u64 id
) {
294 for (uptr i
= 0; i
< rep_
->mutexes
.Size(); i
++) {
295 if (rep_
->mutexes
[i
]->id
== id
)
298 void *mem
= internal_alloc(MBlockReportMutex
, sizeof(ReportMutex
));
299 ReportMutex
*rm
= new(mem
) ReportMutex
;
300 rep_
->mutexes
.PushBack(rm
);
303 rm
->destroyed
= true;
307 void ScopedReport::AddLocation(uptr addr
, uptr size
) {
314 if (FdLocation(addr
, &fd
, &creat_tid
, &creat_stack
)) {
315 ReportLocation
*loc
= ReportLocation::New(ReportLocationFD
);
317 loc
->tid
= creat_tid
;
318 loc
->stack
= SymbolizeStackId(creat_stack
);
319 rep_
->locs
.PushBack(loc
);
320 ThreadContext
*tctx
= FindThreadByUidLocked(creat_tid
);
326 Allocator
*a
= allocator();
327 if (a
->PointerIsMine((void*)addr
)) {
328 void *block_begin
= a
->GetBlockBegin((void*)addr
);
330 b
= ctx
->metamap
.GetBlock((uptr
)block_begin
);
333 ThreadContext
*tctx
= FindThreadByTidLocked(b
->tid
);
334 ReportLocation
*loc
= ReportLocation::New(ReportLocationHeap
);
335 loc
->heap_chunk_start
= (uptr
)allocator()->GetBlockBegin((void *)addr
);
336 loc
->heap_chunk_size
= b
->siz
;
337 loc
->tid
= tctx
? tctx
->tid
: b
->tid
;
338 loc
->stack
= SymbolizeStackId(b
->stk
);
339 rep_
->locs
.PushBack(loc
);
344 bool is_stack
= false;
345 if (ThreadContext
*tctx
= IsThreadStackOrTls(addr
, &is_stack
)) {
346 ReportLocation
*loc
=
347 ReportLocation::New(is_stack
? ReportLocationStack
: ReportLocationTLS
);
348 loc
->tid
= tctx
->tid
;
349 rep_
->locs
.PushBack(loc
);
353 if (ReportLocation
*loc
= SymbolizeData(addr
)) {
354 loc
->suppressable
= true;
355 rep_
->locs
.PushBack(loc
);
361 void ScopedReport::AddSleep(u32 stack_id
) {
362 rep_
->sleep
= SymbolizeStackId(stack_id
);
366 void ScopedReport::SetCount(int count
) {
370 const ReportDesc
*ScopedReport::GetReport() const {
374 void RestoreStack(int tid
, const u64 epoch
, VarSizeStackTrace
*stk
,
376 // This function restores stack trace and mutex set for the thread/epoch.
377 // It does so by getting stack trace and mutex set at the beginning of
378 // trace part, and then replaying the trace till the given epoch.
379 Trace
* trace
= ThreadTrace(tid
);
380 ReadLock
l(&trace
->mtx
);
381 const int partidx
= (epoch
/ kTracePartSize
) % TraceParts();
382 TraceHeader
* hdr
= &trace
->headers
[partidx
];
383 if (epoch
< hdr
->epoch0
|| epoch
>= hdr
->epoch0
+ kTracePartSize
)
385 CHECK_EQ(RoundDown(epoch
, kTracePartSize
), hdr
->epoch0
);
386 const u64 epoch0
= RoundDown(epoch
, TraceSize());
387 const u64 eend
= epoch
% TraceSize();
388 const u64 ebegin
= RoundDown(eend
, kTracePartSize
);
389 DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
390 tid
, (uptr
)epoch
, (uptr
)ebegin
, (uptr
)eend
, partidx
);
391 Vector
<uptr
> stack(MBlockReportStack
);
392 stack
.Resize(hdr
->stack0
.size
+ 64);
393 for (uptr i
= 0; i
< hdr
->stack0
.size
; i
++) {
394 stack
[i
] = hdr
->stack0
.trace
[i
];
395 DPrintf2(" #%02zu: pc=%zx\n", i
, stack
[i
]);
399 uptr pos
= hdr
->stack0
.size
;
400 Event
*events
= (Event
*)GetThreadTrace(tid
);
401 for (uptr i
= ebegin
; i
<= eend
; i
++) {
402 Event ev
= events
[i
];
403 EventType typ
= (EventType
)(ev
>> 61);
404 uptr pc
= (uptr
)(ev
& ((1ull << 61) - 1));
405 DPrintf2(" %zu typ=%d pc=%zx\n", i
, typ
, pc
);
406 if (typ
== EventTypeMop
) {
408 } else if (typ
== EventTypeFuncEnter
) {
409 if (stack
.Size() < pos
+ 2)
410 stack
.Resize(pos
+ 2);
412 } else if (typ
== EventTypeFuncExit
) {
417 if (typ
== EventTypeLock
) {
418 mset
->Add(pc
, true, epoch0
+ i
);
419 } else if (typ
== EventTypeUnlock
) {
421 } else if (typ
== EventTypeRLock
) {
422 mset
->Add(pc
, false, epoch0
+ i
);
423 } else if (typ
== EventTypeRUnlock
) {
424 mset
->Del(pc
, false);
427 for (uptr j
= 0; j
<= pos
; j
++)
428 DPrintf2(" #%zu: %zx\n", j
, stack
[j
]);
430 if (pos
== 0 && stack
[0] == 0)
433 stk
->Init(&stack
[0], pos
);
436 static bool HandleRacyStacks(ThreadState
*thr
, VarSizeStackTrace traces
[2],
437 uptr addr_min
, uptr addr_max
) {
438 bool equal_stack
= false;
440 bool equal_address
= false;
441 RacyAddress ra0
= {addr_min
, addr_max
};
443 ReadLock
lock(&ctx
->racy_mtx
);
444 if (flags()->suppress_equal_stacks
) {
445 hash
.hash
[0] = md5_hash(traces
[0].trace
, traces
[0].size
* sizeof(uptr
));
446 hash
.hash
[1] = md5_hash(traces
[1].trace
, traces
[1].size
* sizeof(uptr
));
447 for (uptr i
= 0; i
< ctx
->racy_stacks
.Size(); i
++) {
448 if (hash
== ctx
->racy_stacks
[i
]) {
450 "ThreadSanitizer: suppressing report as doubled (stack)\n");
456 if (flags()->suppress_equal_addresses
) {
457 for (uptr i
= 0; i
< ctx
->racy_addresses
.Size(); i
++) {
458 RacyAddress ra2
= ctx
->racy_addresses
[i
];
459 uptr maxbeg
= max(ra0
.addr_min
, ra2
.addr_min
);
460 uptr minend
= min(ra0
.addr_max
, ra2
.addr_max
);
461 if (maxbeg
< minend
) {
462 VPrintf(2, "ThreadSanitizer: suppressing report as doubled (addr)\n");
463 equal_address
= true;
469 if (!equal_stack
&& !equal_address
)
472 Lock
lock(&ctx
->racy_mtx
);
473 ctx
->racy_stacks
.PushBack(hash
);
475 if (!equal_address
) {
476 Lock
lock(&ctx
->racy_mtx
);
477 ctx
->racy_addresses
.PushBack(ra0
);
482 static void AddRacyStacks(ThreadState
*thr
, VarSizeStackTrace traces
[2],
483 uptr addr_min
, uptr addr_max
) {
484 Lock
lock(&ctx
->racy_mtx
);
485 if (flags()->suppress_equal_stacks
) {
487 hash
.hash
[0] = md5_hash(traces
[0].trace
, traces
[0].size
* sizeof(uptr
));
488 hash
.hash
[1] = md5_hash(traces
[1].trace
, traces
[1].size
* sizeof(uptr
));
489 ctx
->racy_stacks
.PushBack(hash
);
491 if (flags()->suppress_equal_addresses
) {
492 RacyAddress ra0
= {addr_min
, addr_max
};
493 ctx
->racy_addresses
.PushBack(ra0
);
497 bool OutputReport(ThreadState
*thr
, const ScopedReport
&srep
) {
498 if (!flags()->report_bugs
)
500 atomic_store_relaxed(&ctx
->last_symbolize_time_ns
, NanoTime());
501 const ReportDesc
*rep
= srep
.GetReport();
502 CHECK_EQ(thr
->current_report
, nullptr);
503 thr
->current_report
= rep
;
504 Suppression
*supp
= 0;
506 for (uptr i
= 0; pc_or_addr
== 0 && i
< rep
->mops
.Size(); i
++)
507 pc_or_addr
= IsSuppressed(rep
->typ
, rep
->mops
[i
]->stack
, &supp
);
508 for (uptr i
= 0; pc_or_addr
== 0 && i
< rep
->stacks
.Size(); i
++)
509 pc_or_addr
= IsSuppressed(rep
->typ
, rep
->stacks
[i
], &supp
);
510 for (uptr i
= 0; pc_or_addr
== 0 && i
< rep
->threads
.Size(); i
++)
511 pc_or_addr
= IsSuppressed(rep
->typ
, rep
->threads
[i
]->stack
, &supp
);
512 for (uptr i
= 0; pc_or_addr
== 0 && i
< rep
->locs
.Size(); i
++)
513 pc_or_addr
= IsSuppressed(rep
->typ
, rep
->locs
[i
], &supp
);
514 if (pc_or_addr
!= 0) {
515 Lock
lock(&ctx
->fired_suppressions_mtx
);
516 FiredSuppression s
= {srep
.GetReport()->typ
, pc_or_addr
, supp
};
517 ctx
->fired_suppressions
.push_back(s
);
520 bool old_is_freeing
= thr
->is_freeing
;
521 thr
->is_freeing
= false;
522 bool suppressed
= OnReport(rep
, pc_or_addr
!= 0);
523 thr
->is_freeing
= old_is_freeing
;
525 thr
->current_report
= nullptr;
530 __tsan_on_report(rep
);
532 if (flags()->halt_on_error
)
534 thr
->current_report
= nullptr;
538 bool IsFiredSuppression(Context
*ctx
, ReportType type
, StackTrace trace
) {
539 ReadLock
lock(&ctx
->fired_suppressions_mtx
);
540 for (uptr k
= 0; k
< ctx
->fired_suppressions
.size(); k
++) {
541 if (ctx
->fired_suppressions
[k
].type
!= type
)
543 for (uptr j
= 0; j
< trace
.size
; j
++) {
544 FiredSuppression
*s
= &ctx
->fired_suppressions
[k
];
545 if (trace
.trace
[j
] == s
->pc_or_addr
) {
547 atomic_fetch_add(&s
->supp
->hit_count
, 1, memory_order_relaxed
);
555 static bool IsFiredSuppression(Context
*ctx
, ReportType type
, uptr addr
) {
556 ReadLock
lock(&ctx
->fired_suppressions_mtx
);
557 for (uptr k
= 0; k
< ctx
->fired_suppressions
.size(); k
++) {
558 if (ctx
->fired_suppressions
[k
].type
!= type
)
560 FiredSuppression
*s
= &ctx
->fired_suppressions
[k
];
561 if (addr
== s
->pc_or_addr
) {
563 atomic_fetch_add(&s
->supp
->hit_count
, 1, memory_order_relaxed
);
570 static bool RaceBetweenAtomicAndFree(ThreadState
*thr
) {
571 Shadow
s0(thr
->racy_state
[0]);
572 Shadow
s1(thr
->racy_state
[1]);
573 CHECK(!(s0
.IsAtomic() && s1
.IsAtomic()));
574 if (!s0
.IsAtomic() && !s1
.IsAtomic())
576 if (s0
.IsAtomic() && s1
.IsFreed())
578 if (s1
.IsAtomic() && thr
->is_freeing
)
583 void ReportRace(ThreadState
*thr
) {
586 // Symbolizer makes lots of intercepted calls. If we try to process them,
587 // at best it will cause deadlocks on internal mutexes.
588 ScopedIgnoreInterceptors ignore
;
590 if (!flags()->report_bugs
)
592 if (!flags()->report_atomic_races
&& !RaceBetweenAtomicAndFree(thr
))
597 Shadow
s(thr
->racy_state
[1]);
598 freed
= s
.GetFreedAndReset();
599 thr
->racy_state
[1] = s
.raw();
602 uptr addr
= ShadowToMem((uptr
)thr
->racy_shadow_addr
);
606 uptr a0
= addr
+ Shadow(thr
->racy_state
[0]).addr0();
607 uptr a1
= addr
+ Shadow(thr
->racy_state
[1]).addr0();
608 uptr e0
= a0
+ Shadow(thr
->racy_state
[0]).size();
609 uptr e1
= a1
+ Shadow(thr
->racy_state
[1]).size();
610 addr_min
= min(a0
, a1
);
611 addr_max
= max(e0
, e1
);
612 if (IsExpectedReport(addr_min
, addr_max
- addr_min
))
616 ReportType typ
= ReportTypeRace
;
617 if (thr
->is_vptr_access
&& freed
)
618 typ
= ReportTypeVptrUseAfterFree
;
619 else if (thr
->is_vptr_access
)
620 typ
= ReportTypeVptrRace
;
622 typ
= ReportTypeUseAfterFree
;
624 if (IsFiredSuppression(ctx
, typ
, addr
))
628 VarSizeStackTrace traces
[kMop
];
629 const uptr toppc
= TraceTopPC(thr
);
630 ObtainCurrentStack(thr
, toppc
, &traces
[0]);
631 if (IsFiredSuppression(ctx
, typ
, traces
[0]))
634 // MutexSet is too large to live on stack.
635 Vector
<u64
> mset_buffer(MBlockScopedBuf
);
636 mset_buffer
.Resize(sizeof(MutexSet
) / sizeof(u64
) + 1);
637 MutexSet
*mset2
= new(&mset_buffer
[0]) MutexSet();
639 Shadow
s2(thr
->racy_state
[1]);
640 RestoreStack(s2
.tid(), s2
.epoch(), &traces
[1], mset2
);
641 if (IsFiredSuppression(ctx
, typ
, traces
[1]))
644 if (HandleRacyStacks(thr
, traces
, addr_min
, addr_max
))
647 ThreadRegistryLock
l0(ctx
->thread_registry
);
648 ScopedReport
rep(typ
);
649 for (uptr i
= 0; i
< kMop
; i
++) {
650 Shadow
s(thr
->racy_state
[i
]);
651 rep
.AddMemoryAccess(addr
, s
, traces
[i
], i
== 0 ? &thr
->mset
: mset2
);
654 for (uptr i
= 0; i
< kMop
; i
++) {
655 FastState
s(thr
->racy_state
[i
]);
656 ThreadContext
*tctx
= static_cast<ThreadContext
*>(
657 ctx
->thread_registry
->GetThreadLocked(s
.tid()));
658 if (s
.epoch() < tctx
->epoch0
|| s
.epoch() > tctx
->epoch1
)
663 rep
.AddLocation(addr_min
, addr_max
- addr_min
);
667 Shadow
s(thr
->racy_state
[1]);
668 if (s
.epoch() <= thr
->last_sleep_clock
.get(s
.tid()))
669 rep
.AddSleep(thr
->last_sleep_stack_id
);
673 if (!OutputReport(thr
, rep
))
676 AddRacyStacks(thr
, traces
, addr_min
, addr_max
);
679 void PrintCurrentStack(ThreadState
*thr
, uptr pc
) {
680 VarSizeStackTrace trace
;
681 ObtainCurrentStack(thr
, pc
, &trace
);
682 PrintStack(SymbolizeStack(trace
));
685 // Always inlining PrintCurrentStackSlow, because LocatePcInTrace assumes
686 // __sanitizer_print_stack_trace exists in the actual unwinded stack, but
687 // tail-call to PrintCurrentStackSlow breaks this assumption because
688 // __sanitizer_print_stack_trace disappears after tail-call.
689 // However, this solution is not reliable enough, please see dvyukov's comment
690 // http://reviews.llvm.org/D19148#406208
691 // Also see PR27280 comment 2 and 3 for breaking examples and analysis.
693 void PrintCurrentStackSlow(uptr pc
) {
695 BufferedStackTrace
*ptrace
=
696 new(internal_alloc(MBlockStackTrace
, sizeof(BufferedStackTrace
)))
697 BufferedStackTrace();
698 ptrace
->Unwind(kStackTraceMax
, pc
, 0, 0, 0, 0, false);
699 for (uptr i
= 0; i
< ptrace
->size
/ 2; i
++) {
700 uptr tmp
= ptrace
->trace_buffer
[i
];
701 ptrace
->trace_buffer
[i
] = ptrace
->trace_buffer
[ptrace
->size
- i
- 1];
702 ptrace
->trace_buffer
[ptrace
->size
- i
- 1] = tmp
;
704 PrintStack(SymbolizeStack(*ptrace
));
708 } // namespace __tsan
710 using namespace __tsan
;
713 SANITIZER_INTERFACE_ATTRIBUTE
714 void __sanitizer_print_stack_trace() {
715 PrintCurrentStackSlow(StackTrace::GetCurrentPc());