Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 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 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 14 | #include "ubsan_platform.h" |
| 15 | #if CAN_SANITIZE_UB |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 16 | #include "ubsan_diag.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 17 | #include "ubsan_init.h" |
| 18 | #include "ubsan_flags.h" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_placement_new.h" |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_stacktrace_printer.h" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_suppressions.h" |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace __ubsan; |
| 28 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 29 | static void MaybePrintStackTrace(uptr pc, uptr bp) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 30 | // We assume that flags are already parsed, as UBSan runtime |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 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(); |
| 44 | } |
| 45 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 46 | static const char *ConvertTypeToString(ErrorType Type) { |
| 47 | switch (Type) { |
| 48 | #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \ |
| 49 | case ErrorType::Name: \ |
| 50 | return SummaryKind; |
| 51 | #include "ubsan_checks.inc" |
| 52 | #undef UBSAN_CHECK |
| 53 | } |
| 54 | UNREACHABLE("unknown ErrorType!"); |
| 55 | } |
| 56 | |
| 57 | static const char *ConvertTypeToFlagName(ErrorType Type) { |
| 58 | switch (Type) { |
| 59 | #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \ |
| 60 | case ErrorType::Name: \ |
| 61 | return FSanitizeFlagName; |
| 62 | #include "ubsan_checks.inc" |
| 63 | #undef UBSAN_CHECK |
| 64 | } |
| 65 | UNREACHABLE("unknown ErrorType!"); |
| 66 | } |
| 67 | |
| 68 | static void MaybeReportErrorSummary(Location Loc, ErrorType Type) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 69 | if (!common_flags()->print_summary) |
| 70 | return; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 71 | if (!flags()->report_error_type) |
| 72 | Type = ErrorType::GenericUB; |
| 73 | const char *ErrorKind = ConvertTypeToString(Type); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 74 | if (Loc.isSourceLocation()) { |
| 75 | SourceLocation SLoc = Loc.getSourceLocation(); |
| 76 | if (!SLoc.isInvalid()) { |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 77 | AddressInfo AI; |
| 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. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 82 | ReportErrorSummary(ErrorKind, AI); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 83 | AI.Clear(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 84 | return; |
| 85 | } |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 86 | } else if (Loc.isSymbolizedStack()) { |
| 87 | const AddressInfo &AI = Loc.getSymbolizedStack()->info; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 88 | ReportErrorSummary(ErrorKind, AI); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 89 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 90 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 91 | ReportErrorSummary(ErrorKind); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 94 | namespace { |
| 95 | class Decorator : public SanitizerCommonDecorator { |
| 96 | public: |
| 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(); } |
| 102 | }; |
| 103 | } |
| 104 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 105 | SymbolizedStack *__ubsan::getSymbolizedLocation(uptr PC) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 106 | InitAsStandaloneIfNecessary(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 107 | return Symbolizer::GetOrInit()->SymbolizePC(PC); |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 110 | Diag &Diag::operator<<(const TypeDescriptor &V) { |
| 111 | return AddArg(V.getTypeName()); |
| 112 | } |
| 113 | |
| 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()); |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 119 | else if (V.getType().isFloatTy()) |
| 120 | AddArg(V.getFloatValue()); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 121 | else |
| 122 | AddArg("<unknown>"); |
| 123 | return *this; |
| 124 | } |
| 125 | |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 126 | /// Hexadecimal printing for numbers too large for Printf to handle directly. |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 127 | static void PrintHex(UIntMax Val) { |
Chandler Carruth | ba3fde6 | 2012-10-13 02:30:10 +0000 | [diff] [blame] | 128 | #if HAVE_INT128_T |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 129 | Printf("0x%08x%08x%08x%08x", |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 130 | (unsigned int)(Val >> 96), |
| 131 | (unsigned int)(Val >> 64), |
| 132 | (unsigned int)(Val >> 32), |
| 133 | (unsigned int)(Val)); |
| 134 | #else |
| 135 | UNREACHABLE("long long smaller than 64 bits?"); |
| 136 | #endif |
| 137 | } |
| 138 | |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 139 | static void renderLocation(Location Loc) { |
Alexey Samsonov | aed8584 | 2013-11-14 09:54:10 +0000 | [diff] [blame] | 140 | InternalScopedString LocBuffer(1024); |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 141 | switch (Loc.getKind()) { |
| 142 | case Location::LK_Source: { |
| 143 | SourceLocation SLoc = Loc.getSourceLocation(); |
| 144 | if (SLoc.isInvalid()) |
Alexey Samsonov | aed8584 | 2013-11-14 09:54:10 +0000 | [diff] [blame] | 145 | LocBuffer.append("<unknown>"); |
Alexey Samsonov | 90b0f1e | 2013-10-04 08:55:03 +0000 | [diff] [blame] | 146 | else |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 147 | RenderSourceLocation(&LocBuffer, SLoc.getFilename(), SLoc.getLine(), |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 148 | SLoc.getColumn(), common_flags()->symbolize_vs_style, |
| 149 | common_flags()->strip_path_prefix); |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 152 | case Location::LK_Memory: |
Alexey Samsonov | aed8584 | 2013-11-14 09:54:10 +0000 | [diff] [blame] | 153 | LocBuffer.append("%p", Loc.getMemoryLocation()); |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 154 | break; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 155 | case Location::LK_Symbolized: { |
| 156 | const AddressInfo &Info = Loc.getSymbolizedStack()->info; |
| 157 | if (Info.file) { |
| 158 | RenderSourceLocation(&LocBuffer, Info.file, Info.line, Info.column, |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 159 | common_flags()->symbolize_vs_style, |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 160 | common_flags()->strip_path_prefix); |
| 161 | } else if (Info.module) { |
| 162 | RenderModuleLocation(&LocBuffer, Info.module, Info.module_offset, |
| 163 | common_flags()->strip_path_prefix); |
| 164 | } else { |
| 165 | LocBuffer.append("%p", Info.address); |
| 166 | } |
| 167 | break; |
| 168 | } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 169 | case Location::LK_Null: |
Alexey Samsonov | aed8584 | 2013-11-14 09:54:10 +0000 | [diff] [blame] | 170 | LocBuffer.append("<unknown>"); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 171 | break; |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 172 | } |
Alexey Samsonov | aed8584 | 2013-11-14 09:54:10 +0000 | [diff] [blame] | 173 | Printf("%s:", LocBuffer.data()); |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 176 | static void renderText(const char *Message, const Diag::Arg *Args) { |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 177 | for (const char *Msg = Message; *Msg; ++Msg) { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 178 | if (*Msg != '%') { |
| 179 | char Buffer[64]; |
| 180 | unsigned I; |
| 181 | for (I = 0; Msg[I] && Msg[I] != '%' && I != 63; ++I) |
| 182 | Buffer[I] = Msg[I]; |
| 183 | Buffer[I] = '\0'; |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 184 | Printf(Buffer); |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 185 | Msg += I - 1; |
| 186 | } else { |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 187 | const Diag::Arg &A = Args[*++Msg - '0']; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 188 | switch (A.Kind) { |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 189 | case Diag::AK_String: |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 190 | Printf("%s", A.String); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 191 | break; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 192 | case Diag::AK_TypeName: { |
| 193 | if (SANITIZER_WINDOWS) |
| 194 | // The Windows implementation demangles names early. |
| 195 | Printf("'%s'", A.String); |
| 196 | else |
| 197 | Printf("'%s'", Symbolizer::GetOrInit()->Demangle(A.String)); |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 198 | break; |
| 199 | } |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 200 | case Diag::AK_SInt: |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 201 | // 'long long' is guaranteed to be at least 64 bits wide. |
| 202 | if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX) |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 203 | Printf("%lld", (long long)A.SInt); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 204 | else |
| 205 | PrintHex(A.SInt); |
| 206 | break; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 207 | case Diag::AK_UInt: |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 208 | if (A.UInt <= UINT64_MAX) |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 209 | Printf("%llu", (unsigned long long)A.UInt); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 210 | else |
| 211 | PrintHex(A.UInt); |
| 212 | break; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 213 | case Diag::AK_Float: { |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 214 | // FIXME: Support floating-point formatting in sanitizer_common's |
| 215 | // printf, and stop using snprintf here. |
| 216 | char Buffer[32]; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 217 | #if SANITIZER_WINDOWS |
| 218 | sprintf_s(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float); |
| 219 | #else |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 220 | snprintf(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 221 | #endif |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 222 | Printf("%s", Buffer); |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 223 | break; |
Richard Smith | f493220 | 2012-11-13 23:42:05 +0000 | [diff] [blame] | 224 | } |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 225 | case Diag::AK_Pointer: |
Richard Smith | c193953 | 2013-01-10 22:39:40 +0000 | [diff] [blame] | 226 | Printf("%p", A.Pointer); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /// Find the earliest-starting range in Ranges which ends after Loc. |
| 234 | static Range *upperBound(MemoryLocation Loc, Range *Ranges, |
| 235 | unsigned NumRanges) { |
| 236 | Range *Best = 0; |
| 237 | for (unsigned I = 0; I != NumRanges; ++I) |
| 238 | if (Ranges[I].getEnd().getMemoryLocation() > Loc && |
| 239 | (!Best || |
| 240 | Best->getStart().getMemoryLocation() > |
| 241 | Ranges[I].getStart().getMemoryLocation())) |
| 242 | Best = &Ranges[I]; |
| 243 | return Best; |
| 244 | } |
| 245 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 246 | static inline uptr subtractNoOverflow(uptr LHS, uptr RHS) { |
| 247 | return (LHS < RHS) ? 0 : LHS - RHS; |
| 248 | } |
| 249 | |
| 250 | static inline uptr addNoOverflow(uptr LHS, uptr RHS) { |
| 251 | const uptr Limit = (uptr)-1; |
| 252 | return (LHS > Limit - RHS) ? Limit : LHS + RHS; |
| 253 | } |
| 254 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 255 | /// Render a snippet of the address space near a location. |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 256 | static void renderMemorySnippet(const Decorator &Decor, MemoryLocation Loc, |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 257 | Range *Ranges, unsigned NumRanges, |
| 258 | const Diag::Arg *Args) { |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 259 | // Show at least the 8 bytes surrounding Loc. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 260 | const unsigned MinBytesNearLoc = 4; |
| 261 | MemoryLocation Min = subtractNoOverflow(Loc, MinBytesNearLoc); |
| 262 | MemoryLocation Max = addNoOverflow(Loc, MinBytesNearLoc); |
| 263 | MemoryLocation OrigMin = Min; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 264 | for (unsigned I = 0; I < NumRanges; ++I) { |
| 265 | Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min); |
| 266 | Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max); |
| 267 | } |
| 268 | |
| 269 | // If we have too many interesting bytes, prefer to show bytes after Loc. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 270 | const unsigned BytesToShow = 32; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 271 | if (Max - Min > BytesToShow) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 272 | Min = __sanitizer::Min(Max - BytesToShow, OrigMin); |
| 273 | Max = addNoOverflow(Min, BytesToShow); |
| 274 | |
| 275 | if (!IsAccessibleMemoryRange(Min, Max - Min)) { |
| 276 | Printf("<memory cannot be printed>\n"); |
| 277 | return; |
| 278 | } |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 279 | |
| 280 | // Emit data. |
| 281 | for (uptr P = Min; P != Max; ++P) { |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 282 | unsigned char C = *reinterpret_cast<const unsigned char*>(P); |
| 283 | Printf("%s%02x", (P % 8 == 0) ? " " : " ", C); |
| 284 | } |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 285 | Printf("\n"); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 286 | |
| 287 | // Emit highlights. |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 288 | Printf(Decor.Highlight()); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 289 | Range *InRange = upperBound(Min, Ranges, NumRanges); |
| 290 | for (uptr P = Min; P != Max; ++P) { |
| 291 | char Pad = ' ', Byte = ' '; |
| 292 | if (InRange && InRange->getEnd().getMemoryLocation() == P) |
| 293 | InRange = upperBound(P, Ranges, NumRanges); |
| 294 | if (!InRange && P > Loc) |
| 295 | break; |
| 296 | if (InRange && InRange->getStart().getMemoryLocation() < P) |
| 297 | Pad = '~'; |
| 298 | if (InRange && InRange->getStart().getMemoryLocation() <= P) |
| 299 | Byte = '~'; |
| 300 | char Buffer[] = { Pad, Pad, P == Loc ? '^' : Byte, Byte, 0 }; |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 301 | Printf((P % 8 == 0) ? Buffer : &Buffer[1]); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 302 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 303 | Printf("%s\n", Decor.EndHighlight()); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 304 | |
| 305 | // Go over the line again, and print names for the ranges. |
| 306 | InRange = 0; |
| 307 | unsigned Spaces = 0; |
| 308 | for (uptr P = Min; P != Max; ++P) { |
| 309 | if (!InRange || InRange->getEnd().getMemoryLocation() == P) |
| 310 | InRange = upperBound(P, Ranges, NumRanges); |
| 311 | if (!InRange) |
| 312 | break; |
| 313 | |
| 314 | Spaces += (P % 8) == 0 ? 2 : 1; |
| 315 | |
| 316 | if (InRange && InRange->getStart().getMemoryLocation() == P) { |
| 317 | while (Spaces--) |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 318 | Printf(" "); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 319 | renderText(InRange->getText(), Args); |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 320 | Printf("\n"); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 321 | // FIXME: We only support naming one range for now! |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | Spaces += 2; |
| 326 | } |
| 327 | |
| 328 | // FIXME: Print names for anything we can identify within the line: |
| 329 | // |
| 330 | // * If we can identify the memory itself as belonging to a particular |
| 331 | // global, stack variable, or dynamic allocation, then do so. |
| 332 | // |
| 333 | // * If we have a pointer-size, pointer-aligned range highlighted, |
| 334 | // determine whether the value of that range is a pointer to an |
| 335 | // entity which we can name, and if so, print that name. |
| 336 | // |
| 337 | // This needs an external symbolizer, or (preferably) ASan instrumentation. |
| 338 | } |
| 339 | |
| 340 | Diag::~Diag() { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 341 | // All diagnostics should be printed under report mutex. |
| 342 | CommonSanitizerReportMutex.CheckLocked(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 343 | Decorator Decor; |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 344 | Printf(Decor.Bold()); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 345 | |
| 346 | renderLocation(Loc); |
| 347 | |
| 348 | switch (Level) { |
| 349 | case DL_Error: |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 350 | Printf("%s runtime error: %s%s", |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 351 | Decor.Warning(), Decor.EndWarning(), Decor.Bold()); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 352 | break; |
| 353 | |
| 354 | case DL_Note: |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 355 | Printf("%s note: %s", Decor.Note(), Decor.EndNote()); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | |
| 359 | renderText(Message, Args); |
| 360 | |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 361 | Printf("%s\n", Decor.Default()); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 362 | |
| 363 | if (Loc.isMemoryLocation()) |
Alexey Samsonov | 8f72f7c | 2013-02-27 12:58:24 +0000 | [diff] [blame] | 364 | renderMemorySnippet(Decor, Loc.getMemoryLocation(), Ranges, |
Richard Smith | c242446 | 2013-02-12 22:12:10 +0000 | [diff] [blame] | 365 | NumRanges, Args); |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 366 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 367 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 368 | ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc, |
| 369 | ErrorType Type) |
| 370 | : Opts(Opts), SummaryLoc(SummaryLoc), Type(Type) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 371 | InitAsStandaloneIfNecessary(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 372 | CommonSanitizerReportMutex.Lock(); |
| 373 | } |
| 374 | |
| 375 | ScopedReport::~ScopedReport() { |
| 376 | MaybePrintStackTrace(Opts.pc, Opts.bp); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 377 | MaybeReportErrorSummary(SummaryLoc, Type); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 378 | CommonSanitizerReportMutex.Unlock(); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 379 | if (flags()->halt_on_error) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 380 | Die(); |
| 381 | } |
| 382 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 383 | ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)]; |
| 384 | static SuppressionContext *suppression_ctx = nullptr; |
| 385 | static const char kVptrCheck[] = "vptr_check"; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 386 | static const char *kSuppressionTypes[] = { |
| 387 | #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) FSanitizeFlagName, |
| 388 | #include "ubsan_checks.inc" |
| 389 | #undef UBSAN_CHECK |
| 390 | kVptrCheck, |
| 391 | }; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 392 | |
| 393 | void __ubsan::InitializeSuppressions() { |
| 394 | CHECK_EQ(nullptr, suppression_ctx); |
| 395 | suppression_ctx = new (suppression_placeholder) // NOLINT |
| 396 | SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes)); |
| 397 | suppression_ctx->ParseFromFile(flags()->suppressions); |
| 398 | } |
| 399 | |
| 400 | bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 401 | InitAsStandaloneIfNecessary(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 402 | CHECK(suppression_ctx); |
| 403 | Suppression *s; |
| 404 | return suppression_ctx->Match(TypeName, kVptrCheck, &s); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 405 | } |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 406 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 407 | bool __ubsan::IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename) { |
| 408 | InitAsStandaloneIfNecessary(); |
| 409 | CHECK(suppression_ctx); |
| 410 | const char *SuppType = ConvertTypeToFlagName(ET); |
| 411 | // Fast path: don't symbolize PC if there is no suppressions for given UB |
| 412 | // type. |
| 413 | if (!suppression_ctx->HasSuppressionType(SuppType)) |
| 414 | return false; |
| 415 | Suppression *s = nullptr; |
| 416 | // Suppress by file name known to runtime. |
| 417 | if (Filename != nullptr && suppression_ctx->Match(Filename, SuppType, &s)) |
| 418 | return true; |
| 419 | // Suppress by module name. |
| 420 | if (const char *Module = Symbolizer::GetOrInit()->GetModuleNameForPc(PC)) { |
| 421 | if (suppression_ctx->Match(Module, SuppType, &s)) |
| 422 | return true; |
| 423 | } |
| 424 | // Suppress by function or source file name from debug info. |
| 425 | SymbolizedStackHolder Stack(Symbolizer::GetOrInit()->SymbolizePC(PC)); |
| 426 | const AddressInfo &AI = Stack.get()->info; |
| 427 | return suppression_ctx->Match(AI.function, SuppType, &s) || |
| 428 | suppression_ctx->Match(AI.file, SuppType, &s); |
| 429 | } |
| 430 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 431 | #endif // CAN_SANITIZE_UB |