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