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