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