blob: 87149f2596074a4ed00b199928d74fc782b9b6c8 [file] [log] [blame]
Richard Smith6ebe4512012-10-09 19:34:32 +00001//===-- 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
Stephen Hines6d186232014-11-26 17:56:19 -080027#define UNRECOVERABLE(checkname, ...) \
28 extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
29 void __ubsan_handle_ ## checkname( __VA_ARGS__ );
30
Will Dietza82a5d32012-12-02 19:47:29 +000031#define RECOVERABLE(checkname, ...) \
Will Dietza4411092013-01-10 17:01:13 +000032 extern "C" SANITIZER_INTERFACE_ATTRIBUTE \
33 void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \
Stephen Hines6d186232014-11-26 17:56:19 -080034 extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \
Will Dietza4411092013-01-10 17:01:13 +000035 void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );
Will Dietza82a5d32012-12-02 19:47:29 +000036
Richard Smith6ebe4512012-10-09 19:34:32 +000037/// \brief Handle a runtime type check failure, caused by either a misaligned
38/// pointer, a null pointer, or a pointer to insufficient storage for the
39/// type.
Will Dietza82a5d32012-12-02 19:47:29 +000040RECOVERABLE(type_mismatch, TypeMismatchData *Data, ValueHandle Pointer)
Richard Smith6ebe4512012-10-09 19:34:32 +000041
42struct OverflowData {
43 SourceLocation Loc;
44 const TypeDescriptor &Type;
45};
46
Will Dietz80af6052012-11-27 15:01:43 +000047/// \brief Handle an integer addition overflow.
Will Dietza82a5d32012-12-02 19:47:29 +000048RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
49
Will Dietz80af6052012-11-27 15:01:43 +000050/// \brief Handle an integer subtraction overflow.
Will Dietza82a5d32012-12-02 19:47:29 +000051RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
52
Will Dietz80af6052012-11-27 15:01:43 +000053/// \brief Handle an integer multiplication overflow.
Will Dietza82a5d32012-12-02 19:47:29 +000054RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)
55
Richard Smith6ebe4512012-10-09 19:34:32 +000056/// \brief Handle a signed integer overflow for a unary negate operator.
Will Dietza82a5d32012-12-02 19:47:29 +000057RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)
58
Richard Smith6ebe4512012-10-09 19:34:32 +000059/// \brief Handle an INT_MIN/-1 overflow or division by zero.
Will Dietza82a5d32012-12-02 19:47:29 +000060RECOVERABLE(divrem_overflow, OverflowData *Data,
61 ValueHandle LHS, ValueHandle RHS)
Richard Smith6ebe4512012-10-09 19:34:32 +000062
63struct ShiftOutOfBoundsData {
64 SourceLocation Loc;
65 const TypeDescriptor &LHSType;
66 const TypeDescriptor &RHSType;
67};
68
69/// \brief Handle a shift where the RHS is out of bounds or a left shift where
70/// the LHS is negative or overflows.
Will Dietza82a5d32012-12-02 19:47:29 +000071RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
72 ValueHandle LHS, ValueHandle RHS)
Richard Smith6ebe4512012-10-09 19:34:32 +000073
Richard Smitha0b1e212013-02-23 02:40:07 +000074struct OutOfBoundsData {
75 SourceLocation Loc;
76 const TypeDescriptor &ArrayType;
77 const TypeDescriptor &IndexType;
78};
79
80/// \brief Handle an array index out of bounds error.
81RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
82
Richard Smith6ebe4512012-10-09 19:34:32 +000083struct UnreachableData {
84 SourceLocation Loc;
85};
86
87/// \brief Handle a __builtin_unreachable which is reached.
Stephen Hines6d186232014-11-26 17:56:19 -080088UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)
Richard Smith6ebe4512012-10-09 19:34:32 +000089/// \brief Handle reaching the end of a value-returning function.
Stephen Hines6d186232014-11-26 17:56:19 -080090UNRECOVERABLE(missing_return, UnreachableData *Data)
Richard Smith6ebe4512012-10-09 19:34:32 +000091
Richard Smithb04caf12012-10-10 01:10:59 +000092struct VLABoundData {
93 SourceLocation Loc;
94 const TypeDescriptor &Type;
95};
96
97/// \brief Handle a VLA with a non-positive bound.
Will Dietza82a5d32012-12-02 19:47:29 +000098RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)
Richard Smithb04caf12012-10-10 01:10:59 +000099
Richard Smith58561702012-10-12 22:57:15 +0000100struct FloatCastOverflowData {
101 // FIXME: SourceLocation Loc;
102 const TypeDescriptor &FromType;
103 const TypeDescriptor &ToType;
104};
105
106/// \brief Handle overflow in a conversion to or from a floating-point type.
Will Dietza82a5d32012-12-02 19:47:29 +0000107RECOVERABLE(float_cast_overflow, FloatCastOverflowData *Data, ValueHandle From)
Richard Smith58561702012-10-12 22:57:15 +0000108
Richard Smithf2d77d02012-12-13 07:00:14 +0000109struct InvalidValueData {
Nick Lewyckyd1bf52e2013-10-02 02:29:47 +0000110 SourceLocation Loc;
Richard Smithf2d77d02012-12-13 07:00:14 +0000111 const TypeDescriptor &Type;
112};
113
114/// \brief Handle a load of an invalid value for the type.
115RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
116
Peter Collingbourne9b5f95f2013-10-20 21:29:46 +0000117struct FunctionTypeMismatchData {
118 SourceLocation Loc;
119 const TypeDescriptor &Type;
120};
121
122RECOVERABLE(function_type_mismatch,
123 FunctionTypeMismatchData *Data,
124 ValueHandle Val)
125
Stephen Hines6d186232014-11-26 17:56:19 -0800126struct NonNullReturnData {
127 SourceLocation Loc;
128 SourceLocation AttrLoc;
129};
130
131/// \brief Handle returning null from function with returns_nonnull attribute.
132RECOVERABLE(nonnull_return, NonNullReturnData *Data)
133
134struct NonNullArgData {
135 SourceLocation Loc;
136 SourceLocation AttrLoc;
137 int ArgIndex;
138};
139
140/// \brief Handle passing null pointer to function with nonnull attribute.
141RECOVERABLE(nonnull_arg, NonNullArgData *Data)
142
Richard Smith6ebe4512012-10-09 19:34:32 +0000143}
144
145#endif // UBSAN_HANDLERS_H