Home | History | Annotate | Line # | Download | only in ubsan
ubsan_handlers.h revision 1.1.1.5
      1      1.1  mrg //===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
      2      1.1  mrg //
      3      1.1  mrg // This file is distributed under the University of Illinois Open Source
      4      1.1  mrg // License. See LICENSE.TXT for details.
      5      1.1  mrg //
      6      1.1  mrg //===----------------------------------------------------------------------===//
      7      1.1  mrg //
      8      1.1  mrg // Entry points to the runtime library for Clang's undefined behavior sanitizer.
      9      1.1  mrg //
     10      1.1  mrg //===----------------------------------------------------------------------===//
     11      1.1  mrg #ifndef UBSAN_HANDLERS_H
     12      1.1  mrg #define UBSAN_HANDLERS_H
     13      1.1  mrg 
     14      1.1  mrg #include "ubsan_value.h"
     15      1.1  mrg 
     16      1.1  mrg namespace __ubsan {
     17      1.1  mrg 
     18      1.1  mrg struct TypeMismatchData {
     19      1.1  mrg   SourceLocation Loc;
     20      1.1  mrg   const TypeDescriptor &Type;
     21  1.1.1.5  mrg   uptr Alignment;
     22      1.1  mrg   unsigned char TypeCheckKind;
     23      1.1  mrg };
     24      1.1  mrg 
     25      1.1  mrg #define UNRECOVERABLE(checkname, ...) \
     26      1.1  mrg   extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
     27      1.1  mrg     void __ubsan_handle_ ## checkname( __VA_ARGS__ );
     28      1.1  mrg 
     29      1.1  mrg #define RECOVERABLE(checkname, ...) \
     30      1.1  mrg   extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
     31      1.1  mrg     void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
     32      1.1  mrg   extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
     33      1.1  mrg     void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
     34      1.1  mrg 
     35      1.1  mrg /// \brief Handle a runtime type check failure, caused by either a misaligned
     36      1.1  mrg /// pointer, a null pointer, or a pointer to insufficient storage for the
     37      1.1  mrg /// type.
     38  1.1.1.5  mrg RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer)
     39      1.1  mrg 
     40      1.1  mrg struct OverflowData {
     41      1.1  mrg   SourceLocation Loc;
     42      1.1  mrg   const TypeDescriptor &Type;
     43      1.1  mrg };
     44      1.1  mrg 
     45      1.1  mrg /// \brief Handle an integer addition overflow.
     46      1.1  mrg RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
     47      1.1  mrg 
     48      1.1  mrg /// \brief Handle an integer subtraction overflow.
     49      1.1  mrg RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
     50      1.1  mrg 
     51      1.1  mrg /// \brief Handle an integer multiplication overflow.
     52      1.1  mrg RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
     53      1.1  mrg 
     54      1.1  mrg /// \brief Handle a signed integer overflow for a unary negate operator.
     55      1.1  mrg RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
     56      1.1  mrg 
     57      1.1  mrg /// \brief Handle an INT_MIN/-1 overflow or division by zero.
     58      1.1  mrg RECOVERABLE(divrem_overflow, OverflowData *Data,
     59      1.1  mrg             ValueHandle LHS, ValueHandle RHS)
     60      1.1  mrg 
     61      1.1  mrg struct ShiftOutOfBoundsData {
     62      1.1  mrg   SourceLocation Loc;
     63      1.1  mrg   const TypeDescriptor &LHSType;
     64      1.1  mrg   const TypeDescriptor &RHSType;
     65      1.1  mrg };
     66      1.1  mrg 
     67      1.1  mrg /// \brief Handle a shift where the RHS is out of bounds or a left shift where
     68      1.1  mrg /// the LHS is negative or overflows.
     69      1.1  mrg RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
     70      1.1  mrg             ValueHandle LHS, ValueHandle RHS)
     71      1.1  mrg 
     72      1.1  mrg struct OutOfBoundsData {
     73      1.1  mrg   SourceLocation Loc;
     74      1.1  mrg   const TypeDescriptor &ArrayType;
     75      1.1  mrg   const TypeDescriptor &IndexType;
     76      1.1  mrg };
     77      1.1  mrg 
     78      1.1  mrg /// \brief Handle an array index out of bounds error.
     79      1.1  mrg RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
     80      1.1  mrg 
     81      1.1  mrg struct UnreachableData {
     82      1.1  mrg   SourceLocation Loc;
     83      1.1  mrg };
     84      1.1  mrg 
     85      1.1  mrg /// \brief Handle a __builtin_unreachable which is reached.
     86      1.1  mrg UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
     87      1.1  mrg /// \brief Handle reaching the end of a value-returning function.
     88      1.1  mrg UNRECOVERABLE(missing_return, UnreachableData *Data)
     89      1.1  mrg 
     90      1.1  mrg struct VLABoundData {
     91      1.1  mrg   SourceLocation Loc;
     92      1.1  mrg   const TypeDescriptor &Type;
     93      1.1  mrg };
     94      1.1  mrg 
     95      1.1  mrg /// \brief Handle a VLA with a non-positive bound.
     96      1.1  mrg RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
     97      1.1  mrg 
     98  1.1.1.2  mrg // Keeping this around for binary compatibility with (sanitized) programs
     99  1.1.1.2  mrg // compiled with older compilers.
    100      1.1  mrg struct FloatCastOverflowData {
    101      1.1  mrg   const TypeDescriptor &FromType;
    102      1.1  mrg   const TypeDescriptor &ToType;
    103      1.1  mrg };
    104      1.1  mrg 
    105  1.1.1.2  mrg struct FloatCastOverflowDataV2 {
    106  1.1.1.2  mrg   SourceLocation Loc;
    107  1.1.1.2  mrg   const TypeDescriptor &FromType;
    108  1.1.1.2  mrg   const TypeDescriptor &ToType;
    109  1.1.1.2  mrg };
    110  1.1.1.2  mrg 
    111  1.1.1.2  mrg /// Handle overflow in a conversion to or from a floating-point type.
    112  1.1.1.2  mrg /// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2*
    113  1.1.1.2  mrg RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From)
    114      1.1  mrg 
    115      1.1  mrg struct InvalidValueData {
    116      1.1  mrg   SourceLocation Loc;
    117      1.1  mrg   const TypeDescriptor &Type;
    118      1.1  mrg };
    119      1.1  mrg 
    120      1.1  mrg /// \brief Handle a load of an invalid value for the type.
    121      1.1  mrg RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
    122      1.1  mrg 
    123      1.1  mrg struct FunctionTypeMismatchData {
    124      1.1  mrg   SourceLocation Loc;
    125      1.1  mrg   const TypeDescriptor &Type;
    126      1.1  mrg };
    127      1.1  mrg 
    128      1.1  mrg RECOVERABLE(function_type_mismatch,
    129      1.1  mrg             FunctionTypeMismatchData *Data,
    130      1.1  mrg             ValueHandle Val)
    131      1.1  mrg 
    132      1.1  mrg struct NonNullReturnData {
    133  1.1.1.5  mrg   SourceLocation Loc;
    134      1.1  mrg   SourceLocation AttrLoc;
    135      1.1  mrg };
    136      1.1  mrg 
    137  1.1.1.5  mrg /// \brief Handle returning null from function with returns_nonnull attribute.
    138  1.1.1.5  mrg RECOVERABLE(nonnull_return, NonNullReturnData *Data)
    139      1.1  mrg 
    140      1.1  mrg struct NonNullArgData {
    141      1.1  mrg   SourceLocation Loc;
    142      1.1  mrg   SourceLocation AttrLoc;
    143      1.1  mrg   int ArgIndex;
    144      1.1  mrg };
    145      1.1  mrg 
    146  1.1.1.5  mrg /// \brief Handle passing null pointer to function with nonnull attribute.
    147      1.1  mrg RECOVERABLE(nonnull_arg, NonNullArgData *Data)
    148      1.1  mrg 
    149  1.1.1.3  mrg /// \brief Known CFI check kinds.
    150  1.1.1.3  mrg /// Keep in sync with the enum of the same name in CodeGenFunction.h
    151  1.1.1.3  mrg enum CFITypeCheckKind : unsigned char {
    152  1.1.1.3  mrg   CFITCK_VCall,
    153  1.1.1.3  mrg   CFITCK_NVCall,
    154  1.1.1.3  mrg   CFITCK_DerivedCast,
    155  1.1.1.3  mrg   CFITCK_UnrelatedCast,
    156  1.1.1.3  mrg   CFITCK_ICall,
    157  1.1.1.3  mrg };
    158  1.1.1.3  mrg 
    159  1.1.1.5  mrg struct CFIBadIcallData {
    160  1.1.1.5  mrg   SourceLocation Loc;
    161  1.1.1.5  mrg   const TypeDescriptor &Type;
    162  1.1.1.5  mrg };
    163  1.1.1.5  mrg 
    164  1.1.1.3  mrg struct CFICheckFailData {
    165  1.1.1.3  mrg   CFITypeCheckKind CheckKind;
    166  1.1.1.3  mrg   SourceLocation Loc;
    167  1.1.1.3  mrg   const TypeDescriptor &Type;
    168  1.1.1.3  mrg };
    169  1.1.1.3  mrg 
    170  1.1.1.5  mrg /// \brief Handle control flow integrity failure for indirect function calls.
    171  1.1.1.5  mrg RECOVERABLE(cfi_bad_icall, CFIBadIcallData *Data, ValueHandle Function)
    172  1.1.1.5  mrg 
    173  1.1.1.3  mrg /// \brief Handle control flow integrity failures.
    174  1.1.1.3  mrg RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,
    175  1.1.1.3  mrg             uptr VtableIsValid)
    176      1.1  mrg }
    177      1.1  mrg 
    178      1.1  mrg #endif // UBSAN_HANDLERS_H
    179