6 extern void abort (void);
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
, strcat_disallowed
, strncat_disallowed
;
14 volatile int 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);
19 void __attribute__((noreturn
))
23 __builtin_longjmp (chk_fail_buf
, 1);
28 memcpy (void *dst
, const void *src
, __SIZE_TYPE__ n
)
34 if (memcpy_disallowed
&& inside_main
)
47 __memcpy_chk (void *dst
, const void *src
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
49 /* If size is -1, GCC should always optimize the call into memcpy. */
50 if (size
== (__SIZE_TYPE__
) -1)
55 return memcpy (dst
, src
, n
);
59 mempcpy (void *dst
, const void *src
, __SIZE_TYPE__ n
)
65 if (mempcpy_disallowed
&& inside_main
)
78 __mempcpy_chk (void *dst
, const void *src
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
80 /* If size is -1, GCC should always optimize the call into mempcpy. */
81 if (size
== (__SIZE_TYPE__
) -1)
86 return mempcpy (dst
, src
, n
);
90 memmove (void *dst
, const void *src
, __SIZE_TYPE__ n
)
96 if (memmove_disallowed
&& inside_main
)
113 __memmove_chk (void *dst
, const void *src
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
115 /* If size is -1, GCC should always optimize the call into memmove. */
116 if (size
== (__SIZE_TYPE__
) -1)
121 return memmove (dst
, src
, n
);
125 memset (void *dst
, int c
, __SIZE_TYPE__ n
)
127 /* Single-byte memsets should be done inline when optimisation
130 if (memset_disallowed
&& inside_main
&& n
< 2)
141 __memset_chk (void *dst
, int c
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
143 /* If size is -1, GCC should always optimize the call into memset. */
144 if (size
== (__SIZE_TYPE__
) -1)
149 return memset (dst
, c
, n
);
153 strcpy (char *d
, const char *s
)
157 if (strcpy_disallowed
&& inside_main
)
160 while ((*d
++ = *s
++));
165 __strcpy_chk (char *d
, const char *s
, __SIZE_TYPE__ size
)
167 /* If size is -1, GCC should always optimize the call into strcpy. */
168 if (size
== (__SIZE_TYPE__
) -1)
171 if (strlen (s
) >= size
)
173 return strcpy (d
, s
);
177 stpcpy (char *dst
, const char *src
)
180 if (stpcpy_disallowed
&& inside_main
)
192 __stpcpy_chk (char *d
, const char *s
, __SIZE_TYPE__ size
)
194 /* If size is -1, GCC should always optimize the call into stpcpy. */
195 if (size
== (__SIZE_TYPE__
) -1)
198 if (strlen (s
) >= size
)
200 return stpcpy (d
, s
);
204 strncpy (char *s1
, const char *s2
, __SIZE_TYPE__ n
)
208 if (strncpy_disallowed
&& inside_main
)
211 for (; *s2
&& n
; n
--)
219 __strncpy_chk (char *s1
, const char *s2
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
221 /* If size is -1, GCC should always optimize the call into strncpy. */
222 if (size
== (__SIZE_TYPE__
) -1)
227 return strncpy (s1
, s2
, n
);
231 strcat (char *dst
, const char *src
)
236 if (strcat_disallowed
&& inside_main
)
242 while ((*p
++ = *src
++))
248 __strcat_chk (char *d
, const char *s
, __SIZE_TYPE__ size
)
250 /* If size is -1, GCC should always optimize the call into strcat. */
251 if (size
== (__SIZE_TYPE__
) -1)
254 if (strlen (d
) + strlen (s
) >= size
)
256 return strcat (d
, s
);
260 strncat (char *s1
, const char *s2
, __SIZE_TYPE__ n
)
265 if (strncat_disallowed
&& inside_main
)
284 __strncat_chk (char *d
, const char *s
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
286 __SIZE_TYPE__ len
= strlen (d
), n1
= n
;
289 /* If size is -1, GCC should always optimize the call into strncat. */
290 if (size
== (__SIZE_TYPE__
) -1)
293 while (len
< size
&& n1
> 0)
303 return strncat (d
, s
, n
);
306 /* No chk test in GCC testsuite needs more bytes than this.
307 As we can't expect vsnprintf to be available on the target,
308 assume 4096 bytes is enough. */
309 static char chk_sprintf_buf
[4096];
312 __sprintf_chk (char *str
, int flag
, __SIZE_TYPE__ size
, const char *fmt
, ...)
317 /* If size is -1 and flag 0, GCC should always optimize the call into
319 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
323 if (sprintf_disallowed
&& inside_main
)
327 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
333 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
339 __vsprintf_chk (char *str
, int flag
, __SIZE_TYPE__ size
, const char *fmt
,
344 /* If size is -1 and flag 0, GCC should always optimize the call into
346 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
350 if (vsprintf_disallowed
&& inside_main
)
353 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
358 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
364 __snprintf_chk (char *str
, __SIZE_TYPE__ len
, int flag
, __SIZE_TYPE__ size
,
365 const char *fmt
, ...)
370 /* If size is -1 and flag 0, GCC should always optimize the call into
372 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
378 if (snprintf_disallowed
&& inside_main
)
382 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
387 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
390 memcpy (str
, chk_sprintf_buf
, len
- 1);
398 __vsnprintf_chk (char *str
, __SIZE_TYPE__ len
, int flag
, __SIZE_TYPE__ size
,
399 const char *fmt
, va_list ap
)
403 /* If size is -1 and flag 0, GCC should always optimize the call into
405 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
411 if (vsnprintf_disallowed
&& inside_main
)
414 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
418 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
421 memcpy (str
, chk_sprintf_buf
, len
- 1);
429 snprintf (char *str
, __SIZE_TYPE__ len
, const char *fmt
, ...)
435 if (snprintf_disallowed
&& inside_main
)
439 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
444 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
447 memcpy (str
, chk_sprintf_buf
, len
- 1);
454 /* uClibc's vsprintf calls vsnprintf. */
457 vsnprintf (char *str
, __SIZE_TYPE__ len
, const char *fmt
, va_list ap
)
462 if (vsnprintf_disallowed
&& inside_main
)
465 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
469 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
472 memcpy (str
, chk_sprintf_buf
, len
- 1);