]> granicus.if.org Git - strace/blob - tests/lookup_dcookie.c
241ca4aa498f2336f25b2fcef69debb3818afe86
[strace] / tests / lookup_dcookie.c
1 /*
2  * Check decoding of lookup_dcookie syscall.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11
12 #include <asm/unistd.h>
13
14 #ifdef __NR_lookup_dcookie
15
16 # include <inttypes.h>
17 # include <limits.h>
18 # include <stdio.h>
19 # include <unistd.h>
20
21 static void
22 do_lookup_cookie(uint64_t cookie, char *buf, kernel_ulong_t len)
23 {
24         long rc;
25         const char *errstr;
26
27 # if (LONG_MAX > INT_MAX) \
28   || (defined __x86_64__ && defined __ILP32__) \
29   || defined LINUX_MIPSN32
30         rc = syscall(__NR_lookup_dcookie, cookie, buf, len);
31 # else
32         rc = syscall(__NR_lookup_dcookie, LL_VAL_TO_PAIR(cookie), buf, len);
33 # endif
34
35         errstr = sprintrc(rc);
36         printf("lookup_dcookie(%" PRIu64 ", ", cookie);
37
38         /* Here, we trust successful return code */
39         if ((rc >= 0) && (rc < (long) INT_MAX)) {
40                 printf("%.*s, ", (int) rc, buf);
41         } else {
42                 if (buf != NULL)
43                         printf("%p, ", buf);
44                 else
45                         printf("NULL, ");
46         }
47
48         printf("%" PRIu64 ") = %s\n", (uint64_t) len, errstr);
49 }
50
51 int
52 main(void)
53 {
54         enum { BUF_SIZE = 4096 };
55
56         static const uint64_t bogus_cookie =
57                 (uint64_t) 0xf157feeddeadfaceULL;
58         static const kernel_ulong_t bogus_len =
59                 (kernel_ulong_t) 0xbadc0dedda7a1057ULL;
60
61         char *buf = tail_alloc(BUF_SIZE);
62
63         do_lookup_cookie(0, NULL, 0);
64         do_lookup_cookie(bogus_cookie, buf + BUF_SIZE, bogus_len);
65         do_lookup_cookie(bogus_cookie, buf, BUF_SIZE);
66
67         puts("+++ exited with 0 +++");
68
69         return 0;
70 }
71
72 #else
73
74 SKIP_MAIN_UNDEFINED("__NR_lookup_dcookie");
75
76 #endif