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