]>
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
);
39 format_loc(struct san_src_loc
*loc
, char *dst
, size_t sz
)
41 return scnprintf(dst
, sz
, ", file:\"%s\", line:%d, column:%d },\n",
43 loc
->line
& ~line_acquired
,
49 * return true for the first visit to this loc, false every subsequent time
52 ubsan_loc_acquire(struct san_src_loc
*loc
)
54 uint32_t line
= loc
->line
;
55 if (line
& line_acquired
) {
58 uint32_t acq
= line
| line_acquired
;
59 return atomic_compare_exchange_strong((_Atomic
uint32_t *)&loc
->line
, &line
, acq
);
62 static const char *const
74 format_overflow(struct ubsan_violation
*v
, char *buf
, size_t sz
)
76 struct san_type_desc
*ty
= v
->overflow
->ty
;
77 return scnprintf(buf
, sz
,
78 "problem:\"%s overflow\", op:\"%s\", ty:\"%s\", width:%d, lhs:0x%llx, rhs:0x%llx, ",
79 ty
->issigned
? "signed" : "unsigned",
80 overflow_str
[v
->ubsan_type
],
89 format_shift(struct ubsan_violation
*v
, char *buf
, size_t sz
)
92 struct san_type_desc
*l
= v
->shift
->lhs_t
;
93 struct san_type_desc
*r
= v
->shift
->rhs_t
;
95 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"bad shift\", ");
96 n
+= scnprintf(buf
+ n
, sz
- n
, "lhs:0x%llx, lty:\"%s\", lsigned:%d, lwidth:%d, ", v
->lhs
, l
->name
, l
->issigned
, 1 << l
->width
);
97 n
+= scnprintf(buf
+ n
, sz
- n
, "rhs:0x%llx, rty:\"%s\", rsigned:%d, rwidth:%d, ", v
->rhs
, r
->name
, r
->issigned
, 1 << r
->width
);
102 static const char * const
103 type_check_kinds
[] = {
104 "load of", "store to", "reference binding to", "member access within",
105 "member call on", "constructor call on", "downcast of", "downcast of",
106 "upcast of", "cast to virtual base of", "_Nonnull binding to"
110 get_type_check_kind(uint8_t kind
)
112 return (kind
< (sizeof(type_check_kinds
) / sizeof(type_check_kinds
[0])))
113 ? type_check_kinds
[kind
]
118 format_type_mismatch(struct ubsan_violation
*v
, char *buf
, size_t sz
)
121 size_t alignment
= 1 << v
->align
->align
;
122 void *ptr
= (void*)v
->lhs
;
123 const char * kind
= get_type_check_kind(v
->align
->kind
);
126 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"%s NULL pointer\", ty:\"%s\", ", kind
, v
->align
->ty
->name
);
127 } else if (alignment
&& ((uintptr_t)ptr
& (alignment
- 1))) {
128 //misaligned pointer use
129 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"%s mis-aligned\", address:%p, ty:\"%s\", ", kind
, (void*)v
->lhs
, v
->align
->ty
->name
);
130 n
+= scnprintf(buf
+ n
, sz
- n
, "required_alignment:%d, ", 1 << v
->align
->align
);
132 //insufficient object size
133 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"%s insufficient object size\", ty:\"%s\", address:%p, ",
134 kind
, v
->align
->ty
->name
, ptr
);
141 format_oob(struct ubsan_violation
*v
, char *buf
, size_t sz
)
144 struct san_type_desc
*aty
= v
->oob
->array_ty
;
145 struct san_type_desc
*ity
= v
->oob
->index_ty
;
146 uintptr_t idx
= v
->lhs
;
148 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"OOB array access\", ");
149 n
+= scnprintf(buf
+ n
, sz
- n
, "idx:%ld, ", idx
);
150 n
+= scnprintf(buf
+ n
, sz
- n
, "aty:\"%s\", asigned:%d, awidth:%d, ", aty
->name
, aty
->issigned
, 1 << aty
->width
);
151 n
+= scnprintf(buf
+ n
, sz
- n
, "ity:\"%s\", isigned:%d, iwidth:%d, ", ity
->name
, ity
->issigned
, 1 << ity
->width
);
157 format_load_invalid_value(struct ubsan_violation
*v
, char *buf
, size_t sz
)
159 return scnprintf(buf
, sz
, "problem:\"invalid value load\", type:\"%s\", value:0x%llx",
160 v
->invalid
->type
->name
, v
->lhs
);
164 ubsan_format(struct ubsan_violation
*v
, char *buf
, size_t sz
)
166 size_t n
= scnprintf(buf
, sz
, "{ ");
168 switch (v
->ubsan_type
) {
169 case UBSAN_OVERFLOW_add
... UBSAN_OVERFLOW_negate
:
170 n
+= format_overflow(v
, buf
+ n
, sz
- n
);
172 case UBSAN_UNREACHABLE
:
173 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"unreachable\", ");
176 n
+= format_shift(v
, buf
+ n
, sz
- n
);
178 case UBSAN_TYPE_MISMATCH
:
179 n
+= format_type_mismatch(v
, buf
+ n
, sz
- n
);
181 case UBSAN_POINTER_OVERFLOW
:
182 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"pointer overflow\", before:0x%llx, after:0x%llx, ", v
->lhs
, v
->rhs
);
185 n
+= format_oob(v
, buf
+ n
, sz
- n
);
187 case UBSAN_LOAD_INVALID_VALUE
:
188 n
+= format_load_invalid_value(v
, buf
+ n
, sz
- n
);
191 n
+= scnprintf(buf
+ n
, sz
- n
, "problem:\"generic\", function:\"%s\", ", v
->func
);
194 panic("unknown violation");
197 n
+= format_loc(v
->loc
, buf
+ n
, sz
- n
);
202 enum UBFatality
{ Fatal
, FleshWound
};
205 ubsan_handle(struct ubsan_violation
*v
, enum UBFatality fatality
)
207 if (!ubsan_loc_acquire(v
->loc
)) {
208 /* violation site already reported */
214 if (ubsan_print
|| (fatality
== Fatal
)) {
215 const size_t sz
= 256;
218 ubsan_format(v
, buf
, sz
);
219 printf("UBSan: %s", buf
);
224 __ubsan_handle_builtin_unreachable(struct ubsan_unreachable_desc
*desc
)
226 struct ubsan_violation v
= { UBSAN_UNREACHABLE
, 0, 0, .unreachable
= desc
, &desc
->loc
};
227 ubsan_handle(&v
, Fatal
);
231 __ubsan_handle_shift_out_of_bounds(struct ubsan_shift_desc
*desc
, uint64_t lhs
, uint64_t rhs
)
233 struct ubsan_violation v
= { UBSAN_SHIFT
, lhs
, rhs
, .shift
= desc
, &desc
->loc
};
234 ubsan_handle(&v
, FleshWound
);
238 __ubsan_handle_shift_out_of_bounds_abort(struct ubsan_shift_desc
*desc
, uint64_t lhs
, uint64_t rhs
)
240 struct ubsan_violation v
= { UBSAN_SHIFT
, lhs
, rhs
, .shift
= desc
, &desc
->loc
};
241 ubsan_handle(&v
, Fatal
);
244 #define DEFINE_OVERFLOW(op) \
245 void __ubsan_handle_##op##_overflow(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, FleshWound); \
249 void __ubsan_handle_##op##_overflow_abort(struct ubsan_overflow_desc *desc, uint64_t lhs, uint64_t rhs) { \
250 struct ubsan_violation v = { UBSAN_OVERFLOW_##op, lhs, rhs, .overflow = desc, &desc->loc }; \
251 ubsan_handle(&v, Fatal); \
257 DEFINE_OVERFLOW(divrem
)
258 DEFINE_OVERFLOW(negate
)
261 __ubsan_handle_type_mismatch_v1(struct ubsan_align_desc
*desc
, uint64_t val
)
263 struct ubsan_violation v
= { UBSAN_TYPE_MISMATCH
, val
, 0, .align
= desc
, &desc
->loc
};
264 ubsan_handle(&v
, FleshWound
);
268 __ubsan_handle_type_mismatch_v1_abort(struct ubsan_align_desc
*desc
, uint64_t val
)
270 struct ubsan_violation v
= { UBSAN_TYPE_MISMATCH
, val
, 0, .align
= desc
, &desc
->loc
};
271 ubsan_handle(&v
, Fatal
);
275 __ubsan_handle_pointer_overflow(struct ubsan_ptroverflow_desc
*desc
, uint64_t before
, uint64_t after
)
277 struct ubsan_violation v
= { UBSAN_POINTER_OVERFLOW
, before
, after
, .ptroverflow
= desc
, &desc
->loc
};
278 ubsan_handle(&v
, FleshWound
);
282 __ubsan_handle_pointer_overflow_abort(struct ubsan_ptroverflow_desc
*desc
, uint64_t before
, uint64_t after
)
284 struct ubsan_violation v
= { UBSAN_POINTER_OVERFLOW
, before
, after
, .ptroverflow
= desc
, &desc
->loc
};
285 ubsan_handle(&v
, Fatal
);
289 __ubsan_handle_out_of_bounds(struct ubsan_oob_desc
*desc
, uint64_t idx
)
291 struct ubsan_violation v
= { UBSAN_OOB
, idx
, 0, .oob
= desc
, &desc
->loc
};
292 ubsan_handle(&v
, FleshWound
);
296 __ubsan_handle_out_of_bounds_abort(struct ubsan_oob_desc
*desc
, uint64_t idx
)
298 struct ubsan_violation v
= { UBSAN_OOB
, idx
, 0, .oob
= desc
, &desc
->loc
};
299 ubsan_handle(&v
, Fatal
);
303 __ubsan_handle_load_invalid_value(struct ubsan_load_invalid_desc
*desc
, uint64_t invalid_value
)
305 struct ubsan_violation v
= { UBSAN_LOAD_INVALID_VALUE
, invalid_value
, 0, .invalid
= desc
, &desc
->loc
};
306 ubsan_handle(&v
, Fatal
);
310 __ubsan_handle_load_invalid_value_abort(struct ubsan_load_invalid_desc
*desc
, uint64_t invalid_value
)
312 struct ubsan_violation v
= { UBSAN_LOAD_INVALID_VALUE
, invalid_value
, 0, .invalid
= desc
, &desc
->loc
};
313 ubsan_handle(&v
, Fatal
);
316 #define DEFINE_GENERIC(check) \
317 void __ubsan_handle_##check (struct san_src_loc* loc) \
319 struct ubsan_violation v = { UBSAN_GENERIC, 0, 0, .func = __func__, loc }; \
320 ubsan_handle(&v, FleshWound); \
322 void __ubsan_handle_##check##_abort(struct san_src_loc* loc) \
324 struct ubsan_violation v = { UBSAN_GENERIC, 0, 0, .func = __func__, loc }; \
325 ubsan_handle(&v, Fatal); \
328 DEFINE_GENERIC(invalid_builtin
)
329 DEFINE_GENERIC(nonnull_arg
)
330 DEFINE_GENERIC(vla_bound_not_positive
)
331 DEFINE_GENERIC(float_cast_overflow
)
332 DEFINE_GENERIC(function_type_mismatch
)
333 DEFINE_GENERIC(missing_return
)
334 DEFINE_GENERIC(nonnull_return
)
335 DEFINE_GENERIC(nullability_arg
)
336 DEFINE_GENERIC(nullability_return
)
337 DEFINE_GENERIC(implicit_conversion
)