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