Module Name: src Committed By: mgorny Date: Sun May 5 10:04:11 UTC 2019
Modified Files: src/tests/lib/libc/sys: t_ptrace_amd64_wait.h t_ptrace_i386_wait.h Log Message: Add tests for reading pre-set general purpose register values via PT_GETREGS. Add new tests verifying that PT_GETREGS provides correct register values from the underlying program. The test uses inline assembly in the child program to set registers to well-known constants, then compares the results obtained via PT_GETREGS against them. Reviewed by kamil. To generate a diff of this commit: cvs rdiff -u -r1.6 -r1.7 src/tests/lib/libc/sys/t_ptrace_amd64_wait.h cvs rdiff -u -r1.3 -r1.4 src/tests/lib/libc/sys/t_ptrace_i386_wait.h 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_amd64_wait.h diff -u src/tests/lib/libc/sys/t_ptrace_amd64_wait.h:1.6 src/tests/lib/libc/sys/t_ptrace_amd64_wait.h:1.7 --- src/tests/lib/libc/sys/t_ptrace_amd64_wait.h:1.6 Sun Feb 10 02:13:45 2019 +++ src/tests/lib/libc/sys/t_ptrace_amd64_wait.h Sun May 5 10:04:11 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_amd64_wait.h,v 1.6 2019/02/10 02:13:45 kamil Exp $ */ +/* $NetBSD: t_ptrace_amd64_wait.h,v 1.7 2019/05/05 10:04:11 mgorny Exp $ */ /*- * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc. @@ -111,11 +111,99 @@ ATF_TC_BODY(x86_64_regs1, tc) TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); } +ATF_TC(x86_64_regs_gp_read); +ATF_TC_HEAD(x86_64_regs_gp_read, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Set general-purpose reg values from debugged program and read " + "them via PT_GETREGS, comparing values against expected."); +} + +ATF_TC_BODY(x86_64_regs_gp_read, tc) +{ + const int exitval = 5; + const int sigval = SIGTRAP; + pid_t child, wpid; +#if defined(TWAIT_HAVE_STATUS) + int status; +#endif + struct reg gpr; + + const uint64_t rax = 0x0001020304050607; + const uint64_t rbx = 0x1011121314151617; + const uint64_t rcx = 0x2021222324252627; + const uint64_t rdx = 0x3031323334353637; + const uint64_t rsi = 0x4041424344454647; + const uint64_t rdi = 0x5051525354555657; + const uint64_t rsp = 0x6061626364656667; + const uint64_t rbp = 0x7071727374757677; + + 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 running assembly from child\n"); + + __asm__ __volatile__( + /* rbp & rbp are a bit tricky, we must not clobber them */ + "movq %%rsp, %%r8\n\t" + "movq %%rbp, %%r9\n\t" + "movq %6, %%rsp\n\t" + "movq %7, %%rbp\n\t" + "\n\t" + "int3\n\t" + "\n\t" + "movq %%r8, %%rsp\n\t" + "movq %%r9, %%rbp\n\t" + : + : "a"(rax), "b"(rbx), "c"(rcx), "d"(rdx), "S"(rsi), "D"(rdi), + "i"(rsp), "i"(rbp) + : "%r8", "%r9" + ); + + 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("Call GETREGS for the child process\n"); + SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1); + + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RAX], rax); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RBX], rbx); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RCX], rcx); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RDX], rdx); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RSI], rsi); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RDI], rdi); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RSP], rsp); + ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RBP], rbp); + + 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\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\n", TWAIT_FNAME); + TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); +} + /// ---------------------------------------------------------------------------- #define ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64() \ - ATF_TP_ADD_TC_HAVE_GPREGS(tp, x86_64_regs1); + ATF_TP_ADD_TC_HAVE_GPREGS(tp, x86_64_regs1); \ + ATF_TP_ADD_TC_HAVE_GPREGS(tp, x86_64_regs_gp_read); #else #define ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64() #endif Index: src/tests/lib/libc/sys/t_ptrace_i386_wait.h diff -u src/tests/lib/libc/sys/t_ptrace_i386_wait.h:1.3 src/tests/lib/libc/sys/t_ptrace_i386_wait.h:1.4 --- src/tests/lib/libc/sys/t_ptrace_i386_wait.h:1.3 Sun Feb 10 02:13:45 2019 +++ src/tests/lib/libc/sys/t_ptrace_i386_wait.h Sun May 5 10:04:11 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_i386_wait.h,v 1.3 2019/02/10 02:13:45 kamil Exp $ */ +/* $NetBSD: t_ptrace_i386_wait.h,v 1.4 2019/05/05 10:04:11 mgorny Exp $ */ /*- * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc. @@ -100,8 +100,97 @@ ATF_TC_BODY(i386_regs1, tc) DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); } + +ATF_TC(i386_regs_gp_read); +ATF_TC_HEAD(i386_regs_gp_read, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Set general-purpose reg values from debugged program and read " + "them via PT_GETREGS, comparing values against expected."); +} + +ATF_TC_BODY(i386_regs_gp_read, tc) +{ + const int exitval = 5; + const int sigval = SIGTRAP; + pid_t child, wpid; +#if defined(TWAIT_HAVE_STATUS) + int status; +#endif + struct reg gpr; + + const uint32_t eax = 0x00010203; + const uint32_t ebx = 0x10111213; + const uint32_t ecx = 0x20212223; + const uint32_t edx = 0x30313233; + const uint32_t esi = 0x40414243; + const uint32_t edi = 0x50515253; + const uint32_t esp = 0x60616263; + const uint32_t ebp = 0x70717273; + + 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 running assembly from child\n"); + + __asm__ __volatile__( + /* ebp & ebp are a bit tricky, we must not clobber them */ + "movd %%esp, %%mm0\n\t" + "movd %%ebp, %%mm1\n\t" + "movd %6, %%esp\n\t" + "movd %7, %%ebp\n\t" + "\n\t" + "int3\n\t" + "\n\t" + "movd %%mm0, %%esp\n\t" + "movd %%mm1, %%ebp\n\t" + : + : "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi), + "y"(esp), "y"(ebp) + : "%mm0", "%mm1" + ); + + 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("Call GETREGS for the child process\n"); + SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1); + + ATF_CHECK_EQ((uint32_t)gpr.r_eax, eax); + ATF_CHECK_EQ((uint32_t)gpr.r_ebx, ebx); + ATF_CHECK_EQ((uint32_t)gpr.r_ecx, ecx); + ATF_CHECK_EQ((uint32_t)gpr.r_edx, edx); + ATF_CHECK_EQ((uint32_t)gpr.r_esi, esi); + ATF_CHECK_EQ((uint32_t)gpr.r_edi, edi); + ATF_CHECK_EQ((uint32_t)gpr.r_esp, esp); + ATF_CHECK_EQ((uint32_t)gpr.r_ebp, ebp); + + 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\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\n", TWAIT_FNAME); + TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); +} + #define ATF_TP_ADD_TCS_PTRACE_WAIT_I386() \ - ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs1); + ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs1); \ + ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs_gp_read); #else #define ATF_TP_ADD_TCS_PTRACE_WAIT_I386() #endif