Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 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 | uptr Alignment; |
| 24 | unsigned char TypeCheckKind; |
| 25 | }; |
| 26 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 27 | #define UNRECOVERABLE(checkname, ...) \ |
| 28 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \ |
| 29 | void __ubsan_handle_ ## checkname( __VA_ARGS__ ); |
| 30 | |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 31 | #define RECOVERABLE(checkname, ...) \ |
Will Dietz | a441109 | 2013-01-10 17:01:13 +0000 | [diff] [blame] | 32 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE \ |
| 33 | void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 34 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \ |
Will Dietz | a441109 | 2013-01-10 17:01:13 +0000 | [diff] [blame] | 35 | void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ ); |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 36 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 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. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 40 | RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer) |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 41 | |
| 42 | struct OverflowData { |
| 43 | SourceLocation Loc; |
| 44 | const TypeDescriptor &Type; |
| 45 | }; |
| 46 | |
Will Dietz | 80af605 | 2012-11-27 15:01:43 +0000 | [diff] [blame] | 47 | /// \brief Handle an integer addition overflow. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 48 | RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) |
| 49 | |
Will Dietz | 80af605 | 2012-11-27 15:01:43 +0000 | [diff] [blame] | 50 | /// \brief Handle an integer subtraction overflow. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 51 | RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) |
| 52 | |
Will Dietz | 80af605 | 2012-11-27 15:01:43 +0000 | [diff] [blame] | 53 | /// \brief Handle an integer multiplication overflow. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 54 | RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS) |
| 55 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 56 | /// \brief Handle a signed integer overflow for a unary negate operator. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 57 | RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal) |
| 58 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 59 | /// \brief Handle an INT_MIN/-1 overflow or division by zero. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 60 | RECOVERABLE(divrem_overflow, OverflowData *Data, |
| 61 | ValueHandle LHS, ValueHandle RHS) |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 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. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 71 | RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data, |
| 72 | ValueHandle LHS, ValueHandle RHS) |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 73 | |
Richard Smith | a0b1e21 | 2013-02-23 02:40:07 +0000 | [diff] [blame] | 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 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 83 | struct UnreachableData { |
| 84 | SourceLocation Loc; |
| 85 | }; |
| 86 | |
| 87 | /// \brief Handle a __builtin_unreachable which is reached. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 88 | UNRECOVERABLE(builtin_unreachable, UnreachableData *Data) |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 89 | /// \brief Handle reaching the end of a value-returning function. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 90 | UNRECOVERABLE(missing_return, UnreachableData *Data) |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 91 | |
Richard Smith | b04caf1 | 2012-10-10 01:10:59 +0000 | [diff] [blame] | 92 | struct VLABoundData { |
| 93 | SourceLocation Loc; |
| 94 | const TypeDescriptor &Type; |
| 95 | }; |
| 96 | |
| 97 | /// \brief Handle a VLA with a non-positive bound. |
Will Dietz | a82a5d3 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 98 | RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound) |
Richard Smith | b04caf1 | 2012-10-10 01:10:59 +0000 | [diff] [blame] | 99 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 100 | // Keeping this around for binary compatibility with (sanitized) programs |
| 101 | // compiled with older compilers. |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 102 | struct FloatCastOverflowData { |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 103 | const TypeDescriptor &FromType; |
| 104 | const TypeDescriptor &ToType; |
| 105 | }; |
| 106 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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) |
Richard Smith | 5856170 | 2012-10-12 22:57:15 +0000 | [diff] [blame] | 116 | |
Richard Smith | f2d77d0 | 2012-12-13 07:00:14 +0000 | [diff] [blame] | 117 | struct InvalidValueData { |
Nick Lewycky | d1bf52e | 2013-10-02 02:29:47 +0000 | [diff] [blame] | 118 | SourceLocation Loc; |
Richard Smith | f2d77d0 | 2012-12-13 07:00:14 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 9b5f95f | 2013-10-20 21:29:46 +0000 | [diff] [blame] | 125 | struct FunctionTypeMismatchData { |
| 126 | SourceLocation Loc; |
| 127 | const TypeDescriptor &Type; |
| 128 | }; |
| 129 | |
| 130 | RECOVERABLE(function_type_mismatch, |
| 131 | FunctionTypeMismatchData *Data, |
| 132 | ValueHandle Val) |
| 133 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 134 | struct NonNullReturnData { |
| 135 | SourceLocation Loc; |
| 136 | SourceLocation AttrLoc; |
| 137 | }; |
| 138 | |
| 139 | /// \brief Handle returning null from function with returns_nonnull attribute. |
| 140 | RECOVERABLE(nonnull_return, NonNullReturnData *Data) |
| 141 | |
| 142 | struct NonNullArgData { |
| 143 | SourceLocation Loc; |
| 144 | SourceLocation AttrLoc; |
| 145 | int ArgIndex; |
| 146 | }; |
| 147 | |
| 148 | /// \brief Handle passing null pointer to function with nonnull attribute. |
| 149 | RECOVERABLE(nonnull_arg, NonNullArgData *Data) |
| 150 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 151 | struct CFIBadIcallData { |
| 152 | SourceLocation Loc; |
| 153 | const TypeDescriptor &Type; |
| 154 | }; |
| 155 | |
| 156 | /// \brief Handle control flow integrity failure for indirect function calls. |
| 157 | RECOVERABLE(cfi_bad_icall, CFIBadIcallData *Data, ValueHandle Function) |
| 158 | |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | #endif // UBSAN_HANDLERS_H |