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

1/* Test and measure memcpy 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#ifndef MEMCPY_RESULT
21# define MEMCPY_RESULT(dst, len) dst
22# define MIN_PAGE_SIZE 131072
23# define TEST_MAIN
24# define TEST_NAME "memcpy"
25# include "test-string.h"
26
27char *simple_memcpy (char *, const char *, size_t);
28char *builtin_memcpy (char *, const char *, size_t);
29
30IMPL (simple_memcpy, 0)
31IMPL (builtin_memcpy, 0)
32IMPL (memcpy, 1)
33
34char *
35simple_memcpy (char *dst, const char *src, size_t n)
36{
37 char *ret = dst;
38 while (n--)
39 *dst++ = *src++;
40 return ret;
41}
42
43char *
44builtin_memcpy (char *dst, const char *src, size_t n)
45{
46 return __builtin_memcpy (dst, src, n);
47}
48#endif
49
50typedef char *(*proto_t) (char *, const char *, size_t);
51
52static void
53do_one_test (impl_t *impl, char *dst, const char *src,
54 size_t len)
55{
56 size_t i;
57
58 /* Must clear the destination buffer set by the previous run. */
59 for (i = 0; i < len; i++)
60 dst[i] = 0;
61
62 if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
63 {
64 error (0, 0, "Wrong result in function %s %p %p", impl->name,
65 CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
66 ret = 1;
67 return;
68 }
69
70 if (memcmp (dst, src, len) != 0)
71 {
72 error (0, 0, "Wrong result in function %s dst %p \"%.*s\" src %p \"%.*s\" len %zu",
73 impl->name, dst, (int) len, dst, src, (int) len, src, len);
74 ret = 1;
75 return;
76 }
77}
78
79static void
80do_test (size_t align1, size_t align2, size_t len)
81{
82 size_t i, j;
83 char *s1, *s2;
84
85 align1 &= 63;
86 if (align1 + len >= page_size)
87 return;
88
89 align2 &= 63;
90 if (align2 + len >= page_size)
91 return;
92
93 s1 = (char *) (buf1 + align1);
94 s2 = (char *) (buf2 + align2);
95
96 for (i = 0, j = 1; i < len; i++, j += 23)
97 s1[i] = j;
98
99 FOR_EACH_IMPL (impl, 0)
100 do_one_test (impl, s2, s1, len);
101}
102
103static void
104do_random_tests (void)
105{
106 size_t i, j, n, align1, align2, len, size1, size2, size;
107 int c;
108 unsigned char *p1, *p2;
109 unsigned char *res;
110
111 for (n = 0; n < ITERATIONS; n++)
112 {
113 if (n == 0)
114 {
115 len = getpagesize ();
116 size = len + 512;
117 size1 = size;
118 size2 = size;
119 align1 = 512;
120 align2 = 512;
121 }
122 else
123 {
124 if ((random () & 255) == 0)
125 size = 65536;
126 else
127 size = 768;
128 if (size > page_size)
129 size = page_size;
130 size1 = size;
131 size2 = size;
132 i = random ();
133 if (i & 3)
134 size -= 256;
135 if (i & 1)
136 size1 -= 256;
137 if (i & 2)
138 size2 -= 256;
139 if (i & 4)
140 {
141 len = random () % size;
142 align1 = size1 - len - (random () & 31);
143 align2 = size2 - len - (random () & 31);
144 if (align1 > size1)
145 align1 = 0;
146 if (align2 > size2)
147 align2 = 0;
148 }
149 else
150 {
151 align1 = random () & 63;
152 align2 = random () & 63;
153 len = random () % size;
154 if (align1 + len > size1)
155 align1 = size1 - len;
156 if (align2 + len > size2)
157 align2 = size2 - len;
158 }
159 }
160 p1 = buf1 + page_size - size1;
161 p2 = buf2 + page_size - size2;
162 c = random () & 255;
163 j = align1 + len + 256;
164 if (j > size1)
165 j = size1;
166 for (i = 0; i < j; ++i)
167 p1[i] = random () & 255;
168
169 FOR_EACH_IMPL (impl, 1)
170 {
171 j = align2 + len + 256;
172 if (j > size2)
173 j = size2;
174 memset (p2, c, j);
175 res = (unsigned char *) CALL (impl,
176 (char *) (p2 + align2),
177 (char *) (p1 + align1), len);
178 if (res != MEMCPY_RESULT (p2 + align2, len))
179 {
180 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
181 n, impl->name, align1, align2, len, res,
182 MEMCPY_RESULT (p2 + align2, len));
183 ret = 1;
184 }
185 for (i = 0; i < align2; ++i)
186 {
187 if (p2[i] != c)
188 {
189 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
190 n, impl->name, align1, align2, len);
191 ret = 1;
192 break;
193 }
194 }
195 for (i = align2 + len; i < j; ++i)
196 {
197 if (p2[i] != c)
198 {
199 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
200 n, impl->name, align1, align2, len);
201 ret = 1;
202 break;
203 }
204 }
205 if (memcmp (p1 + align1, p2 + align2, len))
206 {
207 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
208 n, impl->name, align1, align2, len);
209 ret = 1;
210 }
211 }
212 }
213}
214
215static void
216do_test1 (void)
217{
218 size_t size = 0x100000;
219 void *large_buf;
220
221 large_buf = mmap (NULL, size * 2 + page_size, PROT_READ | PROT_WRITE,
222 MAP_PRIVATE | MAP_ANON, -1, 0);
223 if (large_buf == MAP_FAILED)
224 {
225 puts ("Failed to allocat large_buf, skipping do_test1");
226 return;
227 }
228
229 if (mprotect (large_buf + size, page_size, PROT_NONE))
230 error (EXIT_FAILURE, errno, "mprotect failed");
231
232 size_t arrary_size = size / sizeof (uint32_t);
233 uint32_t *dest = large_buf;
234 uint32_t *src = large_buf + size + page_size;
235 size_t i;
236
237 for (i = 0; i < arrary_size; i++)
238 src[i] = (uint32_t) i;
239
240 FOR_EACH_IMPL (impl, 0)
241 {
242 memset (dest, -1, size);
243 CALL (impl, (char *) dest, (char *) src, size);
244 for (i = 0; i < arrary_size; i++)
245 if (dest[i] != src[i])
246 {
247 error (0, 0,
248 "Wrong result in function %s dst \"%p\" src \"%p\" offset \"%zd\"",
249 impl->name, dest, src, i);
250 ret = 1;
251 break;
252 }
253 }
254
255 munmap ((void *) dest, size);
256 munmap ((void *) src, size);
257}
258
259int
260test_main (void)
261{
262 size_t i;
263
264 test_init ();
265
266 printf ("%23s", "");
267 FOR_EACH_IMPL (impl, 0)
268 printf ("\t%s", impl->name);
269 putchar ('\n');
270
271 for (i = 0; i < 18; ++i)
272 {
273 do_test (0, 0, 1 << i);
274 do_test (i, 0, 1 << i);
275 do_test (0, i, 1 << i);
276 do_test (i, i, 1 << i);
277 }
278
279 for (i = 0; i < 32; ++i)
280 {
281 do_test (0, 0, i);
282 do_test (i, 0, i);
283 do_test (0, i, i);
284 do_test (i, i, i);
285 }
286
287 for (i = 3; i < 32; ++i)
288 {
289 if ((i & (i - 1)) == 0)
290 continue;
291 do_test (0, 0, 16 * i);
292 do_test (i, 0, 16 * i);
293 do_test (0, i, 16 * i);
294 do_test (i, i, 16 * i);
295 }
296
297 do_test (0, 0, getpagesize ());
298
299 do_random_tests ();
300
301 do_test1 ();
302
303 return ret;
304}
305
306#include <support/test-driver.c>
307

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