]> git.saurik.com Git - apple/xnu.git/blame - san/ubsan.h
xnu-4903.231.4.tar.gz
[apple/xnu.git] / san / ubsan.h
CommitLineData
d9a64523
A
1/*
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _UBSAN_H_
30#define _UBSAN_H_
31
32#include <stdint.h>
33#include <stdbool.h>
34
35struct san_type_desc {
36 uint16_t type; // 0: integer, 1: float
37 union {
38 struct {
39 uint16_t issigned : 1;
40 uint16_t width : 15;
41 }; /* int descriptor */
42 struct {
43 uint16_t float_desc;
44 }; /* float descriptor */
45 };
46 const char name[];
47};
48
49struct san_src_loc {
50 const char *filename;
51 uint32_t line;
52 uint32_t col;
53};
54
55struct ubsan_overflow_desc {
56 struct san_src_loc loc;
57 struct san_type_desc *ty;
58};
59
60struct ubsan_unreachable_desc {
61 struct san_src_loc loc;
62};
63
64struct ubsan_shift_desc {
65 struct san_src_loc loc;
66 struct san_type_desc *lhs_t;
67 struct san_type_desc *rhs_t;
68};
69
70struct ubsan_align_desc {
71 struct san_src_loc loc;
72 struct san_type_desc *ty;
73 uint8_t align;
74 uint8_t kind;
75};
76
77struct ubsan_ptroverflow_desc {
78 struct san_src_loc loc;
79};
80
81struct ubsan_oob_desc {
82 struct san_src_loc loc;
83 struct san_type_desc *array_ty;
84 struct san_type_desc *index_ty;
85};
86
87enum {
88 UBSAN_OVERFLOW_add = 1,
89 UBSAN_OVERFLOW_sub,
90 UBSAN_OVERFLOW_mul,
91 UBSAN_OVERFLOW_divrem,
92 UBSAN_OVERFLOW_negate,
93 UBSAN_UNREACHABLE,
94 UBSAN_SHIFT,
95 UBSAN_ALIGN,
96 UBSAN_POINTER_OVERFLOW,
97 UBSAN_OOB,
98 UBSAN_VIOLATION_MAX,
99};
100
101struct ubsan_violation {
102 uint8_t ubsan_type;
103 uint64_t lhs;
104 uint64_t rhs;
105 union {
106 struct ubsan_overflow_desc *overflow;
107 struct ubsan_unreachable_desc *unreachable;
108 struct ubsan_shift_desc *shift;
109 struct ubsan_align_desc *align;
110 struct ubsan_ptroverflow_desc *ptroverflow;
111 struct ubsan_oob_desc *oob;
112 };
113 struct san_src_loc *loc;
114};
115
116void ubsan_log_append(struct ubsan_violation *);
117size_t ubsan_format(struct ubsan_violation *, char *buf, size_t sz);
118
119/*
120 * UBSan ABI
121 */
122
123void __ubsan_handle_add_overflow(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
124void __ubsan_handle_sub_overflow(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
125void __ubsan_handle_mul_overflow(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
126void __ubsan_handle_divrem_overflow(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
127void __ubsan_handle_negate_overflow(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
128void __ubsan_handle_add_overflow_abort(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
129void __ubsan_handle_sub_overflow_abort(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
130void __ubsan_handle_mul_overflow_abort(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
131void __ubsan_handle_divrem_overflow_abort(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
132void __ubsan_handle_negate_overflow_abort(struct ubsan_overflow_desc *, uint64_t lhs, uint64_t rhs);
133void __ubsan_handle_builtin_unreachable(struct ubsan_unreachable_desc *);
134void __ubsan_handle_shift_out_of_bounds(struct ubsan_shift_desc *, uint64_t lhs, uint64_t rhs);
135void __ubsan_handle_shift_out_of_bounds_abort(struct ubsan_shift_desc *, uint64_t lhs, uint64_t rhs);
136void __ubsan_handle_type_mismatch_v1(struct ubsan_align_desc *, uint64_t val);
137void __ubsan_handle_type_mismatch_v1_abort(struct ubsan_align_desc *, uint64_t val);
138void __ubsan_handle_pointer_overflow(struct ubsan_ptroverflow_desc *, uint64_t lhs, uint64_t rhs);
139void __ubsan_handle_pointer_overflow_abort(struct ubsan_ptroverflow_desc *, uint64_t lhs, uint64_t rhs);
140void __ubsan_handle_out_of_bounds(struct ubsan_oob_desc *, uint64_t idx);
141void __ubsan_handle_out_of_bounds_abort(struct ubsan_oob_desc *, uint64_t idx);
142
143#endif /* _UBSAN_H_ */