Back to index

glibc  2.9
test-strcpy_chk.c
Go to the documentation of this file.
00001 /* Test and measure __strcpy_chk functions.
00002    Copyright (C) 1999, 2002, 2003, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef STRCPY_RESULT
00022 # define STRCPY_RESULT(dst, len) dst
00023 # define TEST_MAIN
00024 # include "../string/test-string.h"
00025 
00026 extern void __attribute__ ((noreturn)) __chk_fail (void);
00027 char *simple_strcpy_chk (char *, const char *, size_t);
00028 extern char *normal_strcpy (char *, const char *, size_t)
00029   __asm ("strcpy");
00030 extern char *__strcpy_chk (char *, const char *, size_t);
00031 
00032 IMPL (simple_strcpy_chk, 0)
00033 IMPL (normal_strcpy, 1)
00034 IMPL (__strcpy_chk, 2)
00035 
00036 char *
00037 simple_strcpy_chk (char *dst, const char *src, size_t len)
00038 {
00039   char *ret = dst;
00040   if (! len)
00041     __chk_fail ();
00042   while ((*dst++ = *src++) != '\0')
00043     if (--len == 0)
00044       __chk_fail ();
00045   return ret;
00046 }
00047 #endif
00048 
00049 #include <fcntl.h>
00050 #include <paths.h>
00051 #include <setjmp.h>
00052 #include <signal.h>
00053 
00054 volatile int chk_fail_ok;
00055 jmp_buf chk_fail_buf;
00056 
00057 static void
00058 handler (int sig)
00059 {
00060   if (chk_fail_ok)
00061     {
00062       chk_fail_ok = 0;
00063       longjmp (chk_fail_buf, 1);
00064     }
00065   else
00066     _exit (127);
00067 }
00068 
00069 typedef char *(*proto_t) (char *, const char *, size_t);
00070 
00071 static void
00072 do_one_test (impl_t *impl, char *dst, const char *src,
00073             size_t len, size_t dlen)
00074 {
00075   char *res;
00076   if (dlen <= len)
00077     {
00078       if (impl->test == 1)
00079        return;
00080 
00081       chk_fail_ok = 1;
00082       if (setjmp (chk_fail_buf) == 0)
00083        {
00084          res = CALL (impl, dst, src, dlen);
00085          printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
00086                 impl->name, len, dlen);
00087          chk_fail_ok = 0;
00088          ret = 1;
00089        }
00090       return;
00091     }
00092   else
00093     res = CALL (impl, dst, src, dlen);
00094 
00095   if (res != STRCPY_RESULT (dst, len))
00096     {
00097       printf ("Wrong result in function %s %p %p\n", impl->name,
00098              res, STRCPY_RESULT (dst, len));
00099       ret = 1;
00100       return;
00101     }
00102 
00103   if (strcmp (dst, src) != 0)
00104     {
00105       printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
00106              impl->name, dst, src);
00107       ret = 1;
00108       return;
00109     }
00110 
00111   if (HP_TIMING_AVAIL)
00112     {
00113       hp_timing_t start __attribute ((unused));
00114       hp_timing_t stop __attribute ((unused));;
00115       hp_timing_t best_time = ~ (hp_timing_t) 0;
00116       size_t i;
00117 
00118       for (i = 0; i < 32; ++i)
00119        {
00120          HP_TIMING_NOW (start);
00121          CALL (impl, dst, src, dlen);
00122          HP_TIMING_NOW (stop);
00123          HP_TIMING_BEST (best_time, start, stop);
00124        }
00125 
00126       printf ("\t%zd", (size_t) best_time);
00127     }
00128 }
00129 
00130 static void
00131 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
00132 {
00133   size_t i;
00134   char *s1, *s2;
00135 
00136   align1 &= 7;
00137   if (align1 + len >= page_size)
00138     return;
00139 
00140   align2 &= 7;
00141   if (align2 + len >= page_size)
00142     return;
00143 
00144   s1 = (char *) buf1 + align1;
00145   s2 = (char *) buf2 + align2;
00146 
00147   for (i = 0; i < len; i++)
00148     s1[i] = 32 + 23 * i % (max_char - 32);
00149   s1[len] = 0;
00150 
00151   if (HP_TIMING_AVAIL && dlen > len)
00152     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
00153 
00154   FOR_EACH_IMPL (impl, 0)
00155     do_one_test (impl, s2, s1, len, dlen);
00156 
00157   if (HP_TIMING_AVAIL && dlen > len)
00158     putchar ('\n');
00159 }
00160 
00161 static void
00162 do_random_tests (void)
00163 {
00164   size_t i, j, n, align1, align2, len, dlen;
00165   unsigned char *p1 = buf1 + page_size - 512;
00166   unsigned char *p2 = buf2 + page_size - 512;
00167   unsigned char *res;
00168 
00169   for (n = 0; n < ITERATIONS; n++)
00170     {
00171       align1 = random () & 31;
00172       if (random () & 1)
00173        align2 = random () & 31;
00174       else
00175        align2 = align1 + (random () & 24);
00176       len = random () & 511;
00177       j = align1;
00178       if (align2 > j)
00179        j = align2;
00180       if (len + j >= 511)
00181        len = 510 - j - (random () & 7);
00182       j = len + align1 + 64;
00183       if (j > 512)
00184        j = 512;
00185       for (i = 0; i < j; i++)
00186        {
00187          if (i == len + align1)
00188            p1[i] = 0;
00189          else
00190            {
00191              p1[i] = random () & 255;
00192              if (i >= align1 && i < len + align1 && !p1[i])
00193               p1[i] = (random () & 127) + 3;
00194            }
00195        }
00196 
00197       switch (random () & 7)
00198        {
00199        case 0:
00200          dlen = len - (random () & 31);
00201          if (dlen > len)
00202            dlen = len;
00203          break;
00204        case 1:
00205          dlen = (size_t) -1;
00206          break;
00207        case 2:
00208          dlen = len + 1 + (random () & 65535);
00209          break;
00210        case 3:
00211          dlen = len + 1 + (random () & 255);
00212          break;
00213        case 4:
00214          dlen = len + 1 + (random () & 31);
00215          break;
00216        case 5:
00217          dlen = len + 1 + (random () & 7);
00218          break;
00219        case 6:
00220          dlen = len + 1 + (random () & 3);
00221          break;
00222        default:
00223          dlen = len + 1;
00224          break;
00225        }
00226 
00227       FOR_EACH_IMPL (impl, 1)
00228        {
00229          if (dlen <= len)
00230            {
00231              if (impl->test != 1)
00232               {
00233                 chk_fail_ok = 1;
00234                 if (setjmp (chk_fail_buf) == 0)
00235                   {
00236                     res = (unsigned char *)
00237                          CALL (impl, (char *) p2 + align2,
00238                               (char *) p1 + align1, dlen);
00239                     printf ("Iteration %zd - did not __chk_fail\n", n);
00240                     chk_fail_ok = 0;
00241                     ret = 1;
00242                   }
00243               }
00244              continue;
00245            }
00246          memset (p2 - 64, '\1', 512 + 64);
00247          res = (unsigned char *)
00248               CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
00249          if (res != STRCPY_RESULT (p2 + align2, len))
00250            {
00251              printf ("\
00252 Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
00253                     n, impl->name, align1, align2, len, res,
00254                     STRCPY_RESULT (p2 + align2, len));
00255              ret = 1;
00256            }
00257          for (j = 0; j < align2 + 64; ++j)
00258            {
00259              if (p2[j - 64] != '\1')
00260               {
00261                 printf ("\
00262 Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
00263                        n, impl->name, align1, align2, len);
00264                 ret = 1;
00265                 break;
00266               }
00267            }
00268          for (j = align2 + len + 1; j < 512; ++j)
00269            {
00270              if (p2[j] != '\1')
00271               {
00272                 printf ("\
00273 Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
00274                        n, impl->name, align1, align2, len);
00275                 ret = 1;
00276                 break;
00277               }
00278            }
00279          if (memcmp (p1 + align1, p2 + align2, len + 1))
00280            {
00281              printf ("\
00282 Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
00283                     n, impl->name, align1, align2, len);
00284              ret = 1;
00285            }
00286        }
00287     }
00288 }
00289 
00290 int
00291 test_main (void)
00292 {
00293   size_t i;
00294 
00295   struct sigaction sa;
00296   sa.sa_handler = handler;
00297   sa.sa_flags = 0;
00298   sigemptyset (&sa.sa_mask);
00299 
00300   sigaction (SIGABRT, &sa, NULL);
00301 
00302   /* Avoid all the buffer overflow messages on stderr.  */
00303   int fd = open (_PATH_DEVNULL, O_WRONLY);
00304   if (fd == -1)
00305     close (STDERR_FILENO);
00306   else
00307     {
00308       dup2 (fd, STDERR_FILENO);
00309       close (fd);
00310     }
00311   setenv ("LIBC_FATAL_STDERR_", "1", 1);
00312 
00313   test_init ();
00314 
00315   printf ("%23s", "");
00316   FOR_EACH_IMPL (impl, 0)
00317     printf ("\t%s", impl->name);
00318   putchar ('\n');
00319 
00320   for (i = 0; i < 16; ++i)
00321     {
00322       do_test (0, 0, i, i + 1, 127);
00323       do_test (0, 0, i, i + 1, 255);
00324       do_test (0, i, i, i + 1, 127);
00325       do_test (i, 0, i, i + 1, 255);
00326     }
00327 
00328   for (i = 1; i < 8; ++i)
00329     {
00330       do_test (0, 0, 8 << i, (8 << i) + 1, 127);
00331       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
00332     }
00333 
00334   for (i = 1; i < 8; ++i)
00335     {
00336       do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
00337       do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
00338       do_test (i, i, (8 << i), (8 << i) + 1, 127);
00339       do_test (i, i, (8 << i), (8 << i) + 1, 255);
00340     }
00341 
00342   for (i = 0; i < 16; ++i)
00343     {
00344       do_test (0, 0, i, i + 256, 127);
00345       do_test (0, 0, i, i + 256, 255);
00346       do_test (0, i, i, i + 256, 127);
00347       do_test (i, 0, i, i + 256, 255);
00348     }
00349 
00350   for (i = 1; i < 8; ++i)
00351     {
00352       do_test (0, 0, 8 << i, (8 << i) + 256, 127);
00353       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
00354     }
00355 
00356   for (i = 1; i < 8; ++i)
00357     {
00358       do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
00359       do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
00360       do_test (i, i, (8 << i), (8 << i) + 256, 127);
00361       do_test (i, i, (8 << i), (8 << i) + 256, 255);
00362     }
00363 
00364   for (i = 0; i < 16; ++i)
00365     {
00366       do_test (0, 0, i, i, 127);
00367       do_test (0, 0, i, i + 2, 255);
00368       do_test (0, i, i, i + 3, 127);
00369       do_test (i, 0, i, i + 4, 255);
00370     }
00371 
00372   for (i = 1; i < 8; ++i)
00373     {
00374       do_test (0, 0, 8 << i, (8 << i) - 15, 127);
00375       do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
00376     }
00377 
00378   for (i = 1; i < 8; ++i)
00379     {
00380       do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
00381       do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
00382       do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
00383       do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
00384     }
00385 
00386   do_random_tests ();
00387   return ret;
00388 }
00389 
00390 #include "../test-skeleton.c"