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