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 RECOVERABLE(checkname, ...) \
26 extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
27 void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
28 extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
29 void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
31 /// \brief Handle a runtime type check failure, caused by either a misaligned
32 /// pointer, a null pointer, or a pointer to insufficient storage for the
34 RECOVERABLE(type_mismatch
, TypeMismatchData
*Data
, ValueHandle Pointer
)
38 const TypeDescriptor
&Type
;
41 /// \brief Handle an integer addition overflow.
42 RECOVERABLE(add_overflow
, OverflowData
*Data
, ValueHandle LHS
, ValueHandle RHS
)
44 /// \brief Handle an integer subtraction overflow.
45 RECOVERABLE(sub_overflow
, OverflowData
*Data
, ValueHandle LHS
, ValueHandle RHS
)
47 /// \brief Handle an integer multiplication overflow.
48 RECOVERABLE(mul_overflow
, OverflowData
*Data
, ValueHandle LHS
, ValueHandle RHS
)
50 /// \brief Handle a signed integer overflow for a unary negate operator.
51 RECOVERABLE(negate_overflow
, OverflowData
*Data
, ValueHandle OldVal
)
53 /// \brief Handle an INT_MIN/-1 overflow or division by zero.
54 RECOVERABLE(divrem_overflow
, OverflowData
*Data
,
55 ValueHandle LHS
, ValueHandle RHS
)
57 struct ShiftOutOfBoundsData
{
59 const TypeDescriptor
&LHSType
;
60 const TypeDescriptor
&RHSType
;
63 /// \brief Handle a shift where the RHS is out of bounds or a left shift where
64 /// the LHS is negative or overflows.
65 RECOVERABLE(shift_out_of_bounds
, ShiftOutOfBoundsData
*Data
,
66 ValueHandle LHS
, ValueHandle RHS
)
68 struct OutOfBoundsData
{
70 const TypeDescriptor
&ArrayType
;
71 const TypeDescriptor
&IndexType
;
74 /// \brief Handle an array index out of bounds error.
75 RECOVERABLE(out_of_bounds
, OutOfBoundsData
*Data
, ValueHandle Index
)
77 struct UnreachableData
{
81 /// \brief Handle a __builtin_unreachable which is reached.
82 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
83 void __ubsan_handle_builtin_unreachable(UnreachableData
*Data
);
84 /// \brief Handle reaching the end of a value-returning function.
85 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
86 void __ubsan_handle_missing_return(UnreachableData
*Data
);
90 const TypeDescriptor
&Type
;
93 /// \brief Handle a VLA with a non-positive bound.
94 RECOVERABLE(vla_bound_not_positive
, VLABoundData
*Data
, ValueHandle Bound
)
96 struct FloatCastOverflowData
{
97 // FIXME: SourceLocation Loc;
98 const TypeDescriptor
&FromType
;
99 const TypeDescriptor
&ToType
;
102 /// \brief Handle overflow in a conversion to or from a floating-point type.
103 RECOVERABLE(float_cast_overflow
, FloatCastOverflowData
*Data
, ValueHandle From
)
105 struct InvalidValueData
{
107 const TypeDescriptor
&Type
;
110 /// \brief Handle a load of an invalid value for the type.
111 RECOVERABLE(load_invalid_value
, InvalidValueData
*Data
, ValueHandle Val
)
113 struct FunctionTypeMismatchData
{
115 const TypeDescriptor
&Type
;
118 RECOVERABLE(function_type_mismatch
,
119 FunctionTypeMismatchData
*Data
,
124 #endif // UBSAN_HANDLERS_H