gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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 <memory>
    21  #include <string>
    22  #include <vector>
    23  
    24  #include "gtest/gtest.h"
    25  #include "absl/memory/memory.h"
    26  #include "test/syscalls/linux/file_base.h"
    27  #include "test/util/file_descriptor.h"
    28  #include "test/util/temp_path.h"
    29  #include "test/util/test_util.h"
    30  
    31  namespace gvisor {
    32  namespace testing {
    33  
    34  namespace {
    35  
    36  #ifndef SYS_preadv2
    37  #if defined(__x86_64__)
    38  #define SYS_preadv2 327
    39  #elif defined(__aarch64__)
    40  #define SYS_preadv2 286
    41  #else
    42  #error "Unknown architecture"
    43  #endif
    44  #endif  // SYS_preadv2
    45  
    46  #ifndef RWF_HIPRI
    47  #define RWF_HIPRI 0x1
    48  #endif  // RWF_HIPRI
    49  
    50  constexpr int kBufSize = 1024;
    51  
    52  std::string SetContent() {
    53    std::string content;
    54    for (int i = 0; i < kBufSize; i++) {
    55      content += static_cast<char>((i % 10) + '0');
    56    }
    57    return content;
    58  }
    59  
    60  ssize_t preadv2(unsigned long fd, const struct iovec* iov, unsigned long iovcnt,
    61                  off_t offset, unsigned long flags) {
    62    // syscall on preadv2 does some weird things (see man syscall and search
    63    // preadv2), so we insert a 0 to word align the flags argument on native.
    64    return syscall(SYS_preadv2, fd, iov, iovcnt, offset, 0, flags);
    65  }
    66  
    67  // This test is the base case where we call preadv (no offset, no flags).
    68  TEST(Preadv2Test, TestBaseCall) {
    69    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
    70  
    71    std::string content = SetContent();
    72  
    73    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
    74        GetAbsoluteTestTmpdir(), content, TempPath::kDefaultFileMode));
    75    const FileDescriptor fd =
    76        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
    77  
    78    std::vector<char> buf(kBufSize);
    79    struct iovec iov[2];
    80    iov[0].iov_base = buf.data();
    81    iov[0].iov_len = buf.size() / 2;
    82    iov[1].iov_base = static_cast<char*>(iov[0].iov_base) + (content.size() / 2);
    83    iov[1].iov_len = content.size() / 2;
    84  
    85    EXPECT_THAT(preadv2(fd.get(), iov, /*iovcnt*/ 2, /*offset=*/0, /*flags=*/0),
    86                SyscallSucceedsWithValue(kBufSize));
    87  
    88    EXPECT_EQ(content, std::string(buf.data(), buf.size()));
    89  }
    90  
    91  // This test is where we call preadv with an offset and no flags.
    92  TEST(Preadv2Test, TestValidPositiveOffset) {
    93    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
    94  
    95    std::string content = SetContent();
    96    const std::string prefix = "0";
    97  
    98    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
    99        GetAbsoluteTestTmpdir(), prefix + content, TempPath::kDefaultFileMode));
   100    const FileDescriptor fd =
   101        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
   102  
   103    std::vector<char> buf(kBufSize, '0');
   104    struct iovec iov;
   105    iov.iov_base = buf.data();
   106    iov.iov_len = buf.size();
   107  
   108    EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/prefix.size(),
   109                        /*flags=*/0),
   110                SyscallSucceedsWithValue(kBufSize));
   111  
   112    EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), SyscallSucceedsWithValue(0));
   113  
   114    EXPECT_EQ(content, std::string(buf.data(), buf.size()));
   115  }
   116  
   117  // This test is the base case where we call readv by using -1 as the offset. The
   118  // read should use the file offset, so the test increments it by one prior to
   119  // calling preadv2.
   120  TEST(Preadv2Test, TestNegativeOneOffset) {
   121    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
   122  
   123    std::string content = SetContent();
   124    const std::string prefix = "231";
   125  
   126    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
   127        GetAbsoluteTestTmpdir(), prefix + content, TempPath::kDefaultFileMode));
   128    const FileDescriptor fd =
   129        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
   130  
   131    ASSERT_THAT(lseek(fd.get(), prefix.size(), SEEK_SET),
   132                SyscallSucceedsWithValue(prefix.size()));
   133  
   134    std::vector<char> buf(kBufSize, '0');
   135    struct iovec iov;
   136    iov.iov_base = buf.data();
   137    iov.iov_len = buf.size();
   138  
   139    EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/-1, /*flags=*/0),
   140                SyscallSucceedsWithValue(kBufSize));
   141  
   142    EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR),
   143                SyscallSucceedsWithValue(prefix.size() + buf.size()));
   144  
   145    EXPECT_EQ(content, std::string(buf.data(), buf.size()));
   146  }
   147  
   148  // preadv2 requires if the RWF_HIPRI flag is passed, the fd must be opened with
   149  // O_DIRECT. This test implements a correct call with the RWF_HIPRI flag.
   150  TEST(Preadv2Test, TestCallWithRWF_HIPRI) {
   151    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
   152  
   153    std::string content = SetContent();
   154  
   155    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
   156        GetAbsoluteTestTmpdir(), content, TempPath::kDefaultFileMode));
   157    const FileDescriptor fd =
   158        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
   159  
   160    EXPECT_THAT(fsync(fd.get()), SyscallSucceeds());
   161  
   162    std::vector<char> buf(kBufSize, '0');
   163    struct iovec iov;
   164    iov.iov_base = buf.data();
   165    iov.iov_len = buf.size();
   166  
   167    EXPECT_THAT(
   168        preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/0, /*flags=*/RWF_HIPRI),
   169        SyscallSucceedsWithValue(kBufSize));
   170  
   171    EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), SyscallSucceedsWithValue(0));
   172  
   173    EXPECT_EQ(content, std::string(buf.data(), buf.size()));
   174  }
   175  // This test calls preadv2 with an invalid flag.
   176  TEST(Preadv2Test, TestInvalidFlag) {
   177    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
   178  
   179    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
   180        GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
   181    const FileDescriptor fd =
   182        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY | O_DIRECT));
   183  
   184    std::vector<char> buf(kBufSize, '0');
   185    struct iovec iov;
   186    iov.iov_base = buf.data();
   187    iov.iov_len = buf.size();
   188  
   189    EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1,
   190                        /*offset=*/0, /*flags=*/0xF0),
   191                SyscallFailsWithErrno(EOPNOTSUPP));
   192  }
   193  
   194  // This test calls preadv2 with an invalid offset.
   195  TEST(Preadv2Test, TestInvalidOffset) {
   196    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
   197  
   198    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
   199        GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
   200    const FileDescriptor fd =
   201        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY | O_DIRECT));
   202  
   203    auto iov = std::make_unique<struct iovec[]>(1);
   204    iov[0].iov_base = nullptr;
   205    iov[0].iov_len = 0;
   206  
   207    EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1, /*offset=*/-8,
   208                        /*flags=*/0),
   209                SyscallFailsWithErrno(EINVAL));
   210  }
   211  
   212  // This test calls preadv with a file set O_WRONLY.
   213  TEST(Preadv2Test, TestUnreadableFile) {
   214    SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
   215  
   216    const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
   217        GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
   218    const FileDescriptor fd =
   219        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY));
   220  
   221    auto iov = std::make_unique<struct iovec[]>(1);
   222    iov[0].iov_base = nullptr;
   223    iov[0].iov_len = 0;
   224  
   225    EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1,
   226                        /*offset=*/0, /*flags=*/0),
   227                SyscallFailsWithErrno(EBADF));
   228  }
   229  
   230  // This test calls preadv2 with a file opened with O_PATH.
   231  TEST(Preadv2Test, Preadv2WithOpath) {
   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 = std::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 = std::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 = std::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