t_ptrace_step_wait.h revision 1.1
1/* $NetBSD: t_ptrace_step_wait.h,v 1.1 2020/05/04 21:33:20 kamil Exp $ */ 2 3/*- 4 * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 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 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 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#if defined(PT_STEP) 30static void 31ptrace_step(int N, int setstep, bool masked, bool ignored) 32{ 33 const int exitval = 5; 34 const int sigval = SIGSTOP; 35 pid_t child, wpid; 36#if defined(TWAIT_HAVE_STATUS) 37 int status; 38#endif 39 int happy; 40 struct sigaction sa; 41 struct ptrace_siginfo info; 42 sigset_t intmask; 43 struct kinfo_proc2 kp; 44 size_t len = sizeof(kp); 45 46 int name[6]; 47 const size_t namelen = __arraycount(name); 48 ki_sigset_t kp_sigmask; 49 ki_sigset_t kp_sigignore; 50 51#if defined(__arm__) 52 /* PT_STEP not supported on arm 32-bit */ 53 atf_tc_expect_fail("PR kern/52119"); 54#endif 55 56 DPRINTF("Before forking process PID=%d\n", getpid()); 57 SYSCALL_REQUIRE((child = fork()) != -1); 58 if (child == 0) { 59 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 60 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 61 62 if (masked) { 63 sigemptyset(&intmask); 64 sigaddset(&intmask, SIGTRAP); 65 sigprocmask(SIG_BLOCK, &intmask, NULL); 66 } 67 68 if (ignored) { 69 memset(&sa, 0, sizeof(sa)); 70 sa.sa_handler = SIG_IGN; 71 sigemptyset(&sa.sa_mask); 72 FORKEE_ASSERT(sigaction(SIGTRAP, &sa, NULL) != -1); 73 } 74 75 happy = check_happy(999); 76 77 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 78 FORKEE_ASSERT(raise(sigval) == 0); 79 80 FORKEE_ASSERT_EQ(happy, check_happy(999)); 81 82 DPRINTF("Before exiting of the child process\n"); 83 _exit(exitval); 84 } 85 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 86 87 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 88 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 89 90 validate_status_stopped(status, sigval); 91 92 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 93 SYSCALL_REQUIRE( 94 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 95 96 DPRINTF("Before checking siginfo_t\n"); 97 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval); 98 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP); 99 100 name[0] = CTL_KERN, 101 name[1] = KERN_PROC2, 102 name[2] = KERN_PROC_PID; 103 name[3] = child; 104 name[4] = sizeof(kp); 105 name[5] = 1; 106 107 FORKEE_ASSERT_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0); 108 109 if (masked) 110 kp_sigmask = kp.p_sigmask; 111 112 if (ignored) 113 kp_sigignore = kp.p_sigignore; 114 115 while (N --> 0) { 116 if (setstep) { 117 DPRINTF("Before resuming the child process where it " 118 "left off and without signal to be sent (use " 119 "PT_SETSTEP and PT_CONTINUE)\n"); 120 SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1); 121 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) 122 != -1); 123 } else { 124 DPRINTF("Before resuming the child process where it " 125 "left off and without signal to be sent (use " 126 "PT_STEP)\n"); 127 SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) 128 != -1); 129 } 130 131 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 132 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), 133 child); 134 135 validate_status_stopped(status, SIGTRAP); 136 137 DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); 138 SYSCALL_REQUIRE( 139 ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1); 140 141 DPRINTF("Before checking siginfo_t\n"); 142 ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP); 143 ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE); 144 145 if (setstep) { 146 SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1); 147 } 148 149 ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0); 150 151 if (masked) { 152 DPRINTF("kp_sigmask=" 153 "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" 154 PRIx32 "\n", 155 kp_sigmask.__bits[0], kp_sigmask.__bits[1], 156 kp_sigmask.__bits[2], kp_sigmask.__bits[3]); 157 158 DPRINTF("kp.p_sigmask=" 159 "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" 160 PRIx32 "\n", 161 kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1], 162 kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]); 163 164 ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask, 165 sizeof(kp_sigmask))); 166 } 167 168 if (ignored) { 169 DPRINTF("kp_sigignore=" 170 "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" 171 PRIx32 "\n", 172 kp_sigignore.__bits[0], kp_sigignore.__bits[1], 173 kp_sigignore.__bits[2], kp_sigignore.__bits[3]); 174 175 DPRINTF("kp.p_sigignore=" 176 "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" 177 PRIx32 "\n", 178 kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1], 179 kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]); 180 181 ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore, 182 sizeof(kp_sigignore))); 183 } 184 } 185 186 DPRINTF("Before resuming the child process where it left off and " 187 "without signal to be sent\n"); 188 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 189 190 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 191 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 192 193 validate_status_exited(status, exitval); 194 195 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 196 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 197} 198 199#define PTRACE_STEP(test, N, setstep) \ 200ATF_TC(test); \ 201ATF_TC_HEAD(test, tc) \ 202{ \ 203 atf_tc_set_md_var(tc, "descr", \ 204 "Verify " #N " (PT_SETSTEP set to: " #setstep ")"); \ 205} \ 206 \ 207ATF_TC_BODY(test, tc) \ 208{ \ 209 \ 210 ptrace_step(N, setstep, false, false); \ 211} 212 213PTRACE_STEP(step1, 1, 0) 214PTRACE_STEP(step2, 2, 0) 215PTRACE_STEP(step3, 3, 0) 216PTRACE_STEP(step4, 4, 0) 217PTRACE_STEP(setstep1, 1, 1) 218PTRACE_STEP(setstep2, 2, 1) 219PTRACE_STEP(setstep3, 3, 1) 220PTRACE_STEP(setstep4, 4, 1) 221 222ATF_TC(step_signalmasked); 223ATF_TC_HEAD(step_signalmasked, tc) 224{ 225 atf_tc_set_md_var(tc, "descr", "Verify PT_STEP with masked SIGTRAP"); 226} 227 228ATF_TC_BODY(step_signalmasked, tc) 229{ 230 231 ptrace_step(1, 0, true, false); 232} 233 234ATF_TC(step_signalignored); 235ATF_TC_HEAD(step_signalignored, tc) 236{ 237 atf_tc_set_md_var(tc, "descr", "Verify PT_STEP with ignored SIGTRAP"); 238} 239 240ATF_TC_BODY(step_signalignored, tc) 241{ 242 243 ptrace_step(1, 0, false, true); 244} 245#endif 246 247#define ATF_TP_ADD_TCS_PTRACE_WAIT_STEP() \ 248 ATF_TP_ADD_TC_PT_STEP(tp, step1); \ 249 ATF_TP_ADD_TC_PT_STEP(tp, step2); \ 250 ATF_TP_ADD_TC_PT_STEP(tp, step3); \ 251 ATF_TP_ADD_TC_PT_STEP(tp, step4); \ 252 ATF_TP_ADD_TC_PT_STEP(tp, setstep1); \ 253 ATF_TP_ADD_TC_PT_STEP(tp, setstep2); \ 254 ATF_TP_ADD_TC_PT_STEP(tp, setstep3); \ 255 ATF_TP_ADD_TC_PT_STEP(tp, setstep4); \ 256 ATF_TP_ADD_TC_PT_STEP(tp, step_signalmasked); \ 257 ATF_TP_ADD_TC_PT_STEP(tp, step_signalignored); 258