Skip to content

Commit 26f9166

Browse files
authored
[TSan][compiler-rt] Defer symbolization of Reports to as late as possible (#151120)
This is the refactoring portion of: #149516. My aim is for this change to replicate current behaviour - just with Symbolization done explicitly (and later than previously). This change will enable us to perform symboliaztion after releasing the locks in `OutputReport`; this is necessary on Apple platforms in order to avoid a deadlock.
1 parent 6be3cc5 commit 26f9166

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

compiler-rt/lib/tsan/rtl/tsan_report.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#ifndef TSAN_REPORT_H
1313
#define TSAN_REPORT_H
1414

15+
#include "sanitizer_common/sanitizer_internal_defs.h"
16+
#include "sanitizer_common/sanitizer_stacktrace.h"
1517
#include "sanitizer_common/sanitizer_symbolizer.h"
1618
#include "sanitizer_common/sanitizer_thread_registry.h"
1719
#include "sanitizer_common/sanitizer_vector.h"
@@ -56,6 +58,7 @@ struct ReportMop {
5658
bool atomic;
5759
uptr external_tag;
5860
Vector<ReportMopMutex> mset;
61+
StackTrace stack_trace;
5962
ReportStack *stack;
6063

6164
ReportMop();
@@ -79,6 +82,7 @@ struct ReportLocation {
7982
int fd = 0;
8083
bool fd_closed = false;
8184
bool suppressable = false;
85+
StackID stack_id = 0;
8286
ReportStack *stack = nullptr;
8387
};
8488

@@ -89,22 +93,31 @@ struct ReportThread {
8993
ThreadType thread_type;
9094
char *name;
9195
Tid parent_tid;
96+
StackID stack_id;
9297
ReportStack *stack;
98+
bool suppressable;
9399
};
94100

95101
struct ReportMutex {
96102
int id;
97103
uptr addr;
104+
StackID stack_id;
98105
ReportStack *stack;
99106
};
100107

108+
struct AddedLocationAddr {
109+
uptr addr;
110+
usize locs_idx;
111+
};
112+
101113
class ReportDesc {
102114
public:
103115
ReportType typ;
104116
uptr tag;
105117
Vector<ReportStack*> stacks;
106118
Vector<ReportMop*> mops;
107119
Vector<ReportLocation*> locs;
120+
Vector<AddedLocationAddr> added_location_addrs;
108121
Vector<ReportMutex*> mutexes;
109122
Vector<ReportThread*> threads;
110123
Vector<Tid> unique_tids;

compiler-rt/lib/tsan/rtl/tsan_rtl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ class ScopedReportBase {
420420
void AddSleep(StackID stack_id);
421421
void SetCount(int count);
422422
void SetSigNum(int sig);
423+
void SymbolizeStackElems(void);
423424

424425
const ReportDesc *GetReport() const;
425426

@@ -498,7 +499,7 @@ void ForkChildAfter(ThreadState *thr, uptr pc, bool start_thread);
498499

499500
void ReportRace(ThreadState *thr, RawShadow *shadow_mem, Shadow cur, Shadow old,
500501
AccessType typ);
501-
bool OutputReport(ThreadState *thr, const ScopedReport &srep);
502+
bool OutputReport(ThreadState *thr, ScopedReport &srep);
502503
bool IsFiredSuppression(Context *ctx, ReportType type, StackTrace trace);
503504
bool IsExpectedReport(uptr addr, uptr size);
504505

compiler-rt/lib/tsan/rtl/tsan_rtl_mutex.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,15 @@ void ReportDeadlock(ThreadState *thr, uptr pc, DDReport *r) {
539539
for (int i = 0; i < r->n; i++) {
540540
for (int j = 0; j < (flags()->second_deadlock_stack ? 2 : 1); j++) {
541541
u32 stk = r->loop[i].stk[j];
542+
StackTrace stack;
542543
if (stk && stk != kInvalidStackID) {
543-
rep.AddStack(StackDepotGet(stk), true);
544+
stack = StackDepotGet(stk);
544545
} else {
545546
// Sometimes we fail to extract the stack trace (FIXME: investigate),
546547
// but we should still produce some stack trace in the report.
547-
rep.AddStack(StackTrace(&dummy_pc, 1), true);
548+
stack = StackTrace(&dummy_pc, 1);
548549
}
550+
rep.AddStack(stack, true);
549551
}
550552
}
551553
OutputReport(thr, rep);

compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "sanitizer_common/sanitizer_common.h"
14+
#include "sanitizer_common/sanitizer_internal_defs.h"
1415
#include "sanitizer_common/sanitizer_libc.h"
1516
#include "sanitizer_common/sanitizer_placement_new.h"
1617
#include "sanitizer_common/sanitizer_stackdepot.h"
@@ -187,10 +188,8 @@ void ScopedReportBase::AddMemoryAccess(uptr addr, uptr external_tag, Shadow s,
187188
mop->size = size;
188189
mop->write = !(typ & kAccessRead);
189190
mop->atomic = typ & kAccessAtomic;
190-
mop->stack = SymbolizeStack(stack);
191191
mop->external_tag = external_tag;
192-
if (mop->stack)
193-
mop->stack->suppressable = true;
192+
mop->stack_trace = stack;
194193
for (uptr i = 0; i < mset->Size(); i++) {
195194
MutexSet::Desc d = mset->Get(i);
196195
int id = this->AddMutex(d.addr, d.stack_id);
@@ -199,6 +198,56 @@ void ScopedReportBase::AddMemoryAccess(uptr addr, uptr external_tag, Shadow s,
199198
}
200199
}
201200

201+
void ScopedReportBase::SymbolizeStackElems() {
202+
// symbolize memory ops
203+
for (usize i = 0, size = rep_->mops.Size(); i < size; i++) {
204+
ReportMop *mop = rep_->mops[i];
205+
mop->stack = SymbolizeStack(mop->stack_trace);
206+
if (mop->stack)
207+
mop->stack->suppressable = true;
208+
}
209+
210+
// symbolize locations
211+
for (usize i = 0, size = rep_->locs.Size(); i < size; i++) {
212+
// added locations have a NULL placeholder - don't dereference them
213+
if (ReportLocation *loc = rep_->locs[i])
214+
loc->stack = SymbolizeStackId(loc->stack_id);
215+
}
216+
217+
// symbolize any added locations
218+
for (usize i = 0, size = rep_->added_location_addrs.Size(); i < size; i++) {
219+
AddedLocationAddr *added_loc = &rep_->added_location_addrs[i];
220+
if (ReportLocation *loc = SymbolizeData(added_loc->addr)) {
221+
loc->suppressable = true;
222+
rep_->locs[added_loc->locs_idx] = loc;
223+
}
224+
}
225+
226+
// Filter out any added ___location placeholders that could not be symbolized
227+
usize j = 0;
228+
for (usize i = 0, size = rep_->locs.Size(); i < size; i++) {
229+
if (rep_->locs[i] != nullptr) {
230+
rep_->locs[j] = rep_->locs[i];
231+
j++;
232+
}
233+
}
234+
rep_->locs.Resize(j);
235+
236+
// symbolize threads
237+
for (usize i = 0, size = rep_->threads.Size(); i < size; i++) {
238+
ReportThread *rt = rep_->threads[i];
239+
rt->stack = SymbolizeStackId(rt->stack_id);
240+
if (rt->stack)
241+
rt->stack->suppressable = rt->suppressable;
242+
}
243+
244+
// symbolize mutexes
245+
for (usize i = 0, size = rep_->mutexes.Size(); i < size; i++) {
246+
ReportMutex *rm = rep_->mutexes[i];
247+
rm->stack = SymbolizeStackId(rm->stack_id);
248+
}
249+
}
250+
202251
void ScopedReportBase::AddUniqueTid(Tid unique_tid) {
203252
rep_->unique_tids.PushBack(unique_tid);
204253
}
@@ -216,10 +265,8 @@ void ScopedReportBase::AddThread(const ThreadContext *tctx, bool suppressable) {
216265
rt->name = internal_strdup(tctx->name);
217266
rt->parent_tid = tctx->parent_tid;
218267
rt->thread_type = tctx->thread_type;
219-
rt->stack = 0;
220-
rt->stack = SymbolizeStackId(tctx->creation_stack_id);
221-
if (rt->stack)
222-
rt->stack->suppressable = suppressable;
268+
rt->stack_id = tctx->creation_stack_id;
269+
rt->suppressable = suppressable;
223270
}
224271

225272
#if !SANITIZER_GO
@@ -270,7 +317,7 @@ int ScopedReportBase::AddMutex(uptr addr, StackID creation_stack_id) {
270317
rep_->mutexes.PushBack(rm);
271318
rm->id = rep_->mutexes.Size() - 1;
272319
rm->addr = addr;
273-
rm->stack = SymbolizeStackId(creation_stack_id);
320+
rm->stack_id = creation_stack_id;
274321
return rm->id;
275322
}
276323

@@ -288,7 +335,7 @@ void ScopedReportBase::AddLocation(uptr addr, uptr size) {
288335
loc->fd_closed = closed;
289336
loc->fd = fd;
290337
loc->tid = creat_tid;
291-
loc->stack = SymbolizeStackId(creat_stack);
338+
loc->stack_id = creat_stack;
292339
rep_->locs.PushBack(loc);
293340
AddThread(creat_tid);
294341
return;
@@ -310,7 +357,7 @@ void ScopedReportBase::AddLocation(uptr addr, uptr size) {
310357
loc->heap_chunk_size = b->siz;
311358
loc->external_tag = b->tag;
312359
loc->tid = b->tid;
313-
loc->stack = SymbolizeStackId(b->stk);
360+
loc->stack_id = b->stk;
314361
rep_->locs.PushBack(loc);
315362
AddThread(b->tid);
316363
return;
@@ -324,11 +371,8 @@ void ScopedReportBase::AddLocation(uptr addr, uptr size) {
324371
AddThread(tctx);
325372
}
326373
#endif
327-
if (ReportLocation *loc = SymbolizeData(addr)) {
328-
loc->suppressable = true;
329-
rep_->locs.PushBack(loc);
330-
return;
331-
}
374+
rep_->added_location_addrs.PushBack({addr, rep_->locs.Size()});
375+
rep_->locs.PushBack(nullptr);
332376
}
333377

334378
#if !SANITIZER_GO
@@ -628,11 +672,12 @@ static bool HandleRacyStacks(ThreadState *thr, VarSizeStackTrace traces[2]) {
628672
return false;
629673
}
630674

631-
bool OutputReport(ThreadState *thr, const ScopedReport &srep) {
675+
bool OutputReport(ThreadState *thr, ScopedReport &srep) {
632676
// These should have been checked in ShouldReport.
633677
// It's too late to check them here, we have already taken locks.
634678
CHECK(flags()->report_bugs);
635679
CHECK(!thr->suppress_reports);
680+
srep.SymbolizeStackElems();
636681
atomic_store_relaxed(&ctx->last_symbolize_time_ns, NanoTime());
637682
const ReportDesc *rep = srep.GetReport();
638683
CHECK_EQ(thr->current_report, nullptr);

0 commit comments

Comments
 (0)