2013-08-30 18:12:58 +02:00
|
|
|
//===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Entry points to the runtime library for Clang's undefined behavior sanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef UBSAN_HANDLERS_H
|
|
|
|
#define UBSAN_HANDLERS_H
|
|
|
|
|
|
|
|
#include "ubsan_value.h"
|
|
|
|
|
|
|
|
namespace __ubsan {
|
|
|
|
|
|
|
|
struct TypeMismatchData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
uptr Alignment;
|
|
|
|
unsigned char TypeCheckKind;
|
|
|
|
};
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
#define UNRECOVERABLE(checkname, ...) \
|
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
|
|
|
|
void __ubsan_handle_ ## checkname( __VA_ARGS__ );
|
|
|
|
|
2013-08-30 18:12:58 +02:00
|
|
|
#define RECOVERABLE(checkname, ...) \
|
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
|
|
|
|
void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
|
2014-09-23 19:59:53 +02:00
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
|
2013-08-30 18:12:58 +02:00
|
|
|
void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
|
|
|
|
|
|
|
|
/// \brief Handle a runtime type check failure, caused by either a misaligned
|
|
|
|
/// pointer, a null pointer, or a pointer to insufficient storage for the
|
|
|
|
/// type.
|
|
|
|
RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer)
|
|
|
|
|
|
|
|
struct OverflowData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle an integer addition overflow.
|
|
|
|
RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
|
|
|
/// \brief Handle an integer subtraction overflow.
|
|
|
|
RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
|
|
|
/// \brief Handle an integer multiplication overflow.
|
|
|
|
RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
|
|
|
/// \brief Handle a signed integer overflow for a unary negate operator.
|
|
|
|
RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
|
|
|
|
|
|
|
|
/// \brief Handle an INT_MIN/-1 overflow or division by zero.
|
|
|
|
RECOVERABLE(divrem_overflow, OverflowData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
|
|
|
struct ShiftOutOfBoundsData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &LHSType;
|
|
|
|
const TypeDescriptor &RHSType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a shift where the RHS is out of bounds or a left shift where
|
|
|
|
/// the LHS is negative or overflows.
|
|
|
|
RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
|
|
|
struct OutOfBoundsData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &ArrayType;
|
|
|
|
const TypeDescriptor &IndexType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle an array index out of bounds error.
|
|
|
|
RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
|
|
|
|
|
|
|
|
struct UnreachableData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a __builtin_unreachable which is reached.
|
2014-09-23 19:59:53 +02:00
|
|
|
UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
|
2013-08-30 18:12:58 +02:00
|
|
|
/// \brief Handle reaching the end of a value-returning function.
|
2014-09-23 19:59:53 +02:00
|
|
|
UNRECOVERABLE(missing_return, UnreachableData *Data)
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
struct VLABoundData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a VLA with a non-positive bound.
|
|
|
|
RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
// Keeping this around for binary compatibility with (sanitized) programs
|
|
|
|
// compiled with older compilers.
|
2013-08-30 18:12:58 +02:00
|
|
|
struct FloatCastOverflowData {
|
|
|
|
const TypeDescriptor &FromType;
|
|
|
|
const TypeDescriptor &ToType;
|
|
|
|
};
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
struct FloatCastOverflowDataV2 {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &FromType;
|
|
|
|
const TypeDescriptor &ToType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handle overflow in a conversion to or from a floating-point type.
|
|
|
|
/// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2*
|
|
|
|
RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From)
|
2013-08-30 18:12:58 +02:00
|
|
|
|
|
|
|
struct InvalidValueData {
|
2013-12-05 10:18:38 +01:00
|
|
|
SourceLocation Loc;
|
2013-08-30 18:12:58 +02:00
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a load of an invalid value for the type.
|
|
|
|
RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
|
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
struct FunctionTypeMismatchData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
RECOVERABLE(function_type_mismatch,
|
|
|
|
FunctionTypeMismatchData *Data,
|
|
|
|
ValueHandle Val)
|
|
|
|
|
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
|
|
|
struct NonNullReturnData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
SourceLocation AttrLoc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle returning null from function with returns_nonnull attribute.
|
|
|
|
RECOVERABLE(nonnull_return, NonNullReturnData *Data)
|
|
|
|
|
|
|
|
struct NonNullArgData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
SourceLocation AttrLoc;
|
|
|
|
int ArgIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle passing null pointer to function with nonnull attribute.
|
|
|
|
RECOVERABLE(nonnull_arg, NonNullArgData *Data)
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
/// \brief Known CFI check kinds.
|
|
|
|
/// Keep in sync with the enum of the same name in CodeGenFunction.h
|
|
|
|
enum CFITypeCheckKind : unsigned char {
|
|
|
|
CFITCK_VCall,
|
|
|
|
CFITCK_NVCall,
|
|
|
|
CFITCK_DerivedCast,
|
|
|
|
CFITCK_UnrelatedCast,
|
|
|
|
CFITCK_ICall,
|
|
|
|
};
|
|
|
|
|
2016-11-16 12:13:19 +01:00
|
|
|
struct CFIBadIcallData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
struct CFICheckFailData {
|
|
|
|
CFITypeCheckKind CheckKind;
|
2015-10-21 09:32:45 +02:00
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
2016-11-16 12:13:19 +01:00
|
|
|
/// \brief Handle control flow integrity failure for indirect function calls.
|
|
|
|
RECOVERABLE(cfi_bad_icall, CFIBadIcallData *Data, ValueHandle Function)
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
/// \brief Handle control flow integrity failures.
|
|
|
|
RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
|
|
|
|
uptr VtableIsValid)
|
2013-08-30 18:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // UBSAN_HANDLERS_H
|