xref: /xnu-10063.101.15/tests/hw_breakpoint_step_arm64.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions #ifdef T_NAMESPACE
2*94d3b452SApple OSS Distributions #undef T_NAMESPACE
3*94d3b452SApple OSS Distributions #endif
4*94d3b452SApple OSS Distributions 
5*94d3b452SApple OSS Distributions #include <mach/arm/thread_status.h>
6*94d3b452SApple OSS Distributions #include <mach/mach_traps.h>
7*94d3b452SApple OSS Distributions #include <mach-o/dyld.h>
8*94d3b452SApple OSS Distributions #include <mach/mach.h>
9*94d3b452SApple OSS Distributions #include <mach/task.h>
10*94d3b452SApple OSS Distributions 
11*94d3b452SApple OSS Distributions #include <darwintest.h>
12*94d3b452SApple OSS Distributions #include <dispatch/dispatch.h>
13*94d3b452SApple OSS Distributions #include <stdlib.h>
14*94d3b452SApple OSS Distributions 
15*94d3b452SApple OSS Distributions #include <signal.h>
16*94d3b452SApple OSS Distributions #include <spawn.h>
17*94d3b452SApple OSS Distributions #include <spawn_private.h>
18*94d3b452SApple OSS Distributions #include <stdatomic.h>
19*94d3b452SApple OSS Distributions 
20*94d3b452SApple OSS Distributions #include <excserver.h>
21*94d3b452SApple OSS Distributions #include <sys/syslimits.h>
22*94d3b452SApple OSS Distributions 
23*94d3b452SApple OSS Distributions #define SYNC_TIMEOUT dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC)
24*94d3b452SApple OSS Distributions 
25*94d3b452SApple OSS Distributions static dispatch_semaphore_t sync_sema;
26*94d3b452SApple OSS Distributions static _Atomic bool after_kill;
27*94d3b452SApple OSS Distributions 
28*94d3b452SApple OSS Distributions kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count)29*94d3b452SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
30*94d3b452SApple OSS Distributions     mach_port_t thread,
31*94d3b452SApple OSS Distributions     mach_port_t task,
32*94d3b452SApple OSS Distributions     exception_type_t exception,
33*94d3b452SApple OSS Distributions     mach_exception_data_t code,
34*94d3b452SApple OSS Distributions     mach_msg_type_number_t code_count)
35*94d3b452SApple OSS Distributions {
36*94d3b452SApple OSS Distributions #pragma unused(exception_port, thread, task, code, code_count)
37*94d3b452SApple OSS Distributions 	if (exception == EXC_BREAKPOINT || (exception == EXC_CRASH && atomic_load_explicit(&after_kill,
38*94d3b452SApple OSS Distributions 	    memory_order_seq_cst))) {
39*94d3b452SApple OSS Distributions 		T_LOG("Received exception %d", exception);
40*94d3b452SApple OSS Distributions 		dispatch_semaphore_signal(sync_sema);
41*94d3b452SApple OSS Distributions 		return KERN_SUCCESS;
42*94d3b452SApple OSS Distributions 	}
43*94d3b452SApple OSS Distributions 
44*94d3b452SApple OSS Distributions 	T_FAIL("invalid exception type: %d", exception);
45*94d3b452SApple OSS Distributions 
46*94d3b452SApple OSS Distributions 	return KERN_FAILURE;
47*94d3b452SApple OSS Distributions }
48*94d3b452SApple OSS Distributions 
49*94d3b452SApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t exception,const mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,const thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)50*94d3b452SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
51*94d3b452SApple OSS Distributions     exception_type_t exception,
52*94d3b452SApple OSS Distributions     const mach_exception_data_t code,
53*94d3b452SApple OSS Distributions     mach_msg_type_number_t code_count,
54*94d3b452SApple OSS Distributions     int * flavor,
55*94d3b452SApple OSS Distributions     const thread_state_t old_state,
56*94d3b452SApple OSS Distributions     mach_msg_type_number_t old_state_count,
57*94d3b452SApple OSS Distributions     thread_state_t new_state,
58*94d3b452SApple OSS Distributions     mach_msg_type_number_t * new_state_count)
59*94d3b452SApple OSS Distributions {
60*94d3b452SApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
61*94d3b452SApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state");
62*94d3b452SApple OSS Distributions 	return KERN_NOT_SUPPORTED;
63*94d3b452SApple OSS Distributions }
64*94d3b452SApple OSS Distributions 
65*94d3b452SApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)66*94d3b452SApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
67*94d3b452SApple OSS Distributions     mach_port_t thread,
68*94d3b452SApple OSS Distributions     mach_port_t task,
69*94d3b452SApple OSS Distributions     exception_type_t exception,
70*94d3b452SApple OSS Distributions     mach_exception_data_t code,
71*94d3b452SApple OSS Distributions     mach_msg_type_number_t code_count,
72*94d3b452SApple OSS Distributions     int * flavor,
73*94d3b452SApple OSS Distributions     thread_state_t old_state,
74*94d3b452SApple OSS Distributions     mach_msg_type_number_t old_state_count,
75*94d3b452SApple OSS Distributions     thread_state_t new_state,
76*94d3b452SApple OSS Distributions     mach_msg_type_number_t * new_state_count)
77*94d3b452SApple OSS Distributions {
78*94d3b452SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
79*94d3b452SApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
80*94d3b452SApple OSS Distributions 	return KERN_NOT_SUPPORTED;
81*94d3b452SApple OSS Distributions }
82*94d3b452SApple OSS Distributions 
83*94d3b452SApple OSS Distributions static void *
exc_handler(void * arg)84*94d3b452SApple OSS Distributions exc_handler(void * arg)
85*94d3b452SApple OSS Distributions {
86*94d3b452SApple OSS Distributions #pragma unused(arg)
87*94d3b452SApple OSS Distributions 	kern_return_t kret;
88*94d3b452SApple OSS Distributions 	mach_port_t exception_port;
89*94d3b452SApple OSS Distributions 
90*94d3b452SApple OSS Distributions 	kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exception_port);
91*94d3b452SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
92*94d3b452SApple OSS Distributions 		T_FAIL("mach_port_allocate: %s (%d)", mach_error_string(kret), kret);
93*94d3b452SApple OSS Distributions 	}
94*94d3b452SApple OSS Distributions 
95*94d3b452SApple OSS Distributions 	kret = mach_port_insert_right(mach_task_self(), exception_port, exception_port, MACH_MSG_TYPE_MAKE_SEND);
96*94d3b452SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
97*94d3b452SApple OSS Distributions 		T_FAIL("mach_port_insert_right: %s (%d)", mach_error_string(kret), kret);
98*94d3b452SApple OSS Distributions 	}
99*94d3b452SApple OSS Distributions 
100*94d3b452SApple OSS Distributions 	kret = task_set_exception_ports(mach_task_self(), EXC_MASK_CRASH | EXC_MASK_BREAKPOINT, exception_port,
101*94d3b452SApple OSS Distributions 	    (exception_behavior_t)(EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 0);
102*94d3b452SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
103*94d3b452SApple OSS Distributions 		T_FAIL("task_set_exception_ports: %s (%d)", mach_error_string(kret), kret);
104*94d3b452SApple OSS Distributions 	}
105*94d3b452SApple OSS Distributions 
106*94d3b452SApple OSS Distributions 	dispatch_semaphore_signal(sync_sema);
107*94d3b452SApple OSS Distributions 
108*94d3b452SApple OSS Distributions 	kret = mach_msg_server(mach_exc_server, MACH_MSG_SIZE_RELIABLE, exception_port, 0);
109*94d3b452SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
110*94d3b452SApple OSS Distributions 		T_FAIL("mach_msg_server: %s (%d)", mach_error_string(kret), kret);
111*94d3b452SApple OSS Distributions 	}
112*94d3b452SApple OSS Distributions 
113*94d3b452SApple OSS Distributions 	return NULL;
114*94d3b452SApple OSS Distributions }
115*94d3b452SApple OSS Distributions 
116*94d3b452SApple OSS Distributions T_HELPER_DECL(hw_breakpoint_helper, "hw_breakpoint_helper")
117*94d3b452SApple OSS Distributions {
118*94d3b452SApple OSS Distributions 	while (1) {
119*94d3b452SApple OSS Distributions 		sleep(1);
120*94d3b452SApple OSS Distributions 	}
121*94d3b452SApple OSS Distributions }
122*94d3b452SApple OSS Distributions 
123*94d3b452SApple OSS Distributions // Single instruction step
124*94d3b452SApple OSS Distributions // (SS bit in the MDSCR_EL1 register)
125*94d3b452SApple OSS Distributions #define SS_ENABLE ((uint32_t)(1u))
126*94d3b452SApple OSS Distributions 
127*94d3b452SApple OSS Distributions static void
step_thread(mach_port_name_t task,thread_t thread)128*94d3b452SApple OSS Distributions step_thread(mach_port_name_t task, thread_t thread)
129*94d3b452SApple OSS Distributions {
130*94d3b452SApple OSS Distributions 	kern_return_t kr;
131*94d3b452SApple OSS Distributions 
132*94d3b452SApple OSS Distributions 	arm_debug_state64_t dbg;
133*94d3b452SApple OSS Distributions 	mach_msg_type_number_t count = ARM_DEBUG_STATE64_COUNT;
134*94d3b452SApple OSS Distributions 
135*94d3b452SApple OSS Distributions 	kr = thread_get_state(thread, ARM_DEBUG_STATE64,
136*94d3b452SApple OSS Distributions 	    (thread_state_t)&dbg, &count);
137*94d3b452SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "get debug state for target thread");
138*94d3b452SApple OSS Distributions 
139*94d3b452SApple OSS Distributions 	dbg.__mdscr_el1 |= SS_ENABLE;
140*94d3b452SApple OSS Distributions 
141*94d3b452SApple OSS Distributions 	kr = thread_set_state(thread, ARM_DEBUG_STATE64,
142*94d3b452SApple OSS Distributions 	    (thread_state_t)&dbg, count);
143*94d3b452SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "set debug state for target thread");
144*94d3b452SApple OSS Distributions 
145*94d3b452SApple OSS Distributions 	kr = task_resume(task);
146*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "resume target task");
147*94d3b452SApple OSS Distributions 
148*94d3b452SApple OSS Distributions 	long err = dispatch_semaphore_wait(sync_sema, SYNC_TIMEOUT);
149*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(err, 0L, "dispatch_semaphore_wait timeout");
150*94d3b452SApple OSS Distributions }
151*94d3b452SApple OSS Distributions 
152*94d3b452SApple OSS Distributions T_DECL(hw_breakpoint_step, "Ensures that a process can be single-stepped using thread_set_state / ARM_DEBUG_STATE64", T_META_ASROOT(true),
153*94d3b452SApple OSS Distributions     T_META_OWNER("Samuel Lepetit <[email protected]>"))
154*94d3b452SApple OSS Distributions {
155*94d3b452SApple OSS Distributions 	kern_return_t kr;
156*94d3b452SApple OSS Distributions 	pthread_t handle_thread;
157*94d3b452SApple OSS Distributions 	sync_sema = dispatch_semaphore_create(0);
158*94d3b452SApple OSS Distributions 
159*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&handle_thread, NULL, exc_handler, NULL), "pthread_create");
160*94d3b452SApple OSS Distributions 	long err = dispatch_semaphore_wait(sync_sema, SYNC_TIMEOUT);
161*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(err, 0L, "dispatch_semaphore_wait timeout");
162*94d3b452SApple OSS Distributions 
163*94d3b452SApple OSS Distributions 	pid_t pid;
164*94d3b452SApple OSS Distributions 	char path[PATH_MAX];
165*94d3b452SApple OSS Distributions 	uint32_t path_size = sizeof(path);
166*94d3b452SApple OSS Distributions 
167*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
168*94d3b452SApple OSS Distributions 
169*94d3b452SApple OSS Distributions 	char *args[] = { path, "-n", "hw_breakpoint_helper", NULL };
170*94d3b452SApple OSS Distributions 	T_EXPECT_POSIX_ZERO(posix_spawn(&pid, args[0], NULL, NULL, args, NULL), "posix_spawn helper");
171*94d3b452SApple OSS Distributions 
172*94d3b452SApple OSS Distributions 	mach_port_name_t task;
173*94d3b452SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), pid, &task);
174*94d3b452SApple OSS Distributions 	T_ASSERT_TRUE(kr == KERN_SUCCESS, "task_for_pid");
175*94d3b452SApple OSS Distributions 
176*94d3b452SApple OSS Distributions 	kr = task_suspend(task);
177*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(kr == KERN_SUCCESS, "task_suspend");
178*94d3b452SApple OSS Distributions 
179*94d3b452SApple OSS Distributions 	thread_array_t threads = NULL;
180*94d3b452SApple OSS Distributions 	mach_msg_type_number_t thread_count;
181*94d3b452SApple OSS Distributions 	kr = task_threads(task, &threads, &thread_count);
182*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_threads");
183*94d3b452SApple OSS Distributions 
184*94d3b452SApple OSS Distributions 	step_thread(task, threads[0]);
185*94d3b452SApple OSS Distributions 
186*94d3b452SApple OSS Distributions 	kr = task_suspend(task);
187*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(kr == KERN_SUCCESS, "task_suspend");
188*94d3b452SApple OSS Distributions 
189*94d3b452SApple OSS Distributions 	step_thread(task, threads[0]);
190*94d3b452SApple OSS Distributions 
191*94d3b452SApple OSS Distributions 	atomic_store_explicit(&after_kill, 1, memory_order_seq_cst);
192*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(kill(pid, SIGKILL), "kill target process");
193*94d3b452SApple OSS Distributions }
194