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