annotate libsanitizer/tsan/tsan_rtl_report.cc @ 122:fb3d53c41846

do not expand code segment
author mir3636
date Thu, 22 Mar 2018 17:37:58 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- tsan_rtl_report.cc ------------------------------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 #include "sanitizer_common/sanitizer_libc.h"
kono
parents:
diff changeset
13 #include "sanitizer_common/sanitizer_placement_new.h"
kono
parents:
diff changeset
14 #include "sanitizer_common/sanitizer_stackdepot.h"
kono
parents:
diff changeset
15 #include "sanitizer_common/sanitizer_common.h"
kono
parents:
diff changeset
16 #include "sanitizer_common/sanitizer_stacktrace.h"
kono
parents:
diff changeset
17 #include "tsan_platform.h"
kono
parents:
diff changeset
18 #include "tsan_rtl.h"
kono
parents:
diff changeset
19 #include "tsan_suppressions.h"
kono
parents:
diff changeset
20 #include "tsan_symbolize.h"
kono
parents:
diff changeset
21 #include "tsan_report.h"
kono
parents:
diff changeset
22 #include "tsan_sync.h"
kono
parents:
diff changeset
23 #include "tsan_mman.h"
kono
parents:
diff changeset
24 #include "tsan_flags.h"
kono
parents:
diff changeset
25 #include "tsan_fd.h"
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 namespace __tsan {
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 using namespace __sanitizer; // NOLINT
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 static ReportStack *SymbolizeStack(StackTrace trace);
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 void TsanCheckFailed(const char *file, int line, const char *cond,
kono
parents:
diff changeset
34 u64 v1, u64 v2) {
kono
parents:
diff changeset
35 // There is high probability that interceptors will check-fail as well,
kono
parents:
diff changeset
36 // on the other hand there is no sense in processing interceptors
kono
parents:
diff changeset
37 // since we are going to die soon.
kono
parents:
diff changeset
38 ScopedIgnoreInterceptors ignore;
kono
parents:
diff changeset
39 #if !SANITIZER_GO
kono
parents:
diff changeset
40 cur_thread()->ignore_sync++;
kono
parents:
diff changeset
41 cur_thread()->ignore_reads_and_writes++;
kono
parents:
diff changeset
42 #endif
kono
parents:
diff changeset
43 Printf("FATAL: ThreadSanitizer CHECK failed: "
kono
parents:
diff changeset
44 "%s:%d \"%s\" (0x%zx, 0x%zx)\n",
kono
parents:
diff changeset
45 file, line, cond, (uptr)v1, (uptr)v2);
kono
parents:
diff changeset
46 PrintCurrentStackSlow(StackTrace::GetCurrentPc());
kono
parents:
diff changeset
47 Die();
kono
parents:
diff changeset
48 }
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 // Can be overriden by an application/test to intercept reports.
kono
parents:
diff changeset
51 #ifdef TSAN_EXTERNAL_HOOKS
kono
parents:
diff changeset
52 bool OnReport(const ReportDesc *rep, bool suppressed);
kono
parents:
diff changeset
53 #else
kono
parents:
diff changeset
54 SANITIZER_WEAK_CXX_DEFAULT_IMPL
kono
parents:
diff changeset
55 bool OnReport(const ReportDesc *rep, bool suppressed) {
kono
parents:
diff changeset
56 (void)rep;
kono
parents:
diff changeset
57 return suppressed;
kono
parents:
diff changeset
58 }
kono
parents:
diff changeset
59 #endif
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 SANITIZER_WEAK_DEFAULT_IMPL
kono
parents:
diff changeset
62 void __tsan_on_report(const ReportDesc *rep) {
kono
parents:
diff changeset
63 (void)rep;
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 static void StackStripMain(SymbolizedStack *frames) {
kono
parents:
diff changeset
67 SymbolizedStack *last_frame = nullptr;
kono
parents:
diff changeset
68 SymbolizedStack *last_frame2 = nullptr;
kono
parents:
diff changeset
69 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
kono
parents:
diff changeset
70 last_frame2 = last_frame;
kono
parents:
diff changeset
71 last_frame = cur;
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 if (last_frame2 == 0)
kono
parents:
diff changeset
75 return;
kono
parents:
diff changeset
76 #if !SANITIZER_GO
kono
parents:
diff changeset
77 const char *last = last_frame->info.function;
kono
parents:
diff changeset
78 const char *last2 = last_frame2->info.function;
kono
parents:
diff changeset
79 // Strip frame above 'main'
kono
parents:
diff changeset
80 if (last2 && 0 == internal_strcmp(last2, "main")) {
kono
parents:
diff changeset
81 last_frame->ClearAll();
kono
parents:
diff changeset
82 last_frame2->next = nullptr;
kono
parents:
diff changeset
83 // Strip our internal thread start routine.
kono
parents:
diff changeset
84 } else if (last && 0 == internal_strcmp(last, "__tsan_thread_start_func")) {
kono
parents:
diff changeset
85 last_frame->ClearAll();
kono
parents:
diff changeset
86 last_frame2->next = nullptr;
kono
parents:
diff changeset
87 // Strip global ctors init.
kono
parents:
diff changeset
88 } else if (last && 0 == internal_strcmp(last, "__do_global_ctors_aux")) {
kono
parents:
diff changeset
89 last_frame->ClearAll();
kono
parents:
diff changeset
90 last_frame2->next = nullptr;
kono
parents:
diff changeset
91 // If both are 0, then we probably just failed to symbolize.
kono
parents:
diff changeset
92 } else if (last || last2) {
kono
parents:
diff changeset
93 // Ensure that we recovered stack completely. Trimmed stack
kono
parents:
diff changeset
94 // can actually happen if we do not instrument some code,
kono
parents:
diff changeset
95 // so it's only a debug print. However we must try hard to not miss it
kono
parents:
diff changeset
96 // due to our fault.
kono
parents:
diff changeset
97 DPrintf("Bottom stack frame is missed\n");
kono
parents:
diff changeset
98 }
kono
parents:
diff changeset
99 #else
kono
parents:
diff changeset
100 // The last frame always point into runtime (gosched0, goexit0, runtime.main).
kono
parents:
diff changeset
101 last_frame->ClearAll();
kono
parents:
diff changeset
102 last_frame2->next = nullptr;
kono
parents:
diff changeset
103 #endif
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 ReportStack *SymbolizeStackId(u32 stack_id) {
kono
parents:
diff changeset
107 if (stack_id == 0)
kono
parents:
diff changeset
108 return 0;
kono
parents:
diff changeset
109 StackTrace stack = StackDepotGet(stack_id);
kono
parents:
diff changeset
110 if (stack.trace == nullptr)
kono
parents:
diff changeset
111 return nullptr;
kono
parents:
diff changeset
112 return SymbolizeStack(stack);
kono
parents:
diff changeset
113 }
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 static ReportStack *SymbolizeStack(StackTrace trace) {
kono
parents:
diff changeset
116 if (trace.size == 0)
kono
parents:
diff changeset
117 return 0;
kono
parents:
diff changeset
118 SymbolizedStack *top = nullptr;
kono
parents:
diff changeset
119 for (uptr si = 0; si < trace.size; si++) {
kono
parents:
diff changeset
120 const uptr pc = trace.trace[si];
kono
parents:
diff changeset
121 uptr pc1 = pc;
kono
parents:
diff changeset
122 // We obtain the return address, but we're interested in the previous
kono
parents:
diff changeset
123 // instruction.
kono
parents:
diff changeset
124 if ((pc & kExternalPCBit) == 0)
kono
parents:
diff changeset
125 pc1 = StackTrace::GetPreviousInstructionPc(pc);
kono
parents:
diff changeset
126 SymbolizedStack *ent = SymbolizeCode(pc1);
kono
parents:
diff changeset
127 CHECK_NE(ent, 0);
kono
parents:
diff changeset
128 SymbolizedStack *last = ent;
kono
parents:
diff changeset
129 while (last->next) {
kono
parents:
diff changeset
130 last->info.address = pc; // restore original pc for report
kono
parents:
diff changeset
131 last = last->next;
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133 last->info.address = pc; // restore original pc for report
kono
parents:
diff changeset
134 last->next = top;
kono
parents:
diff changeset
135 top = ent;
kono
parents:
diff changeset
136 }
kono
parents:
diff changeset
137 StackStripMain(top);
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 ReportStack *stack = ReportStack::New();
kono
parents:
diff changeset
140 stack->frames = top;
kono
parents:
diff changeset
141 return stack;
kono
parents:
diff changeset
142 }
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 ScopedReport::ScopedReport(ReportType typ, uptr tag) {
kono
parents:
diff changeset
145 ctx->thread_registry->CheckLocked();
kono
parents:
diff changeset
146 void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
kono
parents:
diff changeset
147 rep_ = new(mem) ReportDesc;
kono
parents:
diff changeset
148 rep_->typ = typ;
kono
parents:
diff changeset
149 rep_->tag = tag;
kono
parents:
diff changeset
150 ctx->report_mtx.Lock();
kono
parents:
diff changeset
151 CommonSanitizerReportMutex.Lock();
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 ScopedReport::~ScopedReport() {
kono
parents:
diff changeset
155 CommonSanitizerReportMutex.Unlock();
kono
parents:
diff changeset
156 ctx->report_mtx.Unlock();
kono
parents:
diff changeset
157 DestroyAndFree(rep_);
kono
parents:
diff changeset
158 }
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 void ScopedReport::AddStack(StackTrace stack, bool suppressable) {
kono
parents:
diff changeset
161 ReportStack **rs = rep_->stacks.PushBack();
kono
parents:
diff changeset
162 *rs = SymbolizeStack(stack);
kono
parents:
diff changeset
163 (*rs)->suppressable = suppressable;
kono
parents:
diff changeset
164 }
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 void ScopedReport::AddMemoryAccess(uptr addr, uptr external_tag, Shadow s,
kono
parents:
diff changeset
167 StackTrace stack, const MutexSet *mset) {
kono
parents:
diff changeset
168 void *mem = internal_alloc(MBlockReportMop, sizeof(ReportMop));
kono
parents:
diff changeset
169 ReportMop *mop = new(mem) ReportMop;
kono
parents:
diff changeset
170 rep_->mops.PushBack(mop);
kono
parents:
diff changeset
171 mop->tid = s.tid();
kono
parents:
diff changeset
172 mop->addr = addr + s.addr0();
kono
parents:
diff changeset
173 mop->size = s.size();
kono
parents:
diff changeset
174 mop->write = s.IsWrite();
kono
parents:
diff changeset
175 mop->atomic = s.IsAtomic();
kono
parents:
diff changeset
176 mop->stack = SymbolizeStack(stack);
kono
parents:
diff changeset
177 mop->external_tag = external_tag;
kono
parents:
diff changeset
178 if (mop->stack)
kono
parents:
diff changeset
179 mop->stack->suppressable = true;
kono
parents:
diff changeset
180 for (uptr i = 0; i < mset->Size(); i++) {
kono
parents:
diff changeset
181 MutexSet::Desc d = mset->Get(i);
kono
parents:
diff changeset
182 u64 mid = this->AddMutex(d.id);
kono
parents:
diff changeset
183 ReportMopMutex mtx = {mid, d.write};
kono
parents:
diff changeset
184 mop->mset.PushBack(mtx);
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186 }
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 void ScopedReport::AddUniqueTid(int unique_tid) {
kono
parents:
diff changeset
189 rep_->unique_tids.PushBack(unique_tid);
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 void ScopedReport::AddThread(const ThreadContext *tctx, bool suppressable) {
kono
parents:
diff changeset
193 for (uptr i = 0; i < rep_->threads.Size(); i++) {
kono
parents:
diff changeset
194 if ((u32)rep_->threads[i]->id == tctx->tid)
kono
parents:
diff changeset
195 return;
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197 void *mem = internal_alloc(MBlockReportThread, sizeof(ReportThread));
kono
parents:
diff changeset
198 ReportThread *rt = new(mem) ReportThread;
kono
parents:
diff changeset
199 rep_->threads.PushBack(rt);
kono
parents:
diff changeset
200 rt->id = tctx->tid;
kono
parents:
diff changeset
201 rt->os_id = tctx->os_id;
kono
parents:
diff changeset
202 rt->running = (tctx->status == ThreadStatusRunning);
kono
parents:
diff changeset
203 rt->name = internal_strdup(tctx->name);
kono
parents:
diff changeset
204 rt->parent_tid = tctx->parent_tid;
kono
parents:
diff changeset
205 rt->workerthread = tctx->workerthread;
kono
parents:
diff changeset
206 rt->stack = 0;
kono
parents:
diff changeset
207 rt->stack = SymbolizeStackId(tctx->creation_stack_id);
kono
parents:
diff changeset
208 if (rt->stack)
kono
parents:
diff changeset
209 rt->stack->suppressable = suppressable;
kono
parents:
diff changeset
210 }
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 #if !SANITIZER_GO
kono
parents:
diff changeset
213 static bool FindThreadByUidLockedCallback(ThreadContextBase *tctx, void *arg) {
kono
parents:
diff changeset
214 int unique_id = *(int *)arg;
kono
parents:
diff changeset
215 return tctx->unique_id == (u32)unique_id;
kono
parents:
diff changeset
216 }
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 static ThreadContext *FindThreadByUidLocked(int unique_id) {
kono
parents:
diff changeset
219 ctx->thread_registry->CheckLocked();
kono
parents:
diff changeset
220 return static_cast<ThreadContext *>(
kono
parents:
diff changeset
221 ctx->thread_registry->FindThreadContextLocked(
kono
parents:
diff changeset
222 FindThreadByUidLockedCallback, &unique_id));
kono
parents:
diff changeset
223 }
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 static ThreadContext *FindThreadByTidLocked(int tid) {
kono
parents:
diff changeset
226 ctx->thread_registry->CheckLocked();
kono
parents:
diff changeset
227 return static_cast<ThreadContext*>(
kono
parents:
diff changeset
228 ctx->thread_registry->GetThreadLocked(tid));
kono
parents:
diff changeset
229 }
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 static bool IsInStackOrTls(ThreadContextBase *tctx_base, void *arg) {
kono
parents:
diff changeset
232 uptr addr = (uptr)arg;
kono
parents:
diff changeset
233 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
kono
parents:
diff changeset
234 if (tctx->status != ThreadStatusRunning)
kono
parents:
diff changeset
235 return false;
kono
parents:
diff changeset
236 ThreadState *thr = tctx->thr;
kono
parents:
diff changeset
237 CHECK(thr);
kono
parents:
diff changeset
238 return ((addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size) ||
kono
parents:
diff changeset
239 (addr >= thr->tls_addr && addr < thr->tls_addr + thr->tls_size));
kono
parents:
diff changeset
240 }
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 ThreadContext *IsThreadStackOrTls(uptr addr, bool *is_stack) {
kono
parents:
diff changeset
243 ctx->thread_registry->CheckLocked();
kono
parents:
diff changeset
244 ThreadContext *tctx = static_cast<ThreadContext*>(
kono
parents:
diff changeset
245 ctx->thread_registry->FindThreadContextLocked(IsInStackOrTls,
kono
parents:
diff changeset
246 (void*)addr));
kono
parents:
diff changeset
247 if (!tctx)
kono
parents:
diff changeset
248 return 0;
kono
parents:
diff changeset
249 ThreadState *thr = tctx->thr;
kono
parents:
diff changeset
250 CHECK(thr);
kono
parents:
diff changeset
251 *is_stack = (addr >= thr->stk_addr && addr < thr->stk_addr + thr->stk_size);
kono
parents:
diff changeset
252 return tctx;
kono
parents:
diff changeset
253 }
kono
parents:
diff changeset
254 #endif
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 void ScopedReport::AddThread(int unique_tid, bool suppressable) {
kono
parents:
diff changeset
257 #if !SANITIZER_GO
kono
parents:
diff changeset
258 if (const ThreadContext *tctx = FindThreadByUidLocked(unique_tid))
kono
parents:
diff changeset
259 AddThread(tctx, suppressable);
kono
parents:
diff changeset
260 #endif
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 void ScopedReport::AddMutex(const SyncVar *s) {
kono
parents:
diff changeset
264 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
kono
parents:
diff changeset
265 if (rep_->mutexes[i]->id == s->uid)
kono
parents:
diff changeset
266 return;
kono
parents:
diff changeset
267 }
kono
parents:
diff changeset
268 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
kono
parents:
diff changeset
269 ReportMutex *rm = new(mem) ReportMutex;
kono
parents:
diff changeset
270 rep_->mutexes.PushBack(rm);
kono
parents:
diff changeset
271 rm->id = s->uid;
kono
parents:
diff changeset
272 rm->addr = s->addr;
kono
parents:
diff changeset
273 rm->destroyed = false;
kono
parents:
diff changeset
274 rm->stack = SymbolizeStackId(s->creation_stack_id);
kono
parents:
diff changeset
275 }
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 u64 ScopedReport::AddMutex(u64 id) {
kono
parents:
diff changeset
278 u64 uid = 0;
kono
parents:
diff changeset
279 u64 mid = id;
kono
parents:
diff changeset
280 uptr addr = SyncVar::SplitId(id, &uid);
kono
parents:
diff changeset
281 SyncVar *s = ctx->metamap.GetIfExistsAndLock(addr, true);
kono
parents:
diff changeset
282 // Check that the mutex is still alive.
kono
parents:
diff changeset
283 // Another mutex can be created at the same address,
kono
parents:
diff changeset
284 // so check uid as well.
kono
parents:
diff changeset
285 if (s && s->CheckId(uid)) {
kono
parents:
diff changeset
286 mid = s->uid;
kono
parents:
diff changeset
287 AddMutex(s);
kono
parents:
diff changeset
288 } else {
kono
parents:
diff changeset
289 AddDeadMutex(id);
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291 if (s)
kono
parents:
diff changeset
292 s->mtx.Unlock();
kono
parents:
diff changeset
293 return mid;
kono
parents:
diff changeset
294 }
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 void ScopedReport::AddDeadMutex(u64 id) {
kono
parents:
diff changeset
297 for (uptr i = 0; i < rep_->mutexes.Size(); i++) {
kono
parents:
diff changeset
298 if (rep_->mutexes[i]->id == id)
kono
parents:
diff changeset
299 return;
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301 void *mem = internal_alloc(MBlockReportMutex, sizeof(ReportMutex));
kono
parents:
diff changeset
302 ReportMutex *rm = new(mem) ReportMutex;
kono
parents:
diff changeset
303 rep_->mutexes.PushBack(rm);
kono
parents:
diff changeset
304 rm->id = id;
kono
parents:
diff changeset
305 rm->addr = 0;
kono
parents:
diff changeset
306 rm->destroyed = true;
kono
parents:
diff changeset
307 rm->stack = 0;
kono
parents:
diff changeset
308 }
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 void ScopedReport::AddLocation(uptr addr, uptr size) {
kono
parents:
diff changeset
311 if (addr == 0)
kono
parents:
diff changeset
312 return;
kono
parents:
diff changeset
313 #if !SANITIZER_GO
kono
parents:
diff changeset
314 int fd = -1;
kono
parents:
diff changeset
315 int creat_tid = kInvalidTid;
kono
parents:
diff changeset
316 u32 creat_stack = 0;
kono
parents:
diff changeset
317 if (FdLocation(addr, &fd, &creat_tid, &creat_stack)) {
kono
parents:
diff changeset
318 ReportLocation *loc = ReportLocation::New(ReportLocationFD);
kono
parents:
diff changeset
319 loc->fd = fd;
kono
parents:
diff changeset
320 loc->tid = creat_tid;
kono
parents:
diff changeset
321 loc->stack = SymbolizeStackId(creat_stack);
kono
parents:
diff changeset
322 rep_->locs.PushBack(loc);
kono
parents:
diff changeset
323 ThreadContext *tctx = FindThreadByUidLocked(creat_tid);
kono
parents:
diff changeset
324 if (tctx)
kono
parents:
diff changeset
325 AddThread(tctx);
kono
parents:
diff changeset
326 return;
kono
parents:
diff changeset
327 }
kono
parents:
diff changeset
328 MBlock *b = 0;
kono
parents:
diff changeset
329 Allocator *a = allocator();
kono
parents:
diff changeset
330 if (a->PointerIsMine((void*)addr)) {
kono
parents:
diff changeset
331 void *block_begin = a->GetBlockBegin((void*)addr);
kono
parents:
diff changeset
332 if (block_begin)
kono
parents:
diff changeset
333 b = ctx->metamap.GetBlock((uptr)block_begin);
kono
parents:
diff changeset
334 }
kono
parents:
diff changeset
335 if (b != 0) {
kono
parents:
diff changeset
336 ThreadContext *tctx = FindThreadByTidLocked(b->tid);
kono
parents:
diff changeset
337 ReportLocation *loc = ReportLocation::New(ReportLocationHeap);
kono
parents:
diff changeset
338 loc->heap_chunk_start = (uptr)allocator()->GetBlockBegin((void *)addr);
kono
parents:
diff changeset
339 loc->heap_chunk_size = b->siz;
kono
parents:
diff changeset
340 loc->external_tag = b->tag;
kono
parents:
diff changeset
341 loc->tid = tctx ? tctx->tid : b->tid;
kono
parents:
diff changeset
342 loc->stack = SymbolizeStackId(b->stk);
kono
parents:
diff changeset
343 rep_->locs.PushBack(loc);
kono
parents:
diff changeset
344 if (tctx)
kono
parents:
diff changeset
345 AddThread(tctx);
kono
parents:
diff changeset
346 return;
kono
parents:
diff changeset
347 }
kono
parents:
diff changeset
348 bool is_stack = false;
kono
parents:
diff changeset
349 if (ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack)) {
kono
parents:
diff changeset
350 ReportLocation *loc =
kono
parents:
diff changeset
351 ReportLocation::New(is_stack ? ReportLocationStack : ReportLocationTLS);
kono
parents:
diff changeset
352 loc->tid = tctx->tid;
kono
parents:
diff changeset
353 rep_->locs.PushBack(loc);
kono
parents:
diff changeset
354 AddThread(tctx);
kono
parents:
diff changeset
355 }
kono
parents:
diff changeset
356 #endif
kono
parents:
diff changeset
357 if (ReportLocation *loc = SymbolizeData(addr)) {
kono
parents:
diff changeset
358 loc->suppressable = true;
kono
parents:
diff changeset
359 rep_->locs.PushBack(loc);
kono
parents:
diff changeset
360 return;
kono
parents:
diff changeset
361 }
kono
parents:
diff changeset
362 }
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 #if !SANITIZER_GO
kono
parents:
diff changeset
365 void ScopedReport::AddSleep(u32 stack_id) {
kono
parents:
diff changeset
366 rep_->sleep = SymbolizeStackId(stack_id);
kono
parents:
diff changeset
367 }
kono
parents:
diff changeset
368 #endif
kono
parents:
diff changeset
369
kono
parents:
diff changeset
370 void ScopedReport::SetCount(int count) {
kono
parents:
diff changeset
371 rep_->count = count;
kono
parents:
diff changeset
372 }
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 const ReportDesc *ScopedReport::GetReport() const {
kono
parents:
diff changeset
375 return rep_;
kono
parents:
diff changeset
376 }
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 void RestoreStack(int tid, const u64 epoch, VarSizeStackTrace *stk,
kono
parents:
diff changeset
379 MutexSet *mset, uptr *tag) {
kono
parents:
diff changeset
380 // This function restores stack trace and mutex set for the thread/epoch.
kono
parents:
diff changeset
381 // It does so by getting stack trace and mutex set at the beginning of
kono
parents:
diff changeset
382 // trace part, and then replaying the trace till the given epoch.
kono
parents:
diff changeset
383 Trace* trace = ThreadTrace(tid);
kono
parents:
diff changeset
384 ReadLock l(&trace->mtx);
kono
parents:
diff changeset
385 const int partidx = (epoch / kTracePartSize) % TraceParts();
kono
parents:
diff changeset
386 TraceHeader* hdr = &trace->headers[partidx];
kono
parents:
diff changeset
387 if (epoch < hdr->epoch0 || epoch >= hdr->epoch0 + kTracePartSize)
kono
parents:
diff changeset
388 return;
kono
parents:
diff changeset
389 CHECK_EQ(RoundDown(epoch, kTracePartSize), hdr->epoch0);
kono
parents:
diff changeset
390 const u64 epoch0 = RoundDown(epoch, TraceSize());
kono
parents:
diff changeset
391 const u64 eend = epoch % TraceSize();
kono
parents:
diff changeset
392 const u64 ebegin = RoundDown(eend, kTracePartSize);
kono
parents:
diff changeset
393 DPrintf("#%d: RestoreStack epoch=%zu ebegin=%zu eend=%zu partidx=%d\n",
kono
parents:
diff changeset
394 tid, (uptr)epoch, (uptr)ebegin, (uptr)eend, partidx);
kono
parents:
diff changeset
395 Vector<uptr> stack(MBlockReportStack);
kono
parents:
diff changeset
396 stack.Resize(hdr->stack0.size + 64);
kono
parents:
diff changeset
397 for (uptr i = 0; i < hdr->stack0.size; i++) {
kono
parents:
diff changeset
398 stack[i] = hdr->stack0.trace[i];
kono
parents:
diff changeset
399 DPrintf2(" #%02zu: pc=%zx\n", i, stack[i]);
kono
parents:
diff changeset
400 }
kono
parents:
diff changeset
401 if (mset)
kono
parents:
diff changeset
402 *mset = hdr->mset0;
kono
parents:
diff changeset
403 uptr pos = hdr->stack0.size;
kono
parents:
diff changeset
404 Event *events = (Event*)GetThreadTrace(tid);
kono
parents:
diff changeset
405 for (uptr i = ebegin; i <= eend; i++) {
kono
parents:
diff changeset
406 Event ev = events[i];
kono
parents:
diff changeset
407 EventType typ = (EventType)(ev >> kEventPCBits);
kono
parents:
diff changeset
408 uptr pc = (uptr)(ev & ((1ull << kEventPCBits) - 1));
kono
parents:
diff changeset
409 DPrintf2(" %zu typ=%d pc=%zx\n", i, typ, pc);
kono
parents:
diff changeset
410 if (typ == EventTypeMop) {
kono
parents:
diff changeset
411 stack[pos] = pc;
kono
parents:
diff changeset
412 } else if (typ == EventTypeFuncEnter) {
kono
parents:
diff changeset
413 if (stack.Size() < pos + 2)
kono
parents:
diff changeset
414 stack.Resize(pos + 2);
kono
parents:
diff changeset
415 stack[pos++] = pc;
kono
parents:
diff changeset
416 } else if (typ == EventTypeFuncExit) {
kono
parents:
diff changeset
417 if (pos > 0)
kono
parents:
diff changeset
418 pos--;
kono
parents:
diff changeset
419 }
kono
parents:
diff changeset
420 if (mset) {
kono
parents:
diff changeset
421 if (typ == EventTypeLock) {
kono
parents:
diff changeset
422 mset->Add(pc, true, epoch0 + i);
kono
parents:
diff changeset
423 } else if (typ == EventTypeUnlock) {
kono
parents:
diff changeset
424 mset->Del(pc, true);
kono
parents:
diff changeset
425 } else if (typ == EventTypeRLock) {
kono
parents:
diff changeset
426 mset->Add(pc, false, epoch0 + i);
kono
parents:
diff changeset
427 } else if (typ == EventTypeRUnlock) {
kono
parents:
diff changeset
428 mset->Del(pc, false);
kono
parents:
diff changeset
429 }
kono
parents:
diff changeset
430 }
kono
parents:
diff changeset
431 for (uptr j = 0; j <= pos; j++)
kono
parents:
diff changeset
432 DPrintf2(" #%zu: %zx\n", j, stack[j]);
kono
parents:
diff changeset
433 }
kono
parents:
diff changeset
434 if (pos == 0 && stack[0] == 0)
kono
parents:
diff changeset
435 return;
kono
parents:
diff changeset
436 pos++;
kono
parents:
diff changeset
437 stk->Init(&stack[0], pos);
kono
parents:
diff changeset
438 ExtractTagFromStack(stk, tag);
kono
parents:
diff changeset
439 }
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 static bool HandleRacyStacks(ThreadState *thr, VarSizeStackTrace traces[2],
kono
parents:
diff changeset
442 uptr addr_min, uptr addr_max) {
kono
parents:
diff changeset
443 bool equal_stack = false;
kono
parents:
diff changeset
444 RacyStacks hash;
kono
parents:
diff changeset
445 bool equal_address = false;
kono
parents:
diff changeset
446 RacyAddress ra0 = {addr_min, addr_max};
kono
parents:
diff changeset
447 {
kono
parents:
diff changeset
448 ReadLock lock(&ctx->racy_mtx);
kono
parents:
diff changeset
449 if (flags()->suppress_equal_stacks) {
kono
parents:
diff changeset
450 hash.hash[0] = md5_hash(traces[0].trace, traces[0].size * sizeof(uptr));
kono
parents:
diff changeset
451 hash.hash[1] = md5_hash(traces[1].trace, traces[1].size * sizeof(uptr));
kono
parents:
diff changeset
452 for (uptr i = 0; i < ctx->racy_stacks.Size(); i++) {
kono
parents:
diff changeset
453 if (hash == ctx->racy_stacks[i]) {
kono
parents:
diff changeset
454 VPrintf(2,
kono
parents:
diff changeset
455 "ThreadSanitizer: suppressing report as doubled (stack)\n");
kono
parents:
diff changeset
456 equal_stack = true;
kono
parents:
diff changeset
457 break;
kono
parents:
diff changeset
458 }
kono
parents:
diff changeset
459 }
kono
parents:
diff changeset
460 }
kono
parents:
diff changeset
461 if (flags()->suppress_equal_addresses) {
kono
parents:
diff changeset
462 for (uptr i = 0; i < ctx->racy_addresses.Size(); i++) {
kono
parents:
diff changeset
463 RacyAddress ra2 = ctx->racy_addresses[i];
kono
parents:
diff changeset
464 uptr maxbeg = max(ra0.addr_min, ra2.addr_min);
kono
parents:
diff changeset
465 uptr minend = min(ra0.addr_max, ra2.addr_max);
kono
parents:
diff changeset
466 if (maxbeg < minend) {
kono
parents:
diff changeset
467 VPrintf(2, "ThreadSanitizer: suppressing report as doubled (addr)\n");
kono
parents:
diff changeset
468 equal_address = true;
kono
parents:
diff changeset
469 break;
kono
parents:
diff changeset
470 }
kono
parents:
diff changeset
471 }
kono
parents:
diff changeset
472 }
kono
parents:
diff changeset
473 }
kono
parents:
diff changeset
474 if (!equal_stack && !equal_address)
kono
parents:
diff changeset
475 return false;
kono
parents:
diff changeset
476 if (!equal_stack) {
kono
parents:
diff changeset
477 Lock lock(&ctx->racy_mtx);
kono
parents:
diff changeset
478 ctx->racy_stacks.PushBack(hash);
kono
parents:
diff changeset
479 }
kono
parents:
diff changeset
480 if (!equal_address) {
kono
parents:
diff changeset
481 Lock lock(&ctx->racy_mtx);
kono
parents:
diff changeset
482 ctx->racy_addresses.PushBack(ra0);
kono
parents:
diff changeset
483 }
kono
parents:
diff changeset
484 return true;
kono
parents:
diff changeset
485 }
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 static void AddRacyStacks(ThreadState *thr, VarSizeStackTrace traces[2],
kono
parents:
diff changeset
488 uptr addr_min, uptr addr_max) {
kono
parents:
diff changeset
489 Lock lock(&ctx->racy_mtx);
kono
parents:
diff changeset
490 if (flags()->suppress_equal_stacks) {
kono
parents:
diff changeset
491 RacyStacks hash;
kono
parents:
diff changeset
492 hash.hash[0] = md5_hash(traces[0].trace, traces[0].size * sizeof(uptr));
kono
parents:
diff changeset
493 hash.hash[1] = md5_hash(traces[1].trace, traces[1].size * sizeof(uptr));
kono
parents:
diff changeset
494 ctx->racy_stacks.PushBack(hash);
kono
parents:
diff changeset
495 }
kono
parents:
diff changeset
496 if (flags()->suppress_equal_addresses) {
kono
parents:
diff changeset
497 RacyAddress ra0 = {addr_min, addr_max};
kono
parents:
diff changeset
498 ctx->racy_addresses.PushBack(ra0);
kono
parents:
diff changeset
499 }
kono
parents:
diff changeset
500 }
kono
parents:
diff changeset
501
kono
parents:
diff changeset
502 bool OutputReport(ThreadState *thr, const ScopedReport &srep) {
kono
parents:
diff changeset
503 if (!flags()->report_bugs || thr->suppress_reports)
kono
parents:
diff changeset
504 return false;
kono
parents:
diff changeset
505 atomic_store_relaxed(&ctx->last_symbolize_time_ns, NanoTime());
kono
parents:
diff changeset
506 const ReportDesc *rep = srep.GetReport();
kono
parents:
diff changeset
507 CHECK_EQ(thr->current_report, nullptr);
kono
parents:
diff changeset
508 thr->current_report = rep;
kono
parents:
diff changeset
509 Suppression *supp = 0;
kono
parents:
diff changeset
510 uptr pc_or_addr = 0;
kono
parents:
diff changeset
511 for (uptr i = 0; pc_or_addr == 0 && i < rep->mops.Size(); i++)
kono
parents:
diff changeset
512 pc_or_addr = IsSuppressed(rep->typ, rep->mops[i]->stack, &supp);
kono
parents:
diff changeset
513 for (uptr i = 0; pc_or_addr == 0 && i < rep->stacks.Size(); i++)
kono
parents:
diff changeset
514 pc_or_addr = IsSuppressed(rep->typ, rep->stacks[i], &supp);
kono
parents:
diff changeset
515 for (uptr i = 0; pc_or_addr == 0 && i < rep->threads.Size(); i++)
kono
parents:
diff changeset
516 pc_or_addr = IsSuppressed(rep->typ, rep->threads[i]->stack, &supp);
kono
parents:
diff changeset
517 for (uptr i = 0; pc_or_addr == 0 && i < rep->locs.Size(); i++)
kono
parents:
diff changeset
518 pc_or_addr = IsSuppressed(rep->typ, rep->locs[i], &supp);
kono
parents:
diff changeset
519 if (pc_or_addr != 0) {
kono
parents:
diff changeset
520 Lock lock(&ctx->fired_suppressions_mtx);
kono
parents:
diff changeset
521 FiredSuppression s = {srep.GetReport()->typ, pc_or_addr, supp};
kono
parents:
diff changeset
522 ctx->fired_suppressions.push_back(s);
kono
parents:
diff changeset
523 }
kono
parents:
diff changeset
524 {
kono
parents:
diff changeset
525 bool old_is_freeing = thr->is_freeing;
kono
parents:
diff changeset
526 thr->is_freeing = false;
kono
parents:
diff changeset
527 bool suppressed = OnReport(rep, pc_or_addr != 0);
kono
parents:
diff changeset
528 thr->is_freeing = old_is_freeing;
kono
parents:
diff changeset
529 if (suppressed) {
kono
parents:
diff changeset
530 thr->current_report = nullptr;
kono
parents:
diff changeset
531 return false;
kono
parents:
diff changeset
532 }
kono
parents:
diff changeset
533 }
kono
parents:
diff changeset
534 PrintReport(rep);
kono
parents:
diff changeset
535 __tsan_on_report(rep);
kono
parents:
diff changeset
536 ctx->nreported++;
kono
parents:
diff changeset
537 if (flags()->halt_on_error)
kono
parents:
diff changeset
538 Die();
kono
parents:
diff changeset
539 thr->current_report = nullptr;
kono
parents:
diff changeset
540 return true;
kono
parents:
diff changeset
541 }
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543 bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace) {
kono
parents:
diff changeset
544 ReadLock lock(&ctx->fired_suppressions_mtx);
kono
parents:
diff changeset
545 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
kono
parents:
diff changeset
546 if (ctx->fired_suppressions[k].type != type)
kono
parents:
diff changeset
547 continue;
kono
parents:
diff changeset
548 for (uptr j = 0; j < trace.size; j++) {
kono
parents:
diff changeset
549 FiredSuppression *s = &ctx->fired_suppressions[k];
kono
parents:
diff changeset
550 if (trace.trace[j] == s->pc_or_addr) {
kono
parents:
diff changeset
551 if (s->supp)
kono
parents:
diff changeset
552 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);
kono
parents:
diff changeset
553 return true;
kono
parents:
diff changeset
554 }
kono
parents:
diff changeset
555 }
kono
parents:
diff changeset
556 }
kono
parents:
diff changeset
557 return false;
kono
parents:
diff changeset
558 }
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 static bool IsFiredSuppression(Context *ctx, ReportType type, uptr addr) {
kono
parents:
diff changeset
561 ReadLock lock(&ctx->fired_suppressions_mtx);
kono
parents:
diff changeset
562 for (uptr k = 0; k < ctx->fired_suppressions.size(); k++) {
kono
parents:
diff changeset
563 if (ctx->fired_suppressions[k].type != type)
kono
parents:
diff changeset
564 continue;
kono
parents:
diff changeset
565 FiredSuppression *s = &ctx->fired_suppressions[k];
kono
parents:
diff changeset
566 if (addr == s->pc_or_addr) {
kono
parents:
diff changeset
567 if (s->supp)
kono
parents:
diff changeset
568 atomic_fetch_add(&s->supp->hit_count, 1, memory_order_relaxed);
kono
parents:
diff changeset
569 return true;
kono
parents:
diff changeset
570 }
kono
parents:
diff changeset
571 }
kono
parents:
diff changeset
572 return false;
kono
parents:
diff changeset
573 }
kono
parents:
diff changeset
574
kono
parents:
diff changeset
575 static bool RaceBetweenAtomicAndFree(ThreadState *thr) {
kono
parents:
diff changeset
576 Shadow s0(thr->racy_state[0]);
kono
parents:
diff changeset
577 Shadow s1(thr->racy_state[1]);
kono
parents:
diff changeset
578 CHECK(!(s0.IsAtomic() && s1.IsAtomic()));
kono
parents:
diff changeset
579 if (!s0.IsAtomic() && !s1.IsAtomic())
kono
parents:
diff changeset
580 return true;
kono
parents:
diff changeset
581 if (s0.IsAtomic() && s1.IsFreed())
kono
parents:
diff changeset
582 return true;
kono
parents:
diff changeset
583 if (s1.IsAtomic() && thr->is_freeing)
kono
parents:
diff changeset
584 return true;
kono
parents:
diff changeset
585 return false;
kono
parents:
diff changeset
586 }
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 void ReportRace(ThreadState *thr) {
kono
parents:
diff changeset
589 CheckNoLocks(thr);
kono
parents:
diff changeset
590
kono
parents:
diff changeset
591 // Symbolizer makes lots of intercepted calls. If we try to process them,
kono
parents:
diff changeset
592 // at best it will cause deadlocks on internal mutexes.
kono
parents:
diff changeset
593 ScopedIgnoreInterceptors ignore;
kono
parents:
diff changeset
594
kono
parents:
diff changeset
595 if (!flags()->report_bugs)
kono
parents:
diff changeset
596 return;
kono
parents:
diff changeset
597 if (!flags()->report_atomic_races && !RaceBetweenAtomicAndFree(thr))
kono
parents:
diff changeset
598 return;
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 bool freed = false;
kono
parents:
diff changeset
601 {
kono
parents:
diff changeset
602 Shadow s(thr->racy_state[1]);
kono
parents:
diff changeset
603 freed = s.GetFreedAndReset();
kono
parents:
diff changeset
604 thr->racy_state[1] = s.raw();
kono
parents:
diff changeset
605 }
kono
parents:
diff changeset
606
kono
parents:
diff changeset
607 uptr addr = ShadowToMem((uptr)thr->racy_shadow_addr);
kono
parents:
diff changeset
608 uptr addr_min = 0;
kono
parents:
diff changeset
609 uptr addr_max = 0;
kono
parents:
diff changeset
610 {
kono
parents:
diff changeset
611 uptr a0 = addr + Shadow(thr->racy_state[0]).addr0();
kono
parents:
diff changeset
612 uptr a1 = addr + Shadow(thr->racy_state[1]).addr0();
kono
parents:
diff changeset
613 uptr e0 = a0 + Shadow(thr->racy_state[0]).size();
kono
parents:
diff changeset
614 uptr e1 = a1 + Shadow(thr->racy_state[1]).size();
kono
parents:
diff changeset
615 addr_min = min(a0, a1);
kono
parents:
diff changeset
616 addr_max = max(e0, e1);
kono
parents:
diff changeset
617 if (IsExpectedReport(addr_min, addr_max - addr_min))
kono
parents:
diff changeset
618 return;
kono
parents:
diff changeset
619 }
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 ReportType typ = ReportTypeRace;
kono
parents:
diff changeset
622 if (thr->is_vptr_access && freed)
kono
parents:
diff changeset
623 typ = ReportTypeVptrUseAfterFree;
kono
parents:
diff changeset
624 else if (thr->is_vptr_access)
kono
parents:
diff changeset
625 typ = ReportTypeVptrRace;
kono
parents:
diff changeset
626 else if (freed)
kono
parents:
diff changeset
627 typ = ReportTypeUseAfterFree;
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 if (IsFiredSuppression(ctx, typ, addr))
kono
parents:
diff changeset
630 return;
kono
parents:
diff changeset
631
kono
parents:
diff changeset
632 const uptr kMop = 2;
kono
parents:
diff changeset
633 VarSizeStackTrace traces[kMop];
kono
parents:
diff changeset
634 uptr tags[kMop] = {kExternalTagNone};
kono
parents:
diff changeset
635 uptr toppc = TraceTopPC(thr);
kono
parents:
diff changeset
636 if (toppc >> kEventPCBits) {
kono
parents:
diff changeset
637 // This is a work-around for a known issue.
kono
parents:
diff changeset
638 // The scenario where this happens is rather elaborate and requires
kono
parents:
diff changeset
639 // an instrumented __sanitizer_report_error_summary callback and
kono
parents:
diff changeset
640 // a __tsan_symbolize_external callback and a race during a range memory
kono
parents:
diff changeset
641 // access larger than 8 bytes. MemoryAccessRange adds the current PC to
kono
parents:
diff changeset
642 // the trace and starts processing memory accesses. A first memory access
kono
parents:
diff changeset
643 // triggers a race, we report it and call the instrumented
kono
parents:
diff changeset
644 // __sanitizer_report_error_summary, which adds more stuff to the trace
kono
parents:
diff changeset
645 // since it is intrumented. Then a second memory access in MemoryAccessRange
kono
parents:
diff changeset
646 // also triggers a race and we get here and call TraceTopPC to get the
kono
parents:
diff changeset
647 // current PC, however now it contains some unrelated events from the
kono
parents:
diff changeset
648 // callback. Most likely, TraceTopPC will now return a EventTypeFuncExit
kono
parents:
diff changeset
649 // event. Later we subtract -1 from it (in GetPreviousInstructionPc)
kono
parents:
diff changeset
650 // and the resulting PC has kExternalPCBit set, so we pass it to
kono
parents:
diff changeset
651 // __tsan_symbolize_external. __tsan_symbolize_external is within its rights
kono
parents:
diff changeset
652 // to crash since the PC is completely bogus.
kono
parents:
diff changeset
653 // test/tsan/double_race.cc contains a test case for this.
kono
parents:
diff changeset
654 toppc = 0;
kono
parents:
diff changeset
655 }
kono
parents:
diff changeset
656 ObtainCurrentStack(thr, toppc, &traces[0], &tags[0]);
kono
parents:
diff changeset
657 if (IsFiredSuppression(ctx, typ, traces[0]))
kono
parents:
diff changeset
658 return;
kono
parents:
diff changeset
659
kono
parents:
diff changeset
660 // MutexSet is too large to live on stack.
kono
parents:
diff changeset
661 Vector<u64> mset_buffer(MBlockScopedBuf);
kono
parents:
diff changeset
662 mset_buffer.Resize(sizeof(MutexSet) / sizeof(u64) + 1);
kono
parents:
diff changeset
663 MutexSet *mset2 = new(&mset_buffer[0]) MutexSet();
kono
parents:
diff changeset
664
kono
parents:
diff changeset
665 Shadow s2(thr->racy_state[1]);
kono
parents:
diff changeset
666 RestoreStack(s2.tid(), s2.epoch(), &traces[1], mset2, &tags[1]);
kono
parents:
diff changeset
667 if (IsFiredSuppression(ctx, typ, traces[1]))
kono
parents:
diff changeset
668 return;
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 if (HandleRacyStacks(thr, traces, addr_min, addr_max))
kono
parents:
diff changeset
671 return;
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 // If any of the accesses has a tag, treat this as an "external" race.
kono
parents:
diff changeset
674 uptr tag = kExternalTagNone;
kono
parents:
diff changeset
675 for (uptr i = 0; i < kMop; i++) {
kono
parents:
diff changeset
676 if (tags[i] != kExternalTagNone) {
kono
parents:
diff changeset
677 typ = ReportTypeExternalRace;
kono
parents:
diff changeset
678 tag = tags[i];
kono
parents:
diff changeset
679 break;
kono
parents:
diff changeset
680 }
kono
parents:
diff changeset
681 }
kono
parents:
diff changeset
682
kono
parents:
diff changeset
683 ThreadRegistryLock l0(ctx->thread_registry);
kono
parents:
diff changeset
684 ScopedReport rep(typ, tag);
kono
parents:
diff changeset
685 for (uptr i = 0; i < kMop; i++) {
kono
parents:
diff changeset
686 Shadow s(thr->racy_state[i]);
kono
parents:
diff changeset
687 rep.AddMemoryAccess(addr, tags[i], s, traces[i],
kono
parents:
diff changeset
688 i == 0 ? &thr->mset : mset2);
kono
parents:
diff changeset
689 }
kono
parents:
diff changeset
690
kono
parents:
diff changeset
691 for (uptr i = 0; i < kMop; i++) {
kono
parents:
diff changeset
692 FastState s(thr->racy_state[i]);
kono
parents:
diff changeset
693 ThreadContext *tctx = static_cast<ThreadContext*>(
kono
parents:
diff changeset
694 ctx->thread_registry->GetThreadLocked(s.tid()));
kono
parents:
diff changeset
695 if (s.epoch() < tctx->epoch0 || s.epoch() > tctx->epoch1)
kono
parents:
diff changeset
696 continue;
kono
parents:
diff changeset
697 rep.AddThread(tctx);
kono
parents:
diff changeset
698 }
kono
parents:
diff changeset
699
kono
parents:
diff changeset
700 rep.AddLocation(addr_min, addr_max - addr_min);
kono
parents:
diff changeset
701
kono
parents:
diff changeset
702 #if !SANITIZER_GO
kono
parents:
diff changeset
703 { // NOLINT
kono
parents:
diff changeset
704 Shadow s(thr->racy_state[1]);
kono
parents:
diff changeset
705 if (s.epoch() <= thr->last_sleep_clock.get(s.tid()))
kono
parents:
diff changeset
706 rep.AddSleep(thr->last_sleep_stack_id);
kono
parents:
diff changeset
707 }
kono
parents:
diff changeset
708 #endif
kono
parents:
diff changeset
709
kono
parents:
diff changeset
710 if (!OutputReport(thr, rep))
kono
parents:
diff changeset
711 return;
kono
parents:
diff changeset
712
kono
parents:
diff changeset
713 AddRacyStacks(thr, traces, addr_min, addr_max);
kono
parents:
diff changeset
714 }
kono
parents:
diff changeset
715
kono
parents:
diff changeset
716 void PrintCurrentStack(ThreadState *thr, uptr pc) {
kono
parents:
diff changeset
717 VarSizeStackTrace trace;
kono
parents:
diff changeset
718 ObtainCurrentStack(thr, pc, &trace);
kono
parents:
diff changeset
719 PrintStack(SymbolizeStack(trace));
kono
parents:
diff changeset
720 }
kono
parents:
diff changeset
721
kono
parents:
diff changeset
722 // Always inlining PrintCurrentStackSlow, because LocatePcInTrace assumes
kono
parents:
diff changeset
723 // __sanitizer_print_stack_trace exists in the actual unwinded stack, but
kono
parents:
diff changeset
724 // tail-call to PrintCurrentStackSlow breaks this assumption because
kono
parents:
diff changeset
725 // __sanitizer_print_stack_trace disappears after tail-call.
kono
parents:
diff changeset
726 // However, this solution is not reliable enough, please see dvyukov's comment
kono
parents:
diff changeset
727 // http://reviews.llvm.org/D19148#406208
kono
parents:
diff changeset
728 // Also see PR27280 comment 2 and 3 for breaking examples and analysis.
kono
parents:
diff changeset
729 ALWAYS_INLINE
kono
parents:
diff changeset
730 void PrintCurrentStackSlow(uptr pc) {
kono
parents:
diff changeset
731 #if !SANITIZER_GO
kono
parents:
diff changeset
732 BufferedStackTrace *ptrace =
kono
parents:
diff changeset
733 new(internal_alloc(MBlockStackTrace, sizeof(BufferedStackTrace)))
kono
parents:
diff changeset
734 BufferedStackTrace();
kono
parents:
diff changeset
735 ptrace->Unwind(kStackTraceMax, pc, 0, 0, 0, 0, false);
kono
parents:
diff changeset
736 for (uptr i = 0; i < ptrace->size / 2; i++) {
kono
parents:
diff changeset
737 uptr tmp = ptrace->trace_buffer[i];
kono
parents:
diff changeset
738 ptrace->trace_buffer[i] = ptrace->trace_buffer[ptrace->size - i - 1];
kono
parents:
diff changeset
739 ptrace->trace_buffer[ptrace->size - i - 1] = tmp;
kono
parents:
diff changeset
740 }
kono
parents:
diff changeset
741 PrintStack(SymbolizeStack(*ptrace));
kono
parents:
diff changeset
742 #endif
kono
parents:
diff changeset
743 }
kono
parents:
diff changeset
744
kono
parents:
diff changeset
745 } // namespace __tsan
kono
parents:
diff changeset
746
kono
parents:
diff changeset
747 using namespace __tsan;
kono
parents:
diff changeset
748
kono
parents:
diff changeset
749 extern "C" {
kono
parents:
diff changeset
750 SANITIZER_INTERFACE_ATTRIBUTE
kono
parents:
diff changeset
751 void __sanitizer_print_stack_trace() {
kono
parents:
diff changeset
752 PrintCurrentStackSlow(StackTrace::GetCurrentPc());
kono
parents:
diff changeset
753 }
kono
parents:
diff changeset
754 } // extern "C"