Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / testsuite / gcc.c-torture / execute / builtins / lib / chk.c
blobeb305d472299b60d09481b6013298e9ff9288b81
1 #include <stdarg.h>
3 extern void abort (void);
5 extern int inside_main;
6 void *chk_fail_buf[256] __attribute__((aligned (16)));
7 volatile int chk_fail_allowed, chk_calls;
8 volatile int memcpy_disallowed, mempcpy_disallowed, memmove_disallowed;
9 volatile int memset_disallowed, strcpy_disallowed, stpcpy_disallowed;
10 volatile int strncpy_disallowed, strcat_disallowed, strncat_disallowed;
11 volatile int sprintf_disallowed, vsprintf_disallowed;
12 volatile int snprintf_disallowed, vsnprintf_disallowed;
13 extern __SIZE_TYPE__ strlen (const char *);
14 extern int vsprintf (char *, const char *, va_list);
16 void __attribute__((noreturn))
17 __chk_fail (void)
19 if (chk_fail_allowed)
20 __builtin_longjmp (chk_fail_buf, 1);
21 abort ();
24 void *
25 memcpy (void *dst, const void *src, __SIZE_TYPE__ n)
27 const char *srcp;
28 char *dstp;
30 #ifdef __OPTIMIZE__
31 if (memcpy_disallowed && inside_main)
32 abort ();
33 #endif
35 srcp = src;
36 dstp = dst;
37 while (n-- != 0)
38 *dstp++ = *srcp++;
40 return dst;
43 void *
44 __memcpy_chk (void *dst, const void *src, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
46 /* If size is -1, GCC should always optimize the call into memcpy. */
47 if (size == (__SIZE_TYPE__) -1)
48 abort ();
49 ++chk_calls;
50 if (n > size)
51 __chk_fail ();
52 return memcpy (dst, src, n);
55 void *
56 mempcpy (void *dst, const void *src, __SIZE_TYPE__ n)
58 const char *srcp;
59 char *dstp;
61 #ifdef __OPTIMIZE__
62 if (mempcpy_disallowed && inside_main)
63 abort ();
64 #endif
66 srcp = src;
67 dstp = dst;
68 while (n-- != 0)
69 *dstp++ = *srcp++;
71 return dstp;
74 void *
75 __mempcpy_chk (void *dst, const void *src, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
77 /* If size is -1, GCC should always optimize the call into mempcpy. */
78 if (size == (__SIZE_TYPE__) -1)
79 abort ();
80 ++chk_calls;
81 if (n > size)
82 __chk_fail ();
83 return mempcpy (dst, src, n);
86 void *
87 memmove (void *dst, const void *src, __SIZE_TYPE__ n)
89 const char *srcp;
90 char *dstp;
92 #ifdef __OPTIMIZE__
93 if (memmove_disallowed && inside_main)
94 abort ();
95 #endif
97 srcp = src;
98 dstp = dst;
99 if (srcp < dstp)
100 while (n-- != 0)
101 dstp[n] = srcp[n];
102 else
103 while (n-- != 0)
104 *dstp++ = *srcp++;
106 return dst;
109 void *
110 __memmove_chk (void *dst, const void *src, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
112 /* If size is -1, GCC should always optimize the call into memmove. */
113 if (size == (__SIZE_TYPE__) -1)
114 abort ();
115 ++chk_calls;
116 if (n > size)
117 __chk_fail ();
118 return memmove (dst, src, n);
121 void *
122 memset (void *dst, int c, __SIZE_TYPE__ n)
124 /* Single-byte memsets should be done inline when optimisation
125 is enabled. */
126 #ifdef __OPTIMIZE__
127 if (memset_disallowed && inside_main && n < 2)
128 abort ();
129 #endif
131 while (n-- != 0)
132 n[(char *) dst] = c;
134 return dst;
137 void *
138 __memset_chk (void *dst, int c, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
140 /* If size is -1, GCC should always optimize the call into memset. */
141 if (size == (__SIZE_TYPE__) -1)
142 abort ();
143 ++chk_calls;
144 if (n > size)
145 __chk_fail ();
146 return memset (dst, c, n);
149 char *
150 strcpy (char *d, const char *s)
152 char *r = d;
153 #ifdef __OPTIMIZE__
154 if (strcpy_disallowed && inside_main)
155 abort ();
156 #endif
157 while ((*d++ = *s++));
158 return r;
161 char *
162 __strcpy_chk (char *d, const char *s, __SIZE_TYPE__ size)
164 /* If size is -1, GCC should always optimize the call into strcpy. */
165 if (size == (__SIZE_TYPE__) -1)
166 abort ();
167 ++chk_calls;
168 if (strlen (s) >= size)
169 __chk_fail ();
170 return strcpy (d, s);
173 char *
174 stpcpy (char *dst, const char *src)
176 #ifdef __OPTIMIZE__
177 if (stpcpy_disallowed && inside_main)
178 abort ();
179 #endif
181 while (*src != 0)
182 *dst++ = *src++;
184 *dst = 0;
185 return dst;
188 char *
189 __stpcpy_chk (char *d, const char *s, __SIZE_TYPE__ size)
191 /* If size is -1, GCC should always optimize the call into stpcpy. */
192 if (size == (__SIZE_TYPE__) -1)
193 abort ();
194 ++chk_calls;
195 if (strlen (s) >= size)
196 __chk_fail ();
197 return stpcpy (d, s);
200 char *
201 strncpy (char *s1, const char *s2, __SIZE_TYPE__ n)
203 char *dest = s1;
204 #ifdef __OPTIMIZE__
205 if (strncpy_disallowed && inside_main)
206 abort();
207 #endif
208 for (; *s2 && n; n--)
209 *s1++ = *s2++;
210 while (n--)
211 *s1++ = 0;
212 return dest;
215 char *
216 __strncpy_chk (char *s1, const char *s2, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
218 /* If size is -1, GCC should always optimize the call into strncpy. */
219 if (size == (__SIZE_TYPE__) -1)
220 abort ();
221 ++chk_calls;
222 if (n > size)
223 __chk_fail ();
224 return strncpy (s1, s2, n);
227 char *
228 strcat (char *dst, const char *src)
230 char *p = dst;
232 #ifdef __OPTIMIZE__
233 if (strcat_disallowed && inside_main)
234 abort ();
235 #endif
237 while (*p)
238 p++;
239 while ((*p++ = *src++))
241 return dst;
244 char *
245 __strcat_chk (char *d, const char *s, __SIZE_TYPE__ size)
247 /* If size is -1, GCC should always optimize the call into strcat. */
248 if (size == (__SIZE_TYPE__) -1)
249 abort ();
250 ++chk_calls;
251 if (strlen (d) + strlen (s) >= size)
252 __chk_fail ();
253 return strcat (d, s);
256 char *
257 strncat (char *s1, const char *s2, __SIZE_TYPE__ n)
259 char *dest = s1;
260 char c;
261 #ifdef __OPTIMIZE__
262 if (strncat_disallowed && inside_main)
263 abort();
264 #endif
265 while (*s1) s1++;
266 c = '\0';
267 while (n > 0)
269 c = *s2++;
270 *s1++ = c;
271 if (c == '\0')
272 return dest;
273 n--;
275 if (c != '\0')
276 *s1 = '\0';
277 return dest;
280 char *
281 __strncat_chk (char *d, const char *s, __SIZE_TYPE__ n, __SIZE_TYPE__ size)
283 __SIZE_TYPE__ len = strlen (d), n1 = n;
284 const char *s1 = s;
286 /* If size is -1, GCC should always optimize the call into strncat. */
287 if (size == (__SIZE_TYPE__) -1)
288 abort ();
289 ++chk_calls;
290 while (len < size && n1 > 0)
292 if (*s1++ == '\0')
293 break;
294 ++len;
295 --n1;
298 if (len >= size)
299 __chk_fail ();
300 return strncat (d, s, n);
303 /* No chk test in GCC testsuite needs more bytes than this.
304 As we can't expect vsnprintf to be available on the target,
305 assume 4096 bytes is enough. */
306 static char chk_sprintf_buf[4096];
309 __sprintf_chk (char *str, int flag, __SIZE_TYPE__ size, const char *fmt, ...)
311 int ret;
312 va_list ap;
314 /* If size is -1 and flag 0, GCC should always optimize the call into
315 sprintf. */
316 if (size == (__SIZE_TYPE__) -1 && flag == 0)
317 abort ();
318 ++chk_calls;
319 #ifdef __OPTIMIZE__
320 if (sprintf_disallowed && inside_main)
321 abort();
322 #endif
323 va_start (ap, fmt);
324 ret = vsprintf (chk_sprintf_buf, fmt, ap);
325 va_end (ap);
326 if (ret >= 0)
328 if (ret >= size)
329 __chk_fail ();
330 memcpy (str, chk_sprintf_buf, ret + 1);
332 return ret;
336 __vsprintf_chk (char *str, int flag, __SIZE_TYPE__ size, const char *fmt,
337 va_list ap)
339 int ret;
341 /* If size is -1 and flag 0, GCC should always optimize the call into
342 vsprintf. */
343 if (size == (__SIZE_TYPE__) -1 && flag == 0)
344 abort ();
345 ++chk_calls;
346 #ifdef __OPTIMIZE__
347 if (vsprintf_disallowed && inside_main)
348 abort();
349 #endif
350 ret = vsprintf (chk_sprintf_buf, fmt, ap);
351 if (ret >= 0)
353 if (ret >= size)
354 __chk_fail ();
355 memcpy (str, chk_sprintf_buf, ret + 1);
357 return ret;
361 __snprintf_chk (char *str, __SIZE_TYPE__ len, int flag, __SIZE_TYPE__ size,
362 const char *fmt, ...)
364 int ret;
365 va_list ap;
367 /* If size is -1 and flag 0, GCC should always optimize the call into
368 snprintf. */
369 if (size == (__SIZE_TYPE__) -1 && flag == 0)
370 abort ();
371 ++chk_calls;
372 if (size < len)
373 __chk_fail ();
374 #ifdef __OPTIMIZE__
375 if (snprintf_disallowed && inside_main)
376 abort();
377 #endif
378 va_start (ap, fmt);
379 ret = vsprintf (chk_sprintf_buf, fmt, ap);
380 va_end (ap);
381 if (ret >= 0)
383 if (ret < len)
384 memcpy (str, chk_sprintf_buf, ret + 1);
385 else
387 memcpy (str, chk_sprintf_buf, len - 1);
388 str[len - 1] = '\0';
391 return ret;
395 __vsnprintf_chk (char *str, __SIZE_TYPE__ len, int flag, __SIZE_TYPE__ size,
396 const char *fmt, va_list ap)
398 int ret;
400 /* If size is -1 and flag 0, GCC should always optimize the call into
401 vsnprintf. */
402 if (size == (__SIZE_TYPE__) -1 && flag == 0)
403 abort ();
404 ++chk_calls;
405 if (size < len)
406 __chk_fail ();
407 #ifdef __OPTIMIZE__
408 if (vsnprintf_disallowed && inside_main)
409 abort();
410 #endif
411 ret = vsprintf (chk_sprintf_buf, fmt, ap);
412 if (ret >= 0)
414 if (ret < len)
415 memcpy (str, chk_sprintf_buf, ret + 1);
416 else
418 memcpy (str, chk_sprintf_buf, len - 1);
419 str[len - 1] = '\0';
422 return ret;
426 snprintf (char *str, __SIZE_TYPE__ len, const char *fmt, ...)
428 int ret;
429 va_list ap;
431 #ifdef __OPTIMIZE__
432 if (snprintf_disallowed && inside_main)
433 abort();
434 #endif
435 va_start (ap, fmt);
436 ret = vsprintf (chk_sprintf_buf, fmt, ap);
437 va_end (ap);
438 if (ret >= 0)
440 if (ret < len)
441 memcpy (str, chk_sprintf_buf, ret + 1);
442 else if (len)
444 memcpy (str, chk_sprintf_buf, len - 1);
445 str[len - 1] = '\0';
448 return ret;
452 vsnprintf (char *str, __SIZE_TYPE__ len, const char *fmt, va_list ap)
454 int ret;
456 #ifdef __OPTIMIZE__
457 if (vsnprintf_disallowed && inside_main)
458 abort();
459 #endif
460 ret = vsprintf (chk_sprintf_buf, fmt, ap);
461 if (ret >= 0)
463 if (ret < len)
464 memcpy (str, chk_sprintf_buf, ret + 1);
465 else if (len)
467 memcpy (str, chk_sprintf_buf, len - 1);
468 str[len - 1] = '\0';
471 return ret;