Fix ldbl-128ibm tanhl inaccuracy (bug 18790).
[glibc.git] / debug / test-strcpy_chk.c
blobe93dd69c6b49425327ca789fe8ef443de3c85339
1 /* Test and measure __strcpy_chk functions.
2 Copyright (C) 1999-2015 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 "../test-skeleton.c"
61 volatile int chk_fail_ok;
62 jmp_buf chk_fail_buf;
64 static void
65 handler (int sig)
67 if (chk_fail_ok)
69 chk_fail_ok = 0;
70 longjmp (chk_fail_buf, 1);
72 else
73 _exit (127);
76 typedef char *(*proto_t) (char *, const char *, size_t);
78 static void
79 do_one_test (impl_t *impl, char *dst, const char *src,
80 size_t len, size_t dlen)
82 char *res;
83 if (dlen <= len)
85 if (impl->test == 1)
86 return;
88 chk_fail_ok = 1;
89 if (setjmp (chk_fail_buf) == 0)
91 res = CALL (impl, dst, src, dlen);
92 printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
93 impl->name, len, dlen);
94 chk_fail_ok = 0;
95 ret = 1;
97 return;
99 else
100 res = CALL (impl, dst, src, dlen);
102 if (res != STRCPY_RESULT (dst, len))
104 printf ("Wrong result in function %s %p %p\n", impl->name,
105 res, STRCPY_RESULT (dst, len));
106 ret = 1;
107 return;
110 if (strcmp (dst, src) != 0)
112 printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
113 impl->name, dst, src);
114 ret = 1;
115 return;
119 static void
120 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
122 size_t i;
123 char *s1, *s2;
125 align1 &= 7;
126 if (align1 + len >= page_size)
127 return;
129 align2 &= 7;
130 if (align2 + len >= page_size)
131 return;
133 s1 = (char *) buf1 + align1;
134 s2 = (char *) buf2 + align2;
136 for (i = 0; i < len; i++)
137 s1[i] = 32 + 23 * i % (max_char - 32);
138 s1[len] = 0;
140 FOR_EACH_IMPL (impl, 0)
141 do_one_test (impl, s2, s1, len, dlen);
144 static void
145 do_random_tests (void)
147 size_t i, j, n, align1, align2, len, dlen;
148 unsigned char *p1 = buf1 + page_size - 512;
149 unsigned char *p2 = buf2 + page_size - 512;
150 unsigned char *res;
152 for (n = 0; n < ITERATIONS; n++)
154 align1 = random () & 31;
155 if (random () & 1)
156 align2 = random () & 31;
157 else
158 align2 = align1 + (random () & 24);
159 len = random () & 511;
160 j = align1;
161 if (align2 > j)
162 j = align2;
163 if (len + j >= 511)
164 len = 510 - j - (random () & 7);
165 j = len + align1 + 64;
166 if (j > 512)
167 j = 512;
168 for (i = 0; i < j; i++)
170 if (i == len + align1)
171 p1[i] = 0;
172 else
174 p1[i] = random () & 255;
175 if (i >= align1 && i < len + align1 && !p1[i])
176 p1[i] = (random () & 127) + 3;
180 switch (random () & 7)
182 case 0:
183 dlen = len - (random () & 31);
184 if (dlen > len)
185 dlen = len;
186 break;
187 case 1:
188 dlen = (size_t) -1;
189 break;
190 case 2:
191 dlen = len + 1 + (random () & 65535);
192 break;
193 case 3:
194 dlen = len + 1 + (random () & 255);
195 break;
196 case 4:
197 dlen = len + 1 + (random () & 31);
198 break;
199 case 5:
200 dlen = len + 1 + (random () & 7);
201 break;
202 case 6:
203 dlen = len + 1 + (random () & 3);
204 break;
205 default:
206 dlen = len + 1;
207 break;
210 FOR_EACH_IMPL (impl, 1)
212 if (dlen <= len)
214 if (impl->test != 1)
216 chk_fail_ok = 1;
217 if (setjmp (chk_fail_buf) == 0)
219 res = (unsigned char *)
220 CALL (impl, (char *) p2 + align2,
221 (char *) p1 + align1, dlen);
222 printf ("Iteration %zd - did not __chk_fail\n", n);
223 chk_fail_ok = 0;
224 ret = 1;
227 continue;
229 memset (p2 - 64, '\1', 512 + 64);
230 res = (unsigned char *)
231 CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
232 if (res != STRCPY_RESULT (p2 + align2, len))
234 printf ("\
235 Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
236 n, impl->name, align1, align2, len, res,
237 STRCPY_RESULT (p2 + align2, len));
238 ret = 1;
240 for (j = 0; j < align2 + 64; ++j)
242 if (p2[j - 64] != '\1')
244 printf ("\
245 Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
246 n, impl->name, align1, align2, len);
247 ret = 1;
248 break;
251 for (j = align2 + len + 1; j < 512; ++j)
253 if (p2[j] != '\1')
255 printf ("\
256 Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
257 n, impl->name, align1, align2, len);
258 ret = 1;
259 break;
262 if (memcmp (p1 + align1, p2 + align2, len + 1))
264 printf ("\
265 Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
266 n, impl->name, align1, align2, len);
267 ret = 1;
273 static int
274 test_main (void)
276 size_t i;
278 set_fortify_handler (handler);
280 test_init ();
282 printf ("%23s", "");
283 FOR_EACH_IMPL (impl, 0)
284 printf ("\t%s", impl->name);
285 putchar ('\n');
287 for (i = 0; i < 16; ++i)
289 do_test (0, 0, i, i + 1, 127);
290 do_test (0, 0, i, i + 1, 255);
291 do_test (0, i, i, i + 1, 127);
292 do_test (i, 0, i, i + 1, 255);
295 for (i = 1; i < 8; ++i)
297 do_test (0, 0, 8 << i, (8 << i) + 1, 127);
298 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
301 for (i = 1; i < 8; ++i)
303 do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
304 do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
305 do_test (i, i, (8 << i), (8 << i) + 1, 127);
306 do_test (i, i, (8 << i), (8 << i) + 1, 255);
309 for (i = 0; i < 16; ++i)
311 do_test (0, 0, i, i + 256, 127);
312 do_test (0, 0, i, i + 256, 255);
313 do_test (0, i, i, i + 256, 127);
314 do_test (i, 0, i, i + 256, 255);
317 for (i = 1; i < 8; ++i)
319 do_test (0, 0, 8 << i, (8 << i) + 256, 127);
320 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
323 for (i = 1; i < 8; ++i)
325 do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
326 do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
327 do_test (i, i, (8 << i), (8 << i) + 256, 127);
328 do_test (i, i, (8 << i), (8 << i) + 256, 255);
331 for (i = 0; i < 16; ++i)
333 do_test (0, 0, i, i, 127);
334 do_test (0, 0, i, i + 2, 255);
335 do_test (0, i, i, i + 3, 127);
336 do_test (i, 0, i, i + 4, 255);
339 for (i = 1; i < 8; ++i)
341 do_test (0, 0, 8 << i, (8 << i) - 15, 127);
342 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
345 for (i = 1; i < 8; ++i)
347 do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
348 do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
349 do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
350 do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
353 do_random_tests ();
354 return ret;