Make MicroBlaze support DWARF EH (old Xilinx patch, needed for glibc build).
[official-gcc.git] / libsanitizer / tsan / tsan_rtl_report.cc
blob8f2882485e61ee1d7c023d1f291dde8d8ab72dfe
1 //===-- tsan_rtl_report.cc ------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
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"
18 #include "tsan_rtl.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"
25 #include "tsan_fd.h"
27 namespace __tsan {
29 using namespace __sanitizer; // NOLINT
31 static ReportStack *SymbolizeStack(StackTrace trace);
33 void TsanCheckFailed(const char *file, int line, const char *cond,
34 u64 v1, u64 v2) {
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;
39 #if !SANITIZER_GO
40 cur_thread()->ignore_sync++;
41 cur_thread()->ignore_reads_and_writes++;
42 #endif
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());
47 Die();
50 // Can be overriden by an application/test to intercept reports.
51 #ifdef TSAN_EXTERNAL_HOOKS
52 bool OnReport(const ReportDesc *rep, bool suppressed);
53 #else
54 SANITIZER_WEAK_CXX_DEFAULT_IMPL
55 bool OnReport(const ReportDesc *rep, bool suppressed) {
56 (void)rep;
57 return suppressed;
59 #endif
61 SANITIZER_WEAK_DEFAULT_IMPL
62 void __tsan_on_report(const ReportDesc *rep) {
63 (void)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;
71 last_frame = cur;
74 if (last_frame2 == 0)
75 return;
76 #if !SANITIZER_GO
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
96 // due to our fault.
97 DPrintf("Bottom stack frame is missed\n");
99 #else
100 // The last frame always point into runtime (gosched0, goexit0, runtime.main).
101 last_frame->ClearAll();
102 last_frame2->next = nullptr;
103 #endif
106 ReportStack *SymbolizeStackId(u32 stack_id) {
107 if (stack_id == 0)
108 return 0;
109 StackTrace stack = StackDepotGet(stack_id);
110 if (stack.trace == nullptr)
111 return nullptr;
112 return SymbolizeStack(stack);
115 static ReportStack *SymbolizeStack(StackTrace trace) {
116 if (trace.size == 0)
117 return 0;
118 SymbolizedStack *top = nullptr;
119 for (uptr si = 0; si < trace.size; si++) {
120 const uptr pc = trace.trace[si];
121 uptr pc1 = pc;
122 // We obtain the return address, but we're interested in the previous
123 // instruction.
124 if ((pc & kExternalPCBit) == 0)
125 pc1 = StackTrace::GetPreviousInstructionPc(pc);
126 SymbolizedStack *ent = SymbolizeCode(pc1);
127 CHECK_NE(ent, 0);
128 SymbolizedStack *last = ent;
129 while (last->next) {
130 last->info.address = pc; // restore original pc for report
131 last = last->next;
133 last->info.address = pc; // restore original pc for report
134 last->next = top;
135 top = ent;
137 StackStripMain(top);
139 ReportStack *stack = ReportStack::New();
140 stack->frames = top;
141 return stack;
144 ScopedReport::ScopedReport(ReportType typ) {
145 ctx->thread_registry->CheckLocked();
146 void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
147 rep_ = new(mem) ReportDesc;
148 rep_->typ = typ;
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);
170 mop->tid = s.tid();
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);
176 if (mop->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)
193 return;
195 void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
196 ReportThread *rt = new(mem) ReportThread;
197 rep_->threads.PushBack(rt);
198 rt->id = tctx->tid;
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;
203 rt->stack = 0;
204 rt->stack = SymbolizeStackId(tctx->creation_stack_id);
205 if (rt->stack)
206 rt->stack->suppressable = suppressable;
209 #if !SANITIZER_GO
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)
232 return false;
233 ThreadState *thr = tctx->thr;
234 CHECK(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,
243 (void*)addr));
244 if (!tctx)
245 return 0;
246 ThreadState *thr = tctx->thr;
247 CHECK(thr);
248 *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
249 return tctx;
251 #endif
253 void ScopedReport::AddThread(int unique_tid, bool suppressable) {
254 #if !SANITIZER_GO
255 if (const ThreadContext *tctx = FindThreadByUidLocked(unique_tid))
256 AddThread(tctx, suppressable);
257 #endif
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)
263 return;
265 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
266 ReportMutex *rm = new(mem) ReportMutex;
267 rep_->mutexes.PushBack(rm);
268 rm->id = s->uid;
269 rm->addr = s->addr;
270 rm->destroyed = false;
271 rm->stack = SymbolizeStackId(s->creation_stack_id);
274 u64 ScopedReport::AddMutex(u64 id) {
275 u64 uid = 0;
276 u64 mid = 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)) {
283 mid = s->uid;
284 AddMutex(s);
285 } else {
286 AddDeadMutex(id);
288 if (s)
289 s->mtx.Unlock();
290 return mid;
293 void ScopedReport::AddDeadMutex(u64 id) {
294 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
295 if (rep_->mutexes[i]->id == id)
296 return;
298 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
299 ReportMutex *rm = new(mem) ReportMutex;
300 rep_->mutexes.PushBack(rm);
301 rm->id = id;
302 rm->addr = 0;
303 rm->destroyed = true;
304 rm->stack = 0;
307 void ScopedReport::AddLocation(uptr addr, uptr size) {
308 if (addr == 0)
309 return;
310 #if !SANITIZER_GO
311 int fd = -1;
312 int creat_tid = -1;
313 u32 creat_stack = 0;
314 if (FdLocation(addr, &fd, &creat_tid, &creat_stack)) {
315 ReportLocation *loc = ReportLocation::New(ReportLocationFD);
316 loc->fd = fd;
317 loc->tid = creat_tid;
318 loc->stack = SymbolizeStackId(creat_stack);
319 rep_->locs.PushBack(loc);
320 ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
321 if (tctx)
322 AddThread(tctx);
323 return;
325 MBlock *b = 0;
326 Allocator *a = allocator();
327 if (a->PointerIsMine((void*)addr)) {
328 void *block_begin = a->GetBlockBegin((void*)addr);
329 if (block_begin)
330 b = ctx->metamap.GetBlock((uptr)block_begin);
332 if (b != 0) {
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);
340 if (tctx)
341 AddThread(tctx);
342 return;
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);
350 AddThread(tctx);
352 #endif
353 if (ReportLocation *loc = SymbolizeData(addr)) {
354 loc->suppressable = true;
355 rep_->locs.PushBack(loc);
356 return;
360 #if !SANITIZER_GO
361 void ScopedReport::AddSleep(u32 stack_id) {
362 rep_->sleep = SymbolizeStackId(stack_id);
364 #endif
366 void ScopedReport::SetCount(int count) {
367 rep_->count = count;
370 const ReportDesc *ScopedReport::GetReport() const {
371 return rep_;
374 void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
375 MutexSet *mset) {
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)
384 return;
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]);
397 if (mset)
398 *mset = hdr->mset0;
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) {
407 stack[pos] = pc;
408 } else if (typ == EventTypeFuncEnter) {
409 if (stack.Size() < pos + 2)
410 stack.Resize(pos + 2);
411 stack[pos++] = pc;
412 } else if (typ == EventTypeFuncExit) {
413 if (pos > 0)
414 pos--;
416 if (mset) {
417 if (typ == EventTypeLock) {
418 mset->Add(pc, true, epoch0 + i);
419 } else if (typ == EventTypeUnlock) {
420 mset->Del(pc, true);
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)
431 return;
432 pos++;
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;
439 RacyStacks hash;
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]) {
449 VPrintf(2,
450 "ThreadSanitizer: suppressing report as doubled (stack)\n");
451 equal_stack = true;
452 break;
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;
464 break;
469 if (!equal_stack && !equal_address)
470 return false;
471 if (!equal_stack) {
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);
479 return true;
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) {
486 RacyStacks hash;
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)
499 return false;
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;
505 uptr pc_or_addr = 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;
524 if (suppressed) {
525 thr->current_report = nullptr;
526 return false;
529 PrintReport(rep);
530 __tsan_on_report(rep);
531 ctx->nreported++;
532 if (flags()->halt_on_error)
533 Die();
534 thr->current_report = nullptr;
535 return true;
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)
542 continue;
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) {
546 if (s->supp)
547 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);
548 return true;
552 return false;
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)
559 continue;
560 FiredSuppression *s = &ctx->fired_suppressions[k];
561 if (addr == s->pc_or_addr) {
562 if (s->supp)
563 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);
564 return true;
567 return false;
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())
575 return true;
576 if (s0.IsAtomic() && s1.IsFreed())
577 return true;
578 if (s1.IsAtomic() && thr->is_freeing)
579 return true;
580 return false;
583 void ReportRace(ThreadState *thr) {
584 CheckNoLocks(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)
591 return;
592 if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
593 return;
595 bool freed = false;
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);
603 uptr addr_min = 0;
604 uptr addr_max = 0;
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))
613 return;
616 ReportType typ = ReportTypeRace;
617 if (thr->is_vptr_access && freed)
618 typ = ReportTypeVptrUseAfterFree;
619 else if (thr->is_vptr_access)
620 typ = ReportTypeVptrRace;
621 else if (freed)
622 typ = ReportTypeUseAfterFree;
624 if (IsFiredSuppression(ctx, typ, addr))
625 return;
627 const uptr kMop = 2;
628 VarSizeStackTrace traces[kMop];
629 const uptr toppc = TraceTopPC(thr);
630 ObtainCurrentStack(thr, toppc, &traces[0]);
631 if (IsFiredSuppression(ctx, typ, traces[0]))
632 return;
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]))
642 return;
644 if (HandleRacyStacks(thr, traces, addr_min, addr_max))
645 return;
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)
659 continue;
660 rep.AddThread(tctx);
663 rep.AddLocation(addr_min, addr_max - addr_min);
665 #if !SANITIZER_GO
666 { // NOLINT
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);
671 #endif
673 if (!OutputReport(thr, rep))
674 return;
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.
692 ALWAYS_INLINE
693 void PrintCurrentStackSlow(uptr pc) {
694 #if !SANITIZER_GO
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));
705 #endif
708 } // namespace __tsan
710 using namespace __tsan;
712 extern "C" {
713 SANITIZER_INTERFACE_ATTRIBUTE
714 void __sanitizer_print_stack_trace() {
715 PrintCurrentStackSlow(StackTrace::GetCurrentPc());
717 } // extern "C"