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