xref: /xnu-11417.121.6/tests/ipc/ipc_thread_ports_race.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
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 			T_QUIET; T_ASSERT_EQ(rc, 0, "pthread_create[%d]", i);
41 		}
42 
43 		for (int i = 0; i < BATCH; i++) {
44 			int rc = pthread_join(th[i], NULL);
45 			T_QUIET; T_ASSERT_EQ(rc, 0, "pthread_join[%d]", i);
46 		}
47 	}
48 }
49 
50 static void *
thread_creation_bomb(void * arg)51 thread_creation_bomb(void *arg)
52 {
53 	done = 0;
54 	dispatch_apply_f((size_t)dt_ncpu(), DISPATCH_APPLY_AUTO, NULL,
55 	    thread_creation_bomb_one);
56 	return arg;
57 }
58 
59 static void
test_race(const char * how,task_t task,pid_t pid)60 test_race(const char *how, task_t task, pid_t pid)
61 {
62 	thread_array_t threadList;
63 	mach_msg_type_number_t threadCount = 0;
64 	kern_return_t kr;
65 	uint32_t ths = 0;
66 
67 	T_LOG("Starting: %s (port: %#x, pid: %d)", how, task, pid);
68 
69 	for (uint32_t n = 0; n < COUNT; n++) {
70 		kr = task_threads(task, &threadList, &threadCount);
71 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_threads");
72 
73 		for (mach_msg_type_number_t i = 0; i < threadCount; i++) {
74 			mach_port_deallocate(mach_task_self(), threadList[i]);
75 		}
76 
77 		vm_deallocate(mach_task_self(), (vm_address_t)threadList,
78 		    sizeof(threadList[0]) * threadCount);
79 		ths += threadCount;
80 	}
81 
82 	T_PASS("Done %d loops of %s, found %d threads", COUNT, how, ths);
83 
84 	if (task != mach_task_self()) {
85 		mach_port_deallocate(mach_task_self(), task);
86 	}
87 
88 	if (pid) {
89 		kill(pid, SIGKILL);
90 		waitpid(pid, NULL, 0);
91 	} else {
92 		done = 1;
93 	}
94 }
95 
96 static void
local_test(bool control,task_t tp)97 local_test(bool control, task_t tp)
98 {
99 	pthread_t th;
100 	int rc;
101 
102 	rc = pthread_create(&th, NULL, thread_creation_bomb, NULL);
103 	T_QUIET; T_ASSERT_EQ(rc, 0, "started job");
104 
105 	test_race(control ? "local(ctl)" : "local(read)", tp, 0);
106 
107 	rc = pthread_join(th, NULL);
108 	T_QUIET; T_ASSERT_EQ(rc, 0, "done");
109 }
110 
111 static void
fork_child_test(bool control)112 fork_child_test(bool control)
113 {
114 	task_t tp;
115 	pid_t pid;
116 
117 	signal(SIGCHLD, SIG_IGN);
118 
119 	pid = fork();
120 	if (pid == 0) {
121 		thread_creation_bomb(NULL);
122 		exit(0);
123 	}
124 
125 	if (pid < 0) {
126 		T_ASSERT_POSIX_SUCCESS(pid, "fork");
127 	}
128 
129 	if (control) {
130 		kern_return_t kr = task_for_pid(mach_task_self(), pid, &tp);
131 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid");
132 	} else {
133 		int rc = task_read_for_pid(mach_task_self(), pid, &tp);
134 		T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "task_read_for_pid");
135 	}
136 
137 	test_race(control ? "remote(ctl)" : "remote(read)", tp, pid);
138 }
139 
140 T_DECL(thred_ports_termination_race,
141     "Test for various termination races with thread termination")
142 {
143 	kern_return_t kr;
144 	task_read_t tp;
145 
146 	/*
147 	 * we must do the remote tests first so that we can fork()
148 	 * and still use dispatch.
149 	 */
150 	fork_child_test(true);
151 
152 	fork_child_test(false);
153 
154 	local_test(true, mach_task_self());
155 
156 	kr = task_get_special_port(mach_task_self(), TASK_READ_PORT, &tp);
157 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port(TASK_READ_PORT)");
158 	local_test(false, tp);
159 }
160