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 Validate that clone(2) is handled properl...
details: https://anonhg.NetBSD.org/src/rev/a89bae2abf17
branches: trunk
changeset: 450385:a89bae2abf17
user: kamil <kamil%NetBSD.org@localhost>
date: Thu Apr 11 19:25:31 2019 +0000
description:
Validate that clone(2) is handled properly with more ptrace(2) ATF tests
New tests:
- clone[1-8]
- clone_vm[1-8]
- clone_fs[1-8]
- clone_files[1-8]
- clone_sighand[1-8] // disabled temporarily
- clone_vfork[1-8]
Assert that appropriate events are triggered for the combination of:
- PTRACE_FORK
- PTRACE_VFORK
- PTRACE_VFORK_DONE
diffstat:
tests/lib/libc/sys/t_ptrace_wait.c | 382 ++++++++++++++++++++++++++++++++++--
1 files changed, 357 insertions(+), 25 deletions(-)
diffs (truncated from 430 to 300 lines):
diff -r 36eada689bf2 -r a89bae2abf17 tests/lib/libc/sys/t_ptrace_wait.c
--- a/tests/lib/libc/sys/t_ptrace_wait.c Thu Apr 11 17:46:32 2019 +0000
+++ b/tests/lib/libc/sys/t_ptrace_wait.c Thu Apr 11 19:25:31 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_ptrace_wait.c,v 1.105 2019/04/06 15:35:09 kamil Exp $ */
+/* $NetBSD: t_ptrace_wait.c,v 1.106 2019/04/11 19:25:31 kamil Exp $ */
/*-
* Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.105 2019/04/06 15:35:09 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.106 2019/04/11 19:25:31 kamil Exp $");
#include <sys/param.h>
#include <sys/types.h>
@@ -6283,19 +6283,297 @@
/// ----------------------------------------------------------------------------
-#if defined(TWAIT_HAVE_PID)
-static int
-clone_func(void *arg)
+static void
+clone_body(int flags, bool trackfork, bool trackvfork,
+ bool trackvforkdone)
{
- int ret;
-
- ret = (int)(intptr_t)arg;
-
- return ret;
+ const int exitval = 5;
+ const int exitval2 = 15;
+ 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);
+
+ const size_t stack_size = 1024 * 1024;
+ void *stack, *stack_base;
+
+ stack = malloc(stack_size);
+ ATF_REQUIRE(stack != NULL);
+
+#ifdef __MACHINE_STACK_GROWS_UP
+ stack_base = stack;
+#else
+ stack_base = (char *)stack + stack_size;
+#endif
+
+ 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);
+
+ SYSCALL_REQUIRE((child2 = __clone(clone_func, stack_base,
+ flags|SIGCHLD, (void *)(intptr_t)exitval2)) != -1);
+
+ DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(),
+ child2);
+
+ // XXX WALLSIG?
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, WALLSIG), 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 in EVENT_MASK for the child %d\n",
+ trackfork ? "|PTRACE_FORK" : "",
+ trackvfork ? "|PTRACE_VFORK" : "",
+ trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
+ event.pe_set_event = 0;
+ 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 ((trackfork && !(flags & CLONE_VFORK)) ||
+ (trackvfork && (flags & CLONE_VFORK))) {
+ 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 (trackfork && !(flags & CLONE_VFORK)) {
+ ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
+ PTRACE_FORK);
+ }
+ if (trackvfork && (flags & CLONE_VFORK)) {
+ 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 (trackfork && !(flags & CLONE_VFORK)) {
+ ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
+ PTRACE_FORK);
+ }
+ if (trackvfork && (flags & CLONE_VFORK)) {
+ 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 && (flags & CLONE_VFORK)) {
+ 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 ((trackfork && !(flags & CLONE_VFORK)) ||
+ (trackvfork && (flags & CLONE_VFORK))) {
+ 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 CLONE_TEST(name,flags,tfork,tvfork,tvforkdone) \
+ATF_TC(name); \
+ATF_TC_HEAD(name, tc) \
+{ \
+ atf_tc_set_md_var(tc, "descr", "Verify clone(%s) " \
+ "called with 0%s%s%s in EVENT_MASK", \
+ #flags, \
+ tfork ? "|PTRACE_FORK" : "", \
+ tvfork ? "|PTRACE_VFORK" : "", \
+ tvforkdone ? "|PTRACE_VFORK_DONE" : ""); \
+} \
+ \
+ATF_TC_BODY(name, tc) \
+{ \
+ \
+ clone_body(flags, tfork, tvfork, tvforkdone); \
+}
+
+CLONE_TEST(clone1, 0, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone2, 0, true, false, false)
+CLONE_TEST(clone3, 0, false, true, false)
+CLONE_TEST(clone4, 0, true, true, false)
+#endif
+CLONE_TEST(clone5, 0, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone6, 0, true, false, true)
+CLONE_TEST(clone7, 0, false, true, true)
+CLONE_TEST(clone8, 0, true, true, true)
+#endif
+
+CLONE_TEST(clone_vm1, CLONE_VM, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone_vm2, CLONE_VM, true, false, false)
+CLONE_TEST(clone_vm3, CLONE_VM, false, true, false)
+CLONE_TEST(clone_vm4, CLONE_VM, true, true, false)
+#endif
+CLONE_TEST(clone_vm5, CLONE_VM, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone_vm6, CLONE_VM, true, false, true)
+CLONE_TEST(clone_vm7, CLONE_VM, false, true, true)
+CLONE_TEST(clone_vm8, CLONE_VM, true, true, true)
+#endif
+
+CLONE_TEST(clone_fs1, CLONE_FS, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone_fs2, CLONE_FS, true, false, false)
+CLONE_TEST(clone_fs3, CLONE_FS, false, true, false)
+CLONE_TEST(clone_fs4, CLONE_FS, true, true, false)
+#endif
+CLONE_TEST(clone_fs5, CLONE_FS, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone_fs6, CLONE_FS, true, false, true)
+CLONE_TEST(clone_fs7, CLONE_FS, false, true, true)
+CLONE_TEST(clone_fs8, CLONE_FS, true, true, true)
+#endif
+
+CLONE_TEST(clone_files1, CLONE_FILES, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone_files2, CLONE_FILES, true, false, false)
+CLONE_TEST(clone_files3, CLONE_FILES, false, true, false)
+CLONE_TEST(clone_files4, CLONE_FILES, true, true, false)
+#endif
+CLONE_TEST(clone_files5, CLONE_FILES, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+CLONE_TEST(clone_files6, CLONE_FILES, true, false, true)
+CLONE_TEST(clone_files7, CLONE_FILES, false, true, true)
+CLONE_TEST(clone_files8, CLONE_FILES, true, true, true)
+#endif
+
+//CLONE_TEST(clone_sighand1, CLONE_SIGHAND, false, false, false)
+#if defined(TWAIT_HAVE_PID)
+//CLONE_TEST(clone_sighand2, CLONE_SIGHAND, true, false, false)
+//CLONE_TEST(clone_sighand3, CLONE_SIGHAND, false, true, false)
+//CLONE_TEST(clone_sighand4, CLONE_SIGHAND, true, true, false)
+#endif
+//CLONE_TEST(clone_sighand5, CLONE_SIGHAND, false, false, true)
+#if defined(TWAIT_HAVE_PID)
+//CLONE_TEST(clone_sighand6, CLONE_SIGHAND, true, false, true)
+//CLONE_TEST(clone_sighand7, CLONE_SIGHAND, false, true, true)
Home |
Main Index |
Thread Index |
Old Index