1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Check decoding of preadv and pwritev syscalls.
3*cf84ac9aSAndroid Build Coastguard Worker *
4*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2016 Dmitry V. Levin <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2016-2018 The strace developers.
6*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
7*cf84ac9aSAndroid Build Coastguard Worker *
8*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
9*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
10*cf84ac9aSAndroid Build Coastguard Worker * are met:
11*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
12*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
13*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
14*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
15*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
16*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
17*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
18*cf84ac9aSAndroid Build Coastguard Worker *
19*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*cf84ac9aSAndroid Build Coastguard Worker */
30*cf84ac9aSAndroid Build Coastguard Worker
31*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
32*cf84ac9aSAndroid Build Coastguard Worker
33*cf84ac9aSAndroid Build Coastguard Worker #if defined HAVE_PREADV && defined HAVE_PWRITEV
34*cf84ac9aSAndroid Build Coastguard Worker
35*cf84ac9aSAndroid Build Coastguard Worker # include <fcntl.h>
36*cf84ac9aSAndroid Build Coastguard Worker # include <stdio.h>
37*cf84ac9aSAndroid Build Coastguard Worker # include <sys/uio.h>
38*cf84ac9aSAndroid Build Coastguard Worker # include <unistd.h>
39*cf84ac9aSAndroid Build Coastguard Worker
40*cf84ac9aSAndroid Build Coastguard Worker int
main(void)41*cf84ac9aSAndroid Build Coastguard Worker main(void)
42*cf84ac9aSAndroid Build Coastguard Worker {
43*cf84ac9aSAndroid Build Coastguard Worker tprintf("%s", "");
44*cf84ac9aSAndroid Build Coastguard Worker
45*cf84ac9aSAndroid Build Coastguard Worker static char tmp[] = "preadv-pwritev-tmpfile";
46*cf84ac9aSAndroid Build Coastguard Worker if (open(tmp, O_CREAT|O_RDONLY|O_TRUNC, 0600) != 0)
47*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("creat: %s", tmp);
48*cf84ac9aSAndroid Build Coastguard Worker if (open(tmp, O_WRONLY) != 1)
49*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("open: %s", tmp);
50*cf84ac9aSAndroid Build Coastguard Worker if (unlink(tmp))
51*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("unlink: %s", tmp);
52*cf84ac9aSAndroid Build Coastguard Worker
53*cf84ac9aSAndroid Build Coastguard Worker static const char w0_c[] = "012";
54*cf84ac9aSAndroid Build Coastguard Worker const char *w0_d = hexdump_strdup(w0_c);
55*cf84ac9aSAndroid Build Coastguard Worker void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
56*cf84ac9aSAndroid Build Coastguard Worker
57*cf84ac9aSAndroid Build Coastguard Worker const void *efault = w0 + LENGTH_OF(w0_c);
58*cf84ac9aSAndroid Build Coastguard Worker
59*cf84ac9aSAndroid Build Coastguard Worker static const char w1_c[] = "34567";
60*cf84ac9aSAndroid Build Coastguard Worker const char *w1_d = hexdump_strdup(w1_c);
61*cf84ac9aSAndroid Build Coastguard Worker void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
62*cf84ac9aSAndroid Build Coastguard Worker
63*cf84ac9aSAndroid Build Coastguard Worker static const char w2_c[] = "89abcde";
64*cf84ac9aSAndroid Build Coastguard Worker const char *w2_d = hexdump_strdup(w2_c);
65*cf84ac9aSAndroid Build Coastguard Worker void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
66*cf84ac9aSAndroid Build Coastguard Worker
67*cf84ac9aSAndroid Build Coastguard Worker long rc;
68*cf84ac9aSAndroid Build Coastguard Worker
69*cf84ac9aSAndroid Build Coastguard Worker rc = pwritev(1, efault, 42, 0);
70*cf84ac9aSAndroid Build Coastguard Worker tprintf("pwritev(1, %p, 42, 0) = %ld %s (%m)\n",
71*cf84ac9aSAndroid Build Coastguard Worker efault, rc, errno2name());
72*cf84ac9aSAndroid Build Coastguard Worker
73*cf84ac9aSAndroid Build Coastguard Worker rc = preadv(0, efault, 42, 0);
74*cf84ac9aSAndroid Build Coastguard Worker tprintf("preadv(0, %p, 42, 0) = %ld %s (%m)\n",
75*cf84ac9aSAndroid Build Coastguard Worker efault, rc, errno2name());
76*cf84ac9aSAndroid Build Coastguard Worker
77*cf84ac9aSAndroid Build Coastguard Worker static const char r0_c[] = "01234567";
78*cf84ac9aSAndroid Build Coastguard Worker const char *r0_d = hexdump_strdup(r0_c);
79*cf84ac9aSAndroid Build Coastguard Worker static const char r1_c[] = "89abcde";
80*cf84ac9aSAndroid Build Coastguard Worker const char *r1_d = hexdump_strdup(r1_c);
81*cf84ac9aSAndroid Build Coastguard Worker
82*cf84ac9aSAndroid Build Coastguard Worker const struct iovec w_iov_[] = {
83*cf84ac9aSAndroid Build Coastguard Worker {
84*cf84ac9aSAndroid Build Coastguard Worker .iov_base = w0,
85*cf84ac9aSAndroid Build Coastguard Worker .iov_len = LENGTH_OF(w0_c)
86*cf84ac9aSAndroid Build Coastguard Worker }, {
87*cf84ac9aSAndroid Build Coastguard Worker .iov_base = w1,
88*cf84ac9aSAndroid Build Coastguard Worker .iov_len = LENGTH_OF(w1_c)
89*cf84ac9aSAndroid Build Coastguard Worker }, {
90*cf84ac9aSAndroid Build Coastguard Worker .iov_base = w2,
91*cf84ac9aSAndroid Build Coastguard Worker .iov_len = LENGTH_OF(w2_c)
92*cf84ac9aSAndroid Build Coastguard Worker }
93*cf84ac9aSAndroid Build Coastguard Worker };
94*cf84ac9aSAndroid Build Coastguard Worker const struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
95*cf84ac9aSAndroid Build Coastguard Worker
96*cf84ac9aSAndroid Build Coastguard Worker rc = pwritev(1, w_iov, 0, 0);
97*cf84ac9aSAndroid Build Coastguard Worker if (rc)
98*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("pwritev: expected 0, returned %ld", rc);
99*cf84ac9aSAndroid Build Coastguard Worker tprintf("pwritev(1, [], 0, 0) = 0\n");
100*cf84ac9aSAndroid Build Coastguard Worker
101*cf84ac9aSAndroid Build Coastguard Worker rc = pwritev(1, w_iov + ARRAY_SIZE(w_iov_) - 1, 2, 0);
102*cf84ac9aSAndroid Build Coastguard Worker tprintf("pwritev(1, [{iov_base=\"%s\", iov_len=%u}, ... /* %p */], 2, 0)"
103*cf84ac9aSAndroid Build Coastguard Worker " = %ld %s (%m)\n",
104*cf84ac9aSAndroid Build Coastguard Worker w2_c, LENGTH_OF(w2_c), w_iov + ARRAY_SIZE(w_iov_),
105*cf84ac9aSAndroid Build Coastguard Worker rc, errno2name());
106*cf84ac9aSAndroid Build Coastguard Worker
107*cf84ac9aSAndroid Build Coastguard Worker const unsigned int w_len =
108*cf84ac9aSAndroid Build Coastguard Worker LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
109*cf84ac9aSAndroid Build Coastguard Worker
110*cf84ac9aSAndroid Build Coastguard Worker rc = pwritev(1, w_iov, ARRAY_SIZE(w_iov_), 0);
111*cf84ac9aSAndroid Build Coastguard Worker if (rc != (int) w_len)
112*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("pwritev: expected %u, returned %ld",
113*cf84ac9aSAndroid Build Coastguard Worker w_len, rc);
114*cf84ac9aSAndroid Build Coastguard Worker close(1);
115*cf84ac9aSAndroid Build Coastguard Worker tprintf("pwritev(1, [{iov_base=\"%s\", iov_len=%u}"
116*cf84ac9aSAndroid Build Coastguard Worker ", {iov_base=\"%s\", iov_len=%u}"
117*cf84ac9aSAndroid Build Coastguard Worker ", {iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n"
118*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 0\n"
119*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n"
120*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 1\n"
121*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n"
122*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 2\n"
123*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n",
124*cf84ac9aSAndroid Build Coastguard Worker w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c),
125*cf84ac9aSAndroid Build Coastguard Worker w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w_iov_), w_len,
126*cf84ac9aSAndroid Build Coastguard Worker LENGTH_OF(w0_c), w0_d, w0_c,
127*cf84ac9aSAndroid Build Coastguard Worker LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c);
128*cf84ac9aSAndroid Build Coastguard Worker
129*cf84ac9aSAndroid Build Coastguard Worker const unsigned int r_len = (w_len + 1) / 2;
130*cf84ac9aSAndroid Build Coastguard Worker void *r0 = tail_alloc(r_len);
131*cf84ac9aSAndroid Build Coastguard Worker const struct iovec r0_iov_[] = {
132*cf84ac9aSAndroid Build Coastguard Worker {
133*cf84ac9aSAndroid Build Coastguard Worker .iov_base = r0,
134*cf84ac9aSAndroid Build Coastguard Worker .iov_len = r_len
135*cf84ac9aSAndroid Build Coastguard Worker }
136*cf84ac9aSAndroid Build Coastguard Worker };
137*cf84ac9aSAndroid Build Coastguard Worker const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
138*cf84ac9aSAndroid Build Coastguard Worker
139*cf84ac9aSAndroid Build Coastguard Worker rc = preadv(0, r_iov, ARRAY_SIZE(r0_iov_), 0);
140*cf84ac9aSAndroid Build Coastguard Worker if (rc != (int) r_len)
141*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("preadv: expected %u, returned %ld",
142*cf84ac9aSAndroid Build Coastguard Worker r_len, rc);
143*cf84ac9aSAndroid Build Coastguard Worker tprintf("preadv(0, [{iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n"
144*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 0\n"
145*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n",
146*cf84ac9aSAndroid Build Coastguard Worker r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_),
147*cf84ac9aSAndroid Build Coastguard Worker r_len, r_len, r0_d, r0_c);
148*cf84ac9aSAndroid Build Coastguard Worker
149*cf84ac9aSAndroid Build Coastguard Worker void *r1 = tail_alloc(r_len);
150*cf84ac9aSAndroid Build Coastguard Worker void *r2 = tail_alloc(w_len);
151*cf84ac9aSAndroid Build Coastguard Worker const struct iovec r1_iov_[] = {
152*cf84ac9aSAndroid Build Coastguard Worker {
153*cf84ac9aSAndroid Build Coastguard Worker .iov_base = r1,
154*cf84ac9aSAndroid Build Coastguard Worker .iov_len = r_len
155*cf84ac9aSAndroid Build Coastguard Worker },
156*cf84ac9aSAndroid Build Coastguard Worker {
157*cf84ac9aSAndroid Build Coastguard Worker .iov_base = r2,
158*cf84ac9aSAndroid Build Coastguard Worker .iov_len = w_len
159*cf84ac9aSAndroid Build Coastguard Worker }
160*cf84ac9aSAndroid Build Coastguard Worker };
161*cf84ac9aSAndroid Build Coastguard Worker r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
162*cf84ac9aSAndroid Build Coastguard Worker
163*cf84ac9aSAndroid Build Coastguard Worker rc = preadv(0, r_iov, ARRAY_SIZE(r1_iov_), r_len);
164*cf84ac9aSAndroid Build Coastguard Worker if (rc != (int) w_len - (int) r_len)
165*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("preadv: expected %d, returned %ld",
166*cf84ac9aSAndroid Build Coastguard Worker (int) w_len - r_len, rc);
167*cf84ac9aSAndroid Build Coastguard Worker tprintf("preadv(0, [{iov_base=\"%s\", iov_len=%u}"
168*cf84ac9aSAndroid Build Coastguard Worker ", {iov_base=\"\", iov_len=%u}], %u, %u) = %u\n"
169*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 0\n"
170*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n",
171*cf84ac9aSAndroid Build Coastguard Worker r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_),
172*cf84ac9aSAndroid Build Coastguard Worker r_len, w_len - r_len,
173*cf84ac9aSAndroid Build Coastguard Worker w_len - r_len, r1_d, r1_c);
174*cf84ac9aSAndroid Build Coastguard Worker close(0);
175*cf84ac9aSAndroid Build Coastguard Worker
176*cf84ac9aSAndroid Build Coastguard Worker tprintf("+++ exited with 0 +++\n");
177*cf84ac9aSAndroid Build Coastguard Worker return 0;
178*cf84ac9aSAndroid Build Coastguard Worker }
179*cf84ac9aSAndroid Build Coastguard Worker
180*cf84ac9aSAndroid Build Coastguard Worker #else
181*cf84ac9aSAndroid Build Coastguard Worker
182*cf84ac9aSAndroid Build Coastguard Worker SKIP_MAIN_UNDEFINED("HAVE_PREADV && HAVE_PWRITEV")
183*cf84ac9aSAndroid Build Coastguard Worker
184*cf84ac9aSAndroid Build Coastguard Worker #endif
185