1*699cd480SApple OSS Distributions #include <stdio.h>
2*699cd480SApple OSS Distributions #include <stdlib.h>
3*699cd480SApple OSS Distributions #include <unistd.h>
4*699cd480SApple OSS Distributions #include <darwintest.h>
5*699cd480SApple OSS Distributions #include <mach/mach.h>
6*699cd480SApple OSS Distributions #include <mach/mach_vm.h>
7*699cd480SApple OSS Distributions #include <excserver.h>
8*699cd480SApple OSS Distributions #include <sys/sysctl.h>
9*699cd480SApple OSS Distributions #include <spawn.h>
10*699cd480SApple OSS Distributions #include <signal.h>
11*699cd480SApple OSS Distributions #include <TargetConditionals.h>
12*699cd480SApple OSS Distributions
13*699cd480SApple OSS Distributions #define MAX_ARGV 3
14*699cd480SApple OSS Distributions #define EXC_CODE_SHIFT 32
15*699cd480SApple OSS Distributions #define EXC_GUARD_TYPE_SHIFT 29
16*699cd480SApple OSS Distributions #define MAX_TEST_NUM 21
17*699cd480SApple OSS Distributions
18*699cd480SApple OSS Distributions #define TASK_EXC_GUARD_MP_DELIVER 0x10
19*699cd480SApple OSS Distributions
20*699cd480SApple OSS Distributions extern char **environ;
21*699cd480SApple OSS Distributions static uint64_t exception_code = 0;
22*699cd480SApple OSS Distributions static exception_type_t exception_taken = 0;
23*699cd480SApple OSS Distributions
24*699cd480SApple OSS Distributions #define IKOT_TASK_CONTROL 2
25*699cd480SApple OSS Distributions
26*699cd480SApple OSS Distributions #ifndef kGUARD_EXC_INVALID_OPTIONS
27*699cd480SApple OSS Distributions #define kGUARD_EXC_INVALID_OPTIONS 3
28*699cd480SApple OSS Distributions #endif
29*699cd480SApple OSS Distributions
30*699cd480SApple OSS Distributions /*
31*699cd480SApple OSS Distributions * This test verifies behaviors of immovable/pinned task/thread ports.
32*699cd480SApple OSS Distributions *
33*699cd480SApple OSS Distributions * 1. Compare and verifies port names of mach_{task, thread}_self(),
34*699cd480SApple OSS Distributions * {TASK, THREAD}_KERNEL_PORT, and ports returned from task_threads()
35*699cd480SApple OSS Distributions * and processor_set_tasks().
36*699cd480SApple OSS Distributions * 2. Make sure correct exceptions are raised resulting from moving immovable
37*699cd480SApple OSS Distributions * task/thread control, read and inspect ports.
38*699cd480SApple OSS Distributions * 3. Make sure correct exceptions are raised resulting from deallocating pinned
39*699cd480SApple OSS Distributions * task/thread control ports.
40*699cd480SApple OSS Distributions * 4. Make sure immovable ports cannot be stashed:
41*699cd480SApple OSS Distributions * rdar://70585367 (Disallow immovable port stashing with *_set_special_port() and mach_port_register())
42*699cd480SApple OSS Distributions */
43*699cd480SApple OSS Distributions T_GLOBAL_META(
44*699cd480SApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
45*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
46*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"),
47*699cd480SApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE));
48*699cd480SApple OSS Distributions
49*699cd480SApple OSS Distributions static uint64_t soft_exception_code[] = {
50*699cd480SApple OSS Distributions EXC_GUARD, // Soft crash delivered as EXC_CORPSE_NOTIFY
51*699cd480SApple OSS Distributions EXC_GUARD,
52*699cd480SApple OSS Distributions EXC_GUARD,
53*699cd480SApple OSS Distributions EXC_GUARD,
54*699cd480SApple OSS Distributions EXC_GUARD,
55*699cd480SApple OSS Distributions EXC_GUARD,
56*699cd480SApple OSS Distributions EXC_GUARD,
57*699cd480SApple OSS Distributions
58*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
59*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
60*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
61*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
62*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
63*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
64*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
65*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
66*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
67*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
68*699cd480SApple OSS Distributions
69*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
70*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
71*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
72*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
73*699cd480SApple OSS Distributions };
74*699cd480SApple OSS Distributions
75*699cd480SApple OSS Distributions static uint64_t hard_exception_code[] = {
76*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
77*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
78*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
79*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
80*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
81*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
82*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
83*699cd480SApple OSS Distributions
84*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
85*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
86*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
87*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
88*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
89*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
90*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
91*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
92*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
93*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
94*699cd480SApple OSS Distributions
95*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
96*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
97*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
98*699cd480SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
99*699cd480SApple OSS Distributions };
100*699cd480SApple OSS Distributions
101*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)102*699cd480SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
103*699cd480SApple OSS Distributions exception_type_t exception,
104*699cd480SApple OSS Distributions const mach_exception_data_t code,
105*699cd480SApple OSS Distributions mach_msg_type_number_t code_count,
106*699cd480SApple OSS Distributions int * flavor,
107*699cd480SApple OSS Distributions const thread_state_t old_state,
108*699cd480SApple OSS Distributions mach_msg_type_number_t old_state_count,
109*699cd480SApple OSS Distributions thread_state_t new_state,
110*699cd480SApple OSS Distributions mach_msg_type_number_t * new_state_count)
111*699cd480SApple OSS Distributions {
112*699cd480SApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
113*699cd480SApple OSS Distributions T_FAIL("Unsupported catch_mach_exception_raise_state");
114*699cd480SApple OSS Distributions return KERN_NOT_SUPPORTED;
115*699cd480SApple OSS Distributions }
116*699cd480SApple OSS Distributions
117*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)118*699cd480SApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
119*699cd480SApple OSS Distributions mach_port_t thread,
120*699cd480SApple OSS Distributions mach_port_t task,
121*699cd480SApple OSS Distributions exception_type_t exception,
122*699cd480SApple OSS Distributions mach_exception_data_t code,
123*699cd480SApple OSS Distributions mach_msg_type_number_t code_count,
124*699cd480SApple OSS Distributions int * flavor,
125*699cd480SApple OSS Distributions thread_state_t old_state,
126*699cd480SApple OSS Distributions mach_msg_type_number_t old_state_count,
127*699cd480SApple OSS Distributions thread_state_t new_state,
128*699cd480SApple OSS Distributions mach_msg_type_number_t * new_state_count)
129*699cd480SApple OSS Distributions {
130*699cd480SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
131*699cd480SApple OSS Distributions T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
132*699cd480SApple OSS Distributions return KERN_NOT_SUPPORTED;
133*699cd480SApple OSS Distributions }
134*699cd480SApple OSS Distributions
135*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)136*699cd480SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
137*699cd480SApple OSS Distributions mach_port_t thread,
138*699cd480SApple OSS Distributions mach_port_t task,
139*699cd480SApple OSS Distributions exception_type_t exception,
140*699cd480SApple OSS Distributions mach_exception_data_t code,
141*699cd480SApple OSS Distributions mach_msg_type_number_t code_count)
142*699cd480SApple OSS Distributions {
143*699cd480SApple OSS Distributions #pragma unused(exception_port, code_count)
144*699cd480SApple OSS Distributions pid_t pid;
145*699cd480SApple OSS Distributions kern_return_t kr = pid_for_task(task, &pid);
146*699cd480SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "pid_for_task");
147*699cd480SApple OSS Distributions T_LOG("Crashing child pid: %d, continuing...\n", pid);
148*699cd480SApple OSS Distributions
149*699cd480SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), thread);
150*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_deallocate");
151*699cd480SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), task);
152*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_deallocate");
153*699cd480SApple OSS Distributions
154*699cd480SApple OSS Distributions T_LOG("Caught exception type: %d code: 0x%llx", exception, *((uint64_t*)code));
155*699cd480SApple OSS Distributions if (exception == EXC_GUARD || exception == EXC_CORPSE_NOTIFY) {
156*699cd480SApple OSS Distributions exception_taken = exception;
157*699cd480SApple OSS Distributions exception_code = *((uint64_t *)code);
158*699cd480SApple OSS Distributions } else {
159*699cd480SApple OSS Distributions T_FAIL("Unexpected exception");
160*699cd480SApple OSS Distributions }
161*699cd480SApple OSS Distributions return KERN_SUCCESS;
162*699cd480SApple OSS Distributions }
163*699cd480SApple OSS Distributions
164*699cd480SApple OSS Distributions static void *
exception_server_thread(void * arg)165*699cd480SApple OSS Distributions exception_server_thread(void *arg)
166*699cd480SApple OSS Distributions {
167*699cd480SApple OSS Distributions kern_return_t kr;
168*699cd480SApple OSS Distributions mach_port_t exc_port = *(mach_port_t *)arg;
169*699cd480SApple OSS Distributions
170*699cd480SApple OSS Distributions /* Handle exceptions on exc_port */
171*699cd480SApple OSS Distributions kr = mach_msg_server_once(mach_exc_server, 4096, exc_port, 0);
172*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_msg_server_once");
173*699cd480SApple OSS Distributions
174*699cd480SApple OSS Distributions return NULL;
175*699cd480SApple OSS Distributions }
176*699cd480SApple OSS Distributions
177*699cd480SApple OSS Distributions static mach_port_t
alloc_exception_port(void)178*699cd480SApple OSS Distributions alloc_exception_port(void)
179*699cd480SApple OSS Distributions {
180*699cd480SApple OSS Distributions kern_return_t kret;
181*699cd480SApple OSS Distributions mach_port_t exc_port = MACH_PORT_NULL;
182*699cd480SApple OSS Distributions mach_port_t task = mach_task_self();
183*699cd480SApple OSS Distributions
184*699cd480SApple OSS Distributions kret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
185*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_allocate exc_port");
186*699cd480SApple OSS Distributions
187*699cd480SApple OSS Distributions kret = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
188*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_insert_right exc_port");
189*699cd480SApple OSS Distributions
190*699cd480SApple OSS Distributions return exc_port;
191*699cd480SApple OSS Distributions }
192*699cd480SApple OSS Distributions
193*699cd480SApple OSS Distributions static void
test_immovable_port_stashing(void)194*699cd480SApple OSS Distributions test_immovable_port_stashing(void)
195*699cd480SApple OSS Distributions {
196*699cd480SApple OSS Distributions kern_return_t kr;
197*699cd480SApple OSS Distributions mach_port_t port;
198*699cd480SApple OSS Distributions
199*699cd480SApple OSS Distributions kr = task_set_special_port(mach_task_self(), TASK_BOOTSTRAP_PORT, mach_task_self());
200*699cd480SApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_RIGHT, "should disallow task_set_special_port() with immovable port");
201*699cd480SApple OSS Distributions
202*699cd480SApple OSS Distributions kr = thread_set_special_port(mach_thread_self(), THREAD_KERNEL_PORT, mach_thread_self());
203*699cd480SApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_RIGHT, "should disallow task_set_special_port() with immovable port");
204*699cd480SApple OSS Distributions
205*699cd480SApple OSS Distributions mach_port_t stash[1] = {mach_task_self()};
206*699cd480SApple OSS Distributions kr = mach_ports_register(mach_task_self(), stash, 1);
207*699cd480SApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_RIGHT, "should disallow mach_ports_register() with immovable port");
208*699cd480SApple OSS Distributions
209*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port), "mach_port_allocate");
210*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND), "mach_port_insert_right");
211*699cd480SApple OSS Distributions
212*699cd480SApple OSS Distributions stash[0] = port;
213*699cd480SApple OSS Distributions kr = mach_ports_register(mach_task_self(), stash, 1);
214*699cd480SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_ports_register() should succeed with movable port");
215*699cd480SApple OSS Distributions }
216*699cd480SApple OSS Distributions
217*699cd480SApple OSS Distributions static void
test_task_thread_port_values(void)218*699cd480SApple OSS Distributions test_task_thread_port_values(void)
219*699cd480SApple OSS Distributions {
220*699cd480SApple OSS Distributions T_LOG("Compare various task/thread control port values\n");
221*699cd480SApple OSS Distributions kern_return_t kr;
222*699cd480SApple OSS Distributions mach_port_t port, th_self;
223*699cd480SApple OSS Distributions thread_array_t threadList;
224*699cd480SApple OSS Distributions mach_msg_type_number_t threadCount = 0;
225*699cd480SApple OSS Distributions boolean_t found_self = false;
226*699cd480SApple OSS Distributions processor_set_name_array_t psets;
227*699cd480SApple OSS Distributions processor_set_t pset_priv;
228*699cd480SApple OSS Distributions task_array_t taskList;
229*699cd480SApple OSS Distributions mach_msg_type_number_t pcnt = 0, tcnt = 0;
230*699cd480SApple OSS Distributions mach_port_t host = mach_host_self();
231*699cd480SApple OSS Distributions
232*699cd480SApple OSS Distributions /* Compare with task/thread_get_special_port() */
233*699cd480SApple OSS Distributions kr = task_get_special_port(mach_task_self(), TASK_KERNEL_PORT, &port);
234*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port() - TASK_KERNEL_PORT");
235*699cd480SApple OSS Distributions T_EXPECT_NE(port, mach_task_self(), "TASK_KERNEL_PORT should not match mach_task_self()");
236*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), port);
237*699cd480SApple OSS Distributions
238*699cd480SApple OSS Distributions kr = task_for_pid(mach_task_self(), getpid(), &port);
239*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid()");
240*699cd480SApple OSS Distributions T_EXPECT_EQ(port, mach_task_self(), "task_for_pid(self) should match mach_task_self()");
241*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), port);
242*699cd480SApple OSS Distributions
243*699cd480SApple OSS Distributions th_self = mach_thread_self();
244*699cd480SApple OSS Distributions kr = thread_get_special_port(th_self, THREAD_KERNEL_PORT, &port);
245*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_get_special_port() - THREAD_KERNEL_PORT");
246*699cd480SApple OSS Distributions T_EXPECT_NE(port, th_self, "THREAD_KERNEL_PORT should not match mach_thread_self()");
247*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), port);
248*699cd480SApple OSS Distributions
249*699cd480SApple OSS Distributions /* Make sure task_threads() return immovable thread ports */
250*699cd480SApple OSS Distributions kr = task_threads(mach_task_self(), &threadList, &threadCount);
251*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_threads()");
252*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GE(threadCount, 1, "should have at least 1 thread");
253*699cd480SApple OSS Distributions
254*699cd480SApple OSS Distributions for (size_t i = 0; i < threadCount; i++) {
255*699cd480SApple OSS Distributions if (th_self == threadList[i]) { /* th_self is immovable */
256*699cd480SApple OSS Distributions found_self = true;
257*699cd480SApple OSS Distributions break;
258*699cd480SApple OSS Distributions }
259*699cd480SApple OSS Distributions }
260*699cd480SApple OSS Distributions
261*699cd480SApple OSS Distributions T_EXPECT_TRUE(found_self, "task_threads() should return immovable thread self");
262*699cd480SApple OSS Distributions
263*699cd480SApple OSS Distributions for (size_t i = 0; i < threadCount; i++) {
264*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), threadList[i]);
265*699cd480SApple OSS Distributions }
266*699cd480SApple OSS Distributions
267*699cd480SApple OSS Distributions if (threadCount > 0) {
268*699cd480SApple OSS Distributions mach_vm_deallocate(mach_task_self(),
269*699cd480SApple OSS Distributions (mach_vm_address_t)threadList,
270*699cd480SApple OSS Distributions threadCount * sizeof(mach_port_t));
271*699cd480SApple OSS Distributions }
272*699cd480SApple OSS Distributions
273*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), th_self);
274*699cd480SApple OSS Distributions
275*699cd480SApple OSS Distributions /* Make sure processor_set_tasks() return immovable task self */
276*699cd480SApple OSS Distributions kr = host_processor_sets(host, &psets, &pcnt);
277*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_sets");
278*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GE(pcnt, 1, "should have at least 1 processor set");
279*699cd480SApple OSS Distributions
280*699cd480SApple OSS Distributions kr = host_processor_set_priv(host, psets[0], &pset_priv);
281*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_set_priv");
282*699cd480SApple OSS Distributions for (size_t i = 0; i < pcnt; i++) {
283*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), psets[i]);
284*699cd480SApple OSS Distributions }
285*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), host);
286*699cd480SApple OSS Distributions vm_deallocate(mach_task_self(), (vm_address_t)psets, (vm_size_t)pcnt * sizeof(mach_port_t));
287*699cd480SApple OSS Distributions
288*699cd480SApple OSS Distributions kr = processor_set_tasks_with_flavor(pset_priv, TASK_FLAVOR_CONTROL, &taskList, &tcnt);
289*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "processor_set_tasks_with_flavor");
290*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GE(tcnt, 1, "should have at least 1 task");
291*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), pset_priv);
292*699cd480SApple OSS Distributions
293*699cd480SApple OSS Distributions found_self = false;
294*699cd480SApple OSS Distributions for (size_t i = 0; i < tcnt; i++) {
295*699cd480SApple OSS Distributions if (taskList[i] == mach_task_self()) {
296*699cd480SApple OSS Distributions found_self = true;
297*699cd480SApple OSS Distributions break;
298*699cd480SApple OSS Distributions }
299*699cd480SApple OSS Distributions }
300*699cd480SApple OSS Distributions
301*699cd480SApple OSS Distributions T_EXPECT_TRUE(found_self, " processor_set_tasks() should return immovable task self");
302*699cd480SApple OSS Distributions
303*699cd480SApple OSS Distributions for (size_t i = 0; i < tcnt; i++) {
304*699cd480SApple OSS Distributions mach_port_deallocate(mach_task_self(), taskList[i]);
305*699cd480SApple OSS Distributions }
306*699cd480SApple OSS Distributions
307*699cd480SApple OSS Distributions if (tcnt > 0) {
308*699cd480SApple OSS Distributions mach_vm_deallocate(mach_task_self(),
309*699cd480SApple OSS Distributions (mach_vm_address_t)taskList,
310*699cd480SApple OSS Distributions tcnt * sizeof(mach_port_t));
311*699cd480SApple OSS Distributions }
312*699cd480SApple OSS Distributions }
313*699cd480SApple OSS Distributions
314*699cd480SApple OSS Distributions T_DECL(imm_pinned_control_port, "Test pinned & immovable task and thread control ports",
315*699cd480SApple OSS Distributions T_META_IGNORECRASHES(".*pinned_rights_child.*"),
316*699cd480SApple OSS Distributions T_META_CHECK_LEAKS(false))
317*699cd480SApple OSS Distributions {
318*699cd480SApple OSS Distributions uint32_t task_exc_guard = 0;
319*699cd480SApple OSS Distributions size_t te_size = sizeof(&task_exc_guard);
320*699cd480SApple OSS Distributions posix_spawnattr_t attrs;
321*699cd480SApple OSS Distributions char *test_prog_name = "./imm_pinned_control_port_crasher";
322*699cd480SApple OSS Distributions char *child_args[MAX_ARGV];
323*699cd480SApple OSS Distributions pid_t client_pid = 0;
324*699cd480SApple OSS Distributions uint32_t opts = 0;
325*699cd480SApple OSS Distributions uint64_t *test_exception_code;
326*699cd480SApple OSS Distributions size_t size = sizeof(&opts);
327*699cd480SApple OSS Distributions mach_port_t exc_port;
328*699cd480SApple OSS Distributions pthread_t s_exc_thread;
329*699cd480SApple OSS Distributions uint64_t exc_id;
330*699cd480SApple OSS Distributions
331*699cd480SApple OSS Distributions T_LOG("Check if task_exc_guard exception has been enabled\n");
332*699cd480SApple OSS Distributions int ret = sysctlbyname("kern.task_exc_guard_default", &task_exc_guard, &te_size, NULL, 0);
333*699cd480SApple OSS Distributions T_ASSERT_EQ(ret, 0, "sysctlbyname");
334*699cd480SApple OSS Distributions
335*699cd480SApple OSS Distributions if (!(task_exc_guard & TASK_EXC_GUARD_MP_DELIVER)) {
336*699cd480SApple OSS Distributions T_SKIP("task_exc_guard exception is not enabled");
337*699cd480SApple OSS Distributions }
338*699cd480SApple OSS Distributions
339*699cd480SApple OSS Distributions T_LOG("Check if immovable control port has been enabled\n");
340*699cd480SApple OSS Distributions ret = sysctlbyname("kern.ipc_control_port_options", &opts, &size, NULL, 0);
341*699cd480SApple OSS Distributions
342*699cd480SApple OSS Distributions if (!ret && (opts & 0x8) != 0x8) {
343*699cd480SApple OSS Distributions T_SKIP("hard immovable control port isn't enabled");
344*699cd480SApple OSS Distributions }
345*699cd480SApple OSS Distributions
346*699cd480SApple OSS Distributions if (!ret && (opts & 0x2)) {
347*699cd480SApple OSS Distributions T_LOG("Hard pinning enforcement is on.");
348*699cd480SApple OSS Distributions test_exception_code = hard_exception_code;
349*699cd480SApple OSS Distributions } else {
350*699cd480SApple OSS Distributions T_LOG("Hard pinning enforcement is off.");
351*699cd480SApple OSS Distributions test_exception_code = soft_exception_code;
352*699cd480SApple OSS Distributions }
353*699cd480SApple OSS Distributions
354*699cd480SApple OSS Distributions
355*699cd480SApple OSS Distributions /* first, try out comparing various task/thread ports */
356*699cd480SApple OSS Distributions test_task_thread_port_values();
357*699cd480SApple OSS Distributions
358*699cd480SApple OSS Distributions /* try stashing immovable ports: rdar://70585367 */
359*699cd480SApple OSS Distributions test_immovable_port_stashing();
360*699cd480SApple OSS Distributions
361*699cd480SApple OSS Distributions /* spawn a child and see if EXC_GUARD are correctly generated */
362*699cd480SApple OSS Distributions for (int i = 0; i < MAX_TEST_NUM; i++) {
363*699cd480SApple OSS Distributions /* Create the exception port for the child */
364*699cd480SApple OSS Distributions exc_port = alloc_exception_port();
365*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NE(exc_port, MACH_PORT_NULL, "Create a new exception port");
366*699cd480SApple OSS Distributions
367*699cd480SApple OSS Distributions /* Create exception serving thread */
368*699cd480SApple OSS Distributions ret = pthread_create(&s_exc_thread, NULL, exception_server_thread, &exc_port);
369*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create exception_server_thread");
370*699cd480SApple OSS Distributions
371*699cd480SApple OSS Distributions /* Initialize posix_spawn attributes */
372*699cd480SApple OSS Distributions posix_spawnattr_init(&attrs);
373*699cd480SApple OSS Distributions
374*699cd480SApple OSS Distributions int err = posix_spawnattr_setexceptionports_np(&attrs, EXC_MASK_GUARD | EXC_MASK_CORPSE_NOTIFY, exc_port,
375*699cd480SApple OSS Distributions (exception_behavior_t) (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 0);
376*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "posix_spawnattr_setflags");
377*699cd480SApple OSS Distributions
378*699cd480SApple OSS Distributions child_args[0] = test_prog_name;
379*699cd480SApple OSS Distributions char test_num[10];
380*699cd480SApple OSS Distributions sprintf(test_num, "%d", i);
381*699cd480SApple OSS Distributions child_args[1] = test_num;
382*699cd480SApple OSS Distributions child_args[2] = NULL;
383*699cd480SApple OSS Distributions
384*699cd480SApple OSS Distributions T_LOG("========== Spawning new child ==========");
385*699cd480SApple OSS Distributions err = posix_spawn(&client_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
386*699cd480SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(err, "posix_spawn control_port_options_client = %d test_num = %d", client_pid, i);
387*699cd480SApple OSS Distributions
388*699cd480SApple OSS Distributions /* try extracting child task port: rdar://71744817
389*699cd480SApple OSS Distributions * Moved to tests/extract_right_soft_fail.c
390*699cd480SApple OSS Distributions */
391*699cd480SApple OSS Distributions // test_extract_immovable_task_port(client_pid);
392*699cd480SApple OSS Distributions
393*699cd480SApple OSS Distributions int child_status;
394*699cd480SApple OSS Distributions /* Wait for child and check for exception */
395*699cd480SApple OSS Distributions if (-1 == waitpid(-1, &child_status, 0)) {
396*699cd480SApple OSS Distributions T_FAIL("waitpid: child mia");
397*699cd480SApple OSS Distributions }
398*699cd480SApple OSS Distributions
399*699cd480SApple OSS Distributions if (WIFEXITED(child_status) && WEXITSTATUS(child_status)) {
400*699cd480SApple OSS Distributions T_FAIL("Child exited with status = %x", child_status);
401*699cd480SApple OSS Distributions T_END;
402*699cd480SApple OSS Distributions }
403*699cd480SApple OSS Distributions
404*699cd480SApple OSS Distributions sleep(1);
405*699cd480SApple OSS Distributions kill(1, SIGKILL);
406*699cd480SApple OSS Distributions
407*699cd480SApple OSS Distributions ret = pthread_join(s_exc_thread, NULL);
408*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_join");
409*699cd480SApple OSS Distributions
410*699cd480SApple OSS Distributions if (exception_taken == EXC_GUARD) {
411*699cd480SApple OSS Distributions exc_id = exception_code >> EXC_CODE_SHIFT;
412*699cd480SApple OSS Distributions } else {
413*699cd480SApple OSS Distributions exc_id = exception_code;
414*699cd480SApple OSS Distributions }
415*699cd480SApple OSS Distributions
416*699cd480SApple OSS Distributions T_LOG("Exception code: Received code = 0x%llx Expected code = 0x%llx", exc_id, test_exception_code[i]);
417*699cd480SApple OSS Distributions T_EXPECT_EQ(exc_id, test_exception_code[i], "Exception code: Received == Expected");
418*699cd480SApple OSS Distributions }
419*699cd480SApple OSS Distributions }
420