Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 1 | //===-- ubsan_diag.h --------------------------------------------*- C++ -*-===// |
| 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 | // Diagnostics emission for Clang's undefined behavior sanitizer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef UBSAN_DIAG_H |
| 14 | #define UBSAN_DIAG_H |
| 15 | |
| 16 | #include "ubsan_value.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __ubsan { |
| 21 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 22 | class SymbolizedStackHolder { |
| 23 | SymbolizedStack *Stack; |
| 24 | |
| 25 | void clear() { |
| 26 | if (Stack) |
| 27 | Stack->ClearAll(); |
| 28 | } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 29 | |
| 30 | public: |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 31 | explicit SymbolizedStackHolder(SymbolizedStack *Stack = nullptr) |
| 32 | : Stack(Stack) {} |
| 33 | ~SymbolizedStackHolder() { clear(); } |
| 34 | void reset(SymbolizedStack *S) { |
| 35 | if (Stack != S) |
| 36 | clear(); |
| 37 | Stack = S; |
| 38 | } |
| 39 | const SymbolizedStack *get() const { return Stack; } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 42 | SymbolizedStack *getSymbolizedLocation(uptr PC); |
| 43 | |
| 44 | inline SymbolizedStack *getCallerLocation(uptr CallerPC) { |
| 45 | CHECK(CallerPC); |
| 46 | uptr PC = StackTrace::GetPreviousInstructionPc(CallerPC); |
| 47 | return getSymbolizedLocation(PC); |
| 48 | } |
| 49 | |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 50 | /// A location of some data within the program's address space. |
| 51 | typedef uptr MemoryLocation; |
| 52 | |
| 53 | /// \brief Location at which a diagnostic can be emitted. Either a |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 54 | /// SourceLocation, a MemoryLocation, or a SymbolizedStack. |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 55 | class Location { |
| 56 | public: |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 57 | enum LocationKind { LK_Null, LK_Source, LK_Memory, LK_Symbolized }; |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | LocationKind Kind; |
| 61 | // FIXME: In C++11, wrap these in an anonymous union. |
| 62 | SourceLocation SourceLoc; |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 63 | MemoryLocation MemoryLoc; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 64 | const SymbolizedStack *SymbolizedLoc; // Not owned. |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
| 67 | Location() : Kind(LK_Null) {} |
| 68 | Location(SourceLocation Loc) : |
| 69 | Kind(LK_Source), SourceLoc(Loc) {} |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 70 | Location(MemoryLocation Loc) : |
| 71 | Kind(LK_Memory), MemoryLoc(Loc) {} |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 72 | // SymbolizedStackHolder must outlive Location object. |
| 73 | Location(const SymbolizedStackHolder &Stack) : |
| 74 | Kind(LK_Symbolized), SymbolizedLoc(Stack.get()) {} |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 75 | |
| 76 | LocationKind getKind() const { return Kind; } |
| 77 | |
| 78 | bool isSourceLocation() const { return Kind == LK_Source; } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 79 | bool isMemoryLocation() const { return Kind == LK_Memory; } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 80 | bool isSymbolizedStack() const { return Kind == LK_Symbolized; } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 81 | |
| 82 | SourceLocation getSourceLocation() const { |
| 83 | CHECK(isSourceLocation()); |
| 84 | return SourceLoc; |
| 85 | } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 86 | MemoryLocation getMemoryLocation() const { |
| 87 | CHECK(isMemoryLocation()); |
| 88 | return MemoryLoc; |
| 89 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 90 | const SymbolizedStack *getSymbolizedStack() const { |
| 91 | CHECK(isSymbolizedStack()); |
| 92 | return SymbolizedLoc; |
| 93 | } |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 96 | /// A diagnostic severity level. |
| 97 | enum DiagLevel { |
| 98 | DL_Error, ///< An error. |
| 99 | DL_Note ///< A note, attached to a prior diagnostic. |
| 100 | }; |
| 101 | |
| 102 | /// \brief Annotation for a range of locations in a diagnostic. |
| 103 | class Range { |
| 104 | Location Start, End; |
| 105 | const char *Text; |
| 106 | |
| 107 | public: |
| 108 | Range() : Start(), End(), Text() {} |
| 109 | Range(MemoryLocation Start, MemoryLocation End, const char *Text) |
| 110 | : Start(Start), End(End), Text(Text) {} |
| 111 | Location getStart() const { return Start; } |
| 112 | Location getEnd() const { return End; } |
| 113 | const char *getText() const { return Text; } |
| 114 | }; |
| 115 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 116 | /// \brief A C++ type name. Really just a strong typedef for 'const char*'. |
| 117 | class TypeName { |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 118 | const char *Name; |
| 119 | public: |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 120 | TypeName(const char *Name) : Name(Name) {} |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 121 | const char *getName() const { return Name; } |
| 122 | }; |
| 123 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 124 | /// \brief Representation of an in-flight diagnostic. |
| 125 | /// |
| 126 | /// Temporary \c Diag instances are created by the handler routines to |
| 127 | /// accumulate arguments for a diagnostic. The destructor emits the diagnostic |
| 128 | /// message. |
| 129 | class Diag { |
Richard Smith | 5f11649 | 2012-12-18 04:23:18 +0000 | [diff] [blame] | 130 | /// The location at which the problem occurred. |
| 131 | Location Loc; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 132 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 133 | /// The diagnostic level. |
| 134 | DiagLevel Level; |
| 135 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 136 | /// The message which will be emitted, with %0, %1, ... placeholders for |
| 137 | /// arguments. |
| 138 | const char *Message; |
| 139 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 140 | public: |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 141 | /// Kinds of arguments, corresponding to members of \c Arg's union. |
| 142 | enum ArgKind { |
| 143 | AK_String, ///< A string argument, displayed as-is. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 144 | AK_TypeName,///< A C++ type name, possibly demangled before display. |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 145 | AK_UInt, ///< An unsigned integer argument. |
| 146 | AK_SInt, ///< A signed integer argument. |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 147 | AK_Float, ///< A floating-point argument. |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 148 | AK_Pointer ///< A pointer argument, displayed in hexadecimal. |
| 149 | }; |
| 150 | |
| 151 | /// An individual diagnostic message argument. |
| 152 | struct Arg { |
| 153 | Arg() {} |
| 154 | Arg(const char *String) : Kind(AK_String), String(String) {} |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 155 | Arg(TypeName TN) : Kind(AK_TypeName), String(TN.getName()) {} |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 156 | Arg(UIntMax UInt) : Kind(AK_UInt), UInt(UInt) {} |
| 157 | Arg(SIntMax SInt) : Kind(AK_SInt), SInt(SInt) {} |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 158 | Arg(FloatMax Float) : Kind(AK_Float), Float(Float) {} |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 159 | Arg(const void *Pointer) : Kind(AK_Pointer), Pointer(Pointer) {} |
| 160 | |
| 161 | ArgKind Kind; |
| 162 | union { |
| 163 | const char *String; |
| 164 | UIntMax UInt; |
| 165 | SIntMax SInt; |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 166 | FloatMax Float; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 167 | const void *Pointer; |
| 168 | }; |
| 169 | }; |
| 170 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 171 | private: |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 172 | static const unsigned MaxArgs = 5; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 173 | static const unsigned MaxRanges = 1; |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 174 | |
| 175 | /// The arguments which have been added to this diagnostic so far. |
| 176 | Arg Args[MaxArgs]; |
| 177 | unsigned NumArgs; |
| 178 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 179 | /// The ranges which have been added to this diagnostic so far. |
| 180 | Range Ranges[MaxRanges]; |
| 181 | unsigned NumRanges; |
| 182 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 183 | Diag &AddArg(Arg A) { |
| 184 | CHECK(NumArgs != MaxArgs); |
| 185 | Args[NumArgs++] = A; |
| 186 | return *this; |
| 187 | } |
| 188 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 189 | Diag &AddRange(Range A) { |
| 190 | CHECK(NumRanges != MaxRanges); |
| 191 | Ranges[NumRanges++] = A; |
| 192 | return *this; |
| 193 | } |
| 194 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 195 | /// \c Diag objects are not copyable. |
| 196 | Diag(const Diag &); // NOT IMPLEMENTED |
| 197 | Diag &operator=(const Diag &); |
| 198 | |
| 199 | public: |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 200 | Diag(Location Loc, DiagLevel Level, const char *Message) |
| 201 | : Loc(Loc), Level(Level), Message(Message), NumArgs(0), NumRanges(0) {} |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 202 | ~Diag(); |
| 203 | |
| 204 | Diag &operator<<(const char *Str) { return AddArg(Str); } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 205 | Diag &operator<<(TypeName TN) { return AddArg(TN); } |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 206 | Diag &operator<<(unsigned long long V) { return AddArg(UIntMax(V)); } |
| 207 | Diag &operator<<(const void *V) { return AddArg(V); } |
| 208 | Diag &operator<<(const TypeDescriptor &V); |
| 209 | Diag &operator<<(const Value &V); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 210 | Diag &operator<<(const Range &R) { return AddRange(R); } |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 213 | struct ReportOptions { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 214 | // If FromUnrecoverableHandler is specified, UBSan runtime handler is not |
| 215 | // expected to return. |
| 216 | bool FromUnrecoverableHandler; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 217 | /// pc/bp are used to unwind the stack trace. |
| 218 | uptr pc; |
| 219 | uptr bp; |
| 220 | }; |
| 221 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 222 | enum class ErrorType { |
| 223 | #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) Name, |
| 224 | #include "ubsan_checks.inc" |
| 225 | #undef UBSAN_CHECK |
| 226 | }; |
| 227 | |
| 228 | bool ignoreReport(SourceLocation SLoc, ReportOptions Opts, ErrorType ET); |
| 229 | |
| 230 | #define GET_REPORT_OPTIONS(unrecoverable_handler) \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 231 | GET_CALLER_PC_BP; \ |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 232 | ReportOptions Opts = {unrecoverable_handler, pc, bp} |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 233 | |
| 234 | /// \brief Instantiate this class before printing diagnostics in the error |
| 235 | /// report. This class ensures that reports from different threads and from |
| 236 | /// different sanitizers won't be mixed. |
| 237 | class ScopedReport { |
| 238 | ReportOptions Opts; |
| 239 | Location SummaryLoc; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 240 | ErrorType Type; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 241 | |
| 242 | public: |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 243 | ScopedReport(ReportOptions Opts, Location SummaryLoc, ErrorType Type); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 244 | ~ScopedReport(); |
| 245 | }; |
| 246 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 247 | void InitializeSuppressions(); |
| 248 | bool IsVptrCheckSuppressed(const char *TypeName); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 249 | // Sometimes UBSan runtime can know filename from handlers arguments, even if |
| 250 | // debug info is missing. |
| 251 | bool IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 252 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 253 | } // namespace __ubsan |
| 254 | |
| 255 | #endif // UBSAN_DIAG_H |