1/* Copyright (C) 2011-2024 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/* GNU/Hurd does not define a IOV_MAX constraint. */
28#ifndef IOV_MAX
29# define IOV_MAX 1024
30#endif
31
32
33/* The purpose of this test is to verify that the INTERNAL_[V]SYSCALL_NCS
34 macros on 64-bit platforms don't cast the return type to (int) which would
35 erroneously sign extend the return value should the high bit of the bottom
36 half of the word be '1'. */
37
38#if 0
39/* Used to test the non power-of-2 code path. */
40#undef IOV_MAX
41#define IOV_MAX 1000
42#endif
43
44/* writev() should report that it has written EXPECTED number of bytes. */
45#define EXPECTED ((size_t) INT32_MAX + 1)
46
47static int
48do_test (void)
49{
50 struct iovec iv[IOV_MAX];
51 /* POSIX doesn't guarantee that IOV_MAX is pow of 2 but we're optimistic. */
52 size_t bufsz = EXPECTED / IOV_MAX;
53 size_t bufrem = EXPECTED % IOV_MAX;
54
55 /* If there's a remainder then IOV_MAX probably isn't a power of 2 and we
56 need to make bufsz bigger so that the last iovec, iv[IOV_MAX-1], is free
57 for the remainder. */
58 if (bufrem)
59 {
60 bufsz = bufsz + 1;
61 bufrem = EXPECTED - (bufsz * (IOV_MAX - 1));
62 }
63
64 /* We writev to /dev/null since we're just testing writev's return value. */
65 int fd = open (_PATH_DEVNULL, O_WRONLY);
66 if (fd == -1)
67 {
68 printf (format: "Unable to open /dev/null for writing.\n");
69 return -1;
70 }
71
72 iv[0].iov_base = malloc (size: bufsz);
73 if (iv[0].iov_base == NULL)
74 {
75 printf (format: "malloc (%zu) failed.\n", bufsz);
76 close (fd: fd);
77 return -1;
78 }
79 iv[0].iov_len = bufsz;
80
81 /* We optimistically presume that there isn't a remainder and set all iovec
82 instances to the same base and len as the first instance. */
83 for (int i = 1; i < IOV_MAX; i++)
84 {
85 /* We don't care what the data is so reuse the allocation from iv[0]; */
86 iv[i].iov_base = iv[0].iov_base;
87 iv[i].iov_len = iv[0].iov_len;
88 }
89
90 /* If there is a remainder then we correct the last iov_len. */
91 if (bufrem)
92 iv[IOV_MAX - 1].iov_len = bufrem;
93
94 /* Write junk to /dev/null with the writev syscall in order to get a return
95 of INT32_MAX+1 bytes to verify that the INTERNAL_SYSCALL wrappers aren't
96 mangling the result if the signbit of a 32-bit number is set. */
97 ssize_t ret = writev (fd: fd, iovec: iv, IOV_MAX);
98
99 free (ptr: iv[0].iov_base);
100 close (fd: fd);
101
102 if (ret != (ssize_t) EXPECTED)
103 {
104#ifdef ARTIFICIAL_LIMIT
105 if (ret != (ssize_t) ARTIFICIAL_LIMIT)
106#endif
107 {
108 printf (format: "writev() return value: %zd != EXPECTED: %zd\n",
109 ret, EXPECTED);
110 return 1;
111 }
112 }
113
114 return 0;
115}
116
117#define TEST_FUNCTION do_test ()
118#include "../test-skeleton.c"
119

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