Skip to content

arch/xtensa/esp32[|s3]: Fix ROM strdup heap issue in USER_HEAP mode #18728

Merged
jerpelea merged 3 commits intoapache:masterfrom
tmedicci:bugfix/esp32_psram_usrheap
Apr 14, 2026
Merged

arch/xtensa/esp32[|s3]: Fix ROM strdup heap issue in USER_HEAP mode #18728
jerpelea merged 3 commits intoapache:masterfrom
tmedicci:bugfix/esp32_psram_usrheap

Conversation

@tmedicci
Copy link
Copy Markdown
Contributor

Summary

  • boards/xtensa/esp32/esp32-devkitc: Fix psram_usrheap defconfig

    • When both CONFIG_ESP32_SPIRAM and CONFIG_ESP32_SPIRAM_USER_HEAP are selected, the device must not select CONFIG_NSH_ARCHINIT. Instead, CONFIG_BOARD_LATE_INITIALIZE must be selected. This is necessary because the SPI flash initialization disables the cache, and only internal memory is then accessible. So, if the SPI flash initialization is performed by the entry task (nsh_main, a regular task), it would use the user heap to allocate memory, which would cause a system crash when the cache is disabled. On the other hand, by selecting CONFIG_BOARD_LATE_INITIALIZE, a kernel thread is dedicated to perform the initialization, and the cache can be safely disabled.
  • arch/xtensa/esp32: Fix ROM strdup heap issue in USER_HEAP mode

    • When ESP32's flat build mode is selected with the external PSRAM on USER_HEAP mode, the ROM strdup internally calls malloc which may be mapped to a different heap than lib_free, causing cross-heap allocation issues and system crashes.

Please note that this PR incorporates (and closes) #18563. Original authorship is kept.

Impact

Impact on user: Yes. Enable using esp32-devkitc:psram_usrheap as expected.

Impact on build: No.

Impact on hardware: Yes. ESP32 with an external PSRAM (which can now be assigned to the user heap)

Impact on documentation: No.

Impact on security: No.

Impact on compatibility: No.

Testing

Build esp32-devkitc:psram_usrheap with DEBUG_ASSERTIONS. Flash the firmware, format the SPI flash partition and check user and kernel heap.

Building

make -j distclean
./tools/configure.sh -S esp32-devkitc:psram_usrheap
kconfig-tweak -e DEBUG_ASSERTIONS
make olddefconfig
make flash EXTRAFLAGS="-Wno-cpp -Werror" ESPTOOL_BINDIR=./ ESPTOOL_PORT=/dev/ttyUSB1 -s -j$(nproc)

Running

After flashing, use picocom -b 115200 /dev/ttyUSB1, to run mksmartfs /dev/smart0 and check available memory with free

Results

*** Booting NuttX ***
dram: lma 0x00001020 vma 0x3ffb5d30 len 0xf74    (3956)
iram: lma 0x00001f9c vma 0x40080000 len 0x951c   (38172)
padd: lma 0x0000b4c8 vma 0x00000000 len 0x4b50   (19280)
imap: lma 0x00010020 vma 0x400d0020 len 0x24cb0  (150704)
padd: lma 0x00034cd8 vma 0x00000000 len 0xb340   (45888)
dmap: lma 0x00040020 vma 0x3f400020 len 0xb738   (46904)
total segments stored 6
[CPU1] ERROR: Failed to mount the FS volume: -19
[CPU1] Smartfs seems unformatted. Did you run 'mksmartfs /dev/smart0'?
[CPU1] ERROR: Failed to setup smartfs
[CPU1] ERROR: Failed to initialize SPI Flash

NuttShell (NSH) NuttX-10.4.0
nsh> mksmartfs /dev/smart0
nsh> free
      total       used       free    maxused    maxfree  nused  nfree name
     300232      19608     280624      20008     164560     53      4 Kmem
    4198096       5320    4192776       5720    4192600     10      2 Umem
nsh>

461911662 and others added 3 commits April 13, 2026 13:57
In ESP32-S3 flat build with USER_HEAP mode, ROM strdup internally calls
malloc which may be mapped to a different heap than lib_free, causing
cross-heap allocation issues and system crashes.

This commit adds a new Kconfig option ESP32S3_DONT_USE_ROM_LIBC to
allow using NuttX libc instead of ROM newlib functions. When USER_HEAP
mode is selected, this option is automatically enabled.

Fixes: 60ca804 ("esp32s3: Fix bug related to the PSRAM-allocated task stack")

Signed-off-by: 461911662 <461911662@qq.com>
Reviewed-by: Tiago Medicci Serrano <tiago.medicci@espressif.com>
Reviewed-by: Almir Okato <almir.okato@espressif.com>
Reviewed-by: Eren Terzioglu <eren.terzioglu@espressif.com>
When ESP32's flat build mode is selected with the external PSRAM
on USER_HEAP mode, the ROM strdup internally calls malloc which may
be mapped to a different heap than lib_free, causing cross-heap
allocation issues and system crashes.

This commit adds a new Kconfig option ESP32_DONT_USE_ROM_LIBC to
allow using NuttX libc instead of ROM newlib functions. When
USER_HEAP mode is selected, this option is automatically enabled.

Signed-off-by: Tiago Medicci Serrano <tiago.medicci@espressif.com>
When both CONFIG_ESP32_SPIRAM and  CONFIG_ESP32_SPIRAM_USER_HEAP
are selected, the device must not select CONFIG_NSH_ARCHINIT.
Instead, CONFIG_BOARD_LATE_INITIALIZE must be selected. This is
necessary because the SPI flash initialization disables the cache,
and only internal memory is then accessible. So, if the  SPI flash
initialization is performed by the entry task (nsh_main, a regular
task), it would use the user heap to allocate memory, which would
cause a system crash when the cache is disabled. On the other hand,
by selecting CONFIG_BOARD_LATE_INITIALIZE, a kernel thread is
dedicated to perform the initialization, and the cache can be
safely disabled.

Signed-off-by: Tiago Medicci Serrano <tiago.medicci@espressif.com>
@github-actions github-actions bot added Arch: xtensa Issues related to the Xtensa architecture Size: S The size of the change in this PR is small Board: xtensa labels Apr 13, 2026
@jerpelea jerpelea merged commit b28d609 into apache:master Apr 14, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: xtensa Issues related to the Xtensa architecture Board: xtensa Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants