1 //===-- ubsan_diag.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Diagnostic reporting for the UBSan runtime.
12 //===----------------------------------------------------------------------===//
14 #include "ubsan_platform.h"
16 #include "ubsan_diag.h"
17 #include "ubsan_init.h"
18 #include "ubsan_flags.h"
19 #include "sanitizer_common/sanitizer_placement_new.h"
20 #include "sanitizer_common/sanitizer_report_decorator.h"
21 #include "sanitizer_common/sanitizer_stacktrace.h"
22 #include "sanitizer_common/sanitizer_stacktrace_printer.h"
23 #include "sanitizer_common/sanitizer_suppressions.h"
24 #include "sanitizer_common/sanitizer_symbolizer.h"
27 using namespace __ubsan;
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)
34 // We can only use slow unwind, as we don't have any information about stack
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
39 if (StackTrace::WillUseFastUnwind(false))
41 BufferedStackTrace stack;
42 stack.Unwind(kStackTraceMax, pc, bp, 0, 0, 0, false);
46 static const char *ConvertTypeToString(ErrorType Type) {
48 #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
49 case ErrorType::Name: \
51 #include "ubsan_checks.inc"
54 UNREACHABLE("unknown ErrorType!");
57 static const char *ConvertTypeToFlagName(ErrorType Type) {
59 #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
60 case ErrorType::Name: \
61 return FSanitizeFlagName;
62 #include "ubsan_checks.inc"
65 UNREACHABLE("unknown ErrorType!");
68 static void MaybeReportErrorSummary(Location Loc, ErrorType Type) {
69 if (!common_flags()->print_summary)
71 if (!flags()->report_error_type)
72 Type = ErrorType::GenericUB;
73 const char *ErrorKind = ConvertTypeToString(Type);
74 if (Loc.isSourceLocation()) {
75 SourceLocation SLoc = Loc.getSourceLocation();
76 if (!SLoc.isInvalid()) {
78 AI.file = internal_strdup(SLoc.getFilename());
79 AI.line = SLoc.getLine();
80 AI.column = SLoc.getColumn();
81 AI.function = internal_strdup(""); // Avoid printing ?? as function name.
82 ReportErrorSummary(ErrorKind, AI);
86 } else if (Loc.isSymbolizedStack()) {
87 const AddressInfo &AI = Loc.getSymbolizedStack()->info;
88 ReportErrorSummary(ErrorKind, AI);
91 ReportErrorSummary(ErrorKind);
95 class Decorator : public SanitizerCommonDecorator {
97 Decorator() : SanitizerCommonDecorator() {}
98 const char *Highlight() const { return Green(); }
99 const char *EndHighlight() const { return Default(); }
100 const char *Note() const { return Black(); }
101 const char *EndNote() const { return Default(); }
105 SymbolizedStack *__ubsan::getSymbolizedLocation(uptr PC) {
106 InitAsStandaloneIfNecessary();
107 return Symbolizer::GetOrInit()->SymbolizePC(PC);
110 Diag &Diag::operator<<(const TypeDescriptor &V) {
111 return AddArg(V.getTypeName());
114 Diag &Diag::operator<<(const Value &V) {
115 if (V.getType().isSignedIntegerTy())
116 AddArg(V.getSIntValue());
117 else if (V.getType().isUnsignedIntegerTy())
118 AddArg(V.getUIntValue());
119 else if (V.getType().isFloatTy())
120 AddArg(V.getFloatValue());
126 /// Hexadecimal printing for numbers too large for Printf to handle directly.
127 static void RenderHex(InternalScopedString *Buffer, UIntMax Val) {
129 Buffer->append("0x%08x%08x%08x%08x", (unsigned int)(Val >> 96),
130 (unsigned int)(Val >> 64), (unsigned int)(Val >> 32),
131 (unsigned int)(Val));
133 UNREACHABLE("long long smaller than 64 bits?");
137 static void RenderLocation(InternalScopedString *Buffer, Location Loc) {
138 switch (Loc.getKind()) {
139 case Location::LK_Source: {
140 SourceLocation SLoc = Loc.getSourceLocation();
141 if (SLoc.isInvalid())
142 Buffer->append("<unknown>");
144 RenderSourceLocation(Buffer, SLoc.getFilename(), SLoc.getLine(),
145 SLoc.getColumn(), common_flags()->symbolize_vs_style,
146 common_flags()->strip_path_prefix);
149 case Location::LK_Memory:
150 Buffer->append("%p", Loc.getMemoryLocation());
152 case Location::LK_Symbolized: {
153 const AddressInfo &Info = Loc.getSymbolizedStack()->info;
155 RenderSourceLocation(Buffer, Info.file, Info.line, Info.column,
156 common_flags()->symbolize_vs_style,
157 common_flags()->strip_path_prefix);
158 else if (Info.module)
159 RenderModuleLocation(Buffer, Info.module, Info.module_offset,
160 common_flags()->strip_path_prefix);
162 Buffer->append("%p", Info.address);
165 case Location::LK_Null:
166 Buffer->append("<unknown>");
171 static void RenderText(InternalScopedString *Buffer, const char *Message,
172 const Diag::Arg *Args) {
173 for (const char *Msg = Message; *Msg; ++Msg) {
175 Buffer->append("%c", *Msg);
178 const Diag::Arg &A = Args[*++Msg - '0'];
180 case Diag::AK_String:
181 Buffer->append("%s", A.String);
183 case Diag::AK_TypeName: {
184 if (SANITIZER_WINDOWS)
185 // The Windows implementation demangles names early.
186 Buffer->append("'%s'", A.String);
188 Buffer->append("'%s'", Symbolizer::GetOrInit()->Demangle(A.String));
192 // 'long long' is guaranteed to be at least 64 bits wide.
193 if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX)
194 Buffer->append("%lld", (long long)A.SInt);
196 RenderHex(Buffer, A.SInt);
199 if (A.UInt <= UINT64_MAX)
200 Buffer->append("%llu", (unsigned long long)A.UInt);
202 RenderHex(Buffer, A.UInt);
204 case Diag::AK_Float: {
205 // FIXME: Support floating-point formatting in sanitizer_common's
206 // printf, and stop using snprintf here.
207 char FloatBuffer[32];
208 #if SANITIZER_WINDOWS
209 sprintf_s(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
211 snprintf(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
213 Buffer->append("%s", FloatBuffer);
216 case Diag::AK_Pointer:
217 Buffer->append("%p", A.Pointer);
223 /// Find the earliest-starting range in Ranges which ends after Loc.
224 static Range *upperBound(MemoryLocation Loc, Range *Ranges,
225 unsigned NumRanges) {
227 for (unsigned I = 0; I != NumRanges; ++I)
228 if (Ranges[I].getEnd().getMemoryLocation() > Loc &&
230 Best->getStart().getMemoryLocation() >
231 Ranges[I].getStart().getMemoryLocation()))
236 static inline uptr subtractNoOverflow(uptr LHS, uptr RHS) {
237 return (LHS < RHS) ? 0 : LHS - RHS;
240 static inline uptr addNoOverflow(uptr LHS, uptr RHS) {
241 const uptr Limit = (uptr)-1;
242 return (LHS > Limit - RHS) ? Limit : LHS + RHS;
245 /// Render a snippet of the address space near a location.
246 static void PrintMemorySnippet(const Decorator &Decor, MemoryLocation Loc,
247 Range *Ranges, unsigned NumRanges,
248 const Diag::Arg *Args) {
249 // Show at least the 8 bytes surrounding Loc.
250 const unsigned MinBytesNearLoc = 4;
251 MemoryLocation Min = subtractNoOverflow(Loc, MinBytesNearLoc);
252 MemoryLocation Max = addNoOverflow(Loc, MinBytesNearLoc);
253 MemoryLocation OrigMin = Min;
254 for (unsigned I = 0; I < NumRanges; ++I) {
255 Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min);
256 Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max);
259 // If we have too many interesting bytes, prefer to show bytes after Loc.
260 const unsigned BytesToShow = 32;
261 if (Max - Min > BytesToShow)
262 Min = __sanitizer::Min(Max - BytesToShow, OrigMin);
263 Max = addNoOverflow(Min, BytesToShow);
265 if (!IsAccessibleMemoryRange(Min, Max - Min)) {
266 Printf("<memory cannot be printed>\n");
271 InternalScopedString Buffer(1024);
272 for (uptr P = Min; P != Max; ++P) {
273 unsigned char C = *reinterpret_cast<const unsigned char*>(P);
274 Buffer.append("%s%02x", (P % 8 == 0) ? " " : " ", C);
279 Buffer.append(Decor.Highlight());
280 Range *InRange = upperBound(Min, Ranges, NumRanges);
281 for (uptr P = Min; P != Max; ++P) {
282 char Pad = ' ', Byte = ' ';
283 if (InRange && InRange->getEnd().getMemoryLocation() == P)
284 InRange = upperBound(P, Ranges, NumRanges);
285 if (!InRange && P > Loc)
287 if (InRange && InRange->getStart().getMemoryLocation() < P)
289 if (InRange && InRange->getStart().getMemoryLocation() <= P)
292 Buffer.append("%c", Pad);
293 Buffer.append("%c", Pad);
294 Buffer.append("%c", P == Loc ? '^' : Byte);
295 Buffer.append("%c", Byte);
297 Buffer.append("%s\n", Decor.EndHighlight());
299 // Go over the line again, and print names for the ranges.
302 for (uptr P = Min; P != Max; ++P) {
303 if (!InRange || InRange->getEnd().getMemoryLocation() == P)
304 InRange = upperBound(P, Ranges, NumRanges);
308 Spaces += (P % 8) == 0 ? 2 : 1;
310 if (InRange && InRange->getStart().getMemoryLocation() == P) {
313 RenderText(&Buffer, InRange->getText(), Args);
315 // FIXME: We only support naming one range for now!
322 Printf("%s", Buffer.data());
323 // FIXME: Print names for anything we can identify within the line:
325 // * If we can identify the memory itself as belonging to a particular
326 // global, stack variable, or dynamic allocation, then do so.
328 // * If we have a pointer-size, pointer-aligned range highlighted,
329 // determine whether the value of that range is a pointer to an
330 // entity which we can name, and if so, print that name.
332 // This needs an external symbolizer, or (preferably) ASan instrumentation.
336 // All diagnostics should be printed under report mutex.
337 CommonSanitizerReportMutex.CheckLocked();
339 InternalScopedString Buffer(1024);
341 Buffer.append(Decor.Bold());
342 RenderLocation(&Buffer, Loc);
347 Buffer.append("%s runtime error: %s%s", Decor.Warning(), Decor.EndWarning(),
352 Buffer.append("%s note: %s", Decor.Note(), Decor.EndNote());
356 RenderText(&Buffer, Message, Args);
358 Buffer.append("%s\n", Decor.Default());
359 Printf("%s", Buffer.data());
361 if (Loc.isMemoryLocation())
362 PrintMemorySnippet(Decor, Loc.getMemoryLocation(), Ranges, NumRanges, Args);
365 ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc,
367 : Opts(Opts), SummaryLoc(SummaryLoc), Type(Type) {
368 InitAsStandaloneIfNecessary();
369 CommonSanitizerReportMutex.Lock();
372 ScopedReport::~ScopedReport() {
373 MaybePrintStackTrace(Opts.pc, Opts.bp);
374 MaybeReportErrorSummary(SummaryLoc, Type);
375 CommonSanitizerReportMutex.Unlock();
376 if (flags()->halt_on_error)
380 ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
381 static SuppressionContext *suppression_ctx = nullptr;
382 static const char kVptrCheck[] = "vptr_check";
383 static const char *kSuppressionTypes[] = {
384 #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) FSanitizeFlagName,
385 #include "ubsan_checks.inc"
390 void __ubsan::InitializeSuppressions() {
391 CHECK_EQ(nullptr, suppression_ctx);
392 suppression_ctx = new (suppression_placeholder) // NOLINT
393 SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
394 suppression_ctx->ParseFromFile(flags()->suppressions);
397 bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) {
398 InitAsStandaloneIfNecessary();
399 CHECK(suppression_ctx);
401 return suppression_ctx->Match(TypeName, kVptrCheck, &s);
404 bool __ubsan::IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename) {
405 InitAsStandaloneIfNecessary();
406 CHECK(suppression_ctx);
407 const char *SuppType = ConvertTypeToFlagName(ET);
408 // Fast path: don't symbolize PC if there is no suppressions for given UB
410 if (!suppression_ctx->HasSuppressionType(SuppType))
412 Suppression *s = nullptr;
413 // Suppress by file name known to runtime.
414 if (Filename != nullptr && suppression_ctx->Match(Filename, SuppType, &s))
416 // Suppress by module name.
417 if (const char *Module = Symbolizer::GetOrInit()->GetModuleNameForPc(PC)) {
418 if (suppression_ctx->Match(Module, SuppType, &s))
421 // Suppress by function or source file name from debug info.
422 SymbolizedStackHolder Stack(Symbolizer::GetOrInit()->SymbolizePC(PC));
423 const AddressInfo &AI = Stack.get()->info;
424 return suppression_ctx->Match(AI.function, SuppType, &s) ||
425 suppression_ctx->Match(AI.file, SuppType, &s);
428 #endif // CAN_SANITIZE_UB