2013-08-30 18:12:58 +02:00
|
|
|
//===-- ubsan_diag.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-08-14 10:47:11 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-30 18:12:58 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Diagnostics emission for Clang's undefined behavior sanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef UBSAN_DIAG_H
|
|
|
|
#define UBSAN_DIAG_H
|
|
|
|
|
|
|
|
#include "ubsan_value.h"
|
2014-09-23 19:59:53 +02:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
2015-10-21 09:32:45 +02:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
namespace __ubsan {
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
class SymbolizedStackHolder {
|
|
|
|
SymbolizedStack *Stack;
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
if (Stack)
|
|
|
|
Stack->ClearAll();
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
public:
|
2015-10-21 09:32:45 +02:00
|
|
|
explicit SymbolizedStackHolder(SymbolizedStack *Stack = nullptr)
|
|
|
|
: Stack(Stack) {}
|
|
|
|
~SymbolizedStackHolder() { clear(); }
|
|
|
|
void reset(SymbolizedStack *S) {
|
|
|
|
if (Stack != S)
|
|
|
|
clear();
|
|
|
|
Stack = S;
|
|
|
|
}
|
|
|
|
const SymbolizedStack *get() const { return Stack; }
|
2013-08-30 18:12:58 +02:00
|
|
|
};
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
SymbolizedStack *getSymbolizedLocation(uptr PC);
|
|
|
|
|
|
|
|
inline SymbolizedStack *getCallerLocation(uptr CallerPC) {
|
|
|
|
CHECK(CallerPC);
|
|
|
|
uptr PC = StackTrace::GetPreviousInstructionPc(CallerPC);
|
|
|
|
return getSymbolizedLocation(PC);
|
|
|
|
}
|
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
/// 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
|
2015-10-21 09:32:45 +02:00
|
|
|
/// SourceLocation, a MemoryLocation, or a SymbolizedStack.
|
2013-08-30 18:12:58 +02:00
|
|
|
class Location {
|
|
|
|
public:
|
2015-10-21 09:32:45 +02:00
|
|
|
enum LocationKind { LK_Null, LK_Source, LK_Memory, LK_Symbolized };
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
LocationKind Kind;
|
|
|
|
// FIXME: In C++11, wrap these in an anonymous union.
|
|
|
|
SourceLocation SourceLoc;
|
|
|
|
MemoryLocation MemoryLoc;
|
2015-10-21 09:32:45 +02:00
|
|
|
const SymbolizedStack *SymbolizedLoc; // Not owned.
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
Location() : Kind(LK_Null) {}
|
|
|
|
Location(SourceLocation Loc) :
|
|
|
|
Kind(LK_Source), SourceLoc(Loc) {}
|
|
|
|
Location(MemoryLocation Loc) :
|
|
|
|
Kind(LK_Memory), MemoryLoc(Loc) {}
|
2015-10-21 09:32:45 +02:00
|
|
|
// SymbolizedStackHolder must outlive Location object.
|
|
|
|
Location(const SymbolizedStackHolder &Stack) :
|
|
|
|
Kind(LK_Symbolized), SymbolizedLoc(Stack.get()) {}
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
LocationKind getKind() const { return Kind; }
|
|
|
|
|
|
|
|
bool isSourceLocation() const { return Kind == LK_Source; }
|
|
|
|
bool isMemoryLocation() const { return Kind == LK_Memory; }
|
2015-10-21 09:32:45 +02:00
|
|
|
bool isSymbolizedStack() const { return Kind == LK_Symbolized; }
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
SourceLocation getSourceLocation() const {
|
|
|
|
CHECK(isSourceLocation());
|
|
|
|
return SourceLoc;
|
|
|
|
}
|
|
|
|
MemoryLocation getMemoryLocation() const {
|
|
|
|
CHECK(isMemoryLocation());
|
|
|
|
return MemoryLoc;
|
|
|
|
}
|
2015-10-21 09:32:45 +02:00
|
|
|
const SymbolizedStack *getSymbolizedStack() const {
|
|
|
|
CHECK(isSymbolizedStack());
|
|
|
|
return SymbolizedLoc;
|
|
|
|
}
|
2013-08-30 18:12:58 +02: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; }
|
|
|
|
};
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
/// \brief A C++ type name. Really just a strong typedef for 'const char*'.
|
|
|
|
class TypeName {
|
2013-08-30 18:12:58 +02:00
|
|
|
const char *Name;
|
|
|
|
public:
|
2015-10-21 09:32:45 +02:00
|
|
|
TypeName(const char *Name) : Name(Name) {}
|
2013-08-30 18:12:58 +02:00
|
|
|
const char *getName() const { return Name; }
|
|
|
|
};
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
enum class ErrorType {
|
|
|
|
#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) Name,
|
|
|
|
#include "ubsan_checks.inc"
|
|
|
|
#undef UBSAN_CHECK
|
|
|
|
};
|
|
|
|
|
2013-08-30 18:12:58 +02: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 {
|
|
|
|
/// The location at which the problem occurred.
|
|
|
|
Location Loc;
|
|
|
|
|
|
|
|
/// The diagnostic level.
|
|
|
|
DiagLevel Level;
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
/// The error type.
|
|
|
|
ErrorType ET;
|
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
/// The message which will be emitted, with %0, %1, ... placeholders for
|
|
|
|
/// arguments.
|
|
|
|
const char *Message;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Kinds of arguments, corresponding to members of \c Arg's union.
|
|
|
|
enum ArgKind {
|
|
|
|
AK_String, ///< A string argument, displayed as-is.
|
2015-10-21 09:32:45 +02:00
|
|
|
AK_TypeName,///< A C++ type name, possibly demangled before display.
|
2013-08-30 18:12:58 +02:00
|
|
|
AK_UInt, ///< An unsigned integer argument.
|
|
|
|
AK_SInt, ///< A signed integer argument.
|
|
|
|
AK_Float, ///< A floating-point argument.
|
|
|
|
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) {}
|
2015-10-21 09:32:45 +02:00
|
|
|
Arg(TypeName TN) : Kind(AK_TypeName), String(TN.getName()) {}
|
2013-08-30 18:12:58 +02:00
|
|
|
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:
|
2018-10-31 12:14:23 +01:00
|
|
|
static const unsigned MaxArgs = 8;
|
2013-08-30 18:12:58 +02:00
|
|
|
static const unsigned MaxRanges = 1;
|
|
|
|
|
|
|
|
/// The arguments which have been added to this diagnostic so far.
|
|
|
|
Arg Args[MaxArgs];
|
|
|
|
unsigned NumArgs;
|
|
|
|
|
|
|
|
/// The ranges which have been added to this diagnostic so far.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \c Diag objects are not copyable.
|
|
|
|
Diag(const Diag &); // NOT IMPLEMENTED
|
|
|
|
Diag &operator=(const Diag &);
|
|
|
|
|
|
|
|
public:
|
2018-10-31 12:14:23 +01:00
|
|
|
Diag(Location Loc, DiagLevel Level, ErrorType ET, const char *Message)
|
|
|
|
: Loc(Loc), Level(Level), ET(ET), Message(Message), NumArgs(0),
|
|
|
|
NumRanges(0) {}
|
2013-08-30 18:12:58 +02:00
|
|
|
~Diag();
|
|
|
|
|
|
|
|
Diag &operator<<(const char *Str) { return AddArg(Str); }
|
2015-10-21 09:32:45 +02:00
|
|
|
Diag &operator<<(TypeName TN) { return AddArg(TN); }
|
2013-08-30 18:12:58 +02: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);
|
|
|
|
Diag &operator<<(const Range &R) { return AddRange(R); }
|
|
|
|
};
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
struct ReportOptions {
|
2016-11-08 23:04:09 +01:00
|
|
|
// If FromUnrecoverableHandler is specified, UBSan runtime handler is not
|
|
|
|
// expected to return.
|
|
|
|
bool FromUnrecoverableHandler;
|
2014-09-23 19:59:53 +02:00
|
|
|
/// pc/bp are used to unwind the stack trace.
|
|
|
|
uptr pc;
|
|
|
|
uptr bp;
|
|
|
|
};
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
bool ignoreReport(SourceLocation SLoc, ReportOptions Opts, ErrorType ET);
|
|
|
|
|
|
|
|
#define GET_REPORT_OPTIONS(unrecoverable_handler) \
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_CALLER_PC_BP; \
|
2016-11-08 23:04:09 +01:00
|
|
|
ReportOptions Opts = {unrecoverable_handler, pc, bp}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
/// \brief Instantiate this class before printing diagnostics in the error
|
|
|
|
/// report. This class ensures that reports from different threads and from
|
|
|
|
/// different sanitizers won't be mixed.
|
|
|
|
class ScopedReport {
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
struct Initializer {
|
|
|
|
Initializer();
|
|
|
|
};
|
|
|
|
Initializer initializer_;
|
|
|
|
ScopedErrorReportLock report_lock_;
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
ReportOptions Opts;
|
|
|
|
Location SummaryLoc;
|
2015-10-21 09:32:45 +02:00
|
|
|
ErrorType Type;
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
public:
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport(ReportOptions Opts, Location SummaryLoc, ErrorType Type);
|
2014-09-23 19:59:53 +02:00
|
|
|
~ScopedReport();
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
|
|
|
|
static void CheckLocked() { ScopedErrorReportLock::CheckLocked(); }
|
2014-09-23 19:59:53 +02:00
|
|
|
};
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
void InitializeSuppressions();
|
|
|
|
bool IsVptrCheckSuppressed(const char *TypeName);
|
2016-11-08 23:04:09 +01:00
|
|
|
// Sometimes UBSan runtime can know filename from handlers arguments, even if
|
|
|
|
// debug info is missing.
|
|
|
|
bool IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
} // namespace __ubsan
|
|
|
|
|
|
|
|
#endif // UBSAN_DIAG_H
|