Merge code in tests: fork1 and vfork1 (ATF t_ptrace_wait*)

Marge bodies of two tests into the same function.
Add few checks for regular fork or not (vfork).

Sponsored by <The NetBSD Foundation>
This commit is contained in:
kamil 2018-04-10 00:09:31 +00:00
parent cf8ef0429b
commit 3a2e5c145a
1 changed files with 37 additions and 131 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: t_ptrace_wait.c,v 1.27 2018/04/09 15:45:16 kamil Exp $ */
/* $NetBSD: t_ptrace_wait.c,v 1.28 2018/04/10 00:09:31 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.27 2018/04/09 15:45:16 kamil Exp $");
__RCSID("$NetBSD: t_ptrace_wait.c,v 1.28 2018/04/10 00:09:31 kamil Exp $");
#include <sys/param.h>
#include <sys/types.h>
@ -1322,15 +1322,8 @@ ATF_TC_BODY(eventmask6, tc)
}
#if defined(TWAIT_HAVE_PID)
ATF_TC(fork1);
ATF_TC_HEAD(fork1, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
"set to PTRACE_FORK");
}
ATF_TC_BODY(fork1, tc)
static void
fork_test(bool regular)
{
const int exitval = 5;
const int exitval2 = 15;
@ -1344,6 +1337,10 @@ ATF_TC_BODY(fork1, tc)
ptrace_event_t event;
const int elen = sizeof(event);
if (!regular) {
atf_tc_expect_fail("PR kern/51630");
}
DPRINTF("Before forking process PID=%d\n", getpid());
SYSCALL_REQUIRE((child = fork()) != -1);
if (child == 0) {
@ -1353,7 +1350,7 @@ ATF_TC_BODY(fork1, tc)
DPRINTF("Before raising %s from child\n", strsignal(sigval));
FORKEE_ASSERT(raise(sigval) == 0);
FORKEE_ASSERT((child2 = fork()) != -1);
FORKEE_ASSERT((child2 = (regular ? fork() : vfork())) != -1);
if (child2 == 0)
_exit(exitval2);
@ -1373,16 +1370,18 @@ ATF_TC_BODY(fork1, tc)
validate_status_stopped(status, sigval);
DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
event.pe_set_event = PTRACE_FORK;
DPRINTF("Enable PTRACE_%sFORK in EVENT_MASK for the child %d\n",
regular ? "" : "V", child);
event.pe_set_event = regular ? PTRACE_FORK : PTRACE_VFORK;
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");
DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
"pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
"for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
"state.pe_other_pid=child)\n", child);
"pe_report_event=PTRACE_%sFORK, state.pe_other_pid=child2) and "
"for child2 (TRAP_CHLD, pe_report_event=PTRACE_%sFORK, "
"state.pe_other_pid=child)\n", child, regular ? "" : "V",
regular ? "" : "V");
SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
@ -1391,10 +1390,12 @@ ATF_TC_BODY(fork1, tc)
validate_status_stopped(status, SIGTRAP);
SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
ATF_REQUIRE_EQ(state.pe_report_event,
regular ? PTRACE_FORK : PTRACE_VFORK);
child2 = state.pe_other_pid;
DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
DPRINTF("Reported PTRACE_%sFORK event with forkee %d\n",
regular ? "" : "V", child2);
DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
TWAIT_FNAME, child2, child);
@ -1404,7 +1405,8 @@ ATF_TC_BODY(fork1, tc)
validate_status_stopped(status, SIGTRAP);
SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
ATF_REQUIRE_EQ(state.pe_report_event,
regular ? PTRACE_FORK : PTRACE_VFORK);
ATF_REQUIRE_EQ(state.pe_other_pid, child);
DPRINTF("Before resuming the forkee process where it left off and "
@ -1447,6 +1449,20 @@ ATF_TC_BODY(fork1, tc)
TWAIT_FNAME);
TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
}
ATF_TC(fork1);
ATF_TC_HEAD(fork1, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
"set to PTRACE_FORK");
}
ATF_TC_BODY(fork1, tc)
{
fork_test(true);
}
#endif
ATF_TC(fork2);
@ -1538,118 +1554,8 @@ ATF_TC_HEAD(vfork1, tc)
ATF_TC_BODY(vfork1, tc)
{
const int exitval = 5;
const int exitval2 = 15;
const int sigval = SIGSTOP;
pid_t child, child2, 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);
atf_tc_expect_fail("PR kern/51630");
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);
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("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
event.pe_set_event = PTRACE_VFORK;
SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP);
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 %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);
child2 = state.pe_other_pid;
DPRINTF("Reported PTRACE_VFORK 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);
ATF_REQUIRE_EQ(state.pe_report_event, 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);
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));
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));
fork_test(false);
}
#endif