1 /*
2  * Check decoding of io_pgetevents syscall.
3  *
4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2015-2018 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 #include <unistd.h>
33 #include <asm/unistd.h>
34 #include "scno.h"
35 
36 #if defined __NR_io_setup && defined __NR_io_pgetevents
37 
38 # include <fcntl.h>
39 # include <inttypes.h>
40 # include <stdio.h>
41 # include <time.h>
42 
43 # include "nsig.h"
44 
45 # include <linux/aio_abi.h>
46 
47 # if !HAVE_STRUCT___AIO_SIGSET
48 struct __aio_sigset {
49 	sigset_t *sigmask;
50 	size_t sigsetsize;
51 };
52 # endif
53 
54 static const char *errstr;
55 
56 static long
sys_io_pgetevents(const kernel_ulong_t ctx_id,const kernel_long_t min_nr,const kernel_long_t nr,const kernel_ulong_t events,const kernel_ulong_t timeout,const kernel_ulong_t usig)57 sys_io_pgetevents(const kernel_ulong_t ctx_id,
58 		  const kernel_long_t min_nr,
59 		  const kernel_long_t nr,
60 		  const kernel_ulong_t events,
61 		  const kernel_ulong_t timeout,
62 		  const kernel_ulong_t usig)
63 {
64 	long rc = syscall(__NR_io_pgetevents, ctx_id, min_nr, nr,
65 			  events, timeout, usig);
66 	errstr = sprintrc(rc);
67 	return rc;
68 }
69 
70 int
main(void)71 main(void)
72 {
73 	static const kernel_ulong_t bogus_ctx =
74 		(kernel_ulong_t) 0xface1e55deadbeefLL;
75 	static const kernel_long_t bogus_min_nr =
76 		(kernel_long_t) 0xca7faceddeadf00dLL;
77 	static const kernel_long_t bogus_nr =
78 		(kernel_long_t) 0xba5e1e505ca571e0LL;
79 	static const size_t bogus_sigsetsize =
80 		(size_t) 0xdeadbeefbadcaffeULL;
81 
82 	const unsigned int sizeof_data0 = 4096;
83 	const unsigned int sizeof_data1 = 8192;
84 	void *data0 = tail_alloc(sizeof_data0);
85 	void *data1 = tail_alloc(sizeof_data1);
86 
87 	const struct iocb proto_cb[] = {
88 		{
89 			.aio_data = (unsigned long) 0xfeedface11111111ULL,
90 			.aio_reqprio = 11,
91 			.aio_buf = (unsigned long) data0,
92 			.aio_offset = (unsigned long) 0xdeface1facefeedULL,
93 			.aio_nbytes = sizeof_data0
94 		},
95 		{
96 			.aio_data = (unsigned long) 0xfeedface22222222ULL,
97 			.aio_reqprio = 22,
98 			.aio_buf = (unsigned long) data1,
99 			.aio_offset = (unsigned long) 0xdeface2cafef00dULL,
100 			.aio_nbytes = sizeof_data1
101 		}
102 	};
103 	const struct iocb *cb = tail_memdup(proto_cb, sizeof(proto_cb));
104 
105 	const long proto_cbs[] = {
106 		(long) &cb[0], (long) &cb[1]
107 	};
108 	const long *cbs = tail_memdup(proto_cbs, sizeof(proto_cbs));
109 
110 	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned long, ctx);
111 	*ctx = 0;
112 
113 	const unsigned int nr = ARRAY_SIZE(proto_cb);
114 
115 	const struct io_event *ev = tail_alloc(nr * sizeof(struct io_event));
116 	TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, ts);
117 	TAIL_ALLOC_OBJECT_CONST_PTR(struct __aio_sigset, ss);
118 	TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, sigs);
119 
120 	(void) close(0);
121 	if (open("/dev/zero", O_RDONLY))
122 		perror_msg_and_skip("open: %s", "/dev/zero");
123 
124 	if (syscall(__NR_io_setup, nr, ctx))
125 		perror_msg_and_skip("io_setup");
126 
127 	if (syscall(__NR_io_submit, *ctx, nr, cbs) != (long) nr)
128 		perror_msg_and_skip("io_submit");
129 
130 	sys_io_pgetevents(bogus_ctx, bogus_min_nr, bogus_nr,
131 			  (uintptr_t) (ev + 1), 0, 0);
132 	printf("io_pgetevents(%#jx, %ld, %ld, %p, NULL, NULL) = %s\n",
133 	       (uintmax_t) bogus_ctx, (long) bogus_min_nr,
134 	       (long) bogus_nr, ev + 1, errstr);
135 
136 	sys_io_pgetevents(bogus_ctx, bogus_min_nr, bogus_nr,
137 			  0, (uintptr_t) (ts + 1), 0);
138 	printf("io_pgetevents(%#jx, %ld, %ld, NULL, %p, NULL) = %s\n",
139 	       (uintmax_t) bogus_ctx, (long) bogus_min_nr,
140 	       (long) bogus_nr, ts + 1, errstr);
141 
142 	sys_io_pgetevents(bogus_ctx, bogus_min_nr, bogus_nr,
143 			  0, 0, (uintptr_t) (ss + 1));
144 	printf("io_pgetevents(%#jx, %ld, %ld, NULL, NULL, %p) = %s\n",
145 	       (uintmax_t) bogus_ctx, (long) bogus_min_nr,
146 	       (long) bogus_nr, ss + 1, errstr);
147 
148 	ss->sigmask = sigs + 1;
149 	ss->sigsetsize =  bogus_sigsetsize;
150 	sys_io_pgetevents(bogus_ctx, bogus_min_nr, bogus_nr,
151 			  0, 0, (uintptr_t) ss);
152 	printf("io_pgetevents(%#jx, %ld, %ld, NULL, NULL"
153 	       ", {sigmask=%p, sigsetsize=%zu}) = %s\n",
154 	       (uintmax_t) bogus_ctx, (long) bogus_min_nr,
155 	       (long) bogus_nr, sigs + 1, bogus_sigsetsize, errstr);
156 
157 	ts->tv_sec = 0xdeadbeefU;
158 	ts->tv_nsec = 0xfacefeedU;
159 	ss->sigmask = sigs;
160 	ss->sigsetsize =  NSIG_BYTES;
161 	sys_io_pgetevents(bogus_ctx, 0, 0, 0, (uintptr_t) ts, (uintptr_t) ss);
162 	printf("io_pgetevents(%#jx, 0, 0, NULL"
163 	       ", {tv_sec=%lld, tv_nsec=%llu}"
164 	       ", {sigmask=~[], sigsetsize=%u}) = %s\n",
165 	       (uintmax_t) bogus_ctx, (long long) ts->tv_sec,
166 	       zero_extend_signed_to_ull(ts->tv_nsec), NSIG_BYTES,
167 	       errstr);
168 
169 	sigemptyset(sigs);
170 	sigaddset(sigs, SIGSYS);
171 
172 	ts->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
173 	ts->tv_nsec = (long) 0xbadc0dedfacefeedLL;
174 	sys_io_pgetevents(bogus_ctx, 0, 0, 0, (uintptr_t) ts, (uintptr_t) ss);
175 	printf("io_pgetevents(%#jx, 0, 0, NULL"
176 	       ", {tv_sec=%lld, tv_nsec=%llu}"
177 	       ", {sigmask=[SYS], sigsetsize=%u}) = %s\n",
178 	       (uintmax_t) bogus_ctx, (long long) ts->tv_sec,
179 	       zero_extend_signed_to_ull(ts->tv_nsec), NSIG_BYTES,
180 	       errstr);
181 
182 	puts("+++ exited with 0 +++");
183 	return 0;
184 }
185 
186 #else
187 
188 SKIP_MAIN_UNDEFINED("__NR_io_setup && __NR_io_pgetevents")
189 
190 #endif
191