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