]>
Commit | Line | Data |
---|---|---|
d9a64523 A |
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 <stdatomic.h> | |
30 | #include <kern/kalloc.h> | |
31 | #include <libkern/libkern.h> | |
32 | #include <sys/sysctl.h> | |
33 | #include "ubsan.h" | |
34 | ||
35 | /* | |
36 | * To dump the violation log: | |
37 | * $ sysctl kern.ubsan.log | |
38 | * | |
39 | * To reset: | |
40 | * $ sysctl kern.ubsan.logentries=0 | |
41 | */ | |
42 | ||
43 | static const size_t ubsan_log_size = 2048; | |
44 | struct ubsan_violation ubsan_log[ubsan_log_size]; | |
45 | ||
46 | _Atomic size_t ubsan_log_head = 0; /* first valid entry */ | |
47 | _Atomic size_t ubsan_log_tail = 0; /* next free slot (reader) */ | |
48 | _Atomic size_t ubsan_log_next = 0; /* next free slot (writer) */ | |
49 | ||
50 | static const bool ubsan_logging = true; | |
51 | ||
52 | static inline size_t | |
53 | next_entry(size_t x) | |
54 | { | |
55 | return (x + 1) % ubsan_log_size; | |
56 | } | |
57 | ||
58 | void | |
59 | ubsan_log_append(struct ubsan_violation *e) | |
60 | { | |
61 | if (!ubsan_logging) { | |
62 | return; | |
63 | } | |
64 | ||
65 | /* reserve a slot */ | |
66 | size_t i = atomic_load(&ubsan_log_next); | |
67 | size_t n; | |
68 | do { | |
69 | n = next_entry(i); | |
70 | if (n == ubsan_log_tail) { | |
71 | return; /* full */ | |
72 | } | |
73 | } while (!atomic_compare_exchange_weak(&ubsan_log_next, &i, n)); | |
74 | ||
75 | ubsan_log[i] = *e; | |
76 | ||
77 | /* make the entry available */ | |
78 | size_t prev; | |
79 | do { | |
80 | prev = i; | |
81 | } while (!atomic_compare_exchange_weak(&ubsan_log_head, &prev, n)); | |
82 | } | |
83 | ||
84 | static int | |
85 | sysctl_ubsan_log_dump SYSCTL_HANDLER_ARGS | |
86 | { | |
87 | #pragma unused(oidp, arg1, arg2) | |
88 | const size_t sz = ubsan_log_size * 256; | |
89 | size_t start = atomic_load(&ubsan_log_tail); | |
90 | size_t end = atomic_load(&ubsan_log_head); | |
91 | ||
92 | char *buf; | |
93 | size_t n = 0; | |
94 | int err; | |
95 | ||
96 | if (start == end) { | |
97 | return 0; /* log is empty */ | |
98 | } | |
99 | ||
100 | buf = kalloc(sz); | |
101 | if (!buf) { | |
102 | return 0; | |
103 | } | |
104 | buf[0] = '\0'; | |
105 | ||
106 | for (size_t i = start; i != end; i = next_entry(i)) { | |
0a7de745 | 107 | n += ubsan_format(&ubsan_log[i], buf + n, sz - n); |
d9a64523 A |
108 | } |
109 | ||
110 | err = SYSCTL_OUT(req, buf, n); | |
111 | ||
112 | kfree(buf, sz); | |
113 | return err; | |
114 | } | |
115 | ||
116 | static int | |
117 | sysctl_ubsan_log_entries SYSCTL_HANDLER_ARGS | |
118 | { | |
119 | #pragma unused(oidp, arg1, arg2) | |
120 | int ch, err, val; | |
121 | ||
122 | int nentries; | |
123 | if (ubsan_log_head >= ubsan_log_tail) { | |
124 | nentries = ubsan_log_head - ubsan_log_tail; | |
125 | } else { | |
126 | nentries = ubsan_log_size - (ubsan_log_tail - ubsan_log_head + 1); | |
127 | } | |
128 | ||
129 | err = sysctl_io_number(req, nentries, sizeof(nentries), &val, &ch); | |
130 | if (err == 0 && ch) { | |
131 | if (val != 0) { | |
132 | err = EINVAL; | |
133 | } else { | |
134 | ubsan_log_tail = ubsan_log_head; | |
135 | } | |
136 | } | |
137 | ||
138 | return err; | |
139 | } | |
140 | ||
141 | SYSCTL_DECL(ubsan); | |
142 | SYSCTL_NODE(_kern, OID_AUTO, ubsan, CTLFLAG_RW | CTLFLAG_LOCKED, 0, ""); | |
143 | ||
144 | SYSCTL_COMPAT_UINT(_kern_ubsan, OID_AUTO, logsize, CTLFLAG_RD, NULL, (unsigned)ubsan_log_size, ""); | |
145 | ||
146 | SYSCTL_PROC(_kern_ubsan, OID_AUTO, logentries, | |
0a7de745 A |
147 | CTLTYPE_INT | CTLFLAG_RW, |
148 | 0, 0, sysctl_ubsan_log_entries, "I", ""); | |
d9a64523 A |
149 | |
150 | SYSCTL_PROC(_kern_ubsan, OID_AUTO, log, | |
0a7de745 A |
151 | CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MASKED, |
152 | 0, 0, sysctl_ubsan_log_dump, "A", ""); |