~ubuntu-branches/ubuntu/natty/eglibc/natty-security

« back to all changes in this revision

Viewing changes to string/test-strcpy.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2009-05-05 09:54:14 UTC
  • Revision ID: james.westby@ubuntu.com-20090505095414-c45qsg9ixjheohru
ImportĀ upstreamĀ versionĀ 2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test and measure strcpy functions.
 
2
   Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc.
 
3
   This file is part of the GNU C Library.
 
4
   Written by Jakub Jelinek <jakub@redhat.com>, 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 "test-string.h"
 
25
 
 
26
char *simple_strcpy (char *, const char *);
 
27
 
 
28
IMPL (simple_strcpy, 0)
 
29
IMPL (strcpy, 1)
 
30
 
 
31
char *
 
32
simple_strcpy (char *dst, const char *src)
 
33
{
 
34
  char *ret = dst;
 
35
  while ((*dst++ = *src++) != '\0');
 
36
  return ret;
 
37
}
 
38
#endif
 
39
 
 
40
typedef char *(*proto_t) (char *, const char *);
 
41
 
 
42
static void
 
43
do_one_test (impl_t *impl, char *dst, const char *src,
 
44
             size_t len __attribute__((unused)))
 
45
{
 
46
  if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
 
47
    {
 
48
      error (0, 0, "Wrong result in function %s %p %p", impl->name,
 
49
             CALL (impl, dst, src), STRCPY_RESULT (dst, len));
 
50
      ret = 1;
 
51
      return;
 
52
    }
 
53
 
 
54
  if (strcmp (dst, src) != 0)
 
55
    {
 
56
      error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
 
57
             impl->name, dst, src);
 
58
      ret = 1;
 
59
      return;
 
60
    }
 
61
 
 
62
  if (HP_TIMING_AVAIL)
 
63
    {
 
64
      hp_timing_t start __attribute ((unused));
 
65
      hp_timing_t stop __attribute ((unused));;
 
66
      hp_timing_t best_time = ~ (hp_timing_t) 0;
 
67
      size_t i;
 
68
 
 
69
      for (i = 0; i < 32; ++i)
 
70
        {
 
71
          HP_TIMING_NOW (start);
 
72
          CALL (impl, dst, src);
 
73
          HP_TIMING_NOW (stop);
 
74
          HP_TIMING_BEST (best_time, start, stop);
 
75
        }
 
76
 
 
77
      printf ("\t%zd", (size_t) best_time);
 
78
    }
 
79
}
 
80
 
 
81
static void
 
82
do_test (size_t align1, size_t align2, size_t len, int max_char)
 
83
{
 
84
  size_t i;
 
85
  char *s1, *s2;
 
86
 
 
87
  align1 &= 7;
 
88
  if (align1 + len >= page_size)
 
89
    return;
 
90
 
 
91
  align2 &= 7;
 
92
  if (align2 + len >= page_size)
 
93
    return;
 
94
 
 
95
  s1 = (char *) (buf1 + align1);
 
96
  s2 = (char *) (buf2 + align2);
 
97
 
 
98
  for (i = 0; i < len; i++)
 
99
    s1[i] = 32 + 23 * i % (max_char - 32);
 
100
  s1[len] = 0;
 
101
 
 
102
  if (HP_TIMING_AVAIL)
 
103
    printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
 
104
 
 
105
  FOR_EACH_IMPL (impl, 0)
 
106
    do_one_test (impl, s2, s1, len);
 
107
 
 
108
  if (HP_TIMING_AVAIL)
 
109
    putchar ('\n');
 
110
}
 
111
 
 
112
static void
 
113
do_random_tests (void)
 
