ubsan_handlers.h revision a44110995241d30a886d711cbc33f65269b170ea
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 UnreachableData {
71  SourceLocation Loc;
72};
73
74/// \brief Handle a __builtin_unreachable which is reached.
75extern "C" SANITIZER_INTERFACE_ATTRIBUTE
76void __ubsan_handle_builtin_unreachable(UnreachableData *Data);
77/// \brief Handle reaching the end of a value-returning function.
78extern "C" SANITIZER_INTERFACE_ATTRIBUTE
79void __ubsan_handle_missing_return(UnreachableData *Data);
80
81struct VLABoundData {
82  SourceLocation Loc;
83  const TypeDescriptor &Type;
84};
85
86/// \brief Handle a VLA with a non-positive bound.
87RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
88
89struct FloatCastOverflowData {
90  // FIXME: SourceLocation Loc;
91  const TypeDescriptor &FromType;
92  const TypeDescriptor &ToType;
93};
94
95/// \brief Handle overflow in a conversion to or from a floating-point type.
96RECOVERABLE(float_cast_overflow, FloatCastOverflowData *Data, ValueHandle From)
97
98struct InvalidValueData {
99  // FIXME: SourceLocation Loc;
100  const TypeDescriptor &Type;
101};
102
103/// \brief Handle a load of an invalid value for the type.
104RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
105
106}
107
108#endif // UBSAN_HANDLERS_H
109