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 ATF: ptrace: Merge code in fork2 and vfor...



details:   https://anonhg.NetBSD.org/src/rev/a81b0b7c1790
branches:  trunk
changeset: 321918:a81b0b7c1790
user:      kamil <kamil%NetBSD.org@localhost>
date:      Tue Apr 10 00:40:35 2018 +0000

description:
ATF: ptrace: Merge code in fork2 and vfork2 tests with (v)fork1 ones

Reduce code duplication, use the same function body with conditional
switches.

Sponsored by <The NetBSD Foundation>

diffstat:

 tests/lib/libc/sys/t_ptrace_wait.c |  318 +++++++++++-------------------------
 1 files changed, 102 insertions(+), 216 deletions(-)

diffs (truncated from 388 to 300 lines):

diff -r 37a380bd472c -r a81b0b7c1790 tests/lib/libc/sys/t_ptrace_wait.c
--- a/tests/lib/libc/sys/t_ptrace_wait.c        Tue Apr 10 00:09:31 2018 +0000
+++ b/tests/lib/libc/sys/t_ptrace_wait.c        Tue Apr 10 00:40:35 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.28 2018/04/10 00:09:31 kamil Exp $ */
+/*     $NetBSD: t_ptrace_wait.c,v 1.29 2018/04/10 00:40:35 kamil Exp $ */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.28 2018/04/10 00:09:31 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.29 2018/04/10 00:40:35 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -1321,9 +1321,8 @@
        TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
 }
 
-#if defined(TWAIT_HAVE_PID)
 static void
-fork_test(bool regular)
+fork_test(bool regular, bool track_event)
 {
        const int exitval = 5;
        const int exitval2 = 15;
@@ -1332,12 +1331,14 @@
 #if defined(TWAIT_HAVE_STATUS)
        int status;
 #endif
+#if defined(TWAIT_HAVE_PID)
        ptrace_state_t state;
        const int slen = sizeof(state);
+#endif
        ptrace_event_t event;
        const int elen = sizeof(event);
 
-       if (!regular) {
+       if (!regular && track_event) {
                atf_tc_expect_fail("PR kern/51630");
        }
 
@@ -1370,158 +1371,77 @@
 
        validate_status_stopped(status, sigval);
 
-       DPRINTF("Enable PTRACE_%sFORK in EVENT_MASK for the child %d\n",
-               regular ? "" : "V", child);
-       event.pe_set_event = regular ? PTRACE_FORK : PTRACE_VFORK;
+       DPRINTF("%s PTRACE_%sFORK in EVENT_MASK for the child %d\n",
+               track_event ? "Enable" : "Disable", regular ? "" : "V", child);
+       event.pe_set_event = track_event ? (regular ? PTRACE_FORK : PTRACE_VFORK) : 0;
        SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
 
        DPRINTF("Before resuming the child process where it left off and "
            "without signal to be sent\n");
-        DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
-               "pe_report_event=PTRACE_%sFORK, state.pe_other_pid=child2) and "
-               "for child2 (TRAP_CHLD, pe_report_event=PTRACE_%sFORK, "
-               "state.pe_other_pid=child)\n", child, regular ? "" : "V",
-              regular ? "" : "V");
-       SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-       DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-       validate_status_stopped(status, SIGTRAP);
-
-       SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
-       ATF_REQUIRE_EQ(state.pe_report_event,
-                      regular ? PTRACE_FORK : PTRACE_VFORK);
-
-       child2 = state.pe_other_pid;
-       DPRINTF("Reported PTRACE_%sFORK event with forkee %d\n",
-               regular ? "" : "V", child2);
-
-       DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
-           TWAIT_FNAME, child2, child);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
-           child2);
-
-       validate_status_stopped(status, SIGTRAP);
-
-       SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
-       ATF_REQUIRE_EQ(state.pe_report_event,
-                      regular ? PTRACE_FORK : PTRACE_VFORK);
-       ATF_REQUIRE_EQ(state.pe_other_pid, child);
-
-       DPRINTF("Before resuming the forkee process where it left off and "
-           "without signal to be sent\n");
-       SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
-
-       DPRINTF("Before resuming the child process where it left off and "
-           "without signal to be sent\n");
-       SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-       DPRINTF("Before calling %s() for the forkee - expected exited\n",
-           TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
-           child2);
-
-       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(child2, &status, 0));
-
-       DPRINTF("Before calling %s() for the child - expected stopped "
-           "SIGCHLD\n", TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-       validate_status_stopped(status, SIGCHLD);
-
-       DPRINTF("Before resuming the child process where it left off and "
-           "without signal to be sent\n");
+       if (track_event) {
+               DPRINTF("We expect two SIGTRAP events, for child %d "
+                       "(TRAP_CHLD, pe_report_event=PTRACE_%sFORK, "
+                       "state.pe_other_pid=child2) and for child2 "
+                       "(TRAP_CHLD, pe_report_event=PTRACE_%sFORK, "
+                       "state.pe_other_pid=child)\n", child,
+                       regular ? "" : "V", regular ? "" : "V");
+       }
        SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 
-       DPRINTF("Before calling %s() for the child - expected exited\n",
-           TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-       validate_status_exited(status, exitval);
-
-       DPRINTF("Before calling %s() for the child - expected no process\n",
-           TWAIT_FNAME);
-       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
-
-ATF_TC(fork1);
-ATF_TC_HEAD(fork1, tc)
-{
-       atf_tc_set_md_var(tc, "descr",
-           "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
-           "set to PTRACE_FORK");
-}
-
-ATF_TC_BODY(fork1, tc)
-{
-
-       fork_test(true);
-}
+#if defined(TWAIT_HAVE_PID)
+       if (track_event) {
+               DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
+                       child);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+                                     child);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
+                                      slen) != -1);
+               ATF_REQUIRE_EQ(state.pe_report_event,
+                              regular ? PTRACE_FORK : PTRACE_VFORK);
+
+               child2 = state.pe_other_pid;
+               DPRINTF("Reported PTRACE_%sFORK event with forkee %d\n",
+                       regular ? "" : "V", child2);
+
+               DPRINTF("Before calling %s() for the forkee %d of the child "
+                       "%d\n", TWAIT_FNAME, child2, child);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+                   child2);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state,
+                                      slen) != -1);
+               ATF_REQUIRE_EQ(state.pe_report_event,
+                              regular ? PTRACE_FORK : PTRACE_VFORK);
+               ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+               DPRINTF("Before resuming the forkee process where it left off "
+                   "and without signal to be sent\n");
+               SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0)
+                               != -1);
+
+               DPRINTF("Before resuming the child process where it left off "
+                       "and without signal to be sent\n");
+               SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+               DPRINTF("Before calling %s() for the forkee - expected exited"
+                       "\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+                   child2);
+
+               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(child2, &status, 0));
+       }
 #endif
 
