Update copyright notices with scripts/update-copyrights
[platform/upstream/glibc.git] / debug / test-strcpy_chk.c
1 /* Test and measure __strcpy_chk functions.
2    Copyright (C) 1999-2014 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
115 static void
116 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
117 {
118   size_t i;
119   char *s1, *s2;
120
121   align1 &= 7;
122   if (align1 + len >= page_size)
123     return;
124
125   align2 &= 7;
126   if (align2 + len >= page_size)
127     return;
128
129   s1 = (char *) buf1 + align1;
130   s2 = (char *) buf2 + align2;
131
132   for (i = 0; i < len; i++)
133     s1[i] = 32 + 23 * i % (max_char - 32);
134   s1[len] = 0;
135
136   FOR_EACH_IMPL (impl, 0)
137     do_one_test (impl, s2, s1, len, dlen);
138 }
139
140 static void
141 do_random_tests (void)
142 {
143   size_t i, j, n, align1, align2, len, dlen;
144   unsigned char *p1 = buf1 + page_size - 512;
145   unsigned char *p2 = buf2 + page_size - 512;
146   unsigned char *res;
147
148   for (n = 0; n < ITERATIONS; n++)
149     {
150       align1 = random () & 31;
151       if (random () & 1)
152         align2 = random () & 31;
153       else
154         align2 = align1 + (random () & 24);
155       len = random () & 511;
156       j = align1;
157       if (align2 > j)
158         j = align2;
159       if (len + j >= 511)
160         len = 510 - j - (random () & 7);
161       j = len + align1 + 64;
162       if (j > 512)
163         j = 512;
164       for (i = 0; i < j; i++)
165         {
166           if (i == len + align1)
167             p1[i] = 0;
168           else
169             {
170               p1[i] = random () & 255;
171               if (i >= align1 && i < len + align1 && !p1[i])
172                 p1[i] = (random () & 127) + 3;
173             }
174         }
175
176       switch (random () & 7)
177         {
178         case 0:
179           dlen = len - (random () & 31);
180           if (dlen > len)
181             dlen = len;
182           break;
183         case 1:
184           dlen = (size_t) -1;
185           break;
186         case 2:
187           dlen = len + 1 + (random () & 65535);
188           break;
189         case 3:
190           dlen = len + 1 + (random () & 255);
191           break;
192         case 4:
193           dlen = len + 1 + (random () & 31);
194           break;
195         case 5:
196           dlen = len + 1 + (random () & 7);
197           break;
198         case 6:
199           dlen = len + 1 + (random () & 3);
200           break;
201         default:
202           dlen = len + 1;
203           break;
204         }
205
206       FOR_EACH_IMPL (impl, 1)
207         {
208           if (dlen <= len)
209             {
210               if (impl->test != 1)
211                 {
212                   chk_fail_ok = 1;
213                   if (setjmp (chk_fail_buf) == 0)
214                     {
215                       res = (unsigned char *)
216                             CALL (impl, (char *) p2 + align2,
217                                   (char *) p1 + align1, dlen);
218                       printf ("Iteration %zd - did not __chk_fail\n", n);
219                       chk_fail_ok = 0;
220                       ret = 1;
221                     }
222                 }
223               continue;
224             }
225           memset (p2 - 64, '\1', 512 + 64);
226           res = (unsigned char *)
227                 CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
228           if (res != STRCPY_RESULT (p2 + align2, len))
229             {
230               printf ("\
231 Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
232                       n, impl->name, align1, align2, len, res,
233                       STRCPY_RESULT (p2 + align2, len));
234               ret = 1;
235             }
236           for (j = 0; j < align2 + 64; ++j)
237             {
238               if (p2[j - 64] != '\1')
239                 {
240                   printf ("\
241 Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
242                           n, impl->name, align1, align2, len);
243                   ret = 1;
244                   break;
245                 }
246             }
247           for (j = align2 + len + 1; j < 512; ++j)
248             {
249               if (p2[j] != '\1')
250                 {
251                   printf ("\
252 Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
253                           n, impl->name, align1, align2, len);
254                   ret = 1;
255                   break;
256                 }
257             }
258           if (memcmp (p1 + align1, p2 + align2, len + 1))
259             {
260               printf ("\
261 Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
262                       n, impl->name, align1, align2, len);
263               ret = 1;
264             }
265         }
266     }
267 }
268
269 int
270 test_main (void)
271 {
272   size_t i;
273
274   struct sigaction sa;
275   sa.sa_handler = handler;
276   sa.sa_flags = 0;
277   sigemptyset (&sa.sa_mask);
278
279   sigaction (SIGABRT, &sa, NULL);
280
281   /* Avoid all the buffer overflow messages on stderr.  */
282   int fd = open (_PATH_DEVNULL, O_WRONLY);
283   if (fd == -1)
284     close (STDERR_FILENO);
285   else
286     {
287       dup2 (fd, STDERR_FILENO);
288       close (fd);
289     }
290   setenv ("LIBC_FATAL_STDERR_", "1", 1);
291
292   test_init ();
293
294   printf ("%23s", "");
295   FOR_EACH_IMPL (impl, 0)
296     printf ("\t%s", impl->name);
297   putchar ('\n');
298
299   for (i = 0; i < 16; ++i)
300     {
301       do_test (0, 0, i, i + 1, 127);
302       do_test (0, 0, i, i + 1, 255);
303       do_test (0, i, i, i + 1, 127);
304       do_test (i, 0, i, i + 1, 255);
305     }
306
307   for (i = 1; i < 8; ++i)
308     {
309       do_test (0, 0, 8 << i, (8 << i) + 1, 127);
310       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
311     }
312
313   for (i = 1; i < 8; ++i)
314     {
315       do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
316       do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
317       do_test (i, i, (8 << i), (8 << i) + 1, 127);
318       do_test (i, i, (8 << i), (8 << i) + 1, 255);
319     }
320
321   for (i = 0; i < 16; ++i)
322     {
323       do_test (0, 0, i, i + 256, 127);
324       do_test (0, 0, i, i + 256, 255);
325       do_test (0, i, i, i + 256, 127);
326       do_test (i, 0, i, i + 256, 255);
327     }
328
329   for (i = 1; i < 8; ++i)
330     {
331       do_test (0, 0, 8 << i, (8 << i) + 256, 127);
332       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
333     }
334
335   for (i = 1; i < 8; ++i)
336     {
337       do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
338       do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
339       do_test (i, i, (8 << i), (8 << i) + 256, 127);
340       do_test (i, i, (8 << i), (8 << i) + 256, 255);
341     }
342
343   for (i = 0; i < 16; ++i)
344     {
345       do_test (0, 0, i, i, 127);
346       do_test (0, 0, i, i + 2, 255);
347       do_test (0, i, i, i + 3, 127);
348       do_test (i, 0, i, i + 4, 255);
349     }
350
351   for (i = 1; i < 8; ++i)
352     {
353       do_test (0, 0, 8 << i, (8 << i) - 15, 127);
354       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
355     }
356
357   for (i = 1; i < 8; ++i)
358     {
359       do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
360       do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
361       do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
362       do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
363     }
364
365   do_random_tests ();
366   return ret;
367 }
368
369 #include "../test-skeleton.c"