Increase buffer size due to warning from ToT GCC
[glibc.git] / debug / test-strcpy_chk.c
blob8b729bd6ba1fe9941ff69895fe2756e3c68c5ab1
1 /* Test and measure __strcpy_chk functions.
2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
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.
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.
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/>. */
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"
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);
35 IMPL (simple_strcpy_chk, 0)
36 IMPL (normal_strcpy, 1)
37 IMPL (__strcpy_chk, 2)
39 char *
40 simple_strcpy_chk (char *dst, const char *src, size_t len)
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;
50 #endif
52 #include <fcntl.h>
53 #include <paths.h>
54 #include <setjmp.h>
55 #include <signal.h>
57 static int test_main (void);
58 #define TEST_FUNCTION test_main
59 #include <support/test-driver.c>
60 #include <support/support.h>
62 volatile int chk_fail_ok;
63 jmp_buf chk_fail_buf;
65 static void
66 handler (int sig)
68 if (chk_fail_ok)
70 chk_fail_ok = 0;
71 longjmp (chk_fail_buf, 1);
73 else
74 _exit (127);
77 typedef char *(*proto_t) (char *, const char *, size_t);
79 static void
80 do_one_test (impl_t *impl, char *dst, const char *src,
81 size_t len, size_t dlen)
83 char *res;
84 if (dlen <= len)
86 if (impl->test == 1)
87 return;
89 chk_fail_ok = 1;
90 if (setjmp (chk_fail_buf) == 0)
92 res = CALL (impl, dst, src, dlen);
93 printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
94 impl->name, len, dlen);
95 chk_fail_ok = 0;
96 ret = 1;
98 return;
100 else
101 res = CALL (impl, dst, src, dlen);
103 if (res != STRCPY_RESULT (dst, len))
105 printf ("Wrong result in function %s %p %p\n", impl->name,
106 res, STRCPY_RESULT (dst, len));
107 ret = 1;
108 return;
111 if (strcmp (dst, src) != 0)
113 printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
114 impl->name, dst, src);
115 ret = 1;
116 return;
120 static void
121 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
123 size_t i;
124 char *s1, *s2;
126 align1 &= 7;
127 if (align1 + len >= page_size)
128 return;
130 align2 &= 7;
131 if (align2 + len >= page_size)
132 return;
134 s1 = (char *) buf1 + align1;
135 s2 = (char *) buf2 + align2;
137 for (i = 0; i < len; i++)
138 s1[i] = 32 + 23 * i % (max_char - 32);
139 s1[len] = 0;
141 FOR_EACH_IMPL (impl, 0)
142 do_one_test (impl, s2, s1, len, dlen);
145 static void
146 do_random_tests (void)
148 size_t i, j, n, align1, align2, len, dlen;
149 unsigned char *p1 = buf1 + page_size - 512;
150 unsigned char *p2 = buf2 + page_size - 512;
151 unsigned char *res;
153 for (n = 0; n < ITERATIONS; n++)
155 align1 = random () & 31;
156 if (random () & 1)
157 align2 = random () & 31;
158 else
159 align2 = align1 + (random () & 24);
160 len = random () & 511;
161 j = align1;
162 if (align2 > j)
163 j = align2;
164 if (len + j >= 511)
165 len = 510 - j - (random () & 7);
166 j = len + align1 + 64;
167 if (j > 512)
168 j = 512;
169 for (i = 0; i < j; i++)
171 if (i == len + align1)
172 p1[i] = 0;
173 else
175 p1[i] = random () & 255;
176 if (i >= align1 && i < len + align1 && !p1[i])
177 p1[i] = (random () & 127) + 3;
181 switch (random () & 7)
183 case 0:
184 dlen = len - (random () & 31);
185 if (dlen > len)
186 dlen = len;
187 break;
188 case 1:
189 dlen = (size_t) -1;
190 break;
191 case 2:
192 dlen = len + 1 + (random () & 65535);
193 break;
194 case 3:
195 dlen = len + 1 + (random () & 255);
196 break;
197 case 4:
198 dlen = len + 1 + (random () & 31);
199 break;
200 case 5:
201 dlen = len + 1 + (random () & 7);
202 break;
203 case 6:
204 dlen = len + 1 + (random () & 3);
205 break;
206 default:
207 dlen = len + 1;
208 break;
211 FOR_EACH_IMPL (impl, 1)
213 if (dlen <= len)
215 if (impl->test != 1)
217 chk_fail_ok = 1;
218 if (setjmp (chk_fail_buf) == 0)
220 res = (unsigned char *)
221 CALL (impl, (char *) p2 + align2,
222 (char *) p1 + align1, dlen);
223 printf ("Iteration %zd - did not __chk_fail\n", n);
224 chk_fail_ok = 0;
225 ret = 1;
228 continue;
230 memset (p2 - 64, '\1', 512 + 64);
231 res = (unsigned char *)
232 CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
233 if (res != STRCPY_RESULT (p2 + align2, len))
235 printf ("\
236 Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
237 n, impl->name, align1, align2, len, res,
238 STRCPY_RESULT (p2 + align2, len));
239 ret = 1;
241 for (j = 0; j < align2 + 64; ++j)
243 if (p2[j - 64] != '\1')
245 printf ("\
246 Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
247 n, impl->name, align1, align2, len);
248 ret = 1;
249 break;
252 for (j = align2 + len + 1; j < 512; ++j)
254 if (p2[j] != '\1')
256 printf ("\
257 Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
258 n, impl->name, align1, align2, len);
259 ret = 1;
260 break;
263 if (memcmp (p1 + align1, p2 + align2, len + 1))
265 printf ("\
266 Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
267 n, impl->name, align1, align2, len);
268 ret = 1;
274 static int
275 test_main (void)
277 size_t i;
279 set_fortify_handler (handler);
281 test_init ();
283 printf ("%23s", "");
284 FOR_EACH_IMPL (impl, 0)
285 printf ("\t%s", impl->name);
286 putchar ('\n');
288 for (i = 0; i < 16; ++i)
290 do_test (0, 0, i, i + 1, 127);
291 do_test (0, 0, i, i + 1, 255);
292 do_test (0, i, i, i + 1, 127);
293 do_test (i, 0, i, i + 1, 255);
296 for (i = 1; i < 8; ++i)
298 do_test (0, 0, 8 << i, (8 << i) + 1, 127);
299 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
302 for (i = 1; i < 8; ++i)
304 do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
305 do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
306 do_test (i, i, (8 << i), (8 << i) + 1, 127);
307 do_test (i, i, (8 << i), (8 << i) + 1, 255);
310 for (i = 0; i < 16; ++i)
312 do_test (0, 0, i, i + 256, 127);
313 do_test (0, 0, i, i + 256, 255);
314 do_test (0, i, i, i + 256, 127);
315 do_test (i, 0, i, i + 256, 255);
318 for (i = 1; i < 8; ++i)
320 do_test (0, 0, 8 << i, (8 << i) + 256, 127);
321 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
324 for (i = 1; i < 8; ++i)
326 do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
327 do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
328 do_test (i, i, (8 << i), (8 << i) + 256, 127);
329 do_test (i, i, (8 << i), (8 << i) + 256, 255);
332 for (i = 0; i < 16; ++i)
334 do_test (0, 0, i, i, 127);
335 do_test (0, 0, i, i + 2, 255);
336 do_test (0, i, i, i + 3, 127);
337 do_test (i, 0, i, i + 4, 255);
340 for (i = 1; i < 8; ++i)
342 do_test (0, 0, 8 << i, (8 << i) - 15, 127);
343 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
346 for (i = 1; i < 8; ++i)
348 do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
349 do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
350 do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
351 do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
354 do_random_tests ();
355 return ret;