ubsan_handlers.h revision 1.5 1 1.1 mrg //===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
2 1.1 mrg //
3 1.1 mrg // This file is distributed under the University of Illinois Open Source
4 1.1 mrg // License. See LICENSE.TXT for details.
5 1.1 mrg //
6 1.1 mrg //===----------------------------------------------------------------------===//
7 1.1 mrg //
8 1.1 mrg // Entry points to the runtime library for Clang's undefined behavior sanitizer.
9 1.1 mrg //
10 1.1 mrg //===----------------------------------------------------------------------===//
11 1.1 mrg #ifndef UBSAN_HANDLERS_H
12 1.1 mrg #define UBSAN_HANDLERS_H
13 1.1 mrg
14 1.1 mrg #include "ubsan_value.h"
15 1.1 mrg
16 1.1 mrg namespace __ubsan {
17 1.1 mrg
18 1.1 mrg struct TypeMismatchData {
19 1.1 mrg SourceLocation Loc;
20 1.1 mrg const TypeDescriptor &Type;
21 1.4 mrg unsigned char LogAlignment;
22 1.1 mrg unsigned char TypeCheckKind;
23 1.1 mrg };
24 1.1 mrg
25 1.1 mrg #define UNRECOVERABLE(checkname, ...) \
26 1.1 mrg extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
27 1.1 mrg void __ubsan_handle_ ## checkname( __VA_ARGS__ );
28 1.1 mrg
29 1.1 mrg #define RECOVERABLE(checkname, ...) \
30 1.1 mrg extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
31 1.1 mrg void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
32 1.1 mrg extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
33 1.1 mrg void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
34 1.1 mrg
35 1.1 mrg /// \brief Handle a runtime type check failure, caused by either a misaligned
36 1.1 mrg /// pointer, a null pointer, or a pointer to insufficient storage for the
37 1.1 mrg /// type.
38 1.4 mrg RECOVERABLE(type_mismatch_v1, TypeMismatchData *Data, ValueHandle Pointer)
39 1.1 mrg
40 1.1 mrg struct OverflowData {
41 1.1 mrg SourceLocation Loc;
42 1.1 mrg const TypeDescriptor &Type;
43 1.1 mrg };
44 1.1 mrg
45 1.1 mrg /// \brief Handle an integer addition overflow.
46 1.1 mrg RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
47 1.1 mrg
48 1.1 mrg /// \brief Handle an integer subtraction overflow.
49 1.1 mrg RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
50 1.1 mrg
51 1.1 mrg /// \brief Handle an integer multiplication overflow.
52 1.1 mrg RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
53 1.1 mrg
54 1.1 mrg /// \brief Handle a signed integer overflow for a unary negate operator.
55 1.1 mrg RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
56 1.1 mrg
57 1.1 mrg /// \brief Handle an INT_MIN/-1 overflow or division by zero.
58 1.1 mrg RECOVERABLE(divrem_overflow, OverflowData *Data,
59 1.1 mrg ValueHandle LHS, ValueHandle RHS)
60 1.1 mrg
61 1.1 mrg struct ShiftOutOfBoundsData {
62 1.1 mrg SourceLocation Loc;
63 1.1 mrg const TypeDescriptor &LHSType;
64 1.1 mrg const TypeDescriptor &RHSType;
65 1.1 mrg };
66 1.1 mrg
67 1.1 mrg /// \brief Handle a shift where the RHS is out of bounds or a left shift where
68 1.1 mrg /// the LHS is negative or overflows.
69 1.1 mrg RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
70 1.1 mrg ValueHandle LHS, ValueHandle RHS)
71 1.1 mrg
72 1.1 mrg struct OutOfBoundsData {
73 1.1 mrg SourceLocation Loc;
74 1.1 mrg const TypeDescriptor &ArrayType;
75 1.1 mrg const TypeDescriptor &IndexType;
76 1.1 mrg };
77 1.1 mrg
78 1.1 mrg /// \brief Handle an array index out of bounds error.
79 1.1 mrg RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
80 1.1 mrg
81 1.1 mrg struct UnreachableData {
82 1.1 mrg SourceLocation Loc;
83 1.1 mrg };
84 1.1 mrg
85 1.1 mrg /// \brief Handle a __builtin_unreachable which is reached.
86 1.1 mrg UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
87 1.1 mrg /// \brief Handle reaching the end of a value-returning function.
88 1.1 mrg UNRECOVERABLE(missing_return, UnreachableData *Data)
89 1.1 mrg
90 1.1 mrg struct VLABoundData {
91 1.1 mrg SourceLocation Loc;
92 1.1 mrg const TypeDescriptor &Type;
93 1.1 mrg };
94 1.1 mrg
95 1.1 mrg /// \brief Handle a VLA with a non-positive bound.
96 1.1 mrg RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
97 1.1 mrg
98 1.3 mrg // Keeping this around for binary compatibility with (sanitized) programs
99 1.3 mrg // compiled with older compilers.
100 1.1 mrg struct FloatCastOverflowData {
101 1.1 mrg const TypeDescriptor &FromType;
102 1.1 mrg const TypeDescriptor &ToType;
103 1.1 mrg };
104 1.1 mrg
105 1.3 mrg struct FloatCastOverflowDataV2 {
106 1.3 mrg SourceLocation Loc;
107 1.3 mrg const TypeDescriptor &FromType;
108 1.3 mrg const TypeDescriptor &ToType;
109 1.3 mrg };
110 1.3 mrg
111 1.3 mrg /// Handle overflow in a conversion to or from a floating-point type.
112 1.3 mrg /// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2*
113 1.3 mrg RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From)
114 1.1 mrg
115 1.1 mrg struct InvalidValueData {
116 1.1 mrg SourceLocation Loc;
117 1.1 mrg const TypeDescriptor &Type;
118 1.1 mrg };
119 1.1 mrg
120 1.1 mrg /// \brief Handle a load of an invalid value for the type.
121 1.1 mrg RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
122 1.1 mrg
123 1.5 mrg /// Known implicit conversion check kinds.
124 1.5 mrg /// Keep in sync with the enum of the same name in CGExprScalar.cpp
125 1.5 mrg enum ImplicitConversionCheckKind : unsigned char {
126 1.5 mrg ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7.
127 1.5 mrg ICCK_UnsignedIntegerTruncation = 1,
128 1.5 mrg ICCK_SignedIntegerTruncation = 2,
129 1.5 mrg };
130 1.5 mrg
131 1.5 mrg struct ImplicitConversionData {
132 1.5 mrg SourceLocation Loc;
133 1.5 mrg const TypeDescriptor &FromType;
134 1.5 mrg const TypeDescriptor &ToType;
135 1.5 mrg /* ImplicitConversionCheckKind */ unsigned char Kind;
136 1.5 mrg };
137 1.5 mrg
138 1.5 mrg /// \brief Implict conversion that changed the value.
139 1.5 mrg RECOVERABLE(implicit_conversion, ImplicitConversionData *Data, ValueHandle Src,
140 1.5 mrg ValueHandle Dst)
141 1.5 mrg
142 1.4 mrg /// Known builtin check kinds.
143 1.4 mrg /// Keep in sync with the enum of the same name in CodeGenFunction.h
144 1.4 mrg enum BuiltinCheckKind : unsigned char {
145 1.4 mrg BCK_CTZPassedZero,
146 1.4 mrg BCK_CLZPassedZero,
147 1.4 mrg };
148 1.4 mrg
149 1.4 mrg struct InvalidBuiltinData {
150 1.4 mrg SourceLocation Loc;
151 1.4 mrg unsigned char Kind;
152 1.4 mrg };
153 1.4 mrg
154 1.4 mrg /// Handle a builtin called in an invalid way.
155 1.4 mrg RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data)
156 1.4 mrg
157 1.1 mrg struct FunctionTypeMismatchData {
158 1.1 mrg SourceLocation Loc;
159 1.1 mrg const TypeDescriptor &Type;
160 1.1 mrg };
161 1.1 mrg
162 1.1 mrg RECOVERABLE(function_type_mismatch,
163 1.1 mrg FunctionTypeMismatchData *Data,
164 1.1 mrg ValueHandle Val)
165 1.1 mrg
166 1.1 mrg struct NonNullReturnData {
167 1.1 mrg SourceLocation AttrLoc;
168 1.1 mrg };
169 1.1 mrg
170 1.4 mrg /// \brief Handle returning null from function with the returns_nonnull
171 1.4 mrg /// attribute, or a return type annotated with _Nonnull.
172 1.4 mrg RECOVERABLE(nonnull_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
173 1.4 mrg RECOVERABLE(nullability_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
174 1.1 mrg
175 1.1 mrg struct NonNullArgData {
176 1.1 mrg SourceLocation Loc;
177 1.1 mrg SourceLocation AttrLoc;
178 1.1 mrg int ArgIndex;
179 1.1 mrg };
180 1.1 mrg
181 1.4 mrg /// \brief Handle passing null pointer to a function parameter with the nonnull
182 1.4 mrg /// attribute, or a _Nonnull type annotation.
183 1.1 mrg RECOVERABLE(nonnull_arg, NonNullArgData *Data)
184 1.4 mrg RECOVERABLE(nullability_arg, NonNullArgData *Data)
185 1.4 mrg
186 1.4 mrg struct PointerOverflowData {
187 1.4 mrg SourceLocation Loc;
188 1.4 mrg };
189 1.4 mrg
190 1.4 mrg RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base,
191 1.4 mrg ValueHandle Result)
192 1.1 mrg
193 1.3 mrg /// \brief Known CFI check kinds.
194 1.3 mrg /// Keep in sync with the enum of the same name in CodeGenFunction.h
195 1.3 mrg enum CFITypeCheckKind : unsigned char {
196 1.3 mrg CFITCK_VCall,
197 1.3 mrg CFITCK_NVCall,
198 1.3 mrg CFITCK_DerivedCast,
199 1.3 mrg CFITCK_UnrelatedCast,
200 1.3 mrg CFITCK_ICall,
201 1.5 mrg CFITCK_NVMFCall,
202 1.5 mrg CFITCK_VMFCall,
203 1.5 mrg };
204 1.5 mrg
205 1.5 mrg struct CFIBadIcallData {
206 1.5 mrg SourceLocation Loc;
207 1.5 mrg const TypeDescriptor &Type;
208 1.3 mrg };
209 1.3 mrg
210 1.3 mrg struct CFICheckFailData {
211 1.3 mrg CFITypeCheckKind CheckKind;
212 1.3 mrg SourceLocation Loc;
213 1.3 mrg const TypeDescriptor &Type;
214 1.3 mrg };
215 1.3 mrg
216 1.5 mrg /// \brief Handle control flow integrity failure for indirect function calls.
217 1.5 mrg RECOVERABLE(cfi_bad_icall, CFIBadIcallData *Data, ValueHandle Function)
218 1.5 mrg
219 1.3 mrg /// \brief Handle control flow integrity failures.
220 1.3 mrg RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
221 1.3 mrg uptr VtableIsValid)
222 1.4 mrg
223 1.4 mrg struct ReportOptions;
224 1.4 mrg
225 1.4 mrg extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type(
226 1.4 mrg CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable,
227 1.4 mrg ReportOptions Opts);
228 1.4 mrg
229 1.1 mrg }
230 1.1 mrg
231 1.1 mrg #endif // UBSAN_HANDLERS_H
232