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
);
115 return 0; /* log is empty */
118 char *buf
= kheap_alloc(KHEAP_TEMP
, sz
, Z_WAITOK
| Z_ZERO
);
123 struct ubsan_buf ubsan_buf
= {
129 for (size_t i
= tail
; i
!= head
; i
= next_entry(i
)) {
130 ubsan_format(&ubsan_log
[i
], &ubsan_buf
);
133 int err
= SYSCTL_OUT(req
, buf
, ubsan_buf
.ub_logged
);
135 kheap_free(KHEAP_TEMP
, buf
, sz
);
140 sysctl_ubsan_log_entries SYSCTL_HANDLER_ARGS
142 #pragma unused(oidp, arg1, arg2)
148 head
= os_atomic_load(&ubsan_log_head
, relaxed
);
149 os_atomic_thread_fence(seq_cst
);
150 tail
= os_atomic_load(&ubsan_log_tail
, relaxed
);
153 nentries
= head
- tail
;
155 nentries
= ubsan_log_size
- (tail
- head
+ 1);
158 err
= sysctl_io_number(req
, nentries
, sizeof(nentries
), &val
, &ch
);
159 if (err
== 0 && ch
) {
163 os_atomic_store(&ubsan_log_tail
, head
, relaxed
);
171 SYSCTL_NODE(_kern
, OID_AUTO
, ubsan
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "");
173 SYSCTL_COMPAT_UINT(_kern_ubsan
, OID_AUTO
, logsize
, CTLFLAG_RD
, NULL
, (unsigned)ubsan_log_size
, "");
175 SYSCTL_PROC(_kern_ubsan
, OID_AUTO
, logentries
,
176 CTLTYPE_INT
| CTLFLAG_RW
,
177 0, 0, sysctl_ubsan_log_entries
, "I", "");
179 SYSCTL_PROC(_kern_ubsan
, OID_AUTO
, log
,
180 CTLTYPE_STRING
| CTLFLAG_RD
| CTLFLAG_MASKED
,
181 0, 0, sysctl_ubsan_log_dump
, "A", "");