1 /* Test and measure strcmp and wcscmp functions.
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #define TEST_LEN (getpagesize () * 3)
20 #define MIN_PAGE_SIZE (TEST_LEN + 2 * getpagesize ())
24 # define TEST_NAME "wcscmp"
26 # define TEST_NAME "strcmp"
28 #include "test-string.h"
29 #include <support/test-driver.h>
34 # define L(str) L##str
35 # define STRCMP wcscmp
36 # define STRCPY wcscpy
37 # define STRLEN wcslen
38 # define MEMCPY wmemcpy
39 # define SIMPLE_STRCMP simple_wcscmp
41 # define UCHAR wchar_t
43 # define CHARBYTESLOG 2
44 # define CHARALIGN __alignof__ (CHAR)
45 # define MIDCHAR 0x7fffffff
46 # define LARGECHAR 0xfffffffe
47 # define CHAR__MAX WCHAR_MAX
48 # define CHAR__MIN WCHAR_MIN
50 /* Wcscmp uses signed semantics for comparison, not unsigned */
51 /* Avoid using subtraction since possible overflow */
54 simple_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
66 return c1
< c2
? -1 : 1;
73 # define STRCMP strcmp
74 # define STRCPY strcpy
75 # define STRLEN strlen
76 # define MEMCPY memcpy
77 # define SIMPLE_STRCMP simple_strcmp
79 # define UCHAR unsigned char
81 # define CHARBYTESLOG 0
84 # define LARGECHAR 0xfe
85 # define CHAR__MAX CHAR_MAX
86 # define CHAR__MIN CHAR_MIN
88 /* Strcmp uses unsigned semantics for comparison. */
90 simple_strcmp (const char *s1
, const char *s2
)
94 while ((ret
= *(unsigned char *) s1
- *(unsigned char*) s2
++) == 0 && *s1
++);
100 typedef int (*proto_t
) (const CHAR
*, const CHAR
*);
104 /* Also check the default implementation. */
106 #undef libc_hidden_builtin_def
107 #define libc_hidden_builtin_def(a)
108 #undef libc_hidden_def
109 #define libc_hidden_def(a)
111 #define weak_alias(a, b)
112 #undef attribute_hidden
113 #define attribute_hidden
115 # define STRCMP __strcmp_default
116 # include "string/strcmp.c"
117 # define STRCMP_DEFAULT STRCMP
119 # define WCSCMP __wcscmp_default
120 # include "wcsmbs/wcscmp.c"
121 # define STRCMP_DEFAULT WCSCMP
123 IMPL (STRCMP_DEFAULT
, 1)
127 check_result (impl_t
*impl
,
128 const CHAR
*s1
, const CHAR
*s2
,
131 int result
= CALL (impl
, s1
, s2
);
132 if ((exp_result
== 0 && result
!= 0)
133 || (exp_result
< 0 && result
>= 0)
134 || (exp_result
> 0 && result
<= 0))
136 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
146 do_one_test (impl_t
*impl
,
147 const CHAR
*s1
, const CHAR
*s2
,
150 if (check_result (impl
, s1
, s2
, exp_result
) < 0)
155 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
,
165 align1
&= ~(CHARBYTES
- 1);
166 align2
&= ~(CHARBYTES
- 1);
168 align1
&= getpagesize () - 1;
169 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
172 align2
&= getpagesize () - 1;
173 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
176 /* Put them close to the end of page. */
177 i
= align1
+ CHARBYTES
* (len
+ 2);
178 s1
= (CHAR
*)(buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
179 i
= align2
+ CHARBYTES
* (len
+ 2);
180 s2
= (CHAR
*)(buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
182 for (i
= 0; i
< len
; i
++)
183 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
185 s1
[len
] = s2
[len
] = 0;
187 s2
[len
+ 1] = 24 + exp_result
;
188 s2
[len
- 1] -= exp_result
;
190 FOR_EACH_IMPL (impl
, 0)
191 do_one_test (impl
, s1
, s2
, exp_result
);
196 do_random_tests (void)
198 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
199 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
201 for (size_t n
= 0; n
< ITERATIONS
; n
++)
203 /* for wcscmp case align1 and align2 mean here alignment
204 in wchar_t symbols, it equal 4*k alignment in bytes, we
205 don't check other alignments like for example
206 p1 = (wchar_t *)(buf1 + 1)
207 because it's wrong using of wchar_t type. */
208 size_t align1
= random () & 31;
211 align2
= random () & 31;
213 align2
= align1
+ (random () & 24);
214 size_t pos
= random () & 511;
215 size_t j
= align1
> align2
? align1
: align2
;
217 pos
= 510 - j
- (random () & 7);
218 size_t len1
= random () & 511;
219 if (pos
>= len1
&& (random () & 1))
220 len1
= pos
+ (random () & 7);
222 len1
= 511 - j
- (random () & 7);
227 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
228 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
231 for (size_t i
= 0; i
< j
; ++i
)
233 p1
[i
] = random () & 255;
234 if (i
< len1
+ align1
&& !p1
[i
])
236 p1
[i
] = random () & 255;
238 p1
[i
] = 1 + (random () & 127);
241 for (size_t i
= 0; i
< j
; ++i
)
243 p2
[i
] = random () & 255;
244 if (i
< len2
+ align2
&& !p2
[i
])
246 p2
[i
] = random () & 255;
248 p2
[i
] = 1 + (random () & 127);
253 MEMCPY (p2
+ align2
, p1
+ align1
, pos
);
256 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
258 p2
[align2
+ pos
] = random () & 255;
259 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
260 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
263 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
268 p1
[len1
+ align1
] = 0;
269 p2
[len2
+ align2
] = 0;
271 FOR_EACH_IMPL (impl
, 1)
273 int r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
));
274 /* Test whether on 64-bit architectures where ABI requires
275 callee to promote has the promotion been done. */
276 asm ("" : "=g" (r
) : "0" (r
));
277 if ((r
== 0 && result
)
278 || (r
< 0 && result
>= 0)
279 || (r
> 0 && result
<= 0))
281 error (0, 0, "Iteration %zd - wrong result in function %s (align in bytes: %zd, align in bytes: %zd, len1: %zd, len2: %zd, pos: %zd) %d != %d, p1 %p p2 %p",
282 n
, impl
->name
, (size_t) (p1
+ align1
) & 63, (size_t) (p1
+ align2
) & 63, len1
, len2
, pos
, r
, result
, p1
, p2
);
292 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
293 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
295 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
296 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
298 /* Check correct working for negatives values */
309 /* Check possible overflow bug, actual more for wcscmp */
314 size_t l1
= STRLEN (s1
);
315 size_t l2
= STRLEN (s2
);
317 for (size_t i1
= 0; i1
< l1
; i1
++)
318 for (size_t i2
= 0; i2
< l2
; i2
++)
320 int exp_result
= SIMPLE_STRCMP (s1
+ i1
, s2
+ i2
);
321 FOR_EACH_IMPL (impl
, 0)
322 check_result (impl
, s1
+ i1
, s2
+ i2
, exp_result
);
325 /* Test cases where there are multiple zero bytes after the first. */
327 for (size_t i
= 0; i
< 16 + 1; i
++)
333 for (size_t i
= 0; i
< 16; i
++)
337 for (int val
= 0x01; val
< 0x100; val
++)
339 for (size_t j
= 0; j
< i
; j
++)
347 exp_result
= SIMPLE_STRCMP (s1
, s2
);
348 FOR_EACH_IMPL (impl
, 0)
349 check_result (impl
, s1
, s2
, exp_result
);
357 /* To trigger bug 25933, we need a size that is equal to the vector
358 length times 4. In the case of AVX2 for Intel, we need 32 * 4. We
359 make this test generic and run it for all architectures as additional
360 boundary testing for such related algorithms. */
361 size_t size
= 32 * 4;
362 CHAR
*s1
= (CHAR
*) (buf1
+ (BUF1PAGES
- 1) * page_size
);
363 CHAR
*s2
= (CHAR
*) (buf2
+ (BUF1PAGES
- 1) * page_size
);
366 memset (s1
, 'a', page_size
);
367 memset (s2
, 'a', page_size
);
368 s1
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
369 s2
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
371 /* Iterate over a size that is just below where we expect the bug to
372 trigger up to the size we expect will trigger the bug e.g. [99-128].
373 Likewise iterate the start of two strings between 30 and 31 bytes
374 away from the boundary to simulate alignment changes. */
375 for (size_t s
= 99; s
<= size
; s
++)
376 for (size_t s1a
= 30; s1a
< 32; s1a
++)
377 for (size_t s2a
= 30; s2a
< 32; s2a
++)
379 CHAR
*s1p
= s1
+ (page_size
/ CHARBYTES
- s
) - s1a
;
380 CHAR
*s2p
= s2
+ (page_size
/ CHARBYTES
- s
) - s2a
;
381 exp_result
= SIMPLE_STRCMP (s1p
, s2p
);
382 FOR_EACH_IMPL (impl
, 0)
383 check_result (impl
, s1p
, s2p
, exp_result
);
390 size_t size
= 0xd000 + 0x4000;
392 CHAR
*buffer1
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
,
393 MAP_PRIVATE
| MAP_ANON
, -1, 0);
394 CHAR
*buffer2
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
,
395 MAP_PRIVATE
| MAP_ANON
, -1, 0);
396 if (buffer1
== MAP_FAILED
|| buffer1
== MAP_FAILED
)
397 error (EXIT_UNSUPPORTED
, errno
, "mmap failed");
399 s1
= (CHAR
*) (buffer1
+ 0x8f8 / sizeof (CHAR
));
400 s2
= (CHAR
*) (buffer2
+ 0xcff3 / sizeof (CHAR
));
402 STRCPY(s1
, L("/export/redhat/rpms/BUILD/java-1.8.0-openjdk-1.8.0.312.b07-2.fc35.x86_64/openjdk/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/PathDocFileFactory.java"));
403 STRCPY(s2
, L("/export/redhat/rpms/BUILD/java-1.8.0-openjdk-1.8.0.312.b07-2.fc35.x86_64/openjdk/langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ThrowsTaglet.java"));
405 int exp_result
= SIMPLE_STRCMP (s1
, s2
);
406 FOR_EACH_IMPL (impl
, 0)
407 check_result (impl
, s1
, s2
, exp_result
);
409 munmap ((void *) buffer1
, size
);
410 munmap ((void *) buffer2
, size
);
417 const size_t test_len
= MIN(TEST_LEN
, 3 * 4096);
424 FOR_EACH_IMPL (impl
, 0)
425 printf ("\t%s", impl
->name
);
428 for (i
= 1; i
< 32; ++i
)
430 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
431 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
432 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
435 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
437 do_test (0, 0, 2 << i
, MIDCHAR
, 0);
438 do_test (0, 0, 2 << i
, LARGECHAR
, 0);
439 do_test (0, 0, 2 << i
, MIDCHAR
, 1);
440 do_test (0, 0, 2 << i
, LARGECHAR
, 1);
441 do_test (0, 0, 2 << i
, MIDCHAR
, -1);
442 do_test (0, 0, 2 << i
, LARGECHAR
, -1);
443 do_test (0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
444 do_test (CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
447 for (i
= 1; i
< 8; ++i
)
449 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
450 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
451 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
452 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
453 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
454 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
457 for (j
= 0; j
< 160; ++j
)
459 for (i
= 0; i
< test_len
;)
461 do_test (getpagesize () - j
- 1, 0, i
, 127, 0);
462 do_test (getpagesize () - j
- 1, 0, i
, 127, 1);
463 do_test (getpagesize () - j
- 1, 0, i
, 127, -1);
465 do_test (getpagesize () - j
- 1, j
, i
, 127, 0);
466 do_test (getpagesize () - j
- 1, j
, i
, 127, 1);
467 do_test (getpagesize () - j
- 1, j
, i
, 127, -1);
469 do_test (0, getpagesize () - j
- 1, i
, 127, 0);
470 do_test (0, getpagesize () - j
- 1, i
, 127, 1);
471 do_test (0, getpagesize () - j
- 1, i
, 127, -1);
473 do_test (j
, getpagesize () - j
- 1, i
, 127, 0);
474 do_test (j
, getpagesize () - j
- 1, i
, 127, 1);
475 do_test (j
, getpagesize () - j
- 1, i
, 127, -1);
477 for (k
= 2; k
<= 128; k
+= k
)
479 do_test (getpagesize () - k
, getpagesize () - j
- 1, i
, 127, 0);
480 do_test (getpagesize () - k
- 1, getpagesize () - j
- 1, i
, 127,
482 do_test (getpagesize () - k
, getpagesize () - j
- 1, i
, 127, 1);
483 do_test (getpagesize () - k
- 1, getpagesize () - j
- 1, i
, 127,
485 do_test (getpagesize () - k
, getpagesize () - j
- 1, i
, 127, -1);
486 do_test (getpagesize () - k
- 1, getpagesize () - j
- 1, i
, 127,
498 else if (i
+ 161 < test_len
)
503 if (i
+ 161 > test_len
)
508 else if (i
+ 32 < test_len
)
523 #include <support/test-driver.c>