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