-ATF_TC(fork2);
-ATF_TC_HEAD(fork2, tc)
-{
-       atf_tc_set_md_var(tc, "descr",
-           "Verify that fork(2) is not intercepted by ptrace(2) with empty "
-           "EVENT_MASK");
-}
-
-ATF_TC_BODY(fork2, tc)
-{
-       const int exitval = 5;
-       const int exitval2 = 15;
-       const int sigval = SIGSTOP;
-       pid_t child, child2, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-       int status;
-#endif
-       ptrace_event_t event;
-       const int elen = sizeof(event);
-
-       DPRINTF("Before forking process PID=%d\n", getpid());
-       SYSCALL_REQUIRE((child = fork()) != -1);
-       if (child == 0) {
-               DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
-
-               DPRINTF("Before raising %s from child\n", strsignal(sigval));
-               FORKEE_ASSERT(raise(sigval) == 0);
-
-               FORKEE_ASSERT((child2 = fork()) != -1);
-
-               if (child2 == 0)
-                       _exit(exitval2);
-
-               FORKEE_REQUIRE_SUCCESS
-                   (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
-
-               forkee_status_exited(status, exitval2);
-
-               DPRINTF("Before exiting of the child process\n");
-               _exit(exitval);
-       }
-       DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-       DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-       validate_status_stopped(status, sigval);
-
-       DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
-       event.pe_set_event = 0;
-       SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &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, child, (void *)1, 0) != -1);
-
        DPRINTF("Before calling %s() for the child - expected stopped "
            "SIGCHLD\n", TWAIT_FNAME);
        TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
@@ -1544,6 +1464,36 @@
 }
 
 #if defined(TWAIT_HAVE_PID)
+ATF_TC(fork1);
+ATF_TC_HEAD(fork1, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
+           "set to PTRACE_FORK");
+}
+
+ATF_TC_BODY(fork1, tc)
+{
+
+       fork_test(true, true);
+}
+#endif
+
+ATF_TC(fork2);
+ATF_TC_HEAD(fork2, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify that fork(2) is not intercepted by ptrace(2) with empty "
+           "EVENT_MASK");
+}
+
+ATF_TC_BODY(fork2, tc)
+{
+
+       fork_test(true, false);
+}
+



Home | Main Index | Thread Index | Old Index