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