1 //===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Entry points to the runtime library for Clang's undefined behavior sanitizer.
12 //===----------------------------------------------------------------------===//
13 #ifndef UBSAN_HANDLERS_H
14 #define UBSAN_HANDLERS_H
16 #include "ubsan_value.h"
20 struct TypeMismatchData {
22 const TypeDescriptor &Type;
23 unsigned char LogAlignment;
24 unsigned char TypeCheckKind;
27 #define UNRECOVERABLE(checkname, ...) \
28 extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
29 void __ubsan_handle_ ## checkname( __VA_ARGS__ );
31 #define RECOVERABLE(checkname, ...) \
32 extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
33 void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
34 extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
35 void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
37 /// \brief Handle a runtime type check failure, caused by either a misaligned
38 /// pointer, a null pointer, or a pointer to insufficient storage for the
40 RECOVERABLE(type_mismatch_v1, TypeMismatchData *Data, ValueHandle Pointer)
44 const TypeDescriptor &Type;
47 /// \brief Handle an integer addition overflow.
48 RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
50 /// \brief Handle an integer subtraction overflow.
51 RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
53 /// \brief Handle an integer multiplication overflow.
54 RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
56 /// \brief Handle a signed integer overflow for a unary negate operator.
57 RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
59 /// \brief Handle an INT_MIN/-1 overflow or division by zero.
60 RECOVERABLE(divrem_overflow, OverflowData *Data,
61 ValueHandle LHS, ValueHandle RHS)
63 struct ShiftOutOfBoundsData {
65 const TypeDescriptor &LHSType;
66 const TypeDescriptor &RHSType;
69 /// \brief Handle a shift where the RHS is out of bounds or a left shift where
70 /// the LHS is negative or overflows.
71 RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
72 ValueHandle LHS, ValueHandle RHS)
74 struct OutOfBoundsData {
76 const TypeDescriptor &ArrayType;
77 const TypeDescriptor &IndexType;
80 /// \brief Handle an array index out of bounds error.
81 RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
83 struct UnreachableData {
87 /// \brief Handle a __builtin_unreachable which is reached.
88 UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
89 /// \brief Handle reaching the end of a value-returning function.
90 UNRECOVERABLE(missing_return, UnreachableData *Data)
94 const TypeDescriptor &Type;
97 /// \brief Handle a VLA with a non-positive bound.
98 RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
100 // Keeping this around for binary compatibility with (sanitized) programs
101 // compiled with older compilers.
102 struct FloatCastOverflowData {
103 const TypeDescriptor &FromType;
104 const TypeDescriptor &ToType;
107 struct FloatCastOverflowDataV2 {
109 const TypeDescriptor &FromType;
110 const TypeDescriptor &ToType;
113 /// Handle overflow in a conversion to or from a floating-point type.
114 /// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2*
115 RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From)
117 struct InvalidValueData {
119 const TypeDescriptor &Type;
122 /// \brief Handle a load of an invalid value for the type.
123 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
125 /// Known implicit conversion check kinds.
126 /// Keep in sync with the enum of the same name in CGExprScalar.cpp
127 enum ImplicitConversionCheckKind : unsigned char {
128 ICCK_IntegerTruncation = 0,
131 struct ImplicitConversionData {
133 const TypeDescriptor &FromType;
134 const TypeDescriptor &ToType;
135 /* ImplicitConversionCheckKind */ unsigned char Kind;
138 /// \brief Implict conversion that changed the value.
139 RECOVERABLE(implicit_conversion, ImplicitConversionData *Data, ValueHandle Src,
142 /// Known builtin check kinds.
143 /// Keep in sync with the enum of the same name in CodeGenFunction.h
144 enum BuiltinCheckKind : unsigned char {
149 struct InvalidBuiltinData {
154 /// Handle a builtin called in an invalid way.
155 RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data)
157 struct FunctionTypeMismatchData {
159 const TypeDescriptor &Type;
162 RECOVERABLE(function_type_mismatch,
163 FunctionTypeMismatchData *Data,
166 struct NonNullReturnData {
167 SourceLocation AttrLoc;
170 /// \brief Handle returning null from function with the returns_nonnull
171 /// attribute, or a return type annotated with _Nonnull.
172 RECOVERABLE(nonnull_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
173 RECOVERABLE(nullability_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
175 struct NonNullArgData {
177 SourceLocation AttrLoc;
181 /// \brief Handle passing null pointer to a function parameter with the nonnull
182 /// attribute, or a _Nonnull type annotation.
183 RECOVERABLE(nonnull_arg, NonNullArgData *Data)
184 RECOVERABLE(nullability_arg, NonNullArgData *Data)
186 struct PointerOverflowData {
190 RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base,
193 /// \brief Known CFI check kinds.
194 /// Keep in sync with the enum of the same name in CodeGenFunction.h
195 enum CFITypeCheckKind : unsigned char {
199 CFITCK_UnrelatedCast,
205 struct CFICheckFailData {
206 CFITypeCheckKind CheckKind;
208 const TypeDescriptor &Type;
211 /// \brief Handle control flow integrity failures.
212 RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
215 struct ReportOptions;
217 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type(
218 CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable,
223 #endif // UBSAN_HANDLERS_H