Re: [PATCH v5 3/4] Combine x86 register tests into unified test function

2019-12-24 Thread Christos Zoulas
In article <20191224114717.302420-3-mgo...@gentoo.org>,
MichaŠ Górny   wrote:
>Reduce the code duplication and improve maintainability of x86 register
>tests by combining all of them to a single base function.
>---
> tests/lib/libc/sys/t_ptrace_amd64_wait.h |  406 +---
> tests/lib/libc/sys/t_ptrace_i386_wait.h  |  335 +--
> tests/lib/libc/sys/t_ptrace_x86_wait.h   | 2417 ++
> 3 files changed, 1103 insertions(+), 2055 deletions(-)

That's a good start, but I'd also start using loops for some of the
repetitive code for brevity.

christos



[PATCH v5 3/4] Combine x86 register tests into unified test function

2019-12-24 Thread Michał Górny
Reduce the code duplication and improve maintainability of x86 register
tests by combining all of them to a single base function.
---
 tests/lib/libc/sys/t_ptrace_amd64_wait.h |  406 +---
 tests/lib/libc/sys/t_ptrace_i386_wait.h  |  335 +--
 tests/lib/libc/sys/t_ptrace_x86_wait.h   | 2417 ++
 3 files changed, 1103 insertions(+), 2055 deletions(-)

diff --git a/tests/lib/libc/sys/t_ptrace_amd64_wait.h 
b/tests/lib/libc/sys/t_ptrace_amd64_wait.h
index 1ea17ea1ec1a..0f410f3600d0 100644
--- a/tests/lib/libc/sys/t_ptrace_amd64_wait.h
+++ b/tests/lib/libc/sys/t_ptrace_amd64_wait.h
@@ -111,415 +111,11 @@ ATF_TC_BODY(x86_64_regs1, tc)
TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, , 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;
-   pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-   const int sigval = SIGTRAP;
-   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, , 0), child);
-
-   validate_status_stopped(status, sigval);
-
-   DPRINTF("Call GETREGS for the child process\n");
-   SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, , 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, , 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, , 0));
-}
-
-ATF_TC(x86_64_regs_gp_write);
-ATF_TC_HEAD(x86_64_regs_gp_write, tc)
-{
-   atf_tc_set_md_var(tc, "descr",
-   "Set general-purpose reg values into a debugged program via "
-   "PT_SETREGS and compare the result against expected.");
-}
-
-ATF_TC_BODY(x86_64_regs_gp_write, tc)
-{
-   const int exitval = 5;
-   pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-   const int sigval = SIGTRAP;
-   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;
-