t_fdrestart.c revision 1.3 1 /* $NetBSD: t_fdrestart.c,v 1.3 2023/10/15 15:18:17 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #define _KMEMUSER /* ERESTART */
30
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_fdrestart.c,v 1.3 2023/10/15 15:18:17 riastradh Exp $");
33
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37
38 #include <atf-c.h>
39 #include <errno.h>
40 #include <pthread.h>
41 #include <unistd.h>
42
43 #include <rump/rump.h>
44 #include <rump/rump_syscalls.h>
45
46 #include "h_macros.h"
47
48 struct fdrestart {
49 void (*op)(struct fdrestart *);
50 int fd;
51 pthread_barrier_t barrier;
52 };
53
54 static void
55 waitforbarrier(struct fdrestart *F, const char *caller)
56 {
57 int error;
58
59 error = pthread_barrier_wait(&F->barrier);
60 switch (error) {
61 case 0:
62 case PTHREAD_BARRIER_SERIAL_THREAD:
63 break;
64 default:
65 atf_tc_fail("%s: pthread_barrier_wait: %d, %s", caller, error,
66 strerror(error));
67 }
68 }
69
70 static void
71 doread(struct fdrestart *F)
72 {
73 char c;
74 ssize_t nread;
75 int error;
76
77 /*
78 * Wait for the other thread to be ready.
79 */
80 waitforbarrier(F, "reader");
81
82 /*
83 * Start a read. This should block, and then, when the other
84 * thread closes the fd, should be woken to fail with ERESTART.
85 */
86 nread = rump_sys_read(F->fd, &c, sizeof(c));
87 ATF_REQUIRE_EQ_MSG(nread, -1, "nread=%zd", nread);
88 error = errno;
89 ATF_REQUIRE_EQ_MSG(error, ERESTART, "errno=%d (%s)", error,
90 strerror(error));
91
92 /*
93 * Now further attempts at I/O should fail with EBADF because
94 * the fd has been closed.
95 */
96 nread = rump_sys_read(F->fd, &c, sizeof(c));
97 ATF_REQUIRE_EQ_MSG(nread, -1, "nread=%zd", nread);
98 error = errno;
99 ATF_REQUIRE_EQ_MSG(error, EBADF, "errno=%d (%s)", error,
100 strerror(error));
101 }
102
103 static void
104 dowrite(struct fdrestart *F)
105 {
106 static const char buf[1024*1024]; /* XXX >BIG_PIPE_SIZE */
107 ssize_t nwrit;
108 int error;
109
110 /*
111 * Make sure the pipe's buffer is full first.
112 */
113 for (;;) {
114 int nspace;
115
116 RL(rump_sys_ioctl(F->fd, FIONSPACE, &nspace));
117 ATF_REQUIRE_MSG(nspace >= 0, "nspace=%d", nspace);
118 if (nspace == 0)
119 break;
120 RL(rump_sys_write(F->fd, buf, (size_t)nspace));
121 }
122
123 /*
124 * Wait for the other thread to be ready.
125 */
126 waitforbarrier(F, "writer");
127
128 /*
129 * Start a write. This should block, and then, when the other
130 * thread closes the fd, should be woken to fail with ERESTART.
131 */
132 nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
133 ATF_REQUIRE_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
134 error = errno;
135 ATF_REQUIRE_EQ_MSG(error, ERESTART, "errno=%d (%s)", error,
136 strerror(error));
137
138 /*
139 * Now further attempts at I/O should fail with EBADF because
140 * the fd has been closed.
141 */
142 nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
143 ATF_REQUIRE_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
144 error = errno;
145 ATF_REQUIRE_EQ_MSG(error, EBADF, "errno=%d (%s)", error,
146 strerror(error));
147 }
148
149 static void *
150 doit(void *cookie)
151 {
152 struct fdrestart *F = cookie;
153
154 (*F->op)(F);
155
156 return NULL;
157 }
158
159 static void
160 on_sigalrm(int signo)
161 {
162
163 atf_tc_fail("timed out");
164 }
165
166 static void
167 testfdrestart(struct fdrestart *F)
168 {
169 pthread_t t;
170
171 ATF_REQUIRE_MSG(signal(SIGALRM, &on_sigalrm) != SIG_ERR,
172 "errno=%d (%s)", errno, strerror(errno));
173
174 RZ(pthread_barrier_init(&F->barrier, NULL, 2));
175 RZ(pthread_create(&t, NULL, &doit, F));
176 waitforbarrier(F, "closer"); /* wait for thread to start */
177 (void)sleep(1); /* wait for op to start */
178 (void)alarm(1); /* set a deadline */
179 RL(rump_sys_close(F->fd)); /* wake op in other thread */
180 RZ(pthread_join(t, NULL)); /* wait for op to wake and fail */
181 (void)alarm(0); /* clear the deadline */
182 }
183
184 ATF_TC(pipe_read);
185 ATF_TC_HEAD(pipe_read, tc)
186 {
187 atf_tc_set_md_var(tc, "descr", "Test pipe read fails on close");
188 }
189 ATF_TC_BODY(pipe_read, tc)
190 {
191 struct fdrestart fdrestart, *F = &fdrestart;
192 int fd[2];
193
194 rump_init();
195
196 RL(rump_sys_pipe(fd));
197
198 memset(F, 0, sizeof(*F));
199 F->op = &doread;
200 F->fd = fd[0];
201 atf_tc_expect_fail("PR kern/57659");
202 testfdrestart(F);
203 }
204
205 ATF_TC(pipe_write);
206 ATF_TC_HEAD(pipe_write, tc)
207 {
208 atf_tc_set_md_var(tc, "descr", "Test pipe write fails on close");
209 }
210 ATF_TC_BODY(pipe_write, tc)
211 {
212 struct fdrestart fdrestart, *F = &fdrestart;
213 int fd[2];
214
215 rump_init();
216
217 RL(rump_sys_pipe(fd));
218
219 memset(F, 0, sizeof(*F));
220 F->op = &dowrite;
221 F->fd = fd[1];
222 atf_tc_expect_fail("PR kern/57659");
223 testfdrestart(F);
224 }
225
226 ATF_TC(socketpair_read);
227 ATF_TC_HEAD(socketpair_read, tc)
228 {
229 atf_tc_set_md_var(tc, "descr", "Test socketpair read fails on close");
230 }
231 ATF_TC_BODY(socketpair_read, tc)
232 {
233 struct fdrestart fdrestart, *F = &fdrestart;
234 int fd[2];
235
236 rump_init();
237
238 RL(rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, fd));
239
240 memset(F, 0, sizeof(*F));
241 F->op = &doread;
242 F->fd = fd[0];
243 atf_tc_expect_fail("PR kern/57659");
244 testfdrestart(F);
245 }
246
247 ATF_TC(socketpair_write);
248 ATF_TC_HEAD(socketpair_write, tc)
249 {
250 atf_tc_set_md_var(tc, "descr", "Test socketpair write fails on close");
251 }
252 ATF_TC_BODY(socketpair_write, tc)
253 {
254 struct fdrestart fdrestart, *F = &fdrestart;
255 int fd[2];
256
257 rump_init();
258
259 RL(rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, fd));
260
261 memset(F, 0, sizeof(*F));
262 F->op = &dowrite;
263 F->fd = fd[0];
264 atf_tc_expect_fail("PR kern/57659");
265 testfdrestart(F);
266 }
267
268 ATF_TP_ADD_TCS(tp)
269 {
270
271 ATF_TP_ADD_TC(tp, pipe_read);
272 ATF_TP_ADD_TC(tp, pipe_write);
273 ATF_TP_ADD_TC(tp, socketpair_read);
274 ATF_TP_ADD_TC(tp, socketpair_write);
275
276 return atf_no_error();
277 }
278