2013-08-30 18:12:58 +02:00
|
|
|
//===-- ubsan_handlers.cc -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Error logging entry points for the UBSan runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
#include "ubsan_platform.h"
|
|
|
|
#if CAN_SANITIZE_UB
|
2013-08-30 18:12:58 +02:00
|
|
|
#include "ubsan_handlers.h"
|
|
|
|
#include "ubsan_diag.h"
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
using namespace __ubsan;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
namespace __ubsan {
|
|
|
|
bool ignoreReport(SourceLocation SLoc, ReportOptions Opts, ErrorType ET) {
|
|
|
|
// We are not allowed to skip error report: if we are in unrecoverable
|
|
|
|
// handler, we have to terminate the program right now, and therefore
|
|
|
|
// have to print some diagnostic.
|
|
|
|
//
|
|
|
|
// Even if source location is disabled, it doesn't mean that we have
|
|
|
|
// already report an error to the user: some concurrently running
|
|
|
|
// thread could have acquired it, but not yet printed the report.
|
|
|
|
if (Opts.FromUnrecoverableHandler)
|
|
|
|
return false;
|
|
|
|
return SLoc.isDisabled() || IsPCSuppressed(ET, Opts.pc, SLoc.getFilename());
|
2014-09-23 19:59:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 21:41:38 +01:00
|
|
|
const char *TypeCheckKinds[] = {
|
2013-08-30 18:12:58 +02:00
|
|
|
"load of", "store to", "reference binding to", "member access within",
|
2014-11-13 21:41:38 +01:00
|
|
|
"member call on", "constructor call on", "downcast of", "downcast of",
|
|
|
|
"upcast of", "cast to virtual base of"};
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
|
2015-10-21 09:32:45 +02:00
|
|
|
ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
Location Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
|
|
|
|
ErrorType ET;
|
|
|
|
if (!Pointer)
|
|
|
|
ET = ErrorType::NullPointerUse;
|
|
|
|
else if (Data->Alignment && (Pointer & (Data->Alignment - 1)))
|
|
|
|
ET = ErrorType::MisalignedPointerUse;
|
|
|
|
else
|
|
|
|
ET = ErrorType::InsufficientObjectSize;
|
|
|
|
|
|
|
|
// Use the SourceLocation from Data to track deduplication, even if it's
|
|
|
|
// invalid.
|
|
|
|
if (ignoreReport(Loc.getSourceLocation(), Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
SymbolizedStackHolder FallbackLoc;
|
|
|
|
if (Data->Loc.isInvalid()) {
|
|
|
|
FallbackLoc.reset(getCallerLocation(Opts.pc));
|
2013-08-30 18:12:58 +02:00
|
|
|
Loc = FallbackLoc;
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
switch (ET) {
|
|
|
|
case ErrorType::NullPointerUse:
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error, "%0 null pointer of type %1")
|
2016-11-08 23:04:09 +01:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
|
|
|
|
break;
|
|
|
|
case ErrorType::MisalignedPointerUse:
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error, "%0 misaligned address %1 for type %3, "
|
|
|
|
"which requires %2 byte alignment")
|
2016-11-08 23:04:09 +01:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer
|
|
|
|
<< Data->Alignment << Data->Type;
|
|
|
|
break;
|
|
|
|
case ErrorType::InsufficientObjectSize:
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error, "%0 address %1 with insufficient space "
|
|
|
|
"for an object of type %2")
|
2016-11-08 23:04:09 +01:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Data->Type;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE("unexpected error type!");
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
2016-11-08 23:04:09 +01:00
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
if (Pointer)
|
|
|
|
Diag(Pointer, DL_Note, "pointer points here");
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_type_mismatch(TypeMismatchData *Data,
|
|
|
|
ValueHandle Pointer) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(false);
|
2015-10-21 09:32:45 +02:00
|
|
|
handleTypeMismatchImpl(Data, Pointer, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
|
|
|
void __ubsan::__ubsan_handle_type_mismatch_abort(TypeMismatchData *Data,
|
|
|
|
ValueHandle Pointer) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2015-10-21 09:32:45 +02:00
|
|
|
handleTypeMismatchImpl(Data, Pointer, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Common diagnostic emission for various forms of integer overflow.
|
2014-09-23 19:59:53 +02:00
|
|
|
template <typename T>
|
|
|
|
static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
|
|
|
|
const char *Operator, T RHS,
|
|
|
|
ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
bool IsSigned = Data->Type.isSignedIntegerTy();
|
|
|
|
ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
|
|
|
|
: ErrorType::UnsignedIntegerOverflow;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error, "%0 integer overflow: "
|
|
|
|
"%1 %2 %3 cannot be represented in type %4")
|
2015-10-21 09:32:45 +02:00
|
|
|
<< (IsSigned ? "signed" : "unsigned")
|
2013-08-30 18:12:58 +02:00
|
|
|
<< Value(Data->Type, LHS) << Operator << RHS << Data->Type;
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
#define UBSAN_OVERFLOW_HANDLER(handler_name, op, unrecoverable) \
|
2014-09-23 19:59:53 +02:00
|
|
|
void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS, \
|
|
|
|
ValueHandle RHS) { \
|
2016-11-08 23:04:09 +01:00
|
|
|
GET_REPORT_OPTIONS(unrecoverable); \
|
2014-09-23 19:59:53 +02:00
|
|
|
handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts); \
|
2016-11-08 23:04:09 +01:00
|
|
|
if (unrecoverable) \
|
|
|
|
Die(); \
|
2014-09-23 19:59:53 +02:00
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow, "+", false)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow_abort, "+", true)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow, "-", false)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow_abort, "-", true)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow, "*", false)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow_abort, "*", true)
|
2013-08-30 18:12:58 +02:00
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
|
|
|
|
ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
bool IsSigned = Data->Type.isSignedIntegerTy();
|
|
|
|
ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
|
|
|
|
: ErrorType::UnsignedIntegerOverflow;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
if (IsSigned)
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error,
|
|
|
|
"negation of %0 cannot be represented in type %1; "
|
|
|
|
"cast to an unsigned type to negate this value to itself")
|
2015-10-21 09:32:45 +02:00
|
|
|
<< Value(Data->Type, OldVal) << Data->Type;
|
2013-08-30 18:12:58 +02:00
|
|
|
else
|
2015-10-21 09:32:45 +02:00
|
|
|
Diag(Loc, DL_Error, "negation of %0 cannot be represented in type %1")
|
|
|
|
<< Value(Data->Type, OldVal) << Data->Type;
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
|
|
|
|
ValueHandle OldVal) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleNegateOverflowImpl(Data, OldVal, Opts);
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
|
|
|
|
ValueHandle OldVal) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleNegateOverflowImpl(Data, OldVal, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
|
|
|
|
ValueHandle RHS, ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
Value LHSVal(Data->Type, LHS);
|
|
|
|
Value RHSVal(Data->Type, RHS);
|
|
|
|
|
|
|
|
ErrorType ET;
|
|
|
|
if (RHSVal.isMinusOne())
|
|
|
|
ET = ErrorType::SignedIntegerOverflow;
|
|
|
|
else if (Data->Type.isIntegerTy())
|
|
|
|
ET = ErrorType::IntegerDivideByZero;
|
|
|
|
else
|
|
|
|
ET = ErrorType::FloatDivideByZero;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
switch (ET) {
|
|
|
|
case ErrorType::SignedIntegerOverflow:
|
|
|
|
Diag(Loc, DL_Error, "division of %0 by -1 cannot be represented in type %1")
|
|
|
|
<< LHSVal << Data->Type;
|
|
|
|
break;
|
|
|
|
default:
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error, "division by zero");
|
2016-11-08 23:04:09 +01:00
|
|
|
break;
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleDivremOverflowImpl(Data, LHS, RHS, Opts);
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
|
|
|
|
ValueHandle LHS,
|
|
|
|
ValueHandle RHS) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleDivremOverflowImpl(Data, LHS, RHS, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS,
|
|
|
|
ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
Value LHSVal(Data->LHSType, LHS);
|
|
|
|
Value RHSVal(Data->RHSType, RHS);
|
|
|
|
|
|
|
|
ErrorType ET;
|
|
|
|
if (RHSVal.isNegative() ||
|
|
|
|
RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth())
|
|
|
|
ET = ErrorType::InvalidShiftExponent;
|
|
|
|
else
|
|
|
|
ET = ErrorType::InvalidShiftBase;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
if (ET == ErrorType::InvalidShiftExponent) {
|
|
|
|
if (RHSVal.isNegative())
|
|
|
|
Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal;
|
|
|
|
else
|
|
|
|
Diag(Loc, DL_Error, "shift exponent %0 is too large for %1-bit type %2")
|
|
|
|
<< RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType;
|
2015-10-21 09:32:45 +02:00
|
|
|
} else {
|
2016-11-08 23:04:09 +01:00
|
|
|
if (LHSVal.isNegative())
|
|
|
|
Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal;
|
|
|
|
else
|
|
|
|
Diag(Loc, DL_Error,
|
|
|
|
"left shift of %0 by %1 places cannot be represented in type %2")
|
|
|
|
<< LHSVal << RHSVal << Data->LHSType;
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS,
|
|
|
|
ValueHandle RHS) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
|
|
|
|
ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS,
|
|
|
|
ValueHandle RHS) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
|
|
|
|
ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::OutOfBoundsIndex;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
Value IndexVal(Data->IndexType, Index);
|
|
|
|
Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
|
|
|
|
<< IndexVal << Data->ArrayType;
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
|
|
|
|
ValueHandle Index) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleOutOfBoundsImpl(Data, Index, Opts);
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
|
|
|
|
ValueHandle Index) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleOutOfBoundsImpl(Data, Index, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleBuiltinUnreachableImpl(UnreachableData *Data,
|
|
|
|
ReportOptions Opts) {
|
2015-10-21 09:32:45 +02:00
|
|
|
ScopedReport R(Opts, Data->Loc, ErrorType::UnreachableCall);
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
|
2014-09-23 19:59:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleBuiltinUnreachableImpl(Data, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
|
2015-10-21 09:32:45 +02:00
|
|
|
ScopedReport R(Opts, Data->Loc, ErrorType::MissingReturn);
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Data->Loc, DL_Error,
|
|
|
|
"execution reached the end of a value-returning function "
|
|
|
|
"without returning a value");
|
2014-09-23 19:59:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleMissingReturnImpl(Data, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
|
|
|
|
ReportOptions Opts) {
|
2013-08-30 18:12:58 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::NonPositiveVLAIndex;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-08-30 18:12:58 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
Diag(Loc, DL_Error, "variable length array bound evaluates to "
|
|
|
|
"non-positive value %0")
|
|
|
|
<< Value(Data->Type, Bound);
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
|
|
|
|
ValueHandle Bound) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleVLABoundNotPositive(Data, Bound, Opts);
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
|
2014-09-23 19:59:53 +02:00
|
|
|
ValueHandle Bound) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleVLABoundNotPositive(Data, Bound, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
static bool looksLikeFloatCastOverflowDataV1(void *Data) {
|
|
|
|
// First field is either a pointer to filename or a pointer to a
|
|
|
|
// TypeDescriptor.
|
|
|
|
u8 *FilenameOrTypeDescriptor;
|
|
|
|
internal_memcpy(&FilenameOrTypeDescriptor, Data,
|
|
|
|
sizeof(FilenameOrTypeDescriptor));
|
|
|
|
|
|
|
|
// Heuristic: For float_cast_overflow, the TypeKind will be either TK_Integer
|
|
|
|
// (0x0), TK_Float (0x1) or TK_Unknown (0xff). If both types are known,
|
|
|
|
// adding both bytes will be 0 or 1 (for BE or LE). If it were a filename,
|
|
|
|
// adding two printable characters will not yield such a value. Otherwise,
|
|
|
|
// if one of them is 0xff, this is most likely TK_Unknown type descriptor.
|
|
|
|
u16 MaybeFromTypeKind =
|
|
|
|
FilenameOrTypeDescriptor[0] + FilenameOrTypeDescriptor[1];
|
|
|
|
return MaybeFromTypeKind < 2 || FilenameOrTypeDescriptor[0] == 0xff ||
|
|
|
|
FilenameOrTypeDescriptor[1] == 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleFloatCastOverflow(void *DataPtr, ValueHandle From,
|
|
|
|
ReportOptions Opts) {
|
|
|
|
SymbolizedStackHolder CallerLoc;
|
|
|
|
Location Loc;
|
|
|
|
const TypeDescriptor *FromType, *ToType;
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::FloatCastOverflow;
|
2015-10-21 09:32:45 +02:00
|
|
|
|
|
|
|
if (looksLikeFloatCastOverflowDataV1(DataPtr)) {
|
|
|
|
auto Data = reinterpret_cast<FloatCastOverflowData *>(DataPtr);
|
|
|
|
CallerLoc.reset(getCallerLocation(Opts.pc));
|
|
|
|
Loc = CallerLoc;
|
|
|
|
FromType = &Data->FromType;
|
|
|
|
ToType = &Data->ToType;
|
|
|
|
} else {
|
|
|
|
auto Data = reinterpret_cast<FloatCastOverflowDataV2 *>(DataPtr);
|
|
|
|
SourceLocation SLoc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
if (ignoreReport(SLoc, Opts, ET))
|
2015-10-21 09:32:45 +02:00
|
|
|
return;
|
|
|
|
Loc = SLoc;
|
|
|
|
FromType = &Data->FromType;
|
|
|
|
ToType = &Data->ToType;
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
Diag(Loc, DL_Error,
|
2013-08-30 18:12:58 +02:00
|
|
|
"value %0 is outside the range of representable values of type %2")
|
2015-10-21 09:32:45 +02:00
|
|
|
<< Value(*FromType, From) << *FromType << *ToType;
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
void __ubsan::__ubsan_handle_float_cast_overflow(void *Data, ValueHandle From) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleFloatCastOverflow(Data, From, Opts);
|
|
|
|
}
|
2015-10-21 09:32:45 +02:00
|
|
|
void __ubsan::__ubsan_handle_float_cast_overflow_abort(void *Data,
|
|
|
|
ValueHandle From) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleFloatCastOverflow(Data, From, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
|
|
|
|
ReportOptions Opts) {
|
2013-12-05 10:18:38 +01:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-10-21 09:32:45 +02:00
|
|
|
// This check could be more precise if we used different handlers for
|
|
|
|
// -fsanitize=bool and -fsanitize=enum.
|
|
|
|
bool IsBool = (0 == internal_strcmp(Data->Type.getTypeName(), "'bool'"));
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET =
|
|
|
|
IsBool ? ErrorType::InvalidBoolLoad : ErrorType::InvalidEnumLoad;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
Diag(Loc, DL_Error,
|
2013-08-30 18:12:58 +02:00
|
|
|
"load of value %0, which is not a valid value for type %1")
|
|
|
|
<< Value(Data->Type, Val) << Data->Type;
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
|
|
|
|
ValueHandle Val) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleLoadInvalidValue(Data, Val, Opts);
|
|
|
|
}
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
|
|
|
|
ValueHandle Val) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleLoadInvalidValue(Data, Val, Opts);
|
2013-12-05 10:18:38 +01:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
|
|
|
|
ValueHandle Function,
|
|
|
|
ReportOptions Opts) {
|
2015-10-21 09:32:45 +02:00
|
|
|
SourceLocation CallLoc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::FunctionTypeMismatch;
|
|
|
|
|
|
|
|
if (ignoreReport(CallLoc, Opts, ET))
|
2015-10-21 09:32:45 +02:00
|
|
|
return;
|
2013-12-05 10:18:38 +01:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, CallLoc, ET);
|
2013-12-05 10:18:38 +01:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
|
|
|
|
const char *FName = FLoc.get()->info.function;
|
|
|
|
if (!FName)
|
|
|
|
FName = "(unknown)";
|
2014-09-23 19:59:53 +02:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
Diag(CallLoc, DL_Error,
|
2013-12-05 10:18:38 +01:00
|
|
|
"call to function %0 through pointer to incorrect function type %1")
|
2015-10-21 09:32:45 +02:00
|
|
|
<< FName << Data->Type;
|
|
|
|
Diag(FLoc, DL_Note, "%0 defined here") << FName;
|
2013-12-05 10:18:38 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
void
|
|
|
|
__ubsan::__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,
|
|
|
|
ValueHandle Function) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleFunctionTypeMismatch(Data, Function, Opts);
|
|
|
|
}
|
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
void __ubsan::__ubsan_handle_function_type_mismatch_abort(
|
2014-09-23 19:59:53 +02:00
|
|
|
FunctionTypeMismatchData *Data, ValueHandle Function) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleFunctionTypeMismatch(Data, Function, Opts);
|
2013-08-30 18:12:58 +02:00
|
|
|
Die();
|
|
|
|
}
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleNonNullReturn(NonNullReturnData *Data, ReportOptions Opts) {
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::InvalidNullReturn;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
Diag(Loc, DL_Error, "null pointer returned from function declared to never "
|
|
|
|
"return null");
|
|
|
|
if (!Data->AttrLoc.isInvalid())
|
|
|
|
Diag(Data->AttrLoc, DL_Note, "returns_nonnull attribute specified here");
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nonnull_return(NonNullReturnData *Data) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleNonNullReturn(Data, Opts);
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nonnull_return_abort(NonNullReturnData *Data) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleNonNullReturn(Data, Opts);
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts) {
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::InvalidNullArgument;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
Diag(Loc, DL_Error, "null pointer passed as argument %0, which is declared to "
|
|
|
|
"never be null") << Data->ArgIndex;
|
|
|
|
if (!Data->AttrLoc.isInvalid())
|
|
|
|
Diag(Data->AttrLoc, DL_Note, "nonnull attribute specified here");
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleNonNullArg(Data, Opts);
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
|
2014-09-23 19:59:53 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleNonNullArg(Data, Opts);
|
flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE and SANITIZE_RETURNS_NONNULL_ATTRIBUTE...
gcc/
* flag-types.h (enum sanitize_code): Add SANITIZE_NONNULL_ATTRIBUTE
and SANITIZE_RETURNS_NONNULL_ATTRIBUTE, or them into SANITIZE_UNDEFINED.
* opts.c (common_handle_option): Handle SANITIZE_NONNULL_ATTRIBUTE and
SANITIZE_RETURNS_NONNULL_ATTRIBUTE and disable
flag_delete_null_pointer_checks for them.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): New.
* ubsan.c (instrument_bool_enum_load): Set *gsi back to
stmt's iterator.
(instrument_nonnull_arg, instrument_nonnull_return): New functions.
(pass_ubsan::gate): Return true even for SANITIZE_NONNULL_ATTRIBUTE
or SANITIZE_RETURNS_NONNULL_ATTRIBUTE.
(pass_ubsan::execute): Call instrument_nonnull_{arg,return}.
* doc/invoke.texi (-fsanitize=nonnull-attribute,
-fsanitize=returns-nonnull-attribute): Document.
gcc/testsuite/
* c-c++-common/ubsan/attrib-3.c: New test.
* c-c++-common/ubsan/nonnull-1.c: New test.
* c-c++-common/ubsan/nonnull-2.c: New test.
* c-c++-common/ubsan/nonnull-3.c: New test.
* c-c++-common/ubsan/nonnull-4.c: New test.
* c-c++-common/ubsan/nonnull-5.c: New test.
libsanitizer/
* ubsan/ubsan_handlers.cc, ubsan/ubsan_handlers.h: Cherry pick
upstream r215485, r217389, r217391 and r217400.
From-SVN: r215118
2014-09-10 11:23:16 +02:00
|
|
|
Die();
|
|
|
|
}
|
2015-10-21 09:32:45 +02:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
static void handleCFIBadIcall(CFICheckFailData *Data, ValueHandle Function,
|
2015-10-21 09:32:45 +02:00
|
|
|
ReportOptions Opts) {
|
2016-11-08 23:04:09 +01:00
|
|
|
if (Data->CheckKind != CFITCK_ICall)
|
|
|
|
Die();
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2016-11-08 23:04:09 +01:00
|
|
|
ErrorType ET = ErrorType::CFIBadType;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2015-10-21 09:32:45 +02:00
|
|
|
return;
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2015-10-21 09:32:45 +02:00
|
|
|
|
|
|
|
Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during "
|
|
|
|
"indirect function call")
|
|
|
|
<< Data->Type;
|
|
|
|
|
|
|
|
SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
|
|
|
|
const char *FName = FLoc.get()->info.function;
|
|
|
|
if (!FName)
|
|
|
|
FName = "(unknown)";
|
|
|
|
Diag(FLoc, DL_Note, "%0 defined here") << FName;
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
namespace __ubsan {
|
|
|
|
#ifdef UBSAN_CAN_USE_CXXABI
|
|
|
|
SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
void HandleCFIBadType(CFICheckFailData *Data, ValueHandle Vtable,
|
|
|
|
bool ValidVtable, ReportOptions Opts);
|
|
|
|
#else
|
|
|
|
static void HandleCFIBadType(CFICheckFailData *Data, ValueHandle Vtable,
|
|
|
|
bool ValidVtable, ReportOptions Opts) {
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace __ubsan
|
|
|
|
|
2016-11-16 12:13:19 +01:00
|
|
|
void __ubsan::__ubsan_handle_cfi_bad_icall(CFIBadIcallData *CallData,
|
|
|
|
ValueHandle Function) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
CFICheckFailData Data = {CFITCK_ICall, CallData->Loc, CallData->Type};
|
|
|
|
handleCFIBadIcall(&Data, Function, Opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_cfi_bad_icall_abort(CFIBadIcallData *CallData,
|
|
|
|
ValueHandle Function) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
CFICheckFailData Data = {CFITCK_ICall, CallData->Loc, CallData->Type};
|
|
|
|
handleCFIBadIcall(&Data, Function, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
void __ubsan::__ubsan_handle_cfi_check_fail(CFICheckFailData *Data,
|
|
|
|
ValueHandle Value,
|
|
|
|
uptr ValidVtable) {
|
2015-10-21 09:32:45 +02:00
|
|
|
GET_REPORT_OPTIONS(false);
|
2016-11-08 23:04:09 +01:00
|
|
|
if (Data->CheckKind == CFITCK_ICall)
|
|
|
|
handleCFIBadIcall(Data, Value, Opts);
|
|
|
|
else
|
|
|
|
HandleCFIBadType(Data, Value, ValidVtable, Opts);
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
void __ubsan::__ubsan_handle_cfi_check_fail_abort(CFICheckFailData *Data,
|
|
|
|
ValueHandle Value,
|
|
|
|
uptr ValidVtable) {
|
2015-10-21 09:32:45 +02:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2016-11-08 23:04:09 +01:00
|
|
|
if (Data->CheckKind == CFITCK_ICall)
|
|
|
|
handleCFIBadIcall(Data, Value, Opts);
|
|
|
|
else
|
|
|
|
HandleCFIBadType(Data, Value, ValidVtable, Opts);
|
2015-10-21 09:32:45 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CAN_SANITIZE_UB
|