]> granicus.if.org Git - strace/blob - tests/memfd_create.c
2658a1ba21449b7ef3eaacc69d500bb2d1a27eb2
[strace] / tests / memfd_create.c
1 /*
2  * Check decoding of memfd_create syscall.
3  *
4  * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <asm/unistd.h>
12 #include "scno.h"
13
14 #ifdef __NR_memfd_create
15
16 # include <stdio.h>
17 # include <stdint.h>
18 # include <unistd.h>
19
20 # ifdef HAVE_LINUX_MEMFD_H
21 #  include <linux/memfd.h>
22 # endif
23
24 # ifndef MFD_HUGE_SHIFT
25 #  define MFD_HUGE_SHIFT 26
26 # endif
27
28 # ifndef MFD_HUGE_MASK
29 #  define MFD_HUGE_MASK 0x3f
30 # endif
31
32 static const char *errstr;
33
34 static long
35 k_memfd_create(const kernel_ulong_t name, const kernel_ulong_t flags)
36 {
37         const long rc = syscall(__NR_memfd_create, name, flags);
38         errstr = sprintrc(rc);
39         return rc;
40 }
41
42 int
43 main(void)
44 {
45         const size_t size = 255 - (sizeof("memfd:") - 1) + 1;
46         char *pattern = tail_alloc(size);
47         fill_memory_ex(pattern, size, '0', 10);
48
49         k_memfd_create((uintptr_t) pattern, 0);
50         printf("memfd_create(\"%.*s\"..., 0) = %s\n",
51                (int) size - 1, pattern, errstr);
52
53         kernel_ulong_t flags = (kernel_ulong_t) 0xfacefeed00000007ULL;
54 # define flags1_str "MFD_CLOEXEC|MFD_ALLOW_SEALING|MFD_HUGETLB"
55
56         k_memfd_create((uintptr_t) pattern, flags);
57 # if XLAT_VERBOSE
58         printf("memfd_create(\"%.*s\"..., %s /* %s */) = %s\n",
59                (int) size - 1, pattern,
60                "0x7", flags1_str, errstr);
61 # else
62         printf("memfd_create(\"%.*s\"..., %s) = %s\n",
63                (int) size - 1, pattern,
64 #  if XLAT_RAW
65                "0x7",
66 #  else
67                flags1_str,
68 #  endif
69                errstr);
70 # endif
71
72         pattern[size - 1] = '\0';
73         flags = 30 << MFD_HUGE_SHIFT;
74         k_memfd_create((uintptr_t) pattern, flags);
75 # if XLAT_RAW
76         printf("memfd_create(\"%s\", %#x) = %s\n",
77                pattern, (unsigned int) flags, errstr);
78 # elif XLAT_VERBOSE
79         printf("memfd_create(\"%s\", %#x /* %s */) = %s\n",
80                pattern, (unsigned int) flags, "30<<MFD_HUGE_SHIFT", errstr);
81 # else /* XLAT_ABBREV */
82         printf("memfd_create(\"%s\", 30<<MFD_HUGE_SHIFT) = %s\n",
83                pattern, errstr);
84 # endif
85
86         pattern += size - 1;
87         flags = (kernel_ulong_t) -1ULL;
88         k_memfd_create(0, flags);
89         flags = -1U & ~(7 | (MFD_HUGE_MASK << MFD_HUGE_SHIFT));
90
91 # define memfd_create_fmt "%s|%#x|%u<<MFD_HUGE_SHIFT"
92
93         printf("memfd_create(NULL, "
94 # if XLAT_RAW
95                "0xffffffff) = %s\n",
96 # else
97 #  if XLAT_VERBOSE
98                "0xffffffff /* " memfd_create_fmt " */"
99 #  else /* XLAT_ABBREV */
100                memfd_create_fmt
101 #  endif
102                ") = %s\n",
103                flags1_str, (unsigned int) flags, MFD_HUGE_MASK,
104 # endif
105                errstr);
106
107         puts("+++ exited with 0 +++");
108         return 0;
109 }
110
111 #else
112
113 SKIP_MAIN_UNDEFINED("__NR_memfd_create")
114
115 #endif