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