github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/preadv2.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <fcntl.h> 16 #include <sys/syscall.h> 17 #include <sys/types.h> 18 #include <sys/uio.h> 19 20 #include <string> 21 #include <vector> 22 23 #include "gtest/gtest.h" 24 #include "absl/memory/memory.h" 25 #include "test/syscalls/linux/file_base.h" 26 #include "test/util/file_descriptor.h" 27 #include "test/util/temp_path.h" 28 #include "test/util/test_util.h" 29 30 namespace gvisor { 31 namespace testing { 32 33 namespace { 34 35 #ifndef SYS_preadv2 36 #if defined(__x86_64__) 37 #define SYS_preadv2 327 38 #elif defined(__aarch64__) 39 #define SYS_preadv2 286 40 #else 41 #error "Unknown architecture" 42 #endif 43 #endif // SYS_preadv2 44 45 #ifndef RWF_HIPRI 46 #define RWF_HIPRI 0x1 47 #endif // RWF_HIPRI 48 49 constexpr int kBufSize = 1024; 50 51 std::string SetContent() { 52 std::string content; 53 for (int i = 0; i < kBufSize; i++) { 54 content += static_cast<char>((i % 10) + '0'); 55 } 56 return content; 57 } 58 59 ssize_t preadv2(unsigned long fd, const struct iovec* iov, unsigned long iovcnt, 60 off_t offset, unsigned long flags) { 61 // syscall on preadv2 does some weird things (see man syscall and search 62 // preadv2), so we insert a 0 to word align the flags argument on native. 63 return syscall(SYS_preadv2, fd, iov, iovcnt, offset, 0, flags); 64 } 65 66 // This test is the base case where we call preadv (no offset, no flags). 67 TEST(Preadv2Test, TestBaseCall) { 68 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 69 70 std::string content = SetContent(); 71 72 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 73 GetAbsoluteTestTmpdir(), content, TempPath::kDefaultFileMode)); 74 const FileDescriptor fd = 75 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY)); 76 77 std::vector<char> buf(kBufSize); 78 struct iovec iov[2]; 79 iov[0].iov_base = buf.data(); 80 iov[0].iov_len = buf.size() / 2; 81 iov[1].iov_base = static_cast<char*>(iov[0].iov_base) + (content.size() / 2); 82 iov[1].iov_len = content.size() / 2; 83 84 EXPECT_THAT(preadv2(fd.get(), iov, /*iovcnt*/ 2, /*offset=*/0, /*flags=*/0), 85 SyscallSucceedsWithValue(kBufSize)); 86 87 EXPECT_EQ(content, std::string(buf.data(), buf.size())); 88 } 89 90 // This test is where we call preadv with an offset and no flags. 91 TEST(Preadv2Test, TestValidPositiveOffset) { 92 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 93 94 std::string content = SetContent(); 95 const std::string prefix = "0"; 96 97 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 98 GetAbsoluteTestTmpdir(), prefix + content, TempPath::kDefaultFileMode)); 99 const FileDescriptor fd = 100 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY)); 101 102 std::vector<char> buf(kBufSize, '0'); 103 struct iovec iov; 104 iov.iov_base = buf.data(); 105 iov.iov_len = buf.size(); 106 107 EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/prefix.size(), 108 /*flags=*/0), 109 SyscallSucceedsWithValue(kBufSize)); 110 111 EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), SyscallSucceedsWithValue(0)); 112 113 EXPECT_EQ(content, std::string(buf.data(), buf.size())); 114 } 115 116 // This test is the base case where we call readv by using -1 as the offset. The 117 // read should use the file offset, so the test increments it by one prior to 118 // calling preadv2. 119 TEST(Preadv2Test, TestNegativeOneOffset) { 120 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 121 122 std::string content = SetContent(); 123 const std::string prefix = "231"; 124 125 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 126 GetAbsoluteTestTmpdir(), prefix + content, TempPath::kDefaultFileMode)); 127 const FileDescriptor fd = 128 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY)); 129 130 ASSERT_THAT(lseek(fd.get(), prefix.size(), SEEK_SET), 131 SyscallSucceedsWithValue(prefix.size())); 132 133 std::vector<char> buf(kBufSize, '0'); 134 struct iovec iov; 135 iov.iov_base = buf.data(); 136 iov.iov_len = buf.size(); 137 138 EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/-1, /*flags=*/0), 139 SyscallSucceedsWithValue(kBufSize)); 140 141 EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), 142 SyscallSucceedsWithValue(prefix.size() + buf.size())); 143 144 EXPECT_EQ(content, std::string(buf.data(), buf.size())); 145 } 146 147 // preadv2 requires if the RWF_HIPRI flag is passed, the fd must be opened with 148 // O_DIRECT. This test implements a correct call with the RWF_HIPRI flag. 149 TEST(Preadv2Test, TestCallWithRWF_HIPRI) { 150 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 151 152 std::string content = SetContent(); 153 154 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 155 GetAbsoluteTestTmpdir(), content, TempPath::kDefaultFileMode)); 156 const FileDescriptor fd = 157 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY)); 158 159 EXPECT_THAT(fsync(fd.get()), SyscallSucceeds()); 160 161 std::vector<char> buf(kBufSize, '0'); 162 struct iovec iov; 163 iov.iov_base = buf.data(); 164 iov.iov_len = buf.size(); 165 166 EXPECT_THAT( 167 preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/0, /*flags=*/RWF_HIPRI), 168 SyscallSucceedsWithValue(kBufSize)); 169 170 EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), SyscallSucceedsWithValue(0)); 171 172 EXPECT_EQ(content, std::string(buf.data(), buf.size())); 173 } 174 // This test calls preadv2 with an invalid flag. 175 TEST(Preadv2Test, TestInvalidFlag) { 176 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 177 178 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 179 GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode)); 180 const FileDescriptor fd = 181 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY | O_DIRECT)); 182 183 std::vector<char> buf(kBufSize, '0'); 184 struct iovec iov; 185 iov.iov_base = buf.data(); 186 iov.iov_len = buf.size(); 187 188 EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, 189 /*offset=*/0, /*flags=*/0xF0), 190 SyscallFailsWithErrno(EOPNOTSUPP)); 191 } 192 193 // This test calls preadv2 with an invalid offset. 194 TEST(Preadv2Test, TestInvalidOffset) { 195 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 196 197 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 198 GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode)); 199 const FileDescriptor fd = 200 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY | O_DIRECT)); 201 202 auto iov = absl::make_unique<struct iovec[]>(1); 203 iov[0].iov_base = nullptr; 204 iov[0].iov_len = 0; 205 206 EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1, /*offset=*/-8, 207 /*flags=*/0), 208 SyscallFailsWithErrno(EINVAL)); 209 } 210 211 // This test calls preadv with a file set O_WRONLY. 212 TEST(Preadv2Test, TestUnreadableFile) { 213 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 214 215 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 216 GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode)); 217 const FileDescriptor fd = 218 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY)); 219 220 auto iov = absl::make_unique<struct iovec[]>(1); 221 iov[0].iov_base = nullptr; 222 iov[0].iov_len = 0; 223 224 EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1, 225 /*offset=*/0, /*flags=*/0), 226 SyscallFailsWithErrno(EBADF)); 227 } 228 229 // This test calls preadv2 with a file opened with O_PATH. 230 TEST(Preadv2Test, Preadv2WithOpath) { 231 SKIP_IF(IsRunningWithVFS1()); 232 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 233 234 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 235 const FileDescriptor fd = 236 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_PATH)); 237 238 auto iov = absl::make_unique<struct iovec[]>(1); 239 iov[0].iov_base = nullptr; 240 iov[0].iov_len = 0; 241 242 EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1, /*offset=*/0, 243 /*flags=*/0), 244 SyscallFailsWithErrno(EBADF)); 245 } 246 247 // Calling preadv2 with a non-negative offset calls preadv. Calling preadv with 248 // an unseekable file is not allowed. A pipe is used for an unseekable file. 249 TEST(Preadv2Test, TestUnseekableFileInvalid) { 250 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 251 252 int pipe_fds[2]; 253 254 ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds()); 255 256 auto iov = absl::make_unique<struct iovec[]>(1); 257 iov[0].iov_base = nullptr; 258 iov[0].iov_len = 0; 259 260 EXPECT_THAT(preadv2(pipe_fds[0], iov.get(), /*iovcnt=*/1, 261 /*offset=*/2, /*flags=*/0), 262 SyscallFailsWithErrno(ESPIPE)); 263 264 EXPECT_THAT(close(pipe_fds[0]), SyscallSucceeds()); 265 EXPECT_THAT(close(pipe_fds[1]), SyscallSucceeds()); 266 } 267 268 TEST(Preadv2Test, TestUnseekableFileValid) { 269 SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS); 270 271 int pipe_fds[2]; 272 273 ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds()); 274 275 std::vector<char> content(32, 'X'); 276 277 EXPECT_THAT(write(pipe_fds[1], content.data(), content.size()), 278 SyscallSucceedsWithValue(content.size())); 279 280 std::vector<char> buf(content.size()); 281 auto iov = absl::make_unique<struct iovec[]>(1); 282 iov[0].iov_base = buf.data(); 283 iov[0].iov_len = buf.size(); 284 285 EXPECT_THAT(preadv2(pipe_fds[0], iov.get(), /*iovcnt=*/1, 286 /*offset=*/static_cast<off_t>(-1), /*flags=*/0), 287 SyscallSucceedsWithValue(buf.size())); 288 289 EXPECT_EQ(content, buf); 290 291 EXPECT_THAT(close(pipe_fds[0]), SyscallSucceeds()); 292 EXPECT_THAT(close(pipe_fds[1]), SyscallSucceeds()); 293 } 294 295 } // namespace 296 297 } // namespace testing 298 } // namespace gvisor