Blame misc/tst-preadvwritev2-common.c

Packit Service 82fcde
/* Common function for preadv2 and pwritev2 tests.
Packit Service 82fcde
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <limits.h>
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
Packit Service 82fcde
#ifndef RWF_HIPRI
Packit Service 82fcde
# define RWF_HIPRI 0
Packit Service 82fcde
#endif
Packit Service 82fcde
#ifndef RWF_DSYNC
Packit Service 82fcde
# define RWF_DSYNC 0
Packit Service 82fcde
#endif
Packit Service 82fcde
#ifndef RWF_SYNC
Packit Service 82fcde
# define RWF_SYNC 0
Packit Service 82fcde
#endif
Packit Service 82fcde
#ifndef RWF_NOWAIT
Packit Service 82fcde
# define RWF_NOWAIT 0
Packit Service 82fcde
#endif
Packit Service 82fcde
#ifndef RWF_APPEND
Packit Service 82fcde
# define RWF_APPEND 0
Packit Service 82fcde
#endif
Packit Service 82fcde
#define RWF_SUPPORTED	(RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT \
Packit Service 82fcde
			 | RWF_APPEND)
Packit Service 57ac06
Packit Service 57ac06
static void
Packit Service 57ac06
do_test_with_invalid_fd (void)
Packit Service 57ac06
{
Packit Service 57ac06
  char buf[256];
Packit Service 57ac06
  struct iovec iov = { buf, sizeof buf };
Packit Service 57ac06
Packit Service 57ac06
  /* Check with flag being 0 to use the fallback code which calls pwritev
Packit Service 57ac06
     or writev.  */
Packit Service 57ac06
  TEST_VERIFY (preadv2 (-1, &iov, 1, -1, 0) == -1);
Packit Service 57ac06
  TEST_COMPARE (errno, EBADF);
Packit Service 57ac06
  TEST_VERIFY (pwritev2 (-1, &iov, 1, -1, 0) == -1);
Packit Service 57ac06
  TEST_COMPARE (errno, EBADF);
Packit Service 57ac06
Packit Service 57ac06
  /* Same tests as before but with flags being different than 0.  Since
Packit Service 57ac06
     there is no emulation for any flag value, fallback code returns
Packit Service 57ac06
     ENOTSUP.  This is different running on a kernel with preadv2/pwritev2
Packit Service 57ac06
     support, where EBADF is returned).  */
Packit Service 57ac06
  TEST_VERIFY (preadv2 (-1, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 57ac06
  TEST_VERIFY (errno == EBADF || errno == ENOTSUP);
Packit Service 57ac06
  TEST_VERIFY (pwritev2 (-1, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 57ac06
  TEST_VERIFY (errno == EBADF || errno == ENOTSUP);
Packit Service 57ac06
}
Packit Service 57ac06
Packit Service 57ac06
static void
Packit Service 57ac06
do_test_with_invalid_iov (void)
Packit Service 57ac06
{
Packit Service 57ac06
  {
Packit Service 57ac06
    char buf[256];
Packit Service 57ac06
    struct iovec iov;
Packit Service 57ac06
Packit Service 57ac06
    iov.iov_base = buf;
Packit Service 57ac06
    iov.iov_len = (size_t)SSIZE_MAX + 1;
Packit Service 57ac06
Packit Service 57ac06
    TEST_VERIFY (preadv2 (temp_fd, &iov, 1, 0, 0) == -1);
Packit Service 57ac06
    TEST_COMPARE (errno, EINVAL);
Packit Service 57ac06
    TEST_VERIFY (pwritev2 (temp_fd, &iov, 1, 0, 0) == -1);
Packit Service 57ac06
    TEST_COMPARE (errno, EINVAL);
Packit Service 57ac06
Packit Service 57ac06
    /* Same as for invalid file descriptor tests, emulation fallback
Packit Service 57ac06
       first checks for flag value and return ENOTSUP.  */
Packit Service 57ac06
    TEST_VERIFY (preadv2 (temp_fd, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 57ac06
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 57ac06
    TEST_VERIFY (pwritev2 (temp_fd, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 57ac06
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 57ac06
  }
Packit Service 57ac06
Packit Service 57ac06
  {
Packit Service 57ac06
    /* An invalid iovec buffer should trigger an invalid memory access
Packit Service 57ac06
       or an error (Linux for instance returns EFAULT).  */
Packit Service 57ac06
    struct iovec iov[IOV_MAX+1] = { 0 };
Packit Service 57ac06
Packit Service 57ac06
    TEST_VERIFY (preadv2 (temp_fd, iov, IOV_MAX + 1, 0, RWF_HIPRI) == -1);
Packit Service 57ac06
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 57ac06
    TEST_VERIFY (pwritev2 (temp_fd, iov, IOV_MAX + 1, 0, RWF_HIPRI) == -1);
Packit Service 57ac06
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 57ac06
  }
Packit Service 57ac06
}
Packit Service 57ac06
Packit Service 57ac06
static void
Packit Service 57ac06
do_test_with_invalid_flags (void)
Packit Service 57ac06
{
Packit Service 82fcde
  /* Set the next bit from the mask of all supported flags.  */
Packit Service 82fcde
  int invalid_flag = RWF_SUPPORTED != 0 ? __builtin_clz (RWF_SUPPORTED) : 2;
Packit Service 82fcde
  invalid_flag = 0x1 << ((sizeof (int) * CHAR_BIT) - invalid_flag);
Packit Service 82fcde
Packit Service 82fcde
  char buf[32];
Packit Service 82fcde
  const struct iovec vec = { .iov_base = buf, .iov_len = sizeof (buf) };
Packit Service 82fcde
  if (preadv2 (temp_fd, &vec, 1, 0, invalid_flag) != -1)
Packit Service 82fcde
    FAIL_EXIT1 ("preadv2 did not fail with an invalid flag");
Packit Service 82fcde
  if (errno != ENOTSUP)
Packit Service 82fcde
    FAIL_EXIT1 ("preadv2 failure did not set errno to ENOTSUP (%d)", errno);
Packit Service 82fcde
Packit Service 82fcde
  /* This might fail for compat syscall (32 bits running on 64 bits kernel)
Packit Service 82fcde
     due a kernel issue.  */
Packit Service 82fcde
  if (pwritev2 (temp_fd, &vec, 1, 0, invalid_flag) != -1)
Packit Service 82fcde
    FAIL_EXIT1 ("pwritev2 did not fail with an invalid flag");
Packit Service 82fcde
  if (errno != ENOTSUP)
Packit Service 82fcde
    FAIL_EXIT1 ("pwritev2 failure did not set errno to ENOTSUP (%d)", errno);
Packit Service 82fcde
}