1 /* 2 * Copyright (C) 2017 Red Hat, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 of 7 * the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it would be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 */ 15 16 #ifndef MEMFD_TEST_COMMON 17 #define MEMFD_TEST_COMMON 18 19 #include <sys/types.h> 20 #include <lapi/fcntl.h> 21 #include <lapi/memfd.h> 22 23 /* change macros accordingly if any flags need to be added in the future */ 24 #define FLAGS_ALL_ARRAY_INITIALIZER {MFD_CLOEXEC, MFD_ALLOW_SEALING} 25 #define FLAGS_ALL_MASK (MFD_CLOEXEC | MFD_ALLOW_SEALING) 26 27 #define MFD_DEF_SIZE 8192 28 29 #define GET_MFD_ALL_AVAILABLE_FLAGS() \ 30 get_mfd_all_available_flags(__FILE__, __LINE__) 31 32 #define MFD_FLAGS_AVAILABLE(flags) \ 33 mfd_flags_available(__FILE__, __LINE__, (flags)) 34 35 #define CHECK_MFD_NEW(name, sz, flags) \ 36 check_mfd_new(__FILE__, __LINE__, (name), (sz), (flags)) 37 38 #define CHECK_MFD_FAIL_NEW(name, flags) \ 39 check_mfd_fail_new(__FILE__, __LINE__, (name), (flags)) 40 41 #define CHECK_MMAP(addr, length, prot, flags, fd, offset) \ 42 check_mmap(__FILE__, __LINE__, (addr), (length), (prot), \ 43 (flags), (fd), (offset)) 44 45 #define CHECK_MMAP_FAIL(addr, length, prot, flags, fd, offset) \ 46 check_mmap_fail(__FILE__, __LINE__, (addr), (length), (prot), \ 47 (flags), (fd), (offset)) 48 49 #define CHECK_MUNMAP(p, length) \ 50 check_munmap(__FILE__, __LINE__, (p), (length)) 51 52 #define CHECK_MFD_HAS_SEALS(fd, seals) \ 53 check_mfd_has_seals(__FILE__, __LINE__, (fd), (seals)); 54 55 #define CHECK_MFD_ADD_SEALS(fd, seals) \ 56 ({int r = SAFE_FCNTL((fd), F_ADD_SEALS, (seals)); \ 57 tst_res(TPASS, "fcntl(%d, F_ADD_SEALS, %d) succeeded", \ 58 (fd), (seals)); r; }) 59 60 #define CHECK_MFD_FAIL_ADD_SEALS(fd, seals) \ 61 check_mfd_fail_add_seals(__FILE__, __LINE__, (fd), (seals)) 62 63 #define CHECK_MFD_SIZE(fd, size) \ 64 check_mfd_size(__FILE__, __LINE__, (fd), (size)) 65 66 #define CHECK_MFD_OPEN(fd, flags, mode) \ 67 check_mfd_open(__FILE__, __LINE__, (fd), (flags), (mode)) 68 69 #define CHECK_MFD_FAIL_OPEN(fd, flags, mode) \ 70 check_mfd_fail_open(__FILE__, __LINE__, (fd), (flags), (mode)) 71 72 #define CHECK_MFD_READABLE(fd) \ 73 check_mfd_readable(__FILE__, __LINE__, (fd)) 74 75 #define CHECK_MFD_WRITEABLE(fd) \ 76 check_mfd_writeable(__FILE__, __LINE__, (fd)) 77 78 #define CHECK_MFD_NON_WRITEABLE(fd) \ 79 check_mfd_non_writeable(__FILE__, __LINE__, (fd)) 80 81 #define CHECK_MFD_SHRINKABLE(fd) \ 82 check_mfd_shrinkable(__FILE__, __LINE__, (fd)) 83 84 #define CHECK_MFD_NON_SHRINKABLE(fd) \ 85 check_mfd_non_shrinkable(__FILE__, __LINE__, (fd)) 86 87 #define CHECK_MFD_GROWABLE(fd) \ 88 check_mfd_growable(__FILE__, __LINE__, (fd)) 89 90 #define CHECK_MFD_NON_GROWABLE(fd) \ 91 check_mfd_non_growable(__FILE__, __LINE__, (fd)) 92 93 #define CHECK_MFD_GROWABLE_BY_WRITE(fd) \ 94 check_mfd_growable_by_write(__FILE__, __LINE__, (fd)) 95 96 #define CHECK_MFD_NON_GROWABLE_BY_WRITE(fd) \ 97 check_mfd_non_growable_by_write(__FILE__, __LINE__, (fd)) 98 99 int mfd_flags_available(const char *filename, const int lineno, 100 unsigned int flags); 101 102 int get_mfd_all_available_flags(const char *filename, const int lineno); 103 104 int sys_memfd_create(const char *name, unsigned int flags); 105 106 int check_fallocate(const char *filename, const int lineno, int fd, 107 int mode, off_t offset, off_t len); 108 int check_fallocate_fail(const char *filename, const int lineno, int fd, 109 int mode, off_t offset, off_t len); 110 void check_ftruncate(const char *filename, const int lineno, int fd, 111 off_t length); 112 void check_ftruncate_fail(const char *filename, const int lineno, int fd, 113 off_t length); 114 115 int check_mfd_new(const char *filename, const int lineno, 116 const char *name, loff_t sz, int flags); 117 void check_mfd_fail_new(const char *filename, const int lineno, 118 const char *name, int flags); 119 120 void *check_mmap(const char *file, const int lineno, void *addr, size_t length, 121 int prot, int flags, int fd, off_t offset); 122 void check_mmap_fail(const char *file, const int lineno, void *addr, 123 size_t length, int prot, int flags, int fd, off_t offset); 124 125 void check_munmap(const char *file, const int lineno, void *p, size_t length); 126 127 void check_mfd_has_seals(const char *file, const int lineno, int fd, int seals); 128 129 void check_mprotect(const char *file, const int lineno, void *addr, 130 size_t length, int prot); 131 132 void check_mfd_fail_add_seals(const char *filename, const int lineno, int fd, 133 int seals); 134 135 void check_mfd_size(const char *filename, const int lineno, int fd, 136 size_t size); 137 138 int check_mfd_open(const char *filename, const int lineno, int fd, 139 int flags, mode_t mode); 140 void check_mfd_fail_open(const char *filename, const int lineno, int fd, 141 int flags, mode_t mode); 142 143 void check_mfd_readable(const char *filename, const int lineno, int fd); 144 145 void check_mfd_writeable(const char *filename, const int lineno, int fd); 146 void check_mfd_non_writeable(const char *filename, const int lineno, int fd); 147 148 void check_mfd_shrinkable(const char *filename, const int lineno, int fd); 149 void check_mfd_non_shrinkable(const char *filename, const int lineno, int fd); 150 151 void check_mfd_growable(const char *filename, const int lineno, int fd); 152 void check_mfd_non_growable(const char *filename, const int lineno, int fd); 153 154 void check_mfd_growable_by_write(const char *filename, const int lineno, 155 int fd); 156 void check_mfd_non_growable_by_write(const char *filename, const int lineno, 157 int fd); 158 159 #endif 160