[Patch 3/7 arc] Deprecate *_BY_PIECES_P, move to hookized version
[official-gcc.git] / libsanitizer / tsan / tsan_rtl_report.cc
blobeafd1f4dfcdf76c19f25d0b365f5d572c5c6bae9
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(const 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();
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(ReportStack *stack) {
58 ReportStack *last_frame = 0;
59 ReportStack *last_frame2 = 0;
60 const char *prefix = "__interceptor_";
61 uptr prefix_len = internal_strlen(prefix);
62 const char *path_prefix = common_flags()->strip_path_prefix;
63 uptr path_prefix_len = internal_strlen(path_prefix);
64 char *pos;
65 for (ReportStack *ent = stack; ent; ent = ent->next) {
66 if (ent->func && 0 == internal_strncmp(ent->func, prefix, prefix_len))
67 ent->func += prefix_len;
68 if (ent->file && (pos = internal_strstr(ent->file, path_prefix)))
69 ent->file = pos + path_prefix_len;
70 if (ent->file && ent->file[0] == '.' && ent->file[1] == '/')
71 ent->file += 2;
72 last_frame2 = last_frame;
73 last_frame = ent;
76 if (last_frame2 == 0)
77 return;
78 const char *last = last_frame->func;
79 #ifndef TSAN_GO
80 const char *last2 = last_frame2->func;
81 // Strip frame above 'main'
82 if (last2 && 0 == internal_strcmp(last2, "main")) {
83 last_frame2->next = 0;
84 // Strip our internal thread start routine.
85 } else if (last && 0 == internal_strcmp(last, "__tsan_thread_start_func")) {
86 last_frame2->next = 0;
87 // Strip global ctors init.
88 } else if (last && 0 == internal_strcmp(last, "__do_global_ctors_aux")) {
89 last_frame2->next = 0;
90 // If both are 0, then we probably just failed to symbolize.
91 } else if (last || last2) {
92 // Ensure that we recovered stack completely. Trimmed stack
93 // can actually happen if we do not instrument some code,
94 // so it's only a debug print. However we must try hard to not miss it
95 // due to our fault.
96 DPrintf("Bottom stack frame of stack %zx is missed\n", stack->pc);
98 #else
99 // The last frame always point into runtime (gosched0, goexit0, runtime.main).
100 last_frame2->next = 0;
101 (void)last;
102 #endif
105 ReportStack *SymbolizeStackId(u32 stack_id) {
106 if (stack_id == 0)
107 return 0;
108 uptr ssz = 0;
109 const uptr *stack = StackDepotGet(stack_id, &ssz);
110 if (stack == 0)
111 return 0;
112 StackTrace trace;
113 trace.Init(stack, ssz);
114 return SymbolizeStack(trace);
117 static ReportStack *SymbolizeStack(const StackTrace& trace) {
118 if (trace.IsEmpty())
119 return 0;
120 ReportStack *stack = 0;
121 for (uptr si = 0; si < trace.Size(); si++) {
122 const uptr pc = trace.Get(si);
123 #ifndef TSAN_GO
124 // We obtain the return address, that is, address of the next instruction,
125 // so offset it by 1 byte.
126 const uptr pc1 = __sanitizer::StackTrace::GetPreviousInstructionPc(pc);
127 #else
128 // FIXME(dvyukov): Go sometimes uses address of a function as top pc.
129 uptr pc1 = pc;
130 if (si != trace.Size() - 1)
131 pc1 -= 1;
132 #endif
133 ReportStack *ent = SymbolizeCode(pc1);
134 CHECK_NE(ent, 0);
135 ReportStack *last = ent;
136 while (last->next) {
137 last->pc = pc; // restore original pc for report
138 last = last->next;
140 last->pc = pc; // restore original pc for report
141 last->next = stack;
142 stack = ent;
144 StackStripMain(stack);
145 return stack;
148 ScopedReport::ScopedReport(ReportType typ) {
149 ctx->thread_registry->CheckLocked();
150 void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
151 rep_ = new(mem) ReportDesc;
152 rep_->typ = typ;
153 ctx->report_mtx.Lock();
154 CommonSanitizerReportMutex.Lock();
157 ScopedReport::~ScopedReport() {
158 CommonSanitizerReportMutex.Unlock();
159 ctx->report_mtx.Unlock();
160 DestroyAndFree(rep_);
163 void ScopedReport::AddStack(const StackTrace *stack, bool suppressable) {
164 ReportStack **rs = rep_->stacks.PushBack();
165 *rs = SymbolizeStack(*stack);
166 (*rs)->suppressable = suppressable;
169 void ScopedReport::AddMemoryAccess(uptr addr, Shadow s,
170 const StackTrace *stack, const MutexSet *mset) {
171 void *mem = internal_alloc(MBlockReportMop, sizeof(ReportMop));
172 ReportMop *mop = new(mem) ReportMop;
173 rep_->mops.PushBack(mop);
174 mop->tid = s.tid();
175 mop->addr = addr + s.addr0();
176 mop->size = s.size();
177 mop->write = s.IsWrite();
178 mop->atomic = s.IsAtomic();
179 mop->stack = SymbolizeStack(*stack);
180 if (mop->stack)
181 mop->stack->suppressable = true;
182 for (uptr i = 0; i < mset->Size(); i++) {
183 MutexSet::Desc d = mset->Get(i);
184 u64 mid = this->AddMutex(d.id);
185 ReportMopMutex mtx = {mid, d.write};
186 mop->mset.PushBack(mtx);
190 void ScopedReport::AddUniqueTid(int unique_tid) {
191 rep_->unique_tids.PushBack(unique_tid);
194 void ScopedReport::AddThread(const ThreadContext *tctx, bool suppressable) {
195 for (uptr i = 0; i < rep_->threads.Size(); i++) {
196 if ((u32)rep_->threads[i]->id == tctx->tid)
197 return;
199 void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
200 ReportThread *rt = new(mem) ReportThread();
201 rep_->threads.PushBack(rt);
202 rt->id = tctx->tid;
203 rt->pid = tctx->os_id;
204 rt->running = (tctx->status == ThreadStatusRunning);
205 rt->name = internal_strdup(tctx->name);
206 rt->parent_tid = tctx->parent_tid;
207 rt->stack = 0;
208 rt->stack = SymbolizeStackId(tctx->creation_stack_id);
209 if (rt->stack)
210 rt->stack->suppressable = suppressable;
213 #ifndef TSAN_GO
214 static ThreadContext *FindThreadByUidLocked(int unique_id) {
215 ctx->thread_registry->CheckLocked();
216 for (unsigned i = 0; i < kMaxTid; i++) {
217 ThreadContext *tctx = static_cast<ThreadContext*>(
218 ctx->thread_registry->GetThreadLocked(i));
219 if (tctx && tctx->unique_id == (u32)unique_id) {
220 return tctx;
223 return 0;
226 static ThreadContext *FindThreadByTidLocked(int tid) {
227 ctx->thread_registry->CheckLocked();
228 return static_cast<ThreadContext*>(
229 ctx->thread_registry->GetThreadLocked(tid));
232 static bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {
233 uptr addr = (uptr)arg;
234 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
235 if (tctx->status != ThreadStatusRunning)
236 return false;
237 ThreadState *thr = tctx->thr;
238 CHECK(thr);
239 return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||
240 (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));
243 ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
244 ctx->thread_registry->CheckLocked();
245 ThreadContext *tctx = static_cast<ThreadContext*>(
246 ctx->thread_registry->FindThreadContextLocked(IsInStackOrTls,
247 (void*)addr));
248 if (!tctx)
249 return 0;
250 ThreadState *thr = tctx->thr;
251 CHECK(thr);
252 *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
253 return tctx;
255 #endif
257 void ScopedReport::AddThread(int unique_tid, bool suppressable) {
258 #ifndef TSAN_GO
259 AddThread(FindThreadByUidLocked(unique_tid), suppressable);
260 #endif
263 void ScopedReport::AddMutex(const SyncVar *s) {
264 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
265 if (rep_->mutexes[i]->id == s->uid)
266 return;
268 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
269 ReportMutex *rm = new(mem) ReportMutex();
270 rep_->mutexes.PushBack(rm);
271 rm->id = s->uid;
272 rm->addr = s->addr;
273 rm->destroyed = false;
274 rm->stack = SymbolizeStackId(s->creation_stack_id);
277 u64 ScopedReport::AddMutex(u64 id) {
278 u64 uid = 0;
279 u64 mid = id;
280 uptr addr = SyncVar::SplitId(id, &uid);
281 SyncVar *s = ctx->metamap.GetIfExistsAndLock(addr);
282 // Check that the mutex is still alive.
283 // Another mutex can be created at the same address,
284 // so check uid as well.
285 if (s && s->CheckId(uid)) {
286 mid = s->uid;
287 AddMutex(s);
288 } else {
289 AddDeadMutex(id);
291 if (s)
292 s->mtx.Unlock();
293 return mid;
296 void ScopedReport::AddDeadMutex(u64 id) {
297 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
298 if (rep_->mutexes[i]->id == id)
299 return;
301 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
302 ReportMutex *rm = new(mem) ReportMutex();
303 rep_->mutexes.PushBack(rm);
304 rm->id = id;
305 rm->addr = 0;
306 rm->destroyed = true;
307 rm->stack = 0;
310 void ScopedReport::AddLocation(uptr addr, uptr size) {
311 if (addr == 0)
312 return;
313 #ifndef TSAN_GO
314 int fd = -1;
315 int creat_tid = -1;
316 u32 creat_stack = 0;
317 if (FdLocation(addr, &fd, &creat_tid, &creat_stack)) {
318 void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
319 ReportLocation *loc = new(mem) ReportLocation();
320 rep_->locs.PushBack(loc);
321 loc->type = ReportLocationFD;
322 loc->fd = fd;
323 loc->tid = creat_tid;
324 loc->stack = SymbolizeStackId(creat_stack);
325 ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
326 if (tctx)
327 AddThread(tctx);
328 return;
330 MBlock *b = 0;
331 Allocator *a = allocator();
332 if (a->PointerIsMine((void*)addr)) {
333 void *block_begin = a->GetBlockBegin((void*)addr);
334 if (block_begin)
335 b = ctx->metamap.GetBlock((uptr)block_begin);
337 if (b != 0) {
338 ThreadContext *tctx = FindThreadByTidLocked(b->tid);
339 void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
340 ReportLocation *loc = new(mem) ReportLocation();
341 rep_->locs.PushBack(loc);
342 loc->type = ReportLocationHeap;
343 loc->addr = (uptr)allocator()->GetBlockBegin((void*)addr);
344 loc->size = b->siz;
345 loc->tid = tctx ? tctx->tid : b->tid;
346 loc->name = 0;
347 loc->file = 0;
348 loc->line = 0;
349 loc->stack = 0;
350 loc->stack = SymbolizeStackId(b->stk);
351 if (tctx)
352 AddThread(tctx);
353 return;
355 bool is_stack = false;
356 if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
357 void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
358 ReportLocation *loc = new(mem) ReportLocation();
359 rep_->locs.PushBack(loc);
360 loc->type = is_stack ? ReportLocationStack : ReportLocationTLS;
361 loc->tid = tctx->tid;
362 AddThread(tctx);
364 ReportLocation *loc = SymbolizeData(addr);
365 if (loc) {
366 loc->suppressable = true;
367 rep_->locs.PushBack(loc);
368 return;
370 #endif
373 #ifndef TSAN_GO
374 void ScopedReport::AddSleep(u32 stack_id) {
375 rep_->sleep = SymbolizeStackId(stack_id);
377 #endif
379 void ScopedReport::SetCount(int count) {
380 rep_->count = count;
383 const ReportDesc *ScopedReport::GetReport() const {
384 return rep_;
387 void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset) {
388 // This function restores stack trace and mutex set for the thread/epoch.
389 // It does so by getting stack trace and mutex set at the beginning of
390 // trace part, and then replaying the trace till the given epoch.
391 ctx->thread_registry->CheckLocked();
392 ThreadContext *tctx = static_cast<ThreadContext*>(
393 ctx->thread_registry->GetThreadLocked(tid));
394 if (tctx == 0)
395 return;
396 if (tctx->status != ThreadStatusRunning
397 && tctx->status != ThreadStatusFinished
398 && tctx->status != ThreadStatusDead)
399 return;
400 Trace* trace = ThreadTrace(tctx->tid);
401 Lock l(&trace->mtx);
402 const int partidx = (epoch / kTracePartSize) % TraceParts();
403 TraceHeader* hdr = &trace->headers[partidx];
404 if (epoch < hdr->epoch0)
405 return;
406 const u64 epoch0 = RoundDown(epoch, TraceSize());
407 const u64 eend = epoch % TraceSize();
408 const u64 ebegin = RoundDown(eend, kTracePartSize);
409 DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
410 tid, (uptr)epoch, (uptr)ebegin, (uptr)eend, partidx);
411 InternalScopedBuffer<uptr> stack(kShadowStackSize);
412 for (uptr i = 0; i < hdr->stack0.Size(); i++) {
413 stack[i] = hdr->stack0.Get(i);
414 DPrintf2(" #%02lu: pc=%zx\n", i, stack[i]);
416 if (mset)
417 *mset = hdr->mset0;
418 uptr pos = hdr->stack0.Size();
419 Event *events = (Event*)GetThreadTrace(tid);
420 for (uptr i = ebegin; i <= eend; i++) {
421 Event ev = events[i];
422 EventType typ = (EventType)(ev >> 61);
423 uptr pc = (uptr)(ev & ((1ull << 61) - 1));
424 DPrintf2(" %zu typ=%d pc=%zx\n", i, typ, pc);
425 if (typ == EventTypeMop) {
426 stack[pos] = pc;
427 } else if (typ == EventTypeFuncEnter) {
428 stack[pos++] = pc;
429 } else if (typ == EventTypeFuncExit) {
430 if (pos > 0)
431 pos--;
433 if (mset) {
434 if (typ == EventTypeLock) {
435 mset->Add(pc, true, epoch0 + i);
436 } else if (typ == EventTypeUnlock) {
437 mset->Del(pc, true);
438 } else if (typ == EventTypeRLock) {
439 mset->Add(pc, false, epoch0 + i);
440 } else if (typ == EventTypeRUnlock) {
441 mset->Del(pc, false);
444 for (uptr j = 0; j <= pos; j++)
445 DPrintf2(" #%zu: %zx\n", j, stack[j]);
447 if (pos == 0 && stack[0] == 0)
448 return;
449 pos++;
450 stk->Init(stack.data(), pos);
453 static bool HandleRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
454 uptr addr_min, uptr addr_max) {
455 bool equal_stack = false;
456 RacyStacks hash;
457 if (flags()->suppress_equal_stacks) {
458 hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
459 hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
460 for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {
461 if (hash == ctx->racy_stacks[i]) {
462 DPrintf("ThreadSanitizer: suppressing report as doubled (stack)\n");
463 equal_stack = true;
464 break;
468 bool equal_address = false;
469 RacyAddress ra0 = {addr_min, addr_max};
470 if (flags()->suppress_equal_addresses) {
471 for (uptr i = 0; i < ctx->racy_addresses.Size(); i++) {
472 RacyAddress ra2 = ctx->racy_addresses[i];
473 uptr maxbeg = max(ra0.addr_min, ra2.addr_min);
474 uptr minend = min(ra0.addr_max, ra2.addr_max);
475 if (maxbeg < minend) {
476 DPrintf("ThreadSanitizer: suppressing report as doubled (addr)\n");
477 equal_address = true;
478 break;
482 if (equal_stack || equal_address) {
483 if (!equal_stack)
484 ctx->racy_stacks.PushBack(hash);
485 if (!equal_address)
486 ctx->racy_addresses.PushBack(ra0);
487 return true;
489 return false;
492 static void AddRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
493 uptr addr_min, uptr addr_max) {
494 if (flags()->suppress_equal_stacks) {
495 RacyStacks hash;
496 hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
497 hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
498 ctx->racy_stacks.PushBack(hash);
500 if (flags()->suppress_equal_addresses) {
501 RacyAddress ra0 = {addr_min, addr_max};
502 ctx->racy_addresses.PushBack(ra0);
506 bool OutputReport(ThreadState *thr, const ScopedReport &srep) {
507 atomic_store(&ctx->last_symbolize_time_ns, NanoTime(), memory_order_relaxed);
508 const ReportDesc *rep = srep.GetReport();
509 Suppression *supp = 0;
510 uptr suppress_pc = 0;
511 for (uptr i = 0; suppress_pc == 0 && i < rep->mops.Size(); i++)
512 suppress_pc = IsSuppressed(rep->typ, rep->mops[i]->stack, &supp);
513 for (uptr i = 0; suppress_pc == 0 && i < rep->stacks.Size(); i++)
514 suppress_pc = IsSuppressed(rep->typ, rep->stacks[i], &supp);
515 for (uptr i = 0; suppress_pc == 0 && i < rep->threads.Size(); i++)
516 suppress_pc = IsSuppressed(rep->typ, rep->threads[i]->stack, &supp);
517 for (uptr i = 0; suppress_pc == 0 && i < rep->locs.Size(); i++)
518 suppress_pc = IsSuppressed(rep->typ, rep->locs[i], &supp);
519 if (suppress_pc != 0) {
520 FiredSuppression s = {srep.GetReport()->typ, suppress_pc, supp};
521 ctx->fired_suppressions.push_back(s);
524 bool old_is_freeing = thr->is_freeing;
525 thr->is_freeing = false;
526 bool suppressed = OnReport(rep, suppress_pc != 0);
527 thr->is_freeing = old_is_freeing;
528 if (suppressed)
529 return false;
531 PrintReport(rep);
532 ctx->nreported++;
533 if (flags()->halt_on_error)
534 internal__exit(flags()->exitcode);
535 return true;
538 bool IsFiredSuppression(Context *ctx,
539 const ScopedReport &srep,
540 const StackTrace &trace) {
541 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
542 if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
543 continue;
544 for (uptr j = 0; j < trace.Size(); j++) {
545 FiredSuppression *s = &ctx->fired_suppressions[k];
546 if (trace.Get(j) == s->pc) {
547 if (s->supp)
548 s->supp->hit_count++;
549 return true;
553 return false;
556 static bool IsFiredSuppression(Context *ctx,
557 const ScopedReport &srep,
558 uptr addr) {
559 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
560 if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
561 continue;
562 FiredSuppression *s = &ctx->fired_suppressions[k];
563 if (addr == s->pc) {
564 if (s->supp)
565 s->supp->hit_count++;
566 return true;
569 return false;
572 bool FrameIsInternal(const ReportStack *frame) {
573 return frame != 0 && frame->file != 0
574 && (internal_strstr(frame->file, "tsan_interceptors.cc") ||
575 internal_strstr(frame->file, "sanitizer_common_interceptors.inc") ||
576 internal_strstr(frame->file, "tsan_interface_"));
579 static bool RaceBetweenAtomicAndFree(ThreadState *thr) {
580 Shadow s0(thr->racy_state[0]);
581 Shadow s1(thr->racy_state[1]);
582 CHECK(!(s0.IsAtomic() && s1.IsAtomic()));
583 if (!s0.IsAtomic() && !s1.IsAtomic())
584 return true;
585 if (s0.IsAtomic() && s1.IsFreed())
586 return true;
587 if (s1.IsAtomic() && thr->is_freeing)
588 return true;
589 return false;
592 void ReportRace(ThreadState *thr) {
593 CheckNoLocks(thr);
595 // Symbolizer makes lots of intercepted calls. If we try to process them,
596 // at best it will cause deadlocks on internal mutexes.
597 ScopedIgnoreInterceptors ignore;
599 if (!flags()->report_bugs)
600 return;
601 if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
602 return;
604 bool freed = false;
606 Shadow s(thr->racy_state[1]);
607 freed = s.GetFreedAndReset();
608 thr->racy_state[1] = s.raw();
611 uptr addr = ShadowToMem((uptr)thr->racy_shadow_addr);
612 uptr addr_min = 0;
613 uptr addr_max = 0;
615 uptr a0 = addr + Shadow(thr->racy_state[0]).addr0();
616 uptr a1 = addr + Shadow(thr->racy_state[1]).addr0();
617 uptr e0 = a0 + Shadow(thr->racy_state[0]).size();
618 uptr e1 = a1 + Shadow(thr->racy_state[1]).size();
619 addr_min = min(a0, a1);
620 addr_max = max(e0, e1);
621 if (IsExpectedReport(addr_min, addr_max - addr_min))
622 return;
625 ThreadRegistryLock l0(ctx->thread_registry);
627 ReportType typ = ReportTypeRace;
628 if (thr->is_vptr_access)
629 typ = ReportTypeVptrRace;
630 else if (freed)
631 typ = ReportTypeUseAfterFree;
632 ScopedReport rep(typ);
633 if (IsFiredSuppression(ctx, rep, addr))
634 return;
635 const uptr kMop = 2;
636 StackTrace traces[kMop];
637 const uptr toppc = TraceTopPC(thr);
638 traces[0].ObtainCurrent(thr, toppc);
639 if (IsFiredSuppression(ctx, rep, traces[0]))
640 return;
641 InternalScopedBuffer<MutexSet> mset2(1);
642 new(mset2.data()) MutexSet();
643 Shadow s2(thr->racy_state[1]);
644 RestoreStack(s2.tid(), s2.epoch(), &traces[1], mset2.data());
645 if (IsFiredSuppression(ctx, rep, traces[1]))
646 return;
648 if (HandleRacyStacks(thr, traces, addr_min, addr_max))
649 return;
651 for (uptr i = 0; i < kMop; i++) {
652 Shadow s(thr->racy_state[i]);
653 rep.AddMemoryAccess(addr, s, &traces[i],
654 i == 0 ? &thr->mset : mset2.data());
657 for (uptr i = 0; i < kMop; i++) {
658 FastState s(thr->racy_state[i]);
659 ThreadContext *tctx = static_cast<ThreadContext*>(
660 ctx->thread_registry->GetThreadLocked(s.tid()));
661 if (s.epoch() < tctx->epoch0 || s.epoch() > tctx->epoch1)
662 continue;
663 rep.AddThread(tctx);
666 rep.AddLocation(addr_min, addr_max - addr_min);
668 #ifndef TSAN_GO
669 { // NOLINT
670 Shadow s(thr->racy_state[1]);
671 if (s.epoch() <= thr->last_sleep_clock.get(s.tid()))
672 rep.AddSleep(thr->last_sleep_stack_id);
674 #endif
676 if (!OutputReport(thr, rep))
677 return;
679 AddRacyStacks(thr, traces, addr_min, addr_max);
682 void PrintCurrentStack(ThreadState *thr, uptr pc) {
683 StackTrace trace;
684 trace.ObtainCurrent(thr, pc);
685 PrintStack(SymbolizeStack(trace));
688 void PrintCurrentStackSlow() {
689 #ifndef TSAN_GO
690 __sanitizer::StackTrace *ptrace = new(internal_alloc(MBlockStackTrace,
691 sizeof(__sanitizer::StackTrace))) __sanitizer::StackTrace;
692 ptrace->Unwind(kStackTraceMax, __sanitizer::StackTrace::GetCurrentPc(), 0, 0,
693 0, 0, false);
694 for (uptr i = 0; i < ptrace->size / 2; i++) {
695 uptr tmp = ptrace->trace[i];
696 ptrace->trace[i] = ptrace->trace[ptrace->size - i - 1];
697 ptrace->trace[ptrace->size - i - 1] = tmp;
699 StackTrace trace;
700 trace.Init(ptrace->trace, ptrace->size);
701 PrintStack(SymbolizeStack(trace));
702 #endif
705 } // namespace __tsan