libc: newlib: libc-hooks: Provide our own implementation of __chk_fail()

The version as shipped in Newlib itself is coded a bit sloppily for an
embedded environment. We thus want to override it (and make it weak, to
allow user apps to override it in turn, if needed). The desired
properties of the implementation are:

1. It should call _write() (Newlib implementation calls write()).
2. It should be minimal (Newlib implementation allocates message
on the stack, i.e. misses "static const").

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
Paul Sokolovsky 2020-05-27 18:56:38 +03:00 committed by Carles Cufí
commit 5f05d6598f

View file

@ -275,6 +275,18 @@ __weak int *__errno(void)
return z_errno(); return z_errno();
} }
/* This function gets called if static buffer overflow detection is enabled
* on stdlib side (Newlib here), in case such an overflow is detected. Newlib
* provides an implementation not suitable for us, so we override it here.
*/
__weak FUNC_NORETURN void __chk_fail(void)
{
static const char chk_fail_msg[] = "* buffer overflow detected *\n";
_write(2, chk_fail_msg, sizeof(chk_fail_msg) - 1);
k_oops();
CODE_UNREACHABLE;
}
#if CONFIG_XTENSA #if CONFIG_XTENSA
extern int _read(int fd, char *buf, int nbytes); extern int _read(int fd, char *buf, int nbytes);
extern int _open(const char *name, int mode); extern int _open(const char *name, int mode);