t_ptrace_wait.c revision 1.75
1/*	$NetBSD: t_ptrace_wait.c,v 1.75 2019/02/08 03:08:00 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.75 2019/02/08 03:08:00 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
2854static void
2855ptrace_kill(const char *type)
2856{
2857	const int sigval = SIGSTOP;
2858	pid_t child, wpid;
2859#if defined(TWAIT_HAVE_STATUS)
2860	int status;
2861#endif
2862
2863	DPRINTF("Before forking process PID=%d\n", getpid());
2864	SYSCALL_REQUIRE((child = fork()) != -1);
2865	if (child == 0) {
2866		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2867		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2868
2869		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2870		FORKEE_ASSERT(raise(sigval) == 0);
2871
2872		/* NOTREACHED */
2873		FORKEE_ASSERTX(0 &&
2874		    "Child should be terminated by a signal from its parent");
2875	}
2876	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2877
2878	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2879	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2880
2881	validate_status_stopped(status, sigval);
2882
2883	DPRINTF("Before killing the child process with %s\n", type);
2884	if (strcmp(type, "ptrace(PT_KILL)") == 0) {
2885		SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
2886	} else if (strcmp(type, "kill(SIGKILL)") == 0) {
2887		kill(child, SIGKILL);
2888	} else if (strcmp(type, "killpg(SIGKILL)") == 0) {
2889		setpgid(child, 0);
2890		killpg(getpgid(child), SIGKILL);
2891	}
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, SIGKILL, 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
2902#define PTRACE_KILL(test, type)						\
2903ATF_TC(test);								\
2904ATF_TC_HEAD(test, tc)							\
2905{									\
2906        atf_tc_set_md_var(tc, "descr",					\
2907            "Verify killing the child with " type);			\
2908}									\
2909									\
2910ATF_TC_BODY(test, tc)							\
2911{									\
2912									\
2913        ptrace_kill(type);						\
2914}
2915
2916// PT_CONTINUE with SIGKILL is covered by traceme_sendsignal_simple1
2917PTRACE_KILL(kill1, "ptrace(PT_KILL)")
2918PTRACE_KILL(kill2, "kill(SIGKILL)")
2919PTRACE_KILL(kill3, "killpg(SIGKILL)")
2920
2921/// ----------------------------------------------------------------------------
2922
2923ATF_TC(lwpinfo1);
2924ATF_TC_HEAD(lwpinfo1, tc)
2925{
2926	atf_tc_set_md_var(tc, "descr",
2927	    "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
2928}
2929
2930ATF_TC_BODY(lwpinfo1, tc)
2931{
2932	const int exitval = 5;
2933	const int sigval = SIGSTOP;
2934	pid_t child, wpid;
2935#if defined(TWAIT_HAVE_STATUS)
2936	int status;
2937#endif
2938	struct ptrace_lwpinfo info = {0, 0};
2939
2940	DPRINTF("Before forking process PID=%d\n", getpid());
2941	SYSCALL_REQUIRE((child = fork()) != -1);
2942	if (child == 0) {
2943		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2944		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2945
2946		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2947		FORKEE_ASSERT(raise(sigval) == 0);
2948
2949		DPRINTF("Before exiting of the child process\n");
2950		_exit(exitval);
2951	}
2952	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2953
2954	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2955	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2956
2957	validate_status_stopped(status, sigval);
2958
2959	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2960	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
2961
2962	DPRINTF("Assert that there exists a thread\n");
2963	ATF_REQUIRE(info.pl_lwpid > 0);
2964
2965	DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
2966	    info.pl_lwpid);
2967	ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
2968	    "Received event %d != expected event %d",
2969	    info.pl_event, PL_EVENT_SIGNAL);
2970
2971	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2972	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
2973
2974	DPRINTF("Assert that there are no more lwp threads in child\n");
2975	ATF_REQUIRE_EQ(info.pl_lwpid, 0);
2976
2977	DPRINTF("Before resuming the child process where it left off and "
2978	    "without signal to be sent\n");
2979	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
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_exited(status, exitval);
2985
2986	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2987	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2988}
2989
2990#if defined(TWAIT_HAVE_PID)
2991ATF_TC(lwpinfo2);
2992ATF_TC_HEAD(lwpinfo2, tc)
2993{
2994	atf_tc_set_md_var(tc, "descr",
2995	    "Verify basic LWPINFO call for single thread (PT_ATTACH from "
2996	    "tracer)");
2997}
2998
2999ATF_TC_BODY(lwpinfo2, tc)
3000{
3001	struct msg_fds parent_tracee, parent_tracer;
3002	const int exitval_tracee = 5;
3003	const int exitval_tracer = 10;
3004	pid_t tracee, tracer, wpid;
3005	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
3006#if defined(TWAIT_HAVE_STATUS)
3007	int status;
3008#endif
3009	struct ptrace_lwpinfo info = {0, 0};
3010
3011	DPRINTF("Spawn tracee\n");
3012	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
3013	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
3014	tracee = atf_utils_fork();
3015	if (tracee == 0) {
3016
3017		/* Wait for message from the parent */
3018		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
3019		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
3020
3021		_exit(exitval_tracee);
3022	}
3023	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
3024
3025	DPRINTF("Spawn debugger\n");
3026	tracer = atf_utils_fork();
3027	if (tracer == 0) {
3028		/* No IPC to communicate with the child */
3029		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
3030		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
3031
3032		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
3033		FORKEE_REQUIRE_SUCCESS(
3034		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
3035
3036		forkee_status_stopped(status, SIGSTOP);
3037
3038		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
3039		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
3040		    != -1);
3041
3042		DPRINTF("Assert that there exists a thread\n");
3043		FORKEE_ASSERTX(info.pl_lwpid > 0);
3044
3045		DPRINTF("Assert that lwp thread %d received event "
3046		    "PL_EVENT_SIGNAL\n", info.pl_lwpid);
3047		FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
3048
3049		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
3050		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
3051		    != -1);
3052
3053		DPRINTF("Assert that there are no more lwp threads in child\n");
3054		FORKEE_ASSERTX(info.pl_lwpid == 0);
3055
3056		/* Resume tracee with PT_CONTINUE */
3057		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
3058
3059		/* Inform parent that tracer has attached to tracee */
3060		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
3061		/* Wait for parent */
3062		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
3063
3064		/* Wait for tracee and assert that it exited */
3065		FORKEE_REQUIRE_SUCCESS(
3066		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
3067
3068		forkee_status_exited(status, exitval_tracee);
3069
3070		DPRINTF("Before exiting of the tracer process\n");
3071		_exit(exitval_tracer);
3072	}
3073
3074	DPRINTF("Wait for the tracer to attach to the tracee\n");
3075	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
3076
3077	DPRINTF("Resume the tracee and let it exit\n");
3078	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
3079
3080	DPRINTF("Detect that tracee is zombie\n");
3081	await_zombie(tracee);
3082
3083	DPRINTF("Assert that there is no status about tracee - "
3084	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
3085	TWAIT_REQUIRE_SUCCESS(
3086	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
3087
3088	DPRINTF("Resume the tracer and let it detect exited tracee\n");
3089	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
3090
3091	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
3092	    TWAIT_FNAME);
3093	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
3094	    tracer);
3095
3096	validate_status_exited(status, exitval_tracer);
3097
3098	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
3099	    TWAIT_FNAME);
3100	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
3101	    tracee);
3102
3103	validate_status_exited(status, exitval_tracee);
3104
3105	msg_close(&parent_tracer);
3106	msg_close(&parent_tracee);
3107}
3108#endif
3109
3110ATF_TC(siginfo1);
3111ATF_TC_HEAD(siginfo1, tc)
3112{
3113	atf_tc_set_md_var(tc, "descr",
3114	    "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
3115}
3116
3117ATF_TC_BODY(siginfo1, tc)
3118{
3119	const int exitval = 5;
3120	const int sigval = SIGTRAP;
3121	pid_t child, wpid;
3122#if defined(TWAIT_HAVE_STATUS)
3123	int status;
3124#endif
3125	struct ptrace_siginfo info;
3126	memset(&info, 0, sizeof(info));
3127
3128	DPRINTF("Before forking process PID=%d\n", getpid());
3129	SYSCALL_REQUIRE((child = fork()) != -1);
3130	if (child == 0) {
3131		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3132		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3133
3134		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3135		FORKEE_ASSERT(raise(sigval) == 0);
3136
3137		DPRINTF("Before exiting of the child process\n");
3138		_exit(exitval);
3139	}
3140	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3141
3142	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3143	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3144
3145	validate_status_stopped(status, sigval);
3146
3147	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3148	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3149
3150	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3151	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3152	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3153	    info.psi_siginfo.si_errno);
3154
3155	DPRINTF("Before resuming the child process where it left off and "
3156	    "without signal to be sent\n");
3157	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3158
3159	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3160	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3161
3162	validate_status_exited(status, exitval);
3163
3164	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3165	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3166}
3167
3168ATF_TC(siginfo2);
3169ATF_TC_HEAD(siginfo2, tc)
3170{
3171	atf_tc_set_md_var(tc, "descr",
3172	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
3173	    "modification of SIGINT from tracee");
3174}
3175
3176static int siginfo2_caught = 0;
3177
3178static void
3179siginfo2_sighandler(int sig)
3180{
3181	FORKEE_ASSERT_EQ(sig, SIGINT);
3182
3183	++siginfo2_caught;
3184}
3185
3186ATF_TC_BODY(siginfo2, tc)
3187{
3188	const int exitval = 5;
3189	const int sigval = SIGINT;
3190	pid_t child, wpid;
3191	struct sigaction sa;
3192#if defined(TWAIT_HAVE_STATUS)
3193	int status;
3194#endif
3195	struct ptrace_siginfo info;
3196	memset(&info, 0, sizeof(info));
3197
3198	DPRINTF("Before forking process PID=%d\n", getpid());
3199	SYSCALL_REQUIRE((child = fork()) != -1);
3200	if (child == 0) {
3201		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3202		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3203
3204		sa.sa_handler = siginfo2_sighandler;
3205		sa.sa_flags = SA_SIGINFO;
3206		sigemptyset(&sa.sa_mask);
3207
3208		FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
3209
3210		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3211		FORKEE_ASSERT(raise(sigval) == 0);
3212
3213		FORKEE_ASSERT_EQ(siginfo2_caught, 1);
3214
3215		DPRINTF("Before exiting of the child process\n");
3216		_exit(exitval);
3217	}
3218	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3219
3220	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3221	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3222
3223	validate_status_stopped(status, sigval);
3224
3225	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3226	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3227
3228	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3229	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3230	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3231	    info.psi_siginfo.si_errno);
3232
3233	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
3234	SYSCALL_REQUIRE(
3235	    ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
3236
3237	DPRINTF("Before resuming the child process where it left off and "
3238	    "without signal to be sent\n");
3239	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
3240
3241	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3242	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3243
3244	validate_status_exited(status, exitval);
3245
3246	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3247	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3248}
3249
3250ATF_TC(siginfo3);
3251ATF_TC_HEAD(siginfo3, tc)
3252{
3253	atf_tc_set_md_var(tc, "descr",
3254	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
3255	    "setting signal to new value");
3256}
3257
3258static int siginfo3_caught = 0;
3259
3260static void
3261siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
3262{
3263	FORKEE_ASSERT_EQ(sig, SIGTRAP);
3264
3265	FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
3266	FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
3267
3268	++siginfo3_caught;
3269}
3270
3271ATF_TC_BODY(siginfo3, tc)
3272{
3273	const int exitval = 5;
3274	const int sigval = SIGINT;
3275	const int sigfaked = SIGTRAP;
3276	const int sicodefaked = TRAP_BRKPT;
3277	pid_t child, wpid;
3278	struct sigaction sa;
3279#if defined(TWAIT_HAVE_STATUS)
3280	int status;
3281#endif
3282	struct ptrace_siginfo info;
3283	memset(&info, 0, sizeof(info));
3284
3285	DPRINTF("Before forking process PID=%d\n", getpid());
3286	SYSCALL_REQUIRE((child = fork()) != -1);
3287	if (child == 0) {
3288		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3289		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3290
3291		sa.sa_sigaction = siginfo3_sigaction;
3292		sa.sa_flags = SA_SIGINFO;
3293		sigemptyset(&sa.sa_mask);
3294
3295		FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
3296
3297		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3298		FORKEE_ASSERT(raise(sigval) == 0);
3299
3300		FORKEE_ASSERT_EQ(siginfo3_caught, 1);
3301
3302		DPRINTF("Before exiting of the child process\n");
3303		_exit(exitval);
3304	}
3305	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3306
3307	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3308	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3309
3310	validate_status_stopped(status, sigval);
3311
3312	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3313	SYSCALL_REQUIRE(
3314	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3315
3316	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3317	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3318	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3319	    info.psi_siginfo.si_errno);
3320
3321	DPRINTF("Before setting new faked signal to signo=%d si_code=%d\n",
3322	    sigfaked, sicodefaked);
3323	info.psi_siginfo.si_signo = sigfaked;
3324	info.psi_siginfo.si_code = sicodefaked;
3325
3326	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
3327	SYSCALL_REQUIRE(
3328	    ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
3329
3330	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3331	SYSCALL_REQUIRE(
3332	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3333
3334	DPRINTF("Before checking siginfo_t\n");
3335	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
3336	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
3337
3338	DPRINTF("Before resuming the child process where it left off and "
3339	    "without signal to be sent\n");
3340	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
3341
3342	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3343	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3344
3345	validate_status_exited(status, exitval);
3346
3347	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3348	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3349}
3350
3351ATF_TC(siginfo4);
3352ATF_TC_HEAD(siginfo4, tc)
3353{
3354	atf_tc_set_md_var(tc, "descr",
3355	    "Detect SIGTRAP TRAP_EXEC from tracee");
3356}
3357
3358ATF_TC_BODY(siginfo4, tc)
3359{
3360	const int sigval = SIGTRAP;
3361	pid_t child, wpid;
3362#if defined(TWAIT_HAVE_STATUS)
3363	int status;
3364#endif
3365
3366	struct ptrace_siginfo info;
3367	memset(&info, 0, sizeof(info));
3368
3369	DPRINTF("Before forking process PID=%d\n", getpid());
3370	SYSCALL_REQUIRE((child = fork()) != -1);
3371	if (child == 0) {
3372		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3373		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3374
3375		DPRINTF("Before calling execve(2) from child\n");
3376		execlp("/bin/echo", "/bin/echo", NULL);
3377
3378		FORKEE_ASSERT(0 && "Not reached");
3379	}
3380	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3381
3382	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3383	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3384
3385	validate_status_stopped(status, sigval);
3386
3387	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3388	SYSCALL_REQUIRE(
3389	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3390
3391	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3392	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3393	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3394	    info.psi_siginfo.si_errno);
3395
3396	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3397	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
3398
3399	DPRINTF("Before resuming the child process where it left off and "
3400	    "without signal to be sent\n");
3401	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3402
3403	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3404	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3405
3406	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3407	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3408}
3409
3410#if defined(TWAIT_HAVE_PID)
3411ATF_TC(siginfo5);
3412ATF_TC_HEAD(siginfo5, tc)
3413{
3414	atf_tc_set_md_var(tc, "descr",
3415	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
3416	    "set to PTRACE_FORK and reports correct signal information");
3417}
3418
3419ATF_TC_BODY(siginfo5, tc)
3420{
3421	const int exitval = 5;
3422	const int exitval2 = 15;
3423	const int sigval = SIGSTOP;
3424	pid_t child, child2, wpid;
3425#if defined(TWAIT_HAVE_STATUS)
3426	int status;
3427#endif
3428	ptrace_state_t state;
3429	const int slen = sizeof(state);
3430	ptrace_event_t event;
3431	const int elen = sizeof(event);
3432	struct ptrace_siginfo info;
3433
3434	memset(&info, 0, sizeof(info));
3435
3436	DPRINTF("Before forking process PID=%d\n", getpid());
3437	SYSCALL_REQUIRE((child = fork()) != -1);
3438	if (child == 0) {
3439		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3440		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3441
3442		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3443		FORKEE_ASSERT(raise(sigval) == 0);
3444
3445		FORKEE_ASSERT((child2 = fork()) != -1);
3446
3447		if (child2 == 0)
3448			_exit(exitval2);
3449
3450		FORKEE_REQUIRE_SUCCESS
3451		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3452
3453		forkee_status_exited(status, exitval2);
3454
3455		DPRINTF("Before exiting of the child process\n");
3456		_exit(exitval);
3457	}
3458	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3459
3460	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3461	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3462
3463	validate_status_stopped(status, sigval);
3464
3465	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3466	SYSCALL_REQUIRE(
3467	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3468
3469	DPRINTF("Before checking siginfo_t\n");
3470	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3471	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3472
3473	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
3474	event.pe_set_event = PTRACE_FORK;
3475	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3476
3477	DPRINTF("Before resuming the child process where it left off and "
3478	    "without signal to be sent\n");
3479        DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
3480               "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
3481               "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
3482                "state.pe_other_pid=child)\n", child);
3483	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3484
3485	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
3486	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3487
3488	validate_status_stopped(status, SIGTRAP);
3489
3490	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3491	SYSCALL_REQUIRE(
3492	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3493
3494	DPRINTF("Before checking siginfo_t\n");
3495	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3496	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
3497
3498	SYSCALL_REQUIRE(
3499	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3500	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
3501
3502	child2 = state.pe_other_pid;
3503	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
3504
3505	DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
3506	    TWAIT_FNAME, child2, child);
3507	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3508	    child2);
3509
3510	validate_status_stopped(status, SIGTRAP);
3511
3512	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3513	SYSCALL_REQUIRE(
3514	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3515
3516	DPRINTF("Before checking siginfo_t\n");
3517	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3518	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
3519
3520	SYSCALL_REQUIRE(
3521	    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
3522	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
3523	ATF_REQUIRE_EQ(state.pe_other_pid, child);
3524
3525	DPRINTF("Before resuming the forkee process where it left off and "
3526	    "without signal to be sent\n");
3527	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
3528
3529	DPRINTF("Before resuming the child process where it left off and "
3530	    "without signal to be sent\n");
3531	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3532
3533	DPRINTF("Before calling %s() for the forkee - expected exited\n",
3534	    TWAIT_FNAME);
3535	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3536	    child2);
3537
3538	validate_status_exited(status, exitval2);
3539
3540	DPRINTF("Before calling %s() for the forkee - expected no process\n",
3541	    TWAIT_FNAME);
3542	TWAIT_REQUIRE_FAILURE(ECHILD,
3543	    wpid = TWAIT_GENERIC(child2, &status, 0));
3544
3545	DPRINTF("Before calling %s() for the child - expected stopped "
3546	    "SIGCHLD\n", TWAIT_FNAME);
3547	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3548
3549	validate_status_stopped(status, SIGCHLD);
3550
3551	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3552	SYSCALL_REQUIRE(
3553	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3554
3555	DPRINTF("Before checking siginfo_t\n");
3556	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
3557	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
3558
3559	DPRINTF("Before resuming the child process where it left off and "
3560	    "without signal to be sent\n");
3561	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3562
3563	DPRINTF("Before calling %s() for the child - expected exited\n",
3564	    TWAIT_FNAME);
3565	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3566
3567	validate_status_exited(status, exitval);
3568
3569	DPRINTF("Before calling %s() for the child - expected no process\n",
3570	    TWAIT_FNAME);
3571	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3572}
3573#endif
3574
3575#if defined(PT_STEP)
3576ATF_TC(siginfo6);
3577ATF_TC_HEAD(siginfo6, tc)
3578{
3579	atf_tc_set_md_var(tc, "descr",
3580	    "Verify single PT_STEP call with signal information check");
3581}
3582
3583ATF_TC_BODY(siginfo6, tc)
3584{
3585	const int exitval = 5;
3586	const int sigval = SIGSTOP;
3587	pid_t child, wpid;
3588#if defined(TWAIT_HAVE_STATUS)
3589	int status;
3590#endif
3591	int happy;
3592	struct ptrace_siginfo info;
3593
3594#if defined(__arm__)
3595	/* PT_STEP not supported on arm 32-bit */
3596	atf_tc_expect_fail("PR kern/52119");
3597#endif
3598
3599	memset(&info, 0, sizeof(info));
3600
3601	DPRINTF("Before forking process PID=%d\n", getpid());
3602	SYSCALL_REQUIRE((child = fork()) != -1);
3603	if (child == 0) {
3604		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3605		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3606
3607		happy = check_happy(100);
3608
3609		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3610		FORKEE_ASSERT(raise(sigval) == 0);
3611
3612		FORKEE_ASSERT_EQ(happy, check_happy(100));
3613
3614		DPRINTF("Before exiting of the child process\n");
3615		_exit(exitval);
3616	}
3617	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3618
3619	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3620	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3621
3622	validate_status_stopped(status, sigval);
3623
3624	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3625	SYSCALL_REQUIRE(
3626	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3627
3628	DPRINTF("Before checking siginfo_t\n");
3629	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3630	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3631
3632	DPRINTF("Before resuming the child process where it left off and "
3633	    "without signal to be sent (use PT_STEP)\n");
3634	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
3635
3636	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3637	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3638
3639	validate_status_stopped(status, SIGTRAP);
3640
3641	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3642	SYSCALL_REQUIRE(
3643	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3644
3645	DPRINTF("Before checking siginfo_t\n");
3646	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3647	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
3648
3649	DPRINTF("Before resuming the child process where it left off and "
3650	    "without signal to be sent\n");
3651	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3652
3653	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3654	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3655
3656	validate_status_exited(status, exitval);
3657
3658	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3659	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3660}
3661#endif
3662
3663volatile lwpid_t the_lwp_id = 0;
3664
3665static void
3666lwp_main_func(void *arg)
3667{
3668	the_lwp_id = _lwp_self();
3669	_lwp_exit();
3670}
3671
3672ATF_TC(lwp_create1);
3673ATF_TC_HEAD(lwp_create1, tc)
3674{
3675	atf_tc_set_md_var(tc, "descr",
3676	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
3677	    "EVENT_MASK set to PTRACE_LWP_CREATE");
3678}
3679
3680ATF_TC_BODY(lwp_create1, tc)
3681{
3682	const int exitval = 5;
3683	const int sigval = SIGSTOP;
3684	pid_t child, wpid;
3685#if defined(TWAIT_HAVE_STATUS)
3686	int status;
3687#endif
3688	ptrace_state_t state;
3689	const int slen = sizeof(state);
3690	ptrace_event_t event;
3691	const int elen = sizeof(event);
3692	ucontext_t uc;
3693	lwpid_t lid;
3694	static const size_t ssize = 16*1024;
3695	void *stack;
3696
3697	DPRINTF("Before forking process PID=%d\n", getpid());
3698	SYSCALL_REQUIRE((child = fork()) != -1);
3699	if (child == 0) {
3700		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3701		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3702
3703		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3704		FORKEE_ASSERT(raise(sigval) == 0);
3705
3706		DPRINTF("Before allocating memory for stack in child\n");
3707		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
3708
3709		DPRINTF("Before making context for new lwp in child\n");
3710		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
3711
3712		DPRINTF("Before creating new in child\n");
3713		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
3714
3715		DPRINTF("Before waiting for lwp %d to exit\n", lid);
3716		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
3717
3718		DPRINTF("Before verifying that reported %d and running lid %d "
3719		    "are the same\n", lid, the_lwp_id);
3720		FORKEE_ASSERT_EQ(lid, the_lwp_id);
3721
3722		DPRINTF("Before exiting of the child process\n");
3723		_exit(exitval);
3724	}
3725	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3726
3727	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3728	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3729
3730	validate_status_stopped(status, sigval);
3731
3732	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
3733	event.pe_set_event = PTRACE_LWP_CREATE;
3734	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3735
3736	DPRINTF("Before resuming the child process where it left off and "
3737	    "without signal to be sent\n");
3738	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3739
3740	DPRINTF("Before calling %s() for the child - expected stopped "
3741	    "SIGTRAP\n", TWAIT_FNAME);
3742	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3743
3744	validate_status_stopped(status, SIGTRAP);
3745
3746	SYSCALL_REQUIRE(
3747	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3748
3749	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
3750
3751	lid = state.pe_lwp;
3752	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
3753
3754	DPRINTF("Before resuming the child process where it left off and "
3755	    "without signal to be sent\n");
3756	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3757
3758	DPRINTF("Before calling %s() for the child - expected exited\n",
3759	    TWAIT_FNAME);
3760	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3761
3762	validate_status_exited(status, exitval);
3763
3764	DPRINTF("Before calling %s() for the child - expected no process\n",
3765	    TWAIT_FNAME);
3766	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3767}
3768
3769ATF_TC(lwp_exit1);
3770ATF_TC_HEAD(lwp_exit1, tc)
3771{
3772	atf_tc_set_md_var(tc, "descr",
3773	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
3774	    "EVENT_MASK set to PTRACE_LWP_EXIT");
3775}
3776
3777ATF_TC_BODY(lwp_exit1, tc)
3778{
3779	const int exitval = 5;
3780	const int sigval = SIGSTOP;
3781	pid_t child, wpid;
3782#if defined(TWAIT_HAVE_STATUS)
3783	int status;
3784#endif
3785	ptrace_state_t state;
3786	const int slen = sizeof(state);
3787	ptrace_event_t event;
3788	const int elen = sizeof(event);
3789	ucontext_t uc;
3790	lwpid_t lid;
3791	static const size_t ssize = 16*1024;
3792	void *stack;
3793
3794	DPRINTF("Before forking process PID=%d\n", getpid());
3795	SYSCALL_REQUIRE((child = fork()) != -1);
3796	if (child == 0) {
3797		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3798		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3799
3800		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3801		FORKEE_ASSERT(raise(sigval) == 0);
3802
3803		DPRINTF("Before allocating memory for stack in child\n");
3804		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
3805
3806		DPRINTF("Before making context for new lwp in child\n");
3807		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
3808
3809		DPRINTF("Before creating new in child\n");
3810		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
3811
3812		DPRINTF("Before waiting for lwp %d to exit\n", lid);
3813		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
3814
3815		DPRINTF("Before verifying that reported %d and running lid %d "
3816		    "are the same\n", lid, the_lwp_id);
3817		FORKEE_ASSERT_EQ(lid, the_lwp_id);
3818
3819		DPRINTF("Before exiting of the child process\n");
3820		_exit(exitval);
3821	}
3822	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3823
3824	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3825	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3826
3827	validate_status_stopped(status, sigval);
3828
3829	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
3830	event.pe_set_event = PTRACE_LWP_EXIT;
3831	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3832
3833	DPRINTF("Before resuming the child process where it left off and "
3834	    "without signal to be sent\n");
3835	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3836
3837	DPRINTF("Before calling %s() for the child - expected stopped "
3838	    "SIGTRAP\n", TWAIT_FNAME);
3839	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3840
3841	validate_status_stopped(status, SIGTRAP);
3842
3843	SYSCALL_REQUIRE(
3844	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3845
3846	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
3847
3848	lid = state.pe_lwp;
3849	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
3850
3851	DPRINTF("Before resuming the child process where it left off and "
3852	    "without signal to be sent\n");
3853	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3854
3855	DPRINTF("Before calling %s() for the child - expected exited\n",
3856	    TWAIT_FNAME);
3857	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3858
3859	validate_status_exited(status, exitval);
3860
3861	DPRINTF("Before calling %s() for the child - expected no process\n",
3862	    TWAIT_FNAME);
3863	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3864}
3865
3866ATF_TC(signal1);
3867ATF_TC_HEAD(signal1, tc)
3868{
3869	atf_tc_set_md_var(tc, "descr",
3870	    "Verify that masking single unrelated signal does not stop tracer "
3871	    "from catching other signals");
3872}
3873
3874ATF_TC_BODY(signal1, tc)
3875{
3876	const int exitval = 5;
3877	const int sigval = SIGSTOP;
3878	const int sigmasked = SIGTRAP;
3879	const int signotmasked = SIGINT;
3880	pid_t child, wpid;
3881#if defined(TWAIT_HAVE_STATUS)
3882	int status;
3883#endif
3884	sigset_t intmask;
3885
3886	DPRINTF("Before forking process PID=%d\n", getpid());
3887	SYSCALL_REQUIRE((child = fork()) != -1);
3888	if (child == 0) {
3889		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3890		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3891
3892		sigemptyset(&intmask);
3893		sigaddset(&intmask, sigmasked);
3894		sigprocmask(SIG_BLOCK, &intmask, NULL);
3895
3896		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3897		FORKEE_ASSERT(raise(sigval) == 0);
3898
3899		DPRINTF("Before raising %s from child\n",
3900		    strsignal(signotmasked));
3901		FORKEE_ASSERT(raise(signotmasked) == 0);
3902
3903		DPRINTF("Before exiting of the child process\n");
3904		_exit(exitval);
3905	}
3906	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3907
3908	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3909	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3910
3911	validate_status_stopped(status, sigval);
3912
3913	DPRINTF("Before resuming the child process where it left off and "
3914	    "without signal to be sent\n");
3915	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3916
3917	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3918	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3919
3920	validate_status_stopped(status, signotmasked);
3921
3922	DPRINTF("Before resuming the child process where it left off and "
3923	    "without signal to be sent\n");
3924	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3925
3926	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3927	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3928
3929	validate_status_exited(status, exitval);
3930
3931	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3932	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3933}
3934
3935ATF_TC(signal2);
3936ATF_TC_HEAD(signal2, tc)
3937{
3938	atf_tc_set_md_var(tc, "descr",
3939	    "Verify that masking SIGTRAP in tracee stops tracer from "
3940	    "catching this raised signal");
3941}
3942
3943ATF_TC_BODY(signal2, tc)
3944{
3945	const int exitval = 5;
3946	const int sigval = SIGSTOP;
3947	const int sigmasked = SIGTRAP;
3948	pid_t child, wpid;
3949#if defined(TWAIT_HAVE_STATUS)
3950	int status;
3951#endif
3952	sigset_t intmask;
3953
3954	DPRINTF("Before forking process PID=%d\n", getpid());
3955	SYSCALL_REQUIRE((child = fork()) != -1);
3956	if (child == 0) {
3957		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3958		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3959
3960		sigemptyset(&intmask);
3961		sigaddset(&intmask, sigmasked);
3962		sigprocmask(SIG_BLOCK, &intmask, NULL);
3963
3964		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3965		FORKEE_ASSERT(raise(sigval) == 0);
3966
3967		DPRINTF("Before raising %s breakpoint from child\n",
3968		    strsignal(sigmasked));
3969		FORKEE_ASSERT(raise(sigmasked) == 0);
3970
3971		DPRINTF("Before exiting of the child process\n");
3972		_exit(exitval);
3973	}
3974	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3975
3976	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3977	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3978
3979	validate_status_stopped(status, sigval);
3980
3981	DPRINTF("Before resuming the child process where it left off and "
3982	    "without signal to be sent\n");
3983	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3984
3985	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3986	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3987
3988	validate_status_exited(status, exitval);
3989
3990	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3991	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3992}
3993
3994ATF_TC(signal3);
3995ATF_TC_HEAD(signal3, tc)
3996{
3997	atf_tc_set_md_var(tc, "timeout", "5");
3998	atf_tc_set_md_var(tc, "descr",
3999	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4000	    "catching software breakpoints");
4001}
4002
4003ATF_TC_BODY(signal3, tc)
4004{
4005	const int exitval = 5;
4006	const int sigval = SIGSTOP;
4007	const int sigmasked = SIGTRAP;
4008	pid_t child, wpid;
4009#if defined(TWAIT_HAVE_STATUS)
4010	int status;
4011#endif
4012	sigset_t intmask;
4013
4014	DPRINTF("Before forking process PID=%d\n", getpid());
4015	SYSCALL_REQUIRE((child = fork()) != -1);
4016	if (child == 0) {
4017		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4018		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4019
4020		sigemptyset(&intmask);
4021		sigaddset(&intmask, sigmasked);
4022		sigprocmask(SIG_BLOCK, &intmask, NULL);
4023
4024		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4025		FORKEE_ASSERT(raise(sigval) == 0);
4026
4027		DPRINTF("Before raising software breakpoint from child\n");
4028		trigger_trap();
4029
4030		DPRINTF("Before exiting of the child process\n");
4031		_exit(exitval);
4032	}
4033	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4034
4035	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4036	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4037
4038	validate_status_stopped(status, sigval);
4039
4040	DPRINTF("Before resuming the child process where it left off and "
4041	    "without signal to be sent\n");
4042	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4043
4044	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4045	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4046
4047	validate_status_stopped(status, sigmasked);
4048
4049	DPRINTF("Before resuming the child process where it left off and "
4050	    "without signal to be sent\n");
4051	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
4052
4053	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4054	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4055
4056	validate_status_signaled(status, SIGKILL, 0);
4057
4058	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4059	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4060}
4061
4062#if defined(PT_STEP)
4063ATF_TC(signal4);
4064ATF_TC_HEAD(signal4, tc)
4065{
4066	atf_tc_set_md_var(tc, "descr",
4067	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4068	    "catching single step trap");
4069}
4070
4071ATF_TC_BODY(signal4, tc)
4072{
4073	const int exitval = 5;
4074	const int sigval = SIGSTOP;
4075	const int sigmasked = SIGTRAP;
4076	pid_t child, wpid;
4077#if defined(TWAIT_HAVE_STATUS)
4078	int status;
4079#endif
4080	sigset_t intmask;
4081	int happy;
4082
4083#if defined(__arm__)
4084	/* PT_STEP not supported on arm 32-bit */
4085	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
4086#endif
4087
4088	DPRINTF("Before forking process PID=%d\n", getpid());
4089	SYSCALL_REQUIRE((child = fork()) != -1);
4090	if (child == 0) {
4091		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4092		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4093
4094		happy = check_happy(100);
4095
4096		sigemptyset(&intmask);
4097		sigaddset(&intmask, sigmasked);
4098		sigprocmask(SIG_BLOCK, &intmask, NULL);
4099
4100		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4101		FORKEE_ASSERT(raise(sigval) == 0);
4102
4103		FORKEE_ASSERT_EQ(happy, check_happy(100));
4104
4105		DPRINTF("Before exiting of the child process\n");
4106		_exit(exitval);
4107	}
4108	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4109
4110	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4111	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4112
4113	validate_status_stopped(status, sigval);
4114
4115	DPRINTF("Before resuming the child process where it left off and "
4116	    "without signal to be sent\n");
4117	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
4118
4119	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4120	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4121
4122	validate_status_stopped(status, sigmasked);
4123
4124	DPRINTF("Before resuming the child process where it left off and "
4125	    "without signal to be sent\n");
4126	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4127
4128	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4129	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4130
4131	validate_status_exited(status, exitval);
4132
4133	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4134	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4135}
4136#endif
4137
4138ATF_TC(signal5);
4139ATF_TC_HEAD(signal5, tc)
4140{
4141	atf_tc_set_md_var(tc, "descr",
4142	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4143	    "catching exec() breakpoint");
4144}
4145
4146ATF_TC_BODY(signal5, tc)
4147{
4148	const int sigval = SIGSTOP;
4149	const int sigmasked = SIGTRAP;
4150	pid_t child, wpid;
4151#if defined(TWAIT_HAVE_STATUS)
4152	int status;
4153#endif
4154	struct ptrace_siginfo info;
4155	sigset_t intmask;
4156
4157	memset(&info, 0, sizeof(info));
4158
4159	DPRINTF("Before forking process PID=%d\n", getpid());
4160	SYSCALL_REQUIRE((child = fork()) != -1);
4161	if (child == 0) {
4162		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4163		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4164
4165		sigemptyset(&intmask);
4166		sigaddset(&intmask, sigmasked);
4167		sigprocmask(SIG_BLOCK, &intmask, NULL);
4168
4169		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4170		FORKEE_ASSERT(raise(sigval) == 0);
4171
4172		DPRINTF("Before calling execve(2) from child\n");
4173		execlp("/bin/echo", "/bin/echo", NULL);
4174
4175		/* NOTREACHED */
4176		FORKEE_ASSERTX(0 && "Not reached");
4177	}
4178	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4179
4180	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4181	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4182
4183	validate_status_stopped(status, sigval);
4184
4185	DPRINTF("Before resuming the child process where it left off and "
4186	    "without signal to be sent\n");
4187	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4188
4189	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4190	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4191
4192	validate_status_stopped(status, sigmasked);
4193
4194	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4195	SYSCALL_REQUIRE(
4196	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4197
4198	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4199	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4200	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4201	    info.psi_siginfo.si_errno);
4202
4203	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigmasked);
4204	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
4205
4206	DPRINTF("Before resuming the child process where it left off and "
4207	    "without signal to be sent\n");
4208	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4209
4210	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4211	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4212
4213	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4214	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4215}
4216
4217#if defined(TWAIT_HAVE_PID)
4218ATF_TC(signal6);
4219ATF_TC_HEAD(signal6, tc)
4220{
4221	atf_tc_set_md_var(tc, "timeout", "5");
4222	atf_tc_set_md_var(tc, "descr",
4223	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4224	    "catching PTRACE_FORK breakpoint");
4225}
4226
4227ATF_TC_BODY(signal6, tc)
4228{
4229	const int exitval = 5;
4230	const int exitval2 = 15;
4231	const int sigval = SIGSTOP;
4232	const int sigmasked = SIGTRAP;
4233	pid_t child, child2, wpid;
4234#if defined(TWAIT_HAVE_STATUS)
4235	int status;
4236#endif
4237	sigset_t intmask;
4238	ptrace_state_t state;
4239	const int slen = sizeof(state);
4240	ptrace_event_t event;
4241	const int elen = sizeof(event);
4242
4243	atf_tc_expect_fail("PR kern/51918");
4244
4245	DPRINTF("Before forking process PID=%d\n", getpid());
4246	SYSCALL_REQUIRE((child = fork()) != -1);
4247	if (child == 0) {
4248		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4249		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4250
4251		sigemptyset(&intmask);
4252		sigaddset(&intmask, sigmasked);
4253		sigprocmask(SIG_BLOCK, &intmask, NULL);
4254
4255		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4256		FORKEE_ASSERT(raise(sigval) == 0);
4257
4258		FORKEE_ASSERT((child2 = fork()) != -1);
4259
4260		if (child2 == 0)
4261			_exit(exitval2);
4262
4263		FORKEE_REQUIRE_SUCCESS
4264			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4265
4266		forkee_status_exited(status, exitval2);
4267
4268		DPRINTF("Before exiting of the child process\n");
4269		_exit(exitval);
4270	}
4271	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4272
4273	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4274	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4275
4276	validate_status_stopped(status, sigval);
4277
4278	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
4279	event.pe_set_event = PTRACE_FORK;
4280	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4281
4282	DPRINTF("Before resuming the child process where it left off and "
4283	    "without signal to be sent\n");
4284	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4285
4286	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4287	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4288
4289	validate_status_stopped(status, sigmasked);
4290
4291	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4292	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
4293
4294	child2 = state.pe_other_pid;
4295	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
4296
4297	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
4298	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4299	    child2);
4300
4301	validate_status_stopped(status, SIGTRAP);
4302
4303	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
4304	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
4305	ATF_REQUIRE_EQ(state.pe_other_pid, child);
4306
4307	DPRINTF("Before resuming the forkee process where it left off and "
4308	    "without signal to be sent\n");
4309	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
4310
4311	DPRINTF("Before resuming the child process where it left off and "
4312	    "without signal to be sent\n");
4313	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4314
4315	DPRINTF("Before calling %s() for the forkee - expected exited\n",
4316	    TWAIT_FNAME);
4317	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4318	    child2);
4319
4320	validate_status_exited(status, exitval2);
4321
4322	DPRINTF("Before calling %s() for the forkee - expected no process\n",
4323	    TWAIT_FNAME);
4324	TWAIT_REQUIRE_FAILURE(ECHILD,
4325	    wpid = TWAIT_GENERIC(child2, &status, 0));
4326
4327	DPRINTF("Before calling %s() for the child - expected stopped "
4328	    "SIGCHLD\n", TWAIT_FNAME);
4329	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4330
4331	validate_status_stopped(status, SIGCHLD);
4332
4333	DPRINTF("Before resuming the child process where it left off and "
4334	    "without signal to be sent\n");
4335	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4336
4337	DPRINTF("Before calling %s() for the child - expected exited\n",
4338	    TWAIT_FNAME);
4339	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4340
4341	validate_status_exited(status, exitval);
4342
4343	DPRINTF("Before calling %s() for the child - expected no process\n",
4344	    TWAIT_FNAME);
4345	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4346}
4347#endif
4348
4349#if defined(TWAIT_HAVE_PID)
4350ATF_TC(signal7);
4351ATF_TC_HEAD(signal7, tc)
4352{
4353	atf_tc_set_md_var(tc, "descr",
4354	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4355	    "catching PTRACE_VFORK breakpoint");
4356}
4357
4358ATF_TC_BODY(signal7, tc)
4359{
4360	const int exitval = 5;
4361	const int exitval2 = 15;
4362	const int sigval = SIGSTOP;
4363	const int sigmasked = SIGTRAP;
4364	pid_t child, child2, wpid;
4365#if defined(TWAIT_HAVE_STATUS)
4366	int status;
4367#endif
4368	sigset_t intmask;
4369	ptrace_state_t state;
4370	const int slen = sizeof(state);
4371	ptrace_event_t event;
4372	const int elen = sizeof(event);
4373
4374	atf_tc_expect_fail("PR kern/51918");
4375
4376	DPRINTF("Before forking process PID=%d\n", getpid());
4377	SYSCALL_REQUIRE((child = fork()) != -1);
4378	if (child == 0) {
4379		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4380		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4381
4382		sigemptyset(&intmask);
4383		sigaddset(&intmask, sigmasked);
4384		sigprocmask(SIG_BLOCK, &intmask, NULL);
4385
4386		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4387		FORKEE_ASSERT(raise(sigval) == 0);
4388
4389		FORKEE_ASSERT((child2 = fork()) != -1);
4390
4391		if (child2 == 0)
4392			_exit(exitval2);
4393
4394		FORKEE_REQUIRE_SUCCESS
4395			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4396
4397		forkee_status_exited(status, exitval2);
4398
4399		DPRINTF("Before exiting of the child process\n");
4400		_exit(exitval);
4401	}
4402	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4403
4404	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4405	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4406
4407	validate_status_stopped(status, sigval);
4408
4409	DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
4410	event.pe_set_event = PTRACE_VFORK;
4411	SYSCALL_REQUIRE(
4412	    ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 ||
4413	    errno == ENOTSUP);
4414
4415	DPRINTF("Before resuming the child process where it left off and "
4416	    "without signal to be sent\n");
4417	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4418
4419	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4420	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4421
4422	validate_status_stopped(status, sigmasked);
4423
4424	SYSCALL_REQUIRE(
4425	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4426	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
4427
4428	child2 = state.pe_other_pid;
4429	DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2);
4430
4431	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
4432	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4433	    child2);
4434
4435	validate_status_stopped(status, SIGTRAP);
4436
4437	SYSCALL_REQUIRE(
4438	    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
4439	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
4440	ATF_REQUIRE_EQ(state.pe_other_pid, child);
4441
4442	DPRINTF("Before resuming the forkee process where it left off and "
4443	    "without signal to be sent\n");
4444	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
4445
4446	DPRINTF("Before resuming the child process where it left off and "
4447	    "without signal to be sent\n");
4448	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4449
4450	DPRINTF("Before calling %s() for the forkee - expected exited\n",
4451	    TWAIT_FNAME);
4452	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4453	    child2);
4454
4455	validate_status_exited(status, exitval2);
4456
4457	DPRINTF("Before calling %s() for the forkee - expected no process\n",
4458	    TWAIT_FNAME);
4459	TWAIT_REQUIRE_FAILURE(ECHILD,
4460	    wpid = TWAIT_GENERIC(child2, &status, 0));
4461
4462	DPRINTF("Before calling %s() for the child - expected stopped "
4463	    "SIGCHLD\n", TWAIT_FNAME);
4464	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4465
4466	validate_status_stopped(status, SIGCHLD);
4467
4468	DPRINTF("Before resuming the child process where it left off and "
4469	    "without signal to be sent\n");
4470	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4471
4472	DPRINTF("Before calling %s() for the child - expected exited\n",
4473	    TWAIT_FNAME);
4474	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4475
4476	validate_status_exited(status, exitval);
4477
4478	DPRINTF("Before calling %s() for the child - expected no process\n",
4479	    TWAIT_FNAME);
4480	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4481}
4482#endif
4483
4484ATF_TC(signal8);
4485ATF_TC_HEAD(signal8, tc)
4486{
4487	atf_tc_set_md_var(tc, "descr",
4488	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4489	    "catching PTRACE_VFORK_DONE breakpoint");
4490}
4491
4492ATF_TC_BODY(signal8, tc)
4493{
4494	const int exitval = 5;
4495	const int exitval2 = 15;
4496	const int sigval = SIGSTOP;
4497	const int sigmasked = SIGTRAP;
4498	pid_t child, child2, wpid;
4499#if defined(TWAIT_HAVE_STATUS)
4500	int status;
4501#endif
4502	sigset_t intmask;
4503	ptrace_state_t state;
4504	const int slen = sizeof(state);
4505	ptrace_event_t event;
4506	const int elen = sizeof(event);
4507
4508	atf_tc_expect_fail("PR kern/51918");
4509
4510	DPRINTF("Before forking process PID=%d\n", getpid());
4511	SYSCALL_REQUIRE((child = fork()) != -1);
4512	if (child == 0) {
4513		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4514		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4515
4516		sigemptyset(&intmask);
4517		sigaddset(&intmask, sigmasked);
4518		sigprocmask(SIG_BLOCK, &intmask, NULL);
4519
4520		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4521		FORKEE_ASSERT(raise(sigval) == 0);
4522
4523		FORKEE_ASSERT((child2 = vfork()) != -1);
4524
4525		if (child2 == 0)
4526			_exit(exitval2);
4527
4528		FORKEE_REQUIRE_SUCCESS
4529			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4530
4531		forkee_status_exited(status, exitval2);
4532
4533		DPRINTF("Before exiting of the child process\n");
4534		_exit(exitval);
4535	}
4536	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4537
4538	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4539	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4540
4541	validate_status_stopped(status, sigval);
4542
4543	DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
4544	    child);
4545	event.pe_set_event = PTRACE_VFORK_DONE;
4546	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4547
4548	DPRINTF("Before resuming the child process where it left off and "
4549	    "without signal to be sent\n");
4550	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4551
4552	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4553	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4554
4555	validate_status_stopped(status, sigmasked);
4556
4557	SYSCALL_REQUIRE(
4558	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4559	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
4560
4561	child2 = state.pe_other_pid;
4562	DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
4563
4564	DPRINTF("Before resuming the child process where it left off and "
4565	    "without signal to be sent\n");
4566	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4567
4568	DPRINTF("Before calling %s() for the child - expected stopped "
4569	    "SIGCHLD\n", TWAIT_FNAME);
4570	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4571
4572	validate_status_stopped(status, SIGCHLD);
4573
4574	DPRINTF("Before resuming the child process where it left off and "
4575	    "without signal to be sent\n");
4576	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4577
4578	DPRINTF("Before calling %s() for the child - expected exited\n",
4579	    TWAIT_FNAME);
4580	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4581
4582	validate_status_exited(status, exitval);
4583
4584	DPRINTF("Before calling %s() for the child - expected no process\n",
4585	    TWAIT_FNAME);
4586	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4587}
4588
4589ATF_TC(signal9);
4590ATF_TC_HEAD(signal9, tc)
4591{
4592	atf_tc_set_md_var(tc, "descr",
4593	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4594	    "catching PTRACE_LWP_CREATE breakpoint");
4595}
4596
4597ATF_TC_BODY(signal9, tc)
4598{
4599	const int exitval = 5;
4600	const int sigval = SIGSTOP;
4601	const int sigmasked = SIGTRAP;
4602	pid_t child, wpid;
4603#if defined(TWAIT_HAVE_STATUS)
4604	int status;
4605#endif
4606	sigset_t intmask;
4607	ptrace_state_t state;
4608	const int slen = sizeof(state);
4609	ptrace_event_t event;
4610	const int elen = sizeof(event);
4611	ucontext_t uc;
4612	lwpid_t lid;
4613	static const size_t ssize = 16*1024;
4614	void *stack;
4615
4616	atf_tc_expect_fail("PR kern/51918");
4617
4618	DPRINTF("Before forking process PID=%d\n", getpid());
4619	SYSCALL_REQUIRE((child = fork()) != -1);
4620	if (child == 0) {
4621		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4622		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4623
4624		sigemptyset(&intmask);
4625		sigaddset(&intmask, sigmasked);
4626		sigprocmask(SIG_BLOCK, &intmask, NULL);
4627
4628		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4629		FORKEE_ASSERT(raise(sigval) == 0);
4630
4631		DPRINTF("Before allocating memory for stack in child\n");
4632		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4633
4634		DPRINTF("Before making context for new lwp in child\n");
4635		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
4636
4637		DPRINTF("Before creating new in child\n");
4638		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4639
4640		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4641		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4642
4643		DPRINTF("Before verifying that reported %d and running lid %d "
4644		    "are the same\n", lid, the_lwp_id);
4645		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4646
4647		DPRINTF("Before exiting of the child process\n");
4648		_exit(exitval);
4649	}
4650	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4651
4652	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4653	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4654
4655	validate_status_stopped(status, sigval);
4656
4657	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
4658	event.pe_set_event = PTRACE_LWP_CREATE;
4659	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4660
4661	DPRINTF("Before resuming the child process where it left off and "
4662	    "without signal to be sent\n");
4663	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4664
4665	DPRINTF("Before calling %s() for the child - expected stopped "
4666	    "SIGTRAP\n", TWAIT_FNAME);
4667	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4668
4669	validate_status_stopped(status, sigmasked);
4670
4671	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4672
4673	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
4674
4675	lid = state.pe_lwp;
4676	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
4677
4678	DPRINTF("Before resuming the child process where it left off and "
4679	    "without signal to be sent\n");
4680	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4681
4682	DPRINTF("Before calling %s() for the child - expected exited\n",
4683	    TWAIT_FNAME);
4684	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4685
4686	validate_status_exited(status, exitval);
4687
4688	DPRINTF("Before calling %s() for the child - expected no process\n",
4689	    TWAIT_FNAME);
4690	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4691}
4692
4693ATF_TC(signal10);
4694ATF_TC_HEAD(signal10, tc)
4695{
4696	atf_tc_set_md_var(tc, "descr",
4697	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4698	    "catching PTRACE_LWP_EXIT breakpoint");
4699}
4700
4701ATF_TC_BODY(signal10, tc)
4702{
4703	const int exitval = 5;
4704	const int sigval = SIGSTOP;
4705	const int sigmasked = SIGTRAP;
4706	pid_t child, wpid;
4707#if defined(TWAIT_HAVE_STATUS)
4708	int status;
4709#endif
4710	sigset_t intmask;
4711	ptrace_state_t state;
4712	const int slen = sizeof(state);
4713	ptrace_event_t event;
4714	const int elen = sizeof(event);
4715	ucontext_t uc;
4716	lwpid_t lid;
4717	static const size_t ssize = 16*1024;
4718	void *stack;
4719
4720	atf_tc_expect_fail("PR kern/51918");
4721
4722	DPRINTF("Before forking process PID=%d\n", getpid());
4723	SYSCALL_REQUIRE((child = fork()) != -1);
4724	if (child == 0) {
4725		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4726		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4727
4728		sigemptyset(&intmask);
4729		sigaddset(&intmask, sigmasked);
4730		sigprocmask(SIG_BLOCK, &intmask, NULL);
4731
4732		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4733		FORKEE_ASSERT(raise(sigval) == 0);
4734
4735		DPRINTF("Before allocating memory for stack in child\n");
4736		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4737
4738		DPRINTF("Before making context for new lwp in child\n");
4739		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
4740
4741		DPRINTF("Before creating new in child\n");
4742		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4743
4744		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4745		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4746
4747		DPRINTF("Before verifying that reported %d and running lid %d "
4748		    "are the same\n", lid, the_lwp_id);
4749		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4750
4751		DPRINTF("Before exiting of the child process\n");
4752		_exit(exitval);
4753	}
4754	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4755
4756	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4757	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4758
4759	validate_status_stopped(status, sigval);
4760
4761	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
4762	event.pe_set_event = PTRACE_LWP_EXIT;
4763	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4764
4765	DPRINTF("Before resuming the child process where it left off and "
4766	    "without signal to be sent\n");
4767	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4768
4769	DPRINTF("Before calling %s() for the child - expected stopped "
4770	    "SIGTRAP\n", TWAIT_FNAME);
4771	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4772
4773	validate_status_stopped(status, sigmasked);
4774
4775	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4776
4777	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
4778
4779	lid = state.pe_lwp;
4780	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
4781
4782	DPRINTF("Before resuming the child process where it left off and "
4783	    "without signal to be sent\n");
4784	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4785
4786	DPRINTF("Before calling %s() for the child - expected exited\n",
4787	    TWAIT_FNAME);
4788	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4789
4790	validate_status_exited(status, exitval);
4791
4792	DPRINTF("Before calling %s() for the child - expected no process\n",
4793	    TWAIT_FNAME);
4794	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4795}
4796
4797static void
4798lwp_main_stop(void *arg)
4799{
4800	the_lwp_id = _lwp_self();
4801
4802	raise(SIGTRAP);
4803
4804	_lwp_exit();
4805}
4806
4807ATF_TC(suspend1);
4808ATF_TC_HEAD(suspend1, tc)
4809{
4810	atf_tc_set_md_var(tc, "descr",
4811	    "Verify that a thread can be suspended by a debugger and later "
4812	    "resumed by a tracee");
4813}
4814
4815ATF_TC_BODY(suspend1, tc)
4816{
4817	const int exitval = 5;
4818	const int sigval = SIGSTOP;
4819	pid_t child, wpid;
4820#if defined(TWAIT_HAVE_STATUS)
4821	int status;
4822#endif
4823	ucontext_t uc;
4824	lwpid_t lid;
4825	static const size_t ssize = 16*1024;
4826	void *stack;
4827	struct ptrace_lwpinfo pl;
4828	struct ptrace_siginfo psi;
4829	volatile int go = 0;
4830
4831	// Feature pending for refactoring
4832	atf_tc_expect_fail("PR kern/51995");
4833
4834	// Hangs with qemu
4835	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4836
4837	DPRINTF("Before forking process PID=%d\n", getpid());
4838	SYSCALL_REQUIRE((child = fork()) != -1);
4839	if (child == 0) {
4840		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4841		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4842
4843		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4844		FORKEE_ASSERT(raise(sigval) == 0);
4845
4846		DPRINTF("Before allocating memory for stack in child\n");
4847		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4848
4849		DPRINTF("Before making context for new lwp in child\n");
4850		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
4851
4852		DPRINTF("Before creating new in child\n");
4853		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4854
4855		while (go == 0)
4856			continue;
4857
4858		raise(SIGINT);
4859
4860		FORKEE_ASSERT(_lwp_continue(lid) == 0);
4861
4862		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4863		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4864
4865		DPRINTF("Before verifying that reported %d and running lid %d "
4866		    "are the same\n", lid, the_lwp_id);
4867		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4868
4869		DPRINTF("Before exiting of the child process\n");
4870		_exit(exitval);
4871	}
4872	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4873
4874	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4875	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4876
4877	validate_status_stopped(status, sigval);
4878
4879	DPRINTF("Before resuming the child process where it left off and "
4880	    "without signal to be sent\n");
4881	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4882
4883	DPRINTF("Before calling %s() for the child - expected stopped "
4884	    "SIGTRAP\n", TWAIT_FNAME);
4885	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4886
4887	validate_status_stopped(status, SIGTRAP);
4888
4889	DPRINTF("Before reading siginfo and lwpid_t\n");
4890	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
4891
4892	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
4893	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
4894
4895        DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
4896	    child, getpid());
4897	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
4898
4899	DPRINTF("Before resuming the child process where it left off and "
4900	    "without signal to be sent\n");
4901	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4902
4903	DPRINTF("Before calling %s() for the child - expected stopped "
4904	    "SIGINT\n", TWAIT_FNAME);
4905	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4906
4907	validate_status_stopped(status, SIGINT);
4908
4909	pl.pl_lwpid = 0;
4910
4911	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
4912	while (pl.pl_lwpid != 0) {
4913
4914		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
4915		switch (pl.pl_lwpid) {
4916		case 1:
4917			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
4918			break;
4919		case 2:
4920			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
4921			break;
4922		}
4923	}
4924
4925	DPRINTF("Before resuming the child process where it left off and "
4926	    "without signal to be sent\n");
4927	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4928
4929	DPRINTF("Before calling %s() for the child - expected exited\n",
4930	    TWAIT_FNAME);
4931	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4932
4933	validate_status_exited(status, exitval);
4934
4935	DPRINTF("Before calling %s() for the child - expected no process\n",
4936	    TWAIT_FNAME);
4937	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4938}
4939
4940ATF_TC(suspend2);
4941ATF_TC_HEAD(suspend2, tc)
4942{
4943	atf_tc_set_md_var(tc, "descr",
4944	    "Verify that the while the only thread within a process is "
4945	    "suspended, the whole process cannot be unstopped");
4946}
4947
4948ATF_TC_BODY(suspend2, tc)
4949{
4950	const int exitval = 5;
4951	const int sigval = SIGSTOP;
4952	pid_t child, wpid;
4953#if defined(TWAIT_HAVE_STATUS)
4954	int status;
4955#endif
4956	struct ptrace_siginfo psi;
4957
4958	// Feature pending for refactoring
4959	atf_tc_expect_fail("PR kern/51995");
4960
4961	// Hangs with qemu
4962	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4963
4964	DPRINTF("Before forking process PID=%d\n", getpid());
4965	SYSCALL_REQUIRE((child = fork()) != -1);
4966	if (child == 0) {
4967		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4968		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4969
4970		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4971		FORKEE_ASSERT(raise(sigval) == 0);
4972
4973		DPRINTF("Before exiting of the child process\n");
4974		_exit(exitval);
4975	}
4976	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4977
4978	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4979	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4980
4981	validate_status_stopped(status, sigval);
4982
4983	DPRINTF("Before reading siginfo and lwpid_t\n");
4984	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
4985
4986	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
4987	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
4988
4989	DPRINTF("Before resuming the child process where it left off and "
4990	    "without signal to be sent\n");
4991	ATF_REQUIRE_ERRNO(EDEADLK,
4992	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
4993
4994	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
4995	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
4996
4997	DPRINTF("Before resuming the child process where it left off and "
4998	    "without signal to be sent\n");
4999	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5000
5001	DPRINTF("Before calling %s() for the child - expected exited\n",
5002	    TWAIT_FNAME);
5003	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5004
5005	validate_status_exited(status, exitval);
5006
5007	DPRINTF("Before calling %s() for the child - expected no process\n",
5008	    TWAIT_FNAME);
5009	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5010}
5011
5012ATF_TC(resume1);
5013ATF_TC_HEAD(resume1, tc)
5014{
5015	atf_tc_set_md_var(tc, "timeout", "5");
5016	atf_tc_set_md_var(tc, "descr",
5017	    "Verify that a thread can be suspended by a debugger and later "
5018	    "resumed by the debugger");
5019}
5020
5021ATF_TC_BODY(resume1, tc)
5022{
5023	struct msg_fds fds;
5024	const int exitval = 5;
5025	const int sigval = SIGSTOP;
5026	pid_t child, wpid;
5027	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
5028#if defined(TWAIT_HAVE_STATUS)
5029	int status;
5030#endif
5031	ucontext_t uc;
5032	lwpid_t lid;
5033	static const size_t ssize = 16*1024;
5034	void *stack;
5035	struct ptrace_lwpinfo pl;
5036	struct ptrace_siginfo psi;
5037
5038	// Feature pending for refactoring
5039	atf_tc_expect_fail("PR kern/51995");
5040
5041	// Hangs with qemu
5042	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
5043
5044	SYSCALL_REQUIRE(msg_open(&fds) == 0);
5045
5046	DPRINTF("Before forking process PID=%d\n", getpid());
5047	SYSCALL_REQUIRE((child = fork()) != -1);
5048	if (child == 0) {
5049		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5050		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5051
5052		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5053		FORKEE_ASSERT(raise(sigval) == 0);
5054
5055		DPRINTF("Before allocating memory for stack in child\n");
5056		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5057
5058		DPRINTF("Before making context for new lwp in child\n");
5059		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
5060
5061		DPRINTF("Before creating new in child\n");
5062		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5063
5064		CHILD_TO_PARENT("Message", fds, msg);
5065
5066		raise(SIGINT);
5067
5068		DPRINTF("Before waiting for lwp %d to exit\n", lid);
5069		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5070
5071		DPRINTF("Before verifying that reported %d and running lid %d "
5072		    "are the same\n", lid, the_lwp_id);
5073		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5074
5075		DPRINTF("Before exiting of the child process\n");
5076		_exit(exitval);
5077	}
5078	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5079
5080	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5081	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5082
5083	validate_status_stopped(status, sigval);
5084
5085	DPRINTF("Before resuming the child process where it left off and "
5086	    "without signal to be sent\n");
5087	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5088
5089	DPRINTF("Before calling %s() for the child - expected stopped "
5090	    "SIGTRAP\n", TWAIT_FNAME);
5091	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5092
5093	validate_status_stopped(status, SIGTRAP);
5094
5095	DPRINTF("Before reading siginfo and lwpid_t\n");
5096	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
5097
5098	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
5099	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
5100
5101	PARENT_FROM_CHILD("Message", fds, msg);
5102
5103	DPRINTF("Before resuming the child process where it left off and "
5104	    "without signal to be sent\n");
5105	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5106
5107	DPRINTF("Before calling %s() for the child - expected stopped "
5108	    "SIGINT\n", TWAIT_FNAME);
5109	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5110
5111	validate_status_stopped(status, SIGINT);
5112
5113	pl.pl_lwpid = 0;
5114
5115	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
5116	while (pl.pl_lwpid != 0) {
5117		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
5118		switch (pl.pl_lwpid) {
5119		case 1:
5120			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
5121			break;
5122		case 2:
5123			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
5124			break;
5125		}
5126	}
5127
5128	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
5129	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
5130
5131	DPRINTF("Before resuming the child process where it left off and "
5132	    "without signal to be sent\n");
5133	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5134
5135	DPRINTF("Before calling %s() for the child - expected exited\n",
5136	    TWAIT_FNAME);
5137	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5138
5139	validate_status_exited(status, exitval);
5140
5141	DPRINTF("Before calling %s() for the child - expected no process\n",
5142	    TWAIT_FNAME);
5143	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5144
5145	msg_close(&fds);
5146
5147	DPRINTF("XXX: Test worked this time but for consistency timeout it\n");
5148	sleep(10);
5149}
5150
5151ATF_TC(syscall1);
5152ATF_TC_HEAD(syscall1, tc)
5153{
5154	atf_tc_set_md_var(tc, "descr",
5155	    "Verify that getpid(2) can be traced with PT_SYSCALL");
5156}
5157
5158ATF_TC_BODY(syscall1, tc)
5159{
5160	const int exitval = 5;
5161	const int sigval = SIGSTOP;
5162	pid_t child, wpid;
5163#if defined(TWAIT_HAVE_STATUS)
5164	int status;
5165#endif
5166	struct ptrace_siginfo info;
5167	memset(&info, 0, sizeof(info));
5168
5169	DPRINTF("Before forking process PID=%d\n", getpid());
5170	SYSCALL_REQUIRE((child = fork()) != -1);
5171	if (child == 0) {
5172		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5173		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5174
5175		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5176		FORKEE_ASSERT(raise(sigval) == 0);
5177
5178		syscall(SYS_getpid);
5179
5180		DPRINTF("Before exiting of the child process\n");
5181		_exit(exitval);
5182	}
5183	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5184
5185	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5186	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5187
5188	validate_status_stopped(status, sigval);
5189
5190	DPRINTF("Before resuming the child process where it left off and "
5191	    "without signal to be sent\n");
5192	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5193
5194	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5195	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5196
5197	validate_status_stopped(status, SIGTRAP);
5198
5199	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5200	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5201
5202	DPRINTF("Before checking siginfo_t and lwpid\n");
5203	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
5204	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5205	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
5206
5207	DPRINTF("Before resuming the child process where it left off and "
5208	    "without signal to be sent\n");
5209	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5210
5211	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5212	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5213
5214	validate_status_stopped(status, SIGTRAP);
5215
5216	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5217	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5218
5219	DPRINTF("Before checking siginfo_t and lwpid\n");
5220	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
5221	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5222	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
5223
5224	DPRINTF("Before resuming the child process where it left off and "
5225	    "without signal to be sent\n");
5226	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5227
5228	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5229	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5230
5231	validate_status_exited(status, exitval);
5232
5233	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5234	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5235}
5236
5237ATF_TC(syscallemu1);
5238ATF_TC_HEAD(syscallemu1, tc)
5239{
5240	atf_tc_set_md_var(tc, "descr",
5241	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
5242}
5243
5244ATF_TC_BODY(syscallemu1, tc)
5245{
5246	const int exitval = 5;
5247	const int sigval = SIGSTOP;
5248	pid_t child, wpid;
5249#if defined(TWAIT_HAVE_STATUS)
5250	int status;
5251#endif
5252
5253#if defined(__sparc__) && !defined(__sparc64__)
5254	/* syscallemu does not work on sparc (32-bit) */
5255	atf_tc_expect_fail("PR kern/52166");
5256#endif
5257
5258	DPRINTF("Before forking process PID=%d\n", getpid());
5259	SYSCALL_REQUIRE((child = fork()) != -1);
5260	if (child == 0) {
5261		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5262		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5263
5264		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5265		FORKEE_ASSERT(raise(sigval) == 0);
5266
5267		syscall(SYS_exit, 100);
5268
5269		DPRINTF("Before exiting of the child process\n");
5270		_exit(exitval);
5271	}
5272	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5273
5274	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5275	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5276
5277	validate_status_stopped(status, sigval);
5278
5279	DPRINTF("Before resuming the child process where it left off and "
5280	    "without signal to be sent\n");
5281	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5282
5283	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5284	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5285
5286	validate_status_stopped(status, SIGTRAP);
5287
5288	DPRINTF("Set SYSCALLEMU for intercepted syscall\n");
5289	SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
5290
5291	DPRINTF("Before resuming the child process where it left off and "
5292	    "without signal to be sent\n");
5293	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5294
5295	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5296	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5297
5298	validate_status_stopped(status, SIGTRAP);
5299
5300	DPRINTF("Before resuming the child process where it left off and "
5301	    "without signal to be sent\n");
5302	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5303
5304	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5305	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5306
5307	validate_status_exited(status, exitval);
5308
5309	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5310	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5311}
5312
5313#include "t_ptrace_amd64_wait.h"
5314#include "t_ptrace_i386_wait.h"
5315#include "t_ptrace_x86_wait.h"
5316
5317ATF_TP_ADD_TCS(tp)
5318{
5319	setvbuf(stdout, NULL, _IONBF, 0);
5320	setvbuf(stderr, NULL, _IONBF, 0);
5321
5322	ATF_TP_ADD_TC(tp, traceme_raise1);
5323	ATF_TP_ADD_TC(tp, traceme_raise2);
5324	ATF_TP_ADD_TC(tp, traceme_raise3);
5325	ATF_TP_ADD_TC(tp, traceme_raise4);
5326	ATF_TP_ADD_TC(tp, traceme_raise5);
5327
5328	ATF_TP_ADD_TC(tp, traceme_crash_trap);
5329	ATF_TP_ADD_TC(tp, traceme_crash_segv);
5330	ATF_TP_ADD_TC(tp, traceme_crash_ill);
5331	ATF_TP_ADD_TC(tp, traceme_crash_fpe);
5332	ATF_TP_ADD_TC(tp, traceme_crash_bus);
5333
5334	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle1);
5335	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle2);
5336	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle3);
5337
5338	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked1);
5339	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked2);
5340	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked3);
5341
5342	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored1);
5343	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored2);
5344	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored3);
5345
5346	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple1);
5347	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple2);
5348	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple3);
5349	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple4);
5350	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple5);
5351
5352	ATF_TP_ADD_TC(tp, traceme_pid1_parent);
5353
5354	ATF_TP_ADD_TC(tp, traceme_vfork_raise1);
5355	ATF_TP_ADD_TC(tp, traceme_vfork_raise2);
5356	ATF_TP_ADD_TC(tp, traceme_vfork_raise3);
5357	ATF_TP_ADD_TC(tp, traceme_vfork_raise4);
5358	ATF_TP_ADD_TC(tp, traceme_vfork_raise5);
5359	ATF_TP_ADD_TC(tp, traceme_vfork_raise6);
5360	ATF_TP_ADD_TC(tp, traceme_vfork_raise7);
5361	ATF_TP_ADD_TC(tp, traceme_vfork_raise8);
5362
5363	ATF_TP_ADD_TC(tp, traceme_vfork_crash_trap);
5364	ATF_TP_ADD_TC(tp, traceme_vfork_crash_segv);
5365	ATF_TP_ADD_TC(tp, traceme_vfork_crash_ill);
5366	ATF_TP_ADD_TC(tp, traceme_vfork_crash_fpe);
5367	ATF_TP_ADD_TC(tp, traceme_vfork_crash_bus);
5368
5369	ATF_TP_ADD_TC(tp, traceme_vfork_exec);
5370
5371	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_trap);
5372	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_segv);
5373	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_ill);
5374	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_fpe);
5375	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_bus);
5376
5377	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sees_terminaton_before_the_parent);
5378	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sysctl_lookup_without_duplicates);
5379	ATF_TP_ADD_TC_HAVE_PID(tp,
5380		unrelated_tracer_sees_terminaton_before_the_parent);
5381	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_attach_to_unrelated_stopped_process);
5382
5383	ATF_TP_ADD_TC(tp, parent_attach_to_its_child);
5384	ATF_TP_ADD_TC(tp, parent_attach_to_its_stopped_child);
5385
5386	ATF_TP_ADD_TC(tp, child_attach_to_its_parent);
5387	ATF_TP_ADD_TC(tp, child_attach_to_its_stopped_parent);
5388
5389	ATF_TP_ADD_TC_HAVE_PID(tp,
5390		tracee_sees_its_original_parent_getppid);
5391	ATF_TP_ADD_TC_HAVE_PID(tp,
5392		tracee_sees_its_original_parent_sysctl_kinfo_proc2);
5393	ATF_TP_ADD_TC_HAVE_PID(tp,
5394		tracee_sees_its_original_parent_procfs_status);
5395
5396	ATF_TP_ADD_TC(tp, eventmask_preserved_empty);
5397	ATF_TP_ADD_TC(tp, eventmask_preserved_fork);
5398	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork);
5399	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork_done);
5400	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_create);
5401	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_exit);
5402
5403	ATF_TP_ADD_TC(tp, fork1);
5404	ATF_TP_ADD_TC_HAVE_PID(tp, fork2);
5405	ATF_TP_ADD_TC_HAVE_PID(tp, fork3);
5406	ATF_TP_ADD_TC_HAVE_PID(tp, fork4);
5407	ATF_TP_ADD_TC(tp, fork5);
5408	ATF_TP_ADD_TC_HAVE_PID(tp, fork6);
5409	ATF_TP_ADD_TC_HAVE_PID(tp, fork7);
5410	ATF_TP_ADD_TC_HAVE_PID(tp, fork8);
5411
5412	ATF_TP_ADD_TC(tp, vfork1);
5413	ATF_TP_ADD_TC_HAVE_PID(tp, vfork2);
5414	ATF_TP_ADD_TC_HAVE_PID(tp, vfork3);
5415	ATF_TP_ADD_TC_HAVE_PID(tp, vfork4);
5416	ATF_TP_ADD_TC(tp, vfork5);
5417	ATF_TP_ADD_TC_HAVE_PID(tp, vfork6);
5418// thes tests hang on SMP machines, disable them for now
5419//	ATF_TP_ADD_TC_HAVE_PID(tp, vfork7);
5420//	ATF_TP_ADD_TC_HAVE_PID(tp, vfork8);
5421
5422	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8);
5423	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16);
5424	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32);
5425	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64);
5426
5427	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8);
5428	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16);
5429	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32);
5430	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64);
5431
5432	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8);
5433	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16);
5434	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32);
5435	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64);
5436
5437	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8);
5438	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16);
5439	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32);
5440	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64);
5441
5442	ATF_TP_ADD_TC(tp, bytes_transfer_read_d);
5443	ATF_TP_ADD_TC(tp, bytes_transfer_read_i);
5444	ATF_TP_ADD_TC(tp, bytes_transfer_write_d);
5445	ATF_TP_ADD_TC(tp, bytes_transfer_write_i);
5446
5447	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8_text);
5448	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16_text);
5449	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32_text);
5450	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64_text);
5451
5452	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8_text);
5453	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16_text);
5454	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32_text);
5455	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64_text);
5456
5457	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8_text);
5458	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16_text);
5459	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32_text);
5460	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64_text);
5461
5462	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8_text);
5463	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16_text);
5464	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32_text);
5465	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64_text);
5466
5467	ATF_TP_ADD_TC(tp, bytes_transfer_read_d_text);
5468	ATF_TP_ADD_TC(tp, bytes_transfer_read_i_text);
5469	ATF_TP_ADD_TC(tp, bytes_transfer_write_d_text);
5470	ATF_TP_ADD_TC(tp, bytes_transfer_write_i_text);
5471
5472	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_auxv);
5473
5474	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs1);
5475	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs2);
5476	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs3);
5477	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs4);
5478	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs5);
5479	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs6);
5480
5481	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs1);
5482	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs2);
5483
5484	ATF_TP_ADD_TC_PT_STEP(tp, step1);
5485	ATF_TP_ADD_TC_PT_STEP(tp, step2);
5486	ATF_TP_ADD_TC_PT_STEP(tp, step3);
5487	ATF_TP_ADD_TC_PT_STEP(tp, step4);
5488
5489	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
5490	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
5491	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
5492	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
5493
5494	ATF_TP_ADD_TC(tp, kill1);
5495	ATF_TP_ADD_TC(tp, kill2);
5496	ATF_TP_ADD_TC(tp, kill3);
5497
5498	ATF_TP_ADD_TC(tp, lwpinfo1);
5499	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
5500
5501	ATF_TP_ADD_TC(tp, siginfo1);
5502	ATF_TP_ADD_TC(tp, siginfo2);
5503	ATF_TP_ADD_TC(tp, siginfo3);
5504	ATF_TP_ADD_TC(tp, siginfo4);
5505	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
5506	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
5507
5508	ATF_TP_ADD_TC(tp, lwp_create1);
5509
5510	ATF_TP_ADD_TC(tp, lwp_exit1);
5511
5512	ATF_TP_ADD_TC(tp, signal1);
5513	ATF_TP_ADD_TC(tp, signal2);
5514	ATF_TP_ADD_TC(tp, signal3);
5515	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
5516	ATF_TP_ADD_TC(tp, signal5);
5517	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
5518	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
5519	ATF_TP_ADD_TC(tp, signal8);
5520	ATF_TP_ADD_TC(tp, signal9);
5521	ATF_TP_ADD_TC(tp, signal10);
5522
5523	ATF_TP_ADD_TC(tp, suspend1);
5524	ATF_TP_ADD_TC(tp, suspend2);
5525
5526	ATF_TP_ADD_TC(tp, resume1);
5527
5528	ATF_TP_ADD_TC(tp, syscall1);
5529
5530	ATF_TP_ADD_TC(tp, syscallemu1);
5531
5532	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
5533	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
5534	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
5535
5536	return atf_no_error();
5537}
5538