Skip to content
Permalink
Browse files

newlib: fix user mode calls to _sbrk

Depending on configuration, this value could end up as
a variable and not an array symbol, causing a crash if
newlib decides to call _sbrk on behalf of a user thread,
which needs to perform arithmetic on it.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
  • Loading branch information...
andrewboie authored and nashif committed May 1, 2019
1 parent a682ded commit b8c560a43906404577e098347bc06766939ed381
Showing with 8 additions and 2 deletions.
  1. +8 −2 lib/libc/newlib/libc-hooks.c
@@ -57,16 +57,18 @@ extern void *_heap_sentry;
#define MAX_HEAP_SIZE ((KB(CONFIG_SRAM_SIZE)) - USED_RAM_SIZE)
#endif

static unsigned char *heap_base = UINT_TO_POINTER(USED_RAM_END_ADDR);

#ifdef CONFIG_USERSPACE
struct k_mem_partition z_malloc_partition;

static int malloc_prepare(struct device *unused)
{
ARG_UNUSED(unused);

#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
z_malloc_partition.start = (u32_t)heap_base;
#else
z_malloc_partition.start = USED_RAM_END_ADDR;
#endif
z_malloc_partition.size = MAX_HEAP_SIZE;
z_malloc_partition.attr = K_MEM_PARTITION_P_RW_U_RW;
return 0;
@@ -222,7 +224,11 @@ void _exit(int status)

void *_sbrk(int count)
{
#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
void *ptr = heap_base + heap_sz;
#else
void *ptr = _end + heap_sz;
#endif

if ((heap_sz + count) < MAX_HEAP_SIZE) {
heap_sz += count;

0 comments on commit b8c560a

Please sign in to comment.
You can’t perform that action at this time.