Home | History | Annotate | Download | only in ubsan
      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 
     27 #define RECOVERABLE(checkname, ...) \
     28   extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
     29     void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
     30   extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
     31     void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
     32 
     33 /// \brief Handle a runtime type check failure, caused by either a misaligned
     34 /// pointer, a null pointer, or a pointer to insufficient storage for the
     35 /// type.
     36 RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer)
     37 
     38 struct OverflowData {
     39   SourceLocation Loc;
     40   const TypeDescriptor &Type;
     41 };
     42 
     43 /// \brief Handle an integer addition overflow.
     44 RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
     45 
     46 /// \brief Handle an integer subtraction overflow.
     47 RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
     48 
     49 /// \brief Handle an integer multiplication overflow.
     50 RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
     51 
     52 /// \brief Handle a signed integer overflow for a unary negate operator.
     53 RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
     54 
     55 /// \brief Handle an INT_MIN/-1 overflow or division by zero.
     56 RECOVERABLE(divrem_overflow, OverflowData *Data,
     57             ValueHandle LHS, ValueHandle RHS)
     58 
     59 struct ShiftOutOfBoundsData {
     60   SourceLocation Loc;
     61   const TypeDescriptor &LHSType;
     62   const TypeDescriptor &RHSType;
     63 };
     64 
     65 /// \brief Handle a shift where the RHS is out of bounds or a left shift where
     66 /// the LHS is negative or overflows.
     67 RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
     68             ValueHandle LHS, ValueHandle RHS)
     69 
     70 struct OutOfBoundsData {
     71   SourceLocation Loc;
     72   const TypeDescriptor &ArrayType;
     73   const TypeDescriptor &IndexType;
     74 };
     75 
     76 /// \brief Handle an array index out of bounds error.
     77 RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
     78 
     79 struct UnreachableData {
     80   SourceLocation Loc;
     81 };
     82 
     83 /// \brief Handle a __builtin_unreachable which is reached.
     84 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
     85 void __ubsan_handle_builtin_unreachable(UnreachableData *Data);
     86 /// \brief Handle reaching the end of a value-returning function.
     87 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
     88 void __ubsan_handle_missing_return(UnreachableData *Data);
     89 
     90 struct VLABoundData {
     91   SourceLocation Loc;
     92   const TypeDescriptor &Type;
     93 };
     94 
     95 /// \brief Handle a VLA with a non-positive bound.
     96 RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
     97 
     98 struct FloatCastOverflowData {
     99   // FIXME: SourceLocation Loc;
    100   const TypeDescriptor &FromType;
    101   const TypeDescriptor &ToType;
    102 };
    103 
    104 /// \brief Handle overflow in a conversion to or from a floating-point type.
    105 RECOVERABLE(float_cast_overflow, FloatCastOverflowData *Data, ValueHandle From)
    106 
    107 struct InvalidValueData {
    108   SourceLocation Loc;
    109   const TypeDescriptor &Type;
    110 };
    111 
    112 /// \brief Handle a load of an invalid value for the type.
    113 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
    114 
    115 struct FunctionTypeMismatchData {
    116   SourceLocation Loc;
    117   const TypeDescriptor &Type;
    118 };
    119 
    120 RECOVERABLE(function_type_mismatch,
    121             FunctionTypeMismatchData *Data,
    122             ValueHandle Val)
    123 
    124 }
    125 
    126 #endif // UBSAN_HANDLERS_H
    127