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