]> granicus.if.org Git - strace/blob - tests/remap_file_pages.c
tests: add remap_file_pages test variants with different xlat verbosity levels
[strace] / tests / remap_file_pages.c
1 /*
2  * Check decoding of remap_file_pages syscall.
3  *
4  * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "tests.h"
32 #include <asm/unistd.h>
33 #include "scno.h"
34
35 #ifdef __NR_remap_file_pages
36
37 # include <stdio.h>
38 # include <stdint.h>
39 # include <unistd.h>
40 # include <linux/mman.h>
41
42 static const char *errstr;
43
44 static long
45 k_remap_file_pages(const kernel_ulong_t addr,
46                    const kernel_ulong_t size,
47                    const kernel_ulong_t prot,
48                    const kernel_ulong_t pgoff,
49                    const kernel_ulong_t flags)
50 {
51         const long rc = syscall(__NR_remap_file_pages,
52                                 addr, size, prot, pgoff, flags);
53         errstr = sprintrc(rc);
54         return rc;
55 }
56
57 int
58 main(void)
59 {
60         kernel_ulong_t addr = (kernel_ulong_t) 0xfacefeeddeadbeefULL;
61         kernel_ulong_t size = (kernel_ulong_t) 0xdefaced1bad2f00dULL;
62         kernel_ulong_t prot = PROT_READ|PROT_WRITE|PROT_EXEC;
63         kernel_ulong_t pgoff = (kernel_ulong_t) 0xcaf3babebad4deedULL;
64         kernel_ulong_t flags = MAP_PRIVATE|MAP_ANONYMOUS;
65 # define prot1_str "PROT_READ|PROT_WRITE|PROT_EXEC"
66 # define flags1_str "MAP_PRIVATE|MAP_ANONYMOUS"
67
68         k_remap_file_pages(addr, size, prot, pgoff, flags);
69 # if XLAT_RAW
70         printf("remap_file_pages(%#jx, %ju, %#jx, %ju, %#jx) = %s\n",
71                (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot,
72                (uintmax_t) pgoff, (uintmax_t) flags, errstr);
73 # elif XLAT_VERBOSE
74         printf("remap_file_pages(%#jx, %ju, %#jx /* %s */, %ju, %#jx /* %s */)"
75                " = %s\n",
76                (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot, prot1_str,
77                (uintmax_t) pgoff, (uintmax_t) flags,
78                flags1_str, errstr);
79 # else /* XLAT_ABBREV */
80         printf("remap_file_pages(%#jx, %ju, %s, %ju, %s) = %s\n",
81                (uintmax_t) addr, (uintmax_t) size, prot1_str,
82                (uintmax_t) pgoff, flags1_str, errstr);
83 # endif
84
85 #ifdef MAP_HUGETLB
86 # ifndef MAP_HUGE_2MB
87 #  ifndef MAP_HUGE_SHIFT
88 #   define MAP_HUGE_SHIFT 26
89 #  endif
90 #  define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
91 # endif /* !MAP_HUGE_2MB */
92         addr = (kernel_ulong_t) 0xfacefeeddeadf00dULL;
93         size = (kernel_ulong_t) 0xdefaced1bad2beefULL;
94         prot = (kernel_ulong_t) 0xdefaced00000000ULL | PROT_NONE;
95         flags = MAP_TYPE | MAP_FIXED | MAP_NORESERVE | MAP_HUGETLB | MAP_HUGE_2MB;
96
97         k_remap_file_pages(addr, size, prot, pgoff, flags);
98
99 /*
100  * HP PA-RISC is the only architecture that has MAP_TYPE defined to 0x3, which
101  * is also used for MAP_SHARED_VALIDATE since Linux commit v4.15-rc1~71^2^2~23.
102  */
103 # ifdef __hppa__
104 #  define MAP_TYPE_str "MAP_SHARED_VALIDATE"
105 # else
106 #  define MAP_TYPE_str "0xf /* MAP_??? */"
107 # endif
108 # define flags2_str \
109         MAP_TYPE_str "|MAP_FIXED|MAP_NORESERVE|MAP_HUGETLB|21<<MAP_HUGE_SHIFT"
110
111 # if XLAT_RAW
112         printf("remap_file_pages(%#jx, %ju, %#jx, %ju, %#jx) = %s\n",
113                (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot,
114                (uintmax_t) pgoff, (uintmax_t) flags, errstr);
115 # elif XLAT_VERBOSE
116         printf("remap_file_pages(%#jx, %ju, %#jx /* %s */, %ju, %#jx /* %s */)"
117                " = %s\n",
118                (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot,
119                prot == PROT_NONE ? "PROT_NONE" : "PROT_???",
120                (uintmax_t) pgoff, (uintmax_t) flags, flags2_str, errstr);
121 # else /* XLAT_ABBREV */
122         printf("remap_file_pages(%#jx, %ju, %s, %ju, %s) = %s\n",
123                (uintmax_t) addr, (uintmax_t) size,
124                prot == PROT_NONE ? "PROT_NONE" :
125                                    "0xdefaced00000000 /* PROT_??? */",
126                (uintmax_t) pgoff, flags2_str, errstr);
127 # endif
128 #endif /* MAP_HUGETLB */
129
130         puts("+++ exited with 0 +++");
131         return 0;
132 }
133
134 #else
135
136 SKIP_MAIN_UNDEFINED("__NR_remap_file_pages")
137
138 #endif