ubsan_diag.h revision 5f1164955fb28a9bcb826abc195aa2119feb0f97
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 18namespace __ubsan { 19 20/// \brief A location within a loaded module in the program. These are used when 21/// the location can't be resolved to a SourceLocation. 22class ModuleLocation { 23 const char *ModuleName; 24 uptr Offset; 25 26public: 27 ModuleLocation() : ModuleName(0), Offset(0) {} 28 ModuleLocation(const char *ModuleName, uptr Offset) 29 : ModuleName(ModuleName), Offset(Offset) {} 30 const char *getModuleName() const { return ModuleName; } 31 uptr getOffset() const { return Offset; } 32}; 33 34/// A location of some data within the program's address space. 35typedef uptr MemoryLocation; 36 37/// \brief Location at which a diagnostic can be emitted. Either a 38/// SourceLocation, a ModuleLocation, or a MemoryLocation. 39class Location { 40public: 41 enum LocationKind { LK_Null, LK_Source, LK_Module, LK_Memory }; 42 43private: 44 LocationKind Kind; 45 // FIXME: In C++11, wrap these in an anonymous union. 46 SourceLocation SourceLoc; 47 ModuleLocation ModuleLoc; 48 MemoryLocation MemoryLoc; 49 50public: 51 Location() : Kind(LK_Null) {} 52 Location(SourceLocation Loc) : 53 Kind(LK_Source), SourceLoc(Loc) {} 54 Location(ModuleLocation Loc) : 55 Kind(LK_Module), ModuleLoc(Loc) {} 56 Location(MemoryLocation Loc) : 57 Kind(LK_Memory), MemoryLoc(Loc) {} 58 59 LocationKind getKind() const { return Kind; } 60 61 bool isSourceLocation() const { return Kind == LK_Source; } 62 bool isModuleLocation() const { return Kind == LK_Module; } 63 bool isMemoryLocation() const { return Kind == LK_Memory; } 64 65 SourceLocation getSourceLocation() const { 66 CHECK(isSourceLocation()); 67 return SourceLoc; 68 } 69 ModuleLocation getModuleLocation() const { 70 CHECK(isModuleLocation()); 71 return ModuleLoc; 72 } 73 MemoryLocation getMemoryLocation() const { 74 CHECK(isMemoryLocation()); 75 return MemoryLoc; 76 } 77}; 78 79/// Try to obtain a location for the caller. This might fail, and produce either 80/// an invalid location or a module location for the caller. 81Location getCallerLocation(uptr CallerLoc = GET_CALLER_PC()); 82 83/// \brief Representation of an in-flight diagnostic. 84/// 85/// Temporary \c Diag instances are created by the handler routines to 86/// accumulate arguments for a diagnostic. The destructor emits the diagnostic 87/// message. 88class Diag { 89 /// The location at which the problem occurred. 90 Location Loc; 91 92 /// The message which will be emitted, with %0, %1, ... placeholders for 93 /// arguments. 94 const char *Message; 95 96 /// Kinds of arguments, corresponding to members of \c Arg's union. 97 enum ArgKind { 98 AK_String, ///< A string argument, displayed as-is. 99 AK_UInt, ///< An unsigned integer argument. 100 AK_SInt, ///< A signed integer argument. 101 AK_Float, ///< A floating-point argument. 102 AK_Pointer ///< A pointer argument, displayed in hexadecimal. 103 }; 104 105 /// An individual diagnostic message argument. 106 struct Arg { 107 Arg() {} 108 Arg(const char *String) : Kind(AK_String), String(String) {} 109 Arg(UIntMax UInt) : Kind(AK_UInt), UInt(UInt) {} 110 Arg(SIntMax SInt) : Kind(AK_SInt), SInt(SInt) {} 111 Arg(FloatMax Float) : Kind(AK_Float), Float(Float) {} 112 Arg(const void *Pointer) : Kind(AK_Pointer), Pointer(Pointer) {} 113 114 ArgKind Kind; 115 union { 116 const char *String; 117 UIntMax UInt; 118 SIntMax SInt; 119 FloatMax Float; 120 const void *Pointer; 121 }; 122 }; 123 124 static const unsigned MaxArgs = 5; 125 126 /// The arguments which have been added to this diagnostic so far. 127 Arg Args[MaxArgs]; 128 unsigned NumArgs; 129 130 Diag &AddArg(Arg A) { 131 CHECK(NumArgs != MaxArgs); 132 Args[NumArgs++] = A; 133 return *this; 134 } 135 136 /// \c Diag objects are not copyable. 137 Diag(const Diag &); // NOT IMPLEMENTED 138 Diag &operator=(const Diag &); 139 140public: 141 Diag(Location Loc, const char *Message) 142 : Loc(Loc), Message(Message), NumArgs(0) {} 143 ~Diag(); 144 145 Diag &operator<<(const char *Str) { return AddArg(Str); } 146 Diag &operator<<(unsigned long long V) { return AddArg(UIntMax(V)); } 147 Diag &operator<<(const void *V) { return AddArg(V); } 148 Diag &operator<<(const TypeDescriptor &V); 149 Diag &operator<<(const Value &V); 150}; 151 152} // namespace __ubsan 153 154#endif // UBSAN_DIAG_H 155