1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Check decoding of readv and writev 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 #include <assert.h>
34*cf84ac9aSAndroid Build Coastguard Worker #include <stdio.h>
35*cf84ac9aSAndroid Build Coastguard Worker #include <unistd.h>
36*cf84ac9aSAndroid Build Coastguard Worker #include <sys/uio.h>
37*cf84ac9aSAndroid Build Coastguard Worker
38*cf84ac9aSAndroid Build Coastguard Worker int
main(void)39*cf84ac9aSAndroid Build Coastguard Worker main(void)
40*cf84ac9aSAndroid Build Coastguard Worker {
41*cf84ac9aSAndroid Build Coastguard Worker tprintf("%s", "");
42*cf84ac9aSAndroid Build Coastguard Worker
43*cf84ac9aSAndroid Build Coastguard Worker int fds[2];
44*cf84ac9aSAndroid Build Coastguard Worker pipe_maxfd(fds);
45*cf84ac9aSAndroid Build Coastguard Worker
46*cf84ac9aSAndroid Build Coastguard Worker static const char w0_c[] = "012";
47*cf84ac9aSAndroid Build Coastguard Worker const char *w0_d = hexdump_strdup(w0_c);
48*cf84ac9aSAndroid Build Coastguard Worker void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
49*cf84ac9aSAndroid Build Coastguard Worker
50*cf84ac9aSAndroid Build Coastguard Worker const void *efault = w0 + LENGTH_OF(w0_c);
51*cf84ac9aSAndroid Build Coastguard Worker
52*cf84ac9aSAndroid Build Coastguard Worker static const char w1_c[] = "34567";
53*cf84ac9aSAndroid Build Coastguard Worker const char *w1_d = hexdump_strdup(w1_c);
54*cf84ac9aSAndroid Build Coastguard Worker void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
55*cf84ac9aSAndroid Build Coastguard Worker
56*cf84ac9aSAndroid Build Coastguard Worker static const char w2_c[] = "89abcde";
57*cf84ac9aSAndroid Build Coastguard Worker const char *w2_d = hexdump_strdup(w2_c);
58*cf84ac9aSAndroid Build Coastguard Worker void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
59*cf84ac9aSAndroid Build Coastguard Worker long rc;
60*cf84ac9aSAndroid Build Coastguard Worker
61*cf84ac9aSAndroid Build Coastguard Worker rc = writev(fds[1], efault, 42);
62*cf84ac9aSAndroid Build Coastguard Worker tprintf("writev(%d, %p, 42) = %ld %s (%m)\n",
63*cf84ac9aSAndroid Build Coastguard Worker fds[1], efault, rc, errno2name());
64*cf84ac9aSAndroid Build Coastguard Worker
65*cf84ac9aSAndroid Build Coastguard Worker rc = readv(fds[0], efault, 42);
66*cf84ac9aSAndroid Build Coastguard Worker tprintf("readv(%d, %p, 42) = %ld %s (%m)\n",
67*cf84ac9aSAndroid Build Coastguard Worker fds[0], efault, rc, errno2name());
68*cf84ac9aSAndroid Build Coastguard Worker
69*cf84ac9aSAndroid Build Coastguard Worker static const char r0_c[] = "01234567";
70*cf84ac9aSAndroid Build Coastguard Worker const char *r0_d = hexdump_strdup(r0_c);
71*cf84ac9aSAndroid Build Coastguard Worker static const char r1_c[] = "89abcde";
72*cf84ac9aSAndroid Build Coastguard Worker const char *r1_d = hexdump_strdup(r1_c);
73*cf84ac9aSAndroid Build Coastguard Worker
74*cf84ac9aSAndroid Build Coastguard Worker const struct iovec w_iov_[] = {
75*cf84ac9aSAndroid Build Coastguard Worker {
76*cf84ac9aSAndroid Build Coastguard Worker .iov_base = w0,
77*cf84ac9aSAndroid Build Coastguard Worker .iov_len = LENGTH_OF(w0_c)
78*cf84ac9aSAndroid Build Coastguard Worker }, {
79*cf84ac9aSAndroid Build Coastguard Worker .iov_base = w1,
80*cf84ac9aSAndroid Build Coastguard Worker .iov_len = LENGTH_OF(w1_c)
81*cf84ac9aSAndroid Build Coastguard Worker }, {
82*cf84ac9aSAndroid Build Coastguard Worker .iov_base = w2,
83*cf84ac9aSAndroid Build Coastguard Worker .iov_len = LENGTH_OF(w2_c)
84*cf84ac9aSAndroid Build Coastguard Worker }
85*cf84ac9aSAndroid Build Coastguard Worker };
86*cf84ac9aSAndroid Build Coastguard Worker const struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
87*cf84ac9aSAndroid Build Coastguard Worker
88*cf84ac9aSAndroid Build Coastguard Worker tprintf("writev(%d, [], 0) = %ld\n",
89*cf84ac9aSAndroid Build Coastguard Worker fds[1], (long) writev(fds[1], w_iov, 0));
90*cf84ac9aSAndroid Build Coastguard Worker
91*cf84ac9aSAndroid Build Coastguard Worker rc = writev(fds[1], w_iov + ARRAY_SIZE(w_iov_) - 1, 2);
92*cf84ac9aSAndroid Build Coastguard Worker tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%u}, ... /* %p */], 2)"
93*cf84ac9aSAndroid Build Coastguard Worker " = %ld %s (%m)\n",
94*cf84ac9aSAndroid Build Coastguard Worker fds[1], w2_c, LENGTH_OF(w2_c), w_iov + ARRAY_SIZE(w_iov_),
95*cf84ac9aSAndroid Build Coastguard Worker rc, errno2name());
96*cf84ac9aSAndroid Build Coastguard Worker
97*cf84ac9aSAndroid Build Coastguard Worker const unsigned int w_len =
98*cf84ac9aSAndroid Build Coastguard Worker LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
99*cf84ac9aSAndroid Build Coastguard Worker
100*cf84ac9aSAndroid Build Coastguard Worker assert(writev(fds[1], w_iov, ARRAY_SIZE(w_iov_)) == (int) w_len);
101*cf84ac9aSAndroid Build Coastguard Worker close(fds[1]);
102*cf84ac9aSAndroid Build Coastguard Worker tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%u}"
103*cf84ac9aSAndroid Build Coastguard Worker ", {iov_base=\"%s\", iov_len=%u}"
104*cf84ac9aSAndroid Build Coastguard Worker ", {iov_base=\"%s\", iov_len=%u}], %u) = %u\n"
105*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 0\n"
106*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n"
107*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 1\n"
108*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n"
109*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 2\n"
110*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n",
111*cf84ac9aSAndroid Build Coastguard Worker fds[1], w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c),
112*cf84ac9aSAndroid Build Coastguard Worker w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w_iov_), w_len,
113*cf84ac9aSAndroid Build Coastguard Worker LENGTH_OF(w0_c), w0_d, w0_c,
114*cf84ac9aSAndroid Build Coastguard Worker LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c);
115*cf84ac9aSAndroid Build Coastguard Worker
116*cf84ac9aSAndroid Build Coastguard Worker const unsigned int r_len = (w_len + 1) / 2;
117*cf84ac9aSAndroid Build Coastguard Worker void *r0 = tail_alloc(r_len);
118*cf84ac9aSAndroid Build Coastguard Worker const struct iovec r0_iov_[] = {
119*cf84ac9aSAndroid Build Coastguard Worker {
120*cf84ac9aSAndroid Build Coastguard Worker .iov_base = r0,
121*cf84ac9aSAndroid Build Coastguard Worker .iov_len = r_len
122*cf84ac9aSAndroid Build Coastguard Worker }
123*cf84ac9aSAndroid Build Coastguard Worker };
124*cf84ac9aSAndroid Build Coastguard Worker const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
125*cf84ac9aSAndroid Build Coastguard Worker
126*cf84ac9aSAndroid Build Coastguard Worker assert(readv(fds[0], r_iov, ARRAY_SIZE(r0_iov_)) == (int) r_len);
127*cf84ac9aSAndroid Build Coastguard Worker tprintf("readv(%d, [{iov_base=\"%s\", iov_len=%u}], %u) = %u\n"
128*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 0\n"
129*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n",
130*cf84ac9aSAndroid Build Coastguard Worker fds[0], r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_),
131*cf84ac9aSAndroid Build Coastguard Worker r_len, r_len, r0_d, r0_c);
132*cf84ac9aSAndroid Build Coastguard Worker
133*cf84ac9aSAndroid Build Coastguard Worker void *r1 = tail_alloc(r_len);
134*cf84ac9aSAndroid Build Coastguard Worker void *r2 = tail_alloc(w_len);
135*cf84ac9aSAndroid Build Coastguard Worker const struct iovec r1_iov_[] = {
136*cf84ac9aSAndroid Build Coastguard Worker {
137*cf84ac9aSAndroid Build Coastguard Worker .iov_base = r1,
138*cf84ac9aSAndroid Build Coastguard Worker .iov_len = r_len
139*cf84ac9aSAndroid Build Coastguard Worker },
140*cf84ac9aSAndroid Build Coastguard Worker {
141*cf84ac9aSAndroid Build Coastguard Worker .iov_base = r2,
142*cf84ac9aSAndroid Build Coastguard Worker .iov_len = w_len
143*cf84ac9aSAndroid Build Coastguard Worker }
144*cf84ac9aSAndroid Build Coastguard Worker };
145*cf84ac9aSAndroid Build Coastguard Worker r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
146*cf84ac9aSAndroid Build Coastguard Worker
147*cf84ac9aSAndroid Build Coastguard Worker assert(readv(fds[0], r_iov, ARRAY_SIZE(r1_iov_)) == (int) w_len - (int) r_len);
148*cf84ac9aSAndroid Build Coastguard Worker tprintf("readv(%d, [{iov_base=\"%s\", iov_len=%u}"
149*cf84ac9aSAndroid Build Coastguard Worker ", {iov_base=\"\", iov_len=%u}], %u) = %u\n"
150*cf84ac9aSAndroid Build Coastguard Worker " * %u bytes in buffer 0\n"
151*cf84ac9aSAndroid Build Coastguard Worker " | 00000 %-49s %-16s |\n",
152*cf84ac9aSAndroid Build Coastguard Worker fds[0], r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_),
153*cf84ac9aSAndroid Build Coastguard Worker w_len - r_len, w_len - r_len, r1_d, r1_c);
154*cf84ac9aSAndroid Build Coastguard Worker close(fds[0]);
155*cf84ac9aSAndroid Build Coastguard Worker
156*cf84ac9aSAndroid Build Coastguard Worker tprintf("+++ exited with 0 +++\n");
157*cf84ac9aSAndroid Build Coastguard Worker return 0;
158*cf84ac9aSAndroid Build Coastguard Worker }
159