ptrace_test.c revision 367457
1/*-
2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: stable/11/tests/sys/kern/ptrace_test.c 367457 2020-11-07 18:10:59Z dim $");
28
29#include <sys/types.h>
30#include <sys/cpuset.h>
31#include <sys/event.h>
32#include <sys/file.h>
33#include <sys/time.h>
34#include <sys/procctl.h>
35#define	_WANT_MIPS_REGNUM
36#include <sys/ptrace.h>
37#include <sys/queue.h>
38#include <sys/runq.h>
39#include <sys/syscall.h>
40#include <sys/sysctl.h>
41#include <sys/user.h>
42#include <sys/wait.h>
43#include <errno.h>
44#include <machine/cpufunc.h>
45#include <pthread.h>
46#include <sched.h>
47#include <semaphore.h>
48#include <signal.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <atf-c.h>
53
54/*
55 * Architectures with a user-visible breakpoint().
56 */
57#if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) ||	\
58    defined(__i386__) || defined(__mips__) || defined(__riscv) ||	\
59    defined(__sparc64__)
60#define	HAVE_BREAKPOINT
61#endif
62
63/*
64 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
65 */
66#ifdef HAVE_BREAKPOINT
67#if defined(__aarch64__)
68#define	SKIP_BREAK(reg)	((reg)->elr += 4)
69#elif defined(__amd64__) || defined(__i386__)
70#define	SKIP_BREAK(reg)
71#elif defined(__arm__)
72#define	SKIP_BREAK(reg)	((reg)->r_pc += 4)
73#elif defined(__mips__)
74#define	SKIP_BREAK(reg)	((reg)->r_regs[PC] += 4)
75#elif defined(__riscv)
76#define	SKIP_BREAK(reg)	((reg)->sepc += 4)
77#elif defined(__sparc64__)
78#define	SKIP_BREAK(reg)	do {						\
79	(reg)->r_tpc = (reg)->r_tnpc + 4;				\
80	(reg)->r_tnpc += 8;						\
81} while (0)
82#endif
83#endif
84
85/*
86 * A variant of ATF_REQUIRE that is suitable for use in child
87 * processes.  This only works if the parent process is tripped up by
88 * the early exit and fails some requirement itself.
89 */
90#define	CHILD_REQUIRE(exp) do {						\
91		if (!(exp))						\
92			child_fail_require(__FILE__, __LINE__,		\
93			    #exp " not met");				\
94	} while (0)
95
96static __dead2 void
97child_fail_require(const char *file, int line, const char *str)
98{
99	char buf[128];
100
101	snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
102	write(2, buf, strlen(buf));
103	_exit(32);
104}
105
106static void
107trace_me(void)
108{
109
110	/* Attach the parent process as a tracer of this process. */
111	CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
112
113	/* Trigger a stop. */
114	raise(SIGSTOP);
115}
116
117static void
118attach_child(pid_t pid)
119{
120	pid_t wpid;
121	int status;
122
123	ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
124
125	wpid = waitpid(pid, &status, 0);
126	ATF_REQUIRE(wpid == pid);
127	ATF_REQUIRE(WIFSTOPPED(status));
128	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
129}
130
131static void
132wait_for_zombie(pid_t pid)
133{
134
135	/*
136	 * Wait for a process to exit.  This is kind of gross, but
137	 * there is not a better way.
138	 *
139	 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
140	 * with ESRCH.  After that change, a valid struct kinfo_proc
141	 * is returned for zombies with ki_stat set to SZOMB.
142	 */
143	for (;;) {
144		struct kinfo_proc kp;
145		size_t len;
146		int mib[4];
147
148		mib[0] = CTL_KERN;
149		mib[1] = KERN_PROC;
150		mib[2] = KERN_PROC_PID;
151		mib[3] = pid;
152		len = sizeof(kp);
153		if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
154			ATF_REQUIRE(errno == ESRCH);
155			break;
156		}
157		if (kp.ki_stat == SZOMB)
158			break;
159		usleep(5000);
160	}
161}
162
163/*
164 * Verify that a parent debugger process "sees" the exit of a debugged
165 * process exactly once when attached via PT_TRACE_ME.
166 */
167ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
168ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
169{
170	pid_t child, wpid;
171	int status;
172
173	ATF_REQUIRE((child = fork()) != -1);
174	if (child == 0) {
175		/* Child process. */
176		trace_me();
177
178		_exit(1);
179	}
180
181	/* Parent process. */
182
183	/* The first wait() should report the stop from SIGSTOP. */
184	wpid = waitpid(child, &status, 0);
185	ATF_REQUIRE(wpid == child);
186	ATF_REQUIRE(WIFSTOPPED(status));
187	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
188
189	/* Continue the child ignoring the SIGSTOP. */
190	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
191
192	/* The second wait() should report the exit status. */
193	wpid = waitpid(child, &status, 0);
194	ATF_REQUIRE(wpid == child);
195	ATF_REQUIRE(WIFEXITED(status));
196	ATF_REQUIRE(WEXITSTATUS(status) == 1);
197
198	/* The child should no longer exist. */
199	wpid = waitpid(child, &status, 0);
200	ATF_REQUIRE(wpid == -1);
201	ATF_REQUIRE(errno == ECHILD);
202}
203
204/*
205 * Verify that a parent debugger process "sees" the exit of a debugged
206 * process exactly once when attached via PT_ATTACH.
207 */
208ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
209ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
210{
211	pid_t child, wpid;
212	int cpipe[2], status;
213	char c;
214
215	ATF_REQUIRE(pipe(cpipe) == 0);
216	ATF_REQUIRE((child = fork()) != -1);
217	if (child == 0) {
218		/* Child process. */
219		close(cpipe[0]);
220
221		/* Wait for the parent to attach. */
222		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
223
224		_exit(1);
225	}
226	close(cpipe[1]);
227
228	/* Parent process. */
229
230	/* Attach to the child process. */
231	attach_child(child);
232
233	/* Continue the child ignoring the SIGSTOP. */
234	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
235
236	/* Signal the child to exit. */
237	close(cpipe[0]);
238
239	/* The second wait() should report the exit status. */
240	wpid = waitpid(child, &status, 0);
241	ATF_REQUIRE(wpid == child);
242	ATF_REQUIRE(WIFEXITED(status));
243	ATF_REQUIRE(WEXITSTATUS(status) == 1);
244
245	/* The child should no longer exist. */
246	wpid = waitpid(child, &status, 0);
247	ATF_REQUIRE(wpid == -1);
248	ATF_REQUIRE(errno == ECHILD);
249}
250
251/*
252 * Verify that a parent process "sees" the exit of a debugged process only
253 * after the debugger has seen it.
254 */
255ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
256ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
257{
258	pid_t child, debugger, wpid;
259	int cpipe[2], dpipe[2], status;
260	char c;
261
262	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
263		atf_tc_skip("https://bugs.freebsd.org/239399");
264
265	ATF_REQUIRE(pipe(cpipe) == 0);
266	ATF_REQUIRE((child = fork()) != -1);
267
268	if (child == 0) {
269		/* Child process. */
270		close(cpipe[0]);
271
272		/* Wait for parent to be ready. */
273		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
274
275		_exit(1);
276	}
277	close(cpipe[1]);
278
279	ATF_REQUIRE(pipe(dpipe) == 0);
280	ATF_REQUIRE((debugger = fork()) != -1);
281
282	if (debugger == 0) {
283		/* Debugger process. */
284		close(dpipe[0]);
285
286		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
287
288		wpid = waitpid(child, &status, 0);
289		CHILD_REQUIRE(wpid == child);
290		CHILD_REQUIRE(WIFSTOPPED(status));
291		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
292
293		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
294
295		/* Signal parent that debugger is attached. */
296		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
297
298		/* Wait for parent's failed wait. */
299		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
300
301		wpid = waitpid(child, &status, 0);
302		CHILD_REQUIRE(wpid == child);
303		CHILD_REQUIRE(WIFEXITED(status));
304		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
305
306		_exit(0);
307	}
308	close(dpipe[1]);
309
310	/* Parent process. */
311
312	/* Wait for the debugger to attach to the child. */
313	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
314
315	/* Release the child. */
316	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
317	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
318	close(cpipe[0]);
319
320	wait_for_zombie(child);
321
322	/*
323	 * This wait should return a pid of 0 to indicate no status to
324	 * report.  The parent should see the child as non-exited
325	 * until the debugger sees the exit.
326	 */
327	wpid = waitpid(child, &status, WNOHANG);
328	ATF_REQUIRE(wpid == 0);
329
330	/* Signal the debugger to wait for the child. */
331	close(dpipe[0]);
332
333	/* Wait for the debugger. */
334	wpid = waitpid(debugger, &status, 0);
335	ATF_REQUIRE(wpid == debugger);
336	ATF_REQUIRE(WIFEXITED(status));
337	ATF_REQUIRE(WEXITSTATUS(status) == 0);
338
339	/* The child process should now be ready. */
340	wpid = waitpid(child, &status, WNOHANG);
341	ATF_REQUIRE(wpid == child);
342	ATF_REQUIRE(WIFEXITED(status));
343	ATF_REQUIRE(WEXITSTATUS(status) == 1);
344}
345
346/*
347 * Verify that a parent process "sees" the exit of a debugged process
348 * only after a non-direct-child debugger has seen it.  In particular,
349 * various wait() calls in the parent must avoid failing with ESRCH by
350 * checking the parent's orphan list for the debugee.
351 */
352ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
353ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
354{
355	pid_t child, debugger, fpid, wpid;
356	int cpipe[2], dpipe[2], status;
357	char c;
358
359	ATF_REQUIRE(pipe(cpipe) == 0);
360	ATF_REQUIRE((child = fork()) != -1);
361
362	if (child == 0) {
363		/* Child process. */
364		close(cpipe[0]);
365
366		/* Wait for parent to be ready. */
367		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
368
369		_exit(1);
370	}
371	close(cpipe[1]);
372
373	ATF_REQUIRE(pipe(dpipe) == 0);
374	ATF_REQUIRE((debugger = fork()) != -1);
375
376	if (debugger == 0) {
377		/* Debugger parent. */
378
379		/*
380		 * Fork again and drop the debugger parent so that the
381		 * debugger is not a child of the main parent.
382		 */
383		CHILD_REQUIRE((fpid = fork()) != -1);
384		if (fpid != 0)
385			_exit(2);
386
387		/* Debugger process. */
388		close(dpipe[0]);
389
390		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
391
392		wpid = waitpid(child, &status, 0);
393		CHILD_REQUIRE(wpid == child);
394		CHILD_REQUIRE(WIFSTOPPED(status));
395		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
396
397		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
398
399		/* Signal parent that debugger is attached. */
400		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
401
402		/* Wait for parent's failed wait. */
403		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
404
405		wpid = waitpid(child, &status, 0);
406		CHILD_REQUIRE(wpid == child);
407		CHILD_REQUIRE(WIFEXITED(status));
408		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
409
410		_exit(0);
411	}
412	close(dpipe[1]);
413
414	/* Parent process. */
415
416	/* Wait for the debugger parent process to exit. */
417	wpid = waitpid(debugger, &status, 0);
418	ATF_REQUIRE(wpid == debugger);
419	ATF_REQUIRE(WIFEXITED(status));
420	ATF_REQUIRE(WEXITSTATUS(status) == 2);
421
422	/* A WNOHANG wait here should see the non-exited child. */
423	wpid = waitpid(child, &status, WNOHANG);
424	ATF_REQUIRE(wpid == 0);
425
426	/* Wait for the debugger to attach to the child. */
427	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
428
429	/* Release the child. */
430	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
431	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
432	close(cpipe[0]);
433
434	wait_for_zombie(child);
435
436	/*
437	 * This wait should return a pid of 0 to indicate no status to
438	 * report.  The parent should see the child as non-exited
439	 * until the debugger sees the exit.
440	 */
441	wpid = waitpid(child, &status, WNOHANG);
442	ATF_REQUIRE(wpid == 0);
443
444	/* Signal the debugger to wait for the child. */
445	ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
446
447	/* Wait for the debugger. */
448	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
449	close(dpipe[0]);
450
451	/* The child process should now be ready. */
452	wpid = waitpid(child, &status, WNOHANG);
453	ATF_REQUIRE(wpid == child);
454	ATF_REQUIRE(WIFEXITED(status));
455	ATF_REQUIRE(WEXITSTATUS(status) == 1);
456}
457
458/*
459 * Make sure that we can collect the exit status of an orphaned process.
460 */
461ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
462ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
463{
464	ssize_t n;
465	int cpipe1[2], cpipe2[2], gcpipe[2], status;
466	pid_t child, gchild;
467
468	ATF_REQUIRE(pipe(cpipe1) == 0);
469	ATF_REQUIRE(pipe(cpipe2) == 0);
470	ATF_REQUIRE(pipe(gcpipe) == 0);
471
472	ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
473
474	ATF_REQUIRE((child = fork()) != -1);
475	if (child == 0) {
476		CHILD_REQUIRE((gchild = fork()) != -1);
477		if (gchild == 0) {
478			status = 1;
479			do {
480				n = read(gcpipe[0], &status, sizeof(status));
481			} while (n == -1 && errno == EINTR);
482			_exit(status);
483		}
484
485		CHILD_REQUIRE(write(cpipe1[1], &gchild, sizeof(gchild)) ==
486		    sizeof(gchild));
487		CHILD_REQUIRE(read(cpipe2[0], &status, sizeof(status)) ==
488		    sizeof(status));
489		_exit(status);
490	}
491
492	ATF_REQUIRE(read(cpipe1[0], &gchild, sizeof(gchild)) == sizeof(gchild));
493
494	ATF_REQUIRE(ptrace(PT_ATTACH, gchild, NULL, 0) == 0);
495
496	status = 0;
497	ATF_REQUIRE(write(cpipe2[1], &status, sizeof(status)) ==
498	    sizeof(status));
499	ATF_REQUIRE(waitpid(child, &status, 0) == child);
500	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
501
502	status = 0;
503	ATF_REQUIRE(write(gcpipe[1], &status, sizeof(status)) ==
504	    sizeof(status));
505	ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
506	ATF_REQUIRE(WIFSTOPPED(status));
507	ATF_REQUIRE(ptrace(PT_DETACH, gchild, (caddr_t)1, 0) == 0);
508	ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
509	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
510
511	ATF_REQUIRE(close(cpipe1[0]) == 0);
512	ATF_REQUIRE(close(cpipe1[1]) == 0);
513	ATF_REQUIRE(close(cpipe2[0]) == 0);
514	ATF_REQUIRE(close(cpipe2[1]) == 0);
515	ATF_REQUIRE(close(gcpipe[0]) == 0);
516	ATF_REQUIRE(close(gcpipe[1]) == 0);
517}
518
519/*
520 * The parent process should always act the same regardless of how the
521 * debugger is attached to it.
522 */
523static __dead2 void
524follow_fork_parent(bool use_vfork)
525{
526	pid_t fpid, wpid;
527	int status;
528
529	if (use_vfork)
530		CHILD_REQUIRE((fpid = vfork()) != -1);
531	else
532		CHILD_REQUIRE((fpid = fork()) != -1);
533
534	if (fpid == 0)
535		/* Child */
536		_exit(2);
537
538	wpid = waitpid(fpid, &status, 0);
539	CHILD_REQUIRE(wpid == fpid);
540	CHILD_REQUIRE(WIFEXITED(status));
541	CHILD_REQUIRE(WEXITSTATUS(status) == 2);
542
543	_exit(1);
544}
545
546/*
547 * Helper routine for follow fork tests.  This waits for two stops
548 * that report both "sides" of a fork.  It returns the pid of the new
549 * child process.
550 */
551static pid_t
552handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
553{
554	struct ptrace_lwpinfo pl;
555	bool fork_reported[2];
556	pid_t child, wpid;
557	int i, status;
558
559	fork_reported[0] = false;
560	fork_reported[1] = false;
561	child = -1;
562
563	/*
564	 * Each process should report a fork event.  The parent should
565	 * report a PL_FLAG_FORKED event, and the child should report
566	 * a PL_FLAG_CHILD event.
567	 */
568	for (i = 0; i < 2; i++) {
569		wpid = wait(&status);
570		ATF_REQUIRE(wpid > 0);
571		ATF_REQUIRE(WIFSTOPPED(status));
572
573		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
574		    sizeof(pl)) != -1);
575		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
576		    0);
577		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
578		    (PL_FLAG_FORKED | PL_FLAG_CHILD));
579		if (pl.pl_flags & PL_FLAG_CHILD) {
580			ATF_REQUIRE(wpid != parent);
581			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
582			ATF_REQUIRE(!fork_reported[1]);
583			if (child == -1)
584				child = wpid;
585			else
586				ATF_REQUIRE(child == wpid);
587			if (ppl != NULL)
588				ppl[1] = pl;
589			fork_reported[1] = true;
590		} else {
591			ATF_REQUIRE(wpid == parent);
592			ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
593			ATF_REQUIRE(!fork_reported[0]);
594			if (child == -1)
595				child = pl.pl_child_pid;
596			else
597				ATF_REQUIRE(child == pl.pl_child_pid);
598			if (ppl != NULL)
599				ppl[0] = pl;
600			fork_reported[0] = true;
601		}
602	}
603
604	return (child);
605}
606
607/*
608 * Verify that a new child process is stopped after a followed fork and
609 * that the traced parent sees the exit of the child after the debugger
610 * when both processes remain attached to the debugger.
611 */
612ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
613ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
614{
615	pid_t children[2], fpid, wpid;
616	int status;
617
618	ATF_REQUIRE((fpid = fork()) != -1);
619	if (fpid == 0) {
620		trace_me();
621		follow_fork_parent(false);
622	}
623
624	/* Parent process. */
625	children[0] = fpid;
626
627	/* The first wait() should report the stop from SIGSTOP. */
628	wpid = waitpid(children[0], &status, 0);
629	ATF_REQUIRE(wpid == children[0]);
630	ATF_REQUIRE(WIFSTOPPED(status));
631	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
632
633	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
634
635	/* Continue the child ignoring the SIGSTOP. */
636	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
637
638	children[1] = handle_fork_events(children[0], NULL);
639	ATF_REQUIRE(children[1] > 0);
640
641	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
642	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
643
644	/*
645	 * The child can't exit until the grandchild reports status, so the
646	 * grandchild should report its exit first to the debugger.
647	 */
648	wpid = wait(&status);
649	ATF_REQUIRE(wpid == children[1]);
650	ATF_REQUIRE(WIFEXITED(status));
651	ATF_REQUIRE(WEXITSTATUS(status) == 2);
652
653	wpid = wait(&status);
654	ATF_REQUIRE(wpid == children[0]);
655	ATF_REQUIRE(WIFEXITED(status));
656	ATF_REQUIRE(WEXITSTATUS(status) == 1);
657
658	wpid = wait(&status);
659	ATF_REQUIRE(wpid == -1);
660	ATF_REQUIRE(errno == ECHILD);
661}
662
663/*
664 * Verify that a new child process is stopped after a followed fork
665 * and that the traced parent sees the exit of the child when the new
666 * child process is detached after it reports its fork.
667 */
668ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
669ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
670{
671	pid_t children[2], fpid, wpid;
672	int status;
673
674	ATF_REQUIRE((fpid = fork()) != -1);
675	if (fpid == 0) {
676		trace_me();
677		follow_fork_parent(false);
678	}
679
680	/* Parent process. */
681	children[0] = fpid;
682
683	/* The first wait() should report the stop from SIGSTOP. */
684	wpid = waitpid(children[0], &status, 0);
685	ATF_REQUIRE(wpid == children[0]);
686	ATF_REQUIRE(WIFSTOPPED(status));
687	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
688
689	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
690
691	/* Continue the child ignoring the SIGSTOP. */
692	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
693
694	children[1] = handle_fork_events(children[0], NULL);
695	ATF_REQUIRE(children[1] > 0);
696
697	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
698	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
699
700	/*
701	 * Should not see any status from the grandchild now, only the
702	 * child.
703	 */
704	wpid = wait(&status);
705	ATF_REQUIRE(wpid == children[0]);
706	ATF_REQUIRE(WIFEXITED(status));
707	ATF_REQUIRE(WEXITSTATUS(status) == 1);
708
709	wpid = wait(&status);
710	ATF_REQUIRE(wpid == -1);
711	ATF_REQUIRE(errno == ECHILD);
712}
713
714/*
715 * Verify that a new child process is stopped after a followed fork
716 * and that the traced parent sees the exit of the child when the
717 * traced parent is detached after the fork.
718 */
719ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
720ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
721{
722	pid_t children[2], fpid, wpid;
723	int status;
724
725	ATF_REQUIRE((fpid = fork()) != -1);
726	if (fpid == 0) {
727		trace_me();
728		follow_fork_parent(false);
729	}
730
731	/* Parent process. */
732	children[0] = fpid;
733
734	/* The first wait() should report the stop from SIGSTOP. */
735	wpid = waitpid(children[0], &status, 0);
736	ATF_REQUIRE(wpid == children[0]);
737	ATF_REQUIRE(WIFSTOPPED(status));
738	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
739
740	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
741
742	/* Continue the child ignoring the SIGSTOP. */
743	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
744
745	children[1] = handle_fork_events(children[0], NULL);
746	ATF_REQUIRE(children[1] > 0);
747
748	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
749	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
750
751	/*
752	 * The child can't exit until the grandchild reports status, so the
753	 * grandchild should report its exit first to the debugger.
754	 *
755	 * Even though the child process is detached, it is still a
756	 * child of the debugger, so it will still report it's exit
757	 * after the grandchild.
758	 */
759	wpid = wait(&status);
760	ATF_REQUIRE(wpid == children[1]);
761	ATF_REQUIRE(WIFEXITED(status));
762	ATF_REQUIRE(WEXITSTATUS(status) == 2);
763
764	wpid = wait(&status);
765	ATF_REQUIRE(wpid == children[0]);
766	ATF_REQUIRE(WIFEXITED(status));
767	ATF_REQUIRE(WEXITSTATUS(status) == 1);
768
769	wpid = wait(&status);
770	ATF_REQUIRE(wpid == -1);
771	ATF_REQUIRE(errno == ECHILD);
772}
773
774static void
775attach_fork_parent(int cpipe[2])
776{
777	pid_t fpid;
778
779	close(cpipe[0]);
780
781	/* Double-fork to disassociate from the debugger. */
782	CHILD_REQUIRE((fpid = fork()) != -1);
783	if (fpid != 0)
784		_exit(3);
785
786	/* Send the pid of the disassociated child to the debugger. */
787	fpid = getpid();
788	CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
789
790	/* Wait for the debugger to attach. */
791	CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
792}
793
794/*
795 * Verify that a new child process is stopped after a followed fork and
796 * that the traced parent sees the exit of the child after the debugger
797 * when both processes remain attached to the debugger.  In this test
798 * the parent that forks is not a direct child of the debugger.
799 */
800ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
801ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
802{
803	pid_t children[2], fpid, wpid;
804	int cpipe[2], status;
805
806	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
807		atf_tc_skip("https://bugs.freebsd.org/239397");
808
809	ATF_REQUIRE(pipe(cpipe) == 0);
810	ATF_REQUIRE((fpid = fork()) != -1);
811	if (fpid == 0) {
812		attach_fork_parent(cpipe);
813		follow_fork_parent(false);
814	}
815
816	/* Parent process. */
817	close(cpipe[1]);
818
819	/* Wait for the direct child to exit. */
820	wpid = waitpid(fpid, &status, 0);
821	ATF_REQUIRE(wpid == fpid);
822	ATF_REQUIRE(WIFEXITED(status));
823	ATF_REQUIRE(WEXITSTATUS(status) == 3);
824
825	/* Read the pid of the fork parent. */
826	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
827	    sizeof(children[0]));
828
829	/* Attach to the fork parent. */
830	attach_child(children[0]);
831
832	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
833
834	/* Continue the fork parent ignoring the SIGSTOP. */
835	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
836
837	/* Signal the fork parent to continue. */
838	close(cpipe[0]);
839
840	children[1] = handle_fork_events(children[0], NULL);
841	ATF_REQUIRE(children[1] > 0);
842
843	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
844	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
845
846	/*
847	 * The fork parent can't exit until the child reports status,
848	 * so the child should report its exit first to the debugger.
849	 */
850	wpid = wait(&status);
851	ATF_REQUIRE(wpid == children[1]);
852	ATF_REQUIRE(WIFEXITED(status));
853	ATF_REQUIRE(WEXITSTATUS(status) == 2);
854
855	wpid = wait(&status);
856	ATF_REQUIRE(wpid == children[0]);
857	ATF_REQUIRE(WIFEXITED(status));
858	ATF_REQUIRE(WEXITSTATUS(status) == 1);
859
860	wpid = wait(&status);
861	ATF_REQUIRE(wpid == -1);
862	ATF_REQUIRE(errno == ECHILD);
863}
864
865/*
866 * Verify that a new child process is stopped after a followed fork
867 * and that the traced parent sees the exit of the child when the new
868 * child process is detached after it reports its fork.  In this test
869 * the parent that forks is not a direct child of the debugger.
870 */
871ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
872ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
873{
874	pid_t children[2], fpid, wpid;
875	int cpipe[2], status;
876
877	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
878		atf_tc_skip("https://bugs.freebsd.org/239292");
879
880	ATF_REQUIRE(pipe(cpipe) == 0);
881	ATF_REQUIRE((fpid = fork()) != -1);
882	if (fpid == 0) {
883		attach_fork_parent(cpipe);
884		follow_fork_parent(false);
885	}
886
887	/* Parent process. */
888	close(cpipe[1]);
889
890	/* Wait for the direct child to exit. */
891	wpid = waitpid(fpid, &status, 0);
892	ATF_REQUIRE(wpid == fpid);
893	ATF_REQUIRE(WIFEXITED(status));
894	ATF_REQUIRE(WEXITSTATUS(status) == 3);
895
896	/* Read the pid of the fork parent. */
897	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
898	    sizeof(children[0]));
899
900	/* Attach to the fork parent. */
901	attach_child(children[0]);
902
903	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
904
905	/* Continue the fork parent ignoring the SIGSTOP. */
906	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
907
908	/* Signal the fork parent to continue. */
909	close(cpipe[0]);
910
911	children[1] = handle_fork_events(children[0], NULL);
912	ATF_REQUIRE(children[1] > 0);
913
914	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
915	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
916
917	/*
918	 * Should not see any status from the child now, only the fork
919	 * parent.
920	 */
921	wpid = wait(&status);
922	ATF_REQUIRE(wpid == children[0]);
923	ATF_REQUIRE(WIFEXITED(status));
924	ATF_REQUIRE(WEXITSTATUS(status) == 1);
925
926	wpid = wait(&status);
927	ATF_REQUIRE(wpid == -1);
928	ATF_REQUIRE(errno == ECHILD);
929}
930
931/*
932 * Verify that a new child process is stopped after a followed fork
933 * and that the traced parent sees the exit of the child when the
934 * traced parent is detached after the fork.  In this test the parent
935 * that forks is not a direct child of the debugger.
936 */
937ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
938ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
939{
940	pid_t children[2], fpid, wpid;
941	int cpipe[2], status;
942
943	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
944		atf_tc_skip("https://bugs.freebsd.org/239425");
945
946	ATF_REQUIRE(pipe(cpipe) == 0);
947	ATF_REQUIRE((fpid = fork()) != -1);
948	if (fpid == 0) {
949		attach_fork_parent(cpipe);
950		follow_fork_parent(false);
951	}
952
953	/* Parent process. */
954	close(cpipe[1]);
955
956	/* Wait for the direct child to exit. */
957	wpid = waitpid(fpid, &status, 0);
958	ATF_REQUIRE(wpid == fpid);
959	ATF_REQUIRE(WIFEXITED(status));
960	ATF_REQUIRE(WEXITSTATUS(status) == 3);
961
962	/* Read the pid of the fork parent. */
963	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
964	    sizeof(children[0]));
965
966	/* Attach to the fork parent. */
967	attach_child(children[0]);
968
969	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
970
971	/* Continue the fork parent ignoring the SIGSTOP. */
972	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
973
974	/* Signal the fork parent to continue. */
975	close(cpipe[0]);
976
977	children[1] = handle_fork_events(children[0], NULL);
978	ATF_REQUIRE(children[1] > 0);
979
980	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
981	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
982
983	/*
984	 * Should not see any status from the fork parent now, only
985	 * the child.
986	 */
987	wpid = wait(&status);
988	ATF_REQUIRE(wpid == children[1]);
989	ATF_REQUIRE(WIFEXITED(status));
990	ATF_REQUIRE(WEXITSTATUS(status) == 2);
991
992	wpid = wait(&status);
993	ATF_REQUIRE(wpid == -1);
994	ATF_REQUIRE(errno == ECHILD);
995}
996
997/*
998 * Verify that a child process does not see an unrelated debugger as its
999 * parent but sees its original parent process.
1000 */
1001ATF_TC_WITHOUT_HEAD(ptrace__getppid);
1002ATF_TC_BODY(ptrace__getppid, tc)
1003{
1004	pid_t child, debugger, ppid, wpid;
1005	int cpipe[2], dpipe[2], status;
1006	char c;
1007
1008	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
1009		atf_tc_skip("https://bugs.freebsd.org/240510");
1010
1011
1012	ATF_REQUIRE(pipe(cpipe) == 0);
1013	ATF_REQUIRE((child = fork()) != -1);
1014
1015	if (child == 0) {
1016		/* Child process. */
1017		close(cpipe[0]);
1018
1019		/* Wait for parent to be ready. */
1020		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1021
1022		/* Report the parent PID to the parent. */
1023		ppid = getppid();
1024		CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1025		    sizeof(ppid));
1026
1027		_exit(1);
1028	}
1029	close(cpipe[1]);
1030
1031	ATF_REQUIRE(pipe(dpipe) == 0);
1032	ATF_REQUIRE((debugger = fork()) != -1);
1033
1034	if (debugger == 0) {
1035		/* Debugger process. */
1036		close(dpipe[0]);
1037
1038		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1039
1040		wpid = waitpid(child, &status, 0);
1041		CHILD_REQUIRE(wpid == child);
1042		CHILD_REQUIRE(WIFSTOPPED(status));
1043		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1044
1045		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1046
1047		/* Signal parent that debugger is attached. */
1048		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1049
1050		/* Wait for traced child to exit. */
1051		wpid = waitpid(child, &status, 0);
1052		CHILD_REQUIRE(wpid == child);
1053		CHILD_REQUIRE(WIFEXITED(status));
1054		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1055
1056		_exit(0);
1057	}
1058	close(dpipe[1]);
1059
1060	/* Parent process. */
1061
1062	/* Wait for the debugger to attach to the child. */
1063	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1064
1065	/* Release the child. */
1066	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1067
1068	/* Read the parent PID from the child. */
1069	ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1070	close(cpipe[0]);
1071
1072	ATF_REQUIRE(ppid == getpid());
1073
1074	/* Wait for the debugger. */
1075	wpid = waitpid(debugger, &status, 0);
1076	ATF_REQUIRE(wpid == debugger);
1077	ATF_REQUIRE(WIFEXITED(status));
1078	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1079
1080	/* The child process should now be ready. */
1081	wpid = waitpid(child, &status, WNOHANG);
1082	ATF_REQUIRE(wpid == child);
1083	ATF_REQUIRE(WIFEXITED(status));
1084	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1085}
1086
1087/*
1088 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1089 * child process created via fork() reports the correct value.
1090 */
1091ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1092ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1093{
1094	struct ptrace_lwpinfo pl[2];
1095	pid_t children[2], fpid, wpid;
1096	int status;
1097
1098	ATF_REQUIRE((fpid = fork()) != -1);
1099	if (fpid == 0) {
1100		trace_me();
1101		follow_fork_parent(false);
1102	}
1103
1104	/* Parent process. */
1105	children[0] = fpid;
1106
1107	/* The first wait() should report the stop from SIGSTOP. */
1108	wpid = waitpid(children[0], &status, 0);
1109	ATF_REQUIRE(wpid == children[0]);
1110	ATF_REQUIRE(WIFSTOPPED(status));
1111	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1112
1113	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1114
1115	/* Continue the child ignoring the SIGSTOP. */
1116	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1117
1118	/* Wait for both halves of the fork event to get reported. */
1119	children[1] = handle_fork_events(children[0], pl);
1120	ATF_REQUIRE(children[1] > 0);
1121
1122	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1123	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1124	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1125	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1126	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1127
1128	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1129	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1130
1131	/*
1132	 * The child can't exit until the grandchild reports status, so the
1133	 * grandchild should report its exit first to the debugger.
1134	 */
1135	wpid = wait(&status);
1136	ATF_REQUIRE(wpid == children[1]);
1137	ATF_REQUIRE(WIFEXITED(status));
1138	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1139
1140	wpid = wait(&status);
1141	ATF_REQUIRE(wpid == children[0]);
1142	ATF_REQUIRE(WIFEXITED(status));
1143	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1144
1145	wpid = wait(&status);
1146	ATF_REQUIRE(wpid == -1);
1147	ATF_REQUIRE(errno == ECHILD);
1148}
1149
1150/*
1151 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1152 * child process created via vfork() reports the correct value.
1153 */
1154ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1155ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1156{
1157	struct ptrace_lwpinfo pl[2];
1158	pid_t children[2], fpid, wpid;
1159	int status;
1160
1161	ATF_REQUIRE((fpid = fork()) != -1);
1162	if (fpid == 0) {
1163		trace_me();
1164		follow_fork_parent(true);
1165	}
1166
1167	/* Parent process. */
1168	children[0] = fpid;
1169
1170	/* The first wait() should report the stop from SIGSTOP. */
1171	wpid = waitpid(children[0], &status, 0);
1172	ATF_REQUIRE(wpid == children[0]);
1173	ATF_REQUIRE(WIFSTOPPED(status));
1174	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1175
1176	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1177
1178	/* Continue the child ignoring the SIGSTOP. */
1179	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1180
1181	/* Wait for both halves of the fork event to get reported. */
1182	children[1] = handle_fork_events(children[0], pl);
1183	ATF_REQUIRE(children[1] > 0);
1184
1185	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1186	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1187	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1188	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1189	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1190
1191	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1192	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1193
1194	/*
1195	 * The child can't exit until the grandchild reports status, so the
1196	 * grandchild should report its exit first to the debugger.
1197	 */
1198	wpid = wait(&status);
1199	ATF_REQUIRE(wpid == children[1]);
1200	ATF_REQUIRE(WIFEXITED(status));
1201	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1202
1203	wpid = wait(&status);
1204	ATF_REQUIRE(wpid == children[0]);
1205	ATF_REQUIRE(WIFEXITED(status));
1206	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1207
1208	wpid = wait(&status);
1209	ATF_REQUIRE(wpid == -1);
1210	ATF_REQUIRE(errno == ECHILD);
1211}
1212
1213static void *
1214simple_thread(void *arg __unused)
1215{
1216
1217	pthread_exit(NULL);
1218}
1219
1220static __dead2 void
1221simple_thread_main(void)
1222{
1223	pthread_t thread;
1224
1225	CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1226	CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1227	exit(1);
1228}
1229
1230/*
1231 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1232 * thread reports the correct value.
1233 */
1234ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1235ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1236{
1237	struct ptrace_lwpinfo pl;
1238	pid_t fpid, wpid;
1239	lwpid_t mainlwp;
1240	int status;
1241
1242	ATF_REQUIRE((fpid = fork()) != -1);
1243	if (fpid == 0) {
1244		trace_me();
1245		simple_thread_main();
1246	}
1247
1248	/* The first wait() should report the stop from SIGSTOP. */
1249	wpid = waitpid(fpid, &status, 0);
1250	ATF_REQUIRE(wpid == fpid);
1251	ATF_REQUIRE(WIFSTOPPED(status));
1252	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1253
1254	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1255	    sizeof(pl)) != -1);
1256	mainlwp = pl.pl_lwpid;
1257
1258	/*
1259	 * Continue the child ignoring the SIGSTOP and tracing all
1260	 * system call exits.
1261	 */
1262	ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1263
1264	/*
1265	 * Wait for the new thread to arrive.  pthread_create() might
1266	 * invoke any number of system calls.  For now we just wait
1267	 * for the new thread to arrive and make sure it reports a
1268	 * valid system call code.  If ptrace grows thread event
1269	 * reporting then this test can be made more precise.
1270	 */
1271	for (;;) {
1272		wpid = waitpid(fpid, &status, 0);
1273		ATF_REQUIRE(wpid == fpid);
1274		ATF_REQUIRE(WIFSTOPPED(status));
1275		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1276
1277		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1278		    sizeof(pl)) != -1);
1279		ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1280		ATF_REQUIRE(pl.pl_syscall_code != 0);
1281		if (pl.pl_lwpid != mainlwp)
1282			/* New thread seen. */
1283			break;
1284
1285		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1286	}
1287
1288	/* Wait for the child to exit. */
1289	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1290	for (;;) {
1291		wpid = waitpid(fpid, &status, 0);
1292		ATF_REQUIRE(wpid == fpid);
1293		if (WIFEXITED(status))
1294			break;
1295
1296		ATF_REQUIRE(WIFSTOPPED(status));
1297		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1298		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1299	}
1300
1301	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1302
1303	wpid = wait(&status);
1304	ATF_REQUIRE(wpid == -1);
1305	ATF_REQUIRE(errno == ECHILD);
1306}
1307
1308/*
1309 * Verify that the expected LWP events are reported for a child thread.
1310 */
1311ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1312ATF_TC_BODY(ptrace__lwp_events, tc)
1313{
1314	struct ptrace_lwpinfo pl;
1315	pid_t fpid, wpid;
1316	lwpid_t lwps[2];
1317	int status;
1318
1319	ATF_REQUIRE((fpid = fork()) != -1);
1320	if (fpid == 0) {
1321		trace_me();
1322		simple_thread_main();
1323	}
1324
1325	/* The first wait() should report the stop from SIGSTOP. */
1326	wpid = waitpid(fpid, &status, 0);
1327	ATF_REQUIRE(wpid == fpid);
1328	ATF_REQUIRE(WIFSTOPPED(status));
1329	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1330
1331	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1332	    sizeof(pl)) != -1);
1333	lwps[0] = pl.pl_lwpid;
1334
1335	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1336
1337	/* Continue the child ignoring the SIGSTOP. */
1338	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1339
1340	/* The first event should be for the child thread's birth. */
1341	wpid = waitpid(fpid, &status, 0);
1342	ATF_REQUIRE(wpid == fpid);
1343	ATF_REQUIRE(WIFSTOPPED(status));
1344	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1345
1346	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1347	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1348	    (PL_FLAG_BORN | PL_FLAG_SCX));
1349	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1350	lwps[1] = pl.pl_lwpid;
1351
1352	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1353
1354	/* The next event should be for the child thread's death. */
1355	wpid = waitpid(fpid, &status, 0);
1356	ATF_REQUIRE(wpid == fpid);
1357	ATF_REQUIRE(WIFSTOPPED(status));
1358	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1359
1360	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1361	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1362	    (PL_FLAG_EXITED | PL_FLAG_SCE));
1363	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1364
1365	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1366
1367	/* The last event should be for the child process's exit. */
1368	wpid = waitpid(fpid, &status, 0);
1369	ATF_REQUIRE(WIFEXITED(status));
1370	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1371
1372	wpid = wait(&status);
1373	ATF_REQUIRE(wpid == -1);
1374	ATF_REQUIRE(errno == ECHILD);
1375}
1376
1377static void *
1378exec_thread(void *arg __unused)
1379{
1380
1381	execl("/usr/bin/true", "true", NULL);
1382	exit(127);
1383}
1384
1385static __dead2 void
1386exec_thread_main(void)
1387{
1388	pthread_t thread;
1389
1390	CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1391	for (;;)
1392		sleep(60);
1393	exit(1);
1394}
1395
1396/*
1397 * Verify that the expected LWP events are reported for a multithreaded
1398 * process that calls execve(2).
1399 */
1400ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1401ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1402{
1403	struct ptrace_lwpinfo pl;
1404	pid_t fpid, wpid;
1405	lwpid_t lwps[2];
1406	int status;
1407
1408	ATF_REQUIRE((fpid = fork()) != -1);
1409	if (fpid == 0) {
1410		trace_me();
1411		exec_thread_main();
1412	}
1413
1414	/* The first wait() should report the stop from SIGSTOP. */
1415	wpid = waitpid(fpid, &status, 0);
1416	ATF_REQUIRE(wpid == fpid);
1417	ATF_REQUIRE(WIFSTOPPED(status));
1418	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1419
1420	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1421	    sizeof(pl)) != -1);
1422	lwps[0] = pl.pl_lwpid;
1423
1424	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1425
1426	/* Continue the child ignoring the SIGSTOP. */
1427	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1428
1429	/* The first event should be for the child thread's birth. */
1430	wpid = waitpid(fpid, &status, 0);
1431	ATF_REQUIRE(wpid == fpid);
1432	ATF_REQUIRE(WIFSTOPPED(status));
1433	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1434
1435	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1436	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1437	    (PL_FLAG_BORN | PL_FLAG_SCX));
1438	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1439	lwps[1] = pl.pl_lwpid;
1440
1441	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1442
1443	/*
1444	 * The next event should be for the main thread's death due to
1445	 * single threading from execve().
1446	 */
1447	wpid = waitpid(fpid, &status, 0);
1448	ATF_REQUIRE(wpid == fpid);
1449	ATF_REQUIRE(WIFSTOPPED(status));
1450	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1451
1452	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1453	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1454	    (PL_FLAG_EXITED));
1455	ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1456
1457	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1458
1459	/* The next event should be for the child process's exec. */
1460	wpid = waitpid(fpid, &status, 0);
1461	ATF_REQUIRE(WIFSTOPPED(status));
1462	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1463
1464	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1465	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1466	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1467	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1468
1469	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1470
1471	/* The last event should be for the child process's exit. */
1472	wpid = waitpid(fpid, &status, 0);
1473	ATF_REQUIRE(WIFEXITED(status));
1474	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1475
1476	wpid = wait(&status);
1477	ATF_REQUIRE(wpid == -1);
1478	ATF_REQUIRE(errno == ECHILD);
1479}
1480
1481static void
1482handler(int sig __unused)
1483{
1484}
1485
1486static void
1487signal_main(void)
1488{
1489
1490	signal(SIGINFO, handler);
1491	raise(SIGINFO);
1492	exit(0);
1493}
1494
1495/*
1496 * Verify that the expected ptrace event is reported for a signal.
1497 */
1498ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1499ATF_TC_BODY(ptrace__siginfo, tc)
1500{
1501	struct ptrace_lwpinfo pl;
1502	pid_t fpid, wpid;
1503	int status;
1504
1505	ATF_REQUIRE((fpid = fork()) != -1);
1506	if (fpid == 0) {
1507		trace_me();
1508		signal_main();
1509	}
1510
1511	/* The first wait() should report the stop from SIGSTOP. */
1512	wpid = waitpid(fpid, &status, 0);
1513	ATF_REQUIRE(wpid == fpid);
1514	ATF_REQUIRE(WIFSTOPPED(status));
1515	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1516
1517	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1518
1519	/* The next event should be for the SIGINFO. */
1520	wpid = waitpid(fpid, &status, 0);
1521	ATF_REQUIRE(WIFSTOPPED(status));
1522	ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1523
1524	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1525	ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1526	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1527	ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1528	ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1529
1530	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1531
1532	/* The last event should be for the child process's exit. */
1533	wpid = waitpid(fpid, &status, 0);
1534	ATF_REQUIRE(WIFEXITED(status));
1535	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1536
1537	wpid = wait(&status);
1538	ATF_REQUIRE(wpid == -1);
1539	ATF_REQUIRE(errno == ECHILD);
1540}
1541
1542/*
1543 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1544 */
1545ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1546ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1547{
1548	pid_t fpid, wpid;
1549	int events, status;
1550
1551	ATF_REQUIRE((fpid = fork()) != -1);
1552	if (fpid == 0) {
1553		trace_me();
1554		exec_thread(NULL);
1555	}
1556
1557	/* The first wait() should report the stop from SIGSTOP. */
1558	wpid = waitpid(fpid, &status, 0);
1559	ATF_REQUIRE(wpid == fpid);
1560	ATF_REQUIRE(WIFSTOPPED(status));
1561	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1562
1563	events = 0;
1564	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1565	    sizeof(events)) == 0);
1566
1567	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1568
1569	/* Should get one event at exit. */
1570	wpid = waitpid(fpid, &status, 0);
1571	ATF_REQUIRE(WIFEXITED(status));
1572	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1573
1574	wpid = wait(&status);
1575	ATF_REQUIRE(wpid == -1);
1576	ATF_REQUIRE(errno == ECHILD);
1577}
1578
1579ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1580ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1581{
1582	struct ptrace_lwpinfo pl;
1583	pid_t fpid, wpid;
1584	int events, status;
1585
1586	ATF_REQUIRE((fpid = fork()) != -1);
1587	if (fpid == 0) {
1588		trace_me();
1589		exec_thread(NULL);
1590	}
1591
1592	/* The first wait() should report the stop from SIGSTOP. */
1593	wpid = waitpid(fpid, &status, 0);
1594	ATF_REQUIRE(wpid == fpid);
1595	ATF_REQUIRE(WIFSTOPPED(status));
1596	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1597
1598	events = PTRACE_EXEC;
1599	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1600	    sizeof(events)) == 0);
1601
1602	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1603
1604	/* The next event should be for the child process's exec. */
1605	wpid = waitpid(fpid, &status, 0);
1606	ATF_REQUIRE(WIFSTOPPED(status));
1607	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1608
1609	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1610	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1611	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1612
1613	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1614
1615	/* The last event should be for the child process's exit. */
1616	wpid = waitpid(fpid, &status, 0);
1617	ATF_REQUIRE(WIFEXITED(status));
1618	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1619
1620	wpid = wait(&status);
1621	ATF_REQUIRE(wpid == -1);
1622	ATF_REQUIRE(errno == ECHILD);
1623}
1624
1625ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1626ATF_TC_BODY(ptrace__event_mask, tc)
1627{
1628	pid_t fpid, wpid;
1629	int events, status;
1630
1631	ATF_REQUIRE((fpid = fork()) != -1);
1632	if (fpid == 0) {
1633		trace_me();
1634		exit(0);
1635	}
1636
1637	/* The first wait() should report the stop from SIGSTOP. */
1638	wpid = waitpid(fpid, &status, 0);
1639	ATF_REQUIRE(wpid == fpid);
1640	ATF_REQUIRE(WIFSTOPPED(status));
1641	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1642
1643	/* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1644	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1645	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1646	    sizeof(events)) == 0);
1647	ATF_REQUIRE(events & PTRACE_FORK);
1648	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1649	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1650	    sizeof(events)) == 0);
1651	ATF_REQUIRE(!(events & PTRACE_FORK));
1652
1653	/* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1654	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1655	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1656	    sizeof(events)) == 0);
1657	ATF_REQUIRE(events & PTRACE_LWP);
1658	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1659	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1660	    sizeof(events)) == 0);
1661	ATF_REQUIRE(!(events & PTRACE_LWP));
1662
1663	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1664
1665	/* Should get one event at exit. */
1666	wpid = waitpid(fpid, &status, 0);
1667	ATF_REQUIRE(WIFEXITED(status));
1668	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1669
1670	wpid = wait(&status);
1671	ATF_REQUIRE(wpid == -1);
1672	ATF_REQUIRE(errno == ECHILD);
1673}
1674
1675/*
1676 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1677 */
1678ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1679ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1680{
1681	struct ptrace_lwpinfo pl;
1682	pid_t fpid, wpid;
1683	int events, status;
1684
1685	ATF_REQUIRE((fpid = fork()) != -1);
1686	if (fpid == 0) {
1687		trace_me();
1688		follow_fork_parent(true);
1689	}
1690
1691	/* The first wait() should report the stop from SIGSTOP. */
1692	wpid = waitpid(fpid, &status, 0);
1693	ATF_REQUIRE(wpid == fpid);
1694	ATF_REQUIRE(WIFSTOPPED(status));
1695	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1696
1697	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1698	    sizeof(events)) == 0);
1699	events |= PTRACE_VFORK;
1700	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1701	    sizeof(events)) == 0);
1702
1703	/* Continue the child ignoring the SIGSTOP. */
1704	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1705
1706	/* The next event should report the end of the vfork. */
1707	wpid = wait(&status);
1708	ATF_REQUIRE(wpid == fpid);
1709	ATF_REQUIRE(WIFSTOPPED(status));
1710	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1711	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1712	ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1713
1714	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1715
1716	wpid = wait(&status);
1717	ATF_REQUIRE(wpid == fpid);
1718	ATF_REQUIRE(WIFEXITED(status));
1719	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1720
1721	wpid = wait(&status);
1722	ATF_REQUIRE(wpid == -1);
1723	ATF_REQUIRE(errno == ECHILD);
1724}
1725
1726ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1727ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1728{
1729	struct ptrace_lwpinfo pl[2];
1730	pid_t children[2], fpid, wpid;
1731	int events, status;
1732
1733	ATF_REQUIRE((fpid = fork()) != -1);
1734	if (fpid == 0) {
1735		trace_me();
1736		follow_fork_parent(true);
1737	}
1738
1739	/* Parent process. */
1740	children[0] = fpid;
1741
1742	/* The first wait() should report the stop from SIGSTOP. */
1743	wpid = waitpid(children[0], &status, 0);
1744	ATF_REQUIRE(wpid == children[0]);
1745	ATF_REQUIRE(WIFSTOPPED(status));
1746	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1747
1748	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1749	    sizeof(events)) == 0);
1750	events |= PTRACE_FORK | PTRACE_VFORK;
1751	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1752	    sizeof(events)) == 0);
1753
1754	/* Continue the child ignoring the SIGSTOP. */
1755	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1756
1757	/* Wait for both halves of the fork event to get reported. */
1758	children[1] = handle_fork_events(children[0], pl);
1759	ATF_REQUIRE(children[1] > 0);
1760
1761	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1762
1763	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1764	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1765
1766	/*
1767	 * The child can't exit until the grandchild reports status, so the
1768	 * grandchild should report its exit first to the debugger.
1769	 */
1770	wpid = waitpid(children[1], &status, 0);
1771	ATF_REQUIRE(wpid == children[1]);
1772	ATF_REQUIRE(WIFEXITED(status));
1773	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1774
1775	/*
1776	 * The child should report it's vfork() completion before it
1777	 * exits.
1778	 */
1779	wpid = wait(&status);
1780	ATF_REQUIRE(wpid == children[0]);
1781	ATF_REQUIRE(WIFSTOPPED(status));
1782	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1783	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1784	    -1);
1785	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1786
1787	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1788
1789	wpid = wait(&status);
1790	ATF_REQUIRE(wpid == children[0]);
1791	ATF_REQUIRE(WIFEXITED(status));
1792	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1793
1794	wpid = wait(&status);
1795	ATF_REQUIRE(wpid == -1);
1796	ATF_REQUIRE(errno == ECHILD);
1797}
1798
1799#ifdef HAVE_BREAKPOINT
1800/*
1801 * Verify that no more events are reported after PT_KILL except for the
1802 * process exit when stopped due to a breakpoint trap.
1803 */
1804ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1805ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1806{
1807	pid_t fpid, wpid;
1808	int status;
1809
1810	ATF_REQUIRE((fpid = fork()) != -1);
1811	if (fpid == 0) {
1812		trace_me();
1813		breakpoint();
1814		exit(1);
1815	}
1816
1817	/* The first wait() should report the stop from SIGSTOP. */
1818	wpid = waitpid(fpid, &status, 0);
1819	ATF_REQUIRE(wpid == fpid);
1820	ATF_REQUIRE(WIFSTOPPED(status));
1821	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1822
1823	/* Continue the child ignoring the SIGSTOP. */
1824	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1825
1826	/* The second wait() should report hitting the breakpoint. */
1827	wpid = waitpid(fpid, &status, 0);
1828	ATF_REQUIRE(wpid == fpid);
1829	ATF_REQUIRE(WIFSTOPPED(status));
1830	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1831
1832	/* Kill the child process. */
1833	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1834
1835	/* The last wait() should report the SIGKILL. */
1836	wpid = waitpid(fpid, &status, 0);
1837	ATF_REQUIRE(wpid == fpid);
1838	ATF_REQUIRE(WIFSIGNALED(status));
1839	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1840
1841	wpid = wait(&status);
1842	ATF_REQUIRE(wpid == -1);
1843	ATF_REQUIRE(errno == ECHILD);
1844}
1845#endif /* HAVE_BREAKPOINT */
1846
1847/*
1848 * Verify that no more events are reported after PT_KILL except for the
1849 * process exit when stopped inside of a system call.
1850 */
1851ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1852ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1853{
1854	struct ptrace_lwpinfo pl;
1855	pid_t fpid, wpid;
1856	int status;
1857
1858	ATF_REQUIRE((fpid = fork()) != -1);
1859	if (fpid == 0) {
1860		trace_me();
1861		getpid();
1862		exit(1);
1863	}
1864
1865	/* The first wait() should report the stop from SIGSTOP. */
1866	wpid = waitpid(fpid, &status, 0);
1867	ATF_REQUIRE(wpid == fpid);
1868	ATF_REQUIRE(WIFSTOPPED(status));
1869	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1870
1871	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
1872	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1873
1874	/* The second wait() should report a system call entry for getpid(). */
1875	wpid = waitpid(fpid, &status, 0);
1876	ATF_REQUIRE(wpid == fpid);
1877	ATF_REQUIRE(WIFSTOPPED(status));
1878	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1879
1880	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1881	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1882
1883	/* Kill the child process. */
1884	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1885
1886	/* The last wait() should report the SIGKILL. */
1887	wpid = waitpid(fpid, &status, 0);
1888	ATF_REQUIRE(wpid == fpid);
1889	ATF_REQUIRE(WIFSIGNALED(status));
1890	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1891
1892	wpid = wait(&status);
1893	ATF_REQUIRE(wpid == -1);
1894	ATF_REQUIRE(errno == ECHILD);
1895}
1896
1897/*
1898 * Verify that no more events are reported after PT_KILL except for the
1899 * process exit when killing a multithreaded process.
1900 */
1901ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1902ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1903{
1904	struct ptrace_lwpinfo pl;
1905	pid_t fpid, wpid;
1906	lwpid_t main_lwp;
1907	int status;
1908
1909	ATF_REQUIRE((fpid = fork()) != -1);
1910	if (fpid == 0) {
1911		trace_me();
1912		simple_thread_main();
1913	}
1914
1915	/* The first wait() should report the stop from SIGSTOP. */
1916	wpid = waitpid(fpid, &status, 0);
1917	ATF_REQUIRE(wpid == fpid);
1918	ATF_REQUIRE(WIFSTOPPED(status));
1919	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1920
1921	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1922	    sizeof(pl)) != -1);
1923	main_lwp = pl.pl_lwpid;
1924
1925	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1926
1927	/* Continue the child ignoring the SIGSTOP. */
1928	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1929
1930	/* The first event should be for the child thread's birth. */
1931	wpid = waitpid(fpid, &status, 0);
1932	ATF_REQUIRE(wpid == fpid);
1933	ATF_REQUIRE(WIFSTOPPED(status));
1934	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1935
1936	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1937	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1938	    (PL_FLAG_BORN | PL_FLAG_SCX));
1939	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1940
1941	/* Kill the child process. */
1942	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1943
1944	/* The last wait() should report the SIGKILL. */
1945	wpid = waitpid(fpid, &status, 0);
1946	ATF_REQUIRE(wpid == fpid);
1947	ATF_REQUIRE(WIFSIGNALED(status));
1948	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1949
1950	wpid = wait(&status);
1951	ATF_REQUIRE(wpid == -1);
1952	ATF_REQUIRE(errno == ECHILD);
1953}
1954
1955static void *
1956mask_usr1_thread(void *arg)
1957{
1958	pthread_barrier_t *pbarrier;
1959	sigset_t sigmask;
1960
1961	pbarrier = (pthread_barrier_t*)arg;
1962
1963	sigemptyset(&sigmask);
1964	sigaddset(&sigmask, SIGUSR1);
1965	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1966
1967	/* Sync up with other thread after sigmask updated. */
1968	pthread_barrier_wait(pbarrier);
1969
1970	for (;;)
1971		sleep(60);
1972
1973	return (NULL);
1974}
1975
1976/*
1977 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1978 * and prevents spurious stops due to those other signals.
1979 */
1980ATF_TC(ptrace__PT_KILL_competing_signal);
1981ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1982{
1983
1984	atf_tc_set_md_var(tc, "require.user", "root");
1985}
1986ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1987{
1988	pid_t fpid, wpid;
1989	int status;
1990	cpuset_t setmask;
1991	pthread_t t;
1992	pthread_barrier_t barrier;
1993	struct sched_param sched_param;
1994
1995	ATF_REQUIRE((fpid = fork()) != -1);
1996	if (fpid == 0) {
1997		/* Bind to one CPU so only one thread at a time will run. */
1998		CPU_ZERO(&setmask);
1999		CPU_SET(0, &setmask);
2000		cpusetid_t setid;
2001		CHILD_REQUIRE(cpuset(&setid) == 0);
2002		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2003		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2004
2005		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2006
2007		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2008		    (void*)&barrier) == 0);
2009
2010		/*
2011		 * Give the main thread higher priority. The test always
2012		 * assumes that, if both threads are able to run, the main
2013		 * thread runs first.
2014		 */
2015		sched_param.sched_priority =
2016		    (sched_get_priority_max(SCHED_FIFO) +
2017		    sched_get_priority_min(SCHED_FIFO)) / 2;
2018		CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2019		    SCHED_FIFO, &sched_param) == 0);
2020		sched_param.sched_priority -= RQ_PPQ;
2021		CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2022		    &sched_param) == 0);
2023
2024		sigset_t sigmask;
2025		sigemptyset(&sigmask);
2026		sigaddset(&sigmask, SIGUSR2);
2027		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2028
2029		/* Sync up with other thread after sigmask updated. */
2030		pthread_barrier_wait(&barrier);
2031
2032		trace_me();
2033
2034		for (;;)
2035			sleep(60);
2036
2037		exit(1);
2038	}
2039
2040	/* The first wait() should report the stop from SIGSTOP. */
2041	wpid = waitpid(fpid, &status, 0);
2042	ATF_REQUIRE(wpid == fpid);
2043	ATF_REQUIRE(WIFSTOPPED(status));
2044	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2045
2046	/* Continue the child ignoring the SIGSTOP. */
2047	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2048
2049	/* Send a signal that only the second thread can handle. */
2050	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2051
2052	/* The second wait() should report the SIGUSR2. */
2053	wpid = waitpid(fpid, &status, 0);
2054	ATF_REQUIRE(wpid == fpid);
2055	ATF_REQUIRE(WIFSTOPPED(status));
2056	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2057
2058	/* Send a signal that only the first thread can handle. */
2059	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2060
2061	/* Replace the SIGUSR2 with a kill. */
2062	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2063
2064	/* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2065	wpid = waitpid(fpid, &status, 0);
2066	ATF_REQUIRE(wpid == fpid);
2067	ATF_REQUIRE(WIFSIGNALED(status));
2068	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2069
2070	wpid = wait(&status);
2071	ATF_REQUIRE(wpid == -1);
2072	ATF_REQUIRE(errno == ECHILD);
2073}
2074
2075/*
2076 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2077 * and prevents spurious stops caused by those events.
2078 */
2079ATF_TC(ptrace__PT_KILL_competing_stop);
2080ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2081{
2082
2083	atf_tc_set_md_var(tc, "require.user", "root");
2084}
2085ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2086{
2087	pid_t fpid, wpid;
2088	int status;
2089	cpuset_t setmask;
2090	pthread_t t;
2091	pthread_barrier_t barrier;
2092	lwpid_t main_lwp;
2093	struct ptrace_lwpinfo pl;
2094	struct sched_param sched_param;
2095
2096	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
2097		atf_tc_skip("https://bugs.freebsd.org/220841");
2098
2099	ATF_REQUIRE((fpid = fork()) != -1);
2100	if (fpid == 0) {
2101		trace_me();
2102
2103		/* Bind to one CPU so only one thread at a time will run. */
2104		CPU_ZERO(&setmask);
2105		CPU_SET(0, &setmask);
2106		cpusetid_t setid;
2107		CHILD_REQUIRE(cpuset(&setid) == 0);
2108		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2109		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2110
2111		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2112
2113		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2114		    (void*)&barrier) == 0);
2115
2116		/*
2117		 * Give the main thread higher priority. The test always
2118		 * assumes that, if both threads are able to run, the main
2119		 * thread runs first.
2120		 */
2121		sched_param.sched_priority =
2122		    (sched_get_priority_max(SCHED_FIFO) +
2123		    sched_get_priority_min(SCHED_FIFO)) / 2;
2124		CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2125		    SCHED_FIFO, &sched_param) == 0);
2126		sched_param.sched_priority -= RQ_PPQ;
2127		CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2128		    &sched_param) == 0);
2129
2130		sigset_t sigmask;
2131		sigemptyset(&sigmask);
2132		sigaddset(&sigmask, SIGUSR2);
2133		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2134
2135		/* Sync up with other thread after sigmask updated. */
2136		pthread_barrier_wait(&barrier);
2137
2138		/* Sync up with the test before doing the getpid(). */
2139		raise(SIGSTOP);
2140
2141		getpid();
2142		exit(1);
2143	}
2144
2145	/* The first wait() should report the stop from SIGSTOP. */
2146	wpid = waitpid(fpid, &status, 0);
2147	ATF_REQUIRE(wpid == fpid);
2148	ATF_REQUIRE(WIFSTOPPED(status));
2149	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2150
2151	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2152	main_lwp = pl.pl_lwpid;
2153
2154	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2155	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2156
2157	/*
2158	 * Continue until child is done with setup, which is indicated with
2159	 * SIGSTOP. Ignore system calls in the meantime.
2160	 */
2161	for (;;) {
2162		wpid = waitpid(fpid, &status, 0);
2163		ATF_REQUIRE(wpid == fpid);
2164		ATF_REQUIRE(WIFSTOPPED(status));
2165		if (WSTOPSIG(status) == SIGTRAP) {
2166			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2167			    sizeof(pl)) != -1);
2168			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2169		} else {
2170			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2171			break;
2172		}
2173		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2174	}
2175
2176	/* Proceed, allowing main thread to hit syscall entry for getpid(). */
2177	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2178
2179	wpid = waitpid(fpid, &status, 0);
2180	ATF_REQUIRE(wpid == fpid);
2181	ATF_REQUIRE(WIFSTOPPED(status));
2182	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2183
2184	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2185	    sizeof(pl)) != -1);
2186	ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2187	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2188	/* Prevent the main thread from hitting its syscall exit for now. */
2189	ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2190
2191	/*
2192	 * Proceed, allowing second thread to hit syscall exit for
2193	 * pthread_barrier_wait().
2194	 */
2195	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2196
2197	wpid = waitpid(fpid, &status, 0);
2198	ATF_REQUIRE(wpid == fpid);
2199	ATF_REQUIRE(WIFSTOPPED(status));
2200	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2201
2202	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2203	    sizeof(pl)) != -1);
2204	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2205	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2206
2207	/* Send a signal that only the second thread can handle. */
2208	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2209
2210	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2211
2212	/* The next wait() should report the SIGUSR2. */
2213	wpid = waitpid(fpid, &status, 0);
2214	ATF_REQUIRE(wpid == fpid);
2215	ATF_REQUIRE(WIFSTOPPED(status));
2216	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2217
2218	/* Allow the main thread to try to finish its system call. */
2219	ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2220
2221	/*
2222	 * At this point, the main thread is in the middle of a system call and
2223	 * has been resumed. The second thread has taken a SIGUSR2 which will
2224	 * be replaced with a SIGKILL below. The main thread will get to run
2225	 * first. It should notice the kill request (even though the signal
2226	 * replacement occurred in the other thread) and exit accordingly.  It
2227	 * should not stop for the system call exit event.
2228	 */
2229
2230	/* Replace the SIGUSR2 with a kill. */
2231	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2232
2233	/* The last wait() should report the SIGKILL (not a syscall exit). */
2234	wpid = waitpid(fpid, &status, 0);
2235	ATF_REQUIRE(wpid == fpid);
2236	ATF_REQUIRE(WIFSIGNALED(status));
2237	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2238
2239	wpid = wait(&status);
2240	ATF_REQUIRE(wpid == -1);
2241	ATF_REQUIRE(errno == ECHILD);
2242}
2243
2244static void
2245sigusr1_handler(int sig)
2246{
2247
2248	CHILD_REQUIRE(sig == SIGUSR1);
2249	_exit(2);
2250}
2251
2252/*
2253 * Verify that even if the signal queue is full for a child process,
2254 * a PT_KILL will kill the process.
2255 */
2256ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2257ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2258{
2259	pid_t fpid, wpid;
2260	int status;
2261	int max_pending_per_proc;
2262	size_t len;
2263	int i;
2264
2265	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2266
2267	ATF_REQUIRE((fpid = fork()) != -1);
2268	if (fpid == 0) {
2269		trace_me();
2270		exit(1);
2271	}
2272
2273	/* The first wait() should report the stop from SIGSTOP. */
2274	wpid = waitpid(fpid, &status, 0);
2275	ATF_REQUIRE(wpid == fpid);
2276	ATF_REQUIRE(WIFSTOPPED(status));
2277	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2278
2279	len = sizeof(max_pending_per_proc);
2280	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2281	    &max_pending_per_proc, &len, NULL, 0) == 0);
2282
2283	/* Fill the signal queue. */
2284	for (i = 0; i < max_pending_per_proc; ++i)
2285		ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2286
2287	/* Kill the child process. */
2288	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2289
2290	/* The last wait() should report the SIGKILL. */
2291	wpid = waitpid(fpid, &status, 0);
2292	ATF_REQUIRE(wpid == fpid);
2293	ATF_REQUIRE(WIFSIGNALED(status));
2294	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2295
2296	wpid = wait(&status);
2297	ATF_REQUIRE(wpid == -1);
2298	ATF_REQUIRE(errno == ECHILD);
2299}
2300
2301/*
2302 * Verify that when stopped at a system call entry, a signal can be
2303 * requested with PT_CONTINUE which will be delivered once the system
2304 * call is complete.
2305 */
2306ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2307ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2308{
2309	struct ptrace_lwpinfo pl;
2310	pid_t fpid, wpid;
2311	int status;
2312
2313	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2314
2315	ATF_REQUIRE((fpid = fork()) != -1);
2316	if (fpid == 0) {
2317		trace_me();
2318		getpid();
2319		exit(1);
2320	}
2321
2322	/* The first wait() should report the stop from SIGSTOP. */
2323	wpid = waitpid(fpid, &status, 0);
2324	ATF_REQUIRE(wpid == fpid);
2325	ATF_REQUIRE(WIFSTOPPED(status));
2326	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2327
2328	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2329	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2330
2331	/* The second wait() should report a system call entry for getpid(). */
2332	wpid = waitpid(fpid, &status, 0);
2333	ATF_REQUIRE(wpid == fpid);
2334	ATF_REQUIRE(WIFSTOPPED(status));
2335	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2336
2337	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2338	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2339
2340	/* Continue the child process with a signal. */
2341	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2342
2343	for (;;) {
2344		/*
2345		 * The last wait() should report exit 2, i.e., a normal _exit
2346		 * from the signal handler. In the meantime, catch and proceed
2347		 * past any syscall stops.
2348		 */
2349		wpid = waitpid(fpid, &status, 0);
2350		ATF_REQUIRE(wpid == fpid);
2351		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2352			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2353			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2354			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2355		} else {
2356			ATF_REQUIRE(WIFEXITED(status));
2357			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2358			break;
2359		}
2360	}
2361
2362	wpid = wait(&status);
2363	ATF_REQUIRE(wpid == -1);
2364	ATF_REQUIRE(errno == ECHILD);
2365}
2366
2367static void
2368sigusr1_counting_handler(int sig)
2369{
2370	static int counter = 0;
2371
2372	CHILD_REQUIRE(sig == SIGUSR1);
2373	counter++;
2374	if (counter == 2)
2375		_exit(2);
2376}
2377
2378/*
2379 * Verify that, when continuing from a stop at system call entry and exit,
2380 * a signal can be requested from both stops, and both will be delivered when
2381 * the system call is complete.
2382 */
2383ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2384ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2385{
2386	struct ptrace_lwpinfo pl;
2387	pid_t fpid, wpid;
2388	int status;
2389
2390	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2391
2392	ATF_REQUIRE((fpid = fork()) != -1);
2393	if (fpid == 0) {
2394		trace_me();
2395		getpid();
2396		exit(1);
2397	}
2398
2399	/* The first wait() should report the stop from SIGSTOP. */
2400	wpid = waitpid(fpid, &status, 0);
2401	ATF_REQUIRE(wpid == fpid);
2402	ATF_REQUIRE(WIFSTOPPED(status));
2403	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2404
2405	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2406	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2407
2408	/* The second wait() should report a system call entry for getpid(). */
2409	wpid = waitpid(fpid, &status, 0);
2410	ATF_REQUIRE(wpid == fpid);
2411	ATF_REQUIRE(WIFSTOPPED(status));
2412	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2413
2414	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2415	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2416
2417	/* Continue the child process with a signal. */
2418	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2419
2420	/* The third wait() should report a system call exit for getpid(). */
2421	wpid = waitpid(fpid, &status, 0);
2422	ATF_REQUIRE(wpid == fpid);
2423	ATF_REQUIRE(WIFSTOPPED(status));
2424	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2425
2426	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2427	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2428
2429	/* Continue the child process with a signal. */
2430	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2431
2432	for (;;) {
2433		/*
2434		 * The last wait() should report exit 2, i.e., a normal _exit
2435		 * from the signal handler. In the meantime, catch and proceed
2436		 * past any syscall stops.
2437		 */
2438		wpid = waitpid(fpid, &status, 0);
2439		ATF_REQUIRE(wpid == fpid);
2440		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2441			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2442			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2443			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2444		} else {
2445			ATF_REQUIRE(WIFEXITED(status));
2446			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2447			break;
2448		}
2449	}
2450
2451	wpid = wait(&status);
2452	ATF_REQUIRE(wpid == -1);
2453	ATF_REQUIRE(errno == ECHILD);
2454}
2455
2456/*
2457 * Verify that even if the signal queue is full for a child process,
2458 * a PT_CONTINUE with a signal will not result in loss of that signal.
2459 */
2460ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2461ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2462{
2463	pid_t fpid, wpid;
2464	int status;
2465	int max_pending_per_proc;
2466	size_t len;
2467	int i;
2468
2469	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2470	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2471
2472	ATF_REQUIRE((fpid = fork()) != -1);
2473	if (fpid == 0) {
2474		trace_me();
2475		exit(1);
2476	}
2477
2478	/* The first wait() should report the stop from SIGSTOP. */
2479	wpid = waitpid(fpid, &status, 0);
2480	ATF_REQUIRE(wpid == fpid);
2481	ATF_REQUIRE(WIFSTOPPED(status));
2482	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2483
2484	len = sizeof(max_pending_per_proc);
2485	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2486	    &max_pending_per_proc, &len, NULL, 0) == 0);
2487
2488	/* Fill the signal queue. */
2489	for (i = 0; i < max_pending_per_proc; ++i)
2490		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2491
2492	/* Continue with signal. */
2493	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2494
2495	for (;;) {
2496		wpid = waitpid(fpid, &status, 0);
2497		ATF_REQUIRE(wpid == fpid);
2498		if (WIFSTOPPED(status)) {
2499			ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2500			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2501		} else {
2502			/*
2503			 * The last wait() should report normal _exit from the
2504			 * SIGUSR1 handler.
2505			 */
2506			ATF_REQUIRE(WIFEXITED(status));
2507			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2508			break;
2509		}
2510	}
2511
2512	wpid = wait(&status);
2513	ATF_REQUIRE(wpid == -1);
2514	ATF_REQUIRE(errno == ECHILD);
2515}
2516
2517static sem_t sigusr1_sem;
2518static int got_usr1;
2519
2520static void
2521sigusr1_sempost_handler(int sig __unused)
2522{
2523
2524	got_usr1++;
2525	CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2526}
2527
2528/*
2529 * Verify that even if the signal queue is full for a child process,
2530 * and the signal is masked, a PT_CONTINUE with a signal will not
2531 * result in loss of that signal.
2532 */
2533ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2534ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2535{
2536	struct ptrace_lwpinfo pl;
2537	pid_t fpid, wpid;
2538	int status, err;
2539	int max_pending_per_proc;
2540	size_t len;
2541	int i;
2542	sigset_t sigmask;
2543
2544	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2545	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2546	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2547
2548	got_usr1 = 0;
2549	ATF_REQUIRE((fpid = fork()) != -1);
2550	if (fpid == 0) {
2551		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2552		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2553		CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2554
2555		trace_me();
2556		CHILD_REQUIRE(got_usr1 == 0);
2557
2558		/* Allow the pending SIGUSR1 in now. */
2559		CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2560		/* Wait to receive the SIGUSR1. */
2561		do {
2562			err = sem_wait(&sigusr1_sem);
2563			CHILD_REQUIRE(err == 0 || errno == EINTR);
2564		} while (err != 0 && errno == EINTR);
2565		CHILD_REQUIRE(got_usr1 == 1);
2566		exit(1);
2567	}
2568
2569	/* The first wait() should report the stop from SIGSTOP. */
2570	wpid = waitpid(fpid, &status, 0);
2571	ATF_REQUIRE(wpid == fpid);
2572	ATF_REQUIRE(WIFSTOPPED(status));
2573	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2574
2575	len = sizeof(max_pending_per_proc);
2576	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2577	    &max_pending_per_proc, &len, NULL, 0) == 0);
2578
2579	/* Fill the signal queue. */
2580	for (i = 0; i < max_pending_per_proc; ++i)
2581		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2582
2583	/* Continue with signal. */
2584	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2585
2586	/* Collect and ignore all of the SIGUSR2. */
2587	for (i = 0; i < max_pending_per_proc; ++i) {
2588		wpid = waitpid(fpid, &status, 0);
2589		ATF_REQUIRE(wpid == fpid);
2590		ATF_REQUIRE(WIFSTOPPED(status));
2591		ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2592		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2593	}
2594
2595	/* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2596	wpid = waitpid(fpid, &status, 0);
2597	ATF_REQUIRE(wpid == fpid);
2598	ATF_REQUIRE(WIFSTOPPED(status));
2599	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2600	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2601	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2602
2603	/* Continue the child, ignoring the SIGUSR1. */
2604	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2605
2606	/* The last wait() should report exit after receiving SIGUSR1. */
2607	wpid = waitpid(fpid, &status, 0);
2608	ATF_REQUIRE(wpid == fpid);
2609	ATF_REQUIRE(WIFEXITED(status));
2610	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2611
2612	wpid = wait(&status);
2613	ATF_REQUIRE(wpid == -1);
2614	ATF_REQUIRE(errno == ECHILD);
2615}
2616
2617/*
2618 * Verify that, after stopping due to a signal, that signal can be
2619 * replaced with another signal.
2620 */
2621ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2622ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2623{
2624	struct ptrace_lwpinfo pl;
2625	pid_t fpid, wpid;
2626	int status;
2627
2628	ATF_REQUIRE((fpid = fork()) != -1);
2629	if (fpid == 0) {
2630		trace_me();
2631		sleep(20);
2632		exit(1);
2633	}
2634
2635	/* The first wait() should report the stop from SIGSTOP. */
2636	wpid = waitpid(fpid, &status, 0);
2637	ATF_REQUIRE(wpid == fpid);
2638	ATF_REQUIRE(WIFSTOPPED(status));
2639	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2640
2641	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2642
2643	/* Send a signal without ptrace. */
2644	ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2645
2646	/* The second wait() should report a SIGINT was received. */
2647	wpid = waitpid(fpid, &status, 0);
2648	ATF_REQUIRE(wpid == fpid);
2649	ATF_REQUIRE(WIFSTOPPED(status));
2650	ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2651
2652	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2653	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2654	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2655
2656	/* Continue the child process with a different signal. */
2657	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2658
2659	/*
2660	 * The last wait() should report having died due to the new
2661	 * signal, SIGTERM.
2662	 */
2663	wpid = waitpid(fpid, &status, 0);
2664	ATF_REQUIRE(wpid == fpid);
2665	ATF_REQUIRE(WIFSIGNALED(status));
2666	ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2667
2668	wpid = wait(&status);
2669	ATF_REQUIRE(wpid == -1);
2670	ATF_REQUIRE(errno == ECHILD);
2671}
2672
2673/*
2674 * Verify that a signal can be passed through to the child even when there
2675 * was no true signal originally. Such cases arise when a SIGTRAP is
2676 * invented for e.g, system call stops.
2677 */
2678ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2679ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2680{
2681	struct ptrace_lwpinfo pl;
2682	struct rlimit rl;
2683	pid_t fpid, wpid;
2684	int status;
2685
2686	ATF_REQUIRE((fpid = fork()) != -1);
2687	if (fpid == 0) {
2688		trace_me();
2689		/* SIGTRAP expected to cause exit on syscall entry. */
2690		rl.rlim_cur = rl.rlim_max = 0;
2691		ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2692		getpid();
2693		exit(1);
2694	}
2695
2696	/* The first wait() should report the stop from SIGSTOP. */
2697	wpid = waitpid(fpid, &status, 0);
2698	ATF_REQUIRE(wpid == fpid);
2699	ATF_REQUIRE(WIFSTOPPED(status));
2700	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2701
2702	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2703	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2704
2705	/* The second wait() should report a system call entry for getpid(). */
2706	wpid = waitpid(fpid, &status, 0);
2707	ATF_REQUIRE(wpid == fpid);
2708	ATF_REQUIRE(WIFSTOPPED(status));
2709	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2710
2711	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2712	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2713
2714	/* Continue the child process with a SIGTRAP. */
2715	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2716
2717	for (;;) {
2718		/*
2719		 * The last wait() should report exit due to SIGTRAP.  In the
2720		 * meantime, catch and proceed past any syscall stops.
2721		 */
2722		wpid = waitpid(fpid, &status, 0);
2723		ATF_REQUIRE(wpid == fpid);
2724		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2725			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2726			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2727			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2728		} else {
2729			ATF_REQUIRE(WIFSIGNALED(status));
2730			ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2731			break;
2732		}
2733	}
2734
2735	wpid = wait(&status);
2736	ATF_REQUIRE(wpid == -1);
2737	ATF_REQUIRE(errno == ECHILD);
2738
2739}
2740
2741/*
2742 * A mixed bag PT_CONTINUE with signal test.
2743 */
2744ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2745ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2746{
2747	struct ptrace_lwpinfo pl;
2748	pid_t fpid, wpid;
2749	int status;
2750
2751	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2752
2753	ATF_REQUIRE((fpid = fork()) != -1);
2754	if (fpid == 0) {
2755		trace_me();
2756		getpid();
2757		exit(1);
2758	}
2759
2760	/* The first wait() should report the stop from SIGSTOP. */
2761	wpid = waitpid(fpid, &status, 0);
2762	ATF_REQUIRE(wpid == fpid);
2763	ATF_REQUIRE(WIFSTOPPED(status));
2764	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2765
2766	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2767	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2768
2769	/* The second wait() should report a system call entry for getpid(). */
2770	wpid = waitpid(fpid, &status, 0);
2771	ATF_REQUIRE(wpid == fpid);
2772	ATF_REQUIRE(WIFSTOPPED(status));
2773	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2774
2775	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2776	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2777
2778	/* Continue with the first SIGUSR1. */
2779	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2780
2781	/* The next wait() should report a system call exit for getpid(). */
2782	wpid = waitpid(fpid, &status, 0);
2783	ATF_REQUIRE(wpid == fpid);
2784	ATF_REQUIRE(WIFSTOPPED(status));
2785	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2786
2787	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2788	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2789
2790	/* Send an ABRT without ptrace. */
2791	ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2792
2793	/* Continue normally. */
2794	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2795
2796	/* The next wait() should report the SIGABRT. */
2797	wpid = waitpid(fpid, &status, 0);
2798	ATF_REQUIRE(wpid == fpid);
2799	ATF_REQUIRE(WIFSTOPPED(status));
2800	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2801
2802	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2803	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2804	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2805
2806	/* Continue, replacing the SIGABRT with another SIGUSR1. */
2807	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2808
2809	for (;;) {
2810		/*
2811		 * The last wait() should report exit 2, i.e., a normal _exit
2812		 * from the signal handler. In the meantime, catch and proceed
2813		 * past any syscall stops.
2814		 */
2815		wpid = waitpid(fpid, &status, 0);
2816		ATF_REQUIRE(wpid == fpid);
2817		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2818			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2819			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2820			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2821		} else {
2822			ATF_REQUIRE(WIFEXITED(status));
2823			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2824			break;
2825		}
2826	}
2827
2828	wpid = wait(&status);
2829	ATF_REQUIRE(wpid == -1);
2830	ATF_REQUIRE(errno == ECHILD);
2831
2832}
2833
2834/*
2835 * Verify a signal delivered by ptrace is noticed by kevent(2).
2836 */
2837ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2838ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2839{
2840	pid_t fpid, wpid;
2841	int status, kq, nevents;
2842	struct kevent kev;
2843
2844	ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2845
2846	ATF_REQUIRE((fpid = fork()) != -1);
2847	if (fpid == 0) {
2848		CHILD_REQUIRE((kq = kqueue()) > 0);
2849		EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2850		CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2851
2852		trace_me();
2853
2854		for (;;) {
2855			nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2856			if (nevents == -1 && errno == EINTR)
2857				continue;
2858			CHILD_REQUIRE(nevents > 0);
2859			CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2860			CHILD_REQUIRE(kev.ident == SIGUSR1);
2861			break;
2862		}
2863
2864		exit(1);
2865	}
2866
2867	/* The first wait() should report the stop from SIGSTOP. */
2868	wpid = waitpid(fpid, &status, 0);
2869	ATF_REQUIRE(wpid == fpid);
2870	ATF_REQUIRE(WIFSTOPPED(status));
2871	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2872
2873	/* Continue with the SIGUSR1. */
2874	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2875
2876	/*
2877	 * The last wait() should report normal exit with code 1.
2878	 */
2879	wpid = waitpid(fpid, &status, 0);
2880	ATF_REQUIRE(wpid == fpid);
2881	ATF_REQUIRE(WIFEXITED(status));
2882	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2883
2884	wpid = wait(&status);
2885	ATF_REQUIRE(wpid == -1);
2886	ATF_REQUIRE(errno == ECHILD);
2887}
2888
2889static void *
2890signal_thread(void *arg)
2891{
2892	int err;
2893	sigset_t sigmask;
2894
2895	pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2896
2897	/* Wait for this thread to receive a SIGUSR1. */
2898	do {
2899		err = sem_wait(&sigusr1_sem);
2900		CHILD_REQUIRE(err == 0 || errno == EINTR);
2901	} while (err != 0 && errno == EINTR);
2902
2903	/* Free our companion thread from the barrier. */
2904	pthread_barrier_wait(pbarrier);
2905
2906	/*
2907	 * Swap ignore duties; the next SIGUSR1 should go to the
2908	 * other thread.
2909	 */
2910	CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2911	CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2912	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2913
2914	/* Sync up threads after swapping signal masks. */
2915	pthread_barrier_wait(pbarrier);
2916
2917	/* Wait until our companion has received its SIGUSR1. */
2918	pthread_barrier_wait(pbarrier);
2919
2920	return (NULL);
2921}
2922
2923/*
2924 * Verify that a traced process with blocked signal received the
2925 * signal from kill() once unmasked.
2926 */
2927ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2928ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2929{
2930	struct ptrace_lwpinfo pl;
2931	pid_t fpid, wpid;
2932	int status, err;
2933	sigset_t sigmask;
2934
2935	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2936	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2937	got_usr1 = 0;
2938
2939	ATF_REQUIRE((fpid = fork()) != -1);
2940	if (fpid == 0) {
2941		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2942		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2943		CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2944
2945		trace_me();
2946		CHILD_REQUIRE(got_usr1 == 0);
2947
2948		/* Allow the pending SIGUSR1 in now. */
2949		CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2950		/* Wait to receive a SIGUSR1. */
2951		do {
2952			err = sem_wait(&sigusr1_sem);
2953			CHILD_REQUIRE(err == 0 || errno == EINTR);
2954		} while (err != 0 && errno == EINTR);
2955		CHILD_REQUIRE(got_usr1 == 1);
2956		exit(1);
2957	}
2958
2959	/* The first wait() should report the stop from SIGSTOP. */
2960	wpid = waitpid(fpid, &status, 0);
2961	ATF_REQUIRE(wpid == fpid);
2962	ATF_REQUIRE(WIFSTOPPED(status));
2963	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2964	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2965	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2966
2967	/* Send blocked SIGUSR1 which should cause a stop. */
2968	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2969
2970	/* Continue the child ignoring the SIGSTOP. */
2971	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2972
2973	/* The next wait() should report the kill(SIGUSR1) was received. */
2974	wpid = waitpid(fpid, &status, 0);
2975	ATF_REQUIRE(wpid == fpid);
2976	ATF_REQUIRE(WIFSTOPPED(status));
2977	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2978	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2979	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2980
2981	/* Continue the child, allowing in the SIGUSR1. */
2982	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2983
2984	/* The last wait() should report normal exit with code 1. */
2985	wpid = waitpid(fpid, &status, 0);
2986	ATF_REQUIRE(wpid == fpid);
2987	ATF_REQUIRE(WIFEXITED(status));
2988	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2989
2990	wpid = wait(&status);
2991	ATF_REQUIRE(wpid == -1);
2992	ATF_REQUIRE(errno == ECHILD);
2993}
2994
2995/*
2996 * Verify that a traced process with blocked signal received the
2997 * signal from PT_CONTINUE once unmasked.
2998 */
2999ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
3000ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
3001{
3002	struct ptrace_lwpinfo pl;
3003	pid_t fpid, wpid;
3004	int status, err;
3005	sigset_t sigmask;
3006
3007	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3008	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3009	got_usr1 = 0;
3010
3011	ATF_REQUIRE((fpid = fork()) != -1);
3012	if (fpid == 0) {
3013		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3014		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3015		CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
3016
3017		trace_me();
3018		CHILD_REQUIRE(got_usr1 == 0);
3019
3020		/* Allow the pending SIGUSR1 in now. */
3021		CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3022		/* Wait to receive a SIGUSR1. */
3023		do {
3024			err = sem_wait(&sigusr1_sem);
3025			CHILD_REQUIRE(err == 0 || errno == EINTR);
3026		} while (err != 0 && errno == EINTR);
3027
3028		CHILD_REQUIRE(got_usr1 == 1);
3029		exit(1);
3030	}
3031
3032	/* The first wait() should report the stop from SIGSTOP. */
3033	wpid = waitpid(fpid, &status, 0);
3034	ATF_REQUIRE(wpid == fpid);
3035	ATF_REQUIRE(WIFSTOPPED(status));
3036	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3037	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3038	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3039
3040	/* Continue the child replacing SIGSTOP with SIGUSR1. */
3041	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3042
3043	/* The next wait() should report the SIGUSR1 was received. */
3044	wpid = waitpid(fpid, &status, 0);
3045	ATF_REQUIRE(wpid == fpid);
3046	ATF_REQUIRE(WIFSTOPPED(status));
3047	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3048	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3049	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3050
3051	/* Continue the child, ignoring the SIGUSR1. */
3052	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3053
3054	/* The last wait() should report normal exit with code 1. */
3055	wpid = waitpid(fpid, &status, 0);
3056	ATF_REQUIRE(wpid == fpid);
3057	ATF_REQUIRE(WIFEXITED(status));
3058	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3059
3060	wpid = wait(&status);
3061	ATF_REQUIRE(wpid == -1);
3062	ATF_REQUIRE(errno == ECHILD);
3063}
3064
3065/*
3066 * Verify that if ptrace stops due to a signal but continues with
3067 * a different signal that the new signal is routed to a thread
3068 * that can accept it, and that that thread is awakened by the signal
3069 * in a timely manner.
3070 */
3071ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3072ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3073{
3074	pid_t fpid, wpid;
3075	int status, err;
3076	pthread_t t;
3077	sigset_t sigmask;
3078	pthread_barrier_t barrier;
3079
3080	ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3081	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3082	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3083
3084	ATF_REQUIRE((fpid = fork()) != -1);
3085	if (fpid == 0) {
3086		CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3087
3088		/* The other thread should receive the first SIGUSR1. */
3089		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3090		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3091		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3092
3093		trace_me();
3094
3095		/* Wait until other thread has received its SIGUSR1. */
3096		pthread_barrier_wait(&barrier);
3097
3098		/*
3099		 * Swap ignore duties; the next SIGUSR1 should go to this
3100		 * thread.
3101		 */
3102		CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3103
3104		/* Sync up threads after swapping signal masks. */
3105		pthread_barrier_wait(&barrier);
3106
3107		/*
3108		 * Sync up with test code; we're ready for the next SIGUSR1
3109		 * now.
3110		 */
3111		raise(SIGSTOP);
3112
3113		/* Wait for this thread to receive a SIGUSR1. */
3114		do {
3115			err = sem_wait(&sigusr1_sem);
3116			CHILD_REQUIRE(err == 0 || errno == EINTR);
3117		} while (err != 0 && errno == EINTR);
3118
3119		/* Free the other thread from the barrier. */
3120		pthread_barrier_wait(&barrier);
3121
3122		CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3123
3124		exit(1);
3125	}
3126
3127	/* The first wait() should report the stop from SIGSTOP. */
3128	wpid = waitpid(fpid, &status, 0);
3129	ATF_REQUIRE(wpid == fpid);
3130	ATF_REQUIRE(WIFSTOPPED(status));
3131	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3132
3133	/* Continue the child ignoring the SIGSTOP. */
3134	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3135
3136	/*
3137	 * Send a signal without ptrace that either thread will accept (USR2,
3138	 * in this case).
3139	 */
3140	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3141
3142	/* The second wait() should report a SIGUSR2 was received. */
3143	wpid = waitpid(fpid, &status, 0);
3144	ATF_REQUIRE(wpid == fpid);
3145	ATF_REQUIRE(WIFSTOPPED(status));
3146	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3147
3148	/* Continue the child, changing the signal to USR1. */
3149	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3150
3151	/* The next wait() should report the stop from SIGSTOP. */
3152	wpid = waitpid(fpid, &status, 0);
3153	ATF_REQUIRE(wpid == fpid);
3154	ATF_REQUIRE(WIFSTOPPED(status));
3155	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3156
3157	/* Continue the child ignoring the SIGSTOP. */
3158	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3159
3160	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3161
3162	/* The next wait() should report a SIGUSR2 was received. */
3163	wpid = waitpid(fpid, &status, 0);
3164	ATF_REQUIRE(wpid == fpid);
3165	ATF_REQUIRE(WIFSTOPPED(status));
3166	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3167
3168	/* Continue the child, changing the signal to USR1. */
3169	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3170
3171	/* The last wait() should report normal exit with code 1. */
3172	wpid = waitpid(fpid, &status, 0);
3173	ATF_REQUIRE(wpid == fpid);
3174	ATF_REQUIRE(WIFEXITED(status));
3175	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3176
3177	wpid = wait(&status);
3178	ATF_REQUIRE(wpid == -1);
3179	ATF_REQUIRE(errno == ECHILD);
3180}
3181
3182static void *
3183raise_sigstop_thread(void *arg __unused)
3184{
3185
3186	raise(SIGSTOP);
3187	return NULL;
3188}
3189
3190static void *
3191sleep_thread(void *arg __unused)
3192{
3193
3194	sleep(60);
3195	return NULL;
3196}
3197
3198static void
3199terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3200{
3201	pid_t fpid, wpid;
3202	int status, i;
3203	cpuset_t setmask;
3204	cpusetid_t setid;
3205	pthread_t t;
3206
3207	/*
3208	 * Become the reaper for this process tree. We need to be able to check
3209	 * that both child and grandchild have died.
3210	 */
3211	ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3212
3213	fpid = fork();
3214	ATF_REQUIRE(fpid >= 0);
3215	if (fpid == 0) {
3216		fpid = fork();
3217		CHILD_REQUIRE(fpid >= 0);
3218		if (fpid == 0) {
3219			trace_me();
3220
3221			/* Pin to CPU 0 to serialize thread execution. */
3222			CPU_ZERO(&setmask);
3223			CPU_SET(0, &setmask);
3224			CHILD_REQUIRE(cpuset(&setid) == 0);
3225			CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3226			    CPU_WHICH_CPUSET, setid,
3227			    sizeof(setmask), &setmask) == 0);
3228
3229			if (sigstop_from_main_thread) {
3230				/*
3231				 * We expect the SIGKILL sent when our parent
3232				 * dies to be delivered to the new thread.
3233				 * Raise the SIGSTOP in this thread so the
3234				 * threads compete.
3235				 */
3236				CHILD_REQUIRE(pthread_create(&t, NULL,
3237				    sleep_thread, NULL) == 0);
3238				raise(SIGSTOP);
3239			} else {
3240				/*
3241				 * We expect the SIGKILL to be delivered to
3242				 * this thread. After creating the new thread,
3243				 * just get off the CPU so the other thread can
3244				 * raise the SIGSTOP.
3245				 */
3246				CHILD_REQUIRE(pthread_create(&t, NULL,
3247				    raise_sigstop_thread, NULL) == 0);
3248				sleep(60);
3249			}
3250
3251			exit(0);
3252		}
3253		/* First stop is trace_me() immediately after fork. */
3254		wpid = waitpid(fpid, &status, 0);
3255		CHILD_REQUIRE(wpid == fpid);
3256		CHILD_REQUIRE(WIFSTOPPED(status));
3257		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3258
3259		CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3260
3261		/* Second stop is from the raise(SIGSTOP). */
3262		wpid = waitpid(fpid, &status, 0);
3263		CHILD_REQUIRE(wpid == fpid);
3264		CHILD_REQUIRE(WIFSTOPPED(status));
3265		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3266
3267		/*
3268		 * Terminate tracing process without detaching. Our child
3269		 * should be killed.
3270		 */
3271		exit(0);
3272	}
3273
3274	/*
3275	 * We should get a normal exit from our immediate child and a SIGKILL
3276	 * exit from our grandchild. The latter case is the interesting one.
3277	 * Our grandchild should not have stopped due to the SIGSTOP that was
3278	 * left dangling when its parent died.
3279	 */
3280	for (i = 0; i < 2; ++i) {
3281		wpid = wait(&status);
3282		if (wpid == fpid)
3283			ATF_REQUIRE(WIFEXITED(status) &&
3284			    WEXITSTATUS(status) == 0);
3285		else
3286			ATF_REQUIRE(WIFSIGNALED(status) &&
3287			    WTERMSIG(status) == SIGKILL);
3288	}
3289}
3290
3291/*
3292 * These two tests ensure that if the tracing process exits without detaching
3293 * just after the child received a SIGSTOP, the child is cleanly killed and
3294 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3295 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3296 * different threads, the SIGKILL must win.  There are two variants of this
3297 * test, designed to catch the case where the SIGKILL is delivered to the
3298 * younger thread (the first test) and the case where the SIGKILL is delivered
3299 * to the older thread (the second test). This behavior has changed in the
3300 * past, so make no assumption.
3301 */
3302ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3303ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3304{
3305
3306	atf_tc_set_md_var(tc, "require.user", "root");
3307}
3308ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3309{
3310
3311	terminate_with_pending_sigstop(true);
3312}
3313
3314ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3315ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3316{
3317
3318	atf_tc_set_md_var(tc, "require.user", "root");
3319}
3320ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3321{
3322
3323	terminate_with_pending_sigstop(false);
3324}
3325
3326/*
3327 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3328 * is not modified.
3329 */
3330ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3331ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3332{
3333	struct ptrace_lwpinfo pl;
3334	pid_t fpid, wpid;
3335	int status, event_mask, new_event_mask;
3336
3337	ATF_REQUIRE((fpid = fork()) != -1);
3338	if (fpid == 0) {
3339		trace_me();
3340		raise(SIGSTOP);
3341		exit(0);
3342	}
3343
3344	/* The first wait() should report the stop from trace_me(). */
3345	wpid = waitpid(fpid, &status, 0);
3346	ATF_REQUIRE(wpid == fpid);
3347	ATF_REQUIRE(WIFSTOPPED(status));
3348	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3349
3350	/* Set several unobtrusive event bits. */
3351	event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3352	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3353	    sizeof(event_mask)) == 0);
3354
3355	/* Send a SIGKILL without using ptrace. */
3356	ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3357
3358	/* Continue the child ignoring the SIGSTOP. */
3359	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3360
3361	/* The next stop should be due to the SIGKILL. */
3362	wpid = waitpid(fpid, &status, 0);
3363	ATF_REQUIRE(wpid == fpid);
3364	ATF_REQUIRE(WIFSTOPPED(status));
3365	ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3366
3367	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3368	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3369	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3370
3371	/* Continue the child ignoring the SIGKILL. */
3372	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3373
3374	/* The next wait() should report the stop from SIGSTOP. */
3375	wpid = waitpid(fpid, &status, 0);
3376	ATF_REQUIRE(wpid == fpid);
3377	ATF_REQUIRE(WIFSTOPPED(status));
3378	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3379
3380	/* Check the current event mask. It should not have changed. */
3381	new_event_mask = 0;
3382	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3383	    sizeof(new_event_mask)) == 0);
3384	ATF_REQUIRE(event_mask == new_event_mask);
3385
3386	/* Continue the child to let it exit. */
3387	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3388
3389	/* The last event should be for the child process's exit. */
3390	wpid = waitpid(fpid, &status, 0);
3391	ATF_REQUIRE(WIFEXITED(status));
3392	ATF_REQUIRE(WEXITSTATUS(status) == 0);
3393
3394	wpid = wait(&status);
3395	ATF_REQUIRE(wpid == -1);
3396	ATF_REQUIRE(errno == ECHILD);
3397}
3398
3399static void *
3400flock_thread(void *arg)
3401{
3402	int fd;
3403
3404	fd = *(int *)arg;
3405	(void)flock(fd, LOCK_EX);
3406	(void)flock(fd, LOCK_UN);
3407	return (NULL);
3408}
3409
3410/*
3411 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3412 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3413 * on a lock. This is a regression test for r318191.
3414 */
3415ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3416ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3417{
3418	pthread_barrier_t barrier;
3419	pthread_barrierattr_t battr;
3420	char tmpfile[64];
3421	pid_t child, wpid;
3422	int error, fd, i, status;
3423
3424	ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3425	ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3426	    PTHREAD_PROCESS_SHARED) == 0);
3427	ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3428
3429	(void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3430	fd = mkstemp(tmpfile);
3431	ATF_REQUIRE(fd >= 0);
3432
3433	ATF_REQUIRE((child = fork()) != -1);
3434	if (child == 0) {
3435		pthread_t t[2];
3436		int cfd;
3437
3438		error = pthread_barrier_wait(&barrier);
3439		if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3440			_exit(1);
3441
3442		cfd = open(tmpfile, O_RDONLY);
3443		if (cfd < 0)
3444			_exit(1);
3445
3446		/*
3447		 * We want at least two threads blocked on the file lock since
3448		 * the SIGSTOP from PT_ATTACH may kick one of them out of
3449		 * sleep.
3450		 */
3451		if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3452			_exit(1);
3453		if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3454			_exit(1);
3455		if (pthread_join(t[0], NULL) != 0)
3456			_exit(1);
3457		if (pthread_join(t[1], NULL) != 0)
3458			_exit(1);
3459		_exit(0);
3460	}
3461
3462	ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3463
3464	error = pthread_barrier_wait(&barrier);
3465	ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3466
3467	/*
3468	 * Give the child some time to block. Is there a better way to do this?
3469	 */
3470	sleep(1);
3471
3472	/*
3473	 * Attach and give the child 3 seconds to stop.
3474	 */
3475	ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3476	for (i = 0; i < 3; i++) {
3477		wpid = waitpid(child, &status, WNOHANG);
3478		if (wpid == child && WIFSTOPPED(status) &&
3479		    WSTOPSIG(status) == SIGSTOP)
3480			break;
3481		sleep(1);
3482	}
3483	ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3484
3485	ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3486
3487	ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3488	ATF_REQUIRE(unlink(tmpfile) == 0);
3489	ATF_REQUIRE(close(fd) == 0);
3490}
3491
3492static void
3493sigusr1_step_handler(int sig)
3494{
3495
3496	CHILD_REQUIRE(sig == SIGUSR1);
3497	raise(SIGABRT);
3498}
3499
3500/*
3501 * Verify that PT_STEP with a signal invokes the signal before
3502 * stepping the next instruction (and that the next instruction is
3503 * stepped correctly).
3504 */
3505ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3506ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3507{
3508	struct ptrace_lwpinfo pl;
3509	pid_t fpid, wpid;
3510	int status;
3511
3512	ATF_REQUIRE((fpid = fork()) != -1);
3513	if (fpid == 0) {
3514		trace_me();
3515		signal(SIGUSR1, sigusr1_step_handler);
3516		raise(SIGABRT);
3517		exit(1);
3518	}
3519
3520	/* The first wait() should report the stop from SIGSTOP. */
3521	wpid = waitpid(fpid, &status, 0);
3522	ATF_REQUIRE(wpid == fpid);
3523	ATF_REQUIRE(WIFSTOPPED(status));
3524	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3525
3526	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3527
3528	/* The next stop should report the SIGABRT in the child body. */
3529	wpid = waitpid(fpid, &status, 0);
3530	ATF_REQUIRE(wpid == fpid);
3531	ATF_REQUIRE(WIFSTOPPED(status));
3532	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3533
3534	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3535	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3536	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3537
3538	/* Step the child process inserting SIGUSR1. */
3539	ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3540
3541	/* The next stop should report the SIGABRT in the signal handler. */
3542	wpid = waitpid(fpid, &status, 0);
3543	ATF_REQUIRE(wpid == fpid);
3544	ATF_REQUIRE(WIFSTOPPED(status));
3545	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3546
3547	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3548	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3549	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3550
3551	/* Continue the child process discarding the signal. */
3552	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3553
3554	/* The next stop should report a trace trap from PT_STEP. */
3555	wpid = waitpid(fpid, &status, 0);
3556	ATF_REQUIRE(wpid == fpid);
3557	ATF_REQUIRE(WIFSTOPPED(status));
3558	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3559
3560	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3561	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3562	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3563	ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3564
3565	/* Continue the child to let it exit. */
3566	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3567
3568	/* The last event should be for the child process's exit. */
3569	wpid = waitpid(fpid, &status, 0);
3570	ATF_REQUIRE(WIFEXITED(status));
3571	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3572
3573	wpid = wait(&status);
3574	ATF_REQUIRE(wpid == -1);
3575	ATF_REQUIRE(errno == ECHILD);
3576}
3577
3578#ifdef HAVE_BREAKPOINT
3579/*
3580 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3581 * for a breakpoint trap.
3582 */
3583ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3584ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3585{
3586	struct ptrace_lwpinfo pl;
3587	pid_t fpid, wpid;
3588	int status;
3589
3590	ATF_REQUIRE((fpid = fork()) != -1);
3591	if (fpid == 0) {
3592		trace_me();
3593		breakpoint();
3594		exit(1);
3595	}
3596
3597	/* The first wait() should report the stop from SIGSTOP. */
3598	wpid = waitpid(fpid, &status, 0);
3599	ATF_REQUIRE(wpid == fpid);
3600	ATF_REQUIRE(WIFSTOPPED(status));
3601	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3602
3603	/* Continue the child ignoring the SIGSTOP. */
3604	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3605
3606	/* The second wait() should report hitting the breakpoint. */
3607	wpid = waitpid(fpid, &status, 0);
3608	ATF_REQUIRE(wpid == fpid);
3609	ATF_REQUIRE(WIFSTOPPED(status));
3610	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3611
3612	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3613	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3614	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3615	ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3616
3617	/* Kill the child process. */
3618	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3619
3620	/* The last wait() should report the SIGKILL. */
3621	wpid = waitpid(fpid, &status, 0);
3622	ATF_REQUIRE(wpid == fpid);
3623	ATF_REQUIRE(WIFSIGNALED(status));
3624	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3625
3626	wpid = wait(&status);
3627	ATF_REQUIRE(wpid == -1);
3628	ATF_REQUIRE(errno == ECHILD);
3629}
3630#endif /* HAVE_BREAKPOINT */
3631
3632/*
3633 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3634 * for a single-step trap from PT_STEP.
3635 */
3636ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3637ATF_TC_BODY(ptrace__step_siginfo, tc)
3638{
3639	struct ptrace_lwpinfo pl;
3640	pid_t fpid, wpid;
3641	int status;
3642
3643	ATF_REQUIRE((fpid = fork()) != -1);
3644	if (fpid == 0) {
3645		trace_me();
3646		exit(1);
3647	}
3648
3649	/* The first wait() should report the stop from SIGSTOP. */
3650	wpid = waitpid(fpid, &status, 0);
3651	ATF_REQUIRE(wpid == fpid);
3652	ATF_REQUIRE(WIFSTOPPED(status));
3653	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3654
3655	/* Step the child ignoring the SIGSTOP. */
3656	ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3657
3658	/* The second wait() should report a single-step trap. */
3659	wpid = waitpid(fpid, &status, 0);
3660	ATF_REQUIRE(wpid == fpid);
3661	ATF_REQUIRE(WIFSTOPPED(status));
3662	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3663
3664	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3665	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3666	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3667	ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3668
3669	/* Continue the child process. */
3670	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3671
3672	/* The last event should be for the child process's exit. */
3673	wpid = waitpid(fpid, &status, 0);
3674	ATF_REQUIRE(WIFEXITED(status));
3675	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3676
3677	wpid = wait(&status);
3678	ATF_REQUIRE(wpid == -1);
3679	ATF_REQUIRE(errno == ECHILD);
3680}
3681
3682#if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3683static void *
3684continue_thread(void *arg __unused)
3685{
3686	breakpoint();
3687	return (NULL);
3688}
3689
3690static __dead2 void
3691continue_thread_main(void)
3692{
3693	pthread_t threads[2];
3694
3695	CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3696	    NULL) == 0);
3697	CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3698	    NULL) == 0);
3699	CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3700	CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3701	exit(1);
3702}
3703
3704/*
3705 * Ensure that PT_CONTINUE clears the status of the thread that
3706 * triggered the stop even if a different thread's LWP was passed to
3707 * PT_CONTINUE.
3708 */
3709ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3710ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3711{
3712	struct ptrace_lwpinfo pl;
3713	pid_t fpid, wpid;
3714	lwpid_t lwps[2];
3715	bool hit_break[2];
3716	struct reg reg;
3717	int i, j, status;
3718
3719	ATF_REQUIRE((fpid = fork()) != -1);
3720	if (fpid == 0) {
3721		trace_me();
3722		continue_thread_main();
3723	}
3724
3725	/* The first wait() should report the stop from SIGSTOP. */
3726	wpid = waitpid(fpid, &status, 0);
3727	ATF_REQUIRE(wpid == fpid);
3728	ATF_REQUIRE(WIFSTOPPED(status));
3729	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3730
3731	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3732	    sizeof(pl)) != -1);
3733
3734	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3735
3736	/* Continue the child ignoring the SIGSTOP. */
3737	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3738
3739	/* One of the new threads should report it's birth. */
3740	wpid = waitpid(fpid, &status, 0);
3741	ATF_REQUIRE(wpid == fpid);
3742	ATF_REQUIRE(WIFSTOPPED(status));
3743	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3744
3745	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3746	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3747	    (PL_FLAG_BORN | PL_FLAG_SCX));
3748	lwps[0] = pl.pl_lwpid;
3749
3750	/*
3751	 * Suspend this thread to ensure both threads are alive before
3752	 * hitting the breakpoint.
3753	 */
3754	ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3755
3756	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3757
3758	/* Second thread should report it's birth. */
3759	wpid = waitpid(fpid, &status, 0);
3760	ATF_REQUIRE(wpid == fpid);
3761	ATF_REQUIRE(WIFSTOPPED(status));
3762	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3763
3764	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3765	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3766	    (PL_FLAG_BORN | PL_FLAG_SCX));
3767	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3768	lwps[1] = pl.pl_lwpid;
3769
3770	/* Resume both threads waiting for breakpoint events. */
3771	hit_break[0] = hit_break[1] = false;
3772	ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3773	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3774
3775	/* One thread should report a breakpoint. */
3776	wpid = waitpid(fpid, &status, 0);
3777	ATF_REQUIRE(wpid == fpid);
3778	ATF_REQUIRE(WIFSTOPPED(status));
3779	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3780
3781	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3782	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3783	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3784	    pl.pl_siginfo.si_code == TRAP_BRKPT);
3785	if (pl.pl_lwpid == lwps[0])
3786		i = 0;
3787	else
3788		i = 1;
3789	hit_break[i] = true;
3790	ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)&reg, 0) != -1);
3791	SKIP_BREAK(&reg);
3792	ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)&reg, 0) != -1);
3793
3794	/*
3795	 * Resume both threads but pass the other thread's LWPID to
3796	 * PT_CONTINUE.
3797	 */
3798	ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3799
3800	/*
3801	 * Will now get two thread exit events and one more breakpoint
3802	 * event.
3803	 */
3804	for (j = 0; j < 3; j++) {
3805		wpid = waitpid(fpid, &status, 0);
3806		ATF_REQUIRE(wpid == fpid);
3807		ATF_REQUIRE(WIFSTOPPED(status));
3808		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3809
3810		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3811		    sizeof(pl)) != -1);
3812
3813		if (pl.pl_lwpid == lwps[0])
3814			i = 0;
3815		else
3816			i = 1;
3817
3818		ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3819		if (pl.pl_flags & PL_FLAG_EXITED) {
3820			ATF_REQUIRE_MSG(hit_break[i],
3821			    "exited thread did not report breakpoint");
3822			lwps[i] = 0;
3823		} else {
3824			ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3825			ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3826			    pl.pl_siginfo.si_code == TRAP_BRKPT);
3827			ATF_REQUIRE_MSG(!hit_break[i],
3828			    "double breakpoint event");
3829			hit_break[i] = true;
3830			ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)&reg,
3831			    0) != -1);
3832			SKIP_BREAK(&reg);
3833			ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)&reg,
3834			    0) != -1);
3835		}
3836
3837		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3838	}
3839
3840	/* Both threads should have exited. */
3841	ATF_REQUIRE(lwps[0] == 0);
3842	ATF_REQUIRE(lwps[1] == 0);
3843
3844	/* The last event should be for the child process's exit. */
3845	wpid = waitpid(fpid, &status, 0);
3846	ATF_REQUIRE(WIFEXITED(status));
3847	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3848
3849	wpid = wait(&status);
3850	ATF_REQUIRE(wpid == -1);
3851	ATF_REQUIRE(errno == ECHILD);
3852}
3853#endif
3854
3855/*
3856 * Verify that PT_LWPINFO doesn't return stale siginfo.
3857 */
3858ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3859ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3860{
3861	struct ptrace_lwpinfo pl;
3862	pid_t fpid, wpid;
3863	int events, status;
3864
3865	ATF_REQUIRE((fpid = fork()) != -1);
3866	if (fpid == 0) {
3867		trace_me();
3868		raise(SIGABRT);
3869		exit(1);
3870	}
3871
3872	/* The first wait() should report the stop from SIGSTOP. */
3873	wpid = waitpid(fpid, &status, 0);
3874	ATF_REQUIRE(wpid == fpid);
3875	ATF_REQUIRE(WIFSTOPPED(status));
3876	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3877
3878	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3879
3880	/* The next stop should report the SIGABRT in the child body. */
3881	wpid = waitpid(fpid, &status, 0);
3882	ATF_REQUIRE(wpid == fpid);
3883	ATF_REQUIRE(WIFSTOPPED(status));
3884	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3885
3886	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3887	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3888	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3889
3890	/*
3891	 * Continue the process ignoring the signal, but enabling
3892	 * syscall traps.
3893	 */
3894	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3895
3896	/*
3897	 * The next stop should report a system call entry from
3898	 * exit().  PL_FLAGS_SI should not be set.
3899	 */
3900	wpid = waitpid(fpid, &status, 0);
3901	ATF_REQUIRE(wpid == fpid);
3902	ATF_REQUIRE(WIFSTOPPED(status));
3903	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3904
3905	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3906	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3907	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3908
3909	/* Disable syscall tracing and continue the child to let it exit. */
3910	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3911	    sizeof(events)) == 0);
3912	events &= ~PTRACE_SYSCALL;
3913	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3914	    sizeof(events)) == 0);
3915	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3916
3917	/* The last event should be for the child process's exit. */
3918	wpid = waitpid(fpid, &status, 0);
3919	ATF_REQUIRE(WIFEXITED(status));
3920	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3921
3922	wpid = wait(&status);
3923	ATF_REQUIRE(wpid == -1);
3924	ATF_REQUIRE(errno == ECHILD);
3925}
3926
3927ATF_TP_ADD_TCS(tp)
3928{
3929
3930	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3931	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3932	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3933	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3934	ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
3935	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3936	ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3937	ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3938	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3939	ATF_TP_ADD_TC(tp,
3940	    ptrace__follow_fork_child_detached_unrelated_debugger);
3941	ATF_TP_ADD_TC(tp,
3942	    ptrace__follow_fork_parent_detached_unrelated_debugger);
3943	ATF_TP_ADD_TC(tp, ptrace__getppid);
3944	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3945	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3946	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3947	ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3948	ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3949	ATF_TP_ADD_TC(tp, ptrace__siginfo);
3950	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3951	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3952	ATF_TP_ADD_TC(tp, ptrace__event_mask);
3953	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3954	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3955#ifdef HAVE_BREAKPOINT
3956	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3957#endif
3958	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3959	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3960	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3961	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3962	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3963	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3964	ATF_TP_ADD_TC(tp,
3965	    ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3966	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3967	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
3968	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3969	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3970	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3971	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3972	ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
3973	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
3974	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3975	ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3976	ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3977	ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3978	ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
3979	ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
3980#ifdef HAVE_BREAKPOINT
3981	ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
3982#endif
3983	ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
3984#if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3985	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3986#endif
3987	ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
3988
3989	return (atf_no_error());
3990}
3991