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