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