Source-Changes-HG archive

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

[src/trunk]: src/tests/kernel Add new test attach1 in t_ptrace



details:   https://anonhg.NetBSD.org/src/rev/1f292556764e
branches:  trunk
changeset: 348748:1f292556764e
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Nov 05 01:49:53 2016 +0000

description:
Add new test attach1 in t_ptrace

This test asserts that tracer sees process termination before the parent

Currently this is not true for NetBSD.

The problem has been reported in gnats as kern/51600.

Originally an early version of this test triggered kernel panic, that
was fixed by Christos Zoulas -- thanks! Appropriate fixes are as follows:
http://mail-index.netbsd.org/source-changes/2016/11/04/msg078868.html
http://mail-index.netbsd.org/source-changes/2016/11/04/msg078869.html

Mark this test as expected failure and linked with proper PR.

As an interesting note, this test uses pipe(2) to perform IPC. Use for
messages uint8_t message to be sure that it will never by transmitted
partially from a caller to a callee, this assumption simplifies the code.

Add local function await_zombie() that takes process identifier (pid).
This function uses the sysctl(7) interface to probe p_stat of a requested
process and compares it with LSZOMB.

Try to keep closing all unneeded file descriptors for pipes in order to not
run out of fds later.

Sponsored by <The NetBSD Foundation>.

diffstat:

 tests/kernel/t_ptrace.c |  166 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 164 insertions(+), 2 deletions(-)

diffs (208 lines):

diff -r fdfe06688579 -r 1f292556764e tests/kernel/t_ptrace.c
--- a/tests/kernel/t_ptrace.c   Sat Nov 05 01:09:30 2016 +0000
+++ b/tests/kernel/t_ptrace.c   Sat Nov 05 01:49:53 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace.c,v 1.8 2016/11/04 09:08:11 kamil Exp $       */
+/*     $NetBSD: t_ptrace.c,v 1.9 2016/11/05 01:49:53 kamil Exp $       */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,15 +27,17 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace.c,v 1.8 2016/11/04 09:08:11 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace.c,v 1.9 2016/11/05 01:49:53 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/ptrace.h>
+#include <sys/sysctl.h>
 #include <sys/wait.h>
 #include <err.h>
 #include <errno.h>
 #include <signal.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -68,6 +70,34 @@
                     __FILE__, __LINE__, __func__, #x);                         \
 } while (0)
 
+/* This function is currently designed to be run in the main/parent process */
+static void
+await_zombie(pid_t process)
+{
+       struct kinfo_proc2 p;
+       size_t len = sizeof(p);
+
+       const int name[] = {
+               [0] = CTL_KERN,
+               [1] = KERN_PROC2,
+               [2] = KERN_PROC_PID,
+               [3] = process,
+               [4] = sizeof(p),
+               [5] = 1
+       };
+
+       const size_t namelen = __arraycount(name);
+
+       /* Await the process becoming a zombie */
+       while(1) {
+               ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
+
+               if (p.p_stat == LSZOMB)
+                       break;
+
+               ATF_REQUIRE(usleep(1000) == 0);
+       }
+}
 
 ATF_TC(traceme1);
 ATF_TC_HEAD(traceme1, tc)
@@ -467,12 +497,144 @@
        ATF_REQUIRE(errno == ECHILD);
 }
 
