Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/sys Move byte transfer tests out of t_ptrace_...



details:   https://anonhg.NetBSD.org/src/rev/b84e0ed1bb0d
branches:  trunk
changeset: 971793:b84e0ed1bb0d
user:      kamil <kamil%NetBSD.org@localhost>
date:      Mon May 04 22:05:28 2020 +0000

description:
Move byte transfer tests out of t_ptrace_wait.c to t_ptrace_bytetransfer_wait.h

The same tests are now included with the preprocessor in t_ptrace_wait.c.

No functional change intended.

diffstat:

 tests/lib/libc/sys/t_ptrace_bytetransfer_wait.h |  907 ++++++++++++++++++++++++
 tests/lib/libc/sys/t_ptrace_wait.c              |  826 +---------------------
 2 files changed, 911 insertions(+), 822 deletions(-)

diffs (truncated from 1772 to 300 lines):

diff -r b64dfe9b6a1f -r b84e0ed1bb0d tests/lib/libc/sys/t_ptrace_bytetransfer_wait.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/sys/t_ptrace_bytetransfer_wait.h   Mon May 04 22:05:28 2020 +0000
@@ -0,0 +1,907 @@
+/*     $NetBSD: t_ptrace_bytetransfer_wait.h,v 1.1 2020/05/04 22:05:28 kamil Exp $     */
+
+/*-
+ * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+enum bytes_transfer_type {
+       BYTES_TRANSFER_DATA,
+       BYTES_TRANSFER_DATAIO,
+       BYTES_TRANSFER_TEXT,
+       BYTES_TRANSFER_TEXTIO,
+       BYTES_TRANSFER_AUXV
+};
+
+static int __used
+bytes_transfer_dummy(int a, int b, int c, int d)
+{
+       int e, f, g, h;
+
+       a *= 4;
+       b += 3;
+       c -= 2;
+       d /= 1;
+
+       e = strtol("10", NULL, 10);
+       f = strtol("20", NULL, 10);
+       g = strtol("30", NULL, 10);
+       h = strtol("40", NULL, 10);
+
+       return (a + b * c - d) + (e * f - g / h);
+}
+
+static void
+bytes_transfer(int operation, size_t size, enum bytes_transfer_type type)
+{
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+       bool skip = false;
+
+       int lookup_me = 0;
+       uint8_t lookup_me8 = 0;
+       uint16_t lookup_me16 = 0;
+       uint32_t lookup_me32 = 0;
+       uint64_t lookup_me64 = 0;
+
+       int magic = 0x13579246;
+       uint8_t magic8 = 0xab;
+       uint16_t magic16 = 0x1234;
+       uint32_t magic32 = 0x98765432;
+       uint64_t magic64 = 0xabcdef0123456789;
+
+       struct ptrace_io_desc io;
+#if defined(TWAIT_HAVE_STATUS)
+       int status;
+#endif
+       /* 513 is just enough, for the purposes of ATF it's good enough */
+       AuxInfo ai[513], *aip;
+
+       ATF_REQUIRE(size < sizeof(ai));
+
+       /* Prepare variables for .TEXT transfers */
+       switch (type) {
+       case BYTES_TRANSFER_TEXT:
+               memcpy(&magic, bytes_transfer_dummy, sizeof(magic));
+               break;
+       case BYTES_TRANSFER_TEXTIO:
+               switch (size) {
+               case 8:
+                       memcpy(&magic8, bytes_transfer_dummy, sizeof(magic8));
+                       break;
+               case 16:
+                       memcpy(&magic16, bytes_transfer_dummy, sizeof(magic16));
+                       break;
+               case 32:
+                       memcpy(&magic32, bytes_transfer_dummy, sizeof(magic32));
+                       break;
+               case 64:
+                       memcpy(&magic64, bytes_transfer_dummy, sizeof(magic64));
+                       break;
+               }
+               break;
+       default:
+               break;
+       }
+
+       /* Prepare variables for PIOD and AUXV transfers */
+       switch (type) {
+       case BYTES_TRANSFER_TEXTIO:
+       case BYTES_TRANSFER_DATAIO:
+               io.piod_op = operation;
+               switch (size) {
+               case 8:
+                       io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
+                                      (void *)bytes_transfer_dummy :
+                                      &lookup_me8;
+                       io.piod_addr = &lookup_me8;
+                       io.piod_len = sizeof(lookup_me8);
+                       break;
+               case 16:
+                       io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
+                                      (void *)bytes_transfer_dummy :
+                                      &lookup_me16;
+                       io.piod_addr = &lookup_me16;
+                       io.piod_len = sizeof(lookup_me16);
+                       break;
+               case 32:
+                       io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
+                                      (void *)bytes_transfer_dummy :
+                                      &lookup_me32;
+                       io.piod_addr = &lookup_me32;
+                       io.piod_len = sizeof(lookup_me32);
+                       break;
+               case 64:
+                       io.piod_offs = (type == BYTES_TRANSFER_TEXTIO) ?
+                                      (void *)bytes_transfer_dummy :
+                                      &lookup_me64;
+                       io.piod_addr = &lookup_me64;
+                       io.piod_len = sizeof(lookup_me64);
+                       break;
+               default:
+                       break;
+               }
+               break;
+       case BYTES_TRANSFER_AUXV:
+               io.piod_op = operation;
+               io.piod_offs = 0;
+               io.piod_addr = ai;
+               io.piod_len = size;
+               break;
+       default:
+               break;
+       }
+
+       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);
+
+               switch (type) {
+               case BYTES_TRANSFER_DATA:
+                       switch (operation) {
+                       case PT_READ_D:
+                       case PT_READ_I:
+                               lookup_me = magic;
+                               break;
+                       default:
+                               break;
+                       }
+                       break;
+               case BYTES_TRANSFER_DATAIO:
+                       switch (operation) {
+                       case PIOD_READ_D:
+                       case PIOD_READ_I:
+                               switch (size) {
+                               case 8:
+                                       lookup_me8 = magic8;
+                                       break;
+                               case 16:
+                                       lookup_me16 = magic16;
+                                       break;
+                               case 32:
+                                       lookup_me32 = magic32;
+                                       break;
+                               case 64:
+                                       lookup_me64 = magic64;
+                                       break;
+                               default:
+                                       break;
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+               default:
+                       break;
+               }
+
+               DPRINTF("Before raising %s from child\n", strsignal(sigval));
+               FORKEE_ASSERT(raise(sigval) == 0);
+
+               /* Handle PIOD and PT separately as operation values overlap */
+               switch (type) {
+               case BYTES_TRANSFER_DATA:
+                       switch (operation) {
+                       case PT_WRITE_D:
+                       case PT_WRITE_I:
+                               FORKEE_ASSERT_EQ(lookup_me, magic);
+                               break;
+                       default:
+                               break;
+                       }
+                       break;
+               case BYTES_TRANSFER_DATAIO:
+                       switch (operation) {
+                       case PIOD_WRITE_D:
+                       case PIOD_WRITE_I:
+                               switch (size) {
+                               case 8:
+                                       FORKEE_ASSERT_EQ(lookup_me8, magic8);
+                                       break;
+                               case 16:
+                                       FORKEE_ASSERT_EQ(lookup_me16, magic16);
+                                       break;
+                               case 32:
+                                       FORKEE_ASSERT_EQ(lookup_me32, magic32);
+                                       break;
+                               case 64:
+                                       FORKEE_ASSERT_EQ(lookup_me64, magic64);
+                                       break;
+                               default:
+                                       break;
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+                       break;
+               case BYTES_TRANSFER_TEXT:
+                       FORKEE_ASSERT(memcmp(&magic, bytes_transfer_dummy,
+                                            sizeof(magic)) == 0);
+                       break;
+               case BYTES_TRANSFER_TEXTIO:
+                       switch (size) {
+                       case 8:
+                               FORKEE_ASSERT(memcmp(&magic8,
+                                                    bytes_transfer_dummy,
+                                                    sizeof(magic8)) == 0);
+                               break;
+                       case 16:
+                               FORKEE_ASSERT(memcmp(&magic16,
+                                                    bytes_transfer_dummy,
+                                                    sizeof(magic16)) == 0);
+                               break;
+                       case 32:
+                               FORKEE_ASSERT(memcmp(&magic32,
+                                                    bytes_transfer_dummy,
+                                                    sizeof(magic32)) == 0);
+                               break;
+                       case 64:
+                               FORKEE_ASSERT(memcmp(&magic64,
+                                                    bytes_transfer_dummy,
+                                                    sizeof(magic64)) == 0);
+                               break;
+                       }
+                       break;
+               default:
+                       break;
+               }
+
+               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);
+
+       /* Check PaX MPROTECT */
+       if (!can_we_write_to_text(child)) {
+               switch (type) {
+               case BYTES_TRANSFER_TEXTIO:
+                       switch (operation) {
+                       case PIOD_WRITE_D:
+                       case PIOD_WRITE_I:
+                               skip = true;
+                               break;
+                       default:
+                               break;
+                       }



Home | Main Index | Thread Index | Old Index