xref: /xnu-10063.121.3/tests/backtracing_tests.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions // Copyright (c) 2016-2020 Apple Computer, Inc.  All rights reserved.
2*2c2f96dcSApple OSS Distributions 
3*2c2f96dcSApple OSS Distributions #include <CoreSymbolication/CoreSymbolication.h>
4*2c2f96dcSApple OSS Distributions #include <darwintest.h>
5*2c2f96dcSApple OSS Distributions #include <dispatch/dispatch.h>
6*2c2f96dcSApple OSS Distributions #include <execinfo.h>
7*2c2f96dcSApple OSS Distributions #include <pthread.h>
8*2c2f96dcSApple OSS Distributions #include <ptrauth.h>
9*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
10*2c2f96dcSApple OSS Distributions #include <stdalign.h>
11*2c2f96dcSApple OSS Distributions #include <sys/mman.h>
12*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
13*2c2f96dcSApple OSS Distributions 
14*2c2f96dcSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
15*2c2f96dcSApple OSS Distributions 
16*2c2f96dcSApple OSS Distributions enum test_scenario {
17*2c2f96dcSApple OSS Distributions 	USER_SCENARIO = 0,
18*2c2f96dcSApple OSS Distributions 	RESUME_SCENARIO = 1,
19*2c2f96dcSApple OSS Distributions };
20*2c2f96dcSApple OSS Distributions 
21*2c2f96dcSApple OSS Distributions enum kernel_test_scenario {
22*2c2f96dcSApple OSS Distributions 	PACK_UNPACK_SCENARIO = 0,
23*2c2f96dcSApple OSS Distributions 	PACKED_SCENARIO = 1,
24*2c2f96dcSApple OSS Distributions };
25*2c2f96dcSApple OSS Distributions 
26*2c2f96dcSApple OSS Distributions #define USER_FRAMES (12)
27*2c2f96dcSApple OSS Distributions #define MAX_SYSCALL_SETUP_FRAMES (3)
28*2c2f96dcSApple OSS Distributions #define NON_RECURSE_FRAMES (2)
29*2c2f96dcSApple OSS Distributions #define ASYNC_FRAMES (2 + NON_RECURSE_FRAMES)
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions static const char *user_bt[USER_FRAMES] = {
32*2c2f96dcSApple OSS Distributions 	"backtrace_thread",
33*2c2f96dcSApple OSS Distributions 	"recurse_a", "recurse_b", "recurse_a", "recurse_b",
34*2c2f96dcSApple OSS Distributions 	"recurse_a", "recurse_b", "recurse_a", "recurse_b",
35*2c2f96dcSApple OSS Distributions 	"recurse_a", "recurse_b", "expect_callstack",
36*2c2f96dcSApple OSS Distributions };
37*2c2f96dcSApple OSS Distributions 
38*2c2f96dcSApple OSS Distributions struct callstack_exp {
39*2c2f96dcSApple OSS Distributions 	bool in_syscall_setup;
40*2c2f96dcSApple OSS Distributions 	unsigned int syscall_frames;
41*2c2f96dcSApple OSS Distributions 	const char **callstack;
42*2c2f96dcSApple OSS Distributions 	size_t callstack_len;
43*2c2f96dcSApple OSS Distributions 	unsigned int nchecked;
44*2c2f96dcSApple OSS Distributions };
45*2c2f96dcSApple OSS Distributions 
46*2c2f96dcSApple OSS Distributions #if __has_feature(ptrauth_calls)
47*2c2f96dcSApple OSS Distributions #define __ptrauth_swift_async_context_parent \
48*2c2f96dcSApple OSS Distributions   __ptrauth(ptrauth_key_process_independent_data, 1, 0xbda2)
49*2c2f96dcSApple OSS Distributions #define __ptrauth_swift_async_context_resume \
50*2c2f96dcSApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer, 1, 0xd707)
51*2c2f96dcSApple OSS Distributions #else
52*2c2f96dcSApple OSS Distributions #define __ptrauth_swift_async_context_parent
53*2c2f96dcSApple OSS Distributions #define __ptrauth_swift_async_context_resume
54*2c2f96dcSApple OSS Distributions #endif
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions // This struct fakes the Swift AsyncContext struct which is used by
57*2c2f96dcSApple OSS Distributions // the Swift concurrency runtime. We only care about the first 2 fields.
58*2c2f96dcSApple OSS Distributions struct fake_async_context {
59*2c2f96dcSApple OSS Distributions 	struct fake_async_context* __ptrauth_swift_async_context_parent next;
60*2c2f96dcSApple OSS Distributions 	void(*__ptrauth_swift_async_context_resume resume_pc)(void);
61*2c2f96dcSApple OSS Distributions };
62*2c2f96dcSApple OSS Distributions 
63*2c2f96dcSApple OSS Distributions static void
level1_func()64*2c2f96dcSApple OSS Distributions level1_func()
65*2c2f96dcSApple OSS Distributions {
66*2c2f96dcSApple OSS Distributions }
67*2c2f96dcSApple OSS Distributions static void
level2_func()68*2c2f96dcSApple OSS Distributions level2_func()
69*2c2f96dcSApple OSS Distributions {
70*2c2f96dcSApple OSS Distributions }
71*2c2f96dcSApple OSS Distributions 
72*2c2f96dcSApple OSS Distributions // Create a chain of fake async contexts
73*2c2f96dcSApple OSS Distributions static alignas(16) struct fake_async_context level1 = { 0, level1_func };
74*2c2f96dcSApple OSS Distributions static alignas(16) struct fake_async_context level2 = { &level1, level2_func };
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions static const char *async_bt[ASYNC_FRAMES] = {
77*2c2f96dcSApple OSS Distributions 	"level1_func", "level2_func", "backtrace_thread_async",
78*2c2f96dcSApple OSS Distributions 	"expect_async_callstack",
79*2c2f96dcSApple OSS Distributions };
80*2c2f96dcSApple OSS Distributions 
81*2c2f96dcSApple OSS Distributions static void
expect_frame(struct callstack_exp * cs,CSSymbolRef symbol,unsigned long addr,unsigned int bt_idx)82*2c2f96dcSApple OSS Distributions expect_frame(struct callstack_exp *cs, CSSymbolRef symbol,
83*2c2f96dcSApple OSS Distributions     unsigned long addr, unsigned int bt_idx)
84*2c2f96dcSApple OSS Distributions {
85*2c2f96dcSApple OSS Distributions 	if (CSIsNull(symbol)) {
86*2c2f96dcSApple OSS Distributions 		if (!cs->in_syscall_setup) {
87*2c2f96dcSApple OSS Distributions 			T_FAIL("invalid symbol for address %#lx at frame %d", addr,
88*2c2f96dcSApple OSS Distributions 			    bt_idx);
89*2c2f96dcSApple OSS Distributions 		}
90*2c2f96dcSApple OSS Distributions 		return;
91*2c2f96dcSApple OSS Distributions 	}
92*2c2f96dcSApple OSS Distributions 
93*2c2f96dcSApple OSS Distributions 	const char *name = CSSymbolGetName(symbol);
94*2c2f96dcSApple OSS Distributions 	if (name) {
95*2c2f96dcSApple OSS Distributions 		if (cs->in_syscall_setup) {
96*2c2f96dcSApple OSS Distributions 			if (strcmp(name, cs->callstack[cs->callstack_len - 1]) == 0) {
97*2c2f96dcSApple OSS Distributions 				cs->in_syscall_setup = false;
98*2c2f96dcSApple OSS Distributions 				cs->syscall_frames = bt_idx;
99*2c2f96dcSApple OSS Distributions 				T_LOG("found start of controlled stack at frame %u, expected "
100*2c2f96dcSApple OSS Distributions 				    "index %zu", cs->syscall_frames, cs->callstack_len - 1);
101*2c2f96dcSApple OSS Distributions 			} else {
102*2c2f96dcSApple OSS Distributions 				T_LOG("found syscall setup symbol %s at frame %u", name,
103*2c2f96dcSApple OSS Distributions 				    bt_idx);
104*2c2f96dcSApple OSS Distributions 			}
105*2c2f96dcSApple OSS Distributions 		}
106*2c2f96dcSApple OSS Distributions 		if (!cs->in_syscall_setup) {
107*2c2f96dcSApple OSS Distributions 			if (cs->nchecked >= cs->callstack_len) {
108*2c2f96dcSApple OSS Distributions 				T_LOG("frame %2u: skipping system frame %s", bt_idx, name);
109*2c2f96dcSApple OSS Distributions 			} else {
110*2c2f96dcSApple OSS Distributions 				size_t frame_idx = cs->callstack_len - cs->nchecked - 1;
111*2c2f96dcSApple OSS Distributions 				T_EXPECT_EQ_STR(name, cs->callstack[frame_idx],
112*2c2f96dcSApple OSS Distributions 				    "frame %2zu: saw '%s', expected '%s'",
113*2c2f96dcSApple OSS Distributions 				    frame_idx, name, cs->callstack[frame_idx]);
114*2c2f96dcSApple OSS Distributions 			}
115*2c2f96dcSApple OSS Distributions 			cs->nchecked++;
116*2c2f96dcSApple OSS Distributions 		}
117*2c2f96dcSApple OSS Distributions 	} else {
118*2c2f96dcSApple OSS Distributions 		if (!cs->in_syscall_setup) {
119*2c2f96dcSApple OSS Distributions 			T_ASSERT_NOTNULL(name, NULL, "symbol should not be NULL");
120*2c2f96dcSApple OSS Distributions 		}
121*2c2f96dcSApple OSS Distributions 	}
122*2c2f96dcSApple OSS Distributions }
123*2c2f96dcSApple OSS Distributions 
124*2c2f96dcSApple OSS Distributions static bool
is_kernel_64_bit(void)125*2c2f96dcSApple OSS Distributions is_kernel_64_bit(void)
126*2c2f96dcSApple OSS Distributions {
127*2c2f96dcSApple OSS Distributions 	static dispatch_once_t k64_once;
128*2c2f96dcSApple OSS Distributions 	static bool k64 = false;
129*2c2f96dcSApple OSS Distributions 	dispatch_once(&k64_once, ^{
130*2c2f96dcSApple OSS Distributions 		int errb;
131*2c2f96dcSApple OSS Distributions 		int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 /* kernproc */ };
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions 		struct kinfo_proc kp;
134*2c2f96dcSApple OSS Distributions 		size_t len = sizeof(kp);
135*2c2f96dcSApple OSS Distributions 
136*2c2f96dcSApple OSS Distributions 		errb = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &kp, &len, NULL, 0);
137*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(errb,
138*2c2f96dcSApple OSS Distributions 		"sysctl({ CTL_KERN, KERN_PROC, KERN_PROC_PID, 0})");
139*2c2f96dcSApple OSS Distributions 
140*2c2f96dcSApple OSS Distributions 		k64 = kp.kp_proc.p_flag & P_LP64;
141*2c2f96dcSApple OSS Distributions 		T_LOG("executing with a %s-bit kernel", k64 ? "64" : "32");
142*2c2f96dcSApple OSS Distributions 	});
143*2c2f96dcSApple OSS Distributions 	return k64;
144*2c2f96dcSApple OSS Distributions }
145*2c2f96dcSApple OSS Distributions 
146*2c2f96dcSApple OSS Distributions // Use an extra, non-inlineable function so that any frames after expect_stack
147*2c2f96dcSApple OSS Distributions // can be safely ignored.  This insulates the test from changes in how syscalls
148*2c2f96dcSApple OSS Distributions // are called by Libc and the kernel.
149*2c2f96dcSApple OSS Distributions static void __attribute__((noinline, not_tail_called))
backtrace_current_thread_wrapper(enum test_scenario scenario,uint64_t * bt,size_t * bt_filled)150*2c2f96dcSApple OSS Distributions backtrace_current_thread_wrapper(enum test_scenario scenario, uint64_t *bt,
151*2c2f96dcSApple OSS Distributions     size_t *bt_filled)
152*2c2f96dcSApple OSS Distributions {
153*2c2f96dcSApple OSS Distributions 	int ret = sysctlbyname("kern.backtrace.user", bt, bt_filled, NULL,
154*2c2f96dcSApple OSS Distributions 	    scenario);
155*2c2f96dcSApple OSS Distributions 	getpid(); // Really prevent tail calls.
156*2c2f96dcSApple OSS Distributions 	if (ret == -1 && errno == ENOENT) {
157*2c2f96dcSApple OSS Distributions 		T_SKIP("release kernel: kern.backtrace.user sysctl returned ENOENT");
158*2c2f96dcSApple OSS Distributions 	}
159*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname(\"kern.backtrace.user\")");
160*2c2f96dcSApple OSS Distributions 	T_LOG("kernel returned %zu frame backtrace", *bt_filled);
161*2c2f96dcSApple OSS Distributions }
162*2c2f96dcSApple OSS Distributions 
163*2c2f96dcSApple OSS Distributions static CSSymbolicatorRef
get_symbolicator(void)164*2c2f96dcSApple OSS Distributions get_symbolicator(void)
165*2c2f96dcSApple OSS Distributions {
166*2c2f96dcSApple OSS Distributions 	static CSSymbolicatorRef user_symb;
167*2c2f96dcSApple OSS Distributions 	static dispatch_once_t expect_stack_once;
168*2c2f96dcSApple OSS Distributions 	dispatch_once(&expect_stack_once, ^{
169*2c2f96dcSApple OSS Distributions 		user_symb = CSSymbolicatorCreateWithTask(mach_task_self());
170*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_FALSE(CSIsNull(user_symb), NULL);
171*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_TRUE(CSSymbolicatorIsTaskValid(user_symb), NULL);
172*2c2f96dcSApple OSS Distributions 	});
173*2c2f96dcSApple OSS Distributions 	return user_symb;
174*2c2f96dcSApple OSS Distributions }
175*2c2f96dcSApple OSS Distributions 
176*2c2f96dcSApple OSS Distributions static void __attribute__((noinline, not_tail_called))
expect_callstack(enum test_scenario scenario)177*2c2f96dcSApple OSS Distributions expect_callstack(enum test_scenario scenario)
178*2c2f96dcSApple OSS Distributions {
179*2c2f96dcSApple OSS Distributions 	uint64_t bt[USER_FRAMES + MAX_SYSCALL_SETUP_FRAMES] = { 0 };
180*2c2f96dcSApple OSS Distributions 
181*2c2f96dcSApple OSS Distributions 	CSSymbolicatorRef user_symb = get_symbolicator();
182*2c2f96dcSApple OSS Distributions 	size_t bt_filled = USER_FRAMES + MAX_SYSCALL_SETUP_FRAMES;
183*2c2f96dcSApple OSS Distributions 	backtrace_current_thread_wrapper(scenario, bt, &bt_filled);
184*2c2f96dcSApple OSS Distributions 
185*2c2f96dcSApple OSS Distributions 	unsigned int bt_len = (unsigned int)bt_filled;
186*2c2f96dcSApple OSS Distributions 	T_EXPECT_GE(bt_len, (unsigned int)USER_FRAMES,
187*2c2f96dcSApple OSS Distributions 	    "at least %u frames should be present in backtrace", USER_FRAMES);
188*2c2f96dcSApple OSS Distributions 	T_EXPECT_LE(bt_len, (unsigned int)USER_FRAMES + MAX_SYSCALL_SETUP_FRAMES,
189*2c2f96dcSApple OSS Distributions 	    "at most %u frames should be present in backtrace",
190*2c2f96dcSApple OSS Distributions 	    USER_FRAMES + MAX_SYSCALL_SETUP_FRAMES);
191*2c2f96dcSApple OSS Distributions 
192*2c2f96dcSApple OSS Distributions 	struct callstack_exp callstack = {
193*2c2f96dcSApple OSS Distributions 		.in_syscall_setup = true,
194*2c2f96dcSApple OSS Distributions 		.syscall_frames = 0,
195*2c2f96dcSApple OSS Distributions 		.callstack = user_bt,
196*2c2f96dcSApple OSS Distributions 		.callstack_len = USER_FRAMES,
197*2c2f96dcSApple OSS Distributions 		.nchecked = 0,
198*2c2f96dcSApple OSS Distributions 	};
199*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < bt_len; i++) {
200*2c2f96dcSApple OSS Distributions 		uintptr_t addr;
201*2c2f96dcSApple OSS Distributions #if !defined(__LP64__)
202*2c2f96dcSApple OSS Distributions 		// Backtrace frames come out as kernel words; convert them back to user
203*2c2f96dcSApple OSS Distributions 		// uintptr_t for 32-bit processes.
204*2c2f96dcSApple OSS Distributions 		if (is_kernel_64_bit()) {
205*2c2f96dcSApple OSS Distributions 			addr = (uintptr_t)(bt[i]);
206*2c2f96dcSApple OSS Distributions 		} else {
207*2c2f96dcSApple OSS Distributions 			addr = (uintptr_t)(((uint32_t *)bt)[i]);
208*2c2f96dcSApple OSS Distributions 		}
209*2c2f96dcSApple OSS Distributions #else // defined(__LP32__)
210*2c2f96dcSApple OSS Distributions 		addr = (uintptr_t)bt[i];
211*2c2f96dcSApple OSS Distributions #endif // defined(__LP32__)
212*2c2f96dcSApple OSS Distributions 
213*2c2f96dcSApple OSS Distributions 		CSSymbolRef symbol = CSSymbolicatorGetSymbolWithAddressAtTime(
214*2c2f96dcSApple OSS Distributions 			user_symb, addr, kCSNow);
215*2c2f96dcSApple OSS Distributions 		expect_frame(&callstack, symbol, addr, i);
216*2c2f96dcSApple OSS Distributions 	}
217*2c2f96dcSApple OSS Distributions 
218*2c2f96dcSApple OSS Distributions 	T_EXPECT_GE(callstack.nchecked, USER_FRAMES,
219*2c2f96dcSApple OSS Distributions 	    "checked enough frames for correct symbols");
220*2c2f96dcSApple OSS Distributions }
221*2c2f96dcSApple OSS Distributions 
222*2c2f96dcSApple OSS Distributions static int __attribute__((noinline, not_tail_called))
223*2c2f96dcSApple OSS Distributions recurse_a(enum test_scenario, unsigned int frames);
224*2c2f96dcSApple OSS Distributions static int __attribute__((noinline, not_tail_called))
225*2c2f96dcSApple OSS Distributions recurse_b(enum test_scenario, unsigned int frames);
226*2c2f96dcSApple OSS Distributions 
227*2c2f96dcSApple OSS Distributions static int __attribute__((noinline, not_tail_called))
recurse_a(enum test_scenario scenario,unsigned int frames)228*2c2f96dcSApple OSS Distributions recurse_a(enum test_scenario scenario, unsigned int frames)
229*2c2f96dcSApple OSS Distributions {
230*2c2f96dcSApple OSS Distributions 	if (frames == 1) {
231*2c2f96dcSApple OSS Distributions 		expect_callstack(scenario);
232*2c2f96dcSApple OSS Distributions 		getpid(); // Really prevent tail calls.
233*2c2f96dcSApple OSS Distributions 		return 0;
234*2c2f96dcSApple OSS Distributions 	}
235*2c2f96dcSApple OSS Distributions 
236*2c2f96dcSApple OSS Distributions 	return recurse_b(scenario, frames - 1) + 1;
237*2c2f96dcSApple OSS Distributions }
238*2c2f96dcSApple OSS Distributions 
239*2c2f96dcSApple OSS Distributions static int __attribute__((noinline, not_tail_called))
recurse_b(enum test_scenario scenario,unsigned int frames)240*2c2f96dcSApple OSS Distributions recurse_b(enum test_scenario scenario, unsigned int frames)
241*2c2f96dcSApple OSS Distributions {
242*2c2f96dcSApple OSS Distributions 	if (frames == 1) {
243*2c2f96dcSApple OSS Distributions 		expect_callstack(scenario);
244*2c2f96dcSApple OSS Distributions 		getpid(); // Really prevent tail calls.
245*2c2f96dcSApple OSS Distributions 		return 0;
246*2c2f96dcSApple OSS Distributions 	}
247*2c2f96dcSApple OSS Distributions 
248*2c2f96dcSApple OSS Distributions 	return recurse_a(scenario, frames - 1) + 1;
249*2c2f96dcSApple OSS Distributions }
250*2c2f96dcSApple OSS Distributions 
251*2c2f96dcSApple OSS Distributions static void __attribute__((noinline, not_tail_called))
expect_async_callstack(void)252*2c2f96dcSApple OSS Distributions expect_async_callstack(void)
253*2c2f96dcSApple OSS Distributions {
254*2c2f96dcSApple OSS Distributions 	uint64_t bt[ASYNC_FRAMES + MAX_SYSCALL_SETUP_FRAMES] = { 0 };
255*2c2f96dcSApple OSS Distributions 
256*2c2f96dcSApple OSS Distributions 	CSSymbolicatorRef user_symb = get_symbolicator();
257*2c2f96dcSApple OSS Distributions 	size_t bt_filled = ASYNC_FRAMES + MAX_SYSCALL_SETUP_FRAMES;
258*2c2f96dcSApple OSS Distributions 	backtrace_current_thread_wrapper(USER_SCENARIO, bt, &bt_filled);
259*2c2f96dcSApple OSS Distributions 
260*2c2f96dcSApple OSS Distributions 	unsigned int bt_len = (unsigned int)bt_filled;
261*2c2f96dcSApple OSS Distributions 	T_EXPECT_GE(bt_len, (unsigned int)ASYNC_FRAMES,
262*2c2f96dcSApple OSS Distributions 	    "at least %u frames should be present in backtrace", ASYNC_FRAMES);
263*2c2f96dcSApple OSS Distributions 	T_EXPECT_LE(bt_len, (unsigned int)ASYNC_FRAMES + MAX_SYSCALL_SETUP_FRAMES,
264*2c2f96dcSApple OSS Distributions 	    "at most %u frames should be present in backtrace",
265*2c2f96dcSApple OSS Distributions 	    ASYNC_FRAMES + MAX_SYSCALL_SETUP_FRAMES);
266*2c2f96dcSApple OSS Distributions 
267*2c2f96dcSApple OSS Distributions 	struct callstack_exp callstack = {
268*2c2f96dcSApple OSS Distributions 		.in_syscall_setup = true,
269*2c2f96dcSApple OSS Distributions 		.syscall_frames = 0,
270*2c2f96dcSApple OSS Distributions 		.callstack = async_bt,
271*2c2f96dcSApple OSS Distributions 		.callstack_len = ASYNC_FRAMES,
272*2c2f96dcSApple OSS Distributions 		.nchecked = 0,
273*2c2f96dcSApple OSS Distributions 	};
274*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < bt_len; i++) {
275*2c2f96dcSApple OSS Distributions 		uintptr_t addr;
276*2c2f96dcSApple OSS Distributions #if !defined(__LP64__)
277*2c2f96dcSApple OSS Distributions 		// Backtrace frames come out as kernel words; convert them back to user
278*2c2f96dcSApple OSS Distributions 		// uintptr_t for 32-bit processes.
279*2c2f96dcSApple OSS Distributions 		if (is_kernel_64_bit()) {
280*2c2f96dcSApple OSS Distributions 			addr = (uintptr_t)(bt[i]);
281*2c2f96dcSApple OSS Distributions 		} else {
282*2c2f96dcSApple OSS Distributions 			addr = (uintptr_t)(((uint32_t *)bt)[i]);
283*2c2f96dcSApple OSS Distributions 		}
284*2c2f96dcSApple OSS Distributions #else // defined(__LP32__)
285*2c2f96dcSApple OSS Distributions 		addr = (uintptr_t)bt[i];
286*2c2f96dcSApple OSS Distributions #endif // defined(__LP32__)
287*2c2f96dcSApple OSS Distributions 
288*2c2f96dcSApple OSS Distributions 		CSSymbolRef symbol = CSSymbolicatorGetSymbolWithAddressAtTime(
289*2c2f96dcSApple OSS Distributions 			user_symb, addr, kCSNow);
290*2c2f96dcSApple OSS Distributions 		expect_frame(&callstack, symbol, addr, i);
291*2c2f96dcSApple OSS Distributions 	}
292*2c2f96dcSApple OSS Distributions 
293*2c2f96dcSApple OSS Distributions 	T_EXPECT_GE(callstack.nchecked, ASYNC_FRAMES,
294*2c2f96dcSApple OSS Distributions 	    "checked enough frames for correct symbols");
295*2c2f96dcSApple OSS Distributions }
296*2c2f96dcSApple OSS Distributions 
297*2c2f96dcSApple OSS Distributions static void *
backtrace_thread_async(void * __unused arg)298*2c2f96dcSApple OSS Distributions backtrace_thread_async(void * __unused arg)
299*2c2f96dcSApple OSS Distributions {
300*2c2f96dcSApple OSS Distributions 	uint64_t *fp = __builtin_frame_address(0);
301*2c2f96dcSApple OSS Distributions 	// We cannot use a variable of pointer type, because this ABI is valid
302*2c2f96dcSApple OSS Distributions 	// on arm64_32 where pointers are 32bits, but the context pointer will
303*2c2f96dcSApple OSS Distributions 	// still be stored in a 64bits slot on the stack.
304*2c2f96dcSApple OSS Distributions #if __has_feature(ptrauth_calls)
305*2c2f96dcSApple OSS Distributions #define __stack_context_auth __ptrauth(ptrauth_key_process_dependent_data, 1, \
306*2c2f96dcSApple OSS Distributions 	        0xc31a)
307*2c2f96dcSApple OSS Distributions 	struct fake_async_context * __stack_context_auth ctx = &level2;
308*2c2f96dcSApple OSS Distributions #else // __has_feature(ptrauth_calls)
309*2c2f96dcSApple OSS Distributions 	/* struct fake_async_context * */uint64_t ctx  = (uintptr_t)&level2;
310*2c2f96dcSApple OSS Distributions #endif // !__has_feature(ptrauth_calls)
311*2c2f96dcSApple OSS Distributions 
312*2c2f96dcSApple OSS Distributions 	// The signature of an async frame on the OS stack is:
313*2c2f96dcSApple OSS Distributions 	// [ <AsyncContext address>, <Saved FP | (1<<60)>, <return address> ]
314*2c2f96dcSApple OSS Distributions 	// The Async context must be right before the saved FP on the stack. This
315*2c2f96dcSApple OSS Distributions 	// should happen naturally in an optimized build as it is the only
316*2c2f96dcSApple OSS Distributions 	// variable on the stack.
317*2c2f96dcSApple OSS Distributions 	// This function cannot use T_ASSERT_* becuse it changes the stack
318*2c2f96dcSApple OSS Distributions 	// layout.
319*2c2f96dcSApple OSS Distributions 	assert((uintptr_t)fp - (uintptr_t)&ctx == 8);
320*2c2f96dcSApple OSS Distributions 
321*2c2f96dcSApple OSS Distributions 	// Modify the saved FP on the stack to include the async frame marker
322*2c2f96dcSApple OSS Distributions 	*fp |= (0x1ULL << 60);
323*2c2f96dcSApple OSS Distributions 	expect_async_callstack();
324*2c2f96dcSApple OSS Distributions 	return NULL;
325*2c2f96dcSApple OSS Distributions }
326*2c2f96dcSApple OSS Distributions 
327*2c2f96dcSApple OSS Distributions static void *
backtrace_thread(void * arg)328*2c2f96dcSApple OSS Distributions backtrace_thread(void *arg)
329*2c2f96dcSApple OSS Distributions {
330*2c2f96dcSApple OSS Distributions 	unsigned int calls;
331*2c2f96dcSApple OSS Distributions 	enum test_scenario scenario = (enum test_scenario)arg;
332*2c2f96dcSApple OSS Distributions 
333*2c2f96dcSApple OSS Distributions 	// backtrace_thread, recurse_a, recurse_b, ..., __sysctlbyname
334*2c2f96dcSApple OSS Distributions 	//
335*2c2f96dcSApple OSS Distributions 	// Always make one less call for this frame (backtrace_thread).
336*2c2f96dcSApple OSS Distributions 	calls = USER_FRAMES - NON_RECURSE_FRAMES;
337*2c2f96dcSApple OSS Distributions 
338*2c2f96dcSApple OSS Distributions 	T_LOG("backtrace thread calling into %d frames (already at %d frames)",
339*2c2f96dcSApple OSS Distributions 	    calls, NON_RECURSE_FRAMES);
340*2c2f96dcSApple OSS Distributions 	(void)recurse_a(scenario, calls);
341*2c2f96dcSApple OSS Distributions 	return NULL;
342*2c2f96dcSApple OSS Distributions }
343*2c2f96dcSApple OSS Distributions 
344*2c2f96dcSApple OSS Distributions T_DECL(backtrace_user, "test that the kernel can backtrace user stacks",
345*2c2f96dcSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true))
346*2c2f96dcSApple OSS Distributions {
347*2c2f96dcSApple OSS Distributions 	pthread_t thread;
348*2c2f96dcSApple OSS Distributions 
349*2c2f96dcSApple OSS Distributions 	// Run the test from a different thread to insulate it from libdarwintest
350*2c2f96dcSApple OSS Distributions 	// setup.
351*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&thread, NULL, backtrace_thread,
352*2c2f96dcSApple OSS Distributions 	    (void *)USER_SCENARIO), "create additional thread to backtrace");
353*2c2f96dcSApple OSS Distributions 
354*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL);
355*2c2f96dcSApple OSS Distributions }
356*2c2f96dcSApple OSS Distributions 
357*2c2f96dcSApple OSS Distributions T_DECL(backtrace_user_bounds,
358*2c2f96dcSApple OSS Distributions     "test that the kernel doesn't write frames out of expected bounds")
359*2c2f96dcSApple OSS Distributions {
360*2c2f96dcSApple OSS Distributions 	uint64_t bt_init[USER_FRAMES] = {};
361*2c2f96dcSApple OSS Distributions 	size_t bt_filled = USER_FRAMES, bt_filled_after = 0;
362*2c2f96dcSApple OSS Distributions 	int error = 0;
363*2c2f96dcSApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
364*2c2f96dcSApple OSS Distributions 	void *bt_page = NULL;
365*2c2f96dcSApple OSS Distributions 	void *guard_page = NULL;
366*2c2f96dcSApple OSS Distributions 	void *bt_start = NULL;
367*2c2f96dcSApple OSS Distributions 
368*2c2f96dcSApple OSS Distributions 	// The backtrace addresses come back as kernel words.
369*2c2f96dcSApple OSS Distributions 	size_t kword_size = is_kernel_64_bit() ? 8 : 4;
370*2c2f96dcSApple OSS Distributions 
371*2c2f96dcSApple OSS Distributions 	// Get an idea of how many frames to expect.
372*2c2f96dcSApple OSS Distributions 	int ret = sysctlbyname("kern.backtrace.user", bt_init, &bt_filled, NULL, 0);
373*2c2f96dcSApple OSS Distributions 	if (ret == -1 && errno == ENOENT) {
374*2c2f96dcSApple OSS Distributions 		T_SKIP("release kernel: kern.backtrace.user missing");
375*2c2f96dcSApple OSS Distributions 	}
376*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(error, "sysctlbyname(\"kern.backtrace.user\")");
377*2c2f96dcSApple OSS Distributions 
378*2c2f96dcSApple OSS Distributions 	// Allocate two pages -- a first one that's valid and a second that
379*2c2f96dcSApple OSS Distributions 	// will be non-writeable to catch a copyout that's too large.
380*2c2f96dcSApple OSS Distributions 	bt_page = mmap(NULL, vm_page_size * 2, PROT_READ | PROT_WRITE,
381*2c2f96dcSApple OSS Distributions 	    MAP_ANON | MAP_PRIVATE, -1, 0);
382*2c2f96dcSApple OSS Distributions 	T_WITH_ERRNO;
383*2c2f96dcSApple OSS Distributions 	T_ASSERT_NE(bt_page, MAP_FAILED, "allocated backtrace pages");
384*2c2f96dcSApple OSS Distributions 	guard_page = (char *)bt_page + vm_page_size;
385*2c2f96dcSApple OSS Distributions 
386*2c2f96dcSApple OSS Distributions 	error = mprotect(guard_page, vm_page_size, PROT_READ);
387*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(error, "mprotect(..., PROT_READ) guard page");
388*2c2f96dcSApple OSS Distributions 
389*2c2f96dcSApple OSS Distributions 	// Ensure the pages are set up as expected.
390*2c2f96dcSApple OSS Distributions 	kr = vm_write(mach_task_self(), (vm_address_t)bt_page,
391*2c2f96dcSApple OSS Distributions 	    (vm_offset_t)&(int){ 12345 }, sizeof(int));
392*2c2f96dcSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr,
393*2c2f96dcSApple OSS Distributions 	    "should succeed in writing to backtrace page");
394*2c2f96dcSApple OSS Distributions 	kr = vm_write(mach_task_self(), (vm_address_t)guard_page,
395*2c2f96dcSApple OSS Distributions 	    (vm_offset_t)&(int){ 12345 }, sizeof(int));
396*2c2f96dcSApple OSS Distributions 	T_ASSERT_NE(kr, KERN_SUCCESS, "should fail to write to guard page");
397*2c2f96dcSApple OSS Distributions 
398*2c2f96dcSApple OSS Distributions 	// Ask the kernel to write the backtrace just before the guard page.
399*2c2f96dcSApple OSS Distributions 	bt_start = (char *)guard_page - (kword_size * bt_filled);
400*2c2f96dcSApple OSS Distributions 	bt_filled_after = bt_filled;
401*2c2f96dcSApple OSS Distributions 
402*2c2f96dcSApple OSS Distributions 	error = sysctlbyname("kern.backtrace.user", bt_start, &bt_filled_after,
403*2c2f96dcSApple OSS Distributions 	    NULL, 0);
404*2c2f96dcSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(error,
405*2c2f96dcSApple OSS Distributions 	    "sysctlbyname(\"kern.backtrace.user\") just before guard page");
406*2c2f96dcSApple OSS Distributions 	T_EXPECT_EQ(bt_filled, bt_filled_after,
407*2c2f96dcSApple OSS Distributions 	    "both calls to backtrace should have filled in the same number of "
408*2c2f96dcSApple OSS Distributions 	    "frames");
409*2c2f96dcSApple OSS Distributions 
410*2c2f96dcSApple OSS Distributions 	// Expect the kernel to fault when writing too far.
411*2c2f96dcSApple OSS Distributions 	bt_start = (char *)bt_start + 1;
412*2c2f96dcSApple OSS Distributions 	bt_filled_after = bt_filled;
413*2c2f96dcSApple OSS Distributions 	error = sysctlbyname("kern.backtrace.user", bt_start, &bt_filled_after,
414*2c2f96dcSApple OSS Distributions 	    (void *)USER_SCENARIO, 0);
415*2c2f96dcSApple OSS Distributions 	T_EXPECT_POSIX_FAILURE(error, EFAULT,
416*2c2f96dcSApple OSS Distributions 	    "sysctlbyname(\"kern.backtrace.user\") should fault one byte into "
417*2c2f96dcSApple OSS Distributions 	    "guard page");
418*2c2f96dcSApple OSS Distributions }
419*2c2f96dcSApple OSS Distributions 
420*2c2f96dcSApple OSS Distributions T_DECL(backtrace_user_async,
421*2c2f96dcSApple OSS Distributions     "test that the kernel can backtrace user async stacks",
422*2c2f96dcSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(false))
423*2c2f96dcSApple OSS Distributions {
424*2c2f96dcSApple OSS Distributions #if !defined(__LP64__)
425*2c2f96dcSApple OSS Distributions 	T_SKIP("unsupported on LP32");
426*2c2f96dcSApple OSS Distributions #else // __LP32__
427*2c2f96dcSApple OSS Distributions 	pthread_t thread;
428*2c2f96dcSApple OSS Distributions 	// Run the test from a different thread to insulate it from libdarwintest
429*2c2f96dcSApple OSS Distributions 	// setup.
430*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&thread, NULL,
431*2c2f96dcSApple OSS Distributions 	    backtrace_thread_async, NULL),
432*2c2f96dcSApple OSS Distributions 	    "create additional thread to backtrace");
433*2c2f96dcSApple OSS Distributions 
434*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL);
435*2c2f96dcSApple OSS Distributions #endif // !__LP32__
436*2c2f96dcSApple OSS Distributions }
437*2c2f96dcSApple OSS Distributions 
438*2c2f96dcSApple OSS Distributions T_DECL(backtrace_user_resume,
439*2c2f96dcSApple OSS Distributions     "test that the kernel can resume a backtrace into a smaller buffer",
440*2c2f96dcSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(false))
441*2c2f96dcSApple OSS Distributions {
442*2c2f96dcSApple OSS Distributions 	pthread_t thread;
443*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&thread, NULL, backtrace_thread,
444*2c2f96dcSApple OSS Distributions 	    (void *)RESUME_SCENARIO), "create additional thread to backtrace");
445*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL);
446*2c2f96dcSApple OSS Distributions }
447*2c2f96dcSApple OSS Distributions 
448*2c2f96dcSApple OSS Distributions T_DECL(backtrace_kernel_pack_unpack,
449*2c2f96dcSApple OSS Distributions     "test that a kernel backtrace can be packed and unpacked losslessly",
450*2c2f96dcSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(false))
451*2c2f96dcSApple OSS Distributions {
452*2c2f96dcSApple OSS Distributions 	int error = sysctlbyname("kern.backtrace.kernel_tests", NULL, NULL,
453*2c2f96dcSApple OSS Distributions 	    (void *)PACK_UNPACK_SCENARIO, 0);
454*2c2f96dcSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(error,
455*2c2f96dcSApple OSS Distributions 	    "sysctlbyname(\"kern.backtrace.kernel_tests\", PACK_UNPACK)");
456*2c2f96dcSApple OSS Distributions }
457*2c2f96dcSApple OSS Distributions 
458*2c2f96dcSApple OSS Distributions T_DECL(backtrace_kernel_packed,
459*2c2f96dcSApple OSS Distributions     "test that a kernel backtrace can be recorded as packed losslessly",
460*2c2f96dcSApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(false))
461*2c2f96dcSApple OSS Distributions {
462*2c2f96dcSApple OSS Distributions 	int error = sysctlbyname("kern.backtrace.kernel_tests", NULL, NULL,
463*2c2f96dcSApple OSS Distributions 	    (void *)PACKED_SCENARIO, 0);
464*2c2f96dcSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(error,
465*2c2f96dcSApple OSS Distributions 	    "sysctlbyname(\"kern.backtrace.kernel_tests\", PACKED)");
466*2c2f96dcSApple OSS Distributions }
467*2c2f96dcSApple OSS Distributions 
468*2c2f96dcSApple OSS Distributions #pragma mark - utilities
469*2c2f96dcSApple OSS Distributions 
470*2c2f96dcSApple OSS Distributions static void __attribute__((noinline, not_tail_called))
spin_forever(void)471*2c2f96dcSApple OSS Distributions spin_forever(void)
472*2c2f96dcSApple OSS Distributions {
473*2c2f96dcSApple OSS Distributions 	while (true) {
474*2c2f96dcSApple OSS Distributions 		;
475*2c2f96dcSApple OSS Distributions 	}
476*2c2f96dcSApple OSS Distributions }
477*2c2f96dcSApple OSS Distributions 
478*2c2f96dcSApple OSS Distributions static void
check_stack(uintptr_t fp,uintptr_t ctx)479*2c2f96dcSApple OSS Distributions check_stack(uintptr_t fp, uintptr_t ctx)
480*2c2f96dcSApple OSS Distributions {
481*2c2f96dcSApple OSS Distributions 	if ((fp - ctx) != 0x8) {
482*2c2f96dcSApple OSS Distributions 		fprintf(stderr, "stack frame is not set up properly: "
483*2c2f96dcSApple OSS Distributions 		    "%#lx, %#lx is %lx bytes away\n", fp, ctx, fp - ctx);
484*2c2f96dcSApple OSS Distributions 		exit(1);
485*2c2f96dcSApple OSS Distributions 	}
486*2c2f96dcSApple OSS Distributions }
487*2c2f96dcSApple OSS Distributions 
488*2c2f96dcSApple OSS Distributions static void __attribute__((noinline, not_tail_called))
spin_backtrace_async(void)489*2c2f96dcSApple OSS Distributions spin_backtrace_async(void)
490*2c2f96dcSApple OSS Distributions {
491*2c2f96dcSApple OSS Distributions 	uint64_t *fp = __builtin_frame_address(0);
492*2c2f96dcSApple OSS Distributions #if __has_feature(ptrauth_calls)
493*2c2f96dcSApple OSS Distributions 	struct fake_async_context * __stack_context_auth ctx = &level2;
494*2c2f96dcSApple OSS Distributions #else // __has_feature(ptrauth_calls)
495*2c2f96dcSApple OSS Distributions 	/* struct fake_async_context * */uint64_t ctx  = (uintptr_t)&level2;
496*2c2f96dcSApple OSS Distributions #endif // !__has_feature(ptrauth_calls)
497*2c2f96dcSApple OSS Distributions 	check_stack((uintptr_t)fp, (uintptr_t)&ctx);
498*2c2f96dcSApple OSS Distributions 	*fp |= (0x1ULL << 60);
499*2c2f96dcSApple OSS Distributions 
500*2c2f96dcSApple OSS Distributions 	spin_forever();
501*2c2f96dcSApple OSS Distributions }
502*2c2f96dcSApple OSS Distributions 
503*2c2f96dcSApple OSS Distributions T_DECL(backtrace_user_async_spin_forever,
504*2c2f96dcSApple OSS Distributions     "try spinning forever with an async call stack set up",
505*2c2f96dcSApple OSS Distributions     T_META_ENABLED(false), T_META_CHECK_LEAKS(false),
506*2c2f96dcSApple OSS Distributions     T_META_ALL_VALID_ARCHS(false))
507*2c2f96dcSApple OSS Distributions {
508*2c2f96dcSApple OSS Distributions #if !defined(__LP64__)
509*2c2f96dcSApple OSS Distributions 	T_SKIP("unsupported on LP32");
510*2c2f96dcSApple OSS Distributions #else // __LP32__
511*2c2f96dcSApple OSS Distributions 	spin_backtrace_async();
512*2c2f96dcSApple OSS Distributions #endif // !__LP32__
513*2c2f96dcSApple OSS Distributions }
514