]> git.saurik.com Git - apple/xnu.git/blob - san/ubsan_log.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / san / ubsan_log.c
1 /*
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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>
35 #include "ubsan.h"
36
37 /*
38 * To dump the violation log:
39 * $ sysctl kern.ubsan.log
40 *
41 * To reset:
42 * $ sysctl kern.ubsan.logentries=0
43 */
44
45 static const size_t ubsan_log_size = 2048;
46 struct ubsan_violation ubsan_log[ubsan_log_size];
47
48 /*
49 * Implement a fixed-size buffer FIFO, similar to the Chase-Lev DeQueue.
50 *
51 * See https://fzn.fr/readings/ppopp13.pdf for explanations on barriers.
52 */
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) */
56
57 static const bool ubsan_logging = true;
58
59 static inline size_t
60 next_entry(size_t x)
61 {
62 return (x + 1) % ubsan_log_size;
63 }
64
65 void
66 ubsan_log_append(struct ubsan_violation *violation)
67 {
68 if (!ubsan_logging) {
69 return;
70 }
71
72 /* reserve a slot */
73 size_t i, e, n;
74
75 disable_preemption();
76
77 os_atomic_rmw_loop(&ubsan_log_next, i, n, relaxed, {
78 n = next_entry(i);
79 if (n == os_atomic_load(&ubsan_log_tail, acquire)) {
80 enable_preemption();
81 return; /* full */
82 }
83 });
84
85 ubsan_log[i] = *violation;
86 os_atomic_thread_fence(release);
87
88 /* make the entry available */
89 again:
90 os_atomic_rmw_loop(&ubsan_log_head, e, n, relaxed, {
91 if (e != i) {
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);
95 goto again;
96 });
97 }
98 });
99
100 enable_preemption();
101 }
102
103 static int
104 sysctl_ubsan_log_dump SYSCTL_HANDLER_ARGS
105 {
106 #pragma unused(oidp, arg1, arg2)
107 const size_t sz = ubsan_log_size * 256;
108 size_t head, tail;
109
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);
113
114 char *buf;
115 size_t n = 0;
116 int err;
117
118 if (tail == head) {
119 return 0; /* log is empty */
120 }
121
122 buf = kheap_alloc(KHEAP_TEMP, sz, Z_WAITOK | Z_ZERO);
123 if (!buf) {
124 return 0;
125 }
126
127 for (size_t i = tail; i != head; i = next_entry(i)) {
128 n += ubsan_format(&ubsan_log[i], buf + n, sz - n);
129 }
130
131 err = SYSCTL_OUT(req, buf, n);
132
133 kheap_free(KHEAP_TEMP, buf, sz);
134 return err;
135 }
136
137 static int
138 sysctl_ubsan_log_entries SYSCTL_HANDLER_ARGS
139 {
140 #pragma unused(oidp, arg1, arg2)
141 int ch, err, val;
142 size_t head, tail;
143
144 size_t nentries;
145
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);
149
150 if (head >= tail) {
151 nentries = head - tail;
152 } else {
153 nentries = ubsan_log_size - (tail - head + 1);
154 }
155
156 err = sysctl_io_number(req, nentries, sizeof(nentries), &val, &ch);
157 if (err == 0 && ch) {
158 if (val != 0) {
159 err = EINVAL;
160 } else {
161 os_atomic_store(&ubsan_log_tail, head, relaxed);
162 }
163 }
164
165 return err;
166 }
167
168 SYSCTL_DECL(ubsan);
169 SYSCTL_NODE(_kern, OID_AUTO, ubsan, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");
170
171 SYSCTL_COMPAT_UINT(_kern_ubsan, OID_AUTO, logsize, CTLFLAG_RD, NULL, (unsigned)ubsan_log_size, "");
172
173 SYSCTL_PROC(_kern_ubsan, OID_AUTO, logentries,
174 CTLTYPE_INT | CTLFLAG_RW,
175 0, 0, sysctl_ubsan_log_entries, "I", "");
176
177 SYSCTL_PROC(_kern_ubsan, OID_AUTO, log,
178 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MASKED,
179 0, 0, sysctl_ubsan_log_dump, "A", "");