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