From 15d2d42dbff03d8b6e12471fbb26a2d379927f69 Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Sat, 29 May 2021 03:56:55 +0900 Subject: [PATCH 1/3] lib: newlib: Add minimum required heap size config This commit adds a new `CONFIG_NEWLIB_MIN_REQUIRED_HEAP_SIZE` config that allows user to specify the minimum required heap size for the newlib heap, and makes `malloc_prepare` validate that the memory space available for the newlib heap is greater than this value. The default minimum required heap size values were empiricially determined, so as to allow the basic standard C functions such as `printf` and `scanf` to work properly. Signed-off-by: Stephanos Ioannidis --- lib/libc/Kconfig | 10 ++++++++++ lib/libc/newlib/libc-hooks.c | 16 ++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/libc/Kconfig b/lib/libc/Kconfig index 39c7d42c42b7c..d167d7221ac45 100644 --- a/lib/libc/Kconfig +++ b/lib/libc/Kconfig @@ -62,6 +62,16 @@ config NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE memory used will be the minimum of this value and the amount of free physical memory at kernel boot. +config NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE + int "Newlib minimum required heap size" + default 2048 if NEWLIB_LIBC_NANO + default 8192 if !NEWLIB_LIBC_NANO + help + Specifies the amount of memory space that must be available for the + newlib heap. An assertion failure message will be displayed during + initialization if the memory space available for the newlib heap is + smaller than this value. + config NEWLIB_LIBC_ALIGNED_HEAP_SIZE int "Newlib aligned heap size" depends on MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT diff --git a/lib/libc/newlib/libc-hooks.c b/lib/libc/newlib/libc-hooks.c index c35619ff5fd2d..fdc8c5ead6ea4 100644 --- a/lib/libc/newlib/libc-hooks.c +++ b/lib/libc/newlib/libc-hooks.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -95,11 +96,11 @@ #endif /* CONFIG_XTENSA */ #endif -#ifdef USE_MALLOC_PREPARE static int malloc_prepare(const struct device *unused) { ARG_UNUSED(unused); +#ifdef USE_MALLOC_PREPARE #ifdef CONFIG_MMU max_heap_size = MIN(CONFIG_NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE, k_mem_free_get()); @@ -111,16 +112,27 @@ static int malloc_prepare(const struct device *unused) } #endif /* CONFIG_MMU */ + #ifdef Z_MALLOC_PARTITION_EXISTS z_malloc_partition.start = (uintptr_t)HEAP_BASE; z_malloc_partition.size = (size_t)MAX_HEAP_SIZE; z_malloc_partition.attr = K_MEM_PARTITION_P_RW_U_RW; #endif /* Z_MALLOC_PARTITION_EXISTS */ +#endif /* USE_MALLOC_PREPARE */ + + /* + * Validate that the memory space available for the newlib heap is + * greater than the minimum required size. + */ + __ASSERT(MAX_HEAP_SIZE >= CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE, + "memory space available for newlib heap is less than the " + "minimum required size specified by " + "CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE"); + return 0; } SYS_INIT(malloc_prepare, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); -#endif /* USE_MALLOC_PREPARE */ /* Current offset from HEAP_BASE of unused memory */ LIBC_BSS static size_t heap_sz; From c468af79ccabe04067cbd67b773c6c791a936f46 Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Sun, 30 May 2021 22:12:32 +0900 Subject: [PATCH 2/3] tests: mem_alloc: Increase heap size for newlib nano test This commit increases the heap size for the newlib nano test to 2048, which is the recommended minimum required to ensure proper operation (see #35799). Signed-off-by: Stephanos Ioannidis --- tests/lib/mem_alloc/prj_newlibnano.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/mem_alloc/prj_newlibnano.conf b/tests/lib/mem_alloc/prj_newlibnano.conf index c7ee608b62722..74f3bd7d344bc 100644 --- a/tests/lib/mem_alloc/prj_newlibnano.conf +++ b/tests/lib/mem_alloc/prj_newlibnano.conf @@ -1,5 +1,5 @@ CONFIG_ZTEST=y CONFIG_NEWLIB_LIBC=y CONFIG_NEWLIB_LIBC_NANO=y -CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=512 +CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=2048 CONFIG_TEST_USERSPACE=y From c78992e42de7fa90459caa2a5f6002db10854e84 Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Sun, 30 May 2021 22:20:22 +0900 Subject: [PATCH 3/3] tests: net: getaddrinfo: Increase newlib heap size This commit increases the newlib heap size to 2048, which is the recommended minimum required to ensure proper operation of the newlib nano used by test (see #35799). Signed-off-by: Stephanos Ioannidis --- tests/net/socket/getaddrinfo/prj.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/net/socket/getaddrinfo/prj.conf b/tests/net/socket/getaddrinfo/prj.conf index 40cbb9d90c36d..957244b34ad9d 100644 --- a/tests/net/socket/getaddrinfo/prj.conf +++ b/tests/net/socket/getaddrinfo/prj.conf @@ -33,7 +33,7 @@ CONFIG_ZTEST=y # User mode requirements CONFIG_TEST_USERSPACE=y CONFIG_HEAP_MEM_POOL_SIZE=128 -CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=256 +CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=2048 # We do not need neighbor discovery etc for this test CONFIG_NET_IPV6_DAD=n