2012-10-10 03:34:32 +08:00
|
|
|
//===-- ubsan_diag.cc -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Diagnostic reporting for the UBSan runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#include "ubsan_platform.h"
|
|
|
|
#if CAN_SANITIZE_UB
|
2012-10-10 03:34:32 +08:00
|
|
|
#include "ubsan_diag.h"
|
2014-07-26 06:24:34 +08:00
|
|
|
#include "ubsan_init.h"
|
2014-07-24 02:44:54 +08:00
|
|
|
#include "ubsan_flags.h"
|
2015-02-21 01:41:59 +08:00
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
2013-02-27 20:58:24 +08:00
|
|
|
#include "sanitizer_common/sanitizer_report_decorator.h"
|
2012-12-18 12:23:18 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
2014-11-06 06:44:36 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace_printer.h"
|
2015-02-21 01:41:59 +08:00
|
|
|
#include "sanitizer_common/sanitizer_suppressions.h"
|
2012-12-18 12:23:18 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
2012-10-10 03:34:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace __ubsan;
|
|
|
|
|
2017-09-15 16:11:53 +08:00
|
|
|
void __ubsan::GetStackTraceWithPcBpAndContext(BufferedStackTrace *stack,
|
|
|
|
uptr max_depth, uptr pc, uptr bp,
|
|
|
|
void *context, bool fast) {
|
|
|
|
uptr top = 0;
|
|
|
|
uptr bottom = 0;
|
|
|
|
if (fast)
|
|
|
|
GetThreadStackTopAndBottom(false, &top, &bottom);
|
|
|
|
stack->Unwind(max_depth, pc, bp, context, top, bottom, fast);
|
|
|
|
}
|
|
|
|
|
2014-08-23 05:42:04 +08:00
|
|
|
static void MaybePrintStackTrace(uptr pc, uptr bp) {
|
2015-04-02 06:42:36 +08:00
|
|
|
// We assume that flags are already parsed, as UBSan runtime
|
2014-07-24 02:44:54 +08:00
|
|
|
// will definitely be called when we print the first diagnostics message.
|
|
|
|
if (!flags()->print_stacktrace)
|
|
|
|
return;
|
2017-05-02 05:41:01 +08:00
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
BufferedStackTrace stack;
|
2017-09-15 16:11:53 +08:00
|
|
|
GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, nullptr,
|
|
|
|
common_flags()->fast_unwind_on_fatal);
|
2014-07-24 02:44:54 +08:00
|
|
|
stack.Print();
|
|
|
|
}
|
|
|
|
|
2015-08-25 07:18:49 +08:00
|
|
|
static const char *ConvertTypeToString(ErrorType Type) {
|
|
|
|
switch (Type) {
|
2015-12-09 07:29:36 +08:00
|
|
|
#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
|
2015-08-25 07:18:49 +08:00
|
|
|
case ErrorType::Name: \
|
|
|
|
return SummaryKind;
|
|
|
|
#include "ubsan_checks.inc"
|
|
|
|
#undef UBSAN_CHECK
|
|
|
|
}
|
|
|
|
UNREACHABLE("unknown ErrorType!");
|
|
|
|
}
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
static const char *ConvertTypeToFlagName(ErrorType Type) {
|
|
|
|
switch (Type) {
|
|
|
|
#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
|
|
|
|
case ErrorType::Name: \
|
|
|
|
return FSanitizeFlagName;
|
|
|
|
#include "ubsan_checks.inc"
|
|
|
|
#undef UBSAN_CHECK
|
|
|
|
}
|
|
|
|
UNREACHABLE("unknown ErrorType!");
|
|
|
|
}
|
|
|
|
|
2015-08-25 07:18:49 +08:00
|
|
|
static void MaybeReportErrorSummary(Location Loc, ErrorType Type) {
|
2014-09-20 02:33:45 +08:00
|
|
|
if (!common_flags()->print_summary)
|
|
|
|
return;
|
2015-08-25 07:18:49 +08:00
|
|
|
if (!flags()->report_error_type)
|
|
|
|
Type = ErrorType::GenericUB;
|
|
|
|
const char *ErrorKind = ConvertTypeToString(Type);
|
2014-09-20 02:33:45 +08:00
|
|
|
if (Loc.isSourceLocation()) {
|
|
|
|
SourceLocation SLoc = Loc.getSourceLocation();
|
|
|
|
if (!SLoc.isInvalid()) {
|
2015-02-27 10:29:25 +08:00
|
|
|
AddressInfo AI;
|
|
|
|
AI.file = internal_strdup(SLoc.getFilename());
|
|
|
|
AI.line = SLoc.getLine();
|
|
|
|
AI.column = SLoc.getColumn();
|
|
|
|
AI.function = internal_strdup(""); // Avoid printing ?? as function name.
|
2017-04-15 02:24:35 +08:00
|
|
|
ReportErrorSummary(ErrorKind, AI, GetSanititizerToolName());
|
2015-02-27 10:29:25 +08:00
|
|
|
AI.Clear();
|
2014-09-20 02:33:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-04-23 09:08:31 +08:00
|
|
|
} else if (Loc.isSymbolizedStack()) {
|
|
|
|
const AddressInfo &AI = Loc.getSymbolizedStack()->info;
|
2017-04-15 02:24:35 +08:00
|
|
|
ReportErrorSummary(ErrorKind, AI, GetSanititizerToolName());
|
2015-04-23 09:08:31 +08:00
|
|
|
return;
|
2014-09-20 02:33:45 +08:00
|
|
|
}
|
2017-04-15 02:24:35 +08:00
|
|
|
ReportErrorSummary(ErrorKind, GetSanititizerToolName());
|
2014-09-20 02:33:45 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:46:37 +08:00
|
|
|
namespace {
|
|
|
|
class Decorator : public SanitizerCommonDecorator {
|
|
|
|
public:
|
|
|
|
Decorator() : SanitizerCommonDecorator() {}
|
|
|
|
const char *Highlight() const { return Green(); }
|
|
|
|
const char *Note() const { return Black(); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-02-12 03:45:07 +08:00
|
|
|
SymbolizedStack *__ubsan::getSymbolizedLocation(uptr PC) {
|
2015-04-02 06:42:36 +08:00
|
|
|
InitAsStandaloneIfNecessary();
|
2015-02-12 03:45:07 +08:00
|
|
|
return Symbolizer::GetOrInit()->SymbolizePC(PC);
|
2012-12-18 12:23:18 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
Diag &Diag::operator<<(const TypeDescriptor &V) {
|
|
|
|
return AddArg(V.getTypeName());
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag &Diag::operator<<(const Value &V) {
|
|
|
|
if (V.getType().isSignedIntegerTy())
|
|
|
|
AddArg(V.getSIntValue());
|
|
|
|
else if (V.getType().isUnsignedIntegerTy())
|
|
|
|
AddArg(V.getUIntValue());
|
2012-10-13 06:57:15 +08:00
|
|
|
else if (V.getType().isFloatTy())
|
|
|
|
AddArg(V.getFloatValue());
|
2012-10-10 03:34:32 +08:00
|
|
|
else
|
|
|
|
AddArg("<unknown>");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:30:21 +08:00
|
|
|
/// Hexadecimal printing for numbers too large for Printf to handle directly.
|
2016-03-11 08:37:02 +08:00
|
|
|
static void RenderHex(InternalScopedString *Buffer, UIntMax Val) {
|
2012-10-13 10:30:10 +08:00
|
|
|
#if HAVE_INT128_T
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer->append("0x%08x%08x%08x%08x", (unsigned int)(Val >> 96),
|
|
|
|
(unsigned int)(Val >> 64), (unsigned int)(Val >> 32),
|
|
|
|
(unsigned int)(Val));
|
2012-10-10 03:34:32 +08:00
|
|
|
#else
|
|
|
|
UNREACHABLE("long long smaller than 64 bits?");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:37:02 +08:00
|
|
|
static void RenderLocation(InternalScopedString *Buffer, Location Loc) {
|
2012-12-18 12:23:18 +08:00
|
|
|
switch (Loc.getKind()) {
|
|
|
|
case Location::LK_Source: {
|
|
|
|
SourceLocation SLoc = Loc.getSourceLocation();
|
|
|
|
if (SLoc.isInvalid())
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer->append("<unknown>");
|
2013-10-04 16:55:03 +08:00
|
|
|
else
|
2016-03-11 08:37:02 +08:00
|
|
|
RenderSourceLocation(Buffer, SLoc.getFilename(), SLoc.getLine(),
|
2015-06-04 09:20:06 +08:00
|
|
|
SLoc.getColumn(), common_flags()->symbolize_vs_style,
|
|
|
|
common_flags()->strip_path_prefix);
|
2016-03-11 08:37:02 +08:00
|
|
|
return;
|
2012-12-18 12:23:18 +08:00
|
|
|
}
|
|
|
|
case Location::LK_Memory:
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer->append("%p", Loc.getMemoryLocation());
|
|
|
|
return;
|
2015-02-12 03:45:07 +08:00
|
|
|
case Location::LK_Symbolized: {
|
|
|
|
const AddressInfo &Info = Loc.getSymbolizedStack()->info;
|
2016-03-11 08:37:02 +08:00
|
|
|
if (Info.file)
|
|
|
|
RenderSourceLocation(Buffer, Info.file, Info.line, Info.column,
|
2015-06-04 09:20:06 +08:00
|
|
|
common_flags()->symbolize_vs_style,
|
2015-02-12 03:45:07 +08:00
|
|
|
common_flags()->strip_path_prefix);
|
2016-03-11 08:37:02 +08:00
|
|
|
else if (Info.module)
|
|
|
|
RenderModuleLocation(Buffer, Info.module, Info.module_offset,
|
2017-01-07 05:45:05 +08:00
|
|
|
Info.module_arch, common_flags()->strip_path_prefix);
|
2016-03-11 08:37:02 +08:00
|
|
|
else
|
|
|
|
Buffer->append("%p", Info.address);
|
|
|
|
return;
|
2015-02-12 03:45:07 +08:00
|
|
|
}
|
2012-12-18 12:23:18 +08:00
|
|
|
case Location::LK_Null:
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer->append("<unknown>");
|
|
|
|
return;
|
2012-12-18 12:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:37:02 +08:00
|
|
|
static void RenderText(InternalScopedString *Buffer, const char *Message,
|
|
|
|
const Diag::Arg *Args) {
|
2012-10-10 03:34:32 +08:00
|
|
|
for (const char *Msg = Message; *Msg; ++Msg) {
|
2012-11-14 07:42:05 +08:00
|
|
|
if (*Msg != '%') {
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer->append("%c", *Msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const Diag::Arg &A = Args[*++Msg - '0'];
|
|
|
|
switch (A.Kind) {
|
|
|
|
case Diag::AK_String:
|
|
|
|
Buffer->append("%s", A.String);
|
|
|
|
break;
|
|
|
|
case Diag::AK_TypeName: {
|
|
|
|
if (SANITIZER_WINDOWS)
|
|
|
|
// The Windows implementation demangles names early.
|
|
|
|
Buffer->append("'%s'", A.String);
|
|
|
|
else
|
|
|
|
Buffer->append("'%s'", Symbolizer::GetOrInit()->Demangle(A.String));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Diag::AK_SInt:
|
|
|
|
// 'long long' is guaranteed to be at least 64 bits wide.
|
|
|
|
if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX)
|
|
|
|
Buffer->append("%lld", (long long)A.SInt);
|
|
|
|
else
|
|
|
|
RenderHex(Buffer, A.SInt);
|
|
|
|
break;
|
|
|
|
case Diag::AK_UInt:
|
|
|
|
if (A.UInt <= UINT64_MAX)
|
|
|
|
Buffer->append("%llu", (unsigned long long)A.UInt);
|
|
|
|
else
|
|
|
|
RenderHex(Buffer, A.UInt);
|
|
|
|
break;
|
|
|
|
case Diag::AK_Float: {
|
|
|
|
// FIXME: Support floating-point formatting in sanitizer_common's
|
|
|
|
// printf, and stop using snprintf here.
|
|
|
|
char FloatBuffer[32];
|
2015-07-02 09:44:34 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2016-03-11 08:37:02 +08:00
|
|
|
sprintf_s(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
|
2015-07-02 09:44:34 +08:00
|
|
|
#else
|
2016-03-11 08:37:02 +08:00
|
|
|
snprintf(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
|
2015-07-02 09:44:34 +08:00
|
|
|
#endif
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer->append("%s", FloatBuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Diag::AK_Pointer:
|
|
|
|
Buffer->append("%p", A.Pointer);
|
|
|
|
break;
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 14:30:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the earliest-starting range in Ranges which ends after Loc.
|
|
|
|
static Range *upperBound(MemoryLocation Loc, Range *Ranges,
|
|
|
|
unsigned NumRanges) {
|
|
|
|
Range *Best = 0;
|
|
|
|
for (unsigned I = 0; I != NumRanges; ++I)
|
|
|
|
if (Ranges[I].getEnd().getMemoryLocation() > Loc &&
|
|
|
|
(!Best ||
|
|
|
|
Best->getStart().getMemoryLocation() >
|
|
|
|
Ranges[I].getStart().getMemoryLocation()))
|
|
|
|
Best = &Ranges[I];
|
|
|
|
return Best;
|
|
|
|
}
|
|
|
|
|
2014-09-13 03:51:26 +08:00
|
|
|
static inline uptr subtractNoOverflow(uptr LHS, uptr RHS) {
|
|
|
|
return (LHS < RHS) ? 0 : LHS - RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uptr addNoOverflow(uptr LHS, uptr RHS) {
|
|
|
|
const uptr Limit = (uptr)-1;
|
|
|
|
return (LHS > Limit - RHS) ? Limit : LHS + RHS;
|
|
|
|
}
|
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
/// Render a snippet of the address space near a location.
|
2016-03-11 08:37:02 +08:00
|
|
|
static void PrintMemorySnippet(const Decorator &Decor, MemoryLocation Loc,
|
|
|
|
Range *Ranges, unsigned NumRanges,
|
|
|
|
const Diag::Arg *Args) {
|
2012-12-18 14:30:32 +08:00
|
|
|
// Show at least the 8 bytes surrounding Loc.
|
2014-09-13 03:51:26 +08:00
|
|
|
const unsigned MinBytesNearLoc = 4;
|
|
|
|
MemoryLocation Min = subtractNoOverflow(Loc, MinBytesNearLoc);
|
|
|
|
MemoryLocation Max = addNoOverflow(Loc, MinBytesNearLoc);
|
|
|
|
MemoryLocation OrigMin = Min;
|
2012-12-18 14:30:32 +08:00
|
|
|
for (unsigned I = 0; I < NumRanges; ++I) {
|
|
|
|
Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min);
|
|
|
|
Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have too many interesting bytes, prefer to show bytes after Loc.
|
2014-09-13 03:51:26 +08:00
|
|
|
const unsigned BytesToShow = 32;
|
2012-12-18 14:30:32 +08:00
|
|
|
if (Max - Min > BytesToShow)
|
2014-09-13 03:51:26 +08:00
|
|
|
Min = __sanitizer::Min(Max - BytesToShow, OrigMin);
|
|
|
|
Max = addNoOverflow(Min, BytesToShow);
|
2012-12-18 14:30:32 +08:00
|
|
|
|
2014-09-18 01:56:15 +08:00
|
|
|
if (!IsAccessibleMemoryRange(Min, Max - Min)) {
|
|
|
|
Printf("<memory cannot be printed>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
// Emit data.
|
2016-03-11 08:37:02 +08:00
|
|
|
InternalScopedString Buffer(1024);
|
2012-12-18 14:30:32 +08:00
|
|
|
for (uptr P = Min; P != Max; ++P) {
|
|
|
|
unsigned char C = *reinterpret_cast<const unsigned char*>(P);
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer.append("%s%02x", (P % 8 == 0) ? " " : " ", C);
|
2012-12-18 14:30:32 +08:00
|
|
|
}
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer.append("\n");
|
2012-12-18 14:30:32 +08:00
|
|
|
|
|
|
|
// Emit highlights.
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer.append(Decor.Highlight());
|
2012-12-18 14:30:32 +08:00
|
|
|
Range *InRange = upperBound(Min, Ranges, NumRanges);
|
|
|
|
for (uptr P = Min; P != Max; ++P) {
|
|
|
|
char Pad = ' ', Byte = ' ';
|
|
|
|
if (InRange && InRange->getEnd().getMemoryLocation() == P)
|
|
|
|
InRange = upperBound(P, Ranges, NumRanges);
|
|
|
|
if (!InRange && P > Loc)
|
|
|
|
break;
|
|
|
|
if (InRange && InRange->getStart().getMemoryLocation() < P)
|
|
|
|
Pad = '~';
|
|
|
|
if (InRange && InRange->getStart().getMemoryLocation() <= P)
|
|
|
|
Byte = '~';
|
2016-03-11 08:37:02 +08:00
|
|
|
if (P % 8 == 0)
|
|
|
|
Buffer.append("%c", Pad);
|
|
|
|
Buffer.append("%c", Pad);
|
|
|
|
Buffer.append("%c", P == Loc ? '^' : Byte);
|
|
|
|
Buffer.append("%c", Byte);
|
2012-12-18 14:30:32 +08:00
|
|
|
}
|
2017-09-12 04:55:49 +08:00
|
|
|
Buffer.append("%s\n", Decor.Default());
|
2012-12-18 14:30:32 +08:00
|
|
|
|
|
|
|
// Go over the line again, and print names for the ranges.
|
|
|
|
InRange = 0;
|
|
|
|
unsigned Spaces = 0;
|
|
|
|
for (uptr P = Min; P != Max; ++P) {
|
|
|
|
if (!InRange || InRange->getEnd().getMemoryLocation() == P)
|
|
|
|
InRange = upperBound(P, Ranges, NumRanges);
|
|
|
|
if (!InRange)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Spaces += (P % 8) == 0 ? 2 : 1;
|
|
|
|
|
|
|
|
if (InRange && InRange->getStart().getMemoryLocation() == P) {
|
|
|
|
while (Spaces--)
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer.append(" ");
|
|
|
|
RenderText(&Buffer, InRange->getText(), Args);
|
|
|
|
Buffer.append("\n");
|
2012-12-18 14:30:32 +08:00
|
|
|
// FIXME: We only support naming one range for now!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Spaces += 2;
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:37:02 +08:00
|
|
|
Printf("%s", Buffer.data());
|
2012-12-18 14:30:32 +08:00
|
|
|
// FIXME: Print names for anything we can identify within the line:
|
|
|
|
//
|
|
|
|
// * If we can identify the memory itself as belonging to a particular
|
|
|
|
// global, stack variable, or dynamic allocation, then do so.
|
|
|
|
//
|
|
|
|
// * If we have a pointer-size, pointer-aligned range highlighted,
|
|
|
|
// determine whether the value of that range is a pointer to an
|
|
|
|
// entity which we can name, and if so, print that name.
|
|
|
|
//
|
|
|
|
// This needs an external symbolizer, or (preferably) ASan instrumentation.
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag::~Diag() {
|
2014-07-30 09:49:19 +08:00
|
|
|
// All diagnostics should be printed under report mutex.
|
2017-09-23 10:47:21 +08:00
|
|
|
ScopedReport::CheckLocked();
|
2014-06-14 07:46:37 +08:00
|
|
|
Decorator Decor;
|
2016-03-11 08:37:02 +08:00
|
|
|
InternalScopedString Buffer(1024);
|
2012-12-18 14:30:32 +08:00
|
|
|
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer.append(Decor.Bold());
|
|
|
|
RenderLocation(&Buffer, Loc);
|
|
|
|
Buffer.append(":");
|
2012-12-18 14:30:32 +08:00
|
|
|
|
|
|
|
switch (Level) {
|
|
|
|
case DL_Error:
|
2017-09-12 04:55:49 +08:00
|
|
|
Buffer.append("%s runtime error: %s%s", Decor.Warning(), Decor.Default(),
|
2016-03-11 08:37:02 +08:00
|
|
|
Decor.Bold());
|
2012-12-18 14:30:32 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DL_Note:
|
2017-09-12 04:55:49 +08:00
|
|
|
Buffer.append("%s note: %s", Decor.Note(), Decor.Default());
|
2012-12-18 14:30:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:37:02 +08:00
|
|
|
RenderText(&Buffer, Message, Args);
|
2012-12-18 14:30:32 +08:00
|
|
|
|
2016-03-11 08:37:02 +08:00
|
|
|
Buffer.append("%s\n", Decor.Default());
|
|
|
|
Printf("%s", Buffer.data());
|
2012-12-18 14:30:32 +08:00
|
|
|
|
2014-09-09 01:52:48 +08:00
|
|
|
if (Loc.isMemoryLocation())
|
2016-03-11 08:37:02 +08:00
|
|
|
PrintMemorySnippet(Decor, Loc.getMemoryLocation(), Ranges, NumRanges, Args);
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2017-09-23 10:47:21 +08:00
|
|
|
ScopedReport::Initializer::Initializer() { InitAsStandaloneIfNecessary(); }
|
|
|
|
|
2015-08-25 07:18:49 +08:00
|
|
|
ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc,
|
|
|
|
ErrorType Type)
|
2017-09-23 10:47:21 +08:00
|
|
|
: Opts(Opts), SummaryLoc(SummaryLoc), Type(Type) {}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
ScopedReport::~ScopedReport() {
|
2014-08-23 05:42:04 +08:00
|
|
|
MaybePrintStackTrace(Opts.pc, Opts.bp);
|
2015-08-25 07:18:49 +08:00
|
|
|
MaybeReportErrorSummary(SummaryLoc, Type);
|
2015-12-09 08:12:57 +08:00
|
|
|
if (flags()->halt_on_error)
|
2014-07-30 09:49:19 +08:00
|
|
|
Die();
|
|
|
|
}
|
2014-08-05 09:24:22 +08:00
|
|
|
|
2015-02-21 01:41:59 +08:00
|
|
|
ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
|
|
|
|
static SuppressionContext *suppression_ctx = nullptr;
|
|
|
|
static const char kVptrCheck[] = "vptr_check";
|
2015-12-19 03:56:42 +08:00
|
|
|
static const char *kSuppressionTypes[] = {
|
|
|
|
#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) FSanitizeFlagName,
|
|
|
|
#include "ubsan_checks.inc"
|
|
|
|
#undef UBSAN_CHECK
|
|
|
|
kVptrCheck,
|
|
|
|
};
|
2015-02-21 01:41:59 +08:00
|
|
|
|
|
|
|
void __ubsan::InitializeSuppressions() {
|
|
|
|
CHECK_EQ(nullptr, suppression_ctx);
|
|
|
|
suppression_ctx = new (suppression_placeholder) // NOLINT
|
|
|
|
SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
|
|
|
|
suppression_ctx->ParseFromFile(flags()->suppressions);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) {
|
2015-04-02 06:42:36 +08:00
|
|
|
InitAsStandaloneIfNecessary();
|
2015-02-21 01:41:59 +08:00
|
|
|
CHECK(suppression_ctx);
|
|
|
|
Suppression *s;
|
|
|
|
return suppression_ctx->Match(TypeName, kVptrCheck, &s);
|
2014-08-05 09:24:22 +08:00
|
|
|
}
|
2015-03-27 01:26:04 +08:00
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
bool __ubsan::IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename) {
|
|
|
|
InitAsStandaloneIfNecessary();
|
|
|
|
CHECK(suppression_ctx);
|
|
|
|
const char *SuppType = ConvertTypeToFlagName(ET);
|
|
|
|
// Fast path: don't symbolize PC if there is no suppressions for given UB
|
|
|
|
// type.
|
|
|
|
if (!suppression_ctx->HasSuppressionType(SuppType))
|
|
|
|
return false;
|
|
|
|
Suppression *s = nullptr;
|
|
|
|
// Suppress by file name known to runtime.
|
|
|
|
if (Filename != nullptr && suppression_ctx->Match(Filename, SuppType, &s))
|
|
|
|
return true;
|
|
|
|
// Suppress by module name.
|
|
|
|
if (const char *Module = Symbolizer::GetOrInit()->GetModuleNameForPc(PC)) {
|
|
|
|
if (suppression_ctx->Match(Module, SuppType, &s))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Suppress by function or source file name from debug info.
|
|
|
|
SymbolizedStackHolder Stack(Symbolizer::GetOrInit()->SymbolizePC(PC));
|
|
|
|
const AddressInfo &AI = Stack.get()->info;
|
|
|
|
return suppression_ctx->Match(AI.function, SuppType, &s) ||
|
|
|
|
suppression_ctx->Match(AI.file, SuppType, &s);
|
|
|
|
}
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#endif // CAN_SANITIZE_UB
|