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 Move signal tests out of t_ptrace_wait.c ...



details:   https://anonhg.NetBSD.org/src/rev/cc07c96839b0
branches:  trunk
changeset: 932315:cc07c96839b0
user:      kamil <kamil%NetBSD.org@localhost>
date:      Mon May 04 23:49:31 2020 +0000

description:
Move signal tests out of t_ptrace_wait.c to t_ptrace_signal_wait.h

The same tests are now included with the preprocessor in t_ptrace_wait.c.

No functional change intended.

diffstat:

 tests/lib/libc/sys/t_ptrace_signal_wait.h |  2224 +++++++++++++++++++++++++++++
 tests/lib/libc/sys/t_ptrace_wait.c        |  2220 +----------------------------
 tests/lib/libc/sys/t_ptrace_wait3.c       |     4 +-
 tests/lib/libc/sys/t_ptrace_wait4.c       |     4 +-
 tests/lib/libc/sys/t_ptrace_wait6.c       |     4 +-
 tests/lib/libc/sys/t_ptrace_waitid.c      |     4 +-
 tests/lib/libc/sys/t_ptrace_waitpid.c     |     4 +-
 7 files changed, 2239 insertions(+), 2225 deletions(-)

diffs (truncated from 4591 to 300 lines):

diff -r 906eda0f236e -r cc07c96839b0 tests/lib/libc/sys/t_ptrace_signal_wait.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/sys/t_ptrace_signal_wait.h Mon May 04 23:49:31 2020 +0000
@@ -0,0 +1,2224 @@
+/*     $NetBSD: t_ptrace_signal_wait.h,v 1.1 2020/05/04 23:49:31 kamil Exp $   */
+
+/*-
+ * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+static void
+traceme_raise(int sigval)
+{
+       const int exitval = 5;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+
+       ptrace_state_t state, zero_state;
+       const int slen = sizeof(state);
+       struct ptrace_siginfo info;
+       memset(&zero_state, 0, sizeof(zero_state));
+       memset(&info, 0, sizeof(info));
+
+       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);
+
+               switch (sigval) {
+               case SIGKILL:
+                       /* NOTREACHED */
+                       FORKEE_ASSERTX(0 && "This shall not be reached");
+                       __unreachable();
+               default:
+                       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);
+
+       switch (sigval) {
+       case SIGKILL:
+               validate_status_signaled(status, sigval, 0);
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child, &state, slen) == -1);
+
+               break;
+       default:
+               validate_status_stopped(status, sigval);
+
+               DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
+                       "child\n");
+               SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info,
+                       sizeof(info)) != -1);
+
+               DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+               DPRINTF("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);
+
+               ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
+               ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+               DPRINTF("Assert that PT_GET_PROCESS_STATE returns non-error");
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+               ATF_REQUIRE(memcmp(&state, &zero_state, slen) == 0);
+
+               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\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+                   child);
+               break;
+       }
+
+       DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#define TRACEME_RAISE(test, sig)                                       \
+ATF_TC(test);                                                          \
+ATF_TC_HEAD(test, tc)                                                  \
+{                                                                      \
+       atf_tc_set_md_var(tc, "descr",                                  \
+           "Verify " #sig " followed by _exit(2) in a child");         \
+}                                                                      \
+                                                                       \
+ATF_TC_BODY(test, tc)                                                  \
+{                                                                      \
+                                                                       \
+       traceme_raise(sig);                                             \
+}
+
+TRACEME_RAISE(traceme_raise1, SIGKILL) /* non-maskable */
+TRACEME_RAISE(traceme_raise2, SIGSTOP) /* non-maskable */
+TRACEME_RAISE(traceme_raise3, SIGABRT) /* regular abort trap */
+TRACEME_RAISE(traceme_raise4, SIGHUP)  /* hangup */
+TRACEME_RAISE(traceme_raise5, SIGCONT) /* continued? */
+TRACEME_RAISE(traceme_raise6, SIGTRAP) /* crash signal */
+TRACEME_RAISE(traceme_raise7, SIGBUS) /* crash signal */
+TRACEME_RAISE(traceme_raise8, SIGILL) /* crash signal */
+TRACEME_RAISE(traceme_raise9, SIGFPE) /* crash signal */
+TRACEME_RAISE(traceme_raise10, SIGSEGV) /* crash signal */
+
+/// ----------------------------------------------------------------------------
+
+static void
+traceme_raisesignal_ignored(int sigignored)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+       struct sigaction sa;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       struct ptrace_siginfo info;
+
+       memset(&info, 0, sizeof(info));
+
+       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);
+
+               memset(&sa, 0, sizeof(sa));
+               sa.sa_handler = SIG_IGN;
+               sigemptyset(&sa.sa_mask);
+               FORKEE_ASSERT(sigaction(sigignored, &sa, NULL) != -1);
+
+               DPRINTF("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               DPRINTF("Before raising %s from child\n",
+                   strsignal(sigignored));
+               FORKEE_ASSERT(raise(sigignored) == 0);
+
+               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("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+       SYSCALL_REQUIRE(
+           ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+       DPRINTF("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);
+
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+       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\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, sigignored);
+
+       DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+       SYSCALL_REQUIRE(
+           ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+       DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
+       DPRINTF("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);
+
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigignored);
+       ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+       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\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\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#define TRACEME_RAISESIGNAL_IGNORED(test, sig)                         \
+ATF_TC(test);                                                          \
+ATF_TC_HEAD(test, tc)                                                  \
+{                                                                      \
+       atf_tc_set_md_var(tc, "descr",                                  \
+           "Verify that ignoring (with SIG_IGN) " #sig " in tracee "   \
+           "does not stop tracer from catching this raised signal");   \
+}                                                                      \
+                                                                       \
+ATF_TC_BODY(test, tc)                                                  \
+{                                                                      \
+                                                                       \
+       traceme_raisesignal_ignored(sig);                               \
+}
+
+// A signal handler for SIGKILL and SIGSTOP cannot be ignored.
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored1, SIGABRT) /* abort */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored2, SIGHUP)  /* hangup */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored3, SIGCONT) /* cont. */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored4, SIGTRAP) /* crash */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored5, SIGBUS) /* crash */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored6, SIGILL) /* crash */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored7, SIGFPE) /* crash */
+TRACEME_RAISESIGNAL_IGNORED(traceme_raisesignal_ignored8, SIGSEGV) /* crash */
+
+/// ----------------------------------------------------------------------------
+
+static void
+traceme_raisesignal_masked(int sigmasked)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       sigset_t intmask;
+       struct ptrace_siginfo info;
+
+       memset(&info, 0, sizeof(info));
+
+       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);
+
+               sigemptyset(&intmask);
+               sigaddset(&intmask, sigmasked);
+               sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+               DPRINTF("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               DPRINTF("Before raising %s breakpoint from child\n",
+                   strsignal(sigmasked));
+               FORKEE_ASSERT(raise(sigmasked) == 0);
+
+               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("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+       SYSCALL_REQUIRE(



Home | Main Index | Thread Index | Old Index