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 siginfo[123] in t_ptrace_wait{, 3, ...



details:   https://anonhg.NetBSD.org/src/rev/29b2e037e4d3
branches:  trunk
changeset: 350113:29b2e037e4d3
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Jan 04 22:27:20 2017 +0000

description:
Add new tests siginfo[123] in t_ptrace_wait{,3,4,6,id,pid}

These tests are for the proposed PT_[GS]ET_SIGINFO interface for ptrace(2),
accessors for signal information that caused tracee to be interrupted.

siginfo1:
    Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee

siginfo2:
    Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without
    modification of SIGINT from tracee

siginfo3:
    Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with
    setting signal to new value

New tests are protected with #ifded and currently disabled in the HEAD branch.
They will be automatically enabled once the final implementation will land the
sources.

Sponsored by <The NetBSD Foundation>

diffstat:

 tests/kernel/t_ptrace_wait.c |  251 ++++++++++++++++++++++++++++++++++++++++++-
 tests/kernel/t_ptrace_wait.h |   14 ++-
 2 files changed, 262 insertions(+), 3 deletions(-)

diffs (truncated from 313 to 300 lines):

diff -r b1e32e0723d3 -r 29b2e037e4d3 tests/kernel/t_ptrace_wait.c
--- a/tests/kernel/t_ptrace_wait.c      Wed Jan 04 21:37:46 2017 +0000
+++ b/tests/kernel/t_ptrace_wait.c      Wed Jan 04 22:27:20 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.48 2017/01/02 21:02:03 kamil Exp $ */
+/*     $NetBSD: t_ptrace_wait.c,v 1.49 2017/01/04 22:27:20 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.48 2017/01/02 21:02:03 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.49 2017/01/04 22:27:20 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -4458,6 +4458,249 @@
 }
 #endif
 
+#if defined(HAVE_SIGINFO)
+ATF_TC(siginfo1);
+ATF_TC_HEAD(siginfo1, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
+}
+
+ATF_TC_BODY(siginfo1, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGTRAP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       struct ptrace_siginfo info;
+       memset(&info, 0, sizeof(info));
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       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_GET_SIGINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+       printf("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);
+
+       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));
+}
+#endif
+
+#if defined(HAVE_SIGINFO)
+ATF_TC(siginfo2);
+ATF_TC_HEAD(siginfo2, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
+           "modification of SIGINT from tracee");
+}
+
+static int siginfo2_caught = 0;
+
+static void
+siginfo2_sighandler(int sig)
+{
+       FORKEE_ASSERT_EQ(sig, SIGINT);
+
+       ++siginfo2_caught;
+}
+
+ATF_TC_BODY(siginfo2, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGINT;
+       pid_t child, wpid;
+       struct sigaction sa;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       struct ptrace_siginfo info;
+       memset(&info, 0, sizeof(info));
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               sa.sa_handler = siginfo2_sighandler;
+               sa.sa_flags = SA_SIGINFO;
+               sigemptyset(&sa.sa_mask);
+
+               FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               FORKEE_ASSERT_EQ(siginfo2_caught, 1);
+
+               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_GET_SIGINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+       printf("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);
+
+       printf("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       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, sigval) != -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));
+}
+#endif
+
+#if defined(HAVE_SIGINFO)
+ATF_TC(siginfo3);
+ATF_TC_HEAD(siginfo3, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
+           "setting signal to new value");
+}
+
+static int siginfo3_caught = 0;
+
+static void
+siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
+{
+       FORKEE_ASSERT_EQ(sig, SIGTRAP);
+
+       FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
+       FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
+
+       ++siginfo3_caught;
+}
+
+ATF_TC_BODY(siginfo3, tc)
+{
+       const int exitval = 5;
+       const int sigval = SIGINT;
+       const int sigfaked = SIGTRAP;
+       const int sicodefaked = TRAP_BRKPT;
+       pid_t child, wpid;
+       struct sigaction sa;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       struct ptrace_siginfo info;
+       memset(&info, 0, sizeof(info));
+
+       printf("Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+               FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+               sa.sa_sigaction = siginfo3_sigaction;
+               sa.sa_flags = SA_SIGINFO;
+               sigemptyset(&sa.sa_mask);
+
+               FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
+
+               printf("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               FORKEE_ASSERT_EQ(siginfo3_caught, 1);
+
+               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_GET_SIGINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+       printf("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);
+
+       printf("Before setting new faked signal to signo=%d si_code=%d\n",
+           sigfaked, sicodefaked);
+       info.psi_siginfo.si_signo = sigfaked;
+       info.psi_siginfo.si_code = sicodefaked;
+
+       printf("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+       ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       printf("Before checking siginfo_t\n");
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
+
+       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, sigfaked) != -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));
+}
+#endif
+
 ATF_TP_ADD_TCS(tp)
 {
        setvbuf(stdout, NULL, _IONBF, 0);
@@ -4540,5 +4783,9 @@
        ATF_TP_ADD_TC(tp, lwpinfo1);
        ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
 
+       ATF_TP_ADD_TC_HAVE_SIGINFO(tp, siginfo1);
+       ATF_TP_ADD_TC_HAVE_SIGINFO(tp, siginfo2);
+       ATF_TP_ADD_TC_HAVE_SIGINFO(tp, siginfo3);
+
        return atf_no_error();
 }
diff -r b1e32e0723d3 -r 29b2e037e4d3 tests/kernel/t_ptrace_wait.h
--- a/tests/kernel/t_ptrace_wait.h      Wed Jan 04 21:37:46 2017 +0000
+++ b/tests/kernel/t_ptrace_wait.h      Wed Jan 04 22:27:20 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.h,v 1.5 2016/12/30 17:29:34 kamil Exp $  */
+/*     $NetBSD: t_ptrace_wait.h,v 1.6 2017/01/04 22:27:20 kamil Exp $  */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -196,6 +196,12 @@
 #define HAVE_DBREGS
 #endif
 
+/* Add guards for siginfo_t accessors */
+#if defined(PT_GET_SIGINFO)            \
+    && defined(PT_SET_SIGINFO)
+#define HAVE_SIGINFO
+#endif
+
 /*
  * If waitid(2) returns because one or more processes have a state change to
  * report, 0 is returned.  If an error is detected, a value of -1 is returned



Home | Main Index | Thread Index | Old Index