OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libmudflap/] [testsuite/] [libmudflap.c/] [pass-stratcliff.c] - Blame information for rev 746

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 738 jeremybenn
/* Test for string function add boundaries of usable memory.
2
   Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2011
3
   Free Software Foundation, Inc.
4
   This file is part of the GNU C Library.
5
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6
 
7
   The GNU C Library is free software; you can redistribute it and/or
8
   modify it under the terms of the GNU Lesser General Public
9
   License as published by the Free Software Foundation; either
10
   version 2.1 of the License, or (at your option) any later version.
11
 
12
   The GNU C Library is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
   Lesser General Public License for more details.
16
 
17
   You should have received a copy of the GNU Lesser General Public
18
   License along with the GNU C Library; if not, write to the Free
19
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
   02110-1301 USA.  */
21
 
22
#define _GNU_SOURCE 1
23
#define __USE_GNU
24
 
25
/* Make sure we don't test the optimized inline functions if we want to
26
   test the real implementation.  */
27
#undef __USE_STRING_INLINES
28
 
29
#include "../config.h"
30
 
31
#include <errno.h>
32
#include <stdio.h>
33
#include <string.h>
34
#include <unistd.h>
35
#include <sys/mman.h>
36
#include <sys/param.h>
37
 
38
#ifndef MAX
39
#define MAX(a, b) ((a) > (b) ? (a) : (b))
40
#endif
41
 
42
#ifndef MIN
43
#define MIN(a, b) ((a) < (b) ? (a) : (b))
44
#endif
45
 
