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