Tue Feb 4 21:34:12 2020 UTC ()
Add new ATF ptrace(2) test in t_ptrace_wait*

threads_and_exec - verify that the expected LWP events are reported for a
multithreaded process that calls execve(2).

Test passes.


(kamil)
diff -r1.150 -r1.151 src/tests/lib/libc/sys/t_ptrace_wait.c

cvs diff -r1.150 -r1.151 src/tests/lib/libc/sys/t_ptrace_wait.c (expand / switch to unified diff)

--- src/tests/lib/libc/sys/t_ptrace_wait.c 2020/02/04 15:06:27 1.150
+++ src/tests/lib/libc/sys/t_ptrace_wait.c 2020/02/04 21:34:12 1.151
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_ptrace_wait.c,v 1.150 2020/02/04 15:06:27 kamil Exp $ */ 1/* $NetBSD: t_ptrace_wait.c,v 1.151 2020/02/04 21:34:12 kamil Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc. 4 * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.150 2020/02/04 15:06:27 kamil Exp $"); 30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.151 2020/02/04 21:34:12 kamil Exp $");
31 31
32#define __LEGACY_PT_LWPINFO 32#define __LEGACY_PT_LWPINFO
33 33
34#include <sys/param.h> 34#include <sys/param.h>
35#include <sys/types.h> 35#include <sys/types.h>
36#include <sys/exec_elf.h> 36#include <sys/exec_elf.h>
37#include <sys/mman.h> 37#include <sys/mman.h>
38#include <sys/ptrace.h> 38#include <sys/ptrace.h>
39#include <sys/resource.h> 39#include <sys/resource.h>
40#include <sys/stat.h> 40#include <sys/stat.h>
41#include <sys/syscall.h> 41#include <sys/syscall.h>
42#include <sys/sysctl.h> 42#include <sys/sysctl.h>
43#include <sys/uio.h> 43#include <sys/uio.h>
@@ -7038,26 +7038,215 @@ ATF_TC_BODY(name, tc) \ @@ -7038,26 +7038,215 @@ ATF_TC_BODY(name, tc) \
7038 7038
7039FORK2_TEST(posix_spawn_singalmasked, "spawn", true, false) 7039FORK2_TEST(posix_spawn_singalmasked, "spawn", true, false)
7040FORK2_TEST(posix_spawn_singalignored, "spawn", false, true) 7040FORK2_TEST(posix_spawn_singalignored, "spawn", false, true)
7041FORK2_TEST(fork_singalmasked, "fork", true, false) 7041FORK2_TEST(fork_singalmasked, "fork", true, false)
7042FORK2_TEST(fork_singalignored, "fork", false, true) 7042FORK2_TEST(fork_singalignored, "fork", false, true)
7043FORK2_TEST(vfork_singalmasked, "vfork", true, false) 7043FORK2_TEST(vfork_singalmasked, "vfork", true, false)
7044FORK2_TEST(vfork_singalignored, "vfork", false, true) 7044FORK2_TEST(vfork_singalignored, "vfork", false, true)
7045FORK2_TEST(vforkdone_singalmasked, "vforkdone", true, false) 7045FORK2_TEST(vforkdone_singalmasked, "vforkdone", true, false)
7046FORK2_TEST(vforkdone_singalignored, "vforkdone", false, true) 7046FORK2_TEST(vforkdone_singalignored, "vforkdone", false, true)
7047#endif 7047#endif
7048 7048
7049/// ---------------------------------------------------------------------------- 7049/// ----------------------------------------------------------------------------
7050 7050
 7051static void *
 7052thread_and_exec_thread_cb(void *arg __unused)
 7053{
 7054
 7055 execlp("/bin/echo", "/bin/echo", NULL);
 7056
 7057 abort();
 7058}
 7059
 7060static void
 7061threads_and_exec(void)
 7062{
 7063 const int sigval = SIGSTOP;
 7064 pid_t child, wpid;
 7065#if defined(TWAIT_HAVE_STATUS)
 7066 int status;
 7067#endif
 7068 ptrace_state_t state;
 7069 const int slen = sizeof(state);
 7070 ptrace_event_t event;
 7071 const int elen = sizeof(event);
 7072 struct ptrace_siginfo info;
 7073
 7074 pthread_t t;
 7075 lwpid_t lid;
 7076
 7077 DPRINTF("Before forking process PID=%d\n", getpid());
 7078 SYSCALL_REQUIRE((child = fork()) != -1);
 7079 if (child == 0) {
 7080 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
 7081 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
 7082
 7083 DPRINTF("Before raising %s from child\n", strsignal(sigval));
 7084 FORKEE_ASSERT(raise(sigval) == 0);
 7085
 7086 FORKEE_ASSERT(pthread_create(&t, NULL,
 7087 thread_and_exec_thread_cb, NULL) == 0);
 7088
 7089 for (;;)
 7090 continue;
 7091
 7092 FORKEE_ASSERT(0 && "Not reached");
 7093 }
 7094 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
 7095
 7096 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
 7097 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 7098
 7099 validate_status_stopped(status, sigval);
 7100
 7101 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
 7102 SYSCALL_REQUIRE(
 7103 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
 7104
 7105 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
 7106 DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
 7107 info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
 7108 info.psi_siginfo.si_errno);
 7109
 7110 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
 7111 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
 7112
 7113 DPRINTF("Set LWP event mask for the child %d\n", child);
 7114 memset(&event, 0, sizeof(event));
 7115 event.pe_set_event |= PTRACE_LWP_CREATE | PTRACE_LWP_EXIT;
 7116 SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
 7117
 7118 DPRINTF("Before resuming the child process where it left off and "
 7119 "without signal to be sent\n");
 7120 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 7121
 7122 DPRINTF("Before calling %s() for the child - expected stopped "
 7123 "SIGTRAP\n", TWAIT_FNAME);
 7124 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
 7125 child);
 7126
 7127 validate_status_stopped(status, SIGTRAP);
 7128
 7129 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
 7130 "child\n");
 7131 SYSCALL_REQUIRE(
 7132 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
 7133
 7134 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
 7135 DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
 7136 "si_errno=%#x\n",
 7137 info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
 7138 info.psi_siginfo.si_errno);
 7139
 7140 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
 7141 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_LWP);
 7142
 7143 SYSCALL_REQUIRE(
 7144 ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
 7145
 7146 ATF_REQUIRE_EQ_MSG(state.pe_report_event, PTRACE_LWP_CREATE,
 7147 "%d != %d", state.pe_report_event, PTRACE_LWP_CREATE);
 7148
 7149 lid = state.pe_lwp;
 7150 DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
 7151
 7152 DPRINTF("Before resuming the child process where it left off "
 7153 "and without signal to be sent\n");
 7154 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 7155
 7156 DPRINTF("Before calling %s() for the child - expected stopped "
 7157 "SIGTRAP\n", TWAIT_FNAME);
 7158 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
 7159 child);
 7160
 7161 validate_status_stopped(status, SIGTRAP);
 7162
 7163 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
 7164 "child\n");
 7165 SYSCALL_REQUIRE(
 7166 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
 7167
 7168 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
 7169 DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
 7170 "si_errno=%#x\n",
 7171 info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
 7172 info.psi_siginfo.si_errno);
 7173
 7174 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
 7175 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_LWP);
 7176
 7177 SYSCALL_REQUIRE(
 7178 ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
 7179
 7180 ATF_REQUIRE_EQ_MSG(state.pe_report_event, PTRACE_LWP_EXIT,
 7181 "%d != %d", state.pe_report_event, PTRACE_LWP_EXIT);
 7182
 7183 lid = state.pe_lwp;
 7184 DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
 7185
 7186 DPRINTF("Before resuming the child process where it left off "
 7187 "and without signal to be sent\n");
 7188 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 7189
 7190 DPRINTF("Before calling %s() for the child - expected stopped "
 7191 "SIGTRAP\n", TWAIT_FNAME);
 7192 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
 7193 child);
 7194
 7195 validate_status_stopped(status, SIGTRAP);
 7196
 7197 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for "
 7198 "child\n");
 7199 SYSCALL_REQUIRE(
 7200 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
 7201
 7202 DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
 7203 DPRINTF("Signal properties: si_signo=%#x si_code=%#x "
 7204 "si_errno=%#x\n",
 7205 info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
 7206 info.psi_siginfo.si_errno);
 7207
 7208 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
 7209 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
 7210
 7211 SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
 7212
 7213 DPRINTF("Before calling %s() for the child - expected exited\n",
 7214 TWAIT_FNAME);
 7215 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 7216
 7217 validate_status_signaled(status, SIGKILL, 0);
 7218
 7219 DPRINTF("Before calling %s() for the child - expected no process\n",
 7220 TWAIT_FNAME);
 7221 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
 7222}
 7223
 7224ATF_TC(threads_and_exec);
 7225ATF_TC_HEAD(threads_and_exec, tc)
 7226{
 7227 atf_tc_set_md_var(tc, "descr",
 7228 "Verify that multithreaded application on exec() will report "
 7229 "LWP_EXIT events");
 7230}
 7231
 7232ATF_TC_BODY(threads_and_exec, tc)
 7233{
 7234
 7235 threads_and_exec();
 7236}
 7237
 7238/// ----------------------------------------------------------------------------
 7239
7051volatile lwpid_t the_lwp_id = 0; 7240volatile lwpid_t the_lwp_id = 0;
7052 7241
7053static void 7242static void
7054lwp_main_func(void *arg) 7243lwp_main_func(void *arg)
7055{ 7244{
7056 the_lwp_id = _lwp_self(); 7245 the_lwp_id = _lwp_self();
7057 _lwp_exit(); 7246 _lwp_exit();
7058} 7247}
7059 7248
7060ATF_TC(signal9); 7249ATF_TC(signal9);
7061ATF_TC_HEAD(signal9, tc) 7250ATF_TC_HEAD(signal9, tc)
7062{ 7251{
7063 atf_tc_set_md_var(tc, "descr", 7252 atf_tc_set_md_var(tc, "descr",
@@ -9251,26 +9440,28 @@ ATF_TP_ADD_TCS(tp) @@ -9251,26 +9440,28 @@ ATF_TP_ADD_TCS(tp)
9251 ATF_TP_ADD_TC(tp, trace_thread_lwpcreate_and_exit); 9440 ATF_TP_ADD_TC(tp, trace_thread_lwpcreate_and_exit);
9252 9441
9253 ATF_TP_ADD_TC(tp, signal_mask_unrelated); 9442 ATF_TP_ADD_TC(tp, signal_mask_unrelated);
9254 9443
9255 ATF_TP_ADD_TC_HAVE_PID(tp, posix_spawn_singalmasked); 9444 ATF_TP_ADD_TC_HAVE_PID(tp, posix_spawn_singalmasked);
9256 ATF_TP_ADD_TC_HAVE_PID(tp, posix_spawn_singalignored); 9445 ATF_TP_ADD_TC_HAVE_PID(tp, posix_spawn_singalignored);
9257 ATF_TP_ADD_TC_HAVE_PID(tp, fork_singalmasked); 9446 ATF_TP_ADD_TC_HAVE_PID(tp, fork_singalmasked);
9258 ATF_TP_ADD_TC_HAVE_PID(tp, fork_singalignored); 9447 ATF_TP_ADD_TC_HAVE_PID(tp, fork_singalignored);
9259 ATF_TP_ADD_TC_HAVE_PID(tp, vfork_singalmasked); 9448 ATF_TP_ADD_TC_HAVE_PID(tp, vfork_singalmasked);
9260 ATF_TP_ADD_TC_HAVE_PID(tp, vfork_singalignored); 9449 ATF_TP_ADD_TC_HAVE_PID(tp, vfork_singalignored);
9261 ATF_TP_ADD_TC_HAVE_PID(tp, vforkdone_singalmasked); 9450 ATF_TP_ADD_TC_HAVE_PID(tp, vforkdone_singalmasked);
9262 ATF_TP_ADD_TC_HAVE_PID(tp, vforkdone_singalignored); 9451 ATF_TP_ADD_TC_HAVE_PID(tp, vforkdone_singalignored);
9263 9452
 9453 ATF_TP_ADD_TC(tp, threads_and_exec);
 9454
9264 ATF_TP_ADD_TC(tp, signal9); 9455 ATF_TP_ADD_TC(tp, signal9);
9265 ATF_TP_ADD_TC(tp, signal10); 9456 ATF_TP_ADD_TC(tp, signal10);
9266 9457
9267 ATF_TP_ADD_TC(tp, suspend2); 9458 ATF_TP_ADD_TC(tp, suspend2);
9268 9459
9269 ATF_TP_ADD_TC(tp, resume1); 9460 ATF_TP_ADD_TC(tp, resume1);
9270 9461
9271 ATF_TP_ADD_TC(tp, syscall1); 9462 ATF_TP_ADD_TC(tp, syscall1);
9272 9463
9273 ATF_TP_ADD_TC(tp, syscallemu1); 9464 ATF_TP_ADD_TC(tp, syscallemu1);
9274 9465
9275 ATF_TP_ADD_TC(tp, clone1); 9466 ATF_TP_ADD_TC(tp, clone1);
9276 ATF_TP_ADD_TC_HAVE_PID(tp, clone2); 9467 ATF_TP_ADD_TC_HAVE_PID(tp, clone2);