#ifndef UBSAN_DIAG_H
#define UBSAN_DIAG_H
#include "ubsan_value.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_suppressions.h"
namespace __ubsan {
class ModuleLocation {
const char *ModuleName;
uptr Offset;
public:
ModuleLocation() : ModuleName(0), Offset(0) {}
ModuleLocation(const char *ModuleName, uptr Offset)
: ModuleName(ModuleName), Offset(Offset) {}
const char *getModuleName() const { return ModuleName; }
uptr getOffset() const { return Offset; }
};
typedef uptr MemoryLocation;
class Location {
public:
enum LocationKind { LK_Null, LK_Source, LK_Module, LK_Memory };
private:
LocationKind Kind;
SourceLocation SourceLoc;
ModuleLocation ModuleLoc;
MemoryLocation MemoryLoc;
public:
Location() : Kind(LK_Null) {}
Location(SourceLocation Loc) :
Kind(LK_Source), SourceLoc(Loc) {}
Location(ModuleLocation Loc) :
Kind(LK_Module), ModuleLoc(Loc) {}
Location(MemoryLocation Loc) :
Kind(LK_Memory), MemoryLoc(Loc) {}
LocationKind getKind() const { return Kind; }
bool isSourceLocation() const { return Kind == LK_Source; }
bool isModuleLocation() const { return Kind == LK_Module; }
bool isMemoryLocation() const { return Kind == LK_Memory; }
SourceLocation getSourceLocation() const {
CHECK(isSourceLocation());
return SourceLoc;
}
ModuleLocation getModuleLocation() const {
CHECK(isModuleLocation());
return ModuleLoc;
}
MemoryLocation getMemoryLocation() const {
CHECK(isMemoryLocation());
return MemoryLoc;
}
};
Location getCallerLocation(uptr CallerLoc = GET_CALLER_PC());
Location getFunctionLocation(uptr Loc, const char **FName);
enum DiagLevel {
DL_Error, DL_Note };
class Range {
Location Start, End;
const char *Text;
public:
Range() : Start(), End(), Text() {}
Range(MemoryLocation Start, MemoryLocation End, const char *Text)
: Start(Start), End(End), Text(Text) {}
Location getStart() const { return Start; }
Location getEnd() const { return End; }
const char *getText() const { return Text; }
};
class MangledName {
const char *Name;
public:
MangledName(const char *Name) : Name(Name) {}
const char *getName() const { return Name; }
};
class Diag {
Location Loc;
DiagLevel Level;
const char *Message;
public:
enum ArgKind {
AK_String, AK_Mangled, AK_UInt, AK_SInt, AK_Float, AK_Pointer };
struct Arg {
Arg() {}
Arg(const char *String) : Kind(AK_String), String(String) {}
Arg(MangledName MN) : Kind(AK_Mangled), String(MN.getName()) {}
Arg(UIntMax UInt) : Kind(AK_UInt), UInt(UInt) {}
Arg(SIntMax SInt) : Kind(AK_SInt), SInt(SInt) {}
Arg(FloatMax Float) : Kind(AK_Float), Float(Float) {}
Arg(const void *Pointer) : Kind(AK_Pointer), Pointer(Pointer) {}
ArgKind Kind;
union {
const char *String;
UIntMax UInt;
SIntMax SInt;
FloatMax Float;
const void *Pointer;
};
};
private:
static const unsigned MaxArgs = 5;
static const unsigned MaxRanges = 1;
Arg Args[MaxArgs];
unsigned NumArgs;
Range Ranges[MaxRanges];
unsigned NumRanges;
Diag &AddArg(Arg A) {
CHECK(NumArgs != MaxArgs);
Args[NumArgs++] = A;
return *this;
}
Diag &AddRange(Range A) {
CHECK(NumRanges != MaxRanges);
Ranges[NumRanges++] = A;
return *this;
}
Diag(const Diag &); Diag &operator=(const Diag &);
public:
Diag(Location Loc, DiagLevel Level, const char *Message)
: Loc(Loc), Level(Level), Message(Message), NumArgs(0), NumRanges(0) {}
~Diag();
Diag &operator<<(const char *Str) { return AddArg(Str); }
Diag &operator<<(MangledName MN) { return AddArg(MN); }
Diag &operator<<(unsigned long long V) { return AddArg(UIntMax(V)); }
Diag &operator<<(const void *V) { return AddArg(V); }
Diag &operator<<(const TypeDescriptor &V);
Diag &operator<<(const Value &V);
Diag &operator<<(const Range &R) { return AddRange(R); }
};
struct ReportOptions {
bool DieAfterReport;
uptr pc;
uptr bp;
};
#define GET_REPORT_OPTIONS(die_after_report) \
GET_CALLER_PC_BP; \
ReportOptions Opts = {die_after_report, pc, bp}
class ScopedReport {
ReportOptions Opts;
Location SummaryLoc;
public:
ScopedReport(ReportOptions Opts, Location SummaryLoc);
~ScopedReport();
};
bool MatchSuppression(const char *Str, SuppressionType Type);
}
#endif // UBSAN_DIAG_H