| 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 |
|
| 17 |
namespace __ubsan { |
| 18 |
|
| 19 |
struct 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. |
| 39 |
RECOVERABLE(type_mismatch_v1, TypeMismatchData *Data, ValueHandle Pointer) |
| 40 |
|
| 41 |
struct 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. |
| 49 |
RECOVERABLE(alignment_assumption, AlignmentAssumptionData *Data, |
| 50 |
ValueHandle Pointer, ValueHandle Alignment, ValueHandle Offset) |
| 51 |
|
| 52 |
struct OverflowData { |
| 53 |
SourceLocation Loc; |
| 54 |
const TypeDescriptor &Type; |
| 55 |
}; |
| 56 |
|
| 57 |
/// \brief Handle an integer addition overflow. |
| 58 |
RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) |
| 59 |
|
| 60 |
/// \brief Handle an integer subtraction overflow. |
| 61 |
RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) |
| 62 |
|
| 63 |
/// \brief Handle an integer multiplication overflow. |
| 64 |
RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) |
| 65 |
|
| 66 |
/// \brief Handle a signed integer overflow for a unary negate operator. |
| 67 |
RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal) |
| 68 |
|
| 69 |
/// \brief Handle an INT_MIN/-1 overflow or division by zero. |
| 70 |
RECOVERABLE(divrem_overflow, OverflowData *Data, |
| 71 |
ValueHandle LHS, ValueHandle RHS) |
| 72 |
|
| 73 |
struct ShiftOutOfBoundsData { |
| 74 |
SourceLocation Loc; |
| 75 |
const TypeDescriptor &LHSType; |
| 76 |
const TypeDescriptor &RHSType; |
| 77 |
}; |
| 78 |
|
| 79 |
/// \brief Handle a shift where the RHS is out of bounds or a left shift where |
| 80 |
/// the LHS is negative or overflows. |
| 81 |
RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data, |
| 82 |
ValueHandle LHS, ValueHandle RHS) |
| 83 |
|
| 84 |
struct OutOfBoundsData { |
| 85 |
SourceLocation Loc; |
| 86 |
const TypeDescriptor &ArrayType; |
| 87 |
const TypeDescriptor &IndexType; |
| 88 |
}; |
| 89 |
|
| 90 |
/// \brief Handle an array index out of bounds error. |
| 91 |
RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index) |
| 92 |
|
| 93 |
struct UnreachableData { |
| 94 |
SourceLocation Loc; |
| 95 |
}; |
| 96 |
|
| 97 |
/// \brief Handle a __builtin_unreachable which is reached. |
| 98 |
UNRECOVERABLE(builtin_unreachable, UnreachableData *Data) |
| 99 |
/// \brief Handle reaching the end of a value-returning function. |
| 100 |
UNRECOVERABLE(missing_return, UnreachableData *Data) |
| 101 |
|
| 102 |
struct VLABoundData { |
| 103 |
SourceLocation Loc; |
| 104 |
const TypeDescriptor &Type; |
| 105 |
}; |
| 106 |
|
| 107 |
/// \brief Handle a VLA with a non-positive bound. |
| 108 |
RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound) |
| 109 |
|
| 110 |
// Keeping this around for binary compatibility with (sanitized) programs |
| 111 |
// compiled with older compilers. |
| 112 |
struct FloatCastOverflowData { |
| 113 |
const TypeDescriptor &FromType; |
| 114 |
const TypeDescriptor &ToType; |
| 115 |
}; |
| 116 |
|
| 117 |
struct FloatCastOverflowDataV2 { |
| 118 |
SourceLocation Loc; |
| 119 |
const TypeDescriptor &FromType; |
| 120 |
const TypeDescriptor &ToType; |
| 121 |
}; |
| 122 |
|
| 123 |
/// Handle overflow in a conversion to or from a floating-point type. |
| 124 |
/// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2* |
| 125 |
RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From) |
| 126 |
|
| 127 |
struct InvalidValueData { |
| 128 |
SourceLocation Loc; |
| 129 |
const TypeDescriptor &Type; |
| 130 |
}; |
| 131 |
|
| 132 |
/// \brief Handle a load of an invalid value for the type. |
| 133 |
RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val) |
| 134 |
|
| 135 |
/// Known implicit conversion check kinds. |
| 136 |
/// Keep in sync with the enum of the same name in CGExprScalar.cpp |
| 137 |
enum ImplicitConversionCheckKind : unsigned char { |
| 138 |
ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7. |
| 139 |
ICCK_UnsignedIntegerTruncation = 1, |
| 140 |
ICCK_SignedIntegerTruncation = 2, |
| 141 |
ICCK_IntegerSignChange = 3, |
| 142 |
ICCK_SignedIntegerTruncationOrSignChange = 4, |
| 143 |
}; |
| 144 |
|
| 145 |
struct ImplicitConversionData { |
| 146 |
SourceLocation Loc; |
| 147 |
const TypeDescriptor &FromType; |
| 148 |
const TypeDescriptor &ToType; |
| 149 |
/* ImplicitConversionCheckKind */ unsigned char Kind; |
| 150 |
unsigned int BitfieldBits; |
| 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 |
BCK_AssumePassedFalse, |
| 163 |
}; |
| 164 |
|
| 165 |
struct InvalidBuiltinData { |
| 166 |
SourceLocation Loc; |
| 167 |
unsigned char Kind; |
| 168 |
}; |
| 169 |
|
| 170 |
/// Handle a builtin called in an invalid way. |
| 171 |
RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data) |
| 172 |
|
| 173 |
struct InvalidObjCCast { |
| 174 |
SourceLocation Loc; |
| 175 |
const TypeDescriptor &ExpectedType; |
| 176 |
}; |
| 177 |
|
| 178 |
/// Handle an invalid ObjC cast. |
| 179 |
RECOVERABLE(invalid_objc_cast, InvalidObjCCast *Data, ValueHandle Pointer) |
| 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 CFIBadIcallData { |
| 221 |
SourceLocation Loc; |
| 222 |
const TypeDescriptor &Type; |
| 223 |
}; |
| 224 |
|
| 225 |
struct CFICheckFailData { |
| 226 |
CFITypeCheckKind CheckKind; |
| 227 |
SourceLocation Loc; |
| 228 |
const TypeDescriptor &Type; |
| 229 |
}; |
| 230 |
|
| 231 |
/// \brief Handle control flow integrity failure for indirect function calls. |
| 232 |
RECOVERABLE(cfi_bad_icall, CFIBadIcallData *Data, ValueHandle Function) |
| 233 |
|
| 234 |
/// \brief Handle control flow integrity failures. |
| 235 |
RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function, |
| 236 |
uptr VtableIsValid) |
| 237 |
|
| 238 |
struct ReportOptions; |
| 239 |
|
| 240 |
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type( |
| 241 |
CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable, |
| 242 |
ReportOptions Opts); |
| 243 |
|
| 244 |
struct FunctionTypeMismatchData { |
| 245 |
SourceLocation Loc; |
| 246 |
const TypeDescriptor &Type; |
| 247 |
}; |
| 248 |
|
| 249 |
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void |
| 250 |
__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data, |
| 251 |
ValueHandle Val); |
| 252 |
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void |
| 253 |
__ubsan_handle_function_type_mismatch_abort(FunctionTypeMismatchData *Data, |
| 254 |
ValueHandle Val); |
| 255 |
} |
| 256 |
|
| 257 |
#endif // UBSAN_HANDLERS_H |