]>
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
;
38 format_loc(struct san_src_loc
*loc
, char *dst
, size_t sz
)
40 return snprintf(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 snprintf(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
+= snprintf(buf
+ n
, sz
- n
, "bad shift\n");
95 n
+= snprintf(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
+= snprintf(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
111 format_alignment(struct ubsan_violation
*v
, char *buf
, size_t sz
)
114 struct san_type_desc
*ty
= v
->align
->ty
;
116 n
+= snprintf(buf
+ n
, sz
- n
, "mis-aligned %s of 0x%llx\n", align_kinds
[v
->align
->kind
], v
->lhs
);
117 n
+= snprintf(buf
+ n
, sz
- n
, " expected %d-byte alignment, type = %s\n",
118 1 << v
->align
->align
, ty
->name
);
123 format_oob(struct ubsan_violation
*v
, char *buf
, size_t sz
)
126 struct san_type_desc
*aty
= v
->oob
->array_ty
;
127 struct san_type_desc
*ity
= v
->oob
->index_ty
;
128 uintptr_t idx
= v
->lhs
;
130 n
+= snprintf(buf
+ n
, sz
- n
, "OOB array access\n");
131 n
+= snprintf(buf
+ n
, sz
- n
, " idx %ld\n", idx
);
132 n
+= snprintf(buf
+ n
, sz
- n
, " aty: ty = %s, signed = %d, width = %d\n", aty
->name
, aty
->issigned
, 1 << aty
->width
);
133 n
+= snprintf(buf
+ n
, sz
- n
, " ity: ty = %s, signed = %d, width = %d\n", ity
->name
, ity
->issigned
, 1 << ity
->width
);
139 ubsan_format(struct ubsan_violation
*v
, char *buf
, size_t sz
)
143 switch (v
->ubsan_type
) {
144 case UBSAN_OVERFLOW_add
... UBSAN_OVERFLOW_negate
:
145 n
+= format_overflow(v
, buf
+ n
, sz
- n
);
147 case UBSAN_UNREACHABLE
:
148 n
+= snprintf(buf
+ n
, sz
- n
, "unreachable\n");
151 n
+= format_shift(v
, buf
+ n
, sz
- n
);
154 n
+= format_alignment(v
, buf
+ n
, sz
- n
);
156 case UBSAN_POINTER_OVERFLOW
:
157 n
+= snprintf(buf
+ n
, sz
- n
, "pointer overflow, before = 0x%llx, after = 0x%llx\n", v
->lhs
, v
->rhs
);
160 n
+= format_oob(v
, buf
+ n
, sz
- n
);
163 panic("unknown violation");
166 n
+= format_loc(v
->loc
, buf
+ n
, sz
- n
);
172 ubsan_handle(struct ubsan_violation
*v
, bool fatal
)
174 const size_t sz
= 256;
179 if (!ubsan_loc_acquire(v
->loc
)) {
180 /* violation site already reported */
186 if (ubsan_print
|| fatal
) {
187 n
+= ubsan_format(v
, buf
+ n
, sz
- n
);
191 printf("UBSan: %s", buf
);
195 panic("UBSan: %s", buf
);
200 __ubsan_handle_builtin_unreachable(struct ubsan_unreachable_desc
*desc
)
202 struct ubsan_violation v
= { UBSAN_UNREACHABLE
, 0, 0, .unreachable
= desc
, &desc
->loc
};
203 ubsan_handle(&v
, true);
207 __ubsan_handle_shift_out_of_bounds(struct ubsan_shift_desc
*desc
, uint64_t lhs
, uint64_t rhs
)
209 struct ubsan_violation v
= { UBSAN_SHIFT
, lhs
, rhs
, .shift
= desc
, &desc
->loc
};
210 ubsan_handle(&v
, false);
214 __ubsan_handle_shift_out_of_bounds_abort(struct ubsan_shift_desc
*desc
, uint64_t lhs
, uint64_t rhs
)
216 struct ubsan_violation v
= { UBSAN_SHIFT
, lhs
, rhs
, .shift
= desc
, &desc
->loc
};
217 ubsan_handle(&v
, true);
220 #define DEFINE_OVERFLOW(op) \
221 void __ubsan_handle_##op##_overflow(struct ubsan_overflow_desc *desc, uint64_t lhs, uint64_t rhs) { \
222 struct ubsan_violation v = { UBSAN_OVERFLOW_##op, lhs, rhs, .overflow = desc, &desc->loc }; \
223 ubsan_handle(&v, false); \
225 void __ubsan_handle_##op##_overflow_abort(struct ubsan_overflow_desc *desc, uint64_t lhs, uint64_t rhs) { \
226 struct ubsan_violation v = { UBSAN_OVERFLOW_##op, lhs, rhs, .overflow = desc, &desc->loc }; \
227 ubsan_handle(&v, true); \
233 DEFINE_OVERFLOW(divrem
)
234 DEFINE_OVERFLOW(negate
)
237 __ubsan_handle_type_mismatch_v1(struct ubsan_align_desc
*desc
, uint64_t val
)
239 struct ubsan_violation v
= { UBSAN_ALIGN
, val
, 0, .align
= desc
, &desc
->loc
};
240 ubsan_handle(&v
, false);
244 __ubsan_handle_type_mismatch_v1_abort(struct ubsan_align_desc
*desc
, uint64_t val
)
246 struct ubsan_violation v
= { UBSAN_ALIGN
, val
, 0, .align
= desc
, &desc
->loc
};
247 ubsan_handle(&v
, true);
251 __ubsan_handle_pointer_overflow(struct ubsan_ptroverflow_desc
*desc
, uint64_t before
, uint64_t after
)
253 struct ubsan_violation v
= { UBSAN_POINTER_OVERFLOW
, before
, after
, .ptroverflow
= desc
, &desc
->loc
};
254 ubsan_handle(&v
, false);
258 __ubsan_handle_pointer_overflow_abort(struct ubsan_ptroverflow_desc
*desc
, uint64_t before
, uint64_t after
)
260 struct ubsan_violation v
= { UBSAN_POINTER_OVERFLOW
, before
, after
, .ptroverflow
= desc
, &desc
->loc
};
261 ubsan_handle(&v
, true);
265 __ubsan_handle_out_of_bounds(struct ubsan_oob_desc
*desc
, uint64_t idx
)
267 struct ubsan_violation v
= { UBSAN_OOB
, idx
, 0, .oob
= desc
, &desc
->loc
};
268 ubsan_handle(&v
, false);
272 __ubsan_handle_out_of_bounds_abort(struct ubsan_oob_desc
*desc
, uint64_t idx
)
274 struct ubsan_violation v
= { UBSAN_OOB
, idx
, 0, .oob
= desc
, &desc
->loc
};
275 ubsan_handle(&v
, true);