]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | //===-- ubsan_diag.cc -----------------------------------------------------===// |
2 | // | |
3 | // The LLVM Compiler Infrastructure | |
4 | // | |
5 | // This file is distributed under the University of Illinois Open Source | |
6 | // License. See LICENSE.TXT for details. | |
7 | // | |
8 | //===----------------------------------------------------------------------===// | |
9 | // | |
10 | // Diagnostic reporting for the UBSan runtime. | |
11 | // | |
12 | //===----------------------------------------------------------------------===// | |
13 | ||
92a42be0 SL |
14 | #include "ubsan_platform.h" |
15 | #if CAN_SANITIZE_UB | |
1a4d82fc | 16 | #include "ubsan_diag.h" |
92a42be0 SL |
17 | #include "ubsan_init.h" |
18 | #include "ubsan_flags.h" | |
19 | #include "sanitizer_common/sanitizer_placement_new.h" | |
1a4d82fc JJ |
20 | #include "sanitizer_common/sanitizer_report_decorator.h" |
21 | #include "sanitizer_common/sanitizer_stacktrace.h" | |
92a42be0 SL |
22 | #include "sanitizer_common/sanitizer_stacktrace_printer.h" |
23 | #include "sanitizer_common/sanitizer_suppressions.h" | |
1a4d82fc JJ |
24 | #include "sanitizer_common/sanitizer_symbolizer.h" |
25 | #include <stdio.h> | |
26 | ||
27 | using namespace __ubsan; | |
28 | ||
92a42be0 SL |
29 | static void MaybePrintStackTrace(uptr pc, uptr bp) { |
30 | // We assume that flags are already parsed, as UBSan runtime | |
31 | // will definitely be called when we print the first diagnostics message. | |
32 | if (!flags()->print_stacktrace) | |
33 | return; | |
34 | // We can only use slow unwind, as we don't have any information about stack | |
35 | // top/bottom. | |
36 | // FIXME: It's better to respect "fast_unwind_on_fatal" runtime flag and | |
37 | // fetch stack top/bottom information if we have it (e.g. if we're running | |
38 | // under ASan). | |
39 | if (StackTrace::WillUseFastUnwind(false)) | |
40 | return; | |
41 | BufferedStackTrace stack; | |
42 | stack.Unwind(kStackTraceMax, pc, bp, 0, 0, 0, false); | |
43 | stack.Print(); | |
1a4d82fc JJ |
44 | } |
45 | ||
92a42be0 SL |
46 | static const char *ConvertTypeToString(ErrorType Type) { |
47 | switch (Type) { | |
48 | #define UBSAN_CHECK(Name, SummaryKind, FlagName) \ | |
49 | case ErrorType::Name: \ | |
50 | return SummaryKind; | |
51 | #include "ubsan_checks.inc" | |
52 | #undef UBSAN_CHECK | |
53 | } | |
54 | UNREACHABLE("unknown ErrorType!"); | |
1a4d82fc JJ |
55 | } |
56 | ||
92a42be0 SL |
57 | static void MaybeReportErrorSummary(Location Loc, ErrorType Type) { |
58 | if (!common_flags()->print_summary) | |
59 | return; | |
60 | if (!flags()->report_error_type) | |
61 | Type = ErrorType::GenericUB; | |
62 | const char *ErrorKind = ConvertTypeToString(Type); | |
63 | if (Loc.isSourceLocation()) { | |
64 | SourceLocation SLoc = Loc.getSourceLocation(); | |
65 | if (!SLoc.isInvalid()) { | |
66 | AddressInfo AI; | |
67 | AI.file = internal_strdup(SLoc.getFilename()); | |
68 | AI.line = SLoc.getLine(); | |
69 | AI.column = SLoc.getColumn(); | |
70 | AI.function = internal_strdup(""); // Avoid printing ?? as function name. | |
71 | ReportErrorSummary(ErrorKind, AI); | |
72 | AI.Clear(); | |
73 | return; | |
74 | } | |
75 | } else if (Loc.isSymbolizedStack()) { | |
76 | const AddressInfo &AI = Loc.getSymbolizedStack()->info; | |
77 | ReportErrorSummary(ErrorKind, AI); | |
78 | return; | |
79 | } | |
80 | ReportErrorSummary(ErrorKind); | |
81 | } | |
1a4d82fc | 82 | |
92a42be0 SL |
83 | namespace { |
84 | class Decorator : public SanitizerCommonDecorator { | |
85 | public: | |
86 | Decorator() : SanitizerCommonDecorator() {} | |
87 | const char *Highlight() const { return Green(); } | |
88 | const char *EndHighlight() const { return Default(); } | |
89 | const char *Note() const { return Black(); } | |
90 | const char *EndNote() const { return Default(); } | |
91 | }; | |
92 | } | |
1a4d82fc | 93 | |
92a42be0 SL |
94 | SymbolizedStack *__ubsan::getSymbolizedLocation(uptr PC) { |
95 | InitAsStandaloneIfNecessary(); | |
96 | return Symbolizer::GetOrInit()->SymbolizePC(PC); | |
1a4d82fc JJ |
97 | } |
98 | ||
99 | Diag &Diag::operator<<(const TypeDescriptor &V) { | |
100 | return AddArg(V.getTypeName()); | |
101 | } | |
102 | ||
103 | Diag &Diag::operator<<(const Value &V) { | |
104 | if (V.getType().isSignedIntegerTy()) | |
105 | AddArg(V.getSIntValue()); | |
106 | else if (V.getType().isUnsignedIntegerTy()) | |
107 | AddArg(V.getUIntValue()); | |
108 | else if (V.getType().isFloatTy()) | |
109 | AddArg(V.getFloatValue()); | |
110 | else | |
111 | AddArg("<unknown>"); | |
112 | return *this; | |
113 | } | |
114 | ||
115 | /// Hexadecimal printing for numbers too large for Printf to handle directly. | |
116 | static void PrintHex(UIntMax Val) { | |
117 | #if HAVE_INT128_T | |
118 | Printf("0x%08x%08x%08x%08x", | |
119 | (unsigned int)(Val >> 96), | |
120 | (unsigned int)(Val >> 64), | |
121 | (unsigned int)(Val >> 32), | |
122 | (unsigned int)(Val)); | |
123 | #else | |
124 | UNREACHABLE("long long smaller than 64 bits?"); | |
125 | #endif | |
126 | } | |
127 | ||
128 | static void renderLocation(Location Loc) { | |
129 | InternalScopedString LocBuffer(1024); | |
130 | switch (Loc.getKind()) { | |
131 | case Location::LK_Source: { | |
132 | SourceLocation SLoc = Loc.getSourceLocation(); | |
133 | if (SLoc.isInvalid()) | |
134 | LocBuffer.append("<unknown>"); | |
135 | else | |
92a42be0 SL |
136 | RenderSourceLocation(&LocBuffer, SLoc.getFilename(), SLoc.getLine(), |
137 | SLoc.getColumn(), common_flags()->symbolize_vs_style, | |
138 | common_flags()->strip_path_prefix); | |
1a4d82fc JJ |
139 | break; |
140 | } | |
1a4d82fc JJ |
141 | case Location::LK_Memory: |
142 | LocBuffer.append("%p", Loc.getMemoryLocation()); | |
143 | break; | |
92a42be0 SL |
144 | case Location::LK_Symbolized: { |
145 | const AddressInfo &Info = Loc.getSymbolizedStack()->info; | |
146 | if (Info.file) { | |
147 | RenderSourceLocation(&LocBuffer, Info.file, Info.line, Info.column, | |
148 | common_flags()->symbolize_vs_style, | |
149 | common_flags()->strip_path_prefix); | |
150 | } else if (Info.module) { | |
151 | RenderModuleLocation(&LocBuffer, Info.module, Info.module_offset, | |
152 | common_flags()->strip_path_prefix); | |
153 | } else { | |
154 | LocBuffer.append("%p", Info.address); | |
155 | } | |
156 | break; | |
157 | } | |
1a4d82fc JJ |
158 | case Location::LK_Null: |
159 | LocBuffer.append("<unknown>"); | |
160 | break; | |
161 | } | |
162 | Printf("%s:", LocBuffer.data()); | |
163 | } | |
164 | ||
165 | static void renderText(const char *Message, const Diag::Arg *Args) { | |
166 | for (const char *Msg = Message; *Msg; ++Msg) { | |
167 | if (*Msg != '%') { | |
168 | char Buffer[64]; | |
169 | unsigned I; | |
170 | for (I = 0; Msg[I] && Msg[I] != '%' && I != 63; ++I) | |
171 | Buffer[I] = Msg[I]; | |
172 | Buffer[I] = '\0'; | |
173 | Printf(Buffer); | |
174 | Msg += I - 1; | |
175 | } else { | |
176 | const Diag::Arg &A = Args[*++Msg - '0']; | |
177 | switch (A.Kind) { | |
178 | case Diag::AK_String: | |
179 | Printf("%s", A.String); | |
180 | break; | |
92a42be0 SL |
181 | case Diag::AK_TypeName: { |
182 | if (SANITIZER_WINDOWS) | |
183 | // The Windows implementation demangles names early. | |
184 | Printf("'%s'", A.String); | |
185 | else | |
186 | Printf("'%s'", Symbolizer::GetOrInit()->Demangle(A.String)); | |
1a4d82fc JJ |
187 | break; |
188 | } | |
189 | case Diag::AK_SInt: | |
190 | // 'long long' is guaranteed to be at least 64 bits wide. | |
191 | if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX) | |
192 | Printf("%lld", (long long)A.SInt); | |
193 | else | |
194 | PrintHex(A.SInt); | |
195 | break; | |
196 | case Diag::AK_UInt: | |
197 | if (A.UInt <= UINT64_MAX) | |
198 | Printf("%llu", (unsigned long long)A.UInt); | |
199 | else | |
200 | PrintHex(A.UInt); | |
201 | break; | |
202 | case Diag::AK_Float: { | |
203 | // FIXME: Support floating-point formatting in sanitizer_common's | |
204 | // printf, and stop using snprintf here. | |
205 | char Buffer[32]; | |
92a42be0 SL |
206 | #if SANITIZER_WINDOWS |
207 | sprintf_s(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float); | |
208 | #else | |
1a4d82fc | 209 | snprintf(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float); |
92a42be0 | 210 | #endif |
1a4d82fc JJ |
211 | Printf("%s", Buffer); |
212 | break; | |
213 | } | |
214 | case Diag::AK_Pointer: | |
215 | Printf("%p", A.Pointer); | |
216 | break; | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | /// Find the earliest-starting range in Ranges which ends after Loc. | |
223 | static Range *upperBound(MemoryLocation Loc, Range *Ranges, | |
224 | unsigned NumRanges) { | |
225 | Range *Best = 0; | |
226 | for (unsigned I = 0; I != NumRanges; ++I) | |
227 | if (Ranges[I].getEnd().getMemoryLocation() > Loc && | |
228 | (!Best || | |
229 | Best->getStart().getMemoryLocation() > | |
230 | Ranges[I].getStart().getMemoryLocation())) | |
231 | Best = &Ranges[I]; | |
232 | return Best; | |
233 | } | |
234 | ||
92a42be0 SL |
235 | static inline uptr subtractNoOverflow(uptr LHS, uptr RHS) { |
236 | return (LHS < RHS) ? 0 : LHS - RHS; | |
237 | } | |
238 | ||
239 | static inline uptr addNoOverflow(uptr LHS, uptr RHS) { | |
240 | const uptr Limit = (uptr)-1; | |
241 | return (LHS > Limit - RHS) ? Limit : LHS + RHS; | |
242 | } | |
243 | ||
1a4d82fc | 244 | /// Render a snippet of the address space near a location. |
92a42be0 | 245 | static void renderMemorySnippet(const Decorator &Decor, MemoryLocation Loc, |
1a4d82fc JJ |
246 | Range *Ranges, unsigned NumRanges, |
247 | const Diag::Arg *Args) { | |
1a4d82fc | 248 | // Show at least the 8 bytes surrounding Loc. |
92a42be0 SL |
249 | const unsigned MinBytesNearLoc = 4; |
250 | MemoryLocation Min = subtractNoOverflow(Loc, MinBytesNearLoc); | |
251 | MemoryLocation Max = addNoOverflow(Loc, MinBytesNearLoc); | |
252 | MemoryLocation OrigMin = Min; | |
1a4d82fc JJ |
253 | for (unsigned I = 0; I < NumRanges; ++I) { |
254 | Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min); | |
255 | Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max); | |
256 | } | |
257 | ||
258 | // If we have too many interesting bytes, prefer to show bytes after Loc. | |
92a42be0 | 259 | const unsigned BytesToShow = 32; |
1a4d82fc | 260 | if (Max - Min > BytesToShow) |
92a42be0 SL |
261 | Min = __sanitizer::Min(Max - BytesToShow, OrigMin); |
262 | Max = addNoOverflow(Min, BytesToShow); | |
263 | ||
264 | if (!IsAccessibleMemoryRange(Min, Max - Min)) { | |
265 | Printf("<memory cannot be printed>\n"); | |
266 | return; | |
267 | } | |
1a4d82fc JJ |
268 | |
269 | // Emit data. | |
270 | for (uptr P = Min; P != Max; ++P) { | |
1a4d82fc JJ |
271 | unsigned char C = *reinterpret_cast<const unsigned char*>(P); |
272 | Printf("%s%02x", (P % 8 == 0) ? " " : " ", C); | |
273 | } | |
274 | Printf("\n"); | |
275 | ||
276 | // Emit highlights. | |
92a42be0 | 277 | Printf(Decor.Highlight()); |
1a4d82fc JJ |
278 | Range *InRange = upperBound(Min, Ranges, NumRanges); |
279 | for (uptr P = Min; P != Max; ++P) { | |
280 | char Pad = ' ', Byte = ' '; | |
281 | if (InRange && InRange->getEnd().getMemoryLocation() == P) | |
282 | InRange = upperBound(P, Ranges, NumRanges); | |
283 | if (!InRange && P > Loc) | |
284 | break; | |
285 | if (InRange && InRange->getStart().getMemoryLocation() < P) | |
286 | Pad = '~'; | |
287 | if (InRange && InRange->getStart().getMemoryLocation() <= P) | |
288 | Byte = '~'; | |
289 | char Buffer[] = { Pad, Pad, P == Loc ? '^' : Byte, Byte, 0 }; | |
290 | Printf((P % 8 == 0) ? Buffer : &Buffer[1]); | |
291 | } | |
92a42be0 | 292 | Printf("%s\n", Decor.EndHighlight()); |
1a4d82fc JJ |
293 | |
294 | // Go over the line again, and print names for the ranges. | |
295 | InRange = 0; | |
296 | unsigned Spaces = 0; | |
297 | for (uptr P = Min; P != Max; ++P) { | |
298 | if (!InRange || InRange->getEnd().getMemoryLocation() == P) | |
299 | InRange = upperBound(P, Ranges, NumRanges); | |
300 | if (!InRange) | |
301 | break; | |
302 | ||
303 | Spaces += (P % 8) == 0 ? 2 : 1; | |
304 | ||
305 | if (InRange && InRange->getStart().getMemoryLocation() == P) { | |
306 | while (Spaces--) | |
307 | Printf(" "); | |
308 | renderText(InRange->getText(), Args); | |
309 | Printf("\n"); | |
310 | // FIXME: We only support naming one range for now! | |
311 | break; | |
312 | } | |
313 | ||
314 | Spaces += 2; | |
315 | } | |
316 | ||
317 | // FIXME: Print names for anything we can identify within the line: | |
318 | // | |
319 | // * If we can identify the memory itself as belonging to a particular | |
320 | // global, stack variable, or dynamic allocation, then do so. | |
321 | // | |
322 | // * If we have a pointer-size, pointer-aligned range highlighted, | |
323 | // determine whether the value of that range is a pointer to an | |
324 | // entity which we can name, and if so, print that name. | |
325 | // | |
326 | // This needs an external symbolizer, or (preferably) ASan instrumentation. | |
327 | } | |
328 | ||
329 | Diag::~Diag() { | |
92a42be0 SL |
330 | // All diagnostics should be printed under report mutex. |
331 | CommonSanitizerReportMutex.CheckLocked(); | |
332 | Decorator Decor; | |
1a4d82fc JJ |
333 | Printf(Decor.Bold()); |
334 | ||
335 | renderLocation(Loc); | |
336 | ||
337 | switch (Level) { | |
338 | case DL_Error: | |
339 | Printf("%s runtime error: %s%s", | |
92a42be0 | 340 | Decor.Warning(), Decor.EndWarning(), Decor.Bold()); |
1a4d82fc JJ |
341 | break; |
342 | ||
343 | case DL_Note: | |
92a42be0 | 344 | Printf("%s note: %s", Decor.Note(), Decor.EndNote()); |
1a4d82fc JJ |
345 | break; |
346 | } | |
347 | ||
348 | renderText(Message, Args); | |
349 | ||
350 | Printf("%s\n", Decor.Default()); | |
351 | ||
352 | if (Loc.isMemoryLocation()) | |
353 | renderMemorySnippet(Decor, Loc.getMemoryLocation(), Ranges, | |
354 | NumRanges, Args); | |
355 | } | |
92a42be0 SL |
356 | |
357 | ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc, | |
358 | ErrorType Type) | |
359 | : Opts(Opts), SummaryLoc(SummaryLoc), Type(Type) { | |
360 | InitAsStandaloneIfNecessary(); | |
361 | CommonSanitizerReportMutex.Lock(); | |
362 | } | |
363 | ||
364 | ScopedReport::~ScopedReport() { | |
365 | MaybePrintStackTrace(Opts.pc, Opts.bp); | |
366 | MaybeReportErrorSummary(SummaryLoc, Type); | |
367 | CommonSanitizerReportMutex.Unlock(); | |
368 | if (Opts.DieAfterReport || flags()->halt_on_error) | |
369 | Die(); | |
370 | } | |
371 | ||
372 | ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)]; | |
373 | static SuppressionContext *suppression_ctx = nullptr; | |
374 | static const char kVptrCheck[] = "vptr_check"; | |
375 | static const char *kSuppressionTypes[] = { kVptrCheck }; | |
376 | ||
377 | void __ubsan::InitializeSuppressions() { | |
378 | CHECK_EQ(nullptr, suppression_ctx); | |
379 | suppression_ctx = new (suppression_placeholder) // NOLINT | |
380 | SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes)); | |
381 | suppression_ctx->ParseFromFile(flags()->suppressions); | |
382 | } | |
383 | ||
384 | bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) { | |
385 | InitAsStandaloneIfNecessary(); | |
386 | CHECK(suppression_ctx); | |
387 | Suppression *s; | |
388 | return suppression_ctx->Match(TypeName, kVptrCheck, &s); | |
389 | } | |
390 | ||
391 | #endif // CAN_SANITIZE_UB |