xref: /xnu-10063.141.1/tests/posix_spawnattr_set_crash_behavior_np.c (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions #include <stdio.h>
2*d8b80295SApple OSS Distributions #include <sys/sysctl.h>
3*d8b80295SApple OSS Distributions #include <spawn_private.h>
4*d8b80295SApple OSS Distributions #include <signal.h>
5*d8b80295SApple OSS Distributions #include <sys/reason.h>
6*d8b80295SApple OSS Distributions 
7*d8b80295SApple OSS Distributions #ifdef T_NAMESPACE
8*d8b80295SApple OSS Distributions #undef T_NAMESPACE
9*d8b80295SApple OSS Distributions #endif
10*d8b80295SApple OSS Distributions #include <darwintest.h>
11*d8b80295SApple OSS Distributions #include <darwintest_utils.h>
12*d8b80295SApple OSS Distributions 
13*d8b80295SApple OSS Distributions T_GLOBAL_META(
14*d8b80295SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
15*d8b80295SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("spawn"),
16*d8b80295SApple OSS Distributions 	T_META_NAMESPACE("xnu.spawn"));
17*d8b80295SApple OSS Distributions 
18*d8b80295SApple OSS Distributions extern char **environ;
19*d8b80295SApple OSS Distributions 
20*d8b80295SApple OSS Distributions #define SYSCTL_CRASH_BEHAVIOR_TEST_MODE "kern.crash_behavior_test_mode=1"
21*d8b80295SApple OSS Distributions #define SYSCTL_CRASH_BEHAVIOR_WOULD_PANIC "kern.crash_behavior_test_would_panic"
22*d8b80295SApple OSS Distributions 
23*d8b80295SApple OSS Distributions #define TEST_REASON_CODE 5
24*d8b80295SApple OSS Distributions 
25*d8b80295SApple OSS Distributions static void
_do_set_crash_behavior_test(char * child_mode,int signal,uint32_t flags,bool expect_panic)26*d8b80295SApple OSS Distributions _do_set_crash_behavior_test(char *child_mode, int signal, uint32_t flags, bool expect_panic)
27*d8b80295SApple OSS Distributions {
28*d8b80295SApple OSS Distributions 	bool should_wait = (strcmp(child_mode, "wait") == 0);
29*d8b80295SApple OSS Distributions 	bool reason = (strcmp(child_mode, "reason") == 0);
30*d8b80295SApple OSS Distributions 	bool reason_signal = (strcmp(child_mode, "reason_signal") == 0);
31*d8b80295SApple OSS Distributions 	bool dirty = (strcmp(child_mode, "dirty") == 0);
32*d8b80295SApple OSS Distributions 	bool shutdown = (strcmp(child_mode, "clean") == 0) || dirty;
33*d8b80295SApple OSS Distributions 	uint64_t deadline = mach_continuous_time();
34*d8b80295SApple OSS Distributions 
35*d8b80295SApple OSS Distributions 	// 0. clear SYSCTL_CRASH_BEHAVIOR_WOULD_PANIC
36*d8b80295SApple OSS Distributions 	int would_panic = 0;
37*d8b80295SApple OSS Distributions 	size_t length = sizeof(would_panic);
38*d8b80295SApple OSS Distributions 	int ret = sysctlbyname(SYSCTL_CRASH_BEHAVIOR_WOULD_PANIC, NULL, 0, &would_panic, length);
39*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "Clearing SYSCTL_CRASH_BEHAVIOR_WOULD_PANIC");
40*d8b80295SApple OSS Distributions 
41*d8b80295SApple OSS Distributions 	// 1. posix_spawn a child process
42*d8b80295SApple OSS Distributions 	char *test_program = "./posix_spawnattr_set_crash_behavior_np_child";
43*d8b80295SApple OSS Distributions 	char *child_args[3];
44*d8b80295SApple OSS Distributions 
45*d8b80295SApple OSS Distributions 	posix_spawnattr_t attrs;
46*d8b80295SApple OSS Distributions 	posix_spawnattr_init(&attrs);
47*d8b80295SApple OSS Distributions 
48*d8b80295SApple OSS Distributions 	ret = posix_spawnattr_set_crash_behavior_np(&attrs, flags);
49*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_set_crash_behavior_np");
50*d8b80295SApple OSS Distributions 
51*d8b80295SApple OSS Distributions 
52*d8b80295SApple OSS Distributions 	if (should_wait) {
53*d8b80295SApple OSS Distributions 		// For the purpose of the test we set the deadline to be now to avoid
54*d8b80295SApple OSS Distributions 		// making the test wait
55*d8b80295SApple OSS Distributions 		ret = posix_spawnattr_set_crash_behavior_deadline_np(&attrs, deadline, flags);
56*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_set_crash_behavior_deadline_np: %lld", deadline);
57*d8b80295SApple OSS Distributions 	}
58*d8b80295SApple OSS Distributions 
59*d8b80295SApple OSS Distributions 	child_args[0] = test_program;
60*d8b80295SApple OSS Distributions 	child_args[1] = child_mode;
61*d8b80295SApple OSS Distributions 	child_args[2] = NULL;
62*d8b80295SApple OSS Distributions 
63*d8b80295SApple OSS Distributions 	pid_t child_pid = 0;
64*d8b80295SApple OSS Distributions 	ret = posix_spawn(&child_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
65*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
66*d8b80295SApple OSS Distributions 	posix_spawnattr_destroy(&attrs);
67*d8b80295SApple OSS Distributions 
68*d8b80295SApple OSS Distributions 	if (should_wait) {
69*d8b80295SApple OSS Distributions 		while (mach_continuous_time() <= deadline) {
70*d8b80295SApple OSS Distributions 			usleep(1);
71*d8b80295SApple OSS Distributions 		}
72*d8b80295SApple OSS Distributions 	}
73*d8b80295SApple OSS Distributions 
74*d8b80295SApple OSS Distributions 	if (signal != 0) {
75*d8b80295SApple OSS Distributions 		ret = kill(child_pid, signal);
76*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "kill(%d, %d)", child_pid, signal);
77*d8b80295SApple OSS Distributions 	}
78*d8b80295SApple OSS Distributions 
79*d8b80295SApple OSS Distributions 	if (reason) {
80*d8b80295SApple OSS Distributions 		ret = terminate_with_reason(child_pid, OS_REASON_TEST, TEST_REASON_CODE,
81*d8b80295SApple OSS Distributions 		    "Test forcing crash", OS_REASON_FLAG_CONSISTENT_FAILURE | OS_REASON_FLAG_NO_CRASH_REPORT);
82*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "terminate_with_reason(%d)", child_pid);
83*d8b80295SApple OSS Distributions 	} else if (reason_signal) {
84*d8b80295SApple OSS Distributions 		ret = terminate_with_reason(child_pid, OS_REASON_SIGNAL, SIGABRT,
85*d8b80295SApple OSS Distributions 		    "Test forcing crash with signal", OS_REASON_FLAG_CONSISTENT_FAILURE | OS_REASON_FLAG_NO_CRASH_REPORT);
86*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "terminate_with_reason_signal(%d)", child_pid);
87*d8b80295SApple OSS Distributions 	}
88*d8b80295SApple OSS Distributions 
89*d8b80295SApple OSS Distributions 	if (dirty) {
90*d8b80295SApple OSS Distributions 		ret = proc_set_dirty(child_pid, true);
91*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "proc_set_dirty(%d)", child_pid);
92*d8b80295SApple OSS Distributions 	}
93*d8b80295SApple OSS Distributions 
94*d8b80295SApple OSS Distributions 	if (shutdown) {
95*d8b80295SApple OSS Distributions 		ret = proc_terminate(child_pid, &signal);
96*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "proc_terminate(%d, %d)", child_pid, signal);
97*d8b80295SApple OSS Distributions 	}
98*d8b80295SApple OSS Distributions 
99*d8b80295SApple OSS Distributions 	// 2. Wait for the child to exit
100*d8b80295SApple OSS Distributions 	int child_status;
101*d8b80295SApple OSS Distributions 	ret = wait4(-1, &child_status, 0, NULL);
102*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "wait4");
103*d8b80295SApple OSS Distributions 
104*d8b80295SApple OSS Distributions 	// 3. Check if we would have panic'ed
105*d8b80295SApple OSS Distributions 	would_panic = 0;
106*d8b80295SApple OSS Distributions 	length = sizeof(would_panic);
107*d8b80295SApple OSS Distributions 	ret = sysctlbyname(SYSCTL_CRASH_BEHAVIOR_WOULD_PANIC, &would_panic, &length, NULL, 0);
108*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "SYSCTL_CRASH_BEHAVIOR_WOULD_PANIC");
109*d8b80295SApple OSS Distributions 
110*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(would_panic, expect_panic, NULL);
111*d8b80295SApple OSS Distributions }
112*d8b80295SApple OSS Distributions 
113*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_with_crash,
114*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_with_crash",
115*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
116*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
117*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("crash", 0, POSIX_SPAWN_PANIC_ON_CRASH, true);
118*d8b80295SApple OSS Distributions }
119*d8b80295SApple OSS Distributions 
120*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_with_exit,
121*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_with_exit",
122*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
123*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
124*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("exit", 0, POSIX_SPAWN_PANIC_ON_CRASH, false);
125*d8b80295SApple OSS Distributions }
126*d8b80295SApple OSS Distributions 
127*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_with_success,
128*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_with_success",
129*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
130*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
131*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("success", 0, POSIX_SPAWN_PANIC_ON_CRASH, false);
132*d8b80295SApple OSS Distributions }
133*d8b80295SApple OSS Distributions 
134*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_nonzero_with_crash,
135*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_nonzero_with_crash",
136*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
137*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
138*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("crash", 0, POSIX_SPAWN_PANIC_ON_NON_ZERO_EXIT, false);
139*d8b80295SApple OSS Distributions }
140*d8b80295SApple OSS Distributions 
141*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_nonzero_with_exit,
142*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_nonzero_with_exit",
143*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
144*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
145*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("exit", 0, POSIX_SPAWN_PANIC_ON_NON_ZERO_EXIT, true);
146*d8b80295SApple OSS Distributions }
147*d8b80295SApple OSS Distributions 
148*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_nonzero_with_success,
149*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_nonzero_with_success",
150*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
151*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
152*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("success", 0, POSIX_SPAWN_PANIC_ON_NON_ZERO_EXIT, false);
153*d8b80295SApple OSS Distributions }
154*d8b80295SApple OSS Distributions 
155*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_cancelled,
156*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_cancelled",
157*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
158*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
159*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("wait", SIGUSR1, POSIX_SPAWN_PANIC_ON_CRASH, false);
160*d8b80295SApple OSS Distributions }
161*d8b80295SApple OSS Distributions 
162*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_sigterm,
163*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_sigterm",
164*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
165*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
166*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("spin", SIGTERM, POSIX_SPAWN_PANIC_ON_CRASH, false);
167*d8b80295SApple OSS Distributions }
168*d8b80295SApple OSS Distributions 
169*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_sigkill,
170*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_sigkill",
171*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
172*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
173*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("spin", SIGKILL, POSIX_SPAWN_PANIC_ON_CRASH, false);
174*d8b80295SApple OSS Distributions }
175*d8b80295SApple OSS Distributions 
176*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_terminate_with_reason,
177*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_terminate_with_reason",
178*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
179*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
180*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("reason", 0, POSIX_SPAWN_PANIC_ON_CRASH, true);
181*d8b80295SApple OSS Distributions }
182*d8b80295SApple OSS Distributions 
183*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_terminate_with_reason_signal,
184*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_terminate_with_reason_signal",
185*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
186*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
187*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("reason_signal", 0, POSIX_SPAWN_PANIC_ON_CRASH, true);
188*d8b80295SApple OSS Distributions }
189*d8b80295SApple OSS Distributions 
190*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_proc_terminate_clean,
191*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_proc_terminate_clean",
192*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
193*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
194*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("clean", 0, POSIX_SPAWN_PANIC_ON_CRASH, false);
195*d8b80295SApple OSS Distributions }
196*d8b80295SApple OSS Distributions 
197*d8b80295SApple OSS Distributions T_DECL(set_crash_behavior_panic_on_crash_proc_terminate_dirty,
198*d8b80295SApple OSS Distributions     "set_crash_behavior_panic_on_crash_proc_terminate_dirty",
199*d8b80295SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_CRASH_BEHAVIOR_TEST_MODE),
200*d8b80295SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) {
201*d8b80295SApple OSS Distributions 	_do_set_crash_behavior_test("dirty", 0, POSIX_SPAWN_PANIC_ON_CRASH, false);
202*d8b80295SApple OSS Distributions }
203