]> granicus.if.org Git - strace/commitdiff
tests: check decoding of successful PERF_EVENT_IOC_{ID,QUERY_BPF} ioctls
authorEugene Syromyatnikov <evgsyr@gmail.com>
Mon, 28 May 2018 17:34:50 +0000 (19:34 +0200)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 28 May 2018 17:34:50 +0000 (17:34 +0000)
As these have some argument output on the exit path.

* tests/ioctl_perf-success.c: New file.
* tests/ioctl_perf-success.test: New test.
* tests/Makefile.am (DECODER_TESTS): Add it.
(check_PROGRAMS): Add ioctl_perf-success.
* tests/.gitignore: Likewise.
* tests/ioctl_perf.c: Remove TODO.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
tests/.gitignore
tests/Makefile.am
tests/ioctl_perf-success.c [new file with mode: 0644]
tests/ioctl_perf-success.test [new file with mode: 0755]
tests/ioctl_perf.c

index e585112fc0c13c414ed33df82d7e96f93e6283da..2167bcaebcc4992f2e3c7067af56bc9d069def2e 100644 (file)
@@ -144,6 +144,7 @@ ioctl_loop-v
 ioctl_mtd
 ioctl_nsfs
 ioctl_perf
+ioctl_perf-success
 ioctl_ptp
 ioctl_rtc
 ioctl_rtc-v
index 92be1e8bf777d1dc05635704985d64c07f8ab83e..99c8ec9f30bd84ff2645dfa8ee7c34b095472a96 100644 (file)
@@ -115,6 +115,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
        ioctl_loop-nv \
        ioctl_loop-v \
        ioctl_nsfs \
+       ioctl_perf-success \
        ioctl_rtc-v \
        is_linux_mips_n64 \
        ksysent \
@@ -233,6 +234,7 @@ DECODER_TESTS = \
        futex.test \
        getuid.test \
        ioctl.test \
+       ioctl_perf-success.test \
        ipc_msgbuf.test \
        kern_features-fault.test \
        llseek.test \
