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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [gcc.c-torture/] [execute/] [builtins/] [lib/] [chk.c] - Blame information for rev 774

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

Line No. Rev Author Line
1 688 jeremybenn
#include <stdarg.h>
2
#ifdef __unix__
3
#include <sys/types.h>
4
#endif
5
 
6
extern void abort (void);
7
 
8
extern int inside_main;
9
void *chk_fail_buf[256] __attribute__((aligned (16)));
10
volatile int chk_fail_allowed, chk_calls;
11
volatile int memcpy_disallowed, mempcpy_disallowed, memmove_disallowed;
12
volatile int memset_disallowed, strcpy_disallowed, stpcpy_disallowed;
13
volatile int strncpy_disallowed, stpncpy_disallowed, strcat_disallowed;
14
volatile int strncat_disallowed, sprintf_disallowed, vsprintf_disallowed;
15
volatile int snprintf_disallowed, vsnprintf_disallowed;
16
extern __SIZE_TYPE__ strlen (const char *);
17
extern int vsprintf (char *, const char *, va_list);
18
 
19
void __attribute__((noreturn))
20
__chk_fail (void)
21
{
22
  if (chk_fail_allowed)
23
    __builtin_longjmp (chk_fail_buf, 1);
24
  abort ();
25
}
26
 
27
void *
28
memcpy (void *dst, const void *src, __SIZE_TYPE__ n)
29
{
30
  const char *srcp;
31
  char *dstp;
32
 
33
#ifdef __OPTIMIZE__
34
  if (memcpy_disallowed && inside_main)
35
    abort ();
36
#endif
37
 
38
  srcp = src;
39
  dstp = dst;
40
  while (n-- != 0)
41
    *dstp++ = *srcp++;
42
 
43
  return dst;
44
}
45
 
