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 AlignmentAssumptionData { 43 SourceLocation Loc; 44 SourceLocation AssumptionLoc; 45 const TypeDescriptor &Type; 46 }; 47 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) 52 53 struct OverflowData { 54 SourceLocation Loc; 55 const TypeDescriptor &Type; 56 }; 57 58 /// \brief Handle an integer addition overflow. 59 RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) 60 61 /// \brief Handle an integer subtraction overflow. 62 RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) 63 64 /// \brief Handle an integer multiplication overflow. 65 RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) 66 67 /// \brief Handle a signed integer overflow for a unary negate operator. 68 RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal) 69 70 /// \brief Handle an INT_MIN/-1 overflow or division by zero. 71 RECOVERABLE(divrem_overflow, OverflowData *Data, 72 ValueHandle LHS, ValueHandle RHS) 73 74 struct ShiftOutOfBoundsData { 75 SourceLocation Loc; 76 const TypeDescriptor &LHSType; 77 const TypeDescriptor &RHSType; 78 }; 79 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) 84 85 struct OutOfBoundsData { 86 SourceLocation Loc; 87 const TypeDescriptor &ArrayType; 88 const TypeDescriptor &IndexType; 89 }; 90 91 /// \brief Handle an array index out of bounds error. 92 RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index) 93 94 struct UnreachableData { 95 SourceLocation Loc; 96 }; 97 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) 102 103 struct VLABoundData { 104 SourceLocation Loc; 105 const TypeDescriptor &Type; 106 }; 107 108 /// \brief Handle a VLA with a non-positive bound. 109 RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound) 110 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; 116 }; 117 118 struct FloatCastOverflowDataV2 { 119 SourceLocation Loc; 120 const TypeDescriptor &FromType; 121 const TypeDescriptor &ToType; 122 }; 123 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) 127 128 struct InvalidValueData { 129 SourceLocation Loc; 130 const TypeDescriptor &Type; 131 }; 132 133 /// \brief Handle a load of an invalid value for the type. 134 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val) 135 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, 144 }; 145 146 struct ImplicitConversionData { 147 SourceLocation Loc; 148 const TypeDescriptor &FromType; 149 const TypeDescriptor &ToType; 150 /* ImplicitConversionCheckKind */ unsigned char Kind; 151 }; 152 153 /// \brief Implict conversion that changed the value. 154 RECOVERABLE(implicit_conversion, ImplicitConversionData *Data, ValueHandle Src, 155 ValueHandle Dst) 156 157 /// Known builtin check kinds. 158 /// Keep in sync with the enum of the same name in CodeGenFunction.h 159 enum BuiltinCheckKind : unsigned char { 160 BCK_CTZPassedZero, 161 BCK_CLZPassedZero, 162 }; 163 164 struct InvalidBuiltinData { 165 SourceLocation Loc; 166 unsigned char Kind; 167 }; 168 169 /// Handle a builtin called in an invalid way. 170 RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data) 171 172 struct FunctionTypeMismatchData { 173 SourceLocation Loc; 174 const TypeDescriptor &Type; 175 }; 176 177 RECOVERABLE(function_type_mismatch, 178 FunctionTypeMismatchData *Data, 179 ValueHandle Val) 180 181 struct NonNullReturnData { 182 SourceLocation AttrLoc; 183 }; 184 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) 189 190 struct NonNullArgData { 191 SourceLocation Loc; 192 SourceLocation AttrLoc; 193 int ArgIndex; 194 }; 195 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) 200 201 struct PointerOverflowData { 202 SourceLocation Loc; 203 }; 204 205 RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base, 206 ValueHandle Result) 207 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 { 211 CFITCK_VCall, 212 CFITCK_NVCall, 213 CFITCK_DerivedCast, 214 CFITCK_UnrelatedCast, 215 CFITCK_ICall, 216 CFITCK_NVMFCall, 217 CFITCK_VMFCall, 218 }; 219 220 struct CFICheckFailData { 221 CFITypeCheckKind CheckKind; 222 SourceLocation Loc; 223 const TypeDescriptor &Type; 224 }; 225 226 /// \brief Handle control flow integrity failures. 227 RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function, 228 uptr VtableIsValid) 229 230 struct ReportOptions; 231 232 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type( 233 CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable, 234 ReportOptions Opts); 235 236 } 237 238 #endif // UBSAN_HANDLERS_H 239