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