tsan_rtl_report.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===-- tsan_rtl_report.cc ------------------------------------------------===//
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// License. See LICENSE.TXT for details.
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//===----------------------------------------------------------------------===//
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file is a part of ThreadSanitizer (TSan), a race detector.
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//===----------------------------------------------------------------------===//
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sanitizer_common/sanitizer_libc.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "sanitizer_common/sanitizer_placement_new.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "sanitizer_common/sanitizer_stackdepot.h"
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "sanitizer_common/sanitizer_common.h"
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "sanitizer_common/sanitizer_stacktrace.h"
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "tsan_platform.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_rtl.h"
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_suppressions.h"
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_symbolize.h"
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_report.h"
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_sync.h"
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_mman.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_flags.h"
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "tsan_fd.h"
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace __tsan {
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using namespace __sanitizer;  // NOLINT
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static ReportStack *SymbolizeStack(const StackTrace& trace);
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void TsanCheckFailed(const char *file, int line, const char *cond,
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                     u64 v1, u64 v2) {
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // There is high probability that interceptors will check-fail as well,
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // on the other hand there is no sense in processing interceptors
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // since we are going to die soon.
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ScopedIgnoreInterceptors ignore;
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Printf("FATAL: ThreadSanitizer CHECK failed: "
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         "%s:%d \"%s\" (0x%zx, 0x%zx)\n",
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         file, line, cond, (uptr)v1, (uptr)v2);
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  PrintCurrentStackSlow();
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Die();
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Can be overriden by an application/test to intercept reports.
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifdef TSAN_EXTERNAL_HOOKS
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool OnReport(const ReportDesc *rep, bool suppressed);
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#else
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)SANITIZER_INTERFACE_ATTRIBUTE
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WEAK OnReport(const ReportDesc *rep, bool suppressed) {
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  (void)rep;
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return suppressed;
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static void StackStripMain(ReportStack *stack) {
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ReportStack *last_frame = 0;
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ReportStack *last_frame2 = 0;
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const char *prefix = "__interceptor_";
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  uptr prefix_len = internal_strlen(prefix);
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const char *path_prefix = flags()->strip_path_prefix;
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  uptr path_prefix_len = internal_strlen(path_prefix);
660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  char *pos;
670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  for (ReportStack *ent = stack; ent; ent = ent->next) {
680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (ent->func && 0 == internal_strncmp(ent->func, prefix, prefix_len))
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ent->func += prefix_len;
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (ent->file && (pos = internal_strstr(ent->file, path_prefix)))
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ent->file = pos + path_prefix_len;
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (ent->file && ent->file[0] == '.' && ent->file[1] == '/')
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ent->file += 2;
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    last_frame2 = last_frame;
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    last_frame = ent;
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (last_frame2 == 0)
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const char *last = last_frame->func;
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef TSAN_GO
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const char *last2 = last_frame2->func;
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Strip frame above 'main'
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (last2 && 0 == internal_strcmp(last2, "main")) {
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    last_frame2->next = 0;
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Strip our internal thread start routine.
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } else if (last && 0 == internal_strcmp(last, "__tsan_thread_start_func")) {
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    last_frame2->next = 0;
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Strip global ctors init.
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  } else if (last && 0 == internal_strcmp(last, "__do_global_ctors_aux")) {
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    last_frame2->next = 0;
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If both are 0, then we probably just failed to symbolize.
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (last || last2) {
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // Ensure that we recovered stack completely. Trimmed stack
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // can actually happen if we do not instrument some code,
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // so it's only a debug print. However we must try hard to not miss it
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // due to our fault.
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    DPrintf("Bottom stack frame of stack %zx is missed\n", stack->pc);
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // The last frame always point into runtime (gosched0, goexit0, runtime.main).
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  last_frame2->next = 0;
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  (void)last;
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ReportStack *SymbolizeStackId(u32 stack_id) {
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (stack_id == 0)
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return 0;
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  uptr ssz = 0;
111f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const uptr *stack = StackDepotGet(stack_id, &ssz);
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (stack == 0)
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return 0;
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  StackTrace trace;
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  trace.Init(stack, ssz);
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return SymbolizeStack(trace);
1170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)static ReportStack *SymbolizeStack(const StackTrace& trace) {
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (trace.IsEmpty())
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return 0;
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ReportStack *stack = 0;
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (uptr si = 0; si < trace.Size(); si++) {
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const uptr pc = trace.Get(si);
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef TSAN_GO
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // We obtain the return address, that is, address of the next instruction,
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // so offset it by 1 byte.
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const uptr pc1 = __sanitizer::StackTrace::GetPreviousInstructionPc(pc);
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // FIXME(dvyukov): Go sometimes uses address of a function as top pc.
13146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    uptr pc1 = pc;
13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (si != trace.Size() - 1)
13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      pc1 -= 1;
13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ReportStack *ent = SymbolizeCode(pc1);
13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    CHECK_NE(ent, 0);
13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ReportStack *last = ent;
13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    while (last->next) {
13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      last->pc = pc;  // restore original pc for report
14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      last = last->next;
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    last->pc = pc;  // restore original pc for report
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    last->next = stack;
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    stack = ent;
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  StackStripMain(stack);
14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return stack;
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ScopedReport::ScopedReport(ReportType typ) {
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ctx->thread_registry->CheckLocked();
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  rep_ = new(mem) ReportDesc;
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  rep_->typ = typ;
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ctx->report_mtx.Lock();
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CommonSanitizerReportMutex.Lock();
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ScopedReport::~ScopedReport() {
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CommonSanitizerReportMutex.Unlock();
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ctx->report_mtx.Unlock();
16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DestroyAndFree(rep_);
16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ScopedReport::AddStack(const StackTrace *stack) {
16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ReportStack **rs = rep_->stacks.PushBack();
16790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  *rs = SymbolizeStack(*stack);
16890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ScopedReport::AddMemoryAccess(uptr addr, Shadow s,
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const StackTrace *stack, const MutexSet *mset) {
17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void *mem = internal_alloc(MBlockReportMop, sizeof(ReportMop));
17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ReportMop *mop = new(mem) ReportMop;
17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  rep_->mops.PushBack(mop);
17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  mop->tid = s.tid();
17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  mop->addr = addr + s.addr0();
17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  mop->size = s.size();
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  mop->write = s.IsWrite();
1790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  mop->atomic = s.IsAtomic();
1800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  mop->stack = SymbolizeStack(*stack);
1810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (uptr i = 0; i < mset->Size(); i++) {
18290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    MutexSet::Desc d = mset->Get(i);
18390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    u64 mid = this->AddMutex(d.id);
18490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ReportMopMutex mtx = {mid, d.write};
18590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    mop->mset.PushBack(mtx);
18690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
1870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
18890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ScopedReport::AddUniqueTid(int unique_tid) {
190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  rep_->unique_tids.PushBack(unique_tid);
191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid ScopedReport::AddThread(const ThreadContext *tctx) {
1940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (uptr i = 0; i < rep_->threads.Size(); i++) {
19546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if ((u32)rep_->threads[i]->id == tctx->tid)
1960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
1970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
1980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
1990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ReportThread *rt = new(mem) ReportThread();
2000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rep_->threads.PushBack(rt);
2010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->id = tctx->tid;
2020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->pid = tctx->os_id;
2030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->running = (tctx->status == ThreadStatusRunning);
2040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->name = internal_strdup(tctx->name);
2050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->parent_tid = tctx->parent_tid;
2060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->stack = 0;
2070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rt->stack = SymbolizeStackId(tctx->creation_stack_id);
2080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#ifndef TSAN_GO
2110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochstatic ThreadContext *FindThreadByUidLocked(int unique_id) {
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ctx->thread_registry->CheckLocked();
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (unsigned i = 0; i < kMaxTid; i++) {
21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ThreadContext *tctx = static_cast<ThreadContext*>(
21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        ctx->thread_registry->GetThreadLocked(i));
21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (tctx && tctx->unique_id == (u32)unique_id) {
21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return tctx;
2180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
2190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
2200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return 0;
2210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochstatic ThreadContext *FindThreadByTidLocked(int tid) {
2240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ctx->thread_registry->CheckLocked();
2250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return static_cast<ThreadContext*>(
2260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      ctx->thread_registry->GetThreadLocked(tid));
2270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochstatic bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {
230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  uptr addr = (uptr)arg;
231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
2320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (tctx->status != ThreadStatusRunning)
2330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return false;
2340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ThreadState *thr = tctx->thr;
2350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  CHECK(thr);
2360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||
2370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));
2380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2400529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
2410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ctx->thread_registry->CheckLocked();
2420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ThreadContext *tctx = static_cast<ThreadContext*>(
2430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      ctx->thread_registry->FindThreadContextLocked(IsInStackOrTls,
2440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                    (void*)addr));
2450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!tctx)
246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return 0;
2470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ThreadState *thr = tctx->thr;
2480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  CHECK(thr);
2490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
2500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return tctx;
2510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#endif
2530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid ScopedReport::AddThread(int unique_tid) {
2550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#ifndef TSAN_GO
2560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  AddThread(FindThreadByUidLocked(unique_tid));
2570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#endif
2580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid ScopedReport::AddMutex(const SyncVar *s) {
2610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
2620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (rep_->mutexes[i]->id == s->uid)
2630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
2660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ReportMutex *rm = new(mem) ReportMutex();
2670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rep_->mutexes.PushBack(rm);
2680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rm->id = s->uid;
26946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  rm->addr = s->addr;
27046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  rm->destroyed = false;
2710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rm->stack = SymbolizeStackId(s->creation_stack_id);
27246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
2730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochu64 ScopedReport::AddMutex(u64 id) {
2750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  u64 uid = 0;
2760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  u64 mid = id;
2770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  uptr addr = SyncVar::SplitId(id, &uid);
2780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  SyncVar *s = ctx->synctab.GetIfExistsAndLock(addr, false);
2790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Check that the mutex is still alive.
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Another mutex can be created at the same address,
2810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // so check uid as well.
2820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (s && s->CheckId(uid)) {
283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    mid = s->uid;
2840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    AddMutex(s);
2850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  } else {
2860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    AddDeadMutex(id);
2870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
2880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (s)
2890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    s->mtx.ReadUnlock();
290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return mid;
2910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
2920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
2930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid ScopedReport::AddDeadMutex(u64 id) {
2940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (rep_->mutexes[i]->id == id)
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return;
2970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
298f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
299f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ReportMutex *rm = new(mem) ReportMutex();
3000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rep_->mutexes.PushBack(rm);
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  rm->id = id;
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  rm->addr = 0;
3030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rm->destroyed = true;
3040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rm->stack = 0;
3050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void ScopedReport::AddLocation(uptr addr, uptr size) {
3080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (addr == 0)
3090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef TSAN_GO
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int fd = -1;
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  int creat_tid = -1;
3130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  u32 creat_stack = 0;
314f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (FdLocation(addr, &fd, &creat_tid, &creat_stack)
3150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      || FdLocation(AlternativeAddress(addr), &fd, &creat_tid, &creat_stack)) {
3160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
3170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ReportLocation *loc = new(mem) ReportLocation();
3180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    rep_->locs.PushBack(loc);
3190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->type = ReportLocationFD;
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->fd = fd;
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->tid = creat_tid;
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->stack = SymbolizeStackId(creat_stack);
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
3240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (tctx)
3250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      AddThread(tctx);
326a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
327a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
328a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MBlock *b = 0;
3290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (allocator()->PointerIsMine((void*)addr)
33046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      && (b = user_mblock(0, (void*)addr))) {
3310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ThreadContext *tctx = FindThreadByTidLocked(b->Tid());
332a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
333a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ReportLocation *loc = new(mem) ReportLocation();
33446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    rep_->locs.PushBack(loc);
3350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->type = ReportLocationHeap;
3360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->addr = (uptr)allocator()->GetBlockBegin((void*)addr);
33746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    loc->size = b->Size();
33846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    loc->tid = tctx ? tctx->tid : b->Tid();
3390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->name = 0;
34046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    loc->file = 0;
3410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->line = 0;
3420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->stack = 0;
3430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    loc->stack = SymbolizeStackId(b->StackId());
3440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (tctx)
3450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      AddThread(tctx);
3460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
3470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
348a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool is_stack = false;
3490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void *mem = internal_alloc(MBlockReportLoc, sizeof(ReportLocation));
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ReportLocation *loc = new(mem) ReportLocation();
35246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    rep_->locs.PushBack(loc);
35346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    loc->type = is_stack ? ReportLocationStack : ReportLocationTLS;
35446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    loc->tid = tctx->tid;
35546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    AddThread(tctx);
35646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
35746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ReportLocation *loc = SymbolizeData(addr);
35846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (loc) {
35946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    rep_->locs.PushBack(loc);
36046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
36146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
36246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
36346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
36446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
36546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#ifndef TSAN_GO
366f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void ScopedReport::AddSleep(u32 stack_id) {
367f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  rep_->sleep = SymbolizeStackId(stack_id);
36846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
36946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
37046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
37146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void ScopedReport::SetCount(int count) {
37246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  rep_->count = count;
37346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
37446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
37546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)const ReportDesc *ScopedReport::GetReport() const {
37646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return rep_;
37746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
37846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
37946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void RestoreStack(int tid, const u64 epoch, StackTrace *stk, MutexSet *mset) {
38046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // This function restores stack trace and mutex set for the thread/epoch.
38146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // It does so by getting stack trace and mutex set at the beginning of
382f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // trace part, and then replaying the trace till the given epoch.
38346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ctx->thread_registry->CheckLocked();
38446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ThreadContext *tctx = static_cast<ThreadContext*>(
38546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      ctx->thread_registry->GetThreadLocked(tid));
38646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (tctx == 0)
38746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
38846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (tctx->status != ThreadStatusRunning
38946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      && tctx->status != ThreadStatusFinished
39046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      && tctx->status != ThreadStatusDead)
39146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
39246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  Trace* trace = ThreadTrace(tctx->tid);
39346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  Lock l(&trace->mtx);
39446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const int partidx = (epoch / kTracePartSize) % TraceParts();
39546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TraceHeader* hdr = &trace->headers[partidx];
39646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (epoch < hdr->epoch0)
39746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
39846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const u64 epoch0 = RoundDown(epoch, TraceSize());
39946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const u64 eend = epoch % TraceSize();
40046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const u64 ebegin = RoundDown(eend, kTracePartSize);
40146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
40246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          tid, (uptr)epoch, (uptr)ebegin, (uptr)eend, partidx);
40346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  InternalScopedBuffer<uptr> stack(kShadowStackSize);
40446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (uptr i = 0; i < hdr->stack0.Size(); i++) {
40546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    stack[i] = hdr->stack0.Get(i);
40646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    DPrintf2("  #%02lu: pc=%zx\n", i, stack[i]);
40746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
40846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (mset)
40946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    *mset = hdr->mset0;
41046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  uptr pos = hdr->stack0.Size();
41146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  Event *events = (Event*)GetThreadTrace(tid);
41246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (uptr i = ebegin; i <= eend; i++) {
41346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Event ev = events[i];
414116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    EventType typ = (EventType)(ev >> 61);
41546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    uptr pc = (uptr)(ev & ((1ull << 61) - 1));
41646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    DPrintf2("  %zu typ=%d pc=%zx\n", i, typ, pc);
41746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (typ == EventTypeMop) {
41846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      stack[pos] = pc;
41946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    } else if (typ == EventTypeFuncEnter) {
42046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      stack[pos++] = pc;
42146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    } else if (typ == EventTypeFuncExit) {
42246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      if (pos > 0)
42346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        pos--;
42446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
42546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (mset) {
42646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      if (typ == EventTypeLock) {
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        mset->Add(pc, true, epoch0 + i);
42846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      } else if (typ == EventTypeUnlock) {
429116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        mset->Del(pc, true);
430e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      } else if (typ == EventTypeRLock) {
431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        mset->Add(pc, false, epoch0 + i);
432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      } else if (typ == EventTypeRUnlock) {
433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        mset->Del(pc, false);
434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      }
4350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
436a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for (uptr j = 0; j <= pos; j++)
4375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      DPrintf2("      #%zu: %zx\n", j, stack[j]);
4385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
4396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (pos == 0 && stack[0] == 0)
4406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return;
4416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  pos++;
4426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  stk->Init(stack.data(), pos);
4436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
4446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
4456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)static bool HandleRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
4466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    uptr addr_min, uptr addr_max) {
44790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool equal_stack = false;
44890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  RacyStacks hash;
44990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (flags()->suppress_equal_stacks) {
45090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
45146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
45246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {
45346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      if (hash == ctx->racy_stacks[i]) {
45446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        DPrintf("ThreadSanitizer: suppressing report as doubled (stack)\n");
45546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        equal_stack = true;
45646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        break;
45746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      }
45846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
45990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
46090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool equal_address = false;
461116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  RacyAddress ra0 = {addr_min, addr_max};
462116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (flags()->suppress_equal_addresses) {
463116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    for (uptr i = 0; i < ctx->racy_addresses.Size(); i++) {
464116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      RacyAddress ra2 = ctx->racy_addresses[i];
465116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      uptr maxbeg = max(ra0.addr_min, ra2.addr_min);
466116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      uptr minend = min(ra0.addr_max, ra2.addr_max);
467116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (maxbeg < minend) {
468116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        DPrintf("ThreadSanitizer: suppressing report as doubled (addr)\n");
469116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        equal_address = true;
470116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        break;
471116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
47246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
47346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
474116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (equal_stack || equal_address) {
475116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!equal_stack)
47646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      ctx->racy_stacks.PushBack(hash);
47746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (!equal_address)
478116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ctx->racy_addresses.PushBack(ra0);
479116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return true;
48090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
48190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return false;
4820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
4830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
484e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochstatic void AddRacyStacks(ThreadState *thr, const StackTrace (&traces)[2],
485e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    uptr addr_min, uptr addr_max) {
4860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (flags()->suppress_equal_stacks) {
4870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    RacyStacks hash;
4880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    hash.hash[0] = md5_hash(traces[0].Begin(), traces[0].Size() * sizeof(uptr));
4890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    hash.hash[1] = md5_hash(traces[1].Begin(), traces[1].Size() * sizeof(uptr));
4900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ctx->racy_stacks.PushBack(hash);
4910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
492e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (flags()->suppress_equal_addresses) {
493e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    RacyAddress ra0 = {addr_min, addr_max};
49490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ctx->racy_addresses.PushBack(ra0);
49590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
49690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
49790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
49890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool OutputReport(Context *ctx,
49990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                  const ScopedReport &srep,
50090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                  const ReportStack *suppress_stack1,
50190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                  const ReportStack *suppress_stack2,
50290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                  const ReportLocation *suppress_loc) {
50390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  atomic_store(&ctx->last_symbolize_time_ns, NanoTime(), memory_order_relaxed);
50490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const ReportDesc *rep = srep.GetReport();
50590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Suppression *supp = 0;
50690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  uptr suppress_pc = IsSuppressed(rep->typ, suppress_stack1, &supp);
50790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (suppress_pc == 0)
50890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    suppress_pc = IsSuppressed(rep->typ, suppress_stack2, &supp);
50990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (suppress_pc == 0)
51090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    suppress_pc = IsSuppressed(rep->typ, suppress_loc, &supp);
51190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (suppress_pc != 0) {
51290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FiredSuppression s = {srep.GetReport()->typ, suppress_pc, supp};
51390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ctx->fired_suppressions.push_back(s);
51490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
51590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (OnReport(rep, suppress_pc != 0))
51690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
51790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PrintReport(rep);
51890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ctx->nreported++;
51990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (flags()->halt_on_error)
52090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    internal__exit(flags()->exitcode);
521868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return true;
522868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
523868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
524868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool IsFiredSuppression(Context *ctx,
525868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const ScopedReport &srep,
526868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const StackTrace &trace) {
527868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
528868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
529868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      continue;
5300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    for (uptr j = 0; j < trace.Size(); j++) {
5310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      FiredSuppression *s = &ctx->fired_suppressions[k];
5320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (trace.Get(j) == s->pc) {
5330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        if (s->supp)
5340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          s->supp->hit_count++;
5350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        return true;
5360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      }
5370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
5380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
5390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return false;
5400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
5410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochstatic bool IsFiredSuppression(Context *ctx,
5430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                               const ScopedReport &srep,
5440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                               uptr addr) {
5450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
5460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (ctx->fired_suppressions[k].type != srep.GetReport()->typ)
5470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      continue;
5480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    FiredSuppression *s = &ctx->fired_suppressions[k];
5490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (addr == s->pc) {
5500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      if (s->supp)
5510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        s->supp->hit_count++;
5520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return true;
5530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
5541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
5551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
5561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
5571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
5581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool FrameIsInternal(const ReportStack *frame) {
5591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return frame != 0 && frame->file != 0
5601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      && (internal_strstr(frame->file, "tsan_interceptors.cc") ||
5611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          internal_strstr(frame->file, "sanitizer_common_interceptors.inc") ||
5620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          internal_strstr(frame->file, "tsan_interface_"));
563f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
564f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
565f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// On programs that use Java we see weird reports like:
566f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// WARNING: ThreadSanitizer: data race (pid=22512)
567f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//   Read of size 8 at 0x7d2b00084318 by thread 100:
568f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//     #0 memcpy tsan_interceptors.cc:406 (foo+0x00000d8dfae3)
5690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch//     #1 <null> <null>:0 (0x7f7ad9b40193)
5700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)//   Previous write of size 8 at 0x7d2b00084318 by thread 105:
5710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)//     #0 strncpy tsan_interceptors.cc:501 (foo+0x00000d8e0919)
572f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//     #1 <null> <null>:0 (0x7f7ad9b42707)
573f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)static bool IsJavaNonsense(const ReportDesc *rep) {
574f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#ifndef TSAN_GO
575f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  for (uptr i = 0; i < rep->mops.Size(); i++) {
5760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ReportMop *mop = rep->mops[i];
577f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    ReportStack *frame = mop->stack;
578a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (frame == 0
579a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        || (frame->func == 0 && frame->file == 0 && frame->line == 0
5800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          && frame->module == 0)) {
5810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return true;
582a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
583a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (FrameIsInternal(frame)) {
5846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      frame = frame->next;
5856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      if (frame == 0
5866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          || (frame->func == 0 && frame->file == 0 && frame->line == 0
5876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          && frame->module == 0)) {
5886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        if (frame) {
5896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          FiredSuppression supp = {rep->typ, frame->pc, 0};
5906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          ctx->fired_suppressions.push_back(supp);
5916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        }
5926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        return true;
5936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      }
5946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    }
5956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
5966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#endif
5976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return false;
5986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
5996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
6006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)static bool RaceBetweenAtomicAndFree(ThreadState *thr) {
6016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  Shadow s0(thr->racy_state[0]);
6026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  Shadow s1(thr->racy_state[1]);
6036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  CHECK(!(s0.IsAtomic() && s1.IsAtomic()));
6046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (!s0.IsAtomic() && !s1.IsAtomic())
6056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return true;
6066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (s0.IsAtomic() && s1.IsFreed())
6076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return true;
6086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (s1.IsAtomic() && thr->is_freeing)
6096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return true;
6106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return false;
6116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
6126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
6136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void ReportRace(ThreadState *thr) {
6146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Symbolizer makes lots of intercepted calls. If we try to process them,
6156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // at best it will cause deadlocks on internal mutexes.
6166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ScopedIgnoreInterceptors ignore;
6176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
6186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (!flags()->report_bugs)
6196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return;
6206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
6216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return;
6226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
6236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool freed = false;
6246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  {
6256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    Shadow s(thr->racy_state[1]);
6266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    freed = s.GetFreedAndReset();
6276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    thr->racy_state[1] = s.raw();
6286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  uptr addr = ShadowToMem((uptr)thr->racy_shadow_addr);
6316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  uptr addr_min = 0;
6326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  uptr addr_max = 0;
6336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  {
6346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    uptr a0 = addr + Shadow(thr->racy_state[0]).addr0();
6356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    uptr a1 = addr + Shadow(thr->racy_state[1]).addr0();
6366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    uptr e0 = a0 + Shadow(thr->racy_state[0]).size();
6376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    uptr e1 = a1 + Shadow(thr->racy_state[1]).size();
6386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    addr_min = min(a0, a1);
6396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    addr_max = max(e0, e1);
6406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (IsExpectedReport(addr_min, addr_max - addr_min))
6416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return;
6426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
64490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ThreadRegistryLock l0(ctx->thread_registry);
6450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
6460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ReportType typ = ReportTypeRace;
6470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (thr->is_vptr_access)
64846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    typ = ReportTypeVptrRace;
64990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  else if (freed)
65090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    typ = ReportTypeUseAfterFree;
6510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ScopedReport rep(typ);
65290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (IsFiredSuppression(ctx, rep, addr))
65390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
65490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const uptr kMop = 2;
65546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  StackTrace traces[kMop];
65646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const uptr toppc = TraceTopPC(thr);
65746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  traces[0].ObtainCurrent(thr, toppc);
6581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (IsFiredSuppression(ctx, rep, traces[0]))
659868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
66090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  InternalScopedBuffer<MutexSet> mset2(1);
66190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  new(mset2.data()) MutexSet();
6621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Shadow s2(thr->racy_state[1]);
6631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RestoreStack(s2.tid(), s2.epoch(), &traces[1], mset2.data());
664868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (IsFiredSuppression(ctx, rep, traces[1]))
665868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
666f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
667f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (HandleRacyStacks(thr, traces, addr_min, addr_max))
6686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return;
6696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
67090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (uptr i = 0; i < kMop; i++) {
67190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Shadow s(thr->racy_state[i]);
67290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    rep.AddMemoryAccess(addr, s, &traces[i],
67346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                        i == 0 ? &thr->mset : mset2.data());
67490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
67590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
67690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (flags()->suppress_java && IsJavaNonsense(rep.GetReport()))
6770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
6780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
679e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  for (uptr i = 0; i < kMop; i++) {
6800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    FastState s(thr->racy_state[i]);
6816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    ThreadContext *tctx = static_cast<ThreadContext*>(
6826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)        ctx->thread_registry->GetThreadLocked(s.tid()));
6836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (s.epoch() < tctx->epoch0 || s.epoch() > tctx->epoch1)
684116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      continue;
68590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    rep.AddThread(tctx);
68690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
6870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rep.AddLocation(addr_min, addr_max - addr_min);
6890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
69090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef TSAN_GO
69190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  {  // NOLINT
69290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Shadow s(thr->racy_state[1]);
69390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (s.epoch() <= thr->last_sleep_clock.get(s.tid()))
69490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      rep.AddSleep(thr->last_sleep_stack_id);
69546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
6960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#endif
69790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
69890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ReportLocation *suppress_loc = rep.GetReport()->locs.Size() ?
6990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                 rep.GetReport()->locs[0] : 0;
7000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!OutputReport(ctx, rep, rep.GetReport()->mops[0]->stack,
70190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                              rep.GetReport()->mops[1]->stack,
70290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                              suppress_loc))
7030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
7040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
70590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddRacyStacks(thr, traces, addr_min, addr_max);
70690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
7070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid PrintCurrentStack(ThreadState *thr, uptr pc) {
70990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  StackTrace trace;
7100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  trace.ObtainCurrent(thr, pc);
7110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  PrintStack(SymbolizeStack(trace));
7120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
7130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void PrintCurrentStackSlow() {
7150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#ifndef TSAN_GO
7160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  __sanitizer::StackTrace *ptrace = new(internal_alloc(MBlockStackTrace,
7170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      sizeof(__sanitizer::StackTrace))) __sanitizer::StackTrace;
718a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ptrace->Unwind(kStackTraceMax, __sanitizer::StackTrace::GetCurrentPc(), 0, 0,
7190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 0, 0, false);
7200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (uptr i = 0; i < ptrace->size / 2; i++) {
721a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    uptr tmp = ptrace->trace[i];
722e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    ptrace->trace[i] = ptrace->trace[ptrace->size - i - 1];
7236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    ptrace->trace[ptrace->size - i - 1] = tmp;
7246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
7256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  StackTrace trace;
7266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  trace.Init(ptrace->trace, ptrace->size);
7276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  PrintStack(SymbolizeStack(trace));
7286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#endif
7296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
7306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
7316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace __tsan
7326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)