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