blob: c351c02b93e10d9582ad9d846e05452bcef968ac [file] [log] [blame]
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +00001/*
2 * Check decoding of pread64 and pwrite64 syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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 * 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.
17 *
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.
28 */
29
30#include "tests.h"
31
32#include <fcntl.h>
33#include <stdio.h>
Dmitry V. Levin7b5d8f92016-04-02 18:27:44 +000034#include <stdlib.h>
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +000035#include <sys/uio.h>
36#include <unistd.h>
37
Dmitry V. Levin7b5d8f92016-04-02 18:27:44 +000038static void
39dump_str(const char *str, const unsigned int len)
40{
41 static const char dots[16] = "................";
42 unsigned int i;
43
44 for (i = 0; i < len; i += 16) {
45 unsigned int n = len - i > 16 ? 16 : len - i;
46 const char *dump = hexdump_memdup(str + i, n);
47
48 tprintf(" | %05x %-49s %-16.*s |\n",
49 i, dump, n, dots);
50
51 free((void *) dump);
52 }
53}
54
55static void
56print_hex(const char *str, const unsigned int len)
57{
58 const unsigned char *ustr = (const unsigned char *) str;
59 unsigned int i;
60
61 for (i = 0; i < len; ++i) {
62 unsigned int c = ustr[i];
63
64 switch (c) {
65 case '\t':
66 tprintf("\\t"); break;
67 case '\n':
68 tprintf("\\n"); break;
69 case '\v':
70 tprintf("\\v"); break;
71 case '\f':
72 tprintf("\\f"); break;
73 case '\r':
74 tprintf("\\r"); break;
75 default:
76 tprintf("\\%o", ustr[i]);
77 }
78 }
79}
80
81static void
82test_dump(const unsigned int len)
83{
84 static char *buf;
85
86 if (buf) {
87 size_t ps1 = get_page_size() - 1;
88 buf = (void *) (((size_t) buf + ps1) & ~ps1) - len;
89 } else {
90 buf = tail_alloc(len);
91 }
92
93 const off_t offset = 0xdefaceddeadbeefLL + len;
94 long rc = pread(0, buf, len, offset);
95 if (rc != (int) len)
96 perror_msg_and_fail("pread64: expected %d, returned %ld",
97 len, rc);
98
99 tprintf("%s(%d, \"", "pread64", 0);
100 print_hex(buf, len);
101 tprintf("\", %d, %lld) = %ld\n", len, (long long) offset, rc);
102 dump_str(buf, len);
103
104 unsigned int i;
105 for (i = 0; i < len; ++i)
106 buf[i] = i;
107
108 rc = pwrite(1, buf, len, offset);
109 if (rc != (int) len)
110 perror_msg_and_fail("pwrite64: expected %d, returned %ld",
111 len, rc);
112
113 tprintf("%s(%d, \"", "pwrite64", 1);
114 print_hex(buf, len);
115 tprintf("\", %d, %lld) = %ld\n", len, (long long) offset, rc);
116 dump_str(buf, len);
117
118 if (!len)
119 buf = 0;
120}
121
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +0000122int
123main(void)
124{
125 tprintf("%s", "");
126
127 static char tmp[] = "pread64-pwrite64-tmpfile";
128 if (open(tmp, O_CREAT|O_RDONLY|O_TRUNC, 0600) != 0)
129 perror_msg_and_fail("creat: %s", tmp);
130 if (open(tmp, O_WRONLY) != 1)
131 perror_msg_and_fail("open: %s", tmp);
132
133 char *nil = tail_alloc(1);
134 *nil = '\0';
135
136 static const char w_c[] = "0123456789abcde";
137 const unsigned int w_len = LENGTH_OF(w_c);
138 const char *w_d = hexdump_strdup(w_c);
139 const void *w = tail_memdup(w_c, w_len);
140
141 static const char r0_c[] = "01234567";
142 const char *r0_d = hexdump_strdup(r0_c);
143 const unsigned int r0_len = (w_len + 1) / 2;
144 void *r0 = tail_alloc(r0_len);
145
146 static const char r1_c[] = "89abcde";
147 const char *r1_d = hexdump_strdup(r1_c);
148 const unsigned int r1_len = w_len - r0_len;
149 void *r1 = tail_alloc(w_len);
150
151 void *efault = r1 - get_page_size();
152
153 long rc;
154
155 rc = pwrite(1, w, 0, 0);
156 if (rc)
157 perror_msg_and_fail("pwrite64: expected 0, returned %ld", rc);
158 tprintf("pwrite64(1, \"\", 0, 0) = 0\n");
159
160 rc = pwrite(1, w, w_len + 1, 0);
161 if (rc != -1)
162 perror_msg_and_fail("pwrite64: expected -1 EFAULT"
163 ", returned %ld", rc);
164 tprintf("pwrite64(1, %p, %u, 0) = -1 EFAULT (%m)\n",
165 w, w_len + 1);
166
167 rc = pwrite(1, nil, 1, -3);
168 if (rc != -1)
169 perror_msg_and_fail("pwrite64: expected -1, returned %ld", rc);
170 tprintf("pwrite64(1, \"\\0\", 1, -3) = -1 EINVAL (%m)\n");
171
172 rc = pwrite(1, w, w_len, 0);
173 if (rc != (int) w_len)
174 perror_msg_and_fail("pwrite64: expected %u, returned %ld",
175 w_len, rc);
176 tprintf("pwrite64(1, \"%s\", %u, 0) = %ld\n"
177 " | 00000 %-49s %-16s |\n",
178 w_c, w_len, rc, w_d, w_c);
179 close(1);
180
Dmitry V. Levin7b5d8f92016-04-02 18:27:44 +0000181 rc = pread(0, r0, 0, 0);
182 if (rc)
183 perror_msg_and_fail("pread64: expected 0, returned %ld", rc);
184 tprintf("pread64(0, \"\", 0, 0) = 0\n");
185
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +0000186 rc = pread(0, efault, 1, 0);
187 if (rc != -1)
188 perror_msg_and_fail("pread64: expected -1, returned %ld", rc);
189 tprintf("pread64(0, %p, 1, 0) = -1 EFAULT (%m)\n", efault);
190
191 rc = pread(0, efault, 2, -7);
192 if (rc != -1)
193 perror_msg_and_fail("pread64: expected -1, returned %ld", rc);
194 tprintf("pread64(0, %p, 2, -7) = -1 EINVAL (%m)\n", efault);
195
196 rc = pread(0, r0, r0_len, 0);
197 if (rc != (int) r0_len)
198 perror_msg_and_fail("pread64: expected %u, returned %ld",
199 r0_len, rc);
200 tprintf("pread64(0, \"%s\", %u, 0) = %ld\n"
201 " | 00000 %-49s %-16s |\n",
202 r0_c, r0_len, rc, r0_d, r0_c);
203
204 rc = pread(0, r1, w_len, r0_len);
205 if (rc != (int) r1_len)
206 perror_msg_and_fail("pread64: expected %u, returned %ld",
207 r1_len, rc);
208 tprintf("pread64(0, \"%s\", %u, %u) = %ld\n"
209 " | 00000 %-49s %-16s |\n",
210 r1_c, w_len, r0_len, rc, r1_d, r1_c);
211 close(0);
212
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +0000213 if (open("/dev/zero", O_RDONLY))
214 perror_msg_and_fail("open");
215
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +0000216 if (open("/dev/null", O_WRONLY) != 1)
217 perror_msg_and_fail("open");
218
Dmitry V. Levin7b5d8f92016-04-02 18:27:44 +0000219 unsigned int i;
220 for (i = 0; i <= 32; ++i)
221 test_dump(i);
Dmitry V. Levin9f3a6af2016-04-02 01:08:24 +0000222
223 tprintf("+++ exited with 0 +++\n");
224 return 0;
225}