1 /* Test and measure memcmp functions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #define TEST_MAIN
20 #ifdef TEST_MEMCMPEQ
21 # define TEST_NAME "__memcmpeq"
22 #elif defined WIDE
23 # define TEST_NAME "wmemcmp"
24 #else
25 # define TEST_NAME "memcmp"
26 #endif
27
28 #include "test-string.h"
29 #ifdef WIDE
30 # include <inttypes.h>
31 # include <wchar.h>
32
33 # define MEMCMP wmemcmp
34 # define MEMCPY wmemcpy
35 # define SIMPLE_MEMCMP simple_wmemcmp
36 # define CHAR wchar_t
37 # define UCHAR wchar_t
38 # define CHARBYTES 4
39 # define CHAR__MIN WCHAR_MIN
40 # define CHAR__MAX WCHAR_MAX
41
42 int
SIMPLE_MEMCMP(const wchar_t * s1,const wchar_t * s2,size_t n)43 SIMPLE_MEMCMP (const wchar_t *s1, const wchar_t *s2, size_t n)
44 {
45 int ret = 0;
46 /* Warning!
47 wmemcmp has to use SIGNED comparison for elements.
48 memcmp has to use UNSIGNED comparison for elemnts.
49 */
50 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
51 return ret;
52 }
53 #else
54 # include <limits.h>
55 # ifdef TEST_MEMCMPEQ
56 # define MEMCMP __memcmpeq
57 # define SIMPLE_MEMCMP simple_memcmpeq
58 # else
59 # define MEMCMP memcmp
60 # define SIMPLE_MEMCMP simple_memcmp
61 # endif
62 # define MEMCPY memcpy
63 # define CHAR char
64 # define MAX_CHAR 255
65 # define UCHAR unsigned char
66 # define CHARBYTES 1
67 # define CHAR__MIN CHAR_MIN
68 # define CHAR__MAX CHAR_MAX
69
70 int
SIMPLE_MEMCMP(const char * s1,const char * s2,size_t n)71 SIMPLE_MEMCMP (const char *s1, const char *s2, size_t n)
72 {
73 int ret = 0;
74
75 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
76 return ret;
77 }
78 #endif
79
80 #ifndef BAD_RESULT
81 # define BAD_RESULT(result, expec) \
82 (((result) == 0 && (expec)) || ((result) < 0 && (expec) >= 0) || \
83 ((result) > 0 && (expec) <= 0))
84 # endif
85
86 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
87
88 IMPL (MEMCMP, 1)
89
90 static int
check_result(impl_t * impl,const CHAR * s1,const CHAR * s2,size_t len,int exp_result)91 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
92 int exp_result)
93 {
94 int result = CALL (impl, s1, s2, len);
95 if (BAD_RESULT(result, exp_result))
96 {
97 error (0, 0, "Wrong result in function %s %d %d", impl->name,
98 result, exp_result);
99 ret = 1;
100 return -1;
101 }
102
103 return 0;
104 }
105
106 static void
do_one_test(impl_t * impl,const CHAR * s1,const CHAR * s2,size_t len,int exp_result)107 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
108 int exp_result)
109 {
110 if (check_result (impl, s1, s2, len, exp_result) < 0)
111 return;
112 }
113
114 static void
do_test(size_t align1,size_t align2,size_t len,int exp_result)115 do_test (size_t align1, size_t align2, size_t len, int exp_result)
116 {
117 size_t i;
118 CHAR *s1, *s2;
119
120 if (len == 0)
121 return;
122
123 align1 &= (4096 - CHARBYTES);
124 if (align1 + (len + 1) * CHARBYTES >= page_size)
125 return;
126
127 align2 &= (4096 - CHARBYTES);
128 if (align2 + (len + 1) * CHARBYTES >= page_size)
129 return;
130
131 s1 = (CHAR *) (buf1 + align1);
132 s2 = (CHAR *) (buf2 + align2);
133
134 for (i = 0; i < len; i++)
135 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
136
137 s1[len] = align1;
138 s2[len] = align2;
139 s2[len - 1] -= exp_result;
140
141 FOR_EACH_IMPL (impl, 0)
142 do_one_test (impl, s1, s2, len, exp_result);
143 }
144
145 static void
do_random_tests(void)146 do_random_tests (void)
147 {
148 size_t i, j, n, align1, align2, pos, len;
149 int result;
150 long r;
151 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
152 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
153
154 for (n = 0; n < ITERATIONS; n++)
155 {
156 align1 = random () & 31;
157 if (random () & 1)
158 align2 = random () & 31;
159 else
160 align2 = align1 + (random () & 24);
161 pos = random () & 511;
162 j = align1;
163 if (align2 > j)
164 j = align2;
165 if (pos + j >= 512)
166 pos = 511 - j - (random () & 7);
167 len = random () & 511;
168 if (len + j >= 512)
169 len = 511 - j - (random () & 7);
170 j = len + align1 + 64;
171 if (j > 512) j = 512;
172 for (i = 0; i < j; ++i)
173 p1[i] = random () & 255;
174 for (i = 0; i < j; ++i)
175 p2[i] = random () & 255;
176
177 result = 0;
178 if (pos >= len)
179 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, len);
180 else
181 {
182 MEMCPY ((CHAR *) p2 + align2, (const CHAR *) p1 + align1, pos);
183 if (p2[align2 + pos] == p1[align1 + pos])
184 {
185 p2[align2 + pos] = random () & 255;
186 if (p2[align2 + pos] == p1[align1 + pos])
187 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
188 }
189
190 if (p1[align1 + pos] < p2[align2 + pos])
191 result = -1;
192 else
193 result = 1;
194 }
195
196 FOR_EACH_IMPL (impl, 1)
197 {
198 r = CALL (impl, (CHAR *) p1 + align1, (const CHAR *) p2 + align2,
199 len);
200 if (BAD_RESULT(r, result))
201 {
202 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
203 n, impl->name, align1 * CHARBYTES & 63, align2 * CHARBYTES & 63, len, pos, r, result, p1, p2);
204 ret = 1;
205 }
206 }
207 }
208 }
209
210 static void
check1(void)211 check1 (void)
212 {
213 CHAR s1[116], s2[116];
214 int n, exp_result;
215
216 s1[0] = -108;
217 s2[0] = -108;
218 s1[1] = 99;
219 s2[1] = 99;
220 s1[2] = -113;
221 s2[2] = -113;
222 s1[3] = 1;
223 s2[3] = 1;
224 s1[4] = 116;
225 s2[4] = 116;
226 s1[5] = 99;
227 s2[5] = 99;
228 s1[6] = -113;
229 s2[6] = -113;
230 s1[7] = 1;
231 s2[7] = 1;
232 s1[8] = 84;
233 s2[8] = 84;
234 s1[9] = 99;
235 s2[9] = 99;
236 s1[10] = -113;
237 s2[10] = -113;
238 s1[11] = 1;
239 s2[11] = 1;
240 s1[12] = 52;
241 s2[12] = 52;
242 s1[13] = 99;
243 s2[13] = 99;
244 s1[14] = -113;
245 s2[14] = -113;
246 s1[15] = 1;
247 s2[15] = 1;
248 s1[16] = -76;
249 s2[16] = -76;
250 s1[17] = -14;
251 s2[17] = -14;
252 s1[18] = -109;
253 s2[18] = -109;
254 s1[19] = 1;
255 s2[19] = 1;
256 s1[20] = -108;
257 s2[20] = -108;
258 s1[21] = -14;
259 s2[21] = -14;
260 s1[22] = -109;
261 s2[22] = -109;
262 s1[23] = 1;
263 s2[23] = 1;
264 s1[24] = 84;
265 s2[24] = 84;
266 s1[25] = -15;
267 s2[25] = -15;
268 s1[26] = -109;
269 s2[26] = -109;
270 s1[27] = 1;
271 s2[27] = 1;
272 s1[28] = 52;
273 s2[28] = 52;
274 s1[29] = -15;
275 s2[29] = -15;
276 s1[30] = -109;
277 s2[30] = -109;
278 s1[31] = 1;
279 s2[31] = 1;
280 s1[32] = 20;
281 s2[32] = 20;
282 s1[33] = -15;
283 s2[33] = -15;
284 s1[34] = -109;
285 s2[34] = -109;
286 s1[35] = 1;
287 s2[35] = 1;
288 s1[36] = 20;
289 s2[36] = 20;
290 s1[37] = -14;
291 s2[37] = -14;
292 s1[38] = -109;
293 s2[38] = -109;
294 s1[39] = 1;
295 s2[39] = 1;
296 s1[40] = 52;
297 s2[40] = 52;
298 s1[41] = -14;
299 s2[41] = -14;
300 s1[42] = -109;
301 s2[42] = -109;
302 s1[43] = 1;
303 s2[43] = 1;
304 s1[44] = 84;
305 s2[44] = 84;
306 s1[45] = -14;
307 s2[45] = -14;
308 s1[46] = -109;
309 s2[46] = -109;
310 s1[47] = 1;
311 s2[47] = 1;
312 s1[48] = 116;
313 s2[48] = 116;
314 s1[49] = -14;
315 s2[49] = -14;
316 s1[50] = -109;
317 s2[50] = -109;
318 s1[51] = 1;
319 s2[51] = 1;
320 s1[52] = 116;
321 s2[52] = 116;
322 s1[53] = -15;
323 s2[53] = -15;
324 s1[54] = -109;
325 s2[54] = -109;
326 s1[55] = 1;
327 s2[55] = 1;
328 s1[56] = -44;
329 s2[56] = -44;
330 s1[57] = -14;
331 s2[57] = -14;
332 s1[58] = -109;
333 s2[58] = -109;
334 s1[59] = 1;
335 s2[59] = 1;
336 s1[60] = -108;
337 s2[60] = -108;
338 s1[61] = -15;
339 s2[61] = -15;
340 s1[62] = -109;
341 s2[62] = -109;
342 s1[63] = 1;
343 s2[63] = 1;
344 s1[64] = -76;
345 s2[64] = -76;
346 s1[65] = -15;
347 s2[65] = -15;
348 s1[66] = -109;
349 s2[66] = -109;
350 s1[67] = 1;
351 s2[67] = 1;
352 s1[68] = -44;
353 s2[68] = -44;
354 s1[69] = -15;
355 s2[69] = -15;
356 s1[70] = -109;
357 s2[70] = -109;
358 s1[71] = 1;
359 s2[71] = 1;
360 s1[72] = -12;
361 s2[72] = -12;
362 s1[73] = -15;
363 s2[73] = -15;
364 s1[74] = -109;
365 s2[74] = -109;
366 s1[75] = 1;
367 s2[75] = 1;
368 s1[76] = -12;
369 s2[76] = -12;
370 s1[77] = -14;
371 s2[77] = -14;
372 s1[78] = -109;
373 s2[78] = -109;
374 s1[79] = 1;
375 s2[79] = 1;
376 s1[80] = 20;
377 s2[80] = -68;
378 s1[81] = -12;
379 s2[81] = 64;
380 s1[82] = -109;
381 s2[82] = -106;
382 s1[83] = 1;
383 s2[83] = 1;
384 s1[84] = -12;
385 s2[84] = -12;
386 s1[85] = -13;
387 s2[85] = -13;
388 s1[86] = -109;
389 s2[86] = -109;
390 s1[87] = 1;
391 s2[87] = 1;
392 s1[88] = -44;
393 s2[88] = -44;
394 s1[89] = -13;
395 s2[89] = -13;
396 s1[90] = -109;
397 s2[90] = -109;
398 s1[91] = 1;
399 s2[91] = 1;
400 s1[92] = -76;
401 s2[92] = -76;
402 s1[93] = -13;
403 s2[93] = -13;
404 s1[94] = -109;
405 s2[94] = -109;
406 s1[95] = 1;
407 s2[95] = 1;
408 s1[96] = -108;
409 s2[96] = -108;
410 s1[97] = -13;
411 s2[97] = -13;
412 s1[98] = -109;
413 s2[98] = -109;
414 s1[99] = 1;
415 s2[99] = 1;
416 s1[100] = 116;
417 s2[100] = 116;
418 s1[101] = CHAR__MIN;
419 s2[101] = CHAR__MAX;
420 s1[102] = -109;
421 s2[102] = -109;
422 s1[103] = 1;
423 s2[103] = 1;
424 s1[104] = 84;
425 s2[104] = 84;
426 s1[105] = -13;
427 s2[105] = -13;
428 s1[106] = -109;
429 s2[106] = -109;
430 s1[107] = 1;
431 s2[107] = 1;
432 s1[108] = 52;
433 s2[108] = 52;
434 s1[109] = -13;
435 s2[109] = -13;
436 s1[110] = -109;
437 s2[110] = -109;
438 s1[111] = 1;
439 s2[111] = 1;
440 s1[112] = CHAR__MAX;
441 s2[112] = CHAR__MIN;
442 s1[113] = -13;
443 s2[113] = -13;
444 s1[114] = -109;
445 s2[114] = -109;
446 s1[115] = 1;
447 s2[115] = 1;
448
449 n = 116;
450 for (size_t i = 0; i < n; i++)
451 for (size_t len = 0; len <= n - i; ++len)
452 {
453 exp_result = SIMPLE_MEMCMP (s1 + i, s2 + i, len);
454 FOR_EACH_IMPL (impl, 0)
455 check_result (impl, s1 + i, s2 + i, len, exp_result);
456 }
457 }
458
459 /* This test checks that memcmp doesn't overrun buffers. */
460 static void
check2(void)461 check2 (void)
462 {
463 size_t max_length = page_size / sizeof (CHAR);
464
465 /* Initialize buf2 to the same values as buf1. The bug requires the
466 last compared byte to be different. */
467 memcpy (buf2, buf1, page_size);
468 ((char *) buf2)[page_size - 1] ^= 0x11;
469
470 for (size_t length = 1; length < max_length; length++)
471 {
472 CHAR *s1 = (CHAR *) buf1 + max_length - length;
473 CHAR *s2 = (CHAR *) buf2 + max_length - length;
474
475 const int exp_result = SIMPLE_MEMCMP (s1, s2, length);
476
477 FOR_EACH_IMPL (impl, 0)
478 check_result (impl, s1, s2, length, exp_result);
479 }
480 }
481
482 int
test_main(void)483 test_main (void)
484 {
485 size_t i;
486
487 test_init ();
488
489 check1 ();
490 check2 ();
491
492 printf ("%23s", "");
493 FOR_EACH_IMPL (impl, 0)
494 printf ("\t%s", impl->name);
495 putchar ('\n');
496
497 for (i = 1; i < 32; ++i)
498 {
499 do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
500 do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
501 do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
502 }
503
504 for (i = 0; i < 32; ++i)
505 {
506 do_test (0, 0, i, 0);
507 do_test (0, 0, i, 1);
508 do_test (0, 0, i, -1);
509 do_test (4096 - i, 0, i, 0);
510 do_test (4096 - i, 0, i, 1);
511 do_test (4096 - i, 0, i, -1);
512 do_test (4095, 0, i, 0);
513 do_test (4095, 0, i, 1);
514 do_test (4095, 0, i, -1);
515 do_test (4095, 4095, i, 0);
516 do_test (4095, 4095, i, 1);
517 do_test (4095, 4095, i, -1);
518 do_test (4000, 95, i, 0);
519 do_test (4000, 95, i, 1);
520 do_test (4000, 95, i, -1);
521 }
522
523 for (i = 33; i < 385; i += 32)
524 {
525 do_test (0, 0, i, 0);
526 do_test (0, 0, i, 1);
527 do_test (0, 0, i, -1);
528 do_test (i, 0, i, 0);
529 do_test (0, i, i, 1);
530 do_test (i, i, i, -1);
531 }
532
533 for (i = 1; i < 10; ++i)
534 {
535 do_test (0, 0, 2 << i, 0);
536 do_test (0, 0, 2 << i, 1);
537 do_test (0, 0, 2 << i, -1);
538 do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
539 do_test (0, 0, 16 << i, 0);
540 do_test (0, 0, 16 << i, 1);
541 do_test (0, 0, 16 << i, -1);
542 do_test (i, 0, 2 << i, 0);
543 do_test (0, i, 2 << i, 1);
544 do_test (i, i, 2 << i, -1);
545 do_test (i, 0, 16 << i, 0);
546 do_test (0, i, 16 << i, 1);
547 do_test (i, i, 16 << i, -1);
548 }
549
550 for (i = 1; i < 10; ++i)
551 {
552 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
553 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
554 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
555 }
556
557 do_random_tests ();
558 return ret;
559 }
560
561 #include <support/test-driver.c>
562