46
int
47
main (int argc, char *argv[])
48
{
49
  int size = sysconf (_SC_PAGESIZE);
50
  char *adr, *dest;
51
  int result = 0;
52
 
53
  adr = (char *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
54
                       MAP_PRIVATE | MAP_ANON, -1, 0);
55
  dest = (char *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
56
                        MAP_PRIVATE | MAP_ANON, -1, 0);
57
  if (adr == MAP_FAILED || dest == MAP_FAILED)
58
    {
59
      if (errno == ENOSYS)
60
        puts ("No test, mmap not available.");
61
      else
62
        {
63
          printf ("mmap failed: %m");
64
          result = 1;
65
        }
66
    }
67
  else
68
    {
69
      int inner, middle, outer;
70
 
71
      mprotect(adr, size, PROT_NONE);
72
      mprotect(adr + 2 * size, size, PROT_NONE);
73
      adr += size;
74
 
75
      mprotect(dest, size, PROT_NONE);
76
      mprotect(dest + 2 * size, size, PROT_NONE);
77
      dest += size;
78
 
79
      memset (adr, 'T', size);
80
 
81
      /* strlen test */
82
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
83
        {
84
          for (inner = MAX (outer, size - 64); inner < size; ++inner)
85
            {
86
              adr[inner] = '\0';
87
 
88
              if (strlen (&adr[outer]) != (size_t) (inner - outer))
89
                {
90
                  printf ("strlen flunked for outer = %d, inner = %d\n",
91
                          outer, inner);
92
                  result = 1;
93
                }
94
 
95
              adr[inner] = 'T';
96
            }
97
        }
98
 
99
      /* strchr test */
100
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
101
        {
102
          for (middle = MAX (outer, size - 64); middle < size; ++middle)
103
            {
104
              for (inner = middle; inner < size; ++inner)
105
                {
106
                  char *cp;
107
                  adr[middle] = 'V';
108
                  adr[inner] = '\0';
109
 
110
                  cp = strchr (&adr[outer], 'V');
111
 
112
                  if ((inner == middle && cp != NULL)
113
                      || (inner != middle
114
                          && (cp - &adr[outer]) != middle - outer))
115
                    {
116
                      printf ("strchr flunked for outer = %d, middle = %d, "
117
                              "inner = %d\n", outer, middle, inner);
118
                      result = 1;
119
                    }
120
 
121
                  adr[inner] = 'T';
122
                  adr[middle] = 'T';
123
                }
124
            }
125
        }
126
 
127
      /* Special test.  */
128
      adr[size - 1] = '\0';
129
      if (strchr (&adr[size - 1], '\n') != NULL)
130
        {
131
          puts ("strchr flunked for test of empty string at end of page");
132
          result = 1;
133
        }
134
 
135
      /* strrchr test */
136
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
137
        {
138
          for (middle = MAX (outer, size - 64); middle < size; ++middle)
139
            {
140
              for (inner = middle; inner < size; ++inner)
141
                {
142
                  char *cp;
143
                  adr[middle] = 'V';
144
                  adr[inner] = '\0';
145
 
146
                  cp = strrchr (&adr[outer], 'V');
147
 
148
                  if ((inner == middle && cp != NULL)
149
                      || (inner != middle
150
                          && (cp - &adr[outer]) != middle - outer))
151
                    {
152
                      printf ("strrchr flunked for outer = %d, middle = %d, "
153
                              "inner = %d\n", outer, middle, inner);
154
                      result = 1;
155
                    }
156
 
157
                  adr[inner] = 'T';
158
                  adr[middle] = 'T';
159
                }
160
            }
161
        }
162
 
163
#ifdef HAVE_RAWMEMCHR
164
      /* rawmemchr test */
165
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
166
        {
167
          for (middle = MAX (outer, size - 64); middle < size; ++middle)
168
            {
169
              char *cp;
170
              adr[middle] = 'V';
171
 
172
              cp = (char *) rawmemchr (&adr[outer], 'V');
173
 
174
              if (cp - &adr[outer] != middle - outer)
175
                {
176
                  printf ("rawmemchr flunked for outer = %d, middle = %d\n",
177
                          outer, middle);
178
                  result = 1;
179
                }
180
 
181
              adr[middle] = 'T';
182
            }
183
        }
184
#endif
185
 
186
      /* strcpy test */
187
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
188
        {
189
          for (inner = MAX (outer, size - 64); inner < size; ++inner)
190
            {
191
              adr[inner] = '\0';
192
 
193
              if (strcpy (dest, &adr[outer]) != dest
194
                  || strlen (dest) != (size_t) (inner - outer))
195
                {
196
                  printf ("strcpy flunked for outer = %d, inner = %d\n",
197
                          outer, inner);
198
                  result = 1;
199
                }
200
 
201
              adr[inner] = 'T';
202
            }
203
        }
204
 
205
      /* strncpy tests */
206
      adr[size-1] = 'T';
207
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
208
        {
209
          size_t len;
210
 
211
          for (len = 0; len < size - outer; ++len)
212
            {
213
              if (strncpy (dest, &adr[outer], len) != dest
214
                  || memcmp (dest, &adr[outer], len) != 0)
215
                {
216
                  printf ("outer strncpy flunked for outer = %d, len = %Zd\n",
217
                          outer, len);
218
                  result = 1;
219
                }
220
            }
221
        }
222
      adr[size-1] = '\0';
223
 
224
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
225
        {
226
          for (inner = MAX (outer, size - 64); inner < size; ++inner)
227
            {
228
              size_t len;
229
 
230
              adr[inner] = '\0';
231
 
232
              for (len = 0; len < size - outer + 64; ++len)
233
                {
234
                  if (strncpy (dest, &adr[outer], len) != dest
235
                      || memcmp (dest, &adr[outer],
236
                                 MIN (inner - outer, len)) != 0
237
                      || (inner - outer < len
238
                          && strlen (dest) != (inner - outer)))
239
                    {
240
                      printf ("strncpy flunked for outer = %d, inner = %d, len = %Zd\n",
241
                              outer, inner, len);
242
                      result = 1;
243
                    }
244
                  if (strncpy (dest + 1, &adr[outer], len) != dest + 1
245
                      || memcmp (dest + 1, &adr[outer],
246
                                 MIN (inner - outer, len)) != 0
247
                      || (inner - outer < len
248
                          && strlen (dest + 1) != (inner - outer)))
249
                    {
250
                      printf ("strncpy+1 flunked for outer = %d, inner = %d, len = %Zd\n",
251
                              outer, inner, len);
252
                      result = 1;
253
                    }
254
                }
255
 
256
              adr[inner] = 'T';
257
            }
258
        }
259
 
260
#ifdef HAVE_STPCPY
261
      /* stpcpy test */
262
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
263
        {
264
          for (inner = MAX (outer, size - 64); inner < size; ++inner)
265
            {
266
              adr[inner] = '\0';
267
 
268
              if ((stpcpy (dest, &adr[outer]) - dest) != inner - outer)
269
                {
270
                  printf ("stpcpy flunked for outer = %d, inner = %d\n",
271
                          outer, inner);
272
                  result = 1;
273
                }
274
 
275
              adr[inner] = 'T';
276
            }
277
        }
278
 
279
      /* stpncpy test */
280
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
281
        {
282
          for (middle = MAX (outer, size - 64); middle < size; ++middle)
283
            {
284
              adr[middle] = '\0';
285
 
286
              for (inner = 0; inner < size - outer; ++ inner)
287
                {
288
                  if ((stpncpy (dest, &adr[outer], inner) - dest)
289
                      != MIN (inner, middle - outer))
290
                    {
291
                      printf ("stpncpy flunked for outer = %d, middle = %d, "
292
                              "inner = %d\n", outer, middle, inner);
293
                      result = 1;
294
                    }
295
                }
296
 
297
              adr[middle] = 'T';
298
            }
299
        }
300
#endif
301
 
302
      /* memcpy test */
303
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
304
        for (inner = 0; inner < size - outer; ++inner)
305
          if (memcpy (dest, &adr[outer], inner) !=  dest)
306
            {
307
              printf ("memcpy flunked for outer = %d, inner = %d\n",
308
                      outer, inner);
309
              result = 1;
310
            }
311
 
312
#ifdef HAVE_MEMPCPY
313
      /* mempcpy test */
314
      for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
315
        for (inner = 0; inner < size - outer; ++inner)
316
          if (mempcpy (dest, &adr[outer], inner) !=  dest + inner)
317
            {
318
              printf ("mempcpy flunked for outer = %d, inner = %d\n",
319
                      outer, inner);
320
              result = 1;
321
            }
322
#endif
323
    }
324
 
325
  return result;
326
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.