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