13cab2bb3Spatrick //===-- ubsan_diag.cpp ----------------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // Diagnostic reporting for the UBSan runtime.
103cab2bb3Spatrick //
113cab2bb3Spatrick //===----------------------------------------------------------------------===//
123cab2bb3Spatrick
133cab2bb3Spatrick #include "ubsan_platform.h"
143cab2bb3Spatrick #if CAN_SANITIZE_UB
153cab2bb3Spatrick #include "ubsan_diag.h"
163cab2bb3Spatrick #include "ubsan_init.h"
173cab2bb3Spatrick #include "ubsan_flags.h"
183cab2bb3Spatrick #include "ubsan_monitor.h"
193cab2bb3Spatrick #include "sanitizer_common/sanitizer_placement_new.h"
203cab2bb3Spatrick #include "sanitizer_common/sanitizer_report_decorator.h"
213cab2bb3Spatrick #include "sanitizer_common/sanitizer_stacktrace.h"
223cab2bb3Spatrick #include "sanitizer_common/sanitizer_stacktrace_printer.h"
233cab2bb3Spatrick #include "sanitizer_common/sanitizer_suppressions.h"
243cab2bb3Spatrick #include "sanitizer_common/sanitizer_symbolizer.h"
253cab2bb3Spatrick #include <stdio.h>
263cab2bb3Spatrick
273cab2bb3Spatrick using namespace __ubsan;
283cab2bb3Spatrick
293cab2bb3Spatrick // UBSan is combined with runtimes that already provide this functionality
303cab2bb3Spatrick // (e.g., ASan) as well as runtimes that lack it (e.g., scudo). Tried to use
313cab2bb3Spatrick // weak linkage to resolve this issue which is not portable and breaks on
323cab2bb3Spatrick // Windows.
333cab2bb3Spatrick // TODO(yln): This is a temporary workaround. GetStackTrace functions will be
343cab2bb3Spatrick // removed in the future.
ubsan_GetStackTrace(BufferedStackTrace * stack,uptr max_depth,uptr pc,uptr bp,void * context,bool request_fast)35*810390e3Srobert void ubsan_GetStackTrace(BufferedStackTrace *stack, uptr max_depth, uptr pc,
36*810390e3Srobert uptr bp, void *context, bool request_fast) {
373cab2bb3Spatrick uptr top = 0;
383cab2bb3Spatrick uptr bottom = 0;
393cab2bb3Spatrick GetThreadStackTopAndBottom(false, &top, &bottom);
40*810390e3Srobert bool fast = StackTrace::WillUseFastUnwind(request_fast);
41*810390e3Srobert stack->Unwind(max_depth, pc, bp, context, top, bottom, fast);
423cab2bb3Spatrick }
433cab2bb3Spatrick
MaybePrintStackTrace(uptr pc,uptr bp)443cab2bb3Spatrick static void MaybePrintStackTrace(uptr pc, uptr bp) {
453cab2bb3Spatrick // We assume that flags are already parsed, as UBSan runtime
463cab2bb3Spatrick // will definitely be called when we print the first diagnostics message.
473cab2bb3Spatrick if (!flags()->print_stacktrace)
483cab2bb3Spatrick return;
493cab2bb3Spatrick
503cab2bb3Spatrick BufferedStackTrace stack;
513cab2bb3Spatrick ubsan_GetStackTrace(&stack, kStackTraceMax, pc, bp, nullptr,
523cab2bb3Spatrick common_flags()->fast_unwind_on_fatal);
533cab2bb3Spatrick stack.Print();
543cab2bb3Spatrick }
553cab2bb3Spatrick
ConvertTypeToString(ErrorType Type)563cab2bb3Spatrick static const char *ConvertTypeToString(ErrorType Type) {
573cab2bb3Spatrick switch (Type) {
583cab2bb3Spatrick #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
593cab2bb3Spatrick case ErrorType::Name: \
603cab2bb3Spatrick return SummaryKind;
613cab2bb3Spatrick #include "ubsan_checks.inc"
623cab2bb3Spatrick #undef UBSAN_CHECK
633cab2bb3Spatrick }
643cab2bb3Spatrick UNREACHABLE("unknown ErrorType!");
653cab2bb3Spatrick }
663cab2bb3Spatrick
ConvertTypeToFlagName(ErrorType Type)673cab2bb3Spatrick static const char *ConvertTypeToFlagName(ErrorType Type) {
683cab2bb3Spatrick switch (Type) {
693cab2bb3Spatrick #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
703cab2bb3Spatrick case ErrorType::Name: \
713cab2bb3Spatrick return FSanitizeFlagName;
723cab2bb3Spatrick #include "ubsan_checks.inc"
733cab2bb3Spatrick #undef UBSAN_CHECK
743cab2bb3Spatrick }
753cab2bb3Spatrick UNREACHABLE("unknown ErrorType!");
763cab2bb3Spatrick }
773cab2bb3Spatrick
MaybeReportErrorSummary(Location Loc,ErrorType Type)783cab2bb3Spatrick static void MaybeReportErrorSummary(Location Loc, ErrorType Type) {
793cab2bb3Spatrick if (!common_flags()->print_summary)
803cab2bb3Spatrick return;
813cab2bb3Spatrick if (!flags()->report_error_type)
823cab2bb3Spatrick Type = ErrorType::GenericUB;
833cab2bb3Spatrick const char *ErrorKind = ConvertTypeToString(Type);
843cab2bb3Spatrick if (Loc.isSourceLocation()) {
853cab2bb3Spatrick SourceLocation SLoc = Loc.getSourceLocation();
863cab2bb3Spatrick if (!SLoc.isInvalid()) {
873cab2bb3Spatrick AddressInfo AI;
883cab2bb3Spatrick AI.file = internal_strdup(SLoc.getFilename());
893cab2bb3Spatrick AI.line = SLoc.getLine();
903cab2bb3Spatrick AI.column = SLoc.getColumn();
913cab2bb3Spatrick AI.function = internal_strdup(""); // Avoid printing ?? as function name.
923cab2bb3Spatrick ReportErrorSummary(ErrorKind, AI, GetSanititizerToolName());
933cab2bb3Spatrick AI.Clear();
943cab2bb3Spatrick return;
953cab2bb3Spatrick }
963cab2bb3Spatrick } else if (Loc.isSymbolizedStack()) {
973cab2bb3Spatrick const AddressInfo &AI = Loc.getSymbolizedStack()->info;
983cab2bb3Spatrick ReportErrorSummary(ErrorKind, AI, GetSanititizerToolName());
993cab2bb3Spatrick return;
1003cab2bb3Spatrick }
1013cab2bb3Spatrick ReportErrorSummary(ErrorKind, GetSanititizerToolName());
1023cab2bb3Spatrick }
1033cab2bb3Spatrick
1043cab2bb3Spatrick namespace {
1053cab2bb3Spatrick class Decorator : public SanitizerCommonDecorator {
1063cab2bb3Spatrick public:
Decorator()1073cab2bb3Spatrick Decorator() : SanitizerCommonDecorator() {}
Highlight() const1083cab2bb3Spatrick const char *Highlight() const { return Green(); }
Note() const1093cab2bb3Spatrick const char *Note() const { return Black(); }
1103cab2bb3Spatrick };
1113cab2bb3Spatrick }
1123cab2bb3Spatrick
getSymbolizedLocation(uptr PC)1133cab2bb3Spatrick SymbolizedStack *__ubsan::getSymbolizedLocation(uptr PC) {
1143cab2bb3Spatrick InitAsStandaloneIfNecessary();
1153cab2bb3Spatrick return Symbolizer::GetOrInit()->SymbolizePC(PC);
1163cab2bb3Spatrick }
1173cab2bb3Spatrick
operator <<(const TypeDescriptor & V)1183cab2bb3Spatrick Diag &Diag::operator<<(const TypeDescriptor &V) {
1193cab2bb3Spatrick return AddArg(V.getTypeName());
1203cab2bb3Spatrick }
1213cab2bb3Spatrick
operator <<(const Value & V)1223cab2bb3Spatrick Diag &Diag::operator<<(const Value &V) {
1233cab2bb3Spatrick if (V.getType().isSignedIntegerTy())
1243cab2bb3Spatrick AddArg(V.getSIntValue());
1253cab2bb3Spatrick else if (V.getType().isUnsignedIntegerTy())
1263cab2bb3Spatrick AddArg(V.getUIntValue());
1273cab2bb3Spatrick else if (V.getType().isFloatTy())
1283cab2bb3Spatrick AddArg(V.getFloatValue());
1293cab2bb3Spatrick else
1303cab2bb3Spatrick AddArg("<unknown>");
1313cab2bb3Spatrick return *this;
1323cab2bb3Spatrick }
1333cab2bb3Spatrick
1343cab2bb3Spatrick /// Hexadecimal printing for numbers too large for Printf to handle directly.
RenderHex(InternalScopedString * Buffer,UIntMax Val)1353cab2bb3Spatrick static void RenderHex(InternalScopedString *Buffer, UIntMax Val) {
1363cab2bb3Spatrick #if HAVE_INT128_T
1373cab2bb3Spatrick Buffer->append("0x%08x%08x%08x%08x", (unsigned int)(Val >> 96),
1383cab2bb3Spatrick (unsigned int)(Val >> 64), (unsigned int)(Val >> 32),
1393cab2bb3Spatrick (unsigned int)(Val));
1403cab2bb3Spatrick #else
1413cab2bb3Spatrick UNREACHABLE("long long smaller than 64 bits?");
1423cab2bb3Spatrick #endif
1433cab2bb3Spatrick }
1443cab2bb3Spatrick
RenderLocation(InternalScopedString * Buffer,Location Loc)1453cab2bb3Spatrick static void RenderLocation(InternalScopedString *Buffer, Location Loc) {
1463cab2bb3Spatrick switch (Loc.getKind()) {
1473cab2bb3Spatrick case Location::LK_Source: {
1483cab2bb3Spatrick SourceLocation SLoc = Loc.getSourceLocation();
1493cab2bb3Spatrick if (SLoc.isInvalid())
1503cab2bb3Spatrick Buffer->append("<unknown>");
1513cab2bb3Spatrick else
1523cab2bb3Spatrick RenderSourceLocation(Buffer, SLoc.getFilename(), SLoc.getLine(),
1533cab2bb3Spatrick SLoc.getColumn(), common_flags()->symbolize_vs_style,
1543cab2bb3Spatrick common_flags()->strip_path_prefix);
1553cab2bb3Spatrick return;
1563cab2bb3Spatrick }
1573cab2bb3Spatrick case Location::LK_Memory:
158*810390e3Srobert Buffer->append("%p", reinterpret_cast<void *>(Loc.getMemoryLocation()));
1593cab2bb3Spatrick return;
1603cab2bb3Spatrick case Location::LK_Symbolized: {
1613cab2bb3Spatrick const AddressInfo &Info = Loc.getSymbolizedStack()->info;
1623cab2bb3Spatrick if (Info.file)
1633cab2bb3Spatrick RenderSourceLocation(Buffer, Info.file, Info.line, Info.column,
1643cab2bb3Spatrick common_flags()->symbolize_vs_style,
1653cab2bb3Spatrick common_flags()->strip_path_prefix);
1663cab2bb3Spatrick else if (Info.module)
1673cab2bb3Spatrick RenderModuleLocation(Buffer, Info.module, Info.module_offset,
1683cab2bb3Spatrick Info.module_arch, common_flags()->strip_path_prefix);
1693cab2bb3Spatrick else
170*810390e3Srobert Buffer->append("%p", reinterpret_cast<void *>(Info.address));
1713cab2bb3Spatrick return;
1723cab2bb3Spatrick }
1733cab2bb3Spatrick case Location::LK_Null:
1743cab2bb3Spatrick Buffer->append("<unknown>");
1753cab2bb3Spatrick return;
1763cab2bb3Spatrick }
1773cab2bb3Spatrick }
1783cab2bb3Spatrick
RenderText(InternalScopedString * Buffer,const char * Message,const Diag::Arg * Args)1793cab2bb3Spatrick static void RenderText(InternalScopedString *Buffer, const char *Message,
1803cab2bb3Spatrick const Diag::Arg *Args) {
1813cab2bb3Spatrick for (const char *Msg = Message; *Msg; ++Msg) {
1823cab2bb3Spatrick if (*Msg != '%') {
1833cab2bb3Spatrick Buffer->append("%c", *Msg);
1843cab2bb3Spatrick continue;
1853cab2bb3Spatrick }
1863cab2bb3Spatrick const Diag::Arg &A = Args[*++Msg - '0'];
1873cab2bb3Spatrick switch (A.Kind) {
1883cab2bb3Spatrick case Diag::AK_String:
1893cab2bb3Spatrick Buffer->append("%s", A.String);
1903cab2bb3Spatrick break;
1913cab2bb3Spatrick case Diag::AK_TypeName: {
1923cab2bb3Spatrick if (SANITIZER_WINDOWS)
1933cab2bb3Spatrick // The Windows implementation demangles names early.
1943cab2bb3Spatrick Buffer->append("'%s'", A.String);
1953cab2bb3Spatrick else
1963cab2bb3Spatrick Buffer->append("'%s'", Symbolizer::GetOrInit()->Demangle(A.String));
1973cab2bb3Spatrick break;
1983cab2bb3Spatrick }
1993cab2bb3Spatrick case Diag::AK_SInt:
2003cab2bb3Spatrick // 'long long' is guaranteed to be at least 64 bits wide.
2013cab2bb3Spatrick if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX)
2023cab2bb3Spatrick Buffer->append("%lld", (long long)A.SInt);
2033cab2bb3Spatrick else
2043cab2bb3Spatrick RenderHex(Buffer, A.SInt);
2053cab2bb3Spatrick break;
2063cab2bb3Spatrick case Diag::AK_UInt:
2073cab2bb3Spatrick if (A.UInt <= UINT64_MAX)
2083cab2bb3Spatrick Buffer->append("%llu", (unsigned long long)A.UInt);
2093cab2bb3Spatrick else
2103cab2bb3Spatrick RenderHex(Buffer, A.UInt);
2113cab2bb3Spatrick break;
2123cab2bb3Spatrick case Diag::AK_Float: {
2133cab2bb3Spatrick // FIXME: Support floating-point formatting in sanitizer_common's
2143cab2bb3Spatrick // printf, and stop using snprintf here.
2153cab2bb3Spatrick char FloatBuffer[32];
2163cab2bb3Spatrick #if SANITIZER_WINDOWS
2173cab2bb3Spatrick sprintf_s(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
2183cab2bb3Spatrick #else
2193cab2bb3Spatrick snprintf(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
2203cab2bb3Spatrick #endif
2213cab2bb3Spatrick Buffer->append("%s", FloatBuffer);
2223cab2bb3Spatrick break;
2233cab2bb3Spatrick }
2243cab2bb3Spatrick case Diag::AK_Pointer:
2253cab2bb3Spatrick Buffer->append("%p", A.Pointer);
2263cab2bb3Spatrick break;
2273cab2bb3Spatrick }
2283cab2bb3Spatrick }
2293cab2bb3Spatrick }
2303cab2bb3Spatrick
2313cab2bb3Spatrick /// Find the earliest-starting range in Ranges which ends after Loc.
upperBound(MemoryLocation Loc,Range * Ranges,unsigned NumRanges)2323cab2bb3Spatrick static Range *upperBound(MemoryLocation Loc, Range *Ranges,
2333cab2bb3Spatrick unsigned NumRanges) {
2343cab2bb3Spatrick Range *Best = 0;
2353cab2bb3Spatrick for (unsigned I = 0; I != NumRanges; ++I)
2363cab2bb3Spatrick if (Ranges[I].getEnd().getMemoryLocation() > Loc &&
2373cab2bb3Spatrick (!Best ||
2383cab2bb3Spatrick Best->getStart().getMemoryLocation() >
2393cab2bb3Spatrick Ranges[I].getStart().getMemoryLocation()))
2403cab2bb3Spatrick Best = &Ranges[I];
2413cab2bb3Spatrick return Best;
2423cab2bb3Spatrick }
2433cab2bb3Spatrick
subtractNoOverflow(uptr LHS,uptr RHS)2443cab2bb3Spatrick static inline uptr subtractNoOverflow(uptr LHS, uptr RHS) {
2453cab2bb3Spatrick return (LHS < RHS) ? 0 : LHS - RHS;
2463cab2bb3Spatrick }
2473cab2bb3Spatrick
addNoOverflow(uptr LHS,uptr RHS)2483cab2bb3Spatrick static inline uptr addNoOverflow(uptr LHS, uptr RHS) {
2493cab2bb3Spatrick const uptr Limit = (uptr)-1;
2503cab2bb3Spatrick return (LHS > Limit - RHS) ? Limit : LHS + RHS;
2513cab2bb3Spatrick }
2523cab2bb3Spatrick
2533cab2bb3Spatrick /// Render a snippet of the address space near a location.
PrintMemorySnippet(const Decorator & Decor,MemoryLocation Loc,Range * Ranges,unsigned NumRanges,const Diag::Arg * Args)2543cab2bb3Spatrick static void PrintMemorySnippet(const Decorator &Decor, MemoryLocation Loc,
2553cab2bb3Spatrick Range *Ranges, unsigned NumRanges,
2563cab2bb3Spatrick const Diag::Arg *Args) {
2573cab2bb3Spatrick // Show at least the 8 bytes surrounding Loc.
2583cab2bb3Spatrick const unsigned MinBytesNearLoc = 4;
2593cab2bb3Spatrick MemoryLocation Min = subtractNoOverflow(Loc, MinBytesNearLoc);
2603cab2bb3Spatrick MemoryLocation Max = addNoOverflow(Loc, MinBytesNearLoc);
2613cab2bb3Spatrick MemoryLocation OrigMin = Min;
2623cab2bb3Spatrick for (unsigned I = 0; I < NumRanges; ++I) {
2633cab2bb3Spatrick Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min);
2643cab2bb3Spatrick Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max);
2653cab2bb3Spatrick }
2663cab2bb3Spatrick
2673cab2bb3Spatrick // If we have too many interesting bytes, prefer to show bytes after Loc.
2683cab2bb3Spatrick const unsigned BytesToShow = 32;
2693cab2bb3Spatrick if (Max - Min > BytesToShow)
2703cab2bb3Spatrick Min = __sanitizer::Min(Max - BytesToShow, OrigMin);
2713cab2bb3Spatrick Max = addNoOverflow(Min, BytesToShow);
2723cab2bb3Spatrick
2733cab2bb3Spatrick if (!IsAccessibleMemoryRange(Min, Max - Min)) {
2743cab2bb3Spatrick Printf("<memory cannot be printed>\n");
2753cab2bb3Spatrick return;
2763cab2bb3Spatrick }
2773cab2bb3Spatrick
2783cab2bb3Spatrick // Emit data.
279d89ec533Spatrick InternalScopedString Buffer;
2803cab2bb3Spatrick for (uptr P = Min; P != Max; ++P) {
2813cab2bb3Spatrick unsigned char C = *reinterpret_cast<const unsigned char*>(P);
2823cab2bb3Spatrick Buffer.append("%s%02x", (P % 8 == 0) ? " " : " ", C);
2833cab2bb3Spatrick }
2843cab2bb3Spatrick Buffer.append("\n");
2853cab2bb3Spatrick
2863cab2bb3Spatrick // Emit highlights.
287*810390e3Srobert Buffer.append("%s", Decor.Highlight());
2883cab2bb3Spatrick Range *InRange = upperBound(Min, Ranges, NumRanges);
2893cab2bb3Spatrick for (uptr P = Min; P != Max; ++P) {
2903cab2bb3Spatrick char Pad = ' ', Byte = ' ';
2913cab2bb3Spatrick if (InRange && InRange->getEnd().getMemoryLocation() == P)
2923cab2bb3Spatrick InRange = upperBound(P, Ranges, NumRanges);
2933cab2bb3Spatrick if (!InRange && P > Loc)
2943cab2bb3Spatrick break;
2953cab2bb3Spatrick if (InRange && InRange->getStart().getMemoryLocation() < P)
2963cab2bb3Spatrick Pad = '~';
2973cab2bb3Spatrick if (InRange && InRange->getStart().getMemoryLocation() <= P)
2983cab2bb3Spatrick Byte = '~';
2993cab2bb3Spatrick if (P % 8 == 0)
3003cab2bb3Spatrick Buffer.append("%c", Pad);
3013cab2bb3Spatrick Buffer.append("%c", Pad);
3023cab2bb3Spatrick Buffer.append("%c", P == Loc ? '^' : Byte);
3033cab2bb3Spatrick Buffer.append("%c", Byte);
3043cab2bb3Spatrick }
3053cab2bb3Spatrick Buffer.append("%s\n", Decor.Default());
3063cab2bb3Spatrick
3073cab2bb3Spatrick // Go over the line again, and print names for the ranges.
3083cab2bb3Spatrick InRange = 0;
3093cab2bb3Spatrick unsigned Spaces = 0;
3103cab2bb3Spatrick for (uptr P = Min; P != Max; ++P) {
3113cab2bb3Spatrick if (!InRange || InRange->getEnd().getMemoryLocation() == P)
3123cab2bb3Spatrick InRange = upperBound(P, Ranges, NumRanges);
3133cab2bb3Spatrick if (!InRange)
3143cab2bb3Spatrick break;
3153cab2bb3Spatrick
3163cab2bb3Spatrick Spaces += (P % 8) == 0 ? 2 : 1;
3173cab2bb3Spatrick
3183cab2bb3Spatrick if (InRange && InRange->getStart().getMemoryLocation() == P) {
3193cab2bb3Spatrick while (Spaces--)
3203cab2bb3Spatrick Buffer.append(" ");
3213cab2bb3Spatrick RenderText(&Buffer, InRange->getText(), Args);
3223cab2bb3Spatrick Buffer.append("\n");
3233cab2bb3Spatrick // FIXME: We only support naming one range for now!
3243cab2bb3Spatrick break;
3253cab2bb3Spatrick }
3263cab2bb3Spatrick
3273cab2bb3Spatrick Spaces += 2;
3283cab2bb3Spatrick }
3293cab2bb3Spatrick
3303cab2bb3Spatrick Printf("%s", Buffer.data());
3313cab2bb3Spatrick // FIXME: Print names for anything we can identify within the line:
3323cab2bb3Spatrick //
3333cab2bb3Spatrick // * If we can identify the memory itself as belonging to a particular
3343cab2bb3Spatrick // global, stack variable, or dynamic allocation, then do so.
3353cab2bb3Spatrick //
3363cab2bb3Spatrick // * If we have a pointer-size, pointer-aligned range highlighted,
3373cab2bb3Spatrick // determine whether the value of that range is a pointer to an
3383cab2bb3Spatrick // entity which we can name, and if so, print that name.
3393cab2bb3Spatrick //
3403cab2bb3Spatrick // This needs an external symbolizer, or (preferably) ASan instrumentation.
3413cab2bb3Spatrick }
3423cab2bb3Spatrick
~Diag()3433cab2bb3Spatrick Diag::~Diag() {
3443cab2bb3Spatrick // All diagnostics should be printed under report mutex.
3453cab2bb3Spatrick ScopedReport::CheckLocked();
3463cab2bb3Spatrick Decorator Decor;
347d89ec533Spatrick InternalScopedString Buffer;
3483cab2bb3Spatrick
3493cab2bb3Spatrick // Prepare a report that a monitor process can inspect.
3503cab2bb3Spatrick if (Level == DL_Error) {
3513cab2bb3Spatrick RenderText(&Buffer, Message, Args);
3523cab2bb3Spatrick UndefinedBehaviorReport UBR{ConvertTypeToString(ET), Loc, Buffer};
3533cab2bb3Spatrick Buffer.clear();
3543cab2bb3Spatrick }
3553cab2bb3Spatrick
356*810390e3Srobert Buffer.append("%s", Decor.Bold());
3573cab2bb3Spatrick RenderLocation(&Buffer, Loc);
3583cab2bb3Spatrick Buffer.append(":");
3593cab2bb3Spatrick
3603cab2bb3Spatrick switch (Level) {
3613cab2bb3Spatrick case DL_Error:
3623cab2bb3Spatrick Buffer.append("%s runtime error: %s%s", Decor.Warning(), Decor.Default(),
3633cab2bb3Spatrick Decor.Bold());
3643cab2bb3Spatrick break;
3653cab2bb3Spatrick
3663cab2bb3Spatrick case DL_Note:
3673cab2bb3Spatrick Buffer.append("%s note: %s", Decor.Note(), Decor.Default());
3683cab2bb3Spatrick break;
3693cab2bb3Spatrick }
3703cab2bb3Spatrick
3713cab2bb3Spatrick RenderText(&Buffer, Message, Args);
3723cab2bb3Spatrick
3733cab2bb3Spatrick Buffer.append("%s\n", Decor.Default());
3743cab2bb3Spatrick Printf("%s", Buffer.data());
3753cab2bb3Spatrick
3763cab2bb3Spatrick if (Loc.isMemoryLocation())
3773cab2bb3Spatrick PrintMemorySnippet(Decor, Loc.getMemoryLocation(), Ranges, NumRanges, Args);
3783cab2bb3Spatrick }
3793cab2bb3Spatrick
Initializer()3803cab2bb3Spatrick ScopedReport::Initializer::Initializer() { InitAsStandaloneIfNecessary(); }
3813cab2bb3Spatrick
ScopedReport(ReportOptions Opts,Location SummaryLoc,ErrorType Type)3823cab2bb3Spatrick ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc,
3833cab2bb3Spatrick ErrorType Type)
3843cab2bb3Spatrick : Opts(Opts), SummaryLoc(SummaryLoc), Type(Type) {}
3853cab2bb3Spatrick
~ScopedReport()3863cab2bb3Spatrick ScopedReport::~ScopedReport() {
3873cab2bb3Spatrick MaybePrintStackTrace(Opts.pc, Opts.bp);
3883cab2bb3Spatrick MaybeReportErrorSummary(SummaryLoc, Type);
389d89ec533Spatrick
390d89ec533Spatrick if (common_flags()->print_module_map >= 2)
391d89ec533Spatrick DumpProcessMap();
392d89ec533Spatrick
3933cab2bb3Spatrick if (flags()->halt_on_error)
3943cab2bb3Spatrick Die();
3953cab2bb3Spatrick }
3963cab2bb3Spatrick
3973cab2bb3Spatrick ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
3983cab2bb3Spatrick static SuppressionContext *suppression_ctx = nullptr;
3993cab2bb3Spatrick static const char kVptrCheck[] = "vptr_check";
4003cab2bb3Spatrick static const char *kSuppressionTypes[] = {
4013cab2bb3Spatrick #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) FSanitizeFlagName,
4023cab2bb3Spatrick #include "ubsan_checks.inc"
4033cab2bb3Spatrick #undef UBSAN_CHECK
4043cab2bb3Spatrick kVptrCheck,
4053cab2bb3Spatrick };
4063cab2bb3Spatrick
InitializeSuppressions()4073cab2bb3Spatrick void __ubsan::InitializeSuppressions() {
4083cab2bb3Spatrick CHECK_EQ(nullptr, suppression_ctx);
4093cab2bb3Spatrick suppression_ctx = new (suppression_placeholder)
4103cab2bb3Spatrick SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
4113cab2bb3Spatrick suppression_ctx->ParseFromFile(flags()->suppressions);
4123cab2bb3Spatrick }
4133cab2bb3Spatrick
IsVptrCheckSuppressed(const char * TypeName)4143cab2bb3Spatrick bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) {
4153cab2bb3Spatrick InitAsStandaloneIfNecessary();
4163cab2bb3Spatrick CHECK(suppression_ctx);
4173cab2bb3Spatrick Suppression *s;
4183cab2bb3Spatrick return suppression_ctx->Match(TypeName, kVptrCheck, &s);
4193cab2bb3Spatrick }
4203cab2bb3Spatrick
IsPCSuppressed(ErrorType ET,uptr PC,const char * Filename)4213cab2bb3Spatrick bool __ubsan::IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename) {
4223cab2bb3Spatrick InitAsStandaloneIfNecessary();
4233cab2bb3Spatrick CHECK(suppression_ctx);
4243cab2bb3Spatrick const char *SuppType = ConvertTypeToFlagName(ET);
4253cab2bb3Spatrick // Fast path: don't symbolize PC if there is no suppressions for given UB
4263cab2bb3Spatrick // type.
4273cab2bb3Spatrick if (!suppression_ctx->HasSuppressionType(SuppType))
4283cab2bb3Spatrick return false;
4293cab2bb3Spatrick Suppression *s = nullptr;
4303cab2bb3Spatrick // Suppress by file name known to runtime.
4313cab2bb3Spatrick if (Filename != nullptr && suppression_ctx->Match(Filename, SuppType, &s))
4323cab2bb3Spatrick return true;
4333cab2bb3Spatrick // Suppress by module name.
4343cab2bb3Spatrick if (const char *Module = Symbolizer::GetOrInit()->GetModuleNameForPc(PC)) {
4353cab2bb3Spatrick if (suppression_ctx->Match(Module, SuppType, &s))
4363cab2bb3Spatrick return true;
4373cab2bb3Spatrick }
4383cab2bb3Spatrick // Suppress by function or source file name from debug info.
4393cab2bb3Spatrick SymbolizedStackHolder Stack(Symbolizer::GetOrInit()->SymbolizePC(PC));
4403cab2bb3Spatrick const AddressInfo &AI = Stack.get()->info;
4413cab2bb3Spatrick return suppression_ctx->Match(AI.function, SuppType, &s) ||
4423cab2bb3Spatrick suppression_ctx->Match(AI.file, SuppType, &s);
4433cab2bb3Spatrick }
4443cab2bb3Spatrick
4453cab2bb3Spatrick #endif // CAN_SANITIZE_UB
446