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