]>
git.saurik.com Git - apple/xnu.git/blob - san/ubsan.c
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <stdatomic.h>
30 #include <kern/debug.h>
31 #include <libkern/libkern.h>
34 static const bool ubsan_print
= false;
35 static const uint32_t line_acquired
= 0x80000000UL
;
36 static const char *get_type_check_kind(uint8_t kind
);
38 format_loc(struct san_src_loc
*loc
, char *dst
, size_t sz
)
40 return scnprintf(dst
, sz
, " loc: %s:%d:%d\n",
42 loc
->line
& ~line_acquired
,
48 * return true for the first visit to this loc, false every subsequent time
51 ubsan_loc_acquire(struct san_src_loc
*loc
)
53 uint32_t line
= loc
->line
;
54 if (line
& line_acquired
) {
57 uint32_t acq
= line
| line_acquired
;
58 return atomic_compare_exchange_strong((_Atomic
uint32_t *)&loc
->line
, &line
, acq
);
61 static const char *const
73 format_overflow(struct ubsan_violation
*v
, char *buf
, size_t sz
)
75 struct san_type_desc
*ty
= v
->overflow
->ty
;
76 return scnprintf(buf
, sz
,
77 "%s overflow, op = %s, ty = %s, width = %d, lhs = 0x%llx, rhs = 0x%llx\n",
78 ty
->issigned
? "signed" : "unsigned",
79 overflow_str
[v
->ubsan_type
],
88 format_shift(struct ubsan_violation
*v
, char *buf
, size_t sz
)
91 struct san_type_desc
*l
= v
->shift
->lhs_t
;
92 struct san_type_desc
*r
= v
->shift
->rhs_t
;
94 n
+= scnprintf(buf
+ n
, sz
- n
, "bad shift\n");
95 n
+= scnprintf(buf
+ n
, sz
- n
, " lhs: 0x%llx, ty = %s, signed = %d, width = %d\n", v
->lhs
, l
->name
, l
->issigned
, 1 << l
->width
);
96 n
+= scnprintf(buf
+ n
, sz
- n
, " rhs: 0x%llx, ty = %s, signed = %d, width = %d\n", v
->rhs
, r
->name
, r
->issigned
, 1 << r
->width
);
101 static const char * const
102 type_check_kinds
[] = {
103 "load of", "store to", "reference binding to", "member access within",
104 "member call on", "constructor call on", "downcast of", "downcast of",
105 "upcast of", "cast to virtual base of", "_Nonnull binding to"
109 get_type_check_kind(uint8_t kind
)
111 return (kind
< (sizeof(type_check_kinds
) / sizeof(type_check_kinds
[0])))
112 ? type_check_kinds
[kind
]
117 format_type_mismatch(struct ubsan_violation
*v
, char *buf
, size_t sz
)
120 size_t alignment
= 1 << v
->align
->align
;
121 void *ptr
= (void*)v
->lhs
;
122 const char * kind
= get_type_check_kind(v
->align
->kind
);
125 n
+= scnprintf(buf
+ n
, sz
- n
, "%s NULL pointer of type %s\n", kind
, v
->align
->ty
->name
);
126 } else if (alignment
&& ((uintptr_t)ptr
& (alignment
- 1))) {
127 //misaligned pointer use
128 n
+= scnprintf(buf
+ n
, sz
- n
, "%s mis-aligned address %p for type %s ", kind
, (void*)v
->lhs
, v
->align
->ty
->name
);
129 n
+= scnprintf(buf
+ n
, sz
- n
, "which requires %d byte alignment\n", 1 << v
->align
->align
);
131 //insufficient object size
132 n
+= scnprintf(buf
+ n
, sz
- n
, "%s address %p with insufficient space for an object of type %s\n",
133 kind
, ptr
, v
->align
->ty
->name
);
140 format_oob(struct ubsan_violation
*v
, char *buf
, size_t sz
)
143 struct san_type_desc
*aty
= v
->oob
->array_ty
;
144 struct san_type_desc
*ity
= v
->oob
->index_ty
;
145 uintptr_t idx
= v
->lhs
;
147 n
+= scnprintf(buf
+ n
, sz
- n
, "OOB array access\n");
148 n
+= scnprintf(buf
+ n
, sz
- n
, " idx %ld\n", idx
);
149 n
+= scnprintf(buf
+ n
, sz
- n
, " aty: ty = %s, signed = %d, width = %d\n", aty
->name
, aty
->issigned
, 1 << aty
->width
);
150 n
+= scnprintf(buf
+ n
, sz
- n
, " ity: ty = %s, signed = %d, width = %d\n", ity
->name
, ity
->issigned
, 1 << ity
->width
);
156 ubsan_format(struct ubsan_violation
*v
, char *buf
, size_t sz
)
160 switch (v
->ubsan_type
) {
161 case UBSAN_OVERFLOW_add
... UBSAN_OVERFLOW_negate
:
162 n
+= format_overflow(v
, buf
+ n
, sz
- n
);
164 case UBSAN_UNREACHABLE
:
165 n
+= scnprintf(buf
+ n
, sz
- n
, "unreachable\n");
168 n
+= format_shift(v
, buf
+ n
, sz
- n
);
170 case UBSAN_TYPE_MISMATCH
:
171 n
+= format_type_mismatch(v
, buf
+ n
, sz
- n
);
173 case UBSAN_POINTER_OVERFLOW
:
174 n
+= scnprintf(buf
+ n
, sz
- n
, "pointer overflow, before = 0x%llx, after = 0x%llx\n", v
->lhs
, v
->rhs
);
177 n
+= format_oob(v
, buf
+ n
, sz
- n
);
180 n
+= scnprintf(buf
+ n
, sz
- n
, "%s\n", v
->func
);
183 panic("unknown violation");
186 n
+= format_loc(v
->loc
, buf
+ n
, sz
- n
);
192 ubsan_handle(struct ubsan_violation
*v
, bool fatal
)
194 const size_t sz
= 256;
199 if (!ubsan_loc_acquire(v
->loc
)) {
200 /* violation site already reported */
206 if (ubsan_print
|| fatal
) {
207 n
+= ubsan_format(v
, buf
+ n
, sz
- n
);
211 printf("UBSan: %s", buf
);
215 panic("UBSan: %s", buf
);
220 __ubsan_handle_builtin_unreachable(struct ubsan_unreachable_desc
*desc
)
222 struct ubsan_violation v
= { UBSAN_UNREACHABLE
, 0, 0, .unreachable
= desc
, &desc
->loc
};
223 ubsan_handle(&v
, true);
227 __ubsan_handle_shift_out_of_bounds(struct ubsan_shift_desc
*desc
, uint64_t lhs
, uint64_t rhs
)
229 struct ubsan_violation v
= { UBSAN_SHIFT
, lhs
, rhs
, .shift
= desc
, &desc
->loc
};
230 ubsan_handle(&v
, false);
234 __ubsan_handle_shift_out_of_bounds_abort(struct ubsan_shift_desc
*desc
, uint64_t lhs
, uint64_t rhs
)
236 struct ubsan_violation v
= { UBSAN_SHIFT
, lhs
, rhs
, .shift
= desc
, &desc
->loc
};
237 ubsan_handle(&v
, true);
240 #define DEFINE_OVERFLOW(op) \
241 void __ubsan_handle_##op##_overflow(struct ubsan_overflow_desc *desc, uint64_t lhs, uint64_t rhs) { \
242 struct ubsan_violation v = { UBSAN_OVERFLOW_##op, lhs, rhs, .overflow = desc, &desc->loc }; \
243 ubsan_handle(&v, false); \
245 void __ubsan_handle_##op##_overflow_abort(struct ubsan_overflow_desc *desc, uint64_t lhs, uint64_t rhs) { \
246 struct ubsan_violation v = { UBSAN_OVERFLOW_##op, lhs, rhs, .overflow = desc, &desc->loc }; \
247 ubsan_handle(&v, true); \
253 DEFINE_OVERFLOW(divrem
)
254 DEFINE_OVERFLOW(negate
)
257 __ubsan_handle_type_mismatch_v1(struct ubsan_align_desc
*desc
, uint64_t val
)
259 struct ubsan_violation v
= { UBSAN_TYPE_MISMATCH
, val
, 0, .align
= desc
, &desc
->loc
};
260 ubsan_handle(&v
, false);
264 __ubsan_handle_type_mismatch_v1_abort(struct ubsan_align_desc
*desc
, uint64_t val
)
266 struct ubsan_violation v
= { UBSAN_TYPE_MISMATCH
, val
, 0, .align
= desc
, &desc
->loc
};
267 ubsan_handle(&v
, true);
271 __ubsan_handle_pointer_overflow(struct ubsan_ptroverflow_desc
*desc
, uint64_t before
, uint64_t after
)
273 struct ubsan_violation v
= { UBSAN_POINTER_OVERFLOW
, before
, after
, .ptroverflow
= desc
, &desc
->loc
};
274 ubsan_handle(&v
, false);
278 __ubsan_handle_pointer_overflow_abort(struct ubsan_ptroverflow_desc
*desc
, uint64_t before
, uint64_t after
)
280 struct ubsan_violation v
= { UBSAN_POINTER_OVERFLOW
, before
, after
, .ptroverflow
= desc
, &desc
->loc
};
281 ubsan_handle(&v
, true);
285 __ubsan_handle_out_of_bounds(struct ubsan_oob_desc
*desc
, uint64_t idx
)
287 struct ubsan_violation v
= { UBSAN_OOB
, idx
, 0, .oob
= desc
, &desc
->loc
};
288 ubsan_handle(&v
, false);
292 __ubsan_handle_out_of_bounds_abort(struct ubsan_oob_desc
*desc
, uint64_t idx
)
294 struct ubsan_violation v
= { UBSAN_OOB
, idx
, 0, .oob
= desc
, &desc
->loc
};
295 ubsan_handle(&v
, true);
298 #define DEFINE_GENERIC(check) \
299 void __ubsan_handle_##check (struct san_src_loc* loc) \
301 struct ubsan_violation v = { UBSAN_GENERIC, 0, 0, .func = __func__, loc }; \
302 ubsan_handle(&v, false); \
304 void __ubsan_handle_##check##_abort(struct san_src_loc* loc) \
306 struct ubsan_violation v = { UBSAN_GENERIC, 0, 0, .func = __func__, loc }; \
307 ubsan_handle(&v, true); \
310 DEFINE_GENERIC(invalid_builtin
)
311 DEFINE_GENERIC(load_invalid_value
)
312 DEFINE_GENERIC(nonnull_arg
)
313 DEFINE_GENERIC(vla_bound_not_positive
)
314 DEFINE_GENERIC(float_cast_overflow
)
315 DEFINE_GENERIC(function_type_mismatch
)
316 DEFINE_GENERIC(missing_return
)
317 DEFINE_GENERIC(nonnull_return
)
318 DEFINE_GENERIC(nullability_arg
)
319 DEFINE_GENERIC(nullability_return
)
320 DEFINE_GENERIC(implicit_conversion
)