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