]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/ubsan/ubsan_handlers.h
Merge ^/head r321307 through r321350.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / ubsan / ubsan_handlers.h
1 //===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Entry points to the runtime library for Clang's undefined behavior sanitizer.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef UBSAN_HANDLERS_H
14 #define UBSAN_HANDLERS_H
15
16 #include "ubsan_value.h"
17
18 namespace __ubsan {
19
20 struct TypeMismatchData {
21   SourceLocation Loc;
22   const TypeDescriptor &Type;
23   unsigned char LogAlignment;
24   unsigned char TypeCheckKind;
25 };
26
27 #define UNRECOVERABLE(checkname, ...) \
28   extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
29     void __ubsan_handle_ ## checkname( __VA_ARGS__ );
30
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__ );
36
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
39 /// type.
40 RECOVERABLE(type_mismatch_v1, TypeMismatchData *Data, ValueHandle Pointer)
41
42 struct OverflowData {
43   SourceLocation Loc;
44   const TypeDescriptor &Type;
45 };
46
47 /// \brief Handle an integer addition overflow.
48 RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
49
50 /// \brief Handle an integer subtraction overflow.
51 RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
52
53 /// \brief Handle an integer multiplication overflow.
54 RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
55
56 /// \brief Handle a signed integer overflow for a unary negate operator.
57 RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
58
59 /// \brief Handle an INT_MIN/-1 overflow or division by zero.
60 RECOVERABLE(divrem_overflow, OverflowData *Data,
61             ValueHandle LHS, ValueHandle RHS)
62
63 struct ShiftOutOfBoundsData {
64   SourceLocation Loc;
65   const TypeDescriptor &LHSType;
66   const TypeDescriptor &RHSType;
67 };
68
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)
73
74 struct OutOfBoundsData {
75   SourceLocation Loc;
76   const TypeDescriptor &ArrayType;
77   const TypeDescriptor &IndexType;
78 };
79
80 /// \brief Handle an array index out of bounds error.
81 RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
82
83 struct UnreachableData {
84   SourceLocation Loc;
85 };
86
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)
91
92 struct VLABoundData {
93   SourceLocation Loc;
94   const TypeDescriptor &Type;
95 };
96
97 /// \brief Handle a VLA with a non-positive bound.
98 RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
99
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;
105 };
106
107 struct FloatCastOverflowDataV2 {
108   SourceLocation Loc;
109   const TypeDescriptor &FromType;
110   const TypeDescriptor &ToType;
111 };
112
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)
116
117 struct InvalidValueData {
118   SourceLocation Loc;
119   const TypeDescriptor &Type;
120 };
121
122 /// \brief Handle a load of an invalid value for the type.
123 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
124
125 struct FunctionTypeMismatchData {
126   SourceLocation Loc;
127   const TypeDescriptor &Type;
128 };
129
130 RECOVERABLE(function_type_mismatch,
131             FunctionTypeMismatchData *Data,
132             ValueHandle Val)
133
134 struct NonNullReturnData {
135   SourceLocation AttrLoc;
136 };
137
138 /// \brief Handle returning null from function with the returns_nonnull
139 /// attribute, or a return type annotated with _Nonnull.
140 RECOVERABLE(nonnull_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
141 RECOVERABLE(nullability_return_v1, NonNullReturnData *Data, SourceLocation *Loc)
142
143 struct NonNullArgData {
144   SourceLocation Loc;
145   SourceLocation AttrLoc;
146   int ArgIndex;
147 };
148
149 /// \brief Handle passing null pointer to a function parameter with the nonnull
150 /// attribute, or a _Nonnull type annotation.
151 RECOVERABLE(nonnull_arg, NonNullArgData *Data)
152 RECOVERABLE(nullability_arg, NonNullArgData *Data)
153
154 struct PointerOverflowData {
155   SourceLocation Loc;
156 };
157
158 RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base,
159             ValueHandle Result)
160
161 /// \brief Known CFI check kinds.
162 /// Keep in sync with the enum of the same name in CodeGenFunction.h
163 enum CFITypeCheckKind : unsigned char {
164   CFITCK_VCall,
165   CFITCK_NVCall,
166   CFITCK_DerivedCast,
167   CFITCK_UnrelatedCast,
168   CFITCK_ICall,
169 };
170
171 struct CFICheckFailData {
172   CFITypeCheckKind CheckKind;
173   SourceLocation Loc;
174   const TypeDescriptor &Type;
175 };
176
177 /// \brief Handle control flow integrity failures.
178 RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
179             uptr VtableIsValid)
180 }
181
182 #endif // UBSAN_HANDLERS_H