xref: /xnu-8796.141.3/tests/signal_exit_reason.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions /*
30*1b191cb5SApple OSS Distributions  * These tests verify that proc_pidinfo returns the expected exit reason and
31*1b191cb5SApple OSS Distributions  * namespace for signal-related process termination.
32*1b191cb5SApple OSS Distributions  */
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions #include <darwintest.h>
35*1b191cb5SApple OSS Distributions #include <signal.h>
36*1b191cb5SApple OSS Distributions #include <libproc.h>
37*1b191cb5SApple OSS Distributions #include <sys/wait.h>
38*1b191cb5SApple OSS Distributions #include <sys/reason.h>
39*1b191cb5SApple OSS Distributions #include <stdlib.h>
40*1b191cb5SApple OSS Distributions #include <dispatch/dispatch.h>
41*1b191cb5SApple OSS Distributions #include <unistd.h>
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions T_GLOBAL_META(
44*1b191cb5SApple OSS Distributions 	T_META_NAMESPACE("xnu.misc"),
45*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
46*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("misc"));
47*1b191cb5SApple OSS Distributions 
48*1b191cb5SApple OSS Distributions static dispatch_queue_t exit_queue;
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions static void
cleanup(void)51*1b191cb5SApple OSS Distributions cleanup(void)
52*1b191cb5SApple OSS Distributions {
53*1b191cb5SApple OSS Distributions 	dispatch_release(exit_queue);
54*1b191cb5SApple OSS Distributions }
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions static void
57*1b191cb5SApple OSS Distributions dispatch_test(void (^test)(void))
58*1b191cb5SApple OSS Distributions {
59*1b191cb5SApple OSS Distributions 	// Use dispatch to schedule DISPATCH_PROC_EXIT blocks to read out exit reasons
60*1b191cb5SApple OSS Distributions 	exit_queue = dispatch_queue_create("exit queue", DISPATCH_QUEUE_SERIAL);
61*1b191cb5SApple OSS Distributions 	dispatch_async(dispatch_get_main_queue(), ^{
62*1b191cb5SApple OSS Distributions 		test();
63*1b191cb5SApple OSS Distributions 	});
64*1b191cb5SApple OSS Distributions 	T_ATEND(cleanup);
65*1b191cb5SApple OSS Distributions 	dispatch_main();
66*1b191cb5SApple OSS Distributions }
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions static void
check_exit_reason(int pid,uint64_t expected_reason_namespace,uint64_t expected_signal)69*1b191cb5SApple OSS Distributions check_exit_reason(int pid, uint64_t expected_reason_namespace, uint64_t expected_signal)
70*1b191cb5SApple OSS Distributions {
71*1b191cb5SApple OSS Distributions 	T_LOG("check_exit_reason %d", expected_signal);
72*1b191cb5SApple OSS Distributions 	int ret, status;
73*1b191cb5SApple OSS Distributions 	struct proc_exitreasonbasicinfo exit_reason;
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(
76*1b191cb5SApple OSS Distributions 		ret = proc_pidinfo(pid, PROC_PIDEXITREASONBASICINFO, 1, &exit_reason, PROC_PIDEXITREASONBASICINFOSIZE),
77*1b191cb5SApple OSS Distributions 		"verify proc_pidinfo success"
78*1b191cb5SApple OSS Distributions 		);
79*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, PROC_PIDEXITREASONBASICINFOSIZE, "retrieve basic exit reason info");
80*1b191cb5SApple OSS Distributions 
81*1b191cb5SApple OSS Distributions 	waitpid(pid, &status, 0);
82*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_FALSE(WIFEXITED(status), "process did not exit normally");
83*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE(WIFSIGNALED(status), "process was terminated because of a signal");
84*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(WTERMSIG(status), expected_signal, "process should terminate due to signal %llu", expected_signal);
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(exit_reason.beri_namespace, expected_reason_namespace, "expect OS_REASON_SIGNAL");
87*1b191cb5SApple OSS Distributions 	T_EXPECT_EQ(exit_reason.beri_code, expected_signal, "expect reason code: %llu", expected_signal);
88*1b191cb5SApple OSS Distributions }
89*1b191cb5SApple OSS Distributions 
90*1b191cb5SApple OSS Distributions static void
wait_collect_exit_reason(int pid,int signal)91*1b191cb5SApple OSS Distributions wait_collect_exit_reason(int pid, int signal)
92*1b191cb5SApple OSS Distributions {
93*1b191cb5SApple OSS Distributions 	dispatch_source_t ds_proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, exit_queue);
94*1b191cb5SApple OSS Distributions 	dispatch_semaphore_t sem = dispatch_semaphore_create(0);
95*1b191cb5SApple OSS Distributions 	dispatch_source_set_event_handler(ds_proc, ^{
96*1b191cb5SApple OSS Distributions 		check_exit_reason(pid, OS_REASON_SIGNAL, signal);
97*1b191cb5SApple OSS Distributions 		dispatch_semaphore_signal(sem);
98*1b191cb5SApple OSS Distributions 	});
99*1b191cb5SApple OSS Distributions 	dispatch_activate(ds_proc);
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions 	// Wait till exit reason is processed
102*1b191cb5SApple OSS Distributions 	dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
103*1b191cb5SApple OSS Distributions 	dispatch_release(ds_proc);
104*1b191cb5SApple OSS Distributions 	dispatch_release(sem);
105*1b191cb5SApple OSS Distributions }
106*1b191cb5SApple OSS Distributions 
107*1b191cb5SApple OSS Distributions static void
__test_exit_reason_abort()108*1b191cb5SApple OSS Distributions __test_exit_reason_abort()
109*1b191cb5SApple OSS Distributions {
110*1b191cb5SApple OSS Distributions 	pid_t child = fork();
111*1b191cb5SApple OSS Distributions 	if (child > 0) {
112*1b191cb5SApple OSS Distributions 		wait_collect_exit_reason(child, SIGABRT);
113*1b191cb5SApple OSS Distributions 	} else {
114*1b191cb5SApple OSS Distributions 		abort();
115*1b191cb5SApple OSS Distributions 	}
116*1b191cb5SApple OSS Distributions }
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions T_DECL(test_exit_reason_abort, "tests exit reason for abort()")
119*1b191cb5SApple OSS Distributions {
120*1b191cb5SApple OSS Distributions 	dispatch_test(^{
121*1b191cb5SApple OSS Distributions 		__test_exit_reason_abort();
122*1b191cb5SApple OSS Distributions 		T_END;
123*1b191cb5SApple OSS Distributions 	});
124*1b191cb5SApple OSS Distributions }
125*1b191cb5SApple OSS Distributions 
126*1b191cb5SApple OSS Distributions static void
__test_exit_reason_external_signal(int signal)127*1b191cb5SApple OSS Distributions __test_exit_reason_external_signal(int signal)
128*1b191cb5SApple OSS Distributions {
129*1b191cb5SApple OSS Distributions 	T_LOG("Testing external signal %d", signal);
130*1b191cb5SApple OSS Distributions 	pid_t child = fork();
131*1b191cb5SApple OSS Distributions 	if (child > 0) {
132*1b191cb5SApple OSS Distributions 		// Send external signal
133*1b191cb5SApple OSS Distributions 		kill(child, signal);
134*1b191cb5SApple OSS Distributions 		wait_collect_exit_reason(child, signal);
135*1b191cb5SApple OSS Distributions 	} else {
136*1b191cb5SApple OSS Distributions 		pause();
137*1b191cb5SApple OSS Distributions 	}
138*1b191cb5SApple OSS Distributions }
139*1b191cb5SApple OSS Distributions 
140*1b191cb5SApple OSS Distributions T_DECL(test_exit_reason_external_signal, "tests exit reason for external signals")
141*1b191cb5SApple OSS Distributions {
142*1b191cb5SApple OSS Distributions 	dispatch_test(^{
143*1b191cb5SApple OSS Distributions 		__test_exit_reason_external_signal(SIGABRT);
144*1b191cb5SApple OSS Distributions 		__test_exit_reason_external_signal(SIGKILL);
145*1b191cb5SApple OSS Distributions 		__test_exit_reason_external_signal(SIGSYS);
146*1b191cb5SApple OSS Distributions 		__test_exit_reason_external_signal(SIGUSR1);
147*1b191cb5SApple OSS Distributions 		T_END;
148*1b191cb5SApple OSS Distributions 	});
149*1b191cb5SApple OSS Distributions }
150*1b191cb5SApple OSS Distributions 
151*1b191cb5SApple OSS Distributions struct pthread_kill_helper_args {
152*1b191cb5SApple OSS Distributions 	pthread_t *pthread;
153*1b191cb5SApple OSS Distributions 	int signal;
154*1b191cb5SApple OSS Distributions };
155*1b191cb5SApple OSS Distributions 
156*1b191cb5SApple OSS Distributions static void
pthread_kill_helper(void * msg)157*1b191cb5SApple OSS Distributions pthread_kill_helper(void *msg)
158*1b191cb5SApple OSS Distributions {
159*1b191cb5SApple OSS Distributions 	struct pthread_kill_helper_args *args = (struct pthread_kill_helper_args *)msg;
160*1b191cb5SApple OSS Distributions 	pthread_kill(*args->pthread, args->signal);
161*1b191cb5SApple OSS Distributions }
162*1b191cb5SApple OSS Distributions 
163*1b191cb5SApple OSS Distributions static void
__test_exit_reason_pthread_kill_self(int signal)164*1b191cb5SApple OSS Distributions __test_exit_reason_pthread_kill_self(int signal)
165*1b191cb5SApple OSS Distributions {
166*1b191cb5SApple OSS Distributions 	T_LOG("Testing pthread_kill for signal %d", signal);
167*1b191cb5SApple OSS Distributions 	pid_t child = fork();
168*1b191cb5SApple OSS Distributions 	if (child > 0) {
169*1b191cb5SApple OSS Distributions 		wait_collect_exit_reason(child, signal);
170*1b191cb5SApple OSS Distributions 	} else {
171*1b191cb5SApple OSS Distributions 		pthread_t t;
172*1b191cb5SApple OSS Distributions 		struct pthread_kill_helper_args args = {&t, signal};
173*1b191cb5SApple OSS Distributions 		pthread_create(&t, NULL, pthread_kill_helper, (void *)&args);
174*1b191cb5SApple OSS Distributions 		pthread_join(t, NULL);
175*1b191cb5SApple OSS Distributions 	}
176*1b191cb5SApple OSS Distributions }
177*1b191cb5SApple OSS Distributions 
178*1b191cb5SApple OSS Distributions T_DECL(test_exit_reason_pthread_kill_self, "tests exit reason for pthread_kill on caller thread")
179*1b191cb5SApple OSS Distributions {
180*1b191cb5SApple OSS Distributions 	dispatch_test(^{
181*1b191cb5SApple OSS Distributions 		__test_exit_reason_pthread_kill_self(SIGABRT);
182*1b191cb5SApple OSS Distributions 		__test_exit_reason_pthread_kill_self(SIGKILL);
183*1b191cb5SApple OSS Distributions 		__test_exit_reason_pthread_kill_self(SIGSYS);
184*1b191cb5SApple OSS Distributions 		__test_exit_reason_pthread_kill_self(SIGUSR1);
185*1b191cb5SApple OSS Distributions 		T_END;
186*1b191cb5SApple OSS Distributions 	});
187*1b191cb5SApple OSS Distributions }
188