2004-10-15 Jakub Jelinek <jakub@redhat.com>
[platform/upstream/glibc.git] / debug / test-strcpy_chk.c
1 /* Test and measure __strcpy_chk functions.
2    Copyright (C) 1999, 2002, 2003, 2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #ifndef STRCPY_RESULT
22 # define STRCPY_RESULT(dst, len) dst
23 # define TEST_MAIN
24 # include "../string/test-string.h"
25
26 extern void __attribute__ ((noreturn)) __chk_fail (void);
27 char *simple_strcpy_chk (char *, const char *, size_t);
28 extern char *normal_strcpy (char *, const char *, size_t)
29   __asm ("strcpy");
30 extern char *__strcpy_chk (char *, const char *, size_t);
31
32 IMPL (simple_strcpy_chk, 0)
33 IMPL (normal_strcpy, 1)
34 IMPL (__strcpy_chk, 2)
35
36 char *
37 simple_strcpy_chk (char *dst, const char *src, size_t len)
38 {
39   char *ret = dst;
40   if (! len)
41     __chk_fail ();
42   while ((*dst++ = *src++) != '\0')
43     if (--len == 0)
44       __chk_fail ();
45   return ret;
46 }
47 #endif
48
49 #include <setjmp.h>
50 #include <signal.h>
51
52 volatile int chk_fail_ok;
53 jmp_buf chk_fail_buf;
54
55 static void
56 handler (int sig)
57 {
58   if (chk_fail_ok)
59     {
60       chk_fail_ok = 0;
61       longjmp (chk_fail_buf, 1);
62     }
63   else
64     _exit (127);
65 }
66
67 typedef char *(*proto_t) (char *, const char *, size_t);
68
69 static void
70 do_one_test (impl_t *impl, char *dst, const char *src,
71              size_t len, size_t dlen)
72 {
73   char *res;
74   if (dlen <= len)
75     {
76       if (impl->test == 1)
77         return;
78
79       chk_fail_ok = 1;
80       if (setjmp (chk_fail_buf) == 0)
81         {
82           res = CALL (impl, dst, src, dlen);
83           error (0, 0, "Function %s (%zd; %zd) did not __chk_fail",
84                  impl->name, len, dlen);
85           chk_fail_ok = 0;
86           ret = 1;
87         }
88       return;
89     }
90   else
91     res = CALL (impl, dst, src, dlen);
92
93   if (res != STRCPY_RESULT (dst, len))
94     {
95       error (0, 0, "Wrong result in function %s %p %p", impl->name,
96              res, STRCPY_RESULT (dst, len));
97       ret = 1;
98       return;
99     }
100
101   if (strcmp (dst, src) != 0)
102     {
103       error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
104              impl->name, dst, src);
105       ret = 1;
106       return;
107     }
108
109   if (HP_TIMING_AVAIL)
110     {
111       hp_timing_t start __attribute ((unused));
112       hp_timing_t stop __attribute ((unused));;
113       hp_timing_t best_time = ~ (hp_timing_t) 0;
114       size_t i;
115
116       for (i = 0; i < 32; ++i)
117         {
118           HP_TIMING_NOW (start);
119           CALL (impl, dst, src, dlen);
120           HP_TIMING_NOW (stop);
121           HP_TIMING_BEST (best_time, start, stop);
122         }
123
124       printf ("\t%zd", (size_t) best_time);
125     }
126 }
127
128 static void
129 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
130 {
131   size_t i;
132   char *s1, *s2;
133
134   align1 &= 7;
135   if (align1 + len >= page_size)
136     return;
137
138   align2 &= 7;
139   if (align2 + len >= page_size)
140     return;
141
142   s1 = buf1 + align1;
143   s2 = buf2 + align2;
144
145   for (i = 0; i < len; i++)
146     s1[i] = 32 + 23 * i % (max_char - 32);
147   s1[len] = 0;
148
149   if (HP_TIMING_AVAIL && dlen > len)
150     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
151
152   FOR_EACH_IMPL (impl, 0)
153     do_one_test (impl, s2, s1, len, dlen);
154
155   if (HP_TIMING_AVAIL && dlen > len)
156     putchar ('\n');
157 }
158
159 static void
160 do_random_tests (void)
161 {
162   size_t i, j, n, align1, align2, len, dlen;
163   unsigned char *p1 = buf1 + page_size - 512;
164   unsigned char *p2 = buf2 + page_size - 512;
165   unsigned char *res;
166
167   for (n = 0; n < ITERATIONS; n++)
168     {
169       align1 = random () & 31;
170       if (random () & 1)
171         align2 = random () & 31;
172       else
173         align2 = align1 + (random () & 24);
174       len = random () & 511;
175       j = align1;
176       if (align2 > j)
177         j = align2;
178       if (len + j >= 511)
179         len = 510 - j - (random () & 7);
180       j = len + align1 + 64;
181       if (j > 512)
182         j = 512;
183       for (i = 0; i < j; i++)
184         {
185           if (i == len + align1)
186             p1[i] = 0;
187           else
188             {
189               p1[i] = random () & 255;
190               if (i >= align1 && i < len + align1 && !p1[i])
191                 p1[i] = (random () & 127) + 3;
192             }
193         }
194
195       switch (random () & 7)
196         {
197         case 0:
198           dlen = len - (random () & 31);
199           if (dlen > len)
200             dlen = len;
201           break;
202         case 1:
203           dlen = (size_t) -1;
204           break;
205         case 2:
206           dlen = len + 1 + (random () & 65535);
207           break;
208         case 3:
209           dlen = len + 1 + (random () & 255);
210           break;
211         case 4:
212           dlen = len + 1 + (random () & 31);
213           break;
214         case 5:
215           dlen = len + 1 + (random () & 7);
216           break;
217         case 6:
218           dlen = len + 1 + (random () & 3);
219           break;
220         default:
221           dlen = len + 1;
222           break;
223         }
224
225       FOR_EACH_IMPL (impl, 1)
226         {
227           if (dlen <= len)
228             {
229               if (impl->test != 1)
230                 {
231                   chk_fail_ok = 1;
232                   if (setjmp (chk_fail_buf) == 0)
233                     {
234                       res = CALL (impl, p2 + align2, p1 + align1, dlen);
235                       error (0, 0, "Iteration %zd - did not __chk_fail", n);
236                       chk_fail_ok = 0;
237                       ret = 1;
238                     }
239                 }
240               continue;
241             }
242           memset (p2 - 64, '\1', 512 + 64);
243           res = CALL (impl, p2 + align2, p1 + align1, dlen);
244           if (res != STRCPY_RESULT (p2 + align2, len))
245             {
246               error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
247                      n, impl->name, align1, align2, len, res,
248                      STRCPY_RESULT (p2 + align2, len));
249               ret = 1;
250             }
251           for (j = 0; j < align2 + 64; ++j)
252             {
253               if (p2[j - 64] != '\1')
254                 {
255                   error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
256                          n, impl->name, align1, align2, len);
257                   ret = 1;
258                   break;
259                 }
260             }
261           for (j = align2 + len + 1; j < 512; ++j)
262             {
263               if (p2[j] != '\1')
264                 {
265                   error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
266                          n, impl->name, align1, align2, len);
267                   ret = 1;
268                   break;
269                 }
270             }
271           if (memcmp (p1 + align1, p2 + align2, len + 1))
272             {
273               error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
274                      n, impl->name, align1, align2, len);
275               ret = 1;
276             }
277         }
278     }
279 }
280
281 int
282 test_main (void)
283 {
284   size_t i;
285
286   struct sigaction sa;
287   sa.sa_handler = handler;
288   sa.sa_flags = 0;
289   sigemptyset (&sa.sa_mask);
290
291   sigaction (SIGABRT, &sa, NULL);
292
293   test_init ();
294
295   printf ("%23s", "");
296   FOR_EACH_IMPL (impl, 0)
297     printf ("\t%s", impl->name);
298   putchar ('\n');
299
300   for (i = 0; i < 16; ++i)
301     {
302       do_test (0, 0, i, i + 1, 127);
303       do_test (0, 0, i, i + 1, 255);
304       do_test (0, i, i, i + 1, 127);
305       do_test (i, 0, i, i + 1, 255);
306     }
307
308   for (i = 1; i < 8; ++i)
309     {
310       do_test (0, 0, 8 << i, (8 << i) + 1, 127);
311       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
312     }
313
314   for (i = 1; i < 8; ++i)
315     {
316       do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
317       do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
318       do_test (i, i, (8 << i), (8 << i) + 1, 127);
319       do_test (i, i, (8 << i), (8 << i) + 1, 255);
320     }
321
322   for (i = 0; i < 16; ++i)
323     {
324       do_test (0, 0, i, i + 256, 127);
325       do_test (0, 0, i, i + 256, 255);
326       do_test (0, i, i, i + 256, 127);
327       do_test (i, 0, i, i + 256, 255);
328     }
329
330   for (i = 1; i < 8; ++i)
331     {
332       do_test (0, 0, 8 << i, (8 << i) + 256, 127);
333       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
334     }
335
336   for (i = 1; i < 8; ++i)
337     {
338       do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
339       do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
340       do_test (i, i, (8 << i), (8 << i) + 256, 127);
341       do_test (i, i, (8 << i), (8 << i) + 256, 255);
342     }
343
344   for (i = 0; i < 16; ++i)
345     {
346       do_test (0, 0, i, i, 127);
347       do_test (0, 0, i, i + 2, 255);
348       do_test (0, i, i, i + 3, 127);
349       do_test (i, 0, i, i + 4, 255);
350     }
351
352   for (i = 1; i < 8; ++i)
353     {
354       do_test (0, 0, 8 << i, (8 << i) - 15, 127);
355       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
356     }
357
358   for (i = 1; i < 8; ++i)
359     {
360       do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
361       do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
362       do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
363       do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
364     }
365
366   do_random_tests ();
367   return ret;
368 }
369
370 #include "../test-skeleton.c"