Module Name: src Committed By: kamil Date: Thu Mar 28 08:13:40 UTC 2019
Modified Files: src/tests/lib/libc/sys: t_ptrace_wait.c Log Message: Add new ATF ptrace(2) tests bytes_transfer_alignment* Verify bytes transfer for potentially misaligned operation. New tests: - bytes_transfer_alignment_pt_read_i - bytes_transfer_alignment_pt_read_d - bytes_transfer_alignment_pt_write_i - bytes_transfer_alignment_pt_write_d - bytes_transfer_alignment_piod_read_i - bytes_transfer_alignment_piod_read_d - bytes_transfer_alignment_piod_write_i - bytes_transfer_alignment_piod_write_d - bytes_transfer_alignment_piod_read_auxv All tests pass (at least on x86_64). To generate a diff of this commit: cvs rdiff -u -r1.100 -r1.101 src/tests/lib/libc/sys/t_ptrace_wait.c 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_wait.c diff -u src/tests/lib/libc/sys/t_ptrace_wait.c:1.100 src/tests/lib/libc/sys/t_ptrace_wait.c:1.101 --- src/tests/lib/libc/sys/t_ptrace_wait.c:1.100 Fri Mar 8 23:35:01 2019 +++ src/tests/lib/libc/sys/t_ptrace_wait.c Thu Mar 28 08:13:40 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_wait.c,v 1.100 2019/03/08 23:35:01 kamil Exp $ */ +/* $NetBSD: t_ptrace_wait.c,v 1.101 2019/03/28 08:13:40 kamil Exp $ */ /*- * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc. @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: t_ptrace_wait.c,v 1.100 2019/03/08 23:35:01 kamil Exp $"); +__RCSID("$NetBSD: t_ptrace_wait.c,v 1.101 2019/03/28 08:13:40 kamil Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -3667,6 +3667,185 @@ BYTES_TRANSFER(bytes_transfer_piod_read_ /// ---------------------------------------------------------------------------- +static void +bytes_transfer_alignment(const char *operation) +{ + const int exitval = 5; + const int sigval = SIGSTOP; + pid_t child, wpid; +#if defined(TWAIT_HAVE_STATUS) + int status; +#endif + char *buffer; + int vector; + size_t len; + size_t i; + int op; + + struct ptrace_io_desc io; + struct ptrace_siginfo info; + + memset(&io, 0, sizeof(io)); + memset(&info, 0, sizeof(info)); + + /* Testing misaligned byte transfer crossing page boundaries */ + len = sysconf(_SC_PAGESIZE) * 2; + buffer = malloc(len); + ATF_REQUIRE(buffer != NULL); + + /* Initialize the buffer with random data */ + for (i = 0; i < len; i++) + buffer[i] = i & 0xff; + + 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 raising %s from child\n", strsignal(sigval)); + FORKEE_ASSERT(raise(sigval) == 0); + + 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("Before calling ptrace(2) with PT_GET_SIGINFO for child\n"); + SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) + != -1); + + DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid); + DPRINTF("Signal properties: si_signo=%#x si_code=%#x " + "si_errno=%#x\n", + info.psi_siginfo.si_signo, info.psi_siginfo.si_code, + info.psi_siginfo.si_errno); + + ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval); + ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP); + + if (strcmp(operation, "PT_READ_I") == 0 || + strcmp(operation, "PT_READ_D") == 0) { + if (strcmp(operation, "PT_READ_I")) + op = PT_READ_I; + else + op = PT_READ_D; + + for (i = 0; i <= (len - sizeof(int)); i++) { + errno = 0; + vector = ptrace(op, child, buffer + i, 0); + ATF_REQUIRE_EQ(errno, 0); + ATF_REQUIRE(!memcmp(&vector, buffer + i, sizeof(int))); + } + } else if (strcmp(operation, "PT_WRITE_I") == 0 || + strcmp(operation, "PT_WRITE_D") == 0) { + if (strcmp(operation, "PT_WRITE_I")) + op = PT_WRITE_I; + else + op = PT_WRITE_D; + + for (i = 0; i <= (len - sizeof(int)); i++) { + memcpy(&vector, buffer + i, sizeof(int)); + SYSCALL_REQUIRE(ptrace(op, child, buffer + 1, vector) + != -1); + } + } else if (strcmp(operation, "PIOD_READ_I") == 0 || + strcmp(operation, "PIOD_READ_D") == 0) { + if (strcmp(operation, "PIOD_READ_I")) + op = PIOD_READ_I; + else + op = PIOD_READ_D; + + io.piod_op = op; + io.piod_addr = &vector; + io.piod_len = sizeof(int); + + for (i = 0; i <= (len - sizeof(int)); i++) { + io.piod_offs = buffer + i; + + SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io)) + != -1); + ATF_REQUIRE(!memcmp(&vector, buffer + i, sizeof(int))); + } + } else if (strcmp(operation, "PIOD_WRITE_I") == 0 || + strcmp(operation, "PIOD_WRITE_D") == 0) { + if (strcmp(operation, "PIOD_WRITE_I")) + op = PIOD_WRITE_I; + else + op = PIOD_WRITE_D; + + io.piod_op = op; + io.piod_addr = &vector; + io.piod_len = sizeof(int); + + for (i = 0; i <= (len - sizeof(int)); i++) { + io.piod_offs = buffer + i; + + SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io)) + != -1); + } + } else if (strcmp(operation, "PIOD_READ_AUXV") == 0) { + io.piod_op = PIOD_READ_AUXV; + io.piod_addr = &vector; + io.piod_len = sizeof(int); + + errno = 0; + i = 0; + /* Read the whole AUXV vector, it has no clear length */ + while (errno != EIO) { + io.piod_offs = (void *)(intptr_t)i; + SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, sizeof(io)) + != -1 || (errno == EIO && i > 0)); + ++i; + } + } + + 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); + + DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME); + TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); +} + +#define BYTES_TRANSFER_ALIGNMENT(test, operation) \ +ATF_TC(test); \ +ATF_TC_HEAD(test, tc) \ +{ \ + atf_tc_set_md_var(tc, "descr", \ + "Verify bytes transfer for potentially misaligned " \ + "operation " operation); \ +} \ + \ +ATF_TC_BODY(test, tc) \ +{ \ + \ + bytes_transfer_alignment(operation); \ +} + +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_read_i, "PT_READ_I") +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_read_d, "PT_READ_D") +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_write_i, "PT_WRITE_I") +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_pt_write_d, "PT_WRITE_D") + +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_read_i, "PIOD_READ_I") +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_read_d, "PIOD_READ_D") +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_write_i, "PIOD_WRITE_I") +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_write_d, "PIOD_WRITE_D") + +BYTES_TRANSFER_ALIGNMENT(bytes_transfer_alignment_piod_read_auxv, "PIOD_READ_AUXV") + +/// ---------------------------------------------------------------------------- + #if defined(HAVE_GPREGS) || defined(HAVE_FPREGS) static void access_regs(const char *regset, const char *aux) @@ -6370,6 +6549,18 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_auxv); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_read_i); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_read_d); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_write_i); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_pt_write_d); + + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_read_i); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_read_d); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_write_i); + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_write_d); + + ATF_TP_ADD_TC(tp, bytes_transfer_alignment_piod_read_auxv); + ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs1); ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs2); ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs3);