diff --git a/tests/ioctl_perf-success.c b/tests/ioctl_perf-success.c
new file mode 100644 (file)
index 0000000..7d60927
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ * Check decoding of successful PERF_EVENT_IOC_{ID,QUERY_BPF} ioctls.
+ *
+ * Copyright (c) 2018 The strace developers.
+ * 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+ */
+
+#include "tests.h"
+
+#ifdef HAVE_LINUX_PERF_EVENT_H
+
+# include <assert.h>
+# include <inttypes.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+# include <sys/ioctl.h>
+# include <linux/perf_event.h>
+
+# ifndef PERF_EVENT_IOC_ID
+#  define PERF_EVENT_IOC_ID                    _IOR('$', 7, void *)
+# endif
+
+# ifndef PERF_EVENT_IOC_QUERY_BPF
+#  define PERF_EVENT_IOC_QUERY_BPF             _IOWR('$', 10, void *)
+
+struct perf_event_query_bpf {
+        uint32_t ids_len;
+        uint32_t prog_cnt;
+        uint32_t ids[0];
+};
+# endif
+
+int
+main(int argc, char **argv)
+{
+       static const uint64_t magic64 = 0xfacefeeddeadc0deULL;
+
+       TAIL_ALLOC_OBJECT_CONST_PTR(uint64_t, u64_ptr);
+       uint64_t *const u64_efault = u64_ptr + 1;
+       uint32_t *const u32_arr = tail_alloc(sizeof(uint32_t) * 4);
+       uint32_t *const u32_efault = u32_arr + 4;
+
+       unsigned long num_skip;
+       long inject_retval;
+       bool locked = false;
+
+       *u64_ptr = magic64;
+
+       if (argc == 1)
+               return 0;
+
+       if (argc < 3)
+               error_msg_and_fail("Usage: %s NUM_SKIP INJECT_RETVAL", argv[0]);
+
+       num_skip = strtoul(argv[1], NULL, 0);
+       inject_retval = strtol(argv[2], NULL, 0);
+
+       if (inject_retval < 0)
+               error_msg_and_fail("Expected non-negative INJECT_RETVAL, "
+                                  "but got %ld", inject_retval);
+
+       for (unsigned long i = 0; i < num_skip; i++) {
+               long ret = ioctl(-1, PERF_EVENT_IOC_ID, NULL);
+
+               printf("ioctl(-1, PERF_EVENT_IOC_ID, NULL) = %s%s\n",
+                      sprintrc(ret),
+                      ret == inject_retval ? " (INJECTED)" : "");
+
+               if (ret != inject_retval)
+                       continue;
+
+               locked = true;
+               break;
+       }
+
+       if (!locked)
+               error_msg_and_fail("Hasn't locked on ioctl(-1"
+                                  ", PERF_EVENT_IOC_ID, NULL) returning %lu",
+                                  inject_retval);
+
+       /* PERF_EVENT_IOC_ID */
+       assert(ioctl(-1, PERF_EVENT_IOC_ID, NULL) == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_ID, NULL) = %ld (INJECTED)\n",
+              inject_retval);
+
+       assert(ioctl(-1, PERF_EVENT_IOC_ID, u64_efault) == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_ID, %p) = %ld (INJECTED)\n",
+              u64_efault, inject_retval);
+
+       assert(ioctl(-1, PERF_EVENT_IOC_ID, u64_ptr) == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_ID, [%" PRIu64 "]) = %ld (INJECTED)\n",
+              magic64, inject_retval);
+
+       /* PERF_EVENT_IOC_QUERY_BPF */
+       assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, NULL) == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, NULL) = %ld (INJECTED)\n",
+              inject_retval);
+
+       assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_efault)
+              == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, %p) = %ld (INJECTED)\n",
+              u32_efault, inject_retval);
+
+       u32_arr[3] = 0xdeadbeef;
+       assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr + 3)
+              == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3735928559, ...}) "
+              "= %ld (INJECTED)\n",
+              inject_retval);
+
+       u32_arr[2] = 0xdecaffed;
+       assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr + 2)
+              == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3737845741"
+              ", prog_cnt=3735928559, ids=%p})"
+              " = %ld (INJECTED)\n",
+              u32_efault, inject_retval);
+
+       u32_arr[0] = 0xbadc0ded;
+       u32_arr[1] = 5;
+       assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr) == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3134983661"
+              ", prog_cnt=5, ids=[3737845741, 3735928559, %p]})"
+              " = %ld (INJECTED)\n",
+              u32_efault, inject_retval);
+
+       u32_arr[1] = 2;
+       assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr) == inject_retval);
+       printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3134983661"
+              ", prog_cnt=2, ids=[3737845741, 3735928559]})"
+              " = %ld (INJECTED)\n",
+              inject_retval);
+
+       puts("+++ exited with 0 +++");
+       return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("HAVE_LINUX_PERF_EVENT_H");
+
+#endif
diff --git a/tests/ioctl_perf-success.test b/tests/ioctl_perf-success.test
new file mode 100755 (executable)
index 0000000..779a2a9
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh -efu
+
+# Check decoding of successful PERF_EVENT_IOC_{ID,QUERY_BPF} ioctls.
+
+. "${srcdir=.}/scno_tampering.sh"
+
+: ${IOCTL_INJECT_START=256}
+: ${IOCTL_INJECT_RETVAL=42}
+
+run_prog
+run_strace -a35 -e trace=ioctl \
+       -e inject=ioctl:retval="${IOCTL_INJECT_RETVAL}":when="${IOCTL_INJECT_START}+" \
+       ../ioctl_perf-success "${IOCTL_INJECT_START}" "${IOCTL_INJECT_RETVAL}" > "$EXP"
+grep -v '^ioctl([012][,<]' < "$LOG" > "$OUT"
+match_diff "$OUT" "$EXP"
index c995a4167a142c3ad5b79271475549b8b33e8a18..2fbc753d9cf130f18ebde7351450f0bf596e4777 100644 (file)
@@ -166,7 +166,6 @@ main(void)
               str_ptr + 40);
 
        /* PERF_EVENT_IOC_ID */
-       /* TODO: check return path */
        ioctl(-1, PERF_EVENT_IOC_ID, NULL);
        printf("ioctl(-1, PERF_EVENT_IOC_ID, NULL) = -1 EBADF (%m)\n");
 
@@ -189,7 +188,6 @@ main(void)
               (unsigned long) magic);
 
        /* PERF_EVENT_IOC_QUERY_BPF */
-       /* TODO: check return path */
        ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, NULL);
        printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, NULL) = -1 EBADF (%m)\n");