ubsan_handlers.h 7.1 KB

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