]> git.saurik.com Git - apple/xnu.git/blob - san/ubsan_log.c
xnu-7195.101.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 if (tail == head) {
115 return 0; /* log is empty */
116 }
117
118 char *buf = kheap_alloc(KHEAP_TEMP, sz, Z_WAITOK | Z_ZERO);
119 if (!buf) {
120 return 0;
121 }
122
123 struct ubsan_buf ubsan_buf = {
124 .ub_logged = 0,
125 .ub_buf_size = sz,
126 .ub_buf = buf
127 };
128
129 for (size_t i = tail; i != head; i = next_entry(i)) {
130 ubsan_format(&ubsan_log[i], &ubsan_buf);
131 }
132
133 int err = SYSCTL_OUT(req, buf, ubsan_buf.ub_logged);
134
135 kheap_free(KHEAP_TEMP, buf, sz);
136 return err;
137 }
138
139 static int
140 sysctl_ubsan_log_entries SYSCTL_HANDLER_ARGS
141 {
142 #pragma unused(oidp, arg1, arg2)
143 int ch, err, val;
144 size_t head, tail;
145
146 size_t nentries;
147
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);
151
152 if (head >= tail) {
153 nentries = head - tail;
154 } else {
155 nentries = ubsan_log_size - (tail - head + 1);
156 }
157
158 err = sysctl_io_number(req, nentries, sizeof(nentries), &val, &ch);
159 if (err == 0 && ch) {
160 if (val != 0) {
161 err = EINVAL;
162 } else {
163 os_atomic_store(&ubsan_log_tail, head, relaxed);
164 }
165 }
166
167 return err;
168 }
169
170 SYSCTL_DECL(ubsan);
171 SYSCTL_NODE(_kern, OID_AUTO, ubsan, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");
172
173 SYSCTL_COMPAT_UINT(_kern_ubsan, OID_AUTO, logsize, CTLFLAG_RD, NULL, (unsigned)ubsan_log_size, "");
174
175 SYSCTL_PROC(_kern_ubsan, OID_AUTO, logentries,
176 CTLTYPE_INT | CTLFLAG_RW,
177 0, 0, sysctl_ubsan_log_entries, "I", "");
178
179 SYSCTL_PROC(_kern_ubsan, OID_AUTO, log,
180 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MASKED,
181 0, 0, sysctl_ubsan_log_dump, "A", "");