2 * Check decoding of pread64 and pwrite64 syscalls.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
38 dump_str(const char *str, const unsigned int len)
40 static const char dots[16] = "................";
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);
47 tprintf(" | %05x %-49s %-16.*s |\n",
55 print_hex(const char *str, const unsigned int len)
57 const unsigned char *ustr = (const unsigned char *) str;
60 for (i = 0; i < len; ++i) {
61 unsigned int c = ustr[i];
65 tprintf("\\t"); break;
67 tprintf("\\n"); break;
69 tprintf("\\v"); break;
71 tprintf("\\f"); break;
73 tprintf("\\r"); break;
75 tprintf("\\%o", ustr[i]);
81 test_dump(const unsigned int len)
86 size_t ps1 = get_page_size() - 1;
87 buf = (void *) (((size_t) buf + ps1) & ~ps1) - len;
89 buf = tail_alloc(len);
92 const off_t offset = 0xdefaceddeadbeefLL + len;
93 long rc = pread(0, buf, len, offset);
95 perror_msg_and_fail("pread64: expected %d, returned %ld",
98 tprintf("%s(%d, \"", "pread64", 0);
100 tprintf("\", %d, %lld) = %ld\n", len, (long long) offset, rc);
104 for (i = 0; i < len; ++i)
107 rc = pwrite(1, buf, len, offset);
109 perror_msg_and_fail("pwrite64: expected %d, returned %ld",
112 tprintf("%s(%d, \"", "pwrite64", 1);
114 tprintf("\", %d, %lld) = %ld\n", len, (long long) offset, rc);
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);
132 char *nil = tail_alloc(1);
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);
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);
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);
150 void *efault = r1 - get_page_size();
154 rc = pwrite(1, w, 0, 0);
156 perror_msg_and_fail("pwrite64: expected 0, returned %ld", rc);
157 tprintf("pwrite64(1, \"\", 0, 0) = 0\n");
159 rc = pwrite(1, efault, 1, 0);
161 perror_msg_and_fail("pwrite64: expected -1 EFAULT"
162 ", returned %ld", rc);
163 tprintf("pwrite64(1, %p, 1, 0) = -1 EFAULT (%m)\n", efault);
165 rc = pwrite(1, nil, 1, -3);
167 perror_msg_and_fail("pwrite64: expected -1, returned %ld", rc);
168 tprintf("pwrite64(1, \"\\0\", 1, -3) = -1 EINVAL (%m)\n");
170 rc = pwrite(1, w, w_len, 0);
171 if (rc != (int) w_len)
172 perror_msg_and_fail("pwrite64: expected %u, returned %ld",
174 tprintf("pwrite64(1, \"%s\", %u, 0) = %ld\n"
175 " | 00000 %-49s %-16s |\n",
176 w_c, w_len, rc, w_d, w_c);
179 rc = pread(0, r0, 0, 0);
181 perror_msg_and_fail("pread64: expected 0, returned %ld", rc);
182 tprintf("pread64(0, \"\", 0, 0) = 0\n");
184 rc = pread(0, efault, 1, 0);
186 perror_msg_and_fail("pread64: expected -1, returned %ld", rc);
187 tprintf("pread64(0, %p, 1, 0) = -1 EFAULT (%m)\n", efault);
189 rc = pread(0, efault, 2, -7);
191 perror_msg_and_fail("pread64: expected -1, returned %ld", rc);
192 tprintf("pread64(0, %p, 2, -7) = -1 EINVAL (%m)\n", efault);
194 rc = pread(0, r0, r0_len, 0);
195 if (rc != (int) r0_len)
196 perror_msg_and_fail("pread64: expected %u, returned %ld",
198 tprintf("pread64(0, \"%s\", %u, 0) = %ld\n"
199 " | 00000 %-49s %-16s |\n",
200 r0_c, r0_len, rc, r0_d, r0_c);
202 rc = pread(0, r1, w_len, r0_len);
203 if (rc != (int) r1_len)
204 perror_msg_and_fail("pread64: expected %u, returned %ld",
206 tprintf("pread64(0, \"%s\", %u, %u) = %ld\n"
207 " | 00000 %-49s %-16s |\n",
208 r1_c, w_len, r0_len, rc, r1_d, r1_c);
211 if (open("/dev/zero", O_RDONLY))
212 perror_msg_and_fail("open");
214 if (open("/dev/null", O_WRONLY) != 1)
215 perror_msg_and_fail("open");
218 for (i = 0; i <= 32; ++i)
221 tprintf("+++ exited with 0 +++\n");