1 /* Common function for preadv2 and pwritev2 tests.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #include <support/check.h>
38 # define RWF_NOAPPEND 0
40 #define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT \
41 | RWF_APPEND | RWF_NOAPPEND)
43 /* Generic uio_lim.h does not define IOV_MAX. */
49 do_test_with_invalid_fd (void)
52 struct iovec iov
= { buf
, sizeof buf
};
54 /* Check with flag being 0 to use the fallback code which calls pwritev
56 TEST_VERIFY (preadv2 (-1, &iov
, 1, -1, 0) == -1);
57 TEST_COMPARE (errno
, EBADF
);
58 TEST_VERIFY (pwritev2 (-1, &iov
, 1, -1, 0) == -1);
59 TEST_COMPARE (errno
, EBADF
);
61 /* Same tests as before but with flags being different than 0. Since
62 there is no emulation for any flag value, fallback code returns
63 ENOTSUP. This is different running on a kernel with preadv2/pwritev2
64 support, where EBADF is returned). */
65 TEST_VERIFY (preadv2 (-1, &iov
, 1, 0, RWF_HIPRI
) == -1);
66 TEST_VERIFY (errno
== EBADF
|| errno
== ENOTSUP
);
67 TEST_VERIFY (pwritev2 (-1, &iov
, 1, 0, RWF_HIPRI
) == -1);
68 TEST_VERIFY (errno
== EBADF
|| errno
== ENOTSUP
);
72 do_test_with_invalid_iov (void)
79 iov
.iov_len
= (size_t)SSIZE_MAX
+ 1;
81 TEST_VERIFY (preadv2 (temp_fd
, &iov
, 1, 0, 0) == -1);
82 TEST_COMPARE (errno
, EINVAL
);
83 TEST_VERIFY (pwritev2 (temp_fd
, &iov
, 1, 0, 0) == -1);
84 TEST_COMPARE (errno
, EINVAL
);
86 /* Same as for invalid file descriptor tests, emulation fallback
87 first checks for flag value and return ENOTSUP. */
88 TEST_VERIFY (preadv2 (temp_fd
, &iov
, 1, 0, RWF_HIPRI
) == -1);
89 TEST_VERIFY (errno
== EINVAL
|| errno
== ENOTSUP
);
90 TEST_VERIFY (pwritev2 (temp_fd
, &iov
, 1, 0, RWF_HIPRI
) == -1);
91 TEST_VERIFY (errno
== EINVAL
|| errno
== ENOTSUP
);
95 /* An invalid iovec buffer should trigger an invalid memory access
96 or an error (Linux for instance returns EFAULT). */
97 struct iovec iov
[IOV_MAX
+1] = { 0 };
99 TEST_VERIFY (preadv2 (temp_fd
, iov
, IOV_MAX
+ 1, 0, RWF_HIPRI
) == -1);
100 TEST_VERIFY (errno
== EINVAL
|| errno
== ENOTSUP
);
101 TEST_VERIFY (pwritev2 (temp_fd
, iov
, IOV_MAX
+ 1, 0, RWF_HIPRI
) == -1);
102 TEST_VERIFY (errno
== EINVAL
|| errno
== ENOTSUP
);
107 do_test_with_invalid_flags (void)
109 /* Set the next bit from the mask of all supported flags. */
110 int invalid_flag
= RWF_SUPPORTED
!= 0 ? __builtin_clz (RWF_SUPPORTED
) : 2;
111 invalid_flag
= 0x1 << ((sizeof (int) * CHAR_BIT
) - invalid_flag
);
114 const struct iovec vec
= { .iov_base
= buf
, .iov_len
= sizeof (buf
) };
115 if (preadv2 (temp_fd
, &vec
, 1, 0, invalid_flag
) != -1)
116 FAIL_EXIT1 ("preadv2 did not fail with an invalid flag");
117 if (errno
!= ENOTSUP
)
118 FAIL_EXIT1 ("preadv2 failure did not set errno to ENOTSUP (%d)", errno
);
120 /* This might fail for compat syscall (32 bits running on 64 bits kernel)
121 due a kernel issue. */
122 if (pwritev2 (temp_fd
, &vec
, 1, 0, invalid_flag
) != -1)
123 FAIL_EXIT1 ("pwritev2 did not fail with an invalid flag");
124 if (errno
!= ENOTSUP
)
125 FAIL_EXIT1 ("pwritev2 failure did not set errno to ENOTSUP (%d)", errno
);