t_ptrace_wait.c revision 1.58
1/*	$NetBSD: t_ptrace_wait.c,v 1.58 2018/05/28 11:35:50 kamil Exp $	*/
2
3/*-
4 * Copyright (c) 2016 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_wait.c,v 1.58 2018/05/28 11:35:50 kamil Exp $");
31
32#include <sys/param.h>
33#include <sys/types.h>
34#include <sys/mman.h>
35#include <sys/ptrace.h>
36#include <sys/resource.h>
37#include <sys/stat.h>
38#include <sys/syscall.h>
39#include <sys/sysctl.h>
40#include <sys/wait.h>
41#include <machine/reg.h>
42#include <elf.h>
43#include <err.h>
44#include <errno.h>
45#include <lwp.h>
46#include <sched.h>
47#include <signal.h>
48#include <stdint.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <strings.h>
52#include <time.h>
53#include <unistd.h>
54
55#include <atf-c.h>
56
57#include "h_macros.h"
58
59#include "t_ptrace_wait.h"
60#include "msg.h"
61
62#define PARENT_TO_CHILD(info, fds, msg) \
63    SYSCALL_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, sizeof(msg)) == 0)
64
65#define CHILD_FROM_PARENT(info, fds, msg) \
66    FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
67
68#define CHILD_TO_PARENT(info, fds, msg) \
69    FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, sizeof(msg)) == 0)
70
71#define PARENT_FROM_CHILD(info, fds, msg) \
72    SYSCALL_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
73
74#define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \
75    strerror(errno))
76#define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \
77    "%d(%s) != %d", res, strerror(res), exp)
78
79static int debug = 0;
80
81#define DPRINTF(a, ...)	do  \
82	if (debug) printf(a,  ##__VA_ARGS__); \
83    while (/*CONSTCOND*/0)
84
85/// ----------------------------------------------------------------------------
86
87static void
88traceme_raise(int sigval)
89{
90	const int exitval = 5;
91	pid_t child, wpid;
92#if defined(TWAIT_HAVE_STATUS)
93	int status;
94#endif
95
96	struct ptrace_siginfo info;
97	memset(&info, 0, sizeof(info));
98
99	DPRINTF("Before forking process PID=%d\n", getpid());
100	SYSCALL_REQUIRE((child = fork()) != -1);
101	if (child == 0) {
102		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
103		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
104
105		DPRINTF("Before raising %s from child\n", strsignal(sigval));
106		FORKEE_ASSERT(raise(sigval) == 0);
107
108		switch (sigval) {
109		case SIGKILL:
110			/* NOTREACHED */
111			FORKEE_ASSERTX(0 && "This shall not be reached");
112		default:
113			DPRINTF("Before exiting of the child process\n");
114			_exit(exitval);
115		}
116	}
117	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
118
119	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
120	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
121
122	switch (sigval) {
123	case SIGKILL:
124		validate_status_signaled(status, sigval, 0);
125		break;
126	default:
127		validate_status_stopped(status, sigval);
128
129		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
130		        "child\n");
131		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
132		                       sizeof(info)) != -1);
133
134		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
135		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
136		        "si_errno=%#x\n",
137		        info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
138		        info.psi_siginfo.si_errno);
139
140		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
141		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
142
143		DPRINTF("Before resuming the child process where it left off "
144		    "and without signal to be sent\n");
145		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
146
147		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
148		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
149		                      child);
150		break;
151	}
152
153	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
154	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
155}
156
157#define TRACEME_RAISE(test, sig)						\
158ATF_TC(test);									\
159ATF_TC_HEAD(test, tc)								\
160{										\
161	atf_tc_set_md_var(tc, "descr",						\
162	    "Verify " #sig " followed by _exit(2) in a child");			\
163}										\
164										\
165ATF_TC_BODY(test, tc)								\
166{										\
167										\
168	traceme_raise(sig);							\
169}
170
171TRACEME_RAISE(traceme_raise1, SIGKILL) /* non-maskable */
172TRACEME_RAISE(traceme_raise2, SIGSTOP) /* non-maskable */
173TRACEME_RAISE(traceme_raise3, SIGABRT) /* regular abort trap */
174TRACEME_RAISE(traceme_raise4, SIGHUP)  /* hangup */
175TRACEME_RAISE(traceme_raise5, SIGCONT) /* continued? */
176
177/// ----------------------------------------------------------------------------
178
179static void
180traceme_sendsignal_handle(int sigsent, void (*sah)(int a), int *traceme_caught)
181{
182	const int exitval = 5;
183	const int sigval = SIGSTOP;
184	pid_t child, wpid;
185	struct sigaction sa;
186#if defined(TWAIT_HAVE_STATUS)
187	int status;
188#endif
189
190	struct ptrace_siginfo info;
191	memset(&info, 0, sizeof(info));
192
193	DPRINTF("Before forking process PID=%d\n", getpid());
194	SYSCALL_REQUIRE((child = fork()) != -1);
195	if (child == 0) {
196		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
197		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
198
199		sa.sa_handler = sah;
200		sa.sa_flags = SA_SIGINFO;
201		sigemptyset(&sa.sa_mask);
202
203		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
204
205		DPRINTF("Before raising %s from child\n", strsignal(sigval));
206		FORKEE_ASSERT(raise(sigval) == 0);
207
208		FORKEE_ASSERT_EQ(*traceme_caught, 1);
209
210		DPRINTF("Before exiting of the child process\n");
211		_exit(exitval);
212	}
213	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
214
215	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
216	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
217
218	validate_status_stopped(status, sigval);
219
220	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
221	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
222	                != -1);
223
224	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
225	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
226	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
227	    info.psi_siginfo.si_errno);
228
229	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
230	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
231
232	DPRINTF("Before resuming the child process where it left off and with "
233	    "signal %s to be sent\n", strsignal(sigsent));
234	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
235
236	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
237	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
238
239	validate_status_exited(status, exitval);
240
241	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
242	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
243}
244
245#define TRACEME_SENDSIGNAL_HANDLE(test, sig)					\
246ATF_TC(test);									\
247ATF_TC_HEAD(test, tc)								\
248{										\
249	atf_tc_set_md_var(tc, "descr",						\
250	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
251	    "handled correctly and caught by a signal handler");		\
252}										\
253										\
254static int test##_caught = 0;							\
255										\
256static void									\
257test##_sighandler(int arg)							\
258{										\
259	FORKEE_ASSERT_EQ(arg, sig);						\
260										\
261	++ test##_caught;							\
262}										\
263										\
264ATF_TC_BODY(test, tc)								\
265{										\
266										\
267	traceme_sendsignal_handle(sig, test##_sighandler, & test##_caught);	\
268}
269
270// A signal handler for SIGKILL and SIGSTOP cannot be registered.
271TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle1, SIGABRT) /* abort trap */
272TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle2, SIGHUP)  /* hangup */
273TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle3, SIGCONT) /* continued? */
274
275/// ----------------------------------------------------------------------------
276
277static void
278traceme_sendsignal_masked(int sigsent)
279{
280	const int exitval = 5;
281	const int sigval = SIGSTOP;
282	pid_t child, wpid;
283	sigset_t set;
284#if defined(TWAIT_HAVE_STATUS)
285	int status;
286#endif
287
288	struct ptrace_siginfo info;
289	memset(&info, 0, sizeof(info));
290
291	DPRINTF("Before forking process PID=%d\n", getpid());
292	SYSCALL_REQUIRE((child = fork()) != -1);
293	if (child == 0) {
294		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
295		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
296
297		sigemptyset(&set);
298		sigaddset(&set, sigsent);
299		FORKEE_ASSERT(sigprocmask(SIG_BLOCK, &set, NULL) != -1);
300
301		DPRINTF("Before raising %s from child\n", strsignal(sigval));
302		FORKEE_ASSERT(raise(sigval) == 0);
303
304		_exit(exitval);
305	}
306	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
307
308	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
309	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
310
311	validate_status_stopped(status, sigval);
312
313	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
314	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
315	                != -1);
316
317	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
318	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
319	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
320	    info.psi_siginfo.si_errno);
321
322	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
323	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
324
325	DPRINTF("Before resuming the child process where it left off and with "
326	    "signal %s to be sent\n", strsignal(sigsent));
327	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
328
329	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
330	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
331
332	validate_status_exited(status, exitval);
333
334	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
335	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
336}
337
338#define TRACEME_SENDSIGNAL_MASKED(test, sig)					\
339ATF_TC(test);									\
340ATF_TC_HEAD(test, tc)								\
341{										\
342	atf_tc_set_md_var(tc, "descr",						\
343	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
344	    "handled correctly and the signal is masked by SIG_BLOCK");		\
345}										\
346										\
347ATF_TC_BODY(test, tc)								\
348{										\
349										\
350	traceme_sendsignal_masked(sig);						\
351}
352
353// A signal handler for SIGKILL and SIGSTOP cannot be masked.
354TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked1, SIGABRT) /* abort trap */
355TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked2, SIGHUP)  /* hangup */
356TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked3, SIGCONT) /* continued? */
357
358/// ----------------------------------------------------------------------------
359
360static void
361traceme_sendsignal_ignored(int sigsent)
362{
363	const int exitval = 5;
364	const int sigval = SIGSTOP;
365	pid_t child, wpid;
366	struct sigaction sa;
367#if defined(TWAIT_HAVE_STATUS)
368	int status;
369#endif
370
371	struct ptrace_siginfo info;
372	memset(&info, 0, sizeof(info));
373
374	DPRINTF("Before forking process PID=%d\n", getpid());
375	SYSCALL_REQUIRE((child = fork()) != -1);
376	if (child == 0) {
377		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
378		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
379
380		memset(&sa, 0, sizeof(sa));
381		sa.sa_handler = SIG_IGN;
382		sigemptyset(&sa.sa_mask);
383		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
384
385		DPRINTF("Before raising %s from child\n", strsignal(sigval));
386		FORKEE_ASSERT(raise(sigval) == 0);
387
388		_exit(exitval);
389	}
390	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
391
392	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
393	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
394
395	validate_status_stopped(status, sigval);
396
397	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
398	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
399	                != -1);
400
401	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
402	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
403	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
404	    info.psi_siginfo.si_errno);
405
406	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
407	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
408
409	DPRINTF("Before resuming the child process where it left off and with "
410	    "signal %s to be sent\n", strsignal(sigsent));
411	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
412
413	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
414	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
415
416	validate_status_exited(status, exitval);
417
418	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
419	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
420}
421
422#define TRACEME_SENDSIGNAL_IGNORED(test, sig)					\
423ATF_TC(test);									\
424ATF_TC_HEAD(test, tc)								\
425{										\
426	atf_tc_set_md_var(tc, "descr",						\
427	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
428	    "handled correctly and the signal is masked by SIG_IGN");		\
429}										\
430										\
431ATF_TC_BODY(test, tc)								\
432{										\
433										\
434	traceme_sendsignal_ignored(sig);					\
435}
436
437// A signal handler for SIGKILL and SIGSTOP cannot be ignored.
438TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored1, SIGABRT) /* abort trap */
439TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored2, SIGHUP)  /* hangup */
440TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored3, SIGCONT) /* continued? */
441
442/// ----------------------------------------------------------------------------
443
444static void
445traceme_sendsignal_simple(int sigsent)
446{
447	const int sigval = SIGSTOP;
448	int exitval = 0;
449	pid_t child, wpid;
450#if defined(TWAIT_HAVE_STATUS)
451	int status;
452	int expect_core = (sigsent == SIGABRT) ? 1 : 0;
453#endif
454
455	struct ptrace_siginfo info;
456	memset(&info, 0, sizeof(info));
457
458	DPRINTF("Before forking process PID=%d\n", getpid());
459	SYSCALL_REQUIRE((child = fork()) != -1);
460	if (child == 0) {
461		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
462		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
463
464		DPRINTF("Before raising %s from child\n", strsignal(sigval));
465		FORKEE_ASSERT(raise(sigval) == 0);
466
467		switch (sigsent) {
468		case SIGCONT:
469		case SIGSTOP:
470			_exit(exitval);
471		default:
472			/* NOTREACHED */
473			FORKEE_ASSERTX(0 && "This shall not be reached");
474		}
475	}
476	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
477
478	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
479	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
480
481	validate_status_stopped(status, sigval);
482
483	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
484	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
485	                != -1);
486
487	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
488	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
489	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
490	    info.psi_siginfo.si_errno);
491
492	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
493	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
494
495	DPRINTF("Before resuming the child process where it left off and with "
496	    "signal %s to be sent\n", strsignal(sigsent));
497	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
498
499	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
500	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
501
502	switch (sigsent) {
503	case SIGSTOP:
504		validate_status_stopped(status, sigsent);
505		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
506		        "child\n");
507		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
508		                       sizeof(info)) != -1);
509
510		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
511		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
512		        "si_errno=%#x\n",
513			info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
514		        info.psi_siginfo.si_errno);
515
516		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
517		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
518
519		DPRINTF("Before resuming the child process where it left off "
520		        "and with signal %s to be sent\n", strsignal(sigsent));
521		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
522
523		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
524		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
525		                      child);
526		/* FALLTHROUGH */
527	case SIGCONT:
528		validate_status_exited(status, exitval);
529		break;
530	default:
531		validate_status_signaled(status, sigsent, expect_core);
532		break;
533	}
534
535	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
536	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
537}
538
539#define TRACEME_SENDSIGNAL_SIMPLE(test, sig)					\
540ATF_TC(test);									\
541ATF_TC_HEAD(test, tc)								\
542{										\
543	atf_tc_set_md_var(tc, "descr",						\
544	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
545	    "handled correctly in a child without a signal handler");		\
546}										\
547										\
548ATF_TC_BODY(test, tc)								\
549{										\
550										\
551	traceme_sendsignal_simple(sig);						\
552}
553
554TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple1, SIGKILL) /* non-maskable */
555TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple2, SIGSTOP) /* non-maskable */
556TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple3, SIGABRT) /* abort trap */
557TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple4, SIGHUP)  /* hangup */
558TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple5, SIGCONT) /* continued? */
559
560/// ----------------------------------------------------------------------------
561
562ATF_TC(traceme_pid1_parent);
563ATF_TC_HEAD(traceme_pid1_parent, tc)
564{
565	atf_tc_set_md_var(tc, "descr",
566	    "Verify that PT_TRACE_ME is not allowed when our parent is PID1");
567}
568
569ATF_TC_BODY(traceme_pid1_parent, tc)
570{
571	struct msg_fds parent_child;
572	int exitval_child1 = 1, exitval_child2 = 2;
573	pid_t child1, child2, wpid;
574	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
575#if defined(TWAIT_HAVE_STATUS)
576	int status;
577#endif
578
579	SYSCALL_REQUIRE(msg_open(&parent_child) == 0);
580
581	DPRINTF("Before forking process PID=%d\n", getpid());
582	SYSCALL_REQUIRE((child1 = fork()) != -1);
583	if (child1 == 0) {
584		DPRINTF("Before forking process PID=%d\n", getpid());
585		SYSCALL_REQUIRE((child2 = fork()) != -1);
586		if (child2 != 0) {
587			DPRINTF("Parent process PID=%d, child2's PID=%d\n",
588			        getpid(), child2);
589			_exit(exitval_child1);
590		}
591		CHILD_FROM_PARENT("exit child1", parent_child, msg);
592
593		DPRINTF("Assert that our parent is PID1 (initproc)\n");
594		FORKEE_ASSERT_EQ(getppid(), 1);
595
596		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
597		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) == -1);
598		SYSCALL_REQUIRE_ERRNO(errno, EPERM);
599
600		CHILD_TO_PARENT("child2 exiting", parent_child, msg);
601
602		_exit(exitval_child2);
603	}
604	DPRINTF("Parent process PID=%d, child1's PID=%d\n", getpid(), child1);
605
606	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
607	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child1, &status, WEXITED),
608	                      child1);
609
610	validate_status_exited(status, exitval_child1);
611
612	DPRINTF("Notify that child1 is dead\n");
613	PARENT_TO_CHILD("exit child1", parent_child, msg);
614
615	DPRINTF("Wait for exiting of child2\n");
616	PARENT_FROM_CHILD("child2 exiting", parent_child, msg);
617}
618
619/// ----------------------------------------------------------------------------
620
621static void
622traceme_vfork_raise(int sigval)
623{
624	const int exitval = 5, exitval_watcher = 10;
625	pid_t child, parent, watcher, wpid;
626	int rv;
627#if defined(TWAIT_HAVE_STATUS)
628	int status;
629	int expect_core = (sigval == SIGABRT) ? 1 : 0;
630#endif
631
632	/*
633	 * Spawn a dedicated thread to watch for a stopped child and emit
634	 * the SIGKILL signal to it.
635	 *
636	 * vfork(2) might clobber watcher, this means that it's safer and
637	 * simpler to reparent this process to initproc and forget about it.
638	 */
639	if (sigval == SIGSTOP) {
640		parent = getpid();
641
642		watcher = fork();
643		ATF_REQUIRE(watcher != 1);
644		if (watcher == 0) {
645			/* Double fork(2) trick to reparent to initproc */
646			watcher = fork();
647			FORKEE_ASSERT_NEQ(watcher, -1);
648			if (watcher != 0)
649				_exit(exitval_watcher);
650
651			child = await_stopped_child(parent);
652
653			errno = 0;
654			rv = kill(child, SIGKILL);
655			FORKEE_ASSERT_EQ(rv, 0);
656			FORKEE_ASSERT_EQ(errno, 0);
657
658			/* This exit value will be collected by initproc */
659			_exit(0);
660		}
661		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
662		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(watcher, &status, 0),
663		                      watcher);
664
665		validate_status_exited(status, exitval_watcher);
666
667		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
668		TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(watcher,
669		                                                   &status, 0));
670	}
671
672	DPRINTF("Before forking process PID=%d\n", getpid());
673	SYSCALL_REQUIRE((child = vfork()) != -1);
674	if (child == 0) {
675		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
676		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
677
678		DPRINTF("Before raising %s from child\n", strsignal(sigval));
679		FORKEE_ASSERT(raise(sigval) == 0);
680
681		switch (sigval) {
682		case SIGSTOP:
683		case SIGKILL:
684		case SIGABRT:
685		case SIGHUP:
686			/* NOTREACHED */
687			FORKEE_ASSERTX(0 && "This shall not be reached");
688		default:
689			DPRINTF("Before exiting of the child process\n");
690			_exit(exitval);
691		}
692	}
693	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
694
695	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
696	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
697
698	switch (sigval) {
699	case SIGKILL:
700	case SIGABRT:
701	case SIGHUP:
702		validate_status_signaled(status, sigval, expect_core);
703		break;
704	case SIGSTOP:
705		validate_status_signaled(status, SIGKILL, 0);
706		break;
707	case SIGCONT:
708	case SIGTSTP:
709	case SIGTTIN:
710	case SIGTTOU:
711		validate_status_exited(status, exitval);
712		break;
713	default:
714		/* NOTREACHED */
715		ATF_REQUIRE(0 && "NOT IMPLEMENTED");
716		break;
717	}
718
719	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
720	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
721}
722
723#define TRACEME_VFORK_RAISE(test, sig)						\
724ATF_TC(test);									\
725ATF_TC_HEAD(test, tc)								\
726{										\
727	atf_tc_set_md_var(tc, "descr",						\
728	    "Verify PT_TRACE_ME followed by raise of " #sig " in a vfork(2)ed "	\
729	    "child");								\
730}										\
731										\
732ATF_TC_BODY(test, tc)								\
733{										\
734										\
735	traceme_vfork_raise(sig);						\
736}
737
738TRACEME_VFORK_RAISE(traceme_vfork_raise1, SIGKILL) /* non-maskable */
739TRACEME_VFORK_RAISE(traceme_vfork_raise2, SIGSTOP) /* non-maskable */
740TRACEME_VFORK_RAISE(traceme_vfork_raise3, SIGTSTP) /* ignored in vfork(2) */
741TRACEME_VFORK_RAISE(traceme_vfork_raise4, SIGTTIN) /* ignored in vfork(2) */
742TRACEME_VFORK_RAISE(traceme_vfork_raise5, SIGTTOU) /* ignored in vfork(2) */
743TRACEME_VFORK_RAISE(traceme_vfork_raise6, SIGABRT) /* regular abort trap */
744TRACEME_VFORK_RAISE(traceme_vfork_raise7, SIGHUP)  /* hangup */
745TRACEME_VFORK_RAISE(traceme_vfork_raise8, SIGCONT) /* continued? */
746
747/// ----------------------------------------------------------------------------
748
749static void
750traceme_vfork_crash(int sig)
751{
752	pid_t child, wpid;
753#if defined(TWAIT_HAVE_STATUS)
754	int status;
755#endif
756
757	DPRINTF("Before forking process PID=%d\n", getpid());
758	SYSCALL_REQUIRE((child = vfork()) != -1);
759	if (child == 0) {
760		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
761		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
762
763		DPRINTF("Before executing a trap\n");
764		switch (sig) {
765		case SIGTRAP:
766			trigger_trap();
767			break;
768		case SIGSEGV:
769			trigger_segv();
770			break;
771		case SIGILL:
772			trigger_ill();
773			break;
774		case SIGFPE:
775			trigger_fpe();
776			break;
777		case SIGBUS:
778			trigger_bus();
779			break;
780		default:
781			/* NOTREACHED */
782			FORKEE_ASSERTX(0 && "This shall not be reached");
783		}
784
785		/* NOTREACHED */
786		FORKEE_ASSERTX(0 && "This shall not be reached");
787	}
788	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
789
790	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
791	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
792
793	validate_status_signaled(status, sig, 1);
794
795	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
796	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
797}
798
799#define TRACEME_VFORK_CRASH(test, sig)						\
800ATF_TC(test);									\
801ATF_TC_HEAD(test, tc)								\
802{										\
803	atf_tc_set_md_var(tc, "descr",						\
804	    "Verify PT_TRACE_ME followed by a crash signal " #sig " in a "	\
805	    "vfork(2)ed child");						\
806}										\
807										\
808ATF_TC_BODY(test, tc)								\
809{										\
810										\
811	traceme_vfork_crash(sig);						\
812}
813
814TRACEME_VFORK_CRASH(traceme_vfork_crash_trap, SIGTRAP)
815TRACEME_VFORK_CRASH(traceme_vfork_crash_segv, SIGSEGV)
816//TRACEME_VFORK_CRASH(traceme_vfork_crash_ill, SIGILL)
817TRACEME_VFORK_CRASH(traceme_vfork_crash_fpe, SIGFPE)
818TRACEME_VFORK_CRASH(traceme_vfork_crash_bus, SIGBUS)
819
820/// ----------------------------------------------------------------------------
821
822ATF_TC(traceme_vfork_exec);
823ATF_TC_HEAD(traceme_vfork_exec, tc)
824{
825	atf_tc_set_md_var(tc, "descr",
826	    "Verify PT_TRACE_ME followed by exec(3) in a vfork(2)ed child");
827}
828
829ATF_TC_BODY(traceme_vfork_exec, tc)
830{
831	const int sigval = SIGTRAP;
832	pid_t child, wpid;
833#if defined(TWAIT_HAVE_STATUS)
834	int status;
835#endif
836
837	struct ptrace_siginfo info;
838	memset(&info, 0, sizeof(info));
839
840	DPRINTF("Before forking process PID=%d\n", getpid());
841	SYSCALL_REQUIRE((child = vfork()) != -1);
842	if (child == 0) {
843		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
844		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
845
846		DPRINTF("Before calling execve(2) from child\n");
847		execlp("/bin/echo", "/bin/echo", NULL);
848
849		/* NOTREACHED */
850		FORKEE_ASSERTX(0 && "Not reached");
851	}
852	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
853
854	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
855	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
856
857	validate_status_stopped(status, sigval);
858
859	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
860	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
861
862	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
863	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
864	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
865	    info.psi_siginfo.si_errno);
866
867	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
868	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
869
870	DPRINTF("Before resuming the child process where it left off and "
871	    "without signal to be sent\n");
872	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
873
874	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
875	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
876
877	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
878	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
879}
880
881/// ----------------------------------------------------------------------------
882
883#if defined(TWAIT_HAVE_PID)
884static void
885tracer_sees_terminaton_before_the_parent_raw(bool notimeout, bool unrelated)
886{
887	/*
888	 * notimeout - disable timeout in await zombie function
889	 * unrelated - attach from unrelated tracer reparented to initproc
890	 */
891
892	struct msg_fds parent_tracee, parent_tracer;
893	const int exitval_tracee = 5;
894	const int exitval_tracer = 10;
895	pid_t tracee, tracer, wpid;
896	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
897#if defined(TWAIT_HAVE_STATUS)
898	int status;
899#endif
900
901	DPRINTF("Spawn tracee\n");
902	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
903	tracee = atf_utils_fork();
904	if (tracee == 0) {
905		// Wait for parent to let us exit
906		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
907		_exit(exitval_tracee);
908	}
909
910	DPRINTF("Spawn debugger\n");
911	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
912	tracer = atf_utils_fork();
913	if (tracer == 0) {
914		if(unrelated) {
915			/* Fork again and drop parent to reattach to PID 1 */
916			tracer = atf_utils_fork();
917			if (tracer != 0)
918				_exit(exitval_tracer);
919		}
920
921		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
922		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
923
924		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
925		FORKEE_REQUIRE_SUCCESS(
926		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
927
928		forkee_status_stopped(status, SIGSTOP);
929
930		/* Resume tracee with PT_CONTINUE */
931		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
932
933		/* Inform parent that tracer has attached to tracee */
934		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
935
936		/* Wait for parent to tell use that tracee should have exited */
937		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
938
939		/* Wait for tracee and assert that it exited */
940		FORKEE_REQUIRE_SUCCESS(
941		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
942
943		forkee_status_exited(status, exitval_tracee);
944		DPRINTF("Tracee %d exited with %d\n", tracee, exitval_tracee);
945
946		DPRINTF("Before exiting of the tracer process\n");
947		_exit(unrelated ? 0 /* collect by initproc */ : exitval_tracer);
948	}
949
950	if (unrelated) {
951		DPRINTF("Wait for the tracer process (direct child) to exit "
952		    "calling %s()\n", TWAIT_FNAME);
953		TWAIT_REQUIRE_SUCCESS(
954		    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
955
956		validate_status_exited(status, exitval_tracer);
957
958		DPRINTF("Wait for the non-exited tracee process with %s()\n",
959		    TWAIT_FNAME);
960		TWAIT_REQUIRE_SUCCESS(
961		    wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
962	}
963
964	DPRINTF("Wait for the tracer to attach to the tracee\n");
965	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
966
967	DPRINTF("Resume the tracee and let it exit\n");
968	PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
969
970	DPRINTF("Detect that tracee is zombie\n");
971	if (notimeout)
972		await_zombie_raw(tracee, 0);
973	else
974		await_zombie(tracee);
975
976	DPRINTF("Assert that there is no status about tracee %d - "
977	    "Tracer must detect zombie first - calling %s()\n", tracee,
978	    TWAIT_FNAME);
979	TWAIT_REQUIRE_SUCCESS(
980	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
981
982	if (unrelated) {
983		DPRINTF("Resume the tracer and let it detect exited tracee\n");
984		PARENT_TO_CHILD("Message 2", parent_tracer, msg);
985	} else {
986		DPRINTF("Tell the tracer child should have exited\n");
987		PARENT_TO_CHILD("wait for tracee exit", parent_tracer,  msg);
988		DPRINTF("Wait for tracer to finish its job and exit - calling "
989		        "%s()\n", TWAIT_FNAME);
990
991		DPRINTF("Wait from tracer child to complete waiting for "
992		        "tracee\n");
993		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
994		    tracer);
995
996		validate_status_exited(status, exitval_tracer);
997	}
998
999	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
1000	    TWAIT_FNAME);
1001	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1002
1003	validate_status_exited(status, exitval_tracee);
1004
1005	msg_close(&parent_tracer);
1006	msg_close(&parent_tracee);
1007}
1008
1009ATF_TC(tracer_sees_terminaton_before_the_parent);
1010ATF_TC_HEAD(tracer_sees_terminaton_before_the_parent, tc)
1011{
1012	atf_tc_set_md_var(tc, "descr",
1013	    "Assert that tracer sees process termination before the parent");
1014}
1015
1016ATF_TC_BODY(tracer_sees_terminaton_before_the_parent, tc)
1017{
1018
1019	tracer_sees_terminaton_before_the_parent_raw(false, false);
1020}
1021
1022ATF_TC(tracer_sysctl_lookup_without_duplicates);
1023ATF_TC_HEAD(tracer_sysctl_lookup_without_duplicates, tc)
1024{
1025	atf_tc_set_md_var(tc, "descr",
1026	    "Assert that await_zombie() in attach1 always finds a single "
1027	    "process and no other error is reported");
1028}
1029
1030ATF_TC_BODY(tracer_sysctl_lookup_without_duplicates, tc)
1031{
1032	time_t start, end;
1033	double diff;
1034	unsigned long N = 0;
1035
1036	/*
1037	 * Reuse this test with tracer_sees_terminaton_before_the_parent_raw().
1038	 * This test body isn't specific to this race, however it's just good
1039	 * enough for this purposes, no need to invent a dedicated code flow.
1040	 */
1041
1042	start = time(NULL);
1043	while (true) {
1044		DPRINTF("Step: %lu\n", N);
1045		tracer_sees_terminaton_before_the_parent_raw(true, false);
1046		end = time(NULL);
1047		diff = difftime(end, start);
1048		if (diff >= 5.0)
1049			break;
1050		++N;
1051	}
1052	DPRINTF("Iterations: %lu\n", N);
1053}
1054
1055ATF_TC(unrelated_tracer_sees_terminaton_before_the_parent);
1056ATF_TC_HEAD(unrelated_tracer_sees_terminaton_before_the_parent, tc)
1057{
1058	atf_tc_set_md_var(tc, "descr",
1059	    "Assert that tracer sees process termination before the parent");
1060}
1061
1062ATF_TC_BODY(unrelated_tracer_sees_terminaton_before_the_parent, tc)
1063{
1064
1065	tracer_sees_terminaton_before_the_parent_raw(false, true);
1066}
1067#endif
1068
1069/// ----------------------------------------------------------------------------
1070
1071ATF_TC(parent_attach_to_its_child);
1072ATF_TC_HEAD(parent_attach_to_its_child, tc)
1073{
1074	atf_tc_set_md_var(tc, "descr",
1075	    "Assert that tracer parent can PT_ATTACH to its child");
1076}
1077
1078ATF_TC_BODY(parent_attach_to_its_child, tc)
1079{
1080	struct msg_fds parent_tracee;
1081	const int exitval_tracee = 5;
1082	pid_t tracee, wpid;
1083	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1084#if defined(TWAIT_HAVE_STATUS)
1085	int status;
1086#endif
1087
1088	DPRINTF("Spawn tracee\n");
1089	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1090	tracee = atf_utils_fork();
1091	if (tracee == 0) {
1092		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
1093		DPRINTF("Parent should now attach to tracee\n");
1094
1095		CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
1096		/* Wait for message from the parent */
1097		_exit(exitval_tracee);
1098	}
1099	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
1100
1101	DPRINTF("Before calling PT_ATTACH for tracee %d\n", tracee);
1102	SYSCALL_REQUIRE(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
1103
1104	DPRINTF("Wait for the stopped tracee process with %s()\n",
1105	    TWAIT_FNAME);
1106	TWAIT_REQUIRE_SUCCESS(
1107	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1108
1109	validate_status_stopped(status, SIGSTOP);
1110
1111	DPRINTF("Resume tracee with PT_CONTINUE\n");
1112	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
1113
1114	DPRINTF("Let the tracee exit now\n");
1115	PARENT_TO_CHILD("Message 2", parent_tracee, msg);
1116
1117	DPRINTF("Wait for tracee to exit with %s()\n", TWAIT_FNAME);
1118	TWAIT_REQUIRE_SUCCESS(
1119	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1120
1121	validate_status_exited(status, exitval_tracee);
1122
1123	DPRINTF("Before calling %s() for tracee\n", TWAIT_FNAME);
1124	TWAIT_REQUIRE_FAILURE(ECHILD,
1125	    wpid = TWAIT_GENERIC(tracee, &status, 0));
1126
1127	msg_close(&parent_tracee);
1128}
1129
1130/// ----------------------------------------------------------------------------
1131
1132ATF_TC(child_attach_to_its_parent);
1133ATF_TC_HEAD(child_attach_to_its_parent, tc)
1134{
1135	atf_tc_set_md_var(tc, "descr",
1136	    "Assert that tracer child can PT_ATTACH to its parent");
1137}
1138
1139ATF_TC_BODY(child_attach_to_its_parent, tc)
1140{
1141	struct msg_fds parent_tracee;
1142	const int exitval_tracer = 5;
1143	pid_t tracer, wpid;
1144	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1145#if defined(TWAIT_HAVE_STATUS)
1146	int status;
1147#endif
1148
1149	DPRINTF("Spawn tracer\n");
1150	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1151	tracer = atf_utils_fork();
1152	if (tracer == 0) {
1153
1154		/* Wait for message from the parent */
1155		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
1156
1157		DPRINTF("Attach to parent PID %d with PT_ATTACH from child\n",
1158		    getppid());
1159		FORKEE_ASSERT(ptrace(PT_ATTACH, getppid(), NULL, 0) != -1);
1160
1161		DPRINTF("Wait for the stopped parent process with %s()\n",
1162		    TWAIT_FNAME);
1163		FORKEE_REQUIRE_SUCCESS(
1164		    wpid = TWAIT_GENERIC(getppid(), &status, 0), getppid());
1165
1166		forkee_status_stopped(status, SIGSTOP);
1167
1168		DPRINTF("Resume parent with PT_DETACH\n");
1169		FORKEE_ASSERT(ptrace(PT_DETACH, getppid(), (void *)1, 0)
1170		    != -1);
1171
1172		/* Tell parent we are ready */
1173		CHILD_TO_PARENT("Message 1", parent_tracee, msg);
1174
1175		_exit(exitval_tracer);
1176	}
1177
1178	DPRINTF("Wait for the tracer to become ready\n");
1179	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
1180	DPRINTF("Allow the tracer to exit now\n");
1181	PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
1182
1183	DPRINTF("Wait for tracer to exit with %s()\n", TWAIT_FNAME);
1184	TWAIT_REQUIRE_SUCCESS(
1185	    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
1186
1187	validate_status_exited(status, exitval_tracer);
1188
1189	DPRINTF("Before calling %s() for tracer\n", TWAIT_FNAME);
1190	TWAIT_REQUIRE_FAILURE(ECHILD,
1191	    wpid = TWAIT_GENERIC(tracer, &status, 0));
1192
1193	msg_close(&parent_tracee);
1194}
1195
1196/// ----------------------------------------------------------------------------
1197
1198#if defined(TWAIT_HAVE_PID)
1199
1200enum tracee_sees_its_original_parent_type {
1201	TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID,
1202	TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2,
1203	TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS
1204};
1205
1206static void
1207tracee_sees_its_original_parent(enum tracee_sees_its_original_parent_type type)
1208{
1209	struct msg_fds parent_tracer, parent_tracee;
1210	const int exitval_tracee = 5;
1211	const int exitval_tracer = 10;
1212	pid_t parent, tracee, tracer, wpid;
1213	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1214#if defined(TWAIT_HAVE_STATUS)
1215	int status;
1216#endif
1217	/* sysctl(3) - kinfo_proc2 */
1218	int name[CTL_MAXNAME];
1219	struct kinfo_proc2 kp;
1220	size_t len = sizeof(kp);
1221	unsigned int namelen;
1222
1223	/* procfs - status  */
1224	FILE *fp;
1225	struct stat st;
1226	const char *fname = "/proc/curproc/status";
1227	char s_executable[MAXPATHLEN];
1228	int s_pid, s_ppid;
1229	int rv;
1230
1231	if (type == TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS) {
1232		SYSCALL_REQUIRE((rv = stat(fname, &st)) == 0 ||
1233		                (errno == ENOENT));
1234		if (rv != 0) {
1235			atf_tc_skip("/proc/curproc/status not found");
1236		}
1237	}
1238
1239	DPRINTF("Spawn tracee\n");
1240	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
1241	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1242	tracee = atf_utils_fork();
1243	if (tracee == 0) {
1244		parent = getppid();
1245
1246		/* Emit message to the parent */
1247		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
1248		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
1249
1250		switch (type) {
1251		case TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID:
1252			FORKEE_ASSERT_EQ(parent, getppid());
1253			break;
1254		case TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2:
1255			namelen = 0;
1256			name[namelen++] = CTL_KERN;
1257			name[namelen++] = KERN_PROC2;
1258			name[namelen++] = KERN_PROC_PID;
1259			name[namelen++] = getpid();
1260			name[namelen++] = len;
1261			name[namelen++] = 1;
1262
1263			FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL,
1264			                        0),
1265			                 0);
1266			FORKEE_ASSERT_EQ(parent, kp.p_ppid);
1267			break;
1268		case TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS:
1269			/*
1270			 * Format:
1271			 *  EXECUTABLE PID PPID ...
1272			 */
1273			FORKEE_ASSERT((fp = fopen(fname, "r")) != NULL);
1274			fscanf(fp, "%s %d %d", s_executable, &s_pid, &s_ppid);
1275			FORKEE_ASSERT_EQ(fclose(fp), 0);
1276			FORKEE_ASSERT_EQ(parent, s_ppid);
1277			break;
1278		}
1279
1280		_exit(exitval_tracee);
1281	}
1282	DPRINTF("Wait for child to record its parent identifier (pid)\n");
1283	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
1284
1285	DPRINTF("Spawn debugger\n");
1286	tracer = atf_utils_fork();
1287	if (tracer == 0) {
1288		/* No IPC to communicate with the child */
1289		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
1290		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
1291
1292		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
1293		FORKEE_REQUIRE_SUCCESS(
1294		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1295
1296		forkee_status_stopped(status, SIGSTOP);
1297
1298		/* Resume tracee with PT_CONTINUE */
1299		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
1300
1301		/* Inform parent that tracer has attached to tracee */
1302		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
1303
1304		/* Wait for parent to tell use that tracee should have exited */
1305		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
1306
1307		/* Wait for tracee and assert that it exited */
1308		FORKEE_REQUIRE_SUCCESS(
1309		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1310
1311		forkee_status_exited(status, exitval_tracee);
1312
1313		DPRINTF("Before exiting of the tracer process\n");
1314		_exit(exitval_tracer);
1315	}
1316
1317	DPRINTF("Wait for the tracer to attach to the tracee\n");
1318	PARENT_FROM_CHILD("tracer ready",  parent_tracer, msg);
1319
1320	DPRINTF("Resume the tracee and let it exit\n");
1321	PARENT_TO_CHILD("exit tracee",  parent_tracee, msg);
1322
1323	DPRINTF("Detect that tracee is zombie\n");
1324	await_zombie(tracee);
1325
1326	DPRINTF("Assert that there is no status about tracee - "
1327	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
1328	TWAIT_REQUIRE_SUCCESS(
1329	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
1330
1331	DPRINTF("Tell the tracer child should have exited\n");
1332	PARENT_TO_CHILD("wait for tracee exit",  parent_tracer, msg);
1333
1334	DPRINTF("Wait from tracer child to complete waiting for tracee\n");
1335	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
1336	    tracer);
1337
1338	validate_status_exited(status, exitval_tracer);
1339
1340	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
1341	    TWAIT_FNAME);
1342	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
1343	    tracee);
1344
1345	validate_status_exited(status, exitval_tracee);
1346
1347	msg_close(&parent_tracer);
1348	msg_close(&parent_tracee);
1349}
1350
1351#define TRACEE_SEES_ITS_ORIGINAL_PARENT(test, type, descr)			\
1352ATF_TC(test);									\
1353ATF_TC_HEAD(test, tc)								\
1354{										\
1355	atf_tc_set_md_var(tc, "descr",						\
1356	    "Assert that tracee sees its original parent when being traced "	\
1357	    "(check " descr ")");						\
1358}										\
1359										\
1360ATF_TC_BODY(test, tc)								\
1361{										\
1362										\
1363	tracee_sees_its_original_parent(type);					\
1364}
1365
1366TRACEE_SEES_ITS_ORIGINAL_PARENT(
1367	tracee_sees_its_original_parent_getppid,
1368	TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID,
1369	"getppid(2)");
1370TRACEE_SEES_ITS_ORIGINAL_PARENT(
1371	tracee_sees_its_original_parent_sysctl_kinfo_proc2,
1372	TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2,
1373	"sysctl(3) and kinfo_proc2");
1374TRACEE_SEES_ITS_ORIGINAL_PARENT(
1375	tracee_sees_its_original_parent_procfs_status,
1376	TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS,
1377	"the status file in procfs");
1378#endif
1379
1380/// ----------------------------------------------------------------------------
1381
1382static void
1383eventmask_preserved(int event)
1384{
1385	const int exitval = 5;
1386	const int sigval = SIGSTOP;
1387	pid_t child, wpid;
1388#if defined(TWAIT_HAVE_STATUS)
1389	int status;
1390#endif
1391	ptrace_event_t set_event, get_event;
1392	const int len = sizeof(ptrace_event_t);
1393
1394	DPRINTF("Before forking process PID=%d\n", getpid());
1395	SYSCALL_REQUIRE((child = fork()) != -1);
1396	if (child == 0) {
1397		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1398		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1399
1400		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1401		FORKEE_ASSERT(raise(sigval) == 0);
1402
1403		DPRINTF("Before exiting of the child process\n");
1404		_exit(exitval);
1405	}
1406	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1407
1408	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1409	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1410
1411	validate_status_stopped(status, sigval);
1412
1413	set_event.pe_set_event = event;
1414	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1415	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1416	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1417
1418	DPRINTF("Before resuming the child process where it left off and "
1419	    "without signal to be sent\n");
1420	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1421
1422	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1423	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1424
1425	validate_status_exited(status, exitval);
1426
1427	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1428	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1429}
1430
1431#define EVENTMASK_PRESERVED(test, event)					\
1432ATF_TC(test);									\
1433ATF_TC_HEAD(test, tc)								\
1434{										\
1435	atf_tc_set_md_var(tc, "descr",						\
1436	    "Verify that eventmask " #event " is preserved");			\
1437}										\
1438										\
1439ATF_TC_BODY(test, tc)								\
1440{										\
1441										\
1442	eventmask_preserved(event);						\
1443}
1444
1445EVENTMASK_PRESERVED(eventmask_preserved_empty, 0)
1446EVENTMASK_PRESERVED(eventmask_preserved_fork, PTRACE_FORK)
1447EVENTMASK_PRESERVED(eventmask_preserved_vfork, PTRACE_VFORK)
1448EVENTMASK_PRESERVED(eventmask_preserved_vfork_done, PTRACE_VFORK_DONE)
1449EVENTMASK_PRESERVED(eventmask_preserved_lwp_create, PTRACE_LWP_CREATE)
1450EVENTMASK_PRESERVED(eventmask_preserved_lwp_exit, PTRACE_LWP_EXIT)
1451
1452/// ----------------------------------------------------------------------------
1453
1454static void
1455fork_body(pid_t (*fn)(void), bool trackfork, bool trackvfork,
1456          bool trackvforkdone, bool detachchild, bool detachparent)
1457{
1458	const int exitval = 5;
1459	const int exitval2 = 15;
1460	const int sigval = SIGSTOP;
1461	pid_t child, child2 = 0, wpid;
1462#if defined(TWAIT_HAVE_STATUS)
1463	int status;
1464#endif
1465	ptrace_state_t state;
1466	const int slen = sizeof(state);
1467	ptrace_event_t event;
1468	const int elen = sizeof(event);
1469
1470	DPRINTF("Before forking process PID=%d\n", getpid());
1471	SYSCALL_REQUIRE((child = fork()) != -1);
1472	if (child == 0) {
1473		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1474		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1475
1476		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1477		FORKEE_ASSERT(raise(sigval) == 0);
1478
1479		FORKEE_ASSERT((child2 = (fn)()) != -1);
1480
1481		if (child2 == 0)
1482			_exit(exitval2);
1483
1484		FORKEE_REQUIRE_SUCCESS
1485		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
1486
1487		forkee_status_exited(status, exitval2);
1488
1489		DPRINTF("Before exiting of the child process\n");
1490		_exit(exitval);
1491	}
1492	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1493
1494	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1495	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1496
1497	validate_status_stopped(status, sigval);
1498
1499	DPRINTF("Set 0%s%s%s in EVENT_MASK for the child %d\n",
1500	        trackfork ? "|PTRACE_FORK" : "",
1501	        trackvfork ? "|PTRACE_VFORK" : "",
1502	        trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
1503	event.pe_set_event = 0;
1504	if (trackfork)
1505		event.pe_set_event |= PTRACE_FORK;
1506	if (trackvfork)
1507		event.pe_set_event |= PTRACE_VFORK;
1508	if (trackvforkdone)
1509		event.pe_set_event |= PTRACE_VFORK_DONE;
1510	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
1511
1512	DPRINTF("Before resuming the child process where it left off and "
1513	    "without signal to be sent\n");
1514	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1515
1516#if defined(TWAIT_HAVE_PID)
1517	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
1518		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
1519		        child);
1520		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1521		                      child);
1522
1523		validate_status_stopped(status, SIGTRAP);
1524
1525		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
1526		                       slen) != -1);
1527		if (trackfork && fn == fork) {
1528			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
1529			       PTRACE_FORK);
1530		}
1531		if (trackvfork && fn == vfork) {
1532			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
1533			       PTRACE_VFORK);
1534		}
1535
1536		child2 = state.pe_other_pid;
1537		DPRINTF("Reported ptrace event with forkee %d\n", child2);
1538
1539		DPRINTF("Before calling %s() for the forkee %d of the child "
1540		        "%d\n", TWAIT_FNAME, child2, child);
1541		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
1542		    child2);
1543
1544		validate_status_stopped(status, SIGTRAP);
1545
1546		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state,
1547		                       slen) != -1);
1548		if (trackfork && fn == fork) {
1549			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
1550			       PTRACE_FORK);
1551		}
1552		if (trackvfork && fn == vfork) {
1553			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
1554			       PTRACE_VFORK);
1555		}
1556
1557		ATF_REQUIRE_EQ(state.pe_other_pid, child);
1558
1559		DPRINTF("Before resuming the forkee process where it left off "
1560		    "and without signal to be sent\n");
1561		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0)
1562		                != -1);
1563
1564		DPRINTF("Before resuming the child process where it left off "
1565		        "and without signal to be sent\n");
1566		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1567	}
1568#endif
1569
1570	if (trackvforkdone && fn == vfork) {
1571		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
1572		        child);
1573		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1574		                      child);
1575
1576		validate_status_stopped(status, SIGTRAP);
1577
1578		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
1579		                       slen) != -1);
1580		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
1581
1582		child2 = state.pe_other_pid;
1583		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
1584		        child2);
1585
1586		DPRINTF("Before resuming the child process where it left off "
1587		        "and without signal to be sent\n");
1588		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1589	}
1590
1591#if defined(TWAIT_HAVE_PID)
1592	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
1593		DPRINTF("Before calling %s() for the forkee - expected exited"
1594		        "\n", TWAIT_FNAME);
1595		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
1596		    child2);
1597
1598		validate_status_exited(status, exitval2);
1599
1600		DPRINTF("Before calling %s() for the forkee - expected no "
1601		        "process\n", TWAIT_FNAME);
1602		TWAIT_REQUIRE_FAILURE(ECHILD,
1603		    wpid = TWAIT_GENERIC(child2, &status, 0));
1604	}
1605#endif
1606
1607	DPRINTF("Before calling %s() for the child - expected stopped "
1608	    "SIGCHLD\n", TWAIT_FNAME);
1609	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1610
1611	validate_status_stopped(status, SIGCHLD);
1612
1613	DPRINTF("Before resuming the child process where it left off and "
1614	    "without signal to be sent\n");
1615	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1616
1617	DPRINTF("Before calling %s() for the child - expected exited\n",
1618	    TWAIT_FNAME);
1619	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1620
1621	validate_status_exited(status, exitval);
1622
1623	DPRINTF("Before calling %s() for the child - expected no process\n",
1624	    TWAIT_FNAME);
1625	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1626}
1627
1628#define FORK_TEST(name,descr,fun,tfork,tvfork,tvforkdone,detchild,detparent)	\
1629ATF_TC(name);									\
1630ATF_TC_HEAD(name, tc)								\
1631{										\
1632	atf_tc_set_md_var(tc, "descr", descr);					\
1633}										\
1634										\
1635ATF_TC_BODY(name, tc)								\
1636{										\
1637										\
1638	fork_body(fun, tfork, tvfork, tvforkdone, detchild, detparent);		\
1639}
1640
1641#define F false
1642#define T true
1643
1644#define F_IF__0(x)
1645#define F_IF__1(x) x
1646#define F_IF__(x,y) F_IF__ ## x (y)
1647#define F_IF_(x,y) F_IF__(x,y)
1648#define F_IF(x,y) F_IF_(x,y)
1649
1650#define DSCR(function,forkbit,vforkbit,vforkdonebit,dchildbit,dparentbit)	\
1651        "Verify " #function "(2) called with 0"					\
1652        F_IF(forkbit,"|PTRACE_FORK")						\
1653        F_IF(vforkbit,"|PTRACE_VFORK")						\
1654        F_IF(vforkdonebit,"|PTRACE_VFORK_DONE")					\
1655        " in EVENT_MASK."							\
1656        F_IF(dchildbit," Detach child in this test.")				\
1657        F_IF(dparentbit," Detach parent in this test.")
1658
1659FORK_TEST(fork1, DSCR(fork,0,0,0,0,0), fork, F, F, F, F, F)
1660#if defined(TWAIT_HAVE_PID)
1661FORK_TEST(fork2, DSCR(fork,1,0,0,0,0), fork, T, F, F, F, F)
1662FORK_TEST(fork3, DSCR(fork,0,1,0,0,0), fork, F, T, F, F, F)
1663FORK_TEST(fork4, DSCR(fork,1,1,0,0,0), fork, T, T, F, F, F)
1664#endif
1665FORK_TEST(fork5, DSCR(fork,0,0,1,0,0), fork, F, F, T, F, F)
1666#if defined(TWAIT_HAVE_PID)
1667FORK_TEST(fork6, DSCR(fork,1,0,1,0,0), fork, T, F, T, F, F)
1668FORK_TEST(fork7, DSCR(fork,0,1,1,0,0), fork, F, T, T, F, F)
1669FORK_TEST(fork8, DSCR(fork,1,1,1,0,0), fork, T, T, T, F, F)
1670#endif
1671
1672FORK_TEST(vfork1, DSCR(vfork,0,0,0,0,0), vfork, F, F, F, F, F)
1673#if defined(TWAIT_HAVE_PID)
1674FORK_TEST(vfork2, DSCR(vfork,1,0,0,0,0), vfork, T, F, F, F, F)
1675FORK_TEST(vfork3, DSCR(vfork,0,1,0,0,0), vfork, F, T, F, F, F)
1676FORK_TEST(vfork4, DSCR(vfork,1,1,0,0,0), vfork, T, T, F, F, F)
1677#endif
1678FORK_TEST(vfork5, DSCR(vfork,0,0,1,0,0), vfork, F, F, T, F, F)
1679#if defined(TWAIT_HAVE_PID)
1680FORK_TEST(vfork6, DSCR(vfork,1,0,1,0,0), vfork, T, F, T, F, F)
1681FORK_TEST(vfork7, DSCR(vfork,0,1,1,0,0), vfork, F, T, T, F, F)
1682FORK_TEST(vfork8, DSCR(vfork,1,1,1,0,0), vfork, T, T, T, F, F)
1683#endif
1684
1685/// ----------------------------------------------------------------------------
1686
1687enum bytes_transfer_type {
1688	BYTES_TRANSFER_DATA,
1689	BYTES_TRANSFER_DATAIO,
1690	BYTES_TRANSFER_TEXT,
1691	BYTES_TRANSFER_TEXTIO,
1692	BYTES_TRANSFER_AUXV
1693};
1694
1695static int __used
1696bytes_transfer_dummy(int a, int b, int c, int d)
1697{
1698	int e, f, g, h;
1699
1700	a *= 4;
1701	b += 3;
1702	c -= 2;
1703	d /= 1;
1704
1705	e = strtol("10", NULL, 10);
1706	f = strtol("20", NULL, 10);
1707	g = strtol("30", NULL, 10);
1708	h = strtol("40", NULL, 10);
1709
1710	return (a + b * c - d) + (e * f - g / h);
1711}
1712
1713static void
1714bytes_transfer(int operation, size_t size, enum bytes_transfer_type type)
1715{
1716	const int exitval = 5;
1717	const int sigval = SIGSTOP;
1718	pid_t child, wpid;
1719	bool skip = false;
1720
1721	int lookup_me = 0;
1722	uint8_t lookup_me8 = 0;
1723	uint16_t lookup_me16 = 0;
1724	uint32_t lookup_me32 = 0;
1725	uint64_t lookup_me64 = 0;
1726
1727	int magic = 0x13579246;
1728	uint8_t magic8 = 0xab;
1729	uint16_t magic16 = 0x1234;
1730	uint32_t magic32 = 0x98765432;
1731	uint64_t magic64 = 0xabcdef0123456789;
1732
1733	struct ptrace_io_desc io;
1734#if defined(TWAIT_HAVE_STATUS)
1735	int status;
1736#endif
1737	/* 512 is more than enough, for the purposes of ATF it's good enough */
1738	AuxInfo ai[512], *aip;
1739
1740	ATF_REQUIRE(size < sizeof(ai));
1741
1742	/* Prepare variables for .TEXT transfers */
1743	switch (type) {
1744	case BYTES_TRANSFER_TEXT:
1745		memcpy(&magic, bytes_transfer_dummy, sizeof(magic));
1746		break;
1747	case BYTES_TRANSFER_TEXTIO:
1748		switch (size) {
1749		case 8:
1750			memcpy(&magic8, bytes_transfer_dummy, sizeof(magic8));
1751			break;
1752		case 16:
1753			memcpy(&magic16, bytes_transfer_dummy, sizeof(magic16));
1754			break;
1755		case 32:
1756			memcpy(&magic32, bytes_transfer_dummy, sizeof(magic32));
1757			break;
1758		case 64:
1759			memcpy(&magic64, bytes_transfer_dummy, sizeof(magic64));
1760			break;
1761		}
1762		break;
1763	default:
1764		break;
1765	}
1766
1767	/* Prepare variables for PIOD and AUXV transfers */
1768	switch (type) {
1769	case BYTES_TRANSFER_TEXTIO:
1770	case BYTES_TRANSFER_DATAIO:
1771		io.piod_op = operation;
1772		switch (size) {
1773		case 8:
1774			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1775			               (void *)bytes_transfer_dummy :
1776			               &lookup_me8;
1777			io.piod_addr = &lookup_me8;
1778			io.piod_len = sizeof(lookup_me8);
1779			break;
1780		case 16:
1781			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1782			               (void *)bytes_transfer_dummy :
1783			               &lookup_me16;
1784			io.piod_addr = &lookup_me16;
1785			io.piod_len = sizeof(lookup_me16);
1786			break;
1787		case 32:
1788			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1789			               (void *)bytes_transfer_dummy :
1790			               &lookup_me32;
1791			io.piod_addr = &lookup_me32;
1792			io.piod_len = sizeof(lookup_me32);
1793			break;
1794		case 64:
1795			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1796			               (void *)bytes_transfer_dummy :
1797			               &lookup_me64;
1798			io.piod_addr = &lookup_me64;
1799			io.piod_len = sizeof(lookup_me64);
1800			break;
1801		default:
1802			break;
1803		}
1804		break;
1805	case BYTES_TRANSFER_AUXV:
1806		io.piod_op = operation;
1807		io.piod_offs = 0;
1808		io.piod_addr = ai;
1809		io.piod_len = size;
1810		break;
1811	default:
1812		break;
1813	}
1814
1815	DPRINTF("Before forking process PID=%d\n", getpid());
1816	SYSCALL_REQUIRE((child = fork()) != -1);
1817	if (child == 0) {
1818		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1819		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1820
1821		switch (type) {
1822		case BYTES_TRANSFER_DATA:
1823			switch (operation) {
1824			case PT_READ_D:
1825			case PT_READ_I:
1826				lookup_me = magic;
1827				break;
1828			default:
1829				break;
1830			}
1831			break;
1832		case BYTES_TRANSFER_DATAIO:
1833			switch (operation) {
1834			case PIOD_READ_D:
1835			case PIOD_READ_I:
1836				switch (size) {
1837				case 8:
1838					lookup_me8 = magic8;
1839					break;
1840				case 16:
1841					lookup_me16 = magic16;
1842					break;
1843				case 32:
1844					lookup_me32 = magic32;
1845					break;
1846				case 64:
1847					lookup_me64 = magic64;
1848					break;
1849				default:
1850					break;
1851				}
1852				break;
1853			default:
1854				break;
1855			}
1856		default:
1857			break;
1858		}
1859
1860		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1861		FORKEE_ASSERT(raise(sigval) == 0);
1862
1863		/* Handle PIOD and PT separately as operation values overlap */
1864		switch (type) {
1865		case BYTES_TRANSFER_DATA:
1866			switch (operation) {
1867			case PT_WRITE_D:
1868			case PT_WRITE_I:
1869				FORKEE_ASSERT_EQ(lookup_me, magic);
1870				break;
1871			default:
1872				break;
1873			}
1874			break;
1875		case BYTES_TRANSFER_DATAIO:
1876			switch (operation) {
1877			case PIOD_WRITE_D:
1878			case PIOD_WRITE_I:
1879				switch (size) {
1880				case 8:
1881					FORKEE_ASSERT_EQ(lookup_me8, magic8);
1882					break;
1883				case 16:
1884					FORKEE_ASSERT_EQ(lookup_me16, magic16);
1885					break;
1886				case 32:
1887					FORKEE_ASSERT_EQ(lookup_me32, magic32);
1888					break;
1889				case 64:
1890					FORKEE_ASSERT_EQ(lookup_me64, magic64);
1891					break;
1892				default:
1893					break;
1894				}
1895				break;
1896			default:
1897				break;
1898			}
1899			break;
1900		case BYTES_TRANSFER_TEXT:
1901			FORKEE_ASSERT(memcmp(&magic, bytes_transfer_dummy,
1902			                     sizeof(magic)) == 0);
1903			break;
1904		case BYTES_TRANSFER_TEXTIO:
1905			switch (size) {
1906			case 8:
1907				FORKEE_ASSERT(memcmp(&magic8,
1908				                     bytes_transfer_dummy,
1909				                     sizeof(magic8)) == 0);
1910				break;
1911			case 16:
1912				FORKEE_ASSERT(memcmp(&magic16,
1913				                     bytes_transfer_dummy,
1914				                     sizeof(magic16)) == 0);
1915				break;
1916			case 32:
1917				FORKEE_ASSERT(memcmp(&magic32,
1918				                     bytes_transfer_dummy,
1919				                     sizeof(magic32)) == 0);
1920				break;
1921			case 64:
1922				FORKEE_ASSERT(memcmp(&magic64,
1923				                     bytes_transfer_dummy,
1924				                     sizeof(magic64)) == 0);
1925				break;
1926			}
1927			break;
1928		default:
1929			break;
1930		}
1931
1932		DPRINTF("Before exiting of the child process\n");
1933		_exit(exitval);
1934	}
1935	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1936
1937	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1938	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1939
1940	validate_status_stopped(status, sigval);
1941
1942	/* Check PaX MPROTECT */
1943	if (!can_we_write_to_text(child)) {
1944		switch (type) {
1945		case BYTES_TRANSFER_TEXTIO:
1946			switch (operation) {
1947			case PIOD_WRITE_D:
1948			case PIOD_WRITE_I:
1949				skip = true;
1950				break;
1951			default:
1952				break;
1953			}
1954			break;
1955		case BYTES_TRANSFER_TEXT:
1956			switch (operation) {
1957			case PT_WRITE_D:
1958			case PT_WRITE_I:
1959				skip = true;
1960				break;
1961			default:
1962				break;
1963			}
1964			break;
1965		default:
1966			break;
1967		}
1968	}
1969
1970	/* Bailout cleanly killing the child process */
1971	if (skip) {
1972		SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void *)1, 0) != -1);
1973		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1974		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1975		                      child);
1976
1977		validate_status_signaled(status, SIGKILL, 0);
1978
1979		atf_tc_skip("PaX MPROTECT setup prevents writes to .text");
1980	}
1981
1982	DPRINTF("Calling operation to transfer bytes between child=%d and "
1983	       "parent=%d\n", child, getpid());
1984
1985	switch (type) {
1986	case BYTES_TRANSFER_TEXTIO:
1987	case BYTES_TRANSFER_DATAIO:
1988	case BYTES_TRANSFER_AUXV:
1989		switch (operation) {
1990		case PIOD_WRITE_D:
1991		case PIOD_WRITE_I:
1992			switch (size) {
1993			case 8:
1994				lookup_me8 = magic8;
1995				break;
1996			case 16:
1997				lookup_me16 = magic16;
1998				break;
1999			case 32:
2000				lookup_me32 = magic32;
2001				break;
2002			case 64:
2003				lookup_me64 = magic64;
2004				break;
2005			default:
2006				break;
2007			}
2008			break;
2009		default:
2010			break;
2011		}
2012		SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2013		switch (operation) {
2014		case PIOD_READ_D:
2015		case PIOD_READ_I:
2016			switch (size) {
2017			case 8:
2018				ATF_REQUIRE_EQ(lookup_me8, magic8);
2019				break;
2020			case 16:
2021				ATF_REQUIRE_EQ(lookup_me16, magic16);
2022				break;
2023			case 32:
2024				ATF_REQUIRE_EQ(lookup_me32, magic32);
2025				break;
2026			case 64:
2027				ATF_REQUIRE_EQ(lookup_me64, magic64);
2028				break;
2029			default:
2030				break;
2031			}
2032			break;
2033		case PIOD_READ_AUXV:
2034			DPRINTF("Asserting that AUXV length (%zu) is > 0\n",
2035			        io.piod_len);
2036			ATF_REQUIRE(io.piod_len > 0);
2037			for (aip = ai; aip->a_type != AT_NULL; aip++)
2038				DPRINTF("a_type=%#llx a_v=%#llx\n",
2039				    (long long int)aip->a_type,
2040				    (long long int)aip->a_v);
2041			break;
2042		default:
2043			break;
2044		}
2045		break;
2046	case BYTES_TRANSFER_TEXT:
2047		switch (operation) {
2048		case PT_READ_D:
2049		case PT_READ_I:
2050			errno = 0;
2051			lookup_me = ptrace(operation, child,
2052			                   bytes_transfer_dummy, 0);
2053			ATF_REQUIRE_EQ(lookup_me, magic);
2054			SYSCALL_REQUIRE_ERRNO(errno, 0);
2055			break;
2056		case PT_WRITE_D:
2057		case PT_WRITE_I:
2058			SYSCALL_REQUIRE(ptrace(operation, child,
2059			                       bytes_transfer_dummy, magic)
2060			                != -1);
2061			break;
2062		default:
2063			break;
2064		}
2065		break;
2066	case BYTES_TRANSFER_DATA:
2067		switch (operation) {
2068		case PT_READ_D:
2069		case PT_READ_I:
2070			errno = 0;
2071			lookup_me = ptrace(operation, child, &lookup_me, 0);
2072			ATF_REQUIRE_EQ(lookup_me, magic);
2073			SYSCALL_REQUIRE_ERRNO(errno, 0);
2074			break;
2075		case PT_WRITE_D:
2076		case PT_WRITE_I:
2077			lookup_me = magic;
2078			SYSCALL_REQUIRE(ptrace(operation, child, &lookup_me,
2079			                       magic) != -1);
2080			break;
2081		default:
2082			break;
2083		}
2084		break;
2085	default:
2086		break;
2087	}
2088
2089	DPRINTF("Before resuming the child process where it left off and "
2090	    "without signal to be sent\n");
2091	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2092
2093	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2094	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2095
2096	validate_status_exited(status, exitval);
2097
2098	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2099	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2100}
2101
2102#define BYTES_TRANSFER(test, operation, size, type)				\
2103ATF_TC(test);									\
2104ATF_TC_HEAD(test, tc)								\
2105{										\
2106	atf_tc_set_md_var(tc, "descr",						\
2107	    "Verify bytes transfer operation" #operation " and size " #size	\
2108	    " of type " #type);							\
2109}										\
2110										\
2111ATF_TC_BODY(test, tc)								\
2112{										\
2113										\
2114	bytes_transfer(operation, size, BYTES_TRANSFER_##type);			\
2115}
2116
2117// DATA
2118
2119BYTES_TRANSFER(bytes_transfer_piod_read_d_8, PIOD_READ_D, 8, DATAIO)
2120BYTES_TRANSFER(bytes_transfer_piod_read_d_16, PIOD_READ_D, 16, DATAIO)
2121BYTES_TRANSFER(bytes_transfer_piod_read_d_32, PIOD_READ_D, 32, DATAIO)
2122BYTES_TRANSFER(bytes_transfer_piod_read_d_64, PIOD_READ_D, 64, DATAIO)
2123
2124BYTES_TRANSFER(bytes_transfer_piod_read_i_8, PIOD_READ_I, 8, DATAIO)
2125BYTES_TRANSFER(bytes_transfer_piod_read_i_16, PIOD_READ_I, 16, DATAIO)
2126BYTES_TRANSFER(bytes_transfer_piod_read_i_32, PIOD_READ_I, 32, DATAIO)
2127BYTES_TRANSFER(bytes_transfer_piod_read_i_64, PIOD_READ_I, 64, DATAIO)
2128
2129BYTES_TRANSFER(bytes_transfer_piod_write_d_8, PIOD_WRITE_D, 8, DATAIO)
2130BYTES_TRANSFER(bytes_transfer_piod_write_d_16, PIOD_WRITE_D, 16, DATAIO)
2131BYTES_TRANSFER(bytes_transfer_piod_write_d_32, PIOD_WRITE_D, 32, DATAIO)
2132BYTES_TRANSFER(bytes_transfer_piod_write_d_64, PIOD_WRITE_D, 64, DATAIO)
2133
2134BYTES_TRANSFER(bytes_transfer_piod_write_i_8, PIOD_WRITE_I, 8, DATAIO)
2135BYTES_TRANSFER(bytes_transfer_piod_write_i_16, PIOD_WRITE_I, 16, DATAIO)
2136BYTES_TRANSFER(bytes_transfer_piod_write_i_32, PIOD_WRITE_I, 32, DATAIO)
2137BYTES_TRANSFER(bytes_transfer_piod_write_i_64, PIOD_WRITE_I, 64, DATAIO)
2138
2139BYTES_TRANSFER(bytes_transfer_read_d, PT_READ_D, 32, DATA)
2140BYTES_TRANSFER(bytes_transfer_read_i, PT_READ_I, 32, DATA)
2141BYTES_TRANSFER(bytes_transfer_write_d, PT_WRITE_D, 32, DATA)
2142BYTES_TRANSFER(bytes_transfer_write_i, PT_WRITE_I, 32, DATA)
2143
2144// TEXT
2145
2146BYTES_TRANSFER(bytes_transfer_piod_read_d_8_text, PIOD_READ_D, 8, TEXTIO)
2147BYTES_TRANSFER(bytes_transfer_piod_read_d_16_text, PIOD_READ_D, 16, TEXTIO)
2148BYTES_TRANSFER(bytes_transfer_piod_read_d_32_text, PIOD_READ_D, 32, TEXTIO)
2149BYTES_TRANSFER(bytes_transfer_piod_read_d_64_text, PIOD_READ_D, 64, TEXTIO)
2150
2151BYTES_TRANSFER(bytes_transfer_piod_read_i_8_text, PIOD_READ_I, 8, TEXTIO)
2152BYTES_TRANSFER(bytes_transfer_piod_read_i_16_text, PIOD_READ_I, 16, TEXTIO)
2153BYTES_TRANSFER(bytes_transfer_piod_read_i_32_text, PIOD_READ_I, 32, TEXTIO)
2154BYTES_TRANSFER(bytes_transfer_piod_read_i_64_text, PIOD_READ_I, 64, TEXTIO)
2155
2156BYTES_TRANSFER(bytes_transfer_piod_write_d_8_text, PIOD_WRITE_D, 8, TEXTIO)
2157BYTES_TRANSFER(bytes_transfer_piod_write_d_16_text, PIOD_WRITE_D, 16, TEXTIO)
2158BYTES_TRANSFER(bytes_transfer_piod_write_d_32_text, PIOD_WRITE_D, 32, TEXTIO)
2159BYTES_TRANSFER(bytes_transfer_piod_write_d_64_text, PIOD_WRITE_D, 64, TEXTIO)
2160
2161BYTES_TRANSFER(bytes_transfer_piod_write_i_8_text, PIOD_WRITE_I, 8, TEXTIO)
2162BYTES_TRANSFER(bytes_transfer_piod_write_i_16_text, PIOD_WRITE_I, 16, TEXTIO)
2163BYTES_TRANSFER(bytes_transfer_piod_write_i_32_text, PIOD_WRITE_I, 32, TEXTIO)
2164BYTES_TRANSFER(bytes_transfer_piod_write_i_64_text, PIOD_WRITE_I, 64, TEXTIO)
2165
2166BYTES_TRANSFER(bytes_transfer_read_d_text, PT_READ_D, 32, TEXT)
2167BYTES_TRANSFER(bytes_transfer_read_i_text, PT_READ_I, 32, TEXT)
2168BYTES_TRANSFER(bytes_transfer_write_d_text, PT_WRITE_D, 32, TEXT)
2169BYTES_TRANSFER(bytes_transfer_write_i_text, PT_WRITE_I, 32, TEXT)
2170
2171// AUXV
2172
2173BYTES_TRANSFER(bytes_transfer_piod_read_auxv, PIOD_READ_AUXV, 4096, AUXV)
2174
2175/// ----------------------------------------------------------------------------
2176
2177#if defined(HAVE_GPREGS)
2178ATF_TC(regs1);
2179ATF_TC_HEAD(regs1, tc)
2180{
2181	atf_tc_set_md_var(tc, "descr",
2182	    "Verify plain PT_GETREGS call without further steps");
2183}
2184
2185ATF_TC_BODY(regs1, tc)
2186{
2187	const int exitval = 5;
2188	const int sigval = SIGSTOP;
2189	pid_t child, wpid;
2190#if defined(TWAIT_HAVE_STATUS)
2191	int status;
2192#endif
2193	struct reg r;
2194
2195	DPRINTF("Before forking process PID=%d\n", getpid());
2196	SYSCALL_REQUIRE((child = fork()) != -1);
2197	if (child == 0) {
2198		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2199		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2200
2201		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2202		FORKEE_ASSERT(raise(sigval) == 0);
2203
2204		DPRINTF("Before exiting of the child process\n");
2205		_exit(exitval);
2206	}
2207	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2208
2209	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2210	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2211
2212	validate_status_stopped(status, sigval);
2213
2214	DPRINTF("Call GETREGS for the child process\n");
2215	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2216
2217	DPRINTF("Before resuming the child process where it left off and "
2218	    "without signal to be sent\n");
2219	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2220
2221	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2222	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2223
2224	validate_status_exited(status, exitval);
2225
2226	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2227	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2228}
2229#endif
2230
2231#if defined(HAVE_GPREGS)
2232ATF_TC(regs2);
2233ATF_TC_HEAD(regs2, tc)
2234{
2235	atf_tc_set_md_var(tc, "descr",
2236	    "Verify plain PT_GETREGS call and retrieve PC");
2237}
2238
2239ATF_TC_BODY(regs2, tc)
2240{
2241	const int exitval = 5;
2242	const int sigval = SIGSTOP;
2243	pid_t child, wpid;
2244#if defined(TWAIT_HAVE_STATUS)
2245	int status;
2246#endif
2247	struct reg r;
2248
2249	DPRINTF("Before forking process PID=%d\n", getpid());
2250	SYSCALL_REQUIRE((child = fork()) != -1);
2251	if (child == 0) {
2252		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2253		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2254
2255		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2256		FORKEE_ASSERT(raise(sigval) == 0);
2257
2258		DPRINTF("Before exiting of the child process\n");
2259		_exit(exitval);
2260	}
2261	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2262
2263	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2264	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2265
2266	validate_status_stopped(status, sigval);
2267
2268	DPRINTF("Call GETREGS for the child process\n");
2269	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2270
2271	DPRINTF("Retrieved PC=%" PRIxREGISTER "\n", PTRACE_REG_PC(&r));
2272
2273	DPRINTF("Before resuming the child process where it left off and "
2274	    "without signal to be sent\n");
2275	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2276
2277	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2278	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2279
2280	validate_status_exited(status, exitval);
2281
2282	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2283	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2284}
2285#endif
2286
2287#if defined(HAVE_GPREGS)
2288ATF_TC(regs3);
2289ATF_TC_HEAD(regs3, tc)
2290{
2291	atf_tc_set_md_var(tc, "descr",
2292	    "Verify plain PT_GETREGS call and retrieve SP");
2293}
2294
2295ATF_TC_BODY(regs3, tc)
2296{
2297	const int exitval = 5;
2298	const int sigval = SIGSTOP;
2299	pid_t child, wpid;
2300#if defined(TWAIT_HAVE_STATUS)
2301	int status;
2302#endif
2303	struct reg r;
2304
2305	DPRINTF("Before forking process PID=%d\n", getpid());
2306	SYSCALL_REQUIRE((child = fork()) != -1);
2307	if (child == 0) {
2308		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2309		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2310
2311		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2312		FORKEE_ASSERT(raise(sigval) == 0);
2313
2314		DPRINTF("Before exiting of the child process\n");
2315		_exit(exitval);
2316	}
2317	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2318
2319	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2320	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2321
2322	validate_status_stopped(status, sigval);
2323
2324	DPRINTF("Call GETREGS for the child process\n");
2325	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2326
2327	DPRINTF("Retrieved SP=%" PRIxREGISTER "\n", PTRACE_REG_SP(&r));
2328
2329	DPRINTF("Before resuming the child process where it left off and "
2330	    "without signal to be sent\n");
2331	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2332
2333	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2334	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2335
2336	validate_status_exited(status, exitval);
2337
2338	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2339	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2340}
2341#endif
2342
2343#if defined(HAVE_GPREGS)
2344ATF_TC(regs4);
2345ATF_TC_HEAD(regs4, tc)
2346{
2347	atf_tc_set_md_var(tc, "descr",
2348	    "Verify plain PT_GETREGS call and retrieve INTRV");
2349}
2350
2351ATF_TC_BODY(regs4, tc)
2352{
2353	const int exitval = 5;
2354	const int sigval = SIGSTOP;
2355	pid_t child, wpid;
2356#if defined(TWAIT_HAVE_STATUS)
2357	int status;
2358#endif
2359	struct reg r;
2360
2361	DPRINTF("Before forking process PID=%d\n", getpid());
2362	SYSCALL_REQUIRE((child = fork()) != -1);
2363	if (child == 0) {
2364		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2365		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2366
2367		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2368		FORKEE_ASSERT(raise(sigval) == 0);
2369
2370		DPRINTF("Before exiting of the child process\n");
2371		_exit(exitval);
2372	}
2373	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2374
2375	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2376	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2377
2378	validate_status_stopped(status, sigval);
2379
2380	DPRINTF("Call GETREGS for the child process\n");
2381	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2382
2383	DPRINTF("Retrieved INTRV=%" PRIxREGISTER "\n", PTRACE_REG_INTRV(&r));
2384
2385	DPRINTF("Before resuming the child process where it left off and "
2386	    "without signal to be sent\n");
2387	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2388
2389	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2390	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2391
2392	validate_status_exited(status, exitval);
2393
2394	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2395	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2396}
2397#endif
2398
2399#if defined(HAVE_GPREGS)
2400ATF_TC(regs5);
2401ATF_TC_HEAD(regs5, tc)
2402{
2403	atf_tc_set_md_var(tc, "descr",
2404	    "Verify PT_GETREGS and PT_SETREGS calls without changing regs");
2405}
2406
2407ATF_TC_BODY(regs5, tc)
2408{
2409	const int exitval = 5;
2410	const int sigval = SIGSTOP;
2411	pid_t child, wpid;
2412#if defined(TWAIT_HAVE_STATUS)
2413	int status;
2414#endif
2415	struct reg r;
2416
2417	DPRINTF("Before forking process PID=%d\n", getpid());
2418	SYSCALL_REQUIRE((child = fork()) != -1);
2419	if (child == 0) {
2420		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2421		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2422
2423		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2424		FORKEE_ASSERT(raise(sigval) == 0);
2425
2426		DPRINTF("Before exiting of the child process\n");
2427		_exit(exitval);
2428	}
2429	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2430
2431	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2432	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2433
2434	validate_status_stopped(status, sigval);
2435
2436	DPRINTF("Call GETREGS for the child process\n");
2437	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2438
2439	DPRINTF("Call SETREGS for the child process (without changed regs)\n");
2440	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2441
2442	DPRINTF("Before resuming the child process where it left off and "
2443	    "without signal to be sent\n");
2444	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2445
2446	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2447	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2448
2449	validate_status_exited(status, exitval);
2450
2451	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2452	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2453}
2454#endif
2455
2456#if defined(HAVE_FPREGS)
2457ATF_TC(fpregs1);
2458ATF_TC_HEAD(fpregs1, tc)
2459{
2460	atf_tc_set_md_var(tc, "descr",
2461	    "Verify plain PT_GETFPREGS call without further steps");
2462}
2463
2464ATF_TC_BODY(fpregs1, tc)
2465{
2466	const int exitval = 5;
2467	const int sigval = SIGSTOP;
2468	pid_t child, wpid;
2469#if defined(TWAIT_HAVE_STATUS)
2470	int status;
2471#endif
2472	struct fpreg r;
2473
2474	DPRINTF("Before forking process PID=%d\n", getpid());
2475	SYSCALL_REQUIRE((child = fork()) != -1);
2476	if (child == 0) {
2477		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2478		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2479
2480		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2481		FORKEE_ASSERT(raise(sigval) == 0);
2482
2483		DPRINTF("Before exiting of the child process\n");
2484		_exit(exitval);
2485	}
2486	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2487
2488	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2489	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2490
2491	validate_status_stopped(status, sigval);
2492
2493	DPRINTF("Call GETFPREGS for the child process\n");
2494	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
2495
2496	DPRINTF("Before resuming the child process where it left off and "
2497	    "without signal to be sent\n");
2498	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2499
2500	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2501	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2502
2503	validate_status_exited(status, exitval);
2504
2505	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2506	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2507}
2508#endif
2509
2510#if defined(HAVE_FPREGS)
2511ATF_TC(fpregs2);
2512ATF_TC_HEAD(fpregs2, tc)
2513{
2514	atf_tc_set_md_var(tc, "descr",
2515	    "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing "
2516	    "regs");
2517}
2518
2519ATF_TC_BODY(fpregs2, tc)
2520{
2521	const int exitval = 5;
2522	const int sigval = SIGSTOP;
2523	pid_t child, wpid;
2524#if defined(TWAIT_HAVE_STATUS)
2525	int status;
2526#endif
2527	struct fpreg r;
2528
2529	DPRINTF("Before forking process PID=%d\n", getpid());
2530	SYSCALL_REQUIRE((child = fork()) != -1);
2531	if (child == 0) {
2532		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2533		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2534
2535		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2536		FORKEE_ASSERT(raise(sigval) == 0);
2537
2538		DPRINTF("Before exiting of the child process\n");
2539		_exit(exitval);
2540	}
2541	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2542
2543	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2544	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2545
2546	validate_status_stopped(status, sigval);
2547
2548	DPRINTF("Call GETFPREGS for the child process\n");
2549	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
2550
2551	DPRINTF("Call SETFPREGS for the child (without changed regs)\n");
2552	SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1);
2553
2554	DPRINTF("Before resuming the child process where it left off and "
2555	    "without signal to be sent\n");
2556	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2557
2558	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2559	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2560
2561	validate_status_exited(status, exitval);
2562
2563	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2564	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2565}
2566#endif
2567
2568#if defined(PT_STEP)
2569static void
2570ptrace_step(int N, int setstep)
2571{
2572	const int exitval = 5;
2573	const int sigval = SIGSTOP;
2574	pid_t child, wpid;
2575#if defined(TWAIT_HAVE_STATUS)
2576	int status;
2577#endif
2578	int happy;
2579
2580#if defined(__arm__)
2581	/* PT_STEP not supported on arm 32-bit */
2582	atf_tc_expect_fail("PR kern/52119");
2583#endif
2584
2585	DPRINTF("Before forking process PID=%d\n", getpid());
2586	SYSCALL_REQUIRE((child = fork()) != -1);
2587	if (child == 0) {
2588		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2589		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2590
2591		happy = check_happy(999);
2592
2593		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2594		FORKEE_ASSERT(raise(sigval) == 0);
2595
2596		FORKEE_ASSERT_EQ(happy, check_happy(999));
2597
2598		DPRINTF("Before exiting of the child process\n");
2599		_exit(exitval);
2600	}
2601	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2602
2603	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2604	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2605
2606	validate_status_stopped(status, sigval);
2607
2608	while (N --> 0) {
2609		if (setstep) {
2610			DPRINTF("Before resuming the child process where it "
2611			    "left off and without signal to be sent (use "
2612			    "PT_SETSTEP and PT_CONTINUE)\n");
2613			SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1);
2614			SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0)
2615			    != -1);
2616		} else {
2617			DPRINTF("Before resuming the child process where it "
2618			    "left off and without signal to be sent (use "
2619			    "PT_STEP)\n");
2620			SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0)
2621			    != -1);
2622		}
2623
2624		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2625		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
2626		    child);
2627
2628		validate_status_stopped(status, SIGTRAP);
2629
2630		if (setstep) {
2631			SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1);
2632		}
2633	}
2634
2635	DPRINTF("Before resuming the child process where it left off and "
2636	    "without signal to be sent\n");
2637	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2638
2639	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2640	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2641
2642	validate_status_exited(status, exitval);
2643
2644	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2645	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2646}
2647#endif
2648
2649#if defined(PT_STEP)
2650ATF_TC(step1);
2651ATF_TC_HEAD(step1, tc)
2652{
2653	atf_tc_set_md_var(tc, "descr",
2654	    "Verify single PT_STEP call");
2655}
2656
2657ATF_TC_BODY(step1, tc)
2658{
2659	ptrace_step(1, 0);
2660}
2661#endif
2662
2663#if defined(PT_STEP)
2664ATF_TC(step2);
2665ATF_TC_HEAD(step2, tc)
2666{
2667	atf_tc_set_md_var(tc, "descr",
2668	    "Verify PT_STEP called twice");
2669}
2670
2671ATF_TC_BODY(step2, tc)
2672{
2673	ptrace_step(2, 0);
2674}
2675#endif
2676
2677#if defined(PT_STEP)
2678ATF_TC(step3);
2679ATF_TC_HEAD(step3, tc)
2680{
2681	atf_tc_set_md_var(tc, "descr",
2682	    "Verify PT_STEP called three times");
2683}
2684
2685ATF_TC_BODY(step3, tc)
2686{
2687	ptrace_step(3, 0);
2688}
2689#endif
2690
2691#if defined(PT_STEP)
2692ATF_TC(step4);
2693ATF_TC_HEAD(step4, tc)
2694{
2695	atf_tc_set_md_var(tc, "descr",
2696	    "Verify PT_STEP called four times");
2697}
2698
2699ATF_TC_BODY(step4, tc)
2700{
2701	ptrace_step(4, 0);
2702}
2703#endif
2704
2705#if defined(PT_STEP)
2706ATF_TC(setstep1);
2707ATF_TC_HEAD(setstep1, tc)
2708{
2709	atf_tc_set_md_var(tc, "descr",
2710	    "Verify single PT_SETSTEP call");
2711}
2712
2713ATF_TC_BODY(setstep1, tc)
2714{
2715	ptrace_step(1, 1);
2716}
2717#endif
2718
2719#if defined(PT_STEP)
2720ATF_TC(setstep2);
2721ATF_TC_HEAD(setstep2, tc)
2722{
2723	atf_tc_set_md_var(tc, "descr",
2724	    "Verify PT_SETSTEP called twice");
2725}
2726
2727ATF_TC_BODY(setstep2, tc)
2728{
2729	ptrace_step(2, 1);
2730}
2731#endif
2732
2733#if defined(PT_STEP)
2734ATF_TC(setstep3);
2735ATF_TC_HEAD(setstep3, tc)
2736{
2737	atf_tc_set_md_var(tc, "descr",
2738	    "Verify PT_SETSTEP called three times");
2739}
2740
2741ATF_TC_BODY(setstep3, tc)
2742{
2743	ptrace_step(3, 1);
2744}
2745#endif
2746
2747#if defined(PT_STEP)
2748ATF_TC(setstep4);
2749ATF_TC_HEAD(setstep4, tc)
2750{
2751	atf_tc_set_md_var(tc, "descr",
2752	    "Verify PT_SETSTEP called four times");
2753}
2754
2755ATF_TC_BODY(setstep4, tc)
2756{
2757	ptrace_step(4, 1);
2758}
2759#endif
2760
2761ATF_TC(kill1);
2762ATF_TC_HEAD(kill1, tc)
2763{
2764	atf_tc_set_md_var(tc, "descr",
2765	    "Verify that PT_CONTINUE with SIGKILL terminates child");
2766}
2767
2768ATF_TC_BODY(kill1, tc)
2769{
2770	const int sigval = SIGSTOP, sigsent = SIGKILL;
2771	pid_t child, wpid;
2772#if defined(TWAIT_HAVE_STATUS)
2773	int status;
2774#endif
2775
2776	DPRINTF("Before forking process PID=%d\n", getpid());
2777	SYSCALL_REQUIRE((child = fork()) != -1);
2778	if (child == 0) {
2779		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2780		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2781
2782		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2783		FORKEE_ASSERT(raise(sigval) == 0);
2784
2785		/* NOTREACHED */
2786		FORKEE_ASSERTX(0 &&
2787		    "Child should be terminated by a signal from its parent");
2788	}
2789	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2790
2791	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2792	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2793
2794	validate_status_stopped(status, sigval);
2795
2796	DPRINTF("Before resuming the child process where it left off and "
2797	    "without signal to be sent\n");
2798	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
2799
2800	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2801	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2802
2803	validate_status_signaled(status, sigsent, 0);
2804
2805	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2806	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2807}
2808
2809ATF_TC(kill2);
2810ATF_TC_HEAD(kill2, tc)
2811{
2812	atf_tc_set_md_var(tc, "descr",
2813	    "Verify that PT_KILL terminates child");
2814}
2815
2816ATF_TC_BODY(kill2, tc)
2817{
2818	const int sigval = SIGSTOP;
2819	pid_t child, wpid;
2820#if defined(TWAIT_HAVE_STATUS)
2821	int status;
2822#endif
2823
2824	DPRINTF("Before forking process PID=%d\n", getpid());
2825	SYSCALL_REQUIRE((child = fork()) != -1);
2826	if (child == 0) {
2827		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2828		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2829
2830		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2831		FORKEE_ASSERT(raise(sigval) == 0);
2832
2833		/* NOTREACHED */
2834		FORKEE_ASSERTX(0 &&
2835		    "Child should be terminated by a signal from its parent");
2836	}
2837	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2838
2839	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2840	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2841
2842	validate_status_stopped(status, sigval);
2843
2844	DPRINTF("Before resuming the child process where it left off and "
2845	    "without signal to be sent\n");
2846	SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
2847
2848	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2849	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2850
2851	validate_status_signaled(status, SIGKILL, 0);
2852
2853	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2854	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2855}
2856
2857ATF_TC(lwpinfo1);
2858ATF_TC_HEAD(lwpinfo1, tc)
2859{
2860	atf_tc_set_md_var(tc, "descr",
2861	    "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
2862}
2863
2864ATF_TC_BODY(lwpinfo1, tc)
2865{
2866	const int exitval = 5;
2867	const int sigval = SIGSTOP;
2868	pid_t child, wpid;
2869#if defined(TWAIT_HAVE_STATUS)
2870	int status;
2871#endif
2872	struct ptrace_lwpinfo info = {0, 0};
2873
2874	DPRINTF("Before forking process PID=%d\n", getpid());
2875	SYSCALL_REQUIRE((child = fork()) != -1);
2876	if (child == 0) {
2877		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2878		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2879
2880		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2881		FORKEE_ASSERT(raise(sigval) == 0);
2882
2883		DPRINTF("Before exiting of the child process\n");
2884		_exit(exitval);
2885	}
2886	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2887
2888	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2889	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2890
2891	validate_status_stopped(status, sigval);
2892
2893	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2894	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
2895
2896	DPRINTF("Assert that there exists a thread\n");
2897	ATF_REQUIRE(info.pl_lwpid > 0);
2898
2899	DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
2900	    info.pl_lwpid);
2901	ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
2902	    "Received event %d != expected event %d",
2903	    info.pl_event, PL_EVENT_SIGNAL);
2904
2905	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2906	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
2907
2908	DPRINTF("Assert that there are no more lwp threads in child\n");
2909	ATF_REQUIRE_EQ(info.pl_lwpid, 0);
2910
2911	DPRINTF("Before resuming the child process where it left off and "
2912	    "without signal to be sent\n");
2913	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2914
2915	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2916	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2917
2918	validate_status_exited(status, exitval);
2919
2920	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2921	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2922}
2923
2924#if defined(TWAIT_HAVE_PID)
2925ATF_TC(lwpinfo2);
2926ATF_TC_HEAD(lwpinfo2, tc)
2927{
2928	atf_tc_set_md_var(tc, "descr",
2929	    "Verify basic LWPINFO call for single thread (PT_ATTACH from "
2930	    "tracer)");
2931}
2932
2933ATF_TC_BODY(lwpinfo2, tc)
2934{
2935	struct msg_fds parent_tracee, parent_tracer;
2936	const int exitval_tracee = 5;
2937	const int exitval_tracer = 10;
2938	pid_t tracee, tracer, wpid;
2939	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
2940#if defined(TWAIT_HAVE_STATUS)
2941	int status;
2942#endif
2943	struct ptrace_lwpinfo info = {0, 0};
2944
2945	DPRINTF("Spawn tracee\n");
2946	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
2947	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
2948	tracee = atf_utils_fork();
2949	if (tracee == 0) {
2950
2951		/* Wait for message from the parent */
2952		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
2953		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
2954
2955		_exit(exitval_tracee);
2956	}
2957	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
2958
2959	DPRINTF("Spawn debugger\n");
2960	tracer = atf_utils_fork();
2961	if (tracer == 0) {
2962		/* No IPC to communicate with the child */
2963		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
2964		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
2965
2966		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
2967		FORKEE_REQUIRE_SUCCESS(
2968		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2969
2970		forkee_status_stopped(status, SIGSTOP);
2971
2972		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2973		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
2974		    != -1);
2975
2976		DPRINTF("Assert that there exists a thread\n");
2977		FORKEE_ASSERTX(info.pl_lwpid > 0);
2978
2979		DPRINTF("Assert that lwp thread %d received event "
2980		    "PL_EVENT_SIGNAL\n", info.pl_lwpid);
2981		FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
2982
2983		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2984		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
2985		    != -1);
2986
2987		DPRINTF("Assert that there are no more lwp threads in child\n");
2988		FORKEE_ASSERTX(info.pl_lwpid == 0);
2989
2990		/* Resume tracee with PT_CONTINUE */
2991		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2992
2993		/* Inform parent that tracer has attached to tracee */
2994		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
2995		/* Wait for parent */
2996		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
2997
2998		/* Wait for tracee and assert that it exited */
2999		FORKEE_REQUIRE_SUCCESS(
3000		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
3001
3002		forkee_status_exited(status, exitval_tracee);
3003
3004		DPRINTF("Before exiting of the tracer process\n");
3005		_exit(exitval_tracer);
3006	}
3007
3008	DPRINTF("Wait for the tracer to attach to the tracee\n");
3009	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
3010
3011	DPRINTF("Resume the tracee and let it exit\n");
3012	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
3013
3014	DPRINTF("Detect that tracee is zombie\n");
3015	await_zombie(tracee);
3016
3017	DPRINTF("Assert that there is no status about tracee - "
3018	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
3019	TWAIT_REQUIRE_SUCCESS(
3020	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
3021
3022	DPRINTF("Resume the tracer and let it detect exited tracee\n");
3023	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
3024
3025	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
3026	    TWAIT_FNAME);
3027	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
3028	    tracer);
3029
3030	validate_status_exited(status, exitval_tracer);
3031
3032	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
3033	    TWAIT_FNAME);
3034	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
3035	    tracee);
3036
3037	validate_status_exited(status, exitval_tracee);
3038
3039	msg_close(&parent_tracer);
3040	msg_close(&parent_tracee);
3041}
3042#endif
3043
3044ATF_TC(siginfo1);
3045ATF_TC_HEAD(siginfo1, tc)
3046{
3047	atf_tc_set_md_var(tc, "descr",
3048	    "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
3049}
3050
3051ATF_TC_BODY(siginfo1, tc)
3052{
3053	const int exitval = 5;
3054	const int sigval = SIGTRAP;
3055	pid_t child, wpid;
3056#if defined(TWAIT_HAVE_STATUS)
3057	int status;
3058#endif
3059	struct ptrace_siginfo info;
3060	memset(&info, 0, sizeof(info));
3061
3062	DPRINTF("Before forking process PID=%d\n", getpid());
3063	SYSCALL_REQUIRE((child = fork()) != -1);
3064	if (child == 0) {
3065		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3066		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3067
3068		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3069		FORKEE_ASSERT(raise(sigval) == 0);
3070
3071		DPRINTF("Before exiting of the child process\n");
3072		_exit(exitval);
3073	}
3074	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3075
3076	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3077	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3078
3079	validate_status_stopped(status, sigval);
3080
3081	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3082	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3083
3084	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3085	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3086	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3087	    info.psi_siginfo.si_errno);
3088
3089	DPRINTF("Before resuming the child process where it left off and "
3090	    "without signal to be sent\n");
3091	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3092
3093	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3094	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3095
3096	validate_status_exited(status, exitval);
3097
3098	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3099	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3100}
3101
3102ATF_TC(siginfo2);
3103ATF_TC_HEAD(siginfo2, tc)
3104{
3105	atf_tc_set_md_var(tc, "descr",
3106	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
3107	    "modification of SIGINT from tracee");
3108}
3109
3110static int siginfo2_caught = 0;
3111
3112static void
3113siginfo2_sighandler(int sig)
3114{
3115	FORKEE_ASSERT_EQ(sig, SIGINT);
3116
3117	++siginfo2_caught;
3118}
3119
3120ATF_TC_BODY(siginfo2, tc)
3121{
3122	const int exitval = 5;
3123	const int sigval = SIGINT;
3124	pid_t child, wpid;
3125	struct sigaction sa;
3126#if defined(TWAIT_HAVE_STATUS)
3127	int status;
3128#endif
3129	struct ptrace_siginfo info;
3130	memset(&info, 0, sizeof(info));
3131
3132	DPRINTF("Before forking process PID=%d\n", getpid());
3133	SYSCALL_REQUIRE((child = fork()) != -1);
3134	if (child == 0) {
3135		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3136		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3137
3138		sa.sa_handler = siginfo2_sighandler;
3139		sa.sa_flags = SA_SIGINFO;
3140		sigemptyset(&sa.sa_mask);
3141
3142		FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
3143
3144		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3145		FORKEE_ASSERT(raise(sigval) == 0);
3146
3147		FORKEE_ASSERT_EQ(siginfo2_caught, 1);
3148
3149		DPRINTF("Before exiting of the child process\n");
3150		_exit(exitval);
3151	}
3152	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3153
3154	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3155	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3156
3157	validate_status_stopped(status, sigval);
3158
3159	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3160	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3161
3162	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3163	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3164	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3165	    info.psi_siginfo.si_errno);
3166
3167	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
3168	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
3169
3170	DPRINTF("Before resuming the child process where it left off and "
3171	    "without signal to be sent\n");
3172	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
3173
3174	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3175	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3176
3177	validate_status_exited(status, exitval);
3178
3179	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3180	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3181}
3182
3183ATF_TC(siginfo3);
3184ATF_TC_HEAD(siginfo3, tc)
3185{
3186	atf_tc_set_md_var(tc, "descr",
3187	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
3188	    "setting signal to new value");
3189}
3190
3191static int siginfo3_caught = 0;
3192
3193static void
3194siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
3195{
3196	FORKEE_ASSERT_EQ(sig, SIGTRAP);
3197
3198	FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
3199	FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
3200
3201	++siginfo3_caught;
3202}
3203
3204ATF_TC_BODY(siginfo3, tc)
3205{
3206	const int exitval = 5;
3207	const int sigval = SIGINT;
3208	const int sigfaked = SIGTRAP;
3209	const int sicodefaked = TRAP_BRKPT;
3210	pid_t child, wpid;
3211	struct sigaction sa;
3212#if defined(TWAIT_HAVE_STATUS)
3213	int status;
3214#endif
3215	struct ptrace_siginfo info;
3216	memset(&info, 0, sizeof(info));
3217
3218	DPRINTF("Before forking process PID=%d\n", getpid());
3219	SYSCALL_REQUIRE((child = fork()) != -1);
3220	if (child == 0) {
3221		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3222		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3223
3224		sa.sa_sigaction = siginfo3_sigaction;
3225		sa.sa_flags = SA_SIGINFO;
3226		sigemptyset(&sa.sa_mask);
3227
3228		FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
3229
3230		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3231		FORKEE_ASSERT(raise(sigval) == 0);
3232
3233		FORKEE_ASSERT_EQ(siginfo3_caught, 1);
3234
3235		DPRINTF("Before exiting of the child process\n");
3236		_exit(exitval);
3237	}
3238	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3239
3240	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3241	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3242
3243	validate_status_stopped(status, sigval);
3244
3245	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3246	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3247
3248	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3249	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3250	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3251	    info.psi_siginfo.si_errno);
3252
3253	DPRINTF("Before setting new faked signal to signo=%d si_code=%d\n",
3254	    sigfaked, sicodefaked);
3255	info.psi_siginfo.si_signo = sigfaked;
3256	info.psi_siginfo.si_code = sicodefaked;
3257
3258	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
3259	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
3260
3261	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3262	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3263
3264	DPRINTF("Before checking siginfo_t\n");
3265	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
3266	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
3267
3268	DPRINTF("Before resuming the child process where it left off and "
3269	    "without signal to be sent\n");
3270	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
3271
3272	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3273	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3274
3275	validate_status_exited(status, exitval);
3276
3277	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3278	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3279}
3280
3281ATF_TC(siginfo4);
3282ATF_TC_HEAD(siginfo4, tc)
3283{
3284	atf_tc_set_md_var(tc, "descr",
3285	    "Detect SIGTRAP TRAP_EXEC from tracee");
3286}
3287
3288ATF_TC_BODY(siginfo4, tc)
3289{
3290	const int sigval = SIGTRAP;
3291	pid_t child, wpid;
3292#if defined(TWAIT_HAVE_STATUS)
3293	int status;
3294#endif
3295
3296	struct ptrace_siginfo info;
3297	memset(&info, 0, sizeof(info));
3298
3299	DPRINTF("Before forking process PID=%d\n", getpid());
3300	SYSCALL_REQUIRE((child = fork()) != -1);
3301	if (child == 0) {
3302		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3303		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3304
3305		DPRINTF("Before calling execve(2) from child\n");
3306		execlp("/bin/echo", "/bin/echo", NULL);
3307
3308		FORKEE_ASSERT(0 && "Not reached");
3309	}
3310	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3311
3312	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3313	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3314
3315	validate_status_stopped(status, sigval);
3316
3317	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3318	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3319
3320	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3321	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3322	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3323	    info.psi_siginfo.si_errno);
3324
3325	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3326	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
3327
3328	DPRINTF("Before resuming the child process where it left off and "
3329	    "without signal to be sent\n");
3330	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3331
3332	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3333	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3334
3335	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3336	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3337}
3338
3339#if defined(TWAIT_HAVE_PID)
3340ATF_TC(siginfo5);
3341ATF_TC_HEAD(siginfo5, tc)
3342{
3343	atf_tc_set_md_var(tc, "descr",
3344	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
3345	    "set to PTRACE_FORK and reports correct signal information");
3346}
3347
3348ATF_TC_BODY(siginfo5, tc)
3349{
3350	const int exitval = 5;
3351	const int exitval2 = 15;
3352	const int sigval = SIGSTOP;
3353	pid_t child, child2, wpid;
3354#if defined(TWAIT_HAVE_STATUS)
3355	int status;
3356#endif
3357	ptrace_state_t state;
3358	const int slen = sizeof(state);
3359	ptrace_event_t event;
3360	const int elen = sizeof(event);
3361	struct ptrace_siginfo info;
3362
3363	memset(&info, 0, sizeof(info));
3364
3365	DPRINTF("Before forking process PID=%d\n", getpid());
3366	SYSCALL_REQUIRE((child = fork()) != -1);
3367	if (child == 0) {
3368		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3369		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3370
3371		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3372		FORKEE_ASSERT(raise(sigval) == 0);
3373
3374		FORKEE_ASSERT((child2 = fork()) != -1);
3375
3376		if (child2 == 0)
3377			_exit(exitval2);
3378
3379		FORKEE_REQUIRE_SUCCESS
3380		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3381
3382		forkee_status_exited(status, exitval2);
3383
3384		DPRINTF("Before exiting of the child process\n");
3385		_exit(exitval);
3386	}
3387	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3388
3389	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3390	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3391
3392	validate_status_stopped(status, sigval);
3393
3394	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3395	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3396
3397	DPRINTF("Before checking siginfo_t\n");
3398	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3399	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3400
3401	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
3402	event.pe_set_event = PTRACE_FORK;
3403	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3404
3405	DPRINTF("Before resuming the child process where it left off and "
3406	    "without signal to be sent\n");
3407        DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
3408               "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
3409               "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
3410                "state.pe_other_pid=child)\n", child);
3411	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3412
3413	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
3414	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3415
3416	validate_status_stopped(status, SIGTRAP);
3417
3418	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3419	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3420
3421	DPRINTF("Before checking siginfo_t\n");
3422	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3423	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
3424
3425	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3426	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
3427
3428	child2 = state.pe_other_pid;
3429	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
3430
3431	DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
3432	    TWAIT_FNAME, child2, child);
3433	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3434	    child2);
3435
3436	validate_status_stopped(status, SIGTRAP);
3437
3438	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3439	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3440
3441	DPRINTF("Before checking siginfo_t\n");
3442	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3443	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
3444
3445	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
3446	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
3447	ATF_REQUIRE_EQ(state.pe_other_pid, child);
3448
3449	DPRINTF("Before resuming the forkee process where it left off and "
3450	    "without signal to be sent\n");
3451	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
3452
3453	DPRINTF("Before resuming the child process where it left off and "
3454	    "without signal to be sent\n");
3455	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3456
3457	DPRINTF("Before calling %s() for the forkee - expected exited\n",
3458	    TWAIT_FNAME);
3459	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3460	    child2);
3461
3462	validate_status_exited(status, exitval2);
3463
3464	DPRINTF("Before calling %s() for the forkee - expected no process\n",
3465	    TWAIT_FNAME);
3466	TWAIT_REQUIRE_FAILURE(ECHILD,
3467	    wpid = TWAIT_GENERIC(child2, &status, 0));
3468
3469	DPRINTF("Before calling %s() for the child - expected stopped "
3470	    "SIGCHLD\n", TWAIT_FNAME);
3471	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3472
3473	validate_status_stopped(status, SIGCHLD);
3474
3475	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3476	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3477
3478	DPRINTF("Before checking siginfo_t\n");
3479	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
3480	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
3481
3482	DPRINTF("Before resuming the child process where it left off and "
3483	    "without signal to be sent\n");
3484	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3485
3486	DPRINTF("Before calling %s() for the child - expected exited\n",
3487	    TWAIT_FNAME);
3488	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3489
3490	validate_status_exited(status, exitval);
3491
3492	DPRINTF("Before calling %s() for the child - expected no process\n",
3493	    TWAIT_FNAME);
3494	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3495}
3496#endif
3497
3498#if defined(PT_STEP)
3499ATF_TC(siginfo6);
3500ATF_TC_HEAD(siginfo6, tc)
3501{
3502	atf_tc_set_md_var(tc, "descr",
3503	    "Verify single PT_STEP call with signal information check");
3504}
3505
3506ATF_TC_BODY(siginfo6, tc)
3507{
3508	const int exitval = 5;
3509	const int sigval = SIGSTOP;
3510	pid_t child, wpid;
3511#if defined(TWAIT_HAVE_STATUS)
3512	int status;
3513#endif
3514	int happy;
3515	struct ptrace_siginfo info;
3516
3517#if defined(__arm__)
3518	/* PT_STEP not supported on arm 32-bit */
3519	atf_tc_expect_fail("PR kern/52119");
3520#endif
3521
3522	memset(&info, 0, sizeof(info));
3523
3524	DPRINTF("Before forking process PID=%d\n", getpid());
3525	SYSCALL_REQUIRE((child = fork()) != -1);
3526	if (child == 0) {
3527		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3528		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3529
3530		happy = check_happy(100);
3531
3532		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3533		FORKEE_ASSERT(raise(sigval) == 0);
3534
3535		FORKEE_ASSERT_EQ(happy, check_happy(100));
3536
3537		DPRINTF("Before exiting of the child process\n");
3538		_exit(exitval);
3539	}
3540	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3541
3542	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3543	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3544
3545	validate_status_stopped(status, sigval);
3546
3547	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3548	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3549
3550	DPRINTF("Before checking siginfo_t\n");
3551	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3552	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3553
3554	DPRINTF("Before resuming the child process where it left off and "
3555	    "without signal to be sent (use PT_STEP)\n");
3556	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
3557
3558	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3559	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3560
3561	validate_status_stopped(status, SIGTRAP);
3562
3563	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3564	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3565
3566	DPRINTF("Before checking siginfo_t\n");
3567	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3568	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
3569
3570	DPRINTF("Before resuming the child process where it left off and "
3571	    "without signal to be sent\n");
3572	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3573
3574	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3575	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3576
3577	validate_status_exited(status, exitval);
3578
3579	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3580	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3581}
3582#endif
3583
3584volatile lwpid_t the_lwp_id = 0;
3585
3586static void
3587lwp_main_func(void *arg)
3588{
3589	the_lwp_id = _lwp_self();
3590	_lwp_exit();
3591}
3592
3593ATF_TC(lwp_create1);
3594ATF_TC_HEAD(lwp_create1, tc)
3595{
3596	atf_tc_set_md_var(tc, "descr",
3597	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
3598	    "EVENT_MASK set to PTRACE_LWP_CREATE");
3599}
3600
3601ATF_TC_BODY(lwp_create1, tc)
3602{
3603	const int exitval = 5;
3604	const int sigval = SIGSTOP;
3605	pid_t child, wpid;
3606#if defined(TWAIT_HAVE_STATUS)
3607	int status;
3608#endif
3609	ptrace_state_t state;
3610	const int slen = sizeof(state);
3611	ptrace_event_t event;
3612	const int elen = sizeof(event);
3613	ucontext_t uc;
3614	lwpid_t lid;
3615	static const size_t ssize = 16*1024;
3616	void *stack;
3617
3618	DPRINTF("Before forking process PID=%d\n", getpid());
3619	SYSCALL_REQUIRE((child = fork()) != -1);
3620	if (child == 0) {
3621		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3622		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3623
3624		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3625		FORKEE_ASSERT(raise(sigval) == 0);
3626
3627		DPRINTF("Before allocating memory for stack in child\n");
3628		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
3629
3630		DPRINTF("Before making context for new lwp in child\n");
3631		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
3632
3633		DPRINTF("Before creating new in child\n");
3634		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
3635
3636		DPRINTF("Before waiting for lwp %d to exit\n", lid);
3637		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
3638
3639		DPRINTF("Before verifying that reported %d and running lid %d "
3640		    "are the same\n", lid, the_lwp_id);
3641		FORKEE_ASSERT_EQ(lid, the_lwp_id);
3642
3643		DPRINTF("Before exiting of the child process\n");
3644		_exit(exitval);
3645	}
3646	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3647
3648	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3649	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3650
3651	validate_status_stopped(status, sigval);
3652
3653	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
3654	event.pe_set_event = PTRACE_LWP_CREATE;
3655	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3656
3657	DPRINTF("Before resuming the child process where it left off and "
3658	    "without signal to be sent\n");
3659	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3660
3661	DPRINTF("Before calling %s() for the child - expected stopped "
3662	    "SIGTRAP\n", TWAIT_FNAME);
3663	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3664
3665	validate_status_stopped(status, SIGTRAP);
3666
3667	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3668
3669	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
3670
3671	lid = state.pe_lwp;
3672	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
3673
3674	DPRINTF("Before resuming the child process where it left off and "
3675	    "without signal to be sent\n");
3676	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3677
3678	DPRINTF("Before calling %s() for the child - expected exited\n",
3679	    TWAIT_FNAME);
3680	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3681
3682	validate_status_exited(status, exitval);
3683
3684	DPRINTF("Before calling %s() for the child - expected no process\n",
3685	    TWAIT_FNAME);
3686	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3687}
3688
3689ATF_TC(lwp_exit1);
3690ATF_TC_HEAD(lwp_exit1, tc)
3691{
3692	atf_tc_set_md_var(tc, "descr",
3693	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
3694	    "EVENT_MASK set to PTRACE_LWP_EXIT");
3695}
3696
3697ATF_TC_BODY(lwp_exit1, tc)
3698{
3699	const int exitval = 5;
3700	const int sigval = SIGSTOP;
3701	pid_t child, wpid;
3702#if defined(TWAIT_HAVE_STATUS)
3703	int status;
3704#endif
3705	ptrace_state_t state;
3706	const int slen = sizeof(state);
3707	ptrace_event_t event;
3708	const int elen = sizeof(event);
3709	ucontext_t uc;
3710	lwpid_t lid;
3711	static const size_t ssize = 16*1024;
3712	void *stack;
3713
3714	DPRINTF("Before forking process PID=%d\n", getpid());
3715	SYSCALL_REQUIRE((child = fork()) != -1);
3716	if (child == 0) {
3717		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3718		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3719
3720		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3721		FORKEE_ASSERT(raise(sigval) == 0);
3722
3723		DPRINTF("Before allocating memory for stack in child\n");
3724		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
3725
3726		DPRINTF("Before making context for new lwp in child\n");
3727		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
3728
3729		DPRINTF("Before creating new in child\n");
3730		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
3731
3732		DPRINTF("Before waiting for lwp %d to exit\n", lid);
3733		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
3734
3735		DPRINTF("Before verifying that reported %d and running lid %d "
3736		    "are the same\n", lid, the_lwp_id);
3737		FORKEE_ASSERT_EQ(lid, the_lwp_id);
3738
3739		DPRINTF("Before exiting of the child process\n");
3740		_exit(exitval);
3741	}
3742	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3743
3744	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3745	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3746
3747	validate_status_stopped(status, sigval);
3748
3749	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
3750	event.pe_set_event = PTRACE_LWP_EXIT;
3751	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3752
3753	DPRINTF("Before resuming the child process where it left off and "
3754	    "without signal to be sent\n");
3755	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3756
3757	DPRINTF("Before calling %s() for the child - expected stopped "
3758	    "SIGTRAP\n", TWAIT_FNAME);
3759	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3760
3761	validate_status_stopped(status, SIGTRAP);
3762
3763	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3764
3765	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
3766
3767	lid = state.pe_lwp;
3768	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
3769
3770	DPRINTF("Before resuming the child process where it left off and "
3771	    "without signal to be sent\n");
3772	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3773
3774	DPRINTF("Before calling %s() for the child - expected exited\n",
3775	    TWAIT_FNAME);
3776	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3777
3778	validate_status_exited(status, exitval);
3779
3780	DPRINTF("Before calling %s() for the child - expected no process\n",
3781	    TWAIT_FNAME);
3782	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3783}
3784
3785ATF_TC(signal1);
3786ATF_TC_HEAD(signal1, tc)
3787{
3788	atf_tc_set_md_var(tc, "descr",
3789	    "Verify that masking single unrelated signal does not stop tracer "
3790	    "from catching other signals");
3791}
3792
3793ATF_TC_BODY(signal1, tc)
3794{
3795	const int exitval = 5;
3796	const int sigval = SIGSTOP;
3797	const int sigmasked = SIGTRAP;
3798	const int signotmasked = SIGINT;
3799	pid_t child, wpid;
3800#if defined(TWAIT_HAVE_STATUS)
3801	int status;
3802#endif
3803	sigset_t intmask;
3804
3805	DPRINTF("Before forking process PID=%d\n", getpid());
3806	SYSCALL_REQUIRE((child = fork()) != -1);
3807	if (child == 0) {
3808		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3809		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3810
3811		sigemptyset(&intmask);
3812		sigaddset(&intmask, sigmasked);
3813		sigprocmask(SIG_BLOCK, &intmask, NULL);
3814
3815		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3816		FORKEE_ASSERT(raise(sigval) == 0);
3817
3818		DPRINTF("Before raising %s from child\n",
3819		    strsignal(signotmasked));
3820		FORKEE_ASSERT(raise(signotmasked) == 0);
3821
3822		DPRINTF("Before exiting of the child process\n");
3823		_exit(exitval);
3824	}
3825	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3826
3827	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3828	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3829
3830	validate_status_stopped(status, sigval);
3831
3832	DPRINTF("Before resuming the child process where it left off and "
3833	    "without signal to be sent\n");
3834	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3835
3836	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3837	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3838
3839	validate_status_stopped(status, signotmasked);
3840
3841	DPRINTF("Before resuming the child process where it left off and "
3842	    "without signal to be sent\n");
3843	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3844
3845	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3846	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3847
3848	validate_status_exited(status, exitval);
3849
3850	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3851	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3852}
3853
3854ATF_TC(signal2);
3855ATF_TC_HEAD(signal2, tc)
3856{
3857	atf_tc_set_md_var(tc, "descr",
3858	    "Verify that masking SIGTRAP in tracee stops tracer from "
3859	    "catching this raised signal");
3860}
3861
3862ATF_TC_BODY(signal2, tc)
3863{
3864	const int exitval = 5;
3865	const int sigval = SIGSTOP;
3866	const int sigmasked = SIGTRAP;
3867	pid_t child, wpid;
3868#if defined(TWAIT_HAVE_STATUS)
3869	int status;
3870#endif
3871	sigset_t intmask;
3872
3873	DPRINTF("Before forking process PID=%d\n", getpid());
3874	SYSCALL_REQUIRE((child = fork()) != -1);
3875	if (child == 0) {
3876		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3877		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3878
3879		sigemptyset(&intmask);
3880		sigaddset(&intmask, sigmasked);
3881		sigprocmask(SIG_BLOCK, &intmask, NULL);
3882
3883		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3884		FORKEE_ASSERT(raise(sigval) == 0);
3885
3886		DPRINTF("Before raising %s breakpoint from child\n",
3887		    strsignal(sigmasked));
3888		FORKEE_ASSERT(raise(sigmasked) == 0);
3889
3890		DPRINTF("Before exiting of the child process\n");
3891		_exit(exitval);
3892	}
3893	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3894
3895	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3896	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3897
3898	validate_status_stopped(status, sigval);
3899
3900	DPRINTF("Before resuming the child process where it left off and "
3901	    "without signal to be sent\n");
3902	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3903
3904	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3905	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3906
3907	validate_status_exited(status, exitval);
3908
3909	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3910	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3911}
3912
3913ATF_TC(signal3);
3914ATF_TC_HEAD(signal3, tc)
3915{
3916	atf_tc_set_md_var(tc, "timeout", "5");
3917	atf_tc_set_md_var(tc, "descr",
3918	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
3919	    "catching software breakpoints");
3920}
3921
3922ATF_TC_BODY(signal3, tc)
3923{
3924	const int exitval = 5;
3925	const int sigval = SIGSTOP;
3926	const int sigmasked = SIGTRAP;
3927	pid_t child, wpid;
3928#if defined(TWAIT_HAVE_STATUS)
3929	int status;
3930#endif
3931	sigset_t intmask;
3932
3933	DPRINTF("Before forking process PID=%d\n", getpid());
3934	SYSCALL_REQUIRE((child = fork()) != -1);
3935	if (child == 0) {
3936		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3937		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3938
3939		sigemptyset(&intmask);
3940		sigaddset(&intmask, sigmasked);
3941		sigprocmask(SIG_BLOCK, &intmask, NULL);
3942
3943		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3944		FORKEE_ASSERT(raise(sigval) == 0);
3945
3946		DPRINTF("Before raising software breakpoint from child\n");
3947		trigger_trap();
3948
3949		DPRINTF("Before exiting of the child process\n");
3950		_exit(exitval);
3951	}
3952	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3953
3954	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3955	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3956
3957	validate_status_stopped(status, sigval);
3958
3959	DPRINTF("Before resuming the child process where it left off and "
3960	    "without signal to be sent\n");
3961	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3962
3963	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3964	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3965
3966	validate_status_stopped(status, sigmasked);
3967
3968	DPRINTF("Before resuming the child process where it left off and "
3969	    "without signal to be sent\n");
3970	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
3971
3972	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3973	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3974
3975	validate_status_signaled(status, SIGKILL, 0);
3976
3977	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3978	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3979}
3980
3981#if defined(PT_STEP)
3982ATF_TC(signal4);
3983ATF_TC_HEAD(signal4, tc)
3984{
3985	atf_tc_set_md_var(tc, "descr",
3986	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
3987	    "catching single step trap");
3988}
3989
3990ATF_TC_BODY(signal4, tc)
3991{
3992	const int exitval = 5;
3993	const int sigval = SIGSTOP;
3994	const int sigmasked = SIGTRAP;
3995	pid_t child, wpid;
3996#if defined(TWAIT_HAVE_STATUS)
3997	int status;
3998#endif
3999	sigset_t intmask;
4000	int happy;
4001
4002#if defined(__arm__)
4003	/* PT_STEP not supported on arm 32-bit */
4004	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
4005#endif
4006
4007	DPRINTF("Before forking process PID=%d\n", getpid());
4008	SYSCALL_REQUIRE((child = fork()) != -1);
4009	if (child == 0) {
4010		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4011		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4012
4013		happy = check_happy(100);
4014
4015		sigemptyset(&intmask);
4016		sigaddset(&intmask, sigmasked);
4017		sigprocmask(SIG_BLOCK, &intmask, NULL);
4018
4019		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4020		FORKEE_ASSERT(raise(sigval) == 0);
4021
4022		FORKEE_ASSERT_EQ(happy, check_happy(100));
4023
4024		DPRINTF("Before exiting of the child process\n");
4025		_exit(exitval);
4026	}
4027	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4028
4029	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4030	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4031
4032	validate_status_stopped(status, sigval);
4033
4034	DPRINTF("Before resuming the child process where it left off and "
4035	    "without signal to be sent\n");
4036	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
4037
4038	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4039	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4040
4041	validate_status_stopped(status, sigmasked);
4042
4043	DPRINTF("Before resuming the child process where it left off and "
4044	    "without signal to be sent\n");
4045	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4046
4047	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4048	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4049
4050	validate_status_exited(status, exitval);
4051
4052	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4053	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4054}
4055#endif
4056
4057ATF_TC(signal5);
4058ATF_TC_HEAD(signal5, tc)
4059{
4060	atf_tc_set_md_var(tc, "descr",
4061	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4062	    "catching exec() breakpoint");
4063}
4064
4065ATF_TC_BODY(signal5, tc)
4066{
4067	const int sigval = SIGSTOP;
4068	const int sigmasked = SIGTRAP;
4069	pid_t child, wpid;
4070#if defined(TWAIT_HAVE_STATUS)
4071	int status;
4072#endif
4073	struct ptrace_siginfo info;
4074	sigset_t intmask;
4075
4076	memset(&info, 0, sizeof(info));
4077
4078	DPRINTF("Before forking process PID=%d\n", getpid());
4079	SYSCALL_REQUIRE((child = fork()) != -1);
4080	if (child == 0) {
4081		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4082		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4083
4084		sigemptyset(&intmask);
4085		sigaddset(&intmask, sigmasked);
4086		sigprocmask(SIG_BLOCK, &intmask, NULL);
4087
4088		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4089		FORKEE_ASSERT(raise(sigval) == 0);
4090
4091		DPRINTF("Before calling execve(2) from child\n");
4092		execlp("/bin/echo", "/bin/echo", NULL);
4093
4094		/* NOTREACHED */
4095		FORKEE_ASSERTX(0 && "Not reached");
4096	}
4097	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4098
4099	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4100	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4101
4102	validate_status_stopped(status, sigval);
4103
4104	DPRINTF("Before resuming the child process where it left off and "
4105	    "without signal to be sent\n");
4106	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4107
4108	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4109	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4110
4111	validate_status_stopped(status, sigmasked);
4112
4113	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4114	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4115
4116	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4117	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4118	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4119	    info.psi_siginfo.si_errno);
4120
4121	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigmasked);
4122	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
4123
4124	DPRINTF("Before resuming the child process where it left off and "
4125	    "without signal to be sent\n");
4126	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4127
4128	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4129	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4130
4131	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4132	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4133}
4134
4135#if defined(TWAIT_HAVE_PID)
4136ATF_TC(signal6);
4137ATF_TC_HEAD(signal6, tc)
4138{
4139	atf_tc_set_md_var(tc, "timeout", "5");
4140	atf_tc_set_md_var(tc, "descr",
4141	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4142	    "catching PTRACE_FORK breakpoint");
4143}
4144
4145ATF_TC_BODY(signal6, tc)
4146{
4147	const int exitval = 5;
4148	const int exitval2 = 15;
4149	const int sigval = SIGSTOP;
4150	const int sigmasked = SIGTRAP;
4151	pid_t child, child2, wpid;
4152#if defined(TWAIT_HAVE_STATUS)
4153	int status;
4154#endif
4155	sigset_t intmask;
4156	ptrace_state_t state;
4157	const int slen = sizeof(state);
4158	ptrace_event_t event;
4159	const int elen = sizeof(event);
4160
4161	atf_tc_expect_fail("PR kern/51918");
4162
4163	DPRINTF("Before forking process PID=%d\n", getpid());
4164	SYSCALL_REQUIRE((child = fork()) != -1);
4165	if (child == 0) {
4166		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4167		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4168
4169		sigemptyset(&intmask);
4170		sigaddset(&intmask, sigmasked);
4171		sigprocmask(SIG_BLOCK, &intmask, NULL);
4172
4173		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4174		FORKEE_ASSERT(raise(sigval) == 0);
4175
4176		FORKEE_ASSERT((child2 = fork()) != -1);
4177
4178		if (child2 == 0)
4179			_exit(exitval2);
4180
4181		FORKEE_REQUIRE_SUCCESS
4182			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4183
4184		forkee_status_exited(status, exitval2);
4185
4186		DPRINTF("Before exiting of the child process\n");
4187		_exit(exitval);
4188	}
4189	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4190
4191	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4192	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4193
4194	validate_status_stopped(status, sigval);
4195
4196	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
4197	event.pe_set_event = PTRACE_FORK;
4198	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4199
4200	DPRINTF("Before resuming the child process where it left off and "
4201	    "without signal to be sent\n");
4202	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4203
4204	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4205	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4206
4207	validate_status_stopped(status, sigmasked);
4208
4209	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4210	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
4211
4212	child2 = state.pe_other_pid;
4213	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
4214
4215	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
4216	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4217	    child2);
4218
4219	validate_status_stopped(status, SIGTRAP);
4220
4221	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
4222	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
4223	ATF_REQUIRE_EQ(state.pe_other_pid, child);
4224
4225	DPRINTF("Before resuming the forkee process where it left off and "
4226	    "without signal to be sent\n");
4227	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
4228
4229	DPRINTF("Before resuming the child process where it left off and "
4230	    "without signal to be sent\n");
4231	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4232
4233	DPRINTF("Before calling %s() for the forkee - expected exited\n",
4234	    TWAIT_FNAME);
4235	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4236	    child2);
4237
4238	validate_status_exited(status, exitval2);
4239
4240	DPRINTF("Before calling %s() for the forkee - expected no process\n",
4241	    TWAIT_FNAME);
4242	TWAIT_REQUIRE_FAILURE(ECHILD,
4243	    wpid = TWAIT_GENERIC(child2, &status, 0));
4244
4245	DPRINTF("Before calling %s() for the child - expected stopped "
4246	    "SIGCHLD\n", TWAIT_FNAME);
4247	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4248
4249	validate_status_stopped(status, SIGCHLD);
4250
4251	DPRINTF("Before resuming the child process where it left off and "
4252	    "without signal to be sent\n");
4253	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4254
4255	DPRINTF("Before calling %s() for the child - expected exited\n",
4256	    TWAIT_FNAME);
4257	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4258
4259	validate_status_exited(status, exitval);
4260
4261	DPRINTF("Before calling %s() for the child - expected no process\n",
4262	    TWAIT_FNAME);
4263	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4264}
4265#endif
4266
4267#if defined(TWAIT_HAVE_PID)
4268ATF_TC(signal7);
4269ATF_TC_HEAD(signal7, tc)
4270{
4271	atf_tc_set_md_var(tc, "descr",
4272	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4273	    "catching PTRACE_VFORK breakpoint");
4274}
4275
4276ATF_TC_BODY(signal7, tc)
4277{
4278	const int exitval = 5;
4279	const int exitval2 = 15;
4280	const int sigval = SIGSTOP;
4281	const int sigmasked = SIGTRAP;
4282	pid_t child, child2, wpid;
4283#if defined(TWAIT_HAVE_STATUS)
4284	int status;
4285#endif
4286	sigset_t intmask;
4287	ptrace_state_t state;
4288	const int slen = sizeof(state);
4289	ptrace_event_t event;
4290	const int elen = sizeof(event);
4291
4292	atf_tc_expect_fail("PR kern/51918");
4293
4294	DPRINTF("Before forking process PID=%d\n", getpid());
4295	SYSCALL_REQUIRE((child = fork()) != -1);
4296	if (child == 0) {
4297		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4298		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4299
4300		sigemptyset(&intmask);
4301		sigaddset(&intmask, sigmasked);
4302		sigprocmask(SIG_BLOCK, &intmask, NULL);
4303
4304		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4305		FORKEE_ASSERT(raise(sigval) == 0);
4306
4307		FORKEE_ASSERT((child2 = fork()) != -1);
4308
4309		if (child2 == 0)
4310			_exit(exitval2);
4311
4312		FORKEE_REQUIRE_SUCCESS
4313			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4314
4315		forkee_status_exited(status, exitval2);
4316
4317		DPRINTF("Before exiting of the child process\n");
4318		_exit(exitval);
4319	}
4320	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4321
4322	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4323	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4324
4325	validate_status_stopped(status, sigval);
4326
4327	DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
4328	event.pe_set_event = PTRACE_VFORK;
4329	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP);
4330
4331	DPRINTF("Before resuming the child process where it left off and "
4332	    "without signal to be sent\n");
4333	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4334
4335	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4336	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4337
4338	validate_status_stopped(status, sigmasked);
4339
4340	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4341	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
4342
4343	child2 = state.pe_other_pid;
4344	DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2);
4345
4346	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
4347	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4348	    child2);
4349
4350	validate_status_stopped(status, SIGTRAP);
4351
4352	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
4353	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
4354	ATF_REQUIRE_EQ(state.pe_other_pid, child);
4355
4356	DPRINTF("Before resuming the forkee process where it left off and "
4357	    "without signal to be sent\n");
4358	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
4359
4360	DPRINTF("Before resuming the child process where it left off and "
4361	    "without signal to be sent\n");
4362	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4363
4364	DPRINTF("Before calling %s() for the forkee - expected exited\n",
4365	    TWAIT_FNAME);
4366	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4367	    child2);
4368
4369	validate_status_exited(status, exitval2);
4370
4371	DPRINTF("Before calling %s() for the forkee - expected no process\n",
4372	    TWAIT_FNAME);
4373	TWAIT_REQUIRE_FAILURE(ECHILD,
4374	    wpid = TWAIT_GENERIC(child2, &status, 0));
4375
4376	DPRINTF("Before calling %s() for the child - expected stopped "
4377	    "SIGCHLD\n", TWAIT_FNAME);
4378	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4379
4380	validate_status_stopped(status, SIGCHLD);
4381
4382	DPRINTF("Before resuming the child process where it left off and "
4383	    "without signal to be sent\n");
4384	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4385
4386	DPRINTF("Before calling %s() for the child - expected exited\n",
4387	    TWAIT_FNAME);
4388	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4389
4390	validate_status_exited(status, exitval);
4391
4392	DPRINTF("Before calling %s() for the child - expected no process\n",
4393	    TWAIT_FNAME);
4394	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4395}
4396#endif
4397
4398ATF_TC(signal8);
4399ATF_TC_HEAD(signal8, tc)
4400{
4401	atf_tc_set_md_var(tc, "descr",
4402	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4403	    "catching PTRACE_VFORK_DONE breakpoint");
4404}
4405
4406ATF_TC_BODY(signal8, tc)
4407{
4408	const int exitval = 5;
4409	const int exitval2 = 15;
4410	const int sigval = SIGSTOP;
4411	const int sigmasked = SIGTRAP;
4412	pid_t child, child2, wpid;
4413#if defined(TWAIT_HAVE_STATUS)
4414	int status;
4415#endif
4416	sigset_t intmask;
4417	ptrace_state_t state;
4418	const int slen = sizeof(state);
4419	ptrace_event_t event;
4420	const int elen = sizeof(event);
4421
4422	atf_tc_expect_fail("PR kern/51918");
4423
4424	DPRINTF("Before forking process PID=%d\n", getpid());
4425	SYSCALL_REQUIRE((child = fork()) != -1);
4426	if (child == 0) {
4427		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4428		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4429
4430		sigemptyset(&intmask);
4431		sigaddset(&intmask, sigmasked);
4432		sigprocmask(SIG_BLOCK, &intmask, NULL);
4433
4434		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4435		FORKEE_ASSERT(raise(sigval) == 0);
4436
4437		FORKEE_ASSERT((child2 = vfork()) != -1);
4438
4439		if (child2 == 0)
4440			_exit(exitval2);
4441
4442		FORKEE_REQUIRE_SUCCESS
4443			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4444
4445		forkee_status_exited(status, exitval2);
4446
4447		DPRINTF("Before exiting of the child process\n");
4448		_exit(exitval);
4449	}
4450	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4451
4452	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4453	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4454
4455	validate_status_stopped(status, sigval);
4456
4457	DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
4458	    child);
4459	event.pe_set_event = PTRACE_VFORK_DONE;
4460	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4461
4462	DPRINTF("Before resuming the child process where it left off and "
4463	    "without signal to be sent\n");
4464	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4465
4466	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4467	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4468
4469	validate_status_stopped(status, sigmasked);
4470
4471	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4472	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
4473
4474	child2 = state.pe_other_pid;
4475	DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
4476
4477	DPRINTF("Before resuming the child process where it left off and "
4478	    "without signal to be sent\n");
4479	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4480
4481	DPRINTF("Before calling %s() for the child - expected stopped "
4482	    "SIGCHLD\n", TWAIT_FNAME);
4483	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4484
4485	validate_status_stopped(status, SIGCHLD);
4486
4487	DPRINTF("Before resuming the child process where it left off and "
4488	    "without signal to be sent\n");
4489	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4490
4491	DPRINTF("Before calling %s() for the child - expected exited\n",
4492	    TWAIT_FNAME);
4493	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4494
4495	validate_status_exited(status, exitval);
4496
4497	DPRINTF("Before calling %s() for the child - expected no process\n",
4498	    TWAIT_FNAME);
4499	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4500}
4501
4502ATF_TC(signal9);
4503ATF_TC_HEAD(signal9, tc)
4504{
4505	atf_tc_set_md_var(tc, "descr",
4506	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4507	    "catching PTRACE_LWP_CREATE breakpoint");
4508}
4509
4510ATF_TC_BODY(signal9, tc)
4511{
4512	const int exitval = 5;
4513	const int sigval = SIGSTOP;
4514	const int sigmasked = SIGTRAP;
4515	pid_t child, wpid;
4516#if defined(TWAIT_HAVE_STATUS)
4517	int status;
4518#endif
4519	sigset_t intmask;
4520	ptrace_state_t state;
4521	const int slen = sizeof(state);
4522	ptrace_event_t event;
4523	const int elen = sizeof(event);
4524	ucontext_t uc;
4525	lwpid_t lid;
4526	static const size_t ssize = 16*1024;
4527	void *stack;
4528
4529	atf_tc_expect_fail("PR kern/51918");
4530
4531	DPRINTF("Before forking process PID=%d\n", getpid());
4532	SYSCALL_REQUIRE((child = fork()) != -1);
4533	if (child == 0) {
4534		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4535		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4536
4537		sigemptyset(&intmask);
4538		sigaddset(&intmask, sigmasked);
4539		sigprocmask(SIG_BLOCK, &intmask, NULL);
4540
4541		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4542		FORKEE_ASSERT(raise(sigval) == 0);
4543
4544		DPRINTF("Before allocating memory for stack in child\n");
4545		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4546
4547		DPRINTF("Before making context for new lwp in child\n");
4548		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
4549
4550		DPRINTF("Before creating new in child\n");
4551		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4552
4553		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4554		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4555
4556		DPRINTF("Before verifying that reported %d and running lid %d "
4557		    "are the same\n", lid, the_lwp_id);
4558		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4559
4560		DPRINTF("Before exiting of the child process\n");
4561		_exit(exitval);
4562	}
4563	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4564
4565	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4566	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4567
4568	validate_status_stopped(status, sigval);
4569
4570	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
4571	event.pe_set_event = PTRACE_LWP_CREATE;
4572	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4573
4574	DPRINTF("Before resuming the child process where it left off and "
4575	    "without signal to be sent\n");
4576	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4577
4578	DPRINTF("Before calling %s() for the child - expected stopped "
4579	    "SIGTRAP\n", TWAIT_FNAME);
4580	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4581
4582	validate_status_stopped(status, sigmasked);
4583
4584	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4585
4586	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
4587
4588	lid = state.pe_lwp;
4589	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
4590
4591	DPRINTF("Before resuming the child process where it left off and "
4592	    "without signal to be sent\n");
4593	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4594
4595	DPRINTF("Before calling %s() for the child - expected exited\n",
4596	    TWAIT_FNAME);
4597	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4598
4599	validate_status_exited(status, exitval);
4600
4601	DPRINTF("Before calling %s() for the child - expected no process\n",
4602	    TWAIT_FNAME);
4603	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4604}
4605
4606ATF_TC(signal10);
4607ATF_TC_HEAD(signal10, tc)
4608{
4609	atf_tc_set_md_var(tc, "descr",
4610	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4611	    "catching PTRACE_LWP_EXIT breakpoint");
4612}
4613
4614ATF_TC_BODY(signal10, tc)
4615{
4616	const int exitval = 5;
4617	const int sigval = SIGSTOP;
4618	const int sigmasked = SIGTRAP;
4619	pid_t child, wpid;
4620#if defined(TWAIT_HAVE_STATUS)
4621	int status;
4622#endif
4623	sigset_t intmask;
4624	ptrace_state_t state;
4625	const int slen = sizeof(state);
4626	ptrace_event_t event;
4627	const int elen = sizeof(event);
4628	ucontext_t uc;
4629	lwpid_t lid;
4630	static const size_t ssize = 16*1024;
4631	void *stack;
4632
4633	atf_tc_expect_fail("PR kern/51918");
4634
4635	DPRINTF("Before forking process PID=%d\n", getpid());
4636	SYSCALL_REQUIRE((child = fork()) != -1);
4637	if (child == 0) {
4638		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4639		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4640
4641		sigemptyset(&intmask);
4642		sigaddset(&intmask, sigmasked);
4643		sigprocmask(SIG_BLOCK, &intmask, NULL);
4644
4645		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4646		FORKEE_ASSERT(raise(sigval) == 0);
4647
4648		DPRINTF("Before allocating memory for stack in child\n");
4649		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4650
4651		DPRINTF("Before making context for new lwp in child\n");
4652		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
4653
4654		DPRINTF("Before creating new in child\n");
4655		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4656
4657		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4658		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4659
4660		DPRINTF("Before verifying that reported %d and running lid %d "
4661		    "are the same\n", lid, the_lwp_id);
4662		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4663
4664		DPRINTF("Before exiting of the child process\n");
4665		_exit(exitval);
4666	}
4667	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4668
4669	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4670	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4671
4672	validate_status_stopped(status, sigval);
4673
4674	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
4675	event.pe_set_event = PTRACE_LWP_EXIT;
4676	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4677
4678	DPRINTF("Before resuming the child process where it left off and "
4679	    "without signal to be sent\n");
4680	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4681
4682	DPRINTF("Before calling %s() for the child - expected stopped "
4683	    "SIGTRAP\n", TWAIT_FNAME);
4684	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4685
4686	validate_status_stopped(status, sigmasked);
4687
4688	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4689
4690	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
4691
4692	lid = state.pe_lwp;
4693	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
4694
4695	DPRINTF("Before resuming the child process where it left off and "
4696	    "without signal to be sent\n");
4697	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4698
4699	DPRINTF("Before calling %s() for the child - expected exited\n",
4700	    TWAIT_FNAME);
4701	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4702
4703	validate_status_exited(status, exitval);
4704
4705	DPRINTF("Before calling %s() for the child - expected no process\n",
4706	    TWAIT_FNAME);
4707	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4708}
4709
4710static void
4711lwp_main_stop(void *arg)
4712{
4713	the_lwp_id = _lwp_self();
4714
4715	raise(SIGTRAP);
4716
4717	_lwp_exit();
4718}
4719
4720ATF_TC(suspend1);
4721ATF_TC_HEAD(suspend1, tc)
4722{
4723	atf_tc_set_md_var(tc, "descr",
4724	    "Verify that a thread can be suspended by a debugger and later "
4725	    "resumed by a tracee");
4726}
4727
4728ATF_TC_BODY(suspend1, tc)
4729{
4730	const int exitval = 5;
4731	const int sigval = SIGSTOP;
4732	pid_t child, wpid;
4733#if defined(TWAIT_HAVE_STATUS)
4734	int status;
4735#endif
4736	ucontext_t uc;
4737	lwpid_t lid;
4738	static const size_t ssize = 16*1024;
4739	void *stack;
4740	struct ptrace_lwpinfo pl;
4741	struct ptrace_siginfo psi;
4742	volatile int go = 0;
4743
4744	// Feature pending for refactoring
4745	atf_tc_expect_fail("PR kern/51995");
4746
4747	// Hangs with qemu
4748	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4749
4750	DPRINTF("Before forking process PID=%d\n", getpid());
4751	SYSCALL_REQUIRE((child = fork()) != -1);
4752	if (child == 0) {
4753		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4754		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4755
4756		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4757		FORKEE_ASSERT(raise(sigval) == 0);
4758
4759		DPRINTF("Before allocating memory for stack in child\n");
4760		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4761
4762		DPRINTF("Before making context for new lwp in child\n");
4763		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
4764
4765		DPRINTF("Before creating new in child\n");
4766		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4767
4768		while (go == 0)
4769			continue;
4770
4771		raise(SIGINT);
4772
4773		FORKEE_ASSERT(_lwp_continue(lid) == 0);
4774
4775		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4776		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4777
4778		DPRINTF("Before verifying that reported %d and running lid %d "
4779		    "are the same\n", lid, the_lwp_id);
4780		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4781
4782		DPRINTF("Before exiting of the child process\n");
4783		_exit(exitval);
4784	}
4785	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4786
4787	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4788	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4789
4790	validate_status_stopped(status, sigval);
4791
4792	DPRINTF("Before resuming the child process where it left off and "
4793	    "without signal to be sent\n");
4794	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4795
4796	DPRINTF("Before calling %s() for the child - expected stopped "
4797	    "SIGTRAP\n", TWAIT_FNAME);
4798	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4799
4800	validate_status_stopped(status, SIGTRAP);
4801
4802	DPRINTF("Before reading siginfo and lwpid_t\n");
4803	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
4804
4805	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
4806	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
4807
4808        DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
4809	    child, getpid());
4810	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
4811
4812	DPRINTF("Before resuming the child process where it left off and "
4813	    "without signal to be sent\n");
4814	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4815
4816	DPRINTF("Before calling %s() for the child - expected stopped "
4817	    "SIGINT\n", TWAIT_FNAME);
4818	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4819
4820	validate_status_stopped(status, SIGINT);
4821
4822	pl.pl_lwpid = 0;
4823
4824	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
4825	while (pl.pl_lwpid != 0) {
4826
4827		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
4828		switch (pl.pl_lwpid) {
4829		case 1:
4830			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
4831			break;
4832		case 2:
4833			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
4834			break;
4835		}
4836	}
4837
4838	DPRINTF("Before resuming the child process where it left off and "
4839	    "without signal to be sent\n");
4840	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4841
4842	DPRINTF("Before calling %s() for the child - expected exited\n",
4843	    TWAIT_FNAME);
4844	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4845
4846	validate_status_exited(status, exitval);
4847
4848	DPRINTF("Before calling %s() for the child - expected no process\n",
4849	    TWAIT_FNAME);
4850	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4851}
4852
4853ATF_TC(suspend2);
4854ATF_TC_HEAD(suspend2, tc)
4855{
4856	atf_tc_set_md_var(tc, "descr",
4857	    "Verify that the while the only thread within a process is "
4858	    "suspended, the whole process cannot be unstopped");
4859}
4860
4861ATF_TC_BODY(suspend2, tc)
4862{
4863	const int exitval = 5;
4864	const int sigval = SIGSTOP;
4865	pid_t child, wpid;
4866#if defined(TWAIT_HAVE_STATUS)
4867	int status;
4868#endif
4869	struct ptrace_siginfo psi;
4870
4871	// Feature pending for refactoring
4872	atf_tc_expect_fail("PR kern/51995");
4873
4874	// Hangs with qemu
4875	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4876
4877	DPRINTF("Before forking process PID=%d\n", getpid());
4878	SYSCALL_REQUIRE((child = fork()) != -1);
4879	if (child == 0) {
4880		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4881		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4882
4883		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4884		FORKEE_ASSERT(raise(sigval) == 0);
4885
4886		DPRINTF("Before exiting of the child process\n");
4887		_exit(exitval);
4888	}
4889	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4890
4891	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4892	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4893
4894	validate_status_stopped(status, sigval);
4895
4896	DPRINTF("Before reading siginfo and lwpid_t\n");
4897	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
4898
4899	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
4900	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
4901
4902	DPRINTF("Before resuming the child process where it left off and "
4903	    "without signal to be sent\n");
4904	ATF_REQUIRE_ERRNO(EDEADLK,
4905	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
4906
4907	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
4908	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
4909
4910	DPRINTF("Before resuming the child process where it left off and "
4911	    "without signal to be sent\n");
4912	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4913
4914	DPRINTF("Before calling %s() for the child - expected exited\n",
4915	    TWAIT_FNAME);
4916	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4917
4918	validate_status_exited(status, exitval);
4919
4920	DPRINTF("Before calling %s() for the child - expected no process\n",
4921	    TWAIT_FNAME);
4922	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4923}
4924
4925ATF_TC(resume1);
4926ATF_TC_HEAD(resume1, tc)
4927{
4928	atf_tc_set_md_var(tc, "timeout", "5");
4929	atf_tc_set_md_var(tc, "descr",
4930	    "Verify that a thread can be suspended by a debugger and later "
4931	    "resumed by the debugger");
4932}
4933
4934ATF_TC_BODY(resume1, tc)
4935{
4936	struct msg_fds fds;
4937	const int exitval = 5;
4938	const int sigval = SIGSTOP;
4939	pid_t child, wpid;
4940	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
4941#if defined(TWAIT_HAVE_STATUS)
4942	int status;
4943#endif
4944	ucontext_t uc;
4945	lwpid_t lid;
4946	static const size_t ssize = 16*1024;
4947	void *stack;
4948	struct ptrace_lwpinfo pl;
4949	struct ptrace_siginfo psi;
4950
4951	// Feature pending for refactoring
4952	atf_tc_expect_fail("PR kern/51995");
4953
4954	// Hangs with qemu
4955	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4956
4957	SYSCALL_REQUIRE(msg_open(&fds) == 0);
4958
4959	DPRINTF("Before forking process PID=%d\n", getpid());
4960	SYSCALL_REQUIRE((child = fork()) != -1);
4961	if (child == 0) {
4962		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4963		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4964
4965		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4966		FORKEE_ASSERT(raise(sigval) == 0);
4967
4968		DPRINTF("Before allocating memory for stack in child\n");
4969		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4970
4971		DPRINTF("Before making context for new lwp in child\n");
4972		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
4973
4974		DPRINTF("Before creating new in child\n");
4975		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4976
4977		CHILD_TO_PARENT("Message", fds, msg);
4978
4979		raise(SIGINT);
4980
4981		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4982		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4983
4984		DPRINTF("Before verifying that reported %d and running lid %d "
4985		    "are the same\n", lid, the_lwp_id);
4986		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4987
4988		DPRINTF("Before exiting of the child process\n");
4989		_exit(exitval);
4990	}
4991	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4992
4993	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4994	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4995
4996	validate_status_stopped(status, sigval);
4997
4998	DPRINTF("Before resuming the child process where it left off and "
4999	    "without signal to be sent\n");
5000	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5001
5002	DPRINTF("Before calling %s() for the child - expected stopped "
5003	    "SIGTRAP\n", TWAIT_FNAME);
5004	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5005
5006	validate_status_stopped(status, SIGTRAP);
5007
5008	DPRINTF("Before reading siginfo and lwpid_t\n");
5009	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
5010
5011	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
5012	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
5013
5014	PARENT_FROM_CHILD("Message", fds, msg);
5015
5016	DPRINTF("Before resuming the child process where it left off and "
5017	    "without signal to be sent\n");
5018	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5019
5020	DPRINTF("Before calling %s() for the child - expected stopped "
5021	    "SIGINT\n", TWAIT_FNAME);
5022	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5023
5024	validate_status_stopped(status, SIGINT);
5025
5026	pl.pl_lwpid = 0;
5027
5028	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
5029	while (pl.pl_lwpid != 0) {
5030		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
5031		switch (pl.pl_lwpid) {
5032		case 1:
5033			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
5034			break;
5035		case 2:
5036			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
5037			break;
5038		}
5039	}
5040
5041	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
5042	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
5043
5044	DPRINTF("Before resuming the child process where it left off and "
5045	    "without signal to be sent\n");
5046	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5047
5048	DPRINTF("Before calling %s() for the child - expected exited\n",
5049	    TWAIT_FNAME);
5050	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5051
5052	validate_status_exited(status, exitval);
5053
5054	DPRINTF("Before calling %s() for the child - expected no process\n",
5055	    TWAIT_FNAME);
5056	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5057
5058	msg_close(&fds);
5059
5060	DPRINTF("XXX: Test worked this time but for consistency timeout it\n");
5061	sleep(10);
5062}
5063
5064ATF_TC(syscall1);
5065ATF_TC_HEAD(syscall1, tc)
5066{
5067	atf_tc_set_md_var(tc, "descr",
5068	    "Verify that getpid(2) can be traced with PT_SYSCALL");
5069}
5070
5071ATF_TC_BODY(syscall1, tc)
5072{
5073	const int exitval = 5;
5074	const int sigval = SIGSTOP;
5075	pid_t child, wpid;
5076#if defined(TWAIT_HAVE_STATUS)
5077	int status;
5078#endif
5079	struct ptrace_siginfo info;
5080	memset(&info, 0, sizeof(info));
5081
5082	DPRINTF("Before forking process PID=%d\n", getpid());
5083	SYSCALL_REQUIRE((child = fork()) != -1);
5084	if (child == 0) {
5085		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5086		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5087
5088		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5089		FORKEE_ASSERT(raise(sigval) == 0);
5090
5091		syscall(SYS_getpid);
5092
5093		DPRINTF("Before exiting of the child process\n");
5094		_exit(exitval);
5095	}
5096	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5097
5098	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5099	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5100
5101	validate_status_stopped(status, sigval);
5102
5103	DPRINTF("Before resuming the child process where it left off and "
5104	    "without signal to be sent\n");
5105	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5106
5107	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5108	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5109
5110	validate_status_stopped(status, SIGTRAP);
5111
5112	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5113	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5114
5115	DPRINTF("Before checking siginfo_t and lwpid\n");
5116	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
5117	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5118	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
5119
5120	DPRINTF("Before resuming the child process where it left off and "
5121	    "without signal to be sent\n");
5122	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5123
5124	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5125	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5126
5127	validate_status_stopped(status, SIGTRAP);
5128
5129	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5130	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5131
5132	DPRINTF("Before checking siginfo_t and lwpid\n");
5133	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
5134	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5135	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
5136
5137	DPRINTF("Before resuming the child process where it left off and "
5138	    "without signal to be sent\n");
5139	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5140
5141	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5142	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5143
5144	validate_status_exited(status, exitval);
5145
5146	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5147	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5148}
5149
5150ATF_TC(syscallemu1);
5151ATF_TC_HEAD(syscallemu1, tc)
5152{
5153	atf_tc_set_md_var(tc, "descr",
5154	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
5155}
5156
5157ATF_TC_BODY(syscallemu1, tc)
5158{
5159	const int exitval = 5;
5160	const int sigval = SIGSTOP;
5161	pid_t child, wpid;
5162#if defined(TWAIT_HAVE_STATUS)
5163	int status;
5164#endif
5165
5166#if defined(__sparc__) && !defined(__sparc64__)
5167	/* syscallemu does not work on sparc (32-bit) */
5168	atf_tc_expect_fail("PR kern/52166");
5169#endif
5170
5171	DPRINTF("Before forking process PID=%d\n", getpid());
5172	SYSCALL_REQUIRE((child = fork()) != -1);
5173	if (child == 0) {
5174		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5175		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5176
5177		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5178		FORKEE_ASSERT(raise(sigval) == 0);
5179
5180		syscall(SYS_exit, 100);
5181
5182		DPRINTF("Before exiting of the child process\n");
5183		_exit(exitval);
5184	}
5185	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5186
5187	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5188	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5189
5190	validate_status_stopped(status, sigval);
5191
5192	DPRINTF("Before resuming the child process where it left off and "
5193	    "without signal to be sent\n");
5194	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5195
5196	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5197	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5198
5199	validate_status_stopped(status, SIGTRAP);
5200
5201	DPRINTF("Set SYSCALLEMU for intercepted syscall\n");
5202	SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
5203
5204	DPRINTF("Before resuming the child process where it left off and "
5205	    "without signal to be sent\n");
5206	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5207
5208	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5209	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5210
5211	validate_status_stopped(status, SIGTRAP);
5212
5213	DPRINTF("Before resuming the child process where it left off and "
5214	    "without signal to be sent\n");
5215	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5216
5217	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5218	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5219
5220	validate_status_exited(status, exitval);
5221
5222	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5223	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5224}
5225
5226#include "t_ptrace_amd64_wait.h"
5227#include "t_ptrace_i386_wait.h"
5228#include "t_ptrace_x86_wait.h"
5229
5230ATF_TP_ADD_TCS(tp)
5231{
5232	setvbuf(stdout, NULL, _IONBF, 0);
5233	setvbuf(stderr, NULL, _IONBF, 0);
5234
5235	ATF_TP_ADD_TC(tp, traceme_raise1);
5236	ATF_TP_ADD_TC(tp, traceme_raise2);
5237	ATF_TP_ADD_TC(tp, traceme_raise3);
5238	ATF_TP_ADD_TC(tp, traceme_raise4);
5239	ATF_TP_ADD_TC(tp, traceme_raise5);
5240
5241	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle1);
5242	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle2);
5243	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle3);
5244
5245	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked1);
5246	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked2);
5247	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked3);
5248
5249	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored1);
5250	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored2);
5251	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored3);
5252
5253	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple1);
5254	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple2);
5255	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple3);
5256	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple4);
5257	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple5);
5258
5259	ATF_TP_ADD_TC(tp, traceme_pid1_parent);
5260
5261	ATF_TP_ADD_TC(tp, traceme_vfork_raise1);
5262	ATF_TP_ADD_TC(tp, traceme_vfork_raise2);
5263	ATF_TP_ADD_TC(tp, traceme_vfork_raise3);
5264	ATF_TP_ADD_TC(tp, traceme_vfork_raise4);
5265	ATF_TP_ADD_TC(tp, traceme_vfork_raise5);
5266	ATF_TP_ADD_TC(tp, traceme_vfork_raise6);
5267	ATF_TP_ADD_TC(tp, traceme_vfork_raise7);
5268	ATF_TP_ADD_TC(tp, traceme_vfork_raise8);
5269
5270	ATF_TP_ADD_TC(tp, traceme_vfork_crash_trap);
5271	ATF_TP_ADD_TC(tp, traceme_vfork_crash_segv);
5272//	ATF_TP_ADD_TC(tp, traceme_vfork_crash_ill);
5273	ATF_TP_ADD_TC(tp, traceme_vfork_crash_fpe);
5274	ATF_TP_ADD_TC(tp, traceme_vfork_crash_bus);
5275
5276	ATF_TP_ADD_TC(tp, traceme_vfork_exec);
5277
5278	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sees_terminaton_before_the_parent);
5279	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sysctl_lookup_without_duplicates);
5280	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_terminaton_before_the_parent);
5281
5282	ATF_TP_ADD_TC(tp, parent_attach_to_its_child);
5283
5284	ATF_TP_ADD_TC(tp, child_attach_to_its_parent);
5285
5286	ATF_TP_ADD_TC_HAVE_PID(tp,
5287		tracee_sees_its_original_parent_getppid);
5288	ATF_TP_ADD_TC_HAVE_PID(tp,
5289		tracee_sees_its_original_parent_sysctl_kinfo_proc2);
5290	ATF_TP_ADD_TC_HAVE_PID(tp,
5291		tracee_sees_its_original_parent_procfs_status);
5292
5293	ATF_TP_ADD_TC(tp, eventmask_preserved_empty);
5294	ATF_TP_ADD_TC(tp, eventmask_preserved_fork);
5295	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork);
5296	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork_done);
5297	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_create);
5298	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_exit);
5299
5300	ATF_TP_ADD_TC(tp, fork1);
5301	ATF_TP_ADD_TC_HAVE_PID(tp, fork2);
5302	ATF_TP_ADD_TC_HAVE_PID(tp, fork3);
5303	ATF_TP_ADD_TC_HAVE_PID(tp, fork4);
5304	ATF_TP_ADD_TC(tp, fork5);
5305	ATF_TP_ADD_TC_HAVE_PID(tp, fork6);
5306	ATF_TP_ADD_TC_HAVE_PID(tp, fork7);
5307	ATF_TP_ADD_TC_HAVE_PID(tp, fork8);
5308
5309	ATF_TP_ADD_TC(tp, vfork1);
5310	ATF_TP_ADD_TC_HAVE_PID(tp, vfork2);
5311	ATF_TP_ADD_TC_HAVE_PID(tp, vfork3);
5312	ATF_TP_ADD_TC_HAVE_PID(tp, vfork4);
5313	ATF_TP_ADD_TC(tp, vfork5);
5314	ATF_TP_ADD_TC_HAVE_PID(tp, vfork6);
5315	ATF_TP_ADD_TC_HAVE_PID(tp, vfork7);
5316	ATF_TP_ADD_TC_HAVE_PID(tp, vfork8);
5317
5318	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8);
5319	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16);
5320	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32);
5321	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64);
5322
5323	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8);
5324	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16);
5325	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32);
5326	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64);
5327
5328	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8);
5329	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16);
5330	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32);
5331	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64);
5332
5333	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8);
5334	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16);
5335	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32);
5336	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64);
5337
5338	ATF_TP_ADD_TC(tp, bytes_transfer_read_d);
5339	ATF_TP_ADD_TC(tp, bytes_transfer_read_i);
5340	ATF_TP_ADD_TC(tp, bytes_transfer_write_d);
5341	ATF_TP_ADD_TC(tp, bytes_transfer_write_i);
5342
5343	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8_text);
5344	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16_text);
5345	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32_text);
5346	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64_text);
5347
5348	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8_text);
5349	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16_text);
5350	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32_text);
5351	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64_text);
5352
5353	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8_text);
5354	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16_text);
5355	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32_text);
5356	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64_text);
5357
5358	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8_text);
5359	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16_text);
5360	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32_text);
5361	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64_text);
5362
5363	ATF_TP_ADD_TC(tp, bytes_transfer_read_d_text);
5364	ATF_TP_ADD_TC(tp, bytes_transfer_read_i_text);
5365	ATF_TP_ADD_TC(tp, bytes_transfer_write_d_text);
5366	ATF_TP_ADD_TC(tp, bytes_transfer_write_i_text);
5367
5368	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_auxv);
5369
5370	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
5371	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
5372	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
5373	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
5374	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
5375
5376	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
5377	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
5378
5379	ATF_TP_ADD_TC_PT_STEP(tp, step1);
5380	ATF_TP_ADD_TC_PT_STEP(tp, step2);
5381	ATF_TP_ADD_TC_PT_STEP(tp, step3);
5382	ATF_TP_ADD_TC_PT_STEP(tp, step4);
5383
5384	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
5385	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
5386	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
5387	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
5388
5389	ATF_TP_ADD_TC(tp, kill1);
5390	ATF_TP_ADD_TC(tp, kill2);
5391
5392	ATF_TP_ADD_TC(tp, lwpinfo1);
5393	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
5394
5395	ATF_TP_ADD_TC(tp, siginfo1);
5396	ATF_TP_ADD_TC(tp, siginfo2);
5397	ATF_TP_ADD_TC(tp, siginfo3);
5398	ATF_TP_ADD_TC(tp, siginfo4);
5399	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
5400	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
5401
5402	ATF_TP_ADD_TC(tp, lwp_create1);
5403
5404	ATF_TP_ADD_TC(tp, lwp_exit1);
5405
5406	ATF_TP_ADD_TC(tp, signal1);
5407	ATF_TP_ADD_TC(tp, signal2);
5408	ATF_TP_ADD_TC(tp, signal3);
5409	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
5410	ATF_TP_ADD_TC(tp, signal5);
5411	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
5412	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
5413	ATF_TP_ADD_TC(tp, signal8);
5414	ATF_TP_ADD_TC(tp, signal9);
5415	ATF_TP_ADD_TC(tp, signal10);
5416
5417	ATF_TP_ADD_TC(tp, suspend1);
5418	ATF_TP_ADD_TC(tp, suspend2);
5419
5420	ATF_TP_ADD_TC(tp, resume1);
5421
5422	ATF_TP_ADD_TC(tp, syscall1);
5423
5424	ATF_TP_ADD_TC(tp, syscallemu1);
5425
5426	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
5427	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
5428	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
5429
5430	return atf_no_error();
5431}
5432