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