1 /*
2 * Check decoding of recvmsg and sendmsg syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 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/socket.h>
37 #include <sys/uio.h>
38
39 int
main(void)40 main(void)
41 {
42 tprintf("%s", "");
43
44 int fds[2];
45 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
46 perror_msg_and_skip("socketpair");
47 assert(0 == fds[0]);
48 assert(1 == fds[1]);
49
50 static const char w0_c[] = "012";
51 const char *w0_d = hexdump_strdup(w0_c);
52 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
53
54 static const char w1_c[] = "34567";
55 const char *w1_d = hexdump_strdup(w1_c);
56 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
57
58 static const char w2_c[] = "89abcde";
59 const char *w2_d = hexdump_strdup(w2_c);
60 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
61
62 static const char r0_c[] = "01234567";
63 const char *r0_d = hexdump_strdup(r0_c);
64 static const char r1_c[] = "89abcde";
65 const char *r1_d = hexdump_strdup(r1_c);
66
67 const struct iovec w_iov_[] = {
68 {
69 .iov_base = w0,
70 .iov_len = LENGTH_OF(w0_c)
71 }, {
72 .iov_base = w1,
73 .iov_len = LENGTH_OF(w1_c)
74 }, {
75 .iov_base = w2,
76 .iov_len = LENGTH_OF(w2_c)
77 }
78 };
79 struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
80 const unsigned int w_len =
81 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
82
83 const struct msghdr w_mh_ = {
84 .msg_iov = w_iov,
85 .msg_iovlen = ARRAY_SIZE(w_iov_)
86 };
87 const struct msghdr *w_mh = tail_memdup(&w_mh_, sizeof(w_mh_));
88
89 assert(sendmsg(1, w_mh, 0) == (int) w_len);
90 close(1);
91 tprintf("sendmsg(1, {msg_name=NULL, msg_namelen=0, msg_iov="
92 "[{iov_base=\"%s\", iov_len=%u}, {iov_base=\"%s\", iov_len=%u}"
93 ", {iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
94 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
95 " * %u bytes in buffer 0\n"
96 " | 00000 %-49s %-16s |\n"
97 " * %u bytes in buffer 1\n"
98 " | 00000 %-49s %-16s |\n"
99 " * %u bytes in buffer 2\n"
100 " | 00000 %-49s %-16s |\n",
101 w0_c, LENGTH_OF(w0_c),
102 w1_c, LENGTH_OF(w1_c),
103 w2_c, LENGTH_OF(w2_c),
104 (unsigned int) ARRAY_SIZE(w_iov_), w_len,
105 LENGTH_OF(w0_c), w0_d, w0_c,
106 LENGTH_OF(w1_c), w1_d, w1_c,
107 LENGTH_OF(w2_c), w2_d, w2_c);
108
109 const unsigned int r_len = (w_len + 1) / 2;
110 void *r0 = tail_alloc(r_len);
111 const struct iovec r0_iov_[] = {
112 {
113 .iov_base = r0,
114 .iov_len = r_len
115 }
116 };
117 struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
118
119 const struct msghdr r_mh_ = {
120 .msg_iov = r_iov,
121 .msg_iovlen = ARRAY_SIZE(r0_iov_)
122 };
123 struct msghdr *r_mh = tail_memdup(&r_mh_, sizeof(r_mh_));
124
125 assert(recvmsg(0, r_mh, 0) == (int) r_len);
126 tprintf("recvmsg(0, {msg_name=NULL, msg_namelen=0, msg_iov="
127 "[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
128 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
129 " * %u bytes in buffer 0\n"
130 " | 00000 %-49s %-16s |\n",
131 r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_),
132 r_len, r_len, r0_d, r0_c);
133
134 void *r1 = tail_alloc(r_len);
135 void *r2 = tail_alloc(w_len);
136 const struct iovec r1_iov_[] = {
137 {
138 .iov_base = r1,
139 .iov_len = r_len
140 },
141 {
142 .iov_base = r2,
143 .iov_len = w_len
144 }
145 };
146 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
147 r_mh->msg_iov = r_iov;
148 r_mh->msg_iovlen = ARRAY_SIZE(r1_iov_);
149
150 assert(recvmsg(0, r_mh, 0) == (int) w_len - (int) r_len);
151 tprintf("recvmsg(0, {msg_name=NULL, msg_namelen=0, msg_iov="
152 "[{iov_base=\"%s\", iov_len=%u}, {iov_base=\"\", iov_len=%u}]"
153 ", msg_iovlen=%u, msg_controllen=0, msg_flags=0}, 0) = %u\n"
154 " * %u bytes in buffer 0\n"
155 " | 00000 %-49s %-16s |\n",
156 r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_),
157 w_len - r_len, w_len - r_len, r1_d, r1_c);
158 close(0);
159
160 tprintf("+++ exited with 0 +++\n");
161 return 0;
162 }
163