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

CONFIG_STACK_CANARIES does not work with native_sim #84078

Open
erlingrj opened this issue Jan 16, 2025 · 4 comments
Open

CONFIG_STACK_CANARIES does not work with native_sim #84078

erlingrj opened this issue Jan 16, 2025 · 4 comments
Assignees
Labels
area: Kernel area: native port Host native arch port (native_sim) bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@erlingrj
Copy link

Describe the bug
The stack protection, enabled by CONFIG_STACK_CANARIES=y terminate trivial programs on native_sim when the POSIX API is used. When stepping through the code it appears to happen inside pthread_cond_wait.

To Reproduce
prj.conf

CONFIG_NETWORKING=y
CONFIG_POSIX_API=y
CONFIG_STACK_CANARIES=y

Note: For some reason just selecting CONFIG_NEWLIB_LIBC=y is not enough to get it compiled with newlib and get the stack protection working. It then just defaults back to picolib. But when selecting networking it selects newlib and I get this error.

main.c

#include <zephyr/kernel.h>

int main(void) {
  printf("Hello Before Sleep!\n");
  k_usleep(10000);
  printf("Hello After Sleep!\n");
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.20.0)

set(BOARD native_sim)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

target_sources(app PRIVATE src/main.c)

Compile and run:

west build -p always -t run

Logs and console output

*** Booting Zephyr OS build v3.7.0 ***
Hello Before Sleep!
@ WEST_TOPDIR/deps/zephyr/kernel/compiler_stack_protect.c:39
Exiting due to fatal error

FAILED: zephyr/CMakeFiles/run_native /home/erling/dev/lf-west-template/apps/HelloZephyr/build/zephyr/CMakeFiles/run_native 
cd /home/erling/dev/lf-west-template/apps/HelloZephyr/build && /home/erling/dev/lf-west-template/apps/HelloZephyr/build/zephyr/zephyr.exe
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/erling/dev/lf-west-template/apps/HelloZephyr/build --target run

Environment (please complete the following information):

  • OS: Ubuntu 24.04
  • Toolchain: zephyr-sdk-0.16.8
  • zephyr v3.7.0

Additional context

@erlingrj erlingrj added the bug The issue is a bug, or the PR is fixing a bug label Jan 16, 2025
@kartben kartben added the area: native port Host native arch port (native_sim) label Jan 21, 2025
@kartben
Copy link
Collaborator

kartben commented Jan 21, 2025

cc @aescolar

@kartben kartben added the priority: low Low impact/importance bug label Jan 21, 2025
@aescolar
Copy link
Member

aescolar commented Jan 22, 2025

@erlingrj CONFIG_STACK_CANARIES is not supported in native_sim (in the posix architecture):
https://docs.zephyrproject.org/latest/boards/native/doc/arch_soc.html#posix-arch-unsupported

Sidetrack:

For some reason just selecting CONFIG_NEWLIB_LIBC=y is not enough to get it compiled with newlib

You cannot select NEWLIB_LIBC with native_sim either. That's why kconfig goes back to picolibc.

@aescolar aescolar changed the title Stack protection does not work with native_sim, newlibc and posix API CONFIG_STACK_CANARIES does not work with native_sim Jan 22, 2025
@aescolar
Copy link
Member

aescolar commented Jan 22, 2025

For clarification, there is no need to enable all the options in the bug description. It is enough to do
cmake -GNinja -DBOARD=native_sim ../samples/hello_world/ -DCONFIG_STACK_CANARIES_ALL=y -DCONFIG_ENTROPY_GENERATOR=y in main today.
The issue is just that CONFIG_STACK_CANARIES is not supported in the posix arch/native_sim. As it was never made to work. This is documented and as such not a bug. It could be a feature request. Maybe we could have kconfig prevent this from being set.

From a quick look, it seems the reason why this particular feature does not work in native_sim is that, the kernel init is initializing __stack_chk_guard "too late" in this platform from the point of view of the execution in native_sim, and therefore it looks like it just corrupted the stack guard instead of initializing it which leads to the stack check failing for the host thread that just run z_cstart().

If you want the equivalent functionality when building for native_sim you can add -DEXTRA_CPPFLAGS=-fstack-protector-all to your build command but not adding any of the CONFIG_STACK_CANARIES_* options, so you have the stack protection running but don't have the zephyr init code trying to handle it (badly).

I'd close this as not an issue.

@erlingrj
Copy link
Author

Thank you for the explanation @aescolar that makes sense. It is not a problem for me to disable the stack canaries option, if it is a simple fix, then I think it would be very helpful with a warning/error from Kconfig if board is native_sim and CONFIG_STACK_CANARIES=y.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Kernel area: native port Host native arch port (native_sim) bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

No branches or pull requests

6 participants