]> granicus.if.org Git - strace/blob - tests/aio.c
Update NEWS
[strace] / tests / aio.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "tests.h"
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <asm/unistd.h>
35
36 #if defined __NR_io_setup \
37  && defined __NR_io_submit \
38  && defined __NR_io_getevents \
39  && defined __NR_io_cancel \
40  && defined __NR_io_destroy
41 # include <linux/aio_abi.h>
42
43 int
44 main(void)
45 {
46         static const long bogus_ctx =
47                 (long) 0xface1e55deadbeefLL;
48
49         static const char data2[] =
50                 "\0\1\2\3cat test test test 0123456789abcdef";
51
52         const unsigned int sizeof_data0 = 4096;
53         const unsigned int sizeof_data1 = 8192;
54         void *data0 = tail_alloc(sizeof_data0);
55         void *data1 = tail_alloc(sizeof_data1);
56
57         const struct iocb proto_cb[] = {
58                 {
59                         .aio_data = (unsigned long) 0xfeedface11111111ULL,
60                         .aio_reqprio = 11,
61                         .aio_buf = (unsigned long) data0,
62                         .aio_offset = (unsigned long) 0xdeface1facefeedULL,
63                         .aio_nbytes = sizeof_data0
64                 },
65                 {
66                         .aio_data = (unsigned long) 0xfeedface22222222ULL,
67                         .aio_reqprio = 22,
68                         .aio_buf = (unsigned long) data1,
69                         .aio_offset = (unsigned long) 0xdeface2cafef00dULL,
70                         .aio_nbytes = sizeof_data1
71                 }
72         };
73         const struct iocb *cb = tail_memdup(proto_cb, sizeof(proto_cb));
74
75         const struct iovec proto_iov0[] = {
76                 {
77                         .iov_base = data0,
78                         .iov_len = sizeof_data0 / 4
79                 },
80                 {
81                         .iov_base = data0 + sizeof_data0 / 4,
82                         .iov_len = sizeof_data0 / 4 * 3
83                 },
84         };
85         const struct iovec *iov0 = tail_memdup(proto_iov0, sizeof(proto_iov0));
86
87         const struct iovec proto_iov1[] = {
88                 {
89                         .iov_base = data1,
90                         .iov_len = sizeof_data1 / 4
91                 },
92                 {
93                         .iov_base = data1 + sizeof_data1 / 4,
94                         .iov_len = sizeof_data1 / 4 * 3
95                 },
96         };
97         const struct iovec *iov1 = tail_memdup(proto_iov1, sizeof(proto_iov1));
98
99         const struct iocb proto_cbv[] = {
100                 {
101                         .aio_data = (unsigned long) 0xfeed11111111faceULL,
102                         .aio_lio_opcode = 7,
103                         .aio_reqprio = 111,
104                         .aio_buf = (unsigned long) iov0,
105                         .aio_offset = (unsigned long) 0xdeface1facefeedULL,
106                         .aio_nbytes = ARRAY_SIZE(proto_iov0)
107                 },
108                 {
109                         .aio_data = (unsigned long) 0xfeed22222222faceULL,
110                         .aio_lio_opcode = 7,
111                         .aio_reqprio = 222,
112                         .aio_buf = (unsigned long) iov1,
113                         .aio_offset = (unsigned long) 0xdeface2cafef00dULL,
114                         .aio_nbytes = ARRAY_SIZE(proto_iov1)
115                 }
116         };
117         const struct iocb *cbv = tail_memdup(proto_cbv, sizeof(proto_cbv));
118
119         /* For additional decoder testing */
120         const struct iocb proto_cbv2[] = {
121                 {
122                         .aio_data = 0xbadfacedc0ffeeedULL,
123                         .aio_key = 0xdefaced0,
124                         .aio_lio_opcode = 0xf00d,
125                         .aio_reqprio = 0,
126                         .aio_fildes = 0xdefaced1,
127                         .aio_buf = 0,
128                 },
129                 {
130                         .aio_data = 0,
131                         .aio_key = 0xdefaced0,
132                         .aio_lio_opcode = 1,
133                         .aio_reqprio = 0xbeef,
134                         .aio_fildes = 0xdefaced1,
135                         .aio_buf = 0,
136                         /* In order to make record valid */
137                         .aio_nbytes = (size_t) 0x1020304050607080ULL,
138                         .aio_offset = 0xdeadda7abadc0dedULL,
139 # ifdef IOCB_FLAG_RESFD
140                         .aio_flags = 0xfacef157,
141                         .aio_resfd = 0xded1ca7e,
142 # endif
143                 },
144                 {
145                         .aio_data = 0,
146                         .aio_key = 0xdefaced0,
147                         .aio_lio_opcode = 1,
148                         .aio_reqprio = 0xbeef,
149                         .aio_fildes = 0xdefaced1,
150                         .aio_buf = 0xbadc0ffeedefacedULL,
151                         .aio_nbytes = 0x8090a0b0c0d0e0f0ULL,
152                         .aio_offset = 0xdeadda7abadc0dedULL,
153                 },
154                 {
155                         .aio_data = 0,
156                         .aio_key = 0xdefaced0,
157                         .aio_lio_opcode = 1,
158                         .aio_reqprio = 0xbeef,
159                         .aio_fildes = 0xdefaced1,
160                         .aio_buf = (unsigned long)data2,
161                         .aio_nbytes = sizeof(data2),
162                         .aio_offset = 0xdeadda7abadc0dedULL,
163                 },
164                 {
165                         .aio_data = 0,
166                         .aio_key = 0xdefaced0,
167                         .aio_lio_opcode = 8,
168                         .aio_reqprio = 0xbeef,
169                         .aio_fildes = 0xdefaced1,
170                         .aio_buf = 0,
171                         .aio_nbytes = 0x8090a0b0c0d0e0f0ULL,
172                         .aio_offset = 0xdeadda7abadc0dedULL,
173                 },
174         };
175         const struct iocb *cbv2 = tail_memdup(proto_cbv2, sizeof(proto_cbv2));
176
177         const struct iocb proto_cbc = {
178                 .aio_data = (unsigned long) 0xdeadbeefbadc0dedULL,
179                 .aio_reqprio = 99,
180                 .aio_fildes = -42
181         };
182         const struct iocb *cbc = tail_memdup(&proto_cbc, sizeof(proto_cbc));
183
184         const long proto_cbs[] = {
185                 (long) &cb[0], (long) &cb[1]
186         };
187         const long *cbs = tail_memdup(proto_cbs, sizeof(proto_cbs));
188
189         const long proto_cbvs[] = {
190                 (long) &cbv[0], (long) &cbv[1],
191         };
192         const long *cbvs = tail_memdup(proto_cbvs, sizeof(proto_cbvs));
193
194         const long proto_cbvs2[] = {
195                 (long) &cbv2[0], (long) &cbv2[1], (long) &cbv2[2],
196                 (long) &cbv2[3], (long) &cbv2[4],
197                 (long) NULL, (long) 0xffffffffffffffffLL,
198         };
199         const long *cbvs2 = tail_memdup(proto_cbvs2, sizeof(proto_cbvs2));
200
201         unsigned long *ctx = tail_alloc(sizeof(unsigned long));
202         *ctx = 0;
203
204         const unsigned int nr = ARRAY_SIZE(proto_cb);
205         const unsigned long lnr = (unsigned long) (0xdeadbeef00000000ULL | nr);
206
207         const struct io_event *ev = tail_alloc(nr * sizeof(struct io_event));
208         const struct timespec proto_ts = { .tv_nsec = 123456789 };
209         const struct timespec *ts = tail_memdup(&proto_ts, sizeof(proto_ts));
210
211         (void) close(0);
212         if (open("/dev/zero", O_RDONLY))
213                 perror_msg_and_skip("open: %s", "/dev/zero");
214
215         long rc = syscall(__NR_io_setup, 0xdeadbeef, NULL);
216         printf("io_setup(%u, NULL) = %s\n", 0xdeadbeef, sprintrc(rc));
217
218         rc = syscall(__NR_io_setup, lnr, ctx + 1);
219         printf("io_setup(%u, %p) = %s\n", nr, ctx + 1, sprintrc(rc));
220
221         if (syscall(__NR_io_setup, lnr, ctx))
222                 perror_msg_and_skip("io_setup");
223         printf("io_setup(%u, [%#lx]) = 0\n", nr, *ctx);
224
225         rc = syscall(__NR_io_submit, bogus_ctx, (long) 0xca7faceddeadf00dLL,
226                      NULL);
227         printf("io_submit(%#lx, %ld, NULL) = %s\n",
228                bogus_ctx, (long) 0xca7faceddeadf00dLL, sprintrc(rc));
229
230         rc = syscall(__NR_io_submit, *ctx, nr, cbs + nr);
231         printf("io_submit(%#lx, %ld, %p) = %s\n",
232                *ctx, (long) nr, cbs + nr, sprintrc(rc));
233
234         rc = syscall(__NR_io_submit, *ctx, -1L, cbs);
235         printf("io_submit(%#lx, -1, %p) = %s\n",
236                *ctx, cbs, sprintrc(rc));
237
238         rc = syscall(__NR_io_submit, *ctx, nr, cbs);
239         if (rc != (long) nr)
240                 perror_msg_and_skip("io_submit");
241         printf("io_submit(%#lx, %u, ["
242                "{data=%#" PRI__x64 ", pread, reqprio=11, fildes=0, "
243                        "buf=%p, nbytes=%u, offset=%" PRI__d64 "}, "
244                "{data=%#" PRI__x64 ", pread, reqprio=22, fildes=0, "
245                        "buf=%p, nbytes=%u, offset=%" PRI__d64 "}"
246                "]) = %s\n",
247                *ctx, nr,
248                cb[0].aio_data, data0, sizeof_data0, cb[0].aio_offset,
249                cb[1].aio_data, data1, sizeof_data1, cb[1].aio_offset,
250                sprintrc(rc));
251
252         rc = syscall(__NR_io_getevents, bogus_ctx,
253                      (long) 0xca7faceddeadf00dLL, (long) 0xba5e1e505ca571e0LL,
254                      ev + 1, NULL);
255         printf("io_getevents(%#lx, %ld, %ld, %p, NULL) = %s\n",
256                bogus_ctx, (long) 0xca7faceddeadf00dLL,
257                (long) 0xba5e1e505ca571e0LL, ev + 1, sprintrc(rc));
258
259         rc = syscall(__NR_io_getevents, bogus_ctx,
260                      (long) 0xca7faceddeadf00dLL, (long) 0xba5e1e505ca571e0LL,
261                      NULL, ts + 1);
262         printf("io_getevents(%#lx, %ld, %ld, NULL, %p) = %s\n",
263                bogus_ctx, (long) 0xca7faceddeadf00dLL,
264                (long) 0xba5e1e505ca571e0LL, ts + 1, sprintrc(rc));
265
266         rc = syscall(__NR_io_getevents, *ctx, nr, nr + 1, ev, ts);
267         printf("io_getevents(%#lx, %ld, %ld, ["
268                "{data=%#" PRI__x64 ", obj=%p, res=%u, res2=0}, "
269                "{data=%#" PRI__x64 ", obj=%p, res=%u, res2=0}"
270                "], {tv_sec=0, tv_nsec=123456789}) = %s\n",
271                *ctx, (long) nr, (long) (nr + 1),
272                cb[0].aio_data, &cb[0], sizeof_data0,
273                cb[1].aio_data, &cb[1], sizeof_data1,
274                sprintrc(rc));
275
276         rc = syscall(__NR_io_cancel, bogus_ctx, NULL, NULL);
277         printf("io_cancel(%#lx, NULL, NULL) = %s\n", bogus_ctx, sprintrc(rc));
278
279         rc = syscall(__NR_io_cancel, *ctx, cbc + 1, ev);
280         printf("io_cancel(%#lx, %p, %p) = %s\n", *ctx, cbc + 1, ev,
281                sprintrc(rc));
282
283         rc = syscall(__NR_io_cancel, *ctx, cbc, ev);
284         printf("io_cancel(%#lx, {data=%#" PRI__x64
285                ", pread, reqprio=99, fildes=-42}, %p) = %s\n",
286                *ctx, cbc->aio_data, ev, sprintrc(rc));
287
288         rc = syscall(__NR_io_submit, (unsigned long) 0xfacef157beeff00dULL,
289                      (long) 0xdeadc0defacefeedLL, NULL);
290         printf("io_submit(%#lx, %ld, NULL) = %s\n",
291                (long) 0xfacef157beeff00dULL,
292                (long) 0xdeadc0defacefeedLL, sprintrc(rc));
293
294         rc = syscall(__NR_io_submit, *ctx, -1L, cbvs + nr);
295         printf("io_submit(%#lx, %ld, %p) = %s\n",
296                *ctx, -1L, cbvs + nr, sprintrc(rc));
297
298         rc = syscall(__NR_io_submit, *ctx, 1057L, cbvs2);
299         printf("io_submit(%#lx, %ld, ["
300                "{data=%#" PRI__x64 ", key=%u, %hu /* SUB_??? */, fildes=%d}, "
301                "{key=%u, pwrite, reqprio=%hd, fildes=%d, str=NULL"
302                        ", nbytes=%" PRI__u64 ", offset=%" PRI__d64
303 # ifdef IOCB_FLAG_RESFD
304                        ", resfd=%d, flags=%x"
305 # endif
306                        "}, "
307                "{key=%u, pwrite, reqprio=%hd, fildes=%d, buf=%#" PRI__x64
308                        ", nbytes=%" PRI__u64 ", offset=%" PRI__d64 "}, "
309                "{key=%u, pwrite, reqprio=%hd, fildes=%d"
310                        ", str=\"\\0\\1\\2\\3%.28s\"..."
311                        ", nbytes=%" PRI__u64 ", offset=%" PRI__d64 "}, "
312                "{key=%u, pwritev, reqprio=%hd, fildes=%d, buf=%#" PRI__x64
313                        ", nbytes=%" PRI__u64 ", offset=%" PRI__d64 "}"
314                ", {NULL}, {%#lx}, %p]) = %s\n",
315                *ctx, 1057L,
316                cbv2[0].aio_data, cbv2[0].aio_key,
317                cbv2[0].aio_lio_opcode, cbv2[0].aio_fildes,
318                cbv2[1].aio_key, cbv2[1].aio_reqprio, cbv2[1].aio_fildes,
319                cbv2[1].aio_nbytes, cbv2[1].aio_offset,
320 # ifdef IOCB_FLAG_RESFD
321                cbv2[1].aio_resfd, cbv2[1].aio_flags,
322 # endif
323                cbv2[2].aio_key, cbv2[2].aio_reqprio, cbv2[2].aio_fildes,
324                cbv2[2].aio_buf, cbv2[2].aio_nbytes, cbv2[2].aio_offset,
325                cbv2[3].aio_key, cbv2[3].aio_reqprio, cbv2[3].aio_fildes,
326                data2 + 4, cbv2[3].aio_nbytes, cbv2[3].aio_offset,
327                cbv2[4].aio_key, cbv2[4].aio_reqprio, cbv2[4].aio_fildes,
328                cbv2[4].aio_buf, cbv2[4].aio_nbytes, cbv2[4].aio_offset,
329                cbvs2[6], cbvs2 + 7, sprintrc(rc));
330
331         rc = syscall(__NR_io_submit, *ctx, nr, cbvs);
332         if (rc != (long) nr)
333                 perror_msg_and_skip("io_submit");
334         printf("io_submit(%#lx, %u, ["
335                "{data=%#" PRI__x64 ", preadv, reqprio=%hd, fildes=0, "
336                        "iovec=[{iov_base=%p, iov_len=%u}"
337                        ", {iov_base=%p, iov_len=%u}], offset=%" PRI__d64 "}, "
338                "{data=%#" PRI__x64 ", preadv, reqprio=%hd, fildes=0, "
339                        "iovec=[{iov_base=%p, iov_len=%u}"
340                        ", {iov_base=%p, iov_len=%u}], offset=%" PRI__d64 "}"
341                "]) = %s\n",
342                *ctx, nr,
343                cbv[0].aio_data, cbv[0].aio_reqprio,
344                iov0[0].iov_base, (unsigned int) iov0[0].iov_len,
345                iov0[1].iov_base, (unsigned int) iov0[1].iov_len,
346                cbv[0].aio_offset,
347                cbv[1].aio_data, cbv[1].aio_reqprio,
348                iov1[0].iov_base, (unsigned int) iov1[0].iov_len,
349                iov1[1].iov_base, (unsigned int) iov1[1].iov_len,
350                cbv[1].aio_offset,
351                sprintrc(rc));
352
353         rc = syscall(__NR_io_destroy, bogus_ctx);
354         printf("io_destroy(%#lx) = %s\n",
355                bogus_ctx, sprintrc(rc));
356
357         rc = syscall(__NR_io_destroy, *ctx);
358         printf("io_destroy(%#lx) = %s\n", *ctx, sprintrc(rc));
359
360         puts("+++ exited with 0 +++");
361         return 0;
362 }
363
364 #else
365
366 SKIP_MAIN_UNDEFINED("__NR_io_*")
367
368 #endif