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