]> granicus.if.org Git - strace/blob - tests/prctl-arg2-intptr.c
strace: terminate itself if interrupted by a signal
[strace] / tests / prctl-arg2-intptr.c
1 /*
2  * Check decoding of prctl operations which use arg2 as pointer to an integer
3  * value: PR_GET_CHILD_SUBREAPER, PR_GET_ENDIAN, PR_GET_FPEMU, and PR_GET_FPEXC.
4  *
5  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
6  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7  * Copyright (c) 2016-2017 The strace developers.
8  * All rights reserved.
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include "tests.h"
14 #include <asm/unistd.h>
15
16 #if defined __NR_prctl
17
18 # include <stdint.h>
19 # include <stdio.h>
20 # include <unistd.h>
21 # include <linux/prctl.h>
22
23 static const char *errstr;
24
25 static long
26 prctl(kernel_ulong_t arg1, kernel_ulong_t arg2)
27 {
28         static const kernel_ulong_t bogus_arg =
29                 (kernel_ulong_t) 0xdeadbeefbadc0dedULL;
30         long rc = syscall(__NR_prctl, arg1, arg2, bogus_arg);
31         errstr = sprintrc(rc);
32         return rc;
33 }
34
35 int
36 main(void)
37 {
38         static const kernel_ulong_t bogus_addr1 =
39                 (kernel_ulong_t) 0x1e55c0de00000000ULL;
40         static const kernel_ulong_t bogus_addr2 =
41                 (kernel_ulong_t) 0xfffffffffffffffdULL;
42         static const kernel_ulong_t bogus_op_bits =
43                 (kernel_ulong_t) 0xbadc0ded00000000ULL;
44         static const struct {
45                 kernel_ulong_t val;
46                 const char *str;
47         } options[] = {
48                 { 37, "PR_GET_CHILD_SUBREAPER" },
49                 { 19, "PR_GET_ENDIAN" },
50                 {  9, "PR_GET_FPEMU" },
51                 { 11, "PR_GET_FPEXC" },
52         };
53
54         TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, ptr);
55         long rc;
56         unsigned int i;
57
58         for (i = 0; i < ARRAY_SIZE(options); ++i) {
59                 prctl(options[i].val | bogus_op_bits, 0);
60                 printf("prctl(%s, NULL) = %s\n", options[i].str, errstr);
61
62                 if (bogus_addr1) {
63                         prctl(options[i].val | bogus_op_bits, bogus_addr1);
64                         printf("prctl(%s, %#llx) = %s\n", options[i].str,
65                                (unsigned long long) bogus_addr1, errstr);
66                 }
67
68                 prctl(options[i].val | bogus_op_bits, bogus_addr2);
69                 printf("prctl(%s, %#llx) = %s\n", options[i].str,
70                        (unsigned long long) bogus_addr2, errstr);
71
72                 prctl(options[i].val | bogus_op_bits, (uintptr_t) (ptr + 1));
73                 printf("prctl(%s, %p) = %s\n", options[i].str,
74                        ptr + 1, errstr);
75
76                 rc = prctl(options[i].val | bogus_op_bits, (uintptr_t) ptr);
77                 if (!rc) {
78                         printf("prctl(%s, [%u]) = %s\n",
79                                options[i].str, *ptr, errstr);
80                 } else {
81                         printf("prctl(%s, %p) = %s\n",
82                                options[i].str, ptr, errstr);
83                 }
84
85                 if (F8ILL_KULONG_SUPPORTED) {
86                         kernel_ulong_t bogus_addr3 = f8ill_ptr_to_kulong(ptr);
87                         prctl(options[i].val | bogus_op_bits, bogus_addr3);
88                         printf("prctl(%s, %#llx) = %s\n", options[i].str,
89                                (unsigned long long) bogus_addr3, errstr);
90                 }
91         }
92
93         puts("+++ exited with 0 +++");
94         return 0;
95 }
96
97 #else
98
99 SKIP_MAIN_UNDEFINED("__NR_prctl")
100
101 #endif