xref: /xnu-10002.81.5/tests/ipc_thread_ports_race.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <darwintest.h>
2*5e3eaea3SApple OSS Distributions #include <darwintest_multiprocess.h>
3*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
4*5e3eaea3SApple OSS Distributions 
5*5e3eaea3SApple OSS Distributions #include <mach/mach.h>
6*5e3eaea3SApple OSS Distributions #include <mach/task.h>
7*5e3eaea3SApple OSS Distributions #include <mach/port.h>
8*5e3eaea3SApple OSS Distributions #include <pthread.h>
9*5e3eaea3SApple OSS Distributions #include <dispatch/dispatch.h>
10*5e3eaea3SApple OSS Distributions #include <sys/proc.h>
11*5e3eaea3SApple OSS Distributions #include <signal.h>
12*5e3eaea3SApple OSS Distributions 
13*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
14*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
15*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
16*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
17*5e3eaea3SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
18*5e3eaea3SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(NO));
19*5e3eaea3SApple OSS Distributions 
20*5e3eaea3SApple OSS Distributions #define BATCH 10
21*5e3eaea3SApple OSS Distributions #define COUNT 4000
22*5e3eaea3SApple OSS Distributions 
23*5e3eaea3SApple OSS Distributions static int done;
24*5e3eaea3SApple OSS Distributions 
25*5e3eaea3SApple OSS Distributions static void *
thread_do_nothing(void * arg)26*5e3eaea3SApple OSS Distributions thread_do_nothing(void *arg)
27*5e3eaea3SApple OSS Distributions {
28*5e3eaea3SApple OSS Distributions 	return arg;
29*5e3eaea3SApple OSS Distributions }
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions static void
thread_creation_bomb_one(void * _ctx __unused,size_t _i __unused)32*5e3eaea3SApple OSS Distributions thread_creation_bomb_one(void *_ctx __unused, size_t _i __unused)
33*5e3eaea3SApple OSS Distributions {
34*5e3eaea3SApple OSS Distributions 	while (!done) {
35*5e3eaea3SApple OSS Distributions 		pthread_t th[BATCH];
36*5e3eaea3SApple OSS Distributions 
37*5e3eaea3SApple OSS Distributions 		for (int i = 0; i < BATCH; i++) {
38*5e3eaea3SApple OSS Distributions 			int rc = pthread_create(&th[i], NULL, thread_do_nothing, NULL);
39*5e3eaea3SApple OSS Distributions 			T_QUIET; T_ASSERT_EQ(rc, 0, "pthread_create[%d]", i);
40*5e3eaea3SApple OSS Distributions 		}
41*5e3eaea3SApple OSS Distributions 
42*5e3eaea3SApple OSS Distributions 		for (int i = 0; i < BATCH; i++) {
43*5e3eaea3SApple OSS Distributions 			int rc = pthread_join(th[i], NULL);
44*5e3eaea3SApple OSS Distributions 			T_QUIET; T_ASSERT_EQ(rc, 0, "pthread_join[%d]", i);
45*5e3eaea3SApple OSS Distributions 		}
46*5e3eaea3SApple OSS Distributions 	}
47*5e3eaea3SApple OSS Distributions }
48*5e3eaea3SApple OSS Distributions 
49*5e3eaea3SApple OSS Distributions static void *
thread_creation_bomb(void * arg)50*5e3eaea3SApple OSS Distributions thread_creation_bomb(void *arg)
51*5e3eaea3SApple OSS Distributions {
52*5e3eaea3SApple OSS Distributions 	done = 0;
53*5e3eaea3SApple OSS Distributions 	dispatch_apply_f((size_t)dt_ncpu(), DISPATCH_APPLY_AUTO, NULL,
54*5e3eaea3SApple OSS Distributions 	    thread_creation_bomb_one);
55*5e3eaea3SApple OSS Distributions 	return arg;
56*5e3eaea3SApple OSS Distributions }
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions static void
test_race(const char * how,task_t task,pid_t pid)59*5e3eaea3SApple OSS Distributions test_race(const char *how, task_t task, pid_t pid)
60*5e3eaea3SApple OSS Distributions {
61*5e3eaea3SApple OSS Distributions 	thread_array_t threadList;
62*5e3eaea3SApple OSS Distributions 	mach_msg_type_number_t threadCount = 0;
63*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
64*5e3eaea3SApple OSS Distributions 	uint32_t ths = 0;
65*5e3eaea3SApple OSS Distributions 
66*5e3eaea3SApple OSS Distributions 	T_LOG("Starting: %s (port: %#x, pid: %d)", how, task, pid);
67*5e3eaea3SApple OSS Distributions 
68*5e3eaea3SApple OSS Distributions 	for (uint32_t n = 0; n < COUNT; n++) {
69*5e3eaea3SApple OSS Distributions 		kr = task_threads(task, &threadList, &threadCount);
70*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_threads");
71*5e3eaea3SApple OSS Distributions 
72*5e3eaea3SApple OSS Distributions 		for (mach_msg_type_number_t i = 0; i < threadCount; i++) {
73*5e3eaea3SApple OSS Distributions 			mach_port_deallocate(mach_task_self(), threadList[i]);
74*5e3eaea3SApple OSS Distributions 		}
75*5e3eaea3SApple OSS Distributions 
76*5e3eaea3SApple OSS Distributions 		vm_deallocate(mach_task_self(), (vm_address_t)threadList,
77*5e3eaea3SApple OSS Distributions 		    sizeof(threadList[0]) * threadCount);
78*5e3eaea3SApple OSS Distributions 		ths += threadCount;
79*5e3eaea3SApple OSS Distributions 	}
80*5e3eaea3SApple OSS Distributions 
81*5e3eaea3SApple OSS Distributions 	T_PASS("Done %d loops of %s, found %d threads", COUNT, how, ths);
82*5e3eaea3SApple OSS Distributions 
83*5e3eaea3SApple OSS Distributions 	if (task != mach_task_self()) {
84*5e3eaea3SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), task);
85*5e3eaea3SApple OSS Distributions 	}
86*5e3eaea3SApple OSS Distributions 
87*5e3eaea3SApple OSS Distributions 	if (pid) {
88*5e3eaea3SApple OSS Distributions 		kill(pid, SIGKILL);
89*5e3eaea3SApple OSS Distributions 		waitpid(pid, NULL, 0);
90*5e3eaea3SApple OSS Distributions 	} else {
91*5e3eaea3SApple OSS Distributions 		done = 1;
92*5e3eaea3SApple OSS Distributions 	}
93*5e3eaea3SApple OSS Distributions }
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions static void
local_test(bool control,task_t tp)96*5e3eaea3SApple OSS Distributions local_test(bool control, task_t tp)
97*5e3eaea3SApple OSS Distributions {
98*5e3eaea3SApple OSS Distributions 	pthread_t th;
99*5e3eaea3SApple OSS Distributions 	int rc;
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions 	rc = pthread_create(&th, NULL, thread_creation_bomb, NULL);
102*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(rc, 0, "started job");
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions 	test_race(control ? "local(ctl)" : "local(read)", tp, 0);
105*5e3eaea3SApple OSS Distributions 
106*5e3eaea3SApple OSS Distributions 	rc = pthread_join(th, NULL);
107*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(rc, 0, "done");
108*5e3eaea3SApple OSS Distributions }
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions static void
fork_child_test(bool control)111*5e3eaea3SApple OSS Distributions fork_child_test(bool control)
112*5e3eaea3SApple OSS Distributions {
113*5e3eaea3SApple OSS Distributions 	task_t tp;
114*5e3eaea3SApple OSS Distributions 	pid_t pid;
115*5e3eaea3SApple OSS Distributions 
116*5e3eaea3SApple OSS Distributions 	signal(SIGCHLD, SIG_IGN);
117*5e3eaea3SApple OSS Distributions 
118*5e3eaea3SApple OSS Distributions 	pid = fork();
119*5e3eaea3SApple OSS Distributions 	if (pid == 0) {
120*5e3eaea3SApple OSS Distributions 		thread_creation_bomb(NULL);
121*5e3eaea3SApple OSS Distributions 		exit(0);
122*5e3eaea3SApple OSS Distributions 	}
123*5e3eaea3SApple OSS Distributions 
124*5e3eaea3SApple OSS Distributions 	if (pid < 0) {
125*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(pid, "fork");
126*5e3eaea3SApple OSS Distributions 	}
127*5e3eaea3SApple OSS Distributions 
128*5e3eaea3SApple OSS Distributions 	if (control) {
129*5e3eaea3SApple OSS Distributions 		kern_return_t kr = task_for_pid(mach_task_self(), pid, &tp);
130*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");
131*5e3eaea3SApple OSS Distributions 	} else {
132*5e3eaea3SApple OSS Distributions 		int rc = task_read_for_pid(mach_task_self(), pid, &tp);
133*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "task_read_for_pid");
134*5e3eaea3SApple OSS Distributions 	}
135*5e3eaea3SApple OSS Distributions 
136*5e3eaea3SApple OSS Distributions 	test_race(control ? "remote(ctl)" : "remote(read)", tp, pid);
137*5e3eaea3SApple OSS Distributions }
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions T_DECL(thred_ports_termination_race,
140*5e3eaea3SApple OSS Distributions     "Test for various termination races with thread termination")
141*5e3eaea3SApple OSS Distributions {
142*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
143*5e3eaea3SApple OSS Distributions 	task_read_t tp;
144*5e3eaea3SApple OSS Distributions 
145*5e3eaea3SApple OSS Distributions 	/*
146*5e3eaea3SApple OSS Distributions 	 * we must do the remote tests first so that we can fork()
147*5e3eaea3SApple OSS Distributions 	 * and still use dispatch.
148*5e3eaea3SApple OSS Distributions 	 */
149*5e3eaea3SApple OSS Distributions 	fork_child_test(true);
150*5e3eaea3SApple OSS Distributions 
151*5e3eaea3SApple OSS Distributions 	fork_child_test(false);
152*5e3eaea3SApple OSS Distributions 
153*5e3eaea3SApple OSS Distributions 	local_test(true, mach_task_self());
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions 	kr = task_get_special_port(mach_task_self(), TASK_READ_PORT, &tp);
156*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port(TASK_READ_PORT)");
157*5e3eaea3SApple OSS Distributions 	local_test(false, tp);
158*5e3eaea3SApple OSS Distributions }
159