Fix INTERNAL_[V]SYSCALL_NCS macros to not cast return val to int.
[glibc.git] / sysdeps / wordsize-64 / tst-writev.c
blob6e4788612cee34d0c3f90a255376f1d8a314d38b
1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ryan S. Arnold <rsa@us.ibm.com>, 2011.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <fcntl.h>
21 #include <paths.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/uio.h>
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'. */
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
39 /* writev() should report that it has written EXPECTED number of bytes. */
40 #define EXPECTED ((size_t) INT32_MAX + 1)
42 static int
43 do_test (void)
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;
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)
55 bufsz = bufsz + 1;
56 bufrem = EXPECTED - (bufsz * (IOV_MAX - 1));
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)
63 printf ("Unable to open /dev/null for writing.\n");
64 return -1;
67 iv[0].iov_base = malloc (bufsz);
68 if (iv[0].iov_base == NULL)
70 printf ("malloc (%zu) failed.\n", bufsz);
71 close (fd);
72 return -1;
74 iv[0].iov_len = bufsz;
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++)
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;
85 /* If there is a remainder then we correct the last iov_len. */
86 if (bufrem)
87 iv[IOV_MAX - 1].iov_len = bufrem;
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, iv, IOV_MAX);
94 free (iv[0].iov_base);
95 close (fd);
97 if (ret != (ssize_t) EXPECTED)
99 printf ("writev() return value: %zd != EXPECTED: %zd\n", ret, EXPECTED);
100 return 1;
103 return 0;
106 #define TEST_FUNCTION do_test ()
107 #include "../test-skeleton.c"