Module Name: src Committed By: kamil Date: Tue Feb 4 15:06:27 UTC 2020
Modified Files: src/tests/lib/libc/sys: t_ptrace_wait.c Log Message: Add new ATF tests for unrelated tracer in t_ptrace_wait* New tests: - unrelated_tracer_posix_spawn_detach_spawner - unrelated_tracer_fork_detach_forker - unrelated_tracer_vfork_detach_vforker - unrelated_tracer_vfork_detach_vforkerdone - unrelated_tracer_posix_spawn_kill_spawner - unrelated_tracer_fork_kill_forker - unrelated_tracer_vfork_kill_vforker - unrelated_tracer_vfork_kill_vforkerdone All tests pass. To generate a diff of this commit: cvs rdiff -u -r1.149 -r1.150 src/tests/lib/libc/sys/t_ptrace_wait.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libc/sys/t_ptrace_wait.c diff -u src/tests/lib/libc/sys/t_ptrace_wait.c:1.149 src/tests/lib/libc/sys/t_ptrace_wait.c:1.150 --- src/tests/lib/libc/sys/t_ptrace_wait.c:1.149 Wed Jan 29 03:51:56 2020 +++ src/tests/lib/libc/sys/t_ptrace_wait.c Tue Feb 4 15:06:27 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_wait.c,v 1.149 2020/01/29 03:51:56 kamil Exp $ */ +/* $NetBSD: t_ptrace_wait.c,v 1.150 2020/02/04 15:06:27 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.149 2020/01/29 03:51:56 kamil Exp $"); +__RCSID("$NetBSD: t_ptrace_wait.c,v 1.150 2020/02/04 15:06:27 kamil Exp $"); #define __LEGACY_PT_LWPINFO @@ -116,7 +116,7 @@ static_assert(sizeof(((struct ptrace_sta #define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \ "%d(%s) != %d", res, strerror(res), exp) -static int debug = 0; +static int debug = 1; #define DPRINTF(a, ...) do \ if (debug) \ @@ -3869,6 +3869,292 @@ FORK_DETACH_FORKER(vfork_kill_vforkerdon /// ---------------------------------------------------------------------------- +#if defined(TWAIT_HAVE_PID) +static void +unrelated_tracer_fork_detach_forker_body(const char *fn, bool kill_process) +{ + const int sigval = SIGSTOP; + struct msg_fds parent_tracee, parent_tracer; + const int exitval = 10; + const int exitval2 = 0; /* This matched exit status from /bin/echo */ + pid_t tracee, tracer, wpid; + pid_t tracee2 = 0; + uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */ +#if defined(TWAIT_HAVE_STATUS) + int status; +#endif + int op; + + struct ptrace_siginfo info; + 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("Spawn tracee\n"); + SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0); + tracee = atf_utils_fork(); + if (tracee == 0) { + // Wait for parent to let us crash + CHILD_FROM_PARENT("exit tracee", parent_tracee, msg); + + DPRINTF("Before raising %s from child\n", strsignal(sigval)); + FORKEE_ASSERT(raise(sigval) == 0); + + if (strcmp(fn, "spawn") == 0) { + FORKEE_ASSERT_EQ(posix_spawn(&tracee2, + arg[0], NULL, NULL, arg, NULL), 0); + } else { + if (strcmp(fn, "fork") == 0) { + FORKEE_ASSERT((tracee2 = fork()) != -1); + } else { + FORKEE_ASSERT((tracee2 = vfork()) != -1); + } + + if (tracee2 == 0) + _exit(exitval2); + } + + FORKEE_REQUIRE_SUCCESS + (wpid = TWAIT_GENERIC(tracee2, &status, 0), tracee2); + + forkee_status_exited(status, exitval2); + + DPRINTF("Before exiting of the child process\n"); + _exit(exitval); + } + + DPRINTF("Spawn debugger\n"); + SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0); + tracer = atf_utils_fork(); + if (tracer == 0) { + /* Fork again and drop parent to reattach to PID 1 */ + tracer = atf_utils_fork(); + if (tracer != 0) + _exit(exitval); + + DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid()); + FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1); + + /* Wait for tracee and assert that it was stopped w/ SIGSTOP */ + FORKEE_REQUIRE_SUCCESS( + wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); + + forkee_status_stopped(status, SIGSTOP); + + DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the " + "traced process\n"); + SYSCALL_REQUIRE( + ptrace(PT_GET_SIGINFO, tracee, &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); + + FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, SIGSTOP); + FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_USER); + + /* Resume tracee with PT_CONTINUE */ + FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1); + + /* Inform parent that tracer has attached to tracee */ + CHILD_TO_PARENT("tracer ready", parent_tracer, msg); + + /* Wait for parent to tell use that tracee should have exited */ + CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg); + + /* Wait for tracee and assert that it exited */ + FORKEE_REQUIRE_SUCCESS( + wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); + + forkee_status_stopped(status, sigval); + + DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for the " + "traced process\n"); + SYSCALL_REQUIRE( + ptrace(PT_GET_SIGINFO, tracee, &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); + + FORKEE_ASSERT_EQ(info.psi_siginfo.si_signo, sigval); + FORKEE_ASSERT_EQ(info.psi_siginfo.si_code, SI_LWP); + + DPRINTF("Set EVENT_MASK for the child %d\n", tracee); + event.pe_set_event = PTRACE_POSIX_SPAWN | PTRACE_FORK | PTRACE_VFORK + | PTRACE_VFORK_DONE; + SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, tracee, &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, tracee, (void *)1, 0) != -1); + + DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, tracee); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); + + validate_status_stopped(status, SIGTRAP); + + SYSCALL_REQUIRE( + ptrace(PT_GET_PROCESS_STATE, tracee, &state, slen) != -1); + + if (strcmp(fn, "spawn") == 0) + op = PTRACE_POSIX_SPAWN; + else if (strcmp(fn, "fork") == 0) + op = PTRACE_FORK; + else + op = PTRACE_VFORK; + + ATF_REQUIRE_EQ(state.pe_report_event & op, op); + + tracee2 = state.pe_other_pid; + DPRINTF("Reported ptrace event with forkee %d\n", tracee2); + if (strcmp(fn, "spawn") == 0 || strcmp(fn, "fork") == 0 || + strcmp(fn, "vfork") == 0) + op = kill_process ? PT_KILL : PT_DETACH; + else + op = PT_CONTINUE; + SYSCALL_REQUIRE(ptrace(op, tracee, (void *)1, 0) != -1); + + DPRINTF("Before calling %s() for the forkee %d of the tracee %d\n", + TWAIT_FNAME, tracee2, tracee); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee2, &status, 0), tracee2); + + validate_status_stopped(status, SIGTRAP); + + SYSCALL_REQUIRE( + ptrace(PT_GET_PROCESS_STATE, tracee2, &state, slen) != -1); + if (strcmp(fn, "spawn") == 0) + op = PTRACE_POSIX_SPAWN; + else if (strcmp(fn, "fork") == 0) + op = PTRACE_FORK; + else + op = PTRACE_VFORK; + + ATF_REQUIRE_EQ(state.pe_report_event & op, op); + ATF_REQUIRE_EQ(state.pe_other_pid, tracee); + + DPRINTF("Before resuming the forkee process where it left off " + "and without signal to be sent\n"); + SYSCALL_REQUIRE( + ptrace(PT_CONTINUE, tracee2, (void *)1, 0) != -1); + + if (strcmp(fn, "vforkdone") == 0) { + DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, + tracee); + TWAIT_REQUIRE_SUCCESS( + wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); + + validate_status_stopped(status, SIGTRAP); + + SYSCALL_REQUIRE( + ptrace(PT_GET_PROCESS_STATE, tracee, &state, slen) != -1); + ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE); + + tracee2 = state.pe_other_pid; + DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", + tracee2); + + op = kill_process ? PT_KILL : PT_DETACH; + DPRINTF("Before resuming the child process where it left off " + "and without signal to be sent\n"); + SYSCALL_REQUIRE(ptrace(op, tracee, (void *)1, 0) != -1); + } + + DPRINTF("Before calling %s() for the forkee - expected exited\n", + TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee2, &status, 0), tracee2); + + 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(tracee2, &status, 0)); + + if (kill_process) { + DPRINTF("Before calling %s() for the forkee - expected signaled\n", + TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); + + validate_status_signaled(status, SIGKILL, 0); + } + + /* Inform parent that tracer is exiting normally */ + CHILD_TO_PARENT("tracer done", parent_tracer, msg); + + DPRINTF("Before exiting of the tracer process\n"); + _exit(0 /* collect by initproc */); + } + + DPRINTF("Wait for the tracer process (direct child) to exit " + "calling %s()\n", TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS( + wpid = TWAIT_GENERIC(tracer, &status, 0), tracer); + + validate_status_exited(status, exitval); + + DPRINTF("Wait for the non-exited tracee process with %s()\n", + TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS( + wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0); + + DPRINTF("Wait for the tracer to attach to the tracee\n"); + PARENT_FROM_CHILD("tracer ready", parent_tracer, msg); + + DPRINTF("Resume the tracee and let it crash\n"); + PARENT_TO_CHILD("exit tracee", parent_tracee, msg); + + DPRINTF("Resume the tracer and let it detect crashed tracee\n"); + PARENT_TO_CHILD("Message 2", parent_tracer, msg); + + DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n", + TWAIT_FNAME); + TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0), tracee); + + if (kill_process) { + validate_status_signaled(status, SIGKILL, 0); + } else { + validate_status_exited(status, exitval); + } + + DPRINTF("Await normal exit of tracer\n"); + PARENT_FROM_CHILD("tracer done", parent_tracer, msg); + + msg_close(&parent_tracer); + msg_close(&parent_tracee); +} + +#define UNRELATED_TRACER_FORK_DETACH_FORKER(name,event,kprocess) \ +ATF_TC(name); \ +ATF_TC_HEAD(name, tc) \ +{ \ + atf_tc_set_md_var(tc, "descr", "Verify %s " event, \ + kprocess ? "killed" : "detached"); \ +} \ + \ +ATF_TC_BODY(name, tc) \ +{ \ + \ + unrelated_tracer_fork_detach_forker_body(event, kprocess); \ +} + +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_posix_spawn_detach_spawner, "spawn", false) +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_fork_detach_forker, "fork", false) +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_vfork_detach_vforker, "vfork", false) +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_vfork_detach_vforkerdone, "vforkdone", false) + +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_posix_spawn_kill_spawner, "spawn", true) +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_fork_kill_forker, "fork", true) +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_vfork_kill_vforker, "vfork", true) +UNRELATED_TRACER_FORK_DETACH_FORKER(unrelated_tracer_vfork_kill_vforkerdone, "vforkdone", true) +#endif + +/// ---------------------------------------------------------------------------- + static void traceme_vfork_fork_body(pid_t (*fn)(void)) { @@ -8784,6 +9070,16 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC_HAVE_PID(tp, vfork_kill_vforker); ATF_TP_ADD_TC_HAVE_PID(tp, vfork_kill_vforkerdone); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_posix_spawn_detach_spawner); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_fork_detach_forker); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_vfork_detach_vforker); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_vfork_detach_vforkerdone); + + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_posix_spawn_kill_spawner); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_fork_kill_forker); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_vfork_kill_vforker); + ATF_TP_ADD_TC_HAVE_PID(tp, unrelated_tracer_vfork_kill_vforkerdone); + ATF_TP_ADD_TC(tp, traceme_vfork_fork); ATF_TP_ADD_TC(tp, traceme_vfork_vfork);