Blame misc/tst-preadvwritev2-common.c

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