t_ptrace_i386_wait.h revision 1.6
1/* $NetBSD: t_ptrace_i386_wait.h,v 1.6 2019/05/05 18:15:40 mgorny Exp $ */ 2 3/*- 4 * Copyright (c) 2016, 2017, 2018, 2019 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(__i386__) 30ATF_TC(i386_regs1); 31ATF_TC_HEAD(i386_regs1, tc) 32{ 33 atf_tc_set_md_var(tc, "descr", 34 "Call PT_GETREGS and iterate over General Purpose registers"); 35} 36 37ATF_TC_BODY(i386_regs1, tc) 38{ 39 const int exitval = 5; 40 const int sigval = SIGSTOP; 41 pid_t child, wpid; 42#if defined(TWAIT_HAVE_STATUS) 43 int status; 44#endif 45 struct reg r; 46 47 DPRINTF("Before forking process PID=%d\n", getpid()); 48 SYSCALL_REQUIRE((child = fork()) != -1); 49 if (child == 0) { 50 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 51 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 52 53 DPRINTF("Before raising %s from child\n", strsignal(sigval)); 54 FORKEE_ASSERT(raise(sigval) == 0); 55 56 DPRINTF("Before exiting of the child process\n"); 57 _exit(exitval); 58 } 59 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 60 61 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 62 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 63 64 validate_status_stopped(status, sigval); 65 66 DPRINTF("Call GETREGS for the child process\n"); 67 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 68 69 DPRINTF("EAX=%#" PRIxREGISTER "\n", r.r_eax); 70 DPRINTF("EBX=%#" PRIxREGISTER "\n", r.r_ebx); 71 DPRINTF("ECX=%#" PRIxREGISTER "\n", r.r_ecx); 72 DPRINTF("EDX=%#" PRIxREGISTER "\n", r.r_edx); 73 74 DPRINTF("ESP=%#" PRIxREGISTER "\n", r.r_esp); 75 DPRINTF("EBP=%#" PRIxREGISTER "\n", r.r_ebp); 76 77 DPRINTF("ESI=%#" PRIxREGISTER "\n", r.r_esi); 78 DPRINTF("EDI=%#" PRIxREGISTER "\n", r.r_edi); 79 80 DPRINTF("EIP=%#" PRIxREGISTER "\n", r.r_eip); 81 82 DPRINTF("EFLAGS=%#" PRIxREGISTER "\n", r.r_eflags); 83 84 DPRINTF("CS=%#" PRIxREGISTER "\n", r.r_cs); 85 DPRINTF("SS=%#" PRIxREGISTER "\n", r.r_ss); 86 DPRINTF("DS=%#" PRIxREGISTER "\n", r.r_ds); 87 DPRINTF("ES=%#" PRIxREGISTER "\n", r.r_es); 88 DPRINTF("FS=%#" PRIxREGISTER "\n", r.r_fs); 89 DPRINTF("GS=%#" PRIxREGISTER "\n", r.r_gs); 90 91 DPRINTF("Before resuming the child process where it left off and " 92 "without signal to be sent\n"); 93 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 94 95 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 96 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 97 98 validate_status_exited(status, exitval); 99 100 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 101 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 102} 103 104ATF_TC(i386_regs_gp_read); 105ATF_TC_HEAD(i386_regs_gp_read, tc) 106{ 107 atf_tc_set_md_var(tc, "descr", 108 "Set general-purpose reg values from debugged program and read " 109 "them via PT_GETREGS, comparing values against expected."); 110} 111 112ATF_TC_BODY(i386_regs_gp_read, tc) 113{ 114 const int exitval = 5; 115 const int sigval = SIGTRAP; 116 pid_t child, wpid; 117#if defined(TWAIT_HAVE_STATUS) 118 int status; 119#endif 120 struct reg gpr; 121 122 const uint32_t eax = 0x00010203; 123 const uint32_t ebx = 0x10111213; 124 const uint32_t ecx = 0x20212223; 125 const uint32_t edx = 0x30313233; 126 const uint32_t esi = 0x40414243; 127 const uint32_t edi = 0x50515253; 128 129 DPRINTF("Before forking process PID=%d\n", getpid()); 130 SYSCALL_REQUIRE((child = fork()) != -1); 131 if (child == 0) { 132 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 133 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 134 135 DPRINTF("Before running assembly from child\n"); 136 137 __asm__ __volatile__( 138 "int3\n\t" 139 : 140 : "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi) 141 : 142 ); 143 144 DPRINTF("Before exiting of the child process\n"); 145 _exit(exitval); 146 } 147 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 148 149 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 150 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 151 152 validate_status_stopped(status, sigval); 153 154 DPRINTF("Call GETREGS for the child process\n"); 155 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1); 156 157 ATF_CHECK_EQ((uint32_t)gpr.r_eax, eax); 158 ATF_CHECK_EQ((uint32_t)gpr.r_ebx, ebx); 159 ATF_CHECK_EQ((uint32_t)gpr.r_ecx, ecx); 160 ATF_CHECK_EQ((uint32_t)gpr.r_edx, edx); 161 ATF_CHECK_EQ((uint32_t)gpr.r_esi, esi); 162 ATF_CHECK_EQ((uint32_t)gpr.r_edi, edi); 163 164 DPRINTF("Before resuming the child process where it left off and " 165 "without signal to be sent\n"); 166 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 167 168 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 169 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 170 171 validate_status_exited(status, exitval); 172 173 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 174 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 175} 176 177ATF_TC(i386_regs_ebp_esp_read); 178ATF_TC_HEAD(i386_regs_ebp_esp_read, tc) 179{ 180 atf_tc_set_md_var(tc, "descr", 181 "Set EBP & ESP reg values from debugged program and read " 182 "them via PT_GETREGS, comparing values against expected."); 183} 184 185ATF_TC_BODY(i386_regs_ebp_esp_read, tc) 186{ 187 const int exitval = 5; 188 const int sigval = SIGTRAP; 189 pid_t child, wpid; 190#if defined(TWAIT_HAVE_STATUS) 191 int status; 192#endif 193 struct reg gpr; 194 195 const uint32_t esp = 0x60616263; 196 const uint32_t ebp = 0x70717273; 197 198 DPRINTF("Before forking process PID=%d\n", getpid()); 199 SYSCALL_REQUIRE((child = fork()) != -1); 200 if (child == 0) { 201 DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid()); 202 FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 203 204 DPRINTF("Before running assembly from child\n"); 205 206 __asm__ __volatile__( 207 /* ebp & ebp are a bit tricky, we must not clobber them */ 208 "movl %%esp, %%eax\n\t" 209 "movl %%ebp, %%ebx\n\t" 210 "movl %0, %%esp\n\t" 211 "movl %1, %%ebp\n\t" 212 "\n\t" 213 "int3\n\t" 214 "\n\t" 215 "movl %%eax, %%esp\n\t" 216 "movl %%ebx, %%ebp\n\t" 217 : 218 : "ri"(esp), "ri"(ebp) 219 : "%eax", "%ebx" 220 ); 221 222 DPRINTF("Before exiting of the child process\n"); 223 _exit(exitval); 224 } 225 DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child); 226 227 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 228 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 229 230 validate_status_stopped(status, sigval); 231 232 DPRINTF("Call GETREGS for the child process\n"); 233 SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1); 234 235 ATF_CHECK_EQ((uint32_t)gpr.r_esp, esp); 236 ATF_CHECK_EQ((uint32_t)gpr.r_ebp, ebp); 237 238 DPRINTF("Before resuming the child process where it left off and " 239 "without signal to be sent\n"); 240 SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 241 242 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 243 TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 244 245 validate_status_exited(status, exitval); 246 247 DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); 248 TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 249} 250 251#define ATF_TP_ADD_TCS_PTRACE_WAIT_I386() \ 252 ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs1); \ 253 ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs_gp_read); \ 254 ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs_ebp_esp_read); 255#else 256#define ATF_TP_ADD_TCS_PTRACE_WAIT_I386() 257#endif 258