1/*
2 * Copyright (c) 2016-2020 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 <kern/backtrace.h>
30#include <kern/kalloc.h>
31#include <sys/errno.h>
32#include <sys/sysctl.h>
33#include <sys/systm.h>
34
35#if DEVELOPMENT || DEBUG
36
37/*
38 * Ignore -Wxnu-typed-allocators for this file, as it implements
39 * sysctls that are only available for DEVELOPMENT || DEBUG builds.
40 */
41__typed_allocators_ignore_push
42
43#define MAX_BACKTRACE (128)
44
45#define BACKTRACE_USER (0)
46#define BACKTRACE_USER_RESUME (1)
47static int backtrace_user_sysctl SYSCTL_HANDLER_ARGS;
48
49#define BACKTRACE_KERN_TEST_PACK_UNPACK (0)
50#define BACKTRACE_KERN_TEST_PACKED (1)
51static int backtrace_kernel_sysctl SYSCTL_HANDLER_ARGS;
52
53SYSCTL_NODE(_kern, OID_AUTO, backtrace, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
54 "backtrace");
55
56SYSCTL_PROC(_kern_backtrace, OID_AUTO, user,
57 CTLFLAG_RW | CTLFLAG_LOCKED, (void *)BACKTRACE_USER,
58 sizeof(uint64_t), backtrace_user_sysctl, "O",
59 "take user backtrace of current thread");
60
61SYSCTL_PROC(_kern_backtrace, OID_AUTO, kernel_tests,
62 CTLFLAG_RW | CTLFLAG_LOCKED, (void *)BACKTRACE_USER,
63 sizeof(uint64_t), backtrace_kernel_sysctl, "O",
64 "take user backtrace of current thread");
65
66static int
67backtrace_kernel_sysctl SYSCTL_HANDLER_ARGS
68{
69 unsigned int scenario = (unsigned int)req->newlen;
70 uintptr_t *bt = NULL;
71 uint8_t *packed_bt = NULL;
72 uintptr_t *unpacked_bt = NULL;
73 unsigned int bt_len = 0;
74 size_t bt_size = 0;
75 errno_t error = 0;
76
77 bt_len = 24;
78 bt_size = sizeof(bt[0]) * bt_len;
79 bt = kalloc_data(bt_size, Z_WAITOK | Z_ZERO);
80 packed_bt = kalloc_data(bt_size, Z_WAITOK | Z_ZERO);
81 unpacked_bt = kalloc_data(bt_size, Z_WAITOK | Z_ZERO);
82 if (!bt || !packed_bt || !unpacked_bt) {
83 error = ENOBUFS;
84 goto out;
85 }
86 backtrace_info_t info = BTI_NONE;
87 unsigned int len = backtrace(bt, bt_len, NULL, &info);
88 backtrace_info_t packed_info = BTI_NONE;
89 size_t packed_size = 0;
90 if (scenario == BACKTRACE_KERN_TEST_PACK_UNPACK) {
91 packed_size = backtrace_pack(BTP_KERN_OFFSET_32, packed_bt, bt_size,
92 bt, len);
93 } else {
94 packed_size = backtrace_packed(BTP_KERN_OFFSET_32, packed_bt, bt_size,
95 NULL, &packed_info);
96 }
97 unsigned int unpacked_len = backtrace_unpack(BTP_KERN_OFFSET_32,
98 unpacked_bt, bt_len, packed_bt, packed_size);
99 if (unpacked_len != len) {
100 printf("backtrace_tests: length %u != %u unpacked\n", len,
101 unpacked_len);
102 error = ERANGE;
103 goto out;
104 }
105 for (unsigned int i = 0; i < len; i++) {
106 if (unpacked_bt[i] != bt[i]) {
107 printf("backtrace_tests: bad address %u: 0x%lx != 0x%lx unpacked",
108 i, bt[i], unpacked_bt[i]);
109 error = EINVAL;
110 }
111 }
112
113out:
114 if (bt) {
115 kfree_data(bt, bt_size);
116 }
117 if (packed_bt) {
118 kfree_data(packed_bt, bt_size);
119 }
120 if (unpacked_bt) {
121 kfree_data(unpacked_bt, bt_size);
122 }
123 return error;
124}
125
126static int
127backtrace_user_sysctl SYSCTL_HANDLER_ARGS
128{
129#pragma unused(oidp, arg1, arg2)
130 unsigned int scenario = (unsigned int)req->newlen;
131 uintptr_t *bt = NULL;
132 unsigned int bt_len = 0, bt_filled = 0, bt_space = 0;
133 size_t bt_size = 0;
134 errno_t error = 0;
135
136 bool user_scenario = scenario == BACKTRACE_USER;
137 bool resume_scenario = scenario == BACKTRACE_USER_RESUME;
138 if (!user_scenario && !resume_scenario) {
139 return ENOTSUP;
140 }
141
142 if (req->oldptr == USER_ADDR_NULL || req->oldlen == 0) {
143 return EFAULT;
144 }
145
146 bt_len = req->oldlen > MAX_BACKTRACE ? MAX_BACKTRACE :
147 (unsigned int)req->oldlen;
148 bt_size = sizeof(bt[0]) * bt_len;
149 bt = kalloc_data(bt_size, Z_WAITOK | Z_ZERO);
150 if (!bt) {
151 return ENOBUFS;
152 }
153 bt_space = resume_scenario ? bt_len / 2 : bt_len;
154 struct backtrace_user_info btinfo = BTUINFO_INIT;
155 bt_filled = backtrace_user(bt, bt_space, NULL, &btinfo);
156 error = btinfo.btui_error;
157 if (error != 0) {
158 goto out;
159 }
160 if (resume_scenario) {
161 if (!(btinfo.btui_info & BTI_TRUNCATED)) {
162 error = ENOSPC;
163 goto out;
164 }
165 struct backtrace_control ctl = {
166 .btc_frame_addr = btinfo.btui_next_frame_addr,
167 };
168 btinfo = BTUINFO_INIT;
169 unsigned int bt_more = backtrace_user(bt + bt_filled, bt_space, &ctl,
170 &btinfo);
171 error = btinfo.btui_error;
172 if (error != 0) {
173 goto out;
174 }
175 bt_filled += bt_more;
176 }
177 bt_filled = min(bt_filled, bt_len);
178 if (btinfo.btui_async_frame_addr != 0 &&
179 btinfo.btui_async_start_index != 0) {
180 // Put the async call stack inline after the real call stack.
181 unsigned int start_index = btinfo.btui_async_start_index;
182 uintptr_t frame_addr = btinfo.btui_async_frame_addr;
183 unsigned int bt_left = bt_len - start_index;
184 struct backtrace_control ctl = { .btc_frame_addr = frame_addr, };
185 btinfo = BTUINFO_INIT;
186 unsigned int async_filled = backtrace_user(bt + start_index, bt_left,
187 &ctl, &btinfo);
188 error = btinfo.btui_error;
189 if (error != 0) {
190 goto out;
191 }
192 bt_filled = min(start_index + async_filled, bt_len);
193 }
194
195 error = copyout(bt, req->oldptr, sizeof(bt[0]) * bt_filled);
196 if (error) {
197 goto out;
198 }
199 req->oldidx = bt_filled;
200
201out:
202 kfree_data(bt, bt_size);
203 return error;
204}
205
206__typed_allocators_ignore_pop
207
208#endif /* DEVELOPMENT || DEBUG */
209