Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newlib: assertion failed when printf is called #40686

Closed
atsushi-shinbo-esoltrinity opened this issue Nov 26, 2021 · 1 comment
Closed

newlib: assertion failed when printf is called #40686

atsushi-shinbo-esoltrinity opened this issue Nov 26, 2021 · 1 comment
Assignees
Labels
area: C Library C Standard Library

Comments

@atsushi-shinbo-esoltrinity

Describe the bug
Assertion failed when newlib/printf is called under a specific combination of configuration items.

To Reproduce

  1. Modify samples/hello_world/src/main.c as following.
#include <zephyr.h>
#include <stdio.h>

void main(void)
{
	printf("Hello World! %s\n", CONFIG_BOARD);	// changed from printk() to printf()
}
  1. Add following lines to samples/hello_world/prj.conf
CONFIG_ASSERT=y
CONFIG_USERSPACE=y
CONFIG_MINIMAL_LIBC=n
CONFIG_NEWLIB_LIBC=y
  1. west build -b hifive1_revb samples/hello_world
  2. cd build/zephyr
  3. $ZEPHYR_SDK_INSTALL_DIR/riscv64-zephyr-elf/bin/riscv64-zephyr-elf-objcopy -O ihex zephyr.elf zephyr.hex
  4. Write zephyr.hex to the Hifive1 revB board and run.
Conbinations 1 2 3
CONFIG_USERSPACE y y n
CONFIG_MINIMAL_LIBC n y n
CONFIG_NEWLIB_LIBC y n y
result NG OK OK

In this example, printf() is called from kernel thread.
However, when CONFIG_USERSPACE=y and CONFIG_NEWLIB_LIBC=y, the assertion has been failed.

Logs and console output

*** Booting Zephyr OS version 2.7.99  ***
ASSERTION FAIL [*lock != ((void *)0)] @ ZEPHYR_BASE/lib/libc/newlib/libc-hooks.c:378
        recursive lock allocation failed

Expected behavior

*** Booting Zephyr OS version 2.7.99  ***
Hello World! hifive1_revb

Environment (please complete the following information):

  • OS: Ubuntu 18.04.5 LTS
  • Toolchain: zephyr-sdk-0.13.1
  • Version: v2.7.99
  • Commit: 1238410
  • Target: hifive1_revb
@atsushi-shinbo-esoltrinity atsushi-shinbo-esoltrinity added the bug The issue is a bug, or the PR is fixing a bug label Nov 26, 2021
@carlescufi carlescufi added the area: C Library C Standard Library label Nov 26, 2021
@stephanosio stephanosio added question and removed bug The issue is a bug, or the PR is fixing a bug labels Nov 26, 2021
@stephanosio
Copy link
Member

When userspace is enabled, you need to ensure the following:

  1. Kernel dynamic object allocation is enabled.
  2. The executing thread is assigned a resource memory pool (heap).

You can ensure (1) by setting CONFIG_DYNAMIC_OBJECTS=y.
You can ensure (2) by either:

a. Defining a dedicated heap and assigning it to the executing thread (refer to the k_thread_heap_assign documentation).
b. Enabling the default system heap and assigning it to the executing thread (set CONFIG_HEAP_MEM_POOL_SIZE to a desired value and call k_thread_system_pool_assign).

For more details, refer to the relevant documentation:
https://docs.zephyrproject.org/latest/reference/usermode/kernelobjects.html#dynamic-objects
https://docs.zephyrproject.org/latest/reference/kernel/memory/heap.html
https://docs.zephyrproject.org/latest/reference/usermode/memory_domain.html?highlight=k_thread_system_pool_assign#thread-resource-pools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library
Projects
None yet
Development

No branches or pull requests

3 participants