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