xref: /xnu-8796.141.3/tests/exec-race-58566604.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <assert.h>
2*1b191cb5SApple OSS Distributions #include <stdio.h>
3*1b191cb5SApple OSS Distributions #include <pthread.h>
4*1b191cb5SApple OSS Distributions #include <signal.h>
5*1b191cb5SApple OSS Distributions #include <unistd.h>
6*1b191cb5SApple OSS Distributions #include <errno.h>
7*1b191cb5SApple OSS Distributions #include <string.h>
8*1b191cb5SApple OSS Distributions #include <sys/wait.h>
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions #include <darwintest.h>
11*1b191cb5SApple OSS Distributions 
12*1b191cb5SApple OSS Distributions // rdar://58566604
13*1b191cb5SApple OSS Distributions // Exercise races of signal delivery vs exec in multi-threaded processes
14*1b191cb5SApple OSS Distributions 
15*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.exec"),
16*1b191cb5SApple OSS Distributions     T_META_CHECK_LEAKS(false),
17*1b191cb5SApple OSS Distributions     T_META_ALL_VALID_ARCHS(true));
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions enum { KILL_ONCE, KILL_MANY, KILL_LAST } kill_mode;
20*1b191cb5SApple OSS Distributions enum { EXEC_FIRST, EXEC_SECOND, EXEC_LAST } exec_mode;
21*1b191cb5SApple OSS Distributions 
22*1b191cb5SApple OSS Distributions static int fd[2];
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions static void
do_exec(void)25*1b191cb5SApple OSS Distributions do_exec(void)
26*1b191cb5SApple OSS Distributions {
27*1b191cb5SApple OSS Distributions 	char echo_arg[50] = "";
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions 	snprintf(echo_arg, sizeof(echo_arg), "            Child[%d] says hello after exec", getpid());
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions 	char * new_argv[] = {
32*1b191cb5SApple OSS Distributions 		"/bin/echo",
33*1b191cb5SApple OSS Distributions 		echo_arg,
34*1b191cb5SApple OSS Distributions 		NULL
35*1b191cb5SApple OSS Distributions 	};
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions 	int ret = execv(new_argv[0], new_argv);
38*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "execv()");
39*1b191cb5SApple OSS Distributions }
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions static void*
thread_main(void * arg)42*1b191cb5SApple OSS Distributions thread_main(void* arg)
43*1b191cb5SApple OSS Distributions {
44*1b191cb5SApple OSS Distributions 	T_LOG("mode: %d, %d: Child[%d] created second thread\n",
45*1b191cb5SApple OSS Distributions 	    kill_mode, exec_mode, getpid());
46*1b191cb5SApple OSS Distributions 
47*1b191cb5SApple OSS Distributions 	if (exec_mode == EXEC_SECOND) {
48*1b191cb5SApple OSS Distributions 		int ret = dprintf(fd[1], "Hi!");
49*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "dprintf()");
50*1b191cb5SApple OSS Distributions 		do_exec();
51*1b191cb5SApple OSS Distributions 	}
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions 	while (1) {
54*1b191cb5SApple OSS Distributions 	}
55*1b191cb5SApple OSS Distributions 	return NULL;
56*1b191cb5SApple OSS Distributions }
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions void
run_test(void)59*1b191cb5SApple OSS Distributions run_test(void)
60*1b191cb5SApple OSS Distributions {
61*1b191cb5SApple OSS Distributions 	T_LOG("mode: %d, %d: Parent[%d]: forking\n",
62*1b191cb5SApple OSS Distributions 	    kill_mode, exec_mode, getpid());
63*1b191cb5SApple OSS Distributions 
64*1b191cb5SApple OSS Distributions 	pid_t child_pid = fork();
65*1b191cb5SApple OSS Distributions 
66*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork()");
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions 	int ret = 0;
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions 	if (child_pid == 0) {
71*1b191cb5SApple OSS Distributions 		pthread_t thread;
72*1b191cb5SApple OSS Distributions 		ret = pthread_create(&thread, NULL, thread_main, NULL);
73*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create()");
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 		if (exec_mode == EXEC_FIRST) {
76*1b191cb5SApple OSS Distributions 			ret = dprintf(fd[1], "Hi!");
77*1b191cb5SApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "dprintf()");
78*1b191cb5SApple OSS Distributions 
79*1b191cb5SApple OSS Distributions 			do_exec();
80*1b191cb5SApple OSS Distributions 		}
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions 		while (1) {
83*1b191cb5SApple OSS Distributions 		}
84*1b191cb5SApple OSS Distributions 	} else {
85*1b191cb5SApple OSS Distributions 		char buffer[4] = "";
86*1b191cb5SApple OSS Distributions 		ret = read(fd[0], buffer, sizeof(buffer));
87*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "read()");
88*1b191cb5SApple OSS Distributions 
89*1b191cb5SApple OSS Distributions 		T_LOG("mode: %d, %d: Parent[%d]: got: '%s' from execing child, trying to kill and wait\n",
90*1b191cb5SApple OSS Distributions 		    kill_mode, exec_mode, getpid(), buffer);
91*1b191cb5SApple OSS Distributions 
92*1b191cb5SApple OSS Distributions 		int killcount = 0, status = 0, waitedpid = 0;
93*1b191cb5SApple OSS Distributions 
94*1b191cb5SApple OSS Distributions 		switch (kill_mode) {
95*1b191cb5SApple OSS Distributions 		case KILL_ONCE:
96*1b191cb5SApple OSS Distributions 			ret = kill(child_pid, SIGKILL);
97*1b191cb5SApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kill()");
98*1b191cb5SApple OSS Distributions 
99*1b191cb5SApple OSS Distributions 			waitedpid = waitpid(child_pid, &status, 0);
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(waitedpid, "waitpid()");
102*1b191cb5SApple OSS Distributions 
103*1b191cb5SApple OSS Distributions 			killcount++;
104*1b191cb5SApple OSS Distributions 			break;
105*1b191cb5SApple OSS Distributions 		case KILL_MANY:
106*1b191cb5SApple OSS Distributions 			while (waitedpid == 0) {
107*1b191cb5SApple OSS Distributions 				ret = kill(child_pid, SIGKILL);
108*1b191cb5SApple OSS Distributions 				T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kill()");
109*1b191cb5SApple OSS Distributions 
110*1b191cb5SApple OSS Distributions 				waitedpid = waitpid(child_pid, &status, WNOHANG);
111*1b191cb5SApple OSS Distributions 				T_QUIET; T_ASSERT_POSIX_SUCCESS(waitedpid, "waitpid()");
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions 				killcount++;
114*1b191cb5SApple OSS Distributions 			}
115*1b191cb5SApple OSS Distributions 			break;
116*1b191cb5SApple OSS Distributions 		default:
117*1b191cb5SApple OSS Distributions 			break;
118*1b191cb5SApple OSS Distributions 		}
119*1b191cb5SApple OSS Distributions 
120*1b191cb5SApple OSS Distributions 		T_LOG("mode: %d, %d: Parent[%d]: waitpid returned: %d, errno %d (%s), exit signal %d, after %d loops\n",
121*1b191cb5SApple OSS Distributions 		    kill_mode, exec_mode, getpid(), waitedpid, errno, strerror(errno), WTERMSIG(status), killcount);
122*1b191cb5SApple OSS Distributions 	}
123*1b191cb5SApple OSS Distributions }
124*1b191cb5SApple OSS Distributions 
125*1b191cb5SApple OSS Distributions T_DECL(exec_exit_race_once_first, "Exec-exit race, one kill, exec on first thread") {
126*1b191cb5SApple OSS Distributions 	int rv = pipe(fd);
127*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
128*1b191cb5SApple OSS Distributions 
129*1b191cb5SApple OSS Distributions 	kill_mode = KILL_ONCE;
130*1b191cb5SApple OSS Distributions 	exec_mode = EXEC_FIRST;
131*1b191cb5SApple OSS Distributions 
132*1b191cb5SApple OSS Distributions 	for (int i = 0; i < 1000; i++) {
133*1b191cb5SApple OSS Distributions 		run_test();
134*1b191cb5SApple OSS Distributions 	}
135*1b191cb5SApple OSS Distributions }
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions T_DECL(exec_exit_race_many_first, "Exec-exit race, many kill, exec on first thread") {
138*1b191cb5SApple OSS Distributions 	int rv = pipe(fd);
139*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
140*1b191cb5SApple OSS Distributions 
141*1b191cb5SApple OSS Distributions 	kill_mode = KILL_MANY;
142*1b191cb5SApple OSS Distributions 	exec_mode = EXEC_FIRST;
143*1b191cb5SApple OSS Distributions 
144*1b191cb5SApple OSS Distributions 	for (int i = 0; i < 1000; i++) {
145*1b191cb5SApple OSS Distributions 		run_test();
146*1b191cb5SApple OSS Distributions 	}
147*1b191cb5SApple OSS Distributions }
148*1b191cb5SApple OSS Distributions 
149*1b191cb5SApple OSS Distributions T_DECL(exec_exit_race_once_second, "Exec-exit race, one kill, exec on second thread") {
150*1b191cb5SApple OSS Distributions 	int rv = pipe(fd);
151*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
152*1b191cb5SApple OSS Distributions 
153*1b191cb5SApple OSS Distributions 	kill_mode = KILL_ONCE;
154*1b191cb5SApple OSS Distributions 	exec_mode = EXEC_SECOND;
155*1b191cb5SApple OSS Distributions 
156*1b191cb5SApple OSS Distributions 	for (int i = 0; i < 1000; i++) {
157*1b191cb5SApple OSS Distributions 		run_test();
158*1b191cb5SApple OSS Distributions 	}
159*1b191cb5SApple OSS Distributions }
160*1b191cb5SApple OSS Distributions 
161*1b191cb5SApple OSS Distributions T_DECL(exec_exit_race_many_second, "Exec-exit race, many kill, exec on second thread") {
162*1b191cb5SApple OSS Distributions 	int rv = pipe(fd);
163*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pipe()");
164*1b191cb5SApple OSS Distributions 
165*1b191cb5SApple OSS Distributions 	kill_mode = KILL_MANY;
166*1b191cb5SApple OSS Distributions 	exec_mode = EXEC_SECOND;
167*1b191cb5SApple OSS Distributions 
168*1b191cb5SApple OSS Distributions 	for (int i = 0; i < 1000; i++) {
169*1b191cb5SApple OSS Distributions 		run_test();
170*1b191cb5SApple OSS Distributions 	}
171*1b191cb5SApple OSS Distributions }
172