2012-10-10 03:34:32 +08:00
|
|
|
//===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2012-10-10 03:34:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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;
|
2017-01-06 22:40:28 +08:00
|
|
|
unsigned char LogAlignment;
|
2012-10-10 03:34:32 +08:00
|
|
|
unsigned char TypeCheckKind;
|
|
|
|
};
|
|
|
|
|
2014-08-23 05:42:04 +08:00
|
|
|
#define UNRECOVERABLE(checkname, ...) \
|
2014-09-11 04:43:36 +08:00
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
|
2014-08-23 05:42:04 +08:00
|
|
|
void __ubsan_handle_ ## checkname( __VA_ARGS__ );
|
|
|
|
|
2012-12-03 03:47:29 +08:00
|
|
|
#define RECOVERABLE(checkname, ...) \
|
2013-01-11 01:01:13 +08:00
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
|
|
|
|
void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
|
2014-09-11 04:43:36 +08:00
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
|
2013-01-11 01:01:13 +08:00
|
|
|
void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
|
2012-12-03 03:47:29 +08:00
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
/// \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.
|
2017-01-06 22:40:28 +08:00
|
|
|
RECOVERABLE(type_mismatch_v1, TypeMismatchData *Data, ValueHandle Pointer)
|
2012-10-10 03:34:32 +08:00
|
|
|
|
[compiler-rt][UBSan] Sanitization for alignment assumptions.
Summary:
This is the compiler-rt part.
The clang part is D54589.
This is a second commit, the original one was r351106,
which was mass-reverted in r351159 because 2 compiler-rt tests were failing.
Now, i have fundamentally changed the testing approach:
i malloc a few bytes, intentionally mis-align the pointer
(increment it by one), and check that. Also, i have decreased
the expected alignment. This hopefully should be enough to pacify
all the bots. If not, i guess i might just drop the two 'bad' tests.
Reviewers: filcab, vsk, #sanitizers, vitalybuka, rsmith, morehouse
Reviewed By: morehouse
Subscribers: rjmccall, krytarowski, rsmith, kcc, srhines, kubamracek, dberris, llvm-commits
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D54590
llvm-svn: 351178
2019-01-15 17:44:27 +08:00
|
|
|
struct AlignmentAssumptionData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
SourceLocation AssumptionLoc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a runtime alignment assumption check failure,
|
|
|
|
/// caused by a misaligned pointer.
|
|
|
|
RECOVERABLE(alignment_assumption, AlignmentAssumptionData *Data,
|
|
|
|
ValueHandle Pointer, ValueHandle Alignment, ValueHandle Offset)
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
struct OverflowData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
2012-11-27 23:01:43 +08:00
|
|
|
/// \brief Handle an integer addition overflow.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
2012-11-27 23:01:43 +08:00
|
|
|
/// \brief Handle an integer subtraction overflow.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
2012-11-27 23:01:43 +08:00
|
|
|
/// \brief Handle an integer multiplication overflow.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
/// \brief Handle a signed integer overflow for a unary negate operator.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
/// \brief Handle an INT_MIN/-1 overflow or division by zero.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(divrem_overflow, OverflowData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS)
|
2012-10-10 03:34:32 +08:00
|
|
|
|
|
|
|
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.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS)
|
2012-10-10 03:34:32 +08:00
|
|
|
|
2013-02-23 10:40:07 +08:00
|
|
|
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)
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
struct UnreachableData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a __builtin_unreachable which is reached.
|
2014-08-23 05:42:04 +08:00
|
|
|
UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
|
2012-10-10 03:34:32 +08:00
|
|
|
/// \brief Handle reaching the end of a value-returning function.
|
2014-08-23 05:42:04 +08:00
|
|
|
UNRECOVERABLE(missing_return, UnreachableData *Data)
|
2012-10-10 03:34:32 +08:00
|
|
|
|
2012-10-10 09:10:59 +08:00
|
|
|
struct VLABoundData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a VLA with a non-positive bound.
|
2012-12-03 03:47:29 +08:00
|
|
|
RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
|
2012-10-10 09:10:59 +08:00
|
|
|
|
2015-08-11 12:19:24 +08:00
|
|
|
// Keeping this around for binary compatibility with (sanitized) programs
|
|
|
|
// compiled with older compilers.
|
2012-10-13 06:57:15 +08:00
|
|
|
struct FloatCastOverflowData {
|
|
|
|
const TypeDescriptor &FromType;
|
|
|
|
const TypeDescriptor &ToType;
|
|
|
|
};
|
|
|
|
|
2015-08-11 12:19:24 +08: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)
|
2012-10-13 06:57:15 +08:00
|
|
|
|
2012-12-13 15:00:14 +08:00
|
|
|
struct InvalidValueData {
|
2013-10-02 10:29:47 +08:00
|
|
|
SourceLocation Loc;
|
2012-12-13 15:00:14 +08:00
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Handle a load of an invalid value for the type.
|
|
|
|
RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
|
|
|
|
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
/// Known implicit conversion check kinds.
|
|
|
|
/// Keep in sync with the enum of the same name in CGExprScalar.cpp
|
|
|
|
enum ImplicitConversionCheckKind : unsigned char {
|
2018-10-11 17:09:52 +08:00
|
|
|
ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7.
|
|
|
|
ICCK_UnsignedIntegerTruncation = 1,
|
|
|
|
ICCK_SignedIntegerTruncation = 2,
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer sign change - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D50250.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: vsk, filcab, #sanitizers
Reviewed By: filcab, #sanitizers
Subscribers: mclow.lists, srhines, kubamracek, dberris, rjmccall, rsmith, llvm-commits, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D50251
llvm-svn: 345659
2018-10-31 05:58:54 +08:00
|
|
|
ICCK_IntegerSignChange = 3,
|
|
|
|
ICCK_SignedIntegerTruncationOrSignChange = 4,
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ImplicitConversionData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &FromType;
|
|
|
|
const TypeDescriptor &ToType;
|
|
|
|
/* ImplicitConversionCheckKind */ unsigned char Kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Implict conversion that changed the value.
|
|
|
|
RECOVERABLE(implicit_conversion, ImplicitConversionData *Data, ValueHandle Src,
|
|
|
|
ValueHandle Dst)
|
|
|
|
|
2017-07-29 08:20:02 +08:00
|
|
|
/// Known builtin check kinds.
|
|
|
|
/// Keep in sync with the enum of the same name in CodeGenFunction.h
|
|
|
|
enum BuiltinCheckKind : unsigned char {
|
|
|
|
BCK_CTZPassedZero,
|
|
|
|
BCK_CLZPassedZero,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InvalidBuiltinData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
unsigned char Kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handle a builtin called in an invalid way.
|
|
|
|
RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data)
|
|
|
|
|
2019-12-14 04:59:40 +08:00
|
|
|
struct InvalidObjCCast {
|
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &ExpectedType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handle an invalid ObjC cast.
|
|
|
|
RECOVERABLE(invalid_objc_cast, InvalidObjCCast *Data, ValueHandle Pointer)
|
|
|
|
|
2014-08-13 08:26:40 +08:00
|
|
|
struct NonNullReturnData {
|
2014-09-09 04:17:19 +08:00
|
|
|
SourceLocation AttrLoc;
|
2014-08-13 08:26:40 +08:00
|
|
|
};
|
|
|
|
|
2017-03-15 00:32:27 +08:00
|
|
|
/// \brief Handle returning null from function with the returns_nonnull
|
|
|
|
/// attribute, or a return type annotated with _Nonnull.
|
2017-06-24 05:32:48 +08:00
|
|
|
RECOVERABLE(nonnull_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
|
|
|
|
RECOVERABLE(nullability_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
|
2014-08-13 08:26:40 +08:00
|
|
|
|
2014-09-09 01:22:45 +08:00
|
|
|
struct NonNullArgData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
SourceLocation AttrLoc;
|
|
|
|
int ArgIndex;
|
|
|
|
};
|
|
|
|
|
2017-03-15 00:32:27 +08:00
|
|
|
/// \brief Handle passing null pointer to a function parameter with the nonnull
|
|
|
|
/// attribute, or a _Nonnull type annotation.
|
2014-09-09 07:37:09 +08:00
|
|
|
RECOVERABLE(nonnull_arg, NonNullArgData *Data)
|
2017-03-15 00:32:27 +08:00
|
|
|
RECOVERABLE(nullability_arg, NonNullArgData *Data)
|
2014-09-09 01:22:45 +08:00
|
|
|
|
2017-06-02 03:40:59 +08:00
|
|
|
struct PointerOverflowData {
|
|
|
|
SourceLocation Loc;
|
|
|
|
};
|
|
|
|
|
|
|
|
RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base,
|
|
|
|
ValueHandle Result)
|
|
|
|
|
2016-01-26 07:34:38 +08: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,
|
2018-06-26 10:15:47 +08:00
|
|
|
CFITCK_NVMFCall,
|
|
|
|
CFITCK_VMFCall,
|
2016-01-26 07:34:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CFICheckFailData {
|
|
|
|
CFITypeCheckKind CheckKind;
|
2015-09-10 10:18:02 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
const TypeDescriptor &Type;
|
|
|
|
};
|
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
/// \brief Handle control flow integrity failures.
|
2016-02-04 06:19:04 +08:00
|
|
|
RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
|
|
|
|
uptr VtableIsValid)
|
2017-09-16 04:24:12 +08:00
|
|
|
|
|
|
|
struct ReportOptions;
|
|
|
|
|
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type(
|
|
|
|
CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable,
|
|
|
|
ReportOptions Opts);
|
|
|
|
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // UBSAN_HANDLERS_H
|