Mention that __chk_fail is a public ABI in test-strcpy_chk.c
[platform/upstream/glibc.git] / debug / test-strcpy_chk.c
1 /* Test and measure __strcpy_chk functions.
2    Copyright (C) 1999-2012 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, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #ifndef STRCPY_RESULT
21 # define STRCPY_RESULT(dst, len) dst
22 # define TEST_MAIN
23 # define TEST_NAME "strcpy_chk"
24 # include "../string/test-string.h"
25
26 /* This test case implicitly tests the availability of the __chk_fail
27    symbol, which is part of the public ABI and may be used
28    externally. */
29 extern void __attribute__ ((noreturn)) __chk_fail (void);
30 char *simple_strcpy_chk (char *, const char *, size_t);
31 extern char *normal_strcpy (char *, const char *, size_t)
32   __asm ("strcpy");
33 extern char *__strcpy_chk (char *, const char *, size_t);
34
35 IMPL (simple_strcpy_chk, 0)
36 IMPL (normal_strcpy, 1)
37 IMPL (__strcpy_chk, 2)
38
39 char *
40 simple_strcpy_chk (char *dst, const char *src, size_t len)
41 {
42   char *ret = dst;
43   if (! len)
44     __chk_fail ();
45   while ((*dst++ = *src++) != '\0')
46     if (--len == 0)
47       __chk_fail ();
48   return ret;
49 }
50 #endif
51
52 #include <fcntl.h>
53 #include <paths.h>
54 #include <setjmp.h>
55 #include <signal.h>
56
57 volatile int chk_fail_ok;
58 jmp_buf chk_fail_buf;
59
60 static void
61 handler (int sig)
62 {
63   if (chk_fail_ok)
64     {
65       chk_fail_ok = 0;
66       longjmp (chk_fail_buf, 1);
67     }
68   else
69     _exit (127);
70 }
71
72 typedef char *(*proto_t) (char *, const char *, size_t);
73
74 static void
75 do_one_test (impl_t *impl, char *dst, const char *src,
76              size_t len, size_t dlen)
77 {
78   char *res;
79   if (dlen <= len)
80     {
81       if (impl->test == 1)
82         return;
83
84       chk_fail_ok = 1;
85       if (setjmp (chk_fail_buf) == 0)
86         {
87           res = CALL (impl, dst, src, dlen);
88           printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
89                   impl->name, len, dlen);
90           chk_fail_ok = 0;
91           ret = 1;
92         }
93       return;
94     }
95   else
96     res = CALL (impl, dst, src, dlen);
97
98   if (res != STRCPY_RESULT (dst, len))
99     {
100       printf ("Wrong result in function %s %p %p\n", impl->name,
101               res, STRCPY_RESULT (dst, len));
102       ret = 1;
103       return;
104     }
105
106   if (strcmp (dst, src) != 0)
107     {
108       printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
109               impl->name, dst, src);
110       ret = 1;
111       return;
112     }
113
114   if (HP_TIMING_AVAIL)
115     {
116       hp_timing_t start __attribute ((unused));
117       hp_timing_t stop __attribute ((unused));;
118       hp_timing_t best_time = ~ (hp_timing_t) 0;
119       size_t i;
120
121       for (i = 0; i < 32; ++i)
122         {
123           HP_TIMING_NOW (start);
124           CALL (impl, dst, src, dlen);
125           HP_TIMING_NOW (stop);
126           HP_TIMING_BEST (best_time, start, stop);
127         }
128
129       printf ("\t%zd", (size_t) best_time);
130     }
131 }
132
133 static void
134 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
135 {
136   size_t i;
137   char *s1, *s2;
138
139   align1 &= 7;
140   if (align1 + len >= page_size)
141     return;
142
143   align2 &= 7;
144   if (align2 + len >= page_size)
145     return;
146
147   s1 = (char *) buf1 + align1;
148   s2 = (char *) buf2 + align2;
149
150   for (i = 0; i < len; i++)
151     s1[i] = 32 + 23 * i % (max_char - 32);
152   s1[len] = 0;
153
154   if (HP_TIMING_AVAIL && dlen > len)
155     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
156
157   FOR_EACH_IMPL (impl, 0)
158     do_one_test (impl, s2, s1, len, dlen);
159
160   if (HP_TIMING_AVAIL && dlen > len)
161     putchar ('\n');
162 }
163
164 static void
165 do_random_tests (void)
166 {
167   size_t i, j, n, align1, align2, len, dlen;
168   unsigned char *p1 = buf1 + page_size - 512;
169   unsigned char *p2 = buf2 + page_size - 512;
170   unsigned char *res;
171
172   for (n = 0; n < ITERATIONS; n++)
173     {
174       align1 = random () & 31;
175       if (random () & 1)
176         align2 = random () & 31;
177       else
178         align2 = align1 + (random () & 24);
179       len = random () & 511;
180       j = align1;
181       if (align2 > j)
182         j = align2;
183       if (len + j >= 511)
184         len = 510 - j - (random () & 7);
185       j = len + align1 + 64;
186       if (j > 512)
187         j = 512;
188       for (i = 0; i < j; i++)
189         {
190           if (i == len + align1)
191             p1[i] = 0;
192           else
193             {
194               p1[i] = random () & 255;
195               if (i >= align1 && i < len + align1 && !p1[i])
196                 p1[i] = (random () & 127) + 3;
197             }
198         }
199
200       switch (random () & 7)
201         {
202         case 0:
203           dlen = len - (random () & 31);
204           if (dlen > len)
205             dlen = len;
206           break;
207         case 1:
208           dlen = (size_t) -1;
209           break;
210         case 2:
211           dlen = len + 1 + (random () & 65535);
212           break;
213         case 3:
214           dlen = len + 1 + (random () & 255);
215           break;
216         case 4:
217           dlen = len + 1 + (random () & 31);
218           break;
219         case 5:
220           dlen = len + 1 + (random () & 7);
221           break;
222         case 6:
223           dlen = len + 1 + (random () & 3);
224           break;
225         default:
226           dlen = len + 1;
227           break;
228         }
229
230       FOR_EACH_IMPL (impl, 1)
231         {
232           if (dlen <= len)
233             {
234               if (impl->test != 1)
235                 {
236                   chk_fail_ok = 1;
237                   if (setjmp (chk_fail_buf) == 0)
238                     {
239                       res = (unsigned char *)
240                             CALL (impl, (char *) p2 + align2,
241                                   (char *) p1 + align1, dlen);
242                       printf ("Iteration %zd - did not __chk_fail\n", n);
243                       chk_fail_ok = 0;
244                       ret = 1;
245                     }
246                 }
247               continue;
248             }
249           memset (p2 - 64, '\1', 512 + 64);
250           res = (unsigned char *)
251                 CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
252           if (res != STRCPY_RESULT (p2 + align2, len))
253             {
254               printf ("\
255 Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
256                       n, impl->name, align1, align2, len, res,
257                       STRCPY_RESULT (p2 + align2, len));
258               ret = 1;
259             }
260           for (j = 0; j < align2 + 64; ++j)
261             {
262               if (p2[j - 64] != '\1')
263                 {
264                   printf ("\
265 Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
266                           n, impl->name, align1, align2, len);
267                   ret = 1;
268                   break;
269                 }
270             }
271           for (j = align2 + len + 1; j < 512; ++j)
272             {
273               if (p2[j] != '\1')
274                 {
275                   printf ("\
276 Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
277                           n, impl->name, align1, align2, len);
278                   ret = 1;
279                   break;
280                 }
281             }
282           if (memcmp (p1 + align1, p2 + align2, len + 1))
283             {
284               printf ("\
285 Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
286                       n, impl->name, align1, align2, len);
287               ret = 1;
288             }
289         }
290     }
291 }
292
293 int
294 test_main (void)
295 {
296   size_t i;
297
298   struct sigaction sa;
299   sa.sa_handler = handler;
300   sa.sa_flags = 0;
301   sigemptyset (&sa.sa_mask);
302
303   sigaction (SIGABRT, &sa, NULL);
304
305   /* Avoid all the buffer overflow messages on stderr.  */
306   int fd = open (_PATH_DEVNULL, O_WRONLY);
307   if (fd == -1)
308     close (STDERR_FILENO);
309   else
310     {
311       dup2 (fd, STDERR_FILENO);
312       close (fd);
313     }
314   setenv ("LIBC_FATAL_STDERR_", "1", 1);
315
316   test_init ();
317
318   printf ("%23s", "");
319   FOR_EACH_IMPL (impl, 0)
320     printf ("\t%s", impl->name);
321   putchar ('\n');
322
323   for (i = 0; i < 16; ++i)
324     {
325       do_test (0, 0, i, i + 1, 127);
326       do_test (0, 0, i, i + 1, 255);
327       do_test (0, i, i, i + 1, 127);
328       do_test (i, 0, i, i + 1, 255);
329     }
330
331   for (i = 1; i < 8; ++i)
332     {
333       do_test (0, 0, 8 << i, (8 << i) + 1, 127);
334       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
335     }
336
337   for (i = 1; i < 8; ++i)
338     {
339       do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
340       do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
341       do_test (i, i, (8 << i), (8 << i) + 1, 127);
342       do_test (i, i, (8 << i), (8 << i) + 1, 255);
343     }
344
345   for (i = 0; i < 16; ++i)
346     {
347       do_test (0, 0, i, i + 256, 127);
348       do_test (0, 0, i, i + 256, 255);
349       do_test (0, i, i, i + 256, 127);
350       do_test (i, 0, i, i + 256, 255);
351     }
352
353   for (i = 1; i < 8; ++i)
354     {
355       do_test (0, 0, 8 << i, (8 << i) + 256, 127);
356       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
357     }
358
359   for (i = 1; i < 8; ++i)
360     {
361       do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
362       do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
363       do_test (i, i, (8 << i), (8 << i) + 256, 127);
364       do_test (i, i, (8 << i), (8 << i) + 256, 255);
365     }
366
367   for (i = 0; i < 16; ++i)
368     {
369       do_test (0, 0, i, i, 127);
370       do_test (0, 0, i, i + 2, 255);
371       do_test (0, i, i, i + 3, 127);
372       do_test (i, 0, i, i + 4, 255);
373     }
374
375   for (i = 1; i < 8; ++i)
376     {
377       do_test (0, 0, 8 << i, (8 << i) - 15, 127);
378       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
379     }
380
381   for (i = 1; i < 8; ++i)
382     {
383       do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
384       do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
385       do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
386       do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
387     }
388
389   do_random_tests ();
390   return ret;
391 }
392
393 #include "../test-skeleton.c"