+ATF_TC(attach1);
+ATF_TC_HEAD(attach1, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Assert that tracer sees process termination before the parent");
+}
+
+ATF_TC_BODY(attach1, tc)
+{
+       int fds_totracee[2], fds_totracer[2], fds_fromtracer[2];
+       int status, rv;
+       const int exitval_tracee = 5;
+       const int exitval_tracer = 10;
+       pid_t tracee, tracer, wpid;
+       uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+
+       /* XXX: Linux&FreeBSD and NetBSD have different behavior here.
+        * Tracer on NetBSD isn't promoted to observe - before child's parent -
+        * that tracee exited (and became zombie).
+        */
+       atf_tc_expect_fail("PR kern/51600");
+
+       printf("Spawn tracee\n");
+       ATF_REQUIRE(pipe(fds_totracee) == 0);
+       ATF_REQUIRE((tracee = fork()) != -1);
+       if (tracee == 0) {
+               FORKEE_ASSERT(close(fds_totracee[1]) == 0);
+
+               /* Wait for message from the parent */
+               rv = read(fds_totracee[0], &msg, sizeof(msg));
+               FORKEE_ASSERT(rv == sizeof(msg));
+
+               _exit(exitval_tracee);
+       }
+       ATF_REQUIRE(close(fds_totracee[0]) == 0);
+
+       printf("Spawn debugger\n");
+       ATF_REQUIRE(pipe(fds_totracer) == 0);
+       ATF_REQUIRE(pipe(fds_fromtracer) == 0);
+       ATF_REQUIRE((tracer = fork()) != -1);
+       if (tracer == 0) {
+               /* No IPC to communicate with the child */
+               FORKEE_ASSERT(close(fds_totracee[1]) == 0);
+
+               FORKEE_ASSERT(close(fds_totracer[1]) == 0);
+               FORKEE_ASSERT(close(fds_fromtracer[0]) == 0);
+
+               FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+               /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+               wpid = waitpid(tracee, &status, 0);
+               FORKEE_ASSERTX(wpid == tracee);
+               FORKEE_ASSERTX(!WIFEXITED(status));
+               FORKEE_ASSERTX(!WIFCONTINUED(status));
+               FORKEE_ASSERTX(!WIFSIGNALED(status));
+               FORKEE_ASSERTX(WIFSTOPPED(status));
+               FORKEE_ASSERTX(WSTOPSIG(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 */
+               rv = write(fds_fromtracer[1], &msg, sizeof(msg));
+               FORKEE_ASSERT(rv == sizeof(msg));
+
+               /* Wait for parent */
+               rv = read(fds_totracer[0], &msg, sizeof(msg));
+               FORKEE_ASSERT(rv == sizeof(msg));
+
+               /* Wait for tracee and assert that it exited */
+               wpid = waitpid(tracee, &status, 0);
+               FORKEE_ASSERTX(wpid == tracee);
+               FORKEE_ASSERTX(WIFEXITED(status));
+               FORKEE_ASSERTX(!WIFCONTINUED(status));
+               FORKEE_ASSERTX(!WIFSIGNALED(status));
+               FORKEE_ASSERTX(!WIFSTOPPED(status));
+               FORKEE_ASSERTX(WEXITSTATUS(status) == exitval_tracee);
+
+               _exit(exitval_tracer);
+       }
+       ATF_REQUIRE(close(fds_totracer[0]) == 0);
+       ATF_REQUIRE(close(fds_fromtracer[1]) == 0);
+
+       printf("Wait for the tracer to attach to the tracee\n");
+       rv = read(fds_fromtracer[0], &msg, sizeof(msg));
+       ATF_REQUIRE(rv == sizeof(msg));
+
+       printf("Resume the tracee and let it exit\n");
+       rv = write(fds_totracee[1], &msg, sizeof(msg));
+       ATF_REQUIRE(rv == sizeof(msg));
+
+       printf("fds_totracee is no longer needed - close it\n");
+       ATF_REQUIRE(close(fds_totracee[1]) == 0);
+
+       printf("Detect that tracee is zombie\n");
+       await_zombie(tracee);
+
+       printf("Assert that there is no status about tracee - "
+           "Tracer must detect zombie first\n");
+       wpid = waitpid(tracee, &status, WNOHANG);
+       ATF_REQUIRE(wpid == 0);
+
+       printf("Resume the tracer and let it detect exited tracee\n");
+       rv = write(fds_totracer[1], &msg, sizeof(msg));
+       ATF_REQUIRE(rv == sizeof(msg));
+
+       printf("Wait for tracer to finish its job and exit\n");
+       wpid = waitpid(tracer, &status, 0);
+       ATF_REQUIRE(wpid == tracer);
+       ATF_REQUIRE(WIFEXITED(status));
+       ATF_REQUIRE(!WIFCONTINUED(status));
+       ATF_REQUIRE(!WIFSIGNALED(status));
+       ATF_REQUIRE(!WIFSTOPPED(status));
+       ATF_REQUIRE(WEXITSTATUS(status) == exitval_tracer);
+
+       printf("Wait for tracer to finish its job and exit\n");
+       wpid = waitpid(tracee, &status, WNOHANG);
+       ATF_REQUIRE(wpid == tracee);
+       ATF_REQUIRE(WIFEXITED(status));
+       ATF_REQUIRE(!WIFCONTINUED(status));
+       ATF_REQUIRE(!WIFSIGNALED(status));
+       ATF_REQUIRE(!WIFSTOPPED(status));
+       ATF_REQUIRE(WEXITSTATUS(status) == exitval_tracee);
+
+       printf("fds_fromtracer is no longer needed - close it\n");
+       ATF_REQUIRE(close(fds_fromtracer[0]) == 0);
+
+       printf("fds_totracer is no longer needed - close it\n");
+       ATF_REQUIRE(close(fds_totracer[1]) == 0);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
        ATF_TP_ADD_TC(tp, traceme1);
        ATF_TP_ADD_TC(tp, traceme2);
        ATF_TP_ADD_TC(tp, traceme3);
        ATF_TP_ADD_TC(tp, traceme4);
+       ATF_TP_ADD_TC(tp, attach1);
 
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index