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