1 /* $NetBSD: t_ptrace_sigchld.c,v 1.3 2020/05/05 18:12:20 kamil Exp $ */
2
3 /*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_ptrace_sigchld.c,v 1.3 2020/05/05 18:12:20 kamil Exp $");
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/exec_elf.h>
35 #include <sys/mman.h>
36 #include <sys/ptrace.h>
37 #include <sys/resource.h>
38 #include <sys/stat.h>
39 #include <sys/syscall.h>
40 #include <sys/sysctl.h>
41 #include <sys/uio.h>
42 #include <sys/wait.h>
43 #include <machine/reg.h>
44 #include <assert.h>
45 #include <elf.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <lwp.h>
50 #include <pthread.h>
51 #include <sched.h>
52 #include <signal.h>
53 #include <spawn.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <strings.h>
58 #include <time.h>
59 #include <unistd.h>
60
61 #include <atf-c.h>
62
63 #include "h_macros.h"
64 #include "msg.h"
65
66 #include "t_ptrace_wait.h"
67
68 #define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \
69 strerror(errno))
70 #define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \
71 "%d(%s) != %d", res, strerror(res), exp)
72
73 static int debug = 0;
74
75 #define DPRINTF(a, ...) do \
76 if (debug) \
77 printf("%s() %d.%d %s:%d " a, \
78 __func__, getpid(), _lwp_self(), __FILE__, __LINE__, ##__VA_ARGS__); \
79 while (/*CONSTCOND*/0)
80
81 /// ----------------------------------------------------------------------------
82
83 static int expected_signo;
84 static int expected_code;
85 static int expected_status;
86 static pid_t expected_pid;
87
88 static void
sigchld_action(int sig,siginfo_t * info,void * ctx)89 sigchld_action(int sig, siginfo_t *info, void *ctx)
90 {
91
92 FORKEE_ASSERT_EQ(info->si_signo, expected_signo);
93 FORKEE_ASSERT_EQ(info->si_code, expected_code);
94 FORKEE_ASSERT_EQ(info->si_uid, getuid());
95 FORKEE_ASSERT_EQ(info->si_pid, expected_pid);
96
97 if (WIFEXITED(info->si_status))
98 ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), expected_status);
99 else if (WIFSTOPPED(info->si_status))
100 ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), expected_status);
101 else if (WIFSIGNALED(info->si_status))
102 ATF_REQUIRE_EQ(WTERMSIG(info->si_status), expected_status);
103 /*
104 else if (WIFCONTINUED(info->si_status))
105 ;
106 */
107 }
108
109 static void
traceme_raise(int sigval)110 traceme_raise(int sigval)
111 {
112 const int exitval = 5;
113 struct sigaction sa;
114 pid_t child;
115 struct msg_fds parent_child;
116 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
117
118 struct ptrace_siginfo info;
119 memset(&info, 0, sizeof(info));
120
121 memset(&sa, 0, sizeof(sa));
122 sa.sa_sigaction = sigchld_action;
123 sa.sa_flags = SA_SIGINFO | SA_NOCLDWAIT;
124 sigemptyset(&sa.sa_mask);
125
126 atf_tc_skip("XXX: zombie is not collected before tracer's death");
127
128 SYSCALL_REQUIRE(sigaction(SIGCHLD, &sa, NULL) == 0);
129
130 SYSCALL_REQUIRE(msg_open(&parent_child) == 0);
131
132 DPRINTF("Before forking process PID=%d\n", getpid());
133 SYSCALL_REQUIRE((child = fork()) != -1);
134 if (child == 0) {
135 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
136 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
137
138 CHILD_FROM_PARENT("raise1 child", parent_child, msg);
139
140 raise(sigval);
141
142 CHILD_TO_PARENT("raise2 child", parent_child, msg);
143
144 switch (sigval) {
145 case SIGKILL:
146 /* NOTREACHED */
147 FORKEE_ASSERTX(0 && "This shall not be reached");
148 __unreachable();
149 default:
150 DPRINTF("Before exiting of the child process\n");
151 _exit(exitval);
152 }
153 }
154 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
155
156 expected_signo = SIGCHLD;
157 expected_pid = child;
158 switch (sigval) {
159 case SIGKILL:
160 expected_code = CLD_KILLED;
161 expected_status = SIGKILL;
162 break;
163 case SIGSTOP:
164 expected_code = CLD_STOPPED;
165 expected_status = SIGSTOP;
166 break;
167 default:
168 break;
169 }
170
171 PARENT_TO_CHILD("raise1 child", parent_child, msg);
172
173 switch (sigval) {
174 case SIGKILL:
175 break;
176 default:
177 PARENT_FROM_CHILD("raise2 child", parent_child, msg);
178
179 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
180 "child\n");
181 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
182 sizeof(info)) != -1);
183
184 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
185 DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
186 "si_errno=%#x\n",
187 info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
188 info.psi_siginfo.si_errno);
189
190 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
191 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
192
193 expected_code = CLD_EXITED;
194 expected_status = exitval;
195
196 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0));
197
198 break;
199 }
200
201 await_collected(child); /* XXX: Process is never collected */
202 }
203
204 #define TRACEME_RAISE(test, sig) \
205 ATF_TC(test); \
206 ATF_TC_HEAD(test, tc) \
207 { \
208 atf_tc_set_md_var(tc, "descr", \
209 "Verify " #sig " followed by _exit(2) in a child"); \
210 atf_tc_set_md_var(tc, "timeout", "10"); \
211 } \
212 \
213 ATF_TC_BODY(test, tc) \
214 { \
215 \
216 traceme_raise(sig); \
217 }
218
TRACEME_RAISE(traceme_raise1,SIGKILL)219 TRACEME_RAISE(traceme_raise1, SIGKILL) /* non-maskable */
220 #if notyet
221 TRACEME_RAISE(traceme_raise2, SIGSTOP) /* non-maskable */
222 TRACEME_RAISE(traceme_raise3, SIGABRT) /* regular abort trap */
223 TRACEME_RAISE(traceme_raise4, SIGHUP) /* hangup */
224 TRACEME_RAISE(traceme_raise5, SIGCONT) /* continued? */
225 TRACEME_RAISE(traceme_raise6, SIGTRAP) /* crash signal */
226 TRACEME_RAISE(traceme_raise7, SIGBUS) /* crash signal */
227 TRACEME_RAISE(traceme_raise8, SIGILL) /* crash signal */
228 TRACEME_RAISE(traceme_raise9, SIGFPE) /* crash signal */
229 TRACEME_RAISE(traceme_raise10, SIGSEGV) /* crash signal */
230 #endif
231
232 ATF_TP_ADD_TCS(tp)
233 {
234 setvbuf(stdout, NULL, _IONBF, 0);
235 setvbuf(stderr, NULL, _IONBF, 0);
236
237 ATF_TP_ADD_TC(tp, traceme_raise1);
238 #if notyet
239 ATF_TP_ADD_TC(tp, traceme_raise2);
240 ATF_TP_ADD_TC(tp, traceme_raise3);
241 ATF_TP_ADD_TC(tp, traceme_raise4);
242 ATF_TP_ADD_TC(tp, traceme_raise5);
243 ATF_TP_ADD_TC(tp, traceme_raise6);
244 ATF_TP_ADD_TC(tp, traceme_raise7);
245 ATF_TP_ADD_TC(tp, traceme_raise8);
246 ATF_TP_ADD_TC(tp, traceme_raise9);
247 ATF_TP_ADD_TC(tp, traceme_raise10);
248 #endif
249
250 return atf_no_error();
251 }
252