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 fork/vfork/posix_spawn tests out of ...



details:   https://anonhg.NetBSD.org/src/rev/906eda0f236e
branches:  trunk
changeset: 932314:906eda0f236e
user:      kamil <kamil%NetBSD.org@localhost>
date:      Mon May 04 22:34:22 2020 +0000

description:
Move fork/vfork/posix_spawn tests out of t_ptrace_wait.c to t_ptrace_fork_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_fork_wait.h |  1730 +++++++++++++++++++++++++++++++
 tests/lib/libc/sys/t_ptrace_wait.c      |  1720 +------------------------------
 2 files changed, 1734 insertions(+), 1716 deletions(-)

diffs (truncated from 3510 to 300 lines):

diff -r c7e897866109 -r 906eda0f236e tests/lib/libc/sys/t_ptrace_fork_wait.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/sys/t_ptrace_fork_wait.h   Mon May 04 22:34:22 2020 +0000
@@ -0,0 +1,1730 @@
+/*     $NetBSD: t_ptrace_fork_wait.h,v 1.1 2020/05/04 22:34:22 kamil Exp $     */
+
+/*-
+ * Copyright (c) 2016, 2017, 2018, 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
+fork_body(const char *fn, bool trackspawn, bool trackfork, bool trackvfork,
+    bool trackvforkdone)
+{
+       const int exitval = 5;
+       const int exitval2 = 0; /* This matched exit status from /bin/echo */
+       const int sigval = SIGSTOP;
+       pid_t child, child2 = 0, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       ptrace_state_t state;
+       const int slen = sizeof(state);
+       ptrace_event_t event;
+       const int elen = sizeof(event);
+
+       char * const arg[] = { __UNCONST("/bin/echo"), NULL };
+
+       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);
+
+               if (strcmp(fn, "spawn") == 0) {
+                       FORKEE_ASSERT_EQ(posix_spawn(&child2,
+                           arg[0], NULL, NULL, arg, NULL), 0);
+               } else {
+                       if (strcmp(fn, "fork") == 0) {
+                               FORKEE_ASSERT((child2 = fork()) != -1);
+                       } else if (strcmp(fn, "vfork") == 0) {
+                               FORKEE_ASSERT((child2 = vfork()) != -1);
+                       }
+
+                       if (child2 == 0)
+                               _exit(exitval2);
+               }
+               FORKEE_REQUIRE_SUCCESS
+                   (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+               forkee_status_exited(status, exitval2);
+
+               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("Set 0%s%s%s%s in EVENT_MASK for the child %d\n",
+           trackspawn ? "|PTRACE_POSIX_SPAWN" : "",
+           trackfork ? "|PTRACE_FORK" : "",
+           trackvfork ? "|PTRACE_VFORK" : "",
+           trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
+       event.pe_set_event = 0;
+       if (trackspawn)
+               event.pe_set_event |= PTRACE_POSIX_SPAWN;
+       if (trackfork)
+               event.pe_set_event |= PTRACE_FORK;
+       if (trackvfork)
+               event.pe_set_event |= PTRACE_VFORK;
+       if (trackvforkdone)
+               event.pe_set_event |= PTRACE_VFORK_DONE;
+       SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+       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);
+
+#if defined(TWAIT_HAVE_PID)
+       if ((trackspawn && strcmp(fn, "spawn") == 0) ||
+           (trackfork && strcmp(fn, "fork") == 0) ||
+           (trackvfork && strcmp(fn, "vfork") == 0)) {
+               DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
+                   child);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+                   child);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+               if (trackspawn && strcmp(fn, "spawn") == 0) {
+                       ATF_REQUIRE_EQ(
+                           state.pe_report_event & PTRACE_POSIX_SPAWN,
+                              PTRACE_POSIX_SPAWN);
+               }
+               if (trackfork && strcmp(fn, "fork") == 0) {
+                       ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
+                              PTRACE_FORK);
+               }
+               if (trackvfork && strcmp(fn, "vfork") == 0) {
+                       ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
+                              PTRACE_VFORK);
+               }
+
+               child2 = state.pe_other_pid;
+               DPRINTF("Reported ptrace event with forkee %d\n", child2);
+
+               DPRINTF("Before calling %s() for the forkee %d of the child "
+                   "%d\n", TWAIT_FNAME, child2, child);
+               TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+                   child2);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
+               if (trackspawn && strcmp(fn, "spawn") == 0) {
+                       ATF_REQUIRE_EQ(
+                           state.pe_report_event & PTRACE_POSIX_SPAWN,
+                              PTRACE_POSIX_SPAWN);
+               }
+               if (trackfork && strcmp(fn, "fork") == 0) {
+                       ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
+                              PTRACE_FORK);
+               }
+               if (trackvfork && strcmp(fn, "vfork") == 0) {
+                       ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
+                              PTRACE_VFORK);
+               }
+
+               ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+               DPRINTF("Before resuming the forkee process where it left off "
+                   "and without signal to be sent\n");
+               SYSCALL_REQUIRE(
+                   ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
+
+               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);
+       }
+#endif
+
+       if (trackvforkdone && strcmp(fn, "vfork") == 0) {
+               DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
+                   child);
+               TWAIT_REQUIRE_SUCCESS(
+                   wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+               validate_status_stopped(status, SIGTRAP);
+
+               SYSCALL_REQUIRE(
+                   ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+               ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
+
+               child2 = state.pe_other_pid;
+               DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
+                   child2);
+
+               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);
+       }
+
+#if defined(TWAIT_HAVE_PID)
+       if ((trackspawn && strcmp(fn, "spawn") == 0) ||
+           (trackfork && strcmp(fn, "fork") == 0) ||
+           (trackvfork && strcmp(fn, "vfork") == 0)) {
+               DPRINTF("Before calling %s() for the forkee - expected exited"
+                   "\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_SUCCESS(
+                   wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+               validate_status_exited(status, exitval2);
+
+               DPRINTF("Before calling %s() for the forkee - expected no "
+                   "process\n", TWAIT_FNAME);
+               TWAIT_REQUIRE_FAILURE(ECHILD,
+                   wpid = TWAIT_GENERIC(child2, &status, 0));
+       }
+#endif
+
+       DPRINTF("Before calling %s() for the child - expected stopped "
+           "SIGCHLD\n", TWAIT_FNAME);
+       TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+       validate_status_stopped(status, SIGCHLD);
+
+       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 - expected exited\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 - expected no process\n",
+           TWAIT_FNAME);
+       TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#define FORK_TEST(name,fun,tspawn,tfork,tvfork,tvforkdone)             \
+ATF_TC(name);                                                          \
+ATF_TC_HEAD(name, tc)                                                  \
+{                                                                      \
+       atf_tc_set_md_var(tc, "descr", "Verify " fun "() "              \
+           "called with 0%s%s%s%s in EVENT_MASK",                      \
+           tspawn ? "|PTRACE_POSIX_SPAWN" : "",                        \
+           tfork ? "|PTRACE_FORK" : "",                                \
+           tvfork ? "|PTRACE_VFORK" : "",                              \
+           tvforkdone ? "|PTRACE_VFORK_DONE" : "");                    \
+}                                                                      \
+                                                                       \
+ATF_TC_BODY(name, tc)                                                  \
+{                                                                      \
+                                                                       \
+       fork_body(fun, tspawn, tfork, tvfork, tvforkdone);              \
+}
+
+FORK_TEST(fork1, "fork", false, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(fork2, "fork", false, true, false, false)
+FORK_TEST(fork3, "fork", false, false, true, false)
+FORK_TEST(fork4, "fork", false, true, true, false)
+#endif
+FORK_TEST(fork5, "fork", false, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(fork6, "fork", false, true, false, true)
+FORK_TEST(fork7, "fork", false, false, true, true)
+FORK_TEST(fork8, "fork", false, true, true, true)
+#endif
+FORK_TEST(fork9, "fork", true, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(fork10, "fork", true, true, false, false)
+FORK_TEST(fork11, "fork", true, false, true, false)
+FORK_TEST(fork12, "fork", true, true, true, false)
+#endif
+FORK_TEST(fork13, "fork", true, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(fork14, "fork", true, true, false, true)
+FORK_TEST(fork15, "fork", true, false, true, true)
+FORK_TEST(fork16, "fork", true, true, true, true)
+#endif
+
+FORK_TEST(vfork1, "vfork", false, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(vfork2, "vfork", false, true, false, false)
+FORK_TEST(vfork3, "vfork", false, false, true, false)
+FORK_TEST(vfork4, "vfork", false, true, true, false)
+#endif
+FORK_TEST(vfork5, "vfork", false, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(vfork6, "vfork", false, true, false, true)
+FORK_TEST(vfork7, "vfork", false, false, true, true)
+FORK_TEST(vfork8, "vfork", false, true, true, true)
+#endif
+FORK_TEST(vfork9, "vfork", true, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(vfork10, "vfork", true, true, false, false)
+FORK_TEST(vfork11, "vfork", true, false, true, false)
+FORK_TEST(vfork12, "vfork", true, true, true, false)
+#endif
+FORK_TEST(vfork13, "vfork", true, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+FORK_TEST(vfork14, "vfork", true, true, false, true)
+FORK_TEST(vfork15, "vfork", true, false, true, true)
+FORK_TEST(vfork16, "vfork", true, true, true, true)



Home | Main Index | Thread Index | Old Index