]>
Commit | Line | Data |
---|---|---|
daa9bdb6 | 1 | /* Common function for preadv2 and pwritev2 tests. |
dff8da6b | 2 | Copyright (C) 2017-2024 Free Software Foundation, Inc. |
daa9bdb6 AZ |
3 | This file is part of the GNU C Library. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
daa9bdb6 | 18 | |
94070f86 | 19 | #include <limits.h> |
daa9bdb6 AZ |
20 | #include <support/check.h> |
21 | ||
492cbbed ST |
22 | #ifndef RWF_HIPRI |
23 | # define RWF_HIPRI 0 | |
24 | #endif | |
25 | #ifndef RWF_DSYNC | |
26 | # define RWF_DSYNC 0 | |
27 | #endif | |
28 | #ifndef RWF_SYNC | |
29 | # define RWF_SYNC 0 | |
30 | #endif | |
31 | #ifndef RWF_NOWAIT | |
32 | # define RWF_NOWAIT 0 | |
33 | #endif | |
f2652643 L |
34 | #ifndef RWF_APPEND |
35 | # define RWF_APPEND 0 | |
36 | #endif | |
3db9d208 SH |
37 | #ifndef RWF_NOAPPEND |
38 | # define RWF_NOAPPEND 0 | |
39 | #endif | |
f2652643 | 40 | #define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT \ |
3db9d208 | 41 | | RWF_APPEND | RWF_NOAPPEND) |
7a16bdbb | 42 | |
18ad0de6 AZ |
43 | /* Generic uio_lim.h does not define IOV_MAX. */ |
44 | #ifndef IOV_MAX | |
45 | # define IOV_MAX 1024 | |
46 | #endif | |
47 | ||
7a16bdbb AZ |
48 | static void |
49 | do_test_with_invalid_fd (void) | |
50 | { | |
51 | char buf[256]; | |
52 | struct iovec iov = { buf, sizeof buf }; | |
53 | ||
54 | /* Check with flag being 0 to use the fallback code which calls pwritev | |
55 | or writev. */ | |
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); | |
60 | ||
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); | |
69 | } | |
70 | ||
71 | static void | |
72 | do_test_with_invalid_iov (void) | |
73 | { | |
74 | { | |
75 | char buf[256]; | |
76 | struct iovec iov; | |
77 | ||
78 | iov.iov_base = buf; | |
79 | iov.iov_len = (size_t)SSIZE_MAX + 1; | |
80 | ||
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); | |
85 | ||
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); | |
92 | } | |
93 | ||
94 | { | |
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 }; | |
98 | ||
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); | |
103 | } | |
104 | } | |
105 | ||
106 | static void | |
107 | do_test_with_invalid_flags (void) | |
108 | { | |
94070f86 | 109 | /* Set the next bit from the mask of all supported flags. */ |
492cbbed | 110 | int invalid_flag = RWF_SUPPORTED != 0 ? __builtin_clz (RWF_SUPPORTED) : 2; |
94070f86 | 111 | invalid_flag = 0x1 << ((sizeof (int) * CHAR_BIT) - invalid_flag); |
daa9bdb6 AZ |
112 | |
113 | char buf[32]; | |
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); | |
119 | ||
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); | |
126 | } |