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