2 * Check decoding of recvmsg and sendmsg syscalls.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/socket.h>
44 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
45 perror_msg_and_skip("socketpair");
49 static const char w0_c[] = "012";
50 const char *w0_d = hexdump_strdup(w0_c);
51 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
53 static const char w1_c[] = "34567";
54 const char *w1_d = hexdump_strdup(w1_c);
55 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
57 static const char w2_c[] = "89abcde";
58 const char *w2_d = hexdump_strdup(w2_c);
59 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
61 static const char r0_c[] = "01234567";
62 const char *r0_d = hexdump_strdup(r0_c);
63 static const char r1_c[] = "89abcde";
64 const char *r1_d = hexdump_strdup(r1_c);
66 const struct iovec w_iov_[] = {
69 .iov_len = LENGTH_OF(w0_c)
72 .iov_len = LENGTH_OF(w1_c)
75 .iov_len = LENGTH_OF(w2_c)
78 struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
79 const unsigned int w_len =
80 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
82 const struct msghdr w_mh_ = {
84 .msg_iovlen = ARRAY_SIZE(w_iov_)
86 const struct msghdr *w_mh = tail_memdup(&w_mh_, sizeof(w_mh_));
88 assert(sendmsg(1, w_mh, 0) == (int) w_len);
90 tprintf("sendmsg(1, {msg_name=NULL, msg_namelen=0, msg_iov="
91 "[{iov_base=\"%s\", iov_len=%u}, {iov_base=\"%s\", iov_len=%u}"
92 ", {iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
93 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
94 " * %u bytes in buffer 0\n"
95 " | 00000 %-49s %-16s |\n"
96 " * %u bytes in buffer 1\n"
97 " | 00000 %-49s %-16s |\n"
98 " * %u bytes in buffer 2\n"
99 " | 00000 %-49s %-16s |\n",
100 w0_c, LENGTH_OF(w0_c),
101 w1_c, LENGTH_OF(w1_c),
102 w2_c, LENGTH_OF(w2_c),
103 ARRAY_SIZE(w_iov_), w_len,
104 LENGTH_OF(w0_c), w0_d, w0_c,
105 LENGTH_OF(w1_c), w1_d, w1_c,
106 LENGTH_OF(w2_c), w2_d, w2_c);
108 const unsigned int r_len = (w_len + 1) / 2;
109 void *r0 = tail_alloc(r_len);
110 const struct iovec r0_iov_[] = {
116 struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
118 const struct msghdr r_mh_ = {
120 .msg_iovlen = ARRAY_SIZE(r0_iov_)
122 struct msghdr *r_mh = tail_memdup(&r_mh_, sizeof(r_mh_));
124 assert(recvmsg(0, r_mh, 0) == (int) r_len);
125 tprintf("recvmsg(0, {msg_name=NULL, msg_namelen=0, msg_iov="
126 "[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
127 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
128 " * %u bytes in buffer 0\n"
129 " | 00000 %-49s %-16s |\n",
130 r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len, r_len, r0_d, r0_c);
132 void *r1 = tail_alloc(r_len);
133 void *r2 = tail_alloc(w_len);
134 const struct iovec r1_iov_[] = {
144 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
145 r_mh->msg_iov = r_iov;
146 r_mh->msg_iovlen = ARRAY_SIZE(r1_iov_);
148 assert(recvmsg(0, r_mh, 0) == (int) w_len - (int) r_len);
149 tprintf("recvmsg(0, {msg_name=NULL, msg_namelen=0, msg_iov="
150 "[{iov_base=\"%s\", iov_len=%u}, {iov_base=\"\", iov_len=%u}]"
151 ", msg_iovlen=%u, msg_controllen=0, msg_flags=0}, 0) = %u\n"
152 " * %u bytes in buffer 0\n"
153 " | 00000 %-49s %-16s |\n",
154 r1_c, r_len, w_len, ARRAY_SIZE(r1_iov_), w_len - r_len,
155 w_len - r_len, r1_d, r1_c);
158 tprintf("+++ exited with 0 +++\n");