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 <os/atomic_private.h>
30 #include <kern/cpu_data.h>
31 #include <kern/kalloc.h>
32 #include <kern/simple_lock.h> // hw_wait_while_equals
33 #include <libkern/libkern.h>
34 #include <sys/sysctl.h>
38 * To dump the violation log:
39 * $ sysctl kern.ubsan.log
42 * $ sysctl kern.ubsan.logentries=0
45 static const size_t ubsan_log_size
= 2048;
46 struct ubsan_violation ubsan_log
[ubsan_log_size
];
49 * Implement a fixed-size buffer FIFO, similar to the Chase-Lev DeQueue.
51 * See https://fzn.fr/readings/ppopp13.pdf for explanations on barriers.
53 _Atomic
size_t ubsan_log_head
= 0; /* first valid entry */
54 _Atomic
size_t ubsan_log_tail
= 0; /* next free slot (reader) */
55 _Atomic
size_t ubsan_log_next
= 0; /* next free slot (writer) */
57 static const bool ubsan_logging
= true;
62 return (x
+ 1) % ubsan_log_size
;
66 ubsan_log_append(struct ubsan_violation
*violation
)
77 os_atomic_rmw_loop(&ubsan_log_next
, i
, n
, relaxed
, {
79 if (n
== os_atomic_load(&ubsan_log_tail
, acquire
)) {
85 ubsan_log
[i
] = *violation
;
86 os_atomic_thread_fence(release
);
88 /* make the entry available */
90 os_atomic_rmw_loop(&ubsan_log_head
, e
, n
, relaxed
, {
92 // we need to wait for another enqueuer
93 os_atomic_rmw_loop_give_up({
94 hw_wait_while_equals((void **)&ubsan_log_head
, (void *)e
);
104 sysctl_ubsan_log_dump SYSCTL_HANDLER_ARGS
106 #pragma unused(oidp, arg1, arg2)
107 const size_t sz
= ubsan_log_size
* 256;
110 head
= os_atomic_load(&ubsan_log_head
, relaxed
);
111 os_atomic_thread_fence(seq_cst
);
112 tail
= os_atomic_load(&ubsan_log_tail
, relaxed
);
119 return 0; /* log is empty */
122 buf
= kheap_alloc(KHEAP_TEMP
, sz
, Z_WAITOK
| Z_ZERO
);
127 for (size_t i
= tail
; i
!= head
; i
= next_entry(i
)) {
128 n
+= ubsan_format(&ubsan_log
[i
], buf
+ n
, sz
- n
);
131 err
= SYSCTL_OUT(req
, buf
, n
);
133 kheap_free(KHEAP_TEMP
, buf
, sz
);
138 sysctl_ubsan_log_entries SYSCTL_HANDLER_ARGS
140 #pragma unused(oidp, arg1, arg2)
146 head
= os_atomic_load(&ubsan_log_head
, relaxed
);
147 os_atomic_thread_fence(seq_cst
);
148 tail
= os_atomic_load(&ubsan_log_tail
, relaxed
);
151 nentries
= head
- tail
;
153 nentries
= ubsan_log_size
- (tail
- head
+ 1);
156 err
= sysctl_io_number(req
, nentries
, sizeof(nentries
), &val
, &ch
);
157 if (err
== 0 && ch
) {
161 os_atomic_store(&ubsan_log_tail
, head
, relaxed
);
169 SYSCTL_NODE(_kern
, OID_AUTO
, ubsan
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "");
171 SYSCTL_COMPAT_UINT(_kern_ubsan
, OID_AUTO
, logsize
, CTLFLAG_RD
, NULL
, (unsigned)ubsan_log_size
, "");
173 SYSCTL_PROC(_kern_ubsan
, OID_AUTO
, logentries
,
174 CTLTYPE_INT
| CTLFLAG_RW
,
175 0, 0, sysctl_ubsan_log_entries
, "I", "");
177 SYSCTL_PROC(_kern_ubsan
, OID_AUTO
, log
,
178 CTLTYPE_STRING
| CTLFLAG_RD
| CTLFLAG_MASKED
,
179 0, 0, sysctl_ubsan_log_dump
, "A", "");