Skip to content

Commit 3415656

Browse files
maribucrasbe
andcommitted
sys/newlib_syscalls_default: wrap newlib's assert implementation
This avoids inconsistent output when external code gets linked in that directly links against newlib's assert implementation (e.g. binary blobs or packages that do not add `core/lib/include` to the include paths). This also greatly benefits wrapping printf, as newlib's `__assert_func()` directly links to internal printf functions of newlib. Co-authored-by: crasbe <[email protected]>
1 parent 64c7624 commit 3415656

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

makefiles/libc/newlib.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ ifeq (1,$(USE_NEWLIB_NANO))
100100
INCLUDES := -isystem $(NEWLIB_NANO_INCLUDE_DIR) $(INCLUDES)
101101
endif
102102
endif
103+
104+
# In case externally compiled code gets linked in, it may use newlib's assert
105+
# implementation. We wrap that to RIOT's assert implementation for consistency.
106+
LINKFLAGS += -Wl,-wrap=__assert_func

pkg/mpaland-printf/Makefile.include

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ LINKFLAGS += -Wl,-wrap=puts
1919
# stdio, we will catch any instance of both mpaland-printf and newlib's stdio
2020
# being both linked in.
2121
LINKFLAGS += -Wl,-wrap=_printf_common
22+
LINKFLAGS += -Wl,-wrap=_fprintf_r
23+
LINKFLAGS += -Wl,-wrap=_printf_r
24+
LINKFLAGS += -Wl,-wrap=iprintf
25+
LINKFLAGS += -Wl,-wrap=fiprintf
2226

2327
# Workaround for bug in the newlib headers shipped with e.g. Ubuntu 24.04 LTS
2428
# not defining PRId64 / PRIu64 / PRIx64 / PRIo64 in <inttypes.h> due to an issue

sys/newlib_syscalls_default/syscalls.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <sys/unistd.h>
3535
#include <unistd.h>
3636

37+
#include "assert.h"
3738
#include "log.h"
3839
#include "modules.h"
3940
#include "periph/pm.h"
@@ -646,3 +647,20 @@ clock_t _times_r(struct _reent *ptr, struct tms *ptms)
646647

647648
return (-1);
648649
}
650+
651+
/**
652+
* @brief Wrapper for newlib's assert implementation
653+
*/
654+
NORETURN
655+
void __wrap___assert_func (const char *file, int line, const char *func, const char *expr)
656+
{
657+
(void)file;
658+
(void)line;
659+
(void)func;
660+
(void)expr;
661+
#ifdef DEBUG_ASSERT_VERBOSE
662+
_assert_failure(file, line);
663+
#else
664+
_assert_panic();
665+
#endif
666+
}

0 commit comments

Comments
 (0)