46
void *
47
__memcpy_chk (void *dst, const void *src, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
48
{
49
  /* If size is -1, GCC should always optimize the call into memcpy.  */
50
  if (size == (__SIZE_TYPE__) -1)
51
    abort ();
52
  ++chk_calls;
53
  if (n > size)
54
    __chk_fail ();
55
  return memcpy (dst, src, n);
56
}
57
 
58
void *
59
mempcpy (void *dst, const void *src, __SIZE_TYPE__ n)
60
{
61
  const char *srcp;
62
  char *dstp;
63
 
64
#ifdef __OPTIMIZE__
65
  if (mempcpy_disallowed && inside_main)
66
    abort ();
67
#endif
68
 
69
  srcp = src;
70
  dstp = dst;
71
  while (n-- != 0)
72
    *dstp++ = *srcp++;
73
 
74
  return dstp;
75
}
76
 
77
void *
78
__mempcpy_chk (void *dst, const void *src, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
79
{
80
  /* If size is -1, GCC should always optimize the call into mempcpy.  */
81
  if (size == (__SIZE_TYPE__) -1)
82
    abort ();
83
  ++chk_calls;
84
  if (n > size)
85
    __chk_fail ();
86
  return mempcpy (dst, src, n);
87
}
88
 
89
void *
90
memmove (void *dst, const void *src, __SIZE_TYPE__ n)
91
{
92
  const char *srcp;
93
  char *dstp;
94
 
95
#ifdef __OPTIMIZE__
96
  if (memmove_disallowed && inside_main)
97
    abort ();
98
#endif
99
 
100
  srcp = src;
101
  dstp = dst;
102
  if (srcp < dstp)
103
    while (n-- != 0)
104
      dstp[n] = srcp[n];
105
  else
106
    while (n-- != 0)
107
      *dstp++ = *srcp++;
108
 
109
  return dst;
110
}
111
 
112
void *
113
__memmove_chk (void *dst, const void *src, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
114
{
115
  /* If size is -1, GCC should always optimize the call into memmove.  */
116
  if (size == (__SIZE_TYPE__) -1)
117
    abort ();
118
  ++chk_calls;
119
  if (n > size)
120
    __chk_fail ();
121
  return memmove (dst, src, n);
122
}
123
 
124
void *
125
memset (void *dst, int c, __SIZE_TYPE__ n)
126
{
127
  /* Single-byte memsets should be done inline when optimisation
128
     is enabled.  */
129
#ifdef __OPTIMIZE__
130
  if (memset_disallowed && inside_main && n < 2)
131
    abort ();
132
#endif
133
 
134
  while (n-- != 0)
135
    n[(char *) dst] = c;
136
 
137
  return dst;
138
}
139
 
140
void *
141
__memset_chk (void *dst, int c, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
142
{
143
  /* If size is -1, GCC should always optimize the call into memset.  */
144
  if (size == (__SIZE_TYPE__) -1)
145
    abort ();
146
  ++chk_calls;
147
  if (n > size)
148
    __chk_fail ();
149
  return memset (dst, c, n);
150
}
151
 
152
char *
153
strcpy (char *d, const char *s)
154
{
155
  char *r = d;
156
#ifdef __OPTIMIZE__
157
  if (strcpy_disallowed && inside_main)
158
    abort ();
159
#endif
160
  while ((*d++ = *s++));
161
  return r;
162
}
163
 
164
char *
165
__strcpy_chk (char *d, const char *s, __SIZE_TYPE__ size)
166
{
167
  /* If size is -1, GCC should always optimize the call into strcpy.  */
168
  if (size == (__SIZE_TYPE__) -1)
169
    abort ();
170
  ++chk_calls;
171
  if (strlen (s) >= size)
172
    __chk_fail ();
173
  return strcpy (d, s);
174
}
175
 
176
char *
177
stpcpy (char *dst, const char *src)
178
{
179
#ifdef __OPTIMIZE__
180
  if (stpcpy_disallowed && inside_main)
181
    abort ();
182
#endif
183
 
184
  while (*src != 0)
185
    *dst++ = *src++;
186
 
187
  *dst = 0;
188
  return dst;
189
}
190
 
191
char *
192
__stpcpy_chk (char *d, const char *s, __SIZE_TYPE__ size)
193
{
194
  /* If size is -1, GCC should always optimize the call into stpcpy.  */
195
  if (size == (__SIZE_TYPE__) -1)
196
    abort ();
197
  ++chk_calls;
198
  if (strlen (s) >= size)
199
    __chk_fail ();
200
  return stpcpy (d, s);
201
}
202
 
203
char *
204
stpncpy (char *dst, const char *src, __SIZE_TYPE__ n)
205
{
206
#ifdef __OPTIMIZE__
207
  if (stpncpy_disallowed && inside_main)
208
    abort ();
209
#endif
210
 
211
  for (; *src && n; n--)
212
    *dst++ = *src++;
213
 
214
  char *ret = dst;
215
 
216
  while (n--)
217
    *dst++ = 0;
218
 
219
  return ret;
220
}
221
 
222
 
223
char *
224
__stpncpy_chk (char *s1, const char *s2, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
225
{
226
  /* If size is -1, GCC should always optimize the call into stpncpy.  */
227
  if (size == (__SIZE_TYPE__) -1)
228
    abort ();
229
  ++chk_calls;
230
  if (n > size)
231
    __chk_fail ();
232
  return stpncpy (s1, s2, n);
233
}
234
 
235
char *
236
strncpy (char *s1, const char *s2, __SIZE_TYPE__ n)
237
{
238
  char *dest = s1;
239
#ifdef __OPTIMIZE__
240
  if (strncpy_disallowed && inside_main)
241
    abort();
242
#endif
243
  for (; *s2 && n; n--)
244
    *s1++ = *s2++;
245
  while (n--)
246
    *s1++ = 0;
247
  return dest;
248
}
249
 
250
char *
251
__strncpy_chk (char *s1, const char *s2, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
252
{
253
  /* If size is -1, GCC should always optimize the call into strncpy.  */
254
  if (size == (__SIZE_TYPE__) -1)
255
    abort ();
256
  ++chk_calls;
257
  if (n > size)
258
    __chk_fail ();
259
  return strncpy (s1, s2, n);
260
}
261
 
262
char *
263
strcat (char *dst, const char *src)
264
{
265
  char *p = dst;
266
 
267
#ifdef __OPTIMIZE__
268
  if (strcat_disallowed && inside_main)
269
    abort ();
270
#endif
271
 
272
  while (*p)
273
    p++;
274
  while ((*p++ = *src++))
275
    ;
276
  return dst;
277
}
278
 
279
char *
280
__strcat_chk (char *d, const char *s, __SIZE_TYPE__ size)
281
{
282
  /* If size is -1, GCC should always optimize the call into strcat.  */
283
  if (size == (__SIZE_TYPE__) -1)
284
    abort ();
285
  ++chk_calls;
286
  if (strlen (d) + strlen (s) >= size)
287
    __chk_fail ();
288
  return strcat (d, s);
289
}
290
 
291
char *
292
strncat (char *s1, const char *s2, __SIZE_TYPE__ n)
293
{
294
  char *dest = s1;
295
  char c;
296
#ifdef __OPTIMIZE__
297
  if (strncat_disallowed && inside_main)
298
    abort();
299
#endif
300
  while (*s1) s1++;
301
  c = '\0';
302
  while (n > 0)
303
    {
304
      c = *s2++;
305
      *s1++ = c;
306
      if (c == '\0')
307
        return dest;
308
      n--;
309
    }
310
  if (c != '\0')
311
    *s1 = '\0';
312
  return dest;
313
}
314
 
315
char *
316
__strncat_chk (char *d, const char *s, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
317
{
318
  __SIZE_TYPE__ len = strlen (d), n1 = n;
319
  const char *s1 = s;
320
 
321
  /* If size is -1, GCC should always optimize the call into strncat.  */
322
  if (size == (__SIZE_TYPE__) -1)
323
    abort ();
324
  ++chk_calls;
325
  while (len < size && n1 > 0)
326
    {
327
      if (*s1++ == '\0')
328
        break;
329
      ++len;
330
      --n1;
331
    }
332
 
333
  if (len >= size)
334
    __chk_fail ();
335
  return strncat (d, s, n);
336
}
337
 
338
/* No chk test in GCC testsuite needs more bytes than this.
339
   As we can't expect vsnprintf to be available on the target,
340
   assume 4096 bytes is enough.  */
341
static char chk_sprintf_buf[4096];
342
 
343
int
344
__sprintf_chk (char *str, int flag, __SIZE_TYPE__ size, const char *fmt, ...)
345
{
346
  int ret;
347
  va_list ap;
348
 
349
  /* If size is -1 and flag 0, GCC should always optimize the call into
350
     sprintf.  */
351
  if (size == (__SIZE_TYPE__) -1 && flag == 0)
352
    abort ();
353
  ++chk_calls;
354
#ifdef __OPTIMIZE__
355
  if (sprintf_disallowed && inside_main)
356
    abort();
357
#endif
358
  va_start (ap, fmt);
359
  ret = vsprintf (chk_sprintf_buf, fmt, ap);
360
  va_end (ap);
361
  if (ret >= 0)
362
    {
363
      if (ret >= size)
364
        __chk_fail ();
365
      memcpy (str, chk_sprintf_buf, ret + 1);
366
    }
367
  return ret;
368
}
369
 
370
int
371
__vsprintf_chk (char *str, int flag, __SIZE_TYPE__ size, const char *fmt,
372
                va_list ap)
373
{
374
  int ret;
375
 
376
  /* If size is -1 and flag 0, GCC should always optimize the call into
377
     vsprintf.  */
378
  if (size == (__SIZE_TYPE__) -1 && flag == 0)
379
    abort ();
380
  ++chk_calls;
381
#ifdef __OPTIMIZE__
382
  if (vsprintf_disallowed && inside_main)
383
    abort();
384
#endif
385
  ret = vsprintf (chk_sprintf_buf, fmt, ap);
386
  if (ret >= 0)
387
    {
388
      if (ret >= size)
389
        __chk_fail ();
390
      memcpy (str, chk_sprintf_buf, ret + 1);
391
    }
392
  return ret;
393
}
394
 
395
int
396
__snprintf_chk (char *str, __SIZE_TYPE__ len, int flag, __SIZE_TYPE__ size,
397
                const char *fmt, ...)
398
{
399
  int ret;
400
  va_list ap;
401
 
402
  /* If size is -1 and flag 0, GCC should always optimize the call into
403
     snprintf.  */
404
  if (size == (__SIZE_TYPE__) -1 && flag == 0)
405
    abort ();
406
  ++chk_calls;
407
  if (size < len)
408
    __chk_fail ();
409
#ifdef __OPTIMIZE__
410
  if (snprintf_disallowed && inside_main)
411
    abort();
412
#endif
413
  va_start (ap, fmt);
414
  ret = vsprintf (chk_sprintf_buf, fmt, ap);
415
  va_end (ap);
416
  if (ret >= 0)
417
    {
418
      if (ret < len)
419
        memcpy (str, chk_sprintf_buf, ret + 1);
420
      else
421
        {
422
          memcpy (str, chk_sprintf_buf, len - 1);
423
          str[len - 1] = '\0';
424
        }
425
    }
426
  return ret;
427
}
428
 
429
int
430
__vsnprintf_chk (char *str, __SIZE_TYPE__ len, int flag, __SIZE_TYPE__ size,
431
                 const char *fmt, va_list ap)
432
{
433
  int ret;
434
 
435
  /* If size is -1 and flag 0, GCC should always optimize the call into
436
     vsnprintf.  */
437
  if (size == (__SIZE_TYPE__) -1 && flag == 0)
438
    abort ();
439
  ++chk_calls;
440
  if (size < len)
441
    __chk_fail ();
442
#ifdef __OPTIMIZE__
443
  if (vsnprintf_disallowed && inside_main)
444
    abort();
445
#endif
446
  ret = vsprintf (chk_sprintf_buf, fmt, ap);
447
  if (ret >= 0)
448
    {
449
      if (ret < len)
450
        memcpy (str, chk_sprintf_buf, ret + 1);
451
      else
452
        {
453
          memcpy (str, chk_sprintf_buf, len - 1);
454
          str[len - 1] = '\0';
455
        }
456
    }
457
  return ret;
458
}
459
 
460
int
461
snprintf (char *str, __SIZE_TYPE__ len, const char *fmt, ...)
462
{
463
  int ret;
464
  va_list ap;
465
 
466
#ifdef __OPTIMIZE__
467
  if (snprintf_disallowed && inside_main)
468
    abort();
469
#endif
470
  va_start (ap, fmt);
471
  ret = vsprintf (chk_sprintf_buf, fmt, ap);
472
  va_end (ap);
473
  if (ret >= 0)
474
    {
475
      if (ret < len)
476
        memcpy (str, chk_sprintf_buf, ret + 1);
477
      else if (len)
478
        {
479
          memcpy (str, chk_sprintf_buf, len - 1);
480
          str[len - 1] = '\0';
481
        }
482
    }
483
  return ret;
484
}
485
 
486
/* uClibc's vsprintf calls vsnprintf.  */
487
#ifndef __UCLIBC__
488
int
489
vsnprintf (char *str, __SIZE_TYPE__ len, const char *fmt, va_list ap)
490
{
491
  int ret;
492
 
493
#ifdef __OPTIMIZE__
494
  if (vsnprintf_disallowed && inside_main)
495
    abort();
496
#endif
497
  ret = vsprintf (chk_sprintf_buf, fmt, ap);
498
  if (ret >= 0)
499
    {
500
      if (ret < len)
501
        memcpy (str, chk_sprintf_buf, ret + 1);
502
      else if (len)
503
        {
504
          memcpy (str, chk_sprintf_buf, len - 1);
505
          str[len - 1] = '\0';
506
        }
507
    }
508
  return ret;
509
}
510
#endif

powered by: WebSVN 2.1.0

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