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