1 //===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Entry points to the runtime library for Clang's undefined behavior sanitizer.
10 //===----------------------------------------------------------------------===//
11 #ifndef UBSAN_HANDLERS_H
12 #define UBSAN_HANDLERS_H
14 #include "ubsan_value.h"
18 struct TypeMismatchData
{
20 const TypeDescriptor
&Type
;
22 unsigned char TypeCheckKind
;
25 #define UNRECOVERABLE(checkname, ...) \
26 extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
27 void __ubsan_handle_ ## checkname( __VA_ARGS__ );
29 #define RECOVERABLE(checkname, ...) \
30 extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
31 void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
32 extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
33 void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
35 /// \brief Handle a runtime type check failure, caused by either a misaligned
36 /// pointer, a null pointer, or a pointer to insufficient storage for the
38 RECOVERABLE(type_mismatch
, TypeMismatchData
*Data
, ValueHandle Pointer
)
42 const TypeDescriptor
&Type
;
45 /// \brief Handle an integer addition overflow.
46 RECOVERABLE(add_overflow
, OverflowData
*Data
, ValueHandle LHS
, ValueHandle RHS
)
48 /// \brief Handle an integer subtraction overflow.
49 RECOVERABLE(sub_overflow
, OverflowData
*Data
, ValueHandle LHS
, ValueHandle RHS
)
51 /// \brief Handle an integer multiplication overflow.
52 RECOVERABLE(mul_overflow
, OverflowData
*Data
, ValueHandle LHS
, ValueHandle RHS
)
54 /// \brief Handle a signed integer overflow for a unary negate operator.
55 RECOVERABLE(negate_overflow
, OverflowData
*Data
, ValueHandle OldVal
)
57 /// \brief Handle an INT_MIN/-1 overflow or division by zero.
58 RECOVERABLE(divrem_overflow
, OverflowData
*Data
,
59 ValueHandle LHS
, ValueHandle RHS
)
61 struct ShiftOutOfBoundsData
{
63 const TypeDescriptor
&LHSType
;
64 const TypeDescriptor
&RHSType
;
67 /// \brief Handle a shift where the RHS is out of bounds or a left shift where
68 /// the LHS is negative or overflows.
69 RECOVERABLE(shift_out_of_bounds
, ShiftOutOfBoundsData
*Data
,
70 ValueHandle LHS
, ValueHandle RHS
)
72 struct OutOfBoundsData
{
74 const TypeDescriptor
&ArrayType
;
75 const TypeDescriptor
&IndexType
;
78 /// \brief Handle an array index out of bounds error.
79 RECOVERABLE(out_of_bounds
, OutOfBoundsData
*Data
, ValueHandle Index
)
81 struct UnreachableData
{
85 /// \brief Handle a __builtin_unreachable which is reached.
86 UNRECOVERABLE(builtin_unreachable
, UnreachableData
*Data
)
87 /// \brief Handle reaching the end of a value-returning function.
88 UNRECOVERABLE(missing_return
, UnreachableData
*Data
)
92 const TypeDescriptor
&Type
;
95 /// \brief Handle a VLA with a non-positive bound.
96 RECOVERABLE(vla_bound_not_positive
, VLABoundData
*Data
, ValueHandle Bound
)
98 struct FloatCastOverflowData
{
99 // FIXME: SourceLocation Loc;
100 const TypeDescriptor
&FromType
;
101 const TypeDescriptor
&ToType
;
104 /// \brief Handle overflow in a conversion to or from a floating-point type.
105 RECOVERABLE(float_cast_overflow
, FloatCastOverflowData
*Data
, ValueHandle From
)
107 struct InvalidValueData
{
109 const TypeDescriptor
&Type
;
112 /// \brief Handle a load of an invalid value for the type.
113 RECOVERABLE(load_invalid_value
, InvalidValueData
*Data
, ValueHandle Val
)
115 struct FunctionTypeMismatchData
{
117 const TypeDescriptor
&Type
;
120 RECOVERABLE(function_type_mismatch
,
121 FunctionTypeMismatchData
*Data
,
124 struct NonNullReturnData
{
126 SourceLocation AttrLoc
;
129 /// \brief Handle returning null from function with returns_nonnull attribute.
130 RECOVERABLE(nonnull_return
, NonNullReturnData
*Data
)
132 struct NonNullArgData
{
134 SourceLocation AttrLoc
;
138 /// \brief Handle passing null pointer to function with nonnull attribute.
139 RECOVERABLE(nonnull_arg
, NonNullArgData
*Data
)
143 #endif // UBSAN_HANDLERS_H