Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/sys Add new fork/vfork/posix_spawn ATF tests ...



details:   https://anonhg.NetBSD.org/src/rev/57af7d34fcfc
branches:  trunk
changeset: 1006833:57af7d34fcfc
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Jan 29 03:51:56 2020 +0000

description:
Add new fork/vfork/posix_spawn ATF tests in t_ptrace_wait*

Add unrelated tracer variation of tests: fork1-16, vfork1-16,
posix_spawn1-16.

All tests pass.

diffstat:

 tests/lib/libc/sys/t_ptrace_wait.c |  423 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 421 insertions(+), 2 deletions(-)

diffs (truncated from 451 to 300 lines):

diff -r 8975fab677a6 -r 57af7d34fcfc tests/lib/libc/sys/t_ptrace_wait.c
--- a/tests/lib/libc/sys/t_ptrace_wait.c        Wed Jan 29 03:17:34 2020 +0000
+++ b/tests/lib/libc/sys/t_ptrace_wait.c        Wed Jan 29 03:51:56 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.148 2020/01/23 06:17:21 martin Exp $       */
+/*     $NetBSD: t_ptrace_wait.c,v 1.149 2020/01/29 03:51:56 kamil Exp $        */
 
 /*-
  * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.148 2020/01/23 06:17:21 martin Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.149 2020/01/29 03:51:56 kamil Exp $");
 
 #define __LEGACY_PT_LWPINFO
 
@@ -3312,6 +3312,374 @@
 
 #if defined(TWAIT_HAVE_PID)
 static void
+unrelated_tracer_fork_body(const char *fn, bool trackspawn, bool trackfork,
+    bool trackvfork, bool trackvforkdone)
+{
+       const int sigval = SIGSTOP;
+       struct msg_fds parent_tracee, parent_tracer;
+       const int exitval = 10;
+       const int exitval2 = 0; /* This matched exit status from /bin/echo */
+       pid_t tracee, tracer, wpid;
+       pid_t tracee2 = 0;
+       uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+
+       struct ptrace_siginfo info;
+       ptrace_state_t state;
+       const int slen = sizeof(state);
+       ptrace_event_t event;
+       const int elen = sizeof(event);
+
+       char * const arg[] = { __UNCONST("/bin/echo"), NULL };
+
+       DPRINTF("Spawn tracee\n");
+       SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
+       tracee = atf_utils_fork();
+       if (tracee == 0) {
+               // Wait for parent to let us crash
+               CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
+
+               DPRINTF("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               if (strcmp(fn, "spawn") == 0) {
+                       FORKEE_ASSERT_EQ(posix_spawn(&tracee2,
+                           arg[0], NULL, NULL, arg, NULL), 0);
+               } else {
+                       if (strcmp(fn, "fork") == 0) {
+                               FORKEE_ASSERT((tracee2 = fork()) != -1);
+                       } else if (strcmp(fn, "vfork") == 0) {
+                               FORKEE_ASSERT((tracee2 = vfork()) != -1);
+                       }
+
+                       if (tracee2 == 0)
+                               _exit(exitval2);
+               }
+               FORKEE_REQUIRE_SUCCESS
+                   (wpid = TWAIT_GENERIC(tracee2, &status, 0), tracee2);
+
+               forkee_status_exited(status, exitval2);
+
+               DPRINTF("Before exiting of the child process\n");
+               _exit(exitval);
+       }
+
+       DPRINTF("Spawn debugger\n");
+       SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
+       tracer = atf_utils_fork();
+       if (tracer == 0) {
+               /* Fork again and drop parent to reattach to PID 1 */
+               tracer = atf_utils_fork();
+               if (tracer != 0)
+                       _exit(exitval);
+
+               DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+               /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+               FORKEE_REQUIRE_SUCCESS(
+                   wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+               forkee_status_stopped(status, SIGSTOP);
+
+               DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the "
+                   "traced process\n");
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
+
+               DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+               DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
+                   "si_errno=%#x\n", info.psi_siginfo.si_signo,
+                   info.psi_siginfo.si_code, info.psi_siginfo.si_errno);
+
+               FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, SIGSTOP);
+               FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_USER);
+
+               /* Resume tracee with PT_CONTINUE */
+               FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+               /* Inform parent that tracer has attached to tracee */
+               CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
+
+               /* Wait for parent to tell use that tracee should have exited */
+               CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
+
+               /* Wait for tracee and assert that it exited */
+               FORKEE_REQUIRE_SUCCESS(
+                   wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+               forkee_status_stopped(status, sigval);
+
+               DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the "
+                   "traced process\n");
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_SIGINFO, tracee, &info, sizeof(info)) != -1);
+
+               DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+               DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
+                   "si_errno=%#x\n", info.psi_siginfo.si_signo,
+                   info.psi_siginfo.si_code, info.psi_siginfo.si_errno);
+
+               FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, sigval);
+               FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+               DPRINTF("Set 0%s%s%s%s in EVENT_MASK for the child %d\n",
+                   trackspawn ? "|PTRACE_POSIX_SPAWN" : "",
+                   trackfork ? "|PTRACE_FORK" : "",
+                   trackvfork ? "|PTRACE_VFORK" : "",
+                   trackvforkdone ? "|PTRACE_VFORK_DONE" : "", tracee);
+               event.pe_set_event = 0;
+               if (trackspawn)
+                       event.pe_set_event |= PTRACE_POSIX_SPAWN;
+               if (trackfork)
+                       event.pe_set_event |= PTRACE_FORK;
+               if (trackvfork)
+                       event.pe_set_event |= PTRACE_VFORK;
+               if (trackvforkdone)
+                       event.pe_set_event |= PTRACE_VFORK_DONE;
+               SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, tracee, &event, elen)
+                   != -1);
+
+               DPRINTF("Before resuming the child process where it left off "
+                   "and without signal to be sent\n");
+               SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+               if ((trackspawn && strcmp(fn, "spawn") == 0) ||
+                   (trackfork && strcmp(fn, "fork") == 0) ||
+                   (trackvfork && strcmp(fn, "vfork") == 0)) {
+                       DPRINTF("Before calling %s() for the tracee %d\n", TWAIT_FNAME,
+                           tracee);
+                       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0),
+                           tracee);
+
+                       validate_status_stopped(status, SIGTRAP);
+
+                       SYSCALL_REQUIRE(
+                           ptrace(PT_GET_PROCESS_STATE, tracee, &state, slen) != -1);
+                       if (trackspawn && strcmp(fn, "spawn") == 0) {
+                               ATF_REQUIRE_EQ(
+                                   state.pe_report_event & PTRACE_POSIX_SPAWN,
+                                      PTRACE_POSIX_SPAWN);
+                       }
+                       if (trackfork && strcmp(fn, "fork") == 0) {
+                               ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
+                                      PTRACE_FORK);
+                       }
+                       if (trackvfork && strcmp(fn, "vfork") == 0) {
+                               ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
+                                      PTRACE_VFORK);
+                       }
+
+                       tracee2 = state.pe_other_pid;
+                       DPRINTF("Reported ptrace event with forkee %d\n", tracee2);
+
+                       DPRINTF("Before calling %s() for the forkee %d of the tracee "
+                           "%d\n", TWAIT_FNAME, tracee2, tracee);
+                       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee2, &status, 0),
+                           tracee2);
+
+                       validate_status_stopped(status, SIGTRAP);
+
+                       SYSCALL_REQUIRE(
+                           ptrace(PT_GET_PROCESS_STATE, tracee2, &state, slen) != -1);
+                       if (trackspawn && strcmp(fn, "spawn") == 0) {
+                               ATF_REQUIRE_EQ(
+                                   state.pe_report_event & PTRACE_POSIX_SPAWN,
+                                      PTRACE_POSIX_SPAWN);
+                       }
+                       if (trackfork && strcmp(fn, "fork") == 0) {
+                               ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
+                                      PTRACE_FORK);
+                       }
+                       if (trackvfork && strcmp(fn, "vfork") == 0) {
+                               ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
+                                      PTRACE_VFORK);
+                       }
+
+                       ATF_REQUIRE_EQ(state.pe_other_pid, tracee);
+
+                       DPRINTF("Before resuming the forkee process where it left off "
+                           "and without signal to be sent\n");
+                       SYSCALL_REQUIRE(
+                           ptrace(PT_CONTINUE, tracee2, (void *)1, 0) != -1);
+               
+                       DPRINTF("Before resuming the tracee process where it left off "
+                           "and without signal to be sent\n");
+                       SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+               }
+
+               if (trackvforkdone && strcmp(fn, "vfork") == 0) {
+                       DPRINTF("Before calling %s() for the tracee %d\n", TWAIT_FNAME,
+                           tracee);
+                       TWAIT_REQUIRE_SUCCESS(
+                           wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+                       validate_status_stopped(status, SIGTRAP);
+
+                       SYSCALL_REQUIRE(
+                           ptrace(PT_GET_PROCESS_STATE, tracee, &state, slen) != -1);
+                       ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
+
+                       tracee2 = state.pe_other_pid;
+                       DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
+                           tracee2);
+
+                       DPRINTF("Before resuming the tracee process where it left off "
+                           "and without signal to be sent\n");
+                       SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+               }
+
+
+               if ((trackspawn && strcmp(fn, "spawn") == 0) ||
+                   (trackfork && strcmp(fn, "fork") == 0) ||
+                   (trackvfork && strcmp(fn, "vfork") == 0)) {
+                       DPRINTF("Before calling %s() for the forkee - expected exited"
+                           "\n", TWAIT_FNAME);
+                       TWAIT_REQUIRE_SUCCESS(
+                           wpid = TWAIT_GENERIC(tracee2, &status, 0), tracee2);
+
+                       validate_status_exited(status, exitval2);
+
+                       DPRINTF("Before calling %s() for the forkee - expected no "
+                           "process\n", TWAIT_FNAME);
+                       TWAIT_REQUIRE_FAILURE(ECHILD,
+                           wpid = TWAIT_GENERIC(tracee2, &status, 0));
+               }
+
+               DPRINTF("Before calling %s() for the tracee - expected stopped "
+                   "SIGCHLD\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+               validate_status_stopped(status, SIGCHLD);
+
+               DPRINTF("Before resuming the tracee process where it left off and "
+                   "without signal to be sent\n");
+               SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+               DPRINTF("Before calling %s() for the tracee - expected exited\n",
+                   TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+               validate_status_exited(status, exitval);
+
+               /* Inform parent that tracer is exiting normally */
+               CHILD_TO_PARENT("tracer done", parent_tracer, msg);
+
+               DPRINTF("Before exiting of the tracer process\n");
+               _exit(0 /* collect by initproc */);
+       }
+
+       DPRINTF("Wait for the tracer process (direct child) to exit "
+           "calling %s()\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(
+           wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
+
+       validate_status_exited(status, exitval);
+
+       DPRINTF("Wait for the non-exited tracee process with %s()\n",
+           TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(
+           wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
+
+       DPRINTF("Wait for the tracer to attach to the tracee\n");
+       PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
+
+       DPRINTF("Resume the tracee and let it crash\n");
+       PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
+
+       DPRINTF("Resume the tracer and let it detect crashed tracee\n");



Home | Main Index | Thread Index | Old Index