1 /* $NetBSD: t_ptrace_wait.c,v 1.26 2018/03/13 14:54:13 kamil Exp $ */ 2 3 /*- 4 * Copyright (c) 2016 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __RCSID("$NetBSD: t_ptrace_wait.c,v 1.26 2018/03/13 14:54:13 kamil Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/types.h> 34 #include <sys/ptrace.h> 35 #include <sys/resource.h> 36 #include <sys/stat.h> 37 #include <sys/syscall.h> 38 #include <sys/sysctl.h> 39 #include <sys/wait.h> 40 #include <machine/reg.h> 41 #include <elf.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <lwp.h> 45 #include <sched.h> 46 #include <signal.h> 47 #include <stdint.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <strings.h> 51 #include <time.h> 52 #include <unistd.h> 53 54 #include <atf-c.h> 55 56 #include "h_macros.h" 57 58 #include "t_ptrace_wait.h" 59 #include "msg.h" 60 61 #define PARENT_TO_CHILD(info, fds, msg) \ 62 SYSCALL_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, sizeof(msg)) == 0) 63 64 #define CHILD_FROM_PARENT(info, fds, msg) \ 65 FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0) 66 67 #define CHILD_TO_PARENT(info, fds, msg) \ 68 FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, sizeof(msg)) == 0) 69 70 #define PARENT_FROM_CHILD(info, fds, msg) \ 71 SYSCALL_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0) 72 73 #define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \ 74 strerror(errno)) 75 #define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \ 76 "%d(%s) != %d", res, strerror(res), exp) 77 78 static int debug = 0; 79 80 #define DPRINTF(a, ...) do \ 81 if (debug) printf(a, ##__VA_ARGS__); \ 82 while (/*CONSTCOND*/0) 83 84 85 ATF_TC(traceme1); 86 ATF_TC_HEAD(traceme1, tc) 87 { 88 atf_tc_set_md_var(tc, "descr", 89 "Verify SIGSTOP followed by _exit(2) in a child"); 90 } 91 92 ATF_TC_BODY(traceme1, tc) 93 { 94 const int exitval = 5; 95 const int sigval = SIGSTOP; 96 pid_t child, wpid; 97 #if defined(TWAIT_HAVE_STATUS) 98 int status; 99 #endif 100 101 DPRINTF("Before forking process PID=%d\n", getpid()); 102 SYSCALL_REQUIRE((child = fork()) != -1); 103 if (child == 0) { 104 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 105 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 106 107 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 108 FORKEE_ASSERT(raise(sigval) == 0); 109 110 DPRINTF("Before exiting of the child process\n"); 111 _exit(exitval); 112 } 113 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 114 115 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 116 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 117 118 validate_status_stopped(status, sigval); 119 120 DPRINTF("Before resuming the child process where it left off and " 121 "without signal to be sent\n"); 122 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 123 124 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 125 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 126 127 validate_status_exited(status, exitval); 128 129 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 130 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 131 } 132 133 ATF_TC(traceme2); 134 ATF_TC_HEAD(traceme2, tc) 135 { 136 atf_tc_set_md_var(tc, "descr", 137 "Verify SIGSTOP followed by _exit(2) in a child"); 138 } 139 140 static int traceme2_caught = 0; 141 142 static void 143 traceme2_sighandler(int sig) 144 { 145 FORKEE_ASSERT_EQ(sig, SIGINT); 146 147 ++traceme2_caught; 148 } 149 150 ATF_TC_BODY(traceme2, tc) 151 { 152 const int exitval = 5; 153 const int sigval = SIGSTOP, sigsent = SIGINT; 154 pid_t child, wpid; 155 struct sigaction sa; 156 #if defined(TWAIT_HAVE_STATUS) 157 int status; 158 #endif 159 160 DPRINTF("Before forking process PID=%d\n", getpid()); 161 SYSCALL_REQUIRE((child = fork()) != -1); 162 if (child == 0) { 163 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 164 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 165 166 sa.sa_handler = traceme2_sighandler; 167 sa.sa_flags = SA_SIGINFO; 168 sigemptyset(&sa.sa_mask); 169 170 FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1); 171 172 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 173 FORKEE_ASSERT(raise(sigval) == 0); 174 175 FORKEE_ASSERT_EQ(traceme2_caught, 1); 176 177 DPRINTF("Before exiting of the child process\n"); 178 _exit(exitval); 179 } 180 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 181 182 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 183 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 184 185 validate_status_stopped(status, sigval); 186 187 DPRINTF("Before resuming the child process where it left off and with " 188 "signal %s to be sent\n", strsignal(sigsent)); 189 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1); 190 191 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 192 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 193 194 validate_status_exited(status, exitval); 195 196 DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME); 197 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 198 } 199 200 ATF_TC(traceme3); 201 ATF_TC_HEAD(traceme3, tc) 202 { 203 atf_tc_set_md_var(tc, "descr", 204 "Verify SIGSTOP followed by termination by a signal in a child"); 205 } 206 207 ATF_TC_BODY(traceme3, tc) 208 { 209 const int sigval = SIGSTOP, sigsent = SIGINT /* Without core-dump */; 210 pid_t child, wpid; 211 #if defined(TWAIT_HAVE_STATUS) 212 int status; 213 #endif 214 215 DPRINTF("Before forking process PID=%d\n", getpid()); 216 SYSCALL_REQUIRE((child = fork()) != -1); 217 if (child == 0) { 218 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 219 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 220 221 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 222 FORKEE_ASSERT(raise(sigval) == 0); 223 224 /* NOTREACHED */ 225 FORKEE_ASSERTX(0 && 226 "Child should be terminated by a signal from its parent"); 227 } 228 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 229 230 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 231 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 232 233 validate_status_stopped(status, sigval); 234 235 DPRINTF("Before resuming the child process where it left off and with " 236 "signal %s to be sent\n", strsignal(sigsent)); 237 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1); 238 239 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 240 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 241 242 validate_status_signaled(status, sigsent, 0); 243 244 DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME); 245 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 246 } 247 248 ATF_TC(traceme4); 249 ATF_TC_HEAD(traceme4, tc) 250 { 251 atf_tc_set_md_var(tc, "descr", 252 "Verify SIGSTOP followed by SIGCONT and _exit(2) in a child"); 253 } 254 255 ATF_TC_BODY(traceme4, tc) 256 { 257 const int exitval = 5; 258 const int sigval = SIGSTOP, sigsent = SIGCONT; 259 pid_t child, wpid; 260 #if defined(TWAIT_HAVE_STATUS) 261 int status; 262 #endif 263 264 DPRINTF("Before forking process PID=%d\n", getpid()); 265 SYSCALL_REQUIRE((child = fork()) != -1); 266 if (child == 0) { 267 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 268 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 269 270 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 271 FORKEE_ASSERT(raise(sigval) == 0); 272 273 DPRINTF("Before raising %s from child\n", strsignal(sigsent)); 274 FORKEE_ASSERT(raise(sigsent) == 0); 275 276 DPRINTF("Before exiting of the child process\n"); 277 _exit(exitval); 278 } 279 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(),child); 280 281 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 282 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 283 284 validate_status_stopped(status, sigval); 285 286 DPRINTF("Before resuming the child process where it left off and " 287 "without signal to be sent\n"); 288 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 289 290 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 291 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 292 293 validate_status_stopped(status, sigsent); 294 295 DPRINTF("Before resuming the child process where it left off and " 296 "without signal to be sent\n"); 297 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 298 299 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 300 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 301 302 validate_status_exited(status, exitval); 303 304 DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME); 305 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 306 } 307 308 #if defined(TWAIT_HAVE_PID) 309 ATF_TC(attach1); 310 ATF_TC_HEAD(attach1, tc) 311 { 312 atf_tc_set_md_var(tc, "descr", 313 "Assert that tracer sees process termination before the parent"); 314 } 315 316 static void 317 attach1_raw(bool raw) 318 { 319 struct msg_fds parent_tracee, parent_tracer; 320 const int exitval_tracee = 5; 321 const int exitval_tracer = 10; 322 pid_t tracee, tracer, wpid; 323 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 324 #if defined(TWAIT_HAVE_STATUS) 325 int status; 326 #endif 327 328 DPRINTF("Spawn tracee\n"); 329 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 330 tracee = atf_utils_fork(); 331 if (tracee == 0) { 332 // Wait for parent to let us exit 333 CHILD_FROM_PARENT("exit tracee", parent_tracee, msg); 334 _exit(exitval_tracee); 335 } 336 337 DPRINTF("Spawn debugger\n"); 338 SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); 339 tracer = atf_utils_fork(); 340 if (tracer == 0) { 341 DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); 342 FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 343 344 /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ 345 FORKEE_REQUIRE_SUCCESS( 346 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 347 348 forkee_status_stopped(status, SIGSTOP); 349 350 /* Resume tracee with PT_CONTINUE */ 351 FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 352 353 /* Inform parent that tracer has attached to tracee */ 354 CHILD_TO_PARENT("tracer ready", parent_tracer, msg); 355 356 /* Wait for parent to tell use that tracee should have exited */ 357 CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg); 358 359 /* Wait for tracee and assert that it exited */ 360 FORKEE_REQUIRE_SUCCESS( 361 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 362 363 forkee_status_exited(status, exitval_tracee); 364 DPRINTF("Tracee %d exited with %d\n", tracee, exitval_tracee); 365 366 DPRINTF("Before exiting of the tracer process\n"); 367 _exit(exitval_tracer); 368 } 369 370 DPRINTF("Wait for the tracer to attach to the tracee\n"); 371 PARENT_FROM_CHILD("tracer ready", parent_tracer, msg); 372 373 DPRINTF("Resume the tracee and let it exit\n"); 374 PARENT_TO_CHILD("exit tracee", parent_tracee, msg); 375 376 DPRINTF("Detect that tracee is zombie\n"); 377 if (raw) 378 await_zombie_raw(tracee, 0); 379 else 380 await_zombie(tracee); 381 382 DPRINTF("Assert that there is no status about tracee %d - " 383 "Tracer must detect zombie first - calling %s()\n", tracee, 384 TWAIT_FNAME); 385 TWAIT_REQUIRE_SUCCESS( 386 wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0); 387 388 DPRINTF("Tell the tracer child should have exited\n"); 389 PARENT_TO_CHILD("wait for tracee exit", parent_tracer, msg); 390 DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n", 391 TWAIT_FNAME); 392 393 DPRINTF("Wait from tracer child to complete waiting for tracee\n"); 394 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0), 395 tracer); 396 397 validate_status_exited(status, exitval_tracer); 398 399 DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", 400 TWAIT_FNAME); 401 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 402 tracee); 403 404 validate_status_exited(status, exitval_tracee); 405 406 msg_close(&parent_tracer); 407 msg_close(&parent_tracee); 408 } 409 410 ATF_TC_BODY(attach1, tc) 411 { 412 413 /* Reuse this test with race1 */ 414 attach1_raw(false); 415 } 416 417 #endif 418 419 #if defined(TWAIT_HAVE_PID) 420 ATF_TC(attach2); 421 ATF_TC_HEAD(attach2, tc) 422 { 423 atf_tc_set_md_var(tc, "descr", 424 "Assert that any tracer sees process termination before its " 425 "parent"); 426 } 427 428 ATF_TC_BODY(attach2, tc) 429 { 430 struct msg_fds parent_tracer, parent_tracee; 431 const int exitval_tracee = 5; 432 const int exitval_tracer1 = 10, exitval_tracer2 = 20; 433 pid_t tracee, tracer, wpid; 434 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 435 #if defined(TWAIT_HAVE_STATUS) 436 int status; 437 #endif 438 439 DPRINTF("Spawn tracee\n"); 440 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 441 tracee = atf_utils_fork(); 442 if (tracee == 0) { 443 /* Wait for message from the parent */ 444 CHILD_FROM_PARENT("Message 1", parent_tracee, msg); 445 _exit(exitval_tracee); 446 } 447 448 DPRINTF("Spawn debugger\n"); 449 SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); 450 tracer = atf_utils_fork(); 451 if (tracer == 0) { 452 /* Fork again and drop parent to reattach to PID 1 */ 453 tracer = atf_utils_fork(); 454 if (tracer != 0) 455 _exit(exitval_tracer1); 456 457 DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); 458 FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 459 460 /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ 461 FORKEE_REQUIRE_SUCCESS( 462 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 463 464 forkee_status_stopped(status, SIGSTOP); 465 466 /* Resume tracee with PT_CONTINUE */ 467 FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 468 469 /* Inform parent that tracer has attached to tracee */ 470 CHILD_TO_PARENT("Message 1", parent_tracer, msg); 471 CHILD_FROM_PARENT("Message 2", parent_tracer, msg); 472 473 /* Wait for tracee and assert that it exited */ 474 FORKEE_REQUIRE_SUCCESS( 475 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 476 477 forkee_status_exited(status, exitval_tracee); 478 479 DPRINTF("Before exiting of the tracer process\n"); 480 _exit(exitval_tracer2); 481 } 482 DPRINTF("Wait for the tracer process (direct child) to exit calling " 483 "%s()\n", TWAIT_FNAME); 484 TWAIT_REQUIRE_SUCCESS( 485 wpid = TWAIT_GENERIC(tracer, &status, 0), tracer); 486 487 validate_status_exited(status, exitval_tracer1); 488 489 DPRINTF("Wait for the non-exited tracee process with %s()\n", 490 TWAIT_FNAME); 491 TWAIT_REQUIRE_SUCCESS( 492 wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0); 493 494 DPRINTF("Wait for the tracer to attach to the tracee\n"); 495 PARENT_FROM_CHILD("Message 1", parent_tracer, msg); 496 DPRINTF("Resume the tracee and let it exit\n"); 497 PARENT_TO_CHILD("Message 1", parent_tracee, msg); 498 499 DPRINTF("Detect that tracee is zombie\n"); 500 await_zombie(tracee); 501 502 DPRINTF("Assert that there is no status about tracee - " 503 "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME); 504 TWAIT_REQUIRE_SUCCESS( 505 wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0); 506 507 DPRINTF("Resume the tracer and let it detect exited tracee\n"); 508 PARENT_TO_CHILD("Message 2", parent_tracer, msg); 509 510 DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", 511 TWAIT_FNAME); 512 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), 513 tracee); 514 515 validate_status_exited(status, exitval_tracee); 516 517 msg_close(&parent_tracer); 518 msg_close(&parent_tracee); 519 } 520 #endif 521 522 ATF_TC(attach3); 523 ATF_TC_HEAD(attach3, tc) 524 { 525 atf_tc_set_md_var(tc, "descr", 526 "Assert that tracer parent can PT_ATTACH to its child"); 527 } 528 529 ATF_TC_BODY(attach3, tc) 530 { 531 struct msg_fds parent_tracee; 532 const int exitval_tracee = 5; 533 pid_t tracee, wpid; 534 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 535 #if defined(TWAIT_HAVE_STATUS) 536 int status; 537 #endif 538 539 DPRINTF("Spawn tracee\n"); 540 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 541 tracee = atf_utils_fork(); 542 if (tracee == 0) { 543 CHILD_FROM_PARENT("Message 1", parent_tracee, msg); 544 DPRINTF("Parent should now attach to tracee\n"); 545 546 CHILD_FROM_PARENT("Message 2", parent_tracee, msg); 547 /* Wait for message from the parent */ 548 _exit(exitval_tracee); 549 } 550 PARENT_TO_CHILD("Message 1", parent_tracee, msg); 551 552 DPRINTF("Before calling PT_ATTACH for tracee %d\n", tracee); 553 SYSCALL_REQUIRE(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 554 555 DPRINTF("Wait for the stopped tracee process with %s()\n", 556 TWAIT_FNAME); 557 TWAIT_REQUIRE_SUCCESS( 558 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 559 560 validate_status_stopped(status, SIGSTOP); 561 562 DPRINTF("Resume tracee with PT_CONTINUE\n"); 563 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 564 565 DPRINTF("Let the tracee exit now\n"); 566 PARENT_TO_CHILD("Message 2", parent_tracee, msg); 567 568 DPRINTF("Wait for tracee to exit with %s()\n", TWAIT_FNAME); 569 TWAIT_REQUIRE_SUCCESS( 570 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 571 572 validate_status_exited(status, exitval_tracee); 573 574 DPRINTF("Before calling %s() for tracee\n", TWAIT_FNAME); 575 TWAIT_REQUIRE_FAILURE(ECHILD, 576 wpid = TWAIT_GENERIC(tracee, &status, 0)); 577 578 msg_close(&parent_tracee); 579 } 580 581 ATF_TC(attach4); 582 ATF_TC_HEAD(attach4, tc) 583 { 584 atf_tc_set_md_var(tc, "descr", 585 "Assert that tracer child can PT_ATTACH to its parent"); 586 } 587 588 ATF_TC_BODY(attach4, tc) 589 { 590 struct msg_fds parent_tracee; 591 const int exitval_tracer = 5; 592 pid_t tracer, wpid; 593 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 594 #if defined(TWAIT_HAVE_STATUS) 595 int status; 596 #endif 597 598 DPRINTF("Spawn tracer\n"); 599 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 600 tracer = atf_utils_fork(); 601 if (tracer == 0) { 602 603 /* Wait for message from the parent */ 604 CHILD_FROM_PARENT("Message 1", parent_tracee, msg); 605 606 DPRINTF("Attach to parent PID %d with PT_ATTACH from child\n", 607 getppid()); 608 FORKEE_ASSERT(ptrace(PT_ATTACH, getppid(), NULL, 0) != -1); 609 610 DPRINTF("Wait for the stopped parent process with %s()\n", 611 TWAIT_FNAME); 612 FORKEE_REQUIRE_SUCCESS( 613 wpid = TWAIT_GENERIC(getppid(), &status, 0), getppid()); 614 615 forkee_status_stopped(status, SIGSTOP); 616 617 DPRINTF("Resume parent with PT_DETACH\n"); 618 FORKEE_ASSERT(ptrace(PT_DETACH, getppid(), (void *)1, 0) 619 != -1); 620 621 /* Tell parent we are ready */ 622 CHILD_TO_PARENT("Message 1", parent_tracee, msg); 623 624 _exit(exitval_tracer); 625 } 626 627 DPRINTF("Wait for the tracer to become ready\n"); 628 PARENT_TO_CHILD("Message 1", parent_tracee, msg); 629 DPRINTF("Allow the tracer to exit now\n"); 630 PARENT_FROM_CHILD("Message 1", parent_tracee, msg); 631 632 DPRINTF("Wait for tracer to exit with %s()\n", TWAIT_FNAME); 633 TWAIT_REQUIRE_SUCCESS( 634 wpid = TWAIT_GENERIC(tracer, &status, 0), tracer); 635 636 validate_status_exited(status, exitval_tracer); 637 638 DPRINTF("Before calling %s() for tracer\n", TWAIT_FNAME); 639 TWAIT_REQUIRE_FAILURE(ECHILD, 640 wpid = TWAIT_GENERIC(tracer, &status, 0)); 641 642 msg_close(&parent_tracee); 643 } 644 645 #if defined(TWAIT_HAVE_PID) 646 ATF_TC(attach5); 647 ATF_TC_HEAD(attach5, tc) 648 { 649 atf_tc_set_md_var(tc, "descr", 650 "Assert that tracer sees its parent when attached to tracer " 651 "(check getppid(2))"); 652 } 653 654 ATF_TC_BODY(attach5, tc) 655 { 656 struct msg_fds parent_tracer, parent_tracee; 657 const int exitval_tracee = 5; 658 const int exitval_tracer = 10; 659 pid_t parent, tracee, tracer, wpid; 660 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 661 #if defined(TWAIT_HAVE_STATUS) 662 int status; 663 #endif 664 665 DPRINTF("Spawn tracee\n"); 666 SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); 667 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 668 tracee = atf_utils_fork(); 669 if (tracee == 0) { 670 parent = getppid(); 671 672 /* Emit message to the parent */ 673 CHILD_TO_PARENT("tracee ready", parent_tracee, msg); 674 CHILD_FROM_PARENT("exit tracee", parent_tracee, msg); 675 676 FORKEE_ASSERT_EQ(parent, getppid()); 677 678 _exit(exitval_tracee); 679 } 680 DPRINTF("Wait for child to record its parent identifier (pid)\n"); 681 PARENT_FROM_CHILD("tracee ready", parent_tracee, msg); 682 683 DPRINTF("Spawn debugger\n"); 684 tracer = atf_utils_fork(); 685 if (tracer == 0) { 686 /* No IPC to communicate with the child */ 687 DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); 688 FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 689 690 /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ 691 FORKEE_REQUIRE_SUCCESS( 692 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 693 694 forkee_status_stopped(status, SIGSTOP); 695 696 /* Resume tracee with PT_CONTINUE */ 697 FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 698 699 /* Inform parent that tracer has attached to tracee */ 700 CHILD_TO_PARENT("tracer ready", parent_tracer, msg); 701 702 /* Wait for parent to tell use that tracee should have exited */ 703 CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg); 704 705 /* Wait for tracee and assert that it exited */ 706 FORKEE_REQUIRE_SUCCESS( 707 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 708 709 forkee_status_exited(status, exitval_tracee); 710 711 DPRINTF("Before exiting of the tracer process\n"); 712 _exit(exitval_tracer); 713 } 714 715 DPRINTF("Wait for the tracer to attach to the tracee\n"); 716 PARENT_FROM_CHILD("tracer ready", parent_tracer, msg); 717 718 DPRINTF("Resume the tracee and let it exit\n"); 719 PARENT_TO_CHILD("exit tracee", parent_tracee, msg); 720 721 DPRINTF("Detect that tracee is zombie\n"); 722 await_zombie(tracee); 723 724 DPRINTF("Assert that there is no status about tracee - " 725 "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME); 726 TWAIT_REQUIRE_SUCCESS( 727 wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0); 728 729 DPRINTF("Tell the tracer child should have exited\n"); 730 PARENT_TO_CHILD("wait for tracee exit", parent_tracer, msg); 731 732 DPRINTF("Wait from tracer child to complete waiting for tracee\n"); 733 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0), 734 tracer); 735 736 validate_status_exited(status, exitval_tracer); 737 738 DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", 739 TWAIT_FNAME); 740 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 741 tracee); 742 743 validate_status_exited(status, exitval_tracee); 744 745 msg_close(&parent_tracer); 746 msg_close(&parent_tracee); 747 } 748 #endif 749 750 #if defined(TWAIT_HAVE_PID) 751 ATF_TC(attach6); 752 ATF_TC_HEAD(attach6, tc) 753 { 754 atf_tc_set_md_var(tc, "descr", 755 "Assert that tracer sees its parent when attached to tracer " 756 "(check sysctl(7) and struct kinfo_proc2)"); 757 } 758 759 ATF_TC_BODY(attach6, tc) 760 { 761 struct msg_fds parent_tracee, parent_tracer; 762 const int exitval_tracee = 5; 763 const int exitval_tracer = 10; 764 pid_t parent, tracee, tracer, wpid; 765 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 766 #if defined(TWAIT_HAVE_STATUS) 767 int status; 768 #endif 769 int name[CTL_MAXNAME]; 770 struct kinfo_proc2 kp; 771 size_t len = sizeof(kp); 772 unsigned int namelen; 773 774 DPRINTF("Spawn tracee\n"); 775 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 776 SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); 777 tracee = atf_utils_fork(); 778 if (tracee == 0) { 779 parent = getppid(); 780 781 /* Emit message to the parent */ 782 CHILD_TO_PARENT("Message 1", parent_tracee, msg); 783 CHILD_FROM_PARENT("Message 2", parent_tracee, msg); 784 785 namelen = 0; 786 name[namelen++] = CTL_KERN; 787 name[namelen++] = KERN_PROC2; 788 name[namelen++] = KERN_PROC_PID; 789 name[namelen++] = getpid(); 790 name[namelen++] = len; 791 name[namelen++] = 1; 792 793 FORKEE_ASSERT(sysctl(name, namelen, &kp, &len, NULL, 0) == 0); 794 FORKEE_ASSERT_EQ(parent, kp.p_ppid); 795 796 _exit(exitval_tracee); 797 } 798 799 DPRINTF("Wait for child to record its parent identifier (pid)\n"); 800 PARENT_FROM_CHILD("Message 1", parent_tracee, msg); 801 802 DPRINTF("Spawn debugger\n"); 803 tracer = atf_utils_fork(); 804 if (tracer == 0) { 805 /* No IPC to communicate with the child */ 806 DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); 807 FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 808 809 /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ 810 FORKEE_REQUIRE_SUCCESS( 811 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 812 813 forkee_status_stopped(status, SIGSTOP); 814 815 /* Resume tracee with PT_CONTINUE */ 816 FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 817 818 /* Inform parent that tracer has attached to tracee */ 819 CHILD_TO_PARENT("Message 1", parent_tracer, msg); 820 821 CHILD_FROM_PARENT("Message 2", parent_tracer, msg); 822 823 /* Wait for tracee and assert that it exited */ 824 FORKEE_REQUIRE_SUCCESS( 825 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 826 827 forkee_status_exited(status, exitval_tracee); 828 829 DPRINTF("Before exiting of the tracer process\n"); 830 _exit(exitval_tracer); 831 } 832 833 DPRINTF("Wait for the tracer to attach to the tracee\n"); 834 PARENT_FROM_CHILD("Message 1", parent_tracer, msg); 835 836 DPRINTF("Resume the tracee and let it exit\n"); 837 PARENT_TO_CHILD("Message 1", parent_tracee, msg); 838 839 DPRINTF("Detect that tracee is zombie\n"); 840 await_zombie(tracee); 841 842 DPRINTF("Assert that there is no status about tracee - " 843 "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME); 844 TWAIT_REQUIRE_SUCCESS( 845 wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0); 846 847 DPRINTF("Resume the tracer and let it detect exited tracee\n"); 848 PARENT_TO_CHILD("Message 2", parent_tracer, msg); 849 850 DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n", 851 TWAIT_FNAME); 852 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0), 853 tracer); 854 855 validate_status_exited(status, exitval_tracer); 856 857 DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", 858 TWAIT_FNAME); 859 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 860 tracee); 861 862 validate_status_exited(status, exitval_tracee); 863 864 msg_close(&parent_tracee); 865 msg_close(&parent_tracer); 866 } 867 #endif 868 869 #if defined(TWAIT_HAVE_PID) 870 ATF_TC(attach7); 871 ATF_TC_HEAD(attach7, tc) 872 { 873 atf_tc_set_md_var(tc, "descr", 874 "Assert that tracer sees its parent when attached to tracer " 875 "(check /proc/curproc/status 3rd column)"); 876 } 877 878 ATF_TC_BODY(attach7, tc) 879 { 880 struct msg_fds parent_tracee, parent_tracer; 881 int rv; 882 const int exitval_tracee = 5; 883 const int exitval_tracer = 10; 884 pid_t parent, tracee, tracer, wpid; 885 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 886 #if defined(TWAIT_HAVE_STATUS) 887 int status; 888 #endif 889 FILE *fp; 890 struct stat st; 891 const char *fname = "/proc/curproc/status"; 892 char s_executable[MAXPATHLEN]; 893 int s_pid, s_ppid; 894 /* 895 * Format: 896 * EXECUTABLE PID PPID ... 897 */ 898 899 SYSCALL_REQUIRE((rv = stat(fname, &st)) == 0 || (errno == ENOENT)); 900 if (rv != 0) { 901 atf_tc_skip("/proc/curproc/status not found"); 902 } 903 904 DPRINTF("Spawn tracee\n"); 905 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 906 SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); 907 tracee = atf_utils_fork(); 908 if (tracee == 0) { 909 parent = getppid(); 910 911 // Wait for parent to let us exit 912 CHILD_TO_PARENT("tracee ready", parent_tracee, msg); 913 CHILD_FROM_PARENT("tracee exit", parent_tracee, msg); 914 915 FORKEE_ASSERT((fp = fopen(fname, "r")) != NULL); 916 fscanf(fp, "%s %d %d", s_executable, &s_pid, &s_ppid); 917 FORKEE_ASSERT(fclose(fp) == 0); 918 FORKEE_ASSERT_EQ(parent, s_ppid); 919 920 _exit(exitval_tracee); 921 } 922 923 DPRINTF("Wait for child to record its parent identifier (pid)\n"); 924 PARENT_FROM_CHILD("tracee ready", parent_tracee, msg); 925 926 DPRINTF("Spawn debugger\n"); 927 tracer = atf_utils_fork(); 928 if (tracer == 0) { 929 DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); 930 FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 931 932 /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ 933 FORKEE_REQUIRE_SUCCESS( 934 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 935 936 forkee_status_stopped(status, SIGSTOP); 937 938 /* Resume tracee with PT_CONTINUE */ 939 FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 940 941 /* Inform parent that tracer has attached to tracee */ 942 CHILD_TO_PARENT("tracer ready", parent_tracer, msg); 943 944 /* Wait for parent to tell use that tracee should have exited */ 945 CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg); 946 947 /* Wait for tracee and assert that it exited */ 948 FORKEE_REQUIRE_SUCCESS( 949 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 950 951 forkee_status_exited(status, exitval_tracee); 952 953 DPRINTF("Before exiting of the tracer process\n"); 954 _exit(exitval_tracer); 955 } 956 DPRINTF("Wait for the tracer to attach to the tracee\n"); 957 PARENT_FROM_CHILD("tracer ready", parent_tracer, msg); 958 DPRINTF("Resume the tracee and let it exit\n"); 959 PARENT_TO_CHILD("tracee exit", parent_tracee, msg); 960 961 DPRINTF("Detect that tracee is zombie\n"); 962 await_zombie(tracee); 963 964 DPRINTF("Assert that there is no status about tracee - " 965 "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME); 966 TWAIT_REQUIRE_SUCCESS( 967 wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0); 968 969 DPRINTF("Resume the tracer and let it detect exited tracee\n"); 970 PARENT_TO_CHILD("Message 2", parent_tracer, msg); 971 972 DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n", 973 TWAIT_FNAME); 974 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0), 975 tracer); 976 977 validate_status_exited(status, exitval_tracer); 978 979 DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", 980 TWAIT_FNAME); 981 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 982 tracee); 983 984 validate_status_exited(status, exitval_tracee); 985 986 msg_close(&parent_tracee); 987 msg_close(&parent_tracer); 988 } 989 #endif 990 991 ATF_TC(eventmask1); 992 ATF_TC_HEAD(eventmask1, tc) 993 { 994 atf_tc_set_md_var(tc, "descr", 995 "Verify that empty EVENT_MASK is preserved"); 996 } 997 998 ATF_TC_BODY(eventmask1, tc) 999 { 1000 const int exitval = 5; 1001 const int sigval = SIGSTOP; 1002 pid_t child, wpid; 1003 #if defined(TWAIT_HAVE_STATUS) 1004 int status; 1005 #endif 1006 ptrace_event_t set_event, get_event; 1007 const int len = sizeof(ptrace_event_t); 1008 1009 DPRINTF("Before forking process PID=%d\n", getpid()); 1010 SYSCALL_REQUIRE((child = fork()) != -1); 1011 if (child == 0) { 1012 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1013 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1014 1015 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1016 FORKEE_ASSERT(raise(sigval) == 0); 1017 1018 DPRINTF("Before exiting of the child process\n"); 1019 _exit(exitval); 1020 } 1021 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1022 1023 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1024 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1025 1026 validate_status_stopped(status, sigval); 1027 1028 set_event.pe_set_event = 0; 1029 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1); 1030 SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1); 1031 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0); 1032 1033 DPRINTF("Before resuming the child process where it left off and " 1034 "without signal to be sent\n"); 1035 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1036 1037 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1038 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1039 1040 validate_status_exited(status, exitval); 1041 1042 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1043 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1044 } 1045 1046 ATF_TC(eventmask2); 1047 ATF_TC_HEAD(eventmask2, tc) 1048 { 1049 atf_tc_set_md_var(tc, "descr", 1050 "Verify that PTRACE_FORK in EVENT_MASK is preserved"); 1051 } 1052 1053 ATF_TC_BODY(eventmask2, tc) 1054 { 1055 const int exitval = 5; 1056 const int sigval = SIGSTOP; 1057 pid_t child, wpid; 1058 #if defined(TWAIT_HAVE_STATUS) 1059 int status; 1060 #endif 1061 ptrace_event_t set_event, get_event; 1062 const int len = sizeof(ptrace_event_t); 1063 1064 DPRINTF("Before forking process PID=%d\n", getpid()); 1065 SYSCALL_REQUIRE((child = fork()) != -1); 1066 if (child == 0) { 1067 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1068 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1069 1070 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1071 FORKEE_ASSERT(raise(sigval) == 0); 1072 1073 DPRINTF("Before exiting of the child process\n"); 1074 _exit(exitval); 1075 } 1076 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1077 1078 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1079 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1080 1081 validate_status_stopped(status, sigval); 1082 1083 set_event.pe_set_event = PTRACE_FORK; 1084 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1); 1085 SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1); 1086 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0); 1087 1088 DPRINTF("Before resuming the child process where it left off and " 1089 "without signal to be sent\n"); 1090 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1091 1092 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1093 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1094 1095 validate_status_exited(status, exitval); 1096 1097 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1098 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1099 } 1100 1101 ATF_TC(eventmask3); 1102 ATF_TC_HEAD(eventmask3, tc) 1103 { 1104 atf_tc_set_md_var(tc, "descr", 1105 "Verify that PTRACE_VFORK in EVENT_MASK is preserved"); 1106 } 1107 1108 ATF_TC_BODY(eventmask3, tc) 1109 { 1110 const int exitval = 5; 1111 const int sigval = SIGSTOP; 1112 pid_t child, wpid; 1113 #if defined(TWAIT_HAVE_STATUS) 1114 int status; 1115 #endif 1116 ptrace_event_t set_event, get_event; 1117 const int len = sizeof(ptrace_event_t); 1118 1119 atf_tc_expect_fail("PR kern/51630"); 1120 1121 DPRINTF("Before forking process PID=%d\n", getpid()); 1122 SYSCALL_REQUIRE((child = fork()) != -1); 1123 if (child == 0) { 1124 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1125 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1126 1127 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1128 FORKEE_ASSERT(raise(sigval) == 0); 1129 1130 DPRINTF("Before exiting of the child process\n"); 1131 _exit(exitval); 1132 } 1133 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1134 1135 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1136 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1137 1138 validate_status_stopped(status, sigval); 1139 1140 set_event.pe_set_event = PTRACE_VFORK; 1141 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1 || errno == ENOTSUP); 1142 SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1); 1143 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0); 1144 1145 DPRINTF("Before resuming the child process where it left off and " 1146 "without signal to be sent\n"); 1147 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1148 1149 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1150 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1151 1152 validate_status_exited(status, exitval); 1153 1154 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1155 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1156 } 1157 1158 ATF_TC(eventmask4); 1159 ATF_TC_HEAD(eventmask4, tc) 1160 { 1161 atf_tc_set_md_var(tc, "descr", 1162 "Verify that PTRACE_VFORK_DONE in EVENT_MASK is preserved"); 1163 } 1164 1165 ATF_TC_BODY(eventmask4, tc) 1166 { 1167 const int exitval = 5; 1168 const int sigval = SIGSTOP; 1169 pid_t child, wpid; 1170 #if defined(TWAIT_HAVE_STATUS) 1171 int status; 1172 #endif 1173 ptrace_event_t set_event, get_event; 1174 const int len = sizeof(ptrace_event_t); 1175 1176 DPRINTF("Before forking process PID=%d\n", getpid()); 1177 SYSCALL_REQUIRE((child = fork()) != -1); 1178 if (child == 0) { 1179 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1180 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1181 1182 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1183 FORKEE_ASSERT(raise(sigval) == 0); 1184 1185 DPRINTF("Before exiting of the child process\n"); 1186 _exit(exitval); 1187 } 1188 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1189 1190 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1191 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1192 1193 validate_status_stopped(status, sigval); 1194 1195 set_event.pe_set_event = PTRACE_VFORK_DONE; 1196 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1); 1197 SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1); 1198 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0); 1199 1200 DPRINTF("Before resuming the child process where it left off and " 1201 "without signal to be sent\n"); 1202 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1203 1204 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1205 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1206 1207 validate_status_exited(status, exitval); 1208 1209 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1210 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1211 } 1212 1213 ATF_TC(eventmask5); 1214 ATF_TC_HEAD(eventmask5, tc) 1215 { 1216 atf_tc_set_md_var(tc, "descr", 1217 "Verify that PTRACE_LWP_CREATE in EVENT_MASK is preserved"); 1218 } 1219 1220 ATF_TC_BODY(eventmask5, tc) 1221 { 1222 const int exitval = 5; 1223 const int sigval = SIGSTOP; 1224 pid_t child, wpid; 1225 #if defined(TWAIT_HAVE_STATUS) 1226 int status; 1227 #endif 1228 ptrace_event_t set_event, get_event; 1229 const int len = sizeof(ptrace_event_t); 1230 1231 DPRINTF("Before forking process PID=%d\n", getpid()); 1232 SYSCALL_REQUIRE((child = fork()) != -1); 1233 if (child == 0) { 1234 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1235 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1236 1237 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1238 FORKEE_ASSERT(raise(sigval) == 0); 1239 1240 DPRINTF("Before exiting of the child process\n"); 1241 _exit(exitval); 1242 } 1243 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1244 1245 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1246 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1247 1248 validate_status_stopped(status, sigval); 1249 1250 set_event.pe_set_event = PTRACE_LWP_CREATE; 1251 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1); 1252 SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1); 1253 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0); 1254 1255 DPRINTF("Before resuming the child process where it left off and " 1256 "without signal to be sent\n"); 1257 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1258 1259 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1260 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1261 1262 validate_status_exited(status, exitval); 1263 1264 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1265 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1266 } 1267 1268 ATF_TC(eventmask6); 1269 ATF_TC_HEAD(eventmask6, tc) 1270 { 1271 atf_tc_set_md_var(tc, "descr", 1272 "Verify that PTRACE_LWP_EXIT in EVENT_MASK is preserved"); 1273 } 1274 1275 ATF_TC_BODY(eventmask6, tc) 1276 { 1277 const int exitval = 5; 1278 const int sigval = SIGSTOP; 1279 pid_t child, wpid; 1280 #if defined(TWAIT_HAVE_STATUS) 1281 int status; 1282 #endif 1283 ptrace_event_t set_event, get_event; 1284 const int len = sizeof(ptrace_event_t); 1285 1286 DPRINTF("Before forking process PID=%d\n", getpid()); 1287 SYSCALL_REQUIRE((child = fork()) != -1); 1288 if (child == 0) { 1289 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1290 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1291 1292 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1293 FORKEE_ASSERT(raise(sigval) == 0); 1294 1295 DPRINTF("Before exiting of the child process\n"); 1296 _exit(exitval); 1297 } 1298 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1299 1300 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1301 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1302 1303 validate_status_stopped(status, sigval); 1304 1305 set_event.pe_set_event = PTRACE_LWP_EXIT; 1306 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1); 1307 SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1); 1308 ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0); 1309 1310 DPRINTF("Before resuming the child process where it left off and " 1311 "without signal to be sent\n"); 1312 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1313 1314 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1315 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1316 1317 validate_status_exited(status, exitval); 1318 1319 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1320 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1321 } 1322 1323 #if defined(TWAIT_HAVE_PID) 1324 ATF_TC(fork1); 1325 ATF_TC_HEAD(fork1, tc) 1326 { 1327 atf_tc_set_md_var(tc, "descr", 1328 "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK " 1329 "set to PTRACE_FORK"); 1330 } 1331 1332 ATF_TC_BODY(fork1, tc) 1333 { 1334 const int exitval = 5; 1335 const int exitval2 = 15; 1336 const int sigval = SIGSTOP; 1337 pid_t child, child2, wpid; 1338 #if defined(TWAIT_HAVE_STATUS) 1339 int status; 1340 #endif 1341 ptrace_state_t state; 1342 const int slen = sizeof(state); 1343 ptrace_event_t event; 1344 const int elen = sizeof(event); 1345 1346 DPRINTF("Before forking process PID=%d\n", getpid()); 1347 SYSCALL_REQUIRE((child = fork()) != -1); 1348 if (child == 0) { 1349 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1350 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1351 1352 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1353 FORKEE_ASSERT(raise(sigval) == 0); 1354 1355 FORKEE_ASSERT((child2 = fork()) != -1); 1356 1357 if (child2 == 0) 1358 _exit(exitval2); 1359 1360 FORKEE_REQUIRE_SUCCESS 1361 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 1362 1363 forkee_status_exited(status, exitval2); 1364 1365 DPRINTF("Before exiting of the child process\n"); 1366 _exit(exitval); 1367 } 1368 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1369 1370 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1371 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1372 1373 validate_status_stopped(status, sigval); 1374 1375 DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child); 1376 event.pe_set_event = PTRACE_FORK; 1377 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 1378 1379 DPRINTF("Before resuming the child process where it left off and " 1380 "without signal to be sent\n"); 1381 DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, " 1382 "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and " 1383 "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, " 1384 "state.pe_other_pid=child)\n", child); 1385 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1386 1387 DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child); 1388 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1389 1390 validate_status_stopped(status, SIGTRAP); 1391 1392 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 1393 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK); 1394 1395 child2 = state.pe_other_pid; 1396 DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2); 1397 1398 DPRINTF("Before calling %s() for the forkee %d of the child %d\n", 1399 TWAIT_FNAME, child2, child); 1400 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 1401 child2); 1402 1403 validate_status_stopped(status, SIGTRAP); 1404 1405 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1); 1406 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK); 1407 ATF_REQUIRE_EQ(state.pe_other_pid, child); 1408 1409 DPRINTF("Before resuming the forkee process where it left off and " 1410 "without signal to be sent\n"); 1411 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1); 1412 1413 DPRINTF("Before resuming the child process where it left off and " 1414 "without signal to be sent\n"); 1415 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1416 1417 DPRINTF("Before calling %s() for the forkee - expected exited\n", 1418 TWAIT_FNAME); 1419 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 1420 child2); 1421 1422 validate_status_exited(status, exitval2); 1423 1424 DPRINTF("Before calling %s() for the forkee - expected no process\n", 1425 TWAIT_FNAME); 1426 TWAIT_REQUIRE_FAILURE(ECHILD, 1427 wpid = TWAIT_GENERIC(child2, &status, 0)); 1428 1429 DPRINTF("Before calling %s() for the child - expected stopped " 1430 "SIGCHLD\n", TWAIT_FNAME); 1431 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1432 1433 validate_status_stopped(status, SIGCHLD); 1434 1435 DPRINTF("Before resuming the child process where it left off and " 1436 "without signal to be sent\n"); 1437 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1438 1439 DPRINTF("Before calling %s() for the child - expected exited\n", 1440 TWAIT_FNAME); 1441 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1442 1443 validate_status_exited(status, exitval); 1444 1445 DPRINTF("Before calling %s() for the child - expected no process\n", 1446 TWAIT_FNAME); 1447 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1448 } 1449 #endif 1450 1451 ATF_TC(fork2); 1452 ATF_TC_HEAD(fork2, tc) 1453 { 1454 atf_tc_set_md_var(tc, "descr", 1455 "Verify that fork(2) is not intercepted by ptrace(2) with empty " 1456 "EVENT_MASK"); 1457 } 1458 1459 ATF_TC_BODY(fork2, tc) 1460 { 1461 const int exitval = 5; 1462 const int exitval2 = 15; 1463 const int sigval = SIGSTOP; 1464 pid_t child, child2, wpid; 1465 #if defined(TWAIT_HAVE_STATUS) 1466 int status; 1467 #endif 1468 ptrace_event_t event; 1469 const int elen = sizeof(event); 1470 1471 DPRINTF("Before forking process PID=%d\n", getpid()); 1472 SYSCALL_REQUIRE((child = fork()) != -1); 1473 if (child == 0) { 1474 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1475 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1476 1477 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1478 FORKEE_ASSERT(raise(sigval) == 0); 1479 1480 FORKEE_ASSERT((child2 = fork()) != -1); 1481 1482 if (child2 == 0) 1483 _exit(exitval2); 1484 1485 FORKEE_REQUIRE_SUCCESS 1486 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 1487 1488 forkee_status_exited(status, exitval2); 1489 1490 DPRINTF("Before exiting of the child process\n"); 1491 _exit(exitval); 1492 } 1493 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1494 1495 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1496 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1497 1498 validate_status_stopped(status, sigval); 1499 1500 DPRINTF("Set empty EVENT_MASK for the child %d\n", child); 1501 event.pe_set_event = 0; 1502 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 1503 1504 DPRINTF("Before resuming the child process where it left off and " 1505 "without signal to be sent\n"); 1506 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1507 1508 DPRINTF("Before calling %s() for the child - expected stopped " 1509 "SIGCHLD\n", TWAIT_FNAME); 1510 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1511 1512 validate_status_stopped(status, SIGCHLD); 1513 1514 DPRINTF("Before resuming the child process where it left off and " 1515 "without signal to be sent\n"); 1516 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1517 1518 DPRINTF("Before calling %s() for the child - expected exited\n", 1519 TWAIT_FNAME); 1520 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1521 1522 validate_status_exited(status, exitval); 1523 1524 DPRINTF("Before calling %s() for the child - expected no process\n", 1525 TWAIT_FNAME); 1526 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1527 } 1528 1529 #if defined(TWAIT_HAVE_PID) 1530 ATF_TC(vfork1); 1531 ATF_TC_HEAD(vfork1, tc) 1532 { 1533 atf_tc_set_md_var(tc, "descr", 1534 "Verify that vfork(2) is intercepted by ptrace(2) with EVENT_MASK " 1535 "set to PTRACE_VFORK"); 1536 } 1537 1538 ATF_TC_BODY(vfork1, tc) 1539 { 1540 const int exitval = 5; 1541 const int exitval2 = 15; 1542 const int sigval = SIGSTOP; 1543 pid_t child, child2, wpid; 1544 #if defined(TWAIT_HAVE_STATUS) 1545 int status; 1546 #endif 1547 ptrace_state_t state; 1548 const int slen = sizeof(state); 1549 ptrace_event_t event; 1550 const int elen = sizeof(event); 1551 1552 atf_tc_expect_fail("PR kern/51630"); 1553 1554 DPRINTF("Before forking process PID=%d\n", getpid()); 1555 SYSCALL_REQUIRE((child = fork()) != -1); 1556 if (child == 0) { 1557 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1558 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1559 1560 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1561 FORKEE_ASSERT(raise(sigval) == 0); 1562 1563 FORKEE_ASSERT((child2 = vfork()) != -1); 1564 1565 if (child2 == 0) 1566 _exit(exitval2); 1567 1568 FORKEE_REQUIRE_SUCCESS 1569 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 1570 1571 forkee_status_exited(status, exitval2); 1572 1573 DPRINTF("Before exiting of the child process\n"); 1574 _exit(exitval); 1575 } 1576 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1577 1578 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1579 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1580 1581 validate_status_stopped(status, sigval); 1582 1583 DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child); 1584 event.pe_set_event = PTRACE_VFORK; 1585 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP); 1586 1587 DPRINTF("Before resuming the child process where it left off and " 1588 "without signal to be sent\n"); 1589 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1590 1591 DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child); 1592 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1593 1594 validate_status_stopped(status, SIGTRAP); 1595 1596 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 1597 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK); 1598 1599 child2 = state.pe_other_pid; 1600 DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2); 1601 1602 DPRINTF("Before calling %s() for the forkee %d of the child %d\n", 1603 TWAIT_FNAME, child2, child); 1604 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 1605 child2); 1606 1607 validate_status_stopped(status, SIGTRAP); 1608 1609 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1); 1610 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK); 1611 ATF_REQUIRE_EQ(state.pe_other_pid, child); 1612 1613 DPRINTF("Before resuming the forkee process where it left off and " 1614 "without signal to be sent\n"); 1615 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1); 1616 1617 DPRINTF("Before resuming the child process where it left off and " 1618 "without signal to be sent\n"); 1619 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1620 1621 DPRINTF("Before calling %s() for the forkee - expected exited\n", 1622 TWAIT_FNAME); 1623 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 1624 child2); 1625 1626 validate_status_exited(status, exitval2); 1627 1628 DPRINTF("Before calling %s() for the forkee - expected no process\n", 1629 TWAIT_FNAME); 1630 TWAIT_REQUIRE_FAILURE(ECHILD, 1631 wpid = TWAIT_GENERIC(child2, &status, 0)); 1632 1633 DPRINTF("Before calling %s() for the child - expected stopped " 1634 "SIGCHLD\n", TWAIT_FNAME); 1635 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1636 1637 validate_status_stopped(status, SIGCHLD); 1638 1639 DPRINTF("Before resuming the child process where it left off and " 1640 "without signal to be sent\n"); 1641 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1642 1643 DPRINTF("Before calling %s() for the child - expected exited\n", 1644 TWAIT_FNAME); 1645 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1646 1647 validate_status_exited(status, exitval); 1648 1649 DPRINTF("Before calling %s() for the child - expected no process\n", 1650 TWAIT_FNAME); 1651 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1652 } 1653 #endif 1654 1655 ATF_TC(vfork2); 1656 ATF_TC_HEAD(vfork2, tc) 1657 { 1658 atf_tc_set_md_var(tc, "descr", 1659 "Verify that vfork(2) is not intercepted by ptrace(2) with empty " 1660 "EVENT_MASK"); 1661 } 1662 1663 ATF_TC_BODY(vfork2, tc) 1664 { 1665 const int exitval = 5; 1666 const int exitval2 = 15; 1667 const int sigval = SIGSTOP; 1668 pid_t child, child2, wpid; 1669 #if defined(TWAIT_HAVE_STATUS) 1670 int status; 1671 #endif 1672 ptrace_event_t event; 1673 const int elen = sizeof(event); 1674 1675 DPRINTF("Before forking process PID=%d\n", getpid()); 1676 SYSCALL_REQUIRE((child = fork()) != -1); 1677 if (child == 0) { 1678 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1679 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1680 1681 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1682 FORKEE_ASSERT(raise(sigval) == 0); 1683 1684 FORKEE_ASSERT((child2 = vfork()) != -1); 1685 1686 if (child2 == 0) 1687 _exit(exitval2); 1688 1689 FORKEE_REQUIRE_SUCCESS 1690 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 1691 1692 forkee_status_exited(status, exitval2); 1693 1694 DPRINTF("Before exiting of the child process\n"); 1695 _exit(exitval); 1696 } 1697 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1698 1699 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1700 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1701 1702 validate_status_stopped(status, sigval); 1703 1704 DPRINTF("Set empty EVENT_MASK for the child %d\n", child); 1705 event.pe_set_event = 0; 1706 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 1707 1708 DPRINTF("Before resuming the child process where it left off and " 1709 "without signal to be sent\n"); 1710 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1711 1712 DPRINTF("Before calling %s() for the child - expected stopped " 1713 "SIGCHLD\n", TWAIT_FNAME); 1714 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1715 1716 validate_status_stopped(status, SIGCHLD); 1717 1718 DPRINTF("Before resuming the child process where it left off and " 1719 "without signal to be sent\n"); 1720 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1721 1722 DPRINTF("Before calling %s() for the child - expected exited\n", 1723 TWAIT_FNAME); 1724 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1725 1726 validate_status_exited(status, exitval); 1727 1728 DPRINTF("Before calling %s() for the child - expected no process\n", 1729 TWAIT_FNAME); 1730 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1731 } 1732 1733 ATF_TC(vforkdone1); 1734 ATF_TC_HEAD(vforkdone1, tc) 1735 { 1736 atf_tc_set_md_var(tc, "descr", 1737 "Verify that vfork(2) is intercepted by ptrace(2) with EVENT_MASK " 1738 "set to PTRACE_VFORK_DONE"); 1739 } 1740 1741 ATF_TC_BODY(vforkdone1, tc) 1742 { 1743 const int exitval = 5; 1744 const int exitval2 = 15; 1745 const int sigval = SIGSTOP; 1746 pid_t child, child2, wpid; 1747 #if defined(TWAIT_HAVE_STATUS) 1748 int status; 1749 #endif 1750 ptrace_state_t state; 1751 const int slen = sizeof(state); 1752 ptrace_event_t event; 1753 const int elen = sizeof(event); 1754 1755 DPRINTF("Before forking process PID=%d\n", getpid()); 1756 SYSCALL_REQUIRE((child = fork()) != -1); 1757 if (child == 0) { 1758 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1759 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1760 1761 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1762 FORKEE_ASSERT(raise(sigval) == 0); 1763 1764 FORKEE_ASSERT((child2 = vfork()) != -1); 1765 1766 if (child2 == 0) 1767 _exit(exitval2); 1768 1769 FORKEE_REQUIRE_SUCCESS 1770 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 1771 1772 forkee_status_exited(status, exitval2); 1773 1774 DPRINTF("Before exiting of the child process\n"); 1775 _exit(exitval); 1776 } 1777 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1778 1779 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1780 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1781 1782 validate_status_stopped(status, sigval); 1783 1784 DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n", 1785 child); 1786 event.pe_set_event = PTRACE_VFORK_DONE; 1787 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 1788 1789 DPRINTF("Before resuming the child process where it left off and " 1790 "without signal to be sent\n"); 1791 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1792 1793 DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child); 1794 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1795 1796 validate_status_stopped(status, SIGTRAP); 1797 1798 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 1799 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE); 1800 1801 child2 = state.pe_other_pid; 1802 DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2); 1803 1804 DPRINTF("Before resuming the child process where it left off and " 1805 "without signal to be sent\n"); 1806 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1807 1808 DPRINTF("Before calling %s() for the child - expected stopped " 1809 "SIGCHLD\n", TWAIT_FNAME); 1810 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1811 1812 validate_status_stopped(status, SIGCHLD); 1813 1814 DPRINTF("Before resuming the child process where it left off and " 1815 "without signal to be sent\n"); 1816 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1817 1818 DPRINTF("Before calling %s() for the child - expected exited\n", 1819 TWAIT_FNAME); 1820 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1821 1822 validate_status_exited(status, exitval); 1823 1824 DPRINTF("Before calling %s() for the child - expected no process\n", 1825 TWAIT_FNAME); 1826 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1827 } 1828 1829 ATF_TC(vforkdone2); 1830 ATF_TC_HEAD(vforkdone2, tc) 1831 { 1832 atf_tc_set_md_var(tc, "descr", 1833 "Verify that vfork(2) is intercepted by ptrace(2) with EVENT_MASK " 1834 "set to PTRACE_FORK | PTRACE_VFORK_DONE"); 1835 } 1836 1837 ATF_TC_BODY(vforkdone2, tc) 1838 { 1839 const int exitval = 5; 1840 const int exitval2 = 15; 1841 const int sigval = SIGSTOP; 1842 pid_t child, child2, wpid; 1843 #if defined(TWAIT_HAVE_STATUS) 1844 int status; 1845 #endif 1846 ptrace_state_t state; 1847 const int slen = sizeof(state); 1848 ptrace_event_t event; 1849 const int elen = sizeof(event); 1850 1851 DPRINTF("Before forking process PID=%d\n", getpid()); 1852 SYSCALL_REQUIRE((child = fork()) != -1); 1853 if (child == 0) { 1854 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1855 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1856 1857 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1858 FORKEE_ASSERT(raise(sigval) == 0); 1859 1860 FORKEE_ASSERT((child2 = vfork()) != -1); 1861 1862 if (child2 == 0) 1863 _exit(exitval2); 1864 1865 FORKEE_REQUIRE_SUCCESS 1866 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 1867 1868 forkee_status_exited(status, exitval2); 1869 1870 DPRINTF("Before exiting of the child process\n"); 1871 _exit(exitval); 1872 } 1873 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1874 1875 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1876 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1877 1878 validate_status_stopped(status, sigval); 1879 1880 DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child); 1881 event.pe_set_event = PTRACE_FORK | PTRACE_VFORK_DONE; 1882 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 1883 1884 DPRINTF("Before resuming the child process where it left off and " 1885 "without signal to be sent\n"); 1886 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1887 1888 DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child); 1889 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1890 1891 validate_status_stopped(status, SIGTRAP); 1892 1893 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 1894 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE); 1895 1896 child2 = state.pe_other_pid; 1897 DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2); 1898 1899 DPRINTF("Before resuming the child process where it left off and " 1900 "without signal to be sent\n"); 1901 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1902 1903 DPRINTF("Before calling %s() for the child - expected stopped " 1904 "SIGCHLD\n", TWAIT_FNAME); 1905 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1906 1907 validate_status_stopped(status, SIGCHLD); 1908 1909 DPRINTF("Before resuming the child process where it left off and " 1910 "without signal to be sent\n"); 1911 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1912 1913 DPRINTF("Before calling %s() for the child - expected exited\n", 1914 TWAIT_FNAME); 1915 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1916 1917 validate_status_exited(status, exitval); 1918 1919 DPRINTF("Before calling %s() for the child - expected no process\n", 1920 TWAIT_FNAME); 1921 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1922 } 1923 1924 ATF_TC(io_read_d1); 1925 ATF_TC_HEAD(io_read_d1, tc) 1926 { 1927 atf_tc_set_md_var(tc, "descr", 1928 "Verify PT_IO with PIOD_READ_D and len = sizeof(uint8_t)"); 1929 } 1930 1931 ATF_TC_BODY(io_read_d1, tc) 1932 { 1933 const int exitval = 5; 1934 const int sigval = SIGSTOP; 1935 pid_t child, wpid; 1936 uint8_t lookup_me = 0; 1937 const uint8_t magic = 0xab; 1938 struct ptrace_io_desc io = { 1939 .piod_op = PIOD_READ_D, 1940 .piod_offs = &lookup_me, 1941 .piod_addr = &lookup_me, 1942 .piod_len = sizeof(lookup_me) 1943 }; 1944 #if defined(TWAIT_HAVE_STATUS) 1945 int status; 1946 #endif 1947 1948 DPRINTF("Before forking process PID=%d\n", getpid()); 1949 SYSCALL_REQUIRE((child = fork()) != -1); 1950 if (child == 0) { 1951 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 1952 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 1953 1954 lookup_me = magic; 1955 1956 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 1957 FORKEE_ASSERT(raise(sigval) == 0); 1958 1959 DPRINTF("Before exiting of the child process\n"); 1960 _exit(exitval); 1961 } 1962 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 1963 1964 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1965 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1966 1967 validate_status_stopped(status, sigval); 1968 1969 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 1970 child, getpid()); 1971 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 1972 1973 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 1974 "got value %" PRIx8 " != expected %" PRIx8, lookup_me, magic); 1975 1976 DPRINTF("Before resuming the child process where it left off and " 1977 "without signal to be sent\n"); 1978 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 1979 1980 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1981 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 1982 1983 validate_status_exited(status, exitval); 1984 1985 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 1986 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 1987 } 1988 1989 ATF_TC(io_read_d2); 1990 ATF_TC_HEAD(io_read_d2, tc) 1991 { 1992 atf_tc_set_md_var(tc, "descr", 1993 "Verify PT_IO with PIOD_READ_D and len = sizeof(uint16_t)"); 1994 } 1995 1996 ATF_TC_BODY(io_read_d2, tc) 1997 { 1998 const int exitval = 5; 1999 const int sigval = SIGSTOP; 2000 pid_t child, wpid; 2001 uint16_t lookup_me = 0; 2002 const uint16_t magic = 0x1234; 2003 struct ptrace_io_desc io = { 2004 .piod_op = PIOD_READ_D, 2005 .piod_offs = &lookup_me, 2006 .piod_addr = &lookup_me, 2007 .piod_len = sizeof(lookup_me) 2008 }; 2009 #if defined(TWAIT_HAVE_STATUS) 2010 int status; 2011 #endif 2012 2013 DPRINTF("Before forking process PID=%d\n", getpid()); 2014 SYSCALL_REQUIRE((child = fork()) != -1); 2015 if (child == 0) { 2016 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2017 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2018 2019 lookup_me = magic; 2020 2021 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2022 FORKEE_ASSERT(raise(sigval) == 0); 2023 2024 DPRINTF("Before exiting of the child process\n"); 2025 _exit(exitval); 2026 } 2027 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2028 2029 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2030 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2031 2032 validate_status_stopped(status, sigval); 2033 2034 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 2035 child, getpid()); 2036 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2037 2038 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 2039 "got value %" PRIx16 " != expected %" PRIx16, lookup_me, magic); 2040 2041 DPRINTF("Before resuming the child process where it left off and " 2042 "without signal to be sent\n"); 2043 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2044 2045 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2046 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2047 2048 validate_status_exited(status, exitval); 2049 2050 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2051 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2052 } 2053 2054 ATF_TC(io_read_d3); 2055 ATF_TC_HEAD(io_read_d3, tc) 2056 { 2057 atf_tc_set_md_var(tc, "descr", 2058 "Verify PT_IO with PIOD_READ_D and len = sizeof(uint32_t)"); 2059 } 2060 2061 ATF_TC_BODY(io_read_d3, tc) 2062 { 2063 const int exitval = 5; 2064 const int sigval = SIGSTOP; 2065 pid_t child, wpid; 2066 uint32_t lookup_me = 0; 2067 const uint32_t magic = 0x1234abcd; 2068 struct ptrace_io_desc io = { 2069 .piod_op = PIOD_READ_D, 2070 .piod_offs = &lookup_me, 2071 .piod_addr = &lookup_me, 2072 .piod_len = sizeof(lookup_me) 2073 }; 2074 #if defined(TWAIT_HAVE_STATUS) 2075 int status; 2076 #endif 2077 2078 DPRINTF("Before forking process PID=%d\n", getpid()); 2079 SYSCALL_REQUIRE((child = fork()) != -1); 2080 if (child == 0) { 2081 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2082 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2083 2084 lookup_me = magic; 2085 2086 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2087 FORKEE_ASSERT(raise(sigval) == 0); 2088 2089 DPRINTF("Before exiting of the child process\n"); 2090 _exit(exitval); 2091 } 2092 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2093 2094 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2095 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2096 2097 validate_status_stopped(status, sigval); 2098 2099 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 2100 child, getpid()); 2101 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2102 2103 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 2104 "got value %" PRIx32 " != expected %" PRIx32, lookup_me, magic); 2105 2106 DPRINTF("Before resuming the child process where it left off and " 2107 "without signal to be sent\n"); 2108 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2109 2110 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2111 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2112 2113 validate_status_exited(status, exitval); 2114 2115 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2116 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2117 } 2118 2119 ATF_TC(io_read_d4); 2120 ATF_TC_HEAD(io_read_d4, tc) 2121 { 2122 atf_tc_set_md_var(tc, "descr", 2123 "Verify PT_IO with PIOD_READ_D and len = sizeof(uint64_t)"); 2124 } 2125 2126 ATF_TC_BODY(io_read_d4, tc) 2127 { 2128 const int exitval = 5; 2129 const int sigval = SIGSTOP; 2130 pid_t child, wpid; 2131 uint64_t lookup_me = 0; 2132 const uint64_t magic = 0x1234abcd9876dcfa; 2133 struct ptrace_io_desc io = { 2134 .piod_op = PIOD_READ_D, 2135 .piod_offs = &lookup_me, 2136 .piod_addr = &lookup_me, 2137 .piod_len = sizeof(lookup_me) 2138 }; 2139 #if defined(TWAIT_HAVE_STATUS) 2140 int status; 2141 #endif 2142 2143 DPRINTF("Before forking process PID=%d\n", getpid()); 2144 SYSCALL_REQUIRE((child = fork()) != -1); 2145 if (child == 0) { 2146 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2147 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2148 2149 lookup_me = magic; 2150 2151 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2152 FORKEE_ASSERT(raise(sigval) == 0); 2153 2154 DPRINTF("Before exiting of the child process\n"); 2155 _exit(exitval); 2156 } 2157 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2158 2159 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2160 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2161 2162 validate_status_stopped(status, sigval); 2163 2164 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 2165 child, getpid()); 2166 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2167 2168 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 2169 "got value %" PRIx64 " != expected %" PRIx64, lookup_me, magic); 2170 2171 DPRINTF("Before resuming the child process where it left off and " 2172 "without signal to be sent\n"); 2173 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2174 2175 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2176 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2177 2178 validate_status_exited(status, exitval); 2179 2180 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2181 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2182 } 2183 2184 ATF_TC(io_write_d1); 2185 ATF_TC_HEAD(io_write_d1, tc) 2186 { 2187 atf_tc_set_md_var(tc, "descr", 2188 "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint8_t)"); 2189 } 2190 2191 ATF_TC_BODY(io_write_d1, tc) 2192 { 2193 const int exitval = 5; 2194 const int sigval = SIGSTOP; 2195 pid_t child, wpid; 2196 uint8_t lookup_me = 0; 2197 const uint8_t magic = 0xab; 2198 struct ptrace_io_desc io = { 2199 .piod_op = PIOD_WRITE_D, 2200 .piod_offs = &lookup_me, 2201 .piod_addr = &lookup_me, 2202 .piod_len = sizeof(lookup_me) 2203 }; 2204 #if defined(TWAIT_HAVE_STATUS) 2205 int status; 2206 #endif 2207 2208 DPRINTF("Before forking process PID=%d\n", getpid()); 2209 SYSCALL_REQUIRE((child = fork()) != -1); 2210 if (child == 0) { 2211 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2212 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2213 2214 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2215 FORKEE_ASSERT(raise(sigval) == 0); 2216 2217 FORKEE_ASSERT_EQ(lookup_me, magic); 2218 2219 DPRINTF("Before exiting of the child process\n"); 2220 _exit(exitval); 2221 } 2222 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2223 2224 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2225 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2226 2227 validate_status_stopped(status, sigval); 2228 2229 lookup_me = magic; 2230 2231 DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n", 2232 child, getpid()); 2233 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2234 2235 DPRINTF("Before resuming the child process where it left off and " 2236 "without signal to be sent\n"); 2237 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2238 2239 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2240 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2241 2242 validate_status_exited(status, exitval); 2243 2244 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2245 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2246 } 2247 2248 ATF_TC(io_write_d2); 2249 ATF_TC_HEAD(io_write_d2, tc) 2250 { 2251 atf_tc_set_md_var(tc, "descr", 2252 "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint16_t)"); 2253 } 2254 2255 ATF_TC_BODY(io_write_d2, tc) 2256 { 2257 const int exitval = 5; 2258 const int sigval = SIGSTOP; 2259 pid_t child, wpid; 2260 uint16_t lookup_me = 0; 2261 const uint16_t magic = 0xab12; 2262 struct ptrace_io_desc io = { 2263 .piod_op = PIOD_WRITE_D, 2264 .piod_offs = &lookup_me, 2265 .piod_addr = &lookup_me, 2266 .piod_len = sizeof(lookup_me) 2267 }; 2268 #if defined(TWAIT_HAVE_STATUS) 2269 int status; 2270 #endif 2271 2272 DPRINTF("Before forking process PID=%d\n", getpid()); 2273 SYSCALL_REQUIRE((child = fork()) != -1); 2274 if (child == 0) { 2275 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2276 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2277 2278 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2279 FORKEE_ASSERT(raise(sigval) == 0); 2280 2281 FORKEE_ASSERT_EQ(lookup_me, magic); 2282 2283 DPRINTF("Before exiting of the child process\n"); 2284 _exit(exitval); 2285 } 2286 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2287 2288 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2289 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2290 2291 validate_status_stopped(status, sigval); 2292 2293 lookup_me = magic; 2294 2295 DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n", 2296 child, getpid()); 2297 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2298 2299 DPRINTF("Before resuming the child process where it left off and " 2300 "without signal to be sent\n"); 2301 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2302 2303 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2304 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2305 2306 validate_status_exited(status, exitval); 2307 2308 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2309 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2310 } 2311 2312 ATF_TC(io_write_d3); 2313 ATF_TC_HEAD(io_write_d3, tc) 2314 { 2315 atf_tc_set_md_var(tc, "descr", 2316 "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint32_t)"); 2317 } 2318 2319 ATF_TC_BODY(io_write_d3, tc) 2320 { 2321 const int exitval = 5; 2322 const int sigval = SIGSTOP; 2323 pid_t child, wpid; 2324 uint32_t lookup_me = 0; 2325 const uint32_t magic = 0xab127643; 2326 struct ptrace_io_desc io = { 2327 .piod_op = PIOD_WRITE_D, 2328 .piod_offs = &lookup_me, 2329 .piod_addr = &lookup_me, 2330 .piod_len = sizeof(lookup_me) 2331 }; 2332 #if defined(TWAIT_HAVE_STATUS) 2333 int status; 2334 #endif 2335 2336 DPRINTF("Before forking process PID=%d\n", getpid()); 2337 SYSCALL_REQUIRE((child = fork()) != -1); 2338 if (child == 0) { 2339 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2340 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2341 2342 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2343 FORKEE_ASSERT(raise(sigval) == 0); 2344 2345 FORKEE_ASSERT_EQ(lookup_me, magic); 2346 2347 DPRINTF("Before exiting of the child process\n"); 2348 _exit(exitval); 2349 } 2350 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2351 2352 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2353 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2354 2355 validate_status_stopped(status, sigval); 2356 2357 lookup_me = magic; 2358 2359 DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n", 2360 child, getpid()); 2361 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2362 2363 DPRINTF("Before resuming the child process where it left off and " 2364 "without signal to be sent\n"); 2365 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2366 2367 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2368 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2369 2370 validate_status_exited(status, exitval); 2371 2372 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2373 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2374 } 2375 2376 ATF_TC(io_write_d4); 2377 ATF_TC_HEAD(io_write_d4, tc) 2378 { 2379 atf_tc_set_md_var(tc, "descr", 2380 "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint64_t)"); 2381 } 2382 2383 ATF_TC_BODY(io_write_d4, tc) 2384 { 2385 const int exitval = 5; 2386 const int sigval = SIGSTOP; 2387 pid_t child, wpid; 2388 uint64_t lookup_me = 0; 2389 const uint64_t magic = 0xab12764376490123; 2390 struct ptrace_io_desc io = { 2391 .piod_op = PIOD_WRITE_D, 2392 .piod_offs = &lookup_me, 2393 .piod_addr = &lookup_me, 2394 .piod_len = sizeof(lookup_me) 2395 }; 2396 #if defined(TWAIT_HAVE_STATUS) 2397 int status; 2398 #endif 2399 2400 DPRINTF("Before forking process PID=%d\n", getpid()); 2401 SYSCALL_REQUIRE((child = fork()) != -1); 2402 if (child == 0) { 2403 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2404 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2405 2406 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2407 FORKEE_ASSERT(raise(sigval) == 0); 2408 2409 FORKEE_ASSERT_EQ(lookup_me, magic); 2410 2411 DPRINTF("Before exiting of the child process\n"); 2412 _exit(exitval); 2413 } 2414 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2415 2416 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2417 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2418 2419 validate_status_stopped(status, sigval); 2420 2421 lookup_me = magic; 2422 2423 DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n", 2424 child, getpid()); 2425 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2426 2427 DPRINTF("Before resuming the child process where it left off and " 2428 "without signal to be sent\n"); 2429 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2430 2431 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2432 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2433 2434 validate_status_exited(status, exitval); 2435 2436 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2437 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2438 } 2439 2440 ATF_TC(io_read_auxv1); 2441 ATF_TC_HEAD(io_read_auxv1, tc) 2442 { 2443 atf_tc_set_md_var(tc, "descr", 2444 "Verify PT_READ_AUXV called for tracee"); 2445 } 2446 2447 ATF_TC_BODY(io_read_auxv1, tc) 2448 { 2449 const int exitval = 5; 2450 const int sigval = SIGSTOP; 2451 pid_t child, wpid; 2452 #if defined(TWAIT_HAVE_STATUS) 2453 int status; 2454 #endif 2455 AuxInfo ai[100], *aip; 2456 struct ptrace_io_desc io = { 2457 .piod_op = PIOD_READ_AUXV, 2458 .piod_offs = 0, 2459 .piod_addr = ai, 2460 .piod_len = sizeof(ai) 2461 }; 2462 2463 DPRINTF("Before forking process PID=%d\n", getpid()); 2464 SYSCALL_REQUIRE((child = fork()) != -1); 2465 if (child == 0) { 2466 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2467 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2468 2469 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2470 FORKEE_ASSERT(raise(sigval) == 0); 2471 2472 DPRINTF("Before exiting of the child process\n"); 2473 _exit(exitval); 2474 } 2475 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2476 2477 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2478 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2479 2480 validate_status_stopped(status, sigval); 2481 2482 DPRINTF("Read new AUXV from tracee (PID=%d) by tracer (PID=%d)\n", 2483 child, getpid()); 2484 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 2485 2486 DPRINTF("Asserting that AUXV length (%zu) is > 0\n", io.piod_len); 2487 ATF_REQUIRE(io.piod_len > 0); 2488 2489 for (aip = ai; aip->a_type != AT_NULL; aip++) 2490 DPRINTF("a_type=%#llx a_v=%#llx\n", 2491 (long long int)aip->a_type, (long long int)aip->a_v); 2492 2493 DPRINTF("Before resuming the child process where it left off and " 2494 "without signal to be sent\n"); 2495 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2496 2497 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2498 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2499 2500 validate_status_exited(status, exitval); 2501 2502 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2503 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2504 } 2505 2506 ATF_TC(read_d1); 2507 ATF_TC_HEAD(read_d1, tc) 2508 { 2509 atf_tc_set_md_var(tc, "descr", 2510 "Verify PT_READ_D called once"); 2511 } 2512 2513 ATF_TC_BODY(read_d1, tc) 2514 { 2515 const int exitval = 5; 2516 const int sigval = SIGSTOP; 2517 pid_t child, wpid; 2518 int lookup_me = 0; 2519 const int magic = (int)random(); 2520 #if defined(TWAIT_HAVE_STATUS) 2521 int status; 2522 #endif 2523 2524 DPRINTF("Before forking process PID=%d\n", getpid()); 2525 SYSCALL_REQUIRE((child = fork()) != -1); 2526 if (child == 0) { 2527 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2528 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2529 2530 lookup_me = magic; 2531 2532 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2533 FORKEE_ASSERT(raise(sigval) == 0); 2534 2535 DPRINTF("Before exiting of the child process\n"); 2536 _exit(exitval); 2537 } 2538 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2539 2540 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2541 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2542 2543 validate_status_stopped(status, sigval); 2544 2545 DPRINTF("Read new lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 2546 child, getpid()); 2547 errno = 0; 2548 lookup_me = ptrace(PT_READ_D, child, &lookup_me, 0); 2549 SYSCALL_REQUIRE_ERRNO(errno, 0); 2550 2551 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 2552 "got value %#x != expected %#x", lookup_me, magic); 2553 2554 DPRINTF("Before resuming the child process where it left off and " 2555 "without signal to be sent\n"); 2556 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2557 2558 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2559 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2560 2561 validate_status_exited(status, exitval); 2562 2563 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2564 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2565 } 2566 2567 ATF_TC(read_d2); 2568 ATF_TC_HEAD(read_d2, tc) 2569 { 2570 atf_tc_set_md_var(tc, "descr", 2571 "Verify PT_READ_D called twice"); 2572 } 2573 2574 ATF_TC_BODY(read_d2, tc) 2575 { 2576 const int exitval = 5; 2577 const int sigval = SIGSTOP; 2578 pid_t child, wpid; 2579 int lookup_me1 = 0; 2580 int lookup_me2 = 0; 2581 const int magic1 = (int)random(); 2582 const int magic2 = (int)random(); 2583 #if defined(TWAIT_HAVE_STATUS) 2584 int status; 2585 #endif 2586 2587 DPRINTF("Before forking process PID=%d\n", getpid()); 2588 SYSCALL_REQUIRE((child = fork()) != -1); 2589 if (child == 0) { 2590 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2591 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2592 2593 lookup_me1 = magic1; 2594 lookup_me2 = magic2; 2595 2596 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2597 FORKEE_ASSERT(raise(sigval) == 0); 2598 2599 DPRINTF("Before exiting of the child process\n"); 2600 _exit(exitval); 2601 } 2602 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2603 2604 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2605 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2606 2607 validate_status_stopped(status, sigval); 2608 2609 DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n", 2610 child, getpid()); 2611 errno = 0; 2612 lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0); 2613 SYSCALL_REQUIRE_ERRNO(errno, 0); 2614 2615 ATF_REQUIRE_EQ_MSG(lookup_me1, magic1, 2616 "got value %#x != expected %#x", lookup_me1, magic1); 2617 2618 DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n", 2619 child, getpid()); 2620 errno = 0; 2621 lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0); 2622 SYSCALL_REQUIRE_ERRNO(errno, 0); 2623 2624 ATF_REQUIRE_EQ_MSG(lookup_me2, magic2, 2625 "got value %#x != expected %#x", lookup_me2, magic2); 2626 2627 DPRINTF("Before resuming the child process where it left off and " 2628 "without signal to be sent\n"); 2629 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2630 2631 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2632 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2633 2634 validate_status_exited(status, exitval); 2635 2636 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2637 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2638 } 2639 2640 ATF_TC(read_d3); 2641 ATF_TC_HEAD(read_d3, tc) 2642 { 2643 atf_tc_set_md_var(tc, "descr", 2644 "Verify PT_READ_D called three times"); 2645 } 2646 2647 ATF_TC_BODY(read_d3, tc) 2648 { 2649 const int exitval = 5; 2650 const int sigval = SIGSTOP; 2651 pid_t child, wpid; 2652 int lookup_me1 = 0; 2653 int lookup_me2 = 0; 2654 int lookup_me3 = 0; 2655 const int magic1 = (int)random(); 2656 const int magic2 = (int)random(); 2657 const int magic3 = (int)random(); 2658 #if defined(TWAIT_HAVE_STATUS) 2659 int status; 2660 #endif 2661 2662 DPRINTF("Before forking process PID=%d\n", getpid()); 2663 SYSCALL_REQUIRE((child = fork()) != -1); 2664 if (child == 0) { 2665 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2666 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2667 2668 lookup_me1 = magic1; 2669 lookup_me2 = magic2; 2670 lookup_me3 = magic3; 2671 2672 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2673 FORKEE_ASSERT(raise(sigval) == 0); 2674 2675 DPRINTF("Before exiting of the child process\n"); 2676 _exit(exitval); 2677 } 2678 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2679 2680 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2681 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2682 2683 validate_status_stopped(status, sigval); 2684 2685 DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n", 2686 child, getpid()); 2687 errno = 0; 2688 lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0); 2689 SYSCALL_REQUIRE_ERRNO(errno, 0); 2690 2691 ATF_REQUIRE_EQ_MSG(lookup_me1, magic1, 2692 "got value %#x != expected %#x", lookup_me1, magic1); 2693 2694 DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n", 2695 child, getpid()); 2696 errno = 0; 2697 lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0); 2698 SYSCALL_REQUIRE_ERRNO(errno, 0); 2699 2700 ATF_REQUIRE_EQ_MSG(lookup_me2, magic2, 2701 "got value %#x != expected %#x", lookup_me2, magic2); 2702 2703 DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n", 2704 child, getpid()); 2705 errno = 0; 2706 lookup_me3 = ptrace(PT_READ_D, child, &lookup_me3, 0); 2707 SYSCALL_REQUIRE_ERRNO(errno, 0); 2708 2709 ATF_REQUIRE_EQ_MSG(lookup_me3, magic3, 2710 "got value %#x != expected %#x", lookup_me3, magic3); 2711 2712 DPRINTF("Before resuming the child process where it left off and " 2713 "without signal to be sent\n"); 2714 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2715 2716 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2717 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2718 2719 validate_status_exited(status, exitval); 2720 2721 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2722 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2723 } 2724 2725 ATF_TC(read_d4); 2726 ATF_TC_HEAD(read_d4, tc) 2727 { 2728 atf_tc_set_md_var(tc, "descr", 2729 "Verify PT_READ_D called four times"); 2730 } 2731 2732 ATF_TC_BODY(read_d4, tc) 2733 { 2734 const int exitval = 5; 2735 const int sigval = SIGSTOP; 2736 pid_t child, wpid; 2737 int lookup_me1 = 0; 2738 int lookup_me2 = 0; 2739 int lookup_me3 = 0; 2740 int lookup_me4 = 0; 2741 const int magic1 = (int)random(); 2742 const int magic2 = (int)random(); 2743 const int magic3 = (int)random(); 2744 const int magic4 = (int)random(); 2745 #if defined(TWAIT_HAVE_STATUS) 2746 int status; 2747 #endif 2748 2749 DPRINTF("Before forking process PID=%d\n", getpid()); 2750 SYSCALL_REQUIRE((child = fork()) != -1); 2751 if (child == 0) { 2752 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2753 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2754 2755 lookup_me1 = magic1; 2756 lookup_me2 = magic2; 2757 lookup_me3 = magic3; 2758 lookup_me4 = magic4; 2759 2760 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2761 FORKEE_ASSERT(raise(sigval) == 0); 2762 2763 DPRINTF("Before exiting of the child process\n"); 2764 _exit(exitval); 2765 } 2766 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2767 2768 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2769 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2770 2771 validate_status_stopped(status, sigval); 2772 2773 DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n", 2774 child, getpid()); 2775 errno = 0; 2776 lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0); 2777 SYSCALL_REQUIRE_ERRNO(errno, 0); 2778 2779 ATF_REQUIRE_EQ_MSG(lookup_me1, magic1, 2780 "got value %#x != expected %#x", lookup_me1, magic1); 2781 2782 DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n", 2783 child, getpid()); 2784 errno = 0; 2785 lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0); 2786 SYSCALL_REQUIRE_ERRNO(errno, 0); 2787 2788 ATF_REQUIRE_EQ_MSG(lookup_me2, magic2, 2789 "got value %#x != expected %#x", lookup_me2, magic2); 2790 2791 DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n", 2792 child, getpid()); 2793 errno = 0; 2794 lookup_me3 = ptrace(PT_READ_D, child, &lookup_me3, 0); 2795 SYSCALL_REQUIRE_ERRNO(errno, 0); 2796 2797 ATF_REQUIRE_EQ_MSG(lookup_me3, magic3, 2798 "got value %#x != expected %#x", lookup_me3, magic3); 2799 2800 DPRINTF("Read new lookup_me4 from tracee (PID=%d) by tracer (PID=%d)\n", 2801 child, getpid()); 2802 errno = 0; 2803 lookup_me4 = ptrace(PT_READ_D, child, &lookup_me4, 0); 2804 SYSCALL_REQUIRE_ERRNO(errno, 0); 2805 2806 ATF_REQUIRE_EQ_MSG(lookup_me4, magic4, 2807 "got value %#x != expected %#x", lookup_me4, magic4); 2808 2809 DPRINTF("Before resuming the child process where it left off and " 2810 "without signal to be sent\n"); 2811 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2812 2813 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2814 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2815 2816 validate_status_exited(status, exitval); 2817 2818 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2819 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2820 } 2821 2822 ATF_TC(write_d1); 2823 ATF_TC_HEAD(write_d1, tc) 2824 { 2825 atf_tc_set_md_var(tc, "descr", 2826 "Verify PT_WRITE_D called once"); 2827 } 2828 2829 ATF_TC_BODY(write_d1, tc) 2830 { 2831 const int exitval = 5; 2832 const int sigval = SIGSTOP; 2833 pid_t child, wpid; 2834 int lookup_me = 0; 2835 const int magic = (int)random(); 2836 #if defined(TWAIT_HAVE_STATUS) 2837 int status; 2838 #endif 2839 2840 DPRINTF("Before forking process PID=%d\n", getpid()); 2841 SYSCALL_REQUIRE((child = fork()) != -1); 2842 if (child == 0) { 2843 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2844 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2845 2846 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2847 FORKEE_ASSERT(raise(sigval) == 0); 2848 2849 FORKEE_ASSERT_EQ(lookup_me, magic); 2850 2851 DPRINTF("Before exiting of the child process\n"); 2852 _exit(exitval); 2853 } 2854 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2855 2856 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2857 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2858 2859 validate_status_stopped(status, sigval); 2860 2861 DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n", 2862 child, getpid()); 2863 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me, magic) != -1); 2864 2865 DPRINTF("Before resuming the child process where it left off and " 2866 "without signal to be sent\n"); 2867 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2868 2869 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2870 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2871 2872 validate_status_exited(status, exitval); 2873 2874 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2875 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2876 } 2877 2878 ATF_TC(write_d2); 2879 ATF_TC_HEAD(write_d2, tc) 2880 { 2881 atf_tc_set_md_var(tc, "descr", 2882 "Verify PT_WRITE_D called twice"); 2883 } 2884 2885 ATF_TC_BODY(write_d2, tc) 2886 { 2887 const int exitval = 5; 2888 const int sigval = SIGSTOP; 2889 pid_t child, wpid; 2890 int lookup_me1 = 0; 2891 int lookup_me2 = 0; 2892 const int magic1 = (int)random(); 2893 const int magic2 = (int)random(); 2894 #if defined(TWAIT_HAVE_STATUS) 2895 int status; 2896 #endif 2897 2898 DPRINTF("Before forking process PID=%d\n", getpid()); 2899 SYSCALL_REQUIRE((child = fork()) != -1); 2900 if (child == 0) { 2901 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2902 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2903 2904 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2905 FORKEE_ASSERT(raise(sigval) == 0); 2906 2907 FORKEE_ASSERT_EQ(lookup_me1, magic1); 2908 FORKEE_ASSERT_EQ(lookup_me2, magic2); 2909 2910 DPRINTF("Before exiting of the child process\n"); 2911 _exit(exitval); 2912 } 2913 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2914 2915 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2916 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2917 2918 validate_status_stopped(status, sigval); 2919 2920 DPRINTF("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n", 2921 child, getpid()); 2922 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1); 2923 2924 DPRINTF("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n", 2925 child, getpid()); 2926 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1); 2927 2928 DPRINTF("Before resuming the child process where it left off and " 2929 "without signal to be sent\n"); 2930 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 2931 2932 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2933 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2934 2935 validate_status_exited(status, exitval); 2936 2937 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2938 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 2939 } 2940 2941 ATF_TC(write_d3); 2942 ATF_TC_HEAD(write_d3, tc) 2943 { 2944 atf_tc_set_md_var(tc, "descr", 2945 "Verify PT_WRITE_D called three times"); 2946 } 2947 2948 ATF_TC_BODY(write_d3, tc) 2949 { 2950 const int exitval = 5; 2951 const int sigval = SIGSTOP; 2952 pid_t child, wpid; 2953 int lookup_me1 = 0; 2954 int lookup_me2 = 0; 2955 int lookup_me3 = 0; 2956 const int magic1 = (int)random(); 2957 const int magic2 = (int)random(); 2958 const int magic3 = (int)random(); 2959 #if defined(TWAIT_HAVE_STATUS) 2960 int status; 2961 #endif 2962 2963 DPRINTF("Before forking process PID=%d\n", getpid()); 2964 SYSCALL_REQUIRE((child = fork()) != -1); 2965 if (child == 0) { 2966 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 2967 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 2968 2969 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 2970 FORKEE_ASSERT(raise(sigval) == 0); 2971 2972 FORKEE_ASSERT_EQ(lookup_me1, magic1); 2973 FORKEE_ASSERT_EQ(lookup_me2, magic2); 2974 FORKEE_ASSERT_EQ(lookup_me3, magic3); 2975 2976 DPRINTF("Before exiting of the child process\n"); 2977 _exit(exitval); 2978 } 2979 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 2980 2981 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 2982 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 2983 2984 validate_status_stopped(status, sigval); 2985 2986 DPRINTF("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n", 2987 child, getpid()); 2988 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1); 2989 2990 DPRINTF("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n", 2991 child, getpid()); 2992 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1); 2993 2994 DPRINTF("Write new lookup_me3 to tracee (PID=%d) from tracer (PID=%d)\n", 2995 child, getpid()); 2996 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me3, magic3) != -1); 2997 2998 DPRINTF("Before resuming the child process where it left off and " 2999 "without signal to be sent\n"); 3000 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3001 3002 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3003 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3004 3005 validate_status_exited(status, exitval); 3006 3007 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3008 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3009 } 3010 3011 ATF_TC(write_d4); 3012 ATF_TC_HEAD(write_d4, tc) 3013 { 3014 atf_tc_set_md_var(tc, "descr", 3015 "Verify PT_WRITE_D called four times"); 3016 } 3017 3018 ATF_TC_BODY(write_d4, tc) 3019 { 3020 const int exitval = 5; 3021 const int sigval = SIGSTOP; 3022 pid_t child, wpid; 3023 int lookup_me1 = 0; 3024 int lookup_me2 = 0; 3025 int lookup_me3 = 0; 3026 int lookup_me4 = 0; 3027 const int magic1 = (int)random(); 3028 const int magic2 = (int)random(); 3029 const int magic3 = (int)random(); 3030 const int magic4 = (int)random(); 3031 #if defined(TWAIT_HAVE_STATUS) 3032 int status; 3033 #endif 3034 3035 DPRINTF("Before forking process PID=%d\n", getpid()); 3036 SYSCALL_REQUIRE((child = fork()) != -1); 3037 if (child == 0) { 3038 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3039 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3040 3041 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3042 FORKEE_ASSERT(raise(sigval) == 0); 3043 3044 FORKEE_ASSERT_EQ(lookup_me1, magic1); 3045 FORKEE_ASSERT_EQ(lookup_me2, magic2); 3046 FORKEE_ASSERT_EQ(lookup_me3, magic3); 3047 FORKEE_ASSERT_EQ(lookup_me4, magic4); 3048 3049 DPRINTF("Before exiting of the child process\n"); 3050 _exit(exitval); 3051 } 3052 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3053 3054 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3055 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3056 3057 validate_status_stopped(status, sigval); 3058 3059 DPRINTF("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n", 3060 child, getpid()); 3061 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1); 3062 3063 DPRINTF("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n", 3064 child, getpid()); 3065 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1); 3066 3067 DPRINTF("Write new lookup_me3 to tracee (PID=%d) from tracer (PID=%d)\n", 3068 child, getpid()); 3069 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me3, magic3) != -1); 3070 3071 DPRINTF("Write new lookup_me4 to tracee (PID=%d) from tracer (PID=%d)\n", 3072 child, getpid()); 3073 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me4, magic4) != -1); 3074 3075 DPRINTF("Before resuming the child process where it left off and " 3076 "without signal to be sent\n"); 3077 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3078 3079 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3080 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3081 3082 validate_status_exited(status, exitval); 3083 3084 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3085 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3086 } 3087 3088 ATF_TC(io_read_d_write_d_handshake1); 3089 ATF_TC_HEAD(io_read_d_write_d_handshake1, tc) 3090 { 3091 atf_tc_set_md_var(tc, "descr", 3092 "Verify PT_IO with PIOD_READ_D and PIOD_WRITE_D handshake"); 3093 } 3094 3095 ATF_TC_BODY(io_read_d_write_d_handshake1, tc) 3096 { 3097 const int exitval = 5; 3098 const int sigval = SIGSTOP; 3099 pid_t child, wpid; 3100 uint8_t lookup_me_fromtracee = 0; 3101 const uint8_t magic_fromtracee = (uint8_t)random(); 3102 uint8_t lookup_me_totracee = 0; 3103 const uint8_t magic_totracee = (uint8_t)random(); 3104 struct ptrace_io_desc io_fromtracee = { 3105 .piod_op = PIOD_READ_D, 3106 .piod_offs = &lookup_me_fromtracee, 3107 .piod_addr = &lookup_me_fromtracee, 3108 .piod_len = sizeof(lookup_me_fromtracee) 3109 }; 3110 struct ptrace_io_desc io_totracee = { 3111 .piod_op = PIOD_WRITE_D, 3112 .piod_offs = &lookup_me_totracee, 3113 .piod_addr = &lookup_me_totracee, 3114 .piod_len = sizeof(lookup_me_totracee) 3115 }; 3116 #if defined(TWAIT_HAVE_STATUS) 3117 int status; 3118 #endif 3119 3120 DPRINTF("Before forking process PID=%d\n", getpid()); 3121 SYSCALL_REQUIRE((child = fork()) != -1); 3122 if (child == 0) { 3123 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3124 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3125 3126 lookup_me_fromtracee = magic_fromtracee; 3127 3128 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3129 FORKEE_ASSERT(raise(sigval) == 0); 3130 3131 FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee); 3132 3133 DPRINTF("Before exiting of the child process\n"); 3134 _exit(exitval); 3135 } 3136 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3137 3138 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3139 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3140 3141 validate_status_stopped(status, sigval); 3142 3143 DPRINTF("Read lookup_me_fromtracee PID=%d by tracer (PID=%d)\n", 3144 child, getpid()); 3145 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_fromtracee, 0) != -1); 3146 3147 ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee, 3148 "got value %" PRIx8 " != expected %" PRIx8, lookup_me_fromtracee, 3149 magic_fromtracee); 3150 3151 lookup_me_totracee = magic_totracee; 3152 3153 DPRINTF("Write lookup_me_totracee to PID=%d by tracer (PID=%d)\n", 3154 child, getpid()); 3155 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_totracee, 0) != -1); 3156 3157 ATF_REQUIRE_EQ_MSG(lookup_me_totracee, magic_totracee, 3158 "got value %" PRIx8 " != expected %" PRIx8, lookup_me_totracee, 3159 magic_totracee); 3160 3161 DPRINTF("Before resuming the child process where it left off and " 3162 "without signal to be sent\n"); 3163 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3164 3165 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3166 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3167 3168 validate_status_exited(status, exitval); 3169 3170 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3171 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3172 } 3173 3174 ATF_TC(io_read_d_write_d_handshake2); 3175 ATF_TC_HEAD(io_read_d_write_d_handshake2, tc) 3176 { 3177 atf_tc_set_md_var(tc, "descr", 3178 "Verify PT_IO with PIOD_WRITE_D and PIOD_READ_D handshake"); 3179 } 3180 3181 ATF_TC_BODY(io_read_d_write_d_handshake2, tc) 3182 { 3183 const int exitval = 5; 3184 const int sigval = SIGSTOP; 3185 pid_t child, wpid; 3186 uint8_t lookup_me_fromtracee = 0; 3187 const uint8_t magic_fromtracee = (uint8_t)random(); 3188 uint8_t lookup_me_totracee = 0; 3189 const uint8_t magic_totracee = (uint8_t)random(); 3190 struct ptrace_io_desc io_fromtracee = { 3191 .piod_op = PIOD_READ_D, 3192 .piod_offs = &lookup_me_fromtracee, 3193 .piod_addr = &lookup_me_fromtracee, 3194 .piod_len = sizeof(lookup_me_fromtracee) 3195 }; 3196 struct ptrace_io_desc io_totracee = { 3197 .piod_op = PIOD_WRITE_D, 3198 .piod_offs = &lookup_me_totracee, 3199 .piod_addr = &lookup_me_totracee, 3200 .piod_len = sizeof(lookup_me_totracee) 3201 }; 3202 #if defined(TWAIT_HAVE_STATUS) 3203 int status; 3204 #endif 3205 3206 DPRINTF("Before forking process PID=%d\n", getpid()); 3207 SYSCALL_REQUIRE((child = fork()) != -1); 3208 if (child == 0) { 3209 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3210 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3211 3212 lookup_me_fromtracee = magic_fromtracee; 3213 3214 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3215 FORKEE_ASSERT(raise(sigval) == 0); 3216 3217 FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee); 3218 3219 DPRINTF("Before exiting of the child process\n"); 3220 _exit(exitval); 3221 } 3222 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3223 3224 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3225 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3226 3227 validate_status_stopped(status, sigval); 3228 3229 lookup_me_totracee = magic_totracee; 3230 3231 DPRINTF("Write lookup_me_totracee to PID=%d by tracer (PID=%d)\n", 3232 child, getpid()); 3233 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_totracee, 0) != -1); 3234 3235 ATF_REQUIRE_EQ_MSG(lookup_me_totracee, magic_totracee, 3236 "got value %" PRIx8 " != expected %" PRIx8, lookup_me_totracee, 3237 magic_totracee); 3238 3239 DPRINTF("Read lookup_me_fromtracee PID=%d by tracer (PID=%d)\n", 3240 child, getpid()); 3241 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_fromtracee, 0) != -1); 3242 3243 ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee, 3244 "got value %" PRIx8 " != expected %" PRIx8, lookup_me_fromtracee, 3245 magic_fromtracee); 3246 3247 DPRINTF("Before resuming the child process where it left off and " 3248 "without signal to be sent\n"); 3249 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3250 3251 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3252 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3253 3254 validate_status_exited(status, exitval); 3255 3256 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3257 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3258 } 3259 3260 ATF_TC(read_d_write_d_handshake1); 3261 ATF_TC_HEAD(read_d_write_d_handshake1, tc) 3262 { 3263 atf_tc_set_md_var(tc, "descr", 3264 "Verify PT_READ_D with PT_WRITE_D handshake"); 3265 } 3266 3267 ATF_TC_BODY(read_d_write_d_handshake1, tc) 3268 { 3269 const int exitval = 5; 3270 const int sigval = SIGSTOP; 3271 pid_t child, wpid; 3272 int lookup_me_fromtracee = 0; 3273 const int magic_fromtracee = (int)random(); 3274 int lookup_me_totracee = 0; 3275 const int magic_totracee = (int)random(); 3276 #if defined(TWAIT_HAVE_STATUS) 3277 int status; 3278 #endif 3279 3280 DPRINTF("Before forking process PID=%d\n", getpid()); 3281 SYSCALL_REQUIRE((child = fork()) != -1); 3282 if (child == 0) { 3283 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3284 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3285 3286 lookup_me_fromtracee = magic_fromtracee; 3287 3288 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3289 FORKEE_ASSERT(raise(sigval) == 0); 3290 3291 FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee); 3292 3293 DPRINTF("Before exiting of the child process\n"); 3294 _exit(exitval); 3295 } 3296 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3297 3298 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3299 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3300 3301 validate_status_stopped(status, sigval); 3302 3303 DPRINTF("Read new lookup_me_fromtracee PID=%d by tracer (PID=%d)\n", 3304 child, getpid()); 3305 errno = 0; 3306 lookup_me_fromtracee = 3307 ptrace(PT_READ_D, child, &lookup_me_fromtracee, 0); 3308 SYSCALL_REQUIRE_ERRNO(errno, 0); 3309 3310 ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee, 3311 "got value %#x != expected %#x", lookup_me_fromtracee, 3312 magic_fromtracee); 3313 3314 DPRINTF("Write new lookup_me_totracee to PID=%d from tracer (PID=%d)\n", 3315 child, getpid()); 3316 ATF_REQUIRE 3317 (ptrace(PT_WRITE_D, child, &lookup_me_totracee, magic_totracee) 3318 != -1); 3319 3320 DPRINTF("Before resuming the child process where it left off and " 3321 "without signal to be sent\n"); 3322 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3323 3324 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3325 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3326 3327 validate_status_exited(status, exitval); 3328 3329 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3330 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3331 } 3332 3333 ATF_TC(read_d_write_d_handshake2); 3334 ATF_TC_HEAD(read_d_write_d_handshake2, tc) 3335 { 3336 atf_tc_set_md_var(tc, "descr", 3337 "Verify PT_WRITE_D with PT_READ_D handshake"); 3338 } 3339 3340 ATF_TC_BODY(read_d_write_d_handshake2, tc) 3341 { 3342 const int exitval = 5; 3343 const int sigval = SIGSTOP; 3344 pid_t child, wpid; 3345 int lookup_me_fromtracee = 0; 3346 const int magic_fromtracee = (int)random(); 3347 int lookup_me_totracee = 0; 3348 const int magic_totracee = (int)random(); 3349 #if defined(TWAIT_HAVE_STATUS) 3350 int status; 3351 #endif 3352 3353 DPRINTF("Before forking process PID=%d\n", getpid()); 3354 SYSCALL_REQUIRE((child = fork()) != -1); 3355 if (child == 0) { 3356 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3357 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3358 3359 lookup_me_fromtracee = magic_fromtracee; 3360 3361 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3362 FORKEE_ASSERT(raise(sigval) == 0); 3363 3364 FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee); 3365 3366 DPRINTF("Before exiting of the child process\n"); 3367 _exit(exitval); 3368 } 3369 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3370 3371 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3372 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3373 3374 validate_status_stopped(status, sigval); 3375 3376 DPRINTF("Write new lookup_me_totracee to PID=%d from tracer (PID=%d)\n", 3377 child, getpid()); 3378 ATF_REQUIRE 3379 (ptrace(PT_WRITE_D, child, &lookup_me_totracee, magic_totracee) 3380 != -1); 3381 3382 DPRINTF("Read new lookup_me_fromtracee PID=%d by tracer (PID=%d)\n", 3383 child, getpid()); 3384 errno = 0; 3385 lookup_me_fromtracee = 3386 ptrace(PT_READ_D, child, &lookup_me_fromtracee, 0); 3387 SYSCALL_REQUIRE_ERRNO(errno, 0); 3388 3389 ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee, 3390 "got value %#x != expected %#x", lookup_me_fromtracee, 3391 magic_fromtracee); 3392 3393 DPRINTF("Before resuming the child process where it left off and " 3394 "without signal to be sent\n"); 3395 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3396 3397 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3398 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3399 3400 validate_status_exited(status, exitval); 3401 3402 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3403 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3404 } 3405 3406 /* These dummy functions are used to be copied with ptrace(2) calls */ 3407 static int __used 3408 dummy_fn1(int a, int b, int c, int d) 3409 { 3410 3411 a *= 1; 3412 b += 2; 3413 c -= 3; 3414 d /= 4; 3415 3416 return a + b * c - d; 3417 } 3418 3419 static int __used 3420 dummy_fn2(int a, int b, int c, int d) 3421 { 3422 3423 a *= 4; 3424 b += 3; 3425 c -= 2; 3426 d /= 1; 3427 3428 return a + b * c - d; 3429 } 3430 3431 static int __used 3432 dummy_fn3(int a, int b, int c, int d) 3433 { 3434 3435 a *= 10; 3436 b += 20; 3437 c -= 30; 3438 d /= 40; 3439 3440 return a + b * c - d; 3441 } 3442 3443 static int __used 3444 dummy_fn4(int a, int b, int c, int d) 3445 { 3446 3447 a *= 40; 3448 b += 30; 3449 c -= 20; 3450 d /= 10; 3451 3452 return a + b * c - d; 3453 } 3454 3455 ATF_TC(io_read_i1); 3456 ATF_TC_HEAD(io_read_i1, tc) 3457 { 3458 atf_tc_set_md_var(tc, "descr", 3459 "Verify PT_IO with PIOD_READ_I and len = sizeof(uint8_t)"); 3460 } 3461 3462 ATF_TC_BODY(io_read_i1, tc) 3463 { 3464 const int exitval = 5; 3465 const int sigval = SIGSTOP; 3466 pid_t child, wpid; 3467 uint8_t lookup_me = 0; 3468 uint8_t magic; 3469 memcpy(&magic, dummy_fn1, sizeof(magic)); 3470 struct ptrace_io_desc io = { 3471 .piod_op = PIOD_READ_I, 3472 .piod_offs = dummy_fn1, 3473 .piod_addr = &lookup_me, 3474 .piod_len = sizeof(lookup_me) 3475 }; 3476 #if defined(TWAIT_HAVE_STATUS) 3477 int status; 3478 #endif 3479 3480 DPRINTF("Before forking process PID=%d\n", getpid()); 3481 SYSCALL_REQUIRE((child = fork()) != -1); 3482 if (child == 0) { 3483 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3484 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3485 3486 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3487 FORKEE_ASSERT(raise(sigval) == 0); 3488 3489 DPRINTF("Before exiting of the child process\n"); 3490 _exit(exitval); 3491 } 3492 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3493 3494 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3495 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3496 3497 validate_status_stopped(status, sigval); 3498 3499 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 3500 child, getpid()); 3501 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 3502 3503 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 3504 "got value %" PRIx8 " != expected %" PRIx8, lookup_me, magic); 3505 3506 DPRINTF("Before resuming the child process where it left off and " 3507 "without signal to be sent\n"); 3508 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3509 3510 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3511 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3512 3513 validate_status_exited(status, exitval); 3514 3515 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3516 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3517 } 3518 3519 ATF_TC(io_read_i2); 3520 ATF_TC_HEAD(io_read_i2, tc) 3521 { 3522 atf_tc_set_md_var(tc, "descr", 3523 "Verify PT_IO with PIOD_READ_I and len = sizeof(uint16_t)"); 3524 } 3525 3526 ATF_TC_BODY(io_read_i2, tc) 3527 { 3528 const int exitval = 5; 3529 const int sigval = SIGSTOP; 3530 pid_t child, wpid; 3531 uint16_t lookup_me = 0; 3532 uint16_t magic; 3533 memcpy(&magic, dummy_fn1, sizeof(magic)); 3534 struct ptrace_io_desc io = { 3535 .piod_op = PIOD_READ_I, 3536 .piod_offs = dummy_fn1, 3537 .piod_addr = &lookup_me, 3538 .piod_len = sizeof(lookup_me) 3539 }; 3540 #if defined(TWAIT_HAVE_STATUS) 3541 int status; 3542 #endif 3543 3544 DPRINTF("Before forking process PID=%d\n", getpid()); 3545 SYSCALL_REQUIRE((child = fork()) != -1); 3546 if (child == 0) { 3547 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3548 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3549 3550 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3551 FORKEE_ASSERT(raise(sigval) == 0); 3552 3553 DPRINTF("Before exiting of the child process\n"); 3554 _exit(exitval); 3555 } 3556 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3557 3558 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3559 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3560 3561 validate_status_stopped(status, sigval); 3562 3563 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 3564 child, getpid()); 3565 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 3566 3567 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 3568 "got value %" PRIx16 " != expected %" PRIx16, lookup_me, magic); 3569 3570 DPRINTF("Before resuming the child process where it left off and " 3571 "without signal to be sent\n"); 3572 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3573 3574 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3575 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3576 3577 validate_status_exited(status, exitval); 3578 3579 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3580 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3581 } 3582 3583 ATF_TC(io_read_i3); 3584 ATF_TC_HEAD(io_read_i3, tc) 3585 { 3586 atf_tc_set_md_var(tc, "descr", 3587 "Verify PT_IO with PIOD_READ_I and len = sizeof(uint32_t)"); 3588 } 3589 3590 ATF_TC_BODY(io_read_i3, tc) 3591 { 3592 const int exitval = 5; 3593 const int sigval = SIGSTOP; 3594 pid_t child, wpid; 3595 uint32_t lookup_me = 0; 3596 uint32_t magic; 3597 memcpy(&magic, dummy_fn1, sizeof(magic)); 3598 struct ptrace_io_desc io = { 3599 .piod_op = PIOD_READ_I, 3600 .piod_offs = dummy_fn1, 3601 .piod_addr = &lookup_me, 3602 .piod_len = sizeof(lookup_me) 3603 }; 3604 #if defined(TWAIT_HAVE_STATUS) 3605 int status; 3606 #endif 3607 3608 DPRINTF("Before forking process PID=%d\n", getpid()); 3609 SYSCALL_REQUIRE((child = fork()) != -1); 3610 if (child == 0) { 3611 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3612 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3613 3614 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3615 FORKEE_ASSERT(raise(sigval) == 0); 3616 3617 DPRINTF("Before exiting of the child process\n"); 3618 _exit(exitval); 3619 } 3620 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3621 3622 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3623 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3624 3625 validate_status_stopped(status, sigval); 3626 3627 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 3628 child, getpid()); 3629 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 3630 3631 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 3632 "got value %" PRIx32 " != expected %" PRIx32, lookup_me, magic); 3633 3634 DPRINTF("Before resuming the child process where it left off and " 3635 "without signal to be sent\n"); 3636 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3637 3638 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3639 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3640 3641 validate_status_exited(status, exitval); 3642 3643 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3644 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3645 } 3646 3647 ATF_TC(io_read_i4); 3648 ATF_TC_HEAD(io_read_i4, tc) 3649 { 3650 atf_tc_set_md_var(tc, "descr", 3651 "Verify PT_IO with PIOD_READ_I and len = sizeof(uint64_t)"); 3652 } 3653 3654 ATF_TC_BODY(io_read_i4, tc) 3655 { 3656 const int exitval = 5; 3657 const int sigval = SIGSTOP; 3658 pid_t child, wpid; 3659 uint64_t lookup_me = 0; 3660 uint64_t magic; 3661 memcpy(&magic, dummy_fn1, sizeof(magic)); 3662 struct ptrace_io_desc io = { 3663 .piod_op = PIOD_READ_I, 3664 .piod_offs = dummy_fn1, 3665 .piod_addr = &lookup_me, 3666 .piod_len = sizeof(lookup_me) 3667 }; 3668 #if defined(TWAIT_HAVE_STATUS) 3669 int status; 3670 #endif 3671 3672 DPRINTF("Before forking process PID=%d\n", getpid()); 3673 SYSCALL_REQUIRE((child = fork()) != -1); 3674 if (child == 0) { 3675 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3676 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3677 3678 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3679 FORKEE_ASSERT(raise(sigval) == 0); 3680 3681 DPRINTF("Before exiting of the child process\n"); 3682 _exit(exitval); 3683 } 3684 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3685 3686 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3687 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3688 3689 validate_status_stopped(status, sigval); 3690 3691 DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 3692 child, getpid()); 3693 SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1); 3694 3695 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 3696 "got value %" PRIx64 " != expected %" PRIx64, lookup_me, magic); 3697 3698 DPRINTF("Before resuming the child process where it left off and " 3699 "without signal to be sent\n"); 3700 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3701 3702 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3703 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3704 3705 validate_status_exited(status, exitval); 3706 3707 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3708 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3709 } 3710 3711 ATF_TC(read_i1); 3712 ATF_TC_HEAD(read_i1, tc) 3713 { 3714 atf_tc_set_md_var(tc, "descr", 3715 "Verify PT_READ_I called once"); 3716 } 3717 3718 ATF_TC_BODY(read_i1, tc) 3719 { 3720 const int exitval = 5; 3721 const int sigval = SIGSTOP; 3722 pid_t child, wpid; 3723 int lookup_me = 0; 3724 int magic; 3725 memcpy(&magic, dummy_fn1, sizeof(magic)); 3726 #if defined(TWAIT_HAVE_STATUS) 3727 int status; 3728 #endif 3729 3730 DPRINTF("Before forking process PID=%d\n", getpid()); 3731 SYSCALL_REQUIRE((child = fork()) != -1); 3732 if (child == 0) { 3733 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3734 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3735 3736 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3737 FORKEE_ASSERT(raise(sigval) == 0); 3738 3739 DPRINTF("Before exiting of the child process\n"); 3740 _exit(exitval); 3741 } 3742 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3743 3744 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3745 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3746 3747 validate_status_stopped(status, sigval); 3748 3749 DPRINTF("Read new lookup_me from tracee (PID=%d) by tracer (PID=%d)\n", 3750 child, getpid()); 3751 errno = 0; 3752 lookup_me = ptrace(PT_READ_I, child, dummy_fn1, 0); 3753 SYSCALL_REQUIRE_ERRNO(errno, 0); 3754 3755 ATF_REQUIRE_EQ_MSG(lookup_me, magic, 3756 "got value %#x != expected %#x", lookup_me, magic); 3757 3758 DPRINTF("Before resuming the child process where it left off and " 3759 "without signal to be sent\n"); 3760 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3761 3762 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3763 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3764 3765 validate_status_exited(status, exitval); 3766 3767 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3768 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3769 } 3770 3771 ATF_TC(read_i2); 3772 ATF_TC_HEAD(read_i2, tc) 3773 { 3774 atf_tc_set_md_var(tc, "descr", 3775 "Verify PT_READ_I called twice"); 3776 } 3777 3778 ATF_TC_BODY(read_i2, tc) 3779 { 3780 const int exitval = 5; 3781 const int sigval = SIGSTOP; 3782 pid_t child, wpid; 3783 int lookup_me1 = 0; 3784 int lookup_me2 = 0; 3785 int magic1; 3786 int magic2; 3787 memcpy(&magic1, dummy_fn1, sizeof(magic1)); 3788 memcpy(&magic2, dummy_fn2, sizeof(magic2)); 3789 #if defined(TWAIT_HAVE_STATUS) 3790 int status; 3791 #endif 3792 3793 DPRINTF("Before forking process PID=%d\n", getpid()); 3794 SYSCALL_REQUIRE((child = fork()) != -1); 3795 if (child == 0) { 3796 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3797 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3798 3799 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3800 FORKEE_ASSERT(raise(sigval) == 0); 3801 3802 DPRINTF("Before exiting of the child process\n"); 3803 _exit(exitval); 3804 } 3805 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3806 3807 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3808 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3809 3810 validate_status_stopped(status, sigval); 3811 3812 DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n", 3813 child, getpid()); 3814 errno = 0; 3815 lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0); 3816 SYSCALL_REQUIRE_ERRNO(errno, 0); 3817 3818 ATF_REQUIRE_EQ_MSG(lookup_me1, magic1, 3819 "got value %#x != expected %#x", lookup_me1, magic1); 3820 3821 DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n", 3822 child, getpid()); 3823 errno = 0; 3824 lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0); 3825 SYSCALL_REQUIRE_ERRNO(errno, 0); 3826 3827 ATF_REQUIRE_EQ_MSG(lookup_me2, magic2, 3828 "got value %#x != expected %#x", lookup_me2, magic2); 3829 3830 DPRINTF("Before resuming the child process where it left off and " 3831 "without signal to be sent\n"); 3832 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3833 3834 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3835 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3836 3837 validate_status_exited(status, exitval); 3838 3839 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3840 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3841 } 3842 3843 ATF_TC(read_i3); 3844 ATF_TC_HEAD(read_i3, tc) 3845 { 3846 atf_tc_set_md_var(tc, "descr", 3847 "Verify PT_READ_I called three times"); 3848 } 3849 3850 ATF_TC_BODY(read_i3, tc) 3851 { 3852 const int exitval = 5; 3853 const int sigval = SIGSTOP; 3854 pid_t child, wpid; 3855 int lookup_me1 = 0; 3856 int lookup_me2 = 0; 3857 int lookup_me3 = 0; 3858 int magic1; 3859 int magic2; 3860 int magic3; 3861 memcpy(&magic1, dummy_fn1, sizeof(magic1)); 3862 memcpy(&magic2, dummy_fn2, sizeof(magic2)); 3863 memcpy(&magic3, dummy_fn3, sizeof(magic3)); 3864 #if defined(TWAIT_HAVE_STATUS) 3865 int status; 3866 #endif 3867 3868 DPRINTF("Before forking process PID=%d\n", getpid()); 3869 SYSCALL_REQUIRE((child = fork()) != -1); 3870 if (child == 0) { 3871 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3872 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3873 3874 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3875 FORKEE_ASSERT(raise(sigval) == 0); 3876 3877 DPRINTF("Before exiting of the child process\n"); 3878 _exit(exitval); 3879 } 3880 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3881 3882 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3883 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3884 3885 validate_status_stopped(status, sigval); 3886 3887 DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n", 3888 child, getpid()); 3889 errno = 0; 3890 lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0); 3891 SYSCALL_REQUIRE_ERRNO(errno, 0); 3892 3893 ATF_REQUIRE_EQ_MSG(lookup_me1, magic1, 3894 "got value %#x != expected %#x", lookup_me1, magic1); 3895 3896 DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n", 3897 child, getpid()); 3898 errno = 0; 3899 lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0); 3900 SYSCALL_REQUIRE_ERRNO(errno, 0); 3901 3902 ATF_REQUIRE_EQ_MSG(lookup_me2, magic2, 3903 "got value %#x != expected %#x", lookup_me2, magic2); 3904 3905 DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n", 3906 child, getpid()); 3907 errno = 0; 3908 lookup_me3 = ptrace(PT_READ_I, child, dummy_fn3, 0); 3909 SYSCALL_REQUIRE_ERRNO(errno, 0); 3910 3911 ATF_REQUIRE_EQ_MSG(lookup_me3, magic3, 3912 "got value %#x != expected %#x", lookup_me3, magic3); 3913 3914 DPRINTF("Before resuming the child process where it left off and " 3915 "without signal to be sent\n"); 3916 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 3917 3918 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3919 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3920 3921 validate_status_exited(status, exitval); 3922 3923 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3924 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 3925 } 3926 3927 ATF_TC(read_i4); 3928 ATF_TC_HEAD(read_i4, tc) 3929 { 3930 atf_tc_set_md_var(tc, "descr", 3931 "Verify PT_READ_I called four times"); 3932 } 3933 3934 ATF_TC_BODY(read_i4, tc) 3935 { 3936 const int exitval = 5; 3937 const int sigval = SIGSTOP; 3938 pid_t child, wpid; 3939 int lookup_me1 = 0; 3940 int lookup_me2 = 0; 3941 int lookup_me3 = 0; 3942 int lookup_me4 = 0; 3943 int magic1; 3944 int magic2; 3945 int magic3; 3946 int magic4; 3947 memcpy(&magic1, dummy_fn1, sizeof(magic1)); 3948 memcpy(&magic2, dummy_fn2, sizeof(magic2)); 3949 memcpy(&magic3, dummy_fn3, sizeof(magic3)); 3950 memcpy(&magic4, dummy_fn4, sizeof(magic4)); 3951 #if defined(TWAIT_HAVE_STATUS) 3952 int status; 3953 #endif 3954 3955 DPRINTF("Before forking process PID=%d\n", getpid()); 3956 SYSCALL_REQUIRE((child = fork()) != -1); 3957 if (child == 0) { 3958 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 3959 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 3960 3961 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 3962 FORKEE_ASSERT(raise(sigval) == 0); 3963 3964 DPRINTF("Before exiting of the child process\n"); 3965 _exit(exitval); 3966 } 3967 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 3968 3969 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 3970 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 3971 3972 validate_status_stopped(status, sigval); 3973 3974 DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n", 3975 child, getpid()); 3976 errno = 0; 3977 lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0); 3978 SYSCALL_REQUIRE_ERRNO(errno, 0); 3979 3980 ATF_REQUIRE_EQ_MSG(lookup_me1, magic1, 3981 "got value %#x != expected %#x", lookup_me1, magic1); 3982 3983 DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n", 3984 child, getpid()); 3985 errno = 0; 3986 lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0); 3987 SYSCALL_REQUIRE_ERRNO(errno, 0); 3988 3989 ATF_REQUIRE_EQ_MSG(lookup_me2, magic2, 3990 "got value %#x != expected %#x", lookup_me2, magic2); 3991 3992 DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n", 3993 child, getpid()); 3994 errno = 0; 3995 lookup_me3 = ptrace(PT_READ_I, child, dummy_fn3, 0); 3996 SYSCALL_REQUIRE_ERRNO(errno, 0); 3997 3998 ATF_REQUIRE_EQ_MSG(lookup_me3, magic3, 3999 "got value %#x != expected %#x", lookup_me3, magic3); 4000 4001 DPRINTF("Read new lookup_me4 from tracee (PID=%d) by tracer (PID=%d)\n", 4002 child, getpid()); 4003 errno = 0; 4004 lookup_me4 = ptrace(PT_READ_I, child, dummy_fn4, 0); 4005 SYSCALL_REQUIRE_ERRNO(errno, 0); 4006 4007 ATF_REQUIRE_EQ_MSG(lookup_me4, magic4, 4008 "got value %#x != expected %#x", lookup_me4, magic4); 4009 4010 DPRINTF("Before resuming the child process where it left off and " 4011 "without signal to be sent\n"); 4012 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4013 4014 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4015 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4016 4017 validate_status_exited(status, exitval); 4018 4019 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4020 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4021 } 4022 4023 #if defined(HAVE_GPREGS) 4024 ATF_TC(regs1); 4025 ATF_TC_HEAD(regs1, tc) 4026 { 4027 atf_tc_set_md_var(tc, "descr", 4028 "Verify plain PT_GETREGS call without further steps"); 4029 } 4030 4031 ATF_TC_BODY(regs1, tc) 4032 { 4033 const int exitval = 5; 4034 const int sigval = SIGSTOP; 4035 pid_t child, wpid; 4036 #if defined(TWAIT_HAVE_STATUS) 4037 int status; 4038 #endif 4039 struct reg r; 4040 4041 DPRINTF("Before forking process PID=%d\n", getpid()); 4042 SYSCALL_REQUIRE((child = fork()) != -1); 4043 if (child == 0) { 4044 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4045 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4046 4047 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4048 FORKEE_ASSERT(raise(sigval) == 0); 4049 4050 DPRINTF("Before exiting of the child process\n"); 4051 _exit(exitval); 4052 } 4053 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4054 4055 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4056 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4057 4058 validate_status_stopped(status, sigval); 4059 4060 DPRINTF("Call GETREGS for the child process\n"); 4061 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 4062 4063 DPRINTF("Before resuming the child process where it left off and " 4064 "without signal to be sent\n"); 4065 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4066 4067 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4068 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4069 4070 validate_status_exited(status, exitval); 4071 4072 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4073 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4074 } 4075 #endif 4076 4077 #if defined(HAVE_GPREGS) 4078 ATF_TC(regs2); 4079 ATF_TC_HEAD(regs2, tc) 4080 { 4081 atf_tc_set_md_var(tc, "descr", 4082 "Verify plain PT_GETREGS call and retrieve PC"); 4083 } 4084 4085 ATF_TC_BODY(regs2, tc) 4086 { 4087 const int exitval = 5; 4088 const int sigval = SIGSTOP; 4089 pid_t child, wpid; 4090 #if defined(TWAIT_HAVE_STATUS) 4091 int status; 4092 #endif 4093 struct reg r; 4094 4095 DPRINTF("Before forking process PID=%d\n", getpid()); 4096 SYSCALL_REQUIRE((child = fork()) != -1); 4097 if (child == 0) { 4098 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4099 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4100 4101 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4102 FORKEE_ASSERT(raise(sigval) == 0); 4103 4104 DPRINTF("Before exiting of the child process\n"); 4105 _exit(exitval); 4106 } 4107 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4108 4109 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4110 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4111 4112 validate_status_stopped(status, sigval); 4113 4114 DPRINTF("Call GETREGS for the child process\n"); 4115 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 4116 4117 DPRINTF("Retrieved PC=%" PRIxREGISTER "\n", PTRACE_REG_PC(&r)); 4118 4119 DPRINTF("Before resuming the child process where it left off and " 4120 "without signal to be sent\n"); 4121 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4122 4123 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4124 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4125 4126 validate_status_exited(status, exitval); 4127 4128 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4129 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4130 } 4131 #endif 4132 4133 #if defined(HAVE_GPREGS) 4134 ATF_TC(regs3); 4135 ATF_TC_HEAD(regs3, tc) 4136 { 4137 atf_tc_set_md_var(tc, "descr", 4138 "Verify plain PT_GETREGS call and retrieve SP"); 4139 } 4140 4141 ATF_TC_BODY(regs3, tc) 4142 { 4143 const int exitval = 5; 4144 const int sigval = SIGSTOP; 4145 pid_t child, wpid; 4146 #if defined(TWAIT_HAVE_STATUS) 4147 int status; 4148 #endif 4149 struct reg r; 4150 4151 DPRINTF("Before forking process PID=%d\n", getpid()); 4152 SYSCALL_REQUIRE((child = fork()) != -1); 4153 if (child == 0) { 4154 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4155 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4156 4157 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4158 FORKEE_ASSERT(raise(sigval) == 0); 4159 4160 DPRINTF("Before exiting of the child process\n"); 4161 _exit(exitval); 4162 } 4163 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4164 4165 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4166 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4167 4168 validate_status_stopped(status, sigval); 4169 4170 DPRINTF("Call GETREGS for the child process\n"); 4171 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 4172 4173 DPRINTF("Retrieved SP=%" PRIxREGISTER "\n", PTRACE_REG_SP(&r)); 4174 4175 DPRINTF("Before resuming the child process where it left off and " 4176 "without signal to be sent\n"); 4177 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4178 4179 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4180 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4181 4182 validate_status_exited(status, exitval); 4183 4184 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4185 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4186 } 4187 #endif 4188 4189 #if defined(HAVE_GPREGS) 4190 ATF_TC(regs4); 4191 ATF_TC_HEAD(regs4, tc) 4192 { 4193 atf_tc_set_md_var(tc, "descr", 4194 "Verify plain PT_GETREGS call and retrieve INTRV"); 4195 } 4196 4197 ATF_TC_BODY(regs4, tc) 4198 { 4199 const int exitval = 5; 4200 const int sigval = SIGSTOP; 4201 pid_t child, wpid; 4202 #if defined(TWAIT_HAVE_STATUS) 4203 int status; 4204 #endif 4205 struct reg r; 4206 4207 DPRINTF("Before forking process PID=%d\n", getpid()); 4208 SYSCALL_REQUIRE((child = fork()) != -1); 4209 if (child == 0) { 4210 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4211 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4212 4213 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4214 FORKEE_ASSERT(raise(sigval) == 0); 4215 4216 DPRINTF("Before exiting of the child process\n"); 4217 _exit(exitval); 4218 } 4219 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4220 4221 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4222 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4223 4224 validate_status_stopped(status, sigval); 4225 4226 DPRINTF("Call GETREGS for the child process\n"); 4227 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 4228 4229 DPRINTF("Retrieved INTRV=%" PRIxREGISTER "\n", PTRACE_REG_INTRV(&r)); 4230 4231 DPRINTF("Before resuming the child process where it left off and " 4232 "without signal to be sent\n"); 4233 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4234 4235 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4236 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4237 4238 validate_status_exited(status, exitval); 4239 4240 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4241 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4242 } 4243 #endif 4244 4245 #if defined(HAVE_GPREGS) 4246 ATF_TC(regs5); 4247 ATF_TC_HEAD(regs5, tc) 4248 { 4249 atf_tc_set_md_var(tc, "descr", 4250 "Verify PT_GETREGS and PT_SETREGS calls without changing regs"); 4251 } 4252 4253 ATF_TC_BODY(regs5, tc) 4254 { 4255 const int exitval = 5; 4256 const int sigval = SIGSTOP; 4257 pid_t child, wpid; 4258 #if defined(TWAIT_HAVE_STATUS) 4259 int status; 4260 #endif 4261 struct reg r; 4262 4263 DPRINTF("Before forking process PID=%d\n", getpid()); 4264 SYSCALL_REQUIRE((child = fork()) != -1); 4265 if (child == 0) { 4266 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4267 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4268 4269 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4270 FORKEE_ASSERT(raise(sigval) == 0); 4271 4272 DPRINTF("Before exiting of the child process\n"); 4273 _exit(exitval); 4274 } 4275 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4276 4277 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4278 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4279 4280 validate_status_stopped(status, sigval); 4281 4282 DPRINTF("Call GETREGS for the child process\n"); 4283 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 4284 4285 DPRINTF("Call SETREGS for the child process (without changed regs)\n"); 4286 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 4287 4288 DPRINTF("Before resuming the child process where it left off and " 4289 "without signal to be sent\n"); 4290 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4291 4292 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4293 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4294 4295 validate_status_exited(status, exitval); 4296 4297 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4298 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4299 } 4300 #endif 4301 4302 #if defined(HAVE_FPREGS) 4303 ATF_TC(fpregs1); 4304 ATF_TC_HEAD(fpregs1, tc) 4305 { 4306 atf_tc_set_md_var(tc, "descr", 4307 "Verify plain PT_GETFPREGS call without further steps"); 4308 } 4309 4310 ATF_TC_BODY(fpregs1, tc) 4311 { 4312 const int exitval = 5; 4313 const int sigval = SIGSTOP; 4314 pid_t child, wpid; 4315 #if defined(TWAIT_HAVE_STATUS) 4316 int status; 4317 #endif 4318 struct fpreg r; 4319 4320 DPRINTF("Before forking process PID=%d\n", getpid()); 4321 SYSCALL_REQUIRE((child = fork()) != -1); 4322 if (child == 0) { 4323 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4324 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4325 4326 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4327 FORKEE_ASSERT(raise(sigval) == 0); 4328 4329 DPRINTF("Before exiting of the child process\n"); 4330 _exit(exitval); 4331 } 4332 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4333 4334 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4335 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4336 4337 validate_status_stopped(status, sigval); 4338 4339 DPRINTF("Call GETFPREGS for the child process\n"); 4340 SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1); 4341 4342 DPRINTF("Before resuming the child process where it left off and " 4343 "without signal to be sent\n"); 4344 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4345 4346 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4347 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4348 4349 validate_status_exited(status, exitval); 4350 4351 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4352 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4353 } 4354 #endif 4355 4356 #if defined(HAVE_FPREGS) 4357 ATF_TC(fpregs2); 4358 ATF_TC_HEAD(fpregs2, tc) 4359 { 4360 atf_tc_set_md_var(tc, "descr", 4361 "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing " 4362 "regs"); 4363 } 4364 4365 ATF_TC_BODY(fpregs2, tc) 4366 { 4367 const int exitval = 5; 4368 const int sigval = SIGSTOP; 4369 pid_t child, wpid; 4370 #if defined(TWAIT_HAVE_STATUS) 4371 int status; 4372 #endif 4373 struct fpreg r; 4374 4375 DPRINTF("Before forking process PID=%d\n", getpid()); 4376 SYSCALL_REQUIRE((child = fork()) != -1); 4377 if (child == 0) { 4378 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4379 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4380 4381 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4382 FORKEE_ASSERT(raise(sigval) == 0); 4383 4384 DPRINTF("Before exiting of the child process\n"); 4385 _exit(exitval); 4386 } 4387 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4388 4389 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4390 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4391 4392 validate_status_stopped(status, sigval); 4393 4394 DPRINTF("Call GETFPREGS for the child process\n"); 4395 SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1); 4396 4397 DPRINTF("Call SETFPREGS for the child (without changed regs)\n"); 4398 SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1); 4399 4400 DPRINTF("Before resuming the child process where it left off and " 4401 "without signal to be sent\n"); 4402 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4403 4404 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4405 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4406 4407 validate_status_exited(status, exitval); 4408 4409 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4410 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4411 } 4412 #endif 4413 4414 #if defined(PT_STEP) 4415 static void 4416 ptrace_step(int N, int setstep) 4417 { 4418 const int exitval = 5; 4419 const int sigval = SIGSTOP; 4420 pid_t child, wpid; 4421 #if defined(TWAIT_HAVE_STATUS) 4422 int status; 4423 #endif 4424 int happy; 4425 4426 #if defined(__arm__) 4427 /* PT_STEP not supported on arm 32-bit */ 4428 atf_tc_expect_fail("PR kern/52119"); 4429 #endif 4430 4431 DPRINTF("Before forking process PID=%d\n", getpid()); 4432 SYSCALL_REQUIRE((child = fork()) != -1); 4433 if (child == 0) { 4434 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4435 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4436 4437 happy = check_happy(999); 4438 4439 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4440 FORKEE_ASSERT(raise(sigval) == 0); 4441 4442 FORKEE_ASSERT_EQ(happy, check_happy(999)); 4443 4444 DPRINTF("Before exiting of the child process\n"); 4445 _exit(exitval); 4446 } 4447 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4448 4449 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4450 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4451 4452 validate_status_stopped(status, sigval); 4453 4454 while (N --> 0) { 4455 if (setstep) { 4456 DPRINTF("Before resuming the child process where it " 4457 "left off and without signal to be sent (use " 4458 "PT_SETSTEP and PT_CONTINUE)\n"); 4459 SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1); 4460 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) 4461 != -1); 4462 } else { 4463 DPRINTF("Before resuming the child process where it " 4464 "left off and without signal to be sent (use " 4465 "PT_STEP)\n"); 4466 SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) 4467 != -1); 4468 } 4469 4470 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4471 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), 4472 child); 4473 4474 validate_status_stopped(status, SIGTRAP); 4475 4476 if (setstep) { 4477 SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1); 4478 } 4479 } 4480 4481 DPRINTF("Before resuming the child process where it left off and " 4482 "without signal to be sent\n"); 4483 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4484 4485 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4486 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4487 4488 validate_status_exited(status, exitval); 4489 4490 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4491 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4492 } 4493 #endif 4494 4495 #if defined(PT_STEP) 4496 ATF_TC(step1); 4497 ATF_TC_HEAD(step1, tc) 4498 { 4499 atf_tc_set_md_var(tc, "descr", 4500 "Verify single PT_STEP call"); 4501 } 4502 4503 ATF_TC_BODY(step1, tc) 4504 { 4505 ptrace_step(1, 0); 4506 } 4507 #endif 4508 4509 #if defined(PT_STEP) 4510 ATF_TC(step2); 4511 ATF_TC_HEAD(step2, tc) 4512 { 4513 atf_tc_set_md_var(tc, "descr", 4514 "Verify PT_STEP called twice"); 4515 } 4516 4517 ATF_TC_BODY(step2, tc) 4518 { 4519 ptrace_step(2, 0); 4520 } 4521 #endif 4522 4523 #if defined(PT_STEP) 4524 ATF_TC(step3); 4525 ATF_TC_HEAD(step3, tc) 4526 { 4527 atf_tc_set_md_var(tc, "descr", 4528 "Verify PT_STEP called three times"); 4529 } 4530 4531 ATF_TC_BODY(step3, tc) 4532 { 4533 ptrace_step(3, 0); 4534 } 4535 #endif 4536 4537 #if defined(PT_STEP) 4538 ATF_TC(step4); 4539 ATF_TC_HEAD(step4, tc) 4540 { 4541 atf_tc_set_md_var(tc, "descr", 4542 "Verify PT_STEP called four times"); 4543 } 4544 4545 ATF_TC_BODY(step4, tc) 4546 { 4547 ptrace_step(4, 0); 4548 } 4549 #endif 4550 4551 #if defined(PT_STEP) 4552 ATF_TC(setstep1); 4553 ATF_TC_HEAD(setstep1, tc) 4554 { 4555 atf_tc_set_md_var(tc, "descr", 4556 "Verify single PT_SETSTEP call"); 4557 } 4558 4559 ATF_TC_BODY(setstep1, tc) 4560 { 4561 ptrace_step(1, 1); 4562 } 4563 #endif 4564 4565 #if defined(PT_STEP) 4566 ATF_TC(setstep2); 4567 ATF_TC_HEAD(setstep2, tc) 4568 { 4569 atf_tc_set_md_var(tc, "descr", 4570 "Verify PT_SETSTEP called twice"); 4571 } 4572 4573 ATF_TC_BODY(setstep2, tc) 4574 { 4575 ptrace_step(2, 1); 4576 } 4577 #endif 4578 4579 #if defined(PT_STEP) 4580 ATF_TC(setstep3); 4581 ATF_TC_HEAD(setstep3, tc) 4582 { 4583 atf_tc_set_md_var(tc, "descr", 4584 "Verify PT_SETSTEP called three times"); 4585 } 4586 4587 ATF_TC_BODY(setstep3, tc) 4588 { 4589 ptrace_step(3, 1); 4590 } 4591 #endif 4592 4593 #if defined(PT_STEP) 4594 ATF_TC(setstep4); 4595 ATF_TC_HEAD(setstep4, tc) 4596 { 4597 atf_tc_set_md_var(tc, "descr", 4598 "Verify PT_SETSTEP called four times"); 4599 } 4600 4601 ATF_TC_BODY(setstep4, tc) 4602 { 4603 ptrace_step(4, 1); 4604 } 4605 #endif 4606 4607 ATF_TC(kill1); 4608 ATF_TC_HEAD(kill1, tc) 4609 { 4610 atf_tc_set_md_var(tc, "descr", 4611 "Verify that PT_CONTINUE with SIGKILL terminates child"); 4612 } 4613 4614 ATF_TC_BODY(kill1, tc) 4615 { 4616 const int sigval = SIGSTOP, sigsent = SIGKILL; 4617 pid_t child, wpid; 4618 #if defined(TWAIT_HAVE_STATUS) 4619 int status; 4620 #endif 4621 4622 DPRINTF("Before forking process PID=%d\n", getpid()); 4623 SYSCALL_REQUIRE((child = fork()) != -1); 4624 if (child == 0) { 4625 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4626 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4627 4628 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4629 FORKEE_ASSERT(raise(sigval) == 0); 4630 4631 /* NOTREACHED */ 4632 FORKEE_ASSERTX(0 && 4633 "Child should be terminated by a signal from its parent"); 4634 } 4635 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4636 4637 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4638 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4639 4640 validate_status_stopped(status, sigval); 4641 4642 DPRINTF("Before resuming the child process where it left off and " 4643 "without signal to be sent\n"); 4644 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1); 4645 4646 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4647 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4648 4649 validate_status_signaled(status, sigsent, 0); 4650 4651 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4652 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4653 } 4654 4655 ATF_TC(kill2); 4656 ATF_TC_HEAD(kill2, tc) 4657 { 4658 atf_tc_set_md_var(tc, "descr", 4659 "Verify that PT_KILL terminates child"); 4660 } 4661 4662 ATF_TC_BODY(kill2, tc) 4663 { 4664 const int sigval = SIGSTOP; 4665 pid_t child, wpid; 4666 #if defined(TWAIT_HAVE_STATUS) 4667 int status; 4668 #endif 4669 4670 DPRINTF("Before forking process PID=%d\n", getpid()); 4671 SYSCALL_REQUIRE((child = fork()) != -1); 4672 if (child == 0) { 4673 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4674 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4675 4676 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4677 FORKEE_ASSERT(raise(sigval) == 0); 4678 4679 /* NOTREACHED */ 4680 FORKEE_ASSERTX(0 && 4681 "Child should be terminated by a signal from its parent"); 4682 } 4683 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4684 4685 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4686 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4687 4688 validate_status_stopped(status, sigval); 4689 4690 DPRINTF("Before resuming the child process where it left off and " 4691 "without signal to be sent\n"); 4692 SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1); 4693 4694 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4695 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4696 4697 validate_status_signaled(status, SIGKILL, 0); 4698 4699 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4700 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4701 } 4702 4703 ATF_TC(lwpinfo1); 4704 ATF_TC_HEAD(lwpinfo1, tc) 4705 { 4706 atf_tc_set_md_var(tc, "descr", 4707 "Verify basic LWPINFO call for single thread (PT_TRACE_ME)"); 4708 } 4709 4710 ATF_TC_BODY(lwpinfo1, tc) 4711 { 4712 const int exitval = 5; 4713 const int sigval = SIGSTOP; 4714 pid_t child, wpid; 4715 #if defined(TWAIT_HAVE_STATUS) 4716 int status; 4717 #endif 4718 struct ptrace_lwpinfo info = {0, 0}; 4719 4720 DPRINTF("Before forking process PID=%d\n", getpid()); 4721 SYSCALL_REQUIRE((child = fork()) != -1); 4722 if (child == 0) { 4723 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4724 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4725 4726 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4727 FORKEE_ASSERT(raise(sigval) == 0); 4728 4729 DPRINTF("Before exiting of the child process\n"); 4730 _exit(exitval); 4731 } 4732 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4733 4734 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4735 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4736 4737 validate_status_stopped(status, sigval); 4738 4739 DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n"); 4740 SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1); 4741 4742 DPRINTF("Assert that there exists a thread\n"); 4743 ATF_REQUIRE(info.pl_lwpid > 0); 4744 4745 DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n", 4746 info.pl_lwpid); 4747 ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL, 4748 "Received event %d != expected event %d", 4749 info.pl_event, PL_EVENT_SIGNAL); 4750 4751 DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n"); 4752 SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1); 4753 4754 DPRINTF("Assert that there are no more lwp threads in child\n"); 4755 ATF_REQUIRE_EQ(info.pl_lwpid, 0); 4756 4757 DPRINTF("Before resuming the child process where it left off and " 4758 "without signal to be sent\n"); 4759 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4760 4761 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4762 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4763 4764 validate_status_exited(status, exitval); 4765 4766 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4767 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4768 } 4769 4770 #if defined(TWAIT_HAVE_PID) 4771 ATF_TC(lwpinfo2); 4772 ATF_TC_HEAD(lwpinfo2, tc) 4773 { 4774 atf_tc_set_md_var(tc, "descr", 4775 "Verify basic LWPINFO call for single thread (PT_ATTACH from " 4776 "tracer)"); 4777 } 4778 4779 ATF_TC_BODY(lwpinfo2, tc) 4780 { 4781 struct msg_fds parent_tracee, parent_tracer; 4782 const int exitval_tracee = 5; 4783 const int exitval_tracer = 10; 4784 pid_t tracee, tracer, wpid; 4785 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 4786 #if defined(TWAIT_HAVE_STATUS) 4787 int status; 4788 #endif 4789 struct ptrace_lwpinfo info = {0, 0}; 4790 4791 DPRINTF("Spawn tracee\n"); 4792 SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); 4793 SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); 4794 tracee = atf_utils_fork(); 4795 if (tracee == 0) { 4796 4797 /* Wait for message from the parent */ 4798 CHILD_TO_PARENT("tracee ready", parent_tracee, msg); 4799 CHILD_FROM_PARENT("tracee exit", parent_tracee, msg); 4800 4801 _exit(exitval_tracee); 4802 } 4803 PARENT_FROM_CHILD("tracee ready", parent_tracee, msg); 4804 4805 DPRINTF("Spawn debugger\n"); 4806 tracer = atf_utils_fork(); 4807 if (tracer == 0) { 4808 /* No IPC to communicate with the child */ 4809 DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); 4810 FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); 4811 4812 /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ 4813 FORKEE_REQUIRE_SUCCESS( 4814 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 4815 4816 forkee_status_stopped(status, SIGSTOP); 4817 4818 DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n"); 4819 FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info)) 4820 != -1); 4821 4822 DPRINTF("Assert that there exists a thread\n"); 4823 FORKEE_ASSERTX(info.pl_lwpid > 0); 4824 4825 DPRINTF("Assert that lwp thread %d received event " 4826 "PL_EVENT_SIGNAL\n", info.pl_lwpid); 4827 FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL); 4828 4829 DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n"); 4830 FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info)) 4831 != -1); 4832 4833 DPRINTF("Assert that there are no more lwp threads in child\n"); 4834 FORKEE_ASSERTX(info.pl_lwpid == 0); 4835 4836 /* Resume tracee with PT_CONTINUE */ 4837 FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); 4838 4839 /* Inform parent that tracer has attached to tracee */ 4840 CHILD_TO_PARENT("tracer ready", parent_tracer, msg); 4841 /* Wait for parent */ 4842 CHILD_FROM_PARENT("tracer wait", parent_tracer, msg); 4843 4844 /* Wait for tracee and assert that it exited */ 4845 FORKEE_REQUIRE_SUCCESS( 4846 wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); 4847 4848 forkee_status_exited(status, exitval_tracee); 4849 4850 DPRINTF("Before exiting of the tracer process\n"); 4851 _exit(exitval_tracer); 4852 } 4853 4854 DPRINTF("Wait for the tracer to attach to the tracee\n"); 4855 PARENT_FROM_CHILD("tracer ready", parent_tracer, msg); 4856 4857 DPRINTF("Resume the tracee and let it exit\n"); 4858 PARENT_TO_CHILD("tracee exit", parent_tracee, msg); 4859 4860 DPRINTF("Detect that tracee is zombie\n"); 4861 await_zombie(tracee); 4862 4863 DPRINTF("Assert that there is no status about tracee - " 4864 "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME); 4865 TWAIT_REQUIRE_SUCCESS( 4866 wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0); 4867 4868 DPRINTF("Resume the tracer and let it detect exited tracee\n"); 4869 PARENT_TO_CHILD("tracer wait", parent_tracer, msg); 4870 4871 DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n", 4872 TWAIT_FNAME); 4873 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0), 4874 tracer); 4875 4876 validate_status_exited(status, exitval_tracer); 4877 4878 DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", 4879 TWAIT_FNAME); 4880 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 4881 tracee); 4882 4883 validate_status_exited(status, exitval_tracee); 4884 4885 msg_close(&parent_tracer); 4886 msg_close(&parent_tracee); 4887 } 4888 #endif 4889 4890 ATF_TC(siginfo1); 4891 ATF_TC_HEAD(siginfo1, tc) 4892 { 4893 atf_tc_set_md_var(tc, "descr", 4894 "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee"); 4895 } 4896 4897 ATF_TC_BODY(siginfo1, tc) 4898 { 4899 const int exitval = 5; 4900 const int sigval = SIGTRAP; 4901 pid_t child, wpid; 4902 #if defined(TWAIT_HAVE_STATUS) 4903 int status; 4904 #endif 4905 struct ptrace_siginfo info; 4906 memset(&info, 0, sizeof(info)); 4907 4908 DPRINTF("Before forking process PID=%d\n", getpid()); 4909 SYSCALL_REQUIRE((child = fork()) != -1); 4910 if (child == 0) { 4911 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4912 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4913 4914 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4915 FORKEE_ASSERT(raise(sigval) == 0); 4916 4917 DPRINTF("Before exiting of the child process\n"); 4918 _exit(exitval); 4919 } 4920 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4921 4922 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4923 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4924 4925 validate_status_stopped(status, sigval); 4926 4927 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 4928 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 4929 4930 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid); 4931 DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n", 4932 info.psi_siginfo.si_signo, info.psi_siginfo.si_code, 4933 info.psi_siginfo.si_errno); 4934 4935 DPRINTF("Before resuming the child process where it left off and " 4936 "without signal to be sent\n"); 4937 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 4938 4939 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4940 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 4941 4942 validate_status_exited(status, exitval); 4943 4944 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 4945 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 4946 } 4947 4948 ATF_TC(siginfo2); 4949 ATF_TC_HEAD(siginfo2, tc) 4950 { 4951 atf_tc_set_md_var(tc, "descr", 4952 "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without " 4953 "modification of SIGINT from tracee"); 4954 } 4955 4956 static int siginfo2_caught = 0; 4957 4958 static void 4959 siginfo2_sighandler(int sig) 4960 { 4961 FORKEE_ASSERT_EQ(sig, SIGINT); 4962 4963 ++siginfo2_caught; 4964 } 4965 4966 ATF_TC_BODY(siginfo2, tc) 4967 { 4968 const int exitval = 5; 4969 const int sigval = SIGINT; 4970 pid_t child, wpid; 4971 struct sigaction sa; 4972 #if defined(TWAIT_HAVE_STATUS) 4973 int status; 4974 #endif 4975 struct ptrace_siginfo info; 4976 memset(&info, 0, sizeof(info)); 4977 4978 DPRINTF("Before forking process PID=%d\n", getpid()); 4979 SYSCALL_REQUIRE((child = fork()) != -1); 4980 if (child == 0) { 4981 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 4982 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 4983 4984 sa.sa_handler = siginfo2_sighandler; 4985 sa.sa_flags = SA_SIGINFO; 4986 sigemptyset(&sa.sa_mask); 4987 4988 FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1); 4989 4990 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 4991 FORKEE_ASSERT(raise(sigval) == 0); 4992 4993 FORKEE_ASSERT_EQ(siginfo2_caught, 1); 4994 4995 DPRINTF("Before exiting of the child process\n"); 4996 _exit(exitval); 4997 } 4998 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 4999 5000 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5001 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5002 5003 validate_status_stopped(status, sigval); 5004 5005 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5006 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5007 5008 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid); 5009 DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n", 5010 info.psi_siginfo.si_signo, info.psi_siginfo.si_code, 5011 info.psi_siginfo.si_errno); 5012 5013 DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n"); 5014 SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1); 5015 5016 DPRINTF("Before resuming the child process where it left off and " 5017 "without signal to be sent\n"); 5018 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1); 5019 5020 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5021 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5022 5023 validate_status_exited(status, exitval); 5024 5025 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5026 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5027 } 5028 5029 ATF_TC(siginfo3); 5030 ATF_TC_HEAD(siginfo3, tc) 5031 { 5032 atf_tc_set_md_var(tc, "descr", 5033 "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with " 5034 "setting signal to new value"); 5035 } 5036 5037 static int siginfo3_caught = 0; 5038 5039 static void 5040 siginfo3_sigaction(int sig, siginfo_t *info, void *ctx) 5041 { 5042 FORKEE_ASSERT_EQ(sig, SIGTRAP); 5043 5044 FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP); 5045 FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT); 5046 5047 ++siginfo3_caught; 5048 } 5049 5050 ATF_TC_BODY(siginfo3, tc) 5051 { 5052 const int exitval = 5; 5053 const int sigval = SIGINT; 5054 const int sigfaked = SIGTRAP; 5055 const int sicodefaked = TRAP_BRKPT; 5056 pid_t child, wpid; 5057 struct sigaction sa; 5058 #if defined(TWAIT_HAVE_STATUS) 5059 int status; 5060 #endif 5061 struct ptrace_siginfo info; 5062 memset(&info, 0, sizeof(info)); 5063 5064 DPRINTF("Before forking process PID=%d\n", getpid()); 5065 SYSCALL_REQUIRE((child = fork()) != -1); 5066 if (child == 0) { 5067 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5068 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5069 5070 sa.sa_sigaction = siginfo3_sigaction; 5071 sa.sa_flags = SA_SIGINFO; 5072 sigemptyset(&sa.sa_mask); 5073 5074 FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1); 5075 5076 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5077 FORKEE_ASSERT(raise(sigval) == 0); 5078 5079 FORKEE_ASSERT_EQ(siginfo3_caught, 1); 5080 5081 DPRINTF("Before exiting of the child process\n"); 5082 _exit(exitval); 5083 } 5084 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5085 5086 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5087 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5088 5089 validate_status_stopped(status, sigval); 5090 5091 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5092 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5093 5094 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid); 5095 DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n", 5096 info.psi_siginfo.si_signo, info.psi_siginfo.si_code, 5097 info.psi_siginfo.si_errno); 5098 5099 DPRINTF("Before setting new faked signal to signo=%d si_code=%d\n", 5100 sigfaked, sicodefaked); 5101 info.psi_siginfo.si_signo = sigfaked; 5102 info.psi_siginfo.si_code = sicodefaked; 5103 5104 DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n"); 5105 SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1); 5106 5107 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5108 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5109 5110 DPRINTF("Before checking siginfo_t\n"); 5111 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked); 5112 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked); 5113 5114 DPRINTF("Before resuming the child process where it left off and " 5115 "without signal to be sent\n"); 5116 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1); 5117 5118 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5119 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5120 5121 validate_status_exited(status, exitval); 5122 5123 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5124 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5125 } 5126 5127 ATF_TC(siginfo4); 5128 ATF_TC_HEAD(siginfo4, tc) 5129 { 5130 atf_tc_set_md_var(tc, "descr", 5131 "Detect SIGTRAP TRAP_EXEC from tracee"); 5132 } 5133 5134 ATF_TC_BODY(siginfo4, tc) 5135 { 5136 const int sigval = SIGTRAP; 5137 pid_t child, wpid; 5138 #if defined(TWAIT_HAVE_STATUS) 5139 int status; 5140 #endif 5141 5142 struct ptrace_siginfo info; 5143 memset(&info, 0, sizeof(info)); 5144 5145 DPRINTF("Before forking process PID=%d\n", getpid()); 5146 SYSCALL_REQUIRE((child = fork()) != -1); 5147 if (child == 0) { 5148 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5149 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5150 5151 DPRINTF("Before calling execve(2) from child\n"); 5152 execlp("/bin/echo", "/bin/echo", NULL); 5153 5154 FORKEE_ASSERT(0 && "Not reached"); 5155 } 5156 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5157 5158 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5159 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5160 5161 validate_status_stopped(status, sigval); 5162 5163 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5164 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5165 5166 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid); 5167 DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n", 5168 info.psi_siginfo.si_signo, info.psi_siginfo.si_code, 5169 info.psi_siginfo.si_errno); 5170 5171 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval); 5172 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC); 5173 5174 DPRINTF("Before resuming the child process where it left off and " 5175 "without signal to be sent\n"); 5176 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5177 5178 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5179 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5180 5181 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5182 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5183 } 5184 5185 #if defined(TWAIT_HAVE_PID) 5186 ATF_TC(siginfo5); 5187 ATF_TC_HEAD(siginfo5, tc) 5188 { 5189 atf_tc_set_md_var(tc, "descr", 5190 "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK " 5191 "set to PTRACE_FORK and reports correct signal information"); 5192 } 5193 5194 ATF_TC_BODY(siginfo5, tc) 5195 { 5196 const int exitval = 5; 5197 const int exitval2 = 15; 5198 const int sigval = SIGSTOP; 5199 pid_t child, child2, wpid; 5200 #if defined(TWAIT_HAVE_STATUS) 5201 int status; 5202 #endif 5203 ptrace_state_t state; 5204 const int slen = sizeof(state); 5205 ptrace_event_t event; 5206 const int elen = sizeof(event); 5207 struct ptrace_siginfo info; 5208 5209 memset(&info, 0, sizeof(info)); 5210 5211 DPRINTF("Before forking process PID=%d\n", getpid()); 5212 SYSCALL_REQUIRE((child = fork()) != -1); 5213 if (child == 0) { 5214 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5215 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5216 5217 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5218 FORKEE_ASSERT(raise(sigval) == 0); 5219 5220 FORKEE_ASSERT((child2 = fork()) != -1); 5221 5222 if (child2 == 0) 5223 _exit(exitval2); 5224 5225 FORKEE_REQUIRE_SUCCESS 5226 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 5227 5228 forkee_status_exited(status, exitval2); 5229 5230 DPRINTF("Before exiting of the child process\n"); 5231 _exit(exitval); 5232 } 5233 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5234 5235 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5236 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5237 5238 validate_status_stopped(status, sigval); 5239 5240 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5241 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5242 5243 DPRINTF("Before checking siginfo_t\n"); 5244 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval); 5245 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP); 5246 5247 DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child); 5248 event.pe_set_event = PTRACE_FORK; 5249 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 5250 5251 DPRINTF("Before resuming the child process where it left off and " 5252 "without signal to be sent\n"); 5253 DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, " 5254 "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and " 5255 "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, " 5256 "state.pe_other_pid=child)\n", child); 5257 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5258 5259 DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child); 5260 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5261 5262 validate_status_stopped(status, SIGTRAP); 5263 5264 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5265 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5266 5267 DPRINTF("Before checking siginfo_t\n"); 5268 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP); 5269 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD); 5270 5271 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 5272 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK); 5273 5274 child2 = state.pe_other_pid; 5275 DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2); 5276 5277 DPRINTF("Before calling %s() for the forkee %d of the child %d\n", 5278 TWAIT_FNAME, child2, child); 5279 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 5280 child2); 5281 5282 validate_status_stopped(status, SIGTRAP); 5283 5284 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5285 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5286 5287 DPRINTF("Before checking siginfo_t\n"); 5288 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP); 5289 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD); 5290 5291 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1); 5292 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK); 5293 ATF_REQUIRE_EQ(state.pe_other_pid, child); 5294 5295 DPRINTF("Before resuming the forkee process where it left off and " 5296 "without signal to be sent\n"); 5297 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1); 5298 5299 DPRINTF("Before resuming the child process where it left off and " 5300 "without signal to be sent\n"); 5301 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5302 5303 DPRINTF("Before calling %s() for the forkee - expected exited\n", 5304 TWAIT_FNAME); 5305 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 5306 child2); 5307 5308 validate_status_exited(status, exitval2); 5309 5310 DPRINTF("Before calling %s() for the forkee - expected no process\n", 5311 TWAIT_FNAME); 5312 TWAIT_REQUIRE_FAILURE(ECHILD, 5313 wpid = TWAIT_GENERIC(child2, &status, 0)); 5314 5315 DPRINTF("Before calling %s() for the child - expected stopped " 5316 "SIGCHLD\n", TWAIT_FNAME); 5317 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5318 5319 validate_status_stopped(status, SIGCHLD); 5320 5321 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5322 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5323 5324 DPRINTF("Before checking siginfo_t\n"); 5325 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD); 5326 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED); 5327 5328 DPRINTF("Before resuming the child process where it left off and " 5329 "without signal to be sent\n"); 5330 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5331 5332 DPRINTF("Before calling %s() for the child - expected exited\n", 5333 TWAIT_FNAME); 5334 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5335 5336 validate_status_exited(status, exitval); 5337 5338 DPRINTF("Before calling %s() for the child - expected no process\n", 5339 TWAIT_FNAME); 5340 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5341 } 5342 #endif 5343 5344 #if defined(PT_STEP) 5345 ATF_TC(siginfo6); 5346 ATF_TC_HEAD(siginfo6, tc) 5347 { 5348 atf_tc_set_md_var(tc, "descr", 5349 "Verify single PT_STEP call with signal information check"); 5350 } 5351 5352 ATF_TC_BODY(siginfo6, tc) 5353 { 5354 const int exitval = 5; 5355 const int sigval = SIGSTOP; 5356 pid_t child, wpid; 5357 #if defined(TWAIT_HAVE_STATUS) 5358 int status; 5359 #endif 5360 int happy; 5361 struct ptrace_siginfo info; 5362 5363 #if defined(__arm__) 5364 /* PT_STEP not supported on arm 32-bit */ 5365 atf_tc_expect_fail("PR kern/52119"); 5366 #endif 5367 5368 memset(&info, 0, sizeof(info)); 5369 5370 DPRINTF("Before forking process PID=%d\n", getpid()); 5371 SYSCALL_REQUIRE((child = fork()) != -1); 5372 if (child == 0) { 5373 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5374 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5375 5376 happy = check_happy(100); 5377 5378 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5379 FORKEE_ASSERT(raise(sigval) == 0); 5380 5381 FORKEE_ASSERT_EQ(happy, check_happy(100)); 5382 5383 DPRINTF("Before exiting of the child process\n"); 5384 _exit(exitval); 5385 } 5386 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5387 5388 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5389 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5390 5391 validate_status_stopped(status, sigval); 5392 5393 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5394 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5395 5396 DPRINTF("Before checking siginfo_t\n"); 5397 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval); 5398 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP); 5399 5400 DPRINTF("Before resuming the child process where it left off and " 5401 "without signal to be sent (use PT_STEP)\n"); 5402 SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1); 5403 5404 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5405 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5406 5407 validate_status_stopped(status, SIGTRAP); 5408 5409 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 5410 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 5411 5412 DPRINTF("Before checking siginfo_t\n"); 5413 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP); 5414 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE); 5415 5416 DPRINTF("Before resuming the child process where it left off and " 5417 "without signal to be sent\n"); 5418 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5419 5420 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5421 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5422 5423 validate_status_exited(status, exitval); 5424 5425 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5426 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5427 } 5428 #endif 5429 5430 volatile lwpid_t the_lwp_id = 0; 5431 5432 static void 5433 lwp_main_func(void *arg) 5434 { 5435 the_lwp_id = _lwp_self(); 5436 _lwp_exit(); 5437 } 5438 5439 ATF_TC(lwp_create1); 5440 ATF_TC_HEAD(lwp_create1, tc) 5441 { 5442 atf_tc_set_md_var(tc, "descr", 5443 "Verify that 1 LWP creation is intercepted by ptrace(2) with " 5444 "EVENT_MASK set to PTRACE_LWP_CREATE"); 5445 } 5446 5447 ATF_TC_BODY(lwp_create1, tc) 5448 { 5449 const int exitval = 5; 5450 const int sigval = SIGSTOP; 5451 pid_t child, wpid; 5452 #if defined(TWAIT_HAVE_STATUS) 5453 int status; 5454 #endif 5455 ptrace_state_t state; 5456 const int slen = sizeof(state); 5457 ptrace_event_t event; 5458 const int elen = sizeof(event); 5459 ucontext_t uc; 5460 lwpid_t lid; 5461 static const size_t ssize = 16*1024; 5462 void *stack; 5463 5464 DPRINTF("Before forking process PID=%d\n", getpid()); 5465 SYSCALL_REQUIRE((child = fork()) != -1); 5466 if (child == 0) { 5467 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5468 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5469 5470 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5471 FORKEE_ASSERT(raise(sigval) == 0); 5472 5473 DPRINTF("Before allocating memory for stack in child\n"); 5474 FORKEE_ASSERT((stack = malloc(ssize)) != NULL); 5475 5476 DPRINTF("Before making context for new lwp in child\n"); 5477 _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize); 5478 5479 DPRINTF("Before creating new in child\n"); 5480 FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); 5481 5482 DPRINTF("Before waiting for lwp %d to exit\n", lid); 5483 FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); 5484 5485 DPRINTF("Before verifying that reported %d and running lid %d " 5486 "are the same\n", lid, the_lwp_id); 5487 FORKEE_ASSERT_EQ(lid, the_lwp_id); 5488 5489 DPRINTF("Before exiting of the child process\n"); 5490 _exit(exitval); 5491 } 5492 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5493 5494 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5495 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5496 5497 validate_status_stopped(status, sigval); 5498 5499 DPRINTF("Set empty EVENT_MASK for the child %d\n", child); 5500 event.pe_set_event = PTRACE_LWP_CREATE; 5501 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 5502 5503 DPRINTF("Before resuming the child process where it left off and " 5504 "without signal to be sent\n"); 5505 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5506 5507 DPRINTF("Before calling %s() for the child - expected stopped " 5508 "SIGTRAP\n", TWAIT_FNAME); 5509 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5510 5511 validate_status_stopped(status, SIGTRAP); 5512 5513 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 5514 5515 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE); 5516 5517 lid = state.pe_lwp; 5518 DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid); 5519 5520 DPRINTF("Before resuming the child process where it left off and " 5521 "without signal to be sent\n"); 5522 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5523 5524 DPRINTF("Before calling %s() for the child - expected exited\n", 5525 TWAIT_FNAME); 5526 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5527 5528 validate_status_exited(status, exitval); 5529 5530 DPRINTF("Before calling %s() for the child - expected no process\n", 5531 TWAIT_FNAME); 5532 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5533 } 5534 5535 ATF_TC(lwp_exit1); 5536 ATF_TC_HEAD(lwp_exit1, tc) 5537 { 5538 atf_tc_set_md_var(tc, "descr", 5539 "Verify that 1 LWP creation is intercepted by ptrace(2) with " 5540 "EVENT_MASK set to PTRACE_LWP_EXIT"); 5541 } 5542 5543 ATF_TC_BODY(lwp_exit1, tc) 5544 { 5545 const int exitval = 5; 5546 const int sigval = SIGSTOP; 5547 pid_t child, wpid; 5548 #if defined(TWAIT_HAVE_STATUS) 5549 int status; 5550 #endif 5551 ptrace_state_t state; 5552 const int slen = sizeof(state); 5553 ptrace_event_t event; 5554 const int elen = sizeof(event); 5555 ucontext_t uc; 5556 lwpid_t lid; 5557 static const size_t ssize = 16*1024; 5558 void *stack; 5559 5560 DPRINTF("Before forking process PID=%d\n", getpid()); 5561 SYSCALL_REQUIRE((child = fork()) != -1); 5562 if (child == 0) { 5563 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5564 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5565 5566 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5567 FORKEE_ASSERT(raise(sigval) == 0); 5568 5569 DPRINTF("Before allocating memory for stack in child\n"); 5570 FORKEE_ASSERT((stack = malloc(ssize)) != NULL); 5571 5572 DPRINTF("Before making context for new lwp in child\n"); 5573 _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize); 5574 5575 DPRINTF("Before creating new in child\n"); 5576 FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); 5577 5578 DPRINTF("Before waiting for lwp %d to exit\n", lid); 5579 FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); 5580 5581 DPRINTF("Before verifying that reported %d and running lid %d " 5582 "are the same\n", lid, the_lwp_id); 5583 FORKEE_ASSERT_EQ(lid, the_lwp_id); 5584 5585 DPRINTF("Before exiting of the child process\n"); 5586 _exit(exitval); 5587 } 5588 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5589 5590 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5591 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5592 5593 validate_status_stopped(status, sigval); 5594 5595 DPRINTF("Set empty EVENT_MASK for the child %d\n", child); 5596 event.pe_set_event = PTRACE_LWP_EXIT; 5597 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 5598 5599 DPRINTF("Before resuming the child process where it left off and " 5600 "without signal to be sent\n"); 5601 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5602 5603 DPRINTF("Before calling %s() for the child - expected stopped " 5604 "SIGTRAP\n", TWAIT_FNAME); 5605 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5606 5607 validate_status_stopped(status, SIGTRAP); 5608 5609 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 5610 5611 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT); 5612 5613 lid = state.pe_lwp; 5614 DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid); 5615 5616 DPRINTF("Before resuming the child process where it left off and " 5617 "without signal to be sent\n"); 5618 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5619 5620 DPRINTF("Before calling %s() for the child - expected exited\n", 5621 TWAIT_FNAME); 5622 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5623 5624 validate_status_exited(status, exitval); 5625 5626 DPRINTF("Before calling %s() for the child - expected no process\n", 5627 TWAIT_FNAME); 5628 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5629 } 5630 5631 ATF_TC(signal1); 5632 ATF_TC_HEAD(signal1, tc) 5633 { 5634 atf_tc_set_md_var(tc, "descr", 5635 "Verify that masking single unrelated signal does not stop tracer " 5636 "from catching other signals"); 5637 } 5638 5639 ATF_TC_BODY(signal1, tc) 5640 { 5641 const int exitval = 5; 5642 const int sigval = SIGSTOP; 5643 const int sigmasked = SIGTRAP; 5644 const int signotmasked = SIGINT; 5645 pid_t child, wpid; 5646 #if defined(TWAIT_HAVE_STATUS) 5647 int status; 5648 #endif 5649 sigset_t intmask; 5650 5651 DPRINTF("Before forking process PID=%d\n", getpid()); 5652 SYSCALL_REQUIRE((child = fork()) != -1); 5653 if (child == 0) { 5654 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5655 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5656 5657 sigemptyset(&intmask); 5658 sigaddset(&intmask, sigmasked); 5659 sigprocmask(SIG_BLOCK, &intmask, NULL); 5660 5661 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5662 FORKEE_ASSERT(raise(sigval) == 0); 5663 5664 DPRINTF("Before raising %s from child\n", 5665 strsignal(signotmasked)); 5666 FORKEE_ASSERT(raise(signotmasked) == 0); 5667 5668 DPRINTF("Before exiting of the child process\n"); 5669 _exit(exitval); 5670 } 5671 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5672 5673 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5674 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5675 5676 validate_status_stopped(status, sigval); 5677 5678 DPRINTF("Before resuming the child process where it left off and " 5679 "without signal to be sent\n"); 5680 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5681 5682 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5683 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5684 5685 validate_status_stopped(status, signotmasked); 5686 5687 DPRINTF("Before resuming the child process where it left off and " 5688 "without signal to be sent\n"); 5689 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5690 5691 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5692 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5693 5694 validate_status_exited(status, exitval); 5695 5696 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5697 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5698 } 5699 5700 ATF_TC(signal2); 5701 ATF_TC_HEAD(signal2, tc) 5702 { 5703 atf_tc_set_md_var(tc, "descr", 5704 "Verify that masking SIGTRAP in tracee stops tracer from " 5705 "catching this raised signal"); 5706 } 5707 5708 ATF_TC_BODY(signal2, tc) 5709 { 5710 const int exitval = 5; 5711 const int sigval = SIGSTOP; 5712 const int sigmasked = SIGTRAP; 5713 pid_t child, wpid; 5714 #if defined(TWAIT_HAVE_STATUS) 5715 int status; 5716 #endif 5717 sigset_t intmask; 5718 5719 DPRINTF("Before forking process PID=%d\n", getpid()); 5720 SYSCALL_REQUIRE((child = fork()) != -1); 5721 if (child == 0) { 5722 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5723 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5724 5725 sigemptyset(&intmask); 5726 sigaddset(&intmask, sigmasked); 5727 sigprocmask(SIG_BLOCK, &intmask, NULL); 5728 5729 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5730 FORKEE_ASSERT(raise(sigval) == 0); 5731 5732 DPRINTF("Before raising %s breakpoint from child\n", 5733 strsignal(sigmasked)); 5734 FORKEE_ASSERT(raise(sigmasked) == 0); 5735 5736 DPRINTF("Before exiting of the child process\n"); 5737 _exit(exitval); 5738 } 5739 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5740 5741 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5742 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5743 5744 validate_status_stopped(status, sigval); 5745 5746 DPRINTF("Before resuming the child process where it left off and " 5747 "without signal to be sent\n"); 5748 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5749 5750 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5751 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5752 5753 validate_status_exited(status, exitval); 5754 5755 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5756 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5757 } 5758 5759 ATF_TC(signal3); 5760 ATF_TC_HEAD(signal3, tc) 5761 { 5762 atf_tc_set_md_var(tc, "timeout", "5"); 5763 atf_tc_set_md_var(tc, "descr", 5764 "Verify that masking SIGTRAP in tracee does not stop tracer from " 5765 "catching software breakpoints"); 5766 } 5767 5768 ATF_TC_BODY(signal3, tc) 5769 { 5770 const int exitval = 5; 5771 const int sigval = SIGSTOP; 5772 const int sigmasked = SIGTRAP; 5773 pid_t child, wpid; 5774 #if defined(TWAIT_HAVE_STATUS) 5775 int status; 5776 #endif 5777 sigset_t intmask; 5778 5779 atf_tc_expect_fail("PR kern/51918"); 5780 5781 // This test breaks now on some ports, temporarily disable it 5782 ATF_REQUIRE(0 && "In order to get reliable failure, abort"); 5783 5784 #if defined(__sparc__) 5785 atf_tc_expect_timeout("PR kern/52167"); 5786 5787 // timeout wins, failure still valid 5788 // atf_tc_expect_fail("PR kern/51918"); 5789 #endif 5790 5791 DPRINTF("Before forking process PID=%d\n", getpid()); 5792 SYSCALL_REQUIRE((child = fork()) != -1); 5793 if (child == 0) { 5794 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5795 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5796 5797 sigemptyset(&intmask); 5798 sigaddset(&intmask, sigmasked); 5799 sigprocmask(SIG_BLOCK, &intmask, NULL); 5800 5801 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5802 FORKEE_ASSERT(raise(sigval) == 0); 5803 5804 DPRINTF("Before raising software breakpoint from child\n"); 5805 5806 #ifdef PTRACE_BREAKPOINT_ASM 5807 PTRACE_BREAKPOINT_ASM; 5808 #else 5809 /* port me */ 5810 #endif 5811 5812 DPRINTF("Before exiting of the child process\n"); 5813 _exit(exitval); 5814 } 5815 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5816 5817 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5818 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5819 5820 validate_status_stopped(status, sigval); 5821 5822 DPRINTF("Before resuming the child process where it left off and " 5823 "without signal to be sent\n"); 5824 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5825 5826 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5827 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5828 5829 validate_status_stopped(status, sigmasked); 5830 5831 DPRINTF("Before resuming the child process where it left off and " 5832 "without signal to be sent\n"); 5833 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5834 5835 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5836 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5837 5838 validate_status_exited(status, exitval); 5839 5840 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5841 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5842 } 5843 5844 #if defined(PT_STEP) 5845 ATF_TC(signal4); 5846 ATF_TC_HEAD(signal4, tc) 5847 { 5848 atf_tc_set_md_var(tc, "descr", 5849 "Verify that masking SIGTRAP in tracee does not stop tracer from " 5850 "catching single step trap"); 5851 } 5852 5853 ATF_TC_BODY(signal4, tc) 5854 { 5855 const int exitval = 5; 5856 const int sigval = SIGSTOP; 5857 const int sigmasked = SIGTRAP; 5858 pid_t child, wpid; 5859 #if defined(TWAIT_HAVE_STATUS) 5860 int status; 5861 #endif 5862 sigset_t intmask; 5863 int happy; 5864 5865 #if defined(__arm__) 5866 /* PT_STEP not supported on arm 32-bit */ 5867 atf_tc_expect_fail("PR kern/51918 PR kern/52119"); 5868 #endif 5869 5870 DPRINTF("Before forking process PID=%d\n", getpid()); 5871 SYSCALL_REQUIRE((child = fork()) != -1); 5872 if (child == 0) { 5873 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5874 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5875 5876 happy = check_happy(100); 5877 5878 sigemptyset(&intmask); 5879 sigaddset(&intmask, sigmasked); 5880 sigprocmask(SIG_BLOCK, &intmask, NULL); 5881 5882 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5883 FORKEE_ASSERT(raise(sigval) == 0); 5884 5885 FORKEE_ASSERT_EQ(happy, check_happy(100)); 5886 5887 DPRINTF("Before exiting of the child process\n"); 5888 _exit(exitval); 5889 } 5890 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5891 5892 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5893 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5894 5895 validate_status_stopped(status, sigval); 5896 5897 DPRINTF("Before resuming the child process where it left off and " 5898 "without signal to be sent\n"); 5899 SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1); 5900 5901 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5902 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5903 5904 validate_status_stopped(status, sigmasked); 5905 5906 DPRINTF("Before resuming the child process where it left off and " 5907 "without signal to be sent\n"); 5908 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5909 5910 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5911 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5912 5913 validate_status_exited(status, exitval); 5914 5915 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5916 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5917 } 5918 #endif 5919 5920 ATF_TC(signal5); 5921 ATF_TC_HEAD(signal5, tc) 5922 { 5923 atf_tc_set_md_var(tc, "descr", 5924 "Verify that masking SIGTRAP in tracee does not stop tracer from " 5925 "catching exec() breakpoint"); 5926 } 5927 5928 ATF_TC_BODY(signal5, tc) 5929 { 5930 const int exitval = 5; 5931 const int sigval = SIGSTOP; 5932 const int sigmasked = SIGTRAP; 5933 pid_t child, wpid; 5934 #if defined(TWAIT_HAVE_STATUS) 5935 int status; 5936 #endif 5937 sigset_t intmask; 5938 5939 atf_tc_expect_fail("wrong signal"); 5940 5941 DPRINTF("Before forking process PID=%d\n", getpid()); 5942 SYSCALL_REQUIRE((child = fork()) != -1); 5943 if (child == 0) { 5944 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 5945 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 5946 5947 sigemptyset(&intmask); 5948 sigaddset(&intmask, sigmasked); 5949 sigprocmask(SIG_BLOCK, &intmask, NULL); 5950 5951 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 5952 FORKEE_ASSERT(raise(sigval) == 0); 5953 5954 DPRINTF("Before calling execve(2) from child\n"); 5955 execlp("/bin/echo", "/bin/echo", NULL); 5956 5957 DPRINTF("Before exiting of the child process\n"); 5958 _exit(exitval); 5959 } 5960 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 5961 5962 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5963 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5964 5965 validate_status_stopped(status, sigval); 5966 5967 DPRINTF("Before resuming the child process where it left off and " 5968 "without signal to be sent\n"); 5969 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5970 5971 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5972 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5973 5974 validate_status_stopped(status, sigmasked); 5975 5976 DPRINTF("Before resuming the child process where it left off and " 5977 "without signal to be sent\n"); 5978 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 5979 5980 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5981 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 5982 5983 validate_status_exited(status, exitval); 5984 5985 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 5986 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 5987 } 5988 5989 #if defined(TWAIT_HAVE_PID) 5990 ATF_TC(signal6); 5991 ATF_TC_HEAD(signal6, tc) 5992 { 5993 atf_tc_set_md_var(tc, "timeout", "5"); 5994 atf_tc_set_md_var(tc, "descr", 5995 "Verify that masking SIGTRAP in tracee does not stop tracer from " 5996 "catching PTRACE_FORK breakpoint"); 5997 } 5998 5999 ATF_TC_BODY(signal6, tc) 6000 { 6001 const int exitval = 5; 6002 const int exitval2 = 15; 6003 const int sigval = SIGSTOP; 6004 const int sigmasked = SIGTRAP; 6005 pid_t child, child2, wpid; 6006 #if defined(TWAIT_HAVE_STATUS) 6007 int status; 6008 #endif 6009 sigset_t intmask; 6010 ptrace_state_t state; 6011 const int slen = sizeof(state); 6012 ptrace_event_t event; 6013 const int elen = sizeof(event); 6014 6015 atf_tc_expect_timeout("PR kern/51918"); 6016 6017 DPRINTF("Before forking process PID=%d\n", getpid()); 6018 SYSCALL_REQUIRE((child = fork()) != -1); 6019 if (child == 0) { 6020 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6021 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6022 6023 sigemptyset(&intmask); 6024 sigaddset(&intmask, sigmasked); 6025 sigprocmask(SIG_BLOCK, &intmask, NULL); 6026 6027 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6028 FORKEE_ASSERT(raise(sigval) == 0); 6029 6030 FORKEE_ASSERT((child2 = fork()) != -1); 6031 6032 if (child2 == 0) 6033 _exit(exitval2); 6034 6035 FORKEE_REQUIRE_SUCCESS 6036 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 6037 6038 forkee_status_exited(status, exitval2); 6039 6040 DPRINTF("Before exiting of the child process\n"); 6041 _exit(exitval); 6042 } 6043 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6044 6045 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6046 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6047 6048 validate_status_stopped(status, sigval); 6049 6050 DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child); 6051 event.pe_set_event = PTRACE_FORK; 6052 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 6053 6054 DPRINTF("Before resuming the child process where it left off and " 6055 "without signal to be sent\n"); 6056 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6057 6058 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6059 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6060 6061 validate_status_stopped(status, sigmasked); 6062 6063 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 6064 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK); 6065 6066 child2 = state.pe_other_pid; 6067 DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2); 6068 6069 DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME); 6070 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 6071 child2); 6072 6073 validate_status_stopped(status, SIGTRAP); 6074 6075 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1); 6076 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK); 6077 ATF_REQUIRE_EQ(state.pe_other_pid, child); 6078 6079 DPRINTF("Before resuming the forkee process where it left off and " 6080 "without signal to be sent\n"); 6081 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1); 6082 6083 DPRINTF("Before resuming the child process where it left off and " 6084 "without signal to be sent\n"); 6085 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6086 6087 DPRINTF("Before calling %s() for the forkee - expected exited\n", 6088 TWAIT_FNAME); 6089 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 6090 child2); 6091 6092 validate_status_exited(status, exitval2); 6093 6094 DPRINTF("Before calling %s() for the forkee - expected no process\n", 6095 TWAIT_FNAME); 6096 TWAIT_REQUIRE_FAILURE(ECHILD, 6097 wpid = TWAIT_GENERIC(child2, &status, 0)); 6098 6099 DPRINTF("Before calling %s() for the child - expected stopped " 6100 "SIGCHLD\n", TWAIT_FNAME); 6101 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6102 6103 validate_status_stopped(status, SIGCHLD); 6104 6105 DPRINTF("Before resuming the child process where it left off and " 6106 "without signal to be sent\n"); 6107 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6108 6109 DPRINTF("Before calling %s() for the child - expected exited\n", 6110 TWAIT_FNAME); 6111 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6112 6113 validate_status_exited(status, exitval); 6114 6115 DPRINTF("Before calling %s() for the child - expected no process\n", 6116 TWAIT_FNAME); 6117 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6118 } 6119 #endif 6120 6121 #if defined(TWAIT_HAVE_PID) 6122 ATF_TC(signal7); 6123 ATF_TC_HEAD(signal7, tc) 6124 { 6125 atf_tc_set_md_var(tc, "descr", 6126 "Verify that masking SIGTRAP in tracee does not stop tracer from " 6127 "catching PTRACE_VFORK breakpoint"); 6128 } 6129 6130 ATF_TC_BODY(signal7, tc) 6131 { 6132 const int exitval = 5; 6133 const int exitval2 = 15; 6134 const int sigval = SIGSTOP; 6135 const int sigmasked = SIGTRAP; 6136 pid_t child, child2, wpid; 6137 #if defined(TWAIT_HAVE_STATUS) 6138 int status; 6139 #endif 6140 sigset_t intmask; 6141 ptrace_state_t state; 6142 const int slen = sizeof(state); 6143 ptrace_event_t event; 6144 const int elen = sizeof(event); 6145 6146 atf_tc_expect_fail("PR kern/51918 PR kern/51630"); 6147 6148 DPRINTF("Before forking process PID=%d\n", getpid()); 6149 SYSCALL_REQUIRE((child = fork()) != -1); 6150 if (child == 0) { 6151 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6152 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6153 6154 sigemptyset(&intmask); 6155 sigaddset(&intmask, sigmasked); 6156 sigprocmask(SIG_BLOCK, &intmask, NULL); 6157 6158 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6159 FORKEE_ASSERT(raise(sigval) == 0); 6160 6161 FORKEE_ASSERT((child2 = fork()) != -1); 6162 6163 if (child2 == 0) 6164 _exit(exitval2); 6165 6166 FORKEE_REQUIRE_SUCCESS 6167 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 6168 6169 forkee_status_exited(status, exitval2); 6170 6171 DPRINTF("Before exiting of the child process\n"); 6172 _exit(exitval); 6173 } 6174 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6175 6176 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6177 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6178 6179 validate_status_stopped(status, sigval); 6180 6181 DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child); 6182 event.pe_set_event = PTRACE_VFORK; 6183 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP); 6184 6185 DPRINTF("Before resuming the child process where it left off and " 6186 "without signal to be sent\n"); 6187 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6188 6189 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6190 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6191 6192 validate_status_stopped(status, sigmasked); 6193 6194 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 6195 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK); 6196 6197 child2 = state.pe_other_pid; 6198 DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2); 6199 6200 DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME); 6201 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 6202 child2); 6203 6204 validate_status_stopped(status, SIGTRAP); 6205 6206 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1); 6207 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK); 6208 ATF_REQUIRE_EQ(state.pe_other_pid, child); 6209 6210 DPRINTF("Before resuming the forkee process where it left off and " 6211 "without signal to be sent\n"); 6212 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1); 6213 6214 DPRINTF("Before resuming the child process where it left off and " 6215 "without signal to be sent\n"); 6216 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6217 6218 DPRINTF("Before calling %s() for the forkee - expected exited\n", 6219 TWAIT_FNAME); 6220 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0), 6221 child2); 6222 6223 validate_status_exited(status, exitval2); 6224 6225 DPRINTF("Before calling %s() for the forkee - expected no process\n", 6226 TWAIT_FNAME); 6227 TWAIT_REQUIRE_FAILURE(ECHILD, 6228 wpid = TWAIT_GENERIC(child2, &status, 0)); 6229 6230 DPRINTF("Before calling %s() for the child - expected stopped " 6231 "SIGCHLD\n", TWAIT_FNAME); 6232 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6233 6234 validate_status_stopped(status, SIGCHLD); 6235 6236 DPRINTF("Before resuming the child process where it left off and " 6237 "without signal to be sent\n"); 6238 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6239 6240 DPRINTF("Before calling %s() for the child - expected exited\n", 6241 TWAIT_FNAME); 6242 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6243 6244 validate_status_exited(status, exitval); 6245 6246 DPRINTF("Before calling %s() for the child - expected no process\n", 6247 TWAIT_FNAME); 6248 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6249 } 6250 #endif 6251 6252 ATF_TC(signal8); 6253 ATF_TC_HEAD(signal8, tc) 6254 { 6255 atf_tc_set_md_var(tc, "descr", 6256 "Verify that masking SIGTRAP in tracee does not stop tracer from " 6257 "catching PTRACE_VFORK_DONE breakpoint"); 6258 } 6259 6260 ATF_TC_BODY(signal8, tc) 6261 { 6262 const int exitval = 5; 6263 const int exitval2 = 15; 6264 const int sigval = SIGSTOP; 6265 const int sigmasked = SIGTRAP; 6266 pid_t child, child2, wpid; 6267 #if defined(TWAIT_HAVE_STATUS) 6268 int status; 6269 #endif 6270 sigset_t intmask; 6271 ptrace_state_t state; 6272 const int slen = sizeof(state); 6273 ptrace_event_t event; 6274 const int elen = sizeof(event); 6275 6276 atf_tc_expect_fail("PR kern/51918"); 6277 6278 DPRINTF("Before forking process PID=%d\n", getpid()); 6279 SYSCALL_REQUIRE((child = fork()) != -1); 6280 if (child == 0) { 6281 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6282 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6283 6284 sigemptyset(&intmask); 6285 sigaddset(&intmask, sigmasked); 6286 sigprocmask(SIG_BLOCK, &intmask, NULL); 6287 6288 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6289 FORKEE_ASSERT(raise(sigval) == 0); 6290 6291 FORKEE_ASSERT((child2 = vfork()) != -1); 6292 6293 if (child2 == 0) 6294 _exit(exitval2); 6295 6296 FORKEE_REQUIRE_SUCCESS 6297 (wpid = TWAIT_GENERIC(child2, &status, 0), child2); 6298 6299 forkee_status_exited(status, exitval2); 6300 6301 DPRINTF("Before exiting of the child process\n"); 6302 _exit(exitval); 6303 } 6304 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6305 6306 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6307 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6308 6309 validate_status_stopped(status, sigval); 6310 6311 DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n", 6312 child); 6313 event.pe_set_event = PTRACE_VFORK_DONE; 6314 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 6315 6316 DPRINTF("Before resuming the child process where it left off and " 6317 "without signal to be sent\n"); 6318 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6319 6320 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6321 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6322 6323 validate_status_stopped(status, sigmasked); 6324 6325 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 6326 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE); 6327 6328 child2 = state.pe_other_pid; 6329 DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2); 6330 6331 DPRINTF("Before resuming the child process where it left off and " 6332 "without signal to be sent\n"); 6333 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6334 6335 DPRINTF("Before calling %s() for the child - expected stopped " 6336 "SIGCHLD\n", TWAIT_FNAME); 6337 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6338 6339 validate_status_stopped(status, SIGCHLD); 6340 6341 DPRINTF("Before resuming the child process where it left off and " 6342 "without signal to be sent\n"); 6343 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6344 6345 DPRINTF("Before calling %s() for the child - expected exited\n", 6346 TWAIT_FNAME); 6347 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6348 6349 validate_status_exited(status, exitval); 6350 6351 DPRINTF("Before calling %s() for the child - expected no process\n", 6352 TWAIT_FNAME); 6353 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6354 } 6355 6356 ATF_TC(signal9); 6357 ATF_TC_HEAD(signal9, tc) 6358 { 6359 atf_tc_set_md_var(tc, "descr", 6360 "Verify that masking SIGTRAP in tracee does not stop tracer from " 6361 "catching PTRACE_LWP_CREATE breakpoint"); 6362 } 6363 6364 ATF_TC_BODY(signal9, tc) 6365 { 6366 const int exitval = 5; 6367 const int sigval = SIGSTOP; 6368 const int sigmasked = SIGTRAP; 6369 pid_t child, wpid; 6370 #if defined(TWAIT_HAVE_STATUS) 6371 int status; 6372 #endif 6373 sigset_t intmask; 6374 ptrace_state_t state; 6375 const int slen = sizeof(state); 6376 ptrace_event_t event; 6377 const int elen = sizeof(event); 6378 ucontext_t uc; 6379 lwpid_t lid; 6380 static const size_t ssize = 16*1024; 6381 void *stack; 6382 6383 atf_tc_expect_fail("PR kern/51918"); 6384 6385 DPRINTF("Before forking process PID=%d\n", getpid()); 6386 SYSCALL_REQUIRE((child = fork()) != -1); 6387 if (child == 0) { 6388 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6389 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6390 6391 sigemptyset(&intmask); 6392 sigaddset(&intmask, sigmasked); 6393 sigprocmask(SIG_BLOCK, &intmask, NULL); 6394 6395 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6396 FORKEE_ASSERT(raise(sigval) == 0); 6397 6398 DPRINTF("Before allocating memory for stack in child\n"); 6399 FORKEE_ASSERT((stack = malloc(ssize)) != NULL); 6400 6401 DPRINTF("Before making context for new lwp in child\n"); 6402 _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize); 6403 6404 DPRINTF("Before creating new in child\n"); 6405 FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); 6406 6407 DPRINTF("Before waiting for lwp %d to exit\n", lid); 6408 FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); 6409 6410 DPRINTF("Before verifying that reported %d and running lid %d " 6411 "are the same\n", lid, the_lwp_id); 6412 FORKEE_ASSERT_EQ(lid, the_lwp_id); 6413 6414 DPRINTF("Before exiting of the child process\n"); 6415 _exit(exitval); 6416 } 6417 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6418 6419 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6420 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6421 6422 validate_status_stopped(status, sigval); 6423 6424 DPRINTF("Set empty EVENT_MASK for the child %d\n", child); 6425 event.pe_set_event = PTRACE_LWP_CREATE; 6426 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 6427 6428 DPRINTF("Before resuming the child process where it left off and " 6429 "without signal to be sent\n"); 6430 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6431 6432 DPRINTF("Before calling %s() for the child - expected stopped " 6433 "SIGTRAP\n", TWAIT_FNAME); 6434 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6435 6436 validate_status_stopped(status, sigmasked); 6437 6438 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 6439 6440 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE); 6441 6442 lid = state.pe_lwp; 6443 DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid); 6444 6445 DPRINTF("Before resuming the child process where it left off and " 6446 "without signal to be sent\n"); 6447 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6448 6449 DPRINTF("Before calling %s() for the child - expected exited\n", 6450 TWAIT_FNAME); 6451 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6452 6453 validate_status_exited(status, exitval); 6454 6455 DPRINTF("Before calling %s() for the child - expected no process\n", 6456 TWAIT_FNAME); 6457 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6458 } 6459 6460 ATF_TC(signal10); 6461 ATF_TC_HEAD(signal10, tc) 6462 { 6463 atf_tc_set_md_var(tc, "descr", 6464 "Verify that masking SIGTRAP in tracee does not stop tracer from " 6465 "catching PTRACE_LWP_EXIT breakpoint"); 6466 } 6467 6468 ATF_TC_BODY(signal10, tc) 6469 { 6470 const int exitval = 5; 6471 const int sigval = SIGSTOP; 6472 const int sigmasked = SIGTRAP; 6473 pid_t child, wpid; 6474 #if defined(TWAIT_HAVE_STATUS) 6475 int status; 6476 #endif 6477 sigset_t intmask; 6478 ptrace_state_t state; 6479 const int slen = sizeof(state); 6480 ptrace_event_t event; 6481 const int elen = sizeof(event); 6482 ucontext_t uc; 6483 lwpid_t lid; 6484 static const size_t ssize = 16*1024; 6485 void *stack; 6486 6487 atf_tc_expect_fail("PR kern/51918"); 6488 6489 DPRINTF("Before forking process PID=%d\n", getpid()); 6490 SYSCALL_REQUIRE((child = fork()) != -1); 6491 if (child == 0) { 6492 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6493 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6494 6495 sigemptyset(&intmask); 6496 sigaddset(&intmask, sigmasked); 6497 sigprocmask(SIG_BLOCK, &intmask, NULL); 6498 6499 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6500 FORKEE_ASSERT(raise(sigval) == 0); 6501 6502 DPRINTF("Before allocating memory for stack in child\n"); 6503 FORKEE_ASSERT((stack = malloc(ssize)) != NULL); 6504 6505 DPRINTF("Before making context for new lwp in child\n"); 6506 _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize); 6507 6508 DPRINTF("Before creating new in child\n"); 6509 FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); 6510 6511 DPRINTF("Before waiting for lwp %d to exit\n", lid); 6512 FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); 6513 6514 DPRINTF("Before verifying that reported %d and running lid %d " 6515 "are the same\n", lid, the_lwp_id); 6516 FORKEE_ASSERT_EQ(lid, the_lwp_id); 6517 6518 DPRINTF("Before exiting of the child process\n"); 6519 _exit(exitval); 6520 } 6521 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6522 6523 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6524 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6525 6526 validate_status_stopped(status, sigval); 6527 6528 DPRINTF("Set empty EVENT_MASK for the child %d\n", child); 6529 event.pe_set_event = PTRACE_LWP_EXIT; 6530 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1); 6531 6532 DPRINTF("Before resuming the child process where it left off and " 6533 "without signal to be sent\n"); 6534 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6535 6536 DPRINTF("Before calling %s() for the child - expected stopped " 6537 "SIGTRAP\n", TWAIT_FNAME); 6538 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6539 6540 validate_status_stopped(status, sigmasked); 6541 6542 SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1); 6543 6544 ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT); 6545 6546 lid = state.pe_lwp; 6547 DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid); 6548 6549 DPRINTF("Before resuming the child process where it left off and " 6550 "without signal to be sent\n"); 6551 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6552 6553 DPRINTF("Before calling %s() for the child - expected exited\n", 6554 TWAIT_FNAME); 6555 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6556 6557 validate_status_exited(status, exitval); 6558 6559 DPRINTF("Before calling %s() for the child - expected no process\n", 6560 TWAIT_FNAME); 6561 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6562 } 6563 6564 static void 6565 lwp_main_stop(void *arg) 6566 { 6567 the_lwp_id = _lwp_self(); 6568 6569 raise(SIGTRAP); 6570 6571 _lwp_exit(); 6572 } 6573 6574 ATF_TC(suspend1); 6575 ATF_TC_HEAD(suspend1, tc) 6576 { 6577 atf_tc_set_md_var(tc, "descr", 6578 "Verify that a thread can be suspended by a debugger and later " 6579 "resumed by a tracee"); 6580 } 6581 6582 ATF_TC_BODY(suspend1, tc) 6583 { 6584 const int exitval = 5; 6585 const int sigval = SIGSTOP; 6586 pid_t child, wpid; 6587 #if defined(TWAIT_HAVE_STATUS) 6588 int status; 6589 #endif 6590 ucontext_t uc; 6591 lwpid_t lid; 6592 static const size_t ssize = 16*1024; 6593 void *stack; 6594 struct ptrace_lwpinfo pl; 6595 struct ptrace_siginfo psi; 6596 volatile int go = 0; 6597 6598 // Feature pending for refactoring 6599 atf_tc_expect_fail("PR kern/51995"); 6600 6601 // Hangs with qemu 6602 ATF_REQUIRE(0 && "In order to get reliable failure, abort"); 6603 6604 DPRINTF("Before forking process PID=%d\n", getpid()); 6605 SYSCALL_REQUIRE((child = fork()) != -1); 6606 if (child == 0) { 6607 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6608 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6609 6610 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6611 FORKEE_ASSERT(raise(sigval) == 0); 6612 6613 DPRINTF("Before allocating memory for stack in child\n"); 6614 FORKEE_ASSERT((stack = malloc(ssize)) != NULL); 6615 6616 DPRINTF("Before making context for new lwp in child\n"); 6617 _lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize); 6618 6619 DPRINTF("Before creating new in child\n"); 6620 FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); 6621 6622 while (go == 0) 6623 continue; 6624 6625 raise(SIGINT); 6626 6627 FORKEE_ASSERT(_lwp_continue(lid) == 0); 6628 6629 DPRINTF("Before waiting for lwp %d to exit\n", lid); 6630 FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); 6631 6632 DPRINTF("Before verifying that reported %d and running lid %d " 6633 "are the same\n", lid, the_lwp_id); 6634 FORKEE_ASSERT_EQ(lid, the_lwp_id); 6635 6636 DPRINTF("Before exiting of the child process\n"); 6637 _exit(exitval); 6638 } 6639 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6640 6641 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6642 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6643 6644 validate_status_stopped(status, sigval); 6645 6646 DPRINTF("Before resuming the child process where it left off and " 6647 "without signal to be sent\n"); 6648 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6649 6650 DPRINTF("Before calling %s() for the child - expected stopped " 6651 "SIGTRAP\n", TWAIT_FNAME); 6652 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6653 6654 validate_status_stopped(status, SIGTRAP); 6655 6656 DPRINTF("Before reading siginfo and lwpid_t\n"); 6657 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1); 6658 6659 DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid); 6660 SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1); 6661 6662 DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n", 6663 child, getpid()); 6664 SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1); 6665 6666 DPRINTF("Before resuming the child process where it left off and " 6667 "without signal to be sent\n"); 6668 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6669 6670 DPRINTF("Before calling %s() for the child - expected stopped " 6671 "SIGINT\n", TWAIT_FNAME); 6672 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6673 6674 validate_status_stopped(status, SIGINT); 6675 6676 pl.pl_lwpid = 0; 6677 6678 SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1); 6679 while (pl.pl_lwpid != 0) { 6680 6681 SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1); 6682 switch (pl.pl_lwpid) { 6683 case 1: 6684 ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL); 6685 break; 6686 case 2: 6687 ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED); 6688 break; 6689 } 6690 } 6691 6692 DPRINTF("Before resuming the child process where it left off and " 6693 "without signal to be sent\n"); 6694 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6695 6696 DPRINTF("Before calling %s() for the child - expected exited\n", 6697 TWAIT_FNAME); 6698 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6699 6700 validate_status_exited(status, exitval); 6701 6702 DPRINTF("Before calling %s() for the child - expected no process\n", 6703 TWAIT_FNAME); 6704 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6705 } 6706 6707 ATF_TC(suspend2); 6708 ATF_TC_HEAD(suspend2, tc) 6709 { 6710 atf_tc_set_md_var(tc, "descr", 6711 "Verify that the while the only thread within a process is " 6712 "suspended, the whole process cannot be unstopped"); 6713 } 6714 6715 ATF_TC_BODY(suspend2, tc) 6716 { 6717 const int exitval = 5; 6718 const int sigval = SIGSTOP; 6719 pid_t child, wpid; 6720 #if defined(TWAIT_HAVE_STATUS) 6721 int status; 6722 #endif 6723 struct ptrace_siginfo psi; 6724 6725 // Feature pending for refactoring 6726 atf_tc_expect_fail("PR kern/51995"); 6727 6728 // Hangs with qemu 6729 ATF_REQUIRE(0 && "In order to get reliable failure, abort"); 6730 6731 DPRINTF("Before forking process PID=%d\n", getpid()); 6732 SYSCALL_REQUIRE((child = fork()) != -1); 6733 if (child == 0) { 6734 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6735 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6736 6737 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6738 FORKEE_ASSERT(raise(sigval) == 0); 6739 6740 DPRINTF("Before exiting of the child process\n"); 6741 _exit(exitval); 6742 } 6743 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6744 6745 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6746 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6747 6748 validate_status_stopped(status, sigval); 6749 6750 DPRINTF("Before reading siginfo and lwpid_t\n"); 6751 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1); 6752 6753 DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid); 6754 SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1); 6755 6756 DPRINTF("Before resuming the child process where it left off and " 6757 "without signal to be sent\n"); 6758 ATF_REQUIRE_ERRNO(EDEADLK, 6759 ptrace(PT_CONTINUE, child, (void *)1, 0) == -1); 6760 6761 DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid); 6762 SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1); 6763 6764 DPRINTF("Before resuming the child process where it left off and " 6765 "without signal to be sent\n"); 6766 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6767 6768 DPRINTF("Before calling %s() for the child - expected exited\n", 6769 TWAIT_FNAME); 6770 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6771 6772 validate_status_exited(status, exitval); 6773 6774 DPRINTF("Before calling %s() for the child - expected no process\n", 6775 TWAIT_FNAME); 6776 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6777 } 6778 6779 ATF_TC(resume1); 6780 ATF_TC_HEAD(resume1, tc) 6781 { 6782 atf_tc_set_md_var(tc, "timeout", "5"); 6783 atf_tc_set_md_var(tc, "descr", 6784 "Verify that a thread can be suspended by a debugger and later " 6785 "resumed by the debugger"); 6786 } 6787 6788 ATF_TC_BODY(resume1, tc) 6789 { 6790 struct msg_fds fds; 6791 const int exitval = 5; 6792 const int sigval = SIGSTOP; 6793 pid_t child, wpid; 6794 uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ 6795 #if defined(TWAIT_HAVE_STATUS) 6796 int status; 6797 #endif 6798 ucontext_t uc; 6799 lwpid_t lid; 6800 static const size_t ssize = 16*1024; 6801 void *stack; 6802 struct ptrace_lwpinfo pl; 6803 struct ptrace_siginfo psi; 6804 6805 // Feature pending for refactoring 6806 atf_tc_expect_fail("PR kern/51995"); 6807 6808 // Hangs with qemu 6809 ATF_REQUIRE(0 && "In order to get reliable failure, abort"); 6810 6811 SYSCALL_REQUIRE(msg_open(&fds) == 0); 6812 6813 DPRINTF("Before forking process PID=%d\n", getpid()); 6814 SYSCALL_REQUIRE((child = fork()) != -1); 6815 if (child == 0) { 6816 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6817 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6818 6819 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6820 FORKEE_ASSERT(raise(sigval) == 0); 6821 6822 DPRINTF("Before allocating memory for stack in child\n"); 6823 FORKEE_ASSERT((stack = malloc(ssize)) != NULL); 6824 6825 DPRINTF("Before making context for new lwp in child\n"); 6826 _lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize); 6827 6828 DPRINTF("Before creating new in child\n"); 6829 FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0); 6830 6831 CHILD_TO_PARENT("Message", fds, msg); 6832 6833 raise(SIGINT); 6834 6835 DPRINTF("Before waiting for lwp %d to exit\n", lid); 6836 FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0); 6837 6838 DPRINTF("Before verifying that reported %d and running lid %d " 6839 "are the same\n", lid, the_lwp_id); 6840 FORKEE_ASSERT_EQ(lid, the_lwp_id); 6841 6842 DPRINTF("Before exiting of the child process\n"); 6843 _exit(exitval); 6844 } 6845 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6846 6847 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6848 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6849 6850 validate_status_stopped(status, sigval); 6851 6852 DPRINTF("Before resuming the child process where it left off and " 6853 "without signal to be sent\n"); 6854 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6855 6856 DPRINTF("Before calling %s() for the child - expected stopped " 6857 "SIGTRAP\n", TWAIT_FNAME); 6858 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6859 6860 validate_status_stopped(status, SIGTRAP); 6861 6862 DPRINTF("Before reading siginfo and lwpid_t\n"); 6863 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1); 6864 6865 DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid); 6866 SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1); 6867 6868 PARENT_FROM_CHILD("Message", fds, msg); 6869 6870 DPRINTF("Before resuming the child process where it left off and " 6871 "without signal to be sent\n"); 6872 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6873 6874 DPRINTF("Before calling %s() for the child - expected stopped " 6875 "SIGINT\n", TWAIT_FNAME); 6876 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6877 6878 validate_status_stopped(status, SIGINT); 6879 6880 pl.pl_lwpid = 0; 6881 6882 SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1); 6883 while (pl.pl_lwpid != 0) { 6884 SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1); 6885 switch (pl.pl_lwpid) { 6886 case 1: 6887 ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL); 6888 break; 6889 case 2: 6890 ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED); 6891 break; 6892 } 6893 } 6894 6895 DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid); 6896 SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1); 6897 6898 DPRINTF("Before resuming the child process where it left off and " 6899 "without signal to be sent\n"); 6900 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6901 6902 DPRINTF("Before calling %s() for the child - expected exited\n", 6903 TWAIT_FNAME); 6904 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6905 6906 validate_status_exited(status, exitval); 6907 6908 DPRINTF("Before calling %s() for the child - expected no process\n", 6909 TWAIT_FNAME); 6910 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6911 6912 msg_close(&fds); 6913 6914 DPRINTF("XXX: Test worked this time but for consistency timeout it\n"); 6915 sleep(10); 6916 } 6917 6918 ATF_TC(syscall1); 6919 ATF_TC_HEAD(syscall1, tc) 6920 { 6921 atf_tc_set_md_var(tc, "descr", 6922 "Verify that getpid(2) can be traced with PT_SYSCALL"); 6923 } 6924 6925 ATF_TC_BODY(syscall1, tc) 6926 { 6927 const int exitval = 5; 6928 const int sigval = SIGSTOP; 6929 pid_t child, wpid; 6930 #if defined(TWAIT_HAVE_STATUS) 6931 int status; 6932 #endif 6933 struct ptrace_siginfo info; 6934 memset(&info, 0, sizeof(info)); 6935 6936 DPRINTF("Before forking process PID=%d\n", getpid()); 6937 SYSCALL_REQUIRE((child = fork()) != -1); 6938 if (child == 0) { 6939 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 6940 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 6941 6942 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 6943 FORKEE_ASSERT(raise(sigval) == 0); 6944 6945 syscall(SYS_getpid); 6946 6947 DPRINTF("Before exiting of the child process\n"); 6948 _exit(exitval); 6949 } 6950 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 6951 6952 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6953 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6954 6955 validate_status_stopped(status, sigval); 6956 6957 DPRINTF("Before resuming the child process where it left off and " 6958 "without signal to be sent\n"); 6959 SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1); 6960 6961 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6962 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6963 6964 validate_status_stopped(status, SIGTRAP); 6965 6966 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 6967 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 6968 6969 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP); 6970 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE); 6971 6972 DPRINTF("Before resuming the child process where it left off and " 6973 "without signal to be sent\n"); 6974 SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1); 6975 6976 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6977 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6978 6979 validate_status_stopped(status, SIGTRAP); 6980 6981 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 6982 SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 6983 6984 DPRINTF("Before checking siginfo_t\n"); 6985 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP); 6986 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX); 6987 6988 DPRINTF("Before resuming the child process where it left off and " 6989 "without signal to be sent\n"); 6990 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 6991 6992 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6993 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 6994 6995 validate_status_exited(status, exitval); 6996 6997 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 6998 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 6999 } 7000 7001 ATF_TC(syscallemu1); 7002 ATF_TC_HEAD(syscallemu1, tc) 7003 { 7004 atf_tc_set_md_var(tc, "descr", 7005 "Verify that exit(2) can be intercepted with PT_SYSCALLEMU"); 7006 } 7007 7008 ATF_TC_BODY(syscallemu1, tc) 7009 { 7010 const int exitval = 5; 7011 const int sigval = SIGSTOP; 7012 pid_t child, wpid; 7013 #if defined(TWAIT_HAVE_STATUS) 7014 int status; 7015 #endif 7016 7017 #if defined(__sparc__) && !defined(__sparc64__) 7018 /* syscallemu does not work on sparc (32-bit) */ 7019 atf_tc_expect_fail("PR kern/52166"); 7020 #endif 7021 7022 DPRINTF("Before forking process PID=%d\n", getpid()); 7023 SYSCALL_REQUIRE((child = fork()) != -1); 7024 if (child == 0) { 7025 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 7026 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 7027 7028 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 7029 FORKEE_ASSERT(raise(sigval) == 0); 7030 7031 syscall(SYS_exit, 100); 7032 7033 DPRINTF("Before exiting of the child process\n"); 7034 _exit(exitval); 7035 } 7036 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 7037 7038 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 7039 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 7040 7041 validate_status_stopped(status, sigval); 7042 7043 DPRINTF("Before resuming the child process where it left off and " 7044 "without signal to be sent\n"); 7045 SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1); 7046 7047 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 7048 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 7049 7050 validate_status_stopped(status, SIGTRAP); 7051 7052 DPRINTF("Set SYSCALLEMU for intercepted syscall\n"); 7053 SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1); 7054 7055 DPRINTF("Before resuming the child process where it left off and " 7056 "without signal to be sent\n"); 7057 SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1); 7058 7059 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 7060 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 7061 7062 validate_status_stopped(status, SIGTRAP); 7063 7064 DPRINTF("Before resuming the child process where it left off and " 7065 "without signal to be sent\n"); 7066 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 7067 7068 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 7069 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 7070 7071 validate_status_exited(status, exitval); 7072 7073 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 7074 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 7075 } 7076 7077 #if defined(TWAIT_HAVE_PID) 7078 ATF_TC(race1); 7079 ATF_TC_HEAD(race1, tc) 7080 { 7081 atf_tc_set_md_var(tc, "descr", 7082 "Assert that await_zombie() in attach1 always finds a single " 7083 "process and no other error is reported"); 7084 } 7085 7086 ATF_TC_BODY(race1, tc) 7087 { 7088 time_t start, end; 7089 double diff; 7090 unsigned long N = 0; 7091 7092 /* Reuse this test with attach1 */ 7093 7094 start = time(NULL); 7095 while (true) { 7096 DPRINTF("Step: %lu\n", N); 7097 attach1_raw(true); 7098 end = time(NULL); 7099 diff = difftime(end, start); 7100 if (diff >= 5.0) 7101 break; 7102 ++N; 7103 } 7104 DPRINTF("Iterations: %lu\n", N); 7105 } 7106 #endif 7107 7108 #include "t_ptrace_amd64_wait.h" 7109 #include "t_ptrace_i386_wait.h" 7110 #include "t_ptrace_x86_wait.h" 7111 7112 ATF_TP_ADD_TCS(tp) 7113 { 7114 setvbuf(stdout, NULL, _IONBF, 0); 7115 setvbuf(stderr, NULL, _IONBF, 0); 7116 ATF_TP_ADD_TC(tp, traceme1); 7117 ATF_TP_ADD_TC(tp, traceme2); 7118 ATF_TP_ADD_TC(tp, traceme3); 7119 ATF_TP_ADD_TC(tp, traceme4); 7120 7121 ATF_TP_ADD_TC_HAVE_PID(tp, attach1); 7122 ATF_TP_ADD_TC_HAVE_PID(tp, attach2); 7123 ATF_TP_ADD_TC(tp, attach3); 7124 ATF_TP_ADD_TC(tp, attach4); 7125 ATF_TP_ADD_TC_HAVE_PID(tp, attach5); 7126 ATF_TP_ADD_TC_HAVE_PID(tp, attach6); 7127 ATF_TP_ADD_TC_HAVE_PID(tp, attach7); 7128 7129 ATF_TP_ADD_TC(tp, eventmask1); 7130 ATF_TP_ADD_TC(tp, eventmask2); 7131 ATF_TP_ADD_TC(tp, eventmask3); 7132 ATF_TP_ADD_TC(tp, eventmask4); 7133 ATF_TP_ADD_TC(tp, eventmask5); 7134 ATF_TP_ADD_TC(tp, eventmask6); 7135 7136 ATF_TP_ADD_TC_HAVE_PID(tp, fork1); 7137 ATF_TP_ADD_TC(tp, fork2); 7138 7139 ATF_TP_ADD_TC_HAVE_PID(tp, vfork1); 7140 ATF_TP_ADD_TC(tp, vfork2); 7141 7142 ATF_TP_ADD_TC(tp, vforkdone1); 7143 ATF_TP_ADD_TC(tp, vforkdone2); 7144 7145 ATF_TP_ADD_TC(tp, io_read_d1); 7146 ATF_TP_ADD_TC(tp, io_read_d2); 7147 ATF_TP_ADD_TC(tp, io_read_d3); 7148 ATF_TP_ADD_TC(tp, io_read_d4); 7149 7150 ATF_TP_ADD_TC(tp, io_write_d1); 7151 ATF_TP_ADD_TC(tp, io_write_d2); 7152 ATF_TP_ADD_TC(tp, io_write_d3); 7153 ATF_TP_ADD_TC(tp, io_write_d4); 7154 7155 ATF_TP_ADD_TC(tp, read_d1); 7156 ATF_TP_ADD_TC(tp, read_d2); 7157 ATF_TP_ADD_TC(tp, read_d3); 7158 ATF_TP_ADD_TC(tp, read_d4); 7159 7160 ATF_TP_ADD_TC(tp, write_d1); 7161 ATF_TP_ADD_TC(tp, write_d2); 7162 ATF_TP_ADD_TC(tp, write_d3); 7163 ATF_TP_ADD_TC(tp, write_d4); 7164 7165 ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake1); 7166 ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake2); 7167 7168 ATF_TP_ADD_TC(tp, read_d_write_d_handshake1); 7169 ATF_TP_ADD_TC(tp, read_d_write_d_handshake2); 7170 7171 ATF_TP_ADD_TC(tp, io_read_i1); 7172 ATF_TP_ADD_TC(tp, io_read_i2); 7173 ATF_TP_ADD_TC(tp, io_read_i3); 7174 ATF_TP_ADD_TC(tp, io_read_i4); 7175 7176 ATF_TP_ADD_TC(tp, read_i1); 7177 ATF_TP_ADD_TC(tp, read_i2); 7178 ATF_TP_ADD_TC(tp, read_i3); 7179 ATF_TP_ADD_TC(tp, read_i4); 7180 7181 ATF_TP_ADD_TC(tp, io_read_auxv1); 7182 7183 ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1); 7184 ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2); 7185 ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3); 7186 ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4); 7187 ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5); 7188 7189 ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1); 7190 ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2); 7191 7192 ATF_TP_ADD_TC_PT_STEP(tp, step1); 7193 ATF_TP_ADD_TC_PT_STEP(tp, step2); 7194 ATF_TP_ADD_TC_PT_STEP(tp, step3); 7195 ATF_TP_ADD_TC_PT_STEP(tp, step4); 7196 7197 ATF_TP_ADD_TC_PT_STEP(tp, setstep1); 7198 ATF_TP_ADD_TC_PT_STEP(tp, setstep2); 7199 ATF_TP_ADD_TC_PT_STEP(tp, setstep3); 7200 ATF_TP_ADD_TC_PT_STEP(tp, setstep4); 7201 7202 ATF_TP_ADD_TC(tp, kill1); 7203 ATF_TP_ADD_TC(tp, kill2); 7204 7205 ATF_TP_ADD_TC(tp, lwpinfo1); 7206 ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2); 7207 7208 ATF_TP_ADD_TC(tp, siginfo1); 7209 ATF_TP_ADD_TC(tp, siginfo2); 7210 ATF_TP_ADD_TC(tp, siginfo3); 7211 ATF_TP_ADD_TC(tp, siginfo4); 7212 ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5); 7213 ATF_TP_ADD_TC_PT_STEP(tp, siginfo6); 7214 7215 ATF_TP_ADD_TC(tp, lwp_create1); 7216 7217 ATF_TP_ADD_TC(tp, lwp_exit1); 7218 7219 ATF_TP_ADD_TC(tp, signal1); 7220 ATF_TP_ADD_TC(tp, signal2); 7221 ATF_TP_ADD_TC(tp, signal3); 7222 ATF_TP_ADD_TC_PT_STEP(tp, signal4); 7223 ATF_TP_ADD_TC(tp, signal5); 7224 ATF_TP_ADD_TC_HAVE_PID(tp, signal6); 7225 ATF_TP_ADD_TC_HAVE_PID(tp, signal7); 7226 ATF_TP_ADD_TC(tp, signal8); 7227 ATF_TP_ADD_TC(tp, signal9); 7228 ATF_TP_ADD_TC(tp, signal10); 7229 7230 ATF_TP_ADD_TC(tp, suspend1); 7231 ATF_TP_ADD_TC(tp, suspend2); 7232 7233 ATF_TP_ADD_TC(tp, resume1); 7234 7235 ATF_TP_ADD_TC(tp, syscall1); 7236 7237 ATF_TP_ADD_TC(tp, syscallemu1); 7238 7239 ATF_TP_ADD_TC_HAVE_PID(tp, race1); 7240 7241 ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64(); 7242 ATF_TP_ADD_TCS_PTRACE_WAIT_I386(); 7243 ATF_TP_ADD_TCS_PTRACE_WAIT_X86(); 7244 7245 return atf_no_error(); 7246 } 7247