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