1 /*
2 * Check decoding of readv and writev syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2016-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12
13 #include <assert.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <sys/uio.h>
17
18 int
19 main(void)
20 {
21 tprintf("%s", "");
22
23 int fds[2];
24 pipe_maxfd(fds);
25
26 static const char w0_c[] = "012";
27 const char *w0_d = hexdump_strdup(w0_c);
28 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
29
30 const void *efault = w0 + LENGTH_OF(w0_c);
31
32 static const char w1_c[] = "34567";
33 const char *w1_d = hexdump_strdup(w1_c);
34 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
35
36 static const char w2_c[] = "89abcde";
37 const char *w2_d = hexdump_strdup(w2_c);
38 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
39 long rc;
40
41 rc = writev(fds[1], efault, 42);
42 tprintf("writev(%d, %p, 42) = %ld %s (%m)\n",
43 fds[1], efault, rc, errno2name());
44
45 rc = readv(fds[0], efault, 42);
46 tprintf("readv(%d, %p, 42) = %ld %s (%m)\n",
47 fds[0], efault, rc, errno2name());
48
49 static const char r0_c[] = "01234567";
50 const char *r0_d = hexdump_strdup(r0_c);
51 static const char r1_c[] = "89abcde";
52 const char *r1_d = hexdump_strdup(r1_c);
53
54 const struct iovec w_iov_[] = {
55 {
56 .iov_base = w0,
57 .iov_len = LENGTH_OF(w0_c)
58 }, {
59 .iov_base = w1,
60 .iov_len = LENGTH_OF(w1_c)
61 }, {
62 .iov_base = w2,
63 .iov_len = LENGTH_OF(w2_c)
64 }
65 };
66 const struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
67
68 tprintf("writev(%d, [], 0) = %ld\n",
69 fds[1], (long) writev(fds[1], w_iov, 0));
70
71 rc = writev(fds[1], w_iov + ARRAY_SIZE(w_iov_) - 1, 2);
72 tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%u}, ... /* %p */], 2)"
73 " = %ld %s (%m)\n",
74 fds[1], w2_c, LENGTH_OF(w2_c), w_iov + ARRAY_SIZE(w_iov_),
75 rc, errno2name());
76
77 const unsigned int w_len =
78 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
79
80 assert(writev(fds[1], w_iov, ARRAY_SIZE(w_iov_)) == (int) w_len);
81 close(fds[1]);
82 tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%u}"
83 ", {iov_base=\"%s\", iov_len=%u}"
84 ", {iov_base=\"%s\", iov_len=%u}], %u) = %u\n"
85 " * %u bytes in buffer 0\n"
86 " | 00000 %-49s %-16s |\n"
87 " * %u bytes in buffer 1\n"
88 " | 00000 %-49s %-16s |\n"
89 " * %u bytes in buffer 2\n"
90 " | 00000 %-49s %-16s |\n",
91 fds[1], w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c),
92 w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w_iov_), w_len,
93 LENGTH_OF(w0_c), w0_d, w0_c,
94 LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c);
95
96 const unsigned int r_len = (w_len + 1) / 2;
97 void *r0 = tail_alloc(r_len);
98 const struct iovec r0_iov_[] = {
99 {
100 .iov_base = r0,
101 .iov_len = r_len
102 }
103 };
104 const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
105
106 assert(readv(fds[0], r_iov, ARRAY_SIZE(r0_iov_)) == (int) r_len);
107 tprintf("readv(%d, [{iov_base=\"%s\", iov_len=%u}], %u) = %u\n"
108 " * %u bytes in buffer 0\n"
109 " | 00000 %-49s %-16s |\n",
110 fds[0], r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_),
111 r_len, r_len, r0_d, r0_c);
112
113 void *r1 = tail_alloc(r_len);
114 void *r2 = tail_alloc(w_len);
115 const struct iovec r1_iov_[] = {
116 {
117 .iov_base = r1,
118 .iov_len = r_len
119 },
120 {
121 .iov_base = r2,
122 .iov_len = w_len
123 }
124 };
125 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
126
127 assert(readv(fds[0], r_iov, ARRAY_SIZE(r1_iov_)) == (int) w_len - (int) r_len);
128 tprintf("readv(%d, [{iov_base=\"%s\", iov_len=%u}"
129 ", {iov_base=\"\", iov_len=%u}], %u) = %u\n"
130 " * %u bytes in buffer 0\n"
131 " | 00000 %-49s %-16s |\n",
132 fds[0], r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_),
133 w_len - r_len, w_len - r_len, r1_d, r1_c);
134 close(fds[0]);
135
136 tprintf("+++ exited with 0 +++\n");
137 return 0;
138 }