2 * Check decoding of memfd_create syscall.
4 * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2015-2019 The strace developers.
8 * SPDX-License-Identifier: GPL-2.0-or-later
14 #ifdef __NR_memfd_create
20 # ifdef HAVE_LINUX_MEMFD_H
21 # include <linux/memfd.h>
24 # ifndef MFD_HUGE_SHIFT
25 # define MFD_HUGE_SHIFT 26
28 # ifndef MFD_HUGE_MASK
29 # define MFD_HUGE_MASK 0x3f
32 static const char *errstr;
35 k_memfd_create(const kernel_ulong_t name, const kernel_ulong_t flags)
37 const long rc = syscall(__NR_memfd_create, name, flags);
38 errstr = sprintrc(rc);
45 const size_t size = 255 - (sizeof("memfd:") - 1) + 1;
46 char *pattern = tail_alloc(size);
47 fill_memory_ex(pattern, size, '0', 10);
49 k_memfd_create((uintptr_t) pattern, 0);
50 printf("memfd_create(\"%.*s\"..., 0) = %s\n",
51 (int) size - 1, pattern, errstr);
53 kernel_ulong_t flags = (kernel_ulong_t) 0xfacefeed00000007ULL;
54 # define flags1_str "MFD_CLOEXEC|MFD_ALLOW_SEALING|MFD_HUGETLB"
56 k_memfd_create((uintptr_t) pattern, flags);
58 printf("memfd_create(\"%.*s\"..., %s /* %s */) = %s\n",
59 (int) size - 1, pattern,
60 "0x7", flags1_str, errstr);
62 printf("memfd_create(\"%.*s\"..., %s) = %s\n",
63 (int) size - 1, pattern,
72 pattern[size - 1] = '\0';
73 flags = 30 << MFD_HUGE_SHIFT;
74 k_memfd_create((uintptr_t) pattern, flags);
76 printf("memfd_create(\"%s\", %#x) = %s\n",
77 pattern, (unsigned int) flags, errstr);
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",
87 flags = (kernel_ulong_t) -1ULL;
88 k_memfd_create(0, flags);
89 flags = -1U & ~(7 | (MFD_HUGE_MASK << MFD_HUGE_SHIFT));
91 # define memfd_create_fmt "%s|%#x|%u<<MFD_HUGE_SHIFT"
93 printf("memfd_create(NULL, "
98 "0xffffffff /* " memfd_create_fmt " */"
99 # else /* XLAT_ABBREV */
103 flags1_str, (unsigned int) flags, MFD_HUGE_MASK,
107 puts("+++ exited with 0 +++");
113 SKIP_MAIN_UNDEFINED("__NR_memfd_create")