1 /*
2 * prioritize process launch: Tests prioritized process launch across posix spawn and exec.
3 */
4
5 #ifdef T_NAMESPACE
6 #undef T_NAMESPACE
7 #endif
8
9 #include <darwintest.h>
10 #include <darwintest_multiprocess.h>
11
12 #include <dispatch/dispatch.h>
13 #include <pthread.h>
14 #include <launch.h>
15 #include <mach/mach.h>
16 #include <mach/message.h>
17 #include <mach/mach_voucher.h>
18 #include <pthread/workqueue_private.h>
19 #include <voucher/ipc_pthread_priority_types.h>
20 #include <servers/bootstrap.h>
21 #include <stdlib.h>
22 #include <sys/event.h>
23 #include <unistd.h>
24 #include <crt_externs.h>
25 #include <signal.h>
26 #include <sys/types.h>
27 #include <sys/sysctl.h>
28 #include <libkern/OSAtomic.h>
29 #include <sys/wait.h>
30 #include <spawn.h>
31 #include <spawn_private.h>
32
33 T_GLOBAL_META(T_META_NAMESPACE("xnu.prioritize_process_launch"),
34 T_META_RUN_CONCURRENTLY(true));
35
36 #define HELPER_TIMEOUT_SECS (3000)
37 #define MACH_RCV_OPTIONS (MACH_RCV_MSG | MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY | \
38 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX) | \
39 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0))
40
41 static pthread_t
42 thread_create_at_qos(qos_class_t qos, void * (*function)(void *), void *arg);
43 static mach_port_t sr_port;
44
45
46 #pragma mark Mach receive
47
48 static void
send(mach_port_t send_port,mach_port_t reply_port,mach_port_t msg_port,mach_msg_priority_t priority,mach_msg_option_t options,int send_disposition)49 send(
50 mach_port_t send_port,
51 mach_port_t reply_port,
52 mach_port_t msg_port,
53 mach_msg_priority_t priority,
54 mach_msg_option_t options,
55 int send_disposition)
56 {
57 kern_return_t ret = 0;
58
59 struct {
60 mach_msg_header_t header;
61 mach_msg_body_t body;
62 mach_msg_port_descriptor_t port_descriptor;
63 } send_msg = {
64 .header = {
65 .msgh_remote_port = send_port,
66 .msgh_local_port = reply_port,
67 .msgh_bits = MACH_MSGH_BITS_SET(send_disposition,
68 reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0, 0,
69 MACH_MSGH_BITS_COMPLEX),
70 .msgh_id = 0x100,
71 .msgh_size = sizeof(send_msg),
72 },
73 .body = {
74 .msgh_descriptor_count = 1,
75 },
76 .port_descriptor = {
77 .name = msg_port,
78 .disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
79 .type = MACH_MSG_PORT_DESCRIPTOR,
80 },
81 };
82
83 if (msg_port == MACH_PORT_NULL) {
84 send_msg.body.msgh_descriptor_count = 0;
85 }
86
87 ret = mach_msg(&(send_msg.header),
88 MACH_SEND_MSG |
89 MACH_SEND_TIMEOUT |
90 MACH_SEND_OVERRIDE |
91 options,
92 send_msg.header.msgh_size,
93 0,
94 MACH_PORT_NULL,
95 10000,
96 priority);
97
98 T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "client mach_msg");
99 }
100
101 static void
receive(mach_port_t rcv_port,mach_port_t notify_port)102 receive(
103 mach_port_t rcv_port,
104 mach_port_t notify_port)
105 {
106 kern_return_t ret = 0;
107
108 struct {
109 mach_msg_header_t header;
110 mach_msg_body_t body;
111 mach_msg_port_descriptor_t port_descriptor;
112 mach_msg_trailer_t trailer;
113 } rcv_msg = {
114 .header =
115 {
116 .msgh_remote_port = MACH_PORT_NULL,
117 .msgh_local_port = rcv_port,
118 .msgh_size = sizeof(rcv_msg),
119 },
120 };
121
122 T_LOG("Client: Starting sync receive\n");
123
124 ret = mach_msg(&(rcv_msg.header),
125 MACH_RCV_MSG |
126 MACH_RCV_SYNC_WAIT,
127 0,
128 rcv_msg.header.msgh_size,
129 rcv_port,
130 0,
131 notify_port);
132 }
133
134 static int
get_pri(thread_t thread_port)135 get_pri(thread_t thread_port)
136 {
137 kern_return_t kr;
138
139 thread_extended_info_data_t extended_info;
140 mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
141 kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
142 (thread_info_t)&extended_info, &count);
143
144 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
145
146 return extended_info.pth_curpri;
147 }
148
149 static void
set_thread_name(const char * fn_name)150 set_thread_name(const char *fn_name)
151 {
152 char name[50] = "";
153
154 thread_t thread_port = pthread_mach_thread_np(pthread_self());
155
156 int pri = get_pri(thread_port);
157
158 snprintf(name, sizeof(name), "%s at pri %2d", fn_name, pri);
159 pthread_setname_np(name);
160 }
161
162 static void
thread_wait_to_block(mach_port_t thread_port)163 thread_wait_to_block(mach_port_t thread_port)
164 {
165 thread_extended_info_data_t extended_info;
166 kern_return_t kr;
167
168 while (1) {
169 mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
170 kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
171 (thread_info_t)&extended_info, &count);
172
173 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
174
175 if (extended_info.pth_run_state == TH_STATE_WAITING) {
176 T_LOG("Target thread blocked\n");
177 break;
178 }
179 thread_switch(thread_port, SWITCH_OPTION_DEPRESS, 0);
180 }
181 }
182
183 static void *
thread_sync_rcv(void * arg)184 thread_sync_rcv(void *arg)
185 {
186 mach_port_t port = (mach_port_t)arg;
187 mach_port_t special_reply_port;
188
189 set_thread_name(__FUNCTION__);
190 special_reply_port = thread_get_special_reply_port();
191 T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
192
193 sr_port = special_reply_port;
194 /* Do a sync rcv on special reply port and push on given arg port */
195 receive(special_reply_port, port);
196 return NULL;
197 }
198
199 static pthread_t
thread_create_at_qos(qos_class_t qos,void * (* function)(void *),void * arg)200 thread_create_at_qos(qos_class_t qos, void * (*function)(void *), void *arg)
201 {
202 qos_class_t qos_thread;
203 pthread_t pthread;
204 pthread_attr_t attr;
205 int ret;
206
207 ret = setpriority(PRIO_DARWIN_ROLE, 0, PRIO_DARWIN_ROLE_UI_FOCAL);
208 if (ret != 0) {
209 T_LOG("set priority failed\n");
210 }
211
212 pthread_attr_init(&attr);
213 pthread_attr_set_qos_class_np(&attr, qos, 0);
214 pthread_create(&pthread, &attr, function, arg);
215
216 T_LOG("pthread created\n");
217 pthread_get_qos_class_np(pthread, &qos_thread, NULL);
218 return pthread;
219 }
220
221 static mach_port_t
get_sync_push_port_at_qos(qos_class_t qos)222 get_sync_push_port_at_qos(qos_class_t qos)
223 {
224 mach_port_t port;
225 kern_return_t kr;
226 pthread_t pthread;
227 thread_t thread;
228
229 /* Create a rcv right to have a sync ipc push from a thread */
230 kr = mach_port_allocate(mach_task_self(),
231 MACH_PORT_RIGHT_RECEIVE,
232 &port);
233
234 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "sync push port mach_port_allocate");
235
236 kr = mach_port_insert_right(mach_task_self(),
237 port,
238 port,
239 MACH_MSG_TYPE_MAKE_SEND);
240
241 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "sync push port mach_port_insert_right");
242
243 /* Create a thread at given qos and start a sync push on given port */
244 pthread = thread_create_at_qos(qos, thread_sync_rcv, (void *)(uintptr_t)port);
245 thread = pthread_mach_thread_np(pthread);
246 thread_wait_to_block(thread);
247
248 return port;
249 }
250
251 static mach_port_t
create_port_and_copyin_a_port(mach_port_t port)252 create_port_and_copyin_a_port(mach_port_t port)
253 {
254 mach_port_t new_port;
255 kern_return_t kr;
256
257 /* Create a rcv right */
258 kr = mach_port_allocate(mach_task_self(),
259 MACH_PORT_RIGHT_RECEIVE,
260 &new_port);
261
262 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "copyin mach_port_allocate");
263
264 kr = mach_port_insert_right(mach_task_self(),
265 new_port,
266 new_port,
267 MACH_MSG_TYPE_MAKE_SEND);
268
269 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "copyin mach_port_insert_right");
270
271 send(new_port, MACH_PORT_NULL, port, 0, 0, MACH_MSG_TYPE_COPY_SEND);
272 return new_port;
273 }
274
275 static pid_t
posix_spawn_child_with_watch_ports(char * binary,char * arg,mach_port_t * port_array,int arrayCnt)276 posix_spawn_child_with_watch_ports(
277 char *binary,
278 char *arg,
279 mach_port_t *port_array,
280 int arrayCnt)
281 {
282 pid_t child_pid = 0;
283 char *new_argv[] = { binary, arg, NULL};
284 errno_t ret;
285 posix_spawnattr_t attr;
286
287 ret = posix_spawnattr_init(&attr);
288 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
289
290 ret = posix_spawnattr_set_importancewatch_port_np(&attr, arrayCnt, port_array);
291 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_set_importancewatch_port_np");
292
293 ret = posix_spawn(&child_pid, binary, NULL, &attr, new_argv, NULL);
294 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
295
296 ret = posix_spawnattr_destroy(&attr);
297 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_destroy");
298
299 return child_pid;
300 }
301
302 static void
worker_cb(pthread_priority_t __unused priority)303 worker_cb(pthread_priority_t __unused priority)
304 {
305 T_FAIL("a worker thread was created");
306 }
307
308 static void
event_cb(void ** __unused events,int * __unused nevents)309 event_cb(void ** __unused events, int * __unused nevents)
310 {
311 T_FAIL("a kevent routine was called instead of workloop");
312 }
313
314 static void
workloop_cb_test_intransit(uint64_t * workloop_id __unused,void ** eventslist,int * events)315 workloop_cb_test_intransit(uint64_t *workloop_id __unused, void **eventslist, int *events)
316 {
317 pid_t pid;
318 int stat;
319 int priority;
320 mach_port_t port;
321 struct kevent_qos_s *kev = *eventslist;
322 mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
323 port = hdr->msgh_local_port;
324
325 T_LOG("Workloop handler workloop_cb_test_intransit called. ");
326 T_LOG("The total events returned is %d", *events);
327
328 priority = get_pri(mach_thread_self());
329 T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
330
331 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
332
333 /* Make sure our priority has dropped */
334 priority = get_pri(mach_thread_self());
335 T_EXPECT_EQ(priority, 31, "Priority of servicer is %d", priority);
336
337 sleep(2);
338
339 /*enqueue the port to sever the temp onwer boost */
340 create_port_and_copyin_a_port(port);
341
342 waitpid(pid, &stat, 0);
343
344 *events = 0;
345
346 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
347 T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost did not work correctly with knotes");
348 T_END;
349 }
350
351 static void
workloop_cb_test_knote_kill(uint64_t * workloop_id __unused,void ** eventslist,int * events)352 workloop_cb_test_knote_kill(uint64_t *workloop_id __unused, void **eventslist, int *events)
353 {
354 pid_t pid;
355 int stat;
356 int priority;
357 mach_port_t port;
358 struct kevent_qos_s *kev = *eventslist;
359 mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
360 port = hdr->msgh_local_port;
361
362 T_LOG("Workloop handler workloop_cb_test_knote_kill called. ");
363 T_LOG("The total events returned is %d", *events);
364
365 priority = get_pri(mach_thread_self());
366 T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
367
368 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXIT", &port, 1);
369
370 sleep(2);
371
372 /* Make sure our priority is boosted again */
373 priority = get_pri(mach_thread_self());
374 T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
375
376 waitpid(pid, &stat, 0);
377
378 *events = 0;
379
380 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
381 T_EXPECT_EQ(WEXITSTATUS(stat), 47, "Temp owner boost did not work correctly with knotes");
382 T_END;
383 }
384
385 static void
workloop_cb_test_sync_bootstrap(uint64_t * workloop_id __unused,void ** eventslist,int * events)386 workloop_cb_test_sync_bootstrap(uint64_t *workloop_id __unused, void **eventslist, int *events)
387 {
388 static pid_t pid = 0;
389 int stat;
390 int priority;
391 static mach_port_t port = MACH_PORT_NULL;
392 struct kevent_qos_s *kev = *eventslist;
393 mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
394
395 T_LOG("Workloop handler workloop_cb_test_knote_kill called. ");
396 T_LOG("The total events returned is %d", *events);
397
398 /* Check if called for peek */
399 if (hdr == NULL) {
400 priority = get_pri(mach_thread_self());
401 T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
402
403 port = (mach_port_t)kev->ident;
404 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "MSGSYNC", &port, 1);
405 } else {
406 /* Wait till the priority of servicer is 47 */
407 T_LOG("Waiting for the servicer to be boosted");
408 do {
409 sleep(1);
410 priority = get_pri(mach_thread_self());
411 } while (priority != 47);
412
413 T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
414
415 /* Get the reply port and send the receive right in it */
416 mach_port_t reply_port = hdr->msgh_remote_port;
417 T_LOG("The rcv right to send is %d", port);
418 send(reply_port, MACH_PORT_NULL, port, 0, 0, MACH_MSG_TYPE_MOVE_SEND_ONCE);
419
420 waitpid(pid, &stat, 0);
421
422 /* The handler priority should not be boosted anymore */
423 priority = get_pri(mach_thread_self());
424 T_EXPECT_EQ(priority, 31, "Priority of servicer is %d", priority);
425
426 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
427 T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost did not work correctly with knotes");
428 T_END;
429 }
430 *events = 0;
431 }
432
433 static void
register_workloop_for_port(mach_port_t port,pthread_workqueue_function_workloop_t func,unsigned int options)434 register_workloop_for_port(
435 mach_port_t port,
436 pthread_workqueue_function_workloop_t func,
437 unsigned int options)
438 {
439 int r;
440
441 /* register workloop handler with pthread */
442 if (func != NULL) {
443 T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
444 worker_cb, event_cb,
445 (pthread_workqueue_function_workloop_t)func, 0, 0), NULL);
446 }
447
448 /* attach port to workloop */
449 struct kevent_qos_s kev[] = {{
450 .ident = port,
451 .filter = EVFILT_MACHPORT,
452 .flags = EV_ADD | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED,
453 .fflags = options,
454 .data = 1,
455 .qos = (int32_t)_pthread_qos_class_encode(QOS_CLASS_DEFAULT, 0, 0)
456 }};
457
458 struct kevent_qos_s kev_err[] = {{ 0 }};
459
460 /* Setup workloop for mach msg rcv */
461 r = kevent_id(25, kev, 1, kev_err, 1, NULL,
462 NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
463
464 T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "kevent_id");
465 T_QUIET; T_ASSERT_EQ(r, 0, "no errors returned from kevent_id");
466 }
467
468 /*
469 * Test 1: Test turnstile boosting for temp owner ports for posix_spawn.
470 *
471 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
472 * test that spawned binary has the temp owner push of the port.
473 */
474 T_DECL(posix_spawn_basic_priority, "Basic posix spawn temp owner priority test", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
475 {
476 mach_port_t port;
477 pid_t pid;
478 int stat;
479
480 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
481 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXIT", &port, 1);
482
483 waitpid(pid, &stat, 0);
484
485 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
486 T_EXPECT_EQ(WEXITSTATUS(stat), 47, "spawn did not properly boost main thread");
487 T_END;
488 }
489
490 /*
491 * Test 2: Test turnstile boosting for temp owner ports for posix_spawn and exec.
492 *
493 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
494 * test that spawned binary has the temp owner push of the port. The spawned binary will exec
495 * and verify that it still has the push.
496 */
497 T_DECL(posix_spawn_exec_basic_priority, "Basic posix spawn/exec temp owner priority test", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
498 {
499 mach_port_t port;
500 pid_t pid;
501 int stat;
502
503 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
504 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXEC", &port, 1);
505
506 waitpid(pid, &stat, 0);
507
508 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
509 T_EXPECT_EQ(WEXITSTATUS(stat), 47, "spawn/exec did not properly boost main thread");
510 T_END;
511 }
512
513 /*
514 * Test 3: Test turnstile boosting for temp owner ports for posix_spawn and set exec.
515 *
516 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
517 * test that spawned binary has the temp owner push of the port. The spawned binary will
518 * posix_spawn set exec and verify that it still has the push.
519 */
520 T_DECL(posix_spawn_set_exec_basic_priority, "Basic posix spawn set exec temp owner priority test", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
521 {
522 mach_port_t port;
523 pid_t pid;
524 int stat;
525
526 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
527 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "SETEXEC", &port, 1);
528
529 waitpid(pid, &stat, 0);
530
531 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
532 T_EXPECT_EQ(WEXITSTATUS(stat), 47, "spawn set exec did not properly boost main thread");
533 T_END;
534 }
535
536 /*
537 * Test 4: Test turnstile boosting for temp owner ports for posix_spawn and set exec.
538 *
539 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
540 * test that spawned binary has the temp owner push of the port. The spawned binary already
541 * having the temp owner push will try to do set exec with watchports which should fail.
542 */
543 T_DECL(posix_spawn_set_exec_with_more_ports, "posix spawn set exec with more watch ports", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
544 {
545 mach_port_t port;
546 pid_t pid;
547 int stat;
548
549 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
550 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "SETEXEC_PORTS", &port, 1);
551
552 waitpid(pid, &stat, 0);
553
554 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
555 T_EXPECT_EQ(WEXITSTATUS(stat), EINVAL, "spawn set exec did not error out when watchports were passed to already boosted process");
556 T_END;
557 }
558
559 /*
560 * Test 5: Test turnstile boosting for temp owner ports for multiple posix_spawns.
561 *
562 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port, then
563 * pass the same port as a watchport to another posix_spawn and verify that the boost was
564 * transferred to the new process.
565 */
566 T_DECL(posix_spawn_multiple, "multiple posix_spawn with same watchport", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
567 {
568 mach_port_t port;
569 pid_t pid1, pid2;
570 int stat1, stat2;
571
572 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
573 pid1 = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
574
575 /* Let the child 1 execute a little, the sleep here is optional */
576 sleep(2);
577
578 pid2 = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXIT", &port, 1);
579
580 waitpid(pid2, &stat2, 0);
581 waitpid(pid1, &stat1, 0);
582
583 T_QUIET; T_LOG("The return stat for child 1 is is %d", WEXITSTATUS(stat1));
584 T_QUIET; T_LOG("The return stat for child 2 is is %d", WEXITSTATUS(stat2));
585 T_EXPECT_EQ(WEXITSTATUS(stat2), 47, "spawn of multiple processes with same watchport did not transfer the boost correctly");
586 T_EXPECT_EQ(WEXITSTATUS(stat1), 31, "spawn of multiple processes with same watchport did not transfer the boost correctly");
587 T_END;
588 }
589
590 /*
591 * Test 6: Test turnstile boosting for temp owner ports for posix_spawn for dead port.
592 *
593 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
594 * test that spawned binary has the temp owner push of the port. Destroy the port and verify
595 * the temp owner push has gone away.
596 */
597 T_DECL(posix_spawn_dead_reply_port, "posix spawn with reply port destory", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
598 {
599 mach_port_t port;
600 kern_return_t kr;
601 pid_t pid;
602 int stat;
603
604 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
605 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
606
607 /* Let the child execute a little, the sleep here is optional */
608 sleep(2);
609
610 /* Destory the special reply port */
611 kr = mach_port_mod_refs(mach_task_self(), sr_port, MACH_PORT_RIGHT_RECEIVE, -1);
612 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "posix_spaw_dead_port mach_port_mod_refs");
613
614 waitpid(pid, &stat, 0);
615
616 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
617 T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost was not removed on port death");
618 T_END;
619 }
620
621 /*
622 * Test 7: Test turnstile boosting for temp owner ports for posix_spawn for dead port.
623 *
624 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
625 * test that spawned binary has the temp owner push of the port. Destroy the port and verify
626 * the temp owner push has gone.
627 */
628 T_DECL(posix_spawn_dead_port, "posix spawn with port destory", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
629 {
630 mach_port_t port;
631 kern_return_t kr;
632 pid_t pid;
633 int stat;
634
635 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
636 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
637
638 /* Destory the port */
639 kr = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
640 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "posix_spaw_dead_port mach_port_mod_refs");
641
642 waitpid(pid, &stat, 0);
643
644 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
645 T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost was not removed on port death");
646 T_END;
647 }
648
649 /*
650 * Test 8: Test turnstile boosting for temp owner ports for posix_spawn when port is copied in.
651 *
652 * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
653 * test that spawned binary has the temp owner push of the port. Copyin the port and verify
654 * the temp owner push has gone.
655 */
656 T_DECL(posix_spawn_copyin_port, "posix spawn with copyin port", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
657 {
658 mach_port_t port;
659 pid_t pid;
660 int stat;
661
662 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
663 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
664
665 /* Let the child execute a little, the sleep here is optional */
666 sleep(2);
667
668 /* Copyin the port in another port */
669 create_port_and_copyin_a_port(port);
670
671 waitpid(pid, &stat, 0);
672
673 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
674 T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost was not removed on port copyin");
675 T_END;
676 }
677
678 /*
679 * Test 9: Test turnstile boosting for temp owner ports for posix_spawn with multiple ports.
680 *
681 * Create multiple ports with sync IPC push and then pass the port to posix_spawn as watch ports and
682 * test that spawned binary has the temp owner push of the ports. Copyin ports one by one and verify
683 * the push has gone.
684 */
685 T_DECL(posix_spawn_multiple_port, "posix spawn with multiple ports", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
686 {
687 mach_port_t port[2];
688 pid_t pid;
689 int stat;
690
691 port[0] = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
692 port[1] = get_sync_push_port_at_qos(QOS_CLASS_USER_INITIATED);
693 pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "MULTIWAIT", port, 2);
694
695 /* Let the child execute a little, the sleep here is optional */
696 sleep(2);
697
698 /* Copyin the port in another port */
699 create_port_and_copyin_a_port(port[0]);
700
701 /* Let the child execute a little, the sleep here is optional */
702 sleep(2);
703
704 /* Copyin the port in another port */
705 create_port_and_copyin_a_port(port[1]);
706
707 waitpid(pid, &stat, 0);
708
709 T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
710 T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost did not work correctly with multiple ports");
711 T_END;
712 }
713
714 /*
715 * Test 10: Test turnstile boosting for temp owner ports for posix_spawn when port attached to a knote.
716 *
717 * Create a port with sync IPC push attach a workloop knote to it, send a message on the port, then in the
718 * servicer pass the port to posix_spawn as a watch port and test that spawned binary has the temp owner
719 * push of the port and the servicer looses the boost.
720 */
721 T_DECL(posix_spawn_knote, "posix spawn with temp owner port attached to knote", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
722 {
723 mach_port_t port;
724
725 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
726
727 /* attach port to a workloop */
728 register_workloop_for_port(port, workloop_cb_test_intransit, MACH_RCV_OPTIONS);
729
730 /* send a message on port to activate workloop handler */
731 send(port, MACH_PORT_NULL, MACH_PORT_NULL,
732 mach_msg_priority_encode(0, THREAD_QOS_LEGACY, 0), 0, MACH_MSG_TYPE_COPY_SEND);
733 sigsuspend(0);
734 }
735
736 /*
737 * Test 11: Test turnstile boosting for temp owner ports for posix_spawn when port attached to a knote.
738 *
739 * Create a port with sync IPC push attach a workloop knote to it, send a message on the port, then in the
740 * servicer pass the port to posix_spawn as a watch port and test that spawned binary has the temp owner
741 * push of the port and the servicer looses the boost, verify that once the spawned binary dies, the servicer
742 * gets the push.
743 */
744 T_DECL(posix_spawn_knote_ret, "posix spawn with temp owner port attached to knote with spawned binary dead", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
745 {
746 mach_port_t port;
747
748 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
749
750 register_workloop_for_port(port, workloop_cb_test_knote_kill, MACH_RCV_OPTIONS);
751
752 /* send a message on port to activate workloop handler */
753 send(port, MACH_PORT_NULL, MACH_PORT_NULL,
754 mach_msg_priority_encode(0, THREAD_QOS_LEGACY, 0), 0, MACH_MSG_TYPE_COPY_SEND);
755 sigsuspend(0);
756 }
757
758 /*
759 * Test 12: Test turnstile boosting for temp owner ports and mach msg option for sync bootstrap_checkin.
760 *
761 * Create a port with sync IPC push attach a workloop knote to it, send a message on the port, then in the
762 * servicer pass the port to posix_spawn as a watch port and test that spawned binary has the temp owner
763 * push of the port and the servicer looses the boost, the spawn binary then does a sync bootstrap_checkin
764 * with test binary to get the receive right and verify that is still has the boost.
765 */
766 T_DECL(mach_msg_sync_boostrap_checkin, "test mach msg option for sync bootstrap_checkin", T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED)
767 {
768 mach_port_t port;
769 mach_port_t sync_port;
770 kern_return_t kr;
771
772 port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
773
774 register_workloop_for_port(port, workloop_cb_test_sync_bootstrap, MACH_RCV_SYNC_PEEK);
775
776 /* Create a mach port for spawned binary to do bootstrap checkin */
777 kr = mach_port_allocate(mach_task_self(),
778 MACH_PORT_RIGHT_RECEIVE,
779 &sync_port);
780
781 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_port_allocate");
782
783 kr = mach_port_insert_right(mach_task_self(),
784 sync_port,
785 sync_port,
786 MACH_MSG_TYPE_MAKE_SEND);
787
788 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_port_insert_right");
789
790 kr = mach_port_mod_refs(mach_task_self(), sync_port, MACH_PORT_RIGHT_SEND, 1);
791 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_port_mod_refs");
792
793 register_workloop_for_port(sync_port, NULL, MACH_RCV_OPTIONS);
794
795 /* Stash the port in task to make sure child also gets it */
796 kr = mach_ports_register(mach_task_self(), &sync_port, 1);
797 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_ports_register");
798
799 /* send a message on port to activate workloop handler */
800 send(port, MACH_PORT_NULL, MACH_PORT_NULL,
801 mach_msg_priority_encode(0, THREAD_QOS_LEGACY, 0), 0, MACH_MSG_TYPE_COPY_SEND);
802 sigsuspend(0);
803 }
804