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