assert failed: block_trim_free tlsf.c:502 (block_is_free(block) && "block must be free")

andreaz98
Posts: 4
Joined: Wed Mar 30, 2022 1:42 pm

assert failed: block_trim_free tlsf.c:502 (block_is_free(block) && "block must be free")

Postby andreaz98 » Fri Oct 27, 2023 2:38 pm

Hello guys I am having a assert failed: block_trim_free tlsf.c:502 (block_is_free(block) && "block must be free"). I analyzed the backtrace and figured out the failing function is xTaskCreate:

xTaskCreate((void *)z_task_wrapper, "", 5120, z_arg, configMAX_PRIORITIES / 2, task)

The parameters are (left to right)

z_task_wrapper is:

Code: Select all

void z_task_wrapper(__z_task_arg *targ) {
    targ->_fun(targ->_arg);
    vTaskDelete(NULL);
    z_free(targ);
}
z_arg is user defined and contains a pointer to function and a user-defined struct. I checked the user-defined struct and it is not NULL.

configMAX_PRIORITIES expands to 25.

task is

Code: Select all

_z_task_t *task = (_z_task_t *)z_malloc(sizeof(_z_task_t));
    if (task != NULL) {
        (void)memset(task, 0, sizeof(_z_task_t));
        // code that calls xTaskCreate

The full backtrace addr2line'd:

Code: Select all

chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x4008222e:0x3ffb9ec0
0x4008222e: panic_abort at /home/chloe/.platformio/packages/framework-espidf/components/esp_system/panic.c:412
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x400896e9:0x3ffb9ee0
0x400896e9: esp_system_abort at /home/chloe/.platformio/packages/framework-espidf/components/esp_system/esp_system.c:135
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x400902d9:0x3ffb9f00
0x400902d9: __assert_func at /home/chloe/.platformio/packages/framework-espidf/components/newlib/assert.c:78
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x4008e1f4:0x3ffba020
0x4008e1f4: block_trim_free at /home/chloe/.platformio/packages/framework-espidf/components/heap/tlsf/tlsf.c:502
 (inlined by) block_prepare_used at /home/chloe/.platformio/packages/framework-espidf/components/heap/tlsf/tlsf.c:585
 (inlined by) tlsf_malloc at /home/chloe/.platformio/packages/framework-espidf/components/heap/tlsf/tlsf.c:935
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x40082ed5:0x3ffba060
0x40082ed5: heap_caps_malloc_base at /home/chloe/.platformio/packages/framework-espidf/components/heap/heap_caps.c:146
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x40082f33:0x3ffba080
0x40082f33: heap_caps_malloc at /home/chloe/.platformio/packages/framework-espidf/components/heap/heap_caps.c:166
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x4008b246:0x3ffba0a0
0x4008b246: xTaskCreatePinnedToCore at /home/chloe/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/tasks.c:876
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x400d5769:0x3ffba0e0
0x400d5769: xTaskCreate at /home/chloe/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/include/freertos/task.h:451
 (inlined by) _z_task_init at /home/chloe/Desktop/Tesi_Magistrale/esp32_troubleshooting/lib/zenoh-pico/src/system/espidf/system.c:71
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x400d34a6:0x3ffba110
0x400d34a6: _zp_start_read_task at /home/chloe/Desktop/Tesi_Magistrale/esp32_troubleshooting/lib/zenoh-pico/src/net/session.c:202
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x400d2a0d:0x3ffba130
0x400d2a0d: zp_start_read_task at /home/chloe/Desktop/Tesi_Magistrale/esp32_troubleshooting/lib/zenoh-pico/src/api/api.c:1007
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x400d26c6:0x3ffba150
0x400d26c6: app_main at /home/chloe/Desktop/Tesi_Magistrale/esp32_troubleshooting/src/main.c:183
chloe@andrea:~/Desktop/Tesi_Magistrale/esp32_troubleshooting/.pio/build/az-delivery-devkit-v4$ ~/.platformio/packages/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-addr2line -pfiaC -e firmware.elf 0x4015ddbf:0x3ffba1a0
0x4015ddbf: main_task at /home/chloe/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/portable/port_common.c:131 (discriminator 2)
What can be the reason of this assertion failure? How can I investigate more deeply? This failure happens after a DEEPSLEEP_RESET where the user-defined struct is deserialized from the RTC Slow Memory. This failure happens only with a particular example... in the other examples the task is created then deleted without problems. Note that the user-defined struct is deserialized from the RTC Slow Memory even in the other examples.

I am using:
- framework-espidf @ 3.50000.0 (5.0.0)
- tool-cmake @ 3.16.4
- tool-esptoolpy @ 1.40400.0 (4.4.0)
- tool-mkfatfs @ 2.0.1
- tool-mklittlefs @ 1.203.210628 (2.3)
- tool-mkspiffs @ 2.230.0 (2.30)
- tool-ninja @ 1.7.1
- toolchain-esp32ulp @ 1.23500.220830 (2.35.0)
- toolchain-xtensa-esp32 @ 11.2.0+2022r1

on PlatformIO.

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: assert failed: block_trim_free tlsf.c:502 (block_is_free(block) && "block must be free")

Postby ESP_Sprite » Sun Oct 29, 2023 12:21 am

I'd say in most cases this is a buffer overflow or something similar. Note that the assert only detects the issue, the buffer overflow or whatever happens (potentially a long time) before that. Possibly this helps tracking down the issue.

andreaz98
Posts: 4
Joined: Wed Mar 30, 2022 1:42 pm

Re: assert failed: block_trim_free tlsf.c:502 (block_is_free(block) && "block must be free")

Postby andreaz98 » Mon Oct 30, 2023 2:40 pm

Yup, I found the error was in using a not malloc'd memory area. Thank you ESP_Sprite.

Who is online

Users browsing this forum: No registered users and 82 guests