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)
42 struct AlignmentAssumptionData {
44 SourceLocation AssumptionLoc;
45 const TypeDescriptor &Type;
48 /// \brief Handle a runtime alignment assumption check failure,
49 /// caused by a misaligned pointer.
50 RECOVERABLE(alignment_assumption, AlignmentAssumptionData *Data,
51 ValueHandle Pointer, ValueHandle Alignment, ValueHandle Offset)
55 const TypeDescriptor &Type;
58 /// \brief Handle an integer addition overflow.
59 RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
61 /// \brief Handle an integer subtraction overflow.
62 RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
64 /// \brief Handle an integer multiplication overflow.
65 RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
67 /// \brief Handle a signed integer overflow for a unary negate operator.
68 RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
70 /// \brief Handle an INT_MIN/-1 overflow or division by zero.
71 RECOVERABLE(divrem_overflow, OverflowData *Data,
72 ValueHandle LHS, ValueHandle RHS)
74 struct ShiftOutOfBoundsData {
76 const TypeDescriptor &LHSType;
77 const TypeDescriptor &RHSType;
80 /// \brief Handle a shift where the RHS is out of bounds or a left shift where
81 /// the LHS is negative or overflows.
82 RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
83 ValueHandle LHS, ValueHandle RHS)
85 struct OutOfBoundsData {
87 const TypeDescriptor &ArrayType;
88 const TypeDescriptor &IndexType;
91 /// \brief Handle an array index out of bounds error.
92 RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
94 struct UnreachableData {
98 /// \brief Handle a __builtin_unreachable which is reached.
99 UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
100 /// \brief Handle reaching the end of a value-returning function.
101 UNRECOVERABLE(missing_return, UnreachableData *Data)
103 struct VLABoundData {
105 const TypeDescriptor &Type;
108 /// \brief Handle a VLA with a non-positive bound.
109 RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
111 // Keeping this around for binary compatibility with (sanitized) programs
112 // compiled with older compilers.
113 struct FloatCastOverflowData {
114 const TypeDescriptor &FromType;
115 const TypeDescriptor &ToType;
118 struct FloatCastOverflowDataV2 {
120 const TypeDescriptor &FromType;
121 const TypeDescriptor &ToType;
124 /// Handle overflow in a conversion to or from a floating-point type.
125 /// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2*
126 RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From)
128 struct InvalidValueData {
130 const TypeDescriptor &Type;
133 /// \brief Handle a load of an invalid value for the type.
134 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
136 /// Known implicit conversion check kinds.
137 /// Keep in sync with the enum of the same name in CGExprScalar.cpp
138 enum ImplicitConversionCheckKind : unsigned char {
139 ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7.
140 ICCK_UnsignedIntegerTruncation = 1,
141 ICCK_SignedIntegerTruncation = 2,
142 ICCK_IntegerSignChange = 3,
143 ICCK_SignedIntegerTruncationOrSignChange = 4,
146 struct ImplicitConversionData {
148 const TypeDescriptor &FromType;
149 const TypeDescriptor &ToType;
150 /* ImplicitConversionCheckKind */ unsigned char Kind;
153 /// \brief Implict conversion that changed the value.
154 RECOVERABLE(implicit_conversion, ImplicitConversionData *Data, ValueHandle Src,
157 /// Known builtin check kinds.
158 /// Keep in sync with the enum of the same name in CodeGenFunction.h
159 enum BuiltinCheckKind : unsigned char {
164 struct InvalidBuiltinData {
169 /// Handle a builtin called in an invalid way.
170 RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data)
172 struct FunctionTypeMismatchData {
174 const TypeDescriptor &Type;
177 RECOVERABLE(function_type_mismatch,
178 FunctionTypeMismatchData *Data,
181 struct NonNullReturnData {
182 SourceLocation AttrLoc;
185 /// \brief Handle returning null from function with the returns_nonnull
186 /// attribute, or a return type annotated with _Nonnull.
187 RECOVERABLE(nonnull_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
188 RECOVERABLE(nullability_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
190 struct NonNullArgData {
192 SourceLocation AttrLoc;
196 /// \brief Handle passing null pointer to a function parameter with the nonnull
197 /// attribute, or a _Nonnull type annotation.
198 RECOVERABLE(nonnull_arg, NonNullArgData *Data)
199 RECOVERABLE(nullability_arg, NonNullArgData *Data)
201 struct PointerOverflowData {
205 RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base,
208 /// \brief Known CFI check kinds.
209 /// Keep in sync with the enum of the same name in CodeGenFunction.h
210 enum CFITypeCheckKind : unsigned char {
214 CFITCK_UnrelatedCast,
220 struct CFICheckFailData {
221 CFITypeCheckKind CheckKind;
223 const TypeDescriptor &Type;
226 /// \brief Handle control flow integrity failures.
227 RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
230 struct ReportOptions;
232 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type(
233 CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable,
238 #endif // UBSAN_HANDLERS_H