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 Service 16df1c
Packit Service 16df1c
static void
Packit Service 16df1c
do_test_with_invalid_fd (void)
Packit Service 16df1c
{
Packit Service 16df1c
  char buf[256];
Packit Service 16df1c
  struct iovec iov = { buf, sizeof buf };
Packit Service 16df1c
Packit Service 16df1c
  /* Check with flag being 0 to use the fallback code which calls pwritev
Packit Service 16df1c
     or writev.  */
Packit Service 16df1c
  TEST_VERIFY (preadv2 (-1, &iov, 1, -1, 0) == -1);
Packit Service 16df1c
  TEST_COMPARE (errno, EBADF);
Packit Service 16df1c
  TEST_VERIFY (pwritev2 (-1, &iov, 1, -1, 0) == -1);
Packit Service 16df1c
  TEST_COMPARE (errno, EBADF);
Packit Service 16df1c
Packit Service 16df1c
  /* Same tests as before but with flags being different than 0.  Since
Packit Service 16df1c
     there is no emulation for any flag value, fallback code returns
Packit Service 16df1c
     ENOTSUP.  This is different running on a kernel with preadv2/pwritev2
Packit Service 16df1c
     support, where EBADF is returned).  */
Packit Service 16df1c
  TEST_VERIFY (preadv2 (-1, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 16df1c
  TEST_VERIFY (errno == EBADF || errno == ENOTSUP);
Packit Service 16df1c
  TEST_VERIFY (pwritev2 (-1, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 16df1c
  TEST_VERIFY (errno == EBADF || errno == ENOTSUP);
Packit Service 16df1c
}
Packit Service 16df1c
Packit Service 16df1c
static void
Packit Service 16df1c
do_test_with_invalid_iov (void)
Packit Service 16df1c
{
Packit Service 16df1c
  {
Packit Service 16df1c
    char buf[256];
Packit Service 16df1c
    struct iovec iov;
Packit Service 16df1c
Packit Service 16df1c
    iov.iov_base = buf;
Packit Service 16df1c
    iov.iov_len = (size_t)SSIZE_MAX + 1;
Packit Service 16df1c
Packit Service 16df1c
    TEST_VERIFY (preadv2 (temp_fd, &iov, 1, 0, 0) == -1);
Packit Service 16df1c
    TEST_COMPARE (errno, EINVAL);
Packit Service 16df1c
    TEST_VERIFY (pwritev2 (temp_fd, &iov, 1, 0, 0) == -1);
Packit Service 16df1c
    TEST_COMPARE (errno, EINVAL);
Packit Service 16df1c
Packit Service 16df1c
    /* Same as for invalid file descriptor tests, emulation fallback
Packit Service 16df1c
       first checks for flag value and return ENOTSUP.  */
Packit Service 16df1c
    TEST_VERIFY (preadv2 (temp_fd, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 16df1c
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 16df1c
    TEST_VERIFY (pwritev2 (temp_fd, &iov, 1, 0, RWF_HIPRI) == -1);
Packit Service 16df1c
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 16df1c
  }
Packit Service 16df1c
Packit Service 16df1c
  {
Packit Service 16df1c
    /* An invalid iovec buffer should trigger an invalid memory access
Packit Service 16df1c
       or an error (Linux for instance returns EFAULT).  */
Packit Service 16df1c
    struct iovec iov[IOV_MAX+1] = { 0 };
Packit Service 16df1c
Packit Service 16df1c
    TEST_VERIFY (preadv2 (temp_fd, iov, IOV_MAX + 1, 0, RWF_HIPRI) == -1);
Packit Service 16df1c
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 16df1c
    TEST_VERIFY (pwritev2 (temp_fd, iov, IOV_MAX + 1, 0, RWF_HIPRI) == -1);
Packit Service 16df1c
    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
Packit Service 16df1c
  }
Packit Service 16df1c
}
Packit Service 16df1c
Packit Service 16df1c
static void
Packit Service 16df1c
do_test_with_invalid_flags (void)
Packit Service 16df1c
{
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
}