-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Description
Describe the bug
Application can hit mpu fault before starting main thread. This is caused by stack misuse. In function z_arch_switch_to_main_thread thread entry is provided as forth argument (_main). Then within the function stack pointer is changed:
zephyr/arch/arm/include/kernel_arch_func.h
Line 106 in 269935d
| __set_PSP((u32_t)start_of_main_stack); |
After stack is changed, call to thread entry occurs which is using _main as the argument. If variable is stored on stack, it will be fetched from stack but at that point this is already different stack and value is corrupted:
zephyr/arch/arm/include/kernel_arch_func.h
Line 115 in 269935d
| z_thread_entry(_main, 0, 0, 0); |
So
_main variable will be corrupted.
To Reproduce
Modify hello_world example by using following prj.conf:
CONFIG_MPU_STACK_GUARD=y
CONFIG_NO_OPTIMIZATIONS=y
CONFIG_LOG=y
Steps to reproduce the behavior:
- mkdir build; cd build
- cmake -GNinja -DBOARD=nrf52840_pca10056 ..
- ninja flash
- See error
Fatal fault in essential thread! Spinning...
***** MPU FAULT *****
Instruction Access Violation
***** Hardware exception *****
Current thread ID = 0x200005d8
Faulting instruction address = 0x20001b48
Fatal fault in essential thread! Spinning...
Expected behavior
Sample should start.
Impact
Suddenly system crashes during initialization.