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 <stdatomic.h>
30 #include <kern/kalloc.h>
31 #include <libkern/libkern.h>
32 #include <sys/sysctl.h>
36 * To dump the violation log:
37 * $ sysctl kern.ubsan.log
40 * $ sysctl kern.ubsan.logentries=0
43 static const size_t ubsan_log_size
= 2048;
44 struct ubsan_violation ubsan_log
[ubsan_log_size
];
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) */
50 static const bool ubsan_logging
= true;
55 return (x
+ 1) % ubsan_log_size
;
59 ubsan_log_append(struct ubsan_violation
*e
)
66 size_t i
= atomic_load(&ubsan_log_next
);
70 if (n
== ubsan_log_tail
) {
73 } while (!atomic_compare_exchange_weak(&ubsan_log_next
, &i
, n
));
77 /* make the entry available */
81 } while (!atomic_compare_exchange_weak(&ubsan_log_head
, &prev
, n
));
85 sysctl_ubsan_log_dump SYSCTL_HANDLER_ARGS
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
);
97 return 0; /* log is empty */
106 for (size_t i
= start
; i
!= end
; i
= next_entry(i
)) {
107 n
+= ubsan_format(&ubsan_log
[i
], buf
+n
, sz
-n
);
110 err
= SYSCTL_OUT(req
, buf
, n
);
117 sysctl_ubsan_log_entries SYSCTL_HANDLER_ARGS
119 #pragma unused(oidp, arg1, arg2)
123 if (ubsan_log_head
>= ubsan_log_tail
) {
124 nentries
= ubsan_log_head
- ubsan_log_tail
;
126 nentries
= ubsan_log_size
- (ubsan_log_tail
- ubsan_log_head
+ 1);
129 err
= sysctl_io_number(req
, nentries
, sizeof(nentries
), &val
, &ch
);
130 if (err
== 0 && ch
) {
134 ubsan_log_tail
= ubsan_log_head
;
142 SYSCTL_NODE(_kern
, OID_AUTO
, ubsan
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "");
144 SYSCTL_COMPAT_UINT(_kern_ubsan
, OID_AUTO
, logsize
, CTLFLAG_RD
, NULL
, (unsigned)ubsan_log_size
, "");
146 SYSCTL_PROC(_kern_ubsan
, OID_AUTO
, logentries
,
147 CTLTYPE_INT
| CTLFLAG_RW
,
148 0, 0, sysctl_ubsan_log_entries
, "I", "");
150 SYSCTL_PROC(_kern_ubsan
, OID_AUTO
, log
,
151 CTLTYPE_STRING
| CTLFLAG_RD
| CTLFLAG_MASKED
,
152 0, 0, sysctl_ubsan_log_dump
, "A", "");