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