114
{
 
115
  size_t i, j, n, align1, align2, len;
 
116
  unsigned char *p1 = buf1 + page_size - 512;
 
117
  unsigned char *p2 = buf2 + page_size - 512;
 
118
  unsigned char *res;
 
119
 
 
120
  for (n = 0; n < ITERATIONS; n++)
 
121
    {
 
122
      align1 = random () & 31;
 
123
      if (random () & 1)
 
124
        align2 = random () & 31;
 
125
      else
 
126
        align2 = align1 + (random () & 24);
 
127
      len = random () & 511;
 
128
      j = align1;
 
129
      if (align2 > j)
 
130
        j = align2;
 
131
      if (len + j >= 511)
 
132
        len = 510 - j - (random () & 7);
 
133
      j = len + align1 + 64;
 
134
      if (j > 512)
 
135
        j = 512;
 
136
      for (i = 0; i < j; i++)
 
137
        {
 
138
          if (i == len + align1)
 
139
            p1[i] = 0;
 
140
          else
 
141
            {
 
142
              p1[i] = random () & 255;
 
143
              if (i >= align1 && i < len + align1 && !p1[i])
 
144
                p1[i] = (random () & 127) + 3;
 
145
            }
 
146
        }
 
147
 
 
148
      FOR_EACH_IMPL (impl, 1)
 
149
        {
 
150
          memset (p2 - 64, '\1', 512 + 64);
 
151
          res = (unsigned char *) CALL (impl, (char *) (p2 + align2),
 
152
                                        (char *) (p1 + align1));
 
153
          if (res != STRCPY_RESULT (p2 + align2, len))
 
154
            {
 
155
              error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
 
156
                     n, impl->name, align1, align2, len, res,
 
157
                     STRCPY_RESULT (p2 + align2, len));
 
158
              ret = 1;
 
159
            }
 
160
          for (j = 0; j < align2 + 64; ++j)
 
161
            {
 
162
              if (p2[j - 64] != '\1')
 
163
                {
 
164
                  error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
 
165
                         n, impl->name, align1, align2, len);
 
166
                  ret = 1;
 
167
                  break;
 
168
                }
 
169
            }
 
170
          for (j = align2 + len + 1; j < 512; ++j)
 
171
            {
 
172
              if (p2[j] != '\1')
 
173
                {
 
174
                  error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
 
175
                         n, impl->name, align1, align2, len);
 
176
                  ret = 1;
 
177
                  break;
 
178
                }
 
179
            }
 
180
          if (memcmp (p1 + align1, p2 + align2, len + 1))
 
181
            {
 
182
              error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
 
183
                     n, impl->name, align1, align2, len);
 
184
              ret = 1;
 
185
            }
 
186
        }
 
187
    }
 
188
}
 
189
 
 
190
int
 
191
test_main (void)
 
192
{
 
193
  size_t i;
 
194
 
 
195
  test_init ();
 
196
 
 
197
  printf ("%23s", "");
 
198
  FOR_EACH_IMPL (impl, 0)
 
199
    printf ("\t%s", impl->name);
 
200
  putchar ('\n');
 
201
 
 
202
  for (i = 0; i < 16; ++i)
 
203
    {
 
204
      do_test (0, 0, i, 127);
 
205
      do_test (0, 0, i, 255);
 
206
      do_test (0, i, i, 127);
 
207
      do_test (i, 0, i, 255);
 
208
    }
 
209
 
 
210
  for (i = 1; i < 8; ++i)
 
211
    {
 
212
      do_test (0, 0, 8 << i, 127);
 
213
      do_test (8 - i, 2 * i, 8 << i, 127);
 
214
    }
 
215
 
 
216
  for (i = 1; i < 8; ++i)
 
217
    {
 
218
      do_test (i, 2 * i, 8 << i, 127);
 
219
      do_test (2 * i, i, 8 << i, 255);
 
220
      do_test (i, i, 8 << i, 127);
 
221
      do_test (i, i, 8 << i, 255);
 
222
    }
 
223
 
 
224
  do_random_tests ();
 
225
  return ret;
 
226
}
 
227
 
 
228
#include "../test-skeleton.c"