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