Add missing magic to GLIBC_PROVIDES.
[glibc.git] / debug / test-strcpy_chk.c
blob25363628936d937728d129cbfaeb499af8845caa
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.
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 # include "../string/test-string.h"
25 extern void __attribute__ ((noreturn)) __chk_fail (void);
26 char *simple_strcpy_chk (char *, const char *, size_t);
27 extern char *normal_strcpy (char *, const char *, size_t)
28 __asm ("strcpy");
29 extern char *__strcpy_chk (char *, const char *, size_t);
31 IMPL (simple_strcpy_chk, 0)
32 IMPL (normal_strcpy, 1)
33 IMPL (__strcpy_chk, 2)
35 char *
36 simple_strcpy_chk (char *dst, const char *src, size_t len)
38 char *ret = dst;
39 if (! len)
40 __chk_fail ();
41 while ((*dst++ = *src++) != '\0')
42 if (--len == 0)
43 __chk_fail ();
44 return ret;
46 #endif
48 #include <fcntl.h>
49 #include <paths.h>
50 #include <setjmp.h>
51 #include <signal.h>
53 volatile int chk_fail_ok;
54 jmp_buf chk_fail_buf;
56 static void
57 handler (int sig)
59 if (chk_fail_ok)
61 chk_fail_ok = 0;
62 longjmp (chk_fail_buf, 1);
64 else
65 _exit (127);
68 typedef char *(*proto_t) (char *, const char *, size_t);
70 static void
71 do_one_test (impl_t *impl, char *dst, const char *src,
72 size_t len, size_t dlen)
74 char *res;
75 if (dlen <= len)
77 if (impl->test == 1)
78 return;
80 chk_fail_ok = 1;
81 if (setjmp (chk_fail_buf) == 0)
83 res = CALL (impl, dst, src, dlen);
84 printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
85 impl->name, len, dlen);
86 chk_fail_ok = 0;
87 ret = 1;
89 return;
91 else
92 res = CALL (impl, dst, src, dlen);
94 if (res != STRCPY_RESULT (dst, len))
96 printf ("Wrong result in function %s %p %p\n", impl->name,
97 res, STRCPY_RESULT (dst, len));
98 ret = 1;
99 return;
102 if (strcmp (dst, src) != 0)
104 printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
105 impl->name, dst, src);
106 ret = 1;
107 return;
110 if (HP_TIMING_AVAIL)
112 hp_timing_t start __attribute ((unused));
113 hp_timing_t stop __attribute ((unused));;
114 hp_timing_t best_time = ~ (hp_timing_t) 0;
115 size_t i;
117 for (i = 0; i < 32; ++i)
119 HP_TIMING_NOW (start);
120 CALL (impl, dst, src, dlen);
121 HP_TIMING_NOW (stop);
122 HP_TIMING_BEST (best_time, start, stop);
125 printf ("\t%zd", (size_t) best_time);
129 static void
130 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
132 size_t i;
133 char *s1, *s2;
135 align1 &= 7;
136 if (align1 + len >= page_size)
137 return;
139 align2 &= 7;
140 if (align2 + len >= page_size)
141 return;
143 s1 = (char *) buf1 + align1;
144 s2 = (char *) buf2 + align2;
146 for (i = 0; i < len; i++)
147 s1[i] = 32 + 23 * i % (max_char - 32);
148 s1[len] = 0;
150 if (HP_TIMING_AVAIL && dlen > len)
151 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
153 FOR_EACH_IMPL (impl, 0)
154 do_one_test (impl, s2, s1, len, dlen);
156 if (HP_TIMING_AVAIL && dlen > len)
157 putchar ('\n');
160 static void
161 do_random_tests (void)
163 size_t i, j, n, align1, align2, len, dlen;
164 unsigned char *p1 = buf1 + page_size - 512;
165 unsigned char *p2 = buf2 + page_size - 512;
166 unsigned char *res;
168 for (n = 0; n < ITERATIONS; n++)
170 align1 = random () & 31;
171 if (random () & 1)
172 align2 = random () & 31;
173 else
174 align2 = align1 + (random () & 24);
175 len = random () & 511;
176 j = align1;
177 if (align2 > j)
178 j = align2;
179 if (len + j >= 511)
180 len = 510 - j - (random () & 7);
181 j = len + align1 + 64;
182 if (j > 512)
183 j = 512;
184 for (i = 0; i < j; i++)
186 if (i == len + align1)
187 p1[i] = 0;
188 else
190 p1[i] = random () & 255;
191 if (i >= align1 && i < len + align1 && !p1[i])
192 p1[i] = (random () & 127) + 3;
196 switch (random () & 7)
198 case 0:
199 dlen = len - (random () & 31);
200 if (dlen > len)
201 dlen = len;
202 break;
203 case 1:
204 dlen = (size_t) -1;
205 break;
206 case 2:
207 dlen = len + 1 + (random () & 65535);
208 break;
209 case 3:
210 dlen = len + 1 + (random () & 255);
211 break;
212 case 4:
213 dlen = len + 1 + (random () & 31);
214 break;
215 case 5:
216 dlen = len + 1 + (random () & 7);
217 break;
218 case 6:
219 dlen = len + 1 + (random () & 3);
220 break;
221 default:
222 dlen = len + 1;
223 break;
226 FOR_EACH_IMPL (impl, 1)
228 if (dlen <= len)
230 if (impl->test != 1)
232 chk_fail_ok = 1;
233 if (setjmp (chk_fail_buf) == 0)
235 res = (unsigned char *)
236 CALL (impl, (char *) p2 + align2,
237 (char *) p1 + align1, dlen);
238 printf ("Iteration %zd - did not __chk_fail\n", n);
239 chk_fail_ok = 0;
240 ret = 1;
243 continue;
245 memset (p2 - 64, '\1', 512 + 64);
246 res = (unsigned char *)
247 CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
248 if (res != STRCPY_RESULT (p2 + align2, len))
250 printf ("\
251 Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
252 n, impl->name, align1, align2, len, res,
253 STRCPY_RESULT (p2 + align2, len));
254 ret = 1;
256 for (j = 0; j < align2 + 64; ++j)
258 if (p2[j - 64] != '\1')
260 printf ("\
261 Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
262 n, impl->name, align1, align2, len);
263 ret = 1;
264 break;
267 for (j = align2 + len + 1; j < 512; ++j)
269 if (p2[j] != '\1')
271 printf ("\
272 Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
273 n, impl->name, align1, align2, len);
274 ret = 1;
275 break;
278 if (memcmp (p1 + align1, p2 + align2, len + 1))
280 printf ("\
281 Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
282 n, impl->name, align1, align2, len);
283 ret = 1;
290 test_main (void)
292 size_t i;
294 struct sigaction sa;
295 sa.sa_handler = handler;
296 sa.sa_flags = 0;
297 sigemptyset (&sa.sa_mask);
299 sigaction (SIGABRT, &sa, NULL);
301 /* Avoid all the buffer overflow messages on stderr. */
302 int fd = open (_PATH_DEVNULL, O_WRONLY);
303 if (fd == -1)
304 close (STDERR_FILENO);
305 else
307 dup2 (fd, STDERR_FILENO);
308 close (fd);
310 setenv ("LIBC_FATAL_STDERR_", "1", 1);
312 test_init ();
314 printf ("%23s", "");
315 FOR_EACH_IMPL (impl, 0)
316 printf ("\t%s", impl->name);
317 putchar ('\n');
319 for (i = 0; i < 16; ++i)
321 do_test (0, 0, i, i + 1, 127);
322 do_test (0, 0, i, i + 1, 255);
323 do_test (0, i, i, i + 1, 127);
324 do_test (i, 0, i, i + 1, 255);
327 for (i = 1; i < 8; ++i)
329 do_test (0, 0, 8 << i, (8 << i) + 1, 127);
330 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
333 for (i = 1; i < 8; ++i)
335 do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
336 do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
337 do_test (i, i, (8 << i), (8 << i) + 1, 127);
338 do_test (i, i, (8 << i), (8 << i) + 1, 255);
341 for (i = 0; i < 16; ++i)
343 do_test (0, 0, i, i + 256, 127);
344 do_test (0, 0, i, i + 256, 255);
345 do_test (0, i, i, i + 256, 127);
346 do_test (i, 0, i, i + 256, 255);
349 for (i = 1; i < 8; ++i)
351 do_test (0, 0, 8 << i, (8 << i) + 256, 127);
352 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
355 for (i = 1; i < 8; ++i)
357 do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
358 do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
359 do_test (i, i, (8 << i), (8 << i) + 256, 127);
360 do_test (i, i, (8 << i), (8 << i) + 256, 255);
363 for (i = 0; i < 16; ++i)
365 do_test (0, 0, i, i, 127);
366 do_test (0, 0, i, i + 2, 255);
367 do_test (0, i, i, i + 3, 127);
368 do_test (i, 0, i, i + 4, 255);
371 for (i = 1; i < 8; ++i)
373 do_test (0, 0, 8 << i, (8 << i) - 15, 127);
374 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
377 for (i = 1; i < 8; ++i)
379 do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
380 do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
381 do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
382 do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
385 do_random_tests ();
386 return ret;
389 #include "../test-skeleton.c"