-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
Describe the bug
Environment
- Board: STM32F411CE
- Zephyr Version: 1.3.0
- SDK Version: zephyr-sdk-0.17.0
- Host OS: Windows 11
- Toolchain: arm-zephyr-eabi-gcc 12.2.0
Bug Description
During Zephyr kernel initialization on STM32F411CE, the system gets stuck in an infinite loop within memset()
called from z_bss_zero()
. The issue appears to be a memory address calculation error where the BSS segment boundaries are incorrectly computed.
Reproduction Steps
- Create minimal Zephyr application for STM32F411CE
- Build with
west build -p auto
- Flash to STM32F411CE BlackPill board
- Debug with GDB: the system halts at
z_bss_zero()
→z_early_memset()
→memset()
Expected Behavior
- BSS segment should be properly cleared during initialization
- System should proceed to
main()
function - Application should run normally
Actual Behavior
memset()
enters infinite loop at addresses0x080019e6
-0x080019f0
- BSS memory boundaries are incorrectly calculated
- System never reaches
main()
function
Debug Analysis
GDB Investigation Results:
BSS Segment Information:
(gdb) info symbol __bss_start
z_malloc_heap in section bss
(gdb) info symbol __bss_end
No symbol matches __bss_end. # ← MISSING SYMBOL!
(gdb) print __bss_end - __bss_start
$1 = 244
(gdb) info address z_malloc_heap
Symbol "z_malloc_heap" is static storage at address 0x2000022c.
(gdb) print sizeof(z_malloc_heap)
$2 = 12
memset() Parameters During Hang:
(gdb) info registers r0 r1 r2 r3
r0 0x20000068 # start address (WRONG!)
r1 0x0 # clear value (correct)
r2 0x20000394 # end address (WRONG!)
r3 0x2000006d # current pointer
Critical Issue:
- Expected BSS:
0x2000022c
(12 bytes) - Actual memset range:
0x20000068
to0x20000394
(812 bytes) - Missing symbol:
__bss_end
is undefined
Assembly Analysis:
# Infinite loop in memset():
0x080019e6 <memset+4>: cmp r3, r2
0x080019e8 <memset+6>: bne.n 0x80019ec
0x080019ea <memset+8>: bx lr
0x080019ec <memset+10>: strb.w r1, [r3], #1
0x080019f0: b 0x080019e6 # ← LOOPS HERE
Call Stack:
z_arm_reset() → z_prep_c() → z_bss_zero() → z_early_memset() → memset()
Memory Configuration
CONFIG_SRAM_SIZE=128
CONFIG_SRAM_BASE_ADDRESS=0x20000000
CONFIG_MAIN_STACK_SIZE=129000
Workaround
The issue can be bypassed in GDB by:
(gdb) b main
(gdb) jump main # Skip problematic BSS clearing
Root Cause Analysis
- Missing
__bss_end
symbol in linker script - Incorrect BSS boundary calculation in
z_bss_zero()
- Memory corruption - memset overwrites stack/heap areas
- STM32F411CE-specific memory layout issue
Impact
- Critical: System cannot boot normally
- Affects: All STM32F411CE applications using Zephyr
- Workaround required: Manual GDB intervention needed
Suggested Fix
- Verify linker script defines
__bss_end
symbol correctly - Add boundary validation in
z_bss_zero()
- Add memory protection checks for STM32F411CE
- Review BSS segment calculation logic
Additional Debug Logs
# Zephyr build output shows:
Loading section rom_start, size 0x198 lma 0x8000000
Loading section text, size 0x1920 lma 0x8000198
# ... (no BSS section information)
# Vector table shows incorrect stack pointer:
Contents of section rom_start:
8000000 68f90120 4d080008 7d050008 39080008 h.. M...}...9...
# Stack pointer: 0x2001f968 (should be 0x20020000)
Regression
- This is a regression.
Steps to reproduce
(zephyrproject) PS D:\zephyrproject\zephyrproject\zephyr\samples\sbrviz2_zephyr> arm-none-eabi-gdb build\zephyr\zephyr.elf
GNU gdb (Arm GNU Toolchain 14.2.Rel1 (Build arm-14.52)) 15.2.90.20241130-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=i686-w64-mingw32 --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
https://bugs.linaro.org/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from build\zephyr\zephyr.elf...
(gdb) target extended-remote :3333
`D:\zephyrproject\zephyrproject\zephyr\samples\sbrviz2_zephyr\build\zephyr\zephyr.elf' has changed; re-reading symbols.
could not connect (error 138): ?????????????????????
(gdb) target extended-remote :3333
Remote debugging using :3333
0x1fff1c18 in ?? ()
(gdb) load
Loading section rom_start, size 0x198 lma 0x8000000
Loading section text, size 0x34a4 lma 0x8000198
Loading section .ARM.exidx, size 0x8 lma 0x800363c
Loading section initlevel, size 0x80 lma 0x8003644
Loading section device_area, size 0xf0 lma 0x80036c4
Loading section sw_isr_table, size 0x2b0 lma 0x80037b4
Loading section gpio_driver_api_area, size 0x24 lma 0x8003a64
Loading section reset_driver_api_area, size 0x10 lma 0x8003a88
Loading section clock_control_driver_api_area, size 0x1c lma 0x8003a98
Loading section uart_driver_api_area, size 0x4c lma 0x8003ab4
Loading section rodata, size 0x2bc lma 0x8003b00
Loading section datas, size 0x4c lma 0x8003dbc
Loading section device_states, size 0x18 lma 0x8003e08
Loading section .last_section, size 0x4 lma 0x8003e20
Start address 0x080008ec, load size 15908
Transfer rate: 22 KB/sec, 795 bytes/write.
(gdb) monitor reset halt
Resetting target with halt
Successfully halted device on reset
(gdb) stepi
125 msr BASEPRI, r0
(gdb) stepi
154 ldr r0, =z_interrupt_stacks
(gdb) stepi
155 ldr r1, =CONFIG_ISR_STACK_SIZE + MPU_GUARD_ALIGN_AND_SIZE
(gdb) stepi
156 adds r0, r0, r1
(gdb) stepi
157 msr PSP, r0
(gdb) stepi
158 mrs r0, CONTROL
(gdb) stepi
159 movs r1, #2
(gdb) stepi
160 orrs r0, r1 /* CONTROL_SPSEL_Msk */
(gdb) stepi
161 msr CONTROL, r0
(gdb) stepi
z_arm_reset ()
at D:/zephyrproject/zephyrproject/zephyr/arch/arm/core/cortex_m\reset.S:167
167 isb
(gdb) stepi
174 bl z_prep_c
(gdb) stepi
relocate_vector_table ()
at D:/zephyrproject/zephyrproject/zephyr/arch/arm/core/cortex_m/prep_c.c:56
56 SCB->VTOR = VECTOR_ADDRESS & VTOR_MASK;
(gdb) stepi
0x08000a16 in z_prep_c ()
at D:/zephyrproject/zephyrproject/zephyr/arch/arm/core/cortex_m/prep_c.c:191
191 {
(gdb) stepi
0x08000a18 in relocate_vector_table ()
at D:/zephyrproject/zephyrproject/zephyr/arch/arm/core/cortex_m/prep_c.c:56
56 SCB->VTOR = VECTOR_ADDRESS & VTOR_MASK;
(gdb) stepi
0x08000a1a 56 SCB->VTOR = VECTOR_ADDRESS & VTOR_MASK;
(gdb) stepi
0x08000a1e 56 SCB->VTOR = VECTOR_ADDRESS & VTOR_MASK;
(gdb) stepi
__DSB ()
at D:/zephyrproject/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:271
271 __ASM volatile ("dsb 0xF":::"memory");
(gdb) stepi
__ISB ()
at D:/zephyrproject/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:260
260 __ASM volatile ("isb 0xF":::"memory");
(gdb) stepi
z_arm_floating_point_init ()
at D:/zephyrproject/zephyrproject/zephyr/arch/arm/core/cortex_m/prep_c.c:89
89 SCB->CPACR &= ((CPACR_CP10_Msk | CPACR_CP11_Msk));(CPACR_CP10_Msk | CPACR_CP11_Msk));
(gdb) stepi
0x08000a2c 89 SCB->CPACR &= (
(gdb) stepi
0x08000a30 89 SCB->CPACR &= (~(CPACR_CP10_Msk | CPACR_CP11_Msk));
(gdb) stepi
__get_CONTROL ()
at D:/zephyrproject/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:975
975 __ASM volatile ("MRS %0, control" : "=r" (result) );
(gdb) stepi
z_arm_floating_point_init ()
at D:/zephyrproject/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:1003
1003 __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
(gdb) stepi
0x08000a3c in __set_CONTROL (control=2)
at D:/zephyrproject/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:1003
1003 __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
(gdb) stepi
__ISB ()
at D:/zephyrproject/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include/cmsis_gcc.h:260
260 __ASM volatile ("isb 0xF":::"memory");
(gdb) stepi
z_prep_c ()
at D:/zephyrproject/zephyrproject/zephyr/arch/arm/core/cortex_m/prep_c.c:200
200 z_bss_zero();
(gdb) stepi
z_bss_zero ()
at D:/zephyrproject/zephyrproject/zephyr/kernel/init.c:225
225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
(gdb) stepi
0x08001c82 225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
(gdb) stepi
0x08001c84 225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
(gdb) stepi
0x08001c86 225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
(gdb) stepi
0x08001c88 225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
(gdb) stepi
0x08001c8a 225 z_early_memset(__bss_start, 0, __bss_end - __bss_start);
(gdb) stepi
z_early_memset (dst=0x20000068 <announced_cycles>, c=0,
n=812)
at D:/zephyrproject/zephyrproject/zephyr/kernel/init.c:197
197 (void) memset(dst, c, n);
(gdb) stepi
0x0800355a in memset ()
(gdb) stepi
0x0800355c in memset ()
(gdb) stepi
0x0800355e in memset ()
(gdb) stepi
0x08003560 in memset ()
(gdb) stepi
0x08003564 in memset ()
(gdb) stepi
0x08003568 in memset ()
(gdb) stepi
0x0800355e in memset ()
(gdb) stepi
0x08003560 in memset ()
(gdb) stepi
0x08003564 in memset ()
(gdb) stepi
0x08003568 in memset ()
(gdb) stepi
0x0800355e in memset ()
(gdb) stepi
0x08003560 in memset ()
(gdb) stepi
0x08003564 in memset ()
(gdb) disassemble $pc-8,$pc+8
Dump of assembler code from 0x800355c to 0x800356c:
0x0800355c <memset+2>: mov r3, r0
0x0800355e <memset+4>: cmp r3, r2
0x08003560 <memset+6>: bne.n 0x8003564 <memset+10>
0x08003562 <memset+8>: bx lr
=> 0x08003564 <memset+10>: strb.w r1, [r3], #1
0x08003568 <memset+14>: b.n 0x800355e <memset+4>
0x0800356a <strnlen+0>: push {r4, lr}
End of assembler dump.
(gdb)
Relevant log output
Impact
Annoyance – Minor irritation; no significant impact on usability or functionality.
Environment
No response
Additional Context
No response