t_ptrace_wait.c revision 1.55
1/*	$NetBSD: t_ptrace_wait.c,v 1.55 2018/05/27 00:36:56 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2016 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.55 2018/05/27 00:36:56 christos 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 <sched.h>
47#include <signal.h>
48#include <stdint.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <strings.h>
52#include <time.h>
53#include <unistd.h>
54
55#include <atf-c.h>
56
57#include "h_macros.h"
58
59#include "t_ptrace_wait.h"
60#include "msg.h"
61
62#define PARENT_TO_CHILD(info, fds, msg) \
63    SYSCALL_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, sizeof(msg)) == 0)
64
65#define CHILD_FROM_PARENT(info, fds, msg) \
66    FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
67
68#define CHILD_TO_PARENT(info, fds, msg) \
69    FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, sizeof(msg)) == 0)
70
71#define PARENT_FROM_CHILD(info, fds, msg) \
72    SYSCALL_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
73
74#define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \
75    strerror(errno))
76#define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \
77    "%d(%s) != %d", res, strerror(res), exp)
78
79static int debug = 0;
80
81#define DPRINTF(a, ...)	do  \
82	if (debug) printf(a,  ##__VA_ARGS__); \
83    while (/*CONSTCOND*/0)
84
85/// ----------------------------------------------------------------------------
86
87static void
88traceme_raise(int sigval)
89{
90	const int exitval = 5;
91	pid_t child, wpid;
92#if defined(TWAIT_HAVE_STATUS)
93	int status;
94#endif
95
96	struct ptrace_siginfo info;
97	memset(&info, 0, sizeof(info));
98
99	DPRINTF("Before forking process PID=%d\n", getpid());
100	SYSCALL_REQUIRE((child = fork()) != -1);
101	if (child == 0) {
102		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
103		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
104
105		DPRINTF("Before raising %s from child\n", strsignal(sigval));
106		FORKEE_ASSERT(raise(sigval) == 0);
107
108		switch (sigval) {
109		case SIGKILL:
110			/* NOTREACHED */
111			FORKEE_ASSERTX(0 && "This shall not be reached");
112		default:
113			DPRINTF("Before exiting of the child process\n");
114			_exit(exitval);
115		}
116	}
117	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
118
119	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
120	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
121
122	switch (sigval) {
123	case SIGKILL:
124		validate_status_signaled(status, sigval, 0);
125		break;
126	default:
127		validate_status_stopped(status, sigval);
128
129		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
130		        "child\n");
131		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
132		                       sizeof(info)) != -1);
133
134		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
135		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
136		        "si_errno=%#x\n",
137		        info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
138		        info.psi_siginfo.si_errno);
139
140		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
141		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
142
143		DPRINTF("Before resuming the child process where it left off "
144		    "and without signal to be sent\n");
145		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
146
147		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
148		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
149		                      child);
150		break;
151	}
152
153	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
154	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
155}
156
157#define TRACEME_RAISE(test, sig)						\
158ATF_TC(test);									\
159ATF_TC_HEAD(test, tc)								\
160{										\
161	atf_tc_set_md_var(tc, "descr",						\
162	    "Verify " #sig " followed by _exit(2) in a child");			\
163}										\
164										\
165ATF_TC_BODY(test, tc)								\
166{										\
167										\
168	traceme_raise(sig);							\
169}
170
171TRACEME_RAISE(traceme_raise1, SIGKILL) /* non-maskable */
172TRACEME_RAISE(traceme_raise2, SIGSTOP) /* non-maskable */
173TRACEME_RAISE(traceme_raise3, SIGABRT) /* regular abort trap */
174TRACEME_RAISE(traceme_raise4, SIGHUP)  /* hangup */
175TRACEME_RAISE(traceme_raise5, SIGCONT) /* continued? */
176
177/// ----------------------------------------------------------------------------
178
179static void
180traceme_sendsignal_handle(int sigsent, void (*sah)(int a), int *traceme_caught)
181{
182	const int exitval = 5;
183	const int sigval = SIGSTOP;
184	pid_t child, wpid;
185	struct sigaction sa;
186#if defined(TWAIT_HAVE_STATUS)
187	int status;
188#endif
189
190	struct ptrace_siginfo info;
191	memset(&info, 0, sizeof(info));
192
193	DPRINTF("Before forking process PID=%d\n", getpid());
194	SYSCALL_REQUIRE((child = fork()) != -1);
195	if (child == 0) {
196		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
197		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
198
199		sa.sa_handler = sah;
200		sa.sa_flags = SA_SIGINFO;
201		sigemptyset(&sa.sa_mask);
202
203		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
204
205		DPRINTF("Before raising %s from child\n", strsignal(sigval));
206		FORKEE_ASSERT(raise(sigval) == 0);
207
208		FORKEE_ASSERT_EQ(*traceme_caught, 1);
209
210		DPRINTF("Before exiting of the child process\n");
211		_exit(exitval);
212	}
213	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
214
215	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
216	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
217
218	validate_status_stopped(status, sigval);
219
220	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
221	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
222	                != -1);
223
224	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
225	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
226	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
227	    info.psi_siginfo.si_errno);
228
229	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
230	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
231
232	DPRINTF("Before resuming the child process where it left off and with "
233	    "signal %s to be sent\n", strsignal(sigsent));
234	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
235
236	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
237	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
238
239	validate_status_exited(status, exitval);
240
241	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
242	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
243}
244
245#define TRACEME_SENDSIGNAL_HANDLE(test, sig)					\
246ATF_TC(test);									\
247ATF_TC_HEAD(test, tc)								\
248{										\
249	atf_tc_set_md_var(tc, "descr",						\
250	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
251	    "handled correctly and caught by a signal handler");		\
252}										\
253										\
254static int test##_caught = 0;							\
255										\
256static void									\
257test##_sighandler(int arg)							\
258{										\
259	FORKEE_ASSERT_EQ(arg, sig);						\
260										\
261	++ test##_caught;							\
262}										\
263										\
264ATF_TC_BODY(test, tc)								\
265{										\
266										\
267	traceme_sendsignal_handle(sig, test##_sighandler, & test##_caught);	\
268}
269
270// A signal handler for SIGKILL and SIGSTOP cannot be registered.
271TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle1, SIGABRT) /* abort trap */
272TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle2, SIGHUP)  /* hangup */
273TRACEME_SENDSIGNAL_HANDLE(traceme_sendsignal_handle3, SIGCONT) /* continued? */
274
275/// ----------------------------------------------------------------------------
276
277static void
278traceme_sendsignal_masked(int sigsent)
279{
280	const int exitval = 5;
281	const int sigval = SIGSTOP;
282	pid_t child, wpid;
283	sigset_t set;
284#if defined(TWAIT_HAVE_STATUS)
285	int status;
286#endif
287
288	struct ptrace_siginfo info;
289	memset(&info, 0, sizeof(info));
290
291	DPRINTF("Before forking process PID=%d\n", getpid());
292	SYSCALL_REQUIRE((child = fork()) != -1);
293	if (child == 0) {
294		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
295		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
296
297		sigemptyset(&set);
298		sigaddset(&set, sigsent);
299		FORKEE_ASSERT(sigprocmask(SIG_BLOCK, &set, NULL) != -1);
300
301		DPRINTF("Before raising %s from child\n", strsignal(sigval));
302		FORKEE_ASSERT(raise(sigval) == 0);
303
304		_exit(exitval);
305	}
306	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
307
308	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
309	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
310
311	validate_status_stopped(status, sigval);
312
313	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
314	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
315	                != -1);
316
317	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
318	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
319	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
320	    info.psi_siginfo.si_errno);
321
322	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
323	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
324
325	DPRINTF("Before resuming the child process where it left off and with "
326	    "signal %s to be sent\n", strsignal(sigsent));
327	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
328
329	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
330	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
331
332	validate_status_exited(status, exitval);
333
334	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
335	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
336}
337
338#define TRACEME_SENDSIGNAL_MASKED(test, sig)					\
339ATF_TC(test);									\
340ATF_TC_HEAD(test, tc)								\
341{										\
342	atf_tc_set_md_var(tc, "descr",						\
343	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
344	    "handled correctly and the signal is masked by SIG_BLOCK");		\
345}										\
346										\
347ATF_TC_BODY(test, tc)								\
348{										\
349										\
350	traceme_sendsignal_masked(sig);						\
351}
352
353// A signal handler for SIGKILL and SIGSTOP cannot be masked.
354TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked1, SIGABRT) /* abort trap */
355TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked2, SIGHUP)  /* hangup */
356TRACEME_SENDSIGNAL_MASKED(traceme_sendsignal_masked3, SIGCONT) /* continued? */
357
358/// ----------------------------------------------------------------------------
359
360static void
361traceme_sendsignal_ignored(int sigsent)
362{
363	const int exitval = 5;
364	const int sigval = SIGSTOP;
365	pid_t child, wpid;
366	struct sigaction sa;
367#if defined(TWAIT_HAVE_STATUS)
368	int status;
369#endif
370
371	struct ptrace_siginfo info;
372	memset(&info, 0, sizeof(info));
373
374	DPRINTF("Before forking process PID=%d\n", getpid());
375	SYSCALL_REQUIRE((child = fork()) != -1);
376	if (child == 0) {
377		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
378		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
379
380		memset(&sa, 0, sizeof(sa));
381		sa.sa_handler = SIG_IGN;
382		sigemptyset(&sa.sa_mask);
383		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
384
385		DPRINTF("Before raising %s from child\n", strsignal(sigval));
386		FORKEE_ASSERT(raise(sigval) == 0);
387
388		_exit(exitval);
389	}
390	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
391
392	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
393	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
394
395	validate_status_stopped(status, sigval);
396
397	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
398	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
399	                != -1);
400
401	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
402	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
403	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
404	    info.psi_siginfo.si_errno);
405
406	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
407	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
408
409	DPRINTF("Before resuming the child process where it left off and with "
410	    "signal %s to be sent\n", strsignal(sigsent));
411	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
412
413	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
414	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
415
416	validate_status_exited(status, exitval);
417
418	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
419	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
420}
421
422#define TRACEME_SENDSIGNAL_IGNORED(test, sig)					\
423ATF_TC(test);									\
424ATF_TC_HEAD(test, tc)								\
425{										\
426	atf_tc_set_md_var(tc, "descr",						\
427	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
428	    "handled correctly and the signal is masked by SIG_IGN");		\
429}										\
430										\
431ATF_TC_BODY(test, tc)								\
432{										\
433										\
434	traceme_sendsignal_ignored(sig);					\
435}
436
437// A signal handler for SIGKILL and SIGSTOP cannot be ignored.
438TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored1, SIGABRT) /* abort trap */
439TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored2, SIGHUP)  /* hangup */
440TRACEME_SENDSIGNAL_IGNORED(traceme_sendsignal_ignored3, SIGCONT) /* continued? */
441
442/// ----------------------------------------------------------------------------
443
444static void
445traceme_sendsignal_simple(int sigsent)
446{
447	const int sigval = SIGSTOP;
448	int exitval = 0;
449	pid_t child, wpid;
450#if defined(TWAIT_HAVE_STATUS)
451	int status;
452	int expect_core = (sigsent == SIGABRT) ? 1 : 0;
453#endif
454
455	struct ptrace_siginfo info;
456	memset(&info, 0, sizeof(info));
457
458	DPRINTF("Before forking process PID=%d\n", getpid());
459	SYSCALL_REQUIRE((child = fork()) != -1);
460	if (child == 0) {
461		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
462		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
463
464		DPRINTF("Before raising %s from child\n", strsignal(sigval));
465		FORKEE_ASSERT(raise(sigval) == 0);
466
467		switch (sigsent) {
468		case SIGCONT:
469		case SIGSTOP:
470			_exit(exitval);
471		default:
472			/* NOTREACHED */
473			FORKEE_ASSERTX(0 && "This shall not be reached");
474		}
475	}
476	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
477
478	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
479	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
480
481	validate_status_stopped(status, sigval);
482
483	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
484	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info))
485	                != -1);
486
487	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
488	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
489	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
490	    info.psi_siginfo.si_errno);
491
492	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
493	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
494
495	DPRINTF("Before resuming the child process where it left off and with "
496	    "signal %s to be sent\n", strsignal(sigsent));
497	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
498
499	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
500	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
501
502	switch (sigsent) {
503	case SIGSTOP:
504		validate_status_stopped(status, sigsent);
505		DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
506		        "child\n");
507		SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
508		                       sizeof(info)) != -1);
509
510		DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
511		DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
512		        "si_errno=%#x\n",
513			info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
514		        info.psi_siginfo.si_errno);
515
516		ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
517		ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
518
519		DPRINTF("Before resuming the child process where it left off "
520		        "and with signal %s to be sent\n", strsignal(sigsent));
521		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
522
523		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
524		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
525		                      child);
526		/* FALLTHROUGH */
527	case SIGCONT:
528		validate_status_exited(status, exitval);
529		break;
530	default:
531		validate_status_signaled(status, sigsent, expect_core);
532		break;
533	}
534
535	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
536	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
537}
538
539#define TRACEME_SENDSIGNAL_SIMPLE(test, sig)					\
540ATF_TC(test);									\
541ATF_TC_HEAD(test, tc)								\
542{										\
543	atf_tc_set_md_var(tc, "descr",						\
544	    "Verify that a signal " #sig " emitted by a tracer to a child is "	\
545	    "handled correctly in a child without a signal handler");		\
546}										\
547										\
548ATF_TC_BODY(test, tc)								\
549{										\
550										\
551	traceme_sendsignal_simple(sig);						\
552}
553
554TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple1, SIGKILL) /* non-maskable */
555TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple2, SIGSTOP) /* non-maskable */
556TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple3, SIGABRT) /* abort trap */
557TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple4, SIGHUP)  /* hangup */
558TRACEME_SENDSIGNAL_SIMPLE(traceme_sendsignal_simple5, SIGCONT) /* continued? */
559
560/// ----------------------------------------------------------------------------
561
562ATF_TC(traceme_pid1_parent);
563ATF_TC_HEAD(traceme_pid1_parent, tc)
564{
565	atf_tc_set_md_var(tc, "descr",
566	    "Verify that PT_TRACE_ME is not allowed when our parent is PID1");
567}
568
569ATF_TC_BODY(traceme_pid1_parent, tc)
570{
571	struct msg_fds parent_child;
572	int exitval_child1 = 1, exitval_child2 = 2;
573	pid_t child1, child2, wpid;
574	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
575#if defined(TWAIT_HAVE_STATUS)
576	int status;
577#endif
578
579	SYSCALL_REQUIRE(msg_open(&parent_child) == 0);
580
581	DPRINTF("Before forking process PID=%d\n", getpid());
582	SYSCALL_REQUIRE((child1 = fork()) != -1);
583	if (child1 == 0) {
584		DPRINTF("Before forking process PID=%d\n", getpid());
585		SYSCALL_REQUIRE((child2 = fork()) != -1);
586		if (child2 != 0) {
587			DPRINTF("Parent process PID=%d, child2's PID=%d\n",
588			        getpid(), child2);
589			_exit(exitval_child1);
590		}
591		CHILD_FROM_PARENT("exit child1", parent_child, msg);
592
593		DPRINTF("Assert that our parent is PID1 (initproc)\n");
594		FORKEE_ASSERT_EQ(getppid(), 1);
595
596		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
597		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) == -1);
598		SYSCALL_REQUIRE_ERRNO(errno, EPERM);
599
600		CHILD_TO_PARENT("child2 exiting", parent_child, msg);
601
602		_exit(exitval_child2);
603	}
604	DPRINTF("Parent process PID=%d, child1's PID=%d\n", getpid(), child1);
605
606	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
607	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child1, &status, WEXITED),
608	                      child1);
609
610	validate_status_exited(status, exitval_child1);
611
612	DPRINTF("Notify that child1 is dead\n");
613	PARENT_TO_CHILD("exit child1", parent_child, msg);
614
615	DPRINTF("Wait for exiting of child2\n");
616	PARENT_FROM_CHILD("child2 exiting", parent_child, msg);
617}
618
619/// ----------------------------------------------------------------------------
620
621static void
622traceme_vfork_raise(int sigval)
623{
624	const int exitval = 5, exitval_watcher = 10;
625	pid_t child, parent, watcher, wpid;
626	int rv;
627#if defined(TWAIT_HAVE_STATUS)
628	int status;
629	int expect_core = (sigval == SIGABRT) ? 1 : 0;
630#endif
631
632	/*
633	 * Spawn a dedicated thread to watch for a stopped child and emit
634	 * the SIGKILL signal to it.
635	 *
636	 * vfork(2) might clobber watcher, this means that it's safer and
637	 * simpler to reparent this process to initproc and forget about it.
638	 */
639	if (sigval == SIGSTOP) {
640		parent = getpid();
641
642		watcher = fork();
643		ATF_REQUIRE(watcher != 1);
644		if (watcher == 0) {
645			/* Double fork(2) trick to reparent to initproc */
646			watcher = fork();
647			FORKEE_ASSERT_NEQ(watcher, -1);
648			if (watcher != 0)
649				_exit(exitval_watcher);
650
651			child = await_stopped_child(parent);
652
653			errno = 0;
654			rv = kill(child, SIGKILL);
655			FORKEE_ASSERT_EQ(rv, 0);
656			FORKEE_ASSERT_EQ(errno, 0);
657
658			/* This exit value will be collected by initproc */
659			_exit(0);
660		}
661		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
662		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(watcher, &status, 0),
663		                      watcher);
664
665		validate_status_exited(status, exitval_watcher);
666
667		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
668		TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(watcher,
669		                                                   &status, 0));
670	}
671
672	DPRINTF("Before forking process PID=%d\n", getpid());
673	SYSCALL_REQUIRE((child = vfork()) != -1);
674	if (child == 0) {
675		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
676		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
677
678		DPRINTF("Before raising %s from child\n", strsignal(sigval));
679		FORKEE_ASSERT(raise(sigval) == 0);
680
681		switch (sigval) {
682		case SIGSTOP:
683		case SIGKILL:
684		case SIGABRT:
685		case SIGHUP:
686			/* NOTREACHED */
687			FORKEE_ASSERTX(0 && "This shall not be reached");
688		default:
689			DPRINTF("Before exiting of the child process\n");
690			_exit(exitval);
691		}
692	}
693	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
694
695	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
696	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
697
698	switch (sigval) {
699	case SIGKILL:
700	case SIGABRT:
701	case SIGHUP:
702		validate_status_signaled(status, sigval, expect_core);
703		break;
704	case SIGSTOP:
705		validate_status_signaled(status, SIGKILL, 0);
706		break;
707	case SIGCONT:
708	case SIGTSTP:
709	case SIGTTIN:
710	case SIGTTOU:
711		validate_status_exited(status, exitval);
712		break;
713	default:
714		/* NOTREACHED */
715		ATF_REQUIRE(0 && "NOT IMPLEMENTED");
716		break;
717	}
718
719	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
720	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
721}
722
723#define TRACEME_VFORK_RAISE(test, sig)						\
724ATF_TC(test);									\
725ATF_TC_HEAD(test, tc)								\
726{										\
727	atf_tc_set_md_var(tc, "descr",						\
728	    "Verify PT_TRACE_ME followed by raise of " #sig " in a vfork(2)ed "	\
729	    "child");								\
730}										\
731										\
732ATF_TC_BODY(test, tc)								\
733{										\
734										\
735	traceme_vfork_raise(sig);						\
736}
737
738TRACEME_VFORK_RAISE(traceme_vfork_raise1, SIGKILL) /* non-maskable */
739TRACEME_VFORK_RAISE(traceme_vfork_raise2, SIGSTOP) /* non-maskable */
740TRACEME_VFORK_RAISE(traceme_vfork_raise3, SIGTSTP) /* ignored in vfork(2) */
741TRACEME_VFORK_RAISE(traceme_vfork_raise4, SIGTTIN) /* ignored in vfork(2) */
742TRACEME_VFORK_RAISE(traceme_vfork_raise5, SIGTTOU) /* ignored in vfork(2) */
743TRACEME_VFORK_RAISE(traceme_vfork_raise6, SIGABRT) /* regular abort trap */
744TRACEME_VFORK_RAISE(traceme_vfork_raise7, SIGHUP)  /* hangup */
745TRACEME_VFORK_RAISE(traceme_vfork_raise8, SIGCONT) /* continued? */
746
747/// ----------------------------------------------------------------------------
748
749static void
750traceme_vfork_crash(int sig)
751{
752	pid_t child, wpid;
753#if defined(TWAIT_HAVE_STATUS)
754	int status;
755#endif
756
757	DPRINTF("Before forking process PID=%d\n", getpid());
758	SYSCALL_REQUIRE((child = vfork()) != -1);
759	if (child == 0) {
760		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
761		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
762
763		DPRINTF("Before executing a trap\n");
764		switch (sig) {
765		case SIGTRAP:
766			trigger_trap();
767			break;
768		case SIGSEGV:
769			trigger_segv();
770			break;
771		case SIGILL:
772			trigger_ill();
773			break;
774		case SIGFPE:
775			trigger_fpe();
776			break;
777		case SIGBUS:
778			trigger_bus();
779			break;
780		default:
781			/* NOTREACHED */
782			FORKEE_ASSERTX(0 && "This shall not be reached");
783		}
784
785		/* NOTREACHED */
786		FORKEE_ASSERTX(0 && "This shall not be reached");
787	}
788	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
789
790	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
791	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
792
793	validate_status_signaled(status, sig, 1);
794
795	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
796	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
797}
798
799#define TRACEME_VFORK_CRASH(test, sig)						\
800ATF_TC(test);									\
801ATF_TC_HEAD(test, tc)								\
802{										\
803	atf_tc_set_md_var(tc, "descr",						\
804	    "Verify PT_TRACE_ME followed by a crash signal " #sig " in a "	\
805	    "vfork(2)ed child");						\
806}										\
807										\
808ATF_TC_BODY(test, tc)								\
809{										\
810										\
811	traceme_vfork_crash(sig);						\
812}
813
814TRACEME_VFORK_CRASH(traceme_vfork_crash_trap, SIGTRAP)
815TRACEME_VFORK_CRASH(traceme_vfork_crash_segv, SIGSEGV)
816//TRACEME_VFORK_CRASH(traceme_vfork_crash_ill, SIGILL)
817TRACEME_VFORK_CRASH(traceme_vfork_crash_fpe, SIGFPE)
818TRACEME_VFORK_CRASH(traceme_vfork_crash_bus, SIGBUS)
819
820/// ----------------------------------------------------------------------------
821
822ATF_TC(traceme_vfork_exec);
823ATF_TC_HEAD(traceme_vfork_exec, tc)
824{
825	atf_tc_set_md_var(tc, "descr",
826	    "Verify PT_TRACE_ME followed by exec(3) in a vfork(2)ed child");
827}
828
829ATF_TC_BODY(traceme_vfork_exec, tc)
830{
831	const int sigval = SIGTRAP;
832	pid_t child, wpid;
833#if defined(TWAIT_HAVE_STATUS)
834	int status;
835#endif
836
837	struct ptrace_siginfo info;
838	memset(&info, 0, sizeof(info));
839
840	DPRINTF("Before forking process PID=%d\n", getpid());
841	SYSCALL_REQUIRE((child = vfork()) != -1);
842	if (child == 0) {
843		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
844		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
845
846		DPRINTF("Before calling execve(2) from child\n");
847		execlp("/bin/echo", "/bin/echo", NULL);
848
849		/* NOTREACHED */
850		FORKEE_ASSERTX(0 && "Not reached");
851	}
852	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
853
854	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
855	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
856
857	validate_status_stopped(status, sigval);
858
859	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
860	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
861
862	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
863	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
864	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
865	    info.psi_siginfo.si_errno);
866
867	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
868	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
869
870	DPRINTF("Before resuming the child process where it left off and "
871	    "without signal to be sent\n");
872	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
873
874	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
875	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
876
877	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
878	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
879}
880
881/// ----------------------------------------------------------------------------
882
883#if defined(TWAIT_HAVE_PID)
884static void
885tracer_sees_terminaton_before_the_parent_raw(bool notimeout, bool unrelated)
886{
887	/*
888	 * notimeout - disable timeout in await zombie function
889	 * unrelated - attach from unrelated tracer reparented to initproc
890	 */
891
892	struct msg_fds parent_tracee, parent_tracer;
893	const int exitval_tracee = 5;
894	const int exitval_tracer = 10;
895	pid_t tracee, tracer, wpid;
896	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
897#if defined(TWAIT_HAVE_STATUS)
898	int status;
899#endif
900
901	DPRINTF("Spawn tracee\n");
902	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
903	tracee = atf_utils_fork();
904	if (tracee == 0) {
905		// Wait for parent to let us exit
906		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
907		_exit(exitval_tracee);
908	}
909
910	DPRINTF("Spawn debugger\n");
911	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
912	tracer = atf_utils_fork();
913	if (tracer == 0) {
914		if(unrelated) {
915			/* Fork again and drop parent to reattach to PID 1 */
916			tracer = atf_utils_fork();
917			if (tracer != 0)
918				_exit(exitval_tracer);
919		}
920
921		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
922		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
923
924		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
925		FORKEE_REQUIRE_SUCCESS(
926		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
927
928		forkee_status_stopped(status, SIGSTOP);
929
930		/* Resume tracee with PT_CONTINUE */
931		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
932
933		/* Inform parent that tracer has attached to tracee */
934		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
935
936		/* Wait for parent to tell use that tracee should have exited */
937		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
938
939		/* Wait for tracee and assert that it exited */
940		FORKEE_REQUIRE_SUCCESS(
941		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
942
943		forkee_status_exited(status, exitval_tracee);
944		DPRINTF("Tracee %d exited with %d\n", tracee, exitval_tracee);
945
946		DPRINTF("Before exiting of the tracer process\n");
947		_exit(unrelated ? 0 /* collect by initproc */ : exitval_tracer);
948	}
949
950	if (unrelated) {
951		DPRINTF("Wait for the tracer process (direct child) to exit "
952		    "calling %s()\n", TWAIT_FNAME);
953		TWAIT_REQUIRE_SUCCESS(
954		    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
955
956		validate_status_exited(status, exitval_tracer);
957
958		DPRINTF("Wait for the non-exited tracee process with %s()\n",
959		    TWAIT_FNAME);
960		TWAIT_REQUIRE_SUCCESS(
961		    wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
962	}
963
964	DPRINTF("Wait for the tracer to attach to the tracee\n");
965	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
966
967	DPRINTF("Resume the tracee and let it exit\n");
968	PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
969
970	DPRINTF("Detect that tracee is zombie\n");
971	if (notimeout)
972		await_zombie_raw(tracee, 0);
973	else
974		await_zombie(tracee);
975
976	DPRINTF("Assert that there is no status about tracee %d - "
977	    "Tracer must detect zombie first - calling %s()\n", tracee,
978	    TWAIT_FNAME);
979	TWAIT_REQUIRE_SUCCESS(
980	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
981
982	if (unrelated) {
983		DPRINTF("Resume the tracer and let it detect exited tracee\n");
984		PARENT_TO_CHILD("Message 2", parent_tracer, msg);
985	} else {
986		DPRINTF("Tell the tracer child should have exited\n");
987		PARENT_TO_CHILD("wait for tracee exit", parent_tracer,  msg);
988		DPRINTF("Wait for tracer to finish its job and exit - calling "
989		        "%s()\n", TWAIT_FNAME);
990
991		DPRINTF("Wait from tracer child to complete waiting for "
992		        "tracee\n");
993		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
994		    tracer);
995
996		validate_status_exited(status, exitval_tracer);
997	}
998
999	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
1000	    TWAIT_FNAME);
1001	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1002
1003	validate_status_exited(status, exitval_tracee);
1004
1005	msg_close(&parent_tracer);
1006	msg_close(&parent_tracee);
1007}
1008
1009ATF_TC(tracer_sees_terminaton_before_the_parent);
1010ATF_TC_HEAD(tracer_sees_terminaton_before_the_parent, tc)
1011{
1012	atf_tc_set_md_var(tc, "descr",
1013	    "Assert that tracer sees process termination before the parent");
1014}
1015
1016ATF_TC_BODY(tracer_sees_terminaton_before_the_parent, tc)
1017{
1018
1019	tracer_sees_terminaton_before_the_parent_raw(false, false);
1020}
1021
1022ATF_TC(tracer_sysctl_lookup_without_duplicates);
1023ATF_TC_HEAD(tracer_sysctl_lookup_without_duplicates, tc)
1024{
1025	atf_tc_set_md_var(tc, "descr",
1026	    "Assert that await_zombie() in attach1 always finds a single "
1027	    "process and no other error is reported");
1028}
1029
1030ATF_TC_BODY(tracer_sysctl_lookup_without_duplicates, tc)
1031{
1032	time_t start, end;
1033	double diff;
1034	unsigned long N = 0;
1035
1036	/*
1037	 * Reuse this test with tracer_sees_terminaton_before_the_parent_raw().
1038	 * This test body isn't specific to this race, however it's just good
1039	 * enough for this purposes, no need to invent a dedicated code flow.
1040	 */
1041
1042	start = time(NULL);
1043	while (true) {
1044		DPRINTF("Step: %lu\n", N);
1045		tracer_sees_terminaton_before_the_parent_raw(true, false);
1046		end = time(NULL);
1047		diff = difftime(end, start);
1048		if (diff >= 5.0)
1049			break;
1050		++N;
1051	}
1052	DPRINTF("Iterations: %lu\n", N);
1053}
1054
1055ATF_TC(unrelated_tracer_sees_terminaton_before_the_parent);
1056ATF_TC_HEAD(unrelated_tracer_sees_terminaton_before_the_parent, tc)
1057{
1058	atf_tc_set_md_var(tc, "descr",
1059	    "Assert that tracer sees process termination before the parent");
1060}
1061
1062ATF_TC_BODY(unrelated_tracer_sees_terminaton_before_the_parent, tc)
1063{
1064
1065	tracer_sees_terminaton_before_the_parent_raw(false, true);
1066}
1067#endif
1068
1069/// ----------------------------------------------------------------------------
1070
1071ATF_TC(parent_attach_to_its_child);
1072ATF_TC_HEAD(parent_attach_to_its_child, tc)
1073{
1074	atf_tc_set_md_var(tc, "descr",
1075	    "Assert that tracer parent can PT_ATTACH to its child");
1076}
1077
1078ATF_TC_BODY(parent_attach_to_its_child, tc)
1079{
1080	struct msg_fds parent_tracee;
1081	const int exitval_tracee = 5;
1082	pid_t tracee, wpid;
1083	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1084#if defined(TWAIT_HAVE_STATUS)
1085	int status;
1086#endif
1087
1088	DPRINTF("Spawn tracee\n");
1089	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1090	tracee = atf_utils_fork();
1091	if (tracee == 0) {
1092		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
1093		DPRINTF("Parent should now attach to tracee\n");
1094
1095		CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
1096		/* Wait for message from the parent */
1097		_exit(exitval_tracee);
1098	}
1099	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
1100
1101	DPRINTF("Before calling PT_ATTACH for tracee %d\n", tracee);
1102	SYSCALL_REQUIRE(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
1103
1104	DPRINTF("Wait for the stopped tracee process with %s()\n",
1105	    TWAIT_FNAME);
1106	TWAIT_REQUIRE_SUCCESS(
1107	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1108
1109	validate_status_stopped(status, SIGSTOP);
1110
1111	DPRINTF("Resume tracee with PT_CONTINUE\n");
1112	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
1113
1114	DPRINTF("Let the tracee exit now\n");
1115	PARENT_TO_CHILD("Message 2", parent_tracee, msg);
1116
1117	DPRINTF("Wait for tracee to exit with %s()\n", TWAIT_FNAME);
1118	TWAIT_REQUIRE_SUCCESS(
1119	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1120
1121	validate_status_exited(status, exitval_tracee);
1122
1123	DPRINTF("Before calling %s() for tracee\n", TWAIT_FNAME);
1124	TWAIT_REQUIRE_FAILURE(ECHILD,
1125	    wpid = TWAIT_GENERIC(tracee, &status, 0));
1126
1127	msg_close(&parent_tracee);
1128}
1129
1130/// ----------------------------------------------------------------------------
1131
1132ATF_TC(child_attach_to_its_parent);
1133ATF_TC_HEAD(child_attach_to_its_parent, tc)
1134{
1135	atf_tc_set_md_var(tc, "descr",
1136	    "Assert that tracer child can PT_ATTACH to its parent");
1137}
1138
1139ATF_TC_BODY(child_attach_to_its_parent, tc)
1140{
1141	struct msg_fds parent_tracee;
1142	const int exitval_tracer = 5;
1143	pid_t tracer, wpid;
1144	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1145#if defined(TWAIT_HAVE_STATUS)
1146	int status;
1147#endif
1148
1149	DPRINTF("Spawn tracer\n");
1150	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1151	tracer = atf_utils_fork();
1152	if (tracer == 0) {
1153
1154		/* Wait for message from the parent */
1155		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
1156
1157		DPRINTF("Attach to parent PID %d with PT_ATTACH from child\n",
1158		    getppid());
1159		FORKEE_ASSERT(ptrace(PT_ATTACH, getppid(), NULL, 0) != -1);
1160
1161		DPRINTF("Wait for the stopped parent process with %s()\n",
1162		    TWAIT_FNAME);
1163		FORKEE_REQUIRE_SUCCESS(
1164		    wpid = TWAIT_GENERIC(getppid(), &status, 0), getppid());
1165
1166		forkee_status_stopped(status, SIGSTOP);
1167
1168		DPRINTF("Resume parent with PT_DETACH\n");
1169		FORKEE_ASSERT(ptrace(PT_DETACH, getppid(), (void *)1, 0)
1170		    != -1);
1171
1172		/* Tell parent we are ready */
1173		CHILD_TO_PARENT("Message 1", parent_tracee, msg);
1174
1175		_exit(exitval_tracer);
1176	}
1177
1178	DPRINTF("Wait for the tracer to become ready\n");
1179	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
1180	DPRINTF("Allow the tracer to exit now\n");
1181	PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
1182
1183	DPRINTF("Wait for tracer to exit with %s()\n", TWAIT_FNAME);
1184	TWAIT_REQUIRE_SUCCESS(
1185	    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
1186
1187	validate_status_exited(status, exitval_tracer);
1188
1189	DPRINTF("Before calling %s() for tracer\n", TWAIT_FNAME);
1190	TWAIT_REQUIRE_FAILURE(ECHILD,
1191	    wpid = TWAIT_GENERIC(tracer, &status, 0));
1192
1193	msg_close(&parent_tracee);
1194}
1195
1196/// ----------------------------------------------------------------------------
1197
1198#if defined(TWAIT_HAVE_PID)
1199
1200enum tracee_sees_its_original_parent_type {
1201	TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID,
1202	TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2,
1203	TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS
1204};
1205
1206static void
1207tracee_sees_its_original_parent(enum tracee_sees_its_original_parent_type type)
1208{
1209	struct msg_fds parent_tracer, parent_tracee;
1210	const int exitval_tracee = 5;
1211	const int exitval_tracer = 10;
1212	pid_t parent, tracee, tracer, wpid;
1213	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
1214#if defined(TWAIT_HAVE_STATUS)
1215	int status;
1216#endif
1217	/* sysctl(3) - kinfo_proc2 */
1218	int name[CTL_MAXNAME];
1219	struct kinfo_proc2 kp;
1220	size_t len = sizeof(kp);
1221	unsigned int namelen;
1222
1223	/* procfs - status  */
1224	FILE *fp;
1225	struct stat st;
1226	const char *fname = "/proc/curproc/status";
1227	char s_executable[MAXPATHLEN];
1228	int s_pid, s_ppid;
1229	int rv;
1230
1231	if (type == TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS) {
1232		SYSCALL_REQUIRE((rv = stat(fname, &st)) == 0 ||
1233		                (errno == ENOENT));
1234		if (rv != 0) {
1235			atf_tc_skip("/proc/curproc/status not found");
1236		}
1237	}
1238
1239	DPRINTF("Spawn tracee\n");
1240	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
1241	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
1242	tracee = atf_utils_fork();
1243	if (tracee == 0) {
1244		parent = getppid();
1245
1246		/* Emit message to the parent */
1247		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
1248		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
1249
1250		switch (type) {
1251		case TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID:
1252			FORKEE_ASSERT_EQ(parent, getppid());
1253			break;
1254		case TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2:
1255			namelen = 0;
1256			name[namelen++] = CTL_KERN;
1257			name[namelen++] = KERN_PROC2;
1258			name[namelen++] = KERN_PROC_PID;
1259			name[namelen++] = getpid();
1260			name[namelen++] = len;
1261			name[namelen++] = 1;
1262
1263			FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL,
1264			                        0),
1265			                 0);
1266			FORKEE_ASSERT_EQ(parent, kp.p_ppid);
1267			break;
1268		case TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS:
1269			/*
1270			 * Format:
1271			 *  EXECUTABLE PID PPID ...
1272			 */
1273			FORKEE_ASSERT((fp = fopen(fname, "r")) != NULL);
1274			fscanf(fp, "%s %d %d", s_executable, &s_pid, &s_ppid);
1275			FORKEE_ASSERT_EQ(fclose(fp), 0);
1276			FORKEE_ASSERT_EQ(parent, s_ppid);
1277			break;
1278		}
1279
1280		_exit(exitval_tracee);
1281	}
1282	DPRINTF("Wait for child to record its parent identifier (pid)\n");
1283	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
1284
1285	DPRINTF("Spawn debugger\n");
1286	tracer = atf_utils_fork();
1287	if (tracer == 0) {
1288		/* No IPC to communicate with the child */
1289		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
1290		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
1291
1292		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
1293		FORKEE_REQUIRE_SUCCESS(
1294		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1295
1296		forkee_status_stopped(status, SIGSTOP);
1297
1298		/* Resume tracee with PT_CONTINUE */
1299		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
1300
1301		/* Inform parent that tracer has attached to tracee */
1302		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
1303
1304		/* Wait for parent to tell use that tracee should have exited */
1305		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
1306
1307		/* Wait for tracee and assert that it exited */
1308		FORKEE_REQUIRE_SUCCESS(
1309		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
1310
1311		forkee_status_exited(status, exitval_tracee);
1312
1313		DPRINTF("Before exiting of the tracer process\n");
1314		_exit(exitval_tracer);
1315	}
1316
1317	DPRINTF("Wait for the tracer to attach to the tracee\n");
1318	PARENT_FROM_CHILD("tracer ready",  parent_tracer, msg);
1319
1320	DPRINTF("Resume the tracee and let it exit\n");
1321	PARENT_TO_CHILD("exit tracee",  parent_tracee, msg);
1322
1323	DPRINTF("Detect that tracee is zombie\n");
1324	await_zombie(tracee);
1325
1326	DPRINTF("Assert that there is no status about tracee - "
1327	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
1328	TWAIT_REQUIRE_SUCCESS(
1329	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
1330
1331	DPRINTF("Tell the tracer child should have exited\n");
1332	PARENT_TO_CHILD("wait for tracee exit",  parent_tracer, msg);
1333
1334	DPRINTF("Wait from tracer child to complete waiting for tracee\n");
1335	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
1336	    tracer);
1337
1338	validate_status_exited(status, exitval_tracer);
1339
1340	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
1341	    TWAIT_FNAME);
1342	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
1343	    tracee);
1344
1345	validate_status_exited(status, exitval_tracee);
1346
1347	msg_close(&parent_tracer);
1348	msg_close(&parent_tracee);
1349}
1350
1351#define TRACEE_SEES_ITS_ORIGINAL_PARENT(test, type, descr)			\
1352ATF_TC(test);									\
1353ATF_TC_HEAD(test, tc)								\
1354{										\
1355	atf_tc_set_md_var(tc, "descr",						\
1356	    "Assert that tracee sees its original parent when being traced "	\
1357	    "(check " descr ")");						\
1358}										\
1359										\
1360ATF_TC_BODY(test, tc)								\
1361{										\
1362										\
1363	tracee_sees_its_original_parent(type);					\
1364}
1365
1366TRACEE_SEES_ITS_ORIGINAL_PARENT(
1367	tracee_sees_its_original_parent_getppid,
1368	TRACEE_SEES_ITS_ORIGINAL_PARENT_GETPPID,
1369	"getppid(2)");
1370TRACEE_SEES_ITS_ORIGINAL_PARENT(
1371	tracee_sees_its_original_parent_sysctl_kinfo_proc2,
1372	TRACEE_SEES_ITS_ORIGINAL_PARENT_SYSCTL_KINFO_PROC2,
1373	"sysctl(3) and kinfo_proc2");
1374TRACEE_SEES_ITS_ORIGINAL_PARENT(
1375	tracee_sees_its_original_parent_procfs_status,
1376	TRACEE_SEES_ITS_ORIGINAL_PARENT_PROCFS_STATUS,
1377	"the status file in procfs");
1378#endif
1379
1380/// ----------------------------------------------------------------------------
1381
1382static void
1383eventmask_preserved(int event)
1384{
1385	const int exitval = 5;
1386	const int sigval = SIGSTOP;
1387	pid_t child, wpid;
1388#if defined(TWAIT_HAVE_STATUS)
1389	int status;
1390#endif
1391	ptrace_event_t set_event, get_event;
1392	const int len = sizeof(ptrace_event_t);
1393
1394	DPRINTF("Before forking process PID=%d\n", getpid());
1395	SYSCALL_REQUIRE((child = fork()) != -1);
1396	if (child == 0) {
1397		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1398		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1399
1400		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1401		FORKEE_ASSERT(raise(sigval) == 0);
1402
1403		DPRINTF("Before exiting of the child process\n");
1404		_exit(exitval);
1405	}
1406	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1407
1408	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1409	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1410
1411	validate_status_stopped(status, sigval);
1412
1413	set_event.pe_set_event = event;
1414	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1415	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1416	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1417
1418	DPRINTF("Before resuming the child process where it left off and "
1419	    "without signal to be sent\n");
1420	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1421
1422	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1423	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1424
1425	validate_status_exited(status, exitval);
1426
1427	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1428	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1429}
1430
1431#define EVENTMASK_PRESERVED(test, event)					\
1432ATF_TC(test);									\
1433ATF_TC_HEAD(test, tc)								\
1434{										\
1435	atf_tc_set_md_var(tc, "descr",						\
1436	    "Verify that eventmask " #event " is preserved");			\
1437}										\
1438										\
1439ATF_TC_BODY(test, tc)								\
1440{										\
1441										\
1442	eventmask_preserved(event);						\
1443}
1444
1445EVENTMASK_PRESERVED(eventmask_preserved_empty, 0)
1446EVENTMASK_PRESERVED(eventmask_preserved_fork, PTRACE_FORK)
1447EVENTMASK_PRESERVED(eventmask_preserved_vfork, PTRACE_VFORK)
1448EVENTMASK_PRESERVED(eventmask_preserved_vfork_done, PTRACE_VFORK_DONE)
1449EVENTMASK_PRESERVED(eventmask_preserved_lwp_create, PTRACE_LWP_CREATE)
1450EVENTMASK_PRESERVED(eventmask_preserved_lwp_exit, PTRACE_LWP_EXIT)
1451
1452/// ----------------------------------------------------------------------------
1453
1454static void
1455fork_body(pid_t (*fn)(void), bool trackfork, bool trackvfork,
1456          bool trackvforkdone, bool detachchild, bool detachparent)
1457{
1458	const int exitval = 5;
1459	const int exitval2 = 15;
1460	const int sigval = SIGSTOP;
1461	pid_t child, child2 = 0, wpid;
1462#if defined(TWAIT_HAVE_STATUS)
1463	int status;
1464#endif
1465	ptrace_state_t state;
1466	const int slen = sizeof(state);
1467	ptrace_event_t event;
1468	const int elen = sizeof(event);
1469
1470	DPRINTF("Before forking process PID=%d\n", getpid());
1471	SYSCALL_REQUIRE((child = fork()) != -1);
1472	if (child == 0) {
1473		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1474		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1475
1476		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1477		FORKEE_ASSERT(raise(sigval) == 0);
1478
1479		FORKEE_ASSERT((child2 = (fn)()) != -1);
1480
1481		if (child2 == 0)
1482			_exit(exitval2);
1483
1484		FORKEE_REQUIRE_SUCCESS
1485		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
1486
1487		forkee_status_exited(status, exitval2);
1488
1489		DPRINTF("Before exiting of the child process\n");
1490		_exit(exitval);
1491	}
1492	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1493
1494	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1495	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1496
1497	validate_status_stopped(status, sigval);
1498
1499	DPRINTF("Set 0%s%s%s in EVENT_MASK for the child %d\n",
1500	        trackfork ? "|PTRACE_FORK" : "",
1501	        trackvfork ? "|PTRACE_VFORK" : "",
1502	        trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
1503	event.pe_set_event = 0;
1504	if (trackfork)
1505		event.pe_set_event |= PTRACE_FORK;
1506	if (trackvfork)
1507		event.pe_set_event |= PTRACE_VFORK;
1508	if (trackvforkdone)
1509		event.pe_set_event |= PTRACE_VFORK_DONE;
1510	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
1511
1512	DPRINTF("Before resuming the child process where it left off and "
1513	    "without signal to be sent\n");
1514	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1515
1516#if defined(TWAIT_HAVE_PID)
1517	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
1518		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
1519		        child);
1520		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1521		                      child);
1522
1523		validate_status_stopped(status, SIGTRAP);
1524
1525		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
1526		                       slen) != -1);
1527		if (trackfork && fn == fork) {
1528			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
1529			       PTRACE_FORK);
1530		}
1531		if (trackvfork && fn == vfork) {
1532			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
1533			       PTRACE_VFORK);
1534		}
1535
1536		child2 = state.pe_other_pid;
1537		DPRINTF("Reported ptrace event with forkee %d\n", child2);
1538
1539		DPRINTF("Before calling %s() for the forkee %d of the child "
1540		        "%d\n", TWAIT_FNAME, child2, child);
1541		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
1542		    child2);
1543
1544		validate_status_stopped(status, SIGTRAP);
1545
1546		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state,
1547		                       slen) != -1);
1548		if (trackfork && fn == fork) {
1549			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
1550			       PTRACE_FORK);
1551		}
1552		if (trackvfork && fn == vfork) {
1553			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
1554			       PTRACE_VFORK);
1555		}
1556
1557		ATF_REQUIRE_EQ(state.pe_other_pid, child);
1558
1559		DPRINTF("Before resuming the forkee process where it left off "
1560		    "and without signal to be sent\n");
1561		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0)
1562		                != -1);
1563
1564		DPRINTF("Before resuming the child process where it left off "
1565		        "and without signal to be sent\n");
1566		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1567	}
1568#endif
1569
1570	if (trackvforkdone && fn == vfork) {
1571		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
1572		        child);
1573		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1574		                      child);
1575
1576		validate_status_stopped(status, SIGTRAP);
1577
1578		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
1579		                       slen) != -1);
1580		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
1581
1582		child2 = state.pe_other_pid;
1583		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
1584		        child2);
1585
1586		DPRINTF("Before resuming the child process where it left off "
1587		        "and without signal to be sent\n");
1588		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1589	}
1590
1591#if defined(TWAIT_HAVE_PID)
1592	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
1593		DPRINTF("Before calling %s() for the forkee - expected exited"
1594		        "\n", TWAIT_FNAME);
1595		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
1596		    child2);
1597
1598		validate_status_exited(status, exitval2);
1599
1600		DPRINTF("Before calling %s() for the forkee - expected no "
1601		        "process\n", TWAIT_FNAME);
1602		TWAIT_REQUIRE_FAILURE(ECHILD,
1603		    wpid = TWAIT_GENERIC(child2, &status, 0));
1604	}
1605#endif
1606
1607	DPRINTF("Before calling %s() for the child - expected stopped "
1608	    "SIGCHLD\n", TWAIT_FNAME);
1609	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1610
1611	validate_status_stopped(status, SIGCHLD);
1612
1613	DPRINTF("Before resuming the child process where it left off and "
1614	    "without signal to be sent\n");
1615	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1616
1617	DPRINTF("Before calling %s() for the child - expected exited\n",
1618	    TWAIT_FNAME);
1619	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1620
1621	validate_status_exited(status, exitval);
1622
1623	DPRINTF("Before calling %s() for the child - expected no process\n",
1624	    TWAIT_FNAME);
1625	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1626}
1627
1628#define FORK_TEST(name,descr,fun,tfork,tvfork,tvforkdone,detchild,detparent)	\
1629ATF_TC(name);									\
1630ATF_TC_HEAD(name, tc)								\
1631{										\
1632	atf_tc_set_md_var(tc, "descr", descr);					\
1633}										\
1634										\
1635ATF_TC_BODY(name, tc)								\
1636{										\
1637										\
1638	fork_body(fun, tfork, tvfork, tvforkdone, detchild, detparent);		\
1639}
1640
1641#define F false
1642#define T true
1643
1644#define F_IF__0(x)
1645#define F_IF__1(x) x
1646#define F_IF__(x,y) F_IF__ ## x (y)
1647#define F_IF_(x,y) F_IF__(x,y)
1648#define F_IF(x,y) F_IF_(x,y)
1649
1650#define DSCR(function,forkbit,vforkbit,vforkdonebit,dchildbit,dparentbit)	\
1651        "Verify " #function "(2) called with 0"					\
1652        F_IF(forkbit,"|PTRACE_FORK")						\
1653        F_IF(vforkbit,"|PTRACE_VFORK")						\
1654        F_IF(vforkdonebit,"|PTRACE_VFORK_DONE")					\
1655        " in EVENT_MASK."							\
1656        F_IF(dchildbit," Detach child in this test.")				\
1657        F_IF(dparentbit," Detach parent in this test.")
1658
1659FORK_TEST(fork1, DSCR(fork,0,0,0,0,0), fork, F, F, F, F, F)
1660#if defined(TWAIT_HAVE_PID)
1661FORK_TEST(fork2, DSCR(fork,1,0,0,0,0), fork, T, F, F, F, F)
1662FORK_TEST(fork3, DSCR(fork,0,1,0,0,0), fork, F, T, F, F, F)
1663FORK_TEST(fork4, DSCR(fork,1,1,0,0,0), fork, T, T, F, F, F)
1664#endif
1665FORK_TEST(fork5, DSCR(fork,0,0,1,0,0), fork, F, F, T, F, F)
1666#if defined(TWAIT_HAVE_PID)
1667FORK_TEST(fork6, DSCR(fork,1,0,1,0,0), fork, T, F, T, F, F)
1668FORK_TEST(fork7, DSCR(fork,0,1,1,0,0), fork, F, T, T, F, F)
1669FORK_TEST(fork8, DSCR(fork,1,1,1,0,0), fork, T, T, T, F, F)
1670#endif
1671
1672FORK_TEST(vfork1, DSCR(vfork,0,0,0,0,0), vfork, F, F, F, F, F)
1673#if defined(TWAIT_HAVE_PID)
1674FORK_TEST(vfork2, DSCR(vfork,1,0,0,0,0), vfork, T, F, F, F, F)
1675FORK_TEST(vfork3, DSCR(vfork,0,1,0,0,0), vfork, F, T, F, F, F)
1676FORK_TEST(vfork4, DSCR(vfork,1,1,0,0,0), vfork, T, T, F, F, F)
1677#endif
1678FORK_TEST(vfork5, DSCR(vfork,0,0,1,0,0), vfork, F, F, T, F, F)
1679#if defined(TWAIT_HAVE_PID)
1680FORK_TEST(vfork6, DSCR(vfork,1,0,1,0,0), vfork, T, F, T, F, F)
1681FORK_TEST(vfork7, DSCR(vfork,0,1,1,0,0), vfork, F, T, T, F, F)
1682FORK_TEST(vfork8, DSCR(vfork,1,1,1,0,0), vfork, T, T, T, F, F)
1683#endif
1684
1685/// ----------------------------------------------------------------------------
1686
1687enum bytes_transfer_type {
1688	BYTES_TRANSFER_DATA,
1689	BYTES_TRANSFER_DATAIO,
1690	BYTES_TRANSFER_TEXT,
1691	BYTES_TRANSFER_TEXTIO,
1692	BYTES_TRANSFER_AUXV
1693};
1694
1695static int __used
1696bytes_transfer_dummy(int a, int b, int c, int d)
1697{
1698	int e, f, g, h;
1699
1700	a *= 4;
1701	b += 3;
1702	c -= 2;
1703	d /= 1;
1704
1705	e = strtol("10", NULL, 10);
1706	f = strtol("20", NULL, 10);
1707	g = strtol("30", NULL, 10);
1708	h = strtol("40", NULL, 10);
1709
1710	return (a + b * c - d) + (e * f - g / h);
1711}
1712
1713static void
1714bytes_transfer(int operation, size_t size, enum bytes_transfer_type type)
1715{
1716	const int exitval = 5;
1717	const int sigval = SIGSTOP;
1718	pid_t child, wpid;
1719	bool skip = false;
1720
1721	int lookup_me = 0;
1722	uint8_t lookup_me8 = 0;
1723	uint16_t lookup_me16 = 0;
1724	uint32_t lookup_me32 = 0;
1725	uint64_t lookup_me64 = 0;
1726
1727	int magic = 0x13579246;
1728	uint8_t magic8 = 0xab;
1729	uint16_t magic16 = 0x1234;
1730	uint32_t magic32 = 0x98765432;
1731	uint64_t magic64 = 0xabcdef0123456789;
1732
1733	struct ptrace_io_desc io;
1734#if defined(TWAIT_HAVE_STATUS)
1735	int status;
1736#endif
1737	AuxInfo ai[64], *aip;
1738
1739	ATF_REQUIRE(size < sizeof(ai));
1740
1741	/* Prepare variables for .TEXT transfers */
1742	switch (type) {
1743	case BYTES_TRANSFER_TEXT:
1744		memcpy(&magic, bytes_transfer_dummy, sizeof(magic));
1745		break;
1746	case BYTES_TRANSFER_TEXTIO:
1747		switch (size) {
1748		case 8:
1749			memcpy(&magic8, bytes_transfer_dummy, sizeof(magic8));
1750			break;
1751		case 16:
1752			memcpy(&magic16, bytes_transfer_dummy, sizeof(magic16));
1753			break;
1754		case 32:
1755			memcpy(&magic32, bytes_transfer_dummy, sizeof(magic32));
1756			break;
1757		case 64:
1758			memcpy(&magic64, bytes_transfer_dummy, sizeof(magic64));
1759			break;
1760		}
1761		break;
1762	default:
1763		break;
1764	}
1765
1766	/* Prepare variables for PIOD and AUXV transfers */
1767	switch (type) {
1768	case BYTES_TRANSFER_TEXTIO:
1769	case BYTES_TRANSFER_DATAIO:
1770		io.piod_op = operation;
1771		switch (size) {
1772		case 8:
1773			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1774			               (void *)bytes_transfer_dummy :
1775			               &lookup_me8;
1776			io.piod_addr = &lookup_me8;
1777			io.piod_len = sizeof(lookup_me8);
1778			break;
1779		case 16:
1780			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1781			               (void *)bytes_transfer_dummy :
1782			               &lookup_me16;
1783			io.piod_addr = &lookup_me16;
1784			io.piod_len = sizeof(lookup_me16);
1785			break;
1786		case 32:
1787			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1788			               (void *)bytes_transfer_dummy :
1789			               &lookup_me32;
1790			io.piod_addr = &lookup_me32;
1791			io.piod_len = sizeof(lookup_me32);
1792			break;
1793		case 64:
1794			io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
1795			               (void *)bytes_transfer_dummy :
1796			               &lookup_me64;
1797			io.piod_addr = &lookup_me64;
1798			io.piod_len = sizeof(lookup_me64);
1799			break;
1800		default:
1801			break;
1802		}
1803		break;
1804	case BYTES_TRANSFER_AUXV:
1805		io.piod_op = operation;
1806		io.piod_offs = 0;
1807		io.piod_addr = ai;
1808		io.piod_len = size;
1809		break;
1810	default:
1811		break;
1812	}
1813
1814	DPRINTF("Before forking process PID=%d\n", getpid());
1815	SYSCALL_REQUIRE((child = fork()) != -1);
1816	if (child == 0) {
1817		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1818		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1819
1820		switch (type) {
1821		case BYTES_TRANSFER_DATA:
1822			switch (operation) {
1823			case PT_READ_D:
1824			case PT_READ_I:
1825				lookup_me = magic;
1826				break;
1827			default:
1828				break;
1829			}
1830			break;
1831		case BYTES_TRANSFER_DATAIO:
1832			switch (operation) {
1833			case PIOD_READ_D:
1834			case PIOD_READ_I:
1835				switch (size) {
1836				case 8:
1837					lookup_me8 = magic8;
1838					break;
1839				case 16:
1840					lookup_me16 = magic16;
1841					break;
1842				case 32:
1843					lookup_me32 = magic32;
1844					break;
1845				case 64:
1846					lookup_me64 = magic64;
1847					break;
1848				default:
1849					break;
1850				}
1851				break;
1852			default:
1853				break;
1854			}
1855		default:
1856			break;
1857		}
1858
1859		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1860		FORKEE_ASSERT(raise(sigval) == 0);
1861
1862		/* Handle PIOD and PT separately as operation values overlap */
1863		switch (type) {
1864		case BYTES_TRANSFER_DATA:
1865			switch (operation) {
1866			case PT_WRITE_D:
1867			case PT_WRITE_I:
1868				FORKEE_ASSERT_EQ(lookup_me, magic);
1869				break;
1870			default:
1871				break;
1872			}
1873			break;
1874		case BYTES_TRANSFER_DATAIO:
1875			switch (operation) {
1876			case PIOD_WRITE_D:
1877			case PIOD_WRITE_I:
1878				switch (size) {
1879				case 8:
1880					FORKEE_ASSERT_EQ(lookup_me8, magic8);
1881					break;
1882				case 16:
1883					FORKEE_ASSERT_EQ(lookup_me16, magic16);
1884					break;
1885				case 32:
1886					FORKEE_ASSERT_EQ(lookup_me32, magic32);
1887					break;
1888				case 64:
1889					FORKEE_ASSERT_EQ(lookup_me64, magic64);
1890					break;
1891				default:
1892					break;
1893				}
1894				break;
1895			default:
1896				break;
1897			}
1898			break;
1899		case BYTES_TRANSFER_TEXT:
1900			FORKEE_ASSERT(memcmp(&magic, bytes_transfer_dummy,
1901			                     sizeof(magic)) == 0);
1902			break;
1903		case BYTES_TRANSFER_TEXTIO:
1904			switch (size) {
1905			case 8:
1906				FORKEE_ASSERT(memcmp(&magic8,
1907				                     bytes_transfer_dummy,
1908				                     sizeof(magic8)) == 0);
1909				break;
1910			case 16:
1911				FORKEE_ASSERT(memcmp(&magic16,
1912				                     bytes_transfer_dummy,
1913				                     sizeof(magic16)) == 0);
1914				break;
1915			case 32:
1916				FORKEE_ASSERT(memcmp(&magic32,
1917				                     bytes_transfer_dummy,
1918				                     sizeof(magic32)) == 0);
1919				break;
1920			case 64:
1921				FORKEE_ASSERT(memcmp(&magic64,
1922				                     bytes_transfer_dummy,
1923				                     sizeof(magic64)) == 0);
1924				break;
1925			}
1926			break;
1927		default:
1928			break;
1929		}
1930
1931		DPRINTF("Before exiting of the child process\n");
1932		_exit(exitval);
1933	}
1934	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1935
1936	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1937	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1938
1939	validate_status_stopped(status, sigval);
1940
1941	/* Check PaX MPROTECT */
1942	if (!can_we_write_to_text(child)) {
1943		switch (type) {
1944		case BYTES_TRANSFER_TEXTIO:
1945			switch (operation) {
1946			case PIOD_WRITE_D:
1947			case PIOD_WRITE_I:
1948				skip = true;
1949				break;
1950			default:
1951				break;
1952			}
1953			break;
1954		case BYTES_TRANSFER_TEXT:
1955			switch (operation) {
1956			case PT_WRITE_D:
1957			case PT_WRITE_I:
1958				skip = true;
1959				break;
1960			default:
1961				break;
1962			}
1963			break;
1964		default:
1965			break;
1966		}
1967	}
1968
1969	/* Bailout cleanly killing the child process */
1970	if (skip) {
1971		SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void *)1, 0) != -1);
1972		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1973		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1974		                      child);
1975
1976		validate_status_signaled(status, SIGKILL, 0);
1977
1978		atf_tc_skip("PaX MPROTECT setup prevents writes to .text");
1979	}
1980
1981	DPRINTF("Calling operation to transfer bytes between child=%d and "
1982	       "parent=%d\n", child, getpid());
1983
1984	switch (type) {
1985	case BYTES_TRANSFER_TEXTIO:
1986	case BYTES_TRANSFER_DATAIO:
1987	case BYTES_TRANSFER_AUXV:
1988		switch (operation) {
1989		case PIOD_WRITE_D:
1990		case PIOD_WRITE_I:
1991			switch (size) {
1992			case 8:
1993				lookup_me8 = magic8;
1994				break;
1995			case 16:
1996				lookup_me16 = magic16;
1997				break;
1998			case 32:
1999				lookup_me32 = magic32;
2000				break;
2001			case 64:
2002				lookup_me64 = magic64;
2003				break;
2004			default:
2005				break;
2006			}
2007			break;
2008		default:
2009			break;
2010		}
2011		SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2012		switch (operation) {
2013		case PIOD_READ_D:
2014		case PIOD_READ_I:
2015			switch (size) {
2016			case 8:
2017				ATF_REQUIRE_EQ(lookup_me8, magic8);
2018				break;
2019			case 16:
2020				ATF_REQUIRE_EQ(lookup_me16, magic16);
2021				break;
2022			case 32:
2023				ATF_REQUIRE_EQ(lookup_me32, magic32);
2024				break;
2025			case 64:
2026				ATF_REQUIRE_EQ(lookup_me64, magic64);
2027				break;
2028			default:
2029				break;
2030			}
2031			break;
2032		case PIOD_READ_AUXV:
2033			DPRINTF("Asserting that AUXV length (%zu) is > 0\n",
2034			        io.piod_len);
2035			ATF_REQUIRE(io.piod_len > 0);
2036			for (aip = ai; aip->a_type != AT_NULL; aip++)
2037				DPRINTF("a_type=%#llx a_v=%#llx\n",
2038				    (long long int)aip->a_type,
2039				    (long long int)aip->a_v);
2040			break;
2041		default:
2042			break;
2043		}
2044		break;
2045	case BYTES_TRANSFER_TEXT:
2046		switch (operation) {
2047		case PT_READ_D:
2048		case PT_READ_I:
2049			errno = 0;
2050			lookup_me = ptrace(operation, child,
2051			                   bytes_transfer_dummy, 0);
2052			ATF_REQUIRE_EQ(lookup_me, magic);
2053			SYSCALL_REQUIRE_ERRNO(errno, 0);
2054			break;
2055		case PT_WRITE_D:
2056		case PT_WRITE_I:
2057			SYSCALL_REQUIRE(ptrace(operation, child,
2058			                       bytes_transfer_dummy, magic)
2059			                != -1);
2060			break;
2061		default:
2062			break;
2063		}
2064		break;
2065	case BYTES_TRANSFER_DATA:
2066		switch (operation) {
2067		case PT_READ_D:
2068		case PT_READ_I:
2069			errno = 0;
2070			lookup_me = ptrace(operation, child, &lookup_me, 0);
2071			ATF_REQUIRE_EQ(lookup_me, magic);
2072			SYSCALL_REQUIRE_ERRNO(errno, 0);
2073			break;
2074		case PT_WRITE_D:
2075		case PT_WRITE_I:
2076			lookup_me = magic;
2077			SYSCALL_REQUIRE(ptrace(operation, child, &lookup_me,
2078			                       magic) != -1);
2079			break;
2080		default:
2081			break;
2082		}
2083		break;
2084	default:
2085		break;
2086	}
2087
2088	DPRINTF("Before resuming the child process where it left off and "
2089	    "without signal to be sent\n");
2090	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2091
2092	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2093	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2094
2095	validate_status_exited(status, exitval);
2096
2097	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2098	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2099}
2100
2101#define BYTES_TRANSFER(test, operation, size, type)				\
2102ATF_TC(test);									\
2103ATF_TC_HEAD(test, tc)								\
2104{										\
2105	atf_tc_set_md_var(tc, "descr",						\
2106	    "Verify bytes transfer operation" #operation " and size " #size	\
2107	    " of type " #type);							\
2108}										\
2109										\
2110ATF_TC_BODY(test, tc)								\
2111{										\
2112										\
2113	bytes_transfer(operation, size, BYTES_TRANSFER_##type);			\
2114}
2115
2116// DATA
2117
2118BYTES_TRANSFER(bytes_transfer_piod_read_d_8, PIOD_READ_D, 8, DATAIO)
2119BYTES_TRANSFER(bytes_transfer_piod_read_d_16, PIOD_READ_D, 16, DATAIO)
2120BYTES_TRANSFER(bytes_transfer_piod_read_d_32, PIOD_READ_D, 32, DATAIO)
2121BYTES_TRANSFER(bytes_transfer_piod_read_d_64, PIOD_READ_D, 64, DATAIO)
2122
2123BYTES_TRANSFER(bytes_transfer_piod_read_i_8, PIOD_READ_I, 8, DATAIO)
2124BYTES_TRANSFER(bytes_transfer_piod_read_i_16, PIOD_READ_I, 16, DATAIO)
2125BYTES_TRANSFER(bytes_transfer_piod_read_i_32, PIOD_READ_I, 32, DATAIO)
2126BYTES_TRANSFER(bytes_transfer_piod_read_i_64, PIOD_READ_I, 64, DATAIO)
2127
2128BYTES_TRANSFER(bytes_transfer_piod_write_d_8, PIOD_WRITE_D, 8, DATAIO)
2129BYTES_TRANSFER(bytes_transfer_piod_write_d_16, PIOD_WRITE_D, 16, DATAIO)
2130BYTES_TRANSFER(bytes_transfer_piod_write_d_32, PIOD_WRITE_D, 32, DATAIO)
2131BYTES_TRANSFER(bytes_transfer_piod_write_d_64, PIOD_WRITE_D, 64, DATAIO)
2132
2133BYTES_TRANSFER(bytes_transfer_piod_write_i_8, PIOD_WRITE_I, 8, DATAIO)
2134BYTES_TRANSFER(bytes_transfer_piod_write_i_16, PIOD_WRITE_I, 16, DATAIO)
2135BYTES_TRANSFER(bytes_transfer_piod_write_i_32, PIOD_WRITE_I, 32, DATAIO)
2136BYTES_TRANSFER(bytes_transfer_piod_write_i_64, PIOD_WRITE_I, 64, DATAIO)
2137
2138BYTES_TRANSFER(bytes_transfer_read_d, PT_READ_D, 32, DATA)
2139BYTES_TRANSFER(bytes_transfer_read_i, PT_READ_I, 32, DATA)
2140BYTES_TRANSFER(bytes_transfer_write_d, PT_WRITE_D, 32, DATA)
2141BYTES_TRANSFER(bytes_transfer_write_i, PT_WRITE_I, 32, DATA)
2142
2143// TEXT
2144
2145BYTES_TRANSFER(bytes_transfer_piod_read_d_8_text, PIOD_READ_D, 8, TEXTIO)
2146BYTES_TRANSFER(bytes_transfer_piod_read_d_16_text, PIOD_READ_D, 16, TEXTIO)
2147BYTES_TRANSFER(bytes_transfer_piod_read_d_32_text, PIOD_READ_D, 32, TEXTIO)
2148BYTES_TRANSFER(bytes_transfer_piod_read_d_64_text, PIOD_READ_D, 64, TEXTIO)
2149
2150BYTES_TRANSFER(bytes_transfer_piod_read_i_8_text, PIOD_READ_I, 8, TEXTIO)
2151BYTES_TRANSFER(bytes_transfer_piod_read_i_16_text, PIOD_READ_I, 16, TEXTIO)
2152BYTES_TRANSFER(bytes_transfer_piod_read_i_32_text, PIOD_READ_I, 32, TEXTIO)
2153BYTES_TRANSFER(bytes_transfer_piod_read_i_64_text, PIOD_READ_I, 64, TEXTIO)
2154
2155BYTES_TRANSFER(bytes_transfer_piod_write_d_8_text, PIOD_WRITE_D, 8, TEXTIO)
2156BYTES_TRANSFER(bytes_transfer_piod_write_d_16_text, PIOD_WRITE_D, 16, TEXTIO)
2157BYTES_TRANSFER(bytes_transfer_piod_write_d_32_text, PIOD_WRITE_D, 32, TEXTIO)
2158BYTES_TRANSFER(bytes_transfer_piod_write_d_64_text, PIOD_WRITE_D, 64, TEXTIO)
2159
2160BYTES_TRANSFER(bytes_transfer_piod_write_i_8_text, PIOD_WRITE_I, 8, TEXTIO)
2161BYTES_TRANSFER(bytes_transfer_piod_write_i_16_text, PIOD_WRITE_I, 16, TEXTIO)
2162BYTES_TRANSFER(bytes_transfer_piod_write_i_32_text, PIOD_WRITE_I, 32, TEXTIO)
2163BYTES_TRANSFER(bytes_transfer_piod_write_i_64_text, PIOD_WRITE_I, 64, TEXTIO)
2164
2165BYTES_TRANSFER(bytes_transfer_read_d_text, PT_READ_D, 32, TEXT)
2166BYTES_TRANSFER(bytes_transfer_read_i_text, PT_READ_I, 32, TEXT)
2167BYTES_TRANSFER(bytes_transfer_write_d_text, PT_WRITE_D, 32, TEXT)
2168BYTES_TRANSFER(bytes_transfer_write_i_text, PT_WRITE_I, 32, TEXT)
2169
2170// AUXV
2171
2172BYTES_TRANSFER(bytes_transfer_piod_read_auxv, PIOD_READ_AUXV, 4096, AUXV)
2173
2174/// ----------------------------------------------------------------------------
2175
2176#if defined(HAVE_GPREGS)
2177ATF_TC(regs1);
2178ATF_TC_HEAD(regs1, tc)
2179{
2180	atf_tc_set_md_var(tc, "descr",
2181	    "Verify plain PT_GETREGS call without further steps");
2182}
2183
2184ATF_TC_BODY(regs1, tc)
2185{
2186	const int exitval = 5;
2187	const int sigval = SIGSTOP;
2188	pid_t child, wpid;
2189#if defined(TWAIT_HAVE_STATUS)
2190	int status;
2191#endif
2192	struct reg r;
2193
2194	DPRINTF("Before forking process PID=%d\n", getpid());
2195	SYSCALL_REQUIRE((child = fork()) != -1);
2196	if (child == 0) {
2197		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2198		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2199
2200		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2201		FORKEE_ASSERT(raise(sigval) == 0);
2202
2203		DPRINTF("Before exiting of the child process\n");
2204		_exit(exitval);
2205	}
2206	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2207
2208	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2209	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2210
2211	validate_status_stopped(status, sigval);
2212
2213	DPRINTF("Call GETREGS for the child process\n");
2214	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2215
2216	DPRINTF("Before resuming the child process where it left off and "
2217	    "without signal to be sent\n");
2218	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2219
2220	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2221	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2222
2223	validate_status_exited(status, exitval);
2224
2225	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2226	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2227}
2228#endif
2229
2230#if defined(HAVE_GPREGS)
2231ATF_TC(regs2);
2232ATF_TC_HEAD(regs2, tc)
2233{
2234	atf_tc_set_md_var(tc, "descr",
2235	    "Verify plain PT_GETREGS call and retrieve PC");
2236}
2237
2238ATF_TC_BODY(regs2, tc)
2239{
2240	const int exitval = 5;
2241	const int sigval = SIGSTOP;
2242	pid_t child, wpid;
2243#if defined(TWAIT_HAVE_STATUS)
2244	int status;
2245#endif
2246	struct reg r;
2247
2248	DPRINTF("Before forking process PID=%d\n", getpid());
2249	SYSCALL_REQUIRE((child = fork()) != -1);
2250	if (child == 0) {
2251		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2252		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2253
2254		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2255		FORKEE_ASSERT(raise(sigval) == 0);
2256
2257		DPRINTF("Before exiting of the child process\n");
2258		_exit(exitval);
2259	}
2260	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2261
2262	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2263	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2264
2265	validate_status_stopped(status, sigval);
2266
2267	DPRINTF("Call GETREGS for the child process\n");
2268	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2269
2270	DPRINTF("Retrieved PC=%" PRIxREGISTER "\n", PTRACE_REG_PC(&r));
2271
2272	DPRINTF("Before resuming the child process where it left off and "
2273	    "without signal to be sent\n");
2274	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2275
2276	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2277	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2278
2279	validate_status_exited(status, exitval);
2280
2281	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2282	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2283}
2284#endif
2285
2286#if defined(HAVE_GPREGS)
2287ATF_TC(regs3);
2288ATF_TC_HEAD(regs3, tc)
2289{
2290	atf_tc_set_md_var(tc, "descr",
2291	    "Verify plain PT_GETREGS call and retrieve SP");
2292}
2293
2294ATF_TC_BODY(regs3, tc)
2295{
2296	const int exitval = 5;
2297	const int sigval = SIGSTOP;
2298	pid_t child, wpid;
2299#if defined(TWAIT_HAVE_STATUS)
2300	int status;
2301#endif
2302	struct reg r;
2303
2304	DPRINTF("Before forking process PID=%d\n", getpid());
2305	SYSCALL_REQUIRE((child = fork()) != -1);
2306	if (child == 0) {
2307		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2308		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2309
2310		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2311		FORKEE_ASSERT(raise(sigval) == 0);
2312
2313		DPRINTF("Before exiting of the child process\n");
2314		_exit(exitval);
2315	}
2316	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2317
2318	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2319	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2320
2321	validate_status_stopped(status, sigval);
2322
2323	DPRINTF("Call GETREGS for the child process\n");
2324	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2325
2326	DPRINTF("Retrieved SP=%" PRIxREGISTER "\n", PTRACE_REG_SP(&r));
2327
2328	DPRINTF("Before resuming the child process where it left off and "
2329	    "without signal to be sent\n");
2330	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2331
2332	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2333	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2334
2335	validate_status_exited(status, exitval);
2336
2337	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2338	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2339}
2340#endif
2341
2342#if defined(HAVE_GPREGS)
2343ATF_TC(regs4);
2344ATF_TC_HEAD(regs4, tc)
2345{
2346	atf_tc_set_md_var(tc, "descr",
2347	    "Verify plain PT_GETREGS call and retrieve INTRV");
2348}
2349
2350ATF_TC_BODY(regs4, tc)
2351{
2352	const int exitval = 5;
2353	const int sigval = SIGSTOP;
2354	pid_t child, wpid;
2355#if defined(TWAIT_HAVE_STATUS)
2356	int status;
2357#endif
2358	struct reg r;
2359
2360	DPRINTF("Before forking process PID=%d\n", getpid());
2361	SYSCALL_REQUIRE((child = fork()) != -1);
2362	if (child == 0) {
2363		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2364		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2365
2366		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2367		FORKEE_ASSERT(raise(sigval) == 0);
2368
2369		DPRINTF("Before exiting of the child process\n");
2370		_exit(exitval);
2371	}
2372	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2373
2374	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2375	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2376
2377	validate_status_stopped(status, sigval);
2378
2379	DPRINTF("Call GETREGS for the child process\n");
2380	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2381
2382	DPRINTF("Retrieved INTRV=%" PRIxREGISTER "\n", PTRACE_REG_INTRV(&r));
2383
2384	DPRINTF("Before resuming the child process where it left off and "
2385	    "without signal to be sent\n");
2386	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2387
2388	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2389	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2390
2391	validate_status_exited(status, exitval);
2392
2393	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2394	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2395}
2396#endif
2397
2398#if defined(HAVE_GPREGS)
2399ATF_TC(regs5);
2400ATF_TC_HEAD(regs5, tc)
2401{
2402	atf_tc_set_md_var(tc, "descr",
2403	    "Verify PT_GETREGS and PT_SETREGS calls without changing regs");
2404}
2405
2406ATF_TC_BODY(regs5, tc)
2407{
2408	const int exitval = 5;
2409	const int sigval = SIGSTOP;
2410	pid_t child, wpid;
2411#if defined(TWAIT_HAVE_STATUS)
2412	int status;
2413#endif
2414	struct reg r;
2415
2416	DPRINTF("Before forking process PID=%d\n", getpid());
2417	SYSCALL_REQUIRE((child = fork()) != -1);
2418	if (child == 0) {
2419		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2420		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2421
2422		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2423		FORKEE_ASSERT(raise(sigval) == 0);
2424
2425		DPRINTF("Before exiting of the child process\n");
2426		_exit(exitval);
2427	}
2428	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2429
2430	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2431	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2432
2433	validate_status_stopped(status, sigval);
2434
2435	DPRINTF("Call GETREGS for the child process\n");
2436	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2437
2438	DPRINTF("Call SETREGS for the child process (without changed regs)\n");
2439	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
2440
2441	DPRINTF("Before resuming the child process where it left off and "
2442	    "without signal to be sent\n");
2443	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2444
2445	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2446	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2447
2448	validate_status_exited(status, exitval);
2449
2450	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2451	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2452}
2453#endif
2454
2455#if defined(HAVE_FPREGS)
2456ATF_TC(fpregs1);
2457ATF_TC_HEAD(fpregs1, tc)
2458{
2459	atf_tc_set_md_var(tc, "descr",
2460	    "Verify plain PT_GETFPREGS call without further steps");
2461}
2462
2463ATF_TC_BODY(fpregs1, tc)
2464{
2465	const int exitval = 5;
2466	const int sigval = SIGSTOP;
2467	pid_t child, wpid;
2468#if defined(TWAIT_HAVE_STATUS)
2469	int status;
2470#endif
2471	struct fpreg r;
2472
2473	DPRINTF("Before forking process PID=%d\n", getpid());
2474	SYSCALL_REQUIRE((child = fork()) != -1);
2475	if (child == 0) {
2476		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2477		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2478
2479		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2480		FORKEE_ASSERT(raise(sigval) == 0);
2481
2482		DPRINTF("Before exiting of the child process\n");
2483		_exit(exitval);
2484	}
2485	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2486
2487	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2488	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2489
2490	validate_status_stopped(status, sigval);
2491
2492	DPRINTF("Call GETFPREGS for the child process\n");
2493	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
2494
2495	DPRINTF("Before resuming the child process where it left off and "
2496	    "without signal to be sent\n");
2497	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2498
2499	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2500	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2501
2502	validate_status_exited(status, exitval);
2503
2504	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2505	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2506}
2507#endif
2508
2509#if defined(HAVE_FPREGS)
2510ATF_TC(fpregs2);
2511ATF_TC_HEAD(fpregs2, tc)
2512{
2513	atf_tc_set_md_var(tc, "descr",
2514	    "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing "
2515	    "regs");
2516}
2517
2518ATF_TC_BODY(fpregs2, tc)
2519{
2520	const int exitval = 5;
2521	const int sigval = SIGSTOP;
2522	pid_t child, wpid;
2523#if defined(TWAIT_HAVE_STATUS)
2524	int status;
2525#endif
2526	struct fpreg r;
2527
2528	DPRINTF("Before forking process PID=%d\n", getpid());
2529	SYSCALL_REQUIRE((child = fork()) != -1);
2530	if (child == 0) {
2531		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2532		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2533
2534		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2535		FORKEE_ASSERT(raise(sigval) == 0);
2536
2537		DPRINTF("Before exiting of the child process\n");
2538		_exit(exitval);
2539	}
2540	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2541
2542	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2543	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2544
2545	validate_status_stopped(status, sigval);
2546
2547	DPRINTF("Call GETFPREGS for the child process\n");
2548	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
2549
2550	DPRINTF("Call SETFPREGS for the child (without changed regs)\n");
2551	SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1);
2552
2553	DPRINTF("Before resuming the child process where it left off and "
2554	    "without signal to be sent\n");
2555	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2556
2557	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2558	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2559
2560	validate_status_exited(status, exitval);
2561
2562	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2563	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2564}
2565#endif
2566
2567#if defined(PT_STEP)
2568static void
2569ptrace_step(int N, int setstep)
2570{
2571	const int exitval = 5;
2572	const int sigval = SIGSTOP;
2573	pid_t child, wpid;
2574#if defined(TWAIT_HAVE_STATUS)
2575	int status;
2576#endif
2577	int happy;
2578
2579#if defined(__arm__)
2580	/* PT_STEP not supported on arm 32-bit */
2581	atf_tc_expect_fail("PR kern/52119");
2582#endif
2583
2584	DPRINTF("Before forking process PID=%d\n", getpid());
2585	SYSCALL_REQUIRE((child = fork()) != -1);
2586	if (child == 0) {
2587		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2588		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2589
2590		happy = check_happy(999);
2591
2592		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2593		FORKEE_ASSERT(raise(sigval) == 0);
2594
2595		FORKEE_ASSERT_EQ(happy, check_happy(999));
2596
2597		DPRINTF("Before exiting of the child process\n");
2598		_exit(exitval);
2599	}
2600	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2601
2602	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2603	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2604
2605	validate_status_stopped(status, sigval);
2606
2607	while (N --> 0) {
2608		if (setstep) {
2609			DPRINTF("Before resuming the child process where it "
2610			    "left off and without signal to be sent (use "
2611			    "PT_SETSTEP and PT_CONTINUE)\n");
2612			SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1);
2613			SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0)
2614			    != -1);
2615		} else {
2616			DPRINTF("Before resuming the child process where it "
2617			    "left off and without signal to be sent (use "
2618			    "PT_STEP)\n");
2619			SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0)
2620			    != -1);
2621		}
2622
2623		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2624		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
2625		    child);
2626
2627		validate_status_stopped(status, SIGTRAP);
2628
2629		if (setstep) {
2630			SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1);
2631		}
2632	}
2633
2634	DPRINTF("Before resuming the child process where it left off and "
2635	    "without signal to be sent\n");
2636	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2637
2638	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2639	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2640
2641	validate_status_exited(status, exitval);
2642
2643	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2644	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2645}
2646#endif
2647
2648#if defined(PT_STEP)
2649ATF_TC(step1);
2650ATF_TC_HEAD(step1, tc)
2651{
2652	atf_tc_set_md_var(tc, "descr",
2653	    "Verify single PT_STEP call");
2654}
2655
2656ATF_TC_BODY(step1, tc)
2657{
2658	ptrace_step(1, 0);
2659}
2660#endif
2661
2662#if defined(PT_STEP)
2663ATF_TC(step2);
2664ATF_TC_HEAD(step2, tc)
2665{
2666	atf_tc_set_md_var(tc, "descr",
2667	    "Verify PT_STEP called twice");
2668}
2669
2670ATF_TC_BODY(step2, tc)
2671{
2672	ptrace_step(2, 0);
2673}
2674#endif
2675
2676#if defined(PT_STEP)
2677ATF_TC(step3);
2678ATF_TC_HEAD(step3, tc)
2679{
2680	atf_tc_set_md_var(tc, "descr",
2681	    "Verify PT_STEP called three times");
2682}
2683
2684ATF_TC_BODY(step3, tc)
2685{
2686	ptrace_step(3, 0);
2687}
2688#endif
2689
2690#if defined(PT_STEP)
2691ATF_TC(step4);
2692ATF_TC_HEAD(step4, tc)
2693{
2694	atf_tc_set_md_var(tc, "descr",
2695	    "Verify PT_STEP called four times");
2696}
2697
2698ATF_TC_BODY(step4, tc)
2699{
2700	ptrace_step(4, 0);
2701}
2702#endif
2703
2704#if defined(PT_STEP)
2705ATF_TC(setstep1);
2706ATF_TC_HEAD(setstep1, tc)
2707{
2708	atf_tc_set_md_var(tc, "descr",
2709	    "Verify single PT_SETSTEP call");
2710}
2711
2712ATF_TC_BODY(setstep1, tc)
2713{
2714	ptrace_step(1, 1);
2715}
2716#endif
2717
2718#if defined(PT_STEP)
2719ATF_TC(setstep2);
2720ATF_TC_HEAD(setstep2, tc)
2721{
2722	atf_tc_set_md_var(tc, "descr",
2723	    "Verify PT_SETSTEP called twice");
2724}
2725
2726ATF_TC_BODY(setstep2, tc)
2727{
2728	ptrace_step(2, 1);
2729}
2730#endif
2731
2732#if defined(PT_STEP)
2733ATF_TC(setstep3);
2734ATF_TC_HEAD(setstep3, tc)
2735{
2736	atf_tc_set_md_var(tc, "descr",
2737	    "Verify PT_SETSTEP called three times");
2738}
2739
2740ATF_TC_BODY(setstep3, tc)
2741{
2742	ptrace_step(3, 1);
2743}
2744#endif
2745
2746#if defined(PT_STEP)
2747ATF_TC(setstep4);
2748ATF_TC_HEAD(setstep4, tc)
2749{
2750	atf_tc_set_md_var(tc, "descr",
2751	    "Verify PT_SETSTEP called four times");
2752}
2753
2754ATF_TC_BODY(setstep4, tc)
2755{
2756	ptrace_step(4, 1);
2757}
2758#endif
2759
2760ATF_TC(kill1);
2761ATF_TC_HEAD(kill1, tc)
2762{
2763	atf_tc_set_md_var(tc, "descr",
2764	    "Verify that PT_CONTINUE with SIGKILL terminates child");
2765}
2766
2767ATF_TC_BODY(kill1, tc)
2768{
2769	const int sigval = SIGSTOP, sigsent = SIGKILL;
2770	pid_t child, wpid;
2771#if defined(TWAIT_HAVE_STATUS)
2772	int status;
2773#endif
2774
2775	DPRINTF("Before forking process PID=%d\n", getpid());
2776	SYSCALL_REQUIRE((child = fork()) != -1);
2777	if (child == 0) {
2778		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2779		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2780
2781		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2782		FORKEE_ASSERT(raise(sigval) == 0);
2783
2784		/* NOTREACHED */
2785		FORKEE_ASSERTX(0 &&
2786		    "Child should be terminated by a signal from its parent");
2787	}
2788	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2789
2790	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2791	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2792
2793	validate_status_stopped(status, sigval);
2794
2795	DPRINTF("Before resuming the child process where it left off and "
2796	    "without signal to be sent\n");
2797	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
2798
2799	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2800	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2801
2802	validate_status_signaled(status, sigsent, 0);
2803
2804	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2805	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2806}
2807
2808ATF_TC(kill2);
2809ATF_TC_HEAD(kill2, tc)
2810{
2811	atf_tc_set_md_var(tc, "descr",
2812	    "Verify that PT_KILL terminates child");
2813}
2814
2815ATF_TC_BODY(kill2, tc)
2816{
2817	const int sigval = SIGSTOP;
2818	pid_t child, wpid;
2819#if defined(TWAIT_HAVE_STATUS)
2820	int status;
2821#endif
2822
2823	DPRINTF("Before forking process PID=%d\n", getpid());
2824	SYSCALL_REQUIRE((child = fork()) != -1);
2825	if (child == 0) {
2826		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2827		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2828
2829		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2830		FORKEE_ASSERT(raise(sigval) == 0);
2831
2832		/* NOTREACHED */
2833		FORKEE_ASSERTX(0 &&
2834		    "Child should be terminated by a signal from its parent");
2835	}
2836	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2837
2838	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2839	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2840
2841	validate_status_stopped(status, sigval);
2842
2843	DPRINTF("Before resuming the child process where it left off and "
2844	    "without signal to be sent\n");
2845	SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
2846
2847	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2848	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2849
2850	validate_status_signaled(status, SIGKILL, 0);
2851
2852	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2853	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2854}
2855
2856ATF_TC(lwpinfo1);
2857ATF_TC_HEAD(lwpinfo1, tc)
2858{
2859	atf_tc_set_md_var(tc, "descr",
2860	    "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
2861}
2862
2863ATF_TC_BODY(lwpinfo1, tc)
2864{
2865	const int exitval = 5;
2866	const int sigval = SIGSTOP;
2867	pid_t child, wpid;
2868#if defined(TWAIT_HAVE_STATUS)
2869	int status;
2870#endif
2871	struct ptrace_lwpinfo info = {0, 0};
2872
2873	DPRINTF("Before forking process PID=%d\n", getpid());
2874	SYSCALL_REQUIRE((child = fork()) != -1);
2875	if (child == 0) {
2876		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2877		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2878
2879		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2880		FORKEE_ASSERT(raise(sigval) == 0);
2881
2882		DPRINTF("Before exiting of the child process\n");
2883		_exit(exitval);
2884	}
2885	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2886
2887	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2888	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2889
2890	validate_status_stopped(status, sigval);
2891
2892	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2893	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
2894
2895	DPRINTF("Assert that there exists a thread\n");
2896	ATF_REQUIRE(info.pl_lwpid > 0);
2897
2898	DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
2899	    info.pl_lwpid);
2900	ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
2901	    "Received event %d != expected event %d",
2902	    info.pl_event, PL_EVENT_SIGNAL);
2903
2904	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2905	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
2906
2907	DPRINTF("Assert that there are no more lwp threads in child\n");
2908	ATF_REQUIRE_EQ(info.pl_lwpid, 0);
2909
2910	DPRINTF("Before resuming the child process where it left off and "
2911	    "without signal to be sent\n");
2912	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2913
2914	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2915	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2916
2917	validate_status_exited(status, exitval);
2918
2919	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2920	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2921}
2922
2923#if defined(TWAIT_HAVE_PID)
2924ATF_TC(lwpinfo2);
2925ATF_TC_HEAD(lwpinfo2, tc)
2926{
2927	atf_tc_set_md_var(tc, "descr",
2928	    "Verify basic LWPINFO call for single thread (PT_ATTACH from "
2929	    "tracer)");
2930}
2931
2932ATF_TC_BODY(lwpinfo2, tc)
2933{
2934	struct msg_fds parent_tracee, parent_tracer;
2935	const int exitval_tracee = 5;
2936	const int exitval_tracer = 10;
2937	pid_t tracee, tracer, wpid;
2938	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
2939#if defined(TWAIT_HAVE_STATUS)
2940	int status;
2941#endif
2942	struct ptrace_lwpinfo info = {0, 0};
2943
2944	DPRINTF("Spawn tracee\n");
2945	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
2946	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
2947	tracee = atf_utils_fork();
2948	if (tracee == 0) {
2949
2950		/* Wait for message from the parent */
2951		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
2952		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
2953
2954		_exit(exitval_tracee);
2955	}
2956	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
2957
2958	DPRINTF("Spawn debugger\n");
2959	tracer = atf_utils_fork();
2960	if (tracer == 0) {
2961		/* No IPC to communicate with the child */
2962		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
2963		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
2964
2965		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
2966		FORKEE_REQUIRE_SUCCESS(
2967		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
2968
2969		forkee_status_stopped(status, SIGSTOP);
2970
2971		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2972		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
2973		    != -1);
2974
2975		DPRINTF("Assert that there exists a thread\n");
2976		FORKEE_ASSERTX(info.pl_lwpid > 0);
2977
2978		DPRINTF("Assert that lwp thread %d received event "
2979		    "PL_EVENT_SIGNAL\n", info.pl_lwpid);
2980		FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
2981
2982		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
2983		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
2984		    != -1);
2985
2986		DPRINTF("Assert that there are no more lwp threads in child\n");
2987		FORKEE_ASSERTX(info.pl_lwpid == 0);
2988
2989		/* Resume tracee with PT_CONTINUE */
2990		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
2991
2992		/* Inform parent that tracer has attached to tracee */
2993		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
2994		/* Wait for parent */
2995		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
2996
2997		/* Wait for tracee and assert that it exited */
2998		FORKEE_REQUIRE_SUCCESS(
2999		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
3000
3001		forkee_status_exited(status, exitval_tracee);
3002
3003		DPRINTF("Before exiting of the tracer process\n");
3004		_exit(exitval_tracer);
3005	}
3006
3007	DPRINTF("Wait for the tracer to attach to the tracee\n");
3008	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
3009
3010	DPRINTF("Resume the tracee and let it exit\n");
3011	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
3012
3013	DPRINTF("Detect that tracee is zombie\n");
3014	await_zombie(tracee);
3015
3016	DPRINTF("Assert that there is no status about tracee - "
3017	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
3018	TWAIT_REQUIRE_SUCCESS(
3019	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
3020
3021	DPRINTF("Resume the tracer and let it detect exited tracee\n");
3022	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
3023
3024	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
3025	    TWAIT_FNAME);
3026	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
3027	    tracer);
3028
3029	validate_status_exited(status, exitval_tracer);
3030
3031	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
3032	    TWAIT_FNAME);
3033	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
3034	    tracee);
3035
3036	validate_status_exited(status, exitval_tracee);
3037
3038	msg_close(&parent_tracer);
3039	msg_close(&parent_tracee);
3040}
3041#endif
3042
3043ATF_TC(siginfo1);
3044ATF_TC_HEAD(siginfo1, tc)
3045{
3046	atf_tc_set_md_var(tc, "descr",
3047	    "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
3048}
3049
3050ATF_TC_BODY(siginfo1, tc)
3051{
3052	const int exitval = 5;
3053	const int sigval = SIGTRAP;
3054	pid_t child, wpid;
3055#if defined(TWAIT_HAVE_STATUS)
3056	int status;
3057#endif
3058	struct ptrace_siginfo info;
3059	memset(&info, 0, sizeof(info));
3060
3061	DPRINTF("Before forking process PID=%d\n", getpid());
3062	SYSCALL_REQUIRE((child = fork()) != -1);
3063	if (child == 0) {
3064		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3065		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3066
3067		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3068		FORKEE_ASSERT(raise(sigval) == 0);
3069
3070		DPRINTF("Before exiting of the child process\n");
3071		_exit(exitval);
3072	}
3073	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3074
3075	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3076	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3077
3078	validate_status_stopped(status, sigval);
3079
3080	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3081	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3082
3083	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3084	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3085	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3086	    info.psi_siginfo.si_errno);
3087
3088	DPRINTF("Before resuming the child process where it left off and "
3089	    "without signal to be sent\n");
3090	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3091
3092	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3093	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3094
3095	validate_status_exited(status, exitval);
3096
3097	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3098	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3099}
3100
3101ATF_TC(siginfo2);
3102ATF_TC_HEAD(siginfo2, tc)
3103{
3104	atf_tc_set_md_var(tc, "descr",
3105	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
3106	    "modification of SIGINT from tracee");
3107}
3108
3109static int siginfo2_caught = 0;
3110
3111static void
3112siginfo2_sighandler(int sig)
3113{
3114	FORKEE_ASSERT_EQ(sig, SIGINT);
3115
3116	++siginfo2_caught;
3117}
3118
3119ATF_TC_BODY(siginfo2, tc)
3120{
3121	const int exitval = 5;
3122	const int sigval = SIGINT;
3123	pid_t child, wpid;
3124	struct sigaction sa;
3125#if defined(TWAIT_HAVE_STATUS)
3126	int status;
3127#endif
3128	struct ptrace_siginfo info;
3129	memset(&info, 0, sizeof(info));
3130
3131	DPRINTF("Before forking process PID=%d\n", getpid());
3132	SYSCALL_REQUIRE((child = fork()) != -1);
3133	if (child == 0) {
3134		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3135		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3136
3137		sa.sa_handler = siginfo2_sighandler;
3138		sa.sa_flags = SA_SIGINFO;
3139		sigemptyset(&sa.sa_mask);
3140
3141		FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
3142
3143		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3144		FORKEE_ASSERT(raise(sigval) == 0);
3145
3146		FORKEE_ASSERT_EQ(siginfo2_caught, 1);
3147
3148		DPRINTF("Before exiting of the child process\n");
3149		_exit(exitval);
3150	}
3151	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3152
3153	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3154	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3155
3156	validate_status_stopped(status, sigval);
3157
3158	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3159	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3160
3161	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3162	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3163	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3164	    info.psi_siginfo.si_errno);
3165
3166	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
3167	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
3168
3169	DPRINTF("Before resuming the child process where it left off and "
3170	    "without signal to be sent\n");
3171	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
3172
3173	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3174	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3175
3176	validate_status_exited(status, exitval);
3177
3178	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3179	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3180}
3181
3182ATF_TC(siginfo3);
3183ATF_TC_HEAD(siginfo3, tc)
3184{
3185	atf_tc_set_md_var(tc, "descr",
3186	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
3187	    "setting signal to new value");
3188}
3189
3190static int siginfo3_caught = 0;
3191
3192static void
3193siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
3194{
3195	FORKEE_ASSERT_EQ(sig, SIGTRAP);
3196
3197	FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
3198	FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
3199
3200	++siginfo3_caught;
3201}
3202
3203ATF_TC_BODY(siginfo3, tc)
3204{
3205	const int exitval = 5;
3206	const int sigval = SIGINT;
3207	const int sigfaked = SIGTRAP;
3208	const int sicodefaked = TRAP_BRKPT;
3209	pid_t child, wpid;
3210	struct sigaction sa;
3211#if defined(TWAIT_HAVE_STATUS)
3212	int status;
3213#endif
3214	struct ptrace_siginfo info;
3215	memset(&info, 0, sizeof(info));
3216
3217	DPRINTF("Before forking process PID=%d\n", getpid());
3218	SYSCALL_REQUIRE((child = fork()) != -1);
3219	if (child == 0) {
3220		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3221		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3222
3223		sa.sa_sigaction = siginfo3_sigaction;
3224		sa.sa_flags = SA_SIGINFO;
3225		sigemptyset(&sa.sa_mask);
3226
3227		FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
3228
3229		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3230		FORKEE_ASSERT(raise(sigval) == 0);
3231
3232		FORKEE_ASSERT_EQ(siginfo3_caught, 1);
3233
3234		DPRINTF("Before exiting of the child process\n");
3235		_exit(exitval);
3236	}
3237	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3238
3239	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3240	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3241
3242	validate_status_stopped(status, sigval);
3243
3244	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3245	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3246
3247	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3248	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3249	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3250	    info.psi_siginfo.si_errno);
3251
3252	DPRINTF("Before setting new faked signal to signo=%d si_code=%d\n",
3253	    sigfaked, sicodefaked);
3254	info.psi_siginfo.si_signo = sigfaked;
3255	info.psi_siginfo.si_code = sicodefaked;
3256
3257	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
3258	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
3259
3260	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3261	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3262
3263	DPRINTF("Before checking siginfo_t\n");
3264	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
3265	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
3266
3267	DPRINTF("Before resuming the child process where it left off and "
3268	    "without signal to be sent\n");
3269	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
3270
3271	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3272	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3273
3274	validate_status_exited(status, exitval);
3275
3276	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3277	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3278}
3279
3280ATF_TC(siginfo4);
3281ATF_TC_HEAD(siginfo4, tc)
3282{
3283	atf_tc_set_md_var(tc, "descr",
3284	    "Detect SIGTRAP TRAP_EXEC from tracee");
3285}
3286
3287ATF_TC_BODY(siginfo4, tc)
3288{
3289	const int sigval = SIGTRAP;
3290	pid_t child, wpid;
3291#if defined(TWAIT_HAVE_STATUS)
3292	int status;
3293#endif
3294
3295	struct ptrace_siginfo info;
3296	memset(&info, 0, sizeof(info));
3297
3298	DPRINTF("Before forking process PID=%d\n", getpid());
3299	SYSCALL_REQUIRE((child = fork()) != -1);
3300	if (child == 0) {
3301		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3302		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3303
3304		DPRINTF("Before calling execve(2) from child\n");
3305		execlp("/bin/echo", "/bin/echo", NULL);
3306
3307		FORKEE_ASSERT(0 && "Not reached");
3308	}
3309	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3310
3311	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3312	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3313
3314	validate_status_stopped(status, sigval);
3315
3316	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3317	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3318
3319	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
3320	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
3321	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
3322	    info.psi_siginfo.si_errno);
3323
3324	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3325	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
3326
3327	DPRINTF("Before resuming the child process where it left off and "
3328	    "without signal to be sent\n");
3329	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3330
3331	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3332	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3333
3334	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3335	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3336}
3337
3338#if defined(TWAIT_HAVE_PID)
3339ATF_TC(siginfo5);
3340ATF_TC_HEAD(siginfo5, tc)
3341{
3342	atf_tc_set_md_var(tc, "descr",
3343	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
3344	    "set to PTRACE_FORK and reports correct signal information");
3345}
3346
3347ATF_TC_BODY(siginfo5, tc)
3348{
3349	const int exitval = 5;
3350	const int exitval2 = 15;
3351	const int sigval = SIGSTOP;
3352	pid_t child, child2, wpid;
3353#if defined(TWAIT_HAVE_STATUS)
3354	int status;
3355#endif
3356	ptrace_state_t state;
3357	const int slen = sizeof(state);
3358	ptrace_event_t event;
3359	const int elen = sizeof(event);
3360	struct ptrace_siginfo info;
3361
3362	memset(&info, 0, sizeof(info));
3363
3364	DPRINTF("Before forking process PID=%d\n", getpid());
3365	SYSCALL_REQUIRE((child = fork()) != -1);
3366	if (child == 0) {
3367		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3368		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3369
3370		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3371		FORKEE_ASSERT(raise(sigval) == 0);
3372
3373		FORKEE_ASSERT((child2 = fork()) != -1);
3374
3375		if (child2 == 0)
3376			_exit(exitval2);
3377
3378		FORKEE_REQUIRE_SUCCESS
3379		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
3380
3381		forkee_status_exited(status, exitval2);
3382
3383		DPRINTF("Before exiting of the child process\n");
3384		_exit(exitval);
3385	}
3386	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3387
3388	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3389	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3390
3391	validate_status_stopped(status, sigval);
3392
3393	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3394	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3395
3396	DPRINTF("Before checking siginfo_t\n");
3397	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3398	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3399
3400	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
3401	event.pe_set_event = PTRACE_FORK;
3402	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3403
3404	DPRINTF("Before resuming the child process where it left off and "
3405	    "without signal to be sent\n");
3406        DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
3407               "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
3408               "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
3409                "state.pe_other_pid=child)\n", child);
3410	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3411
3412	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
3413	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3414
3415	validate_status_stopped(status, SIGTRAP);
3416
3417	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3418	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3419
3420	DPRINTF("Before checking siginfo_t\n");
3421	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3422	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
3423
3424	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3425	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
3426
3427	child2 = state.pe_other_pid;
3428	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
3429
3430	DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
3431	    TWAIT_FNAME, child2, child);
3432	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3433	    child2);
3434
3435	validate_status_stopped(status, SIGTRAP);
3436
3437	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3438	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3439
3440	DPRINTF("Before checking siginfo_t\n");
3441	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3442	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
3443
3444	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
3445	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
3446	ATF_REQUIRE_EQ(state.pe_other_pid, child);
3447
3448	DPRINTF("Before resuming the forkee process where it left off and "
3449	    "without signal to be sent\n");
3450	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
3451
3452	DPRINTF("Before resuming the child process where it left off and "
3453	    "without signal to be sent\n");
3454	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3455
3456	DPRINTF("Before calling %s() for the forkee - expected exited\n",
3457	    TWAIT_FNAME);
3458	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
3459	    child2);
3460
3461	validate_status_exited(status, exitval2);
3462
3463	DPRINTF("Before calling %s() for the forkee - expected no process\n",
3464	    TWAIT_FNAME);
3465	TWAIT_REQUIRE_FAILURE(ECHILD,
3466	    wpid = TWAIT_GENERIC(child2, &status, 0));
3467
3468	DPRINTF("Before calling %s() for the child - expected stopped "
3469	    "SIGCHLD\n", TWAIT_FNAME);
3470	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3471
3472	validate_status_stopped(status, SIGCHLD);
3473
3474	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3475	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3476
3477	DPRINTF("Before checking siginfo_t\n");
3478	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
3479	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
3480
3481	DPRINTF("Before resuming the child process where it left off and "
3482	    "without signal to be sent\n");
3483	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3484
3485	DPRINTF("Before calling %s() for the child - expected exited\n",
3486	    TWAIT_FNAME);
3487	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3488
3489	validate_status_exited(status, exitval);
3490
3491	DPRINTF("Before calling %s() for the child - expected no process\n",
3492	    TWAIT_FNAME);
3493	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3494}
3495#endif
3496
3497#if defined(PT_STEP)
3498ATF_TC(siginfo6);
3499ATF_TC_HEAD(siginfo6, tc)
3500{
3501	atf_tc_set_md_var(tc, "descr",
3502	    "Verify single PT_STEP call with signal information check");
3503}
3504
3505ATF_TC_BODY(siginfo6, tc)
3506{
3507	const int exitval = 5;
3508	const int sigval = SIGSTOP;
3509	pid_t child, wpid;
3510#if defined(TWAIT_HAVE_STATUS)
3511	int status;
3512#endif
3513	int happy;
3514	struct ptrace_siginfo info;
3515
3516#if defined(__arm__)
3517	/* PT_STEP not supported on arm 32-bit */
3518	atf_tc_expect_fail("PR kern/52119");
3519#endif
3520
3521	memset(&info, 0, sizeof(info));
3522
3523	DPRINTF("Before forking process PID=%d\n", getpid());
3524	SYSCALL_REQUIRE((child = fork()) != -1);
3525	if (child == 0) {
3526		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3527		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3528
3529		happy = check_happy(100);
3530
3531		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3532		FORKEE_ASSERT(raise(sigval) == 0);
3533
3534		FORKEE_ASSERT_EQ(happy, check_happy(100));
3535
3536		DPRINTF("Before exiting of the child process\n");
3537		_exit(exitval);
3538	}
3539	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3540
3541	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3542	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3543
3544	validate_status_stopped(status, sigval);
3545
3546	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3547	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3548
3549	DPRINTF("Before checking siginfo_t\n");
3550	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
3551	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
3552
3553	DPRINTF("Before resuming the child process where it left off and "
3554	    "without signal to be sent (use PT_STEP)\n");
3555	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
3556
3557	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3558	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3559
3560	validate_status_stopped(status, SIGTRAP);
3561
3562	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
3563	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
3564
3565	DPRINTF("Before checking siginfo_t\n");
3566	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
3567	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
3568
3569	DPRINTF("Before resuming the child process where it left off and "
3570	    "without signal to be sent\n");
3571	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3572
3573	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3574	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3575
3576	validate_status_exited(status, exitval);
3577
3578	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3579	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3580}
3581#endif
3582
3583volatile lwpid_t the_lwp_id = 0;
3584
3585static void
3586lwp_main_func(void *arg)
3587{
3588	the_lwp_id = _lwp_self();
3589	_lwp_exit();
3590}
3591
3592ATF_TC(lwp_create1);
3593ATF_TC_HEAD(lwp_create1, tc)
3594{
3595	atf_tc_set_md_var(tc, "descr",
3596	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
3597	    "EVENT_MASK set to PTRACE_LWP_CREATE");
3598}
3599
3600ATF_TC_BODY(lwp_create1, tc)
3601{
3602	const int exitval = 5;
3603	const int sigval = SIGSTOP;
3604	pid_t child, wpid;
3605#if defined(TWAIT_HAVE_STATUS)
3606	int status;
3607#endif
3608	ptrace_state_t state;
3609	const int slen = sizeof(state);
3610	ptrace_event_t event;
3611	const int elen = sizeof(event);
3612	ucontext_t uc;
3613	lwpid_t lid;
3614	static const size_t ssize = 16*1024;
3615	void *stack;
3616
3617	DPRINTF("Before forking process PID=%d\n", getpid());
3618	SYSCALL_REQUIRE((child = fork()) != -1);
3619	if (child == 0) {
3620		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3621		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3622
3623		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3624		FORKEE_ASSERT(raise(sigval) == 0);
3625
3626		DPRINTF("Before allocating memory for stack in child\n");
3627		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
3628
3629		DPRINTF("Before making context for new lwp in child\n");
3630		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
3631
3632		DPRINTF("Before creating new in child\n");
3633		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
3634
3635		DPRINTF("Before waiting for lwp %d to exit\n", lid);
3636		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
3637
3638		DPRINTF("Before verifying that reported %d and running lid %d "
3639		    "are the same\n", lid, the_lwp_id);
3640		FORKEE_ASSERT_EQ(lid, the_lwp_id);
3641
3642		DPRINTF("Before exiting of the child process\n");
3643		_exit(exitval);
3644	}
3645	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3646
3647	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3648	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3649
3650	validate_status_stopped(status, sigval);
3651
3652	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
3653	event.pe_set_event = PTRACE_LWP_CREATE;
3654	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3655
3656	DPRINTF("Before resuming the child process where it left off and "
3657	    "without signal to be sent\n");
3658	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3659
3660	DPRINTF("Before calling %s() for the child - expected stopped "
3661	    "SIGTRAP\n", TWAIT_FNAME);
3662	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3663
3664	validate_status_stopped(status, SIGTRAP);
3665
3666	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3667
3668	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
3669
3670	lid = state.pe_lwp;
3671	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
3672
3673	DPRINTF("Before resuming the child process where it left off and "
3674	    "without signal to be sent\n");
3675	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3676
3677	DPRINTF("Before calling %s() for the child - expected exited\n",
3678	    TWAIT_FNAME);
3679	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3680
3681	validate_status_exited(status, exitval);
3682
3683	DPRINTF("Before calling %s() for the child - expected no process\n",
3684	    TWAIT_FNAME);
3685	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3686}
3687
3688ATF_TC(lwp_exit1);
3689ATF_TC_HEAD(lwp_exit1, tc)
3690{
3691	atf_tc_set_md_var(tc, "descr",
3692	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
3693	    "EVENT_MASK set to PTRACE_LWP_EXIT");
3694}
3695
3696ATF_TC_BODY(lwp_exit1, tc)
3697{
3698	const int exitval = 5;
3699	const int sigval = SIGSTOP;
3700	pid_t child, wpid;
3701#if defined(TWAIT_HAVE_STATUS)
3702	int status;
3703#endif
3704	ptrace_state_t state;
3705	const int slen = sizeof(state);
3706	ptrace_event_t event;
3707	const int elen = sizeof(event);
3708	ucontext_t uc;
3709	lwpid_t lid;
3710	static const size_t ssize = 16*1024;
3711	void *stack;
3712
3713	DPRINTF("Before forking process PID=%d\n", getpid());
3714	SYSCALL_REQUIRE((child = fork()) != -1);
3715	if (child == 0) {
3716		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3717		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3718
3719		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3720		FORKEE_ASSERT(raise(sigval) == 0);
3721
3722		DPRINTF("Before allocating memory for stack in child\n");
3723		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
3724
3725		DPRINTF("Before making context for new lwp in child\n");
3726		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
3727
3728		DPRINTF("Before creating new in child\n");
3729		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
3730
3731		DPRINTF("Before waiting for lwp %d to exit\n", lid);
3732		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
3733
3734		DPRINTF("Before verifying that reported %d and running lid %d "
3735		    "are the same\n", lid, the_lwp_id);
3736		FORKEE_ASSERT_EQ(lid, the_lwp_id);
3737
3738		DPRINTF("Before exiting of the child process\n");
3739		_exit(exitval);
3740	}
3741	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3742
3743	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3744	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3745
3746	validate_status_stopped(status, sigval);
3747
3748	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
3749	event.pe_set_event = PTRACE_LWP_EXIT;
3750	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
3751
3752	DPRINTF("Before resuming the child process where it left off and "
3753	    "without signal to be sent\n");
3754	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3755
3756	DPRINTF("Before calling %s() for the child - expected stopped "
3757	    "SIGTRAP\n", TWAIT_FNAME);
3758	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3759
3760	validate_status_stopped(status, SIGTRAP);
3761
3762	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
3763
3764	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
3765
3766	lid = state.pe_lwp;
3767	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
3768
3769	DPRINTF("Before resuming the child process where it left off and "
3770	    "without signal to be sent\n");
3771	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3772
3773	DPRINTF("Before calling %s() for the child - expected exited\n",
3774	    TWAIT_FNAME);
3775	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3776
3777	validate_status_exited(status, exitval);
3778
3779	DPRINTF("Before calling %s() for the child - expected no process\n",
3780	    TWAIT_FNAME);
3781	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3782}
3783
3784ATF_TC(signal1);
3785ATF_TC_HEAD(signal1, tc)
3786{
3787	atf_tc_set_md_var(tc, "descr",
3788	    "Verify that masking single unrelated signal does not stop tracer "
3789	    "from catching other signals");
3790}
3791
3792ATF_TC_BODY(signal1, tc)
3793{
3794	const int exitval = 5;
3795	const int sigval = SIGSTOP;
3796	const int sigmasked = SIGTRAP;
3797	const int signotmasked = SIGINT;
3798	pid_t child, wpid;
3799#if defined(TWAIT_HAVE_STATUS)
3800	int status;
3801#endif
3802	sigset_t intmask;
3803
3804	DPRINTF("Before forking process PID=%d\n", getpid());
3805	SYSCALL_REQUIRE((child = fork()) != -1);
3806	if (child == 0) {
3807		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3808		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3809
3810		sigemptyset(&intmask);
3811		sigaddset(&intmask, sigmasked);
3812		sigprocmask(SIG_BLOCK, &intmask, NULL);
3813
3814		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3815		FORKEE_ASSERT(raise(sigval) == 0);
3816
3817		DPRINTF("Before raising %s from child\n",
3818		    strsignal(signotmasked));
3819		FORKEE_ASSERT(raise(signotmasked) == 0);
3820
3821		DPRINTF("Before exiting of the child process\n");
3822		_exit(exitval);
3823	}
3824	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3825
3826	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3827	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3828
3829	validate_status_stopped(status, sigval);
3830
3831	DPRINTF("Before resuming the child process where it left off and "
3832	    "without signal to be sent\n");
3833	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3834
3835	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3836	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3837
3838	validate_status_stopped(status, signotmasked);
3839
3840	DPRINTF("Before resuming the child process where it left off and "
3841	    "without signal to be sent\n");
3842	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3843
3844	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3845	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3846
3847	validate_status_exited(status, exitval);
3848
3849	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3850	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3851}
3852
3853ATF_TC(signal2);
3854ATF_TC_HEAD(signal2, tc)
3855{
3856	atf_tc_set_md_var(tc, "descr",
3857	    "Verify that masking SIGTRAP in tracee stops tracer from "
3858	    "catching this raised signal");
3859}
3860
3861ATF_TC_BODY(signal2, tc)
3862{
3863	const int exitval = 5;
3864	const int sigval = SIGSTOP;
3865	const int sigmasked = SIGTRAP;
3866	pid_t child, wpid;
3867#if defined(TWAIT_HAVE_STATUS)
3868	int status;
3869#endif
3870	sigset_t intmask;
3871
3872	DPRINTF("Before forking process PID=%d\n", getpid());
3873	SYSCALL_REQUIRE((child = fork()) != -1);
3874	if (child == 0) {
3875		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3876		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3877
3878		sigemptyset(&intmask);
3879		sigaddset(&intmask, sigmasked);
3880		sigprocmask(SIG_BLOCK, &intmask, NULL);
3881
3882		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3883		FORKEE_ASSERT(raise(sigval) == 0);
3884
3885		DPRINTF("Before raising %s breakpoint from child\n",
3886		    strsignal(sigmasked));
3887		FORKEE_ASSERT(raise(sigmasked) == 0);
3888
3889		DPRINTF("Before exiting of the child process\n");
3890		_exit(exitval);
3891	}
3892	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3893
3894	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3895	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3896
3897	validate_status_stopped(status, sigval);
3898
3899	DPRINTF("Before resuming the child process where it left off and "
3900	    "without signal to be sent\n");
3901	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3902
3903	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3904	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3905
3906	validate_status_exited(status, exitval);
3907
3908	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3909	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3910}
3911
3912ATF_TC(signal3);
3913ATF_TC_HEAD(signal3, tc)
3914{
3915	atf_tc_set_md_var(tc, "timeout", "5");
3916	atf_tc_set_md_var(tc, "descr",
3917	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
3918	    "catching software breakpoints");
3919}
3920
3921ATF_TC_BODY(signal3, tc)
3922{
3923	const int exitval = 5;
3924	const int sigval = SIGSTOP;
3925	const int sigmasked = SIGTRAP;
3926	pid_t child, wpid;
3927#if defined(TWAIT_HAVE_STATUS)
3928	int status;
3929#endif
3930	sigset_t intmask;
3931
3932	DPRINTF("Before forking process PID=%d\n", getpid());
3933	SYSCALL_REQUIRE((child = fork()) != -1);
3934	if (child == 0) {
3935		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3936		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3937
3938		sigemptyset(&intmask);
3939		sigaddset(&intmask, sigmasked);
3940		sigprocmask(SIG_BLOCK, &intmask, NULL);
3941
3942		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3943		FORKEE_ASSERT(raise(sigval) == 0);
3944
3945		DPRINTF("Before raising software breakpoint from child\n");
3946		trigger_trap();
3947
3948		DPRINTF("Before exiting of the child process\n");
3949		_exit(exitval);
3950	}
3951	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3952
3953	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3954	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3955
3956	validate_status_stopped(status, sigval);
3957
3958	DPRINTF("Before resuming the child process where it left off and "
3959	    "without signal to be sent\n");
3960	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3961
3962	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3963	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3964
3965	validate_status_stopped(status, sigmasked);
3966
3967	DPRINTF("Before resuming the child process where it left off and "
3968	    "without signal to be sent\n");
3969	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
3970
3971	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3972	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3973
3974	validate_status_signaled(status, SIGKILL, 0);
3975
3976	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3977	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3978}
3979
3980#if defined(PT_STEP)
3981ATF_TC(signal4);
3982ATF_TC_HEAD(signal4, tc)
3983{
3984	atf_tc_set_md_var(tc, "descr",
3985	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
3986	    "catching single step trap");
3987}
3988
3989ATF_TC_BODY(signal4, tc)
3990{
3991	const int exitval = 5;
3992	const int sigval = SIGSTOP;
3993	const int sigmasked = SIGTRAP;
3994	pid_t child, wpid;
3995#if defined(TWAIT_HAVE_STATUS)
3996	int status;
3997#endif
3998	sigset_t intmask;
3999	int happy;
4000
4001#if defined(__arm__)
4002	/* PT_STEP not supported on arm 32-bit */
4003	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
4004#endif
4005
4006	DPRINTF("Before forking process PID=%d\n", getpid());
4007	SYSCALL_REQUIRE((child = fork()) != -1);
4008	if (child == 0) {
4009		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4010		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4011
4012		happy = check_happy(100);
4013
4014		sigemptyset(&intmask);
4015		sigaddset(&intmask, sigmasked);
4016		sigprocmask(SIG_BLOCK, &intmask, NULL);
4017
4018		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4019		FORKEE_ASSERT(raise(sigval) == 0);
4020
4021		FORKEE_ASSERT_EQ(happy, check_happy(100));
4022
4023		DPRINTF("Before exiting of the child process\n");
4024		_exit(exitval);
4025	}
4026	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4027
4028	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4029	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4030
4031	validate_status_stopped(status, sigval);
4032
4033	DPRINTF("Before resuming the child process where it left off and "
4034	    "without signal to be sent\n");
4035	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
4036
4037	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4038	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4039
4040	validate_status_stopped(status, sigmasked);
4041
4042	DPRINTF("Before resuming the child process where it left off and "
4043	    "without signal to be sent\n");
4044	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4045
4046	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4047	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4048
4049	validate_status_exited(status, exitval);
4050
4051	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4052	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4053}
4054#endif
4055
4056ATF_TC(signal5);
4057ATF_TC_HEAD(signal5, tc)
4058{
4059	atf_tc_set_md_var(tc, "descr",
4060	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4061	    "catching exec() breakpoint");
4062}
4063
4064ATF_TC_BODY(signal5, tc)
4065{
4066	const int exitval = 5;
4067	const int sigval = SIGSTOP;
4068	const int sigmasked = SIGTRAP;
4069	pid_t child, wpid;
4070#if defined(TWAIT_HAVE_STATUS)
4071	int status;
4072#endif
4073	sigset_t intmask;
4074
4075	atf_tc_expect_fail("wrong signal");
4076
4077	DPRINTF("Before forking process PID=%d\n", getpid());
4078	SYSCALL_REQUIRE((child = fork()) != -1);
4079	if (child == 0) {
4080		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4081		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4082
4083		sigemptyset(&intmask);
4084		sigaddset(&intmask, sigmasked);
4085		sigprocmask(SIG_BLOCK, &intmask, NULL);
4086
4087		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4088		FORKEE_ASSERT(raise(sigval) == 0);
4089
4090		DPRINTF("Before calling execve(2) from child\n");
4091		execlp("/bin/echo", "/bin/echo", NULL);
4092
4093		DPRINTF("Before exiting of the child process\n");
4094		_exit(exitval);
4095	}
4096	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4097
4098	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4099	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4100
4101	validate_status_stopped(status, sigval);
4102
4103	DPRINTF("Before resuming the child process where it left off and "
4104	    "without signal to be sent\n");
4105	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4106
4107	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4108	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4109
4110	validate_status_stopped(status, sigmasked);
4111
4112	DPRINTF("Before resuming the child process where it left off and "
4113	    "without signal to be sent\n");
4114	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4115
4116	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4117	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4118
4119	validate_status_exited(status, exitval);
4120
4121	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4122	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4123}
4124
4125#if defined(TWAIT_HAVE_PID)
4126ATF_TC(signal6);
4127ATF_TC_HEAD(signal6, tc)
4128{
4129	atf_tc_set_md_var(tc, "timeout", "5");
4130	atf_tc_set_md_var(tc, "descr",
4131	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4132	    "catching PTRACE_FORK breakpoint");
4133}
4134
4135ATF_TC_BODY(signal6, tc)
4136{
4137	const int exitval = 5;
4138	const int exitval2 = 15;
4139	const int sigval = SIGSTOP;
4140	const int sigmasked = SIGTRAP;
4141	pid_t child, child2, wpid;
4142#if defined(TWAIT_HAVE_STATUS)
4143	int status;
4144#endif
4145	sigset_t intmask;
4146	ptrace_state_t state;
4147	const int slen = sizeof(state);
4148	ptrace_event_t event;
4149	const int elen = sizeof(event);
4150
4151	atf_tc_expect_fail("PR kern/51918");
4152
4153	DPRINTF("Before forking process PID=%d\n", getpid());
4154	SYSCALL_REQUIRE((child = fork()) != -1);
4155	if (child == 0) {
4156		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4157		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4158
4159		sigemptyset(&intmask);
4160		sigaddset(&intmask, sigmasked);
4161		sigprocmask(SIG_BLOCK, &intmask, NULL);
4162
4163		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4164		FORKEE_ASSERT(raise(sigval) == 0);
4165
4166		FORKEE_ASSERT((child2 = fork()) != -1);
4167
4168		if (child2 == 0)
4169			_exit(exitval2);
4170
4171		FORKEE_REQUIRE_SUCCESS
4172			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4173
4174		forkee_status_exited(status, exitval2);
4175
4176		DPRINTF("Before exiting of the child process\n");
4177		_exit(exitval);
4178	}
4179	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4180
4181	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4182	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4183
4184	validate_status_stopped(status, sigval);
4185
4186	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
4187	event.pe_set_event = PTRACE_FORK;
4188	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4189
4190	DPRINTF("Before resuming the child process where it left off and "
4191	    "without signal to be sent\n");
4192	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4193
4194	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4195	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4196
4197	validate_status_stopped(status, sigmasked);
4198
4199	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4200	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
4201
4202	child2 = state.pe_other_pid;
4203	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
4204
4205	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
4206	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4207	    child2);
4208
4209	validate_status_stopped(status, SIGTRAP);
4210
4211	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
4212	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
4213	ATF_REQUIRE_EQ(state.pe_other_pid, child);
4214
4215	DPRINTF("Before resuming the forkee process where it left off and "
4216	    "without signal to be sent\n");
4217	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
4218
4219	DPRINTF("Before resuming the child process where it left off and "
4220	    "without signal to be sent\n");
4221	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4222
4223	DPRINTF("Before calling %s() for the forkee - expected exited\n",
4224	    TWAIT_FNAME);
4225	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4226	    child2);
4227
4228	validate_status_exited(status, exitval2);
4229
4230	DPRINTF("Before calling %s() for the forkee - expected no process\n",
4231	    TWAIT_FNAME);
4232	TWAIT_REQUIRE_FAILURE(ECHILD,
4233	    wpid = TWAIT_GENERIC(child2, &status, 0));
4234
4235	DPRINTF("Before calling %s() for the child - expected stopped "
4236	    "SIGCHLD\n", TWAIT_FNAME);
4237	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4238
4239	validate_status_stopped(status, SIGCHLD);
4240
4241	DPRINTF("Before resuming the child process where it left off and "
4242	    "without signal to be sent\n");
4243	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4244
4245	DPRINTF("Before calling %s() for the child - expected exited\n",
4246	    TWAIT_FNAME);
4247	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4248
4249	validate_status_exited(status, exitval);
4250
4251	DPRINTF("Before calling %s() for the child - expected no process\n",
4252	    TWAIT_FNAME);
4253	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4254}
4255#endif
4256
4257#if defined(TWAIT_HAVE_PID)
4258ATF_TC(signal7);
4259ATF_TC_HEAD(signal7, tc)
4260{
4261	atf_tc_set_md_var(tc, "descr",
4262	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4263	    "catching PTRACE_VFORK breakpoint");
4264}
4265
4266ATF_TC_BODY(signal7, tc)
4267{
4268	const int exitval = 5;
4269	const int exitval2 = 15;
4270	const int sigval = SIGSTOP;
4271	const int sigmasked = SIGTRAP;
4272	pid_t child, child2, wpid;
4273#if defined(TWAIT_HAVE_STATUS)
4274	int status;
4275#endif
4276	sigset_t intmask;
4277	ptrace_state_t state;
4278	const int slen = sizeof(state);
4279	ptrace_event_t event;
4280	const int elen = sizeof(event);
4281
4282	atf_tc_expect_fail("PR kern/51918");
4283
4284	DPRINTF("Before forking process PID=%d\n", getpid());
4285	SYSCALL_REQUIRE((child = fork()) != -1);
4286	if (child == 0) {
4287		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4288		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4289
4290		sigemptyset(&intmask);
4291		sigaddset(&intmask, sigmasked);
4292		sigprocmask(SIG_BLOCK, &intmask, NULL);
4293
4294		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4295		FORKEE_ASSERT(raise(sigval) == 0);
4296
4297		FORKEE_ASSERT((child2 = fork()) != -1);
4298
4299		if (child2 == 0)
4300			_exit(exitval2);
4301
4302		FORKEE_REQUIRE_SUCCESS
4303			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4304
4305		forkee_status_exited(status, exitval2);
4306
4307		DPRINTF("Before exiting of the child process\n");
4308		_exit(exitval);
4309	}
4310	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4311
4312	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4313	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4314
4315	validate_status_stopped(status, sigval);
4316
4317	DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
4318	event.pe_set_event = PTRACE_VFORK;
4319	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP);
4320
4321	DPRINTF("Before resuming the child process where it left off and "
4322	    "without signal to be sent\n");
4323	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4324
4325	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4326	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4327
4328	validate_status_stopped(status, sigmasked);
4329
4330	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4331	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
4332
4333	child2 = state.pe_other_pid;
4334	DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2);
4335
4336	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
4337	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4338	    child2);
4339
4340	validate_status_stopped(status, SIGTRAP);
4341
4342	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
4343	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
4344	ATF_REQUIRE_EQ(state.pe_other_pid, child);
4345
4346	DPRINTF("Before resuming the forkee process where it left off and "
4347	    "without signal to be sent\n");
4348	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
4349
4350	DPRINTF("Before resuming the child process where it left off and "
4351	    "without signal to be sent\n");
4352	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4353
4354	DPRINTF("Before calling %s() for the forkee - expected exited\n",
4355	    TWAIT_FNAME);
4356	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
4357	    child2);
4358
4359	validate_status_exited(status, exitval2);
4360
4361	DPRINTF("Before calling %s() for the forkee - expected no process\n",
4362	    TWAIT_FNAME);
4363	TWAIT_REQUIRE_FAILURE(ECHILD,
4364	    wpid = TWAIT_GENERIC(child2, &status, 0));
4365
4366	DPRINTF("Before calling %s() for the child - expected stopped "
4367	    "SIGCHLD\n", TWAIT_FNAME);
4368	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4369
4370	validate_status_stopped(status, SIGCHLD);
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, 0) != -1);
4375
4376	DPRINTF("Before calling %s() for the child - expected exited\n",
4377	    TWAIT_FNAME);
4378	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4379
4380	validate_status_exited(status, exitval);
4381
4382	DPRINTF("Before calling %s() for the child - expected no process\n",
4383	    TWAIT_FNAME);
4384	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4385}
4386#endif
4387
4388ATF_TC(signal8);
4389ATF_TC_HEAD(signal8, tc)
4390{
4391	atf_tc_set_md_var(tc, "descr",
4392	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4393	    "catching PTRACE_VFORK_DONE breakpoint");
4394}
4395
4396ATF_TC_BODY(signal8, tc)
4397{
4398	const int exitval = 5;
4399	const int exitval2 = 15;
4400	const int sigval = SIGSTOP;
4401	const int sigmasked = SIGTRAP;
4402	pid_t child, child2, wpid;
4403#if defined(TWAIT_HAVE_STATUS)
4404	int status;
4405#endif
4406	sigset_t intmask;
4407	ptrace_state_t state;
4408	const int slen = sizeof(state);
4409	ptrace_event_t event;
4410	const int elen = sizeof(event);
4411
4412	atf_tc_expect_fail("PR kern/51918");
4413
4414	DPRINTF("Before forking process PID=%d\n", getpid());
4415	SYSCALL_REQUIRE((child = fork()) != -1);
4416	if (child == 0) {
4417		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4418		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4419
4420		sigemptyset(&intmask);
4421		sigaddset(&intmask, sigmasked);
4422		sigprocmask(SIG_BLOCK, &intmask, NULL);
4423
4424		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4425		FORKEE_ASSERT(raise(sigval) == 0);
4426
4427		FORKEE_ASSERT((child2 = vfork()) != -1);
4428
4429		if (child2 == 0)
4430			_exit(exitval2);
4431
4432		FORKEE_REQUIRE_SUCCESS
4433			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
4434
4435		forkee_status_exited(status, exitval2);
4436
4437		DPRINTF("Before exiting of the child process\n");
4438		_exit(exitval);
4439	}
4440	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4441
4442	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4443	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4444
4445	validate_status_stopped(status, sigval);
4446
4447	DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
4448	    child);
4449	event.pe_set_event = PTRACE_VFORK_DONE;
4450	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4451
4452	DPRINTF("Before resuming the child process where it left off and "
4453	    "without signal to be sent\n");
4454	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4455
4456	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4457	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4458
4459	validate_status_stopped(status, sigmasked);
4460
4461	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4462	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
4463
4464	child2 = state.pe_other_pid;
4465	DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
4466
4467	DPRINTF("Before resuming the child process where it left off and "
4468	    "without signal to be sent\n");
4469	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4470
4471	DPRINTF("Before calling %s() for the child - expected stopped "
4472	    "SIGCHLD\n", TWAIT_FNAME);
4473	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4474
4475	validate_status_stopped(status, SIGCHLD);
4476
4477	DPRINTF("Before resuming the child process where it left off and "
4478	    "without signal to be sent\n");
4479	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4480
4481	DPRINTF("Before calling %s() for the child - expected exited\n",
4482	    TWAIT_FNAME);
4483	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4484
4485	validate_status_exited(status, exitval);
4486
4487	DPRINTF("Before calling %s() for the child - expected no process\n",
4488	    TWAIT_FNAME);
4489	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4490}
4491
4492ATF_TC(signal9);
4493ATF_TC_HEAD(signal9, tc)
4494{
4495	atf_tc_set_md_var(tc, "descr",
4496	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4497	    "catching PTRACE_LWP_CREATE breakpoint");
4498}
4499
4500ATF_TC_BODY(signal9, tc)
4501{
4502	const int exitval = 5;
4503	const int sigval = SIGSTOP;
4504	const int sigmasked = SIGTRAP;
4505	pid_t child, wpid;
4506#if defined(TWAIT_HAVE_STATUS)
4507	int status;
4508#endif
4509	sigset_t intmask;
4510	ptrace_state_t state;
4511	const int slen = sizeof(state);
4512	ptrace_event_t event;
4513	const int elen = sizeof(event);
4514	ucontext_t uc;
4515	lwpid_t lid;
4516	static const size_t ssize = 16*1024;
4517	void *stack;
4518
4519	atf_tc_expect_fail("PR kern/51918");
4520
4521	DPRINTF("Before forking process PID=%d\n", getpid());
4522	SYSCALL_REQUIRE((child = fork()) != -1);
4523	if (child == 0) {
4524		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4525		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4526
4527		sigemptyset(&intmask);
4528		sigaddset(&intmask, sigmasked);
4529		sigprocmask(SIG_BLOCK, &intmask, NULL);
4530
4531		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4532		FORKEE_ASSERT(raise(sigval) == 0);
4533
4534		DPRINTF("Before allocating memory for stack in child\n");
4535		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4536
4537		DPRINTF("Before making context for new lwp in child\n");
4538		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
4539
4540		DPRINTF("Before creating new in child\n");
4541		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4542
4543		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4544		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4545
4546		DPRINTF("Before verifying that reported %d and running lid %d "
4547		    "are the same\n", lid, the_lwp_id);
4548		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4549
4550		DPRINTF("Before exiting of the child process\n");
4551		_exit(exitval);
4552	}
4553	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4554
4555	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4556	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4557
4558	validate_status_stopped(status, sigval);
4559
4560	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
4561	event.pe_set_event = PTRACE_LWP_CREATE;
4562	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4563
4564	DPRINTF("Before resuming the child process where it left off and "
4565	    "without signal to be sent\n");
4566	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4567
4568	DPRINTF("Before calling %s() for the child - expected stopped "
4569	    "SIGTRAP\n", TWAIT_FNAME);
4570	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4571
4572	validate_status_stopped(status, sigmasked);
4573
4574	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4575
4576	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
4577
4578	lid = state.pe_lwp;
4579	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
4580
4581	DPRINTF("Before resuming the child process where it left off and "
4582	    "without signal to be sent\n");
4583	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4584
4585	DPRINTF("Before calling %s() for the child - expected exited\n",
4586	    TWAIT_FNAME);
4587	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4588
4589	validate_status_exited(status, exitval);
4590
4591	DPRINTF("Before calling %s() for the child - expected no process\n",
4592	    TWAIT_FNAME);
4593	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4594}
4595
4596ATF_TC(signal10);
4597ATF_TC_HEAD(signal10, tc)
4598{
4599	atf_tc_set_md_var(tc, "descr",
4600	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
4601	    "catching PTRACE_LWP_EXIT breakpoint");
4602}
4603
4604ATF_TC_BODY(signal10, tc)
4605{
4606	const int exitval = 5;
4607	const int sigval = SIGSTOP;
4608	const int sigmasked = SIGTRAP;
4609	pid_t child, wpid;
4610#if defined(TWAIT_HAVE_STATUS)
4611	int status;
4612#endif
4613	sigset_t intmask;
4614	ptrace_state_t state;
4615	const int slen = sizeof(state);
4616	ptrace_event_t event;
4617	const int elen = sizeof(event);
4618	ucontext_t uc;
4619	lwpid_t lid;
4620	static const size_t ssize = 16*1024;
4621	void *stack;
4622
4623	atf_tc_expect_fail("PR kern/51918");
4624
4625	DPRINTF("Before forking process PID=%d\n", getpid());
4626	SYSCALL_REQUIRE((child = fork()) != -1);
4627	if (child == 0) {
4628		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4629		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4630
4631		sigemptyset(&intmask);
4632		sigaddset(&intmask, sigmasked);
4633		sigprocmask(SIG_BLOCK, &intmask, NULL);
4634
4635		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4636		FORKEE_ASSERT(raise(sigval) == 0);
4637
4638		DPRINTF("Before allocating memory for stack in child\n");
4639		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4640
4641		DPRINTF("Before making context for new lwp in child\n");
4642		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
4643
4644		DPRINTF("Before creating new in child\n");
4645		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4646
4647		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4648		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4649
4650		DPRINTF("Before verifying that reported %d and running lid %d "
4651		    "are the same\n", lid, the_lwp_id);
4652		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4653
4654		DPRINTF("Before exiting of the child process\n");
4655		_exit(exitval);
4656	}
4657	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4658
4659	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4660	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4661
4662	validate_status_stopped(status, sigval);
4663
4664	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
4665	event.pe_set_event = PTRACE_LWP_EXIT;
4666	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
4667
4668	DPRINTF("Before resuming the child process where it left off and "
4669	    "without signal to be sent\n");
4670	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4671
4672	DPRINTF("Before calling %s() for the child - expected stopped "
4673	    "SIGTRAP\n", TWAIT_FNAME);
4674	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4675
4676	validate_status_stopped(status, sigmasked);
4677
4678	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
4679
4680	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
4681
4682	lid = state.pe_lwp;
4683	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
4684
4685	DPRINTF("Before resuming the child process where it left off and "
4686	    "without signal to be sent\n");
4687	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4688
4689	DPRINTF("Before calling %s() for the child - expected exited\n",
4690	    TWAIT_FNAME);
4691	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4692
4693	validate_status_exited(status, exitval);
4694
4695	DPRINTF("Before calling %s() for the child - expected no process\n",
4696	    TWAIT_FNAME);
4697	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4698}
4699
4700static void
4701lwp_main_stop(void *arg)
4702{
4703	the_lwp_id = _lwp_self();
4704
4705	raise(SIGTRAP);
4706
4707	_lwp_exit();
4708}
4709
4710ATF_TC(suspend1);
4711ATF_TC_HEAD(suspend1, tc)
4712{
4713	atf_tc_set_md_var(tc, "descr",
4714	    "Verify that a thread can be suspended by a debugger and later "
4715	    "resumed by a tracee");
4716}
4717
4718ATF_TC_BODY(suspend1, tc)
4719{
4720	const int exitval = 5;
4721	const int sigval = SIGSTOP;
4722	pid_t child, wpid;
4723#if defined(TWAIT_HAVE_STATUS)
4724	int status;
4725#endif
4726	ucontext_t uc;
4727	lwpid_t lid;
4728	static const size_t ssize = 16*1024;
4729	void *stack;
4730	struct ptrace_lwpinfo pl;
4731	struct ptrace_siginfo psi;
4732	volatile int go = 0;
4733
4734	// Feature pending for refactoring
4735	atf_tc_expect_fail("PR kern/51995");
4736
4737	// Hangs with qemu
4738	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4739
4740	DPRINTF("Before forking process PID=%d\n", getpid());
4741	SYSCALL_REQUIRE((child = fork()) != -1);
4742	if (child == 0) {
4743		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4744		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4745
4746		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4747		FORKEE_ASSERT(raise(sigval) == 0);
4748
4749		DPRINTF("Before allocating memory for stack in child\n");
4750		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4751
4752		DPRINTF("Before making context for new lwp in child\n");
4753		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
4754
4755		DPRINTF("Before creating new in child\n");
4756		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4757
4758		while (go == 0)
4759			continue;
4760
4761		raise(SIGINT);
4762
4763		FORKEE_ASSERT(_lwp_continue(lid) == 0);
4764
4765		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4766		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4767
4768		DPRINTF("Before verifying that reported %d and running lid %d "
4769		    "are the same\n", lid, the_lwp_id);
4770		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4771
4772		DPRINTF("Before exiting of the child process\n");
4773		_exit(exitval);
4774	}
4775	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4776
4777	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4778	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4779
4780	validate_status_stopped(status, sigval);
4781
4782	DPRINTF("Before resuming the child process where it left off and "
4783	    "without signal to be sent\n");
4784	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4785
4786	DPRINTF("Before calling %s() for the child - expected stopped "
4787	    "SIGTRAP\n", TWAIT_FNAME);
4788	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4789
4790	validate_status_stopped(status, SIGTRAP);
4791
4792	DPRINTF("Before reading siginfo and lwpid_t\n");
4793	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
4794
4795	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
4796	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
4797
4798        DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
4799	    child, getpid());
4800	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
4801
4802	DPRINTF("Before resuming the child process where it left off and "
4803	    "without signal to be sent\n");
4804	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4805
4806	DPRINTF("Before calling %s() for the child - expected stopped "
4807	    "SIGINT\n", TWAIT_FNAME);
4808	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4809
4810	validate_status_stopped(status, SIGINT);
4811
4812	pl.pl_lwpid = 0;
4813
4814	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
4815	while (pl.pl_lwpid != 0) {
4816
4817		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
4818		switch (pl.pl_lwpid) {
4819		case 1:
4820			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
4821			break;
4822		case 2:
4823			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
4824			break;
4825		}
4826	}
4827
4828	DPRINTF("Before resuming the child process where it left off and "
4829	    "without signal to be sent\n");
4830	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4831
4832	DPRINTF("Before calling %s() for the child - expected exited\n",
4833	    TWAIT_FNAME);
4834	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4835
4836	validate_status_exited(status, exitval);
4837
4838	DPRINTF("Before calling %s() for the child - expected no process\n",
4839	    TWAIT_FNAME);
4840	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4841}
4842
4843ATF_TC(suspend2);
4844ATF_TC_HEAD(suspend2, tc)
4845{
4846	atf_tc_set_md_var(tc, "descr",
4847	    "Verify that the while the only thread within a process is "
4848	    "suspended, the whole process cannot be unstopped");
4849}
4850
4851ATF_TC_BODY(suspend2, tc)
4852{
4853	const int exitval = 5;
4854	const int sigval = SIGSTOP;
4855	pid_t child, wpid;
4856#if defined(TWAIT_HAVE_STATUS)
4857	int status;
4858#endif
4859	struct ptrace_siginfo psi;
4860
4861	// Feature pending for refactoring
4862	atf_tc_expect_fail("PR kern/51995");
4863
4864	// Hangs with qemu
4865	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4866
4867	DPRINTF("Before forking process PID=%d\n", getpid());
4868	SYSCALL_REQUIRE((child = fork()) != -1);
4869	if (child == 0) {
4870		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4871		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4872
4873		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4874		FORKEE_ASSERT(raise(sigval) == 0);
4875
4876		DPRINTF("Before exiting of the child process\n");
4877		_exit(exitval);
4878	}
4879	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4880
4881	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4882	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4883
4884	validate_status_stopped(status, sigval);
4885
4886	DPRINTF("Before reading siginfo and lwpid_t\n");
4887	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
4888
4889	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
4890	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
4891
4892	DPRINTF("Before resuming the child process where it left off and "
4893	    "without signal to be sent\n");
4894	ATF_REQUIRE_ERRNO(EDEADLK,
4895	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
4896
4897	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
4898	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
4899
4900	DPRINTF("Before resuming the child process where it left off and "
4901	    "without signal to be sent\n");
4902	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4903
4904	DPRINTF("Before calling %s() for the child - expected exited\n",
4905	    TWAIT_FNAME);
4906	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4907
4908	validate_status_exited(status, exitval);
4909
4910	DPRINTF("Before calling %s() for the child - expected no process\n",
4911	    TWAIT_FNAME);
4912	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4913}
4914
4915ATF_TC(resume1);
4916ATF_TC_HEAD(resume1, tc)
4917{
4918	atf_tc_set_md_var(tc, "timeout", "5");
4919	atf_tc_set_md_var(tc, "descr",
4920	    "Verify that a thread can be suspended by a debugger and later "
4921	    "resumed by the debugger");
4922}
4923
4924ATF_TC_BODY(resume1, tc)
4925{
4926	struct msg_fds fds;
4927	const int exitval = 5;
4928	const int sigval = SIGSTOP;
4929	pid_t child, wpid;
4930	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
4931#if defined(TWAIT_HAVE_STATUS)
4932	int status;
4933#endif
4934	ucontext_t uc;
4935	lwpid_t lid;
4936	static const size_t ssize = 16*1024;
4937	void *stack;
4938	struct ptrace_lwpinfo pl;
4939	struct ptrace_siginfo psi;
4940
4941	// Feature pending for refactoring
4942	atf_tc_expect_fail("PR kern/51995");
4943
4944	// Hangs with qemu
4945	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4946
4947	SYSCALL_REQUIRE(msg_open(&fds) == 0);
4948
4949	DPRINTF("Before forking process PID=%d\n", getpid());
4950	SYSCALL_REQUIRE((child = fork()) != -1);
4951	if (child == 0) {
4952		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4953		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4954
4955		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4956		FORKEE_ASSERT(raise(sigval) == 0);
4957
4958		DPRINTF("Before allocating memory for stack in child\n");
4959		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
4960
4961		DPRINTF("Before making context for new lwp in child\n");
4962		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
4963
4964		DPRINTF("Before creating new in child\n");
4965		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
4966
4967		CHILD_TO_PARENT("Message", fds, msg);
4968
4969		raise(SIGINT);
4970
4971		DPRINTF("Before waiting for lwp %d to exit\n", lid);
4972		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
4973
4974		DPRINTF("Before verifying that reported %d and running lid %d "
4975		    "are the same\n", lid, the_lwp_id);
4976		FORKEE_ASSERT_EQ(lid, the_lwp_id);
4977
4978		DPRINTF("Before exiting of the child process\n");
4979		_exit(exitval);
4980	}
4981	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4982
4983	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4984	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4985
4986	validate_status_stopped(status, sigval);
4987
4988	DPRINTF("Before resuming the child process where it left off and "
4989	    "without signal to be sent\n");
4990	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4991
4992	DPRINTF("Before calling %s() for the child - expected stopped "
4993	    "SIGTRAP\n", TWAIT_FNAME);
4994	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4995
4996	validate_status_stopped(status, SIGTRAP);
4997
4998	DPRINTF("Before reading siginfo and lwpid_t\n");
4999	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
5000
5001	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
5002	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
5003
5004	PARENT_FROM_CHILD("Message", fds, msg);
5005
5006	DPRINTF("Before resuming the child process where it left off and "
5007	    "without signal to be sent\n");
5008	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5009
5010	DPRINTF("Before calling %s() for the child - expected stopped "
5011	    "SIGINT\n", TWAIT_FNAME);
5012	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5013
5014	validate_status_stopped(status, SIGINT);
5015
5016	pl.pl_lwpid = 0;
5017
5018	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
5019	while (pl.pl_lwpid != 0) {
5020		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
5021		switch (pl.pl_lwpid) {
5022		case 1:
5023			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
5024			break;
5025		case 2:
5026			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
5027			break;
5028		}
5029	}
5030
5031	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
5032	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
5033
5034	DPRINTF("Before resuming the child process where it left off and "
5035	    "without signal to be sent\n");
5036	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5037
5038	DPRINTF("Before calling %s() for the child - expected exited\n",
5039	    TWAIT_FNAME);
5040	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5041
5042	validate_status_exited(status, exitval);
5043
5044	DPRINTF("Before calling %s() for the child - expected no process\n",
5045	    TWAIT_FNAME);
5046	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5047
5048	msg_close(&fds);
5049
5050	DPRINTF("XXX: Test worked this time but for consistency timeout it\n");
5051	sleep(10);
5052}
5053
5054ATF_TC(syscall1);
5055ATF_TC_HEAD(syscall1, tc)
5056{
5057	atf_tc_set_md_var(tc, "descr",
5058	    "Verify that getpid(2) can be traced with PT_SYSCALL");
5059}
5060
5061ATF_TC_BODY(syscall1, tc)
5062{
5063	const int exitval = 5;
5064	const int sigval = SIGSTOP;
5065	pid_t child, wpid;
5066#if defined(TWAIT_HAVE_STATUS)
5067	int status;
5068#endif
5069	struct ptrace_siginfo info;
5070	memset(&info, 0, sizeof(info));
5071
5072	DPRINTF("Before forking process PID=%d\n", getpid());
5073	SYSCALL_REQUIRE((child = fork()) != -1);
5074	if (child == 0) {
5075		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5076		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5077
5078		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5079		FORKEE_ASSERT(raise(sigval) == 0);
5080
5081		syscall(SYS_getpid);
5082
5083		DPRINTF("Before exiting of the child process\n");
5084		_exit(exitval);
5085	}
5086	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5087
5088	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5089	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5090
5091	validate_status_stopped(status, sigval);
5092
5093	DPRINTF("Before resuming the child process where it left off and "
5094	    "without signal to be sent\n");
5095	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5096
5097	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5098	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5099
5100	validate_status_stopped(status, SIGTRAP);
5101
5102	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5103	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5104
5105	DPRINTF("Before checking siginfo_t and lwpid\n");
5106	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
5107	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5108	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
5109
5110	DPRINTF("Before resuming the child process where it left off and "
5111	    "without signal to be sent\n");
5112	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5113
5114	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5115	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5116
5117	validate_status_stopped(status, SIGTRAP);
5118
5119	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5120	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5121
5122	DPRINTF("Before checking siginfo_t and lwpid\n");
5123	ATF_REQUIRE_EQ(info.psi_lwpid, 1);
5124	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5125	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
5126
5127	DPRINTF("Before resuming the child process where it left off and "
5128	    "without signal to be sent\n");
5129	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5130
5131	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5132	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5133
5134	validate_status_exited(status, exitval);
5135
5136	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5137	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5138}
5139
5140ATF_TC(syscallemu1);
5141ATF_TC_HEAD(syscallemu1, tc)
5142{
5143	atf_tc_set_md_var(tc, "descr",
5144	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
5145}
5146
5147ATF_TC_BODY(syscallemu1, tc)
5148{
5149	const int exitval = 5;
5150	const int sigval = SIGSTOP;
5151	pid_t child, wpid;
5152#if defined(TWAIT_HAVE_STATUS)
5153	int status;
5154#endif
5155
5156#if defined(__sparc__) && !defined(__sparc64__)
5157	/* syscallemu does not work on sparc (32-bit) */
5158	atf_tc_expect_fail("PR kern/52166");
5159#endif
5160
5161	DPRINTF("Before forking process PID=%d\n", getpid());
5162	SYSCALL_REQUIRE((child = fork()) != -1);
5163	if (child == 0) {
5164		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5165		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5166
5167		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5168		FORKEE_ASSERT(raise(sigval) == 0);
5169
5170		syscall(SYS_exit, 100);
5171
5172		DPRINTF("Before exiting of the child process\n");
5173		_exit(exitval);
5174	}
5175	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5176
5177	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5178	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5179
5180	validate_status_stopped(status, sigval);
5181
5182	DPRINTF("Before resuming the child process where it left off and "
5183	    "without signal to be sent\n");
5184	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5185
5186	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5187	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5188
5189	validate_status_stopped(status, SIGTRAP);
5190
5191	DPRINTF("Set SYSCALLEMU for intercepted syscall\n");
5192	SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
5193
5194	DPRINTF("Before resuming the child process where it left off and "
5195	    "without signal to be sent\n");
5196	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
5197
5198	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5199	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5200
5201	validate_status_stopped(status, SIGTRAP);
5202
5203	DPRINTF("Before resuming the child process where it left off and "
5204	    "without signal to be sent\n");
5205	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5206
5207	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5208	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5209
5210	validate_status_exited(status, exitval);
5211
5212	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5213	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5214}
5215
5216#include "t_ptrace_amd64_wait.h"
5217#include "t_ptrace_i386_wait.h"
5218#include "t_ptrace_x86_wait.h"
5219
5220ATF_TP_ADD_TCS(tp)
5221{
5222	setvbuf(stdout, NULL, _IONBF, 0);
5223	setvbuf(stderr, NULL, _IONBF, 0);
5224
5225	ATF_TP_ADD_TC(tp, traceme_raise1);
5226	ATF_TP_ADD_TC(tp, traceme_raise2);
5227	ATF_TP_ADD_TC(tp, traceme_raise3);
5228	ATF_TP_ADD_TC(tp, traceme_raise4);
5229	ATF_TP_ADD_TC(tp, traceme_raise5);
5230
5231	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle1);
5232	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle2);
5233	ATF_TP_ADD_TC(tp, traceme_sendsignal_handle3);
5234
5235	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked1);
5236	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked2);
5237	ATF_TP_ADD_TC(tp, traceme_sendsignal_masked3);
5238
5239	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored1);
5240	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored2);
5241	ATF_TP_ADD_TC(tp, traceme_sendsignal_ignored3);
5242
5243	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple1);
5244	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple2);
5245	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple3);
5246	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple4);
5247	ATF_TP_ADD_TC(tp, traceme_sendsignal_simple5);
5248
5249	ATF_TP_ADD_TC(tp, traceme_pid1_parent);
5250
5251	ATF_TP_ADD_TC(tp, traceme_vfork_raise1);
5252	ATF_TP_ADD_TC(tp, traceme_vfork_raise2);
5253	ATF_TP_ADD_TC(tp, traceme_vfork_raise3);
5254	ATF_TP_ADD_TC(tp, traceme_vfork_raise4);
5255	ATF_TP_ADD_TC(tp, traceme_vfork_raise5);
5256	ATF_TP_ADD_TC(tp, traceme_vfork_raise6);
5257	ATF_TP_ADD_TC(tp, traceme_vfork_raise7);
5258	ATF_TP_ADD_TC(tp, traceme_vfork_raise8);
5259
5260	ATF_TP_ADD_TC(tp, traceme_vfork_crash_trap);
5261	ATF_TP_ADD_TC(tp, traceme_vfork_crash_segv);
5262//	ATF_TP_ADD_TC(tp, traceme_vfork_crash_ill);
5263	ATF_TP_ADD_TC(tp, traceme_vfork_crash_fpe);
5264	ATF_TP_ADD_TC(tp, traceme_vfork_crash_bus);
5265
5266	ATF_TP_ADD_TC(tp, traceme_vfork_exec);
5267
5268	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sees_terminaton_before_the_parent);
5269	ATF_TP_ADD_TC_HAVE_PID(tp, tracer_sysctl_lookup_without_duplicates);
5270	ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_sees_terminaton_before_the_parent);
5271
5272	ATF_TP_ADD_TC(tp, parent_attach_to_its_child);
5273
5274	ATF_TP_ADD_TC(tp, child_attach_to_its_parent);
5275
5276	ATF_TP_ADD_TC_HAVE_PID(tp,
5277		tracee_sees_its_original_parent_getppid);
5278	ATF_TP_ADD_TC_HAVE_PID(tp,
5279		tracee_sees_its_original_parent_sysctl_kinfo_proc2);
5280	ATF_TP_ADD_TC_HAVE_PID(tp,
5281		tracee_sees_its_original_parent_procfs_status);
5282
5283	ATF_TP_ADD_TC(tp, eventmask_preserved_empty);
5284	ATF_TP_ADD_TC(tp, eventmask_preserved_fork);
5285	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork);
5286	ATF_TP_ADD_TC(tp, eventmask_preserved_vfork_done);
5287	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_create);
5288	ATF_TP_ADD_TC(tp, eventmask_preserved_lwp_exit);
5289
5290	ATF_TP_ADD_TC(tp, fork1);
5291	ATF_TP_ADD_TC_HAVE_PID(tp, fork2);
5292	ATF_TP_ADD_TC_HAVE_PID(tp, fork3);
5293	ATF_TP_ADD_TC_HAVE_PID(tp, fork4);
5294	ATF_TP_ADD_TC(tp, fork5);
5295	ATF_TP_ADD_TC_HAVE_PID(tp, fork6);
5296	ATF_TP_ADD_TC_HAVE_PID(tp, fork7);
5297	ATF_TP_ADD_TC_HAVE_PID(tp, fork8);
5298
5299	ATF_TP_ADD_TC(tp, vfork1);
5300	ATF_TP_ADD_TC_HAVE_PID(tp, vfork2);
5301	ATF_TP_ADD_TC_HAVE_PID(tp, vfork3);
5302	ATF_TP_ADD_TC_HAVE_PID(tp, vfork4);
5303	ATF_TP_ADD_TC(tp, vfork5);
5304	ATF_TP_ADD_TC_HAVE_PID(tp, vfork6);
5305	ATF_TP_ADD_TC_HAVE_PID(tp, vfork7);
5306	ATF_TP_ADD_TC_HAVE_PID(tp, vfork8);
5307
5308	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8);
5309	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16);
5310	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32);
5311	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64);
5312
5313	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8);
5314	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16);
5315	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32);
5316	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64);
5317
5318	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8);
5319	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16);
5320	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32);
5321	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64);
5322
5323	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8);
5324	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16);
5325	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32);
5326	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64);
5327
5328	ATF_TP_ADD_TC(tp, bytes_transfer_read_d);
5329	ATF_TP_ADD_TC(tp, bytes_transfer_read_i);
5330	ATF_TP_ADD_TC(tp, bytes_transfer_write_d);
5331	ATF_TP_ADD_TC(tp, bytes_transfer_write_i);
5332
5333	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_8_text);
5334	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_16_text);
5335	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_32_text);
5336	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_d_64_text);
5337
5338	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_8_text);
5339	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_16_text);
5340	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_32_text);
5341	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_i_64_text);
5342
5343	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_8_text);
5344	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_16_text);
5345	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_32_text);
5346	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_d_64_text);
5347
5348	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_8_text);
5349	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_16_text);
5350	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_32_text);
5351	ATF_TP_ADD_TC(tp, bytes_transfer_piod_write_i_64_text);
5352
5353	ATF_TP_ADD_TC(tp, bytes_transfer_read_d_text);
5354	ATF_TP_ADD_TC(tp, bytes_transfer_read_i_text);
5355	ATF_TP_ADD_TC(tp, bytes_transfer_write_d_text);
5356	ATF_TP_ADD_TC(tp, bytes_transfer_write_i_text);
5357
5358	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_auxv);
5359
5360	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
5361	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
5362	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
5363	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
5364	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
5365
5366	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
5367	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
5368
5369	ATF_TP_ADD_TC_PT_STEP(tp, step1);
5370	ATF_TP_ADD_TC_PT_STEP(tp, step2);
5371	ATF_TP_ADD_TC_PT_STEP(tp, step3);
5372	ATF_TP_ADD_TC_PT_STEP(tp, step4);
5373
5374	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
5375	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
5376	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
5377	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
5378
5379	ATF_TP_ADD_TC(tp, kill1);
5380	ATF_TP_ADD_TC(tp, kill2);
5381
5382	ATF_TP_ADD_TC(tp, lwpinfo1);
5383	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
5384
5385	ATF_TP_ADD_TC(tp, siginfo1);
5386	ATF_TP_ADD_TC(tp, siginfo2);
5387	ATF_TP_ADD_TC(tp, siginfo3);
5388	ATF_TP_ADD_TC(tp, siginfo4);
5389	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
5390	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
5391
5392	ATF_TP_ADD_TC(tp, lwp_create1);
5393
5394	ATF_TP_ADD_TC(tp, lwp_exit1);
5395
5396	ATF_TP_ADD_TC(tp, signal1);
5397	ATF_TP_ADD_TC(tp, signal2);
5398	ATF_TP_ADD_TC(tp, signal3);
5399	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
5400	ATF_TP_ADD_TC(tp, signal5);
5401	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
5402	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
5403	ATF_TP_ADD_TC(tp, signal8);
5404	ATF_TP_ADD_TC(tp, signal9);
5405	ATF_TP_ADD_TC(tp, signal10);
5406
5407	ATF_TP_ADD_TC(tp, suspend1);
5408	ATF_TP_ADD_TC(tp, suspend2);
5409
5410	ATF_TP_ADD_TC(tp, resume1);
5411
5412	ATF_TP_ADD_TC(tp, syscall1);
5413
5414	ATF_TP_ADD_TC(tp, syscallemu1);
5415
5416	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
5417	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
5418	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
5419
5420	return atf_no_error();
5421}
5422