t_ptrace_wait.c revision 1.117
1/*	$NetBSD: t_ptrace_wait.c,v 1.117 2019/05/01 21:59:32 kamil Exp $	*/
2
3/*-
4 * Copyright (c) 2016, 2017, 2018, 2019 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.117 2019/05/01 21:59:32 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 <pthread.h>
47#include <sched.h>
48#include <signal.h>
49#include <stdint.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <strings.h>
53#include <time.h>
54#include <unistd.h>
55
56#include <fenv.h>
57#if (__arm__ && !__SOFTFP__) || __aarch64__
58#include <ieeefp.h> /* only need for ARM Cortex/Neon hack */
59#endif
60
61#include <atf-c.h>
62
63#include "h_macros.h"
64
65#include "t_ptrace_wait.h"
66#include "msg.h"
67
68#define PARENT_TO_CHILD(info, fds, msg) \
69    SYSCALL_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, \
70	sizeof(msg)) == 0)
71
72#define CHILD_FROM_PARENT(info, fds, msg) \
73    FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, \
74	sizeof(msg)) == 0)
75
76#define CHILD_TO_PARENT(info, fds, msg) \
77    FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, \
78	sizeof(msg)) == 0)
79
80#define PARENT_FROM_CHILD(info, fds, msg) \
81    SYSCALL_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, \
82	sizeof(msg)) == 0)
83
84#define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \
85    strerror(errno))
86#define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \
87    "%d(%s) != %d", res, strerror(res), exp)
88
89static int debug = 0;
90
91#define DPRINTF(a, ...)	do  \
92	if (debug) printf(a,  ##__VA_ARGS__); \
93    while (/*CONSTCOND*/0)
94
95#ifndef TEST_VFORK_ENABLED
96#define TEST_VFORK_ENABLED 0
97#endif
98
99/// ----------------------------------------------------------------------------
100
101static void
102traceme_raise(int sigval)
103{
104	const int exitval = 5;
105	pid_t child, wpid;
106#if defined(TWAIT_HAVE_STATUS)
107	int status;
108#endif
109
110	struct ptrace_siginfo info;
111	memset(&info, 0, sizeof(info));
112
113	DPRINTF("Before forking process PID=%d\n", getpid());
114	SYSCALL_REQUIRE((child = fork()) != -1);
115	if (child == 0) {
116		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
117		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
118
119		DPRINTF("Before raising %s from child\n", strsignal(sigval));
120		FORKEE_ASSERT(raise(sigval) == 0);
121
122		switch (sigval) {
123		case SIGKILL:
124			/* NOTREACHED */
125			FORKEE_ASSERTX(0 && "This shall not be reached");
126			__unreachable();
127		default:
128			DPRINTF("Before exiting of the child process\n");
129			_exit(exitval);
130		}
131	}
132	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
133
134	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
135	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
136
137	switch (sigval) {
138	case SIGKILL:
139		validate_status_signaled(status, sigval, 0);
140		break;
141	default:
142		validate_status_stopped(status, sigval);
143
144		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
145			"child\n");
146		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
147			sizeof(info)) != -1);
148
149		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
150		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
151			"si_errno=%#x\n",
152			info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
153			info.psi_siginfo.si_errno);
154
155		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
156		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
157
158		DPRINTF("Before resuming the child process where it left off "
159		    "and without signal to be sent\n");
160		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
161
162		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
163		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
164		    child);
165		break;
166	}
167
168	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
169	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
170}
171
172#define TRACEME_RAISE(test, sig)					\
173ATF_TC(test);								\
174ATF_TC_HEAD(test, tc)							\
175{									\
176	atf_tc_set_md_var(tc, "descr",					\
177	    "Verify " #sig " followed by _exit(2) in a child");		\
178}									\
179									\
180ATF_TC_BODY(test, tc)							\
181{									\
182									\
183	traceme_raise(sig);						\
184}
185
186TRACEME_RAISE(traceme_raise1, SIGKILL) /* non-maskable */
187TRACEME_RAISE(traceme_raise2, SIGSTOP) /* non-maskable */
188TRACEME_RAISE(traceme_raise3, SIGABRT) /* regular abort trap */
189TRACEME_RAISE(traceme_raise4, SIGHUP)  /* hangup */
190TRACEME_RAISE(traceme_raise5, SIGCONT) /* continued? */
191TRACEME_RAISE(traceme_raise6, SIGTRAP) /* crash signal */
192TRACEME_RAISE(traceme_raise7, SIGBUS) /* crash signal */
193TRACEME_RAISE(traceme_raise8, SIGILL) /* crash signal */
194TRACEME_RAISE(traceme_raise9, SIGFPE) /* crash signal */
195TRACEME_RAISE(traceme_raise10, SIGSEGV) /* crash signal */
196
197/// ----------------------------------------------------------------------------
198
199static void
200traceme_raisesignal_ignored(int sigignored)
201{
202	const int exitval = 5;
203	const int sigval = SIGSTOP;
204	pid_t child, wpid;
205	struct sigaction sa;
206#if defined(TWAIT_HAVE_STATUS)
207	int status;
208#endif
209	struct ptrace_siginfo info;
210
211	memset(&info, 0, sizeof(info));
212
213	DPRINTF("Before forking process PID=%d\n", getpid());
214	SYSCALL_REQUIRE((child = fork()) != -1);
215	if (child == 0) {
216		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
217		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
218
219		memset(&sa, 0, sizeof(sa));
220		sa.sa_handler = SIG_IGN;
221		sigemptyset(&sa.sa_mask);
222		FORKEE_ASSERT(sigaction(sigignored, &sa, NULL) != -1);
223
224		DPRINTF("Before raising %s from child\n", strsignal(sigval));
225		FORKEE_ASSERT(raise(sigval) == 0);
226
227		DPRINTF("Before raising %s from child\n",
228		    strsignal(sigignored));
229		FORKEE_ASSERT(raise(sigignored) == 0);
230
231		DPRINTF("Before exiting of the child process\n");
232		_exit(exitval);
233	}
234	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
235
236	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
237	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
238
239	validate_status_stopped(status, sigval);
240
241	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
242	SYSCALL_REQUIRE(
243	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
244
245	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
246	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
247	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
248	    info.psi_siginfo.si_errno);
249
250	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
251	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
252
253	DPRINTF("Before resuming the child process where it left off and "
254	    "without signal to be sent\n");
255	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
256
257	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
258	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
259
260	validate_status_stopped(status, sigignored);
261
262	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
263	SYSCALL_REQUIRE(
264	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
265
266	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
267	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
268	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
269	    info.psi_siginfo.si_errno);
270
271	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigignored);
272	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
273
274	DPRINTF("Before resuming the child process where it left off and "
275	    "without signal to be sent\n");
276	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
277
278	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
279	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
280
281	validate_status_exited(status, exitval);
282
283	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
284	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
285}
286
287#define TRACEME_RAISESIGNAL_IGNORED(test, sig)				\
288ATF_TC(test);								\
289ATF_TC_HEAD(test, tc)							\
290{									\
291	atf_tc_set_md_var(tc, "descr",					\
292	    "Verify that ignoring (with SIG_IGN) " #sig " in tracee "	\
293	    "does not stop tracer from catching this raised signal");	\
294}									\
295									\
296ATF_TC_BODY(test, tc)							\
297{									\
298									\
299	traceme_raisesignal_ignored(sig);				\
300}
301
302// A signal handler for SIGKILL and SIGSTOP cannot be ignored.
303TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored1, SIGABRT) /* abort */
304TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored2, SIGHUP)  /* hangup */
305TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored3, SIGCONT) /* cont. */
306TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored4, SIGTRAP) /* crash */
307TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored5, SIGBUS) /* crash */
308TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored6, SIGILL) /* crash */
309TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored7, SIGFPE) /* crash */
310TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored8, SIGSEGV) /* crash */
311
312/// ----------------------------------------------------------------------------
313
314static void
315traceme_raisesignal_masked(int sigmasked)
316{
317	const int exitval = 5;
318	const int sigval = SIGSTOP;
319	pid_t child, wpid;
320#if defined(TWAIT_HAVE_STATUS)
321	int status;
322#endif
323	sigset_t intmask;
324	struct ptrace_siginfo info;
325
326	memset(&info, 0, sizeof(info));
327
328	DPRINTF("Before forking process PID=%d\n", getpid());
329	SYSCALL_REQUIRE((child = fork()) != -1);
330	if (child == 0) {
331		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
332		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
333
334		sigemptyset(&intmask);
335		sigaddset(&intmask, sigmasked);
336		sigprocmask(SIG_BLOCK, &intmask, NULL);
337
338		DPRINTF("Before raising %s from child\n", strsignal(sigval));
339		FORKEE_ASSERT(raise(sigval) == 0);
340
341		DPRINTF("Before raising %s breakpoint from child\n",
342		    strsignal(sigmasked));
343		FORKEE_ASSERT(raise(sigmasked) == 0);
344
345		DPRINTF("Before exiting of the child process\n");
346		_exit(exitval);
347	}
348	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
349
350	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
351	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
352
353	validate_status_stopped(status, sigval);
354
355	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
356	SYSCALL_REQUIRE(
357	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
358
359	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
360	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
361	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
362	    info.psi_siginfo.si_errno);
363
364	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
365	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
366
367	DPRINTF("Before resuming the child process where it left off and "
368	    "without signal to be sent\n");
369	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
370
371	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
372	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
373
374	validate_status_exited(status, exitval);
375
376	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
377	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
378}
379
380#define TRACEME_RAISESIGNAL_MASKED(test, sig)				\
381ATF_TC(test);								\
382ATF_TC_HEAD(test, tc)							\
383{									\
384	atf_tc_set_md_var(tc, "descr",					\
385	    "Verify that masking (with SIG_BLOCK) " #sig " in tracee "	\
386	    "stops tracer from catching this raised signal");		\
387}									\
388									\
389ATF_TC_BODY(test, tc)							\
390{									\
391									\
392	traceme_raisesignal_masked(sig);				\
393}
394
395// A signal handler for SIGKILL and SIGSTOP cannot be masked.
396TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked1, SIGABRT) /* abort trap */
397TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked2, SIGHUP)  /* hangup */
398TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked3, SIGCONT) /* continued? */
399TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked4, SIGTRAP) /* crash sig. */
400TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked5, SIGBUS) /* crash sig. */
401TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked6, SIGILL) /* crash sig. */
402TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked7, SIGFPE) /* crash sig. */
403TRACEME_RAISESIGNAL_MASKED(traceme_raisesignal_masked8, SIGSEGV) /* crash sig. */
404
405/// ----------------------------------------------------------------------------
406
407static void
408traceme_crash(int sig)
409{
410	pid_t child, wpid;
411#if defined(TWAIT_HAVE_STATUS)
412	int status;
413#endif
414	struct ptrace_siginfo info;
415
416#ifndef PTRACE_ILLEGAL_ASM
417	if (sig == SIGILL)
418		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
419#endif
420
421	if (sig == SIGFPE && !are_fpu_exceptions_supported())
422		atf_tc_skip("FP exceptions are not supported");
423
424	memset(&info, 0, sizeof(info));
425
426	DPRINTF("Before forking process PID=%d\n", getpid());
427	SYSCALL_REQUIRE((child = fork()) != -1);
428	if (child == 0) {
429		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
430		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
431
432		DPRINTF("Before executing a trap\n");
433		switch (sig) {
434		case SIGTRAP:
435			trigger_trap();
436			break;
437		case SIGSEGV:
438			trigger_segv();
439			break;
440		case SIGILL:
441			trigger_ill();
442			break;
443		case SIGFPE:
444			trigger_fpe();
445			break;
446		case SIGBUS:
447			trigger_bus();
448			break;
449		default:
450			/* NOTREACHED */
451			FORKEE_ASSERTX(0 && "This shall not be reached");
452		}
453
454		/* NOTREACHED */
455		FORKEE_ASSERTX(0 && "This shall not be reached");
456	}
457	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
458
459	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
460	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
461
462	validate_status_stopped(status, sig);
463
464	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child");
465	SYSCALL_REQUIRE(
466	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
467
468	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
469	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
470	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
471	    info.psi_siginfo.si_errno);
472
473	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sig);
474	switch (sig) {
475	case SIGTRAP:
476		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_BRKPT);
477		break;
478	case SIGSEGV:
479		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SEGV_MAPERR);
480		break;
481	case SIGILL:
482		ATF_REQUIRE(info.psi_siginfo.si_code >= ILL_ILLOPC &&
483		            info.psi_siginfo.si_code <= ILL_BADSTK);
484		break;
485	case SIGFPE:
486		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, FPE_INTDIV);
487		break;
488	case SIGBUS:
489		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, BUS_ADRERR);
490		break;
491	}
492
493	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
494
495	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
496	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
497
498	validate_status_signaled(status, SIGKILL, 0);
499
500	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
501	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
502}
503
504#define TRACEME_CRASH(test, sig)					\
505ATF_TC(test);								\
506ATF_TC_HEAD(test, tc)							\
507{									\
508	atf_tc_set_md_var(tc, "descr",					\
509	    "Verify crash signal " #sig " in a child after PT_TRACE_ME"); \
510}									\
511									\
512ATF_TC_BODY(test, tc)							\
513{									\
514									\
515	traceme_crash(sig);						\
516}
517
518TRACEME_CRASH(traceme_crash_trap, SIGTRAP)
519TRACEME_CRASH(traceme_crash_segv, SIGSEGV)
520TRACEME_CRASH(traceme_crash_ill, SIGILL)
521TRACEME_CRASH(traceme_crash_fpe, SIGFPE)
522TRACEME_CRASH(traceme_crash_bus, SIGBUS)
523
524/// ----------------------------------------------------------------------------
525
526static void
527traceme_signalmasked_crash(int sig)
528{
529	const int sigval = SIGSTOP;
530	pid_t child, wpid;
531#if defined(TWAIT_HAVE_STATUS)
532	int status;
533#endif
534	struct ptrace_siginfo info;
535	sigset_t intmask;
536	struct kinfo_proc2 kp;
537	size_t len = sizeof(kp);
538
539	int name[6];
540	const size_t namelen = __arraycount(name);
541	ki_sigset_t kp_sigmask;
542
543#ifndef PTRACE_ILLEGAL_ASM
544	if (sig == SIGILL)
545		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
546#endif
547
548	if (sig == SIGFPE && !are_fpu_exceptions_supported())
549		atf_tc_skip("FP exceptions are not supported");
550
551	memset(&info, 0, sizeof(info));
552
553	DPRINTF("Before forking process PID=%d\n", getpid());
554	SYSCALL_REQUIRE((child = fork()) != -1);
555	if (child == 0) {
556		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
557		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
558
559		sigemptyset(&intmask);
560		sigaddset(&intmask, sig);
561		sigprocmask(SIG_BLOCK, &intmask, NULL);
562
563		DPRINTF("Before raising %s from child\n", strsignal(sigval));
564		FORKEE_ASSERT(raise(sigval) == 0);
565
566		DPRINTF("Before executing a trap\n");
567		switch (sig) {
568		case SIGTRAP:
569			trigger_trap();
570			break;
571		case SIGSEGV:
572			trigger_segv();
573			break;
574		case SIGILL:
575			trigger_ill();
576			break;
577		case SIGFPE:
578			trigger_fpe();
579			break;
580		case SIGBUS:
581			trigger_bus();
582			break;
583		default:
584			/* NOTREACHED */
585			FORKEE_ASSERTX(0 && "This shall not be reached");
586		}
587
588		/* NOTREACHED */
589		FORKEE_ASSERTX(0 && "This shall not be reached");
590	}
591	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
592
593	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
594	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
595
596	validate_status_stopped(status, sigval);
597
598	name[0] = CTL_KERN,
599	name[1] = KERN_PROC2,
600	name[2] = KERN_PROC_PID;
601	name[3] = child;
602	name[4] = sizeof(kp);
603	name[5] = 1;
604
605	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
606
607	kp_sigmask = kp.p_sigmask;
608
609	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
610	SYSCALL_REQUIRE(
611	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
612
613	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
614	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
615	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
616	    info.psi_siginfo.si_errno);
617
618	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
619	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
620
621	DPRINTF("Before resuming the child process where it left off and "
622	    "without signal to be sent\n");
623	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
624
625	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
626	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
627
628	validate_status_stopped(status, sig);
629
630	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child");
631	SYSCALL_REQUIRE(
632	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
633
634	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
635	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
636	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
637	    info.psi_siginfo.si_errno);
638
639	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
640
641	DPRINTF("kp_sigmask="
642	    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
643	    kp_sigmask.__bits[0], kp_sigmask.__bits[1], kp_sigmask.__bits[2],
644	    kp_sigmask.__bits[3]);
645
646	DPRINTF("kp.p_sigmask="
647	    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
648	    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
649	    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
650
651	ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask, sizeof(kp_sigmask)));
652
653	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sig);
654	switch (sig) {
655	case SIGTRAP:
656		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_BRKPT);
657		break;
658	case SIGSEGV:
659		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SEGV_MAPERR);
660		break;
661	case SIGILL:
662		ATF_REQUIRE(info.psi_siginfo.si_code >= ILL_ILLOPC &&
663		            info.psi_siginfo.si_code <= ILL_BADSTK);
664		break;
665	case SIGFPE:
666		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, FPE_INTDIV);
667		break;
668	case SIGBUS:
669		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, BUS_ADRERR);
670		break;
671	}
672
673	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
674
675	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
676	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
677
678	validate_status_signaled(status, SIGKILL, 0);
679
680	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
681	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
682}
683
684#define TRACEME_SIGNALMASKED_CRASH(test, sig)				\
685ATF_TC(test);								\
686ATF_TC_HEAD(test, tc)							\
687{									\
688	atf_tc_set_md_var(tc, "descr",					\
689	    "Verify masked crash signal " #sig " in a child after "	\
690	    "PT_TRACE_ME is delivered to its tracer");			\
691}									\
692									\
693ATF_TC_BODY(test, tc)							\
694{									\
695									\
696	traceme_signalmasked_crash(sig);				\
697}
698
699TRACEME_SIGNALMASKED_CRASH(traceme_signalmasked_crash_trap, SIGTRAP)
700TRACEME_SIGNALMASKED_CRASH(traceme_signalmasked_crash_segv, SIGSEGV)
701TRACEME_SIGNALMASKED_CRASH(traceme_signalmasked_crash_ill, SIGILL)
702TRACEME_SIGNALMASKED_CRASH(traceme_signalmasked_crash_fpe, SIGFPE)
703TRACEME_SIGNALMASKED_CRASH(traceme_signalmasked_crash_bus, SIGBUS)
704
705/// ----------------------------------------------------------------------------
706
707static void
708traceme_signalignored_crash(int sig)
709{
710	const int sigval = SIGSTOP;
711	pid_t child, wpid;
712#if defined(TWAIT_HAVE_STATUS)
713	int status;
714#endif
715	struct sigaction sa;
716	struct ptrace_siginfo info;
717	struct kinfo_proc2 kp;
718	size_t len = sizeof(kp);
719
720	int name[6];
721	const size_t namelen = __arraycount(name);
722	ki_sigset_t kp_sigignore;
723
724#ifndef PTRACE_ILLEGAL_ASM
725	if (sig == SIGILL)
726		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
727#endif
728
729	if (sig == SIGFPE && !are_fpu_exceptions_supported())
730		atf_tc_skip("FP exceptions are not supported");
731
732	memset(&info, 0, sizeof(info));
733
734	DPRINTF("Before forking process PID=%d\n", getpid());
735	SYSCALL_REQUIRE((child = fork()) != -1);
736	if (child == 0) {
737		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
738		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
739
740		memset(&sa, 0, sizeof(sa));
741		sa.sa_handler = SIG_IGN;
742		sigemptyset(&sa.sa_mask);
743
744		FORKEE_ASSERT(sigaction(sig, &sa, NULL) != -1);
745
746		DPRINTF("Before raising %s from child\n", strsignal(sigval));
747		FORKEE_ASSERT(raise(sigval) == 0);
748
749		DPRINTF("Before executing a trap\n");
750		switch (sig) {
751		case SIGTRAP:
752			trigger_trap();
753			break;
754		case SIGSEGV:
755			trigger_segv();
756			break;
757		case SIGILL:
758			trigger_ill();
759			break;
760		case SIGFPE:
761			trigger_fpe();
762			break;
763		case SIGBUS:
764			trigger_bus();
765			break;
766		default:
767			/* NOTREACHED */
768			FORKEE_ASSERTX(0 && "This shall not be reached");
769		}
770
771		/* NOTREACHED */
772		FORKEE_ASSERTX(0 && "This shall not be reached");
773	}
774	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
775
776	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
777	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
778
779	validate_status_stopped(status, sigval);
780
781	name[0] = CTL_KERN,
782	name[1] = KERN_PROC2,
783	name[2] = KERN_PROC_PID;
784	name[3] = child;
785	name[4] = sizeof(kp);
786	name[5] = 1;
787
788	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
789
790	kp_sigignore = kp.p_sigignore;
791
792	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
793	SYSCALL_REQUIRE(
794	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
795
796	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
797	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
798	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
799	    info.psi_siginfo.si_errno);
800
801	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
802	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
803
804	DPRINTF("Before resuming the child process where it left off and "
805	    "without signal to be sent\n");
806	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
807
808	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
809	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
810
811	validate_status_stopped(status, sig);
812
813	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child");
814	SYSCALL_REQUIRE(
815	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
816
817	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
818	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
819	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
820	    info.psi_siginfo.si_errno);
821
822	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
823
824	DPRINTF("kp_sigignore="
825	    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
826	    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
827	    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
828
829	DPRINTF("kp.p_sigignore="
830	    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
831	    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
832	    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
833
834	ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore, sizeof(kp_sigignore)));
835
836	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sig);
837	switch (sig) {
838	case SIGTRAP:
839		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_BRKPT);
840		break;
841	case SIGSEGV:
842		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SEGV_MAPERR);
843		break;
844	case SIGILL:
845		ATF_REQUIRE(info.psi_siginfo.si_code >= ILL_ILLOPC &&
846		            info.psi_siginfo.si_code <= ILL_BADSTK);
847		break;
848	case SIGFPE:
849		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, FPE_INTDIV);
850		break;
851	case SIGBUS:
852		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, BUS_ADRERR);
853		break;
854	}
855
856	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
857
858	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
859	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
860
861	validate_status_signaled(status, SIGKILL, 0);
862
863	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
864	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
865}
866
867#define TRACEME_SIGNALIGNORED_CRASH(test, sig)				\
868ATF_TC(test);								\
869ATF_TC_HEAD(test, tc)							\
870{									\
871	atf_tc_set_md_var(tc, "descr",					\
872	    "Verify ignored crash signal " #sig " in a child after "	\
873	    "PT_TRACE_ME is delivered to its tracer"); 			\
874}									\
875									\
876ATF_TC_BODY(test, tc)							\
877{									\
878									\
879	traceme_signalignored_crash(sig);				\
880}
881
882TRACEME_SIGNALIGNORED_CRASH(traceme_signalignored_crash_trap, SIGTRAP)
883TRACEME_SIGNALIGNORED_CRASH(traceme_signalignored_crash_segv, SIGSEGV)
884TRACEME_SIGNALIGNORED_CRASH(traceme_signalignored_crash_ill, SIGILL)
885TRACEME_SIGNALIGNORED_CRASH(traceme_signalignored_crash_fpe, SIGFPE)
886TRACEME_SIGNALIGNORED_CRASH(traceme_signalignored_crash_bus, SIGBUS)
887
888/// ----------------------------------------------------------------------------
889
890static void
891traceme_sendsignal_handle(int sigsent, void (*sah)(int a), int *traceme_caught)
892{
893	const int exitval = 5;
894	const int sigval = SIGSTOP;
895	pid_t child, wpid;
896	struct sigaction sa;
897#if defined(TWAIT_HAVE_STATUS)
898	int status;
899#endif
900	struct ptrace_siginfo info;
901
902	memset(&info, 0, sizeof(info));
903
904	DPRINTF("Before forking process PID=%d\n", getpid());
905	SYSCALL_REQUIRE((child = fork()) != -1);
906	if (child == 0) {
907		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
908		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
909
910		sa.sa_handler = sah;
911		sa.sa_flags = SA_SIGINFO;
912		sigemptyset(&sa.sa_mask);
913
914		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
915
916		DPRINTF("Before raising %s from child\n", strsignal(sigval));
917		FORKEE_ASSERT(raise(sigval) == 0);
918
919		FORKEE_ASSERT_EQ(*traceme_caught, 1);
920
921		DPRINTF("Before exiting of the child process\n");
922		_exit(exitval);
923	}
924	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
925
926	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
927	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
928
929	validate_status_stopped(status, sigval);
930
931	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
932	SYSCALL_REQUIRE(
933	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
934
935	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
936	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
937	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
938	    info.psi_siginfo.si_errno);
939
940	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
941	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
942
943	DPRINTF("Before resuming the child process where it left off and with "
944	    "signal %s to be sent\n", strsignal(sigsent));
945	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
946
947	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
948	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
949
950	validate_status_exited(status, exitval);
951
952	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
953	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
954}
955
956#define TRACEME_SENDSIGNAL_HANDLE(test, sig)				\
957ATF_TC(test);								\
958ATF_TC_HEAD(test, tc)							\
959{									\
960	atf_tc_set_md_var(tc, "descr",					\
961	    "Verify that a signal " #sig " emitted by a tracer to a child is " \
962	    "handled correctly and caught by a signal handler");	\
963}									\
964									\
965static int test##_caught = 0;						\
966									\
967static void								\
968test##_sighandler(int arg)						\
969{									\
970	FORKEE_ASSERT_EQ(arg, sig);					\
971									\
972	++ test##_caught;						\
973}									\
974									\
975ATF_TC_BODY(test, tc)							\
976{									\
977									\
978	traceme_sendsignal_handle(sig, test##_sighandler, & test##_caught); \
979}
980
981// A signal handler for SIGKILL and SIGSTOP cannot be registered.
982TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle1, SIGABRT) /* abort trap */
983TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle2, SIGHUP)  /* hangup */
984TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle3, SIGCONT) /* continued? */
985TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle4, SIGTRAP) /* crash sig. */
986TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle5, SIGBUS) /* crash sig. */
987TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle6, SIGILL) /* crash sig. */
988TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle7, SIGFPE) /* crash sig. */
989TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle8, SIGSEGV) /* crash sig. */
990
991/// ----------------------------------------------------------------------------
992
993static void
994traceme_sendsignal_masked(int sigsent)
995{
996	const int exitval = 5;
997	const int sigval = SIGSTOP;
998	pid_t child, wpid;
999	sigset_t set;
1000#if defined(TWAIT_HAVE_STATUS)
1001	int status;
1002#endif
1003	struct ptrace_siginfo info;
1004
1005	memset(&info, 0, sizeof(info));
1006
1007	DPRINTF("Before forking process PID=%d\n", getpid());
1008	SYSCALL_REQUIRE((child = fork()) != -1);
1009	if (child == 0) {
1010		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1011		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1012
1013		sigemptyset(&set);
1014		sigaddset(&set, sigsent);
1015		FORKEE_ASSERT(sigprocmask(SIG_BLOCK, &set, NULL) != -1);
1016
1017		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1018		FORKEE_ASSERT(raise(sigval) == 0);
1019
1020		_exit(exitval);
1021	}
1022	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1023
1024	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1025	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1026
1027	validate_status_stopped(status, sigval);
1028
1029	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
1030	SYSCALL_REQUIRE(
1031	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
1032
1033	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
1034	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
1035	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
1036	    info.psi_siginfo.si_errno);
1037
1038	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
1039	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
1040
1041	DPRINTF("Before resuming the child process where it left off and with "
1042	    "signal %s to be sent\n", strsignal(sigsent));
1043	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
1044
1045	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1046	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1047
1048	validate_status_exited(status, exitval);
1049
1050	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
1051	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1052}
1053
1054#define TRACEME_SENDSIGNAL_MASKED(test, sig)				\
1055ATF_TC(test);								\
1056ATF_TC_HEAD(test, tc)							\
1057{									\
1058	atf_tc_set_md_var(tc, "descr",					\
1059	    "Verify that a signal " #sig " emitted by a tracer to a child is " \
1060	    "handled correctly and the signal is masked by SIG_BLOCK");	\
1061}									\
1062									\
1063ATF_TC_BODY(test, tc)							\
1064{									\
1065									\
1066	traceme_sendsignal_masked(sig);					\
1067}
1068
1069// A signal handler for SIGKILL and SIGSTOP cannot be masked.
1070TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked1, SIGABRT) /* abort trap */
1071TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked2, SIGHUP)  /* hangup */
1072TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked3, SIGCONT) /* continued? */
1073TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked4, SIGTRAP) /* crash sig. */
1074TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked5, SIGBUS) /* crash sig. */
1075TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked6, SIGILL) /* crash sig. */
1076TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked7, SIGFPE) /* crash sig. */
1077TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked8, SIGSEGV) /* crash sig. */
1078
1079/// ----------------------------------------------------------------------------
1080
1081static void
1082traceme_sendsignal_ignored(int sigsent)
1083{
1084	const int exitval = 5;
1085	const int sigval = SIGSTOP;
1086	pid_t child, wpid;
1087	struct sigaction sa;
1088#if defined(TWAIT_HAVE_STATUS)
1089	int status;
1090#endif
1091	struct ptrace_siginfo info;
1092
1093	memset(&info, 0, sizeof(info));
1094
1095	DPRINTF("Before forking process PID=%d\n", getpid());
1096	SYSCALL_REQUIRE((child = fork()) != -1);
1097	if (child == 0) {
1098		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1099
1100		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1101
1102		memset(&sa, 0, sizeof(sa));
1103		sa.sa_handler = SIG_IGN;
1104		sigemptyset(&sa.sa_mask);
1105		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
1106
1107		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1108		FORKEE_ASSERT(raise(sigval) == 0);
1109
1110		_exit(exitval);
1111	}
1112	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1113
1114	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1115	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1116
1117	validate_status_stopped(status, sigval);
1118
1119	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
1120	SYSCALL_REQUIRE(
1121	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
1122
1123	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
1124	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
1125	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
1126	    info.psi_siginfo.si_errno);
1127
1128	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
1129	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
1130
1131	DPRINTF("Before resuming the child process where it left off and with "
1132	    "signal %s to be sent\n", strsignal(sigsent));
1133	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
1134
1135	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1136	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1137
1138	validate_status_exited(status, exitval);
1139
1140	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
1141	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1142}
1143
1144#define TRACEME_SENDSIGNAL_IGNORED(test, sig)				\
1145ATF_TC(test);								\
1146ATF_TC_HEAD(test, tc)							\
1147{									\
1148	atf_tc_set_md_var(tc, "descr",					\
1149	    "Verify that a signal " #sig " emitted by a tracer to a child is " \
1150	    "handled correctly and the signal is masked by SIG_IGN");	\
1151}									\
1152									\
1153ATF_TC_BODY(test, tc)							\
1154{									\
1155									\
1156	traceme_sendsignal_ignored(sig);				\
1157}
1158
1159// A signal handler for SIGKILL and SIGSTOP cannot be ignored.
1160TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored1, SIGABRT) /* abort */
1161TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored2, SIGHUP)  /* hangup */
1162TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored3, SIGCONT) /* continued */
1163TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored4, SIGTRAP) /* crash s. */
1164TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored5, SIGBUS) /* crash s. */
1165TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored6, SIGILL) /* crash s. */
1166TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored7, SIGFPE) /* crash s. */
1167TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored8, SIGSEGV) /* crash s. */
1168
1169/// ----------------------------------------------------------------------------
1170
1171static void
1172traceme_sendsignal_simple(int sigsent)
1173{
1174	const int sigval = SIGSTOP;
1175	int exitval = 0;
1176	pid_t child, wpid;
1177#if defined(TWAIT_HAVE_STATUS)
1178	int status;
1179	int expect_core;
1180
1181	switch (sigsent) {
1182	case SIGABRT:
1183	case SIGTRAP:
1184	case SIGBUS:
1185	case SIGILL:
1186	case SIGFPE:
1187	case SIGSEGV:
1188		expect_core = 1;
1189		break;
1190	default:
1191		expect_core = 0;
1192		break;
1193	}
1194#endif
1195	struct ptrace_siginfo info;
1196
1197	memset(&info, 0, sizeof(info));
1198
1199	DPRINTF("Before forking process PID=%d\n", getpid());
1200	SYSCALL_REQUIRE((child = fork()) != -1);
1201	if (child == 0) {
1202		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1203		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1204
1205		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1206		FORKEE_ASSERT(raise(sigval) == 0);
1207
1208		switch (sigsent) {
1209		case SIGCONT:
1210		case SIGSTOP:
1211			_exit(exitval);
1212		default:
1213			/* NOTREACHED */
1214			FORKEE_ASSERTX(0 && "This shall not be reached");
1215		}
1216	}
1217	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1218
1219	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1220	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1221
1222	validate_status_stopped(status, sigval);
1223
1224	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
1225	SYSCALL_REQUIRE(
1226	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
1227
1228	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
1229	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
1230	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
1231	    info.psi_siginfo.si_errno);
1232
1233	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
1234	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
1235
1236	DPRINTF("Before resuming the child process where it left off and with "
1237	    "signal %s to be sent\n", strsignal(sigsent));
1238	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
1239
1240	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1241	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1242
1243	switch (sigsent) {
1244	case SIGSTOP:
1245		validate_status_stopped(status, sigsent);
1246		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
1247		    "child\n");
1248		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
1249		    sizeof(info)) != -1);
1250
1251		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
1252		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
1253		    "si_errno=%#x\n",
1254		    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
1255		    info.psi_siginfo.si_errno);
1256
1257		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
1258		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
1259
1260		DPRINTF("Before resuming the child process where it left off "
1261		    "and with signal %s to be sent\n", strsignal(sigsent));
1262		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1263
1264		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1265		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1266		    child);
1267		/* FALLTHROUGH */
1268	case SIGCONT:
1269		validate_status_exited(status, exitval);
1270		break;
1271	default:
1272		validate_status_signaled(status, sigsent, expect_core);
1273		break;
1274	}
1275
1276	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
1277	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1278}
1279
1280#define TRACEME_SENDSIGNAL_SIMPLE(test, sig)				\
1281ATF_TC(test);								\
1282ATF_TC_HEAD(test, tc)							\
1283{									\
1284	atf_tc_set_md_var(tc, "descr",					\
1285	    "Verify that a signal " #sig " emitted by a tracer to a child is " \
1286	    "handled correctly in a child without a signal handler");	\
1287}									\
1288									\
1289ATF_TC_BODY(test, tc)							\
1290{									\
1291									\
1292	traceme_sendsignal_simple(sig);					\
1293}
1294
1295TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple1, SIGKILL) /* non-maskable*/
1296TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple2, SIGSTOP) /* non-maskable*/
1297TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple3, SIGABRT) /* abort trap */
1298TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple4, SIGHUP)  /* hangup */
1299TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple5, SIGCONT) /* continued? */
1300TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple6, SIGTRAP) /* crash sig. */
1301TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple7, SIGBUS) /* crash sig. */
1302TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple8, SIGILL) /* crash sig. */
1303TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple9, SIGFPE) /* crash sig. */
1304TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple10, SIGSEGV) /* crash sig. */
1305
1306/// ----------------------------------------------------------------------------
1307
1308ATF_TC(traceme_pid1_parent);
1309ATF_TC_HEAD(traceme_pid1_parent, tc)
1310{
1311	atf_tc_set_md_var(tc, "descr",
1312	    "Verify that PT_TRACE_ME is not allowed when our parent is PID1");
1313}
1314
1315ATF_TC_BODY(traceme_pid1_parent, tc)
1316{
1317	struct msg_fds parent_child;
1318	int exitval_child1 = 1, exitval_child2 = 2;
1319	pid_t child1, child2, wpid;
1320	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1321#if defined(TWAIT_HAVE_STATUS)
1322	int status;
1323#endif
1324
1325	SYSCALL_REQUIRE(msg_open(&parent_child) == 0);
1326
1327	DPRINTF("Before forking process PID=%d\n", getpid());
1328	SYSCALL_REQUIRE((child1 = fork()) != -1);
1329	if (child1 == 0) {
1330		DPRINTF("Before forking process PID=%d\n", getpid());
1331		SYSCALL_REQUIRE((child2 = fork()) != -1);
1332		if (child2 != 0) {
1333			DPRINTF("Parent process PID=%d, child2's PID=%d\n",
1334			    getpid(), child2);
1335			_exit(exitval_child1);
1336		}
1337		CHILD_FROM_PARENT("exit child1", parent_child, msg);
1338
1339		DPRINTF("Assert that our parent is PID1 (initproc)\n");
1340		FORKEE_ASSERT_EQ(getppid(), 1);
1341
1342		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1343		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) == -1);
1344		SYSCALL_REQUIRE_ERRNO(errno, EPERM);
1345
1346		CHILD_TO_PARENT("child2 exiting", parent_child, msg);
1347
1348		_exit(exitval_child2);
1349	}
1350	DPRINTF("Parent process PID=%d, child1's PID=%d\n", getpid(), child1);
1351
1352	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1353	TWAIT_REQUIRE_SUCCESS(
1354	    wpid = TWAIT_GENERIC(child1, &status, WEXITED), child1);
1355
1356	validate_status_exited(status, exitval_child1);
1357
1358	DPRINTF("Notify that child1 is dead\n");
1359	PARENT_TO_CHILD("exit child1", parent_child, msg);
1360
1361	DPRINTF("Wait for exiting of child2\n");
1362	PARENT_FROM_CHILD("child2 exiting", parent_child, msg);
1363}
1364
1365/// ----------------------------------------------------------------------------
1366
1367static void
1368traceme_vfork_raise(int sigval)
1369{
1370	const int exitval = 5, exitval_watcher = 10;
1371	pid_t child, parent, watcher, wpid;
1372	int rv;
1373#if defined(TWAIT_HAVE_STATUS)
1374	int status;
1375
1376	/* volatile workarounds GCC -Werror=clobbered */
1377	volatile int expect_core;
1378
1379	switch (sigval) {
1380	case SIGABRT:
1381	case SIGTRAP:
1382	case SIGBUS:
1383	case SIGILL:
1384	case SIGFPE:
1385	case SIGSEGV:
1386		expect_core = 1;
1387		break;
1388	default:
1389		expect_core = 0;
1390		break;
1391	}
1392#endif
1393
1394	/*
1395	 * Spawn a dedicated thread to watch for a stopped child and emit
1396	 * the SIGKILL signal to it.
1397	 *
1398	 * vfork(2) might clobber watcher, this means that it's safer and
1399	 * simpler to reparent this process to initproc and forget about it.
1400	 */
1401	if (sigval == SIGSTOP) {
1402		parent = getpid();
1403
1404		watcher = fork();
1405		ATF_REQUIRE(watcher != 1);
1406		if (watcher == 0) {
1407			/* Double fork(2) trick to reparent to initproc */
1408			watcher = fork();
1409			FORKEE_ASSERT_NEQ(watcher, -1);
1410			if (watcher != 0)
1411				_exit(exitval_watcher);
1412
1413			child = await_stopped_child(parent);
1414
1415			errno = 0;
1416			rv = kill(child, SIGKILL);
1417			FORKEE_ASSERT_EQ(rv, 0);
1418			FORKEE_ASSERT_EQ(errno, 0);
1419
1420			/* This exit value will be collected by initproc */
1421			_exit(0);
1422		}
1423		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1424		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(watcher, &status, 0),
1425		    watcher);
1426
1427		validate_status_exited(status, exitval_watcher);
1428
1429		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1430		TWAIT_REQUIRE_FAILURE(ECHILD,
1431		    wpid = TWAIT_GENERIC(watcher, &status, 0));
1432	}
1433
1434	DPRINTF("Before forking process PID=%d\n", getpid());
1435	SYSCALL_REQUIRE((child = vfork()) != -1);
1436	if (child == 0) {
1437		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1438		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1439
1440		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1441		FORKEE_ASSERT(raise(sigval) == 0);
1442
1443		switch (sigval) {
1444		case SIGSTOP:
1445		case SIGKILL:
1446		case SIGABRT:
1447		case SIGHUP:
1448		case SIGTRAP:
1449		case SIGBUS:
1450		case SIGILL:
1451		case SIGFPE:
1452		case SIGSEGV:
1453			/* NOTREACHED */
1454			FORKEE_ASSERTX(0 && "This shall not be reached");
1455			__unreachable();
1456		default:
1457			DPRINTF("Before exiting of the child process\n");
1458			_exit(exitval);
1459		}
1460	}
1461	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1462
1463	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1464	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1465
1466	switch (sigval) {
1467	case SIGKILL:
1468	case SIGABRT:
1469	case SIGHUP:
1470	case SIGTRAP:
1471	case SIGBUS:
1472	case SIGILL:
1473	case SIGFPE:
1474	case SIGSEGV:
1475		validate_status_signaled(status, sigval, expect_core);
1476		break;
1477	case SIGSTOP:
1478		validate_status_signaled(status, SIGKILL, 0);
1479		break;
1480	case SIGCONT:
1481	case SIGTSTP:
1482	case SIGTTIN:
1483	case SIGTTOU:
1484		validate_status_exited(status, exitval);
1485		break;
1486	default:
1487		/* NOTREACHED */
1488		ATF_REQUIRE(0 && "NOT IMPLEMENTED");
1489		break;
1490	}
1491
1492	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1493	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1494}
1495
1496#define TRACEME_VFORK_RAISE(test, sig)					\
1497ATF_TC(test);								\
1498ATF_TC_HEAD(test, tc)							\
1499{									\
1500	atf_tc_set_md_var(tc, "descr",					\
1501	    "Verify PT_TRACE_ME followed by raise of " #sig " in a "	\
1502	    "vfork(2)ed child");					\
1503}									\
1504									\
1505ATF_TC_BODY(test, tc)							\
1506{									\
1507									\
1508	traceme_vfork_raise(sig);					\
1509}
1510
1511TRACEME_VFORK_RAISE(traceme_vfork_raise1, SIGKILL) /* non-maskable */
1512TRACEME_VFORK_RAISE(traceme_vfork_raise2, SIGSTOP) /* non-maskable */
1513TRACEME_VFORK_RAISE(traceme_vfork_raise3, SIGTSTP) /* ignored in vfork(2) */
1514TRACEME_VFORK_RAISE(traceme_vfork_raise4, SIGTTIN) /* ignored in vfork(2) */
1515TRACEME_VFORK_RAISE(traceme_vfork_raise5, SIGTTOU) /* ignored in vfork(2) */
1516TRACEME_VFORK_RAISE(traceme_vfork_raise6, SIGABRT) /* regular abort trap */
1517TRACEME_VFORK_RAISE(traceme_vfork_raise7, SIGHUP)  /* hangup */
1518TRACEME_VFORK_RAISE(traceme_vfork_raise8, SIGCONT) /* continued? */
1519TRACEME_VFORK_RAISE(traceme_vfork_raise9, SIGTRAP) /* crash signal */
1520TRACEME_VFORK_RAISE(traceme_vfork_raise10, SIGBUS) /* crash signal */
1521TRACEME_VFORK_RAISE(traceme_vfork_raise11, SIGILL) /* crash signal */
1522TRACEME_VFORK_RAISE(traceme_vfork_raise12, SIGFPE) /* crash signal */
1523TRACEME_VFORK_RAISE(traceme_vfork_raise13, SIGSEGV) /* crash signal */
1524
1525/// ----------------------------------------------------------------------------
1526
1527static void
1528traceme_vfork_crash(int sig)
1529{
1530	pid_t child, wpid;
1531#if defined(TWAIT_HAVE_STATUS)
1532	int status;
1533#endif
1534
1535#ifndef PTRACE_ILLEGAL_ASM
1536	if (sig == SIGILL)
1537		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
1538#endif
1539
1540	if (sig == SIGFPE && !are_fpu_exceptions_supported())
1541		atf_tc_skip("FP exceptions are not supported");
1542
1543	DPRINTF("Before forking process PID=%d\n", getpid());
1544	SYSCALL_REQUIRE((child = vfork()) != -1);
1545	if (child == 0) {
1546		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1547		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1548
1549		DPRINTF("Before executing a trap\n");
1550		switch (sig) {
1551		case SIGTRAP:
1552			trigger_trap();
1553			break;
1554		case SIGSEGV:
1555			trigger_segv();
1556			break;
1557		case SIGILL:
1558			trigger_ill();
1559			break;
1560		case SIGFPE:
1561			trigger_fpe();
1562			break;
1563		case SIGBUS:
1564			trigger_bus();
1565			break;
1566		default:
1567			/* NOTREACHED */
1568			FORKEE_ASSERTX(0 && "This shall not be reached");
1569		}
1570
1571		/* NOTREACHED */
1572		FORKEE_ASSERTX(0 && "This shall not be reached");
1573	}
1574	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1575
1576	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1577	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1578
1579	validate_status_signaled(status, sig, 1);
1580
1581	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1582	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1583}
1584
1585#define TRACEME_VFORK_CRASH(test, sig)					\
1586ATF_TC(test);								\
1587ATF_TC_HEAD(test, tc)							\
1588{									\
1589	atf_tc_set_md_var(tc, "descr",					\
1590	    "Verify PT_TRACE_ME followed by a crash signal " #sig " in a " \
1591	    "vfork(2)ed child");					\
1592}									\
1593									\
1594ATF_TC_BODY(test, tc)							\
1595{									\
1596									\
1597	traceme_vfork_crash(sig);					\
1598}
1599
1600TRACEME_VFORK_CRASH(traceme_vfork_crash_trap, SIGTRAP)
1601TRACEME_VFORK_CRASH(traceme_vfork_crash_segv, SIGSEGV)
1602TRACEME_VFORK_CRASH(traceme_vfork_crash_ill, SIGILL)
1603TRACEME_VFORK_CRASH(traceme_vfork_crash_fpe, SIGFPE)
1604TRACEME_VFORK_CRASH(traceme_vfork_crash_bus, SIGBUS)
1605
1606/// ----------------------------------------------------------------------------
1607
1608static void
1609traceme_vfork_signalmasked_crash(int sig)
1610{
1611	pid_t child, wpid;
1612#if defined(TWAIT_HAVE_STATUS)
1613	int status;
1614#endif
1615	sigset_t intmask;
1616
1617#ifndef PTRACE_ILLEGAL_ASM
1618	if (sig == SIGILL)
1619		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
1620#endif
1621
1622	if (sig == SIGFPE && !are_fpu_exceptions_supported())
1623		atf_tc_skip("FP exceptions are not supported");
1624
1625	DPRINTF("Before forking process PID=%d\n", getpid());
1626	SYSCALL_REQUIRE((child = vfork()) != -1);
1627	if (child == 0) {
1628		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1629		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1630
1631		sigemptyset(&intmask);
1632		sigaddset(&intmask, sig);
1633		sigprocmask(SIG_BLOCK, &intmask, NULL);
1634
1635		DPRINTF("Before executing a trap\n");
1636		switch (sig) {
1637		case SIGTRAP:
1638			trigger_trap();
1639			break;
1640		case SIGSEGV:
1641			trigger_segv();
1642			break;
1643		case SIGILL:
1644			trigger_ill();
1645			break;
1646		case SIGFPE:
1647			trigger_fpe();
1648			break;
1649		case SIGBUS:
1650			trigger_bus();
1651			break;
1652		default:
1653			/* NOTREACHED */
1654			FORKEE_ASSERTX(0 && "This shall not be reached");
1655		}
1656
1657		/* NOTREACHED */
1658		FORKEE_ASSERTX(0 && "This shall not be reached");
1659	}
1660	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1661
1662	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1663	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1664
1665	validate_status_signaled(status, sig, 1);
1666
1667	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1668	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1669}
1670
1671#define TRACEME_VFORK_SIGNALMASKED_CRASH(test, sig)			\
1672ATF_TC(test);								\
1673ATF_TC_HEAD(test, tc)							\
1674{									\
1675	atf_tc_set_md_var(tc, "descr",					\
1676	    "Verify PT_TRACE_ME followed by a crash signal " #sig " in a " \
1677	    "vfork(2)ed child with a masked signal");			\
1678}									\
1679									\
1680ATF_TC_BODY(test, tc)							\
1681{									\
1682									\
1683	traceme_vfork_signalmasked_crash(sig);				\
1684}
1685
1686TRACEME_VFORK_SIGNALMASKED_CRASH(traceme_vfork_signalmasked_crash_trap, SIGTRAP)
1687TRACEME_VFORK_SIGNALMASKED_CRASH(traceme_vfork_signalmasked_crash_segv, SIGSEGV)
1688TRACEME_VFORK_SIGNALMASKED_CRASH(traceme_vfork_signalmasked_crash_ill, SIGILL)
1689TRACEME_VFORK_SIGNALMASKED_CRASH(traceme_vfork_signalmasked_crash_fpe, SIGFPE)
1690TRACEME_VFORK_SIGNALMASKED_CRASH(traceme_vfork_signalmasked_crash_bus, SIGBUS)
1691
1692/// ----------------------------------------------------------------------------
1693
1694static void
1695traceme_vfork_signalignored_crash(int sig)
1696{
1697	pid_t child, wpid;
1698#if defined(TWAIT_HAVE_STATUS)
1699	int status;
1700#endif
1701	struct sigaction sa;
1702
1703#ifndef PTRACE_ILLEGAL_ASM
1704	if (sig == SIGILL)
1705		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
1706#endif
1707
1708	if (sig == SIGFPE && !are_fpu_exceptions_supported())
1709		atf_tc_skip("FP exceptions are not supported");
1710
1711	DPRINTF("Before forking process PID=%d\n", getpid());
1712	SYSCALL_REQUIRE((child = vfork()) != -1);
1713	if (child == 0) {
1714		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1715		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1716
1717		memset(&sa, 0, sizeof(sa));
1718		sa.sa_handler = SIG_IGN;
1719		sigemptyset(&sa.sa_mask);
1720
1721		FORKEE_ASSERT(sigaction(sig, &sa, NULL) != -1);
1722
1723		DPRINTF("Before executing a trap\n");
1724		switch (sig) {
1725		case SIGTRAP:
1726			trigger_trap();
1727			break;
1728		case SIGSEGV:
1729			trigger_segv();
1730			break;
1731		case SIGILL:
1732			trigger_ill();
1733			break;
1734		case SIGFPE:
1735			trigger_fpe();
1736			break;
1737		case SIGBUS:
1738			trigger_bus();
1739			break;
1740		default:
1741			/* NOTREACHED */
1742			FORKEE_ASSERTX(0 && "This shall not be reached");
1743		}
1744
1745		/* NOTREACHED */
1746		FORKEE_ASSERTX(0 && "This shall not be reached");
1747	}
1748	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1749
1750	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1751	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1752
1753	validate_status_signaled(status, sig, 1);
1754
1755	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1756	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1757}
1758
1759#define TRACEME_VFORK_SIGNALIGNORED_CRASH(test, sig)			\
1760ATF_TC(test);								\
1761ATF_TC_HEAD(test, tc)							\
1762{									\
1763	atf_tc_set_md_var(tc, "descr",					\
1764	    "Verify PT_TRACE_ME followed by a crash signal " #sig " in a " \
1765	    "vfork(2)ed child with ignored signal");			\
1766}									\
1767									\
1768ATF_TC_BODY(test, tc)							\
1769{									\
1770									\
1771	traceme_vfork_signalignored_crash(sig);				\
1772}
1773
1774TRACEME_VFORK_SIGNALIGNORED_CRASH(traceme_vfork_signalignored_crash_trap,
1775    SIGTRAP)
1776TRACEME_VFORK_SIGNALIGNORED_CRASH(traceme_vfork_signalignored_crash_segv,
1777    SIGSEGV)
1778TRACEME_VFORK_SIGNALIGNORED_CRASH(traceme_vfork_signalignored_crash_ill,
1779    SIGILL)
1780TRACEME_VFORK_SIGNALIGNORED_CRASH(traceme_vfork_signalignored_crash_fpe,
1781    SIGFPE)
1782TRACEME_VFORK_SIGNALIGNORED_CRASH(traceme_vfork_signalignored_crash_bus,
1783    SIGBUS)
1784
1785/// ----------------------------------------------------------------------------
1786
1787static void
1788traceme_vfork_exec(bool masked, bool ignored)
1789{
1790	const int sigval = SIGTRAP;
1791	pid_t child, wpid;
1792#if defined(TWAIT_HAVE_STATUS)
1793	int status;
1794#endif
1795	struct sigaction sa;
1796	struct ptrace_siginfo info;
1797	sigset_t intmask;
1798	struct kinfo_proc2 kp;
1799	size_t len = sizeof(kp);
1800
1801	int name[6];
1802	const size_t namelen = __arraycount(name);
1803	ki_sigset_t kp_sigmask;
1804	ki_sigset_t kp_sigignore;
1805
1806	memset(&info, 0, sizeof(info));
1807
1808	DPRINTF("Before forking process PID=%d\n", getpid());
1809	SYSCALL_REQUIRE((child = vfork()) != -1);
1810	if (child == 0) {
1811		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1812		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1813
1814		if (masked) {
1815			sigemptyset(&intmask);
1816			sigaddset(&intmask, sigval);
1817			sigprocmask(SIG_BLOCK, &intmask, NULL);
1818		}
1819
1820		if (ignored) {
1821			memset(&sa, 0, sizeof(sa));
1822			sa.sa_handler = SIG_IGN;
1823			sigemptyset(&sa.sa_mask);
1824			FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
1825		}
1826
1827		DPRINTF("Before calling execve(2) from child\n");
1828		execlp("/bin/echo", "/bin/echo", NULL);
1829
1830		/* NOTREACHED */
1831		FORKEE_ASSERTX(0 && "Not reached");
1832	}
1833	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1834
1835	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1836	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1837
1838	validate_status_stopped(status, sigval);
1839
1840	name[0] = CTL_KERN,
1841	name[1] = KERN_PROC2,
1842	name[2] = KERN_PROC_PID;
1843	name[3] = getpid();
1844	name[4] = sizeof(kp);
1845	name[5] = 1;
1846
1847	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
1848
1849	if (masked)
1850		kp_sigmask = kp.p_sigmask;
1851
1852	if (ignored)
1853		kp_sigignore = kp.p_sigignore;
1854
1855	name[3] = getpid();
1856
1857	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
1858
1859	if (masked) {
1860		DPRINTF("kp_sigmask="
1861		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
1862		    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
1863		    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
1864
1865	        DPRINTF("kp.p_sigmask="
1866	            "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
1867	            kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
1868	            kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
1869
1870		ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
1871		    sizeof(kp_sigmask)));
1872	}
1873
1874	if (ignored) {
1875		DPRINTF("kp_sigignore="
1876		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
1877		    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
1878		    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
1879
1880	        DPRINTF("kp.p_sigignore="
1881	            "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
1882	            kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
1883	            kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
1884
1885		ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
1886		    sizeof(kp_sigignore)));
1887	}
1888
1889	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
1890	SYSCALL_REQUIRE(
1891	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
1892
1893	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
1894	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
1895	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
1896	    info.psi_siginfo.si_errno);
1897
1898	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
1899	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
1900
1901	DPRINTF("Before resuming the child process where it left off and "
1902	    "without signal to be sent\n");
1903	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1904
1905	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1906	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1907
1908	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1909	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1910}
1911
1912#define TRACEME_VFORK_EXEC(test, masked, ignored)			\
1913ATF_TC(test);								\
1914ATF_TC_HEAD(test, tc)							\
1915{									\
1916	atf_tc_set_md_var(tc, "descr",					\
1917	    "Verify PT_TRACE_ME followed by exec(3) in a vfork(2)ed "	\
1918	    "child%s%s", masked ? " with masked signal" : "",		\
1919	    masked ? " with ignored signal" : "");			\
1920}									\
1921									\
1922ATF_TC_BODY(test, tc)							\
1923{									\
1924									\
1925	traceme_vfork_exec(masked, ignored);				\
1926}
1927
1928TRACEME_VFORK_EXEC(traceme_vfork_exec, false, false)
1929TRACEME_VFORK_EXEC(traceme_vfork_signalmasked_exec, true, false)
1930TRACEME_VFORK_EXEC(traceme_vfork_signalignored_exec, false, true)
1931
1932/// ----------------------------------------------------------------------------
1933
1934#if defined(TWAIT_HAVE_PID)
1935static void
1936unrelated_tracer_sees_crash(int sig, bool masked, bool ignored)
1937{
1938	const int sigval = SIGSTOP;
1939	struct msg_fds parent_tracee, parent_tracer;
1940	const int exitval = 10;
1941	pid_t tracee, tracer, wpid;
1942	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1943#if defined(TWAIT_HAVE_STATUS)
1944	int status;
1945#endif
1946	struct sigaction sa;
1947	struct ptrace_siginfo info;
1948	sigset_t intmask;
1949	struct kinfo_proc2 kp;
1950	size_t len = sizeof(kp);
1951
1952	int name[6];
1953	const size_t namelen = __arraycount(name);
1954	ki_sigset_t kp_sigmask;
1955	ki_sigset_t kp_sigignore;
1956
1957#ifndef PTRACE_ILLEGAL_ASM
1958	if (sig == SIGILL)
1959		atf_tc_skip("PTRACE_ILLEGAL_ASM not defined");
1960#endif
1961
1962	if (sig == SIGFPE && !are_fpu_exceptions_supported())
1963		atf_tc_skip("FP exceptions are not supported");
1964
1965	memset(&info, 0, sizeof(info));
1966
1967	DPRINTF("Spawn tracee\n");
1968	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1969	tracee = atf_utils_fork();
1970	if (tracee == 0) {
1971		// Wait for parent to let us crash
1972		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
1973
1974		if (masked) {
1975			sigemptyset(&intmask);
1976			sigaddset(&intmask, sig);
1977			sigprocmask(SIG_BLOCK, &intmask, NULL);
1978		}
1979
1980		if (ignored) {
1981			memset(&sa, 0, sizeof(sa));
1982			sa.sa_handler = SIG_IGN;
1983			sigemptyset(&sa.sa_mask);
1984			FORKEE_ASSERT(sigaction(sig, &sa, NULL) != -1);
1985		}
1986
1987		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1988		FORKEE_ASSERT(raise(sigval) == 0);
1989
1990		DPRINTF("Before executing a trap\n");
1991		switch (sig) {
1992		case SIGTRAP:
1993			trigger_trap();
1994			break;
1995		case SIGSEGV:
1996			trigger_segv();
1997			break;
1998		case SIGILL:
1999			trigger_ill();
2000			break;
2001		case SIGFPE:
2002			trigger_fpe();
2003			break;
2004		case SIGBUS:
2005			trigger_bus();
2006			break;
2007		default:
2008			/* NOTREACHED */
2009			FORKEE_ASSERTX(0 && "This shall not be reached");
2010		}
2011
2012		/* NOTREACHED */
2013		FORKEE_ASSERTX(0 && "This shall not be reached");
2014	}
2015
2016	DPRINTF("Spawn debugger\n");
2017	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
2018	tracer = atf_utils_fork();
2019	if (tracer == 0) {
2020		/* Fork again and drop parent to reattach to PID 1 */
2021		tracer = atf_utils_fork();
2022		if (tracer != 0)
2023			_exit(exitval);
2024
2025		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
2026		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
2027
2028		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
2029		FORKEE_REQUIRE_SUCCESS(
2030		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2031
2032		forkee_status_stopped(status, SIGSTOP);
2033
2034		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the "
2035		    "traced process\n");
2036		SYSCALL_REQUIRE(
2037		    ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
2038
2039		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
2040		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
2041		    "si_errno=%#x\n", info.psi_siginfo.si_signo,
2042		    info.psi_siginfo.si_code, info.psi_siginfo.si_errno);
2043
2044		FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, SIGSTOP);
2045		FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_USER);
2046
2047		/* Resume tracee with PT_CONTINUE */
2048		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2049
2050		/* Inform parent that tracer has attached to tracee */
2051		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
2052
2053		/* Wait for parent to tell use that tracee should have exited */
2054		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
2055
2056		/* Wait for tracee and assert that it exited */
2057		FORKEE_REQUIRE_SUCCESS(
2058		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2059
2060		forkee_status_stopped(status, sigval);
2061
2062		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the "
2063		    "traced process\n");
2064		SYSCALL_REQUIRE(
2065		    ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
2066
2067		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
2068		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
2069		    "si_errno=%#x\n", info.psi_siginfo.si_signo,
2070		    info.psi_siginfo.si_code, info.psi_siginfo.si_errno);
2071
2072		FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, sigval);
2073		FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_LWP);
2074
2075		name[0] = CTL_KERN,
2076		name[1] = KERN_PROC2,
2077		name[2] = KERN_PROC_PID;
2078		name[3] = tracee;
2079		name[4] = sizeof(kp);
2080		name[5] = 1;
2081
2082		FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
2083
2084		if (masked)
2085			kp_sigmask = kp.p_sigmask;
2086
2087		if (ignored)
2088			kp_sigignore = kp.p_sigignore;
2089
2090		/* Resume tracee with PT_CONTINUE */
2091		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2092
2093		/* Wait for tracee and assert that it exited */
2094		FORKEE_REQUIRE_SUCCESS(
2095		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2096
2097		forkee_status_stopped(status, sig);
2098
2099		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the "
2100		    "traced process\n");
2101		SYSCALL_REQUIRE(
2102		    ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
2103
2104		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
2105		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
2106		    "si_errno=%#x\n", info.psi_siginfo.si_signo,
2107		    info.psi_siginfo.si_code, info.psi_siginfo.si_errno);
2108
2109		FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, sig);
2110
2111		FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
2112
2113		if (masked) {
2114			DPRINTF("kp_sigmask="
2115			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
2116			    PRIx32 "\n",
2117			    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
2118			    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
2119
2120			DPRINTF("kp.p_sigmask="
2121			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
2122			    PRIx32 "\n",
2123			    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
2124			    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
2125
2126			FORKEE_ASSERTX(!memcmp(&kp_sigmask, &kp.p_sigmask,
2127			    sizeof(kp_sigmask)));
2128		}
2129
2130		if (ignored) {
2131			DPRINTF("kp_sigignore="
2132			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
2133			    PRIx32 "\n",
2134			    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
2135			    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
2136
2137			DPRINTF("kp.p_sigignore="
2138			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
2139			    PRIx32 "\n",
2140			    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
2141			    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
2142
2143			FORKEE_ASSERTX(!memcmp(&kp_sigignore, &kp.p_sigignore,
2144			    sizeof(kp_sigignore)));
2145		}
2146
2147		switch (sig) {
2148		case SIGTRAP:
2149			FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, TRAP_BRKPT);
2150			break;
2151		case SIGSEGV:
2152			FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SEGV_MAPERR);
2153			break;
2154		case SIGILL:
2155			FORKEE_ASSERT(info.psi_siginfo.si_code >= ILL_ILLOPC &&
2156			            info.psi_siginfo.si_code <= ILL_BADSTK);
2157			break;
2158		case SIGFPE:
2159			FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, FPE_INTDIV);
2160			break;
2161		case SIGBUS:
2162			FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, BUS_ADRERR);
2163			break;
2164		}
2165
2166		FORKEE_ASSERT(ptrace(PT_KILL, tracee, NULL, 0) != -1);
2167		DPRINTF("Before calling %s() for the tracee\n", TWAIT_FNAME);
2168		FORKEE_REQUIRE_SUCCESS(
2169		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2170
2171		forkee_status_signaled(status, SIGKILL, 0);
2172
2173		/* Inform parent that tracer is exiting normally */
2174		CHILD_TO_PARENT("tracer done", parent_tracer, msg);
2175
2176		DPRINTF("Before exiting of the tracer process\n");
2177		_exit(0 /* collect by initproc */);
2178	}
2179
2180	DPRINTF("Wait for the tracer process (direct child) to exit "
2181	    "calling %s()\n", TWAIT_FNAME);
2182	TWAIT_REQUIRE_SUCCESS(
2183	    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
2184
2185	validate_status_exited(status, exitval);
2186
2187	DPRINTF("Wait for the non-exited tracee process with %s()\n",
2188	    TWAIT_FNAME);
2189	TWAIT_REQUIRE_SUCCESS(
2190	    wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
2191
2192	DPRINTF("Wait for the tracer to attach to the tracee\n");
2193	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
2194
2195	DPRINTF("Resume the tracee and let it crash\n");
2196	PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
2197
2198	DPRINTF("Resume the tracer and let it detect crashed tracee\n");
2199	PARENT_TO_CHILD("Message 2", parent_tracer, msg);
2200
2201	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
2202	    TWAIT_FNAME);
2203	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2204
2205	validate_status_signaled(status, SIGKILL, 0);
2206
2207	DPRINTF("Await normal exit of tracer\n");
2208	PARENT_FROM_CHILD("tracer done", parent_tracer, msg);
2209
2210	msg_close(&parent_tracer);
2211	msg_close(&parent_tracee);
2212}
2213
2214#define UNRELATED_TRACER_SEES_CRASH(test, sig)				\
2215ATF_TC(test);								\
2216ATF_TC_HEAD(test, tc)							\
2217{									\
2218	atf_tc_set_md_var(tc, "descr",					\
2219	    "Assert that an unrelated tracer sees crash signal from "	\
2220	    "the debuggee");						\
2221}									\
2222									\
2223ATF_TC_BODY(test, tc)							\
2224{									\
2225									\
2226	unrelated_tracer_sees_crash(sig, false, false);			\
2227}
2228
2229UNRELATED_TRACER_SEES_CRASH(unrelated_tracer_sees_crash_trap, SIGTRAP)
2230UNRELATED_TRACER_SEES_CRASH(unrelated_tracer_sees_crash_segv, SIGSEGV)
2231UNRELATED_TRACER_SEES_CRASH(unrelated_tracer_sees_crash_ill, SIGILL)
2232UNRELATED_TRACER_SEES_CRASH(unrelated_tracer_sees_crash_fpe, SIGFPE)
2233UNRELATED_TRACER_SEES_CRASH(unrelated_tracer_sees_crash_bus, SIGBUS)
2234
2235#define UNRELATED_TRACER_SEES_SIGNALMASKED_CRASH(test, sig)		\
2236ATF_TC(test);								\
2237ATF_TC_HEAD(test, tc)							\
2238{									\
2239	atf_tc_set_md_var(tc, "descr",					\
2240	    "Assert that an unrelated tracer sees crash signal from "	\
2241	    "the debuggee with masked signal");				\
2242}									\
2243									\
2244ATF_TC_BODY(test, tc)							\
2245{									\
2246									\
2247	unrelated_tracer_sees_crash(sig, true, false);			\
2248}
2249
2250UNRELATED_TRACER_SEES_SIGNALMASKED_CRASH(
2251    unrelated_tracer_sees_signalmasked_crash_trap, SIGTRAP)
2252UNRELATED_TRACER_SEES_SIGNALMASKED_CRASH(
2253    unrelated_tracer_sees_signalmasked_crash_segv, SIGSEGV)
2254UNRELATED_TRACER_SEES_SIGNALMASKED_CRASH(
2255    unrelated_tracer_sees_signalmasked_crash_ill, SIGILL)
2256UNRELATED_TRACER_SEES_SIGNALMASKED_CRASH(
2257    unrelated_tracer_sees_signalmasked_crash_fpe, SIGFPE)
2258UNRELATED_TRACER_SEES_SIGNALMASKED_CRASH(
2259    unrelated_tracer_sees_signalmasked_crash_bus, SIGBUS)
2260
2261#define UNRELATED_TRACER_SEES_SIGNALIGNORED_CRASH(test, sig)		\
2262ATF_TC(test);								\
2263ATF_TC_HEAD(test, tc)							\
2264{									\
2265	atf_tc_set_md_var(tc, "descr",					\
2266	    "Assert that an unrelated tracer sees crash signal from "	\
2267	    "the debuggee with signal ignored");			\
2268}									\
2269									\
2270ATF_TC_BODY(test, tc)							\
2271{									\
2272									\
2273	unrelated_tracer_sees_crash(sig, false, true);			\
2274}
2275
2276UNRELATED_TRACER_SEES_SIGNALIGNORED_CRASH(
2277    unrelated_tracer_sees_signalignored_crash_trap, SIGTRAP)
2278UNRELATED_TRACER_SEES_SIGNALIGNORED_CRASH(
2279    unrelated_tracer_sees_signalignored_crash_segv, SIGSEGV)
2280UNRELATED_TRACER_SEES_SIGNALIGNORED_CRASH(
2281    unrelated_tracer_sees_signalignored_crash_ill, SIGILL)
2282UNRELATED_TRACER_SEES_SIGNALIGNORED_CRASH(
2283    unrelated_tracer_sees_signalignored_crash_fpe, SIGFPE)
2284UNRELATED_TRACER_SEES_SIGNALIGNORED_CRASH(
2285    unrelated_tracer_sees_signalignored_crash_bus, SIGBUS)
2286#endif
2287
2288/// ----------------------------------------------------------------------------
2289
2290#if defined(TWAIT_HAVE_PID)
2291static void
2292tracer_sees_terminaton_before_the_parent_raw(bool notimeout, bool unrelated,
2293                                             bool stopped)
2294{
2295	/*
2296	 * notimeout - disable timeout in await zombie function
2297	 * unrelated - attach from unrelated tracer reparented to initproc
2298	 * stopped - attach to a stopped process
2299	 */
2300
2301	struct msg_fds parent_tracee, parent_tracer;
2302	const int exitval_tracee = 5;
2303	const int exitval_tracer = 10;
2304	pid_t tracee, tracer, wpid;
2305	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
2306#if defined(TWAIT_HAVE_STATUS)
2307	int status;
2308#endif
2309
2310	/*
2311	 * Only a subset of options are supported.
2312	 */
2313	ATF_REQUIRE((!notimeout && !unrelated && !stopped) ||
2314	            (!notimeout && unrelated && !stopped) ||
2315	            (notimeout && !unrelated && !stopped) ||
2316	            (!notimeout && unrelated && stopped));
2317
2318	DPRINTF("Spawn tracee\n");
2319	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
2320	tracee = atf_utils_fork();
2321	if (tracee == 0) {
2322		if (stopped) {
2323			DPRINTF("Stop self PID %d\n", getpid());
2324			raise(SIGSTOP);
2325		}
2326
2327		// Wait for parent to let us exit
2328		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
2329		_exit(exitval_tracee);
2330	}
2331
2332	DPRINTF("Spawn debugger\n");
2333	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
2334	tracer = atf_utils_fork();
2335	if (tracer == 0) {
2336		if(unrelated) {
2337			/* Fork again and drop parent to reattach to PID 1 */
2338			tracer = atf_utils_fork();
2339			if (tracer != 0)
2340				_exit(exitval_tracer);
2341		}
2342
2343		if (stopped) {
2344			DPRINTF("Await for a stopped parent PID %d\n", tracee);
2345			await_stopped(tracee);
2346		}
2347
2348		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
2349		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
2350
2351		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
2352		FORKEE_REQUIRE_SUCCESS(
2353		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2354
2355		forkee_status_stopped(status, SIGSTOP);
2356
2357		/* Resume tracee with PT_CONTINUE */
2358		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2359
2360		/* Inform parent that tracer has attached to tracee */
2361		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
2362
2363		/* Wait for parent to tell use that tracee should have exited */
2364		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
2365
2366		/* Wait for tracee and assert that it exited */
2367		FORKEE_REQUIRE_SUCCESS(
2368		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2369
2370		forkee_status_exited(status, exitval_tracee);
2371		DPRINTF("Tracee %d exited with %d\n", tracee, exitval_tracee);
2372
2373		DPRINTF("Before exiting of the tracer process\n");
2374		_exit(unrelated ? 0 /* collect by initproc */ : exitval_tracer);
2375	}
2376
2377	if (unrelated) {
2378		DPRINTF("Wait for the tracer process (direct child) to exit "
2379		    "calling %s()\n", TWAIT_FNAME);
2380		TWAIT_REQUIRE_SUCCESS(
2381		    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
2382
2383		validate_status_exited(status, exitval_tracer);
2384
2385		DPRINTF("Wait for the non-exited tracee process with %s()\n",
2386		    TWAIT_FNAME);
2387		TWAIT_REQUIRE_SUCCESS(
2388		    wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
2389	}
2390
2391	DPRINTF("Wait for the tracer to attach to the tracee\n");
2392	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
2393
2394	DPRINTF("Resume the tracee and let it exit\n");
2395	PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
2396
2397	DPRINTF("Detect that tracee is zombie\n");
2398	if (notimeout)
2399		await_zombie_raw(tracee, 0);
2400	else
2401		await_zombie(tracee);
2402
2403	DPRINTF("Assert that there is no status about tracee %d - "
2404	    "Tracer must detect zombie first - calling %s()\n", tracee,
2405	    TWAIT_FNAME);
2406	TWAIT_REQUIRE_SUCCESS(
2407	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
2408
2409	if (unrelated) {
2410		DPRINTF("Resume the tracer and let it detect exited tracee\n");
2411		PARENT_TO_CHILD("Message 2", parent_tracer, msg);
2412	} else {
2413		DPRINTF("Tell the tracer child should have exited\n");
2414		PARENT_TO_CHILD("wait for tracee exit", parent_tracer,  msg);
2415		DPRINTF("Wait for tracer to finish its job and exit - calling "
2416			"%s()\n", TWAIT_FNAME);
2417
2418		DPRINTF("Wait from tracer child to complete waiting for "
2419			"tracee\n");
2420		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
2421		    tracer);
2422
2423		validate_status_exited(status, exitval_tracer);
2424	}
2425
2426	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
2427	    TWAIT_FNAME);
2428	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2429
2430	validate_status_exited(status, exitval_tracee);
2431
2432	msg_close(&parent_tracer);
2433	msg_close(&parent_tracee);
2434}
2435
2436ATF_TC(tracer_sees_terminaton_before_the_parent);
2437ATF_TC_HEAD(tracer_sees_terminaton_before_the_parent, tc)
2438{
2439	atf_tc_set_md_var(tc, "descr",
2440	    "Assert that tracer sees process termination before the parent");
2441}
2442
2443ATF_TC_BODY(tracer_sees_terminaton_before_the_parent, tc)
2444{
2445
2446	tracer_sees_terminaton_before_the_parent_raw(false, false, false);
2447}
2448
2449ATF_TC(tracer_sysctl_lookup_without_duplicates);
2450ATF_TC_HEAD(tracer_sysctl_lookup_without_duplicates, tc)
2451{
2452	atf_tc_set_md_var(tc, "descr",
2453	    "Assert that await_zombie() in attach1 always finds a single "
2454	    "process and no other error is reported");
2455}
2456
2457ATF_TC_BODY(tracer_sysctl_lookup_without_duplicates, tc)
2458{
2459	time_t start, end;
2460	double diff;
2461	unsigned long N = 0;
2462
2463	/*
2464	 * Reuse this test with tracer_sees_terminaton_before_the_parent_raw().
2465	 * This test body isn't specific to this race, however it's just good
2466	 * enough for this purposes, no need to invent a dedicated code flow.
2467	 */
2468
2469	start = time(NULL);
2470	while (true) {
2471		DPRINTF("Step: %lu\n", N);
2472		tracer_sees_terminaton_before_the_parent_raw(true, false,
2473		                                             false);
2474		end = time(NULL);
2475		diff = difftime(end, start);
2476		if (diff >= 5.0)
2477			break;
2478		++N;
2479	}
2480	DPRINTF("Iterations: %lu\n", N);
2481}
2482
2483ATF_TC(unrelated_tracer_sees_terminaton_before_the_parent);
2484ATF_TC_HEAD(unrelated_tracer_sees_terminaton_before_the_parent, tc)
2485{
2486	atf_tc_set_md_var(tc, "descr",
2487	    "Assert that tracer sees process termination before the parent");
2488}
2489
2490ATF_TC_BODY(unrelated_tracer_sees_terminaton_before_the_parent, tc)
2491{
2492
2493	tracer_sees_terminaton_before_the_parent_raw(false, true, false);
2494}
2495
2496ATF_TC(tracer_attach_to_unrelated_stopped_process);
2497ATF_TC_HEAD(tracer_attach_to_unrelated_stopped_process, tc)
2498{
2499	atf_tc_set_md_var(tc, "descr",
2500	    "Assert that tracer can attach to an unrelated stopped process");
2501}
2502
2503ATF_TC_BODY(tracer_attach_to_unrelated_stopped_process, tc)
2504{
2505
2506	tracer_sees_terminaton_before_the_parent_raw(false, true, true);
2507}
2508#endif
2509
2510/// ----------------------------------------------------------------------------
2511
2512static void
2513parent_attach_to_its_child(bool stopped)
2514{
2515	struct msg_fds parent_tracee;
2516	const int exitval_tracee = 5;
2517	pid_t tracee, wpid;
2518	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
2519#if defined(TWAIT_HAVE_STATUS)
2520	int status;
2521#endif
2522
2523	DPRINTF("Spawn tracee\n");
2524	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
2525	tracee = atf_utils_fork();
2526	if (tracee == 0) {
2527		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
2528		DPRINTF("Parent should now attach to tracee\n");
2529
2530		if (stopped) {
2531			DPRINTF("Stop self PID %d\n", getpid());
2532			SYSCALL_REQUIRE(raise(SIGSTOP) != -1);
2533		}
2534
2535		CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
2536		/* Wait for message from the parent */
2537		_exit(exitval_tracee);
2538	}
2539	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
2540
2541	if (stopped) {
2542		DPRINTF("Await for a stopped tracee PID %d\n", tracee);
2543		await_stopped(tracee);
2544	}
2545
2546	DPRINTF("Before calling PT_ATTACH for tracee %d\n", tracee);
2547	SYSCALL_REQUIRE(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
2548
2549	DPRINTF("Wait for the stopped tracee process with %s()\n",
2550	    TWAIT_FNAME);
2551	TWAIT_REQUIRE_SUCCESS(
2552	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2553
2554	validate_status_stopped(status, SIGSTOP);
2555
2556	DPRINTF("Resume tracee with PT_CONTINUE\n");
2557	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2558
2559	DPRINTF("Let the tracee exit now\n");
2560	PARENT_TO_CHILD("Message 2", parent_tracee, msg);
2561
2562	DPRINTF("Wait for tracee to exit with %s()\n", TWAIT_FNAME);
2563	TWAIT_REQUIRE_SUCCESS(
2564	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2565
2566	validate_status_exited(status, exitval_tracee);
2567
2568	DPRINTF("Before calling %s() for tracee\n", TWAIT_FNAME);
2569	TWAIT_REQUIRE_FAILURE(ECHILD,
2570	    wpid = TWAIT_GENERIC(tracee, &status, 0));
2571
2572	msg_close(&parent_tracee);
2573}
2574
2575ATF_TC(parent_attach_to_its_child);
2576ATF_TC_HEAD(parent_attach_to_its_child, tc)
2577{
2578	atf_tc_set_md_var(tc, "descr",
2579	    "Assert that tracer parent can PT_ATTACH to its child");
2580}
2581
2582ATF_TC_BODY(parent_attach_to_its_child, tc)
2583{
2584
2585	parent_attach_to_its_child(false);
2586}
2587
2588ATF_TC(parent_attach_to_its_stopped_child);
2589ATF_TC_HEAD(parent_attach_to_its_stopped_child, tc)
2590{
2591	atf_tc_set_md_var(tc, "descr",
2592	    "Assert that tracer parent can PT_ATTACH to its stopped child");
2593}
2594
2595ATF_TC_BODY(parent_attach_to_its_stopped_child, tc)
2596{
2597
2598	parent_attach_to_its_child(true);
2599}
2600
2601/// ----------------------------------------------------------------------------
2602
2603static void
2604child_attach_to_its_parent(bool stopped)
2605{
2606	struct msg_fds parent_tracee;
2607	const int exitval_tracer = 5;
2608	pid_t tracer, wpid;
2609	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
2610#if defined(TWAIT_HAVE_STATUS)
2611	int status;
2612#endif
2613
2614	DPRINTF("Spawn tracer\n");
2615	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
2616	tracer = atf_utils_fork();
2617	if (tracer == 0) {
2618		/* Wait for message from the parent */
2619		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
2620
2621		if (stopped) {
2622			DPRINTF("Await for a stopped parent PID %d\n",
2623			        getppid());
2624			await_stopped(getppid());
2625		}
2626
2627		DPRINTF("Attach to parent PID %d with PT_ATTACH from child\n",
2628		    getppid());
2629		FORKEE_ASSERT(ptrace(PT_ATTACH, getppid(), NULL, 0) != -1);
2630
2631		DPRINTF("Wait for the stopped parent process with %s()\n",
2632		    TWAIT_FNAME);
2633		FORKEE_REQUIRE_SUCCESS(
2634		    wpid = TWAIT_GENERIC(getppid(), &status, 0), getppid());
2635
2636		forkee_status_stopped(status, SIGSTOP);
2637
2638		DPRINTF("Resume parent with PT_DETACH\n");
2639		FORKEE_ASSERT(ptrace(PT_DETACH, getppid(), (void *)1, 0)
2640		    != -1);
2641
2642		/* Tell parent we are ready */
2643		CHILD_TO_PARENT("Message 1", parent_tracee, msg);
2644
2645		_exit(exitval_tracer);
2646	}
2647
2648	DPRINTF("Wait for the tracer to become ready\n");
2649	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
2650
2651	if (stopped) {
2652		DPRINTF("Stop self PID %d\n", getpid());
2653		SYSCALL_REQUIRE(raise(SIGSTOP) != -1);
2654	}
2655
2656	DPRINTF("Allow the tracer to exit now\n");
2657	PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
2658
2659	DPRINTF("Wait for tracer to exit with %s()\n", TWAIT_FNAME);
2660	TWAIT_REQUIRE_SUCCESS(
2661	    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
2662
2663	validate_status_exited(status, exitval_tracer);
2664
2665	DPRINTF("Before calling %s() for tracer\n", TWAIT_FNAME);
2666	TWAIT_REQUIRE_FAILURE(ECHILD,
2667	    wpid = TWAIT_GENERIC(tracer, &status, 0));
2668
2669	msg_close(&parent_tracee);
2670}
2671
2672ATF_TC(child_attach_to_its_parent);
2673ATF_TC_HEAD(child_attach_to_its_parent, tc)
2674{
2675	atf_tc_set_md_var(tc, "descr",
2676	    "Assert that tracer child can PT_ATTACH to its parent");
2677}
2678
2679ATF_TC_BODY(child_attach_to_its_parent, tc)
2680{
2681
2682	child_attach_to_its_parent(false);
2683}
2684
2685ATF_TC(child_attach_to_its_stopped_parent);
2686ATF_TC_HEAD(child_attach_to_its_stopped_parent, tc)
2687{
2688	atf_tc_set_md_var(tc, "descr",
2689	    "Assert that tracer child can PT_ATTACH to its stopped parent");
2690}
2691
2692ATF_TC_BODY(child_attach_to_its_stopped_parent, tc)
2693{
2694	/*
2695	 * The ATF framework (atf-run) does not tolerate raise(SIGSTOP), as
2696	 * this causes a pipe (established from atf-run) to be broken.
2697	 * atf-run uses this mechanism to monitor whether a test is alive.
2698	 *
2699	 * As a workaround spawn this test as a subprocess.
2700	 */
2701
2702	const int exitval = 15;
2703	pid_t child, wpid;
2704#if defined(TWAIT_HAVE_STATUS)
2705	int status;
2706#endif
2707
2708	SYSCALL_REQUIRE((child = fork()) != -1);
2709	if (child == 0) {
2710		child_attach_to_its_parent(true);
2711		_exit(exitval);
2712	} else {
2713		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2714		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2715
2716		validate_status_exited(status, exitval);
2717
2718		DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
2719		TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2720	}
2721}
2722
2723/// ----------------------------------------------------------------------------
2724
2725#if defined(TWAIT_HAVE_PID)
2726
2727enum tracee_sees_its_original_parent_type {
2728	TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID,
2729	TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2,
2730	TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS
2731};
2732
2733static void
2734tracee_sees_its_original_parent(enum tracee_sees_its_original_parent_type type)
2735{
2736	struct msg_fds parent_tracer, parent_tracee;
2737	const int exitval_tracee = 5;
2738	const int exitval_tracer = 10;
2739	pid_t parent, tracee, tracer, wpid;
2740	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
2741#if defined(TWAIT_HAVE_STATUS)
2742	int status;
2743#endif
2744	/* sysctl(3) - kinfo_proc2 */
2745	int name[CTL_MAXNAME];
2746	struct kinfo_proc2 kp;
2747	size_t len = sizeof(kp);
2748	unsigned int namelen;
2749
2750	/* procfs - status  */
2751	FILE *fp;
2752	struct stat st;
2753	const char *fname = "/proc/curproc/status";
2754	char s_executable[MAXPATHLEN];
2755	int s_pid, s_ppid;
2756	int rv;
2757
2758	if (type == TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS) {
2759		SYSCALL_REQUIRE(
2760		    (rv = stat(fname, &st)) == 0 || (errno == ENOENT));
2761		if (rv != 0)
2762			atf_tc_skip("/proc/curproc/status not found");
2763	}
2764
2765	DPRINTF("Spawn tracee\n");
2766	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
2767	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
2768	tracee = atf_utils_fork();
2769	if (tracee == 0) {
2770		parent = getppid();
2771
2772		/* Emit message to the parent */
2773		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
2774		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
2775
2776		switch (type) {
2777		case TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID:
2778			FORKEE_ASSERT_EQ(parent, getppid());
2779			break;
2780		case TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2:
2781			namelen = 0;
2782			name[namelen++] = CTL_KERN;
2783			name[namelen++] = KERN_PROC2;
2784			name[namelen++] = KERN_PROC_PID;
2785			name[namelen++] = getpid();
2786			name[namelen++] = len;
2787			name[namelen++] = 1;
2788
2789			FORKEE_ASSERT_EQ(
2790			    sysctl(name, namelen, &kp, &len, NULL, 0), 0);
2791			FORKEE_ASSERT_EQ(parent, kp.p_ppid);
2792			break;
2793		case TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS:
2794			/*
2795			 * Format:
2796			 *  EXECUTABLE PID PPID ...
2797			 */
2798			FORKEE_ASSERT((fp = fopen(fname, "r")) != NULL);
2799			fscanf(fp, "%s %d %d", s_executable, &s_pid, &s_ppid);
2800			FORKEE_ASSERT_EQ(fclose(fp), 0);
2801			FORKEE_ASSERT_EQ(parent, s_ppid);
2802			break;
2803		}
2804
2805		_exit(exitval_tracee);
2806	}
2807	DPRINTF("Wait for child to record its parent identifier (pid)\n");
2808	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
2809
2810	DPRINTF("Spawn debugger\n");
2811	tracer = atf_utils_fork();
2812	if (tracer == 0) {
2813		/* No IPC to communicate with the child */
2814		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
2815		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
2816
2817		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
2818		FORKEE_REQUIRE_SUCCESS(
2819		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2820
2821		forkee_status_stopped(status, SIGSTOP);
2822
2823		/* Resume tracee with PT_CONTINUE */
2824		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2825
2826		/* Inform parent that tracer has attached to tracee */
2827		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
2828
2829		/* Wait for parent to tell use that tracee should have exited */
2830		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
2831
2832		/* Wait for tracee and assert that it exited */
2833		FORKEE_REQUIRE_SUCCESS(
2834		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2835
2836		forkee_status_exited(status, exitval_tracee);
2837
2838		DPRINTF("Before exiting of the tracer process\n");
2839		_exit(exitval_tracer);
2840	}
2841
2842	DPRINTF("Wait for the tracer to attach to the tracee\n");
2843	PARENT_FROM_CHILD("tracer ready",  parent_tracer, msg);
2844
2845	DPRINTF("Resume the tracee and let it exit\n");
2846	PARENT_TO_CHILD("exit tracee",  parent_tracee, msg);
2847
2848	DPRINTF("Detect that tracee is zombie\n");
2849	await_zombie(tracee);
2850
2851	DPRINTF("Assert that there is no status about tracee - "
2852	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
2853	TWAIT_REQUIRE_SUCCESS(
2854	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
2855
2856	DPRINTF("Tell the tracer child should have exited\n");
2857	PARENT_TO_CHILD("wait for tracee exit",  parent_tracer, msg);
2858
2859	DPRINTF("Wait from tracer child to complete waiting for tracee\n");
2860	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
2861	    tracer);
2862
2863	validate_status_exited(status, exitval_tracer);
2864
2865	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
2866	    TWAIT_FNAME);
2867	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
2868	    tracee);
2869
2870	validate_status_exited(status, exitval_tracee);
2871
2872	msg_close(&parent_tracer);
2873	msg_close(&parent_tracee);
2874}
2875
2876#define TRACEE_SEES_ITS_ORIGINAL_PARENT(test, type, descr)		\
2877ATF_TC(test);								\
2878ATF_TC_HEAD(test, tc)							\
2879{									\
2880	atf_tc_set_md_var(tc, "descr",					\
2881	    "Assert that tracee sees its original parent when being traced " \
2882	    "(check " descr ")");					\
2883}									\
2884									\
2885ATF_TC_BODY(test, tc)							\
2886{									\
2887									\
2888	tracee_sees_its_original_parent(type);				\
2889}
2890
2891TRACEE_SEES_ITS_ORIGINAL_PARENT(
2892	tracee_sees_its_original_parent_getppid,
2893	TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID,
2894	"getppid(2)");
2895TRACEE_SEES_ITS_ORIGINAL_PARENT(
2896	tracee_sees_its_original_parent_sysctl_kinfo_proc2,
2897	TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2,
2898	"sysctl(3) and kinfo_proc2");
2899TRACEE_SEES_ITS_ORIGINAL_PARENT(
2900	tracee_sees_its_original_parent_procfs_status,
2901	TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS,
2902	"the status file in procfs");
2903#endif
2904
2905/// ----------------------------------------------------------------------------
2906
2907static void
2908eventmask_preserved(int event)
2909{
2910	const int exitval = 5;
2911	const int sigval = SIGSTOP;
2912	pid_t child, wpid;
2913#if defined(TWAIT_HAVE_STATUS)
2914	int status;
2915#endif
2916	ptrace_event_t set_event, get_event;
2917	const int len = sizeof(ptrace_event_t);
2918
2919	DPRINTF("Before forking process PID=%d\n", getpid());
2920	SYSCALL_REQUIRE((child = fork()) != -1);
2921	if (child == 0) {
2922		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2923		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2924
2925		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2926		FORKEE_ASSERT(raise(sigval) == 0);
2927
2928		DPRINTF("Before exiting of the child process\n");
2929		_exit(exitval);
2930	}
2931	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2932
2933	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2934	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2935
2936	validate_status_stopped(status, sigval);
2937
2938	set_event.pe_set_event = event;
2939	SYSCALL_REQUIRE(
2940	    ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
2941	SYSCALL_REQUIRE(
2942	    ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
2943	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
2944
2945	DPRINTF("Before resuming the child process where it left off and "
2946	    "without signal to be sent\n");
2947	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2948
2949	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2950	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2951
2952	validate_status_exited(status, exitval);
2953
2954	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2955	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2956}
2957
2958#define EVENTMASK_PRESERVED(test, event)				\
2959ATF_TC(test);								\
2960ATF_TC_HEAD(test, tc)							\
2961{									\
2962	atf_tc_set_md_var(tc, "descr",					\
2963	    "Verify that eventmask " #event " is preserved");		\
2964}									\
2965									\
2966ATF_TC_BODY(test, tc)							\
2967{									\
2968									\
2969	eventmask_preserved(event);					\
2970}
2971
2972EVENTMASK_PRESERVED(eventmask_preserved_empty, 0)
2973EVENTMASK_PRESERVED(eventmask_preserved_fork, PTRACE_FORK)
2974EVENTMASK_PRESERVED(eventmask_preserved_vfork, PTRACE_VFORK)
2975EVENTMASK_PRESERVED(eventmask_preserved_vfork_done, PTRACE_VFORK_DONE)
2976EVENTMASK_PRESERVED(eventmask_preserved_lwp_create, PTRACE_LWP_CREATE)
2977EVENTMASK_PRESERVED(eventmask_preserved_lwp_exit, PTRACE_LWP_EXIT)
2978
2979/// ----------------------------------------------------------------------------
2980
2981static void
2982fork_body(pid_t (*fn)(void), bool trackfork, bool trackvfork,
2983    bool trackvforkdone)
2984{
2985	const int exitval = 5;
2986	const int exitval2 = 15;
2987	const int sigval = SIGSTOP;
2988	pid_t child, child2 = 0, wpid;
2989#if defined(TWAIT_HAVE_STATUS)
2990	int status;
2991#endif
2992	ptrace_state_t state;
2993	const int slen = sizeof(state);
2994	ptrace_event_t event;
2995	const int elen = sizeof(event);
2996
2997	DPRINTF("Before forking process PID=%d\n", getpid());
2998	SYSCALL_REQUIRE((child = fork()) != -1);
2999	if (child == 0) {
3000		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3001		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3002
3003		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3004		FORKEE_ASSERT(raise(sigval) == 0);
3005
3006		FORKEE_ASSERT((child2 = (fn)()) != -1);
3007
3008		if (child2 == 0)
3009			_exit(exitval2);
3010
3011		FORKEE_REQUIRE_SUCCESS
3012		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3013
3014		forkee_status_exited(status, exitval2);
3015
3016		DPRINTF("Before exiting of the child process\n");
3017		_exit(exitval);
3018	}
3019	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3020
3021	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3022	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3023
3024	validate_status_stopped(status, sigval);
3025
3026	DPRINTF("Set 0%s%s%s in EVENT_MASK for the child %d\n",
3027	    trackfork ? "|PTRACE_FORK" : "",
3028	    trackvfork ? "|PTRACE_VFORK" : "",
3029	    trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
3030	event.pe_set_event = 0;
3031	if (trackfork)
3032		event.pe_set_event |= PTRACE_FORK;
3033	if (trackvfork)
3034		event.pe_set_event |= PTRACE_VFORK;
3035	if (trackvforkdone)
3036		event.pe_set_event |= PTRACE_VFORK_DONE;
3037	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3038
3039	DPRINTF("Before resuming the child process where it left off and "
3040	    "without signal to be sent\n");
3041	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3042
3043#if defined(TWAIT_HAVE_PID)
3044	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
3045		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
3046		    child);
3047		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
3048		    child);
3049
3050		validate_status_stopped(status, SIGTRAP);
3051
3052		SYSCALL_REQUIRE(
3053		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3054		if (trackfork && fn == fork) {
3055			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
3056			       PTRACE_FORK);
3057		}
3058		if (trackvfork && fn == vfork) {
3059			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
3060			       PTRACE_VFORK);
3061		}
3062
3063		child2 = state.pe_other_pid;
3064		DPRINTF("Reported ptrace event with forkee %d\n", child2);
3065
3066		DPRINTF("Before calling %s() for the forkee %d of the child "
3067		    "%d\n", TWAIT_FNAME, child2, child);
3068		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3069		    child2);
3070
3071		validate_status_stopped(status, SIGTRAP);
3072
3073		SYSCALL_REQUIRE(
3074		    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
3075		if (trackfork && fn == fork) {
3076			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
3077			       PTRACE_FORK);
3078		}
3079		if (trackvfork && fn == vfork) {
3080			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
3081			       PTRACE_VFORK);
3082		}
3083
3084		ATF_REQUIRE_EQ(state.pe_other_pid, child);
3085
3086		DPRINTF("Before resuming the forkee process where it left off "
3087		    "and without signal to be sent\n");
3088		SYSCALL_REQUIRE(
3089		    ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
3090
3091		DPRINTF("Before resuming the child process where it left off "
3092		    "and without signal to be sent\n");
3093		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3094	}
3095#endif
3096
3097	if (trackvforkdone && fn == vfork) {
3098		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
3099		    child);
3100		TWAIT_REQUIRE_SUCCESS(
3101		    wpid = TWAIT_GENERIC(child, &status, 0), child);
3102
3103		validate_status_stopped(status, SIGTRAP);
3104
3105		SYSCALL_REQUIRE(
3106		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3107		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
3108
3109		child2 = state.pe_other_pid;
3110		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
3111		    child2);
3112
3113		DPRINTF("Before resuming the child process where it left off "
3114		    "and without signal to be sent\n");
3115		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3116	}
3117
3118#if defined(TWAIT_HAVE_PID)
3119	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
3120		DPRINTF("Before calling %s() for the forkee - expected exited"
3121		    "\n", TWAIT_FNAME);
3122		TWAIT_REQUIRE_SUCCESS(
3123		    wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3124
3125		validate_status_exited(status, exitval2);
3126
3127		DPRINTF("Before calling %s() for the forkee - expected no "
3128		    "process\n", TWAIT_FNAME);
3129		TWAIT_REQUIRE_FAILURE(ECHILD,
3130		    wpid = TWAIT_GENERIC(child2, &status, 0));
3131	}
3132#endif
3133
3134	DPRINTF("Before calling %s() for the child - expected stopped "
3135	    "SIGCHLD\n", TWAIT_FNAME);
3136	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3137
3138	validate_status_stopped(status, SIGCHLD);
3139
3140	DPRINTF("Before resuming the child process where it left off and "
3141	    "without signal to be sent\n");
3142	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3143
3144	DPRINTF("Before calling %s() for the child - expected exited\n",
3145	    TWAIT_FNAME);
3146	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3147
3148	validate_status_exited(status, exitval);
3149
3150	DPRINTF("Before calling %s() for the child - expected no process\n",
3151	    TWAIT_FNAME);
3152	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3153}
3154
3155#define FORK_TEST(name,fun,tfork,tvfork,tvforkdone)			\
3156ATF_TC(name);								\
3157ATF_TC_HEAD(name, tc)							\
3158{									\
3159	atf_tc_set_md_var(tc, "descr", "Verify " #fun "(2) "		\
3160	    "called with 0%s%s%s in EVENT_MASK",			\
3161	    tfork ? "|PTRACE_FORK" : "",				\
3162	    tvfork ? "|PTRACE_VFORK" : "",				\
3163	    tvforkdone ? "|PTRACE_VFORK_DONE" : "");			\
3164}									\
3165									\
3166ATF_TC_BODY(name, tc)							\
3167{									\
3168									\
3169	fork_body(fun, tfork, tvfork, tvforkdone);			\
3170}
3171
3172FORK_TEST(fork1, fork, false, false, false)
3173#if defined(TWAIT_HAVE_PID)
3174FORK_TEST(fork2, fork, true, false, false)
3175FORK_TEST(fork3, fork, false, true, false)
3176FORK_TEST(fork4, fork, true, true, false)
3177#endif
3178FORK_TEST(fork5, fork, false, false, true)
3179#if defined(TWAIT_HAVE_PID)
3180FORK_TEST(fork6, fork, true, false, true)
3181FORK_TEST(fork7, fork, false, true, true)
3182FORK_TEST(fork8, fork, true, true, true)
3183#endif
3184
3185#if TEST_VFORK_ENABLED
3186FORK_TEST(vfork1, vfork, false, false, false)
3187#if defined(TWAIT_HAVE_PID)
3188FORK_TEST(vfork2, vfork, true, false, false)
3189FORK_TEST(vfork3, vfork, false, true, false)
3190FORK_TEST(vfork4, vfork, true, true, false)
3191#endif
3192FORK_TEST(vfork5, vfork, false, false, true)
3193#if defined(TWAIT_HAVE_PID)
3194FORK_TEST(vfork6, vfork, true, false, true)
3195FORK_TEST(vfork7, vfork, false, true, true)
3196FORK_TEST(vfork8, vfork, true, true, true)
3197#endif
3198#endif
3199
3200/// ----------------------------------------------------------------------------
3201
3202#if defined(TWAIT_HAVE_PID)
3203static void
3204fork_detach_forker_body(bool detachfork, bool detachvfork,
3205    bool detachvforkdone, bool kill_process)
3206{
3207	const int exitval = 5;
3208	const int exitval2 = 15;
3209	const int sigval = SIGSTOP;
3210	pid_t child, child2 = 0, wpid;
3211#if defined(TWAIT_HAVE_STATUS)
3212	int status;
3213#endif
3214	ptrace_state_t state;
3215	const int slen = sizeof(state);
3216	ptrace_event_t event;
3217	const int elen = sizeof(event);
3218
3219	pid_t (*fn)(void);
3220	int op;
3221
3222	ATF_REQUIRE((detachfork && !detachvfork && !detachvforkdone) ||
3223	            (!detachfork && detachvfork && !detachvforkdone) ||
3224	            (!detachfork && !detachvfork && detachvforkdone));
3225
3226	if (detachfork)
3227		fn = fork;
3228	else
3229		fn = vfork;
3230
3231	DPRINTF("Before forking process PID=%d\n", getpid());
3232	SYSCALL_REQUIRE((child = fork()) != -1);
3233	if (child == 0) {
3234		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3235		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3236
3237		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3238		FORKEE_ASSERT(raise(sigval) == 0);
3239
3240		FORKEE_ASSERT((child2 = (fn)()) != -1);
3241
3242		if (child2 == 0)
3243			_exit(exitval2);
3244
3245		FORKEE_REQUIRE_SUCCESS
3246		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3247
3248		forkee_status_exited(status, exitval2);
3249
3250		DPRINTF("Before exiting of the child process\n");
3251		_exit(exitval);
3252	}
3253	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3254
3255	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3256	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3257
3258	validate_status_stopped(status, sigval);
3259
3260	DPRINTF("Set EVENT_MASK for the child %d\n", child);
3261	event.pe_set_event = PTRACE_FORK | PTRACE_VFORK | PTRACE_VFORK_DONE;
3262	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3263
3264	DPRINTF("Before resuming the child process where it left off and "
3265	    "without signal to be sent\n");
3266	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3267
3268	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
3269	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3270
3271	validate_status_stopped(status, SIGTRAP);
3272
3273	SYSCALL_REQUIRE(
3274	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3275	op = (fn == fork) ? PTRACE_FORK : PTRACE_VFORK;
3276	ATF_REQUIRE_EQ(state.pe_report_event & op, op);
3277
3278	child2 = state.pe_other_pid;
3279	DPRINTF("Reported ptrace event with forkee %d\n", child2);
3280
3281	if (detachfork || detachvfork)
3282		op = kill_process ? PT_KILL : PT_DETACH;
3283	else
3284		op = PT_CONTINUE;
3285	SYSCALL_REQUIRE(ptrace(op, child, (void *)1, 0) != -1);
3286
3287	DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
3288	    TWAIT_FNAME, child2, child);
3289	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3290
3291	validate_status_stopped(status, SIGTRAP);
3292
3293	SYSCALL_REQUIRE(
3294	    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
3295	op = (fn == fork) ? PTRACE_FORK : PTRACE_VFORK;
3296	ATF_REQUIRE_EQ(state.pe_report_event & op, op);
3297	ATF_REQUIRE_EQ(state.pe_other_pid, child);
3298
3299	DPRINTF("Before resuming the forkee process where it left off "
3300	    "and without signal to be sent\n");
3301 	SYSCALL_REQUIRE(
3302	    ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
3303
3304	if (detachvforkdone && fn == vfork) {
3305		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
3306		    child);
3307		TWAIT_REQUIRE_SUCCESS(
3308		    wpid = TWAIT_GENERIC(child, &status, 0), child);
3309
3310		validate_status_stopped(status, SIGTRAP);
3311
3312		SYSCALL_REQUIRE(
3313		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3314		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
3315
3316		child2 = state.pe_other_pid;
3317		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
3318		    child2);
3319
3320		op = kill_process ? PT_KILL : PT_DETACH;
3321		DPRINTF("Before resuming the child process where it left off "
3322		    "and without signal to be sent\n");
3323		SYSCALL_REQUIRE(ptrace(op, child, (void *)1, 0) != -1);
3324	}
3325
3326	DPRINTF("Before calling %s() for the forkee - expected exited\n",
3327	    TWAIT_FNAME);
3328	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3329
3330	validate_status_exited(status, exitval2);
3331
3332	DPRINTF("Before calling %s() for the forkee - expected no process\n",
3333	    TWAIT_FNAME);
3334	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child2, &status, 0));
3335
3336	DPRINTF("Before calling %s() for the forkee - expected exited\n",
3337	    TWAIT_FNAME);
3338	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3339
3340	if (kill_process) {
3341		validate_status_signaled(status, SIGKILL, 0);
3342	} else {
3343		validate_status_exited(status, exitval);
3344	}
3345
3346	DPRINTF("Before calling %s() for the child - expected no process\n",
3347	    TWAIT_FNAME);
3348	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3349}
3350
3351#define FORK_DETACH_FORKER(name,detfork,detvfork,detvforkdone,kprocess)	\
3352ATF_TC(name);								\
3353ATF_TC_HEAD(name, tc)							\
3354{									\
3355	atf_tc_set_md_var(tc, "descr", "Verify %s %s%s%s",		\
3356	    kprocess ? "killed" : "detached",				\
3357	    detfork ? "forker" : "",					\
3358	    detvfork ? "vforker" : "",					\
3359	    detvforkdone ? "vforker done" : "");			\
3360}									\
3361									\
3362ATF_TC_BODY(name, tc)							\
3363{									\
3364									\
3365	fork_detach_forker_body(detfork, detvfork, detvforkdone,	\
3366	                        kprocess);				\
3367}
3368
3369FORK_DETACH_FORKER(fork_detach_forker, true, false, false, false)
3370#if TEST_VFORK_ENABLED
3371FORK_DETACH_FORKER(vfork_detach_vforker, false, true, false, false)
3372FORK_DETACH_FORKER(vfork_detach_vforkerdone, false, false, true, false)
3373#endif
3374FORK_DETACH_FORKER(fork_kill_forker, true, false, false, true)
3375#if TEST_VFORK_ENABLED
3376FORK_DETACH_FORKER(vfork_kill_vforker, false, true, false, true)
3377FORK_DETACH_FORKER(vfork_kill_vforkerdone, false, false, true, true)
3378#endif
3379#endif
3380
3381/// ----------------------------------------------------------------------------
3382
3383#if TEST_VFORK_ENABLED
3384static void
3385traceme_vfork_fork_body(pid_t (*fn)(void))
3386{
3387	const int exitval = 5;
3388	const int exitval2 = 15;
3389	pid_t child, child2 = 0, wpid;
3390#if defined(TWAIT_HAVE_STATUS)
3391	int status;
3392#endif
3393
3394	DPRINTF("Before forking process PID=%d\n", getpid());
3395	SYSCALL_REQUIRE((child = vfork()) != -1);
3396	if (child == 0) {
3397		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3398		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3399
3400		FORKEE_ASSERT((child2 = (fn)()) != -1);
3401
3402		if (child2 == 0)
3403			_exit(exitval2);
3404
3405		FORKEE_REQUIRE_SUCCESS
3406		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3407
3408		forkee_status_exited(status, exitval2);
3409
3410		DPRINTF("Before exiting of the child process\n");
3411		_exit(exitval);
3412	}
3413	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3414
3415	DPRINTF("Before calling %s() for the child - expected exited\n",
3416	    TWAIT_FNAME);
3417	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3418
3419	validate_status_exited(status, exitval);
3420
3421	DPRINTF("Before calling %s() for the child - expected no process\n",
3422	    TWAIT_FNAME);
3423	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3424}
3425
3426#define TRACEME_VFORK_FORK_TEST(name,fun)				\
3427ATF_TC(name);								\
3428ATF_TC_HEAD(name, tc)							\
3429{									\
3430	atf_tc_set_md_var(tc, "descr", "Verify " #fun "(2) "		\
3431	    "called from vfork(2)ed child");				\
3432}									\
3433									\
3434ATF_TC_BODY(name, tc)							\
3435{									\
3436									\
3437	traceme_vfork_fork_body(fun);					\
3438}
3439
3440TRACEME_VFORK_FORK_TEST(traceme_vfork_fork, fork)
3441TRACEME_VFORK_FORK_TEST(traceme_vfork_vfork, vfork)
3442#endif
3443
3444/// ----------------------------------------------------------------------------
3445
3446enum bytes_transfer_type {
3447	BYTES_TRANSFER_DATA,
3448	BYTES_TRANSFER_DATAIO,
3449	BYTES_TRANSFER_TEXT,
3450	BYTES_TRANSFER_TEXTIO,
3451	BYTES_TRANSFER_AUXV
3452};
3453
3454static int __used
3455bytes_transfer_dummy(int a, int b, int c, int d)
3456{
3457	int e, f, g, h;
3458
3459	a *= 4;
3460	b += 3;
3461	c -= 2;
3462	d /= 1;
3463
3464	e = strtol("10", NULL, 10);
3465	f = strtol("20", NULL, 10);
3466	g = strtol("30", NULL, 10);
3467	h = strtol("40", NULL, 10);
3468
3469	return (a + b * c - d) + (e * f - g / h);
3470}
3471
3472static void
3473bytes_transfer(int operation, size_t size, enum bytes_transfer_type type)
3474{
3475	const int exitval = 5;
3476	const int sigval = SIGSTOP;
3477	pid_t child, wpid;
3478	bool skip = false;
3479
3480	int lookup_me = 0;
3481	uint8_t lookup_me8 = 0;
3482	uint16_t lookup_me16 = 0;
3483	uint32_t lookup_me32 = 0;
3484	uint64_t lookup_me64 = 0;
3485
3486	int magic = 0x13579246;
3487	uint8_t magic8 = 0xab;
3488	uint16_t magic16 = 0x1234;
3489	uint32_t magic32 = 0x98765432;
3490	uint64_t magic64 = 0xabcdef0123456789;
3491
3492	struct ptrace_io_desc io;
3493#if defined(TWAIT_HAVE_STATUS)
3494	int status;
3495#endif
3496	/* 513 is just enough, for the purposes of ATF it's good enough */
3497	AuxInfo ai[513], *aip;
3498
3499	ATF_REQUIRE(size < sizeof(ai));
3500
3501	/* Prepare variables for .TEXT transfers */
3502	switch (type) {
3503	case BYTES_TRANSFER_TEXT:
3504		memcpy(&magic, bytes_transfer_dummy, sizeof(magic));
3505		break;
3506	case BYTES_TRANSFER_TEXTIO:
3507		switch (size) {
3508		case 8:
3509			memcpy(&magic8, bytes_transfer_dummy, sizeof(magic8));
3510			break;
3511		case 16:
3512			memcpy(&magic16, bytes_transfer_dummy, sizeof(magic16));
3513			break;
3514		case 32:
3515			memcpy(&magic32, bytes_transfer_dummy, sizeof(magic32));
3516			break;
3517		case 64:
3518			memcpy(&magic64, bytes_transfer_dummy, sizeof(magic64));
3519			break;
3520		}
3521		break;
3522	default:
3523		break;
3524	}
3525
3526	/* Prepare variables for PIOD and AUXV transfers */
3527	switch (type) {
3528	case BYTES_TRANSFER_TEXTIO:
3529	case BYTES_TRANSFER_DATAIO:
3530		io.piod_op = operation;
3531		switch (size) {
3532		case 8:
3533			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
3534			               (void *)bytes_transfer_dummy :
3535			               &lookup_me8;
3536			io.piod_addr = &lookup_me8;
3537			io.piod_len = sizeof(lookup_me8);
3538			break;
3539		case 16:
3540			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
3541			               (void *)bytes_transfer_dummy :
3542			               &lookup_me16;
3543			io.piod_addr = &lookup_me16;
3544			io.piod_len = sizeof(lookup_me16);
3545			break;
3546		case 32:
3547			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
3548			               (void *)bytes_transfer_dummy :
3549			               &lookup_me32;
3550			io.piod_addr = &lookup_me32;
3551			io.piod_len = sizeof(lookup_me32);
3552			break;
3553		case 64:
3554			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
3555			               (void *)bytes_transfer_dummy :
3556			               &lookup_me64;
3557			io.piod_addr = &lookup_me64;
3558			io.piod_len = sizeof(lookup_me64);
3559			break;
3560		default:
3561			break;
3562		}
3563		break;
3564	case BYTES_TRANSFER_AUXV:
3565		io.piod_op = operation;
3566		io.piod_offs = 0;
3567		io.piod_addr = ai;
3568		io.piod_len = size;
3569		break;
3570	default:
3571		break;
3572	}
3573
3574	DPRINTF("Before forking process PID=%d\n", getpid());
3575	SYSCALL_REQUIRE((child = fork()) != -1);
3576	if (child == 0) {
3577		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3578		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3579
3580		switch (type) {
3581		case BYTES_TRANSFER_DATA:
3582			switch (operation) {
3583			case PT_READ_D:
3584			case PT_READ_I:
3585				lookup_me = magic;
3586				break;
3587			default:
3588				break;
3589			}
3590			break;
3591		case BYTES_TRANSFER_DATAIO:
3592			switch (operation) {
3593			case PIOD_READ_D:
3594			case PIOD_READ_I:
3595				switch (size) {
3596				case 8:
3597					lookup_me8 = magic8;
3598					break;
3599				case 16:
3600					lookup_me16 = magic16;
3601					break;
3602				case 32:
3603					lookup_me32 = magic32;
3604					break;
3605				case 64:
3606					lookup_me64 = magic64;
3607					break;
3608				default:
3609					break;
3610				}
3611				break;
3612			default:
3613				break;
3614			}
3615		default:
3616			break;
3617		}
3618
3619		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3620		FORKEE_ASSERT(raise(sigval) == 0);
3621
3622		/* Handle PIOD and PT separately as operation values overlap */
3623		switch (type) {
3624		case BYTES_TRANSFER_DATA:
3625			switch (operation) {
3626			case PT_WRITE_D:
3627			case PT_WRITE_I:
3628				FORKEE_ASSERT_EQ(lookup_me, magic);
3629				break;
3630			default:
3631				break;
3632			}
3633			break;
3634		case BYTES_TRANSFER_DATAIO:
3635			switch (operation) {
3636			case PIOD_WRITE_D:
3637			case PIOD_WRITE_I:
3638				switch (size) {
3639				case 8:
3640					FORKEE_ASSERT_EQ(lookup_me8, magic8);
3641					break;
3642				case 16:
3643					FORKEE_ASSERT_EQ(lookup_me16, magic16);
3644					break;
3645				case 32:
3646					FORKEE_ASSERT_EQ(lookup_me32, magic32);
3647					break;
3648				case 64:
3649					FORKEE_ASSERT_EQ(lookup_me64, magic64);
3650					break;
3651				default:
3652					break;
3653				}
3654				break;
3655			default:
3656				break;
3657			}
3658			break;
3659		case BYTES_TRANSFER_TEXT:
3660			FORKEE_ASSERT(memcmp(&magic, bytes_transfer_dummy,
3661			                     sizeof(magic)) == 0);
3662			break;
3663		case BYTES_TRANSFER_TEXTIO:
3664			switch (size) {
3665			case 8:
3666				FORKEE_ASSERT(memcmp(&magic8,
3667				                     bytes_transfer_dummy,
3668				                     sizeof(magic8)) == 0);
3669				break;
3670			case 16:
3671				FORKEE_ASSERT(memcmp(&magic16,
3672				                     bytes_transfer_dummy,
3673				                     sizeof(magic16)) == 0);
3674				break;
3675			case 32:
3676				FORKEE_ASSERT(memcmp(&magic32,
3677				                     bytes_transfer_dummy,
3678				                     sizeof(magic32)) == 0);
3679				break;
3680			case 64:
3681				FORKEE_ASSERT(memcmp(&magic64,
3682				                     bytes_transfer_dummy,
3683				                     sizeof(magic64)) == 0);
3684				break;
3685			}
3686			break;
3687		default:
3688			break;
3689		}
3690
3691		DPRINTF("Before exiting of the child process\n");
3692		_exit(exitval);
3693	}
3694	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3695
3696	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3697	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3698
3699	validate_status_stopped(status, sigval);
3700
3701	/* Check PaX MPROTECT */
3702	if (!can_we_write_to_text(child)) {
3703		switch (type) {
3704		case BYTES_TRANSFER_TEXTIO:
3705			switch (operation) {
3706			case PIOD_WRITE_D:
3707			case PIOD_WRITE_I:
3708				skip = true;
3709				break;
3710			default:
3711				break;
3712			}
3713			break;
3714		case BYTES_TRANSFER_TEXT:
3715			switch (operation) {
3716			case PT_WRITE_D:
3717			case PT_WRITE_I:
3718				skip = true;
3719				break;
3720			default:
3721				break;
3722			}
3723			break;
3724		default:
3725			break;
3726		}
3727	}
3728
3729	/* Bailout cleanly killing the child process */
3730	if (skip) {
3731		SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void *)1, 0) != -1);
3732		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3733		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
3734		                      child);
3735
3736		validate_status_signaled(status, SIGKILL, 0);
3737
3738		atf_tc_skip("PaX MPROTECT setup prevents writes to .text");
3739	}
3740
3741	DPRINTF("Calling operation to transfer bytes between child=%d and "
3742	       "parent=%d\n", child, getpid());
3743
3744	switch (type) {
3745	case BYTES_TRANSFER_TEXTIO:
3746	case BYTES_TRANSFER_DATAIO:
3747	case BYTES_TRANSFER_AUXV:
3748		switch (operation) {
3749		case PIOD_WRITE_D:
3750		case PIOD_WRITE_I:
3751			switch (size) {
3752			case 8:
3753				lookup_me8 = magic8;
3754				break;
3755			case 16:
3756				lookup_me16 = magic16;
3757				break;
3758			case 32:
3759				lookup_me32 = magic32;
3760				break;
3761			case 64:
3762				lookup_me64 = magic64;
3763				break;
3764			default:
3765				break;
3766			}
3767			break;
3768		default:
3769			break;
3770		}
3771		SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
3772		switch (operation) {
3773		case PIOD_READ_D:
3774		case PIOD_READ_I:
3775			switch (size) {
3776			case 8:
3777				ATF_REQUIRE_EQ(lookup_me8, magic8);
3778				break;
3779			case 16:
3780				ATF_REQUIRE_EQ(lookup_me16, magic16);
3781				break;
3782			case 32:
3783				ATF_REQUIRE_EQ(lookup_me32, magic32);
3784				break;
3785			case 64:
3786				ATF_REQUIRE_EQ(lookup_me64, magic64);
3787				break;
3788			default:
3789				break;
3790			}
3791			break;
3792		case PIOD_READ_AUXV:
3793			DPRINTF("Asserting that AUXV length (%zu) is > 0\n",
3794			        io.piod_len);
3795			ATF_REQUIRE(io.piod_len > 0);
3796			for (aip = ai; aip->a_type != AT_NULL; aip++)
3797				DPRINTF("a_type=%#llx a_v=%#llx\n",
3798				    (long long int)aip->a_type,
3799				    (long long int)aip->a_v);
3800			break;
3801		default:
3802			break;
3803		}
3804		break;
3805	case BYTES_TRANSFER_TEXT:
3806		switch (operation) {
3807		case PT_READ_D:
3808		case PT_READ_I:
3809			errno = 0;
3810			lookup_me = ptrace(operation, child,
3811			                   bytes_transfer_dummy, 0);
3812			ATF_REQUIRE_EQ(lookup_me, magic);
3813			SYSCALL_REQUIRE_ERRNO(errno, 0);
3814			break;
3815		case PT_WRITE_D:
3816		case PT_WRITE_I:
3817			SYSCALL_REQUIRE(ptrace(operation, child,
3818			                       bytes_transfer_dummy, magic)
3819			                != -1);
3820			break;
3821		default:
3822			break;
3823		}
3824		break;
3825	case BYTES_TRANSFER_DATA:
3826		switch (operation) {
3827		case PT_READ_D:
3828		case PT_READ_I:
3829			errno = 0;
3830			lookup_me = ptrace(operation, child, &lookup_me, 0);
3831			ATF_REQUIRE_EQ(lookup_me, magic);
3832			SYSCALL_REQUIRE_ERRNO(errno, 0);
3833			break;
3834		case PT_WRITE_D:
3835		case PT_WRITE_I:
3836			lookup_me = magic;
3837			SYSCALL_REQUIRE(ptrace(operation, child, &lookup_me,
3838			                       magic) != -1);
3839			break;
3840		default:
3841			break;
3842		}
3843		break;
3844	default:
3845		break;
3846	}
3847
3848	DPRINTF("Before resuming the child process where it left off and "
3849	    "without signal to be sent\n");
3850	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3851
3852	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3853	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3854
3855	validate_status_exited(status, exitval);
3856
3857	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3858	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3859}
3860
3861#define BYTES_TRANSFER(test, operation, size, type)			\
3862ATF_TC(test);								\
3863ATF_TC_HEAD(test, tc)							\
3864{									\
3865	atf_tc_set_md_var(tc, "descr",					\
3866	    "Verify bytes transfer operation" #operation " and size " #size \
3867	    " of type " #type);						\
3868}									\
3869									\
3870ATF_TC_BODY(test, tc)							\
3871{									\
3872									\
3873	bytes_transfer(operation, size, BYTES_TRANSFER_##type);		\
3874}
3875
3876// DATA
3877
3878BYTES_TRANSFER(bytes_transfer_piod_read_d_8, PIOD_READ_D, 8, DATAIO)
3879BYTES_TRANSFER(bytes_transfer_piod_read_d_16, PIOD_READ_D, 16, DATAIO)
3880BYTES_TRANSFER(bytes_transfer_piod_read_d_32, PIOD_READ_D, 32, DATAIO)
3881BYTES_TRANSFER(bytes_transfer_piod_read_d_64, PIOD_READ_D, 64, DATAIO)
3882
3883BYTES_TRANSFER(bytes_transfer_piod_read_i_8, PIOD_READ_I, 8, DATAIO)
3884BYTES_TRANSFER(bytes_transfer_piod_read_i_16, PIOD_READ_I, 16, DATAIO)
3885BYTES_TRANSFER(bytes_transfer_piod_read_i_32, PIOD_READ_I, 32, DATAIO)
3886BYTES_TRANSFER(bytes_transfer_piod_read_i_64, PIOD_READ_I, 64, DATAIO)
3887
3888BYTES_TRANSFER(bytes_transfer_piod_write_d_8, PIOD_WRITE_D, 8, DATAIO)
3889BYTES_TRANSFER(bytes_transfer_piod_write_d_16, PIOD_WRITE_D, 16, DATAIO)
3890BYTES_TRANSFER(bytes_transfer_piod_write_d_32, PIOD_WRITE_D, 32, DATAIO)
3891BYTES_TRANSFER(bytes_transfer_piod_write_d_64, PIOD_WRITE_D, 64, DATAIO)
3892
3893BYTES_TRANSFER(bytes_transfer_piod_write_i_8, PIOD_WRITE_I, 8, DATAIO)
3894BYTES_TRANSFER(bytes_transfer_piod_write_i_16, PIOD_WRITE_I, 16, DATAIO)
3895BYTES_TRANSFER(bytes_transfer_piod_write_i_32, PIOD_WRITE_I, 32, DATAIO)
3896BYTES_TRANSFER(bytes_transfer_piod_write_i_64, PIOD_WRITE_I, 64, DATAIO)
3897
3898BYTES_TRANSFER(bytes_transfer_read_d, PT_READ_D, 32, DATA)
3899BYTES_TRANSFER(bytes_transfer_read_i, PT_READ_I, 32, DATA)
3900BYTES_TRANSFER(bytes_transfer_write_d, PT_WRITE_D, 32, DATA)
3901BYTES_TRANSFER(bytes_transfer_write_i, PT_WRITE_I, 32, DATA)
3902
3903// TEXT
3904
3905BYTES_TRANSFER(bytes_transfer_piod_read_d_8_text, PIOD_READ_D, 8, TEXTIO)
3906BYTES_TRANSFER(bytes_transfer_piod_read_d_16_text, PIOD_READ_D, 16, TEXTIO)
3907BYTES_TRANSFER(bytes_transfer_piod_read_d_32_text, PIOD_READ_D, 32, TEXTIO)
3908BYTES_TRANSFER(bytes_transfer_piod_read_d_64_text, PIOD_READ_D, 64, TEXTIO)
3909
3910BYTES_TRANSFER(bytes_transfer_piod_read_i_8_text, PIOD_READ_I, 8, TEXTIO)
3911BYTES_TRANSFER(bytes_transfer_piod_read_i_16_text, PIOD_READ_I, 16, TEXTIO)
3912BYTES_TRANSFER(bytes_transfer_piod_read_i_32_text, PIOD_READ_I, 32, TEXTIO)
3913BYTES_TRANSFER(bytes_transfer_piod_read_i_64_text, PIOD_READ_I, 64, TEXTIO)
3914
3915BYTES_TRANSFER(bytes_transfer_piod_write_d_8_text, PIOD_WRITE_D, 8, TEXTIO)
3916BYTES_TRANSFER(bytes_transfer_piod_write_d_16_text, PIOD_WRITE_D, 16, TEXTIO)
3917BYTES_TRANSFER(bytes_transfer_piod_write_d_32_text, PIOD_WRITE_D, 32, TEXTIO)
3918BYTES_TRANSFER(bytes_transfer_piod_write_d_64_text, PIOD_WRITE_D, 64, TEXTIO)
3919
3920BYTES_TRANSFER(bytes_transfer_piod_write_i_8_text, PIOD_WRITE_I, 8, TEXTIO)
3921BYTES_TRANSFER(bytes_transfer_piod_write_i_16_text, PIOD_WRITE_I, 16, TEXTIO)
3922BYTES_TRANSFER(bytes_transfer_piod_write_i_32_text, PIOD_WRITE_I, 32, TEXTIO)
3923BYTES_TRANSFER(bytes_transfer_piod_write_i_64_text, PIOD_WRITE_I, 64, TEXTIO)
3924
3925BYTES_TRANSFER(bytes_transfer_read_d_text, PT_READ_D, 32, TEXT)
3926BYTES_TRANSFER(bytes_transfer_read_i_text, PT_READ_I, 32, TEXT)
3927BYTES_TRANSFER(bytes_transfer_write_d_text, PT_WRITE_D, 32, TEXT)
3928BYTES_TRANSFER(bytes_transfer_write_i_text, PT_WRITE_I, 32, TEXT)
3929
3930// AUXV
3931
3932BYTES_TRANSFER(bytes_transfer_piod_read_auxv, PIOD_READ_AUXV, 4096, AUXV)
3933
3934/// ----------------------------------------------------------------------------
3935
3936static void
3937bytes_transfer_alignment(const char *operation)
3938{
3939	const int exitval = 5;
3940	const int sigval = SIGSTOP;
3941	pid_t child, wpid;
3942#if defined(TWAIT_HAVE_STATUS)
3943	int status;
3944#endif
3945	char *buffer;
3946	int vector;
3947	size_t len;
3948	size_t i;
3949	int op;
3950
3951	struct ptrace_io_desc io;
3952	struct ptrace_siginfo info;
3953
3954	memset(&io, 0, sizeof(io));
3955	memset(&info, 0, sizeof(info));
3956
3957	/* Testing misaligned byte transfer crossing page boundaries */
3958	len = sysconf(_SC_PAGESIZE) * 2;
3959	buffer = malloc(len);
3960	ATF_REQUIRE(buffer != NULL);
3961
3962	/* Initialize the buffer with random data */
3963	for (i = 0; i < len; i++)
3964		buffer[i] = i & 0xff;
3965
3966	DPRINTF("Before forking process PID=%d\n", getpid());
3967	SYSCALL_REQUIRE((child = fork()) != -1);
3968	if (child == 0) {
3969		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3970		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3971
3972		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3973		FORKEE_ASSERT(raise(sigval) == 0);
3974
3975		DPRINTF("Before exiting of the child process\n");
3976		_exit(exitval);
3977	}
3978	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3979
3980	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3981	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3982
3983	validate_status_stopped(status, sigval);
3984
3985	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3986	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
3987		!= -1);
3988
3989	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3990	DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
3991		"si_errno=%#x\n",
3992		info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3993		info.psi_siginfo.si_errno);
3994
3995	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3996	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3997
3998	if (strcmp(operation, "PT_READ_I") == 0 ||
3999	    strcmp(operation, "PT_READ_D") == 0) {
4000		if (strcmp(operation, "PT_READ_I"))
4001			op = PT_READ_I;
4002		else
4003			op = PT_READ_D;
4004
4005		for (i = 0; i <= (len - sizeof(int)); i++) {
4006			errno = 0;
4007			vector = ptrace(op, child, buffer + i, 0);
4008			ATF_REQUIRE_EQ(errno, 0);
4009			ATF_REQUIRE(!memcmp(&vector, buffer + i, sizeof(int)));
4010		}
4011	} else if (strcmp(operation, "PT_WRITE_I") == 0 ||
4012	           strcmp(operation, "PT_WRITE_D") == 0) {
4013		if (strcmp(operation, "PT_WRITE_I"))
4014			op = PT_WRITE_I;
4015		else
4016			op = PT_WRITE_D;
4017
4018		for (i = 0; i <= (len - sizeof(int)); i++) {
4019			memcpy(&vector, buffer + i, sizeof(int));
4020			SYSCALL_REQUIRE(ptrace(op, child, buffer + 1, vector)
4021			    != -1);
4022		}
4023	} else if (strcmp(operation, "PIOD_READ_I") == 0 ||
4024	           strcmp(operation, "PIOD_READ_D") == 0) {
4025		if (strcmp(operation, "PIOD_READ_I"))
4026			op = PIOD_READ_I;
4027		else
4028			op = PIOD_READ_D;
4029
4030		io.piod_op = op;
4031		io.piod_addr = &vector;
4032		io.piod_len = sizeof(int);
4033
4034		for (i = 0; i <= (len - sizeof(int)); i++) {
4035			io.piod_offs = buffer + i;
4036
4037			SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io))
4038			                != -1);
4039			ATF_REQUIRE(!memcmp(&vector, buffer + i, sizeof(int)));
4040		}
4041	} else if (strcmp(operation, "PIOD_WRITE_I") == 0 ||
4042	           strcmp(operation, "PIOD_WRITE_D") == 0) {
4043		if (strcmp(operation, "PIOD_WRITE_I"))
4044			op = PIOD_WRITE_I;
4045		else
4046			op = PIOD_WRITE_D;
4047
4048		io.piod_op = op;
4049		io.piod_addr = &vector;
4050		io.piod_len = sizeof(int);
4051
4052		for (i = 0; i <= (len - sizeof(int)); i++) {
4053			io.piod_offs = buffer + i;
4054
4055			SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io))
4056			                != -1);
4057		}
4058	} else if (strcmp(operation, "PIOD_READ_AUXV") == 0) {
4059		io.piod_op = PIOD_READ_AUXV;
4060		io.piod_addr = &vector;
4061		io.piod_len = sizeof(int);
4062
4063		errno = 0;
4064		i = 0;
4065		/* Read the whole AUXV vector, it has no clear length */
4066		while (errno != EIO) {
4067			io.piod_offs = (void *)(intptr_t)i;
4068			SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io))
4069			                != -1 || (errno == EIO && i > 0));
4070			++i;
4071		}
4072	}
4073
4074	DPRINTF("Before resuming the child process where it left off "
4075	    "and without signal to be sent\n");
4076	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4077
4078	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4079	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
4080	    child);
4081
4082	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4083	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4084}
4085
4086#define BYTES_TRANSFER_ALIGNMENT(test, operation)			\
4087ATF_TC(test);								\
4088ATF_TC_HEAD(test, tc)							\
4089{									\
4090	atf_tc_set_md_var(tc, "descr",					\
4091	    "Verify bytes transfer for potentially misaligned "		\
4092	    "operation " operation);					\
4093}									\
4094									\
4095ATF_TC_BODY(test, tc)							\
4096{									\
4097									\
4098	bytes_transfer_alignment(operation);				\
4099}
4100
4101BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_read_i, "PT_READ_I")
4102BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_read_d, "PT_READ_D")
4103BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_write_i, "PT_WRITE_I")
4104BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_write_d, "PT_WRITE_D")
4105
4106BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_read_i, "PIOD_READ_I")
4107BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_read_d, "PIOD_READ_D")
4108BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_write_i, "PIOD_WRITE_I")
4109BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_write_d, "PIOD_WRITE_D")
4110
4111BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_read_auxv, "PIOD_READ_AUXV")
4112
4113/// ----------------------------------------------------------------------------
4114
4115static void
4116bytes_transfer_eof(const char *operation)
4117{
4118	const int exitval = 5;
4119	const int sigval = SIGSTOP;
4120	pid_t child, wpid;
4121#if defined(TWAIT_HAVE_STATUS)
4122	int status;
4123#endif
4124	FILE *fp;
4125	char *p;
4126	int vector;
4127	int op;
4128
4129	struct ptrace_io_desc io;
4130	struct ptrace_siginfo info;
4131
4132	memset(&io, 0, sizeof(io));
4133	memset(&info, 0, sizeof(info));
4134
4135	vector = 0;
4136
4137	fp = tmpfile();
4138	ATF_REQUIRE(fp != NULL);
4139
4140	p = mmap(0, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(fp), 0);
4141	ATF_REQUIRE(p != MAP_FAILED);
4142
4143	DPRINTF("Before forking process PID=%d\n", getpid());
4144	SYSCALL_REQUIRE((child = fork()) != -1);
4145	if (child == 0) {
4146		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4147		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4148
4149		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4150		FORKEE_ASSERT(raise(sigval) == 0);
4151
4152		DPRINTF("Before exiting of the child process\n");
4153		_exit(exitval);
4154	}
4155	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4156
4157	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4158	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4159
4160	validate_status_stopped(status, sigval);
4161
4162	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4163	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
4164		!= -1);
4165
4166	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4167	DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
4168		"si_errno=%#x\n",
4169		info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4170		info.psi_siginfo.si_errno);
4171
4172	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
4173	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
4174
4175	if (strcmp(operation, "PT_READ_I") == 0 ||
4176	    strcmp(operation, "PT_READ_D") == 0) {
4177		if (strcmp(operation, "PT_READ_I"))
4178			op = PT_READ_I;
4179		else
4180			op = PT_READ_D;
4181
4182		errno = 0;
4183		SYSCALL_REQUIRE(ptrace(op, child, p, 0) == -1);
4184		ATF_REQUIRE_EQ(errno, EINVAL);
4185	} else if (strcmp(operation, "PT_WRITE_I") == 0 ||
4186	           strcmp(operation, "PT_WRITE_D") == 0) {
4187		if (strcmp(operation, "PT_WRITE_I"))
4188			op = PT_WRITE_I;
4189		else
4190			op = PT_WRITE_D;
4191
4192		errno = 0;
4193		SYSCALL_REQUIRE(ptrace(op, child, p, vector) == -1);
4194		ATF_REQUIRE_EQ(errno, EINVAL);
4195	} else if (strcmp(operation, "PIOD_READ_I") == 0 ||
4196	           strcmp(operation, "PIOD_READ_D") == 0) {
4197		if (strcmp(operation, "PIOD_READ_I"))
4198			op = PIOD_READ_I;
4199		else
4200			op = PIOD_READ_D;
4201
4202		io.piod_op = op;
4203		io.piod_addr = &vector;
4204		io.piod_len = sizeof(int);
4205		io.piod_offs = p;
4206
4207		errno = 0;
4208		SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io)) == -1);
4209		ATF_REQUIRE_EQ(errno, EINVAL);
4210	} else if (strcmp(operation, "PIOD_WRITE_I") == 0 ||
4211	           strcmp(operation, "PIOD_WRITE_D") == 0) {
4212		if (strcmp(operation, "PIOD_WRITE_I"))
4213			op = PIOD_WRITE_I;
4214		else
4215			op = PIOD_WRITE_D;
4216
4217		io.piod_op = op;
4218		io.piod_addr = &vector;
4219		io.piod_len = sizeof(int);
4220		io.piod_offs = p;
4221
4222		errno = 0;
4223		SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io)) == -1);
4224		ATF_REQUIRE_EQ(errno, EINVAL);
4225	}
4226
4227	DPRINTF("Before resuming the child process where it left off "
4228	    "and without signal to be sent\n");
4229	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4230
4231	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4232	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
4233	    child);
4234
4235	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4236	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4237}
4238
4239#define BYTES_TRANSFER_EOF(test, operation)				\
4240ATF_TC(test);								\
4241ATF_TC_HEAD(test, tc)							\
4242{									\
4243	atf_tc_set_md_var(tc, "descr",					\
4244	    "Verify bytes EOF byte transfer for the " operation		\
4245	    " operation");						\
4246}									\
4247									\
4248ATF_TC_BODY(test, tc)							\
4249{									\
4250									\
4251	bytes_transfer_eof(operation);					\
4252}
4253
4254BYTES_TRANSFER_EOF(bytes_transfer_eof_pt_read_i, "PT_READ_I")
4255BYTES_TRANSFER_EOF(bytes_transfer_eof_pt_read_d, "PT_READ_D")
4256BYTES_TRANSFER_EOF(bytes_transfer_eof_pt_write_i, "PT_WRITE_I")
4257BYTES_TRANSFER_EOF(bytes_transfer_eof_pt_write_d, "PT_WRITE_D")
4258
4259BYTES_TRANSFER_EOF(bytes_transfer_eof_piod_read_i, "PIOD_READ_I")
4260BYTES_TRANSFER_EOF(bytes_transfer_eof_piod_read_d, "PIOD_READ_D")
4261BYTES_TRANSFER_EOF(bytes_transfer_eof_piod_write_i, "PIOD_WRITE_I")
4262BYTES_TRANSFER_EOF(bytes_transfer_eof_piod_write_d, "PIOD_WRITE_D")
4263
4264/// ----------------------------------------------------------------------------
4265
4266#if defined(HAVE_GPREGS) || defined(HAVE_FPREGS)
4267static void
4268access_regs(const char *regset, const char *aux)
4269{
4270	const int exitval = 5;
4271	const int sigval = SIGSTOP;
4272	pid_t child, wpid;
4273#if defined(TWAIT_HAVE_STATUS)
4274	int status;
4275#endif
4276#if defined(HAVE_GPREGS)
4277	struct reg gpr;
4278	register_t rgstr;
4279#endif
4280#if defined(HAVE_FPREGS)
4281	struct fpreg fpr;
4282#endif
4283
4284#if !defined(HAVE_GPREGS)
4285	if (strcmp(regset, "regs") == 0)
4286		atf_tc_fail("Impossible test scenario!");
4287#endif
4288
4289#if !defined(HAVE_FPREGS)
4290	if (strcmp(regset, "fpregs") == 0)
4291		atf_tc_fail("Impossible test scenario!");
4292#endif
4293
4294	DPRINTF("Before forking process PID=%d\n", getpid());
4295	SYSCALL_REQUIRE((child = fork()) != -1);
4296	if (child == 0) {
4297		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4298		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4299
4300		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4301		FORKEE_ASSERT(raise(sigval) == 0);
4302
4303		DPRINTF("Before exiting of the child process\n");
4304		_exit(exitval);
4305	}
4306	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4307
4308	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4309	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4310
4311	validate_status_stopped(status, sigval);
4312
4313#if defined(HAVE_GPREGS)
4314	if (strcmp(regset, "regs") == 0) {
4315		DPRINTF("Call GETREGS for the child process\n");
4316		SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1);
4317
4318		if (strcmp(aux, "none") == 0) {
4319			DPRINTF("Retrieved registers\n");
4320		} else if (strcmp(aux, "pc") == 0) {
4321			rgstr = PTRACE_REG_PC(&gpr);
4322			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
4323		} else if (strcmp(aux, "set_pc") == 0) {
4324			rgstr = PTRACE_REG_PC(&gpr);
4325			PTRACE_REG_SET_PC(&gpr, rgstr);
4326		} else if (strcmp(aux, "sp") == 0) {
4327			rgstr = PTRACE_REG_SP(&gpr);
4328			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
4329		} else if (strcmp(aux, "intrv") == 0) {
4330			rgstr = PTRACE_REG_INTRV(&gpr);
4331			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
4332		} else if (strcmp(aux, "setregs") == 0) {
4333			DPRINTF("Call SETREGS for the child process\n");
4334			SYSCALL_REQUIRE(
4335			    ptrace(PT_GETREGS, child, &gpr, 0) != -1);
4336		}
4337	}
4338#endif
4339
4340#if defined(HAVE_FPREGS)
4341	if (strcmp(regset, "fpregs") == 0) {
4342		DPRINTF("Call GETFPREGS for the child process\n");
4343		SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &fpr, 0) != -1);
4344
4345		if (strcmp(aux, "getfpregs") == 0) {
4346			DPRINTF("Retrieved FP registers\n");
4347		} else if (strcmp(aux, "setfpregs") == 0) {
4348			DPRINTF("Call SETFPREGS for the child\n");
4349			SYSCALL_REQUIRE(
4350			    ptrace(PT_SETFPREGS, child, &fpr, 0) != -1);
4351		}
4352	}
4353#endif
4354
4355	DPRINTF("Before resuming the child process where it left off and "
4356	    "without signal to be sent\n");
4357	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4358
4359	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4360	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4361
4362	validate_status_exited(status, exitval);
4363
4364	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4365	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4366}
4367
4368#define ACCESS_REGS(test, regset, aux)					\
4369ATF_TC(test);								\
4370ATF_TC_HEAD(test, tc)							\
4371{									\
4372        atf_tc_set_md_var(tc, "descr",					\
4373            "Verify " regset " with auxiliary operation: " aux);	\
4374}									\
4375									\
4376ATF_TC_BODY(test, tc)							\
4377{									\
4378									\
4379        access_regs(regset, aux);					\
4380}
4381#endif
4382
4383#if defined(HAVE_GPREGS)
4384ACCESS_REGS(access_regs1, "regs", "none")
4385ACCESS_REGS(access_regs2, "regs", "pc")
4386ACCESS_REGS(access_regs3, "regs", "set_pc")
4387ACCESS_REGS(access_regs4, "regs", "sp")
4388ACCESS_REGS(access_regs5, "regs", "intrv")
4389ACCESS_REGS(access_regs6, "regs", "setregs")
4390#endif
4391#if defined(HAVE_FPREGS)
4392ACCESS_REGS(access_fpregs1, "fpregs", "getfpregs")
4393ACCESS_REGS(access_fpregs2, "fpregs", "setfpregs")
4394#endif
4395
4396/// ----------------------------------------------------------------------------
4397
4398#if defined(PT_STEP)
4399static void
4400ptrace_step(int N, int setstep, bool masked, bool ignored)
4401{
4402	const int exitval = 5;
4403	const int sigval = SIGSTOP;
4404	pid_t child, wpid;
4405#if defined(TWAIT_HAVE_STATUS)
4406	int status;
4407#endif
4408	int happy;
4409	struct sigaction sa;
4410	struct ptrace_siginfo info;
4411	sigset_t intmask;
4412	struct kinfo_proc2 kp;
4413	size_t len = sizeof(kp);
4414
4415	int name[6];
4416	const size_t namelen = __arraycount(name);
4417	ki_sigset_t kp_sigmask;
4418	ki_sigset_t kp_sigignore;
4419
4420#if defined(__arm__)
4421	/* PT_STEP not supported on arm 32-bit */
4422	atf_tc_expect_fail("PR kern/52119");
4423#endif
4424
4425	DPRINTF("Before forking process PID=%d\n", getpid());
4426	SYSCALL_REQUIRE((child = fork()) != -1);
4427	if (child == 0) {
4428		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4429		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4430
4431		if (masked) {
4432			sigemptyset(&intmask);
4433			sigaddset(&intmask, SIGTRAP);
4434			sigprocmask(SIG_BLOCK, &intmask, NULL);
4435		}
4436
4437		if (ignored) {
4438			memset(&sa, 0, sizeof(sa));
4439			sa.sa_handler = SIG_IGN;
4440			sigemptyset(&sa.sa_mask);
4441			FORKEE_ASSERT(sigaction(SIGTRAP, &sa, NULL) != -1);
4442		}
4443
4444		happy = check_happy(999);
4445
4446		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4447		FORKEE_ASSERT(raise(sigval) == 0);
4448
4449		FORKEE_ASSERT_EQ(happy, check_happy(999));
4450
4451		DPRINTF("Before exiting of the child process\n");
4452		_exit(exitval);
4453	}
4454	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4455
4456	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4457	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4458
4459	validate_status_stopped(status, sigval);
4460
4461	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4462	SYSCALL_REQUIRE(
4463	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4464
4465	DPRINTF("Before checking siginfo_t\n");
4466	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
4467	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
4468
4469	name[0] = CTL_KERN,
4470	name[1] = KERN_PROC2,
4471	name[2] = KERN_PROC_PID;
4472	name[3] = child;
4473	name[4] = sizeof(kp);
4474	name[5] = 1;
4475
4476	FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
4477
4478	if (masked)
4479		kp_sigmask = kp.p_sigmask;
4480
4481	if (ignored)
4482		kp_sigignore = kp.p_sigignore;
4483
4484	while (N --> 0) {
4485		if (setstep) {
4486			DPRINTF("Before resuming the child process where it "
4487			    "left off and without signal to be sent (use "
4488			    "PT_SETSTEP and PT_CONTINUE)\n");
4489			SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1);
4490			SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0)
4491			    != -1);
4492		} else {
4493			DPRINTF("Before resuming the child process where it "
4494			    "left off and without signal to be sent (use "
4495			    "PT_STEP)\n");
4496			SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0)
4497			    != -1);
4498		}
4499
4500		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4501		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
4502		    child);
4503
4504		validate_status_stopped(status, SIGTRAP);
4505
4506		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4507		SYSCALL_REQUIRE(
4508		    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4509
4510		DPRINTF("Before checking siginfo_t\n");
4511		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
4512		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
4513
4514		if (setstep) {
4515			SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1);
4516		}
4517
4518		ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
4519
4520		if (masked) {
4521			DPRINTF("kp_sigmask="
4522			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
4523			    PRIx32 "\n",
4524			    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
4525			    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
4526
4527			DPRINTF("kp.p_sigmask="
4528			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
4529			    PRIx32 "\n",
4530			    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
4531			    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
4532
4533			ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
4534			    sizeof(kp_sigmask)));
4535		}
4536
4537		if (ignored) {
4538			DPRINTF("kp_sigignore="
4539			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
4540			    PRIx32 "\n",
4541			    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
4542			    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
4543
4544			DPRINTF("kp.p_sigignore="
4545			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
4546			    PRIx32 "\n",
4547			    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
4548			    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
4549
4550			ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
4551			    sizeof(kp_sigignore)));
4552		}
4553	}
4554
4555	DPRINTF("Before resuming the child process where it left off and "
4556	    "without signal to be sent\n");
4557	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4558
4559	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4560	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4561
4562	validate_status_exited(status, exitval);
4563
4564	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4565	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4566}
4567
4568#define PTRACE_STEP(test, N, setstep)					\
4569ATF_TC(test);								\
4570ATF_TC_HEAD(test, tc)							\
4571{									\
4572        atf_tc_set_md_var(tc, "descr",					\
4573            "Verify " #N " (PT_SETSTEP set to: " #setstep ")");		\
4574}									\
4575									\
4576ATF_TC_BODY(test, tc)							\
4577{									\
4578									\
4579        ptrace_step(N, setstep, false, false);				\
4580}
4581
4582PTRACE_STEP(step1, 1, 0)
4583PTRACE_STEP(step2, 2, 0)
4584PTRACE_STEP(step3, 3, 0)
4585PTRACE_STEP(step4, 4, 0)
4586PTRACE_STEP(setstep1, 1, 1)
4587PTRACE_STEP(setstep2, 2, 1)
4588PTRACE_STEP(setstep3, 3, 1)
4589PTRACE_STEP(setstep4, 4, 1)
4590
4591ATF_TC(step_signalmasked);
4592ATF_TC_HEAD(step_signalmasked, tc)
4593{
4594	atf_tc_set_md_var(tc, "descr", "Verify PT_STEP with masked SIGTRAP");
4595}
4596
4597ATF_TC_BODY(step_signalmasked, tc)
4598{
4599
4600	ptrace_step(1, 0, true, false);
4601}
4602
4603ATF_TC(step_signalignored);
4604ATF_TC_HEAD(step_signalignored, tc)
4605{
4606	atf_tc_set_md_var(tc, "descr", "Verify PT_STEP with ignored SIGTRAP");
4607}
4608
4609ATF_TC_BODY(step_signalignored, tc)
4610{
4611
4612	ptrace_step(1, 0, false, true);
4613}
4614#endif
4615
4616/// ----------------------------------------------------------------------------
4617
4618static void
4619ptrace_kill(const char *type)
4620{
4621	const int sigval = SIGSTOP;
4622	pid_t child, wpid;
4623#if defined(TWAIT_HAVE_STATUS)
4624	int status;
4625#endif
4626
4627	DPRINTF("Before forking process PID=%d\n", getpid());
4628	SYSCALL_REQUIRE((child = fork()) != -1);
4629	if (child == 0) {
4630		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4631		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4632
4633		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4634		FORKEE_ASSERT(raise(sigval) == 0);
4635
4636		/* NOTREACHED */
4637		FORKEE_ASSERTX(0 &&
4638		    "Child should be terminated by a signal from its parent");
4639	}
4640	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4641
4642	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4643	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4644
4645	validate_status_stopped(status, sigval);
4646
4647	DPRINTF("Before killing the child process with %s\n", type);
4648	if (strcmp(type, "ptrace(PT_KILL)") == 0) {
4649		SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
4650	} else if (strcmp(type, "kill(SIGKILL)") == 0) {
4651		kill(child, SIGKILL);
4652	} else if (strcmp(type, "killpg(SIGKILL)") == 0) {
4653		setpgid(child, 0);
4654		killpg(getpgid(child), SIGKILL);
4655	}
4656
4657	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4658	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4659
4660	validate_status_signaled(status, SIGKILL, 0);
4661
4662	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4663	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4664}
4665
4666#define PTRACE_KILL(test, type)						\
4667ATF_TC(test);								\
4668ATF_TC_HEAD(test, tc)							\
4669{									\
4670        atf_tc_set_md_var(tc, "descr",					\
4671            "Verify killing the child with " type);			\
4672}									\
4673									\
4674ATF_TC_BODY(test, tc)							\
4675{									\
4676									\
4677        ptrace_kill(type);						\
4678}
4679
4680// PT_CONTINUE with SIGKILL is covered by traceme_sendsignal_simple1
4681PTRACE_KILL(kill1, "ptrace(PT_KILL)")
4682PTRACE_KILL(kill2, "kill(SIGKILL)")
4683PTRACE_KILL(kill3, "killpg(SIGKILL)")
4684
4685/// ----------------------------------------------------------------------------
4686
4687static void
4688traceme_lwpinfo(const int threads)
4689{
4690	const int sigval = SIGSTOP;
4691	const int sigval2 = SIGINT;
4692	pid_t child, wpid;
4693#if defined(TWAIT_HAVE_STATUS)
4694	int status;
4695#endif
4696	struct ptrace_lwpinfo lwp = {0, 0};
4697	struct ptrace_siginfo info;
4698
4699	/* Maximum number of supported threads in this test */
4700	pthread_t t[3];
4701	int n, rv;
4702
4703	ATF_REQUIRE((int)__arraycount(t) >= threads);
4704
4705	DPRINTF("Before forking process PID=%d\n", getpid());
4706	SYSCALL_REQUIRE((child = fork()) != -1);
4707	if (child == 0) {
4708		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4709		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4710
4711		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4712		FORKEE_ASSERT(raise(sigval) == 0);
4713
4714		for (n = 0; n < threads; n++) {
4715			rv = pthread_create(&t[n], NULL, infinite_thread, NULL);
4716			FORKEE_ASSERT(rv == 0);
4717		}
4718
4719		DPRINTF("Before raising %s from child\n", strsignal(sigval2));
4720		FORKEE_ASSERT(raise(sigval2) == 0);
4721
4722		/* NOTREACHED */
4723		FORKEE_ASSERTX(0 && "Not reached");
4724	}
4725	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4726
4727	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4728	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4729
4730	validate_status_stopped(status, sigval);
4731
4732	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child");
4733	SYSCALL_REQUIRE(
4734	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4735
4736	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4737	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4738	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4739	    info.psi_siginfo.si_errno);
4740
4741	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
4742	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
4743
4744	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4745	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &lwp, sizeof(lwp)) != -1);
4746
4747	DPRINTF("Assert that there exists a single thread only\n");
4748	ATF_REQUIRE(lwp.pl_lwpid > 0);
4749
4750	DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
4751	    lwp.pl_lwpid);
4752	FORKEE_ASSERT_EQ(lwp.pl_event, PL_EVENT_SIGNAL);
4753
4754	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4755	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &lwp, sizeof(lwp)) != -1);
4756
4757	DPRINTF("Assert that there exists a single thread only\n");
4758	ATF_REQUIRE_EQ(lwp.pl_lwpid, 0);
4759
4760	DPRINTF("Before resuming the child process where it left off and "
4761	    "without signal to be sent\n");
4762	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4763
4764	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4765	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4766
4767	validate_status_stopped(status, sigval2);
4768
4769	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child");
4770	SYSCALL_REQUIRE(
4771	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4772
4773	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4774	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4775	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4776	    info.psi_siginfo.si_errno);
4777
4778	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval2);
4779	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
4780
4781	memset(&lwp, 0, sizeof(lwp));
4782
4783	for (n = 0; n <= threads; n++) {
4784		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4785		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &lwp, sizeof(lwp)) != -1);
4786		DPRINTF("LWP=%d\n", lwp.pl_lwpid);
4787
4788		DPRINTF("Assert that the thread exists\n");
4789		ATF_REQUIRE(lwp.pl_lwpid > 0);
4790
4791		DPRINTF("Assert that lwp thread %d received expected event\n",
4792		    lwp.pl_lwpid);
4793		FORKEE_ASSERT_EQ(lwp.pl_event, info.psi_lwpid == lwp.pl_lwpid ?
4794		    PL_EVENT_SIGNAL : PL_EVENT_NONE);
4795	}
4796	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4797	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &lwp, sizeof(lwp)) != -1);
4798	DPRINTF("LWP=%d\n", lwp.pl_lwpid);
4799
4800	DPRINTF("Assert that there are no more threads\n");
4801	ATF_REQUIRE_EQ(lwp.pl_lwpid, 0);
4802
4803	DPRINTF("Before resuming the child process where it left off and "
4804	    "without signal to be sent\n");
4805	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, SIGKILL) != -1);
4806
4807	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4808	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4809
4810	validate_status_signaled(status, SIGKILL, 0);
4811
4812	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4813	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4814}
4815
4816#define TRACEME_LWPINFO(test, threads)					\
4817ATF_TC(test);								\
4818ATF_TC_HEAD(test, tc)							\
4819{									\
4820	atf_tc_set_md_var(tc, "descr",					\
4821	    "Verify LWPINFO with the child with " #threads		\
4822	    " spawned extra threads");					\
4823}									\
4824									\
4825ATF_TC_BODY(test, tc)							\
4826{									\
4827									\
4828	traceme_lwpinfo(threads);					\
4829}
4830
4831TRACEME_LWPINFO(traceme_lwpinfo0, 0)
4832TRACEME_LWPINFO(traceme_lwpinfo1, 1)
4833TRACEME_LWPINFO(traceme_lwpinfo2, 2)
4834TRACEME_LWPINFO(traceme_lwpinfo3, 3)
4835
4836/// ----------------------------------------------------------------------------
4837
4838#if defined(TWAIT_HAVE_PID)
4839static void
4840attach_lwpinfo(const int threads)
4841{
4842	const int sigval = SIGINT;
4843	struct msg_fds parent_tracee, parent_tracer;
4844	const int exitval_tracer = 10;
4845	pid_t tracee, tracer, wpid;
4846	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
4847#if defined(TWAIT_HAVE_STATUS)
4848	int status;
4849#endif
4850	struct ptrace_lwpinfo lwp = {0, 0};
4851	struct ptrace_siginfo info;
4852
4853	/* Maximum number of supported threads in this test */
4854	pthread_t t[3];
4855	int n, rv;
4856
4857	DPRINTF("Spawn tracee\n");
4858	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
4859	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
4860	tracee = atf_utils_fork();
4861	if (tracee == 0) {
4862		/* Wait for message from the parent */
4863		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
4864
4865		CHILD_FROM_PARENT("spawn threads", parent_tracee, msg);
4866
4867		for (n = 0; n < threads; n++) {
4868			rv = pthread_create(&t[n], NULL, infinite_thread, NULL);
4869			FORKEE_ASSERT(rv == 0);
4870		}
4871
4872		CHILD_TO_PARENT("tracee exit", parent_tracee, msg);
4873
4874		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4875		FORKEE_ASSERT(raise(sigval) == 0);
4876
4877		/* NOTREACHED */
4878		FORKEE_ASSERTX(0 && "Not reached");
4879	}
4880	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
4881
4882	DPRINTF("Spawn debugger\n");
4883	tracer = atf_utils_fork();
4884	if (tracer == 0) {
4885		/* No IPC to communicate with the child */
4886		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
4887		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
4888
4889		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
4890		FORKEE_REQUIRE_SUCCESS(
4891		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4892
4893		forkee_status_stopped(status, SIGSTOP);
4894
4895		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
4896		    "tracee");
4897		FORKEE_ASSERT(
4898		    ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
4899
4900		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4901		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
4902		    "si_errno=%#x\n",
4903		    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4904		    info.psi_siginfo.si_errno);
4905
4906		FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, SIGSTOP);
4907		FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_USER);
4908
4909		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4910		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &lwp, sizeof(lwp))
4911		    != -1);
4912
4913		DPRINTF("Assert that there exists a thread\n");
4914		FORKEE_ASSERTX(lwp.pl_lwpid > 0);
4915
4916		DPRINTF("Assert that lwp thread %d received event "
4917		    "PL_EVENT_SIGNAL\n", lwp.pl_lwpid);
4918		FORKEE_ASSERT_EQ(lwp.pl_event, PL_EVENT_SIGNAL);
4919
4920		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for "
4921		    "tracee\n");
4922		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &lwp, sizeof(lwp))
4923		    != -1);
4924
4925		DPRINTF("Assert that there are no more lwp threads in "
4926		    "tracee\n");
4927		FORKEE_ASSERT_EQ(lwp.pl_lwpid, 0);
4928
4929		/* Resume tracee with PT_CONTINUE */
4930		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
4931
4932		/* Inform parent that tracer has attached to tracee */
4933		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
4934
4935		/* Wait for parent */
4936		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
4937
4938		/* Wait for tracee and assert that it raised a signal */
4939		FORKEE_REQUIRE_SUCCESS(
4940		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4941
4942		forkee_status_stopped(status, SIGINT);
4943
4944		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
4945		    "child");
4946		FORKEE_ASSERT(
4947		    ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
4948
4949		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4950		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
4951		    "si_errno=%#x\n",
4952		    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4953		    info.psi_siginfo.si_errno);
4954
4955		FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, sigval);
4956		FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_LWP);
4957
4958		memset(&lwp, 0, sizeof(lwp));
4959
4960		for (n = 0; n <= threads; n++) {
4961			DPRINTF("Before calling ptrace(2) with PT_LWPINFO for "
4962			    "child\n");
4963			FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &lwp,
4964			    sizeof(lwp)) != -1);
4965			DPRINTF("LWP=%d\n", lwp.pl_lwpid);
4966
4967			DPRINTF("Assert that the thread exists\n");
4968			FORKEE_ASSERT(lwp.pl_lwpid > 0);
4969
4970			DPRINTF("Assert that lwp thread %d received expected "
4971			    "event\n", lwp.pl_lwpid);
4972			FORKEE_ASSERT_EQ(lwp.pl_event,
4973			    info.psi_lwpid == lwp.pl_lwpid ?
4974			    PL_EVENT_SIGNAL : PL_EVENT_NONE);
4975		}
4976		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for "
4977		    "tracee\n");
4978		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &lwp, sizeof(lwp))
4979		    != -1);
4980		DPRINTF("LWP=%d\n", lwp.pl_lwpid);
4981
4982		DPRINTF("Assert that there are no more threads\n");
4983		FORKEE_ASSERT_EQ(lwp.pl_lwpid, 0);
4984
4985		DPRINTF("Before resuming the child process where it left off "
4986		    "and without signal to be sent\n");
4987		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, SIGKILL)
4988		    != -1);
4989
4990		/* Wait for tracee and assert that it exited */
4991		FORKEE_REQUIRE_SUCCESS(
4992		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4993
4994		forkee_status_signaled(status, SIGKILL, 0);
4995
4996		DPRINTF("Before exiting of the tracer process\n");
4997		_exit(exitval_tracer);
4998	}
4999
5000	DPRINTF("Wait for the tracer to attach to the tracee\n");
5001	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
5002
5003	DPRINTF("Resume the tracee and spawn threads\n");
5004	PARENT_TO_CHILD("spawn threads", parent_tracee, msg);
5005
5006	DPRINTF("Resume the tracee and let it exit\n");
5007	PARENT_FROM_CHILD("tracee exit", parent_tracee, msg);
5008
5009	DPRINTF("Resume the tracer and let it detect multiple threads\n");
5010	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
5011
5012	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
5013	    TWAIT_FNAME);
5014	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
5015	    tracer);
5016
5017	validate_status_exited(status, exitval_tracer);
5018
5019	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
5020	    TWAIT_FNAME);
5021	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
5022	    tracee);
5023
5024	validate_status_signaled(status, SIGKILL, 0);
5025
5026	msg_close(&parent_tracer);
5027	msg_close(&parent_tracee);
5028}
5029
5030#define ATTACH_LWPINFO(test, threads)					\
5031ATF_TC(test);								\
5032ATF_TC_HEAD(test, tc)							\
5033{									\
5034	atf_tc_set_md_var(tc, "descr",					\
5035	    "Verify LWPINFO with the child with " #threads		\
5036	    " spawned extra threads (tracer is not the original "	\
5037	    "parent)");							\
5038}									\
5039									\
5040ATF_TC_BODY(test, tc)							\
5041{									\
5042									\
5043	attach_lwpinfo(threads);					\
5044}
5045
5046ATTACH_LWPINFO(attach_lwpinfo0, 0)
5047ATTACH_LWPINFO(attach_lwpinfo1, 1)
5048ATTACH_LWPINFO(attach_lwpinfo2, 2)
5049ATTACH_LWPINFO(attach_lwpinfo3, 3)
5050#endif
5051
5052/// ----------------------------------------------------------------------------
5053
5054static void
5055ptrace_siginfo(bool faked, void (*sah)(int a, siginfo_t *b, void *c), int *signal_caught)
5056{
5057	const int exitval = 5;
5058	const int sigval = SIGINT;
5059	const int sigfaked = SIGTRAP;
5060	const int sicodefaked = TRAP_BRKPT;
5061	pid_t child, wpid;
5062	struct sigaction sa;
5063#if defined(TWAIT_HAVE_STATUS)
5064	int status;
5065#endif
5066	struct ptrace_siginfo info;
5067	memset(&info, 0, sizeof(info));
5068
5069	DPRINTF("Before forking process PID=%d\n", getpid());
5070	SYSCALL_REQUIRE((child = fork()) != -1);
5071	if (child == 0) {
5072		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5073		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5074
5075		sa.sa_sigaction = sah;
5076		sa.sa_flags = SA_SIGINFO;
5077		sigemptyset(&sa.sa_mask);
5078
5079		FORKEE_ASSERT(sigaction(faked ? sigfaked : sigval, &sa, NULL)
5080		    != -1);
5081
5082		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5083		FORKEE_ASSERT(raise(sigval) == 0);
5084
5085		FORKEE_ASSERT_EQ(*signal_caught, 1);
5086
5087		DPRINTF("Before exiting of the child process\n");
5088		_exit(exitval);
5089	}
5090	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5091
5092	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5093	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5094
5095	validate_status_stopped(status, sigval);
5096
5097	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5098	SYSCALL_REQUIRE(
5099	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5100
5101	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5102	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5103	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5104	    info.psi_siginfo.si_errno);
5105
5106	if (faked) {
5107		DPRINTF("Before setting new faked signal to signo=%d "
5108		    "si_code=%d\n", sigfaked, sicodefaked);
5109		info.psi_siginfo.si_signo = sigfaked;
5110		info.psi_siginfo.si_code = sicodefaked;
5111	}
5112
5113	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
5114	SYSCALL_REQUIRE(
5115	    ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
5116
5117	if (faked) {
5118		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
5119		    "child\n");
5120		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
5121		    sizeof(info)) != -1);
5122
5123		DPRINTF("Before checking siginfo_t\n");
5124		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
5125		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
5126	}
5127
5128	DPRINTF("Before resuming the child process where it left off and "
5129	    "without signal to be sent\n");
5130	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1,
5131	    faked ? sigfaked : sigval) != -1);
5132
5133	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5134	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5135
5136	validate_status_exited(status, exitval);
5137
5138	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5139	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5140}
5141
5142#define PTRACE_SIGINFO(test, faked)					\
5143ATF_TC(test);								\
5144ATF_TC_HEAD(test, tc)							\
5145{									\
5146	atf_tc_set_md_var(tc, "descr",					\
5147	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls"	\
5148	    "with%s setting signal to new value", faked ? "" : "out");	\
5149}									\
5150									\
5151static int test##_caught = 0;						\
5152									\
5153static void								\
5154test##_sighandler(int sig, siginfo_t *info, void *ctx)			\
5155{									\
5156	if (faked) {							\
5157		FORKEE_ASSERT_EQ(sig, SIGTRAP);				\
5158		FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);		\
5159		FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);		\
5160	} else {							\
5161		FORKEE_ASSERT_EQ(sig, SIGINT);				\
5162		FORKEE_ASSERT_EQ(info->si_signo, SIGINT);		\
5163		FORKEE_ASSERT_EQ(info->si_code, SI_LWP);		\
5164	}								\
5165									\
5166	++ test##_caught;						\
5167}									\
5168									\
5169ATF_TC_BODY(test, tc)							\
5170{									\
5171									\
5172	ptrace_siginfo(faked, test##_sighandler, & test##_caught); 	\
5173}
5174
5175PTRACE_SIGINFO(siginfo_set_unmodified, false)
5176PTRACE_SIGINFO(siginfo_set_faked, true)
5177
5178/// ----------------------------------------------------------------------------
5179
5180static void
5181traceme_exec(bool masked, bool ignored)
5182{
5183	const int sigval = SIGTRAP;
5184	pid_t child, wpid;
5185#if defined(TWAIT_HAVE_STATUS)
5186	int status;
5187#endif
5188	struct sigaction sa;
5189	struct ptrace_siginfo info;
5190	sigset_t intmask;
5191	struct kinfo_proc2 kp;
5192	size_t len = sizeof(kp);
5193
5194	int name[6];
5195	const size_t namelen = __arraycount(name);
5196	ki_sigset_t kp_sigmask;
5197	ki_sigset_t kp_sigignore;
5198
5199	memset(&info, 0, sizeof(info));
5200
5201	DPRINTF("Before forking process PID=%d\n", getpid());
5202	SYSCALL_REQUIRE((child = fork()) != -1);
5203	if (child == 0) {
5204		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5205		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5206
5207		if (masked) {
5208			sigemptyset(&intmask);
5209			sigaddset(&intmask, sigval);
5210			sigprocmask(SIG_BLOCK, &intmask, NULL);
5211		}
5212
5213		if (ignored) {
5214			memset(&sa, 0, sizeof(sa));
5215			sa.sa_handler = SIG_IGN;
5216			sigemptyset(&sa.sa_mask);
5217			FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
5218		}
5219
5220		DPRINTF("Before calling execve(2) from child\n");
5221		execlp("/bin/echo", "/bin/echo", NULL);
5222
5223		FORKEE_ASSERT(0 && "Not reached");
5224	}
5225	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5226
5227	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5228	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5229
5230	validate_status_stopped(status, sigval);
5231
5232	name[0] = CTL_KERN,
5233	name[1] = KERN_PROC2,
5234	name[2] = KERN_PROC_PID;
5235	name[3] = getpid();
5236	name[4] = sizeof(kp);
5237	name[5] = 1;
5238
5239	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
5240
5241	if (masked)
5242		kp_sigmask = kp.p_sigmask;
5243
5244	if (ignored)
5245		kp_sigignore = kp.p_sigignore;
5246
5247	name[3] = getpid();
5248
5249	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
5250
5251	if (masked) {
5252		DPRINTF("kp_sigmask="
5253		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
5254		    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
5255		    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
5256
5257		DPRINTF("kp.p_sigmask="
5258		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
5259		    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
5260		    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
5261
5262		ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
5263		    sizeof(kp_sigmask)));
5264	}
5265
5266	if (ignored) {
5267		DPRINTF("kp_sigignore="
5268		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
5269		    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
5270		    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
5271
5272		DPRINTF("kp.p_sigignore="
5273		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
5274		    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
5275		    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
5276
5277		ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
5278		    sizeof(kp_sigignore)));
5279	}
5280
5281	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5282	SYSCALL_REQUIRE(
5283	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5284
5285	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5286	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5287	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5288	    info.psi_siginfo.si_errno);
5289
5290	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5291	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
5292
5293	DPRINTF("Before resuming the child process where it left off and "
5294	    "without signal to be sent\n");
5295	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5296
5297	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5298	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5299
5300	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5301	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5302}
5303
5304#define TRACEME_EXEC(test, masked, ignored)				\
5305ATF_TC(test);								\
5306ATF_TC_HEAD(test, tc)							\
5307{									\
5308       atf_tc_set_md_var(tc, "descr",					\
5309           "Detect SIGTRAP TRAP_EXEC from "				\
5310           "child%s%s", masked ? " with masked signal" : "",		\
5311           masked ? " with ignored signal" : "");			\
5312}									\
5313									\
5314ATF_TC_BODY(test, tc)							\
5315{									\
5316									\
5317       traceme_exec(masked, ignored);					\
5318}
5319
5320TRACEME_EXEC(traceme_exec, false, false)
5321TRACEME_EXEC(traceme_signalmasked_exec, true, false)
5322TRACEME_EXEC(traceme_signalignored_exec, false, true)
5323
5324/// ----------------------------------------------------------------------------
5325
5326static volatile int done;
5327
5328static void *
5329trace_threads_cb(void *arg __unused)
5330{
5331
5332	done++;
5333
5334	while (done < 3)
5335		continue;
5336
5337	return NULL;
5338}
5339
5340static void
5341trace_threads(bool trace_create, bool trace_exit)
5342{
5343	const int sigval = SIGSTOP;
5344	pid_t child, wpid;
5345#if defined(TWAIT_HAVE_STATUS)
5346	int status;
5347#endif
5348	ptrace_state_t state;
5349	const int slen = sizeof(state);
5350	ptrace_event_t event;
5351	const int elen = sizeof(event);
5352	struct ptrace_siginfo info;
5353
5354	pthread_t t[3];
5355	int rv;
5356	size_t n;
5357	lwpid_t lid;
5358
5359	/* Track created and exited threads */
5360	bool traced_lwps[__arraycount(t)];
5361
5362	atf_tc_skip("PR kern/51995");
5363
5364	DPRINTF("Before forking process PID=%d\n", getpid());
5365	SYSCALL_REQUIRE((child = fork()) != -1);
5366	if (child == 0) {
5367		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5368		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5369
5370		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5371		FORKEE_ASSERT(raise(sigval) == 0);
5372
5373		for (n = 0; n < __arraycount(t); n++) {
5374			rv = pthread_create(&t[n], NULL, trace_threads_cb,
5375			    NULL);
5376			FORKEE_ASSERT(rv == 0);
5377		}
5378
5379		for (n = 0; n < __arraycount(t); n++) {
5380			rv = pthread_join(t[n], NULL);
5381			FORKEE_ASSERT(rv == 0);
5382		}
5383
5384		/*
5385		 * There is race between _exit() and pthread_join() detaching
5386		 * a thread. For simplicity kill the process after detecting
5387		 * LWP events.
5388		 */
5389		while (true)
5390			continue;
5391
5392		FORKEE_ASSERT(0 && "Not reached");
5393	}
5394	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5395
5396	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5397	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5398
5399	validate_status_stopped(status, sigval);
5400
5401	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5402	SYSCALL_REQUIRE(
5403	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5404
5405	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5406	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5407	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5408	    info.psi_siginfo.si_errno);
5409
5410	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5411	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5412
5413	DPRINTF("Set LWP event mask for the child %d\n", child);
5414	memset(&event, 0, sizeof(event));
5415	if (trace_create)
5416		event.pe_set_event |= PTRACE_LWP_CREATE;
5417	if (trace_exit)
5418		event.pe_set_event |= PTRACE_LWP_EXIT;
5419	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5420
5421	DPRINTF("Before resuming the child process where it left off and "
5422	    "without signal to be sent\n");
5423	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5424
5425	memset(traced_lwps, 0, sizeof(traced_lwps));
5426
5427	for (n = 0; n < (trace_create ? __arraycount(t) : 0); n++) {
5428		DPRINTF("Before calling %s() for the child - expected stopped "
5429		    "SIGTRAP\n", TWAIT_FNAME);
5430		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
5431		    child);
5432
5433		validate_status_stopped(status, SIGTRAP);
5434
5435		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
5436		    "child\n");
5437		SYSCALL_REQUIRE(
5438		    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5439
5440		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5441		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
5442		    "si_errno=%#x\n",
5443		    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5444		    info.psi_siginfo.si_errno);
5445
5446		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5447		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_LWP);
5448
5449		SYSCALL_REQUIRE(
5450		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5451
5452		ATF_REQUIRE_EQ_MSG(state.pe_report_event, PTRACE_LWP_CREATE,
5453		    "%d != %d", state.pe_report_event, PTRACE_LWP_CREATE);
5454
5455		lid = state.pe_lwp;
5456		DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
5457
5458		traced_lwps[lid - 1] = true;
5459
5460		DPRINTF("Before resuming the child process where it left off "
5461		    "and without signal to be sent\n");
5462		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5463	}
5464
5465	for (n = 0; n < (trace_exit ? __arraycount(t) : 0); n++) {
5466		DPRINTF("Before calling %s() for the child - expected stopped "
5467		    "SIGTRAP\n", TWAIT_FNAME);
5468		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
5469		    child);
5470
5471		validate_status_stopped(status, SIGTRAP);
5472
5473		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
5474		    "child\n");
5475		SYSCALL_REQUIRE(
5476		    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5477
5478		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5479		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
5480		    "si_errno=%#x\n",
5481		    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5482		    info.psi_siginfo.si_errno);
5483
5484		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5485		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_LWP);
5486
5487		SYSCALL_REQUIRE(
5488		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5489
5490		ATF_REQUIRE_EQ_MSG(state.pe_report_event, PTRACE_LWP_EXIT,
5491		    "%d != %d", state.pe_report_event, PTRACE_LWP_EXIT);
5492
5493		lid = state.pe_lwp;
5494		DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
5495
5496		if (trace_create) {
5497			ATF_REQUIRE(traced_lwps[lid - 1] == true);
5498			traced_lwps[lid - 1] = false;
5499		}
5500
5501		DPRINTF("Before resuming the child process where it left off "
5502		    "and without signal to be sent\n");
5503		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5504	}
5505
5506	kill(child, SIGKILL);
5507
5508	DPRINTF("Before calling %s() for the child - expected exited\n",
5509	    TWAIT_FNAME);
5510	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5511
5512	validate_status_signaled(status, SIGKILL, 0);
5513
5514	DPRINTF("Before calling %s() for the child - expected no process\n",
5515	    TWAIT_FNAME);
5516	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5517}
5518
5519#define TRACE_THREADS(test, trace_create, trace_exit)			\
5520ATF_TC(test);								\
5521ATF_TC_HEAD(test, tc)							\
5522{									\
5523        atf_tc_set_md_var(tc, "descr",					\
5524            "Verify spawning threads with%s tracing LWP create and"	\
5525	    "with%s tracing LWP exit", trace_create ? "" : "out",	\
5526	    trace_exit ? "" : "out");					\
5527}									\
5528									\
5529ATF_TC_BODY(test, tc)							\
5530{									\
5531									\
5532        trace_threads(trace_create, trace_exit);			\
5533}
5534
5535TRACE_THREADS(trace_thread1, false, false)
5536TRACE_THREADS(trace_thread2, false, true)
5537TRACE_THREADS(trace_thread3, true, false)
5538TRACE_THREADS(trace_thread4, true, true)
5539
5540/// ----------------------------------------------------------------------------
5541
5542ATF_TC(signal_mask_unrelated);
5543ATF_TC_HEAD(signal_mask_unrelated, tc)
5544{
5545	atf_tc_set_md_var(tc, "descr",
5546	    "Verify that masking single unrelated signal does not stop tracer "
5547	    "from catching other signals");
5548}
5549
5550ATF_TC_BODY(signal_mask_unrelated, tc)
5551{
5552	const int exitval = 5;
5553	const int sigval = SIGSTOP;
5554	const int sigmasked = SIGTRAP;
5555	const int signotmasked = SIGINT;
5556	pid_t child, wpid;
5557#if defined(TWAIT_HAVE_STATUS)
5558	int status;
5559#endif
5560	sigset_t intmask;
5561
5562	DPRINTF("Before forking process PID=%d\n", getpid());
5563	SYSCALL_REQUIRE((child = fork()) != -1);
5564	if (child == 0) {
5565		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5566		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5567
5568		sigemptyset(&intmask);
5569		sigaddset(&intmask, sigmasked);
5570		sigprocmask(SIG_BLOCK, &intmask, NULL);
5571
5572		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5573		FORKEE_ASSERT(raise(sigval) == 0);
5574
5575		DPRINTF("Before raising %s from child\n",
5576		    strsignal(signotmasked));
5577		FORKEE_ASSERT(raise(signotmasked) == 0);
5578
5579		DPRINTF("Before exiting of the child process\n");
5580		_exit(exitval);
5581	}
5582	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5583
5584	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5585	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5586
5587	validate_status_stopped(status, sigval);
5588
5589	DPRINTF("Before resuming the child process where it left off and "
5590	    "without signal to be sent\n");
5591	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5592
5593	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5594	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5595
5596	validate_status_stopped(status, signotmasked);
5597
5598	DPRINTF("Before resuming the child process where it left off and "
5599	    "without signal to be sent\n");
5600	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5601
5602	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5603	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5604
5605	validate_status_exited(status, exitval);
5606
5607	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5608	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5609}
5610
5611/// ----------------------------------------------------------------------------
5612
5613#if defined(TWAIT_HAVE_PID)
5614static void
5615fork2_body(bool trackfork, bool trackvfork, bool trackvforkdone, bool masked,
5616           bool ignored)
5617{
5618	const int exitval = 5;
5619	const int exitval2 = 15;
5620	const int sigval = SIGSTOP;
5621	pid_t child, child2 = 0, wpid;
5622#if defined(TWAIT_HAVE_STATUS)
5623	int status;
5624#endif
5625	ptrace_state_t state;
5626	const int slen = sizeof(state);
5627	ptrace_event_t event;
5628	const int elen = sizeof(event);
5629	pid_t (*fn)(void);
5630	struct sigaction sa;
5631	struct ptrace_siginfo info;
5632	sigset_t intmask;
5633	struct kinfo_proc2 kp;
5634	size_t len = sizeof(kp);
5635
5636	int name[6];
5637	const size_t namelen = __arraycount(name);
5638	ki_sigset_t kp_sigmask;
5639	ki_sigset_t kp_sigignore;
5640
5641	if (trackfork)
5642		fn = fork;
5643	if (trackvfork || trackvforkdone)
5644		fn = vfork;
5645
5646	DPRINTF("Before forking process PID=%d\n", getpid());
5647	SYSCALL_REQUIRE((child = fork()) != -1);
5648	if (child == 0) {
5649		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5650		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5651
5652		if (masked) {
5653			sigemptyset(&intmask);
5654			sigaddset(&intmask, SIGTRAP);
5655			sigprocmask(SIG_BLOCK, &intmask, NULL);
5656		}
5657
5658		if (ignored) {
5659			memset(&sa, 0, sizeof(sa));
5660			sa.sa_handler = SIG_IGN;
5661			sigemptyset(&sa.sa_mask);
5662			FORKEE_ASSERT(sigaction(SIGTRAP, &sa, NULL) != -1);
5663		}
5664
5665		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5666		FORKEE_ASSERT(raise(sigval) == 0);
5667
5668		FORKEE_ASSERT((child2 = (fn)()) != -1);
5669
5670		if (child2 == 0)
5671			_exit(exitval2);
5672
5673		FORKEE_REQUIRE_SUCCESS
5674		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5675
5676		forkee_status_exited(status, exitval2);
5677
5678		DPRINTF("Before exiting of the child process\n");
5679		_exit(exitval);
5680	}
5681	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5682
5683	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5684	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5685
5686	validate_status_stopped(status, sigval);
5687
5688	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5689	SYSCALL_REQUIRE(
5690	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5691
5692	DPRINTF("Before checking siginfo_t\n");
5693	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5694	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5695
5696	name[0] = CTL_KERN,
5697	name[1] = KERN_PROC2,
5698	name[2] = KERN_PROC_PID;
5699	name[3] = child;
5700	name[4] = sizeof(kp);
5701	name[5] = 1;
5702
5703	FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
5704
5705	if (masked)
5706		kp_sigmask = kp.p_sigmask;
5707
5708	if (ignored)
5709		kp_sigignore = kp.p_sigignore;
5710
5711	DPRINTF("Set 0%s%s%s in EVENT_MASK for the child %d\n",
5712	    trackfork ? "|PTRACE_FORK" : "",
5713	    trackvfork ? "|PTRACE_VFORK" : "",
5714	    trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
5715	event.pe_set_event = 0;
5716	if (trackfork)
5717		event.pe_set_event |= PTRACE_FORK;
5718	if (trackvfork)
5719		event.pe_set_event |= PTRACE_VFORK;
5720	if (trackvforkdone)
5721		event.pe_set_event |= PTRACE_VFORK_DONE;
5722	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5723
5724	DPRINTF("Before resuming the child process where it left off and "
5725	    "without signal to be sent\n");
5726	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5727
5728	if (trackfork || trackvfork) {
5729		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
5730		    child);
5731		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
5732		    child);
5733
5734		validate_status_stopped(status, SIGTRAP);
5735
5736		ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
5737
5738		if (masked) {
5739			DPRINTF("kp_sigmask="
5740			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5741			    PRIx32 "\n",
5742			    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
5743			    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
5744
5745			DPRINTF("kp.p_sigmask="
5746			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5747			    PRIx32 "\n",
5748			    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
5749			    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
5750
5751			ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
5752			    sizeof(kp_sigmask)));
5753		}
5754
5755		if (ignored) {
5756			DPRINTF("kp_sigignore="
5757			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5758			    PRIx32 "\n",
5759			    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
5760			    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
5761
5762			DPRINTF("kp.p_sigignore="
5763			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5764			    PRIx32 "\n",
5765			    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
5766			    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
5767
5768			ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
5769			    sizeof(kp_sigignore)));
5770		}
5771
5772		SYSCALL_REQUIRE(
5773		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5774		if (trackfork) {
5775			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
5776			       PTRACE_FORK);
5777		}
5778		if (trackvfork) {
5779			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
5780			       PTRACE_VFORK);
5781		}
5782
5783		child2 = state.pe_other_pid;
5784		DPRINTF("Reported ptrace event with forkee %d\n", child2);
5785
5786		DPRINTF("Before calling %s() for the forkee %d of the child "
5787		    "%d\n", TWAIT_FNAME, child2, child);
5788		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5789		    child2);
5790
5791		validate_status_stopped(status, SIGTRAP);
5792
5793		name[3] = child2;
5794		ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
5795
5796		if (masked) {
5797			DPRINTF("kp_sigmask="
5798			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5799			    PRIx32 "\n",
5800			    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
5801			    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
5802
5803			DPRINTF("kp.p_sigmask="
5804			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5805			    PRIx32 "\n",
5806			    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
5807			    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
5808
5809			ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
5810			    sizeof(kp_sigmask)));
5811		}
5812
5813		if (ignored) {
5814			DPRINTF("kp_sigignore="
5815			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5816			    PRIx32 "\n",
5817			    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
5818			    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
5819
5820			DPRINTF("kp.p_sigignore="
5821			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5822			    PRIx32 "\n",
5823			    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
5824			    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
5825
5826			ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
5827			    sizeof(kp_sigignore)));
5828		}
5829
5830		SYSCALL_REQUIRE(
5831		    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
5832		if (trackfork) {
5833			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
5834			       PTRACE_FORK);
5835		}
5836		if (trackvfork) {
5837			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
5838			       PTRACE_VFORK);
5839		}
5840
5841		ATF_REQUIRE_EQ(state.pe_other_pid, child);
5842
5843		DPRINTF("Before resuming the forkee process where it left off "
5844		    "and without signal to be sent\n");
5845		SYSCALL_REQUIRE(
5846		    ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
5847
5848		DPRINTF("Before resuming the child process where it left off "
5849		    "and without signal to be sent\n");
5850		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5851	}
5852
5853	if (trackvforkdone) {
5854		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
5855		    child);
5856		TWAIT_REQUIRE_SUCCESS(
5857		    wpid = TWAIT_GENERIC(child, &status, 0), child);
5858
5859		validate_status_stopped(status, SIGTRAP);
5860
5861		name[3] = child;
5862		ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
5863
5864		/*
5865		 * SIGCHLD is now pending in the signal queue and
5866		 * the kernel presents it to userland as a masked signal.
5867		 */
5868		sigdelset((sigset_t *)&kp.p_sigmask, SIGCHLD);
5869
5870		if (masked) {
5871			DPRINTF("kp_sigmask="
5872			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5873			    PRIx32 "\n",
5874			    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
5875			    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
5876
5877			DPRINTF("kp.p_sigmask="
5878			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5879			    PRIx32 "\n",
5880			    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
5881			    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
5882
5883			ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
5884			    sizeof(kp_sigmask)));
5885		}
5886
5887		if (ignored) {
5888			DPRINTF("kp_sigignore="
5889			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5890			    PRIx32 "\n",
5891			    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
5892			    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
5893
5894			DPRINTF("kp.p_sigignore="
5895			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
5896			    PRIx32 "\n",
5897			    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
5898			    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
5899
5900			ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
5901			    sizeof(kp_sigignore)));
5902		}
5903
5904		SYSCALL_REQUIRE(
5905		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5906		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
5907
5908		child2 = state.pe_other_pid;
5909		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
5910		    child2);
5911
5912		DPRINTF("Before resuming the child process where it left off "
5913		    "and without signal to be sent\n");
5914		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5915	}
5916
5917	if (trackfork || trackvfork) {
5918		DPRINTF("Before calling %s() for the forkee - expected exited"
5919		    "\n", TWAIT_FNAME);
5920		TWAIT_REQUIRE_SUCCESS(
5921		    wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5922
5923		validate_status_exited(status, exitval2);
5924
5925		DPRINTF("Before calling %s() for the forkee - expected no "
5926		    "process\n", TWAIT_FNAME);
5927		TWAIT_REQUIRE_FAILURE(ECHILD,
5928		    wpid = TWAIT_GENERIC(child2, &status, 0));
5929	}
5930
5931	DPRINTF("Before calling %s() for the child - expected stopped "
5932	    "SIGCHLD\n", TWAIT_FNAME);
5933	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5934
5935	validate_status_stopped(status, SIGCHLD);
5936
5937	DPRINTF("Before resuming the child process where it left off and "
5938	    "without signal to be sent\n");
5939	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5940
5941	DPRINTF("Before calling %s() for the child - expected exited\n",
5942	    TWAIT_FNAME);
5943	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5944
5945	validate_status_exited(status, exitval);
5946
5947	DPRINTF("Before calling %s() for the child - expected no process\n",
5948	    TWAIT_FNAME);
5949	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5950}
5951
5952#define FORK2_TEST(name,trackfork,trackvfork,trackvforkdone,		\
5953                   masked,ignored)					\
5954ATF_TC(name);								\
5955ATF_TC_HEAD(name, tc)							\
5956{									\
5957	atf_tc_set_md_var(tc, "descr", "Verify that %s%s%s is caught "	\
5958	    "regardless of signal %s%s", 				\
5959	    trackfork ? "PTRACE_FORK" : "",				\
5960	    trackvfork ? "PTRACE_VFORK" : "",				\
5961	    trackvforkdone ? "PTRACE_VFORK_DONE" : "",			\
5962	    masked ? "masked" : "", ignored ? "ignored" : "");		\
5963}									\
5964									\
5965ATF_TC_BODY(name, tc)							\
5966{									\
5967									\
5968	fork2_body(trackfork, trackvfork, trackvforkdone, masked,	\
5969	           ignored);						\
5970}
5971
5972FORK2_TEST(fork_singalmasked, true, false, false, true, false)
5973FORK2_TEST(fork_singalignored, true, false, false, false, true)
5974#if TEST_VFORK_ENABLED
5975FORK2_TEST(vfork_singalmasked, false, true, false, true, false)
5976FORK2_TEST(vfork_singalignored, false, true, false, false, true)
5977FORK2_TEST(vforkdone_singalmasked, false, false, true, true, false)
5978FORK2_TEST(vforkdone_singalignored, false, false, true, false, true)
5979#endif
5980#endif
5981
5982/// ----------------------------------------------------------------------------
5983
5984volatile lwpid_t the_lwp_id = 0;
5985
5986static void
5987lwp_main_func(void *arg)
5988{
5989	the_lwp_id = _lwp_self();
5990	_lwp_exit();
5991}
5992
5993ATF_TC(signal9);
5994ATF_TC_HEAD(signal9, tc)
5995{
5996	atf_tc_set_md_var(tc, "descr",
5997	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5998	    "catching PTRACE_LWP_CREATE breakpoint");
5999}
6000
6001ATF_TC_BODY(signal9, tc)
6002{
6003	const int exitval = 5;
6004	const int sigval = SIGSTOP;
6005	const int sigmasked = SIGTRAP;
6006	pid_t child, wpid;
6007#if defined(TWAIT_HAVE_STATUS)
6008	int status;
6009#endif
6010	sigset_t intmask;
6011	ptrace_state_t state;
6012	const int slen = sizeof(state);
6013	ptrace_event_t event;
6014	const int elen = sizeof(event);
6015	ucontext_t uc;
6016	lwpid_t lid;
6017	static const size_t ssize = 16*1024;
6018	void *stack;
6019
6020	DPRINTF("Before forking process PID=%d\n", getpid());
6021	SYSCALL_REQUIRE((child = fork()) != -1);
6022	if (child == 0) {
6023		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6024		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6025
6026		sigemptyset(&intmask);
6027		sigaddset(&intmask, sigmasked);
6028		sigprocmask(SIG_BLOCK, &intmask, NULL);
6029
6030		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6031		FORKEE_ASSERT(raise(sigval) == 0);
6032
6033		DPRINTF("Before allocating memory for stack in child\n");
6034		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6035
6036		DPRINTF("Before making context for new lwp in child\n");
6037		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6038
6039		DPRINTF("Before creating new in child\n");
6040		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6041
6042		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6043		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6044
6045		DPRINTF("Before verifying that reported %d and running lid %d "
6046		    "are the same\n", lid, the_lwp_id);
6047		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6048
6049		DPRINTF("Before exiting of the child process\n");
6050		_exit(exitval);
6051	}
6052	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6053
6054	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6055	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6056
6057	validate_status_stopped(status, sigval);
6058
6059	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
6060	event.pe_set_event = PTRACE_LWP_CREATE;
6061	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6062
6063	DPRINTF("Before resuming the child process where it left off and "
6064	    "without signal to be sent\n");
6065	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6066
6067	DPRINTF("Before calling %s() for the child - expected stopped "
6068	    "SIGTRAP\n", TWAIT_FNAME);
6069	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6070
6071	validate_status_stopped(status, sigmasked);
6072
6073	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6074
6075	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
6076
6077	lid = state.pe_lwp;
6078	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
6079
6080	DPRINTF("Before resuming the child process where it left off and "
6081	    "without signal to be sent\n");
6082	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6083
6084	DPRINTF("Before calling %s() for the child - expected exited\n",
6085	    TWAIT_FNAME);
6086	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6087
6088	validate_status_exited(status, exitval);
6089
6090	DPRINTF("Before calling %s() for the child - expected no process\n",
6091	    TWAIT_FNAME);
6092	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6093}
6094
6095ATF_TC(signal10);
6096ATF_TC_HEAD(signal10, tc)
6097{
6098	atf_tc_set_md_var(tc, "descr",
6099	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6100	    "catching PTRACE_LWP_EXIT breakpoint");
6101}
6102
6103ATF_TC_BODY(signal10, tc)
6104{
6105	const int exitval = 5;
6106	const int sigval = SIGSTOP;
6107	const int sigmasked = SIGTRAP;
6108	pid_t child, wpid;
6109#if defined(TWAIT_HAVE_STATUS)
6110	int status;
6111#endif
6112	sigset_t intmask;
6113	ptrace_state_t state;
6114	const int slen = sizeof(state);
6115	ptrace_event_t event;
6116	const int elen = sizeof(event);
6117	ucontext_t uc;
6118	lwpid_t lid;
6119	static const size_t ssize = 16*1024;
6120	void *stack;
6121
6122	DPRINTF("Before forking process PID=%d\n", getpid());
6123	SYSCALL_REQUIRE((child = fork()) != -1);
6124	if (child == 0) {
6125		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6126		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6127
6128		sigemptyset(&intmask);
6129		sigaddset(&intmask, sigmasked);
6130		sigprocmask(SIG_BLOCK, &intmask, NULL);
6131
6132		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6133		FORKEE_ASSERT(raise(sigval) == 0);
6134
6135		DPRINTF("Before allocating memory for stack in child\n");
6136		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6137
6138		DPRINTF("Before making context for new lwp in child\n");
6139		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6140
6141		DPRINTF("Before creating new in child\n");
6142		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6143
6144		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6145		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6146
6147		DPRINTF("Before verifying that reported %d and running lid %d "
6148		    "are the same\n", lid, the_lwp_id);
6149		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6150
6151		DPRINTF("Before exiting of the child process\n");
6152		_exit(exitval);
6153	}
6154	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6155
6156	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6157	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6158
6159	validate_status_stopped(status, sigval);
6160
6161	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
6162	event.pe_set_event = PTRACE_LWP_EXIT;
6163	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6164
6165	DPRINTF("Before resuming the child process where it left off and "
6166	    "without signal to be sent\n");
6167	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6168
6169	DPRINTF("Before calling %s() for the child - expected stopped "
6170	    "SIGTRAP\n", TWAIT_FNAME);
6171	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6172
6173	validate_status_stopped(status, sigmasked);
6174
6175	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6176
6177	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
6178
6179	lid = state.pe_lwp;
6180	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
6181
6182	DPRINTF("Before resuming the child process where it left off and "
6183	    "without signal to be sent\n");
6184	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6185
6186	DPRINTF("Before calling %s() for the child - expected exited\n",
6187	    TWAIT_FNAME);
6188	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6189
6190	validate_status_exited(status, exitval);
6191
6192	DPRINTF("Before calling %s() for the child - expected no process\n",
6193	    TWAIT_FNAME);
6194	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6195}
6196
6197static void
6198lwp_main_stop(void *arg)
6199{
6200	the_lwp_id = _lwp_self();
6201
6202	raise(SIGTRAP);
6203
6204	_lwp_exit();
6205}
6206
6207ATF_TC(suspend1);
6208ATF_TC_HEAD(suspend1, tc)
6209{
6210	atf_tc_set_md_var(tc, "descr",
6211	    "Verify that a thread can be suspended by a debugger and later "
6212	    "resumed by a tracee");
6213}
6214
6215ATF_TC_BODY(suspend1, tc)
6216{
6217	const int exitval = 5;
6218	const int sigval = SIGSTOP;
6219	pid_t child, wpid;
6220#if defined(TWAIT_HAVE_STATUS)
6221	int status;
6222#endif
6223	ucontext_t uc;
6224	lwpid_t lid;
6225	static const size_t ssize = 16*1024;
6226	void *stack;
6227	struct ptrace_lwpinfo pl;
6228	struct ptrace_siginfo psi;
6229	volatile int go = 0;
6230
6231	// Feature pending for refactoring
6232	atf_tc_expect_fail("PR kern/51995");
6233
6234	// Hangs with qemu
6235	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
6236
6237	DPRINTF("Before forking process PID=%d\n", getpid());
6238	SYSCALL_REQUIRE((child = fork()) != -1);
6239	if (child == 0) {
6240		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6241		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6242
6243		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6244		FORKEE_ASSERT(raise(sigval) == 0);
6245
6246		DPRINTF("Before allocating memory for stack in child\n");
6247		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6248
6249		DPRINTF("Before making context for new lwp in child\n");
6250		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
6251
6252		DPRINTF("Before creating new in child\n");
6253		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6254
6255		while (go == 0)
6256			continue;
6257
6258		raise(SIGINT);
6259
6260		FORKEE_ASSERT(_lwp_continue(lid) == 0);
6261
6262		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6263		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6264
6265		DPRINTF("Before verifying that reported %d and running lid %d "
6266		    "are the same\n", lid, the_lwp_id);
6267		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6268
6269		DPRINTF("Before exiting of the child process\n");
6270		_exit(exitval);
6271	}
6272	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6273
6274	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6275	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6276
6277	validate_status_stopped(status, sigval);
6278
6279	DPRINTF("Before resuming the child process where it left off and "
6280	    "without signal to be sent\n");
6281	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6282
6283	DPRINTF("Before calling %s() for the child - expected stopped "
6284	    "SIGTRAP\n", TWAIT_FNAME);
6285	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6286
6287	validate_status_stopped(status, SIGTRAP);
6288
6289	DPRINTF("Before reading siginfo and lwpid_t\n");
6290	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
6291
6292	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
6293	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
6294
6295        DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
6296	    child, getpid());
6297	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
6298
6299	DPRINTF("Before resuming the child process where it left off and "
6300	    "without signal to be sent\n");
6301	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6302
6303	DPRINTF("Before calling %s() for the child - expected stopped "
6304	    "SIGINT\n", TWAIT_FNAME);
6305	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6306
6307	validate_status_stopped(status, SIGINT);
6308
6309	pl.pl_lwpid = 0;
6310
6311	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6312	while (pl.pl_lwpid != 0) {
6313
6314		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6315		switch (pl.pl_lwpid) {
6316		case 1:
6317			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
6318			break;
6319		case 2:
6320			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
6321			break;
6322		}
6323	}
6324
6325	DPRINTF("Before resuming the child process where it left off and "
6326	    "without signal to be sent\n");
6327	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6328
6329	DPRINTF("Before calling %s() for the child - expected exited\n",
6330	    TWAIT_FNAME);
6331	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6332
6333	validate_status_exited(status, exitval);
6334
6335	DPRINTF("Before calling %s() for the child - expected no process\n",
6336	    TWAIT_FNAME);
6337	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6338}
6339
6340ATF_TC(suspend2);
6341ATF_TC_HEAD(suspend2, tc)
6342{
6343	atf_tc_set_md_var(tc, "descr",
6344	    "Verify that the while the only thread within a process is "
6345	    "suspended, the whole process cannot be unstopped");
6346}
6347
6348ATF_TC_BODY(suspend2, tc)
6349{
6350	const int exitval = 5;
6351	const int sigval = SIGSTOP;
6352	pid_t child, wpid;
6353#if defined(TWAIT_HAVE_STATUS)
6354	int status;
6355#endif
6356	struct ptrace_siginfo psi;
6357
6358	// Feature pending for refactoring
6359	atf_tc_expect_fail("PR kern/51995");
6360
6361	// Hangs with qemu
6362	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
6363
6364	DPRINTF("Before forking process PID=%d\n", getpid());
6365	SYSCALL_REQUIRE((child = fork()) != -1);
6366	if (child == 0) {
6367		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6368		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6369
6370		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6371		FORKEE_ASSERT(raise(sigval) == 0);
6372
6373		DPRINTF("Before exiting of the child process\n");
6374		_exit(exitval);
6375	}
6376	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6377
6378	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6379	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6380
6381	validate_status_stopped(status, sigval);
6382
6383	DPRINTF("Before reading siginfo and lwpid_t\n");
6384	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
6385
6386	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
6387	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
6388
6389	DPRINTF("Before resuming the child process where it left off and "
6390	    "without signal to be sent\n");
6391	ATF_REQUIRE_ERRNO(EDEADLK,
6392	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
6393
6394	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
6395	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
6396
6397	DPRINTF("Before resuming the child process where it left off and "
6398	    "without signal to be sent\n");
6399	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6400
6401	DPRINTF("Before calling %s() for the child - expected exited\n",
6402	    TWAIT_FNAME);
6403	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6404
6405	validate_status_exited(status, exitval);
6406
6407	DPRINTF("Before calling %s() for the child - expected no process\n",
6408	    TWAIT_FNAME);
6409	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6410}
6411
6412ATF_TC(resume1);
6413ATF_TC_HEAD(resume1, tc)
6414{
6415	atf_tc_set_md_var(tc, "timeout", "5");
6416	atf_tc_set_md_var(tc, "descr",
6417	    "Verify that a thread can be suspended by a debugger and later "
6418	    "resumed by the debugger");
6419}
6420
6421ATF_TC_BODY(resume1, tc)
6422{
6423	struct msg_fds fds;
6424	const int exitval = 5;
6425	const int sigval = SIGSTOP;
6426	pid_t child, wpid;
6427	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
6428#if defined(TWAIT_HAVE_STATUS)
6429	int status;
6430#endif
6431	ucontext_t uc;
6432	lwpid_t lid;
6433	static const size_t ssize = 16*1024;
6434	void *stack;
6435	struct ptrace_lwpinfo pl;
6436	struct ptrace_siginfo psi;
6437
6438	// Feature pending for refactoring
6439	atf_tc_expect_fail("PR kern/51995");
6440
6441	// Hangs with qemu
6442	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
6443
6444	SYSCALL_REQUIRE(msg_open(&fds) == 0);
6445
6446	DPRINTF("Before forking process PID=%d\n", getpid());
6447	SYSCALL_REQUIRE((child = fork()) != -1);
6448	if (child == 0) {
6449		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6450		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6451
6452		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6453		FORKEE_ASSERT(raise(sigval) == 0);
6454
6455		DPRINTF("Before allocating memory for stack in child\n");
6456		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6457
6458		DPRINTF("Before making context for new lwp in child\n");
6459		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
6460
6461		DPRINTF("Before creating new in child\n");
6462		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6463
6464		CHILD_TO_PARENT("Message", fds, msg);
6465
6466		raise(SIGINT);
6467
6468		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6469		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6470
6471		DPRINTF("Before verifying that reported %d and running lid %d "
6472		    "are the same\n", lid, the_lwp_id);
6473		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6474
6475		DPRINTF("Before exiting of the child process\n");
6476		_exit(exitval);
6477	}
6478	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6479
6480	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6481	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6482
6483	validate_status_stopped(status, sigval);
6484
6485	DPRINTF("Before resuming the child process where it left off and "
6486	    "without signal to be sent\n");
6487	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6488
6489	DPRINTF("Before calling %s() for the child - expected stopped "
6490	    "SIGTRAP\n", TWAIT_FNAME);
6491	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6492
6493	validate_status_stopped(status, SIGTRAP);
6494
6495	DPRINTF("Before reading siginfo and lwpid_t\n");
6496	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
6497
6498	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
6499	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
6500
6501	PARENT_FROM_CHILD("Message", fds, msg);
6502
6503	DPRINTF("Before resuming the child process where it left off and "
6504	    "without signal to be sent\n");
6505	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6506
6507	DPRINTF("Before calling %s() for the child - expected stopped "
6508	    "SIGINT\n", TWAIT_FNAME);
6509	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6510
6511	validate_status_stopped(status, SIGINT);
6512
6513	pl.pl_lwpid = 0;
6514
6515	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6516	while (pl.pl_lwpid != 0) {
6517		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6518		switch (pl.pl_lwpid) {
6519		case 1:
6520			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
6521			break;
6522		case 2:
6523			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
6524			break;
6525		}
6526	}
6527
6528	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
6529	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
6530
6531	DPRINTF("Before resuming the child process where it left off and "
6532	    "without signal to be sent\n");
6533	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6534
6535	DPRINTF("Before calling %s() for the child - expected exited\n",
6536	    TWAIT_FNAME);
6537	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6538
6539	validate_status_exited(status, exitval);
6540
6541	DPRINTF("Before calling %s() for the child - expected no process\n",
6542	    TWAIT_FNAME);
6543	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6544
6545	msg_close(&fds);
6546
6547	DPRINTF("XXX: Test worked this time but for consistency timeout it\n");
6548	sleep(10);
6549}
6550
6551ATF_TC(syscall1);
6552ATF_TC_HEAD(syscall1, tc)
6553{
6554	atf_tc_set_md_var(tc, "descr",
6555	    "Verify that getpid(2) can be traced with PT_SYSCALL");
6556}
6557
6558ATF_TC_BODY(syscall1, tc)
6559{
6560	const int exitval = 5;
6561	const int sigval = SIGSTOP;
6562	pid_t child, wpid;
6563#if defined(TWAIT_HAVE_STATUS)
6564	int status;
6565#endif
6566	struct ptrace_siginfo info;
6567	memset(&info, 0, sizeof(info));
6568
6569	DPRINTF("Before forking process PID=%d\n", getpid());
6570	SYSCALL_REQUIRE((child = fork()) != -1);
6571	if (child == 0) {
6572		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6573		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6574
6575		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6576		FORKEE_ASSERT(raise(sigval) == 0);
6577
6578		syscall(SYS_getpid);
6579
6580		DPRINTF("Before exiting of the child process\n");
6581		_exit(exitval);
6582	}
6583	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6584
6585	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6586	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6587
6588	validate_status_stopped(status, sigval);
6589
6590	DPRINTF("Before resuming the child process where it left off and "
6591	    "without signal to be sent\n");
6592	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6593
6594	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6595	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6596
6597	validate_status_stopped(status, SIGTRAP);
6598
6599	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
6600	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
6601
6602	DPRINTF("Before checking siginfo_t and lwpid\n");
6603	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
6604	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
6605	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
6606
6607	DPRINTF("Before resuming the child process where it left off and "
6608	    "without signal to be sent\n");
6609	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6610
6611	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6612	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6613
6614	validate_status_stopped(status, SIGTRAP);
6615
6616	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
6617	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
6618
6619	DPRINTF("Before checking siginfo_t and lwpid\n");
6620	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
6621	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
6622	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
6623
6624	DPRINTF("Before resuming the child process where it left off and "
6625	    "without signal to be sent\n");
6626	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6627
6628	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6629	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6630
6631	validate_status_exited(status, exitval);
6632
6633	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6634	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6635}
6636
6637ATF_TC(syscallemu1);
6638ATF_TC_HEAD(syscallemu1, tc)
6639{
6640	atf_tc_set_md_var(tc, "descr",
6641	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
6642}
6643
6644ATF_TC_BODY(syscallemu1, tc)
6645{
6646	const int exitval = 5;
6647	const int sigval = SIGSTOP;
6648	pid_t child, wpid;
6649#if defined(TWAIT_HAVE_STATUS)
6650	int status;
6651#endif
6652
6653#if defined(__sparc__) && !defined(__sparc64__)
6654	/* syscallemu does not work on sparc (32-bit) */
6655	atf_tc_expect_fail("PR kern/52166");
6656#endif
6657
6658	DPRINTF("Before forking process PID=%d\n", getpid());
6659	SYSCALL_REQUIRE((child = fork()) != -1);
6660	if (child == 0) {
6661		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6662		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6663
6664		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6665		FORKEE_ASSERT(raise(sigval) == 0);
6666
6667		syscall(SYS_exit, 100);
6668
6669		DPRINTF("Before exiting of the child process\n");
6670		_exit(exitval);
6671	}
6672	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6673
6674	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6675	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6676
6677	validate_status_stopped(status, sigval);
6678
6679	DPRINTF("Before resuming the child process where it left off and "
6680	    "without signal to be sent\n");
6681	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6682
6683	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6684	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6685
6686	validate_status_stopped(status, SIGTRAP);
6687
6688	DPRINTF("Set SYSCALLEMU for intercepted syscall\n");
6689	SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
6690
6691	DPRINTF("Before resuming the child process where it left off and "
6692	    "without signal to be sent\n");
6693	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6694
6695	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6696	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6697
6698	validate_status_stopped(status, SIGTRAP);
6699
6700	DPRINTF("Before resuming the child process where it left off and "
6701	    "without signal to be sent\n");
6702	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6703
6704	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6705	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6706
6707	validate_status_exited(status, exitval);
6708
6709	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6710	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6711}
6712
6713/// ----------------------------------------------------------------------------
6714
6715static void
6716clone_body(int flags, bool trackfork, bool trackvfork,
6717    bool trackvforkdone)
6718{
6719	const int exitval = 5;
6720	const int exitval2 = 15;
6721	const int sigval = SIGSTOP;
6722	pid_t child, child2 = 0, wpid;
6723#if defined(TWAIT_HAVE_STATUS)
6724	int status;
6725#endif
6726	ptrace_state_t state;
6727	const int slen = sizeof(state);
6728	ptrace_event_t event;
6729	const int elen = sizeof(event);
6730
6731	const size_t stack_size = 1024 * 1024;
6732	void *stack, *stack_base;
6733
6734	stack = malloc(stack_size);
6735	ATF_REQUIRE(stack != NULL);
6736
6737#ifdef __MACHINE_STACK_GROWS_UP
6738	stack_base = stack;
6739#else
6740	stack_base = (char *)stack + stack_size;
6741#endif
6742
6743	DPRINTF("Before forking process PID=%d\n", getpid());
6744	SYSCALL_REQUIRE((child = fork()) != -1);
6745	if (child == 0) {
6746		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6747		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6748
6749		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6750		FORKEE_ASSERT(raise(sigval) == 0);
6751
6752		SYSCALL_REQUIRE((child2 = __clone(clone_func, stack_base,
6753		    flags|SIGCHLD, (void *)(intptr_t)exitval2)) != -1);
6754
6755		DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(),
6756		    child2);
6757
6758		// XXX WALLSIG?
6759		FORKEE_REQUIRE_SUCCESS
6760		    (wpid = TWAIT_GENERIC(child2, &status, WALLSIG), child2);
6761
6762		forkee_status_exited(status, exitval2);
6763
6764		DPRINTF("Before exiting of the child process\n");
6765		_exit(exitval);
6766	}
6767	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6768
6769	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6770	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6771
6772	validate_status_stopped(status, sigval);
6773
6774	DPRINTF("Set 0%s%s%s in EVENT_MASK for the child %d\n",
6775	    trackfork ? "|PTRACE_FORK" : "",
6776	    trackvfork ? "|PTRACE_VFORK" : "",
6777	    trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
6778	event.pe_set_event = 0;
6779	if (trackfork)
6780		event.pe_set_event |= PTRACE_FORK;
6781	if (trackvfork)
6782		event.pe_set_event |= PTRACE_VFORK;
6783	if (trackvforkdone)
6784		event.pe_set_event |= PTRACE_VFORK_DONE;
6785	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6786
6787	DPRINTF("Before resuming the child process where it left off and "
6788	    "without signal to be sent\n");
6789	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6790
6791#if defined(TWAIT_HAVE_PID)
6792	if ((trackfork && !(flags & CLONE_VFORK)) ||
6793	    (trackvfork && (flags & CLONE_VFORK))) {
6794		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
6795		    child);
6796		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
6797		    child);
6798
6799		validate_status_stopped(status, SIGTRAP);
6800
6801		SYSCALL_REQUIRE(
6802		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6803		if (trackfork && !(flags & CLONE_VFORK)) {
6804			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
6805			       PTRACE_FORK);
6806		}
6807		if (trackvfork && (flags & CLONE_VFORK)) {
6808			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
6809			       PTRACE_VFORK);
6810		}
6811
6812		child2 = state.pe_other_pid;
6813		DPRINTF("Reported ptrace event with forkee %d\n", child2);
6814
6815		DPRINTF("Before calling %s() for the forkee %d of the child "
6816		    "%d\n", TWAIT_FNAME, child2, child);
6817		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6818		    child2);
6819
6820		validate_status_stopped(status, SIGTRAP);
6821
6822		SYSCALL_REQUIRE(
6823		    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6824		if (trackfork && !(flags & CLONE_VFORK)) {
6825			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
6826			       PTRACE_FORK);
6827		}
6828		if (trackvfork && (flags & CLONE_VFORK)) {
6829			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
6830			       PTRACE_VFORK);
6831		}
6832
6833		ATF_REQUIRE_EQ(state.pe_other_pid, child);
6834
6835		DPRINTF("Before resuming the forkee process where it left off "
6836		    "and without signal to be sent\n");
6837		SYSCALL_REQUIRE(
6838		    ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
6839
6840		DPRINTF("Before resuming the child process where it left off "
6841		    "and without signal to be sent\n");
6842		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6843	}
6844#endif
6845
6846	if (trackvforkdone && (flags & CLONE_VFORK)) {
6847		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
6848		    child);
6849		TWAIT_REQUIRE_SUCCESS(
6850		    wpid = TWAIT_GENERIC(child, &status, 0), child);
6851
6852		validate_status_stopped(status, SIGTRAP);
6853
6854		SYSCALL_REQUIRE(
6855		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6856		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
6857
6858		child2 = state.pe_other_pid;
6859		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
6860		    child2);
6861
6862		DPRINTF("Before resuming the child process where it left off "
6863		    "and without signal to be sent\n");
6864		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6865	}
6866
6867#if defined(TWAIT_HAVE_PID)
6868	if ((trackfork && !(flags & CLONE_VFORK)) ||
6869	    (trackvfork && (flags & CLONE_VFORK))) {
6870		DPRINTF("Before calling %s() for the forkee - expected exited"
6871		    "\n", TWAIT_FNAME);
6872		TWAIT_REQUIRE_SUCCESS(
6873		    wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6874
6875		validate_status_exited(status, exitval2);
6876
6877		DPRINTF("Before calling %s() for the forkee - expected no "
6878		    "process\n", TWAIT_FNAME);
6879		TWAIT_REQUIRE_FAILURE(ECHILD,
6880		    wpid = TWAIT_GENERIC(child2, &status, 0));
6881	}
6882#endif
6883
6884	DPRINTF("Before calling %s() for the child - expected stopped "
6885	    "SIGCHLD\n", TWAIT_FNAME);
6886	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6887
6888	validate_status_stopped(status, SIGCHLD);
6889
6890	DPRINTF("Before resuming the child process where it left off and "
6891	    "without signal to be sent\n");
6892	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6893
6894	DPRINTF("Before calling %s() for the child - expected exited\n",
6895	    TWAIT_FNAME);
6896	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6897
6898	validate_status_exited(status, exitval);
6899
6900	DPRINTF("Before calling %s() for the child - expected no process\n",
6901	    TWAIT_FNAME);
6902	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6903}
6904
6905#define CLONE_TEST(name,flags,tfork,tvfork,tvforkdone)			\
6906ATF_TC(name);								\
6907ATF_TC_HEAD(name, tc)							\
6908{									\
6909	atf_tc_set_md_var(tc, "descr", "Verify clone(%s) "		\
6910	    "called with 0%s%s%s in EVENT_MASK",			\
6911	    #flags,							\
6912	    tfork ? "|PTRACE_FORK" : "",				\
6913	    tvfork ? "|PTRACE_VFORK" : "",				\
6914	    tvforkdone ? "|PTRACE_VFORK_DONE" : "");			\
6915}									\
6916									\
6917ATF_TC_BODY(name, tc)							\
6918{									\
6919									\
6920	clone_body(flags, tfork, tvfork, tvforkdone);			\
6921}
6922
6923CLONE_TEST(clone1, 0, false, false, false)
6924#if defined(TWAIT_HAVE_PID)
6925CLONE_TEST(clone2, 0, true, false, false)
6926CLONE_TEST(clone3, 0, false, true, false)
6927CLONE_TEST(clone4, 0, true, true, false)
6928#endif
6929CLONE_TEST(clone5, 0, false, false, true)
6930#if defined(TWAIT_HAVE_PID)
6931CLONE_TEST(clone6, 0, true, false, true)
6932CLONE_TEST(clone7, 0, false, true, true)
6933CLONE_TEST(clone8, 0, true, true, true)
6934#endif
6935
6936CLONE_TEST(clone_vm1, CLONE_VM, false, false, false)
6937#if defined(TWAIT_HAVE_PID)
6938CLONE_TEST(clone_vm2, CLONE_VM, true, false, false)
6939CLONE_TEST(clone_vm3, CLONE_VM, false, true, false)
6940CLONE_TEST(clone_vm4, CLONE_VM, true, true, false)
6941#endif
6942CLONE_TEST(clone_vm5, CLONE_VM, false, false, true)
6943#if defined(TWAIT_HAVE_PID)
6944CLONE_TEST(clone_vm6, CLONE_VM, true, false, true)
6945CLONE_TEST(clone_vm7, CLONE_VM, false, true, true)
6946CLONE_TEST(clone_vm8, CLONE_VM, true, true, true)
6947#endif
6948
6949CLONE_TEST(clone_fs1, CLONE_FS, false, false, false)
6950#if defined(TWAIT_HAVE_PID)
6951CLONE_TEST(clone_fs2, CLONE_FS, true, false, false)
6952CLONE_TEST(clone_fs3, CLONE_FS, false, true, false)
6953CLONE_TEST(clone_fs4, CLONE_FS, true, true, false)
6954#endif
6955CLONE_TEST(clone_fs5, CLONE_FS, false, false, true)
6956#if defined(TWAIT_HAVE_PID)
6957CLONE_TEST(clone_fs6, CLONE_FS, true, false, true)
6958CLONE_TEST(clone_fs7, CLONE_FS, false, true, true)
6959CLONE_TEST(clone_fs8, CLONE_FS, true, true, true)
6960#endif
6961
6962CLONE_TEST(clone_files1, CLONE_FILES, false, false, false)
6963#if defined(TWAIT_HAVE_PID)
6964CLONE_TEST(clone_files2, CLONE_FILES, true, false, false)
6965CLONE_TEST(clone_files3, CLONE_FILES, false, true, false)
6966CLONE_TEST(clone_files4, CLONE_FILES, true, true, false)
6967#endif
6968CLONE_TEST(clone_files5, CLONE_FILES, false, false, true)
6969#if defined(TWAIT_HAVE_PID)
6970CLONE_TEST(clone_files6, CLONE_FILES, true, false, true)
6971CLONE_TEST(clone_files7, CLONE_FILES, false, true, true)
6972CLONE_TEST(clone_files8, CLONE_FILES, true, true, true)
6973#endif
6974
6975//CLONE_TEST(clone_sighand1, CLONE_SIGHAND, false, false, false)
6976#if defined(TWAIT_HAVE_PID)
6977//CLONE_TEST(clone_sighand2, CLONE_SIGHAND, true, false, false)
6978//CLONE_TEST(clone_sighand3, CLONE_SIGHAND, false, true, false)
6979//CLONE_TEST(clone_sighand4, CLONE_SIGHAND, true, true, false)
6980#endif
6981//CLONE_TEST(clone_sighand5, CLONE_SIGHAND, false, false, true)
6982#if defined(TWAIT_HAVE_PID)
6983//CLONE_TEST(clone_sighand6, CLONE_SIGHAND, true, false, true)
6984//CLONE_TEST(clone_sighand7, CLONE_SIGHAND, false, true, true)
6985//CLONE_TEST(clone_sighand8, CLONE_SIGHAND, true, true, true)
6986#endif
6987
6988#if TEST_VFORK_ENABLED
6989CLONE_TEST(clone_vfork1, CLONE_VFORK, false, false, false)
6990#if defined(TWAIT_HAVE_PID)
6991CLONE_TEST(clone_vfork2, CLONE_VFORK, true, false, false)
6992CLONE_TEST(clone_vfork3, CLONE_VFORK, false, true, false)
6993CLONE_TEST(clone_vfork4, CLONE_VFORK, true, true, false)
6994#endif
6995CLONE_TEST(clone_vfork5, CLONE_VFORK, false, false, true)
6996#if defined(TWAIT_HAVE_PID)
6997CLONE_TEST(clone_vfork6, CLONE_VFORK, true, false, true)
6998CLONE_TEST(clone_vfork7, CLONE_VFORK, false, true, true)
6999CLONE_TEST(clone_vfork8, CLONE_VFORK, true, true, true)
7000#endif
7001#endif
7002
7003/// ----------------------------------------------------------------------------
7004
7005#if defined(TWAIT_HAVE_PID)
7006static void
7007clone_body2(int flags, bool masked, bool ignored)
7008{
7009	const int exitval = 5;
7010	const int exitval2 = 15;
7011	const int sigval = SIGSTOP;
7012	pid_t child, child2 = 0, wpid;
7013#if defined(TWAIT_HAVE_STATUS)
7014	int status;
7015#endif
7016	ptrace_state_t state;
7017	const int slen = sizeof(state);
7018	ptrace_event_t event;
7019	const int elen = sizeof(event);
7020	struct sigaction sa;
7021	struct ptrace_siginfo info;
7022	sigset_t intmask;
7023	struct kinfo_proc2 kp;
7024	size_t len = sizeof(kp);
7025
7026	int name[6];
7027	const size_t namelen = __arraycount(name);
7028	ki_sigset_t kp_sigmask;
7029	ki_sigset_t kp_sigignore;
7030
7031	const size_t stack_size = 1024 * 1024;
7032	void *stack, *stack_base;
7033
7034	stack = malloc(stack_size);
7035	ATF_REQUIRE(stack != NULL);
7036
7037#ifdef __MACHINE_STACK_GROWS_UP
7038	stack_base = stack;
7039#else
7040	stack_base = (char *)stack + stack_size;
7041#endif
7042
7043	SYSCALL_REQUIRE((child = fork()) != -1);
7044	if (child == 0) {
7045		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7046		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7047
7048		if (masked) {
7049			sigemptyset(&intmask);
7050			sigaddset(&intmask, SIGTRAP);
7051			sigprocmask(SIG_BLOCK, &intmask, NULL);
7052		}
7053
7054		if (ignored) {
7055			memset(&sa, 0, sizeof(sa));
7056			sa.sa_handler = SIG_IGN;
7057			sigemptyset(&sa.sa_mask);
7058			FORKEE_ASSERT(sigaction(SIGTRAP, &sa, NULL) != -1);
7059		}
7060		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7061		FORKEE_ASSERT(raise(sigval) == 0);
7062
7063		DPRINTF("Before forking process PID=%d flags=%#x\n", getpid(),
7064		    flags);
7065		SYSCALL_REQUIRE((child2 = __clone(clone_func, stack_base,
7066		    flags|SIGCHLD, (void *)(intptr_t)exitval2)) != -1);
7067
7068		DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(),
7069		    child2);
7070
7071		// XXX WALLSIG?
7072		FORKEE_REQUIRE_SUCCESS
7073		    (wpid = TWAIT_GENERIC(child2, &status, WALLSIG), child2);
7074
7075		forkee_status_exited(status, exitval2);
7076
7077		DPRINTF("Before exiting of the child process\n");
7078		_exit(exitval);
7079	}
7080	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7081
7082	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7083	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7084
7085	validate_status_stopped(status, sigval);
7086
7087	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7088	SYSCALL_REQUIRE(
7089	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7090
7091	DPRINTF("Before checking siginfo_t\n");
7092	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
7093	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
7094
7095	name[0] = CTL_KERN,
7096	name[1] = KERN_PROC2,
7097	name[2] = KERN_PROC_PID;
7098	name[3] = child;
7099	name[4] = sizeof(kp);
7100	name[5] = 1;
7101
7102	FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
7103
7104	if (masked)
7105		kp_sigmask = kp.p_sigmask;
7106
7107	if (ignored)
7108		kp_sigignore = kp.p_sigignore;
7109
7110	DPRINTF("Set PTRACE_FORK | PTRACE_VFORK | PTRACE_VFORK_DONE in "
7111	    "EVENT_MASK for the child %d\n", child);
7112	event.pe_set_event = PTRACE_FORK | PTRACE_VFORK | PTRACE_VFORK_DONE;
7113	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
7114
7115	DPRINTF("Before resuming the child process where it left off and "
7116	    "without signal to be sent\n");
7117	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7118
7119	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
7120	    child);
7121	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
7122	    child);
7123
7124	validate_status_stopped(status, SIGTRAP);
7125
7126	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
7127
7128	if (masked) {
7129		DPRINTF("kp_sigmask="
7130		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7131		    PRIx32 "\n",
7132		    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
7133		    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
7134
7135		DPRINTF("kp.p_sigmask="
7136		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7137		    PRIx32 "\n",
7138		    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
7139		    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
7140
7141		ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
7142		    sizeof(kp_sigmask)));
7143	}
7144
7145	if (ignored) {
7146		DPRINTF("kp_sigignore="
7147		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7148		    PRIx32 "\n",
7149		    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
7150		    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
7151
7152		DPRINTF("kp.p_sigignore="
7153		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7154		    PRIx32 "\n",
7155		    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
7156		    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
7157
7158		ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
7159		    sizeof(kp_sigignore)));
7160	}
7161
7162	SYSCALL_REQUIRE(
7163	    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
7164	DPRINTF("state.pe_report_event=%#x pid=%d\n", state.pe_report_event,
7165	    child2);
7166	if (!(flags & CLONE_VFORK)) {
7167		ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
7168		       PTRACE_FORK);
7169	} else {
7170		ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
7171		       PTRACE_VFORK);
7172	}
7173
7174	child2 = state.pe_other_pid;
7175	DPRINTF("Reported ptrace event with forkee %d\n", child2);
7176
7177	DPRINTF("Before calling %s() for the forkee %d of the child "
7178	    "%d\n", TWAIT_FNAME, child2, child);
7179	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
7180	    child2);
7181
7182	validate_status_stopped(status, SIGTRAP);
7183
7184	name[3] = child2;
7185	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
7186
7187	if (masked) {
7188		DPRINTF("kp_sigmask="
7189		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7190		    PRIx32 "\n",
7191		    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
7192		    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
7193
7194		DPRINTF("kp.p_sigmask="
7195		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7196		    PRIx32 "\n",
7197		    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
7198		    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
7199
7200		ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
7201		    sizeof(kp_sigmask)));
7202	}
7203
7204	if (ignored) {
7205		DPRINTF("kp_sigignore="
7206		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7207		    PRIx32 "\n",
7208		    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
7209		    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
7210
7211		DPRINTF("kp.p_sigignore="
7212		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7213		    PRIx32 "\n",
7214		    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
7215		    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
7216
7217		ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
7218		    sizeof(kp_sigignore)));
7219	}
7220
7221	SYSCALL_REQUIRE(
7222	    ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
7223	if (!(flags & CLONE_VFORK)) {
7224		ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
7225		       PTRACE_FORK);
7226	} else {
7227		ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
7228		       PTRACE_VFORK);
7229	}
7230
7231	ATF_REQUIRE_EQ(state.pe_other_pid, child);
7232
7233	DPRINTF("Before resuming the forkee process where it left off "
7234	    "and without signal to be sent\n");
7235	SYSCALL_REQUIRE(
7236	    ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
7237
7238	DPRINTF("Before resuming the child process where it left off "
7239	    "and without signal to be sent\n");
7240	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7241
7242	if (flags & CLONE_VFORK) {
7243		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
7244		    child);
7245		TWAIT_REQUIRE_SUCCESS(
7246		    wpid = TWAIT_GENERIC(child, &status, 0), child);
7247
7248		validate_status_stopped(status, SIGTRAP);
7249
7250		name[3] = child;
7251		ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
7252
7253		/*
7254		 * SIGCHLD is now pending in the signal queue and
7255		 * the kernel presents it to userland as a masked signal.
7256		 */
7257		sigdelset((sigset_t *)&kp.p_sigmask, SIGCHLD);
7258
7259		if (masked) {
7260			DPRINTF("kp_sigmask="
7261			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7262			    PRIx32 "\n",
7263			    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
7264			    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
7265
7266			DPRINTF("kp.p_sigmask="
7267			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7268			    PRIx32 "\n",
7269			    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
7270			    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
7271
7272			ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
7273			    sizeof(kp_sigmask)));
7274		}
7275
7276		if (ignored) {
7277			DPRINTF("kp_sigignore="
7278			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7279			    PRIx32 "\n",
7280			    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
7281			    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
7282
7283			DPRINTF("kp.p_sigignore="
7284			    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02"
7285			    PRIx32 "\n",
7286			    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
7287			    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
7288
7289			ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
7290			    sizeof(kp_sigignore)));
7291		}
7292
7293		SYSCALL_REQUIRE(
7294		    ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
7295		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
7296
7297		child2 = state.pe_other_pid;
7298		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
7299		    child2);
7300
7301		DPRINTF("Before resuming the child process where it left off "
7302		    "and without signal to be sent\n");
7303		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7304	}
7305
7306	DPRINTF("Before calling %s() for the forkee - expected exited"
7307	    "\n", TWAIT_FNAME);
7308	TWAIT_REQUIRE_SUCCESS(
7309	    wpid = TWAIT_GENERIC(child2, &status, 0), child2);
7310
7311	validate_status_exited(status, exitval2);
7312
7313	DPRINTF("Before calling %s() for the forkee - expected no "
7314	    "process\n", TWAIT_FNAME);
7315	TWAIT_REQUIRE_FAILURE(ECHILD,
7316	    wpid = TWAIT_GENERIC(child2, &status, 0));
7317
7318	DPRINTF("Before calling %s() for the child - expected stopped "
7319	    "SIGCHLD\n", TWAIT_FNAME);
7320	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7321
7322	validate_status_stopped(status, SIGCHLD);
7323
7324	DPRINTF("Before resuming the child process where it left off and "
7325	    "without signal to be sent\n");
7326	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7327
7328	DPRINTF("Before calling %s() for the child - expected exited\n",
7329	    TWAIT_FNAME);
7330	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7331
7332	validate_status_exited(status, exitval);
7333
7334	DPRINTF("Before calling %s() for the child - expected no process\n",
7335	    TWAIT_FNAME);
7336	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7337}
7338
7339#define CLONE_TEST2(name,flags,masked,ignored)				\
7340ATF_TC(name);								\
7341ATF_TC_HEAD(name, tc)							\
7342{									\
7343	atf_tc_set_md_var(tc, "descr", "Verify that clone(%s) is caught"\
7344	    " regardless of signal %s%s", 				\
7345	    #flags, masked ? "masked" : "", ignored ? "ignored" : "");	\
7346}									\
7347									\
7348ATF_TC_BODY(name, tc)							\
7349{									\
7350									\
7351	clone_body2(flags, masked, ignored);				\
7352}
7353
7354CLONE_TEST2(clone_signalignored, 0, true, false)
7355CLONE_TEST2(clone_signalmasked, 0, false, true)
7356CLONE_TEST2(clone_vm_signalignored, CLONE_VM, true, false)
7357CLONE_TEST2(clone_vm_signalmasked, CLONE_VM, false, true)
7358CLONE_TEST2(clone_fs_signalignored, CLONE_FS, true, false)
7359CLONE_TEST2(clone_fs_signalmasked, CLONE_FS, false, true)
7360CLONE_TEST2(clone_files_signalignored, CLONE_FILES, true, false)
7361CLONE_TEST2(clone_files_signalmasked, CLONE_FILES, false, true)
7362//CLONE_TEST2(clone_sighand_signalignored, CLONE_SIGHAND, true, false) // XXX
7363//CLONE_TEST2(clone_sighand_signalmasked, CLONE_SIGHAND, false, true)  // XXX
7364#if TEST_VFORK_ENABLED
7365CLONE_TEST2(clone_vfork_signalignored, CLONE_VFORK, true, false)
7366CLONE_TEST2(clone_vfork_signalmasked, CLONE_VFORK, false, true)
7367#endif
7368#endif
7369
7370/// ----------------------------------------------------------------------------
7371
7372#if TEST_VFORK_ENABLED
7373#if defined(TWAIT_HAVE_PID)
7374static void
7375traceme_vfork_clone_body(int flags)
7376{
7377	const int exitval = 5;
7378	const int exitval2 = 15;
7379	pid_t child, child2 = 0, wpid;
7380#if defined(TWAIT_HAVE_STATUS)
7381	int status;
7382#endif
7383
7384	const size_t stack_size = 1024 * 1024;
7385	void *stack, *stack_base;
7386
7387	stack = malloc(stack_size);
7388	ATF_REQUIRE(stack != NULL);
7389
7390#ifdef __MACHINE_STACK_GROWS_UP
7391	stack_base = stack;
7392#else
7393	stack_base = (char *)stack + stack_size;
7394#endif
7395
7396	SYSCALL_REQUIRE((child = vfork()) != -1);
7397	if (child == 0) {
7398		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7399		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7400
7401		DPRINTF("Before forking process PID=%d flags=%#x\n", getpid(),
7402		    flags);
7403		SYSCALL_REQUIRE((child2 = __clone(clone_func, stack_base,
7404		    flags|SIGCHLD, (void *)(intptr_t)exitval2)) != -1);
7405
7406		DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(),
7407		    child2);
7408
7409		// XXX WALLSIG?
7410		FORKEE_REQUIRE_SUCCESS
7411		    (wpid = TWAIT_GENERIC(child2, &status, WALLSIG), child2);
7412
7413		forkee_status_exited(status, exitval2);
7414
7415		DPRINTF("Before exiting of the child process\n");
7416		_exit(exitval);
7417	}
7418	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7419
7420	DPRINTF("Before calling %s() for the child - expected exited\n",
7421	    TWAIT_FNAME);
7422	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7423
7424	validate_status_exited(status, exitval);
7425
7426	DPRINTF("Before calling %s() for the child - expected no process\n",
7427	    TWAIT_FNAME);
7428	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7429}
7430
7431#define TRACEME_VFORK_CLONE_TEST(name,flags)				\
7432ATF_TC(name);								\
7433ATF_TC_HEAD(name, tc)							\
7434{									\
7435	atf_tc_set_md_var(tc, "descr", "Verify that clone(%s) is "	\
7436	    "handled correctly with vfork(2)ed tracer", 		\
7437	    #flags);							\
7438}									\
7439									\
7440ATF_TC_BODY(name, tc)							\
7441{									\
7442									\
7443	traceme_vfork_clone_body(flags);				\
7444}
7445
7446TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone, 0)
7447TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_vm, CLONE_VM)
7448TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_fs, CLONE_FS)
7449TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_files, CLONE_FILES)
7450//TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_sighand, CLONE_SIGHAND)  // XXX
7451TRACEME_VFORK_CLONE_TEST(traceme_vfork_clone_vfork, CLONE_VFORK)
7452#endif
7453#endif
7454
7455/// ----------------------------------------------------------------------------
7456
7457#include "t_ptrace_amd64_wait.h"
7458#include "t_ptrace_i386_wait.h"
7459#include "t_ptrace_x86_wait.h"
7460
7461ATF_TP_ADD_TCS(tp)
7462{
7463	setvbuf(stdout, NULL, _IONBF, 0);
7464	setvbuf(stderr, NULL, _IONBF, 0);
7465
7466	ATF_TP_ADD_TC(tp, traceme_raise1);
7467	ATF_TP_ADD_TC(tp, traceme_raise2);
7468	ATF_TP_ADD_TC(tp, traceme_raise3);
7469	ATF_TP_ADD_TC(tp, traceme_raise4);
7470	ATF_TP_ADD_TC(tp, traceme_raise5);
7471	ATF_TP_ADD_TC(tp, traceme_raise6);
7472	ATF_TP_ADD_TC(tp, traceme_raise7);
7473	ATF_TP_ADD_TC(tp, traceme_raise8);
7474	ATF_TP_ADD_TC(tp, traceme_raise9);
7475	ATF_TP_ADD_TC(tp, traceme_raise10);
7476
7477	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored1);
7478	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored2);
7479	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored3);
7480	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored4);
7481	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored5);
7482	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored6);
7483	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored7);
7484	ATF_TP_ADD_TC(tp, traceme_raisesignal_ignored8);
7485
7486	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked1);
7487	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked2);
7488	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked3);
7489	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked4);
7490	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked5);
7491	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked6);
7492	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked7);
7493	ATF_TP_ADD_TC(tp, traceme_raisesignal_masked8);
7494
7495	ATF_TP_ADD_TC(tp, traceme_crash_trap);
7496	ATF_TP_ADD_TC(tp, traceme_crash_segv);
7497	ATF_TP_ADD_TC(tp, traceme_crash_ill);
7498	ATF_TP_ADD_TC(tp, traceme_crash_fpe);
7499	ATF_TP_ADD_TC(tp, traceme_crash_bus);
7500
7501	ATF_TP_ADD_TC(tp, traceme_signalmasked_crash_trap);
7502	ATF_TP_ADD_TC(tp, traceme_signalmasked_crash_segv);
7503	ATF_TP_ADD_TC(tp, traceme_signalmasked_crash_ill);
7504	ATF_TP_ADD_TC(tp, traceme_signalmasked_crash_fpe);
7505	ATF_TP_ADD_TC(tp, traceme_signalmasked_crash_bus);
7506
7507	ATF_TP_ADD_TC(tp, traceme_signalignored_crash_trap);
7508	ATF_TP_ADD_TC(tp, traceme_signalignored_crash_segv);
7509	ATF_TP_ADD_TC(tp, traceme_signalignored_crash_ill);
7510	ATF_TP_ADD_TC(tp, traceme_signalignored_crash_fpe);
7511	ATF_TP_ADD_TC(tp, traceme_signalignored_crash_bus);
7512
7513	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle1);
7514	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle2);
7515	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle3);
7516	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle4);
7517	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle5);
7518	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle6);
7519	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle7);
7520	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle8);
7521
7522	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked1);
7523	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked2);
7524	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked3);
7525	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked4);
7526	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked5);
7527	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked6);
7528	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked7);
7529	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked8);
7530
7531	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored1);
7532	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored2);
7533	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored3);
7534	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored4);
7535	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored5);
7536	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored6);
7537	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored7);
7538	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored8);
7539
7540	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple1);
7541	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple2);
7542	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple3);
7543	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple4);
7544	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple5);
7545	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple6);
7546	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple7);
7547	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple8);
7548	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple9);
7549	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple10);
7550
7551	ATF_TP_ADD_TC(tp, traceme_pid1_parent);
7552
7553	ATF_TP_ADD_TC(tp, traceme_vfork_raise1);
7554	ATF_TP_ADD_TC(tp, traceme_vfork_raise2);
7555	ATF_TP_ADD_TC(tp, traceme_vfork_raise3);
7556	ATF_TP_ADD_TC(tp, traceme_vfork_raise4);
7557	ATF_TP_ADD_TC(tp, traceme_vfork_raise5);
7558	ATF_TP_ADD_TC(tp, traceme_vfork_raise6);
7559	ATF_TP_ADD_TC(tp, traceme_vfork_raise7);
7560	ATF_TP_ADD_TC(tp, traceme_vfork_raise8);
7561	ATF_TP_ADD_TC(tp, traceme_vfork_raise9);
7562	ATF_TP_ADD_TC(tp, traceme_vfork_raise10);
7563	ATF_TP_ADD_TC(tp, traceme_vfork_raise11);
7564	ATF_TP_ADD_TC(tp, traceme_vfork_raise12);
7565	ATF_TP_ADD_TC(tp, traceme_vfork_raise13);
7566
7567	ATF_TP_ADD_TC(tp, traceme_vfork_crash_trap);
7568	ATF_TP_ADD_TC(tp, traceme_vfork_crash_segv);
7569	ATF_TP_ADD_TC(tp, traceme_vfork_crash_ill);
7570	ATF_TP_ADD_TC(tp, traceme_vfork_crash_fpe);
7571	ATF_TP_ADD_TC(tp, traceme_vfork_crash_bus);
7572
7573	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_crash_trap);
7574	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_crash_segv);
7575	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_crash_ill);
7576	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_crash_fpe);
7577	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_crash_bus);
7578
7579	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_crash_trap);
7580	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_crash_segv);
7581	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_crash_ill);
7582	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_crash_fpe);
7583	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_crash_bus);
7584
7585	ATF_TP_ADD_TC(tp, traceme_vfork_exec);
7586	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_exec);
7587	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_exec);
7588
7589	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_trap);
7590	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_segv);
7591	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_ill);
7592	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_fpe);
7593	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_crash_bus);
7594
7595	ATF_TP_ADD_TC_HAVE_PID(tp,
7596	    unrelated_tracer_sees_signalmasked_crash_trap);
7597	ATF_TP_ADD_TC_HAVE_PID(tp,
7598	    unrelated_tracer_sees_signalmasked_crash_segv);
7599	ATF_TP_ADD_TC_HAVE_PID(tp,
7600	    unrelated_tracer_sees_signalmasked_crash_ill);
7601	ATF_TP_ADD_TC_HAVE_PID(tp,
7602	    unrelated_tracer_sees_signalmasked_crash_fpe);
7603	ATF_TP_ADD_TC_HAVE_PID(tp,
7604	    unrelated_tracer_sees_signalmasked_crash_bus);
7605
7606	ATF_TP_ADD_TC_HAVE_PID(tp,
7607	    unrelated_tracer_sees_signalignored_crash_trap);
7608	ATF_TP_ADD_TC_HAVE_PID(tp,
7609	    unrelated_tracer_sees_signalignored_crash_segv);
7610	ATF_TP_ADD_TC_HAVE_PID(tp,
7611	    unrelated_tracer_sees_signalignored_crash_ill);
7612	ATF_TP_ADD_TC_HAVE_PID(tp,
7613	    unrelated_tracer_sees_signalignored_crash_fpe);
7614	ATF_TP_ADD_TC_HAVE_PID(tp,
7615	    unrelated_tracer_sees_signalignored_crash_bus);
7616
7617	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sees_terminaton_before_the_parent);
7618	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sysctl_lookup_without_duplicates);
7619	ATF_TP_ADD_TC_HAVE_PID(tp,
7620		unrelated_tracer_sees_terminaton_before_the_parent);
7621	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_attach_to_unrelated_stopped_process);
7622
7623	ATF_TP_ADD_TC(tp, parent_attach_to_its_child);
7624	ATF_TP_ADD_TC(tp, parent_attach_to_its_stopped_child);
7625
7626	ATF_TP_ADD_TC(tp, child_attach_to_its_parent);
7627	ATF_TP_ADD_TC(tp, child_attach_to_its_stopped_parent);
7628
7629	ATF_TP_ADD_TC_HAVE_PID(tp,
7630		tracee_sees_its_original_parent_getppid);
7631	ATF_TP_ADD_TC_HAVE_PID(tp,
7632		tracee_sees_its_original_parent_sysctl_kinfo_proc2);
7633	ATF_TP_ADD_TC_HAVE_PID(tp,
7634		tracee_sees_its_original_parent_procfs_status);
7635
7636	ATF_TP_ADD_TC(tp, eventmask_preserved_empty);
7637	ATF_TP_ADD_TC(tp, eventmask_preserved_fork);
7638	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork);
7639	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork_done);
7640	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_create);
7641	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_exit);
7642
7643	ATF_TP_ADD_TC(tp, fork1);
7644	ATF_TP_ADD_TC_HAVE_PID(tp, fork2);
7645	ATF_TP_ADD_TC_HAVE_PID(tp, fork3);
7646	ATF_TP_ADD_TC_HAVE_PID(tp, fork4);
7647	ATF_TP_ADD_TC(tp, fork5);
7648	ATF_TP_ADD_TC_HAVE_PID(tp, fork6);
7649	ATF_TP_ADD_TC_HAVE_PID(tp, fork7);
7650	ATF_TP_ADD_TC_HAVE_PID(tp, fork8);
7651
7652#if TEST_VFORK_ENABLED
7653	ATF_TP_ADD_TC(tp, vfork1);
7654	ATF_TP_ADD_TC_HAVE_PID(tp, vfork2);
7655	ATF_TP_ADD_TC_HAVE_PID(tp, vfork3);
7656	ATF_TP_ADD_TC_HAVE_PID(tp, vfork4);
7657	ATF_TP_ADD_TC(tp, vfork5);
7658	ATF_TP_ADD_TC_HAVE_PID(tp, vfork6);
7659	ATF_TP_ADD_TC_HAVE_PID(tp, vfork7);
7660	ATF_TP_ADD_TC_HAVE_PID(tp, vfork8);
7661#endif
7662
7663	ATF_TP_ADD_TC_HAVE_PID(tp, fork_detach_forker);
7664#if TEST_VFORK_ENABLED
7665	ATF_TP_ADD_TC_HAVE_PID(tp, vfork_detach_vforker);
7666	ATF_TP_ADD_TC_HAVE_PID(tp, vfork_detach_vforkerdone);
7667#endif
7668	ATF_TP_ADD_TC_HAVE_PID(tp, fork_kill_forker);
7669#if TEST_VFORK_ENABLED
7670	ATF_TP_ADD_TC_HAVE_PID(tp, vfork_kill_vforker);
7671	ATF_TP_ADD_TC_HAVE_PID(tp, vfork_kill_vforkerdone);
7672#endif
7673
7674#if TEST_VFORK_ENABLED
7675	ATF_TP_ADD_TC(tp, traceme_vfork_fork);
7676	ATF_TP_ADD_TC(tp, traceme_vfork_vfork);
7677#endif
7678
7679	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8);
7680	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16);
7681	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32);
7682	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64);
7683
7684	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8);
7685	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16);
7686	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32);
7687	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64);
7688
7689	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8);
7690	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16);
7691	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32);
7692	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64);
7693
7694	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8);
7695	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16);
7696	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32);
7697	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64);
7698
7699	ATF_TP_ADD_TC(tp, bytes_transfer_read_d);
7700	ATF_TP_ADD_TC(tp, bytes_transfer_read_i);
7701	ATF_TP_ADD_TC(tp, bytes_transfer_write_d);
7702	ATF_TP_ADD_TC(tp, bytes_transfer_write_i);
7703
7704	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8_text);
7705	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16_text);
7706	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32_text);
7707	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64_text);
7708
7709	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8_text);
7710	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16_text);
7711	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32_text);
7712	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64_text);
7713
7714	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8_text);
7715	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16_text);
7716	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32_text);
7717	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64_text);
7718
7719	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8_text);
7720	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16_text);
7721	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32_text);
7722	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64_text);
7723
7724	ATF_TP_ADD_TC(tp, bytes_transfer_read_d_text);
7725	ATF_TP_ADD_TC(tp, bytes_transfer_read_i_text);
7726	ATF_TP_ADD_TC(tp, bytes_transfer_write_d_text);
7727	ATF_TP_ADD_TC(tp, bytes_transfer_write_i_text);
7728
7729	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_auxv);
7730
7731	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_read_i);
7732	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_read_d);
7733	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_write_i);
7734	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_write_d);
7735
7736	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_read_i);
7737	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_read_d);
7738	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_write_i);
7739	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_write_d);
7740
7741	ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_read_auxv);
7742
7743	ATF_TP_ADD_TC(tp, bytes_transfer_eof_pt_read_i);
7744	ATF_TP_ADD_TC(tp, bytes_transfer_eof_pt_read_d);
7745	ATF_TP_ADD_TC(tp, bytes_transfer_eof_pt_write_i);
7746	ATF_TP_ADD_TC(tp, bytes_transfer_eof_pt_write_d);
7747
7748	ATF_TP_ADD_TC(tp, bytes_transfer_eof_piod_read_i);
7749	ATF_TP_ADD_TC(tp, bytes_transfer_eof_piod_read_d);
7750	ATF_TP_ADD_TC(tp, bytes_transfer_eof_piod_write_i);
7751	ATF_TP_ADD_TC(tp, bytes_transfer_eof_piod_write_d);
7752
7753	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs1);
7754	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs2);
7755	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs3);
7756	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs4);
7757	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs5);
7758	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs6);
7759
7760	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs1);
7761	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs2);
7762
7763	ATF_TP_ADD_TC_PT_STEP(tp, step1);
7764	ATF_TP_ADD_TC_PT_STEP(tp, step2);
7765	ATF_TP_ADD_TC_PT_STEP(tp, step3);
7766	ATF_TP_ADD_TC_PT_STEP(tp, step4);
7767
7768	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
7769	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
7770	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
7771	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
7772
7773	ATF_TP_ADD_TC_PT_STEP(tp, step_signalmasked);
7774	ATF_TP_ADD_TC_PT_STEP(tp, step_signalignored);
7775
7776	ATF_TP_ADD_TC(tp, kill1);
7777	ATF_TP_ADD_TC(tp, kill2);
7778	ATF_TP_ADD_TC(tp, kill3);
7779
7780	ATF_TP_ADD_TC(tp, traceme_lwpinfo0);
7781	ATF_TP_ADD_TC(tp, traceme_lwpinfo1);
7782	ATF_TP_ADD_TC(tp, traceme_lwpinfo2);
7783	ATF_TP_ADD_TC(tp, traceme_lwpinfo3);
7784
7785	ATF_TP_ADD_TC_HAVE_PID(tp, attach_lwpinfo0);
7786	ATF_TP_ADD_TC_HAVE_PID(tp, attach_lwpinfo1);
7787	ATF_TP_ADD_TC_HAVE_PID(tp, attach_lwpinfo2);
7788	ATF_TP_ADD_TC_HAVE_PID(tp, attach_lwpinfo3);
7789
7790	ATF_TP_ADD_TC(tp, siginfo_set_unmodified);
7791	ATF_TP_ADD_TC(tp, siginfo_set_faked);
7792
7793	ATF_TP_ADD_TC(tp, traceme_exec);
7794	ATF_TP_ADD_TC(tp, traceme_signalmasked_exec);
7795	ATF_TP_ADD_TC(tp, traceme_signalignored_exec);
7796
7797	ATF_TP_ADD_TC(tp, trace_thread1);
7798	ATF_TP_ADD_TC(tp, trace_thread2);
7799	ATF_TP_ADD_TC(tp, trace_thread3);
7800	ATF_TP_ADD_TC(tp, trace_thread4);
7801
7802	ATF_TP_ADD_TC(tp, signal_mask_unrelated);
7803
7804	ATF_TP_ADD_TC_HAVE_PID(tp, fork_singalmasked);
7805	ATF_TP_ADD_TC_HAVE_PID(tp, fork_singalignored);
7806#if TEST_VFORK_ENABLED
7807	ATF_TP_ADD_TC_HAVE_PID(tp, vfork_singalmasked);
7808	ATF_TP_ADD_TC_HAVE_PID(tp, vfork_singalignored);
7809	ATF_TP_ADD_TC_HAVE_PID(tp, vforkdone_singalmasked);
7810	ATF_TP_ADD_TC_HAVE_PID(tp, vforkdone_singalignored);
7811#endif
7812
7813	ATF_TP_ADD_TC(tp, signal9);
7814	ATF_TP_ADD_TC(tp, signal10);
7815
7816	ATF_TP_ADD_TC(tp, suspend1);
7817	ATF_TP_ADD_TC(tp, suspend2);
7818
7819	ATF_TP_ADD_TC(tp, resume1);
7820
7821	ATF_TP_ADD_TC(tp, syscall1);
7822
7823	ATF_TP_ADD_TC(tp, syscallemu1);
7824
7825	ATF_TP_ADD_TC(tp, clone1);
7826	ATF_TP_ADD_TC_HAVE_PID(tp, clone2);
7827	ATF_TP_ADD_TC_HAVE_PID(tp, clone3);
7828	ATF_TP_ADD_TC_HAVE_PID(tp, clone4);
7829	ATF_TP_ADD_TC(tp, clone5);
7830	ATF_TP_ADD_TC_HAVE_PID(tp, clone6);
7831	ATF_TP_ADD_TC_HAVE_PID(tp, clone7);
7832	ATF_TP_ADD_TC_HAVE_PID(tp, clone8);
7833
7834	ATF_TP_ADD_TC(tp, clone_vm1);
7835	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm2);
7836	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm3);
7837	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm4);
7838	ATF_TP_ADD_TC(tp, clone_vm5);
7839	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm6);
7840	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm7);
7841	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm8);
7842
7843	ATF_TP_ADD_TC(tp, clone_fs1);
7844	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs2);
7845	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs3);
7846	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs4);
7847	ATF_TP_ADD_TC(tp, clone_fs5);
7848	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs6);
7849	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs7);
7850	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs8);
7851
7852	ATF_TP_ADD_TC(tp, clone_files1);
7853	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files2);
7854	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files3);
7855	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files4);
7856	ATF_TP_ADD_TC(tp, clone_files5);
7857	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files6);
7858	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files7);
7859	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files8);
7860
7861//	ATF_TP_ADD_TC(tp, clone_sighand1); // XXX
7862//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand2); // XXX
7863//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand3); // XXX
7864//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand4); // XXX
7865//	ATF_TP_ADD_TC(tp, clone_sighand5); // XXX
7866//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand6); // XXX
7867//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand7); // XXX
7868//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand8); // XXX
7869
7870#if TEST_VFORK_ENABLED
7871	ATF_TP_ADD_TC(tp, clone_vfork1);
7872	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork2);
7873	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork3);
7874	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork4);
7875	ATF_TP_ADD_TC(tp, clone_vfork5);
7876	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork6);
7877	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork7);
7878	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork8);
7879#endif
7880
7881	ATF_TP_ADD_TC_HAVE_PID(tp, clone_signalignored);
7882	ATF_TP_ADD_TC_HAVE_PID(tp, clone_signalmasked);
7883	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm_signalignored);
7884	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vm_signalmasked);
7885	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs_signalignored);
7886	ATF_TP_ADD_TC_HAVE_PID(tp, clone_fs_signalmasked);
7887	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files_signalignored);
7888	ATF_TP_ADD_TC_HAVE_PID(tp, clone_files_signalmasked);
7889//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand_signalignored); // XXX
7890//	ATF_TP_ADD_TC_HAVE_PID(tp, clone_sighand_signalmasked); // XXX
7891#if TEST_VFORK_ENABLED
7892	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork_signalignored);
7893	ATF_TP_ADD_TC_HAVE_PID(tp, clone_vfork_signalmasked);
7894#endif
7895
7896#if TEST_VFORK_ENABLED
7897	ATF_TP_ADD_TC_HAVE_PID(tp, traceme_vfork_clone);
7898	ATF_TP_ADD_TC_HAVE_PID(tp, traceme_vfork_clone_vm);
7899	ATF_TP_ADD_TC_HAVE_PID(tp, traceme_vfork_clone_fs);
7900	ATF_TP_ADD_TC_HAVE_PID(tp, traceme_vfork_clone_files);
7901//	ATF_TP_ADD_TC_HAVE_PID(tp, traceme_vfork_clone_sighand); // XXX
7902	ATF_TP_ADD_TC_HAVE_PID(tp, traceme_vfork_clone_vfork);
7903#endif
7904
7905	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
7906	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
7907	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
7908
7909	return atf_no_error();
7910}
7911