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 // The last frame always point into runtime (gosched0, goexit0, runtime.main).
99 last_frame2->next = 0;
100 (void)last;
101 #endif
102 }
103
SymbolizeStack(const StackTrace & trace)104 static ReportStack *SymbolizeStack(const StackTrace& trace) {
105 if (trace.IsEmpty())
106 return 0;
107 ReportStack *stack = 0;
108 for (uptr si = 0; si < trace.Size(); si++) {
109 const uptr pc = trace.Get(si);
110 #ifndef TSAN_GO
111 // We obtain the return address, that is, address of the next instruction,
112 // so offset it by 1 byte.
113 const uptr pc1 = __sanitizer::StackTrace::GetPreviousInstructionPc(pc);
114 #else
115 // FIXME(dvyukov): Go sometimes uses address of a function as top pc.
116 uptr pc1 = pc;
117 if (si != trace.Size() - 1)
118 pc1 -= 1;
119 #endif
120 ReportStack *ent = SymbolizeCode(pc1);
121 CHECK_NE(ent, 0);
122 ReportStack *last = ent;
123 while (last->next) {
124 last->pc = pc; // restore original pc for report
125 last = last->next;
126 }
127 last->pc = pc; // restore original pc for report
128 last->next = stack;
129 stack = ent;
130 }
131 StackStripMain(stack);
132 return stack;
133 }
134
ScopedReport(ReportType typ)135 ScopedReport::ScopedReport(ReportType typ) {
136 ctx_ = CTX();
137 ctx_->thread_registry->CheckLocked();
138 void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
139 rep_ = new(mem) ReportDesc;
140 rep_->typ = typ;
141 ctx_->report_mtx.Lock();
142 CommonSanitizerReportMutex.Lock();
143 }
144
~ScopedReport()145 ScopedReport::~ScopedReport() {
146 CommonSanitizerReportMutex.Unlock();
147 ctx_->report_mtx.Unlock();
148 DestroyAndFree(rep_);
149 }
150
AddStack(const StackTrace * stack)151 void ScopedReport::AddStack(const StackTrace *stack) {
152 ReportStack **rs = rep_->stacks.PushBack();
153 *rs = SymbolizeStack(*stack);
154 }
155
AddMemoryAccess(uptr addr,Shadow s,const StackTrace * stack,const MutexSet * mset)156 void ScopedReport::AddMemoryAccess(uptr addr, Shadow s,
157 const StackTrace *stack, 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 for (uptr i = 0; i < mset->Size(); i++) {
168 MutexSet::Desc d = mset->Get(i);
169 u64 uid = 0;
170 uptr addr = SyncVar::SplitId(d.id, &uid);
171 SyncVar *s = ctx_->synctab.GetIfExistsAndLock(addr, false);
172 // Check that the mutex is still alive.
173 // Another mutex can be created at the same address,
174 // so check uid as well.
175 if (s && s->CheckId(uid)) {
176 ReportMopMutex mtx = {s->uid, d.write};
177 mop->mset.PushBack(mtx);
178 AddMutex(s);
179 } else {
180 ReportMopMutex mtx = {d.id, d.write};
181 mop->mset.PushBack(mtx);
182 AddMutex(d.id);
183 }
184 if (s)
185 s->mtx.ReadUnlock();
186 }
187 }
188
AddThread(const ThreadContext * tctx)189 void ScopedReport::AddThread(const ThreadContext *tctx) {
190 for (uptr i = 0; i < rep_->threads.Size(); i++) {
191 if ((u32)rep_->threads[i]->id == tctx->tid)
192 return;
193 }
194 void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
195 ReportThread *rt = new(mem) ReportThread();
196 rep_->threads.PushBack(rt);
197 rt->id = tctx->tid;
198 rt->pid = tctx->os_id;
199 rt->running = (tctx->status == ThreadStatusRunning);
200 rt->name = tctx->name ? internal_strdup(tctx->name) : 0;
201 rt->parent_tid = tctx->parent_tid;
202 rt->stack = 0;
203 #ifdef TSAN_GO
204 rt->stack = SymbolizeStack(tctx->creation_stack);
205 #else
206 uptr ssz = 0;
207 const uptr *stack = StackDepotGet(tctx->creation_stack_id, &ssz);
208 if (stack) {
209 StackTrace trace;
210 trace.Init(stack, ssz);
211 rt->stack = SymbolizeStack(trace);
212 }
213 #endif
214 }
215
216 #ifndef TSAN_GO
FindThreadByUidLocked(int unique_id)217 static ThreadContext *FindThreadByUidLocked(int unique_id) {
218 Context *ctx = CTX();
219 ctx->thread_registry->CheckLocked();
220 for (unsigned i = 0; i < kMaxTid; i++) {
221 ThreadContext *tctx = static_cast<ThreadContext*>(
222 ctx->thread_registry->GetThreadLocked(i));
223 if (tctx && tctx->unique_id == (u32)unique_id) {
224 return tctx;
225 }
226 }
227 return 0;
228 }
229
FindThreadByTidLocked(int tid)230 static ThreadContext *FindThreadByTidLocked(int tid) {
231 Context *ctx = CTX();
232 ctx->thread_registry->CheckLocked();
233 return static_cast<ThreadContext*>(
234 ctx->thread_registry->GetThreadLocked(tid));
235 }
236
IsInStackOrTls(ThreadContextBase * tctx_base,void * arg)237 static bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {
238 uptr addr = (uptr)arg;
239 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
240 if (tctx->status != ThreadStatusRunning)
241 return false;
242 ThreadState *thr = tctx->thr;
243 CHECK(thr);
244 return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||
245 (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));
246 }
247
IsThreadStackOrTls(uptr addr,bool * is_stack)248 ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
249 Context *ctx = CTX();
250 ctx->thread_registry->CheckLocked();
251 ThreadContext *tctx = static_cast<ThreadContext*>(
252 ctx->thread_registry->FindThreadContextLocked(IsInStackOrTls,
253 (void*)addr));
254 if (!tctx)
255 return 0;
256 ThreadState *thr = tctx->thr;
257 CHECK(thr);
258 *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
259 return tctx;
260 }
261 #endif
262
AddMutex(const SyncVar * s)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;
267 }
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->destroyed = false;
273 rm->stack = 0;
274 #ifndef TSAN_GO
275 uptr ssz = 0;
276 const uptr *stack = StackDepotGet(s->creation_stack_id, &ssz);
277 if (stack) {
278 StackTrace trace;
279 trace.Init(stack, ssz);
280 rm->stack = SymbolizeStack(trace);
281 }
282 #endif
283 }
284
AddMutex(u64 id)285 void ScopedReport::AddMutex(u64 id) {
286 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
287 if (rep_->mutexes[i]->id == id)
288 return;
289 }
290 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
291 ReportMutex *rm = new(mem) ReportMutex();
292 rep_->mutexes.PushBack(rm);
293 rm->id = id;
294 rm->destroyed = true;
295 rm->stack = 0;
296 }
297
AddLocation(uptr addr,uptr size)298 void ScopedReport::AddLocation(uptr addr, uptr size) {
299 if (addr == 0)
300 return;
301 #ifndef TSAN_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 || FdLocation(AlternativeAddress(addr), &fd, &creat_tid, &creat_stack)) {
307 void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
308 ReportLocation *loc = new(mem) ReportLocation();
309 rep_->locs.PushBack(loc);
310 loc->type = ReportLocationFD;
311 loc->fd = fd;
312 loc->tid = creat_tid;
313 uptr ssz = 0;
314 const uptr *stack = StackDepotGet(creat_stack, &ssz);
315 if (stack) {
316 StackTrace trace;
317 trace.Init(stack, ssz);
318 loc->stack = SymbolizeStack(trace);
319 }
320 ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
321 if (tctx)
322 AddThread(tctx);
323 return;
324 }
325 MBlock *b = 0;
326 if (allocator()->PointerIsMine((void*)addr)
327 && (b = user_mblock(0, (void*)addr))) {
328 ThreadContext *tctx = FindThreadByTidLocked(b->Tid());
329 void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
330 ReportLocation *loc = new(mem) ReportLocation();
331 rep_->locs.PushBack(loc);
332 loc->type = ReportLocationHeap;
333 loc->addr = (uptr)allocator()->GetBlockBegin((void*)addr);
334 loc->size = b->Size();
335 loc->tid = tctx ? tctx->tid : b->Tid();
336 loc->name = 0;
337 loc->file = 0;
338 loc->line = 0;
339 loc->stack = 0;
340 uptr ssz = 0;
341 const uptr *stack = StackDepotGet(b->StackId(), &ssz);
342 if (stack) {
343 StackTrace trace;
344 trace.Init(stack, ssz);
345 loc->stack = SymbolizeStack(trace);
346 }
347 if (tctx)
348 AddThread(tctx);
349 return;
350 }
351 bool is_stack = false;
352 if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
353 void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
354 ReportLocation *loc = new(mem) ReportLocation();
355 rep_->locs.PushBack(loc);
356 loc->type = is_stack ? ReportLocationStack : ReportLocationTLS;
357 loc->tid = tctx->tid;
358 AddThread(tctx);
359 }
360 ReportLocation *loc = SymbolizeData(addr);
361 if (loc) {
362 rep_->locs.PushBack(loc);
363 return;
364 }
365 #endif
366 }
367
368 #ifndef TSAN_GO
AddSleep(u32 stack_id)369 void ScopedReport::AddSleep(u32 stack_id) {
370 uptr ssz = 0;
371 const uptr *stack = StackDepotGet(stack_id, &ssz);
372 if (stack) {
373 StackTrace trace;
374 trace.Init(stack, ssz);
375 rep_->sleep = SymbolizeStack(trace);
376 }
377 }
378 #endif
379
SetCount(int count)380 void ScopedReport::SetCount(int count) {
381 rep_->count = count;
382 }
383
GetReport() const384 const ReportDesc *ScopedReport::GetReport() const {
385 return rep_;
386 }
387
RestoreStack(int tid,const u64 epoch,StackTrace * stk,MutexSet * mset)388 void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset) {
389 // This function restores stack trace and mutex set for the thread/epoch.
390 // It does so by getting stack trace and mutex set at the beginning of
391 // trace part, and then replaying the trace till the given epoch.
392 Context *ctx = CTX();
393 ctx->thread_registry->CheckLocked();
394 ThreadContext *tctx = static_cast<ThreadContext*>(
395 ctx->thread_registry->GetThreadLocked(tid));
396 if (tctx == 0)
397 return;
398 if (tctx->status != ThreadStatusRunning
399 && tctx->status != ThreadStatusFinished
400 && tctx->status != ThreadStatusDead)
401 return;
402 Trace* trace = ThreadTrace(tctx->tid);
403 Lock l(&trace->mtx);
404 const int partidx = (epoch / kTracePartSize) % TraceParts();
405 TraceHeader* hdr = &trace->headers[partidx];
406 if (epoch < hdr->epoch0)
407 return;
408 const u64 epoch0 = RoundDown(epoch, TraceSize());
409 const u64 eend = epoch % TraceSize();
410 const u64 ebegin = RoundDown(eend, kTracePartSize);
411 DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
412 tid, (uptr)epoch, (uptr)ebegin, (uptr)eend, partidx);
413 InternalScopedBuffer<uptr> stack(1024); // FIXME: de-hardcode 1024
414 for (uptr i = 0; i < hdr->stack0.Size(); i++) {
415 stack[i] = hdr->stack0.Get(i);
416 DPrintf2(" #%02lu: pc=%zx\n", i, stack[i]);
417 }
418 if (mset)
419 *mset = hdr->mset0;
420 uptr pos = hdr->stack0.Size();
421 Event *events = (Event*)GetThreadTrace(tid);
422 for (uptr i = ebegin; i <= eend; i++) {
423 Event ev = events[i];
424 EventType typ = (EventType)(ev >> 61);
425 uptr pc = (uptr)(ev & ((1ull << 61) - 1));
426 DPrintf2(" %zu typ=%d pc=%zx\n", i, typ, pc);
427 if (typ == EventTypeMop) {
428 stack[pos] = pc;
429 } else if (typ == EventTypeFuncEnter) {
430 stack[pos++] = pc;
431 } else if (typ == EventTypeFuncExit) {
432 if (pos > 0)
433 pos--;
434 }
435 if (mset) {
436 if (typ == EventTypeLock) {
437 mset->Add(pc, true, epoch0 + i);
438 } else if (typ == EventTypeUnlock) {
439 mset->Del(pc, true);
440 } else if (typ == EventTypeRLock) {
441 mset->Add(pc, false, epoch0 + i);
442 } else if (typ == EventTypeRUnlock) {
443 mset->Del(pc, false);
444 }
445 }
446 for (uptr j = 0; j <= pos; j++)
447 DPrintf2(" #%zu: %zx\n", j, stack[j]);
448 }
449 if (pos == 0 && stack[0] == 0)
450 return;
451 pos++;
452 stk->Init(stack.data(), pos);
453 }
454
HandleRacyStacks(ThreadState * thr,const StackTrace (& traces)[2],uptr addr_min,uptr addr_max)455 static bool HandleRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
456 uptr addr_min, uptr addr_max) {
457 Context *ctx = CTX();
458 bool equal_stack = false;
459 RacyStacks hash;
460 if (flags()->suppress_equal_stacks) {
461 hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
462 hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
463 for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {
464 if (hash == ctx->racy_stacks[i]) {
465 DPrintf("ThreadSanitizer: suppressing report as doubled (stack)\n");
466 equal_stack = true;
467 break;
468 }
469 }
470 }
471 bool equal_address = false;
472 RacyAddress ra0 = {addr_min, addr_max};
473 if (flags()->suppress_equal_addresses) {
474 for (uptr i = 0; i < ctx->racy_addresses.Size(); i++) {
475 RacyAddress ra2 = ctx->racy_addresses[i];
476 uptr maxbeg = max(ra0.addr_min, ra2.addr_min);
477 uptr minend = min(ra0.addr_max, ra2.addr_max);
478 if (maxbeg < minend) {
479 DPrintf("ThreadSanitizer: suppressing report as doubled (addr)\n");
480 equal_address = true;
481 break;
482 }
483 }
484 }
485 if (equal_stack || equal_address) {
486 if (!equal_stack)
487 ctx->racy_stacks.PushBack(hash);
488 if (!equal_address)
489 ctx->racy_addresses.PushBack(ra0);
490 return true;
491 }
492 return false;
493 }
494
AddRacyStacks(ThreadState * thr,const StackTrace (& traces)[2],uptr addr_min,uptr addr_max)495 static void AddRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
496 uptr addr_min, uptr addr_max) {
497 Context *ctx = CTX();
498 if (flags()->suppress_equal_stacks) {
499 RacyStacks hash;
500 hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
501 hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
502 ctx->racy_stacks.PushBack(hash);
503 }
504 if (flags()->suppress_equal_addresses) {
505 RacyAddress ra0 = {addr_min, addr_max};
506 ctx->racy_addresses.PushBack(ra0);
507 }
508 }
509
OutputReport(Context * ctx,const ScopedReport & srep,const ReportStack * suppress_stack1,const ReportStack * suppress_stack2,const ReportLocation * suppress_loc)510 bool OutputReport(Context *ctx,
511 const ScopedReport &srep,
512 const ReportStack *suppress_stack1,
513 const ReportStack *suppress_stack2,
514 const ReportLocation *suppress_loc) {
515 atomic_store(&ctx->last_symbolize_time_ns, NanoTime(), memory_order_relaxed);
516 const ReportDesc *rep = srep.GetReport();
517 Suppression *supp = 0;
518 uptr suppress_pc = IsSuppressed(rep->typ, suppress_stack1, &supp);
519 if (suppress_pc == 0)
520 suppress_pc = IsSuppressed(rep->typ, suppress_stack2, &supp);
521 if (suppress_pc == 0)
522 suppress_pc = IsSuppressed(rep->typ, suppress_loc, &supp);
523 if (suppress_pc != 0) {
524 FiredSuppression s = {srep.GetReport()->typ, suppress_pc, supp};
525 ctx->fired_suppressions.push_back(s);
526 }
527 if (OnReport(rep, suppress_pc != 0))
528 return false;
529 PrintReport(rep);
530 CTX()->nreported++;
531 return true;
532 }
533
IsFiredSuppression(Context * ctx,const ScopedReport & srep,const StackTrace & trace)534 bool IsFiredSuppression(Context *ctx,
535 const ScopedReport &srep,
536 const StackTrace &trace) {
537 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
538 if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
539 continue;
540 for (uptr j = 0; j < trace.Size(); j++) {
541 FiredSuppression *s = &ctx->fired_suppressions[k];
542 if (trace.Get(j) == s->pc) {
543 if (s->supp)
544 s->supp->hit_count++;
545 return true;
546 }
547 }
548 }
549 return false;
550 }
551
IsFiredSuppression(Context * ctx,const ScopedReport & srep,uptr addr)552 static bool IsFiredSuppression(Context *ctx,
553 const ScopedReport &srep,
554 uptr addr) {
555 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
556 if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
557 continue;
558 FiredSuppression *s = &ctx->fired_suppressions[k];
559 if (addr == s->pc) {
560 if (s->supp)
561 s->supp->hit_count++;
562 return true;
563 }
564 }
565 return false;
566 }
567
FrameIsInternal(const ReportStack * frame)568 bool FrameIsInternal(const ReportStack *frame) {
569 return frame != 0 && frame->file != 0
570 && (internal_strstr(frame->file, "tsan_interceptors.cc") ||
571 internal_strstr(frame->file, "sanitizer_common_interceptors.inc") ||
572 internal_strstr(frame->file, "tsan_interface_"));
573 }
574
575 // On programs that use Java we see weird reports like:
576 // WARNING: ThreadSanitizer: data race (pid=22512)
577 // Read of size 8 at 0x7d2b00084318 by thread 100:
578 // #0 memcpy tsan_interceptors.cc:406 (foo+0x00000d8dfae3)
579 // #1 <null> <null>:0 (0x7f7ad9b40193)
580 // Previous write of size 8 at 0x7d2b00084318 by thread 105:
581 // #0 strncpy tsan_interceptors.cc:501 (foo+0x00000d8e0919)
582 // #1 <null> <null>:0 (0x7f7ad9b42707)
IsJavaNonsense(const ReportDesc * rep)583 static bool IsJavaNonsense(const ReportDesc *rep) {
584 #ifndef TSAN_GO
585 for (uptr i = 0; i < rep->mops.Size(); i++) {
586 ReportMop *mop = rep->mops[i];
587 ReportStack *frame = mop->stack;
588 if (frame == 0
589 || (frame->func == 0 && frame->file == 0 && frame->line == 0
590 && frame->module == 0)) {
591 return true;
592 }
593 if (FrameIsInternal(frame)) {
594 frame = frame->next;
595 if (frame == 0
596 || (frame->func == 0 && frame->file == 0 && frame->line == 0
597 && frame->module == 0)) {
598 if (frame) {
599 FiredSuppression supp = {rep->typ, frame->pc, 0};
600 CTX()->fired_suppressions.push_back(supp);
601 }
602 return true;
603 }
604 }
605 }
606 #endif
607 return false;
608 }
609
RaceBetweenAtomicAndFree(ThreadState * thr)610 static bool RaceBetweenAtomicAndFree(ThreadState *thr) {
611 Shadow s0(thr->racy_state[0]);
612 Shadow s1(thr->racy_state[1]);
613 CHECK(!(s0.IsAtomic() && s1.IsAtomic()));
614 if (!s0.IsAtomic() && !s1.IsAtomic())
615 return true;
616 if (s0.IsAtomic() && s1.IsFreed())
617 return true;
618 if (s1.IsAtomic() && thr->is_freeing)
619 return true;
620 return false;
621 }
622
ReportRace(ThreadState * thr)623 void ReportRace(ThreadState *thr) {
624 if (!flags()->report_bugs)
625 return;
626 ScopedInRtl in_rtl;
627
628 if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
629 return;
630
631 bool freed = false;
632 {
633 Shadow s(thr->racy_state[1]);
634 freed = s.GetFreedAndReset();
635 thr->racy_state[1] = s.raw();
636 }
637
638 uptr addr = ShadowToMem((uptr)thr->racy_shadow_addr);
639 uptr addr_min = 0;
640 uptr addr_max = 0;
641 {
642 uptr a0 = addr + Shadow(thr->racy_state[0]).addr0();
643 uptr a1 = addr + Shadow(thr->racy_state[1]).addr0();
644 uptr e0 = a0 + Shadow(thr->racy_state[0]).size();
645 uptr e1 = a1 + Shadow(thr->racy_state[1]).size();
646 addr_min = min(a0, a1);
647 addr_max = max(e0, e1);
648 if (IsExpectedReport(addr_min, addr_max - addr_min))
649 return;
650 }
651
652 Context *ctx = CTX();
653 ThreadRegistryLock l0(ctx->thread_registry);
654
655 ReportType typ = ReportTypeRace;
656 if (thr->is_vptr_access)
657 typ = ReportTypeVptrRace;
658 else if (freed)
659 typ = ReportTypeUseAfterFree;
660 ScopedReport rep(typ);
661 if (IsFiredSuppression(ctx, rep, addr))
662 return;
663 const uptr kMop = 2;
664 StackTrace traces[kMop];
665 const uptr toppc = TraceTopPC(thr);
666 traces[0].ObtainCurrent(thr, toppc);
667 if (IsFiredSuppression(ctx, rep, traces[0]))
668 return;
669 InternalScopedBuffer<MutexSet> mset2(1);
670 new(mset2.data()) MutexSet();
671 Shadow s2(thr->racy_state[1]);
672 RestoreStack(s2.tid(), s2.epoch(), &traces[1], mset2.data());
673 if (IsFiredSuppression(ctx, rep, traces[1]))
674 return;
675
676 if (HandleRacyStacks(thr, traces, addr_min, addr_max))
677 return;
678
679 for (uptr i = 0; i < kMop; i++) {
680 Shadow s(thr->racy_state[i]);
681 rep.AddMemoryAccess(addr, s, &traces[i],
682 i == 0 ? &thr->mset : mset2.data());
683 }
684
685 if (flags()->suppress_java && IsJavaNonsense(rep.GetReport()))
686 return;
687
688 for (uptr i = 0; i < kMop; i++) {
689 FastState s(thr->racy_state[i]);
690 ThreadContext *tctx = static_cast<ThreadContext*>(
691 ctx->thread_registry->GetThreadLocked(s.tid()));
692 if (s.epoch() < tctx->epoch0 || s.epoch() > tctx->epoch1)
693 continue;
694 rep.AddThread(tctx);
695 }
696
697 rep.AddLocation(addr_min, addr_max - addr_min);
698
699 #ifndef TSAN_GO
700 { // NOLINT
701 Shadow s(thr->racy_state[1]);
702 if (s.epoch() <= thr->last_sleep_clock.get(s.tid()))
703 rep.AddSleep(thr->last_sleep_stack_id);
704 }
705 #endif
706
707 ReportLocation *suppress_loc = rep.GetReport()->locs.Size() ?
708 rep.GetReport()->locs[0] : 0;
709 if (!OutputReport(ctx, rep, rep.GetReport()->mops[0]->stack,
710 rep.GetReport()->mops[1]->stack,
711 suppress_loc))
712 return;
713
714 AddRacyStacks(thr, traces, addr_min, addr_max);
715 }
716
PrintCurrentStack(ThreadState * thr,uptr pc)717 void PrintCurrentStack(ThreadState *thr, uptr pc) {
718 StackTrace trace;
719 trace.ObtainCurrent(thr, pc);
720 PrintStack(SymbolizeStack(trace));
721 }
722
PrintCurrentStackSlow()723 void PrintCurrentStackSlow() {
724 #ifndef TSAN_GO
725 __sanitizer::StackTrace *ptrace = new(internal_alloc(MBlockStackTrace,
726 sizeof(__sanitizer::StackTrace))) __sanitizer::StackTrace;
727 ptrace->SlowUnwindStack(__sanitizer::StackTrace::GetCurrentPc(),
728 kStackTraceMax);
729 for (uptr i = 0; i < ptrace->size / 2; i++) {
730 uptr tmp = ptrace->trace[i];
731 ptrace->trace[i] = ptrace->trace[ptrace->size - i - 1];
732 ptrace->trace[ptrace->size - i - 1] = tmp;
733 }
734 StackTrace trace;
735 trace.Init(ptrace->trace, ptrace->size);
736 PrintStack(SymbolizeStack(trace));
737 #endif
738 }
739
740 } // namespace __tsan
741