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
18namespace __ubsan {
19
20struct 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.
36RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer)
37
38struct OverflowData {
39  SourceLocation Loc;
40  const TypeDescriptor &Type;
41};
42
43/// \brief Handle an integer addition overflow.
44RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
45
46/// \brief Handle an integer subtraction overflow.
47RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
48
49/// \brief Handle an integer multiplication overflow.
50RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
51
52/// \brief Handle a signed integer overflow for a unary negate operator.
53RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
54
55/// \brief Handle an INT_MIN/-1 overflow or division by zero.
56RECOVERABLE(divrem_overflow, OverflowData *Data,
57            ValueHandle LHS, ValueHandle RHS)
58
59struct 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.
67RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
68            ValueHandle LHS, ValueHandle RHS)
69
70struct OutOfBoundsData {
71  SourceLocation Loc;
72  const TypeDescriptor &ArrayType;
73  const TypeDescriptor &IndexType;
74};
75
76/// \brief Handle an array index out of bounds error.
77RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
78
79struct UnreachableData {
80  SourceLocation Loc;
81};
82
83/// \brief Handle a __builtin_unreachable which is reached.
84extern "C" SANITIZER_INTERFACE_ATTRIBUTE
85void __ubsan_handle_builtin_unreachable(UnreachableData *Data);
86/// \brief Handle reaching the end of a value-returning function.
87extern "C" SANITIZER_INTERFACE_ATTRIBUTE
88void __ubsan_handle_missing_return(UnreachableData *Data);
89
90struct VLABoundData {
91  SourceLocation Loc;
92  const TypeDescriptor &Type;
93};
94
95/// \brief Handle a VLA with a non-positive bound.
96RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
97
98struct 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.
105RECOVERABLE(float_cast_overflow, FloatCastOverflowData *Data, ValueHandle From)
106
107struct InvalidValueData {
108  SourceLocation Loc;
109  const TypeDescriptor &Type;
110};
111
112/// \brief Handle a load of an invalid value for the type.
113RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
114
115struct FunctionTypeMismatchData {
116  SourceLocation Loc;
117  const TypeDescriptor &Type;
118};
119
120RECOVERABLE(function_type_mismatch,
121            FunctionTypeMismatchData *Data,
122            ValueHandle Val)
123
124}
125
126#endif // UBSAN_HANDLERS_H
127