933 lines · cpp
1//===-- tsan_rtl_report.cpp -----------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of ThreadSanitizer (TSan), a race detector.10//11//===----------------------------------------------------------------------===//12 13#include "sanitizer_common/sanitizer_common.h"14#include "sanitizer_common/sanitizer_internal_defs.h"15#include "sanitizer_common/sanitizer_libc.h"16#include "sanitizer_common/sanitizer_placement_new.h"17#include "sanitizer_common/sanitizer_stackdepot.h"18#include "sanitizer_common/sanitizer_stacktrace.h"19#include "tsan_defs.h"20#include "tsan_fd.h"21#include "tsan_flags.h"22#include "tsan_mman.h"23#include "tsan_platform.h"24#include "tsan_report.h"25#include "tsan_rtl.h"26#include "tsan_suppressions.h"27#include "tsan_symbolize.h"28#include "tsan_sync.h"29 30namespace __tsan {31 32using namespace __sanitizer;33 34static ReportStack *SymbolizeStack(StackTrace trace);35 36// Can be overriden by an application/test to intercept reports.37#ifdef TSAN_EXTERNAL_HOOKS38bool OnReport(const ReportDesc *rep, bool suppressed);39#else40SANITIZER_WEAK_CXX_DEFAULT_IMPL41bool OnReport(const ReportDesc *rep, bool suppressed) {42 (void)rep;43 return suppressed;44}45#endif46 47SANITIZER_WEAK_DEFAULT_IMPL48void __tsan_on_report(const ReportDesc *rep) {49 (void)rep;50}51 52static void StackStripMain(SymbolizedStack *frames) {53 SymbolizedStack *last_frame = nullptr;54 SymbolizedStack *last_frame2 = nullptr;55 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {56 last_frame2 = last_frame;57 last_frame = cur;58 }59 60 if (last_frame2 == 0)61 return;62#if !SANITIZER_GO63 const char *last = last_frame->info.function;64 const char *last2 = last_frame2->info.function;65 // Strip frame above 'main'66 if (last2 && 0 == internal_strcmp(last2, "main")) {67 last_frame->ClearAll();68 last_frame2->next = nullptr;69 // Strip our internal thread start routine.70 } else if (last && 0 == internal_strcmp(last, "__tsan_thread_start_func")) {71 last_frame->ClearAll();72 last_frame2->next = nullptr;73 // Strip global ctors init, .preinit_array and main caller.74 } else if (last && (0 == internal_strcmp(last, "__do_global_ctors_aux") ||75 0 == internal_strcmp(last, "__libc_csu_init") ||76 0 == internal_strcmp(last, "__libc_start_main"))) {77 last_frame->ClearAll();78 last_frame2->next = nullptr;79 // If both are 0, then we probably just failed to symbolize.80 } else if (last || last2) {81 // Ensure that we recovered stack completely. Trimmed stack82 // can actually happen if we do not instrument some code,83 // so it's only a debug print. However we must try hard to not miss it84 // due to our fault.85 DPrintf("Bottom stack frame is missed\n");86 }87#else88 // The last frame always point into runtime (gosched0, goexit0, runtime.main).89 last_frame->ClearAll();90 last_frame2->next = nullptr;91#endif92}93 94ReportStack *SymbolizeStackId(u32 stack_id) {95 if (stack_id == 0)96 return 0;97 StackTrace stack = StackDepotGet(stack_id);98 if (stack.trace == nullptr)99 return nullptr;100 return SymbolizeStack(stack);101}102 103static ReportStack *SymbolizeStack(StackTrace trace) {104 if (trace.size == 0)105 return 0;106 SymbolizedStack *top = nullptr;107 for (uptr si = 0; si < trace.size; si++) {108 const uptr pc = trace.trace[si];109 uptr pc1 = pc;110 // We obtain the return address, but we're interested in the previous111 // instruction.112 if ((pc & kExternalPCBit) == 0)113 pc1 = StackTrace::GetPreviousInstructionPc(pc);114 SymbolizedStack *ent = SymbolizeCode(pc1);115 CHECK_NE(ent, 0);116 SymbolizedStack *last = ent;117 while (last->next) {118 last->info.address = pc; // restore original pc for report119 last = last->next;120 }121 last->info.address = pc; // restore original pc for report122 last->next = top;123 top = ent;124 }125 StackStripMain(top);126 127 auto *stack = New<ReportStack>();128 stack->frames = top;129 return stack;130}131 132bool ShouldReport(ThreadState *thr, ReportType typ) {133 // We set thr->suppress_reports in the fork context.134 // Taking any locking in the fork context can lead to deadlocks.135 // If any locks are already taken, it's too late to do this check.136 CheckedMutex::CheckNoLocks();137 // For the same reason check we didn't lock thread_registry yet.138 if (SANITIZER_DEBUG)139 ThreadRegistryLock l(&ctx->thread_registry);140 if (!flags()->report_bugs || thr->suppress_reports)141 return false;142 switch (typ) {143 case ReportTypeSignalUnsafe:144 return flags()->report_signal_unsafe;145 case ReportTypeThreadLeak:146#if !SANITIZER_GO147 // It's impossible to join phantom threads148 // in the child after fork.149 if (ctx->after_multithreaded_fork)150 return false;151#endif152 return flags()->report_thread_leaks;153 case ReportTypeMutexDestroyLocked:154 return flags()->report_destroy_locked;155 default:156 return true;157 }158}159 160ScopedReportBase::ScopedReportBase(ReportType typ, uptr tag) {161 ctx->thread_registry.CheckLocked();162 rep_ = New<ReportDesc>();163 rep_->typ = typ;164 rep_->tag = tag;165 ctx->report_mtx.Lock();166}167 168ScopedReportBase::~ScopedReportBase() {169 ctx->report_mtx.Unlock();170 DestroyAndFree(rep_);171}172 173void ScopedReportBase::AddStack(StackTrace stack, bool suppressable) {174 ReportStack **rs = rep_->stacks.PushBack();175 *rs = SymbolizeStack(stack);176 (*rs)->suppressable = suppressable;177}178 179void ScopedReportBase::AddMemoryAccess(uptr addr, uptr external_tag, Shadow s,180 Tid tid, StackTrace stack,181 const MutexSet *mset) {182 uptr addr0, size;183 AccessType typ;184 s.GetAccess(&addr0, &size, &typ);185 auto *mop = New<ReportMop>();186 rep_->mops.PushBack(mop);187 mop->tid = tid;188 mop->addr = addr + addr0;189 mop->size = size;190 mop->write = !(typ & kAccessRead);191 mop->atomic = typ & kAccessAtomic;192 mop->external_tag = external_tag;193 mop->stack_trace = stack;194 for (uptr i = 0; i < mset->Size(); i++) {195 MutexSet::Desc d = mset->Get(i);196 int id = this->AddMutex(d.addr, d.stack_id);197 ReportMopMutex mtx = {id, d.write};198 mop->mset.PushBack(mtx);199 }200}201 202void ScopedReportBase::SymbolizeStackElems() {203 // symbolize memory ops204 for (usize i = 0, size = rep_->mops.Size(); i < size; i++) {205 ReportMop *mop = rep_->mops[i];206 mop->stack = SymbolizeStack(mop->stack_trace);207 if (mop->stack)208 mop->stack->suppressable = true;209 }210 211 // symbolize locations212 for (usize i = 0, size = rep_->locs.Size(); i < size; i++) {213 // added locations have a NULL placeholder - don't dereference them214 if (ReportLocation *loc = rep_->locs[i])215 loc->stack = SymbolizeStackId(loc->stack_id);216 }217 218 // symbolize any added locations219 for (usize i = 0, size = rep_->added_location_addrs.Size(); i < size; i++) {220 AddedLocationAddr *added_loc = &rep_->added_location_addrs[i];221 if (ReportLocation *loc = SymbolizeData(added_loc->addr)) {222 loc->suppressable = true;223 rep_->locs[added_loc->locs_idx] = loc;224 }225 }226 227 // Filter out any added location placeholders that could not be symbolized228 usize j = 0;229 for (usize i = 0, size = rep_->locs.Size(); i < size; i++) {230 if (rep_->locs[i] != nullptr) {231 rep_->locs[j] = rep_->locs[i];232 j++;233 }234 }235 rep_->locs.Resize(j);236 237 // symbolize threads238 for (usize i = 0, size = rep_->threads.Size(); i < size; i++) {239 ReportThread *rt = rep_->threads[i];240 rt->stack = SymbolizeStackId(rt->stack_id);241 if (rt->stack)242 rt->stack->suppressable = rt->suppressable;243 }244 245 // symbolize mutexes246 for (usize i = 0, size = rep_->mutexes.Size(); i < size; i++) {247 ReportMutex *rm = rep_->mutexes[i];248 rm->stack = SymbolizeStackId(rm->stack_id);249 }250}251 252void ScopedReportBase::AddUniqueTid(Tid unique_tid) {253 rep_->unique_tids.PushBack(unique_tid);254}255 256void ScopedReportBase::AddThread(const ThreadContext *tctx, bool suppressable) {257 for (uptr i = 0; i < rep_->threads.Size(); i++) {258 if ((u32)rep_->threads[i]->id == tctx->tid)259 return;260 }261 auto *rt = New<ReportThread>();262 rep_->threads.PushBack(rt);263 rt->id = tctx->tid;264 rt->os_id = tctx->os_id;265 rt->running = (tctx->status == ThreadStatusRunning);266 rt->name = internal_strdup(tctx->name);267 rt->parent_tid = tctx->parent_tid;268 rt->thread_type = tctx->thread_type;269 rt->stack_id = tctx->creation_stack_id;270 rt->suppressable = suppressable;271}272 273#if !SANITIZER_GO274static ThreadContext *FindThreadByTidLocked(Tid tid) {275 ctx->thread_registry.CheckLocked();276 return static_cast<ThreadContext *>(277 ctx->thread_registry.GetThreadLocked(tid));278}279 280static bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {281 uptr addr = (uptr)arg;282 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);283 if (tctx->status != ThreadStatusRunning)284 return false;285 ThreadState *thr = tctx->thr;286 CHECK(thr);287 return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||288 (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));289}290 291ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {292 ctx->thread_registry.CheckLocked();293 ThreadContext *tctx =294 static_cast<ThreadContext *>(ctx->thread_registry.FindThreadContextLocked(295 IsInStackOrTls, (void *)addr));296 if (!tctx)297 return 0;298 ThreadState *thr = tctx->thr;299 CHECK(thr);300 *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);301 return tctx;302}303#endif304 305void ScopedReportBase::AddThread(Tid tid, bool suppressable) {306#if !SANITIZER_GO307 if (const ThreadContext *tctx = FindThreadByTidLocked(tid))308 AddThread(tctx, suppressable);309#endif310}311 312int ScopedReportBase::AddMutex(uptr addr, StackID creation_stack_id) {313 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {314 if (rep_->mutexes[i]->addr == addr)315 return rep_->mutexes[i]->id;316 }317 auto *rm = New<ReportMutex>();318 rep_->mutexes.PushBack(rm);319 rm->id = rep_->mutexes.Size() - 1;320 rm->addr = addr;321 rm->stack_id = creation_stack_id;322 return rm->id;323}324 325void ScopedReportBase::AddLocation(uptr addr, uptr size) {326 if (addr == 0)327 return;328#if !SANITIZER_GO329 int fd = -1;330 Tid creat_tid = kInvalidTid;331 StackID creat_stack = 0;332 bool closed = false;333 if (FdLocation(addr, &fd, &creat_tid, &creat_stack, &closed)) {334 auto *loc = New<ReportLocation>();335 loc->type = ReportLocationFD;336 loc->fd_closed = closed;337 loc->fd = fd;338 loc->tid = creat_tid;339 loc->stack_id = creat_stack;340 rep_->locs.PushBack(loc);341 AddThread(creat_tid);342 return;343 }344 MBlock *b = 0;345 uptr block_begin = 0;346 Allocator *a = allocator();347 if (a->PointerIsMine((void*)addr)) {348 block_begin = (uptr)a->GetBlockBegin((void *)addr);349 if (block_begin)350 b = ctx->metamap.GetBlock(block_begin);351 }352 if (!b)353 b = JavaHeapBlock(addr, &block_begin);354 if (b != 0) {355 auto *loc = New<ReportLocation>();356 loc->type = ReportLocationHeap;357 loc->heap_chunk_start = block_begin;358 loc->heap_chunk_size = b->siz;359 loc->external_tag = b->tag;360 loc->tid = b->tid;361 loc->stack_id = b->stk;362 rep_->locs.PushBack(loc);363 AddThread(b->tid);364 return;365 }366 bool is_stack = false;367 if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {368 auto *loc = New<ReportLocation>();369 loc->type = is_stack ? ReportLocationStack : ReportLocationTLS;370 loc->tid = tctx->tid;371 rep_->locs.PushBack(loc);372 AddThread(tctx);373 }374#endif375 rep_->added_location_addrs.PushBack({addr, rep_->locs.Size()});376 rep_->locs.PushBack(nullptr);377}378 379#if !SANITIZER_GO380void ScopedReportBase::AddSleep(StackID stack_id) {381 rep_->sleep = SymbolizeStackId(stack_id);382}383#endif384 385void ScopedReportBase::SetCount(int count) { rep_->count = count; }386 387void ScopedReportBase::SetSigNum(int sig) { rep_->signum = sig; }388 389const ReportDesc *ScopedReportBase::GetReport() const { return rep_; }390 391ScopedReport::ScopedReport(ReportType typ, uptr tag)392 : ScopedReportBase(typ, tag) {}393 394ScopedReport::~ScopedReport() {}395 396// Replays the trace up to last_pos position in the last part397// or up to the provided epoch/sid (whichever is earlier)398// and calls the provided function f for each event.399template <typename Func>400void TraceReplay(Trace *trace, TracePart *last, Event *last_pos, Sid sid,401 Epoch epoch, Func f) {402 TracePart *part = trace->parts.Front();403 Sid ev_sid = kFreeSid;404 Epoch ev_epoch = kEpochOver;405 for (;;) {406 DCHECK_EQ(part->trace, trace);407 // Note: an event can't start in the last element.408 // Since an event can take up to 2 elements,409 // we ensure we have at least 2 before adding an event.410 Event *end = &part->events[TracePart::kSize - 1];411 if (part == last)412 end = last_pos;413 f(kFreeSid, kEpochOver, nullptr); // notify about part start414 for (Event *evp = &part->events[0]; evp < end; evp++) {415 Event *evp0 = evp;416 if (!evp->is_access && !evp->is_func) {417 switch (evp->type) {418 case EventType::kTime: {419 auto *ev = reinterpret_cast<EventTime *>(evp);420 ev_sid = static_cast<Sid>(ev->sid);421 ev_epoch = static_cast<Epoch>(ev->epoch);422 if (ev_sid == sid && ev_epoch > epoch)423 return;424 break;425 }426 case EventType::kAccessExt:427 FALLTHROUGH;428 case EventType::kAccessRange:429 FALLTHROUGH;430 case EventType::kLock:431 FALLTHROUGH;432 case EventType::kRLock:433 // These take 2 Event elements.434 evp++;435 break;436 case EventType::kUnlock:437 // This takes 1 Event element.438 break;439 }440 }441 CHECK_NE(ev_sid, kFreeSid);442 CHECK_NE(ev_epoch, kEpochOver);443 f(ev_sid, ev_epoch, evp0);444 }445 if (part == last)446 return;447 part = trace->parts.Next(part);448 CHECK(part);449 }450 CHECK(0);451}452 453static void RestoreStackMatch(VarSizeStackTrace *pstk, MutexSet *pmset,454 Vector<uptr> *stack, MutexSet *mset, uptr pc,455 bool *found) {456 DPrintf2(" MATCHED\n");457 *pmset = *mset;458 stack->PushBack(pc);459 pstk->Init(&(*stack)[0], stack->Size());460 stack->PopBack();461 *found = true;462}463 464// Checks if addr1|size1 is fully contained in addr2|size2.465// We check for fully contained instread of just overlapping466// because a memory access is always traced once, but can be467// split into multiple accesses in the shadow.468static constexpr bool IsWithinAccess(uptr addr1, uptr size1, uptr addr2,469 uptr size2) {470 return addr1 >= addr2 && addr1 + size1 <= addr2 + size2;471}472 473// Replays the trace of slot sid up to the target event identified474// by epoch/addr/size/typ and restores and returns tid, stack, mutex set475// and tag for that event. If there are multiple such events, it returns476// the last one. Returns false if the event is not present in the trace.477bool RestoreStack(EventType type, Sid sid, Epoch epoch, uptr addr, uptr size,478 AccessType typ, Tid *ptid, VarSizeStackTrace *pstk,479 MutexSet *pmset, uptr *ptag) {480 // This function restores stack trace and mutex set for the thread/epoch.481 // It does so by getting stack trace and mutex set at the beginning of482 // trace part, and then replaying the trace till the given epoch.483 DPrintf2("RestoreStack: sid=%u@%u addr=0x%zx/%zu typ=%x\n",484 static_cast<int>(sid), static_cast<int>(epoch), addr, size,485 static_cast<int>(typ));486 ctx->slot_mtx.CheckLocked(); // needed to prevent trace part recycling487 ctx->thread_registry.CheckLocked();488 TidSlot *slot = &ctx->slots[static_cast<uptr>(sid)];489 Tid tid = kInvalidTid;490 // Need to lock the slot mutex as it protects slot->journal.491 slot->mtx.CheckLocked();492 for (uptr i = 0; i < slot->journal.Size(); i++) {493 DPrintf2(" journal: epoch=%d tid=%d\n",494 static_cast<int>(slot->journal[i].epoch), slot->journal[i].tid);495 if (i == slot->journal.Size() - 1 || slot->journal[i + 1].epoch > epoch) {496 tid = slot->journal[i].tid;497 break;498 }499 }500 if (tid == kInvalidTid)501 return false;502 *ptid = tid;503 ThreadContext *tctx =504 static_cast<ThreadContext *>(ctx->thread_registry.GetThreadLocked(tid));505 Trace *trace = &tctx->trace;506 // Snapshot first/last parts and the current position in the last part.507 TracePart *first_part;508 TracePart *last_part;509 Event *last_pos;510 {511 Lock lock(&trace->mtx);512 first_part = trace->parts.Front();513 if (!first_part) {514 DPrintf2("RestoreStack: tid=%d trace=%p no trace parts\n", tid, trace);515 return false;516 }517 last_part = trace->parts.Back();518 last_pos = trace->final_pos;519 if (tctx->thr)520 last_pos = (Event *)atomic_load_relaxed(&tctx->thr->trace_pos);521 }522 DynamicMutexSet mset;523 Vector<uptr> stack;524 uptr prev_pc = 0;525 bool found = false;526 bool is_read = typ & kAccessRead;527 bool is_atomic = typ & kAccessAtomic;528 bool is_free = typ & kAccessFree;529 DPrintf2("RestoreStack: tid=%d parts=[%p-%p] last_pos=%p\n", tid,530 trace->parts.Front(), last_part, last_pos);531 TraceReplay(532 trace, last_part, last_pos, sid, epoch,533 [&](Sid ev_sid, Epoch ev_epoch, Event *evp) {534 if (evp == nullptr) {535 // Each trace part is self-consistent, so we reset state.536 stack.Resize(0);537 mset->Reset();538 prev_pc = 0;539 return;540 }541 bool match = ev_sid == sid && ev_epoch == epoch;542 if (evp->is_access) {543 if (evp->is_func == 0 && evp->type == EventType::kAccessExt &&544 evp->_ == 0) // NopEvent545 return;546 auto *ev = reinterpret_cast<EventAccess *>(evp);547 uptr ev_addr = RestoreAddr(ev->addr);548 uptr ev_size = 1 << ev->size_log;549 uptr ev_pc =550 prev_pc + ev->pc_delta - (1 << (EventAccess::kPCBits - 1));551 prev_pc = ev_pc;552 DPrintf2(" Access: pc=0x%zx addr=0x%zx/%zu type=%u/%u\n", ev_pc,553 ev_addr, ev_size, ev->is_read, ev->is_atomic);554 if (match && type == EventType::kAccessExt &&555 IsWithinAccess(addr, size, ev_addr, ev_size) &&556 is_read == ev->is_read && is_atomic == ev->is_atomic && !is_free)557 RestoreStackMatch(pstk, pmset, &stack, mset, ev_pc, &found);558 return;559 }560 if (evp->is_func) {561 auto *ev = reinterpret_cast<EventFunc *>(evp);562 if (ev->pc) {563 DPrintf2(" FuncEnter: pc=0x%llx\n", ev->pc);564 stack.PushBack(ev->pc);565 } else {566 DPrintf2(" FuncExit\n");567 // We don't log pathologically large stacks in each part,568 // if the stack was truncated we can have more func exits than569 // entries.570 if (stack.Size())571 stack.PopBack();572 }573 return;574 }575 switch (evp->type) {576 case EventType::kAccessExt: {577 auto *ev = reinterpret_cast<EventAccessExt *>(evp);578 uptr ev_addr = RestoreAddr(ev->addr);579 uptr ev_size = 1 << ev->size_log;580 prev_pc = ev->pc;581 DPrintf2(" AccessExt: pc=0x%llx addr=0x%zx/%zu type=%u/%u\n",582 ev->pc, ev_addr, ev_size, ev->is_read, ev->is_atomic);583 if (match && type == EventType::kAccessExt &&584 IsWithinAccess(addr, size, ev_addr, ev_size) &&585 is_read == ev->is_read && is_atomic == ev->is_atomic &&586 !is_free)587 RestoreStackMatch(pstk, pmset, &stack, mset, ev->pc, &found);588 break;589 }590 case EventType::kAccessRange: {591 auto *ev = reinterpret_cast<EventAccessRange *>(evp);592 uptr ev_addr = RestoreAddr(ev->addr);593 uptr ev_size =594 (ev->size_hi << EventAccessRange::kSizeLoBits) + ev->size_lo;595 uptr ev_pc = RestoreAddr(ev->pc);596 prev_pc = ev_pc;597 DPrintf2(" Range: pc=0x%zx addr=0x%zx/%zu type=%u/%u\n", ev_pc,598 ev_addr, ev_size, ev->is_read, ev->is_free);599 if (match && type == EventType::kAccessExt &&600 IsWithinAccess(addr, size, ev_addr, ev_size) &&601 is_read == ev->is_read && !is_atomic && is_free == ev->is_free)602 RestoreStackMatch(pstk, pmset, &stack, mset, ev_pc, &found);603 break;604 }605 case EventType::kLock:606 FALLTHROUGH;607 case EventType::kRLock: {608 auto *ev = reinterpret_cast<EventLock *>(evp);609 bool is_write = ev->type == EventType::kLock;610 uptr ev_addr = RestoreAddr(ev->addr);611 uptr ev_pc = RestoreAddr(ev->pc);612 StackID stack_id =613 (ev->stack_hi << EventLock::kStackIDLoBits) + ev->stack_lo;614 DPrintf2(" Lock: pc=0x%zx addr=0x%zx stack=%u write=%d\n", ev_pc,615 ev_addr, stack_id, is_write);616 mset->AddAddr(ev_addr, stack_id, is_write);617 // Events with ev_pc == 0 are written to the beginning of trace618 // part as initial mutex set (are not real).619 if (match && type == EventType::kLock && addr == ev_addr && ev_pc)620 RestoreStackMatch(pstk, pmset, &stack, mset, ev_pc, &found);621 break;622 }623 case EventType::kUnlock: {624 auto *ev = reinterpret_cast<EventUnlock *>(evp);625 uptr ev_addr = RestoreAddr(ev->addr);626 DPrintf2(" Unlock: addr=0x%zx\n", ev_addr);627 mset->DelAddr(ev_addr);628 break;629 }630 case EventType::kTime:631 // TraceReplay already extracted sid/epoch from it,632 // nothing else to do here.633 break;634 }635 });636 ExtractTagFromStack(pstk, ptag);637 return found;638}639 640bool RacyStacks::operator==(const RacyStacks &other) const {641 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])642 return true;643 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])644 return true;645 return false;646}647 648static bool FindRacyStacks(const RacyStacks &hash) {649 for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {650 if (hash == ctx->racy_stacks[i]) {651 VPrintf(2, "ThreadSanitizer: suppressing report as doubled (stack)\n");652 return true;653 }654 }655 return false;656}657 658static bool HandleRacyStacks(ThreadState *thr, VarSizeStackTrace traces[2]) {659 if (!flags()->suppress_equal_stacks)660 return false;661 RacyStacks hash;662 hash.hash[0] = md5_hash(traces[0].trace, traces[0].size * sizeof(uptr));663 hash.hash[1] = md5_hash(traces[1].trace, traces[1].size * sizeof(uptr));664 {665 ReadLock lock(&ctx->racy_mtx);666 if (FindRacyStacks(hash))667 return true;668 }669 Lock lock(&ctx->racy_mtx);670 if (FindRacyStacks(hash))671 return true;672 ctx->racy_stacks.PushBack(hash);673 return false;674}675 676bool OutputReport(ThreadState *thr, ScopedReport &srep) {677 // These should have been checked in ShouldReport.678 // It's too late to check them here, we have already taken locks.679 CHECK(flags()->report_bugs);680 CHECK(!thr->suppress_reports);681 srep.SymbolizeStackElems();682 atomic_store_relaxed(&ctx->last_symbolize_time_ns, NanoTime());683 const ReportDesc *rep = srep.GetReport();684 CHECK_EQ(thr->current_report, nullptr);685 thr->current_report = rep;686 Suppression *supp = 0;687 uptr pc_or_addr = 0;688 for (uptr i = 0; pc_or_addr == 0 && i < rep->mops.Size(); i++)689 pc_or_addr = IsSuppressed(rep->typ, rep->mops[i]->stack, &supp);690 for (uptr i = 0; pc_or_addr == 0 && i < rep->stacks.Size(); i++)691 pc_or_addr = IsSuppressed(rep->typ, rep->stacks[i], &supp);692 for (uptr i = 0; pc_or_addr == 0 && i < rep->threads.Size(); i++)693 pc_or_addr = IsSuppressed(rep->typ, rep->threads[i]->stack, &supp);694 for (uptr i = 0; pc_or_addr == 0 && i < rep->locs.Size(); i++)695 pc_or_addr = IsSuppressed(rep->typ, rep->locs[i], &supp);696 if (pc_or_addr != 0) {697 Lock lock(&ctx->fired_suppressions_mtx);698 FiredSuppression s = {srep.GetReport()->typ, pc_or_addr, supp};699 ctx->fired_suppressions.push_back(s);700 }701 {702 bool suppressed = OnReport(rep, pc_or_addr != 0);703 if (suppressed) {704 thr->current_report = nullptr;705 return false;706 }707 }708 PrintReport(rep);709 __tsan_on_report(rep);710 ctx->nreported++;711 if (flags()->halt_on_error)712 Die();713 thr->current_report = nullptr;714 return true;715}716 717bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace) {718 ReadLock lock(&ctx->fired_suppressions_mtx);719 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {720 if (ctx->fired_suppressions[k].type != type)721 continue;722 for (uptr j = 0; j < trace.size; j++) {723 FiredSuppression *s = &ctx->fired_suppressions[k];724 if (trace.trace[j] == s->pc_or_addr) {725 if (s->supp)726 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);727 return true;728 }729 }730 }731 return false;732}733 734static bool IsFiredSuppression(Context *ctx, ReportType type, uptr addr) {735 ReadLock lock(&ctx->fired_suppressions_mtx);736 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {737 if (ctx->fired_suppressions[k].type != type)738 continue;739 FiredSuppression *s = &ctx->fired_suppressions[k];740 if (addr == s->pc_or_addr) {741 if (s->supp)742 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);743 return true;744 }745 }746 return false;747}748 749static bool SpuriousRace(Shadow old) {750 Shadow last(LoadShadow(&ctx->last_spurious_race));751 return last.sid() == old.sid() && last.epoch() == old.epoch();752}753 754void ReportRace(ThreadState *thr, RawShadow *shadow_mem, Shadow cur, Shadow old,755 AccessType typ0) {756 CheckedMutex::CheckNoLocks();757 758 // Symbolizer makes lots of intercepted calls. If we try to process them,759 // at best it will cause deadlocks on internal mutexes.760 ScopedIgnoreInterceptors ignore;761 762 uptr addr = ShadowToMem(shadow_mem);763 DPrintf("#%d: ReportRace %p\n", thr->tid, (void *)addr);764 if (!ShouldReport(thr, ReportTypeRace))765 return;766 uptr addr_off0, size0;767 cur.GetAccess(&addr_off0, &size0, nullptr);768 uptr addr_off1, size1, typ1;769 old.GetAccess(&addr_off1, &size1, &typ1);770 if (!flags()->report_atomic_races &&771 ((typ0 & kAccessAtomic) || (typ1 & kAccessAtomic)) &&772 !(typ0 & kAccessFree) && !(typ1 & kAccessFree))773 return;774 if (SpuriousRace(old))775 return;776 777 const uptr kMop = 2;778 Shadow s[kMop] = {cur, old};779 uptr addr0 = addr + addr_off0;780 uptr addr1 = addr + addr_off1;781 uptr end0 = addr0 + size0;782 uptr end1 = addr1 + size1;783 uptr addr_min = min(addr0, addr1);784 uptr addr_max = max(end0, end1);785 if (IsExpectedReport(addr_min, addr_max - addr_min))786 return;787 788 ReportType rep_typ = ReportTypeRace;789 if ((typ0 & kAccessVptr) && (typ1 & kAccessFree))790 rep_typ = ReportTypeVptrUseAfterFree;791 else if (typ0 & kAccessVptr)792 rep_typ = ReportTypeVptrRace;793 else if (typ1 & kAccessFree)794 rep_typ = ReportTypeUseAfterFree;795 796 if (IsFiredSuppression(ctx, rep_typ, addr))797 return;798 799 VarSizeStackTrace traces[kMop];800 Tid tids[kMop] = {thr->tid, kInvalidTid};801 uptr tags[kMop] = {kExternalTagNone, kExternalTagNone};802 803 ObtainCurrentStack(thr, thr->trace_prev_pc, &traces[0], &tags[0]);804 if (IsFiredSuppression(ctx, rep_typ, traces[0]))805 return;806 807 DynamicMutexSet mset1;808 MutexSet *mset[kMop] = {&thr->mset, mset1};809 810 // Use alloca, because malloc during signal handling deadlocks811 ScopedReport *rep = (ScopedReport *)__builtin_alloca(sizeof(ScopedReport));812 // Take a new scope as Apple platforms require the below locks released813 // before symbolizing in order to avoid a deadlock814 {815 // We need to lock the slot during RestoreStack because it protects816 // the slot journal.817 Lock slot_lock(&ctx->slots[static_cast<uptr>(s[1].sid())].mtx);818 ThreadRegistryLock l0(&ctx->thread_registry);819 Lock slots_lock(&ctx->slot_mtx);820 if (SpuriousRace(old))821 return;822 if (!RestoreStack(EventType::kAccessExt, s[1].sid(), s[1].epoch(), addr1,823 size1, typ1, &tids[1], &traces[1], mset[1], &tags[1])) {824 StoreShadow(&ctx->last_spurious_race, old.raw());825 return;826 }827 828 if (IsFiredSuppression(ctx, rep_typ, traces[1]))829 return;830 831 if (HandleRacyStacks(thr, traces))832 return;833 834 // If any of the accesses has a tag, treat this as an "external" race.835 uptr tag = kExternalTagNone;836 for (uptr i = 0; i < kMop; i++) {837 if (tags[i] != kExternalTagNone) {838 rep_typ = ReportTypeExternalRace;839 tag = tags[i];840 break;841 }842 }843 844 new (rep) ScopedReport(rep_typ, tag);845 for (uptr i = 0; i < kMop; i++)846 rep->AddMemoryAccess(addr, tags[i], s[i], tids[i], traces[i], mset[i]);847 848 for (uptr i = 0; i < kMop; i++) {849 ThreadContext *tctx = static_cast<ThreadContext *>(850 ctx->thread_registry.GetThreadLocked(tids[i]));851 rep->AddThread(tctx);852 }853 854 rep->AddLocation(addr_min, addr_max - addr_min);855 856 if (flags()->print_full_thread_history) {857 const ReportDesc *rep_desc = rep->GetReport();858 for (uptr i = 0; i < rep_desc->threads.Size(); i++) {859 Tid parent_tid = rep_desc->threads[i]->parent_tid;860 if (parent_tid == kMainTid || parent_tid == kInvalidTid)861 continue;862 ThreadContext *parent_tctx = static_cast<ThreadContext *>(863 ctx->thread_registry.GetThreadLocked(parent_tid));864 rep->AddThread(parent_tctx);865 }866 }867 868#if !SANITIZER_GO869 if (!((typ0 | typ1) & kAccessFree) &&870 s[1].epoch() <= thr->last_sleep_clock.Get(s[1].sid()))871 rep->AddSleep(thr->last_sleep_stack_id);872#endif873 874#if SANITIZER_APPLE875 } // Close this scope to release the locks876#endif877 OutputReport(thr, *rep);878 879 // Need to manually destroy this because we used placement new to allocate880 rep->~ScopedReport();881#if !SANITIZER_APPLE882 }883#endif884}885 886void PrintCurrentStack(ThreadState *thr, uptr pc) {887 VarSizeStackTrace trace;888 ObtainCurrentStack(thr, pc, &trace);889 PrintStack(SymbolizeStack(trace));890}891 892// Always inlining PrintCurrentStack, because LocatePcInTrace assumes893// __sanitizer_print_stack_trace exists in the actual unwinded stack, but894// tail-call to PrintCurrentStack breaks this assumption because895// __sanitizer_print_stack_trace disappears after tail-call.896// However, this solution is not reliable enough, please see dvyukov's comment897// http://reviews.llvm.org/D19148#406208898// Also see PR27280 comment 2 and 3 for breaking examples and analysis.899ALWAYS_INLINE USED void PrintCurrentStack(uptr pc, bool fast) {900#if !SANITIZER_GO901 uptr bp = GET_CURRENT_FRAME();902 auto *ptrace = New<BufferedStackTrace>();903 ptrace->Unwind(pc, bp, nullptr, fast);904 905 for (uptr i = 0; i < ptrace->size / 2; i++) {906 uptr tmp = ptrace->trace_buffer[i];907 ptrace->trace_buffer[i] = ptrace->trace_buffer[ptrace->size - i - 1];908 ptrace->trace_buffer[ptrace->size - i - 1] = tmp;909 }910 911 if (ready_to_symbolize) {912 PrintStack(SymbolizeStack(*ptrace));913 } else {914 Printf(915 "WARNING: PrintCurrentStack() has been called too early, before "916 "symbolization is possible. Printing unsymbolized stack trace:\n");917 for (unsigned int i = 0; i < ptrace->size; i++)918 Printf(" #%u: 0x%zx\n", i, ptrace->trace[i]);919 }920#endif921}922 923} // namespace __tsan924 925using namespace __tsan;926 927extern "C" {928SANITIZER_INTERFACE_ATTRIBUTE929void __sanitizer_print_stack_trace() {930 PrintCurrentStack(StackTrace::GetCurrentPc(), false);931}932} // extern "C"933