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