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 Refactor the attach[1-8] and race1 ATF t_...



details:   https://anonhg.NetBSD.org/src/rev/5eb831a3118a
branches:  trunk
changeset: 322898:5eb831a3118a
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed May 23 01:29:43 2018 +0000

description:
Refactor the attach[1-8] and race1 ATF t_ptrace_wait* tests

Share code in: attach1, attach2 and race1 in the same function body.
Rename thsee tests to more verbose names:
 - tracer_sees_terminaton_before_the_parent
 - tracer_sysctl_lookup_without_duplicates
 - unrelated_tracer_sees_terminaton_before_the_parent

Rename attach3 to parent_attach_to_its_child.

Rename attach4 to child_attach_to_its_parent.

Share code in attach5, attach6 and attach7 in the same function body and
reanem the tests to:
 - tracee_sees_its_original_parent_getppid
 - tracee_sees_its_original_parent_sysctl_kinfo_proc2
 - tracee_sees_its_original_parent_procfs_status

Reduce the code by around 50%.

No functional change intended. All tests pass.

Sponsored by <The NetBSD Foundation>

diffstat:

 tests/lib/libc/sys/t_ptrace_wait.c |  644 ++++++++++++------------------------
 1 files changed, 217 insertions(+), 427 deletions(-)

diffs (truncated from 779 to 300 lines):

diff -r 52a165edad0e -r 5eb831a3118a tests/lib/libc/sys/t_ptrace_wait.c
--- a/tests/lib/libc/sys/t_ptrace_wait.c        Wed May 23 01:05:47 2018 +0000
+++ b/tests/lib/libc/sys/t_ptrace_wait.c        Wed May 23 01:29:43 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.50 2018/05/22 10:48:06 kamil Exp $ */
+/*     $NetBSD: t_ptrace_wait.c,v 1.51 2018/05/23 01:29:43 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.50 2018/05/22 10:48:06 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.51 2018/05/23 01:29:43 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -852,16 +852,14 @@
 /// ----------------------------------------------------------------------------
 
 #if defined(TWAIT_HAVE_PID)
-ATF_TC(attach1);
-ATF_TC_HEAD(attach1, tc)
-{
-       atf_tc_set_md_var(tc, "descr",
-           "Assert that tracer sees process termination before the parent");
-}
-
 static void
-attach1_raw(bool raw)
-{
+tracer_sees_terminaton_before_the_parent_raw(bool notimeout, bool unrelated)
+{
+       /*
+        * notimeout - disable timeout in await zombie function
+        * unrelated - attach from unrelated tracer reparented to initproc
+        */
+
        struct msg_fds parent_tracee, parent_tracer;
        const int exitval_tracee = 5;
        const int exitval_tracer = 10;
@@ -884,6 +882,13 @@
        SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
        tracer = atf_utils_fork();
        if (tracer == 0) {
+               if(unrelated) {
+                       /* Fork again and drop parent to reattach to PID 1 */
+                       tracer = atf_utils_fork();
+                       if (tracer != 0)
+                               _exit(exitval_tracer);
+               }
+
                DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
                FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
 
@@ -910,7 +915,21 @@
                DPRINTF("Tracee %d exited with %d\n", tracee, exitval_tracee);
 
                DPRINTF("Before exiting of the tracer process\n");
-               _exit(exitval_tracer);
+               _exit(unrelated ? 0 /* collect by initproc */ : exitval_tracer);
+       }
+
+       if (unrelated) {
+               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_tracer);
+
+               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");
@@ -920,7 +939,7 @@
        PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
 
        DPRINTF("Detect that tracee is zombie\n");
-       if (raw)
+       if (notimeout)
                await_zombie_raw(tracee, 0);
        else
                await_zombie(tracee);
@@ -931,21 +950,26 @@
        TWAIT_REQUIRE_SUCCESS(
            wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
 
-       DPRINTF("Tell the tracer child should have exited\n");
-       PARENT_TO_CHILD("wait for tracee exit", parent_tracer,  msg);
-       DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
-           TWAIT_FNAME);
-
-       DPRINTF("Wait from tracer child to complete waiting for tracee\n");
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
-           tracer);
-
-       validate_status_exited(status, exitval_tracer);
+       if (unrelated) {
+               DPRINTF("Resume the tracer and let it detect exited tracee\n");
+               PARENT_TO_CHILD("Message 2", parent_tracer, msg);
+       } else {
+               DPRINTF("Tell the tracer child should have exited\n");
+               PARENT_TO_CHILD("wait for tracee exit", parent_tracer,  msg);
+               DPRINTF("Wait for tracer to finish its job and exit - calling "
+                       "%s()\n", TWAIT_FNAME);
+
+               DPRINTF("Wait from tracer child to complete waiting for "
+                       "tracee\n");
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+                   tracer);
+
+               validate_status_exited(status, exitval_tracer);
+       }
 
        DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
            TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
