2016-03-10 Andre Vieira <andre.simoesdiasvieira@arm.com>
[official-gcc.git] / libsanitizer / tsan / tsan_rtl_report.cc
blobd0d1fbaf45ac526fd0c99468b6556e9769819323
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 Printf("FATAL: ThreadSanitizer CHECK failed: "
40 "%s:%d \"%s\" (0x%zx, 0x%zx)\n",
41 file, line, cond, (uptr)v1, (uptr)v2);
42 PrintCurrentStackSlow(StackTrace::GetCurrentPc());
43 Die();
46 // Can be overriden by an application/test to intercept reports.
47 #ifdef TSAN_EXTERNAL_HOOKS
48 bool OnReport(const ReportDesc *rep, bool suppressed);
49 #else
50 SANITIZER_INTERFACE_ATTRIBUTE
51 bool WEAK OnReport(const ReportDesc *rep, bool suppressed) {
52 (void)rep;
53 return suppressed;
55 #endif
57 static void StackStripMain(SymbolizedStack *frames) {
58 SymbolizedStack *last_frame = nullptr;
59 SymbolizedStack *last_frame2 = nullptr;
60 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
61 last_frame2 = last_frame;
62 last_frame = cur;
65 if (last_frame2 == 0)
66 return;
67 #ifndef SANITIZER_GO
68 const char *last = last_frame->info.function;
69 const char *last2 = last_frame2->info.function;
70 // Strip frame above 'main'
71 if (last2 && 0 == internal_strcmp(last2, "main")) {
72 last_frame->ClearAll();
73 last_frame2->next = nullptr;
74 // Strip our internal thread start routine.
75 } else if (last && 0 == internal_strcmp(last, "__tsan_thread_start_func")) {
76 last_frame->ClearAll();
77 last_frame2->next = nullptr;
78 // Strip global ctors init.
79 } else if (last && 0 == internal_strcmp(last, "__do_global_ctors_aux")) {
80 last_frame->ClearAll();
81 last_frame2->next = nullptr;
82 // If both are 0, then we probably just failed to symbolize.
83 } else if (last || last2) {
84 // Ensure that we recovered stack completely. Trimmed stack
85 // can actually happen if we do not instrument some code,
86 // so it's only a debug print. However we must try hard to not miss it
87 // due to our fault.
88 DPrintf("Bottom stack frame is missed\n");
90 #else
91 // The last frame always point into runtime (gosched0, goexit0, runtime.main).
92 last_frame->ClearAll();
93 last_frame2->next = nullptr;
94 #endif
97 ReportStack *SymbolizeStackId(u32 stack_id) {
98 if (stack_id == 0)
99 return 0;
100 StackTrace stack = StackDepotGet(stack_id);
101 if (stack.trace == nullptr)
102 return nullptr;
103 return SymbolizeStack(stack);
106 static ReportStack *SymbolizeStack(StackTrace trace) {
107 if (trace.size == 0)
108 return 0;
109 SymbolizedStack *top = nullptr;
110 for (uptr si = 0; si < trace.size; si++) {
111 const uptr pc = trace.trace[si];
112 uptr pc1 = pc;
113 // We obtain the return address, but we're interested in the previous
114 // instruction.
115 if ((pc & kExternalPCBit) == 0)
116 pc1 = StackTrace::GetPreviousInstructionPc(pc);
117 SymbolizedStack *ent = SymbolizeCode(pc1);
118 CHECK_NE(ent, 0);
119 SymbolizedStack *last = ent;
120 while (last->next) {
121 last->info.address = pc; // restore original pc for report
122 last = last->next;
124 last->info.address = pc; // restore original pc for report
125 last->next = top;
126 top = ent;
128 StackStripMain(top);
130 ReportStack *stack = ReportStack::New();
131 stack->frames = top;
132 return stack;
135 ScopedReport::ScopedReport(ReportType typ) {
136 ctx->thread_registry->CheckLocked();
137 void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
138 rep_ = new(mem) ReportDesc;
139 rep_->typ = typ;
140 ctx->report_mtx.Lock();
141 CommonSanitizerReportMutex.Lock();
144 ScopedReport::~ScopedReport() {
145 CommonSanitizerReportMutex.Unlock();
146 ctx->report_mtx.Unlock();
147 DestroyAndFree(rep_);
150 void ScopedReport::AddStack(StackTrace stack, bool suppressable) {
151 ReportStack **rs = rep_->stacks.PushBack();
152 *rs = SymbolizeStack(stack);
153 (*rs)->suppressable = suppressable;
156 void ScopedReport::AddMemoryAccess(uptr addr, Shadow s, StackTrace stack,
157 const MutexSet *mset) {
158 void *mem = internal_alloc(MBlockReportMop, sizeof(ReportMop));
159 ReportMop *mop = new(mem) ReportMop;
160 rep_->mops.PushBack(mop);
161 mop->tid = s.tid();
162 mop->addr = addr + s.addr0();
163 mop->size = s.size();
164 mop->write = s.IsWrite();
165 mop->atomic = s.IsAtomic();
166 mop->stack = SymbolizeStack(stack);
167 if (mop->stack)
168 mop->stack->suppressable = true;
169 for (uptr i = 0; i < mset->Size(); i++) {
170 MutexSet::Desc d = mset->Get(i);
171 u64 mid = this->AddMutex(d.id);
172 ReportMopMutex mtx = {mid, d.write};
173 mop->mset.PushBack(mtx);
177 void ScopedReport::AddUniqueTid(int unique_tid) {
178 rep_->unique_tids.PushBack(unique_tid);
181 void ScopedReport::AddThread(const ThreadContext *tctx, bool suppressable) {
182 for (uptr i = 0; i < rep_->threads.Size(); i++) {
183 if ((u32)rep_->threads[i]->id == tctx->tid)
184 return;
186 void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
187 ReportThread *rt = new(mem) ReportThread();
188 rep_->threads.PushBack(rt);
189 rt->id = tctx->tid;
190 rt->pid = tctx->os_id;
191 rt->running = (tctx->status == ThreadStatusRunning);
192 rt->name = internal_strdup(tctx->name);
193 rt->parent_tid = tctx->parent_tid;
194 rt->stack = 0;
195 rt->stack = SymbolizeStackId(tctx->creation_stack_id);
196 if (rt->stack)
197 rt->stack->suppressable = suppressable;
200 #ifndef SANITIZER_GO
201 static ThreadContext *FindThreadByUidLocked(int unique_id) {
202 ctx->thread_registry->CheckLocked();
203 for (unsigned i = 0; i < kMaxTid; i++) {
204 ThreadContext *tctx = static_cast<ThreadContext*>(
205 ctx->thread_registry->GetThreadLocked(i));
206 if (tctx && tctx->unique_id == (u32)unique_id) {
207 return tctx;
210 return 0;
213 static ThreadContext *FindThreadByTidLocked(int tid) {
214 ctx->thread_registry->CheckLocked();
215 return static_cast<ThreadContext*>(
216 ctx->thread_registry->GetThreadLocked(tid));
219 static bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {
220 uptr addr = (uptr)arg;
221 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
222 if (tctx->status != ThreadStatusRunning)
223 return false;
224 ThreadState *thr = tctx->thr;
225 CHECK(thr);
226 return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||
227 (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));
230 ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
231 ctx->thread_registry->CheckLocked();
232 ThreadContext *tctx = static_cast<ThreadContext*>(
233 ctx->thread_registry->FindThreadContextLocked(IsInStackOrTls,
234 (void*)addr));
235 if (!tctx)
236 return 0;
237 ThreadState *thr = tctx->thr;
238 CHECK(thr);
239 *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
240 return tctx;
242 #endif
244 void ScopedReport::AddThread(int unique_tid, bool suppressable) {
245 #ifndef SANITIZER_GO
246 if (const ThreadContext *tctx = FindThreadByUidLocked(unique_tid))
247 AddThread(tctx, suppressable);
248 #endif
251 void ScopedReport::AddMutex(const SyncVar *s) {
252 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
253 if (rep_->mutexes[i]->id == s->uid)
254 return;
256 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
257 ReportMutex *rm = new(mem) ReportMutex();
258 rep_->mutexes.PushBack(rm);
259 rm->id = s->uid;
260 rm->addr = s->addr;
261 rm->destroyed = false;
262 rm->stack = SymbolizeStackId(s->creation_stack_id);
265 u64 ScopedReport::AddMutex(u64 id) {
266 u64 uid = 0;
267 u64 mid = id;
268 uptr addr = SyncVar::SplitId(id, &uid);
269 SyncVar *s = ctx->metamap.GetIfExistsAndLock(addr);
270 // Check that the mutex is still alive.
271 // Another mutex can be created at the same address,
272 // so check uid as well.
273 if (s && s->CheckId(uid)) {
274 mid = s->uid;
275 AddMutex(s);
276 } else {
277 AddDeadMutex(id);
279 if (s)
280 s->mtx.Unlock();
281 return mid;
284 void ScopedReport::AddDeadMutex(u64 id) {
285 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
286 if (rep_->mutexes[i]->id == id)
287 return;
289 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
290 ReportMutex *rm = new(mem) ReportMutex();
291 rep_->mutexes.PushBack(rm);
292 rm->id = id;
293 rm->addr = 0;
294 rm->destroyed = true;
295 rm->stack = 0;
298 void ScopedReport::AddLocation(uptr addr, uptr size) {
299 if (addr == 0)
300 return;
301 #ifndef SANITIZER_GO
302 int fd = -1;
303 int creat_tid = -1;
304 u32 creat_stack = 0;
305 if (FdLocation(addr, &fd, &creat_tid, &creat_stack)) {
306 ReportLocation *loc = ReportLocation::New(ReportLocationFD);
307 loc->fd = fd;
308 loc->tid = creat_tid;
309 loc->stack = SymbolizeStackId(creat_stack);
310 rep_->locs.PushBack(loc);
311 ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
312 if (tctx)
313 AddThread(tctx);
314 return;
316 MBlock *b = 0;
317 Allocator *a = allocator();
318 if (a->PointerIsMine((void*)addr)) {
319 void *block_begin = a->GetBlockBegin((void*)addr);
320 if (block_begin)
321 b = ctx->metamap.GetBlock((uptr)block_begin);
323 if (b != 0) {
324 ThreadContext *tctx = FindThreadByTidLocked(b->tid);
325 ReportLocation *loc = ReportLocation::New(ReportLocationHeap);
326 loc->heap_chunk_start = (uptr)allocator()->GetBlockBegin((void *)addr);
327 loc->heap_chunk_size = b->siz;
328 loc->tid = tctx ? tctx->tid : b->tid;
329 loc->stack = SymbolizeStackId(b->stk);
330 rep_->locs.PushBack(loc);
331 if (tctx)
332 AddThread(tctx);
333 return;
335 bool is_stack = false;
336 if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
337 ReportLocation *loc =
338 ReportLocation::New(is_stack ? ReportLocationStack : ReportLocationTLS);
339 loc->tid = tctx->tid;
340 rep_->locs.PushBack(loc);
341 AddThread(tctx);
343 if (ReportLocation *loc = SymbolizeData(addr)) {
344 loc->suppressable = true;
345 rep_->locs.PushBack(loc);
346 return;
348 #endif
351 #ifndef SANITIZER_GO
352 void ScopedReport::AddSleep(u32 stack_id) {
353 rep_->sleep = SymbolizeStackId(stack_id);
355 #endif
357 void ScopedReport::SetCount(int count) {
358 rep_->count = count;
361 const ReportDesc *ScopedReport::GetReport() const {
362 return rep_;
365 void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
366 MutexSet *mset) {
367 // This function restores stack trace and mutex set for the thread/epoch.
368 // It does so by getting stack trace and mutex set at the beginning of
369 // trace part, and then replaying the trace till the given epoch.
370 Trace* trace = ThreadTrace(tid);
371 ReadLock l(&trace->mtx);
372 const int partidx = (epoch / kTracePartSize) % TraceParts();
373 TraceHeader* hdr = &trace->headers[partidx];
374 if (epoch < hdr->epoch0 || epoch >= hdr->epoch0 + kTracePartSize)
375 return;
376 CHECK_EQ(RoundDown(epoch, kTracePartSize), hdr->epoch0);
377 const u64 epoch0 = RoundDown(epoch, TraceSize());
378 const u64 eend = epoch % TraceSize();
379 const u64 ebegin = RoundDown(eend, kTracePartSize);
380 DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
381 tid, (uptr)epoch, (uptr)ebegin, (uptr)eend, partidx);
382 Vector<uptr> stack(MBlockReportStack);
383 stack.Resize(hdr->stack0.size + 64);
384 for (uptr i = 0; i < hdr->stack0.size; i++) {
385 stack[i] = hdr->stack0.trace[i];
386 DPrintf2(" #%02zu: pc=%zx\n", i, stack[i]);
388 if (mset)
389 *mset = hdr->mset0;
390 uptr pos = hdr->stack0.size;
391 Event *events = (Event*)GetThreadTrace(tid);
392 for (uptr i = ebegin; i <= eend; i++) {
393 Event ev = events[i];
394 EventType typ = (EventType)(ev >> 61);
395 uptr pc = (uptr)(ev & ((1ull << 61) - 1));
396 DPrintf2(" %zu typ=%d pc=%zx\n", i, typ, pc);
397 if (typ == EventTypeMop) {
398 stack[pos] = pc;
399 } else if (typ == EventTypeFuncEnter) {
400 if (stack.Size() < pos + 2)
401 stack.Resize(pos + 2);
402 stack[pos++] = pc;
403 } else if (typ == EventTypeFuncExit) {
404 if (pos > 0)
405 pos--;
407 if (mset) {
408 if (typ == EventTypeLock) {
409 mset->Add(pc, true, epoch0 + i);
410 } else if (typ == EventTypeUnlock) {
411 mset->Del(pc, true);
412 } else if (typ == EventTypeRLock) {
413 mset->Add(pc, false, epoch0 + i);
414 } else if (typ == EventTypeRUnlock) {
415 mset->Del(pc, false);
418 for (uptr j = 0; j <= pos; j++)
419 DPrintf2(" #%zu: %zx\n", j, stack[j]);
421 if (pos == 0 && stack[0] == 0)
422 return;
423 pos++;
424 stk->Init(&stack[0], pos);
427 static bool HandleRacyStacks(ThreadState *thr, VarSizeStackTrace traces[2],
428 uptr addr_min, uptr addr_max) {
429 bool equal_stack = false;
430 RacyStacks hash;
431 bool equal_address = false;
432 RacyAddress ra0 = {addr_min, addr_max};
434 ReadLock lock(&ctx->racy_mtx);
435 if (flags()->suppress_equal_stacks) {
436 hash.hash[0] = md5_hash(traces[0].trace, traces[0].size * sizeof(uptr));
437 hash.hash[1] = md5_hash(traces[1].trace, traces[1].size * sizeof(uptr));
438 for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {
439 if (hash == ctx->racy_stacks[i]) {
440 VPrintf(2,
441 "ThreadSanitizer: suppressing report as doubled (stack)\n");
442 equal_stack = true;
443 break;
447 if (flags()->suppress_equal_addresses) {
448 for (uptr i = 0; i < ctx->racy_addresses.Size(); i++) {
449 RacyAddress ra2 = ctx->racy_addresses[i];
450 uptr maxbeg = max(ra0.addr_min, ra2.addr_min);
451 uptr minend = min(ra0.addr_max, ra2.addr_max);
452 if (maxbeg < minend) {
453 VPrintf(2, "ThreadSanitizer: suppressing report as doubled (addr)\n");
454 equal_address = true;
455 break;
460 if (!equal_stack && !equal_address)
461 return false;
462 if (!equal_stack) {
463 Lock lock(&ctx->racy_mtx);
464 ctx->racy_stacks.PushBack(hash);
466 if (!equal_address) {
467 Lock lock(&ctx->racy_mtx);
468 ctx->racy_addresses.PushBack(ra0);
470 return true;
473 static void AddRacyStacks(ThreadState *thr, VarSizeStackTrace traces[2],
474 uptr addr_min, uptr addr_max) {
475 Lock lock(&ctx->racy_mtx);
476 if (flags()->suppress_equal_stacks) {
477 RacyStacks hash;
478 hash.hash[0] = md5_hash(traces[0].trace, traces[0].size * sizeof(uptr));
479 hash.hash[1] = md5_hash(traces[1].trace, traces[1].size * sizeof(uptr));
480 ctx->racy_stacks.PushBack(hash);
482 if (flags()->suppress_equal_addresses) {
483 RacyAddress ra0 = {addr_min, addr_max};
484 ctx->racy_addresses.PushBack(ra0);
488 bool OutputReport(ThreadState *thr, const ScopedReport &srep) {
489 if (!flags()->report_bugs)
490 return false;
491 atomic_store_relaxed(&ctx->last_symbolize_time_ns, NanoTime());
492 const ReportDesc *rep = srep.GetReport();
493 Suppression *supp = 0;
494 uptr pc_or_addr = 0;
495 for (uptr i = 0; pc_or_addr == 0 && i < rep->mops.Size(); i++)
496 pc_or_addr = IsSuppressed(rep->typ, rep->mops[i]->stack, &supp);
497 for (uptr i = 0; pc_or_addr == 0 && i < rep->stacks.Size(); i++)
498 pc_or_addr = IsSuppressed(rep->typ, rep->stacks[i], &supp);
499 for (uptr i = 0; pc_or_addr == 0 && i < rep->threads.Size(); i++)
500 pc_or_addr = IsSuppressed(rep->typ, rep->threads[i]->stack, &supp);
501 for (uptr i = 0; pc_or_addr == 0 && i < rep->locs.Size(); i++)
502 pc_or_addr = IsSuppressed(rep->typ, rep->locs[i], &supp);
503 if (pc_or_addr != 0) {
504 Lock lock(&ctx->fired_suppressions_mtx);
505 FiredSuppression s = {srep.GetReport()->typ, pc_or_addr, supp};
506 ctx->fired_suppressions.push_back(s);
509 bool old_is_freeing = thr->is_freeing;
510 thr->is_freeing = false;
511 bool suppressed = OnReport(rep, pc_or_addr != 0);
512 thr->is_freeing = old_is_freeing;
513 if (suppressed)
514 return false;
516 PrintReport(rep);
517 ctx->nreported++;
518 if (flags()->halt_on_error)
519 Die();
520 return true;
523 bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace) {
524 ReadLock lock(&ctx->fired_suppressions_mtx);
525 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
526 if (ctx->fired_suppressions[k].type != type)
527 continue;
528 for (uptr j = 0; j < trace.size; j++) {
529 FiredSuppression *s = &ctx->fired_suppressions[k];
530 if (trace.trace[j] == s->pc_or_addr) {
531 if (s->supp)
532 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);
533 return true;
537 return false;
540 static bool IsFiredSuppression(Context *ctx, ReportType type, uptr addr) {
541 ReadLock lock(&ctx->fired_suppressions_mtx);
542 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
543 if (ctx->fired_suppressions[k].type != type)
544 continue;
545 FiredSuppression *s = &ctx->fired_suppressions[k];
546 if (addr == s->pc_or_addr) {
547 if (s->supp)
548 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);
549 return true;
552 return false;
555 static bool RaceBetweenAtomicAndFree(ThreadState *thr) {
556 Shadow s0(thr->racy_state[0]);
557 Shadow s1(thr->racy_state[1]);
558 CHECK(!(s0.IsAtomic() && s1.IsAtomic()));
559 if (!s0.IsAtomic() && !s1.IsAtomic())
560 return true;
561 if (s0.IsAtomic() && s1.IsFreed())
562 return true;
563 if (s1.IsAtomic() && thr->is_freeing)
564 return true;
565 return false;
568 void ReportRace(ThreadState *thr) {
569 CheckNoLocks(thr);
571 // Symbolizer makes lots of intercepted calls. If we try to process them,
572 // at best it will cause deadlocks on internal mutexes.
573 ScopedIgnoreInterceptors ignore;
575 if (!flags()->report_bugs)
576 return;
577 if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
578 return;
580 bool freed = false;
582 Shadow s(thr->racy_state[1]);
583 freed = s.GetFreedAndReset();
584 thr->racy_state[1] = s.raw();
587 uptr addr = ShadowToMem((uptr)thr->racy_shadow_addr);
588 uptr addr_min = 0;
589 uptr addr_max = 0;
591 uptr a0 = addr + Shadow(thr->racy_state[0]).addr0();
592 uptr a1 = addr + Shadow(thr->racy_state[1]).addr0();
593 uptr e0 = a0 + Shadow(thr->racy_state[0]).size();
594 uptr e1 = a1 + Shadow(thr->racy_state[1]).size();
595 addr_min = min(a0, a1);
596 addr_max = max(e0, e1);
597 if (IsExpectedReport(addr_min, addr_max - addr_min))
598 return;
601 ReportType typ = ReportTypeRace;
602 if (thr->is_vptr_access && freed)
603 typ = ReportTypeVptrUseAfterFree;
604 else if (thr->is_vptr_access)
605 typ = ReportTypeVptrRace;
606 else if (freed)
607 typ = ReportTypeUseAfterFree;
609 if (IsFiredSuppression(ctx, typ, addr))
610 return;
612 const uptr kMop = 2;
613 VarSizeStackTrace traces[kMop];
614 const uptr toppc = TraceTopPC(thr);
615 ObtainCurrentStack(thr, toppc, &traces[0]);
616 if (IsFiredSuppression(ctx, typ, traces[0]))
617 return;
619 // MutexSet is too large to live on stack.
620 Vector<u64> mset_buffer(MBlockScopedBuf);
621 mset_buffer.Resize(sizeof(MutexSet) / sizeof(u64) + 1);
622 MutexSet *mset2 = new(&mset_buffer[0]) MutexSet();
624 Shadow s2(thr->racy_state[1]);
625 RestoreStack(s2.tid(), s2.epoch(), &traces[1], mset2);
626 if (IsFiredSuppression(ctx, typ, traces[1]))
627 return;
629 if (HandleRacyStacks(thr, traces, addr_min, addr_max))
630 return;
632 ThreadRegistryLock l0(ctx->thread_registry);
633 ScopedReport rep(typ);
634 for (uptr i = 0; i < kMop; i++) {
635 Shadow s(thr->racy_state[i]);
636 rep.AddMemoryAccess(addr, s, traces[i], i == 0 ? &thr->mset : mset2);
639 for (uptr i = 0; i < kMop; i++) {
640 FastState s(thr->racy_state[i]);
641 ThreadContext *tctx = static_cast<ThreadContext*>(
642 ctx->thread_registry->GetThreadLocked(s.tid()));
643 if (s.epoch() < tctx->epoch0 || s.epoch() > tctx->epoch1)
644 continue;
645 rep.AddThread(tctx);
648 rep.AddLocation(addr_min, addr_max - addr_min);
650 #ifndef SANITIZER_GO
651 { // NOLINT
652 Shadow s(thr->racy_state[1]);
653 if (s.epoch() <= thr->last_sleep_clock.get(s.tid()))
654 rep.AddSleep(thr->last_sleep_stack_id);
656 #endif
658 if (!OutputReport(thr, rep))
659 return;
661 AddRacyStacks(thr, traces, addr_min, addr_max);
664 void PrintCurrentStack(ThreadState *thr, uptr pc) {
665 VarSizeStackTrace trace;
666 ObtainCurrentStack(thr, pc, &trace);
667 PrintStack(SymbolizeStack(trace));
670 void PrintCurrentStackSlow(uptr pc) {
671 #ifndef SANITIZER_GO
672 BufferedStackTrace *ptrace =
673 new(internal_alloc(MBlockStackTrace, sizeof(BufferedStackTrace)))
674 BufferedStackTrace();
675 ptrace->Unwind(kStackTraceMax, pc, 0, 0, 0, 0, false);
676 for (uptr i = 0; i < ptrace->size / 2; i++) {
677 uptr tmp = ptrace->trace_buffer[i];
678 ptrace->trace_buffer[i] = ptrace->trace_buffer[ptrace->size - i - 1];
679 ptrace->trace_buffer[ptrace->size - i - 1] = tmp;
681 PrintStack(SymbolizeStack(*ptrace));
682 #endif
685 } // namespace __tsan
687 using namespace __tsan;
689 extern "C" {
690 SANITIZER_INTERFACE_ATTRIBUTE
691 void __sanitizer_print_stack_trace() {
692 PrintCurrentStackSlow(StackTrace::GetCurrentPc());
694 } // extern "C"