1 /* Test and measure strcmp and wcscmp functions.
2 Copyright (C) 1999-2022 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 substraction 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
*);
105 check_result (impl_t
*impl
,
106 const CHAR
*s1
, const CHAR
*s2
,
109 int result
= CALL (impl
, s1
, s2
);
110 if ((exp_result
== 0 && result
!= 0)
111 || (exp_result
< 0 && result
>= 0)
112 || (exp_result
> 0 && result
<= 0))
114 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
124 do_one_test (impl_t
*impl
,
125 const CHAR
*s1
, const CHAR
*s2
,
128 if (check_result (impl
, s1
, s2
, exp_result
) < 0)
133 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
,
143 align1
&= ~(CHARBYTES
- 1);
144 align2
&= ~(CHARBYTES
- 1);
146 align1
&= getpagesize () - 1;
147 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
150 align2
&= getpagesize () - 1;
151 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
154 /* Put them close to the end of page. */
155 i
= align1
+ CHARBYTES
* (len
+ 2);
156 s1
= (CHAR
*)(buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
157 i
= align2
+ CHARBYTES
* (len
+ 2);
158 s2
= (CHAR
*)(buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
160 for (i
= 0; i
< len
; i
++)
161 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
163 s1
[len
] = s2
[len
] = 0;
165 s2
[len
+ 1] = 24 + exp_result
;
166 s2
[len
- 1] -= exp_result
;
168 FOR_EACH_IMPL (impl
, 0)
169 do_one_test (impl
, s1
, s2
, exp_result
);
174 do_random_tests (void)
176 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
177 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
179 for (size_t n
= 0; n
< ITERATIONS
; n
++)
181 /* for wcscmp case align1 and align2 mean here alignment
182 in wchar_t symbols, it equal 4*k alignment in bytes, we
183 don't check other alignments like for example
184 p1 = (wchar_t *)(buf1 + 1)
185 because it's wrong using of wchar_t type. */
186 size_t align1
= random () & 31;
189 align2
= random () & 31;
191 align2
= align1
+ (random () & 24);
192 size_t pos
= random () & 511;
193 size_t j
= align1
> align2
? align1
: align2
;
195 pos
= 510 - j
- (random () & 7);
196 size_t len1
= random () & 511;
197 if (pos
>= len1
&& (random () & 1))
198 len1
= pos
+ (random () & 7);
200 len1
= 511 - j
- (random () & 7);
205 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
206 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
209 for (size_t i
= 0; i
< j
; ++i
)
211 p1
[i
] = random () & 255;
212 if (i
< len1
+ align1
&& !p1
[i
])
214 p1
[i
] = random () & 255;
216 p1
[i
] = 1 + (random () & 127);
219 for (size_t i
= 0; i
< j
; ++i
)
221 p2
[i
] = random () & 255;
222 if (i
< len2
+ align2
&& !p2
[i
])
224 p2
[i
] = random () & 255;
226 p2
[i
] = 1 + (random () & 127);
231 MEMCPY (p2
+ align2
, p1
+ align1
, pos
);
234 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
236 p2
[align2
+ pos
] = random () & 255;
237 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
238 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
241 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
246 p1
[len1
+ align1
] = 0;
247 p2
[len2
+ align2
] = 0;
249 FOR_EACH_IMPL (impl
, 1)
251 int r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
));
252 /* Test whether on 64-bit architectures where ABI requires
253 callee to promote has the promotion been done. */
254 asm ("" : "=g" (r
) : "0" (r
));
255 if ((r
== 0 && result
)
256 || (r
< 0 && result
>= 0)
257 || (r
> 0 && result
<= 0))
259 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",
260 n
, impl
->name
, (size_t) (p1
+ align1
) & 63, (size_t) (p1
+ align2
) & 63, len1
, len2
, pos
, r
, result
, p1
, p2
);
270 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
271 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
273 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
274 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
276 /* Check correct working for negatives values */
287 /* Check possible overflow bug, actual more for wcscmp */
292 size_t l1
= STRLEN (s1
);
293 size_t l2
= STRLEN (s2
);
295 for (size_t i1
= 0; i1
< l1
; i1
++)
296 for (size_t i2
= 0; i2
< l2
; i2
++)
298 int exp_result
= SIMPLE_STRCMP (s1
+ i1
, s2
+ i2
);
299 FOR_EACH_IMPL (impl
, 0)
300 check_result (impl
, s1
+ i1
, s2
+ i2
, exp_result
);
303 /* Test cases where there are multiple zero bytes after the first. */
305 for (size_t i
= 0; i
< 16 + 1; i
++)
311 for (size_t i
= 0; i
< 16; i
++)
315 for (int val
= 0x01; val
< 0x100; val
++)
317 for (size_t j
= 0; j
< i
; j
++)
325 exp_result
= SIMPLE_STRCMP (s1
, s2
);
326 FOR_EACH_IMPL (impl
, 0)
327 check_result (impl
, s1
, s2
, exp_result
);
335 /* To trigger bug 25933, we need a size that is equal to the vector
336 length times 4. In the case of AVX2 for Intel, we need 32 * 4. We
337 make this test generic and run it for all architectures as additional
338 boundary testing for such related algorithms. */
339 size_t size
= 32 * 4;
340 CHAR
*s1
= (CHAR
*) (buf1
+ (BUF1PAGES
- 1) * page_size
);
341 CHAR
*s2
= (CHAR
*) (buf2
+ (BUF1PAGES
- 1) * page_size
);
344 memset (s1
, 'a', page_size
);
345 memset (s2
, 'a', page_size
);
346 s1
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
347 s2
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
349 /* Iterate over a size that is just below where we expect the bug to
350 trigger up to the size we expect will trigger the bug e.g. [99-128].
351 Likewise iterate the start of two strings between 30 and 31 bytes
352 away from the boundary to simulate alignment changes. */
353 for (size_t s
= 99; s
<= size
; s
++)
354 for (size_t s1a
= 30; s1a
< 32; s1a
++)
355 for (size_t s2a
= 30; s2a
< 32; s2a
++)
357 CHAR
*s1p
= s1
+ (page_size
/ CHARBYTES
- s
) - s1a
;
358 CHAR
*s2p
= s2
+ (page_size
/ CHARBYTES
- s
) - s2a
;
359 exp_result
= SIMPLE_STRCMP (s1p
, s2p
);
360 FOR_EACH_IMPL (impl
, 0)
361 check_result (impl
, s1p
, s2p
, exp_result
);
368 size_t size
= 0xd000 + 0x4000;
370 CHAR
*buffer1
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
,
371 MAP_PRIVATE
| MAP_ANON
, -1, 0);
372 CHAR
*buffer2
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
,
373 MAP_PRIVATE
| MAP_ANON
, -1, 0);
374 if (buffer1
== MAP_FAILED
|| buffer1
== MAP_FAILED
)
375 error (EXIT_UNSUPPORTED
, errno
, "mmap failed");
377 s1
= (CHAR
*) (buffer1
+ 0x8f8 / sizeof (CHAR
));
378 s2
= (CHAR
*) (buffer2
+ 0xcff3 / sizeof (CHAR
));
380 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"));
381 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"));
383 int exp_result
= SIMPLE_STRCMP (s1
, s2
);
384 FOR_EACH_IMPL (impl
, 0)
385 check_result (impl
, s1
, s2
, exp_result
);
387 munmap ((void *) buffer1
, size
);
388 munmap ((void *) buffer2
, size
);
395 const size_t test_len
= MIN(TEST_LEN
, 3 * 4096);
402 FOR_EACH_IMPL (impl
, 0)
403 printf ("\t%s", impl
->name
);
406 for (i
= 1; i
< 32; ++i
)
408 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
409 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
410 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
413 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
415 do_test (0, 0, 2 << i
, MIDCHAR
, 0);
416 do_test (0, 0, 2 << i
, LARGECHAR
, 0);
417 do_test (0, 0, 2 << i
, MIDCHAR
, 1);
418 do_test (0, 0, 2 << i
, LARGECHAR
, 1);
419 do_test (0, 0, 2 << i
, MIDCHAR
, -1);
420 do_test (0, 0, 2 << i
, LARGECHAR
, -1);
421 do_test (0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
422 do_test (CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
425 for (i
= 1; i
< 8; ++i
)
427 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
428 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
429 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
430 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
431 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
432 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
435 for (j
= 0; j
< 160; ++j
)
437 for (i
= 0; i
< test_len
;)
439 do_test (getpagesize () - j
- 1, 0, i
, 127, 0);
440 do_test (getpagesize () - j
- 1, 0, i
, 127, 1);
441 do_test (getpagesize () - j
- 1, 0, i
, 127, -1);
443 do_test (getpagesize () - j
- 1, j
, i
, 127, 0);
444 do_test (getpagesize () - j
- 1, j
, i
, 127, 1);
445 do_test (getpagesize () - j
- 1, j
, i
, 127, -1);
447 do_test (0, getpagesize () - j
- 1, i
, 127, 0);
448 do_test (0, getpagesize () - j
- 1, i
, 127, 1);
449 do_test (0, getpagesize () - j
- 1, i
, 127, -1);
451 do_test (j
, getpagesize () - j
- 1, i
, 127, 0);
452 do_test (j
, getpagesize () - j
- 1, i
, 127, 1);
453 do_test (j
, getpagesize () - j
- 1, i
, 127, -1);
455 for (k
= 2; k
<= 128; k
+= k
)
457 do_test (getpagesize () - k
, getpagesize () - j
- 1, i
, 127, 0);
458 do_test (getpagesize () - k
- 1, getpagesize () - j
- 1, i
, 127,
460 do_test (getpagesize () - k
, getpagesize () - j
- 1, i
, 127, 1);
461 do_test (getpagesize () - k
- 1, getpagesize () - j
- 1, i
, 127,
463 do_test (getpagesize () - k
, getpagesize () - j
- 1, i
, 127, -1);
464 do_test (getpagesize () - k
- 1, getpagesize () - j
- 1, i
, 127,
476 else if (i
+ 161 < test_len
)
481 if (i
+ 161 > test_len
)
486 else if (i
+ 32 < test_len
)
501 #include <support/test-driver.c>