1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
* Copyright (c) 2018-2021 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <linux/input.h>
#include "print_fields.h"
#define NUM_WORDS 4
static const char *errstr;
struct evdev_check {
unsigned long cmd;
const char *cmd_str;
const void *arg_ptr;
void (*print_arg)(long rc, const void *ptr, const void *arg);
};
static long
invoke_test_syscall(unsigned long cmd, const void *p)
{
long rc = ioctl(-1, cmd, p);
errstr = sprintrc(rc);
static char inj_errstr[4096];
snprintf(inj_errstr, sizeof(inj_errstr), "%s (INJECTED)", errstr);
errstr = inj_errstr;
return rc;
}
static void
test_evdev(struct evdev_check *check, const void *arg)
{
long rc = invoke_test_syscall(check->cmd, check->arg_ptr);
printf("ioctl(-1, %s, ", sprintxlat(check->cmd_str, check->cmd, NULL));
if (check->print_arg)
check->print_arg(rc, check->arg_ptr, arg);
else
printf("%p", check->arg_ptr);
printf(") = %s\n", errstr);
}
static void
print_input_absinfo(long rc, const void *ptr, const void *arg)
{
const struct input_absinfo *absinfo = ptr;
#if VERBOSE
const uintptr_t sz = (uintptr_t) arg;
#endif
if (rc < 0) {
printf("%p", absinfo);
return;
}
printf("{");
PRINT_FIELD_U(*absinfo, value);
printf(", ");
PRINT_FIELD_U(*absinfo, minimum);
#if VERBOSE
printf(", ");
PRINT_FIELD_U(*absinfo, maximum);
printf(", ");
PRINT_FIELD_U(*absinfo, fuzz);
printf(", ");
PRINT_FIELD_U(*absinfo, flat);
if (sz > offsetofend(struct input_absinfo, flat)) {
if (sz >= 24) {
printf(", ");
PRINT_FIELD_U(*absinfo, resolution);
if (sz > 24)
printf(", ...");
} else {
printf(", ...");
}
}
#else
printf(", ...");
#endif
printf("}");
}
static void
print_input_id(long rc, const void *ptr, const void *arg)
{
const struct input_id *id = ptr;
if (rc < 0) {
printf("%p", id);
return;
}
printf("{bustype=%" PRIu16
", vendor=%" PRIu16
", product=%" PRIu16
", version=%" PRIu16 "}",
id->bustype, id->vendor, id->product, id->version);
}
static void
print_mtslots(long rc, const void *ptr, const void *arg)
{
const unsigned int *buffer = ptr;
const char * const * str = arg;
if (rc < 0) {
printf("%p", buffer);
return;
}
printf("{code=%s", sprintxlat(*str, *buffer, NULL));
printf(", values=[");
for (unsigned int i = 1; str[i]; i++)
printf("%s%s", i > 1 ? ", " : "", str[i]);
printf("]}");
}
static void
print_getbit(long rc, const void *ptr, const void *arg)
{
const char * const *str = arg + sizeof(char *);
#if XLAT_RAW || XLAT_VERBOSE
const unsigned long *buf = ptr;
const unsigned long buf_size = (uintptr_t) (str[-1]);
#endif
if (rc <= 0) {
printf("%p", ptr);
return;
}
#if !XLAT_RAW
printf("[");
for (unsigned long i = 0; str[i]; i++) {
# if ! VERBOSE
if (i >= 4) {
printf(" ...");
break;
}
# endif
if (i)
printf(" ");
printf("%s", str[i]);
}
printf("]");
#endif /* !XLAT_RAW */
#if XLAT_VERBOSE
printf(" /* ");
#endif
#if XLAT_RAW || XLAT_VERBOSE
printf("[");
const unsigned long cnt =
(MIN((unsigned long) rc, buf_size) + sizeof(long) - 1)
/ sizeof(long);
for (unsigned long i = 0; i < cnt; i++)
printf("%s%#lx", i ? ", " : "", buf[i]);
printf("]");
#endif
#if XLAT_VERBOSE
printf(" */");
#endif
}
int
main(int argc, char **argv)
{
unsigned long num_skip;
long inject_retval;
bool locked = false;
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 int i = 0; i < num_skip; i++) {
long rc = ioctl(-1, EVIOCGID, NULL);
printf("ioctl(-1, %s, NULL) = %s%s\n",
XLAT_STR(EVIOCGID), sprintrc(rc),
rc == inject_retval ? " (INJECTED)" : "");
if (rc != inject_retval)
continue;
locked = true;
break;
}
if (!locked)
error_msg_and_fail("Hasn't locked on ioctl(-1"
", EVIOCGID, NULL) returning %lu",
inject_retval);
static const void *absinfo_sz =
(const void *) (uintptr_t) sizeof(struct input_absinfo);
TAIL_ALLOC_OBJECT_CONST_PTR(struct input_id, id);
TAIL_ALLOC_OBJECT_CONST_PTR(struct input_absinfo, absinfo);
TAIL_ALLOC_OBJECT_CONST_PTR(int, bad_addr_slot);
struct input_absinfo *absinfo_24 = tail_alloc(MAX(sizeof(*absinfo_24),
24));
struct input_absinfo *absinfo_32 = tail_alloc(MAX(sizeof(*absinfo_32),
32));
fill_memory(absinfo, sizeof(struct input_absinfo));
fill_memory(absinfo_24, 24);
fill_memory(absinfo_32, 32);
static const unsigned int mtslots[] = { ABS_MT_SLOT, 1, 3 };
static const char * const mtslots_str[] = {
"ABS_MT_SLOT", "1", "3", NULL };
/* invalid flag */
static const unsigned int invalid_mtslot[] = { -1, 1 };
static const char * const invalid_mtslot_str[] = {
""
#if !XLAT_RAW && !XLAT_VERBOSE
"0xffffffff"
#endif
#if !XLAT_VERBOSE
" /* "
#endif
"ABS_MT_???"
#if !XLAT_VERBOSE
" */"
#endif
, "1", NULL };
enum { ULONG_BIT = sizeof(unsigned long) * 8 };
/* set more than 4 bits */
static const unsigned long ev_more[NUM_WORDS] = {
1 << EV_ABS | 1 << EV_MSC | 1 << EV_LED | 1 << EV_SND
| 1 << EV_PWR };
static const char * const ev_more_str_2[] = {
(char *) (uintptr_t) 4,
XLAT_KNOWN(0x3, "EV_ABS"), XLAT_KNOWN(0x4, "EV_MSC"), NULL };
static const char * const ev_more_str_3[] = {
(char *) (uintptr_t) 4,
XLAT_KNOWN(0x3, "EV_ABS"), XLAT_KNOWN(0x4, "EV_MSC"),
XLAT_KNOWN(0x11, "EV_LED"), XLAT_KNOWN(0x12, "EV_SND"),
XLAT_KNOWN(0x16, "EV_PWR"), NULL };
/* set less than 4 bits */
static const unsigned long ev_less[NUM_WORDS] = {
1 << EV_ABS | 1 << EV_MSC | 1 << EV_LED };
static const char * const ev_less_str_2[] = {
(char *) (uintptr_t) 4,
XLAT_KNOWN(0x3, "EV_ABS"), XLAT_KNOWN(0x4, "EV_MSC"), NULL };
static const char * const ev_less_str_3[] = {
(char *) (uintptr_t) 4,
XLAT_KNOWN(0x3, "EV_ABS"), XLAT_KNOWN(0x4, "EV_MSC"),
XLAT_KNOWN(0x11, "EV_LED"), NULL };
/* set zero bit */
static const unsigned long ev_zero[NUM_WORDS] = { 0x0 };
static const char * const ev_zero_str[] = {
(char *) (uintptr_t) 1,
"", NULL };
/* KEY_MAX is 0x2ff which is greater than retval * 8 */
static const unsigned long key[NUM_WORDS] = {
1 << KEY_1 | 1 << KEY_2,
[ KEY_F12 / ULONG_BIT ] = 1 << (KEY_F12 % ULONG_BIT) };
static const char * const key_str_8[] = {
(char *) (uintptr_t) (NUM_WORDS * sizeof(long)),
XLAT_KNOWN(0x2, "KEY_1"), XLAT_KNOWN(0x3, "KEY_2"), NULL };
static const char * const key_str_16[] = {
(char *) (uintptr_t) (NUM_WORDS * sizeof(long)),
XLAT_KNOWN(0x2, "KEY_1"), XLAT_KNOWN(0x3, "KEY_2"),
XLAT_KNOWN(0x58, "KEY_F12"), NULL };
assert(sizeof(ev_more) >= (unsigned long) inject_retval);
assert(sizeof(ev_less) >= (unsigned long) inject_retval);
assert(sizeof(ev_zero) >= (unsigned long) inject_retval);
assert(sizeof(key) >= (unsigned long) inject_retval);
struct {
struct evdev_check check;
const void *ptr;
} a[] = {
{ { ARG_STR(EVIOCGID), id, print_input_id }, NULL },
{ { _IOC(_IOC_READ, 'E', 0x40 + ABS_Y, 19), "EVIOCGABS(ABS_Y)",
absinfo, NULL }, NULL },
{ { _IOC(_IOC_READ, 'E', 0x40 + ABS_Y, 20),
"EVIOCGABS(ABS_Y)", absinfo, print_input_absinfo },
(const void *) (uintptr_t) 20 },
{ { _IOC(_IOC_READ, 'E', 0x40 + ABS_Y, 21),
"EVIOCGABS(ABS_Y)", absinfo_24, print_input_absinfo },
(const void *) (uintptr_t) 21 },
{ { _IOC(_IOC_READ, 'E', 0x40 + ABS_Y, 24),
"EVIOCGABS(ABS_Y)", absinfo_24, print_input_absinfo },
(const void *) (uintptr_t) 24 },
{ { _IOC(_IOC_READ, 'E', 0x40 + ABS_Y, 32),
"EVIOCGABS(ABS_Y)", absinfo_32, print_input_absinfo },
(const void *) (uintptr_t) 32 },
{ { ARG_STR(EVIOCGABS(ABS_X)), absinfo, print_input_absinfo },
absinfo_sz},
{ { ARG_STR(EVIOCGABS(ABS_Y)), absinfo, print_input_absinfo },
absinfo_sz },
{ { ARG_STR(EVIOCGABS(ABS_Y)), absinfo, print_input_absinfo },
absinfo_sz },
{ { ARG_STR(EVIOCGBIT(0, 0)), ev_more, print_getbit },
inject_retval * 8 <= EV_LED
? (const void *) &ev_more_str_2
: (const void *) &ev_more_str_3 },
{ { ARG_STR(EVIOCGBIT(0, 0)), ev_less, print_getbit },
inject_retval * 8 <= EV_LED
? (const void *) &ev_less_str_2
: (const void *) &ev_less_str_3 },
{ { ARG_STR(EVIOCGBIT(0, 0)), ev_zero, print_getbit }, &ev_zero_str },
{ { ARG_STR(EVIOCGBIT(EV_KEY, 0)), key, print_getbit },
inject_retval * 8 <= KEY_F12
? (const void *) &key_str_8
: (const void *) &key_str_16 },
{ { ARG_STR(EVIOCGMTSLOTS(12)), mtslots, print_mtslots }, &mtslots_str },
{ { ARG_STR(EVIOCGMTSLOTS(8)), invalid_mtslot, print_mtslots }, &invalid_mtslot_str }
};
for (unsigned int i = 0; i < ARRAY_SIZE(a); i++) {
test_evdev(&a[i].check, a[i].ptr);
}
puts("+++ exited with 0 +++");
return 0;
}
|