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