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 tests lwpinfo1 in t_ptrace_wait* and lw...



details:   https://anonhg.NetBSD.org/src/rev/b96f75545e91
branches:  trunk
changeset: 349271:b96f75545e91
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Dec 03 07:23:08 2016 +0000

description:
Add new tests lwpinfo1 in t_ptrace_wait* and lwpinfo2 under HAVE_PID guard

lwpinfo1:
    Verify baic LWPINFO call for single thread (PT_TRACE_ME)

lwpinfo2:
    Verify baic LWPINFO call for single thread (PT_ATTACH from tracer)

Both tests are marked as expected failure PR kern/51685:
    ptrace(2): Signal does not set PL_EVENT_SIGNAL in
    (struct ptrace_lwpinfo.)pl_event

Sponsored by <The NetBSD Foundation>

diffstat:

 tests/kernel/t_ptrace_wait.c |  228 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 226 insertions(+), 2 deletions(-)

diffs (255 lines):

diff -r a762e644c85a -r b96f75545e91 tests/kernel/t_ptrace_wait.c
--- a/tests/kernel/t_ptrace_wait.c      Sat Dec 03 04:06:25 2016 +0000
+++ b/tests/kernel/t_ptrace_wait.c      Sat Dec 03 07:23:08 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.37 2016/12/01 20:11:17 kamil Exp $ */
+/*     $NetBSD: t_ptrace_wait.c,v 1.38 2016/12/03 07:23:08 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.37 2016/12/01 20:11:17 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.38 2016/12/03 07:23:08 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -4620,6 +4620,227 @@
        TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
 }
 
+ATF_TC(lwpinfo1);
+ATF_TC_HEAD(lwpinfo1, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify baic LWPINFO call for single thread (PT_TRACE_ME)");
+}
+
+ATF_TC_BODY(lwpinfo1, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       struct ptrace_lwpinfo info = {0};
+
+       /*
+        * ptrace(2): Signal does not set PL_EVENT_SIGNAL in
+        * (struct ptrace_lwpinfo.)pl_event
+        */
+       atf_tc_expect_fail("PR kern/51685");
+
+       printf("Before forking process PID=%d\n", getpid());
+       child = atf_utils_fork();
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               printf("Before exiting of the child process\n");
+               _exit(exitval);
+       }
+       printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigval);
+
+       printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Assert that there exists a thread\n");
+       ATF_REQUIRE(info.pl_lwpid > 0);
+
+       printf("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
+           info.pl_lwpid);
+       ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
+           "Received event %d != expected event %d",
+           info.pl_event, PL_EVENT_SIGNAL);
+
+       printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Assert that there are no more lwp threads in child\n");
+       ATF_REQUIRE_EQ(info.pl_lwpid, 0);
+
+       printf("Before resuming the child process where it left off and "
+           "without signal to be sent\n");
+       ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_exited(status, exitval);
+
+       printf("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(lwpinfo2);
+ATF_TC_HEAD(lwpinfo2, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify baic LWPINFO call for single thread (PT_ATTACH from "
+           "tracer)");
+}
+
+ATF_TC_BODY(lwpinfo2, tc)
+{
+       int fds_totracee[2], fds_totracer[2], fds_fromtracer[2];
+       int 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) */
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       struct ptrace_lwpinfo info = {0};
+
+       /*
+        * ptrace(2): Signal does not set PL_EVENT_SIGNAL in
+        * (struct ptrace_lwpinfo.)pl_event
+        */
+       atf_tc_expect_fail("PR kern/51685");
+
+       printf("Spawn tracee\n");
+       ATF_REQUIRE(pipe(fds_totracee) == 0);
+       tracee = atf_utils_fork();
+       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);
+       tracer = atf_utils_fork();
+       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);
+
+               printf("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);
+
+               printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+               FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
+                   != -1);
+
+               printf("Assert that there exists a thread\n");
+               FORKEE_ASSERTX(info.pl_lwpid > 0);
+
+               printf("Assert that lwp thread %d received event "
+                   "PL_EVENT_SIGNAL\n", info.pl_lwpid);
+               FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
+
+               printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+               FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
+                   != -1);
+
+               printf("Assert that there are no more lwp threads in child\n");
+               FORKEE_ASSERTX(info.pl_lwpid == 0);
+
+               /* 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 */
+               FORKEE_REQUIRE_SUCCESS(
+                   wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+               forkee_status_exited(status, exitval_tracee);
+
+               printf("Before exiting of the tracer process\n");
+               _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 - calling %s()\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(
+           wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 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 - calling %s()\n",
+           TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+           tracer);
+
+       validate_status_exited(status, exitval_tracer);
+
+       printf("Wait for tracee to finish its job and exit - calling %s()\n",
+           TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+           tracee);
+
+       validate_status_exited(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);
+}
+#endif
+
 ATF_TP_ADD_TCS(tp)
 {
        setvbuf(stdout, NULL, _IONBF, 0);
@@ -4702,5 +4923,8 @@
        ATF_TP_ADD_TC(tp, kill1);
        ATF_TP_ADD_TC(tp, kill2);
 
+       ATF_TP_ADD_TC(tp, lwpinfo1);
+       ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
+
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index