1 /* Test and measure strstr functions.
2    Copyright (C) 2010-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 #define TEST_NAME "strstr"
21 #include "test-string.h"
22 
23 
24 #define STRSTR c_strstr
25 #define libc_hidden_builtin_def(arg) /* nothing */
26 #define __strnlen strnlen
27 #include "strstr.c"
28 
29 /* Naive implementation to verify results.  */
30 static char *
simple_strstr(const char * s1,const char * s2)31 simple_strstr (const char *s1, const char *s2)
32 {
33   ssize_t s1len = strlen (s1);
34   ssize_t s2len = strlen (s2);
35 
36   if (s2len > s1len)
37     return NULL;
38 
39   for (ssize_t i = 0; i <= s1len - s2len; ++i)
40     {
41       size_t j;
42       for (j = 0; j < s2len; ++j)
43 	if (s1[i + j] != s2[j])
44 	  break;
45       if (j == s2len)
46 	return (char *) s1 + i;
47     }
48 
49   return NULL;
50 }
51 
52 
53 typedef char *(*proto_t) (const char *, const char *);
54 
55 IMPL (c_strstr, 0)
56 IMPL (strstr, 1)
57 
58 
59 static int
check_result(impl_t * impl,const char * s1,const char * s2,char * exp_result)60 check_result (impl_t *impl, const char *s1, const char *s2,
61 	      char *exp_result)
62 {
63   char *result = CALL (impl, s1, s2);
64   if (result != exp_result)
65     {
66       error (0, 0, "Wrong result in function %s %s %s", impl->name,
67 	     (result == NULL) ? "(null)" : result,
68 	     (exp_result == NULL) ? "(null)" : exp_result);
69       ret = 1;
70       return -1;
71     }
72 
73   return 0;
74 }
75 
76 static void
do_one_test(impl_t * impl,const char * s1,const char * s2,char * exp_result)77 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
78 {
79   if (check_result (impl, s1, s2, exp_result) < 0)
80     return;
81 }
82 
83 
84 static void
do_test(size_t align1,size_t align2,size_t len1,size_t len2,int fail)85 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
86 	 int fail)
87 {
88   char *s1 = (char *) (buf1 + align1);
89   char *s2 = (char *) (buf2 + align2);
90 
91   static const char d[] = "1234567890abcdef";
92 #define dl (sizeof (d) - 1)
93   char *ss2 = s2;
94   for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
95     {
96       size_t t = l > dl ? dl : l;
97       ss2 = mempcpy (ss2, d, t);
98     }
99   s2[len2] = '\0';
100 
101   if (fail)
102     {
103       char *ss1 = s1;
104       for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
105 	{
106 	  size_t t = l > dl ? dl : l;
107 	  memcpy (ss1, d, t);
108 	  ++ss1[len2 > 7 ? 7 : len2 - 1];
109 	  ss1 += t;
110 	}
111     }
112   else
113     {
114       memset (s1, '0', len1);
115       memcpy (s1 + len1 - len2, s2, len2);
116     }
117   s1[len1] = '\0';
118 
119   FOR_EACH_IMPL (impl, 0)
120     do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
121 
122 }
123 
124 static void
check1(void)125 check1 (void)
126 {
127   const char s1[] =
128     "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_A7_20_EF_BF_BD";
129   const char s2[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
130   char *exp_result;
131 
132   exp_result = simple_strstr (s1, s2);
133   FOR_EACH_IMPL (impl, 0)
134     check_result (impl, s1, s2, exp_result);
135 }
136 
137 static void
check2(void)138 check2 (void)
139 {
140   const char s1_stack[] = ", enable_static, \0, enable_shared, ";
141   const size_t s1_byte_count = 18;
142   const char *s2_stack = &(s1_stack[s1_byte_count]);
143   const size_t s2_byte_count = 18;
144   char *exp_result;
145   const size_t page_size_real = getpagesize ();
146 
147   /* Haystack at end of page.  The following page is protected.  */
148   char *s1_page_end = (void *) buf1 + page_size - s1_byte_count;
149   strcpy (s1_page_end, s1_stack);
150 
151   /* Haystack which crosses a page boundary.
152      Note: page_size is at least 2 * getpagesize.  See test_init.  */
153   char *s1_page_cross = (void *) buf1 + page_size_real - 8;
154   strcpy (s1_page_cross, s1_stack);
155 
156   /* Needle at end of page.  The following page is protected.  */
157   char *s2_page_end = (void *) buf2 + page_size - s2_byte_count;
158   strcpy (s2_page_end, s2_stack);
159 
160   /* Needle which crosses a page boundary.
161      Note: page_size is at least 2 * getpagesize.  See test_init.  */
162   char *s2_page_cross = (void *) buf2 + page_size_real - 8;
163   strcpy (s2_page_cross, s2_stack);
164 
165   exp_result = simple_strstr (s1_stack, s2_stack);
166   FOR_EACH_IMPL (impl, 0)
167     {
168       check_result (impl, s1_stack, s2_stack, exp_result);
169       check_result (impl, s1_stack, s2_page_end, exp_result);
170       check_result (impl, s1_stack, s2_page_cross, exp_result);
171 
172       check_result (impl, s1_page_end, s2_stack, exp_result);
173       check_result (impl, s1_page_end, s2_page_end, exp_result);
174       check_result (impl, s1_page_end, s2_page_cross, exp_result);
175 
176       check_result (impl, s1_page_cross, s2_stack, exp_result);
177       check_result (impl, s1_page_cross, s2_page_end, exp_result);
178       check_result (impl, s1_page_cross, s2_page_cross, exp_result);
179     }
180 }
181 
182 #define N 1024
183 
184 static void
pr23637(void)185 pr23637 (void)
186 {
187   char *h = (char*) buf1;
188   char *n = (char*) buf2;
189 
190   for (int i = 0; i < N; i++)
191     {
192       n[i] = 'x';
193       h[i] = ' ';
194       h[i + N] = 'x';
195     }
196 
197   n[N] = '\0';
198   h[N * 2] = '\0';
199 
200   /* Ensure we don't match at the first 'x'.  */
201   h[0] = 'x';
202 
203   char *exp_result = simple_strstr (h, n);
204   FOR_EACH_IMPL (impl, 0)
205     check_result (impl, h, n, exp_result);
206 }
207 
208 static int
test_main(void)209 test_main (void)
210 {
211   test_init ();
212 
213   check1 ();
214   check2 ();
215   pr23637 ();
216 
217   printf ("%23s", "");
218   FOR_EACH_IMPL (impl, 0)
219     printf ("\t%s", impl->name);
220   putchar ('\n');
221 
222   for (size_t klen = 2; klen < 32; ++klen)
223     for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
224       {
225 	do_test (0, 0, hlen, klen, 0);
226 	do_test (0, 0, hlen, klen, 1);
227 	do_test (0, 3, hlen, klen, 0);
228 	do_test (0, 3, hlen, klen, 1);
229 	do_test (0, 9, hlen, klen, 0);
230 	do_test (0, 9, hlen, klen, 1);
231 	do_test (0, 15, hlen, klen, 0);
232 	do_test (0, 15, hlen, klen, 1);
233 
234 	do_test (3, 0, hlen, klen, 0);
235 	do_test (3, 0, hlen, klen, 1);
236 	do_test (3, 3, hlen, klen, 0);
237 	do_test (3, 3, hlen, klen, 1);
238 	do_test (3, 9, hlen, klen, 0);
239 	do_test (3, 9, hlen, klen, 1);
240 	do_test (3, 15, hlen, klen, 0);
241 	do_test (3, 15, hlen, klen, 1);
242 
243 	do_test (9, 0, hlen, klen, 0);
244 	do_test (9, 0, hlen, klen, 1);
245 	do_test (9, 3, hlen, klen, 0);
246 	do_test (9, 3, hlen, klen, 1);
247 	do_test (9, 9, hlen, klen, 0);
248 	do_test (9, 9, hlen, klen, 1);
249 	do_test (9, 15, hlen, klen, 0);
250 	do_test (9, 15, hlen, klen, 1);
251 
252 	do_test (15, 0, hlen, klen, 0);
253 	do_test (15, 0, hlen, klen, 1);
254 	do_test (15, 3, hlen, klen, 0);
255 	do_test (15, 3, hlen, klen, 1);
256 	do_test (15, 9, hlen, klen, 0);
257 	do_test (15, 9, hlen, klen, 1);
258 	do_test (15, 15, hlen, klen, 0);
259 	do_test (15, 15, hlen, klen, 1);
260 
261 	do_test (15, 15, hlen + klen * 4, klen * 4, 0);
262 	do_test (15, 15, hlen + klen * 4, klen * 4, 1);
263       }
264 
265   do_test (0, 0, page_size - 1, 16, 0);
266   do_test (0, 0, page_size - 1, 16, 1);
267 
268   return ret;
269 }
270 
271 #include <support/test-driver.c>
272