1/* Copyright (C) 2011-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <fcntl.h>
19#include <limits.h>
20#include <paths.h>
21#include <stdio.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <sys/uio.h>
26
27
28/* The purpose of this test is to verify that the INTERNAL_[V]SYSCALL_NCS
29 macros on 64-bit platforms don't cast the return type to (int) which would
30 erroneously sign extend the return value should the high bit of the bottom
31 half of the word be '1'. */
32
33#if 0
34/* Used to test the non power-of-2 code path. */
35#undef IOV_MAX
36#define IOV_MAX 1000
37#endif
38
39/* writev() should report that it has written EXPECTED number of bytes. */
40#define EXPECTED ((size_t) INT32_MAX + 1)
41
42static int
43do_test (void)
44{
45 struct iovec iv[IOV_MAX];
46 /* POSIX doesn't guarantee that IOV_MAX is pow of 2 but we're optimistic. */
47 size_t bufsz = EXPECTED / IOV_MAX;
48 size_t bufrem = EXPECTED % IOV_MAX;
49
50 /* If there's a remainder then IOV_MAX probably isn't a power of 2 and we
51 need to make bufsz bigger so that the last iovec, iv[IOV_MAX-1], is free
52 for the remainder. */
53 if (bufrem)
54 {
55 bufsz = bufsz + 1;
56 bufrem = EXPECTED - (bufsz * (IOV_MAX - 1));
57 }
58
59 /* We writev to /dev/null since we're just testing writev's return value. */
60 int fd = open (_PATH_DEVNULL, O_WRONLY);
61 if (fd == -1)
62 {
63 printf (format: "Unable to open /dev/null for writing.\n");
64 return -1;
65 }
66
67 iv[0].iov_base = malloc (size: bufsz);
68 if (iv[0].iov_base == NULL)
69 {
70 printf (format: "malloc (%zu) failed.\n", bufsz);
71 close (fd: fd);
72 return -1;
73 }
74 iv[0].iov_len = bufsz;
75
76 /* We optimistically presume that there isn't a remainder and set all iovec
77 instances to the same base and len as the first instance. */
78 for (int i = 1; i < IOV_MAX; i++)
79 {
80 /* We don't care what the data is so reuse the allocation from iv[0]; */
81 iv[i].iov_base = iv[0].iov_base;
82 iv[i].iov_len = iv[0].iov_len;
83 }
84
85 /* If there is a remainder then we correct the last iov_len. */
86 if (bufrem)
87 iv[IOV_MAX - 1].iov_len = bufrem;
88
89 /* Write junk to /dev/null with the writev syscall in order to get a return
90 of INT32_MAX+1 bytes to verify that the INTERNAL_SYSCALL wrappers aren't
91 mangling the result if the signbit of a 32-bit number is set. */
92 ssize_t ret = writev (fd: fd, iovec: iv, IOV_MAX);
93
94 free (ptr: iv[0].iov_base);
95 close (fd: fd);
96
97 if (ret != (ssize_t) EXPECTED)
98 {
99#ifdef ARTIFICIAL_LIMIT
100 if (ret != (ssize_t) ARTIFICIAL_LIMIT)
101#endif
102 {
103 printf (format: "writev() return value: %zd != EXPECTED: %zd\n",
104 ret, EXPECTED);
105 return 1;
106 }
107 }
108
109 return 0;
110}
111
112#define TEST_FUNCTION do_test ()
113#include "../test-skeleton.c"
114

source code of glibc/sysdeps/wordsize-64/tst-writev.c