Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test and measure memmove functions.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#define TEST_MAIN
21#ifdef TEST_BCOPY
22# define TEST_NAME "bcopy"
23#else
24# define TEST_NAME "memmove"
25#endif
26#include "test-string.h"
27#include <support/test-driver.h>
28
29char *simple_memmove (char *, const char *, size_t);
30
31#ifdef TEST_BCOPY
32typedef void (*proto_t) (const char *, char *, size_t);
33void simple_bcopy (const char *, char *, size_t);
34
35IMPL (simple_bcopy, 0)
36IMPL (bcopy, 1)
37
38void
39simple_bcopy (const char *src, char *dst, size_t n)
40{
41 simple_memmove (dst, src, n);
42}
43#else
44typedef char *(*proto_t) (char *, const char *, size_t);
45
46IMPL (simple_memmove, 0)
47IMPL (memmove, 1)
48#endif
49
50char *
51inhibit_loop_to_libcall
52simple_memmove (char *dst, const char *src, size_t n)
53{
54 char *ret = dst;
55 if (src < dst)
56 {
57 dst += n;
58 src += n;
59 while (n--)
60 *--dst = *--src;
61 }
62 else
63 while (n--)
64 *dst++ = *src++;
65 return ret;
66}
67
68static void
69do_one_test (impl_t *impl, char *dst, char *src, const char *orig_src,
70 size_t len)
71{
72 /* This also clears the destination buffer set by the previous run. */
73 memcpy (src, orig_src, len);
74#ifdef TEST_BCOPY
75 CALL (impl, src, dst, len);
76#else
77 char *res;
78
79 res = CALL (impl, dst, src, len);
80 if (res != dst)
81 {
82 error (0, 0, "Wrong result in function %s %p %p", impl->name,
83 res, dst);
84 ret = 1;
85 return;
86 }
87#endif
88
89 if (memcmp (dst, orig_src, len) != 0)
90 {
91 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
92 impl->name, dst, src);
93 ret = 1;
94 return;
95 }
96}
97
98static void
99do_test (size_t align1, size_t align2, size_t len)
100{
101 size_t i, j;
102 char *s1, *s2;
103
104 align1 &= 63;
105 if (align1 + len >= page_size)
106 return;
107
108 align2 &= 63;
109 if (align2 + len >= page_size)
110 return;
111
112 s1 = (char *) (buf1 + align1);
113 s2 = (char *) (buf2 + align2);
114
115 for (i = 0, j = 1; i < len; i++, j += 23)
116 s1[i] = j;
117
118 FOR_EACH_IMPL (impl, 0)
119 do_one_test (impl, s2, (char *) (buf2 + align1), s1, len);
120}
121
122static void
123do_random_tests (void)
124{
125 size_t i, n, align1, align2, len, size;
126 size_t srcstart, srcend, dststart, dstend;
127 int c;
128 unsigned char *p1, *p2;
129#ifndef TEST_BCOPY
130 unsigned char *res;
131#endif
132
133 for (n = 0; n < ITERATIONS; n++)
134 {
135 if ((random () & 255) == 0)
136 size = 65536;
137 else
138 size = 512;
139 if (size > page_size)
140 size = page_size;
141 if ((random () & 3) == 0)
142 {
143 len = random () & (size - 1);
144 align1 = size - len - (random () & 31);
145 align2 = size - len - (random () & 31);
146 if (align1 > size)
147 align1 = 0;
148 if (align2 > size)
149 align2 = 0;
150 }
151 else
152 {
153 align1 = random () & (size / 2 - 1);
154 align2 = random () & (size / 2 - 1);
155 len = random () & (size - 1);
156 if (align1 + len > size)
157 align1 = size - len;
158 if (align2 + len > size)
159 align2 = size - len;
160 }
161
162 p1 = buf1 + page_size - size;
163 p2 = buf2 + page_size - size;
164 c = random () & 255;
165 srcend = align1 + len + 256;
166 if (srcend > size)
167 srcend = size;
168 if (align1 > 256)
169 srcstart = align1 - 256;
170 else
171 srcstart = 0;
172 for (i = srcstart; i < srcend; ++i)
173 p1[i] = random () & 255;
174 dstend = align2 + len + 256;
175 if (dstend > size)
176 dstend = size;
177 if (align2 > 256)
178 dststart = align2 - 256;
179 else
180 dststart = 0;
181
182 FOR_EACH_IMPL (impl, 1)
183 {
184 memset (p2 + dststart, c, dstend - dststart);
185 memcpy (p2 + srcstart, p1 + srcstart, srcend - srcstart);
186#ifdef TEST_BCOPY
187 CALL (impl, (char *) (p2 + align1), (char *) (p2 + align2), len);
188#else
189 res = (unsigned char *) CALL (impl,
190 (char *) (p2 + align2),
191 (char *) (p2 + align1), len);
192 if (res != p2 + align2)
193 {
194 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
195 n, impl->name, align1, align2, len, res, p2 + align2);
196 ret = 1;
197 }
198#endif
199 if (memcmp (p1 + align1, p2 + align2, len))
200 {
201 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
202 n, impl->name, align1, align2, len);
203 ret = 1;
204 }
205 for (i = dststart; i < dstend; ++i)
206 {
207 if (i >= align2 && i < align2 + len)
208 {
209 i = align2 + len - 1;
210 continue;
211 }
212 if (i >= srcstart && i < srcend)
213 {
214 i = srcend - 1;
215 continue;
216 }
217 if (p2[i] != c)
218 {
219 error (0, 0, "Iteration %zd - garbage in memset area, %s (%zd, %zd, %zd)",
220 n, impl->name, align1, align2, len);
221 ret = 1;
222 break;
223 }
224 }
225
226 if (srcstart < align2
227 && memcmp (p2 + srcstart, p1 + srcstart,
228 (srcend > align2 ? align2 : srcend) - srcstart))
229 {
230 error (0, 0, "Iteration %zd - garbage before dst, %s (%zd, %zd, %zd)",
231 n, impl->name, align1, align2, len);
232 ret = 1;
233 break;
234 }
235
236 i = srcstart > align2 + len ? srcstart : align2 + len;
237 if (srcend > align2 + len
238 && memcmp (p2 + i, p1 + i, srcend - i))
239 {
240 error (0, 0, "Iteration %zd - garbage after dst, %s (%zd, %zd, %zd)",
241 n, impl->name, align1, align2, len);
242 ret = 1;
243 break;
244 }
245 }
246 }
247}
248
249static void
250do_test2 (void)
251{
252 size_t size = 0x20000000;
253 uint32_t * large_buf;
254
255 large_buf = mmap ((void*) 0x70000000, size, PROT_READ | PROT_WRITE,
256 MAP_PRIVATE | MAP_ANON, -1, 0);
257
258 if (large_buf == MAP_FAILED)
259 error (EXIT_UNSUPPORTED, errno, "Large mmap failed");
260
261 if ((uintptr_t) large_buf > 0x80000000 - 128
262 || 0x80000000 - (uintptr_t) large_buf > 0x20000000)
263 {
264 error (0, 0, "Large mmap allocated improperly");
265 ret = EXIT_UNSUPPORTED;
266 munmap ((void *) large_buf, size);
267 return;
268 }
269
270 size_t bytes_move = 0x80000000 - (uintptr_t) large_buf;
271 size_t arr_size = bytes_move / sizeof (uint32_t);
272 size_t i;
273
274 FOR_EACH_IMPL (impl, 0)
275 {
276 for (i = 0; i < arr_size; i++)
277 large_buf[i] = (uint32_t) i;
278
279 uint32_t * dst = &large_buf[33];
280
281#ifdef TEST_BCOPY
282 CALL (impl, (char *) large_buf, (char *) dst, bytes_move);
283#else
284 CALL (impl, (char *) dst, (char *) large_buf, bytes_move);
285#endif
286
287 for (i = 0; i < arr_size; i++)
288 {
289 if (dst[i] != (uint32_t) i)
290 {
291 error (0, 0,
292 "Wrong result in function %s dst \"%p\" src \"%p\" offset \"%zd\"",
293 impl->name, dst, large_buf, i);
294 ret = 1;
295 break;
296 }
297 }
298 }
299
300 munmap ((void *) large_buf, size);
301}
302
303int
304test_main (void)
305{
306 size_t i;
307
308 test_init ();
309
310 printf ("%23s", "");
311 FOR_EACH_IMPL (impl, 0)
312 printf ("\t%s", impl->name);
313 putchar ('\n');
314
315 for (i = 0; i < 14; ++i)
316 {
317 do_test (0, 32, 1 << i);
318 do_test (32, 0, 1 << i);
319 do_test (0, i, 1 << i);
320 do_test (i, 0, 1 << i);
321 }
322
323 for (i = 0; i < 32; ++i)
324 {
325 do_test (0, 32, i);
326 do_test (32, 0, i);
327 do_test (0, i, i);
328 do_test (i, 0, i);
329 }
330
331 for (i = 3; i < 32; ++i)
332 {
333 if ((i & (i - 1)) == 0)
334 continue;
335 do_test (0, 32, 16 * i);
336 do_test (32, 0, 16 * i);
337 do_test (0, i, 16 * i);
338 do_test (i, 0, 16 * i);
339 }
340
341 do_random_tests ();
342
343 do_test2 ();
344
345 return ret;
346}
347
348#include <support/test-driver.c>
349

Warning: That file was not part of the compilation database. It may have many parsing errors.