1 //===-- ubsan_diag.h --------------------------------------------*- C++ -*-===//
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 // Diagnostics emission for Clang's undefined behavior sanitizer.
12 //===----------------------------------------------------------------------===//
16 #include "ubsan_value.h"
17 #include "sanitizer_common/sanitizer_stacktrace.h"
18 #include "sanitizer_common/sanitizer_symbolizer.h"
22 class SymbolizedStackHolder {
23 SymbolizedStack *Stack;
31 explicit SymbolizedStackHolder(SymbolizedStack *Stack = nullptr)
33 ~SymbolizedStackHolder() { clear(); }
34 void reset(SymbolizedStack *S) {
39 const SymbolizedStack *get() const { return Stack; }
42 SymbolizedStack *getSymbolizedLocation(uptr PC);
44 inline SymbolizedStack *getCallerLocation(uptr CallerPC) {
46 uptr PC = StackTrace::GetPreviousInstructionPc(CallerPC);
47 return getSymbolizedLocation(PC);
50 /// A location of some data within the program's address space.
51 typedef uptr MemoryLocation;
53 /// \brief Location at which a diagnostic can be emitted. Either a
54 /// SourceLocation, a MemoryLocation, or a SymbolizedStack.
57 enum LocationKind { LK_Null, LK_Source, LK_Memory, LK_Symbolized };
61 // FIXME: In C++11, wrap these in an anonymous union.
62 SourceLocation SourceLoc;
63 MemoryLocation MemoryLoc;
64 const SymbolizedStack *SymbolizedLoc; // Not owned.
67 Location() : Kind(LK_Null) {}
68 Location(SourceLocation Loc) :
69 Kind(LK_Source), SourceLoc(Loc) {}
70 Location(MemoryLocation Loc) :
71 Kind(LK_Memory), MemoryLoc(Loc) {}
72 // SymbolizedStackHolder must outlive Location object.
73 Location(const SymbolizedStackHolder &Stack) :
74 Kind(LK_Symbolized), SymbolizedLoc(Stack.get()) {}
76 LocationKind getKind() const { return Kind; }
78 bool isSourceLocation() const { return Kind == LK_Source; }
79 bool isMemoryLocation() const { return Kind == LK_Memory; }
80 bool isSymbolizedStack() const { return Kind == LK_Symbolized; }
82 SourceLocation getSourceLocation() const {
83 CHECK(isSourceLocation());
86 MemoryLocation getMemoryLocation() const {
87 CHECK(isMemoryLocation());
90 const SymbolizedStack *getSymbolizedStack() const {
91 CHECK(isSymbolizedStack());
96 /// A diagnostic severity level.
98 DL_Error, ///< An error.
99 DL_Note ///< A note, attached to a prior diagnostic.
102 /// \brief Annotation for a range of locations in a diagnostic.
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; }
116 /// \brief A C++ type name. Really just a strong typedef for 'const char*'.
120 TypeName(const char *Name) : Name(Name) {}
121 const char *getName() const { return Name; }
124 /// \brief Representation of an in-flight diagnostic.
126 /// Temporary \c Diag instances are created by the handler routines to
127 /// accumulate arguments for a diagnostic. The destructor emits the diagnostic
130 /// The location at which the problem occurred.
133 /// The diagnostic level.
136 /// The message which will be emitted, with %0, %1, ... placeholders for
141 /// Kinds of arguments, corresponding to members of \c Arg's union.
143 AK_String, ///< A string argument, displayed as-is.
144 AK_TypeName,///< A C++ type name, possibly demangled before display.
145 AK_UInt, ///< An unsigned integer argument.
146 AK_SInt, ///< A signed integer argument.
147 AK_Float, ///< A floating-point argument.
148 AK_Pointer ///< A pointer argument, displayed in hexadecimal.
151 /// An individual diagnostic message argument.
154 Arg(const char *String) : Kind(AK_String), String(String) {}
155 Arg(TypeName TN) : Kind(AK_TypeName), String(TN.getName()) {}
156 Arg(UIntMax UInt) : Kind(AK_UInt), UInt(UInt) {}
157 Arg(SIntMax SInt) : Kind(AK_SInt), SInt(SInt) {}
158 Arg(FloatMax Float) : Kind(AK_Float), Float(Float) {}
159 Arg(const void *Pointer) : Kind(AK_Pointer), Pointer(Pointer) {}
172 static const unsigned MaxArgs = 5;
173 static const unsigned MaxRanges = 1;
175 /// The arguments which have been added to this diagnostic so far.
179 /// The ranges which have been added to this diagnostic so far.
180 Range Ranges[MaxRanges];
183 Diag &AddArg(Arg A) {
184 CHECK(NumArgs != MaxArgs);
189 Diag &AddRange(Range A) {
190 CHECK(NumRanges != MaxRanges);
191 Ranges[NumRanges++] = A;
195 /// \c Diag objects are not copyable.
196 Diag(const Diag &); // NOT IMPLEMENTED
197 Diag &operator=(const Diag &);
200 Diag(Location Loc, DiagLevel Level, const char *Message)
201 : Loc(Loc), Level(Level), Message(Message), NumArgs(0), NumRanges(0) {}
204 Diag &operator<<(const char *Str) { return AddArg(Str); }
205 Diag &operator<<(TypeName TN) { return AddArg(TN); }
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);
210 Diag &operator<<(const Range &R) { return AddRange(R); }
213 struct ReportOptions {
214 // If FromUnrecoverableHandler is specified, UBSan runtime handler is not
215 // expected to return.
216 bool FromUnrecoverableHandler;
217 /// pc/bp are used to unwind the stack trace.
222 enum class ErrorType {
223 #define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) Name,
224 #include "ubsan_checks.inc"
228 bool ignoreReport(SourceLocation SLoc, ReportOptions Opts, ErrorType ET);
230 #define GET_REPORT_OPTIONS(unrecoverable_handler) \
232 ReportOptions Opts = {unrecoverable_handler, pc, bp}
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.
243 ScopedReport(ReportOptions Opts, Location SummaryLoc, ErrorType Type);
247 void InitializeSuppressions();
248 bool IsVptrCheckSuppressed(const char *TypeName);
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);
253 } // namespace __ubsan
255 #endif // UBSAN_DIAG_H