1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Host ConcurrencySanitizer report generation.
11///
12//===----------------------------------------------------------------------===//
13
14#include "csan.h"
15
16#include "sanitizer_common/sanitizer_common.h"
17#include "sanitizer_common/sanitizer_flags.h"
18#include "sanitizer_common/sanitizer_libc.h"
19#include "sanitizer_common/sanitizer_mutex.h"
20#include "sanitizer_common/sanitizer_report_decorator.h"
21#include "sanitizer_common/sanitizer_stacktrace.h"
22#include "sanitizer_common/sanitizer_symbolizer.h"
23
24using namespace __sanitizer;
25
26namespace __sanitizer {
27void BufferedStackTrace::UnwindImpl(uptr pc, uptr bp, void *context,
28 bool request_fast, u32 max_depth) {
29 uptr top = 0;
30 uptr bottom = 0;
31 GetThreadStackTopAndBottom(at_initialization: false, stack_top: &top, stack_bottom: &bottom);
32 bool fast = StackTrace::WillUseFastUnwind(request_fast_unwind: request_fast);
33 Unwind(max_depth, pc, bp, context, stack_top: top, stack_bottom: bottom, request_fast_unwind: fast);
34}
35} // namespace __sanitizer
36
37namespace __csan {
38namespace {
39
40class Decorator : public SanitizerCommonDecorator {
41public:
42 const char *Access() { return Blue(); }
43 const char *Location() { return Green(); }
44};
45
46// A pair of racing program counter values to deduplicate and check.
47struct RacyPcs {
48 uptr pc[2];
49
50 bool operator==(const RacyPcs &Other) const {
51 if (pc[0] == Other.pc[0] && pc[1] == Other.pc[1])
52 return true;
53 return pc[0] == Other.pc[1] && pc[1] == Other.pc[0];
54 }
55};
56InternalMmapVectorNoCtor<RacyPcs> RacyPcsSeen;
57
58struct PeerInfo {
59 uptr pc;
60 uptr size;
61 int access_type;
62};
63
64bool HandleRacyPcs(uptr PC, uptr PeerPC) {
65 const RacyPcs Racy = {.pc: {PC, PeerPC}};
66 for (uptr I = 0; I < RacyPcsSeen.size(); ++I) {
67 if (Racy == RacyPcsSeen[I]) {
68 VReport(2, "%s: suppressing report as doubled\n", SanitizerToolName);
69 return true;
70 }
71 }
72 RacyPcsSeen.push_back(element: Racy);
73 return false;
74}
75
76const char *AccessKind(int Type) {
77 const bool Write = Type & CSAN_ACCESS_WRITE;
78 const bool Atomic = Type & CSAN_ACCESS_ATOMIC;
79 const bool Compound = Type & CSAN_ACCESS_COMPOUND;
80 if (Compound && Write)
81 return Atomic ? "read-write (atomic)" : "read-write";
82 if (Write)
83 return Atomic ? "write (atomic)" : "write";
84 return Atomic ? "read (atomic)" : "read";
85}
86
87const char *MemOpDesc(bool First, int Type) {
88 const bool Write = Type & CSAN_ACCESS_WRITE;
89 const bool Atomic = Type & CSAN_ACCESS_ATOMIC;
90 const bool Compound = Type & CSAN_ACCESS_COMPOUND;
91 if (Compound && Write)
92 return Atomic ? (First ? "Read-write (atomic)"
93 : "Previous read-write (atomic)")
94 : (First ? "Read-write" : "Previous read-write");
95 if (Write)
96 return Atomic ? (First ? "Write (atomic)" : "Previous write (atomic)")
97 : (First ? "Write" : "Previous write");
98 return Atomic ? (First ? "Read (atomic)" : "Previous read (atomic)")
99 : (First ? "Read" : "Previous read");
100}
101
102void CaptureStack(uptr PC, uptr BP, uptr *Out, u32 *N) {
103 UNINITIALIZED BufferedStackTrace Stack;
104 Stack.Unwind(pc: PC, bp: BP, context: nullptr, request_fast: common_flags()->fast_unwind_on_fatal,
105 max_depth: kMaxStackFrames);
106 *N = Stack.size > kMaxStackFrames ? kMaxStackFrames : Stack.size;
107 if (*N)
108 internal_memcpy(dest: Out, src: Stack.trace, n: *N * sizeof(uptr));
109}
110
111SymbolizedStack *SymbolizeFrame(uptr PC) {
112 return Symbolizer::GetOrInit()->SymbolizePC(
113 address: StackTrace::GetPreviousInstructionPc(pc: PC));
114}
115
116u32 SkipRuntimeFrames(const uptr *PCs, u32 N) {
117 for (u32 I = 0; I < N; ++I) {
118 SymbolizedStack *Frames = SymbolizeFrame(PC: PCs[I]);
119 const bool User = Frames && SkipInternalFrames(frames: Frames);
120 if (Frames)
121 Frames->ClearAll();
122 if (User)
123 return I;
124 }
125 return 0;
126}
127
128void CopyFuncName(uptr PC, InternalScopedString *Out) {
129 SymbolizedStack *Frames = SymbolizeFrame(PC);
130 const SymbolizedStack *User = Frames ? SkipInternalFrames(frames: Frames) : nullptr;
131 if (!User)
132 User = Frames;
133 if (User && User->info.function && User->info.function[0])
134 Out->Append(str: User->info.function);
135 else
136 Out->AppendF(format: "%p", (void *)PC);
137 if (Frames)
138 Frames->ClearAll();
139}
140
141void PrintFrames(const uptr *PCs, u32 N) {
142 if (!N) {
143 Printf(format: " [failed to restore the stack]\n\n");
144 return;
145 }
146 const u32 Skip = SkipRuntimeFrames(PCs, N);
147 StackTrace Trace(PCs + Skip, N - Skip);
148 Trace.Print();
149}
150
151void PrintLocation(uptr Addr) {
152 DataInfo Loc;
153 if (!Symbolizer::GetOrInit()->SymbolizeData(address: Addr, info: &Loc) || !Loc.name ||
154 !Loc.name[0] || Loc.name[0] == '?') {
155 Loc.Clear();
156 return;
157 }
158 Decorator D;
159 Printf(format: "%s", D.Location());
160 if (Loc.size)
161 Printf(format: " Location is global '%s' of size %zu at %p\n", Loc.name, Loc.size,
162 (void *)Addr);
163 else
164 Printf(format: " Location is global '%s' at %p\n", Loc.name, (void *)Addr);
165 Printf(format: "%s", D.Default());
166 Loc.Clear();
167}
168
169void PrintHexValue(u64 V, uptr Size) {
170 switch (Size) {
171 case 1:
172 Printf(format: "0x%02llx", (unsigned long long)V);
173 break;
174 case 2:
175 Printf(format: "0x%04llx", (unsigned long long)V);
176 break;
177 case 4:
178 Printf(format: "0x%08llx", (unsigned long long)V);
179 break;
180 default:
181 Printf(format: "0x%016llx", (unsigned long long)V);
182 break;
183 }
184}
185
186void PrintValueChange(uptr Size, u64 Old, u64 New) {
187 if (Size == 0 || Size > 8 || Old == New)
188 return;
189 Printf(format: " value changed: ");
190 PrintHexValue(V: Old, Size);
191 Printf(format: " -> ");
192 PrintHexValue(V: New, Size);
193 Printf(format: "\n");
194}
195
196void PrintReport(const AccessInfo &AI, const PeerInfo *Other, u64 Old,
197 u64 New) {
198 if (HandleRacyPcs(PC: AI.pc, PeerPC: Other ? Other->pc : 0))
199 return;
200
201 UNINITIALIZED uptr ThisStack[kMaxStackFrames];
202 u32 ThisN = 0;
203 CaptureStack(PC: AI.pc, BP: AI.bp, Out: ThisStack, N: &ThisN);
204
205 const u32 ThisSkip = SkipRuntimeFrames(PCs: ThisStack, N: ThisN);
206 const uptr ThisFrame = ThisN ? ThisStack[ThisSkip] : AI.pc;
207 const uptr OtherFrame = Other ? Other->pc : 0;
208
209 RecordDataRace();
210
211 Decorator D;
212 InternalScopedString ThisFn;
213 CopyFuncName(PC: ThisFrame, Out: &ThisFn);
214
215 Printf(format: "==================\n");
216 Printf(format: "%s", D.Warning());
217 if (Other) {
218 InternalScopedString OtherFn;
219 CopyFuncName(PC: OtherFrame, Out: &OtherFn);
220 const int Cmp = internal_strcmp(s1: OtherFn.data(), s2: ThisFn.data());
221 Printf(format: "WARNING: ConcurrencySanitizer: data race in %s / %s\n",
222 Cmp < 0 ? OtherFn.data() : ThisFn.data(),
223 Cmp < 0 ? ThisFn.data() : OtherFn.data());
224 } else {
225 Printf(format: "WARNING: ConcurrencySanitizer: data race of unknown origin in %s\n",
226 ThisFn.data());
227 }
228 Printf(format: "%s", D.Default());
229
230 Printf(format: "%s", D.Access());
231 if (Other) {
232 Printf(format: " %s of size %zu at %p by thread %u:\n",
233 MemOpDesc(First: true, Type: AI.access_type), AI.size, AI.ptr, AI.tid);
234 Printf(format: "%s", D.Default());
235 PrintFrames(PCs: ThisStack, N: ThisN);
236
237 Printf(format: "%s", D.Access());
238 Printf(format: " %s of size %zu at %p:\n", MemOpDesc(First: false, Type: Other->access_type),
239 Other->size, AI.ptr);
240 Printf(format: "%s", D.Default());
241 PrintFrames(PCs: &Other->pc, N: 1);
242 } else {
243 Printf(
244 format: " race at unknown origin, with %s of size %zu at %p by thread %u:\n",
245 AccessKind(Type: AI.access_type), AI.size, AI.ptr, AI.tid);
246 Printf(format: "%s", D.Default());
247 PrintFrames(PCs: ThisStack, N: ThisN);
248 }
249
250 PrintValueChange(Size: AI.size, Old, New);
251 PrintLocation(Addr: (uptr)AI.ptr);
252
253 if (ThisN) {
254 StackTrace Summary(ThisStack + ThisSkip, ThisN - ThisSkip);
255 ReportErrorSummary(error_type: Other ? "data race" : "data race of unknown origin",
256 trace: &Summary);
257 }
258 Printf(format: "==================\n");
259 if (flags()->halt_on_error)
260 Die();
261}
262
263} // namespace
264
265void ReportKnownOrigin(const AccessInfo &AI, ValueChange VC, uptr PeerPC,
266 int PeerAccess, uptr PeerSize, u64 Old, u64 New) {
267 ScopedErrorReportLock L;
268 if (VC == kValueChangeFalse)
269 return;
270 const PeerInfo Peer = {.pc: PeerPC, .size: PeerSize, .access_type: PeerAccess};
271 PrintReport(AI, Other: &Peer, Old, New);
272}
273
274void ReportUnknownOrigin(const AccessInfo &AI, u64 Old, u64 New) {
275 ScopedErrorReportLock L;
276 PrintReport(AI, Other: nullptr, Old, New);
277}
278
279} // namespace __csan
280