]> granicus.if.org Git - strace/blob - tests/ioctl_perf-success.c
print_array: enhance printing of unfetchable object addresses
[strace] / tests / ioctl_perf-success.c
1 /*
2  * Check decoding of successful PERF_EVENT_IOC_{ID,QUERY_BPF} ioctls.
3  *
4  * Copyright (c) 2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31
32 #ifdef HAVE_LINUX_PERF_EVENT_H
33
34 # include <assert.h>
35 # include <inttypes.h>
36 # include <stdio.h>
37 # include <stdlib.h>
38 # include <string.h>
39 # include <sys/ioctl.h>
40 # include <linux/perf_event.h>
41
42 # ifndef PERF_EVENT_IOC_ID
43 #  define PERF_EVENT_IOC_ID                     _IOR('$', 7, void *)
44 # endif
45
46 # ifndef PERF_EVENT_IOC_QUERY_BPF
47 #  define PERF_EVENT_IOC_QUERY_BPF              _IOWR('$', 10, void *)
48
49 struct perf_event_query_bpf {
50         uint32_t ids_len;
51         uint32_t prog_cnt;
52         uint32_t ids[0];
53 };
54 # endif
55
56 int
57 main(int argc, char **argv)
58 {
59         static const uint64_t magic64 = 0xfacefeeddeadc0deULL;
60
61         TAIL_ALLOC_OBJECT_CONST_PTR(uint64_t, u64_ptr);
62         uint64_t *const u64_efault = u64_ptr + 1;
63         uint32_t *const u32_arr = tail_alloc(sizeof(uint32_t) * 4);
64         uint32_t *const u32_efault = u32_arr + 4;
65
66         unsigned long num_skip;
67         long inject_retval;
68         bool locked = false;
69
70         *u64_ptr = magic64;
71
72         if (argc == 1)
73                 return 0;
74
75         if (argc < 3)
76                 error_msg_and_fail("Usage: %s NUM_SKIP INJECT_RETVAL", argv[0]);
77
78         num_skip = strtoul(argv[1], NULL, 0);
79         inject_retval = strtol(argv[2], NULL, 0);
80
81         if (inject_retval < 0)
82                 error_msg_and_fail("Expected non-negative INJECT_RETVAL, "
83                                    "but got %ld", inject_retval);
84
85         for (unsigned long i = 0; i < num_skip; i++) {
86                 long ret = ioctl(-1, PERF_EVENT_IOC_ID, NULL);
87
88                 printf("ioctl(-1, PERF_EVENT_IOC_ID, NULL) = %s%s\n",
89                        sprintrc(ret),
90                        ret == inject_retval ? " (INJECTED)" : "");
91
92                 if (ret != inject_retval)
93                         continue;
94
95                 locked = true;
96                 break;
97         }
98
99         if (!locked)
100                 error_msg_and_fail("Hasn't locked on ioctl(-1"
101                                    ", PERF_EVENT_IOC_ID, NULL) returning %lu",
102                                    inject_retval);
103
104         /* PERF_EVENT_IOC_ID */
105         assert(ioctl(-1, PERF_EVENT_IOC_ID, NULL) == inject_retval);
106         printf("ioctl(-1, PERF_EVENT_IOC_ID, NULL) = %ld (INJECTED)\n",
107                inject_retval);
108
109         assert(ioctl(-1, PERF_EVENT_IOC_ID, u64_efault) == inject_retval);
110         printf("ioctl(-1, PERF_EVENT_IOC_ID, %p) = %ld (INJECTED)\n",
111                u64_efault, inject_retval);
112
113         assert(ioctl(-1, PERF_EVENT_IOC_ID, u64_ptr) == inject_retval);
114         printf("ioctl(-1, PERF_EVENT_IOC_ID, [%" PRIu64 "]) = %ld (INJECTED)\n",
115                magic64, inject_retval);
116
117         /* PERF_EVENT_IOC_QUERY_BPF */
118         assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, NULL) == inject_retval);
119         printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, NULL) = %ld (INJECTED)\n",
120                inject_retval);
121
122         assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_efault)
123                == inject_retval);
124         printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, %p) = %ld (INJECTED)\n",
125                u32_efault, inject_retval);
126
127         u32_arr[3] = 0xdeadbeef;
128         assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr + 3)
129                == inject_retval);
130         printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3735928559, ...}) "
131                "= %ld (INJECTED)\n",
132                inject_retval);
133
134         u32_arr[2] = 0xdecaffed;
135         assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr + 2)
136                == inject_retval);
137         printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3737845741"
138                ", prog_cnt=3735928559, ids=%p})"
139                " = %ld (INJECTED)\n",
140                u32_efault, inject_retval);
141
142         u32_arr[0] = 0xbadc0ded;
143         u32_arr[1] = 5;
144         assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr) == inject_retval);
145         printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3134983661"
146                ", prog_cnt=5, ids=[3737845741, 3735928559, ... /* %p */]})"
147                " = %ld (INJECTED)\n",
148                u32_efault, inject_retval);
149
150         u32_arr[1] = 2;
151         assert(ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, u32_arr) == inject_retval);
152         printf("ioctl(-1, PERF_EVENT_IOC_QUERY_BPF, {ids_len=3134983661"
153                ", prog_cnt=2, ids=[3737845741, 3735928559]})"
154                " = %ld (INJECTED)\n",
155                inject_retval);
156
157         puts("+++ exited with 0 +++");
158         return 0;
159 }
160
161 #else
162
163 SKIP_MAIN_UNDEFINED("HAVE_LINUX_PERF_EVENT_H");
164
165 #endif