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