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