platformio / platform-espressif32

Espressif 32: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/espressif32
Apache License 2.0
903 stars 608 forks source link

[ESP32S3] An issue of Heap Corruption, How can I change the Stack Size of "BTC_TASK"? #1461

Closed yuzukihironaka-lefixea closed 2 weeks ago

yuzukihironaka-lefixea commented 2 weeks ago

Hi there, I am experiencing heap corruption as shown below, and I am looking for a way to resolve this issue. This phenomenon frequently occurs during BLE disconnection. Not every time is the content of this crash log, but sometimes a different message is output (but they all seem to be related to the heap).

CORRUPT HEAP: Bad head at 0x3fcc4524. Expected 0xabba1234 got 0x00001234

assert failed: multi_heap_free multi_heap_poisoning.c:259 (head != NULL)

Backtrace: 0x40377c82:0x3fcb8650 0x40381405:0x3fcb8670 0x40387505:0x3fcb8690 0x40387115:0x3fcb87c0 0x40378361:0x3fcb87e0 0x40387535:0x3fcb8800 0x40382812:0x3fcb8820 0x42086759:0x3fcb8840 0x42085604:0x3fcb8860 0x4207e460:0x3fcb8880 0x4207e365:0x3fcb88a0 0x4207e5c4:0x3fcb88d0 0x4207ce82:0x3fcb88f0 0x420647a7:0x3fcb8920 0x4207003d:0x3fcb8950 0x42070c3e:0x3fcb89d0 0x42086307:0x3fcb89f0

  #0  0x40377c82:0x3fcb8650 in panic_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/panic.c:408
  #1  0x40381405:0x3fcb8670 in esp_system_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/esp_system.c:137
  #2  0x40387505:0x3fcb8690 in __assert_func at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/assert.c:85
  #3  0x40387115:0x3fcb87c0 in multi_heap_free at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/heap/multi_heap_poisoning.c:259 (discriminator 1)
  #4  0x40378361:0x3fcb87e0 in heap_caps_free at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/heap/heap_caps.c:382
  #5  0x40387535:0x3fcb8800 in free at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/heap.c:39
  #6  0x40382812:0x3fcb8820 in vQueueDelete at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/freertos/queue.c:2152
  #7  0x42086759:0x3fcb8840 in osi_sem_free at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/common/osi/semaphore.c:75
  #8  0x42085604:0x3fcb8860 in fixed_queue_free at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/common/osi/fixed_queue.c:89
      (inlined by) fixed_queue_free at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/common/osi/fixed_queue.c:71
  #9  0x4207e460:0x3fcb8880 in l2cu_release_ccb at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c:1727
  #10 0x4207e365:0x3fcb88a0 in l2cu_process_fixed_disc_cback at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c:2926
  #11 0x4207e5c4:0x3fcb88d0 in l2cu_release_lcb at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c:212
  #12 0x4207ce82:0x3fcb88f0 in l2c_link_hci_disc_comp at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/l2cap/l2c_link.c:500
  #13 0x420647a7:0x3fcb8920 in btm_acl_disconnected at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/btm/btm_acl.c:2671
  #14 0x4207003d:0x3fcb8950 in btu_hcif_disconnection_comp_evt at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/btu/btu_hcif.c:707
      (inlined by) btu_hcif_process_event at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/btu/btu_hcif.c:204
  #15 0x42070c3e:0x3fcb89d0 in btu_hci_msg_process at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/host/bluedroid/stack/btu/btu_task.c:159
  #16 0x42086307:0x3fcb89f0 in osi_thread_run at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/bt/common/osi/thread.c:165

I executed the following code in the onConnect() override to check the stack size of "BTC_TASK", and it seems that there might not be enough space left.

void BleServerService::onConnect(BLEServer* pServer) {
  log_i("BLE Connected");
  log_e("onConnect Stack size: %d", uxTaskGetStackHighWaterMark(NULL));
  log_i("onConnect Task Name: %s", pcTaskGetName(NULL));
}
[ 55216][E][ble_server_service.cpp:28] onConnect(): onConnect Stack size: 485

According to this issue (https://github.com/espressif/arduino-esp32/issues/7428), changing the CONFIG_BT_BTC_TASK_STACK_SIZE in sdkconfig.h should change the stack size of "BTC_TASK", but even after editing the file, the stack size did not change. If you changed CONFIG_ARDUINO_LOOP_STACK_SIZE in the same file, this was reflected, so I think you have the right file to edit.

Here is my platformio.ini

[env:esp32s3]
platform = espressif32@6.8.1
board = 4d_systems_esp32s3_gen4_r8n16
board_upload.flash_size = 16MB
board_build.flash_mode = qio
board_build.partitions = partitions\large_spiffs_16MB.csv
board_build.f_flash = 80000000L
; build_flags = -DCORE_DEBUG_LEVEL=0 -std=gnu++2a,  -D TINY_GSM_MODEM_SIM7500 ; NONE
build_flags = -DCORE_DEBUG_LEVEL=1 -std=gnu++2a, -D TINY_GSM_MODEM_SIM7500 ; ERROR
; build_flags = -DCORE_DEBUG_LEVEL=2 -std=gnu++2a, -D TINY_GSM_MODEM_SIM7500 ; WARN
; build_flags = -DCORE_DEBUG_LEVEL=3 -std=gnu++2a, -D TINY_GSM_MODEM_SIM7500 ; INFO
; build_flags = -DCORE_DEBUG_LEVEL=4 -std=gnu++2a, -D TINY_GSM_MODEM_SIM7500 ; DEBUG
; build_flags = -DCORE_DEBUG_LEVEL=5 -std=gnu++2a, -D TINY_GSM_MODEM_SIM7500 ; VERBOSE
monitor_speed = 115200
monitor_filters = 
    esp32_exception_decoder
upload_speed = 921600
framework = arduino

How can I change the stack size of BTC_TASK? Also, will changing the stack size resolve this heap corruption issue?

Thanks in advance.

valeros commented 2 weeks ago

Hi @yuzukihironaka-lefixea, first of all, this repository is not the right place to ask project-specific questions.

How can I change the stack size of BTC_TASK? Also, will changing the stack size resolve this heap corruption issue?

You can't change the value of CONFIG_BT_BTC_TASK_STACK_SIZE option because the Arduino framework uses precompiled IDF libraries with a predefined configuration. As a workaround, you can try to convert your app to a mixed IDF/Arduino project (for example) and set the CONFIG_BT_BTC_TASK_STACK_SIZE value as you wish.