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