h2zero / esp-nimble-cpp

C++ library for the esp32 NimBLE stack based on and mostly compatible with @nkolban cpp_utils BLE library.
https://h2zero.github.io/esp-nimble-cpp/
Apache License 2.0
181 stars 62 forks source link

Calling NimBLEDevice::deinit before NimBLEDevice::init causes core panic #203

Closed jefflongo closed 2 weeks ago

jefflongo commented 1 month ago
#include "Arduino.h"
#include "NimBLEDevice.h"

extern "C" void app_main()
{
  initArduino();

  Serial.begin(115200);
  while(!Serial)
    ; 

  NimBLEDevice::deinit(true);

  while(1)
    ;
}

With this basic program, the device will continuously crash and reset. It could be argued that calling a "deinit" function before "init" function is undefined behavior, but I don't think a crash is acceptable.

SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x1204
load:0x403cc710,len:0x770
load:0x403ce710,len:0x2d1c
entry 0x403cc710
Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.

Core  0 register dump:
--- Stack dump detected
MEPC    : 0x4200febc  RA      : 0x420086f2  SP      : 0x3fc93510  GP      : 0x3fc8e000  
--- 0x4200febc: ble_npl_sem_init at /redacted/esp-idf/components/bt/host/nimble/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h:246
 (inlined by) nimble_port_stop at /redacted/esp-idf/components/bt/host/nimble/nimble/porting/nimble/src/nimble_port.c:239
0x420086f2: NimBLEDevice::deinit(bool) at /redacted/components/esp-nimble-cpp/src/NimBLEDevice.cpp:954

TP      : 0x3fc7ed4c  T0      : 0x4005890e  T1      : 0x1313bff8  T2      : 0xffffffff  
S0/FP   : 0x3fc8fbe4  S1      : 0x00000000  A0      : 0x00000000  A1      : 0x00000000  
A2      : 0x00000000  A3      : 0x00000001  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x3fc8dac8  A7      : 0x00000000  S2      : 0x00000000  S3      : 0x00000000  
S4      : 0x00000000  S5      : 0x00000000  S6      : 0x00000000  S7      : 0x00000000  
--- 0x4005890e: memset in ROM

S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0xe5b6385f  T4      : 0x80064205  T5      : 0xffffffff  T6      : 0xffffffff  
MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x0000004c  
--- 0x40380001: _vector_table at ??:?

MHARTID : 0x00000000  

--- Backtrace:

0x4200febc in ble_npl_sem_init (tokens=0, sem=0x3fc902cc <ble_hs_stop_sem>) at /redacted/esp-idf/components/bt/host/nimble/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h:246
246        return npl_funcs->p_ble_npl_sem_init(sem, tokens);
#0  0x4200febc in ble_npl_sem_init (tokens=0, sem=0x3fc902cc <ble_hs_stop_sem>) at /redacted/esp-idf/components/bt/host/nimble/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h:246
#1  nimble_port_stop () at /redacted/esp-idf/components/bt/host/nimble/nimble/porting/nimble/src/nimble_port.c:239
#2  0x420086f2 in NimBLEDevice::deinit (clearAll=clearAll@entry=true) at /redacted/components/esp-nimble-cpp/src/NimBLEDevice.cpp:953
#3  0x42008410 in app_main () at /redacted/main/main.cpp:16
#4  0x420368bc in main_task (args=<error reading variable: value has been optimized out>) at /redacted/esp-idf/components/freertos/app_startup.c:208
#5  0x40388a08 in vPortTaskWrapper (pxCode=<optimized out>, pvParameters=<optimized out>) at /redacted/esp-idf/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:202

ELF file SHA256: 8c2c157ba3d1c1bc
h2zero commented 1 month ago

Nice catch, thanks. Amazing that this has never been mentioned before. I'll fix this in the currently open PR.