xref: /xnu-8020.140.41/tests/exc_resource_threads.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions #ifdef T_NAMESPACE
2*27b03b36SApple OSS Distributions #undef T_NAMESPACE
3*27b03b36SApple OSS Distributions #endif
4*27b03b36SApple OSS Distributions 
5*27b03b36SApple OSS Distributions 
6*27b03b36SApple OSS Distributions #include <darwintest.h>
7*27b03b36SApple OSS Distributions #include <dispatch/dispatch.h>
8*27b03b36SApple OSS Distributions #include <stdlib.h>
9*27b03b36SApple OSS Distributions #include <spawn.h>
10*27b03b36SApple OSS Distributions #include <spawn_private.h>
11*27b03b36SApple OSS Distributions 
12*27b03b36SApple OSS Distributions #include <mach-o/dyld.h>
13*27b03b36SApple OSS Distributions #include <mach/mach.h>
14*27b03b36SApple OSS Distributions #include <mach/task.h>
15*27b03b36SApple OSS Distributions 
16*27b03b36SApple OSS Distributions #include <signal.h>
17*27b03b36SApple OSS Distributions #include <sys/sysctl.h>
18*27b03b36SApple OSS Distributions #include <sys/syslimits.h>
19*27b03b36SApple OSS Distributions 
20*27b03b36SApple OSS Distributions #include <excserver.h>
21*27b03b36SApple OSS Distributions 
22*27b03b36SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
23*27b03b36SApple OSS Distributions 
24*27b03b36SApple OSS Distributions static dispatch_semaphore_t sync_sema;
25*27b03b36SApple OSS Distributions 
26*27b03b36SApple 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)27*27b03b36SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
28*27b03b36SApple OSS Distributions     mach_port_t thread,
29*27b03b36SApple OSS Distributions     mach_port_t task,
30*27b03b36SApple OSS Distributions     exception_type_t exception,
31*27b03b36SApple OSS Distributions     mach_exception_data_t code,
32*27b03b36SApple OSS Distributions     mach_msg_type_number_t code_count)
33*27b03b36SApple OSS Distributions {
34*27b03b36SApple OSS Distributions #pragma unused(exception_port, thread, task, code, code_count)
35*27b03b36SApple OSS Distributions 	pid_t pid;
36*27b03b36SApple OSS Distributions 	pid_for_task(task, &pid);
37*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(exception, EXC_CORPSE_NOTIFY, "exception type");
38*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(kill(pid, SIGKILL), "kill");
39*27b03b36SApple OSS Distributions 	dispatch_semaphore_signal(sync_sema);
40*27b03b36SApple OSS Distributions 	return KERN_SUCCESS;
41*27b03b36SApple OSS Distributions }
42*27b03b36SApple OSS Distributions 
43*27b03b36SApple 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)44*27b03b36SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
45*27b03b36SApple OSS Distributions     exception_type_t exception,
46*27b03b36SApple OSS Distributions     const mach_exception_data_t code,
47*27b03b36SApple OSS Distributions     mach_msg_type_number_t code_count,
48*27b03b36SApple OSS Distributions     int * flavor,
49*27b03b36SApple OSS Distributions     const thread_state_t old_state,
50*27b03b36SApple OSS Distributions     mach_msg_type_number_t old_state_count,
51*27b03b36SApple OSS Distributions     thread_state_t new_state,
52*27b03b36SApple OSS Distributions     mach_msg_type_number_t * new_state_count)
53*27b03b36SApple OSS Distributions {
54*27b03b36SApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
55*27b03b36SApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state");
56*27b03b36SApple OSS Distributions 	return KERN_NOT_SUPPORTED;
57*27b03b36SApple OSS Distributions }
58*27b03b36SApple OSS Distributions 
59*27b03b36SApple 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)60*27b03b36SApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
61*27b03b36SApple OSS Distributions     mach_port_t thread,
62*27b03b36SApple OSS Distributions     mach_port_t task,
63*27b03b36SApple OSS Distributions     exception_type_t exception,
64*27b03b36SApple OSS Distributions     mach_exception_data_t code,
65*27b03b36SApple OSS Distributions     mach_msg_type_number_t code_count,
66*27b03b36SApple OSS Distributions     int * flavor,
67*27b03b36SApple OSS Distributions     thread_state_t old_state,
68*27b03b36SApple OSS Distributions     mach_msg_type_number_t old_state_count,
69*27b03b36SApple OSS Distributions     thread_state_t new_state,
70*27b03b36SApple OSS Distributions     mach_msg_type_number_t * new_state_count)
71*27b03b36SApple OSS Distributions {
72*27b03b36SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
73*27b03b36SApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
74*27b03b36SApple OSS Distributions 	return KERN_NOT_SUPPORTED;
75*27b03b36SApple OSS Distributions }
76*27b03b36SApple OSS Distributions 
77*27b03b36SApple OSS Distributions 
78*27b03b36SApple OSS Distributions /*
79*27b03b36SApple OSS Distributions  * setup exception handling port for EXC_CORPSE_NOTIFY.
80*27b03b36SApple OSS Distributions  * runs mach_msg_server once for receiving exception messages from kernel.
81*27b03b36SApple OSS Distributions  */
82*27b03b36SApple OSS Distributions static void *
exc_handler(void * arg)83*27b03b36SApple OSS Distributions exc_handler(void * arg)
84*27b03b36SApple OSS Distributions {
85*27b03b36SApple OSS Distributions #pragma unused(arg)
86*27b03b36SApple OSS Distributions 	kern_return_t kret;
87*27b03b36SApple OSS Distributions 	mach_port_t exception_port;
88*27b03b36SApple OSS Distributions 
89*27b03b36SApple OSS Distributions 	kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exception_port);
90*27b03b36SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
91*27b03b36SApple OSS Distributions 		T_FAIL("mach_port_allocate: %s (%d)", mach_error_string(kret), kret);
92*27b03b36SApple OSS Distributions 	}
93*27b03b36SApple OSS Distributions 
94*27b03b36SApple OSS Distributions 	kret = mach_port_insert_right(mach_task_self(), exception_port, exception_port, MACH_MSG_TYPE_MAKE_SEND);
95*27b03b36SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
96*27b03b36SApple OSS Distributions 		T_FAIL("mach_port_insert_right: %s (%d)", mach_error_string(kret), kret);
97*27b03b36SApple OSS Distributions 	}
98*27b03b36SApple OSS Distributions 
99*27b03b36SApple OSS Distributions 	kret = task_set_exception_ports(mach_task_self(), EXC_MASK_CRASH | EXC_MASK_CORPSE_NOTIFY, exception_port,
100*27b03b36SApple OSS Distributions 	    (exception_behavior_t)(EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 0);
101*27b03b36SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
102*27b03b36SApple OSS Distributions 		T_FAIL("task_set_exception_ports: %s (%d)", mach_error_string(kret), kret);
103*27b03b36SApple OSS Distributions 	}
104*27b03b36SApple OSS Distributions 
105*27b03b36SApple OSS Distributions 	dispatch_semaphore_signal(sync_sema);
106*27b03b36SApple OSS Distributions 
107*27b03b36SApple OSS Distributions 	kret = mach_msg_server(mach_exc_server, MACH_MSG_SIZE_RELIABLE, exception_port, 0);
108*27b03b36SApple OSS Distributions 	if (kret != KERN_SUCCESS) {
109*27b03b36SApple OSS Distributions 		T_FAIL("mach_msg_server: %s (%d)", mach_error_string(kret), kret);
110*27b03b36SApple OSS Distributions 	}
111*27b03b36SApple OSS Distributions 
112*27b03b36SApple OSS Distributions 	return NULL;
113*27b03b36SApple OSS Distributions }
114*27b03b36SApple OSS Distributions 
115*27b03b36SApple OSS Distributions static void*
dummy_thread(void * arg)116*27b03b36SApple OSS Distributions dummy_thread(void *arg)
117*27b03b36SApple OSS Distributions {
118*27b03b36SApple OSS Distributions #pragma unused(arg)
119*27b03b36SApple OSS Distributions 	while (1) {
120*27b03b36SApple OSS Distributions 		sleep(60);
121*27b03b36SApple OSS Distributions 	}
122*27b03b36SApple OSS Distributions }
123*27b03b36SApple OSS Distributions 
124*27b03b36SApple OSS Distributions #define THREAD_LIMIT 2
125*27b03b36SApple OSS Distributions 
126*27b03b36SApple OSS Distributions T_HELPER_DECL(exc_resource_helper, "exc_resource helper")
127*27b03b36SApple OSS Distributions {
128*27b03b36SApple OSS Distributions 	pthread_t tid;
129*27b03b36SApple OSS Distributions 	for (int i = 0; i < THREAD_LIMIT; i++) {
130*27b03b36SApple OSS Distributions 		T_QUIET;
131*27b03b36SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(pthread_create(&tid, NULL, dummy_thread, NULL), "pthread_create");
132*27b03b36SApple OSS Distributions 	}
133*27b03b36SApple OSS Distributions 	while (1) {
134*27b03b36SApple OSS Distributions 		sleep(60);
135*27b03b36SApple OSS Distributions 	}
136*27b03b36SApple OSS Distributions }
137*27b03b36SApple OSS Distributions 
138*27b03b36SApple OSS Distributions static void
check_exc_resource_threads_enabled()139*27b03b36SApple OSS Distributions check_exc_resource_threads_enabled()
140*27b03b36SApple OSS Distributions {
141*27b03b36SApple OSS Distributions 	int err;
142*27b03b36SApple OSS Distributions 	int enabled;
143*27b03b36SApple OSS Distributions 	size_t enabled_size = sizeof(enabled);
144*27b03b36SApple OSS Distributions 	err = sysctlbyname("kern.exc_resource_threads_enabled", &enabled, &enabled_size, NULL, 0);
145*27b03b36SApple OSS Distributions 
146*27b03b36SApple OSS Distributions 	if (err || !enabled) {
147*27b03b36SApple OSS Distributions 		T_SKIP("EXC_RESOURCE RESOURCE_TYPE_THREADS not enabled on this system");
148*27b03b36SApple OSS Distributions 	}
149*27b03b36SApple OSS Distributions }
150*27b03b36SApple OSS Distributions 
151*27b03b36SApple OSS Distributions T_DECL(exc_resource_threads, "Ensures that a process with a thread_limit set will receive an exc_resource when it crosses its thread limit",
152*27b03b36SApple OSS Distributions     T_META_ASROOT(true),
153*27b03b36SApple OSS Distributions     T_META_CHECK_LEAKS(false))
154*27b03b36SApple OSS Distributions {
155*27b03b36SApple OSS Distributions 	pthread_t handle_thread;
156*27b03b36SApple OSS Distributions 
157*27b03b36SApple OSS Distributions 	check_exc_resource_threads_enabled();
158*27b03b36SApple OSS Distributions 
159*27b03b36SApple OSS Distributions 	sync_sema = dispatch_semaphore_create(0);
160*27b03b36SApple OSS Distributions 
161*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&handle_thread, NULL, exc_handler, NULL), "pthread_create");
162*27b03b36SApple OSS Distributions 	dispatch_semaphore_wait(sync_sema, DISPATCH_TIME_FOREVER);
163*27b03b36SApple OSS Distributions 
164*27b03b36SApple OSS Distributions 	pid_t helper_pid;
165*27b03b36SApple OSS Distributions 	char path[PATH_MAX];
166*27b03b36SApple OSS Distributions 	uint32_t path_size = sizeof(path);
167*27b03b36SApple OSS Distributions 
168*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
169*27b03b36SApple OSS Distributions 
170*27b03b36SApple OSS Distributions 	char *args[] = { path, "-n", "exc_resource_helper", NULL };
171*27b03b36SApple OSS Distributions 
172*27b03b36SApple OSS Distributions 	posix_spawnattr_t attr;
173*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(posix_spawnattr_init(&attr), "posix_spawnattr_init");
174*27b03b36SApple OSS Distributions 
175*27b03b36SApple OSS Distributions 	T_EXPECT_POSIX_ZERO(posix_spawnattr_set_threadlimit_ext(&attr, THREAD_LIMIT), "posix_spawnattr_set_threadlimit_ext");
176*27b03b36SApple OSS Distributions 
177*27b03b36SApple OSS Distributions 	T_EXPECT_POSIX_ZERO(posix_spawn(&helper_pid, args[0], NULL, &attr, args, NULL), "posix_spawn");
178*27b03b36SApple OSS Distributions 
179*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(posix_spawnattr_destroy(&attr), "posix_spawnattr_destroy");
180*27b03b36SApple OSS Distributions 
181*27b03b36SApple OSS Distributions 	dispatch_semaphore_wait(sync_sema, DISPATCH_TIME_FOREVER);
182*27b03b36SApple OSS Distributions }
183