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