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