• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- tsan_rtl_report.cc ------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_common/sanitizer_libc.h"
15 #include "sanitizer_common/sanitizer_placement_new.h"
16 #include "sanitizer_common/sanitizer_stackdepot.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_stacktrace.h"
19 #include "tsan_platform.h"
20 #include "tsan_rtl.h"
21 #include "tsan_suppressions.h"
22 #include "tsan_symbolize.h"
23 #include "tsan_report.h"
24 #include "tsan_sync.h"
25 #include "tsan_mman.h"
26 #include "tsan_flags.h"
27 #include "tsan_fd.h"
28 
29 namespace __tsan {
30 
31 using namespace __sanitizer;  // NOLINT
32 
33 static ReportStack *SymbolizeStack(const StackTrace& trace);
34 
TsanCheckFailed(const char * file,int line,const char * cond,u64 v1,u64 v2)35 void TsanCheckFailed(const char *file, int line, const char *cond,
36                      u64 v1, u64 v2) {
37   ScopedInRtl in_rtl;
38   Printf("FATAL: ThreadSanitizer CHECK failed: "
39          "%s:%d \"%s\" (0x%zx, 0x%zx)\n",
40          file, line, cond, (uptr)v1, (uptr)v2);
41   PrintCurrentStackSlow();
42   Die();
43 }
44 
45 // Can be overriden by an application/test to intercept reports.
46 #ifdef TSAN_EXTERNAL_HOOKS
47 bool OnReport(const ReportDesc *rep, bool suppressed);
48 #else
49 SANITIZER_INTERFACE_ATTRIBUTE
OnReport(const ReportDesc * rep,bool suppressed)50 bool WEAK OnReport(const ReportDesc *rep, bool suppressed) {
51   (void)rep;
52   return suppressed;
53 }
54 #endif
55 
StackStripMain(ReportStack * stack)56 static void StackStripMain(ReportStack *stack) {
57   ReportStack *last_frame = 0;
58   ReportStack *last_frame2 = 0;
59   const char *prefix = "__interceptor_";
60   uptr prefix_len = internal_strlen(prefix);
61   const char *path_prefix = flags()->strip_path_prefix;
62   uptr path_prefix_len = internal_strlen(path_prefix);
63   char *pos;
64   for (ReportStack *ent = stack; ent; ent = ent->next) {
65     if (ent->func && 0 == internal_strncmp(ent->func, prefix, prefix_len))
66       ent->func += prefix_len;
67     if (ent->file && (pos = internal_strstr(ent->file, path_prefix)))
68       ent->file = pos + path_prefix_len;
69     if (ent->file && ent->file[0] == '.' && ent->file[1] == '/')
70       ent->file += 2;
71     last_frame2 = last_frame;
72     last_frame = ent;
73   }
74 
75   if (last_frame2 == 0)
76     return;
77   const char *last = last_frame->func;
78 #ifndef TSAN_GO
79   const char *last2 = last_frame2->func;
80   // Strip frame above 'main'
81   if (last2 && 0 == internal_strcmp(last2, "main")) {
82     last_frame2->next = 0;
83   // Strip our internal thread start routine.
84   } else if (last && 0 == internal_strcmp(last, "__tsan_thread_start_func")) {
85     last_frame2->next = 0;
86   // Strip global ctors init.
87   } else if (last && 0 == internal_strcmp(last, "__do_global_ctors_aux")) {
88     last_frame2->next = 0;
89   // If both are 0, then we probably just failed to symbolize.
90   } else if (last || last2) {
91     // Ensure that we recovered stack completely. Trimmed stack
92     // can actually happen if we do not instrument some code,
93     // so it's only a debug print. However we must try hard to not miss it
94     // due to our fault.
95     DPrintf("Bottom stack frame of stack %zx is missed\n", stack->pc);
96   }
97 #else
98   if (last && 0 == internal_strcmp(last, "schedunlock"))
99     last_frame2->next = 0;
100 #endif
101 }
102 
SymbolizeStack(const StackTrace & trace)103 static ReportStack *SymbolizeStack(const StackTrace& trace) {
104   if (trace.IsEmpty())
105     return 0;
106   ReportStack *stack = 0;
107   for (uptr si = 0; si < trace.Size(); si++) {
108     // We obtain the return address, that is, address of the next instruction,
109     // so offset it by 1 byte.
110     bool is_last = (si == trace.Size() - 1);
111     ReportStack *ent = SymbolizeCode(trace.Get(si) - !is_last);
112     CHECK_NE(ent, 0);
113     ReportStack *last = ent;
114     while (last->next) {
115       last->pc += !is_last;
116       last = last->next;
117     }
118     last->pc += !is_last;
119     last->next = stack;
120     stack = ent;
121   }
122   StackStripMain(stack);
123   return stack;
124 }
125 
ScopedReport(ReportType typ)126 ScopedReport::ScopedReport(ReportType typ) {
127   ctx_ = CTX();
128   ctx_->thread_registry->CheckLocked();
129   void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
130   rep_ = new(mem) ReportDesc;
131   rep_->typ = typ;
132   ctx_->report_mtx.Lock();
133 }
134 
~ScopedReport()135 ScopedReport::~ScopedReport() {
136   ctx_->report_mtx.Unlock();
137   DestroyAndFree(rep_);
138 }
139 
AddStack(const StackTrace * stack)140 void ScopedReport::AddStack(const StackTrace *stack) {
141   ReportStack **rs = rep_->stacks.PushBack();
142   *rs = SymbolizeStack(*stack);
143 }
144 
AddMemoryAccess(uptr addr,Shadow s,const StackTrace * stack,const MutexSet * mset)145 void ScopedReport::AddMemoryAccess(uptr addr, Shadow s,
146     const StackTrace *stack, const MutexSet *mset) {
147   void *mem = internal_alloc(MBlockReportMop, sizeof(ReportMop));
148   ReportMop *mop = new(mem) ReportMop;
149   rep_->mops.PushBack(mop);
150   mop->tid = s.tid();
151   mop->addr = addr + s.addr0();
152   mop->size = s.size();
153   mop->write = s.IsWrite();
154   mop->atomic = s.IsAtomic();
155   mop->stack = SymbolizeStack(*stack);
156   for (uptr i = 0; i < mset->Size(); i++) {
157     MutexSet::Desc d = mset->Get(i);
158     u64 uid = 0;
159     uptr addr = SyncVar::SplitId(d.id, &uid);
160     SyncVar *s = ctx_->synctab.GetIfExistsAndLock(addr, false);
161     // Check that the mutex is still alive.
162     // Another mutex can be created at the same address,
163     // so check uid as well.
164     if (s && s->CheckId(uid)) {
165       ReportMopMutex mtx = {s->uid, d.write};
166       mop->mset.PushBack(mtx);
167       AddMutex(s);
168     } else {
169       ReportMopMutex mtx = {d.id, d.write};
170       mop->mset.PushBack(mtx);
171       AddMutex(d.id);
172     }
173     if (s)
174       s->mtx.ReadUnlock();
175   }
176 }
177 
AddThread(const ThreadContext * tctx)178 void ScopedReport::AddThread(const ThreadContext *tctx) {
179   for (uptr i = 0; i < rep_->threads.Size(); i++) {
180     if ((u32)rep_->threads[i]->id == tctx->tid)
181       return;
182   }
183   void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
184   ReportThread *rt = new(mem) ReportThread();
185   rep_->threads.PushBack(rt);
186   rt->id = tctx->tid;
187   rt->pid = tctx->os_id;
188   rt->running = (tctx->status == ThreadStatusRunning);
189   rt->name = tctx->name ? internal_strdup(tctx->name) : 0;
190   rt->parent_tid = tctx->parent_tid;
191   rt->stack = 0;
192 #ifdef TSAN_GO
193   rt->stack = SymbolizeStack(tctx->creation_stack);
194 #else
195   uptr ssz = 0;
196   const uptr *stack = StackDepotGet(tctx->creation_stack_id, &ssz);
197   if (stack) {
198     StackTrace trace;
199     trace.Init(stack, ssz);
200     rt->stack = SymbolizeStack(trace);
201   }
202 #endif
203 }
204 
205 #ifndef TSAN_GO
FindThreadByUidLocked(int unique_id)206 static ThreadContext *FindThreadByUidLocked(int unique_id) {
207   Context *ctx = CTX();
208   ctx->thread_registry->CheckLocked();
209   for (unsigned i = 0; i < kMaxTid; i++) {
210     ThreadContext *tctx = static_cast<ThreadContext*>(
211         ctx->thread_registry->GetThreadLocked(i));
212     if (tctx && tctx->unique_id == (u32)unique_id) {
213       return tctx;
214     }
215   }
216   return 0;
217 }
218 
FindThreadByTidLocked(int tid)219 static ThreadContext *FindThreadByTidLocked(int tid) {
220   Context *ctx = CTX();
221   ctx->thread_registry->CheckLocked();
222   return static_cast<ThreadContext*>(
223       ctx->thread_registry->GetThreadLocked(tid));
224 }
225 
IsInStackOrTls(ThreadContextBase * tctx_base,void * arg)226 static bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {
227   uptr addr = (uptr)arg;
228   ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
229   if (tctx->status != ThreadStatusRunning)
230     return false;
231   ThreadState *thr = tctx->thr;
232   CHECK(thr);
233   return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||
234           (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));
235 }
236 
IsThreadStackOrTls(uptr addr,bool * is_stack)237 ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
238   Context *ctx = CTX();
239   ctx->thread_registry->CheckLocked();
240   ThreadContext *tctx = static_cast<ThreadContext*>(
241       ctx->thread_registry->FindThreadContextLocked(IsInStackOrTls,
242                                                     (void*)addr));
243   if (!tctx)
244     return 0;
245   ThreadState *thr = tctx->thr;
246   CHECK(thr);
247   *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
248   return tctx;
249 }
250 #endif
251 
AddMutex(const SyncVar * s)252 void ScopedReport::AddMutex(const SyncVar *s) {
253   for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
254     if (rep_->mutexes[i]->id == s->uid)
255       return;
256   }
257   void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
258   ReportMutex *rm = new(mem) ReportMutex();
259   rep_->mutexes.PushBack(rm);
260   rm->id = s->uid;
261   rm->destroyed = false;
262   rm->stack = 0;
263 #ifndef TSAN_GO
264   uptr ssz = 0;
265   const uptr *stack = StackDepotGet(s->creation_stack_id, &ssz);
266   if (stack) {
267     StackTrace trace;
268     trace.Init(stack, ssz);
269     rm->stack = SymbolizeStack(trace);
270   }
271 #endif
272 }
273 
AddMutex(u64 id)274 void ScopedReport::AddMutex(u64 id) {
275   for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
276     if (rep_->mutexes[i]->id == id)
277       return;
278   }
279   void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
280   ReportMutex *rm = new(mem) ReportMutex();
281   rep_->mutexes.PushBack(rm);
282   rm->id = id;
283   rm->destroyed = true;
284   rm->stack = 0;
285 }
286 
AddLocation(uptr addr,uptr size)287 void ScopedReport::AddLocation(uptr addr, uptr size) {
288   if (addr == 0)
289     return;
290 #ifndef TSAN_GO
291   int fd = -1;
292   int creat_tid = -1;
293   u32 creat_stack = 0;
294   if (FdLocation(addr, &fd, &creat_tid, &creat_stack)
295       || FdLocation(AlternativeAddress(addr), &fd, &creat_tid, &creat_stack)) {
296     void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
297     ReportLocation *loc = new(mem) ReportLocation();
298     rep_->locs.PushBack(loc);
299     loc->type = ReportLocationFD;
300     loc->fd = fd;
301     loc->tid = creat_tid;
302     uptr ssz = 0;
303     const uptr *stack = StackDepotGet(creat_stack, &ssz);
304     if (stack) {
305       StackTrace trace;
306       trace.Init(stack, ssz);
307       loc->stack = SymbolizeStack(trace);
308     }
309     ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
310     if (tctx)
311       AddThread(tctx);
312     return;
313   }
314   if (allocator()->PointerIsMine((void*)addr)) {
315     MBlock *b = user_mblock(0, (void*)addr);
316     ThreadContext *tctx = FindThreadByTidLocked(b->Tid());
317     void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
318     ReportLocation *loc = new(mem) ReportLocation();
319     rep_->locs.PushBack(loc);
320     loc->type = ReportLocationHeap;
321     loc->addr = (uptr)allocator()->GetBlockBegin((void*)addr);
322     loc->size = b->Size();
323     loc->tid = tctx ? tctx->tid : b->Tid();
324     loc->name = 0;
325     loc->file = 0;
326     loc->line = 0;
327     loc->stack = 0;
328     uptr ssz = 0;
329     const uptr *stack = StackDepotGet(b->StackId(), &ssz);
330     if (stack) {
331       StackTrace trace;
332       trace.Init(stack, ssz);
333       loc->stack = SymbolizeStack(trace);
334     }
335     if (tctx)
336       AddThread(tctx);
337     return;
338   }
339   bool is_stack = false;
340   if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
341     void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
342     ReportLocation *loc = new(mem) ReportLocation();
343     rep_->locs.PushBack(loc);
344     loc->type = is_stack ? ReportLocationStack : ReportLocationTLS;
345     loc->tid = tctx->tid;
346     AddThread(tctx);
347   }
348   ReportLocation *loc = SymbolizeData(addr);
349   if (loc) {
350     rep_->locs.PushBack(loc);
351     return;
352   }
353 #endif
354 }
355 
356 #ifndef TSAN_GO
AddSleep(u32 stack_id)357 void ScopedReport::AddSleep(u32 stack_id) {
358   uptr ssz = 0;
359   const uptr *stack = StackDepotGet(stack_id, &ssz);
360   if (stack) {
361     StackTrace trace;
362     trace.Init(stack, ssz);
363     rep_->sleep = SymbolizeStack(trace);
364   }
365 }
366 #endif
367 
GetReport() const368 const ReportDesc *ScopedReport::GetReport() const {
369   return rep_;
370 }
371 
RestoreStack(int tid,const u64 epoch,StackTrace * stk,MutexSet * mset)372 void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset) {
373   // This function restores stack trace and mutex set for the thread/epoch.
374   // It does so by getting stack trace and mutex set at the beginning of
375   // trace part, and then replaying the trace till the given epoch.
376   Context *ctx = CTX();
377   ctx->thread_registry->CheckLocked();
378   ThreadContext *tctx = static_cast<ThreadContext*>(
379       ctx->thread_registry->GetThreadLocked(tid));
380   if (tctx == 0)
381     return;
382   Trace* trace = 0;
383   if (tctx->status == ThreadStatusRunning) {
384     CHECK(tctx->thr);
385     trace = &tctx->thr->trace;
386   } else if (tctx->status == ThreadStatusFinished
387       || tctx->status == ThreadStatusDead) {
388     if (tctx->dead_info == 0)
389       return;
390     trace = &tctx->dead_info->trace;
391   } else {
392     return;
393   }
394   Lock l(&trace->mtx);
395   const int partidx = (epoch / kTracePartSize) % TraceParts();
396   TraceHeader* hdr = &trace->headers[partidx];
397   if (epoch < hdr->epoch0)
398     return;
399   const u64 epoch0 = RoundDown(epoch, TraceSize());
400   const u64 eend = epoch % TraceSize();
401   const u64 ebegin = RoundDown(eend, kTracePartSize);
402   DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
403           tid, (uptr)epoch, (uptr)ebegin, (uptr)eend, partidx);
404   InternalScopedBuffer<uptr> stack(1024);  // FIXME: de-hardcode 1024
405   for (uptr i = 0; i < hdr->stack0.Size(); i++) {
406     stack[i] = hdr->stack0.Get(i);
407     DPrintf2("  #%02lu: pc=%zx\n", i, stack[i]);
408   }
409   if (mset)
410     *mset = hdr->mset0;
411   uptr pos = hdr->stack0.Size();
412   Event *events = (Event*)GetThreadTrace(tid);
413   for (uptr i = ebegin; i <= eend; i++) {
414     Event ev = events[i];
415     EventType typ = (EventType)(ev >> 61);
416     uptr pc = (uptr)(ev & ((1ull << 61) - 1));
417     DPrintf2("  %zu typ=%d pc=%zx\n", i, typ, pc);
418     if (typ == EventTypeMop) {
419       stack[pos] = pc;
420     } else if (typ == EventTypeFuncEnter) {
421       stack[pos++] = pc;
422     } else if (typ == EventTypeFuncExit) {
423       if (pos > 0)
424         pos--;
425     }
426     if (mset) {
427       if (typ == EventTypeLock) {
428         mset->Add(pc, true, epoch0 + i);
429       } else if (typ == EventTypeUnlock) {
430         mset->Del(pc, true);
431       } else if (typ == EventTypeRLock) {
432         mset->Add(pc, false, epoch0 + i);
433       } else if (typ == EventTypeRUnlock) {
434         mset->Del(pc, false);
435       }
436     }
437     for (uptr j = 0; j <= pos; j++)
438       DPrintf2("      #%zu: %zx\n", j, stack[j]);
439   }
440   if (pos == 0 && stack[0] == 0)
441     return;
442   pos++;
443   stk->Init(stack.data(), pos);
444 }
445 
HandleRacyStacks(ThreadState * thr,const StackTrace (& traces)[2],uptr addr_min,uptr addr_max)446 static bool HandleRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
447     uptr addr_min, uptr addr_max) {
448   Context *ctx = CTX();
449   bool equal_stack = false;
450   RacyStacks hash;
451   if (flags()->suppress_equal_stacks) {
452     hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
453     hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
454     for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {
455       if (hash == ctx->racy_stacks[i]) {
456         DPrintf("ThreadSanitizer: suppressing report as doubled (stack)\n");
457         equal_stack = true;
458         break;
459       }
460     }
461   }
462   bool equal_address = false;
463   RacyAddress ra0 = {addr_min, addr_max};
464   if (flags()->suppress_equal_addresses) {
465     for (uptr i = 0; i < ctx->racy_addresses.Size(); i++) {
466       RacyAddress ra2 = ctx->racy_addresses[i];
467       uptr maxbeg = max(ra0.addr_min, ra2.addr_min);
468       uptr minend = min(ra0.addr_max, ra2.addr_max);
469       if (maxbeg < minend) {
470         DPrintf("ThreadSanitizer: suppressing report as doubled (addr)\n");
471         equal_address = true;
472         break;
473       }
474     }
475   }
476   if (equal_stack || equal_address) {
477     if (!equal_stack)
478       ctx->racy_stacks.PushBack(hash);
479     if (!equal_address)
480       ctx->racy_addresses.PushBack(ra0);
481     return true;
482   }
483   return false;
484 }
485 
AddRacyStacks(ThreadState * thr,const StackTrace (& traces)[2],uptr addr_min,uptr addr_max)486 static void AddRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
487     uptr addr_min, uptr addr_max) {
488   Context *ctx = CTX();
489   if (flags()->suppress_equal_stacks) {
490     RacyStacks hash;
491     hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
492     hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
493     ctx->racy_stacks.PushBack(hash);
494   }
495   if (flags()->suppress_equal_addresses) {
496     RacyAddress ra0 = {addr_min, addr_max};
497     ctx->racy_addresses.PushBack(ra0);
498   }
499 }
500 
OutputReport(Context * ctx,const ScopedReport & srep,const ReportStack * suppress_stack1,const ReportStack * suppress_stack2)501 bool OutputReport(Context *ctx,
502                   const ScopedReport &srep,
503                   const ReportStack *suppress_stack1,
504                   const ReportStack *suppress_stack2) {
505   const ReportDesc *rep = srep.GetReport();
506   uptr suppress_pc = IsSuppressed(rep->typ, suppress_stack1);
507   if (suppress_pc == 0)
508     suppress_pc = IsSuppressed(rep->typ, suppress_stack2);
509   if (suppress_pc != 0) {
510     FiredSuppression supp = {srep.GetReport()->typ, suppress_pc};
511     ctx->fired_suppressions.PushBack(supp);
512   }
513   if (OnReport(rep, suppress_pc != 0))
514     return false;
515   PrintReport(rep);
516   CTX()->nreported++;
517   return true;
518 }
519 
IsFiredSuppression(Context * ctx,const ScopedReport & srep,const StackTrace & trace)520 bool IsFiredSuppression(Context *ctx,
521                         const ScopedReport &srep,
522                         const StackTrace &trace) {
523   for (uptr k = 0; k < ctx->fired_suppressions.Size(); k++) {
524     if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
525       continue;
526     for (uptr j = 0; j < trace.Size(); j++) {
527       if (trace.Get(j) == ctx->fired_suppressions[k].pc)
528         return true;
529     }
530   }
531   return false;
532 }
533 
FrameIsInternal(const ReportStack * frame)534 bool FrameIsInternal(const ReportStack *frame) {
535   return frame != 0 && frame->file != 0
536       && (internal_strstr(frame->file, "tsan_interceptors.cc") ||
537           internal_strstr(frame->file, "sanitizer_common_interceptors.inc") ||
538           internal_strstr(frame->file, "tsan_interface_"));
539 }
540 
541 // On programs that use Java we see weird reports like:
542 // WARNING: ThreadSanitizer: data race (pid=22512)
543 //   Read of size 8 at 0x7d2b00084318 by thread 100:
544 //     #0 memcpy tsan_interceptors.cc:406 (foo+0x00000d8dfae3)
545 //     #1 <null> <null>:0 (0x7f7ad9b40193)
546 //   Previous write of size 8 at 0x7d2b00084318 by thread 105:
547 //     #0 strncpy tsan_interceptors.cc:501 (foo+0x00000d8e0919)
548 //     #1 <null> <null>:0 (0x7f7ad9b42707)
IsJavaNonsense(const ReportDesc * rep)549 static bool IsJavaNonsense(const ReportDesc *rep) {
550 #ifndef TSAN_GO
551   for (uptr i = 0; i < rep->mops.Size(); i++) {
552     ReportMop *mop = rep->mops[i];
553     ReportStack *frame = mop->stack;
554     if (frame == 0
555         || (frame->func == 0 && frame->file == 0 && frame->line == 0
556           && frame->module == 0)) {
557       return true;
558     }
559     if (FrameIsInternal(frame)) {
560       frame = frame->next;
561       if (frame == 0
562           || (frame->func == 0 && frame->file == 0 && frame->line == 0
563           && frame->module == 0)) {
564         if (frame) {
565           FiredSuppression supp = {rep->typ, frame->pc};
566           CTX()->fired_suppressions.PushBack(supp);
567         }
568         return true;
569       }
570     }
571   }
572 #endif
573   return false;
574 }
575 
RaceBetweenAtomicAndFree(ThreadState * thr)576 static bool RaceBetweenAtomicAndFree(ThreadState *thr) {
577   Shadow s0(thr->racy_state[0]);
578   Shadow s1(thr->racy_state[1]);
579   CHECK(!(s0.IsAtomic() && s1.IsAtomic()));
580   if (!s0.IsAtomic() && !s1.IsAtomic())
581     return true;
582   if (s0.IsAtomic() && s1.IsFreed())
583     return true;
584   if (s1.IsAtomic() && thr->is_freeing)
585     return true;
586   return false;
587 }
588 
ReportRace(ThreadState * thr)589 void ReportRace(ThreadState *thr) {
590   if (!flags()->report_bugs)
591     return;
592   ScopedInRtl in_rtl;
593 
594   if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
595     return;
596 
597   if (thr->in_signal_handler)
598     Printf("ThreadSanitizer: printing report from signal handler."
599            " Can crash or hang.\n");
600 
601   bool freed = false;
602   {
603     Shadow s(thr->racy_state[1]);
604     freed = s.GetFreedAndReset();
605     thr->racy_state[1] = s.raw();
606   }
607 
608   uptr addr = ShadowToMem((uptr)thr->racy_shadow_addr);
609   uptr addr_min = 0;
610   uptr addr_max = 0;
611   {
612     uptr a0 = addr + Shadow(thr->racy_state[0]).addr0();
613     uptr a1 = addr + Shadow(thr->racy_state[1]).addr0();
614     uptr e0 = a0 + Shadow(thr->racy_state[0]).size();
615     uptr e1 = a1 + Shadow(thr->racy_state[1]).size();
616     addr_min = min(a0, a1);
617     addr_max = max(e0, e1);
618     if (IsExpectedReport(addr_min, addr_max - addr_min))
619       return;
620   }
621 
622   Context *ctx = CTX();
623   ThreadRegistryLock l0(ctx->thread_registry);
624 
625   ScopedReport rep(freed ? ReportTypeUseAfterFree : ReportTypeRace);
626   const uptr kMop = 2;
627   StackTrace traces[kMop];
628   const uptr toppc = TraceTopPC(thr);
629   traces[0].ObtainCurrent(thr, toppc);
630   if (IsFiredSuppression(ctx, rep, traces[0]))
631     return;
632   InternalScopedBuffer<MutexSet> mset2(1);
633   new(mset2.data()) MutexSet();
634   Shadow s2(thr->racy_state[1]);
635   RestoreStack(s2.tid(), s2.epoch(), &traces[1], mset2.data());
636 
637   if (HandleRacyStacks(thr, traces, addr_min, addr_max))
638     return;
639 
640   for (uptr i = 0; i < kMop; i++) {
641     Shadow s(thr->racy_state[i]);
642     rep.AddMemoryAccess(addr, s, &traces[i],
643                         i == 0 ? &thr->mset : mset2.data());
644   }
645 
646   if (flags()->suppress_java && IsJavaNonsense(rep.GetReport()))
647     return;
648 
649   for (uptr i = 0; i < kMop; i++) {
650     FastState s(thr->racy_state[i]);
651     ThreadContext *tctx = static_cast<ThreadContext*>(
652         ctx->thread_registry->GetThreadLocked(s.tid()));
653     if (s.epoch() < tctx->epoch0 || s.epoch() > tctx->epoch1)
654       continue;
655     rep.AddThread(tctx);
656   }
657 
658   rep.AddLocation(addr_min, addr_max - addr_min);
659 
660 #ifndef TSAN_GO
661   {  // NOLINT
662     Shadow s(thr->racy_state[1]);
663     if (s.epoch() <= thr->last_sleep_clock.get(s.tid()))
664       rep.AddSleep(thr->last_sleep_stack_id);
665   }
666 #endif
667 
668   if (!OutputReport(ctx, rep, rep.GetReport()->mops[0]->stack,
669                               rep.GetReport()->mops[1]->stack))
670     return;
671 
672   AddRacyStacks(thr, traces, addr_min, addr_max);
673 }
674 
PrintCurrentStack(ThreadState * thr,uptr pc)675 void PrintCurrentStack(ThreadState *thr, uptr pc) {
676   StackTrace trace;
677   trace.ObtainCurrent(thr, pc);
678   PrintStack(SymbolizeStack(trace));
679 }
680 
PrintCurrentStackSlow()681 void PrintCurrentStackSlow() {
682 #ifndef TSAN_GO
683   __sanitizer::StackTrace *ptrace = new(internal_alloc(MBlockStackTrace,
684       sizeof(__sanitizer::StackTrace))) __sanitizer::StackTrace;
685   ptrace->SlowUnwindStack(__sanitizer::StackTrace::GetCurrentPc(),
686       kStackTraceMax);
687   StackTrace trace;
688   trace.Init(ptrace->trace, ptrace->size);
689   PrintStack(SymbolizeStack(trace));
690 #endif
691 }
692 
693 }  // namespace __tsan
694