esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 36 forks source link

Stack corruption on receiving BLE notification with arduino framework #3607

Closed voed closed 9 months ago

voed commented 2 years ago

The problem

I am trying to parse BLE notification result with ble_client sensor, but running this code using arduino framework causes memory corruption and crash. With esp-idf it works fine. The response from server looks like this:

BE EF 05 02 00 00 00 00 5A 00 00 00 00 00 00 00 01 50 52 4E 41 51 61 71 50 52 41 4E 41 20 20 20 20 20 20 20 20 20 20 20 20 03 F0 03 F2 03 F4 03 F6 10 00 00 00 00 00 00 04 12 20 0A 0A 0A 64 64 64 01 09 01 03 0A 64 0A 0A 64 0A 0A 64 0A 00 01 1E 05 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Or even array of zeros

Which version of ESPHome has the issue?

2022.9

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2022.9.6

What platform are you using?

ESP32

Board

nodemcu-32s

Component causing the issue

ble_client

Example YAML snippet

esp32:
  board: nodemcu-32s
  framework:
    type: arduino
    version: latest
    # Custom sdkconfig options
    #sdkconfig_options:
    #  CONFIG_COMPILER_OPTIMIZATION_SIZE: y
    # Advanced tweaking options
    #advanced:
    #  ignore_efuse_mac_crc: false

# Enable logging
logger:
  level: DEBUG

esp32_ble_tracker:
  scan_parameters:
    active: true

ble_client:
  - mac_address: 00:A0:50:00:00:03
    id: prana

sensor:
    -   platform: ble_client
        id: "prana_sensor"
        ble_client_id: prana
        service_uuid: 'baba'
        characteristic_uuid: 'cccc'
        notify: true
#        lambda: |-
#            ESP_LOGD("prana_sensor", "%d %d", x[30]/10, x[34]/10);
#            return 0.0;

Anything in the logs that might be useful for us?

[D][ble_client:047]: Found device at MAC address [00:A0:50:00:00:03]
[I][ble_client:086]: Attempting BLE connection to 00:a0:50:00:00:03
[D][ble_client.automation:048]: Connection established with 00:a0:50:00:00:03
[I][ble_sensor:031]: [prana_sensor] Connected successfully!
[D][api:102]: Accepted ::FFFF:C0A8:131
[D][api.connection:861]: ESPHome Logs 2022.9.0 (::FFFF:C0A8:131): Connected successfully
[I][ble_client:171]: Service UUID: 0x1800
[I][ble_client:172]:   start_handle: 0x1  end_handle: 0x7
[I][ble_client:384]:  characteristic 0x2A00, handle 0x3, properties 0x2
[I][ble_client:384]:  characteristic 0x2A01, handle 0x5, properties 0x2
[I][ble_client:384]:  characteristic 0x2A04, handle 0x7, properties 0x2
[I][ble_client:171]: Service UUID: 0x1801
[I][ble_client:172]:   start_handle: 0x8  end_handle: 0xb
[I][ble_client:384]:  characteristic 0x2A05, handle 0xa, properties 0x22
[I][ble_client:171]: Service UUID: 0xBABA
[I][ble_client:172]:   start_handle: 0xc  end_handle: 0xf
[I][ble_client:384]:  characteristic 0xCCCC, handle 0xe, properties 0x1a
[D][ble_client.automation:061]: Found characteristic 0000CCCC-0000-1000-8000-00805F9B34FB on device 00:a0:50:00:00:03

CORRUPT HEAP: multi_heap.c:187 detected at 0x3fff3f00
abort() was called at PC 0x4009363e on core 0

ELF file SHA256: 0000000000000000

Backtrace: 0x4008f6e4:0x3ffd40b0 0x4008f961:0x3ffd40d0 0x4009363e:0x3ffd40f0 0x40093bd9:0x3ffd4110 0x400940aa:0x3ffd4130 0x40081eb6:0x3ffd4150 0x40086c61:0x3ffd4170 0x4000bec7:0x3ffd4190 0x401255fd:0x3ffd41b0 0x400909d6:0x3ffd41e0

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_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:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8


### Additional information

_No response_
nagyrobi commented 2 years ago

Add to your config, and flash again:

debug:

sensor:
- platform: template
  name: Free HEAP
  lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  icon: "mdi:memory"
  entity_category: diagnostic
  state_class: measurement
  unit_of_measurement: "b"
  update_interval: 60s
- platform: uptime
  name: Uptime
- platform: debug
  free:
    name: Free MEM

Wait for some time and look at the graphs.