2012-10-10 03:34:32 +08:00
|
|
|
//===-- ubsan_diag.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Diagnostics emission for Clang's undefined behavior sanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef UBSAN_DIAG_H
|
|
|
|
#define UBSAN_DIAG_H
|
|
|
|
|
|
|
|
#include "ubsan_value.h"
|
2014-07-24 02:44:54 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
2014-08-05 09:24:22 +08:00
|
|
|
#include "sanitizer_common/sanitizer_suppressions.h"
|
2012-10-10 03:34:32 +08:00
|
|
|
|
|
|
|
namespace __ubsan {
|
|
|
|
|
2012-12-18 12:23:18 +08:00
|
|
|
/// \brief A location within a loaded module in the program. These are used when
|
|
|
|
/// the location can't be resolved to a SourceLocation.
|
|
|
|
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; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A location of some data within the program's address space.
|
|
|
|
typedef uptr MemoryLocation;
|
|
|
|
|
|
|
|
/// \brief Location at which a diagnostic can be emitted. Either a
|
|
|
|
/// SourceLocation, a ModuleLocation, or a MemoryLocation.
|
|
|
|
class Location {
|
|
|
|
public:
|
|
|
|
enum LocationKind { LK_Null, LK_Source, LK_Module, LK_Memory };
|
|
|
|
|
|
|
|
private:
|
|
|
|
LocationKind Kind;
|
|
|
|
// FIXME: In C++11, wrap these in an anonymous union.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Try to obtain a location for the caller. This might fail, and produce either
|
|
|
|
/// an invalid location or a module location for the caller.
|
2015-02-11 03:50:20 +08:00
|
|
|
Location getCallerLocation(uptr CallerPC);
|
2012-12-18 12:23:18 +08:00
|
|
|
|
2013-10-21 05:29:46 +08:00
|
|
|
/// Try to obtain a location for the given function pointer. This might fail,
|
|
|
|
/// and produce either an invalid location or a module location for the caller.
|
|
|
|
/// If FName is non-null and the name of the function is known, set *FName to
|
|
|
|
/// the function name, otherwise *FName is unchanged.
|
2015-02-11 03:50:20 +08:00
|
|
|
Location getFunctionLocation(uptr PC, const char **FName);
|
2013-10-21 05:29:46 +08:00
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
/// A diagnostic severity level.
|
|
|
|
enum DiagLevel {
|
|
|
|
DL_Error, ///< An error.
|
|
|
|
DL_Note ///< A note, attached to a prior diagnostic.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Annotation for a range of locations in a diagnostic.
|
|
|
|
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; }
|
|
|
|
};
|
|
|
|
|
2012-12-18 17:30:21 +08:00
|
|
|
/// \brief A mangled C++ name. Really just a strong typedef for 'const char*'.
|
|
|
|
class MangledName {
|
|
|
|
const char *Name;
|
|
|
|
public:
|
|
|
|
MangledName(const char *Name) : Name(Name) {}
|
|
|
|
const char *getName() const { return Name; }
|
|
|
|
};
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
/// \brief Representation of an in-flight diagnostic.
|
|
|
|
///
|
|
|
|
/// Temporary \c Diag instances are created by the handler routines to
|
|
|
|
/// accumulate arguments for a diagnostic. The destructor emits the diagnostic
|
|
|
|
/// message.
|
|
|
|
class Diag {
|
2012-12-18 12:23:18 +08:00
|
|
|
/// The location at which the problem occurred.
|
|
|
|
Location Loc;
|
2012-10-10 03:34:32 +08:00
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
/// The diagnostic level.
|
|
|
|
DiagLevel Level;
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
/// The message which will be emitted, with %0, %1, ... placeholders for
|
|
|
|
/// arguments.
|
|
|
|
const char *Message;
|
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
public:
|
2012-10-10 03:34:32 +08:00
|
|
|
/// Kinds of arguments, corresponding to members of \c Arg's union.
|
|
|
|
enum ArgKind {
|
|
|
|
AK_String, ///< A string argument, displayed as-is.
|
2012-12-18 17:30:21 +08:00
|
|
|
AK_Mangled,///< A C++ mangled name, demangled before display.
|
2012-10-10 03:34:32 +08:00
|
|
|
AK_UInt, ///< An unsigned integer argument.
|
|
|
|
AK_SInt, ///< A signed integer argument.
|
2012-10-13 06:57:15 +08:00
|
|
|
AK_Float, ///< A floating-point argument.
|
2012-10-10 03:34:32 +08:00
|
|
|
AK_Pointer ///< A pointer argument, displayed in hexadecimal.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An individual diagnostic message argument.
|
|
|
|
struct Arg {
|
|
|
|
Arg() {}
|
|
|
|
Arg(const char *String) : Kind(AK_String), String(String) {}
|
2012-12-18 17:30:21 +08:00
|
|
|
Arg(MangledName MN) : Kind(AK_Mangled), String(MN.getName()) {}
|
2012-10-10 03:34:32 +08:00
|
|
|
Arg(UIntMax UInt) : Kind(AK_UInt), UInt(UInt) {}
|
|
|
|
Arg(SIntMax SInt) : Kind(AK_SInt), SInt(SInt) {}
|
2012-10-13 06:57:15 +08:00
|
|
|
Arg(FloatMax Float) : Kind(AK_Float), Float(Float) {}
|
2012-10-10 03:34:32 +08:00
|
|
|
Arg(const void *Pointer) : Kind(AK_Pointer), Pointer(Pointer) {}
|
|
|
|
|
|
|
|
ArgKind Kind;
|
|
|
|
union {
|
|
|
|
const char *String;
|
|
|
|
UIntMax UInt;
|
|
|
|
SIntMax SInt;
|
2012-10-13 06:57:15 +08:00
|
|
|
FloatMax Float;
|
2012-10-10 03:34:32 +08:00
|
|
|
const void *Pointer;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
private:
|
2012-10-10 03:34:32 +08:00
|
|
|
static const unsigned MaxArgs = 5;
|
2012-12-18 14:30:32 +08:00
|
|
|
static const unsigned MaxRanges = 1;
|
2012-10-10 03:34:32 +08:00
|
|
|
|
|
|
|
/// The arguments which have been added to this diagnostic so far.
|
|
|
|
Arg Args[MaxArgs];
|
|
|
|
unsigned NumArgs;
|
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
/// The ranges which have been added to this diagnostic so far.
|
|
|
|
Range Ranges[MaxRanges];
|
|
|
|
unsigned NumRanges;
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
Diag &AddArg(Arg A) {
|
|
|
|
CHECK(NumArgs != MaxArgs);
|
|
|
|
Args[NumArgs++] = A;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
Diag &AddRange(Range A) {
|
|
|
|
CHECK(NumRanges != MaxRanges);
|
|
|
|
Ranges[NumRanges++] = A;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
/// \c Diag objects are not copyable.
|
|
|
|
Diag(const Diag &); // NOT IMPLEMENTED
|
|
|
|
Diag &operator=(const Diag &);
|
|
|
|
|
|
|
|
public:
|
2012-12-18 14:30:32 +08:00
|
|
|
Diag(Location Loc, DiagLevel Level, const char *Message)
|
|
|
|
: Loc(Loc), Level(Level), Message(Message), NumArgs(0), NumRanges(0) {}
|
2012-10-10 03:34:32 +08:00
|
|
|
~Diag();
|
|
|
|
|
|
|
|
Diag &operator<<(const char *Str) { return AddArg(Str); }
|
2012-12-18 17:30:21 +08:00
|
|
|
Diag &operator<<(MangledName MN) { return AddArg(MN); }
|
2012-10-10 03:34:32 +08:00
|
|
|
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);
|
2012-12-18 14:30:32 +08:00
|
|
|
Diag &operator<<(const Range &R) { return AddRange(R); }
|
2012-10-10 03:34:32 +08:00
|
|
|
};
|
|
|
|
|
2014-08-23 05:42:04 +08:00
|
|
|
struct ReportOptions {
|
|
|
|
/// If DieAfterReport is specified, UBSan will terminate the program after the
|
|
|
|
/// report is printed.
|
|
|
|
bool DieAfterReport;
|
|
|
|
/// pc/bp are used to unwind the stack trace.
|
|
|
|
uptr pc;
|
|
|
|
uptr bp;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define GET_REPORT_OPTIONS(die_after_report) \
|
|
|
|
GET_CALLER_PC_BP; \
|
|
|
|
ReportOptions Opts = {die_after_report, pc, bp}
|
2014-07-24 02:44:54 +08:00
|
|
|
|
2014-07-30 09:49:19 +08:00
|
|
|
/// \brief Instantiate this class before printing diagnostics in the error
|
|
|
|
/// report. This class ensures that reports from different threads and from
|
2014-08-23 05:42:04 +08:00
|
|
|
/// different sanitizers won't be mixed.
|
2014-07-30 09:49:19 +08:00
|
|
|
class ScopedReport {
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts;
|
2014-09-20 02:33:45 +08:00
|
|
|
Location SummaryLoc;
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
public:
|
2014-09-20 02:33:45 +08:00
|
|
|
ScopedReport(ReportOptions Opts, Location SummaryLoc);
|
2014-07-30 09:49:19 +08:00
|
|
|
~ScopedReport();
|
|
|
|
};
|
|
|
|
|
2014-08-05 09:24:22 +08:00
|
|
|
bool MatchSuppression(const char *Str, SuppressionType Type);
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
} // namespace __ubsan
|
|
|
|
|
|
|
|
#endif // UBSAN_DIAG_H
|