Skip to content

C++: Boot fails with global STL objects #75853

@elazar-vayyar

Description

@elazar-vayyar

Describe the bug
For a C++ app (main.cpp), when declaring a global variable of an STL class, e.g., std::string, the app fails to boot (even the boot banner is not shown on the UART console).
It seems like this problem is related to static construction of STL objects.

This happens since I upgraded to Zephyr v3.7.0-rc*. I did not experience this with v3.6.0, so it looks like a regression.

Zephyr version: v3.7.0-rc3 (commit: fd0e46506fabd4c0c91ecb336a39cb6a6aa2b270)
Zephyr SDK: 0.16.8
C++ implementation: CONFIG_GLIBCXX_LIBCPP=y
Target: esp32_devkitc_wroom/esp32/procpu

To Reproduce

  1. Apply the following patch to samples/cpp/cpp_synchronization
    diff --git a/samples/cpp/cpp_synchronization/prj.conf b/samples/cpp/cpp_synchronization/prj.conf
    index c6b0deca74d..8d9aaba6271 100644
    --- a/samples/cpp/cpp_synchronization/prj.conf
    +++ b/samples/cpp/cpp_synchronization/prj.conf
    @@ -1,2 +1,3 @@
     CONFIG_CPP=y
    +CONFIG_GLIBCXX_LIBCPP=y
     CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=128
    diff --git a/samples/cpp/cpp_synchronization/src/main.cpp b/samples/cpp/cpp_synchronization/src/main.cpp
    index 3ccf39bbb28..1c7db919e76 100644
    --- a/samples/cpp/cpp_synchronization/src/main.cpp
    +++ b/samples/cpp/cpp_synchronization/src/main.cpp
    @@ -23,6 +23,9 @@
     #include <zephyr/arch/cpu.h>
     #include <zephyr/sys/printk.h>
     
    +#include <string>
    +std::string foo;
    +
     /**
      * @class semaphore the basic pure virtual semaphore class
      */
    
    
  2. cd samples/cpp/cpp_synchronization
  3. west build -b "esp32_devkitc_wroom/esp32/procpu"
  4. west flash
  5. west espressif monitor

Expected behavior
Successful sample app boot that starts with the boot banner:

*** Booting Zephyr OS build v3.7.0-rc3 ***
Create semaphore 0x3ffb2934
Create semaphore 0x3ffb2920
main: Hello World!
coop_thread_entry: Hello World!
...

Actual behavior
The bootloader stops after setting up memory segments and does not reach the boot banner stage.

Impact
Cannot run C++ applications with global/static STL objects.
In addition, cannot declare initialized STL rvalue in functions, for example, declaring the following function would also reproduce this problem (probably since it actually creates a static const object as rvalue to return):

std::string foo_func()
{
    return std::string("bar");
}

Logs and console output
UART console log output of samples/cpp/cpp_synchronization when declaring global STL object as suggested in the reproduction steps given above:

$ west espressif monitor
Serial port /dev/ttyUSB0
Connecting.....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting......
Detecting chip type... ESP32
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3ffb0000,len:6016
load:0x40080000,len:33520
0x40080000: _WindowOverflow4 at /home/user/zephyrproject/zephyr/arch/xtensa/core/window_vectors.S:56

entry 0x40083bf8
0x40083bf8: __start at /home/user/zephyrproject/zephyr/soc/espressif/common/loader.c:229

I (29) boot: ESP Simple boot
I (29) boot: compile time Jul 14 2024 12:04:40
W (30) boot: Unicore bootloader
I (30) spi_flash: detected chip: generic
I (32) spi_flash: flash io: dio
I (35) boot: chip revision: v1.0
I (38) boot.esp32: SPI Speed      : 40MHz
I (42) boot.esp32: SPI Mode       : DIO
I (46) boot.esp32: SPI Flash Size : 4MB
I (49) boot: Enabling RNG early entropy source...
[esp32] [INF] DRAM: lma 0x00001020 vma 0x3ffb0000 len 0x1780   (6016)
[esp32] [INF] IRAM: lma 0x000027a8 vma 0x40080000 len 0x82f0   (33520)
0x40080000: _WindowOverflow4 at /home/user/zephyrproject/zephyr/arch/xtensa/core/window_vectors.S:56

[esp32] [INF] padd: lma 0x0000aaa8 vma 0x00000000 len 0x5550   (21840)
[esp32] [INF] IMAP: lma 0x00010000 vma 0x400d0000 len 0x869c   (34460)
0x400d0000: _image_irom_vaddr at ??:?

[esp32] [INF] padd: lma 0x000186a4 vma 0x00000000 len 0x7954   (31060)
[esp32] [INF] DMAP: lma 0x00020000 vma 0x3f400000 len 0x138e4  (80100)
[esp32] [INF] Image with 6 segments
[esp32] [INF] DROM segment: paddr=00020000h, vaddr=3f410000h, size=038E4h ( 14564) map
[esp32] [INF] IROM segment: paddr=00010000h, vaddr=400d0000h, size=0869Ch ( 34460) map

(output stops after the last line quoted above - no boot banner or any other output)

Environment:

  • OS: Linux - Ubuntu 22.04.4 LTS
  • Toolchain: Zephyr SDK 0.16.8
  • Target board: esp32_devkitc_wroom/esp32/procpu
  • C++ implementation (for STL): CONFIG_GLIBCXX_LIBCPP
  • Commit SHA or Version used: v3.7.0-rc3 (fd0e46506fabd4c0c91ecb336a39cb6a6aa2b270)

Metadata

Metadata

Assignees

Labels

area: C++bugThe issue is a bug, or the PR is fixing a bugplatform: ESP32Espressif ESP32priority: lowLow impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions