ptrace_test.c revision 342704
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/10/tests/sys/kern/ptrace_test.c 342704 2019-01-02 19:11:49Z jhb $");
29
30#include <sys/param.h>
31#include <sys/cpuset.h>
32#include <sys/event.h>
33#include <sys/time.h>
34#include <sys/procctl.h>
35#include <sys/ptrace.h>
36#include <sys/queue.h>
37#include <sys/runq.h>
38#include <sys/syscall.h>
39#include <sys/sysctl.h>
40#include <sys/user.h>
41#include <sys/wait.h>
42#include <errno.h>
43#include <machine/cpufunc.h>
44#include <pthread.h>
45#include <sched.h>
46#include <semaphore.h>
47#include <signal.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <unistd.h>
51#include <atf-c.h>
52
53/*
54 * A variant of ATF_REQUIRE that is suitable for use in child
55 * processes.  This only works if the parent process is tripped up by
56 * the early exit and fails some requirement itself.
57 */
58#define	CHILD_REQUIRE(exp) do {						\
59		if (!(exp))						\
60			child_fail_require(__FILE__, __LINE__,		\
61			    #exp " not met");				\
62	} while (0)
63
64static __dead2 void
65child_fail_require(const char *file, int line, const char *str)
66{
67	char buf[128];
68
69	snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
70	write(2, buf, strlen(buf));
71	_exit(32);
72}
73
74static void
75trace_me(void)
76{
77
78	/* Attach the parent process as a tracer of this process. */
79	CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
80
81	/* Trigger a stop. */
82	raise(SIGSTOP);
83}
84
85static void
86attach_child(pid_t pid)
87{
88	pid_t wpid;
89	int status;
90
91	ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
92
93	wpid = waitpid(pid, &status, 0);
94	ATF_REQUIRE(wpid == pid);
95	ATF_REQUIRE(WIFSTOPPED(status));
96	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
97}
98
99static void
100wait_for_zombie(pid_t pid)
101{
102
103	/*
104	 * Wait for a process to exit.  This is kind of gross, but
105	 * there is not a better way.
106	 *
107	 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
108	 * with ESRCH.  After that change, a valid struct kinfo_proc
109	 * is returned for zombies with ki_stat set to SZOMB.
110	 */
111	for (;;) {
112		struct kinfo_proc kp;
113		size_t len;
114		int mib[4];
115
116		mib[0] = CTL_KERN;
117		mib[1] = KERN_PROC;
118		mib[2] = KERN_PROC_PID;
119		mib[3] = pid;
120		len = sizeof(kp);
121		if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
122			ATF_REQUIRE(errno == ESRCH);
123			break;
124		}
125		if (kp.ki_stat == SZOMB)
126			break;
127		usleep(5000);
128	}
129}
130
131/*
132 * Verify that a parent debugger process "sees" the exit of a debugged
133 * process exactly once when attached via PT_TRACE_ME.
134 */
135ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
136ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
137{
138	pid_t child, wpid;
139	int status;
140
141	ATF_REQUIRE((child = fork()) != -1);
142	if (child == 0) {
143		/* Child process. */
144		trace_me();
145
146		_exit(1);
147	}
148
149	/* Parent process. */
150
151	/* The first wait() should report the stop from SIGSTOP. */
152	wpid = waitpid(child, &status, 0);
153	ATF_REQUIRE(wpid == child);
154	ATF_REQUIRE(WIFSTOPPED(status));
155	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
156
157	/* Continue the child ignoring the SIGSTOP. */
158	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
159
160	/* The second wait() should report the exit status. */
161	wpid = waitpid(child, &status, 0);
162	ATF_REQUIRE(wpid == child);
163	ATF_REQUIRE(WIFEXITED(status));
164	ATF_REQUIRE(WEXITSTATUS(status) == 1);
165
166	/* The child should no longer exist. */
167	wpid = waitpid(child, &status, 0);
168	ATF_REQUIRE(wpid == -1);
169	ATF_REQUIRE(errno == ECHILD);
170}
171
172/*
173 * Verify that a parent debugger process "sees" the exit of a debugged
174 * process exactly once when attached via PT_ATTACH.
175 */
176ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
177ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
178{
179	pid_t child, wpid;
180	int cpipe[2], status;
181	char c;
182
183	ATF_REQUIRE(pipe(cpipe) == 0);
184	ATF_REQUIRE((child = fork()) != -1);
185	if (child == 0) {
186		/* Child process. */
187		close(cpipe[0]);
188
189		/* Wait for the parent to attach. */
190		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
191
192		_exit(1);
193	}
194	close(cpipe[1]);
195
196	/* Parent process. */
197
198	/* Attach to the child process. */
199	attach_child(child);
200
201	/* Continue the child ignoring the SIGSTOP. */
202	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
203
204	/* Signal the child to exit. */
205	close(cpipe[0]);
206
207	/* The second wait() should report the exit status. */
208	wpid = waitpid(child, &status, 0);
209	ATF_REQUIRE(wpid == child);
210	ATF_REQUIRE(WIFEXITED(status));
211	ATF_REQUIRE(WEXITSTATUS(status) == 1);
212
213	/* The child should no longer exist. */
214	wpid = waitpid(child, &status, 0);
215	ATF_REQUIRE(wpid == -1);
216	ATF_REQUIRE(errno == ECHILD);
217}
218
219/*
220 * Verify that a parent process "sees" the exit of a debugged process only
221 * after the debugger has seen it.
222 */
223ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
224ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
225{
226	pid_t child, debugger, wpid;
227	int cpipe[2], dpipe[2], status;
228	char c;
229
230	ATF_REQUIRE(pipe(cpipe) == 0);
231	ATF_REQUIRE((child = fork()) != -1);
232
233	if (child == 0) {
234		/* Child process. */
235		close(cpipe[0]);
236
237		/* Wait for parent to be ready. */
238		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
239
240		_exit(1);
241	}
242	close(cpipe[1]);
243
244	ATF_REQUIRE(pipe(dpipe) == 0);
245	ATF_REQUIRE((debugger = fork()) != -1);
246
247	if (debugger == 0) {
248		/* Debugger process. */
249		close(dpipe[0]);
250
251		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
252
253		wpid = waitpid(child, &status, 0);
254		CHILD_REQUIRE(wpid == child);
255		CHILD_REQUIRE(WIFSTOPPED(status));
256		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
257
258		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
259
260		/* Signal parent that debugger is attached. */
261		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
262
263		/* Wait for parent's failed wait. */
264		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
265
266		wpid = waitpid(child, &status, 0);
267		CHILD_REQUIRE(wpid == child);
268		CHILD_REQUIRE(WIFEXITED(status));
269		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
270
271		_exit(0);
272	}
273	close(dpipe[1]);
274
275	/* Parent process. */
276
277	/* Wait for the debugger to attach to the child. */
278	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
279
280	/* Release the child. */
281	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
282	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
283	close(cpipe[0]);
284
285	wait_for_zombie(child);
286
287	/*
288	 * This wait should return a pid of 0 to indicate no status to
289	 * report.  The parent should see the child as non-exited
290	 * until the debugger sees the exit.
291	 */
292	wpid = waitpid(child, &status, WNOHANG);
293	ATF_REQUIRE(wpid == 0);
294
295	/* Signal the debugger to wait for the child. */
296	close(dpipe[0]);
297
298	/* Wait for the debugger. */
299	wpid = waitpid(debugger, &status, 0);
300	ATF_REQUIRE(wpid == debugger);
301	ATF_REQUIRE(WIFEXITED(status));
302	ATF_REQUIRE(WEXITSTATUS(status) == 0);
303
304	/* The child process should now be ready. */
305	wpid = waitpid(child, &status, WNOHANG);
306	ATF_REQUIRE(wpid == child);
307	ATF_REQUIRE(WIFEXITED(status));
308	ATF_REQUIRE(WEXITSTATUS(status) == 1);
309}
310
311/*
312 * Verify that a parent process "sees" the exit of a debugged process
313 * only after a non-direct-child debugger has seen it.  In particular,
314 * various wait() calls in the parent must avoid failing with ESRCH by
315 * checking the parent's orphan list for the debugee.
316 */
317ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
318ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
319{
320	pid_t child, debugger, fpid, wpid;
321	int cpipe[2], dpipe[2], status;
322	char c;
323
324	ATF_REQUIRE(pipe(cpipe) == 0);
325	ATF_REQUIRE((child = fork()) != -1);
326
327	if (child == 0) {
328		/* Child process. */
329		close(cpipe[0]);
330
331		/* Wait for parent to be ready. */
332		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
333
334		_exit(1);
335	}
336	close(cpipe[1]);
337
338	ATF_REQUIRE(pipe(dpipe) == 0);
339	ATF_REQUIRE((debugger = fork()) != -1);
340
341	if (debugger == 0) {
342		/* Debugger parent. */
343
344		/*
345		 * Fork again and drop the debugger parent so that the
346		 * debugger is not a child of the main parent.
347		 */
348		CHILD_REQUIRE((fpid = fork()) != -1);
349		if (fpid != 0)
350			_exit(2);
351
352		/* Debugger process. */
353		close(dpipe[0]);
354
355		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
356
357		wpid = waitpid(child, &status, 0);
358		CHILD_REQUIRE(wpid == child);
359		CHILD_REQUIRE(WIFSTOPPED(status));
360		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
361
362		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
363
364		/* Signal parent that debugger is attached. */
365		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
366
367		/* Wait for parent's failed wait. */
368		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
369
370		wpid = waitpid(child, &status, 0);
371		CHILD_REQUIRE(wpid == child);
372		CHILD_REQUIRE(WIFEXITED(status));
373		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
374
375		_exit(0);
376	}
377	close(dpipe[1]);
378
379	/* Parent process. */
380
381	/* Wait for the debugger parent process to exit. */
382	wpid = waitpid(debugger, &status, 0);
383	ATF_REQUIRE(wpid == debugger);
384	ATF_REQUIRE(WIFEXITED(status));
385	ATF_REQUIRE(WEXITSTATUS(status) == 2);
386
387	/* A WNOHANG wait here should see the non-exited child. */
388	wpid = waitpid(child, &status, WNOHANG);
389	ATF_REQUIRE(wpid == 0);
390
391	/* Wait for the debugger to attach to the child. */
392	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
393
394	/* Release the child. */
395	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
396	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
397	close(cpipe[0]);
398
399	wait_for_zombie(child);
400
401	/*
402	 * This wait should return a pid of 0 to indicate no status to
403	 * report.  The parent should see the child as non-exited
404	 * until the debugger sees the exit.
405	 */
406	wpid = waitpid(child, &status, WNOHANG);
407	ATF_REQUIRE(wpid == 0);
408
409	/* Signal the debugger to wait for the child. */
410	ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
411
412	/* Wait for the debugger. */
413	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
414	close(dpipe[0]);
415
416	/* The child process should now be ready. */
417	wpid = waitpid(child, &status, WNOHANG);
418	ATF_REQUIRE(wpid == child);
419	ATF_REQUIRE(WIFEXITED(status));
420	ATF_REQUIRE(WEXITSTATUS(status) == 1);
421}
422
423/*
424 * The parent process should always act the same regardless of how the
425 * debugger is attached to it.
426 */
427static __dead2 void
428follow_fork_parent(bool use_vfork)
429{
430	pid_t fpid, wpid;
431	int status;
432
433	if (use_vfork)
434		CHILD_REQUIRE((fpid = vfork()) != -1);
435	else
436		CHILD_REQUIRE((fpid = fork()) != -1);
437
438	if (fpid == 0)
439		/* Child */
440		_exit(2);
441
442	wpid = waitpid(fpid, &status, 0);
443	CHILD_REQUIRE(wpid == fpid);
444	CHILD_REQUIRE(WIFEXITED(status));
445	CHILD_REQUIRE(WEXITSTATUS(status) == 2);
446
447	_exit(1);
448}
449
450/*
451 * Helper routine for follow fork tests.  This waits for two stops
452 * that report both "sides" of a fork.  It returns the pid of the new
453 * child process.
454 */
455static pid_t
456handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
457{
458	struct ptrace_lwpinfo pl;
459	bool fork_reported[2];
460	pid_t child, wpid;
461	int i, status;
462
463	fork_reported[0] = false;
464	fork_reported[1] = false;
465	child = -1;
466
467	/*
468	 * Each process should report a fork event.  The parent should
469	 * report a PL_FLAG_FORKED event, and the child should report
470	 * a PL_FLAG_CHILD event.
471	 */
472	for (i = 0; i < 2; i++) {
473		wpid = wait(&status);
474		ATF_REQUIRE(wpid > 0);
475		ATF_REQUIRE(WIFSTOPPED(status));
476
477		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
478		    sizeof(pl)) != -1);
479		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
480		    0);
481		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
482		    (PL_FLAG_FORKED | PL_FLAG_CHILD));
483		if (pl.pl_flags & PL_FLAG_CHILD) {
484			ATF_REQUIRE(wpid != parent);
485			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
486			ATF_REQUIRE(!fork_reported[1]);
487			if (child == -1)
488				child = wpid;
489			else
490				ATF_REQUIRE(child == wpid);
491			if (ppl != NULL)
492				ppl[1] = pl;
493			fork_reported[1] = true;
494		} else {
495			ATF_REQUIRE(wpid == parent);
496			ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
497			ATF_REQUIRE(!fork_reported[0]);
498			if (child == -1)
499				child = pl.pl_child_pid;
500			else
501				ATF_REQUIRE(child == pl.pl_child_pid);
502			if (ppl != NULL)
503				ppl[0] = pl;
504			fork_reported[0] = true;
505		}
506	}
507
508	return (child);
509}
510
511/*
512 * Verify that a new child process is stopped after a followed fork and
513 * that the traced parent sees the exit of the child after the debugger
514 * when both processes remain attached to the debugger.
515 */
516ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
517ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
518{
519	pid_t children[2], fpid, wpid;
520	int status;
521
522	ATF_REQUIRE((fpid = fork()) != -1);
523	if (fpid == 0) {
524		trace_me();
525		follow_fork_parent(false);
526	}
527
528	/* Parent process. */
529	children[0] = fpid;
530
531	/* The first wait() should report the stop from SIGSTOP. */
532	wpid = waitpid(children[0], &status, 0);
533	ATF_REQUIRE(wpid == children[0]);
534	ATF_REQUIRE(WIFSTOPPED(status));
535	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
536
537	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
538
539	/* Continue the child ignoring the SIGSTOP. */
540	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
541
542	children[1] = handle_fork_events(children[0], NULL);
543	ATF_REQUIRE(children[1] > 0);
544
545	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
546	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
547
548	/*
549	 * The child can't exit until the grandchild reports status, so the
550	 * grandchild should report its exit first to the debugger.
551	 */
552	wpid = wait(&status);
553	ATF_REQUIRE(wpid == children[1]);
554	ATF_REQUIRE(WIFEXITED(status));
555	ATF_REQUIRE(WEXITSTATUS(status) == 2);
556
557	wpid = wait(&status);
558	ATF_REQUIRE(wpid == children[0]);
559	ATF_REQUIRE(WIFEXITED(status));
560	ATF_REQUIRE(WEXITSTATUS(status) == 1);
561
562	wpid = wait(&status);
563	ATF_REQUIRE(wpid == -1);
564	ATF_REQUIRE(errno == ECHILD);
565}
566
567/*
568 * Verify that a new child process is stopped after a followed fork
569 * and that the traced parent sees the exit of the child when the new
570 * child process is detached after it reports its fork.
571 */
572ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
573ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
574{
575	pid_t children[2], fpid, wpid;
576	int status;
577
578	ATF_REQUIRE((fpid = fork()) != -1);
579	if (fpid == 0) {
580		trace_me();
581		follow_fork_parent(false);
582	}
583
584	/* Parent process. */
585	children[0] = fpid;
586
587	/* The first wait() should report the stop from SIGSTOP. */
588	wpid = waitpid(children[0], &status, 0);
589	ATF_REQUIRE(wpid == children[0]);
590	ATF_REQUIRE(WIFSTOPPED(status));
591	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
592
593	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
594
595	/* Continue the child ignoring the SIGSTOP. */
596	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
597
598	children[1] = handle_fork_events(children[0], NULL);
599	ATF_REQUIRE(children[1] > 0);
600
601	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
602	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
603
604	/*
605	 * Should not see any status from the grandchild now, only the
606	 * child.
607	 */
608	wpid = wait(&status);
609	ATF_REQUIRE(wpid == children[0]);
610	ATF_REQUIRE(WIFEXITED(status));
611	ATF_REQUIRE(WEXITSTATUS(status) == 1);
612
613	wpid = wait(&status);
614	ATF_REQUIRE(wpid == -1);
615	ATF_REQUIRE(errno == ECHILD);
616}
617
618/*
619 * Verify that a new child process is stopped after a followed fork
620 * and that the traced parent sees the exit of the child when the
621 * traced parent is detached after the fork.
622 */
623ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
624ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
625{
626	pid_t children[2], fpid, wpid;
627	int status;
628
629	ATF_REQUIRE((fpid = fork()) != -1);
630	if (fpid == 0) {
631		trace_me();
632		follow_fork_parent(false);
633	}
634
635	/* Parent process. */
636	children[0] = fpid;
637
638	/* The first wait() should report the stop from SIGSTOP. */
639	wpid = waitpid(children[0], &status, 0);
640	ATF_REQUIRE(wpid == children[0]);
641	ATF_REQUIRE(WIFSTOPPED(status));
642	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
643
644	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
645
646	/* Continue the child ignoring the SIGSTOP. */
647	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
648
649	children[1] = handle_fork_events(children[0], NULL);
650	ATF_REQUIRE(children[1] > 0);
651
652	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
653	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
654
655	/*
656	 * The child can't exit until the grandchild reports status, so the
657	 * grandchild should report its exit first to the debugger.
658	 *
659	 * Even though the child process is detached, it is still a
660	 * child of the debugger, so it will still report it's exit
661	 * after the grandchild.
662	 */
663	wpid = wait(&status);
664	ATF_REQUIRE(wpid == children[1]);
665	ATF_REQUIRE(WIFEXITED(status));
666	ATF_REQUIRE(WEXITSTATUS(status) == 2);
667
668	wpid = wait(&status);
669	ATF_REQUIRE(wpid == children[0]);
670	ATF_REQUIRE(WIFEXITED(status));
671	ATF_REQUIRE(WEXITSTATUS(status) == 1);
672
673	wpid = wait(&status);
674	ATF_REQUIRE(wpid == -1);
675	ATF_REQUIRE(errno == ECHILD);
676}
677
678static void
679attach_fork_parent(int cpipe[2])
680{
681	pid_t fpid;
682
683	close(cpipe[0]);
684
685	/* Double-fork to disassociate from the debugger. */
686	CHILD_REQUIRE((fpid = fork()) != -1);
687	if (fpid != 0)
688		_exit(3);
689
690	/* Send the pid of the disassociated child to the debugger. */
691	fpid = getpid();
692	CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
693
694	/* Wait for the debugger to attach. */
695	CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
696}
697
698/*
699 * Verify that a new child process is stopped after a followed fork and
700 * that the traced parent sees the exit of the child after the debugger
701 * when both processes remain attached to the debugger.  In this test
702 * the parent that forks is not a direct child of the debugger.
703 */
704ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
705ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
706{
707	pid_t children[2], fpid, wpid;
708	int cpipe[2], status;
709
710	ATF_REQUIRE(pipe(cpipe) == 0);
711	ATF_REQUIRE((fpid = fork()) != -1);
712	if (fpid == 0) {
713		attach_fork_parent(cpipe);
714		follow_fork_parent(false);
715	}
716
717	/* Parent process. */
718	close(cpipe[1]);
719
720	/* Wait for the direct child to exit. */
721	wpid = waitpid(fpid, &status, 0);
722	ATF_REQUIRE(wpid == fpid);
723	ATF_REQUIRE(WIFEXITED(status));
724	ATF_REQUIRE(WEXITSTATUS(status) == 3);
725
726	/* Read the pid of the fork parent. */
727	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
728	    sizeof(children[0]));
729
730	/* Attach to the fork parent. */
731	attach_child(children[0]);
732
733	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
734
735	/* Continue the fork parent ignoring the SIGSTOP. */
736	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
737
738	/* Signal the fork parent to continue. */
739	close(cpipe[0]);
740
741	children[1] = handle_fork_events(children[0], NULL);
742	ATF_REQUIRE(children[1] > 0);
743
744	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
745	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
746
747	/*
748	 * The fork parent can't exit until the child reports status,
749	 * so the child should report its exit first to the debugger.
750	 */
751	wpid = wait(&status);
752	ATF_REQUIRE(wpid == children[1]);
753	ATF_REQUIRE(WIFEXITED(status));
754	ATF_REQUIRE(WEXITSTATUS(status) == 2);
755
756	wpid = wait(&status);
757	ATF_REQUIRE(wpid == children[0]);
758	ATF_REQUIRE(WIFEXITED(status));
759	ATF_REQUIRE(WEXITSTATUS(status) == 1);
760
761	wpid = wait(&status);
762	ATF_REQUIRE(wpid == -1);
763	ATF_REQUIRE(errno == ECHILD);
764}
765
766/*
767 * Verify that a new child process is stopped after a followed fork
768 * and that the traced parent sees the exit of the child when the new
769 * child process is detached after it reports its fork.  In this test
770 * the parent that forks is not a direct child of the debugger.
771 */
772ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
773ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
774{
775	pid_t children[2], fpid, wpid;
776	int cpipe[2], status;
777
778	ATF_REQUIRE(pipe(cpipe) == 0);
779	ATF_REQUIRE((fpid = fork()) != -1);
780	if (fpid == 0) {
781		attach_fork_parent(cpipe);
782		follow_fork_parent(false);
783	}
784
785	/* Parent process. */
786	close(cpipe[1]);
787
788	/* Wait for the direct child to exit. */
789	wpid = waitpid(fpid, &status, 0);
790	ATF_REQUIRE(wpid == fpid);
791	ATF_REQUIRE(WIFEXITED(status));
792	ATF_REQUIRE(WEXITSTATUS(status) == 3);
793
794	/* Read the pid of the fork parent. */
795	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
796	    sizeof(children[0]));
797
798	/* Attach to the fork parent. */
799	attach_child(children[0]);
800
801	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
802
803	/* Continue the fork parent ignoring the SIGSTOP. */
804	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
805
806	/* Signal the fork parent to continue. */
807	close(cpipe[0]);
808
809	children[1] = handle_fork_events(children[0], NULL);
810	ATF_REQUIRE(children[1] > 0);
811
812	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
813	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
814
815	/*
816	 * Should not see any status from the child now, only the fork
817	 * parent.
818	 */
819	wpid = wait(&status);
820	ATF_REQUIRE(wpid == children[0]);
821	ATF_REQUIRE(WIFEXITED(status));
822	ATF_REQUIRE(WEXITSTATUS(status) == 1);
823
824	wpid = wait(&status);
825	ATF_REQUIRE(wpid == -1);
826	ATF_REQUIRE(errno == ECHILD);
827}
828
829/*
830 * Verify that a new child process is stopped after a followed fork
831 * and that the traced parent sees the exit of the child when the
832 * traced parent is detached after the fork.  In this test the parent
833 * that forks is not a direct child of the debugger.
834 */
835ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
836ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
837{
838	pid_t children[2], fpid, wpid;
839	int cpipe[2], status;
840
841	ATF_REQUIRE(pipe(cpipe) == 0);
842	ATF_REQUIRE((fpid = fork()) != -1);
843	if (fpid == 0) {
844		attach_fork_parent(cpipe);
845		follow_fork_parent(false);
846	}
847
848	/* Parent process. */
849	close(cpipe[1]);
850
851	/* Wait for the direct child to exit. */
852	wpid = waitpid(fpid, &status, 0);
853	ATF_REQUIRE(wpid == fpid);
854	ATF_REQUIRE(WIFEXITED(status));
855	ATF_REQUIRE(WEXITSTATUS(status) == 3);
856
857	/* Read the pid of the fork parent. */
858	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
859	    sizeof(children[0]));
860
861	/* Attach to the fork parent. */
862	attach_child(children[0]);
863
864	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
865
866	/* Continue the fork parent ignoring the SIGSTOP. */
867	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
868
869	/* Signal the fork parent to continue. */
870	close(cpipe[0]);
871
872	children[1] = handle_fork_events(children[0], NULL);
873	ATF_REQUIRE(children[1] > 0);
874
875	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
876	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
877
878	/*
879	 * Should not see any status from the fork parent now, only
880	 * the child.
881	 */
882	wpid = wait(&status);
883	ATF_REQUIRE(wpid == children[1]);
884	ATF_REQUIRE(WIFEXITED(status));
885	ATF_REQUIRE(WEXITSTATUS(status) == 2);
886
887	wpid = wait(&status);
888	ATF_REQUIRE(wpid == -1);
889	ATF_REQUIRE(errno == ECHILD);
890}
891
892/*
893 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
894 * child process created via fork() reports the correct value.
895 */
896ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
897ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
898{
899	struct ptrace_lwpinfo pl[2];
900	pid_t children[2], fpid, wpid;
901	int status;
902
903	ATF_REQUIRE((fpid = fork()) != -1);
904	if (fpid == 0) {
905		trace_me();
906		follow_fork_parent(false);
907	}
908
909	/* Parent process. */
910	children[0] = fpid;
911
912	/* The first wait() should report the stop from SIGSTOP. */
913	wpid = waitpid(children[0], &status, 0);
914	ATF_REQUIRE(wpid == children[0]);
915	ATF_REQUIRE(WIFSTOPPED(status));
916	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
917
918	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
919
920	/* Continue the child ignoring the SIGSTOP. */
921	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
922
923	/* Wait for both halves of the fork event to get reported. */
924	children[1] = handle_fork_events(children[0], pl);
925	ATF_REQUIRE(children[1] > 0);
926
927	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
928	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
929	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
930	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
931	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
932
933	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
934	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
935
936	/*
937	 * The child can't exit until the grandchild reports status, so the
938	 * grandchild should report its exit first to the debugger.
939	 */
940	wpid = wait(&status);
941	ATF_REQUIRE(wpid == children[1]);
942	ATF_REQUIRE(WIFEXITED(status));
943	ATF_REQUIRE(WEXITSTATUS(status) == 2);
944
945	wpid = wait(&status);
946	ATF_REQUIRE(wpid == children[0]);
947	ATF_REQUIRE(WIFEXITED(status));
948	ATF_REQUIRE(WEXITSTATUS(status) == 1);
949
950	wpid = wait(&status);
951	ATF_REQUIRE(wpid == -1);
952	ATF_REQUIRE(errno == ECHILD);
953}
954
955/*
956 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
957 * child process created via vfork() reports the correct value.
958 */
959ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
960ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
961{
962	struct ptrace_lwpinfo pl[2];
963	pid_t children[2], fpid, wpid;
964	int status;
965
966	ATF_REQUIRE((fpid = fork()) != -1);
967	if (fpid == 0) {
968		trace_me();
969		follow_fork_parent(true);
970	}
971
972	/* Parent process. */
973	children[0] = fpid;
974
975	/* The first wait() should report the stop from SIGSTOP. */
976	wpid = waitpid(children[0], &status, 0);
977	ATF_REQUIRE(wpid == children[0]);
978	ATF_REQUIRE(WIFSTOPPED(status));
979	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
980
981	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
982
983	/* Continue the child ignoring the SIGSTOP. */
984	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
985
986	/* Wait for both halves of the fork event to get reported. */
987	children[1] = handle_fork_events(children[0], pl);
988	ATF_REQUIRE(children[1] > 0);
989
990	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
991	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
992	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
993	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
994	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
995
996	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
997	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
998
999	/*
1000	 * The child can't exit until the grandchild reports status, so the
1001	 * grandchild should report its exit first to the debugger.
1002	 */
1003	wpid = wait(&status);
1004	ATF_REQUIRE(wpid == children[1]);
1005	ATF_REQUIRE(WIFEXITED(status));
1006	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1007
1008	wpid = wait(&status);
1009	ATF_REQUIRE(wpid == children[0]);
1010	ATF_REQUIRE(WIFEXITED(status));
1011	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1012
1013	wpid = wait(&status);
1014	ATF_REQUIRE(wpid == -1);
1015	ATF_REQUIRE(errno == ECHILD);
1016}
1017
1018static void *
1019simple_thread(void *arg __unused)
1020{
1021
1022	pthread_exit(NULL);
1023}
1024
1025static __dead2 void
1026simple_thread_main(void)
1027{
1028	pthread_t thread;
1029
1030	CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1031	CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1032	exit(1);
1033}
1034
1035/*
1036 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1037 * thread reports the correct value.
1038 */
1039ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1040ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1041{
1042	struct ptrace_lwpinfo pl;
1043	pid_t fpid, wpid;
1044	lwpid_t mainlwp;
1045	int status;
1046
1047	ATF_REQUIRE((fpid = fork()) != -1);
1048	if (fpid == 0) {
1049		trace_me();
1050		simple_thread_main();
1051	}
1052
1053	/* The first wait() should report the stop from SIGSTOP. */
1054	wpid = waitpid(fpid, &status, 0);
1055	ATF_REQUIRE(wpid == fpid);
1056	ATF_REQUIRE(WIFSTOPPED(status));
1057	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1058
1059	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1060	    sizeof(pl)) != -1);
1061	mainlwp = pl.pl_lwpid;
1062
1063	/*
1064	 * Continue the child ignoring the SIGSTOP and tracing all
1065	 * system call exits.
1066	 */
1067	ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1068
1069	/*
1070	 * Wait for the new thread to arrive.  pthread_create() might
1071	 * invoke any number of system calls.  For now we just wait
1072	 * for the new thread to arrive and make sure it reports a
1073	 * valid system call code.  If ptrace grows thread event
1074	 * reporting then this test can be made more precise.
1075	 */
1076	for (;;) {
1077		wpid = waitpid(fpid, &status, 0);
1078		ATF_REQUIRE(wpid == fpid);
1079		ATF_REQUIRE(WIFSTOPPED(status));
1080		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1081
1082		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1083		    sizeof(pl)) != -1);
1084		ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1085		ATF_REQUIRE(pl.pl_syscall_code != 0);
1086		if (pl.pl_lwpid != mainlwp)
1087			/* New thread seen. */
1088			break;
1089
1090		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1091	}
1092
1093	/* Wait for the child to exit. */
1094	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1095	for (;;) {
1096		wpid = waitpid(fpid, &status, 0);
1097		ATF_REQUIRE(wpid == fpid);
1098		if (WIFEXITED(status))
1099			break;
1100
1101		ATF_REQUIRE(WIFSTOPPED(status));
1102		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1103		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1104	}
1105
1106	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1107
1108	wpid = wait(&status);
1109	ATF_REQUIRE(wpid == -1);
1110	ATF_REQUIRE(errno == ECHILD);
1111}
1112
1113/*
1114 * Verify that the expected LWP events are reported for a child thread.
1115 */
1116ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1117ATF_TC_BODY(ptrace__lwp_events, tc)
1118{
1119	struct ptrace_lwpinfo pl;
1120	pid_t fpid, wpid;
1121	lwpid_t lwps[2];
1122	int status;
1123
1124	ATF_REQUIRE((fpid = fork()) != -1);
1125	if (fpid == 0) {
1126		trace_me();
1127		simple_thread_main();
1128	}
1129
1130	/* The first wait() should report the stop from SIGSTOP. */
1131	wpid = waitpid(fpid, &status, 0);
1132	ATF_REQUIRE(wpid == fpid);
1133	ATF_REQUIRE(WIFSTOPPED(status));
1134	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1135
1136	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1137	    sizeof(pl)) != -1);
1138	lwps[0] = pl.pl_lwpid;
1139
1140	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1141
1142	/* Continue the child ignoring the SIGSTOP. */
1143	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1144
1145	/* The first event should be for the child thread's birth. */
1146	wpid = waitpid(fpid, &status, 0);
1147	ATF_REQUIRE(wpid == fpid);
1148	ATF_REQUIRE(WIFSTOPPED(status));
1149	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1150
1151	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1152	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1153	    (PL_FLAG_BORN | PL_FLAG_SCX));
1154	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1155	lwps[1] = pl.pl_lwpid;
1156
1157	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1158
1159	/* The next event should be for the child thread's death. */
1160	wpid = waitpid(fpid, &status, 0);
1161	ATF_REQUIRE(wpid == fpid);
1162	ATF_REQUIRE(WIFSTOPPED(status));
1163	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1164
1165	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1166	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1167	    (PL_FLAG_EXITED | PL_FLAG_SCE));
1168	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1169
1170	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1171
1172	/* The last event should be for the child process's exit. */
1173	wpid = waitpid(fpid, &status, 0);
1174	ATF_REQUIRE(WIFEXITED(status));
1175	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1176
1177	wpid = wait(&status);
1178	ATF_REQUIRE(wpid == -1);
1179	ATF_REQUIRE(errno == ECHILD);
1180}
1181
1182static void *
1183exec_thread(void *arg __unused)
1184{
1185
1186	execl("/usr/bin/true", "true", NULL);
1187	exit(127);
1188}
1189
1190static __dead2 void
1191exec_thread_main(void)
1192{
1193	pthread_t thread;
1194
1195	CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1196	for (;;)
1197		sleep(60);
1198	exit(1);
1199}
1200
1201/*
1202 * Verify that the expected LWP events are reported for a multithreaded
1203 * process that calls execve(2).
1204 */
1205ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1206ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1207{
1208	struct ptrace_lwpinfo pl;
1209	pid_t fpid, wpid;
1210	lwpid_t lwps[2];
1211	int status;
1212
1213	ATF_REQUIRE((fpid = fork()) != -1);
1214	if (fpid == 0) {
1215		trace_me();
1216		exec_thread_main();
1217	}
1218
1219	/* The first wait() should report the stop from SIGSTOP. */
1220	wpid = waitpid(fpid, &status, 0);
1221	ATF_REQUIRE(wpid == fpid);
1222	ATF_REQUIRE(WIFSTOPPED(status));
1223	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1224
1225	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1226	    sizeof(pl)) != -1);
1227	lwps[0] = pl.pl_lwpid;
1228
1229	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1230
1231	/* Continue the child ignoring the SIGSTOP. */
1232	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1233
1234	/* The first event should be for the child thread's birth. */
1235	wpid = waitpid(fpid, &status, 0);
1236	ATF_REQUIRE(wpid == fpid);
1237	ATF_REQUIRE(WIFSTOPPED(status));
1238	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1239
1240	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1241	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1242	    (PL_FLAG_BORN | PL_FLAG_SCX));
1243	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1244	lwps[1] = pl.pl_lwpid;
1245
1246	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1247
1248	/*
1249	 * The next event should be for the main thread's death due to
1250	 * single threading from execve().
1251	 */
1252	wpid = waitpid(fpid, &status, 0);
1253	ATF_REQUIRE(wpid == fpid);
1254	ATF_REQUIRE(WIFSTOPPED(status));
1255	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1256
1257	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1258	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1259	    (PL_FLAG_EXITED));
1260	ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1261
1262	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1263
1264	/* The next event should be for the child process's exec. */
1265	wpid = waitpid(fpid, &status, 0);
1266	ATF_REQUIRE(WIFSTOPPED(status));
1267	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1268
1269	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1270	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1271	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1272	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1273
1274	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1275
1276	/* The last event should be for the child process's exit. */
1277	wpid = waitpid(fpid, &status, 0);
1278	ATF_REQUIRE(WIFEXITED(status));
1279	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1280
1281	wpid = wait(&status);
1282	ATF_REQUIRE(wpid == -1);
1283	ATF_REQUIRE(errno == ECHILD);
1284}
1285
1286static void
1287handler(int sig __unused)
1288{
1289}
1290
1291static void
1292signal_main(void)
1293{
1294
1295	signal(SIGINFO, handler);
1296	raise(SIGINFO);
1297	exit(0);
1298}
1299
1300/*
1301 * Verify that the expected ptrace event is reported for a signal.
1302 */
1303ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1304ATF_TC_BODY(ptrace__siginfo, tc)
1305{
1306	struct ptrace_lwpinfo pl;
1307	pid_t fpid, wpid;
1308	int status;
1309
1310	ATF_REQUIRE((fpid = fork()) != -1);
1311	if (fpid == 0) {
1312		trace_me();
1313		signal_main();
1314	}
1315
1316	/* The first wait() should report the stop from SIGSTOP. */
1317	wpid = waitpid(fpid, &status, 0);
1318	ATF_REQUIRE(wpid == fpid);
1319	ATF_REQUIRE(WIFSTOPPED(status));
1320	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1321
1322	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1323
1324	/* The next event should be for the SIGINFO. */
1325	wpid = waitpid(fpid, &status, 0);
1326	ATF_REQUIRE(WIFSTOPPED(status));
1327	ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1328
1329	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1330	ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1331	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1332	ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1333	ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1334
1335	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1336
1337	/* The last event should be for the child process's exit. */
1338	wpid = waitpid(fpid, &status, 0);
1339	ATF_REQUIRE(WIFEXITED(status));
1340	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1341
1342	wpid = wait(&status);
1343	ATF_REQUIRE(wpid == -1);
1344	ATF_REQUIRE(errno == ECHILD);
1345}
1346
1347/*
1348 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1349 */
1350ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1351ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1352{
1353	pid_t fpid, wpid;
1354	int events, status;
1355
1356	ATF_REQUIRE((fpid = fork()) != -1);
1357	if (fpid == 0) {
1358		trace_me();
1359		exec_thread(NULL);
1360	}
1361
1362	/* The first wait() should report the stop from SIGSTOP. */
1363	wpid = waitpid(fpid, &status, 0);
1364	ATF_REQUIRE(wpid == fpid);
1365	ATF_REQUIRE(WIFSTOPPED(status));
1366	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1367
1368	events = 0;
1369	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1370	    sizeof(events)) == 0);
1371
1372	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1373
1374	/* Should get one event at exit. */
1375	wpid = waitpid(fpid, &status, 0);
1376	ATF_REQUIRE(WIFEXITED(status));
1377	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1378
1379	wpid = wait(&status);
1380	ATF_REQUIRE(wpid == -1);
1381	ATF_REQUIRE(errno == ECHILD);
1382}
1383
1384ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1385ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1386{
1387	struct ptrace_lwpinfo pl;
1388	pid_t fpid, wpid;
1389	int events, status;
1390
1391	ATF_REQUIRE((fpid = fork()) != -1);
1392	if (fpid == 0) {
1393		trace_me();
1394		exec_thread(NULL);
1395	}
1396
1397	/* The first wait() should report the stop from SIGSTOP. */
1398	wpid = waitpid(fpid, &status, 0);
1399	ATF_REQUIRE(wpid == fpid);
1400	ATF_REQUIRE(WIFSTOPPED(status));
1401	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1402
1403	events = PTRACE_EXEC;
1404	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1405	    sizeof(events)) == 0);
1406
1407	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1408
1409	/* The next event should be for the child process's exec. */
1410	wpid = waitpid(fpid, &status, 0);
1411	ATF_REQUIRE(WIFSTOPPED(status));
1412	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1413
1414	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1415	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1416	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1417
1418	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1419
1420	/* The last event should be for the child process's exit. */
1421	wpid = waitpid(fpid, &status, 0);
1422	ATF_REQUIRE(WIFEXITED(status));
1423	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1424
1425	wpid = wait(&status);
1426	ATF_REQUIRE(wpid == -1);
1427	ATF_REQUIRE(errno == ECHILD);
1428}
1429
1430ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1431ATF_TC_BODY(ptrace__event_mask, tc)
1432{
1433	pid_t fpid, wpid;
1434	int events, status;
1435
1436	ATF_REQUIRE((fpid = fork()) != -1);
1437	if (fpid == 0) {
1438		trace_me();
1439		exit(0);
1440	}
1441
1442	/* The first wait() should report the stop from SIGSTOP. */
1443	wpid = waitpid(fpid, &status, 0);
1444	ATF_REQUIRE(wpid == fpid);
1445	ATF_REQUIRE(WIFSTOPPED(status));
1446	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1447
1448	/* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1449	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1450	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1451	    sizeof(events)) == 0);
1452	ATF_REQUIRE(events & PTRACE_FORK);
1453	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1454	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1455	    sizeof(events)) == 0);
1456	ATF_REQUIRE(!(events & PTRACE_FORK));
1457
1458	/* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1459	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1460	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1461	    sizeof(events)) == 0);
1462	ATF_REQUIRE(events & PTRACE_LWP);
1463	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1464	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1465	    sizeof(events)) == 0);
1466	ATF_REQUIRE(!(events & PTRACE_LWP));
1467
1468	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1469
1470	/* Should get one event at exit. */
1471	wpid = waitpid(fpid, &status, 0);
1472	ATF_REQUIRE(WIFEXITED(status));
1473	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1474
1475	wpid = wait(&status);
1476	ATF_REQUIRE(wpid == -1);
1477	ATF_REQUIRE(errno == ECHILD);
1478}
1479
1480/*
1481 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1482 */
1483ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1484ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1485{
1486	struct ptrace_lwpinfo pl;
1487	pid_t fpid, wpid;
1488	int events, status;
1489
1490	ATF_REQUIRE((fpid = fork()) != -1);
1491	if (fpid == 0) {
1492		trace_me();
1493		follow_fork_parent(true);
1494	}
1495
1496	/* The first wait() should report the stop from SIGSTOP. */
1497	wpid = waitpid(fpid, &status, 0);
1498	ATF_REQUIRE(wpid == fpid);
1499	ATF_REQUIRE(WIFSTOPPED(status));
1500	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1501
1502	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1503	    sizeof(events)) == 0);
1504	events |= PTRACE_VFORK;
1505	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1506	    sizeof(events)) == 0);
1507
1508	/* Continue the child ignoring the SIGSTOP. */
1509	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1510
1511	/* The next event should report the end of the vfork. */
1512	wpid = wait(&status);
1513	ATF_REQUIRE(wpid == fpid);
1514	ATF_REQUIRE(WIFSTOPPED(status));
1515	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1516	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1517	ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1518
1519	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1520
1521	wpid = wait(&status);
1522	ATF_REQUIRE(wpid == fpid);
1523	ATF_REQUIRE(WIFEXITED(status));
1524	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1525
1526	wpid = wait(&status);
1527	ATF_REQUIRE(wpid == -1);
1528	ATF_REQUIRE(errno == ECHILD);
1529}
1530
1531ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1532ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1533{
1534	struct ptrace_lwpinfo pl[2];
1535	pid_t children[2], fpid, wpid;
1536	int events, status;
1537
1538	ATF_REQUIRE((fpid = fork()) != -1);
1539	if (fpid == 0) {
1540		trace_me();
1541		follow_fork_parent(true);
1542	}
1543
1544	/* Parent process. */
1545	children[0] = fpid;
1546
1547	/* The first wait() should report the stop from SIGSTOP. */
1548	wpid = waitpid(children[0], &status, 0);
1549	ATF_REQUIRE(wpid == children[0]);
1550	ATF_REQUIRE(WIFSTOPPED(status));
1551	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1552
1553	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1554	    sizeof(events)) == 0);
1555	events |= PTRACE_FORK | PTRACE_VFORK;
1556	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1557	    sizeof(events)) == 0);
1558
1559	/* Continue the child ignoring the SIGSTOP. */
1560	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1561
1562	/* Wait for both halves of the fork event to get reported. */
1563	children[1] = handle_fork_events(children[0], pl);
1564	ATF_REQUIRE(children[1] > 0);
1565
1566	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1567
1568	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1569	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1570
1571	/*
1572	 * The child can't exit until the grandchild reports status, so the
1573	 * grandchild should report its exit first to the debugger.
1574	 */
1575	wpid = waitpid(children[1], &status, 0);
1576	ATF_REQUIRE(wpid == children[1]);
1577	ATF_REQUIRE(WIFEXITED(status));
1578	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1579
1580	/*
1581	 * The child should report it's vfork() completion before it
1582	 * exits.
1583	 */
1584	wpid = wait(&status);
1585	ATF_REQUIRE(wpid == children[0]);
1586	ATF_REQUIRE(WIFSTOPPED(status));
1587	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1588	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1589	    -1);
1590	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1591
1592	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1593
1594	wpid = wait(&status);
1595	ATF_REQUIRE(wpid == children[0]);
1596	ATF_REQUIRE(WIFEXITED(status));
1597	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1598
1599	wpid = wait(&status);
1600	ATF_REQUIRE(wpid == -1);
1601	ATF_REQUIRE(errno == ECHILD);
1602}
1603
1604/*
1605 * XXX: There's nothing inherently platform specific about this test, however a
1606 * userspace visible breakpoint() is a prerequisite.
1607 */
1608 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1609/*
1610 * Verify that no more events are reported after PT_KILL except for the
1611 * process exit when stopped due to a breakpoint trap.
1612 */
1613ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1614ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1615{
1616	pid_t fpid, wpid;
1617	int status;
1618
1619	ATF_REQUIRE((fpid = fork()) != -1);
1620	if (fpid == 0) {
1621		trace_me();
1622		breakpoint();
1623		exit(1);
1624	}
1625
1626	/* The first wait() should report the stop from SIGSTOP. */
1627	wpid = waitpid(fpid, &status, 0);
1628	ATF_REQUIRE(wpid == fpid);
1629	ATF_REQUIRE(WIFSTOPPED(status));
1630	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1631
1632	/* Continue the child ignoring the SIGSTOP. */
1633	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1634
1635	/* The second wait() should report hitting the breakpoint. */
1636	wpid = waitpid(fpid, &status, 0);
1637	ATF_REQUIRE(wpid == fpid);
1638	ATF_REQUIRE(WIFSTOPPED(status));
1639	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1640
1641	/* Kill the child process. */
1642	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1643
1644	/* The last wait() should report the SIGKILL. */
1645	wpid = waitpid(fpid, &status, 0);
1646	ATF_REQUIRE(wpid == fpid);
1647	ATF_REQUIRE(WIFSIGNALED(status));
1648	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1649
1650	wpid = wait(&status);
1651	ATF_REQUIRE(wpid == -1);
1652	ATF_REQUIRE(errno == ECHILD);
1653}
1654#endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1655
1656/*
1657 * Verify that no more events are reported after PT_KILL except for the
1658 * process exit when stopped inside of a system call.
1659 */
1660ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1661ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1662{
1663	struct ptrace_lwpinfo pl;
1664	pid_t fpid, wpid;
1665	int status;
1666
1667	ATF_REQUIRE((fpid = fork()) != -1);
1668	if (fpid == 0) {
1669		trace_me();
1670		getpid();
1671		exit(1);
1672	}
1673
1674	/* The first wait() should report the stop from SIGSTOP. */
1675	wpid = waitpid(fpid, &status, 0);
1676	ATF_REQUIRE(wpid == fpid);
1677	ATF_REQUIRE(WIFSTOPPED(status));
1678	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1679
1680	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
1681	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1682
1683	/* The second wait() should report a system call entry for getpid(). */
1684	wpid = waitpid(fpid, &status, 0);
1685	ATF_REQUIRE(wpid == fpid);
1686	ATF_REQUIRE(WIFSTOPPED(status));
1687	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1688
1689	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1690	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1691
1692	/* Kill the child process. */
1693	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1694
1695	/* The last wait() should report the SIGKILL. */
1696	wpid = waitpid(fpid, &status, 0);
1697	ATF_REQUIRE(wpid == fpid);
1698	ATF_REQUIRE(WIFSIGNALED(status));
1699	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1700
1701	wpid = wait(&status);
1702	ATF_REQUIRE(wpid == -1);
1703	ATF_REQUIRE(errno == ECHILD);
1704}
1705
1706/*
1707 * Verify that no more events are reported after PT_KILL except for the
1708 * process exit when killing a multithreaded process.
1709 */
1710ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1711ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1712{
1713	struct ptrace_lwpinfo pl;
1714	pid_t fpid, wpid;
1715	lwpid_t main_lwp;
1716	int status;
1717
1718	ATF_REQUIRE((fpid = fork()) != -1);
1719	if (fpid == 0) {
1720		trace_me();
1721		simple_thread_main();
1722	}
1723
1724	/* The first wait() should report the stop from SIGSTOP. */
1725	wpid = waitpid(fpid, &status, 0);
1726	ATF_REQUIRE(wpid == fpid);
1727	ATF_REQUIRE(WIFSTOPPED(status));
1728	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1729
1730	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1731	    sizeof(pl)) != -1);
1732	main_lwp = pl.pl_lwpid;
1733
1734	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1735
1736	/* Continue the child ignoring the SIGSTOP. */
1737	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1738
1739	/* The first event should be for the child thread's birth. */
1740	wpid = waitpid(fpid, &status, 0);
1741	ATF_REQUIRE(wpid == fpid);
1742	ATF_REQUIRE(WIFSTOPPED(status));
1743	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1744
1745	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1746	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1747	    (PL_FLAG_BORN | PL_FLAG_SCX));
1748	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1749
1750	/* Kill the child process. */
1751	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1752
1753	/* The last wait() should report the SIGKILL. */
1754	wpid = waitpid(fpid, &status, 0);
1755	ATF_REQUIRE(wpid == fpid);
1756	ATF_REQUIRE(WIFSIGNALED(status));
1757	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1758
1759	wpid = wait(&status);
1760	ATF_REQUIRE(wpid == -1);
1761	ATF_REQUIRE(errno == ECHILD);
1762}
1763
1764static void *
1765mask_usr1_thread(void *arg)
1766{
1767	pthread_barrier_t *pbarrier;
1768	sigset_t sigmask;
1769
1770	pbarrier = (pthread_barrier_t*)arg;
1771
1772	sigemptyset(&sigmask);
1773	sigaddset(&sigmask, SIGUSR1);
1774	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1775
1776	/* Sync up with other thread after sigmask updated. */
1777	pthread_barrier_wait(pbarrier);
1778
1779	for (;;)
1780		sleep(60);
1781
1782	return (NULL);
1783}
1784
1785/*
1786 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1787 * and prevents spurious stops due to those other signals.
1788 */
1789ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
1790ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1791{
1792	pid_t fpid, wpid;
1793	int status;
1794	cpuset_t setmask;
1795	pthread_t t;
1796	pthread_barrier_t barrier;
1797	struct sched_param sched_param;
1798
1799	ATF_REQUIRE((fpid = fork()) != -1);
1800	if (fpid == 0) {
1801		/* Bind to one CPU so only one thread at a time will run. */
1802		CPU_ZERO(&setmask);
1803		CPU_SET(0, &setmask);
1804		cpusetid_t setid;
1805		CHILD_REQUIRE(cpuset(&setid) == 0);
1806		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1807		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1808
1809		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1810
1811		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1812		    (void*)&barrier) == 0);
1813
1814		/*
1815		 * Give the main thread higher priority. The test always
1816		 * assumes that, if both threads are able to run, the main
1817		 * thread runs first.
1818		 */
1819		sched_param.sched_priority =
1820		    (sched_get_priority_max(SCHED_FIFO) +
1821		    sched_get_priority_min(SCHED_FIFO)) / 2;
1822		CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1823		    SCHED_FIFO, &sched_param) == 0);
1824		sched_param.sched_priority -= RQ_PPQ;
1825		CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1826		    &sched_param) == 0);
1827
1828		sigset_t sigmask;
1829		sigemptyset(&sigmask);
1830		sigaddset(&sigmask, SIGUSR2);
1831		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1832
1833		/* Sync up with other thread after sigmask updated. */
1834		pthread_barrier_wait(&barrier);
1835
1836		trace_me();
1837
1838		for (;;)
1839			sleep(60);
1840
1841		exit(1);
1842	}
1843
1844	/* The first wait() should report the stop from SIGSTOP. */
1845	wpid = waitpid(fpid, &status, 0);
1846	ATF_REQUIRE(wpid == fpid);
1847	ATF_REQUIRE(WIFSTOPPED(status));
1848	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1849
1850	/* Continue the child ignoring the SIGSTOP. */
1851	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1852
1853	/* Send a signal that only the second thread can handle. */
1854	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1855
1856	/* The second wait() should report the SIGUSR2. */
1857	wpid = waitpid(fpid, &status, 0);
1858	ATF_REQUIRE(wpid == fpid);
1859	ATF_REQUIRE(WIFSTOPPED(status));
1860	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1861
1862	/* Send a signal that only the first thread can handle. */
1863	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1864
1865	/* Replace the SIGUSR2 with a kill. */
1866	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1867
1868	/* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1869	wpid = waitpid(fpid, &status, 0);
1870	ATF_REQUIRE(wpid == fpid);
1871	ATF_REQUIRE(WIFSIGNALED(status));
1872	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1873
1874	wpid = wait(&status);
1875	ATF_REQUIRE(wpid == -1);
1876	ATF_REQUIRE(errno == ECHILD);
1877}
1878
1879/*
1880 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1881 * and prevents spurious stops caused by those events.
1882 */
1883ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
1884ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1885{
1886	pid_t fpid, wpid;
1887	int status;
1888	cpuset_t setmask;
1889	pthread_t t;
1890	pthread_barrier_t barrier;
1891	lwpid_t main_lwp;
1892	struct ptrace_lwpinfo pl;
1893	struct sched_param sched_param;
1894
1895	ATF_REQUIRE((fpid = fork()) != -1);
1896	if (fpid == 0) {
1897		trace_me();
1898
1899		/* Bind to one CPU so only one thread at a time will run. */
1900		CPU_ZERO(&setmask);
1901		CPU_SET(0, &setmask);
1902		cpusetid_t setid;
1903		CHILD_REQUIRE(cpuset(&setid) == 0);
1904		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1905		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1906
1907		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1908
1909		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1910		    (void*)&barrier) == 0);
1911
1912		/*
1913		 * Give the main thread higher priority. The test always
1914		 * assumes that, if both threads are able to run, the main
1915		 * thread runs first.
1916		 */
1917		sched_param.sched_priority =
1918		    (sched_get_priority_max(SCHED_FIFO) +
1919		    sched_get_priority_min(SCHED_FIFO)) / 2;
1920		CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1921		    SCHED_FIFO, &sched_param) == 0);
1922		sched_param.sched_priority -= RQ_PPQ;
1923		CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1924		    &sched_param) == 0);
1925
1926		sigset_t sigmask;
1927		sigemptyset(&sigmask);
1928		sigaddset(&sigmask, SIGUSR2);
1929		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1930
1931		/* Sync up with other thread after sigmask updated. */
1932		pthread_barrier_wait(&barrier);
1933
1934		/* Sync up with the test before doing the getpid(). */
1935		raise(SIGSTOP);
1936
1937		getpid();
1938		exit(1);
1939	}
1940
1941	/* The first wait() should report the stop from SIGSTOP. */
1942	wpid = waitpid(fpid, &status, 0);
1943	ATF_REQUIRE(wpid == fpid);
1944	ATF_REQUIRE(WIFSTOPPED(status));
1945	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1946
1947	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1948	main_lwp = pl.pl_lwpid;
1949
1950	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
1951	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1952
1953	/*
1954	 * Continue until child is done with setup, which is indicated with
1955	 * SIGSTOP. Ignore system calls in the meantime.
1956	 */
1957	for (;;) {
1958		wpid = waitpid(fpid, &status, 0);
1959		ATF_REQUIRE(wpid == fpid);
1960		ATF_REQUIRE(WIFSTOPPED(status));
1961		if (WSTOPSIG(status) == SIGTRAP) {
1962			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1963			    sizeof(pl)) != -1);
1964			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
1965		} else {
1966			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1967			break;
1968		}
1969		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1970	}
1971
1972	/* Proceed, allowing main thread to hit syscall entry for getpid(). */
1973	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1974
1975	wpid = waitpid(fpid, &status, 0);
1976	ATF_REQUIRE(wpid == fpid);
1977	ATF_REQUIRE(WIFSTOPPED(status));
1978	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1979
1980	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1981	    sizeof(pl)) != -1);
1982	ATF_REQUIRE(pl.pl_lwpid == main_lwp);
1983	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1984	/* Prevent the main thread from hitting its syscall exit for now. */
1985	ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
1986
1987	/*
1988	 * Proceed, allowing second thread to hit syscall exit for
1989	 * pthread_barrier_wait().
1990	 */
1991	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1992
1993	wpid = waitpid(fpid, &status, 0);
1994	ATF_REQUIRE(wpid == fpid);
1995	ATF_REQUIRE(WIFSTOPPED(status));
1996	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1997
1998	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1999	    sizeof(pl)) != -1);
2000	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2001	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2002
2003	/* Send a signal that only the second thread can handle. */
2004	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2005
2006	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2007
2008	/* The next wait() should report the SIGUSR2. */
2009	wpid = waitpid(fpid, &status, 0);
2010	ATF_REQUIRE(wpid == fpid);
2011	ATF_REQUIRE(WIFSTOPPED(status));
2012	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2013
2014	/* Allow the main thread to try to finish its system call. */
2015	ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2016
2017	/*
2018	 * At this point, the main thread is in the middle of a system call and
2019	 * has been resumed. The second thread has taken a SIGUSR2 which will
2020	 * be replaced with a SIGKILL below. The main thread will get to run
2021	 * first. It should notice the kill request (even though the signal
2022	 * replacement occurred in the other thread) and exit accordingly.  It
2023	 * should not stop for the system call exit event.
2024	 */
2025
2026	/* Replace the SIGUSR2 with a kill. */
2027	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2028
2029	/* The last wait() should report the SIGKILL (not a syscall exit). */
2030	wpid = waitpid(fpid, &status, 0);
2031	ATF_REQUIRE(wpid == fpid);
2032	ATF_REQUIRE(WIFSIGNALED(status));
2033	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2034
2035	wpid = wait(&status);
2036	ATF_REQUIRE(wpid == -1);
2037	ATF_REQUIRE(errno == ECHILD);
2038}
2039
2040static void
2041sigusr1_handler(int sig)
2042{
2043
2044	CHILD_REQUIRE(sig == SIGUSR1);
2045	_exit(2);
2046}
2047
2048/*
2049 * Verify that even if the signal queue is full for a child process,
2050 * a PT_KILL will kill the process.
2051 */
2052ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2053ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2054{
2055	pid_t fpid, wpid;
2056	int status;
2057	int max_pending_per_proc;
2058	size_t len;
2059	int i;
2060
2061	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2062
2063	ATF_REQUIRE((fpid = fork()) != -1);
2064	if (fpid == 0) {
2065		trace_me();
2066		exit(1);
2067	}
2068
2069	/* The first wait() should report the stop from SIGSTOP. */
2070	wpid = waitpid(fpid, &status, 0);
2071	ATF_REQUIRE(wpid == fpid);
2072	ATF_REQUIRE(WIFSTOPPED(status));
2073	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2074
2075	len = sizeof(max_pending_per_proc);
2076	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2077	    &max_pending_per_proc, &len, NULL, 0) == 0);
2078
2079	/* Fill the signal queue. */
2080	for (i = 0; i < max_pending_per_proc; ++i)
2081		ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2082
2083	/* Kill the child process. */
2084	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2085
2086	/* The last wait() should report the SIGKILL. */
2087	wpid = waitpid(fpid, &status, 0);
2088	ATF_REQUIRE(wpid == fpid);
2089	ATF_REQUIRE(WIFSIGNALED(status));
2090	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2091
2092	wpid = wait(&status);
2093	ATF_REQUIRE(wpid == -1);
2094	ATF_REQUIRE(errno == ECHILD);
2095}
2096
2097/*
2098 * Verify that when stopped at a system call entry, a signal can be
2099 * requested with PT_CONTINUE which will be delivered once the system
2100 * call is complete.
2101 */
2102ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2103ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2104{
2105	struct ptrace_lwpinfo pl;
2106	pid_t fpid, wpid;
2107	int status;
2108
2109	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2110
2111	ATF_REQUIRE((fpid = fork()) != -1);
2112	if (fpid == 0) {
2113		trace_me();
2114		getpid();
2115		exit(1);
2116	}
2117
2118	/* The first wait() should report the stop from SIGSTOP. */
2119	wpid = waitpid(fpid, &status, 0);
2120	ATF_REQUIRE(wpid == fpid);
2121	ATF_REQUIRE(WIFSTOPPED(status));
2122	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2123
2124	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2125	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2126
2127	/* The second wait() should report a system call entry for getpid(). */
2128	wpid = waitpid(fpid, &status, 0);
2129	ATF_REQUIRE(wpid == fpid);
2130	ATF_REQUIRE(WIFSTOPPED(status));
2131	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2132
2133	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2134	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2135
2136	/* Continue the child process with a signal. */
2137	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2138
2139	for (;;) {
2140		/*
2141		 * The last wait() should report exit 2, i.e., a normal _exit
2142		 * from the signal handler. In the meantime, catch and proceed
2143		 * past any syscall stops.
2144		 */
2145		wpid = waitpid(fpid, &status, 0);
2146		ATF_REQUIRE(wpid == fpid);
2147		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2148			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2149			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2150			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2151		} else {
2152			ATF_REQUIRE(WIFEXITED(status));
2153			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2154			break;
2155		}
2156	}
2157
2158	wpid = wait(&status);
2159	ATF_REQUIRE(wpid == -1);
2160	ATF_REQUIRE(errno == ECHILD);
2161}
2162
2163static void
2164sigusr1_counting_handler(int sig)
2165{
2166	static int counter = 0;
2167
2168	CHILD_REQUIRE(sig == SIGUSR1);
2169	counter++;
2170	if (counter == 2)
2171		_exit(2);
2172}
2173
2174/*
2175 * Verify that, when continuing from a stop at system call entry and exit,
2176 * a signal can be requested from both stops, and both will be delivered when
2177 * the system call is complete.
2178 */
2179ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2180ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2181{
2182	struct ptrace_lwpinfo pl;
2183	pid_t fpid, wpid;
2184	int status;
2185
2186	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2187
2188	ATF_REQUIRE((fpid = fork()) != -1);
2189	if (fpid == 0) {
2190		trace_me();
2191		getpid();
2192		exit(1);
2193	}
2194
2195	/* The first wait() should report the stop from SIGSTOP. */
2196	wpid = waitpid(fpid, &status, 0);
2197	ATF_REQUIRE(wpid == fpid);
2198	ATF_REQUIRE(WIFSTOPPED(status));
2199	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2200
2201	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2202	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2203
2204	/* The second wait() should report a system call entry for getpid(). */
2205	wpid = waitpid(fpid, &status, 0);
2206	ATF_REQUIRE(wpid == fpid);
2207	ATF_REQUIRE(WIFSTOPPED(status));
2208	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2209
2210	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2211	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2212
2213	/* Continue the child process with a signal. */
2214	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2215
2216	/* The third wait() should report a system call exit for getpid(). */
2217	wpid = waitpid(fpid, &status, 0);
2218	ATF_REQUIRE(wpid == fpid);
2219	ATF_REQUIRE(WIFSTOPPED(status));
2220	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2221
2222	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2223	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2224
2225	/* Continue the child process with a signal. */
2226	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2227
2228	for (;;) {
2229		/*
2230		 * The last wait() should report exit 2, i.e., a normal _exit
2231		 * from the signal handler. In the meantime, catch and proceed
2232		 * past any syscall stops.
2233		 */
2234		wpid = waitpid(fpid, &status, 0);
2235		ATF_REQUIRE(wpid == fpid);
2236		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2237			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2238			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2239			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2240		} else {
2241			ATF_REQUIRE(WIFEXITED(status));
2242			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2243			break;
2244		}
2245	}
2246
2247	wpid = wait(&status);
2248	ATF_REQUIRE(wpid == -1);
2249	ATF_REQUIRE(errno == ECHILD);
2250}
2251
2252/*
2253 * Verify that even if the signal queue is full for a child process,
2254 * a PT_CONTINUE with a signal will not result in loss of that signal.
2255 */
2256ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2257ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2258{
2259	pid_t fpid, wpid;
2260	int status;
2261	int max_pending_per_proc;
2262	size_t len;
2263	int i;
2264
2265	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2266	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2267
2268	ATF_REQUIRE((fpid = fork()) != -1);
2269	if (fpid == 0) {
2270		trace_me();
2271		exit(1);
2272	}
2273
2274	/* The first wait() should report the stop from SIGSTOP. */
2275	wpid = waitpid(fpid, &status, 0);
2276	ATF_REQUIRE(wpid == fpid);
2277	ATF_REQUIRE(WIFSTOPPED(status));
2278	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2279
2280	len = sizeof(max_pending_per_proc);
2281	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2282	    &max_pending_per_proc, &len, NULL, 0) == 0);
2283
2284	/* Fill the signal queue. */
2285	for (i = 0; i < max_pending_per_proc; ++i)
2286		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2287
2288	/* Continue with signal. */
2289	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2290
2291	for (;;) {
2292		wpid = waitpid(fpid, &status, 0);
2293		ATF_REQUIRE(wpid == fpid);
2294		if (WIFSTOPPED(status)) {
2295			ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2296			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2297		} else {
2298			/*
2299			 * The last wait() should report normal _exit from the
2300			 * SIGUSR1 handler.
2301			 */
2302			ATF_REQUIRE(WIFEXITED(status));
2303			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2304			break;
2305		}
2306	}
2307
2308	wpid = wait(&status);
2309	ATF_REQUIRE(wpid == -1);
2310	ATF_REQUIRE(errno == ECHILD);
2311}
2312
2313/*
2314 * Verify that, after stopping due to a signal, that signal can be
2315 * replaced with another signal.
2316 */
2317ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2318ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2319{
2320	struct ptrace_lwpinfo pl;
2321	pid_t fpid, wpid;
2322	int status;
2323
2324	ATF_REQUIRE((fpid = fork()) != -1);
2325	if (fpid == 0) {
2326		trace_me();
2327		sleep(20);
2328		exit(1);
2329	}
2330
2331	/* The first wait() should report the stop from SIGSTOP. */
2332	wpid = waitpid(fpid, &status, 0);
2333	ATF_REQUIRE(wpid == fpid);
2334	ATF_REQUIRE(WIFSTOPPED(status));
2335	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2336
2337	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2338
2339	/* Send a signal without ptrace. */
2340	ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2341
2342	/* The second wait() should report a SIGINT was received. */
2343	wpid = waitpid(fpid, &status, 0);
2344	ATF_REQUIRE(wpid == fpid);
2345	ATF_REQUIRE(WIFSTOPPED(status));
2346	ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2347
2348	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2349	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2350	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2351
2352	/* Continue the child process with a different signal. */
2353	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2354
2355	/*
2356	 * The last wait() should report having died due to the new
2357	 * signal, SIGTERM.
2358	 */
2359	wpid = waitpid(fpid, &status, 0);
2360	ATF_REQUIRE(wpid == fpid);
2361	ATF_REQUIRE(WIFSIGNALED(status));
2362	ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2363
2364	wpid = wait(&status);
2365	ATF_REQUIRE(wpid == -1);
2366	ATF_REQUIRE(errno == ECHILD);
2367}
2368
2369/*
2370 * Verify that a signal can be passed through to the child even when there
2371 * was no true signal originally. Such cases arise when a SIGTRAP is
2372 * invented for e.g, system call stops.
2373 */
2374ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2375ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2376{
2377	struct ptrace_lwpinfo pl;
2378	pid_t fpid, wpid;
2379	int status;
2380
2381	ATF_REQUIRE((fpid = fork()) != -1);
2382	if (fpid == 0) {
2383		trace_me();
2384		getpid();
2385		exit(1);
2386	}
2387
2388	/* The first wait() should report the stop from SIGSTOP. */
2389	wpid = waitpid(fpid, &status, 0);
2390	ATF_REQUIRE(wpid == fpid);
2391	ATF_REQUIRE(WIFSTOPPED(status));
2392	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2393
2394	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2395	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2396
2397	/* The second wait() should report a system call entry for getpid(). */
2398	wpid = waitpid(fpid, &status, 0);
2399	ATF_REQUIRE(wpid == fpid);
2400	ATF_REQUIRE(WIFSTOPPED(status));
2401	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2402
2403	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2404	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2405
2406	/* Continue the child process with a SIGTRAP. */
2407	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2408
2409	for (;;) {
2410		/*
2411		 * The last wait() should report exit due to SIGTRAP.  In the
2412		 * meantime, catch and proceed past any syscall stops.
2413		 */
2414		wpid = waitpid(fpid, &status, 0);
2415		ATF_REQUIRE(wpid == fpid);
2416		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2417			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2418			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2419			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2420		} else {
2421			ATF_REQUIRE(WIFSIGNALED(status));
2422			ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2423			break;
2424		}
2425	}
2426
2427	wpid = wait(&status);
2428	ATF_REQUIRE(wpid == -1);
2429	ATF_REQUIRE(errno == ECHILD);
2430
2431}
2432
2433/*
2434 * A mixed bag PT_CONTINUE with signal test.
2435 */
2436ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2437ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2438{
2439	struct ptrace_lwpinfo pl;
2440	pid_t fpid, wpid;
2441	int status;
2442
2443	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2444
2445	ATF_REQUIRE((fpid = fork()) != -1);
2446	if (fpid == 0) {
2447		trace_me();
2448		getpid();
2449		exit(1);
2450	}
2451
2452	/* The first wait() should report the stop from SIGSTOP. */
2453	wpid = waitpid(fpid, &status, 0);
2454	ATF_REQUIRE(wpid == fpid);
2455	ATF_REQUIRE(WIFSTOPPED(status));
2456	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2457
2458	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2459	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2460
2461	/* The second wait() should report a system call entry for getpid(). */
2462	wpid = waitpid(fpid, &status, 0);
2463	ATF_REQUIRE(wpid == fpid);
2464	ATF_REQUIRE(WIFSTOPPED(status));
2465	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2466
2467	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2468	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2469
2470	/* Continue with the first SIGUSR1. */
2471	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2472
2473	/* The next wait() should report a system call exit for getpid(). */
2474	wpid = waitpid(fpid, &status, 0);
2475	ATF_REQUIRE(wpid == fpid);
2476	ATF_REQUIRE(WIFSTOPPED(status));
2477	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2478
2479	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2480	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2481
2482	/* Send an ABRT without ptrace. */
2483	ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2484
2485	/* Continue normally. */
2486	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2487
2488	/* The next wait() should report the SIGABRT. */
2489	wpid = waitpid(fpid, &status, 0);
2490	ATF_REQUIRE(wpid == fpid);
2491	ATF_REQUIRE(WIFSTOPPED(status));
2492	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2493
2494	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2495	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2496	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2497
2498	/* Continue, replacing the SIGABRT with another SIGUSR1. */
2499	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2500
2501	for (;;) {
2502		/*
2503		 * The last wait() should report exit 2, i.e., a normal _exit
2504		 * from the signal handler. In the meantime, catch and proceed
2505		 * past any syscall stops.
2506		 */
2507		wpid = waitpid(fpid, &status, 0);
2508		ATF_REQUIRE(wpid == fpid);
2509		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2510			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2511			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2512			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2513		} else {
2514			ATF_REQUIRE(WIFEXITED(status));
2515			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2516			break;
2517		}
2518	}
2519
2520	wpid = wait(&status);
2521	ATF_REQUIRE(wpid == -1);
2522	ATF_REQUIRE(errno == ECHILD);
2523
2524}
2525
2526/*
2527 * Verify a signal delivered by ptrace is noticed by kevent(2).
2528 */
2529ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2530ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2531{
2532	pid_t fpid, wpid;
2533	int status, kq, nevents;
2534	struct kevent kev;
2535
2536	ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2537
2538	ATF_REQUIRE((fpid = fork()) != -1);
2539	if (fpid == 0) {
2540		CHILD_REQUIRE((kq = kqueue()) > 0);
2541		EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2542		CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2543
2544		trace_me();
2545
2546		for (;;) {
2547			nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2548			if (nevents == -1 && errno == EINTR)
2549				continue;
2550			CHILD_REQUIRE(nevents > 0);
2551			CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2552			CHILD_REQUIRE(kev.ident == SIGUSR1);
2553			break;
2554		}
2555
2556		exit(1);
2557	}
2558
2559	/* The first wait() should report the stop from SIGSTOP. */
2560	wpid = waitpid(fpid, &status, 0);
2561	ATF_REQUIRE(wpid == fpid);
2562	ATF_REQUIRE(WIFSTOPPED(status));
2563	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2564
2565	/* Continue with the SIGUSR1. */
2566	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2567
2568	/*
2569	 * The last wait() should report normal exit with code 1.
2570	 */
2571	wpid = waitpid(fpid, &status, 0);
2572	ATF_REQUIRE(wpid == fpid);
2573	ATF_REQUIRE(WIFEXITED(status));
2574	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2575
2576	wpid = wait(&status);
2577	ATF_REQUIRE(wpid == -1);
2578	ATF_REQUIRE(errno == ECHILD);
2579}
2580
2581static sem_t sigusr1_sem;
2582
2583static void
2584sigusr1_sempost_handler(int sig __unused)
2585{
2586
2587	CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2588}
2589
2590static void *
2591signal_thread(void *arg)
2592{
2593	int err;
2594	sigset_t sigmask;
2595
2596	pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2597
2598	/* Wait for this thread to receive a SIGUSR1. */
2599	do {
2600		err = sem_wait(&sigusr1_sem);
2601		CHILD_REQUIRE(err == 0 || errno == EINTR);
2602	} while (err != 0 && errno == EINTR);
2603
2604	/* Free our companion thread from the barrier. */
2605	pthread_barrier_wait(pbarrier);
2606
2607	/*
2608	 * Swap ignore duties; the next SIGUSR1 should go to the
2609	 * other thread.
2610	 */
2611	CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2612	CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2613	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2614
2615	/* Sync up threads after swapping signal masks. */
2616	pthread_barrier_wait(pbarrier);
2617
2618	/* Wait until our companion has received its SIGUSR1. */
2619	pthread_barrier_wait(pbarrier);
2620
2621	return (NULL);
2622}
2623
2624/*
2625 * Verify that if ptrace stops due to a signal but continues with
2626 * a different signal that the new signal is routed to a thread
2627 * that can accept it, and that that thread is awakened by the signal
2628 * in a timely manner.
2629 */
2630ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2631ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2632{
2633	pid_t fpid, wpid;
2634	int status, err;
2635	pthread_t t;
2636	sigset_t sigmask;
2637	pthread_barrier_t barrier;
2638
2639	ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2640	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2641	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2642
2643	ATF_REQUIRE((fpid = fork()) != -1);
2644	if (fpid == 0) {
2645		CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2646
2647		/* The other thread should receive the first SIGUSR1. */
2648		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2649		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2650		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2651
2652		trace_me();
2653
2654		/* Wait until other thread has received its SIGUSR1. */
2655		pthread_barrier_wait(&barrier);
2656
2657		/*
2658		 * Swap ignore duties; the next SIGUSR1 should go to this
2659		 * thread.
2660		 */
2661		CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2662
2663		/* Sync up threads after swapping signal masks. */
2664		pthread_barrier_wait(&barrier);
2665
2666		/*
2667		 * Sync up with test code; we're ready for the next SIGUSR1
2668		 * now.
2669		 */
2670		raise(SIGSTOP);
2671
2672		/* Wait for this thread to receive a SIGUSR1. */
2673		do {
2674			err = sem_wait(&sigusr1_sem);
2675			CHILD_REQUIRE(err == 0 || errno == EINTR);
2676		} while (err != 0 && errno == EINTR);
2677
2678		/* Free the other thread from the barrier. */
2679		pthread_barrier_wait(&barrier);
2680
2681		CHILD_REQUIRE(pthread_join(t, NULL) == 0);
2682
2683		exit(1);
2684	}
2685
2686	/* The first wait() should report the stop from SIGSTOP. */
2687	wpid = waitpid(fpid, &status, 0);
2688	ATF_REQUIRE(wpid == fpid);
2689	ATF_REQUIRE(WIFSTOPPED(status));
2690	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2691
2692	/* Continue the child ignoring the SIGSTOP. */
2693	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2694
2695	/*
2696	 * Send a signal without ptrace that either thread will accept (USR2,
2697	 * in this case).
2698	 */
2699	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2700
2701	/* The second wait() should report a SIGUSR2 was received. */
2702	wpid = waitpid(fpid, &status, 0);
2703	ATF_REQUIRE(wpid == fpid);
2704	ATF_REQUIRE(WIFSTOPPED(status));
2705	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2706
2707	/* Continue the child, changing the signal to USR1. */
2708	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2709
2710	/* The next wait() should report the stop from SIGSTOP. */
2711	wpid = waitpid(fpid, &status, 0);
2712	ATF_REQUIRE(wpid == fpid);
2713	ATF_REQUIRE(WIFSTOPPED(status));
2714	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2715
2716	/* Continue the child ignoring the SIGSTOP. */
2717	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2718
2719	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2720
2721	/* The next wait() should report a SIGUSR2 was received. */
2722	wpid = waitpid(fpid, &status, 0);
2723	ATF_REQUIRE(wpid == fpid);
2724	ATF_REQUIRE(WIFSTOPPED(status));
2725	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2726
2727	/* Continue the child, changing the signal to USR1. */
2728	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2729
2730	/* The last wait() should report normal exit with code 1. */
2731	wpid = waitpid(fpid, &status, 0);
2732	ATF_REQUIRE(wpid == fpid);
2733	ATF_REQUIRE(WIFEXITED(status));
2734	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2735
2736	wpid = wait(&status);
2737	ATF_REQUIRE(wpid == -1);
2738	ATF_REQUIRE(errno == ECHILD);
2739}
2740
2741static void *
2742raise_sigstop_thread(void *arg __unused)
2743{
2744
2745	raise(SIGSTOP);
2746	return NULL;
2747}
2748
2749static void *
2750sleep_thread(void *arg __unused)
2751{
2752
2753	sleep(60);
2754	return NULL;
2755}
2756
2757static void
2758terminate_with_pending_sigstop(bool sigstop_from_main_thread)
2759{
2760	pid_t fpid, wpid;
2761	int status, i;
2762	cpuset_t setmask;
2763	cpusetid_t setid;
2764	pthread_t t;
2765
2766	/*
2767	 * Become the reaper for this process tree. We need to be able to check
2768	 * that both child and grandchild have died.
2769	 */
2770	ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
2771
2772	fpid = fork();
2773	ATF_REQUIRE(fpid >= 0);
2774	if (fpid == 0) {
2775		fpid = fork();
2776		CHILD_REQUIRE(fpid >= 0);
2777		if (fpid == 0) {
2778			trace_me();
2779
2780			/* Pin to CPU 0 to serialize thread execution. */
2781			CPU_ZERO(&setmask);
2782			CPU_SET(0, &setmask);
2783			CHILD_REQUIRE(cpuset(&setid) == 0);
2784			CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2785			    CPU_WHICH_CPUSET, setid,
2786			    sizeof(setmask), &setmask) == 0);
2787
2788			if (sigstop_from_main_thread) {
2789				/*
2790				 * We expect the SIGKILL sent when our parent
2791				 * dies to be delivered to the new thread.
2792				 * Raise the SIGSTOP in this thread so the
2793				 * threads compete.
2794				 */
2795				CHILD_REQUIRE(pthread_create(&t, NULL,
2796				    sleep_thread, NULL) == 0);
2797				raise(SIGSTOP);
2798			} else {
2799				/*
2800				 * We expect the SIGKILL to be delivered to
2801				 * this thread. After creating the new thread,
2802				 * just get off the CPU so the other thread can
2803				 * raise the SIGSTOP.
2804				 */
2805				CHILD_REQUIRE(pthread_create(&t, NULL,
2806				    raise_sigstop_thread, NULL) == 0);
2807				sleep(60);
2808			}
2809
2810			exit(0);
2811		}
2812		/* First stop is trace_me() immediately after fork. */
2813		wpid = waitpid(fpid, &status, 0);
2814		CHILD_REQUIRE(wpid == fpid);
2815		CHILD_REQUIRE(WIFSTOPPED(status));
2816		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2817
2818		CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2819
2820		/* Second stop is from the raise(SIGSTOP). */
2821		wpid = waitpid(fpid, &status, 0);
2822		CHILD_REQUIRE(wpid == fpid);
2823		CHILD_REQUIRE(WIFSTOPPED(status));
2824		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2825
2826		/*
2827		 * Terminate tracing process without detaching. Our child
2828		 * should be killed.
2829		 */
2830		exit(0);
2831	}
2832
2833	/*
2834	 * We should get a normal exit from our immediate child and a SIGKILL
2835	 * exit from our grandchild. The latter case is the interesting one.
2836	 * Our grandchild should not have stopped due to the SIGSTOP that was
2837	 * left dangling when its parent died.
2838	 */
2839	for (i = 0; i < 2; ++i) {
2840		wpid = wait(&status);
2841		if (wpid == fpid)
2842			ATF_REQUIRE(WIFEXITED(status) &&
2843			    WEXITSTATUS(status) == 0);
2844		else
2845			ATF_REQUIRE(WIFSIGNALED(status) &&
2846			    WTERMSIG(status) == SIGKILL);
2847	}
2848}
2849
2850/*
2851 * These two tests ensure that if the tracing process exits without detaching
2852 * just after the child received a SIGSTOP, the child is cleanly killed and
2853 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
2854 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
2855 * different threads, the SIGKILL must win.  There are two variants of this
2856 * test, designed to catch the case where the SIGKILL is delivered to the
2857 * younger thread (the first test) and the case where the SIGKILL is delivered
2858 * to the older thread (the second test). This behavior has changed in the
2859 * past, so make no assumption.
2860 */
2861ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop1);
2862ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
2863{
2864
2865	terminate_with_pending_sigstop(true);
2866}
2867ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop2);
2868ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
2869{
2870
2871	terminate_with_pending_sigstop(false);
2872}
2873
2874/*
2875 * Verify that after ptrace() discards a SIGKILL signal, the event mask
2876 * is not modified.
2877 */
2878ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
2879ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
2880{
2881	struct ptrace_lwpinfo pl;
2882	pid_t fpid, wpid;
2883	int status, event_mask, new_event_mask;
2884
2885	ATF_REQUIRE((fpid = fork()) != -1);
2886	if (fpid == 0) {
2887		trace_me();
2888		raise(SIGSTOP);
2889		exit(0);
2890	}
2891
2892	/* The first wait() should report the stop from trace_me(). */
2893	wpid = waitpid(fpid, &status, 0);
2894	ATF_REQUIRE(wpid == fpid);
2895	ATF_REQUIRE(WIFSTOPPED(status));
2896	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2897
2898	/* Set several unobtrusive event bits. */
2899	event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
2900	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
2901	    sizeof(event_mask)) == 0);
2902
2903	/* Send a SIGKILL without using ptrace. */
2904	ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
2905
2906	/* Continue the child ignoring the SIGSTOP. */
2907	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2908
2909	/* The next stop should be due to the SIGKILL. */
2910	wpid = waitpid(fpid, &status, 0);
2911	ATF_REQUIRE(wpid == fpid);
2912	ATF_REQUIRE(WIFSTOPPED(status));
2913	ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
2914
2915	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2916	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2917	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
2918
2919	/* Continue the child ignoring the SIGKILL. */
2920	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2921
2922	/* The next wait() should report the stop from SIGSTOP. */
2923	wpid = waitpid(fpid, &status, 0);
2924	ATF_REQUIRE(wpid == fpid);
2925	ATF_REQUIRE(WIFSTOPPED(status));
2926	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2927
2928	/* Check the current event mask. It should not have changed. */
2929	new_event_mask = 0;
2930	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
2931	    sizeof(new_event_mask)) == 0);
2932	ATF_REQUIRE(event_mask == new_event_mask);
2933
2934	/* Continue the child to let it exit. */
2935	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2936
2937	/* The last event should be for the child process's exit. */
2938	wpid = waitpid(fpid, &status, 0);
2939	ATF_REQUIRE(WIFEXITED(status));
2940	ATF_REQUIRE(WEXITSTATUS(status) == 0);
2941
2942	wpid = wait(&status);
2943	ATF_REQUIRE(wpid == -1);
2944	ATF_REQUIRE(errno == ECHILD);
2945}
2946
2947#if defined(__amd64__) || defined(__i386__)
2948/*
2949 * Only x86 both define breakpoint() and have a PC after breakpoint so
2950 * that restarting doesn't retrigger the breakpoint.
2951 */
2952static void *
2953continue_thread(void *arg __unused)
2954{
2955	breakpoint();
2956	return (NULL);
2957}
2958
2959static __dead2 void
2960continue_thread_main(void)
2961{
2962	pthread_t threads[2];
2963
2964	CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
2965	    NULL) == 0);
2966	CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
2967	    NULL) == 0);
2968	CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
2969	CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
2970	exit(1);
2971}
2972
2973/*
2974 * Ensure that PT_CONTINUE clears the status of the thread that
2975 * triggered the stop even if a different thread's LWP was passed to
2976 * PT_CONTINUE.
2977 */
2978ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
2979ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
2980{
2981	struct ptrace_lwpinfo pl;
2982	pid_t fpid, wpid;
2983	lwpid_t lwps[2];
2984	bool hit_break[2];
2985	int i, j, status;
2986
2987	ATF_REQUIRE((fpid = fork()) != -1);
2988	if (fpid == 0) {
2989		trace_me();
2990		continue_thread_main();
2991	}
2992
2993	/* The first wait() should report the stop from SIGSTOP. */
2994	wpid = waitpid(fpid, &status, 0);
2995	ATF_REQUIRE(wpid == fpid);
2996	ATF_REQUIRE(WIFSTOPPED(status));
2997	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2998
2999	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3000	    sizeof(pl)) != -1);
3001
3002	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3003
3004	/* Continue the child ignoring the SIGSTOP. */
3005	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3006
3007	/* One of the new threads should report it's birth. */
3008	wpid = waitpid(fpid, &status, 0);
3009	ATF_REQUIRE(wpid == fpid);
3010	ATF_REQUIRE(WIFSTOPPED(status));
3011	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3012
3013	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3014	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3015	    (PL_FLAG_BORN | PL_FLAG_SCX));
3016	lwps[0] = pl.pl_lwpid;
3017
3018	/*
3019	 * Suspend this thread to ensure both threads are alive before
3020	 * hitting the breakpoint.
3021	 */
3022	ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3023
3024	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3025
3026	/* Second thread should report it's birth. */
3027	wpid = waitpid(fpid, &status, 0);
3028	ATF_REQUIRE(wpid == fpid);
3029	ATF_REQUIRE(WIFSTOPPED(status));
3030	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3031
3032	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3033	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3034	    (PL_FLAG_BORN | PL_FLAG_SCX));
3035	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3036	lwps[1] = pl.pl_lwpid;
3037
3038	/* Resume both threads waiting for breakpoint events. */
3039	hit_break[0] = hit_break[1] = false;
3040	ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3041	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3042
3043	/* One thread should report a breakpoint. */
3044	wpid = waitpid(fpid, &status, 0);
3045	ATF_REQUIRE(wpid == fpid);
3046	ATF_REQUIRE(WIFSTOPPED(status));
3047	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3048
3049	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3050	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3051	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3052	    pl.pl_siginfo.si_code == TRAP_BRKPT);
3053	if (pl.pl_lwpid == lwps[0])
3054		i = 0;
3055	else
3056		i = 1;
3057	hit_break[i] = true;
3058
3059	/*
3060	 * Resume both threads but pass the other thread's LWPID to
3061	 * PT_CONTINUE.
3062	 */
3063	ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3064
3065	/*
3066	 * Will now get two thread exit events and one more breakpoint
3067	 * event.
3068	 */
3069	for (j = 0; j < 3; j++) {
3070		wpid = waitpid(fpid, &status, 0);
3071		ATF_REQUIRE(wpid == fpid);
3072		ATF_REQUIRE(WIFSTOPPED(status));
3073		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3074
3075		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3076		    sizeof(pl)) != -1);
3077
3078		if (pl.pl_lwpid == lwps[0])
3079			i = 0;
3080		else
3081			i = 1;
3082
3083		ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3084		if (pl.pl_flags & PL_FLAG_EXITED) {
3085			ATF_REQUIRE_MSG(hit_break[i],
3086			    "exited thread did not report breakpoint");
3087			lwps[i] = 0;
3088		} else {
3089			ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3090			ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3091			    pl.pl_siginfo.si_code == TRAP_BRKPT);
3092			ATF_REQUIRE_MSG(!hit_break[i],
3093			    "double breakpoint event");
3094			hit_break[i] = true;
3095		}
3096
3097		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3098	}
3099
3100	/* Both threads should have exited. */
3101	ATF_REQUIRE(lwps[0] == 0);
3102	ATF_REQUIRE(lwps[1] == 0);
3103
3104	/* The last event should be for the child process's exit. */
3105	wpid = waitpid(fpid, &status, 0);
3106	ATF_REQUIRE(WIFEXITED(status));
3107	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3108
3109	wpid = wait(&status);
3110	ATF_REQUIRE(wpid == -1);
3111	ATF_REQUIRE(errno == ECHILD);
3112}
3113#endif
3114
3115/*
3116 * Verify that PT_LWPINFO doesn't return stale siginfo.
3117 */
3118ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3119ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3120{
3121	struct ptrace_lwpinfo pl;
3122	pid_t fpid, wpid;
3123	int events, status;
3124
3125	ATF_REQUIRE((fpid = fork()) != -1);
3126	if (fpid == 0) {
3127		trace_me();
3128		raise(SIGABRT);
3129		exit(1);
3130	}
3131
3132	/* The first 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	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3139
3140	/* The next stop should report the SIGABRT in the child body. */
3141	wpid = waitpid(fpid, &status, 0);
3142	ATF_REQUIRE(wpid == fpid);
3143	ATF_REQUIRE(WIFSTOPPED(status));
3144	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3145
3146	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3147	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3148	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3149
3150	/*
3151	 * Continue the process ignoring the signal, but enabling
3152	 * syscall traps.
3153	 */
3154	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3155
3156	/*
3157	 * The next stop should report a system call entry from
3158	 * exit().  PL_FLAGS_SI should not be set.
3159	 */
3160	wpid = waitpid(fpid, &status, 0);
3161	ATF_REQUIRE(wpid == fpid);
3162	ATF_REQUIRE(WIFSTOPPED(status));
3163	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3164
3165	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3166	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3167	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3168
3169	/* Disable syscall tracing and continue the child to let it exit. */
3170	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3171	    sizeof(events)) == 0);
3172	events &= ~PTRACE_SYSCALL;
3173	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3174	    sizeof(events)) == 0);
3175	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3176
3177	/* The last event should be for the child process's exit. */
3178	wpid = waitpid(fpid, &status, 0);
3179	ATF_REQUIRE(WIFEXITED(status));
3180	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3181
3182	wpid = wait(&status);
3183	ATF_REQUIRE(wpid == -1);
3184	ATF_REQUIRE(errno == ECHILD);
3185}
3186
3187ATF_TP_ADD_TCS(tp)
3188{
3189
3190	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3191	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3192	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3193	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3194	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3195	ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3196	ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3197	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3198	ATF_TP_ADD_TC(tp,
3199	    ptrace__follow_fork_child_detached_unrelated_debugger);
3200	ATF_TP_ADD_TC(tp,
3201	    ptrace__follow_fork_parent_detached_unrelated_debugger);
3202	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3203	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3204	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3205	ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3206	ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3207	ATF_TP_ADD_TC(tp, ptrace__siginfo);
3208	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3209	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3210	ATF_TP_ADD_TC(tp, ptrace__event_mask);
3211	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3212	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3213#if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
3214	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3215#endif
3216	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3217	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3218	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3219	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3220	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3221	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3222	ATF_TP_ADD_TC(tp,
3223	    ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3224	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3225	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3226	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3227	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3228	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3229	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3230	ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3231	ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3232	ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3233#if defined(__amd64__) || defined(__i386__)
3234	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3235#endif
3236	ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
3237
3238	return (atf_no_error());
3239}
3240