-           tracee);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
 
        validate_status_exited(status, exitval_tracee);
 
@@ -953,126 +977,76 @@
        msg_close(&parent_tracee);
 }
 
-ATF_TC_BODY(attach1, tc)
-{
-
-       /* Reuse this test with race1 */
-       attach1_raw(false);
-}
-
-#endif
-
-#if defined(TWAIT_HAVE_PID)
-ATF_TC(attach2);
-ATF_TC_HEAD(attach2, tc)
+ATF_TC(tracer_sees_terminaton_before_the_parent);
+ATF_TC_HEAD(tracer_sees_terminaton_before_the_parent, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Assert that tracer sees process termination before the parent");
+}
+
+ATF_TC_BODY(tracer_sees_terminaton_before_the_parent, tc)
+{
+
+       tracer_sees_terminaton_before_the_parent_raw(false, false);
+}
+
+ATF_TC(tracer_sysctl_lookup_without_duplicates);
+ATF_TC_HEAD(tracer_sysctl_lookup_without_duplicates, tc)
 {
        atf_tc_set_md_var(tc, "descr",
-           "Assert that any tracer sees process termination before its "
-           "parent");
-}
-
-ATF_TC_BODY(attach2, tc)
-{
-       struct msg_fds parent_tracer, parent_tracee;
-       const int exitval_tracee = 5;
-       const int exitval_tracer1 = 10, exitval_tracer2 = 20;
-       pid_t tracee, tracer, wpid;
-       uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
-#if defined(TWAIT_HAVE_STATUS)
-       int status;
-#endif
-
-       DPRINTF("Spawn tracee\n");
-       SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
-       tracee = atf_utils_fork();
-       if (tracee == 0) {
-               /* Wait for message from the parent */
-               CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
-               _exit(exitval_tracee);
+           "Assert that await_zombie() in attach1 always finds a single "
+           "process and no other error is reported");
+}
+
+ATF_TC_BODY(tracer_sysctl_lookup_without_duplicates, tc)
+{
+       time_t start, end;
+       double diff;
+       unsigned long N = 0;
+
+       /*
+        * Reuse this test with tracer_sees_terminaton_before_the_parent_raw().
+        * This test body isn't specific to this race, however it's just good
+        * enough for this purposes, no need to invent a dedicated code flow.
+        */
+
+       start = time(NULL);
+       while (true) {
+               DPRINTF("Step: %lu\n", N);
+               tracer_sees_terminaton_before_the_parent_raw(true, false);
+               end = time(NULL);
+               diff = difftime(end, start);
+               if (diff >= 5.0)
+                       break;
+               ++N;
        }
-
-       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_tracer1);
-
-               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);
-
-               /* 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("Message 1", parent_tracer, msg);
-               CHILD_FROM_PARENT("Message 2", parent_tracer, msg);
-
-               /* Wait for tracee and assert that it exited */
-               FORKEE_REQUIRE_SUCCESS(
-                   wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
-
-               forkee_status_exited(status, exitval_tracee);
-
-               DPRINTF("Before exiting of the tracer process\n");
-               _exit(exitval_tracer2);
-       }
-       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_tracer1);
-
-       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("Message 1", parent_tracer, msg);
-       DPRINTF("Resume the tracee and let it exit\n");
-       PARENT_TO_CHILD("Message 1", parent_tracee, msg);
-
-       DPRINTF("Detect that tracee is zombie\n");
-       await_zombie(tracee);
-
-       DPRINTF("Assert that there is no status about tracee - "
-           "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(
-           wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
-
-       DPRINTF("Resume the tracer and let it detect exited tracee\n");
-       PARENT_TO_CHILD("Message 2", parent_tracer, msg);
-
-       DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
-           TWAIT_FNAME);
-       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0),
-           tracee);
-
-       validate_status_exited(status, exitval_tracee);
-
-       msg_close(&parent_tracer);
-       msg_close(&parent_tracee);
+       DPRINTF("Iterations: %lu\n", N);
+}
+
+ATF_TC(unrelated_tracer_sees_terminaton_before_the_parent);
+ATF_TC_HEAD(unrelated_tracer_sees_terminaton_before_the_parent, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Assert that tracer sees process termination before the parent");
+}
+
+ATF_TC_BODY(unrelated_tracer_sees_terminaton_before_the_parent, tc)
+{
+
+       tracer_sees_terminaton_before_the_parent_raw(false, true);
 }
 #endif
 
-ATF_TC(attach3);
-ATF_TC_HEAD(attach3, tc)
+/// ----------------------------------------------------------------------------



Home | Main Index | Thread Index | Old Index