test-memchr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Test 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. #define TEST_MAIN
  16. #ifndef WIDE
  17. # define TEST_NAME "memchr"
  18. #else
  19. # define TEST_NAME "wmemchr"
  20. #endif /* WIDE */
  21. #include "test-string.h"
  22. #include <stdint.h>
  23. #ifndef WIDE
  24. # define MEMCHR memchr
  25. # define CHAR char
  26. # define UCHAR unsigned char
  27. # define BIG_CHAR CHAR_MAX
  28. # define SMALL_CHAR 127
  29. #else
  30. # include <wchar.h>
  31. # define MEMCHR wmemchr
  32. # define CHAR wchar_t
  33. # define UCHAR wchar_t
  34. # define BIG_CHAR WCHAR_MAX
  35. # define SMALL_CHAR 1273
  36. #endif /* WIDE */
  37. typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
  38. IMPL (MEMCHR, 1)
  39. /* Also check the generic implementation. */
  40. #undef MEMCHR
  41. #undef static_weak_alias
  42. #define static_weak_alias(a, b)
  43. #undef libc_hidden_builtin_def
  44. #define libc_hidden_builtin_def(a)
  45. #undef libc_hidden_def
  46. #define libc_hidden_def(a)
  47. #undef libc_hidden_weak
  48. #define libc_hidden_weak(a)
  49. #ifndef WIDE
  50. # define MEMCHR __memchr_default
  51. # include "string/memchr.c"
  52. # define MEMCHR_DEFAULT MEMCHR
  53. #else
  54. # define WMEMCHR __wmemchr_default
  55. # include "wcsmbs/wmemchr.c"
  56. # define MEMCHR_DEFAULT WMEMCHR
  57. #endif
  58. IMPL (MEMCHR_DEFAULT, 1)
  59. static void
  60. do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res)
  61. {
  62. CHAR *res = CALL (impl, s, c, n);
  63. if (res != exp_res)
  64. {
  65. error (0, 0, "Wrong result in function %s (%p, %d, %zu) -> %p != %p",
  66. impl->name, s, c, n, res, exp_res);
  67. ret = 1;
  68. return;
  69. }
  70. }
  71. static void
  72. do_test (size_t align, size_t pos, size_t len, size_t n, int seek_char)
  73. {
  74. size_t i;
  75. CHAR *result;
  76. if ((align + len) * sizeof (CHAR) >= page_size)
  77. return;
  78. CHAR *buf = (CHAR *) (buf1);
  79. for (i = 0; i < len; ++i)
  80. {
  81. buf[align + i] = 1 + 23 * i % SMALL_CHAR;
  82. if (buf[align + i] == seek_char)
  83. buf[align + i] = seek_char + 1;
  84. }
  85. buf[align + len] = 0;
  86. if (pos < MIN(n, len))
  87. {
  88. buf[align + pos] = seek_char;
  89. buf[align + len] = -seek_char;
  90. result = (CHAR *) (buf + align + pos);
  91. }
  92. else
  93. {
  94. result = NULL;
  95. buf[align + len] = seek_char;
  96. }
  97. FOR_EACH_IMPL (impl, 0)
  98. do_one_test (impl, (CHAR *) (buf + align), seek_char, n, result);
  99. }
  100. static void
  101. do_overflow_tests (void)
  102. {
  103. size_t i, j, len;
  104. const size_t one = 1;
  105. uintptr_t buf_addr = (uintptr_t) buf1;
  106. for (i = 0; i < 750; ++i)
  107. {
  108. do_test (0, i, 751, SIZE_MAX - i, BIG_CHAR);
  109. do_test (0, i, 751, i - buf_addr, BIG_CHAR);
  110. do_test (0, i, 751, -buf_addr - i, BIG_CHAR);
  111. do_test (0, i, 751, SIZE_MAX - buf_addr - i, BIG_CHAR);
  112. do_test (0, i, 751, SIZE_MAX - buf_addr + i, BIG_CHAR);
  113. len = 0;
  114. for (j = 8 * sizeof(size_t) - 1; j ; --j)
  115. {
  116. len |= one << j;
  117. do_test (0, i, 751, len - i, BIG_CHAR);
  118. do_test (0, i, 751, len + i, BIG_CHAR);
  119. do_test (0, i, 751, len - buf_addr - i, BIG_CHAR);
  120. do_test (0, i, 751, len - buf_addr + i, BIG_CHAR);
  121. do_test (0, i, 751, ~len - i, BIG_CHAR);
  122. do_test (0, i, 751, ~len + i, BIG_CHAR);
  123. do_test (0, i, 751, ~len - buf_addr - i, BIG_CHAR);
  124. do_test (0, i, 751, ~len - buf_addr + i, BIG_CHAR);
  125. }
  126. }
  127. }
  128. static void
  129. do_random_tests (void)
  130. {
  131. size_t i, j, n, align, pos, len;
  132. int seek_char;
  133. CHAR *result;
  134. UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
  135. for (n = 0; n < ITERATIONS; n++)
  136. {
  137. align = random () & 15;
  138. pos = random () & 511;
  139. if (pos + align >= 512)
  140. pos = 511 - align - (random () & 7);
  141. len = random () & 511;
  142. if (pos >= len)
  143. len = pos + (random () & 7);
  144. if (len + align >= 512)
  145. len = 512 - align - (random () & 7);
  146. seek_char = random () & BIG_CHAR;
  147. j = len + align + 64;
  148. if (j > 512)
  149. j = 512;
  150. for (i = 0; i < j; i++)
  151. {
  152. if (i == pos + align)
  153. p[i] = seek_char;
  154. else
  155. {
  156. p[i] = random () & BIG_CHAR;
  157. if (i < pos + align && p[i] == seek_char)
  158. p[i] = seek_char + 13;
  159. }
  160. }
  161. if (pos < len)
  162. {
  163. size_t r = random ();
  164. if ((r & 31) == 0)
  165. len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
  166. result = (CHAR *) (p + pos + align);
  167. }
  168. else
  169. result = NULL;
  170. FOR_EACH_IMPL (impl, 1)
  171. if (CALL (impl, (CHAR *) (p + align), seek_char, len) != result)
  172. {
  173. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
  174. n, impl->name, align, seek_char, len, pos,
  175. CALL (impl, (CHAR *) (p + align), seek_char, len),
  176. result, p);
  177. ret = 1;
  178. }
  179. }
  180. }
  181. int
  182. test_main (void)
  183. {
  184. size_t i, j;
  185. test_init ();
  186. printf ("%20s", "");
  187. FOR_EACH_IMPL (impl, 0)
  188. printf ("\t%s", impl->name);
  189. putchar ('\n');
  190. for (i = 1; i < 8; ++i)
  191. {
  192. /* Test n == 0. */
  193. do_test (i, i, 0, 0, 23);
  194. do_test (i, i, 0, 0, 0);
  195. do_test (0, 16 << i, 2048, 2048, 23);
  196. do_test (i, 64, 256, 256, 23);
  197. do_test (0, 16 << i, 2048, 2048, 0);
  198. do_test (i, 64, 256, 256, 0);
  199. /* Check for large input sizes and for these cases we need to
  200. make sure the byte is within the size range (that's why
  201. 7 << i must be smaller than 2048). */
  202. do_test (0, 7 << i, 2048, SIZE_MAX, 23);
  203. do_test (0, 2048 - i, 2048, SIZE_MAX, 23);
  204. do_test (i, 64, 256, SIZE_MAX, 23);
  205. do_test (0, 7 << i, 2048, SIZE_MAX, 0);
  206. do_test (0, 2048 - i, 2048, SIZE_MAX, 0);
  207. do_test (i, 64, 256, SIZE_MAX, 0);
  208. }
  209. for (i = 1; i < 64; ++i)
  210. {
  211. for (j = 1; j < 64; j++)
  212. {
  213. do_test (0, 64 - j, 64, SIZE_MAX, 23);
  214. do_test (i, 64 - j, 64, SIZE_MAX, 23);
  215. }
  216. }
  217. for (i = 1; i < 32; ++i)
  218. {
  219. do_test (0, i, i + 1, i + 1, 23);
  220. do_test (0, i, i + 1, i + 1, 0);
  221. }
  222. /* BZ#21182 - wrong overflow calculation for i686 implementation
  223. with address near end of the page. */
  224. for (i = 2; i < 16; ++i)
  225. {
  226. /* page_size is in fact getpagesize() * 2. */
  227. do_test (page_size / 2 - i, i, i, 1, 0x9B);
  228. do_test (page_size / 2 - i, i - 1, i - 1, 1, 0x9B);
  229. do_test (page_size / 2 - (i * 4), i + 128, i + 128, i, 0x9B);
  230. }
  231. do_random_tests ();
  232. do_overflow_tests ();
  233. return ret;
  234. }
  235. #include <support/test-driver.c>