1 /* Measure strncmp functions.
2    Copyright (C) 2013-2021 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 #ifdef WIDE
21 # define TEST_NAME "wcsncmp"
22 #else
23 # define TEST_NAME "strncmp"
24 #endif /* !WIDE */
25 #include "bench-string.h"
26 #include "json-lib.h"
27 
28 #ifdef WIDE
29 # define L(str) L##str
30 # define STRDUP wcsdup
31 # define SIMPLE_STRNCMP simple_wcsncmp
32 
33 /* Wcsncmp uses signed semantics for comparison, not unsigned.
34    Avoid using substraction since possible overflow.  */
35 int
simple_wcsncmp(const CHAR * s1,const CHAR * s2,size_t n)36 simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
37 {
38   wchar_t c1, c2;
39 
40   while (n--)
41     {
42       c1 = *s1++;
43       c2 = *s2++;
44       if (c1 == L ('\0') || c1 != c2)
45 	return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
46     }
47   return 0;
48 }
49 
50 #else
51 # define L(str) str
52 # define STRDUP strdup
53 # define SIMPLE_STRNCMP simple_strncmp
54 
55 /* Strncmp uses unsigned semantics for comparison.  */
56 int
simple_strncmp(const char * s1,const char * s2,size_t n)57 simple_strncmp (const char *s1, const char *s2, size_t n)
58 {
59   int ret = 0;
60 
61   while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
62 	 && *s1++);
63   return ret;
64 }
65 
66 #endif /* !WIDE */
67 
68 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
69 
70 IMPL (SIMPLE_STRNCMP, 0)
71 IMPL (STRNCMP, 1)
72 
73 
74 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,const CHAR * s1,const CHAR * s2,size_t n,int exp_result)75 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR
76 	     *s2, size_t n, int exp_result)
77 {
78   size_t i, iters = INNER_LOOP_ITERS8;
79   timing_t start, stop, cur;
80 
81   TIMING_NOW (start);
82   for (i = 0; i < iters; ++i)
83     {
84       CALL (impl, s1, s2, n);
85     }
86   TIMING_NOW (stop);
87 
88   TIMING_DIFF (cur, start, stop);
89 
90   json_element_double (json_ctx, (double) cur / (double) iters);
91 }
92 
93 static void
do_test_limit(json_ctx_t * json_ctx,size_t align1,size_t align2,size_t len,size_t n,int max_char,int exp_result)94 do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
95 	       size_t n, int max_char, int exp_result)
96 {
97   size_t i, align_n;
98   CHAR *s1, *s2;
99 
100   align1 &= 15;
101   align2 &= 15;
102   align_n = (page_size - n * CHARBYTES) & 15;
103 
104   json_element_object_begin (json_ctx);
105   json_attr_uint (json_ctx, "strlen", (double) len);
106   json_attr_uint (json_ctx, "len", (double) n);
107   json_attr_uint (json_ctx, "align1", (double) align1);
108   json_attr_uint (json_ctx, "align2", (double) align2);
109   json_array_begin (json_ctx, "timings");
110 
111   FOR_EACH_IMPL (impl, 0)
112     {
113       alloc_bufs ();
114       s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
115       s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
116 
117       if (align1 < align_n)
118 	s1 = (CHAR *) ((char *) s1 - (align_n - align1));
119 
120       if (align2 < align_n)
121 	s2 = (CHAR *) ((char *) s2 - (align_n - align2));
122 
123       for (i = 0; i < n; i++)
124 	s1[i] = s2[i] = 1 + 23 * i % max_char;
125 
126       if (len < n)
127 	{
128 	  s1[len] = 0;
129 	  s2[len] = 0;
130 	  if (exp_result < 0)
131 	    s2[len] = 32;
132 	  else if (exp_result > 0)
133 	    s1[len] = 64;
134 	}
135 
136       do_one_test (json_ctx, impl, s1, s2, n, exp_result);
137     }
138 
139   json_array_end (json_ctx);
140   json_element_object_end (json_ctx);
141 }
142 
143 static void
do_test(json_ctx_t * json_ctx,size_t align1,size_t align2,size_t len,size_t n,int max_char,int exp_result)144 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
145 	 n, int max_char, int exp_result)
146 {
147   size_t i;
148   CHAR *s1, *s2;
149 
150   if (n == 0)
151     return;
152 
153   align1 &= 63;
154   if (align1 + (n + 1) * CHARBYTES >= page_size)
155     return;
156 
157   align2 &= 7;
158   if (align2 + (n + 1) * CHARBYTES >= page_size)
159     return;
160 
161   json_element_object_begin (json_ctx);
162   json_attr_uint (json_ctx, "strlen", (double) len);
163   json_attr_uint (json_ctx, "len", (double) n);
164   json_attr_uint (json_ctx, "align1", (double) align1);
165   json_attr_uint (json_ctx, "align2", (double) align2);
166   json_array_begin (json_ctx, "timings");
167 
168   FOR_EACH_IMPL (impl, 0)
169     {
170       alloc_bufs ();
171       s1 = (CHAR *) (buf1 + align1);
172       s2 = (CHAR *) (buf2 + align2);
173 
174       for (i = 0; i < n; i++)
175 	s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
176 
177       s1[n] = 24 + exp_result;
178       s2[n] = 23;
179       s1[len] = 0;
180       s2[len] = 0;
181       if (exp_result < 0)
182 	s2[len] = 32;
183       else if (exp_result > 0)
184 	s1[len] = 64;
185       if (len >= n)
186 	s2[n - 1] -= exp_result;
187 
188       do_one_test (json_ctx, impl, s1, s2, n, exp_result);
189     }
190 
191   json_array_end (json_ctx);
192   json_element_object_end (json_ctx);
193 }
194 
195 static void
do_one_test_page_boundary(json_ctx_t * json_ctx,CHAR * s1,CHAR * s2,size_t align1,size_t align2,size_t len,size_t n,int exp_result)196 do_one_test_page_boundary (json_ctx_t *json_ctx, CHAR *s1, CHAR *s2,
197 			   size_t align1, size_t align2, size_t len,
198 			   size_t n, int exp_result)
199 {
200   json_element_object_begin (json_ctx);
201   json_attr_uint (json_ctx, "strlen", (double) len);
202   json_attr_uint (json_ctx, "len", (double) n);
203   json_attr_uint (json_ctx, "align1", (double) align1);
204   json_attr_uint (json_ctx, "align2", (double) align2);
205   json_array_begin (json_ctx, "timings");
206   FOR_EACH_IMPL (impl, 0)
207     do_one_test (json_ctx, impl, s1, s2, n, exp_result);
208   json_array_end (json_ctx);
209   json_element_object_end (json_ctx);
210 }
211 
212 static void
do_test_page_boundary(json_ctx_t * json_ctx)213 do_test_page_boundary (json_ctx_t *json_ctx)
214 {
215   /* To trigger bug 25933, we need a size that is equal to the vector
216      length times 4. In the case of AVX2 for Intel, we need 32 * 4.  We
217      make this test generic and run it for all architectures as additional
218      boundary testing for such related algorithms.  */
219   size_t size = 32 * 4;
220   size_t len;
221   CHAR *s1 = (CHAR *) (buf1 + (BUF1PAGES - 1) * page_size);
222   CHAR *s2 = (CHAR *) (buf2 + (BUF1PAGES - 1) * page_size);
223   int exp_result;
224 
225   memset (s1, 'a', page_size);
226   memset (s2, 'a', page_size);
227 
228   s1[(page_size / CHARBYTES) - 1] = (CHAR) 0;
229 
230   /* Iterate over a size that is just below where we expect the bug to
231      trigger up to the size we expect will trigger the bug e.g. [99-128].
232      Likewise iterate the start of two strings between 30 and 31 bytes
233      away from the boundary to simulate alignment changes.  */
234   for (size_t s = 99; s <= size; s++)
235     for (size_t s1a = 30; s1a < 32; s1a++)
236       for (size_t s2a = 30; s2a < 32; s2a++)
237 	{
238 	  size_t align1 = (page_size / CHARBYTES - s) - s1a;
239 	  size_t align2 = (page_size / CHARBYTES - s) - s2a;
240 	  CHAR *s1p = s1 + align1;
241 	  CHAR *s2p = s2 + align2;
242 	  len = (page_size / CHARBYTES) - 1 - align1;
243 	  exp_result = SIMPLE_STRNCMP (s1p, s2p, s);
244 	  do_one_test_page_boundary (json_ctx, s1p, s2p, align1, align2,
245 				     len, s, exp_result);
246 	}
247 }
248 
249 static void
do_one_test_page(json_ctx_t * json_ctx,size_t offset1,size_t offset2,CHAR * s2)250 do_one_test_page (json_ctx_t *json_ctx, size_t offset1, size_t offset2,
251 		  CHAR *s2)
252 {
253   CHAR *s1;
254   int exp_result;
255 
256   if (offset1 * CHARBYTES  >= page_size
257       || offset2 * CHARBYTES >= page_size)
258     return;
259 
260   s1 = (CHAR *) buf1;
261   s1 += offset1;
262   s2 += offset2;
263 
264   size_t len = (page_size / CHARBYTES) - offset1;
265 
266   exp_result= *s1;
267 
268   json_element_object_begin (json_ctx);
269   json_attr_uint (json_ctx, "strlen", (double) len);
270   json_attr_uint (json_ctx, "len", (double) page_size);
271   json_attr_uint (json_ctx, "align1", (double) offset1);
272   json_attr_uint (json_ctx, "align2", (double) offset2);
273   json_array_begin (json_ctx, "timings");
274   {
275     FOR_EACH_IMPL (impl, 0)
276       do_one_test (json_ctx, impl, s1, s2, page_size, -exp_result);
277   }
278   json_array_end (json_ctx);
279   json_element_object_end (json_ctx);
280 
281   json_element_object_begin (json_ctx);
282   json_attr_uint (json_ctx, "strlen", (double) len);
283   json_attr_uint (json_ctx, "len", (double) page_size);
284   json_attr_uint (json_ctx, "align1", (double) offset1);
285   json_attr_uint (json_ctx, "align2", (double) offset2);
286   json_array_begin (json_ctx, "timings");
287   {
288     FOR_EACH_IMPL (impl, 0)
289       do_one_test (json_ctx, impl, s1, s2, page_size, exp_result);
290   }
291   json_array_end (json_ctx);
292   json_element_object_end (json_ctx);
293 }
294 
295 static void
do_test_page(json_ctx_t * json_ctx)296 do_test_page (json_ctx_t *json_ctx)
297 {
298   size_t i;
299   CHAR *s1, *s2;
300 
301   s1 = (CHAR *) buf1;
302   /* Fill buf1 with 23. */
303   for (i = 0; i < (page_size / CHARBYTES) - 1; i++)
304     s1[i] = 23;
305   s1[i] = 0;
306 
307   /* Make a copy of buf1.  */
308   s2 = STRDUP (s1);
309 
310   /* Test should terminate within the page boundary.  */
311   for (i = 0; i < (108 / CHARBYTES); ++i)
312     do_one_test_page (json_ctx, ((page_size - 108) / CHARBYTES) + i,
313 		      ((page_size - 1460) / CHARBYTES), s2);
314 
315   free (s2);
316 }
317 
318 int
test_main(void)319 test_main (void)
320 {
321   json_ctx_t json_ctx;
322   size_t i;
323 
324   test_init ();
325 
326   json_init (&json_ctx, 0, stdout);
327 
328   json_document_begin (&json_ctx);
329   json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
330 
331   json_attr_object_begin (&json_ctx, "functions");
332   json_attr_object_begin (&json_ctx, TEST_NAME);
333   json_attr_string (&json_ctx, "bench-variant", "default");
334 
335   json_array_begin (&json_ctx, "ifuncs");
336   FOR_EACH_IMPL (impl, 0)
337     json_element_string (&json_ctx, impl->name);
338   json_array_end (&json_ctx);
339 
340   json_array_begin (&json_ctx, "results");
341 
342   for (i =0; i < 16; ++i)
343     {
344       do_test (&json_ctx, 0, 0, 8, i, 127, 0);
345       do_test (&json_ctx, 0, 0, 8, i, 127, -1);
346       do_test (&json_ctx, 0, 0, 8, i, 127, 1);
347       do_test (&json_ctx, i, i, 8, i, 127, 0);
348       do_test (&json_ctx, i, i, 8, i, 127, 1);
349       do_test (&json_ctx, i, i, 8, i, 127, -1);
350       do_test (&json_ctx, i, 2 * i, 8, i, 127, 0);
351       do_test (&json_ctx, 2 * i, i, 8, i, 127, 1);
352       do_test (&json_ctx, i, 3 * i, 8, i, 127, -1);
353       do_test (&json_ctx, 0, 0, 8, i, 255, 0);
354       do_test (&json_ctx, 0, 0, 8, i, 255, -1);
355       do_test (&json_ctx, 0, 0, 8, i, 255, 1);
356       do_test (&json_ctx, i, i, 8, i, 255, 0);
357       do_test (&json_ctx, i, i, 8, i, 255, 1);
358       do_test (&json_ctx, i, i, 8, i, 255, -1);
359       do_test (&json_ctx, i, 2 * i, 8, i, 255, 0);
360       do_test (&json_ctx, 2 * i, i, 8, i, 255, 1);
361       do_test (&json_ctx, i, 3 * i, 8, i, 255, -1);
362     }
363 
364   for (i = 1; i < 8; ++i)
365     {
366       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0);
367       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 1);
368       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, -1);
369       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 0);
370       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 1);
371       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, -1);
372       do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
373       do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
374       do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 0);
375       do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 1);
376     }
377 
378   do_test_limit (&json_ctx, 4, 0, 21, 20, 127, 0);
379   do_test_limit (&json_ctx, 0, 4, 21, 20, 127, 0);
380   do_test_limit (&json_ctx, 8, 0, 25, 24, 127, 0);
381   do_test_limit (&json_ctx, 0, 8, 25, 24, 127, 0);
382 
383   for (i = 0; i < 8; ++i)
384     {
385       do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 127, 0);
386       do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 255, 0);
387       do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 0);
388       do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 1);
389       do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, -1);
390       do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 0);
391       do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 1);
392       do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, -1);
393     }
394 
395   do_test_page_boundary (&json_ctx);
396   do_test_page (&json_ctx);
397 
398   json_array_end (&json_ctx);
399   json_attr_object_end (&json_ctx);
400   json_attr_object_end (&json_ctx);
401   json_document_end (&json_ctx);
402 
403   return ret;
404 }
405 
406 #include <support/test-driver.c>
407