test-rawmemchr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Test and measure memchr functions.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <support/xunistd.h>
  17. #define TEST_MAIN
  18. #define TEST_NAME "rawmemchr"
  19. #include "test-string.h"
  20. typedef char *(*proto_t) (const char *, int);
  21. char *simple_rawmemchr (const char *, int);
  22. IMPL (simple_rawmemchr, 0)
  23. IMPL (rawmemchr, 1)
  24. char *
  25. simple_rawmemchr (const char *s, int c)
  26. {
  27. while (1)
  28. if (*s++ == (char) c)
  29. return (char *) s - 1;
  30. return NULL;
  31. }
  32. static void
  33. do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
  34. {
  35. char *res = CALL (impl, s, c);
  36. if (res != exp_res)
  37. {
  38. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  39. res, exp_res);
  40. ret = 1;
  41. return;
  42. }
  43. }
  44. static void
  45. do_test_bz29234 (void)
  46. {
  47. size_t i, j;
  48. char *ptr_start;
  49. char *buf = xmmap (0, 8192, PROT_READ | PROT_WRITE,
  50. MAP_PRIVATE | MAP_ANONYMOUS, -1);
  51. memset (buf, -1, 8192);
  52. ptr_start = buf + 4096 - 8;
  53. /* Out of range matches before the start of a page. */
  54. memset (ptr_start - 8, 0x1, 8);
  55. for (j = 0; j < 8; ++j)
  56. {
  57. for (i = 0; i < 128; ++i)
  58. {
  59. ptr_start[i + j] = 0x1;
  60. FOR_EACH_IMPL (impl, 0)
  61. do_one_test (impl, (char *) (ptr_start + j), 0x1,
  62. ptr_start + i + j);
  63. ptr_start[i + j] = 0xff;
  64. }
  65. }
  66. xmunmap (buf, 8192);
  67. }
  68. static void
  69. do_test (size_t align, size_t pos, size_t len, int seek_char)
  70. {
  71. size_t i;
  72. char *result;
  73. align &= getpagesize () - 1;
  74. if (align + len >= page_size)
  75. return;
  76. for (i = 0; i < len; ++i)
  77. {
  78. buf1[align + i] = 1 + 23 * i % 127;
  79. if (buf1[align + i] == seek_char)
  80. buf1[align + i] = seek_char + 1;
  81. }
  82. buf1[align + len] = 0;
  83. assert (pos < len);
  84. buf1[align + pos] = seek_char;
  85. buf1[align + len] = -seek_char;
  86. result = (char *) (buf1 + align + pos);
  87. FOR_EACH_IMPL (impl, 0)
  88. do_one_test (impl, (char *) (buf1 + align), seek_char, result);
  89. }
  90. static void
  91. do_random_tests (void)
  92. {
  93. size_t i, j, n, align, pos, len;
  94. int seek_char;
  95. char *result;
  96. unsigned char *p = buf1 + page_size - 512;
  97. for (n = 0; n < ITERATIONS; n++)
  98. {
  99. align = random () & 15;
  100. pos = random () & 511;
  101. if (pos + align >= 512)
  102. pos = 511 - align - (random () & 7);
  103. len = random () & 511;
  104. if (len + align >= 512)
  105. len = 512 - align - (random () & 7);
  106. if (pos >= len)
  107. continue;
  108. seek_char = random () & 255;
  109. j = len + align + 64;
  110. if (j > 512)
  111. j = 512;
  112. for (i = 0; i < j; i++)
  113. {
  114. if (i == pos + align)
  115. p[i] = seek_char;
  116. else
  117. {
  118. p[i] = random () & 255;
  119. if (i < pos + align && p[i] == seek_char)
  120. p[i] = seek_char + 13;
  121. }
  122. }
  123. if (align)
  124. {
  125. p[align - 1] = seek_char;
  126. if (align > 4)
  127. p[align - 4] = seek_char;
  128. }
  129. assert (pos < len);
  130. size_t r = random ();
  131. if ((r & 31) == 0)
  132. len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
  133. result = (char *) (p + pos + align);
  134. FOR_EACH_IMPL (impl, 1)
  135. if (CALL (impl, (char *) (p + align), seek_char) != result)
  136. {
  137. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
  138. n, impl->name, align, seek_char, len, pos,
  139. CALL (impl, (char *) (p + align), seek_char),
  140. result, p);
  141. ret = 1;
  142. }
  143. if (align)
  144. {
  145. p[align - 1] = seek_char;
  146. if (align > 4)
  147. p[align - 4] = seek_char;
  148. }
  149. }
  150. }
  151. int
  152. test_main (void)
  153. {
  154. size_t i;
  155. test_init ();
  156. printf ("%20s", "");
  157. FOR_EACH_IMPL (impl, 0)
  158. printf ("\t%s", impl->name);
  159. putchar ('\n');
  160. for (i = 1; i < 7; ++i)
  161. {
  162. do_test (0, 16 << i, 2048, 23);
  163. do_test (i, 64, 256, 23);
  164. do_test (0, 16 << i, 2048, 0);
  165. do_test (i, 64, 256, 0);
  166. do_test (getpagesize () - i, 64, 256, 23);
  167. do_test (getpagesize () - i, 64, 256, 0);
  168. }
  169. for (i = 1; i < 32; ++i)
  170. {
  171. do_test (0, i, i + 1, 23);
  172. do_test (0, i, i + 1, 0);
  173. do_test (getpagesize () - 7, i, i + 1, 23);
  174. do_test (getpagesize () - i / 2, i, i + 1, 23);
  175. do_test (getpagesize () - i, i, i + 1, 23);
  176. }
  177. do_random_tests ();
  178. do_test_bz29234 ();
  179. return ret;
  180. }
  181. #include <support/test-driver.c>