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