ubsan_handlers.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Entry points to the runtime library for Clang's undefined behavior sanitizer.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #ifndef UBSAN_HANDLERS_H
  12. #define UBSAN_HANDLERS_H
  13. #include "ubsan_value.h"
  14. namespace __ubsan {
  15. struct TypeMismatchData {
  16. SourceLocation Loc;
  17. const TypeDescriptor &Type;
  18. uptr Alignment;
  19. unsigned char TypeCheckKind;
  20. };
  21. #define UNRECOVERABLE(checkname, ...) \
  22. extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
  23. void __ubsan_handle_ ## checkname( __VA_ARGS__ );
  24. #define RECOVERABLE(checkname, ...) \
  25. extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
  26. void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
  27. extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
  28. void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
  29. /// \brief Handle a runtime type check failure, caused by either a misaligned
  30. /// pointer, a null pointer, or a pointer to insufficient storage for the
  31. /// type.
  32. RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer)
  33. struct OverflowData {
  34. SourceLocation Loc;
  35. const TypeDescriptor &Type;
  36. };
  37. /// \brief Handle an integer addition overflow.
  38. RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
  39. /// \brief Handle an integer subtraction overflow.
  40. RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
  41. /// \brief Handle an integer multiplication overflow.
  42. RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
  43. /// \brief Handle a signed integer overflow for a unary negate operator.
  44. RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
  45. /// \brief Handle an INT_MIN/-1 overflow or division by zero.
  46. RECOVERABLE(divrem_overflow, OverflowData *Data,
  47. ValueHandle LHS, ValueHandle RHS)
  48. struct ShiftOutOfBoundsData {
  49. SourceLocation Loc;
  50. const TypeDescriptor &LHSType;
  51. const TypeDescriptor &RHSType;
  52. };
  53. /// \brief Handle a shift where the RHS is out of bounds or a left shift where
  54. /// the LHS is negative or overflows.
  55. RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
  56. ValueHandle LHS, ValueHandle RHS)
  57. struct OutOfBoundsData {
  58. SourceLocation Loc;
  59. const TypeDescriptor &ArrayType;
  60. const TypeDescriptor &IndexType;
  61. };
  62. /// \brief Handle an array index out of bounds error.
  63. RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
  64. struct UnreachableData {
  65. SourceLocation Loc;
  66. };
  67. /// \brief Handle a __builtin_unreachable which is reached.
  68. UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
  69. /// \brief Handle reaching the end of a value-returning function.
  70. UNRECOVERABLE(missing_return, UnreachableData *Data)
  71. struct VLABoundData {
  72. SourceLocation Loc;
  73. const TypeDescriptor &Type;
  74. };
  75. /// \brief Handle a VLA with a non-positive bound.
  76. RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
  77. struct FloatCastOverflowData {
  78. // FIXME: SourceLocation Loc;
  79. const TypeDescriptor &FromType;
  80. const TypeDescriptor &ToType;
  81. };
  82. /// \brief Handle overflow in a conversion to or from a floating-point type.
  83. RECOVERABLE(float_cast_overflow, FloatCastOverflowData *Data, ValueHandle From)
  84. struct InvalidValueData {
  85. SourceLocation Loc;
  86. const TypeDescriptor &Type;
  87. };
  88. /// \brief Handle a load of an invalid value for the type.
  89. RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
  90. struct FunctionTypeMismatchData {
  91. SourceLocation Loc;
  92. const TypeDescriptor &Type;
  93. };
  94. RECOVERABLE(function_type_mismatch,
  95. FunctionTypeMismatchData *Data,
  96. ValueHandle Val)
  97. struct NonNullReturnData {
  98. SourceLocation Loc;
  99. SourceLocation AttrLoc;
  100. };
  101. /// \brief Handle returning null from function with returns_nonnull attribute.
  102. RECOVERABLE(nonnull_return, NonNullReturnData *Data)
  103. struct NonNullArgData {
  104. SourceLocation Loc;
  105. SourceLocation AttrLoc;
  106. int ArgIndex;
  107. };
  108. /// \brief Handle passing null pointer to function with nonnull attribute.
  109. RECOVERABLE(nonnull_arg, NonNullArgData *Data)
  110. }
  111. #endif // UBSAN_HANDLERS_H