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