Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_cble50y24_81' into 'master'
Browse files Browse the repository at this point in the history
feat(bt/bluedroid): Add print and assert when failed to alloc memory

Closes BLERP-809

See merge request espressif/esp-idf!31634
  • Loading branch information
Isl2017 committed Aug 6, 2024
2 parents e094c92 + 800f360 commit 707dd9b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 59 deletions.
14 changes: 14 additions & 0 deletions components/bt/common/include/bt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL
#define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL

// MEMORY
#if UC_BT_BLE_DYNAMIC_ENV_MEMORY
#define BT_BLE_DYNAMIC_ENV_MEMORY TRUE
#define BTC_DYNAMIC_MEMORY TRUE
Expand All @@ -64,6 +65,19 @@
#define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
#endif

#if UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST TRUE
#else
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

#if UC_BT_ABORT_WHEN_ALLOCATION_FAILS
#define HEAP_ALLOCATION_FAILS_ABORT TRUE
#else
#define HEAP_ALLOCATION_FAILS_ABORT FALSE
#endif

// HCI LOG
#if UC_BT_HCI_LOG_DEBUG_EN
#define BT_HCI_LOG_INCLUDED UC_BT_HCI_LOG_DEBUG_EN
#else
Expand Down
15 changes: 14 additions & 1 deletion components/bt/common/include/bt_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,26 @@
#define UC_BT_BLUFI_ENABLE FALSE
#endif

//MEMORY DEBUG
//MEMORY
#ifdef CONFIG_BT_BLUEDROID_MEM_DEBUG
#define UC_BT_BLUEDROID_MEM_DEBUG TRUE
#else
#define UC_BT_BLUEDROID_MEM_DEBUG FALSE
#endif

#ifdef CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#else
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

#ifdef CONFIG_BT_ABORT_WHEN_ALLOCATION_FAILS
#define UC_BT_ABORT_WHEN_ALLOCATION_FAILS CONFIG_BT_ABORT_WHEN_ALLOCATION_FAILS
#else
#define UC_BT_ABORT_WHEN_ALLOCATION_FAILS FALSE
#endif

//HCI LOG
#ifdef CONFIG_BT_HCI_LOG_DEBUG_EN
#define UC_BT_HCI_LOG_DEBUG_EN TRUE
#else
Expand Down
51 changes: 18 additions & 33 deletions components/bt/common/osi/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,48 +213,33 @@ char *osi_strdup(const char *str)

void *osi_malloc_func(size_t size)
{
#if HEAP_MEMORY_DEBUG
void *p;
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
p = malloc(size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
osi_mem_dbg_record(p, size, __func__, __LINE__);
void *p = osi_malloc_base(size);

if (size != 0 && p == NULL) {
OSI_TRACE_ERROR("malloc failed (caller=%p size=%u)\n", __builtin_return_address(0), size);
#if HEAP_ALLOCATION_FAILS_ABORT
assert(0);
#endif
}

return p;
#else
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
return malloc(size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
#endif /* #if HEAP_MEMORY_DEBUG */
}

void *osi_calloc_func(size_t size)
{
#if HEAP_MEMORY_DEBUG
void *p;
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
p = heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
p = calloc(1, size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
osi_mem_dbg_record(p, size, __func__, __LINE__);
void *p = osi_calloc_base(size);

if (size != 0 && p == NULL) {
OSI_TRACE_ERROR("calloc failed (caller=%p size=%u)\n", __builtin_return_address(0), size);
#if HEAP_ALLOCATION_FAILS_ABORT
assert(0);
#endif
}

return p;
#else
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
#else
return calloc(1, size);
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
#endif /* #if HEAP_MEMORY_DEBUG */
}

void osi_free_func(void *ptr)
{
#if HEAP_MEMORY_DEBUG
osi_mem_dbg_clean(ptr, __func__, __LINE__);
#endif
free(ptr);
}
13 changes: 9 additions & 4 deletions components/bt/common/osi/include/osi/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ do { \

#else

// Memory alloc function without print and assertion
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define osi_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_malloc_base(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_calloc_base(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#else
#define osi_malloc(size) malloc((size))
#define osi_calloc(size) calloc(1, (size))
#define osi_malloc_base(size) malloc((size))
#define osi_calloc_base(size) calloc(1, (size))
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */

// Memory alloc function with print and assertion when fails
#define osi_malloc(size) osi_malloc_func((size))
#define osi_calloc(size) osi_calloc_func((size))
#define osi_free(p) free((p))

#endif /* HEAP_MEMORY_DEBUG */
Expand Down
7 changes: 7 additions & 0 deletions components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,10 @@ config BT_BLE_HIGH_DUTY_ADV_INTERVAL
default n
help
This enable BLE high duty advertising interval feature

config BT_ABORT_WHEN_ALLOCATION_FAILS
bool "Abort when memory allocation fails in BT/BLE stack"
depends on BT_BLUEDROID_ENABLED
default n
help
This enables abort when memory allocation fails
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,6 @@
* Memory reference
**********************************************************/

//MEMORY ALLOCATOR
#ifdef CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
#else
#define UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

//MEMORY DEBUG
#ifdef CONFIG_BT_BLUEDROID_MEM_DEBUG
#define UC_BT_BLUEDROID_MEM_DEBUG CONFIG_BT_BLUEDROID_MEM_DEBUG
#else
#define UC_BT_BLUEDROID_MEM_DEBUG FALSE
#endif

//ESP COEXIST VSC
#ifdef CONFIG_BT_BLUEDROID_ESP_COEX_VSC
#define UC_BT_BLUEDROID_ESP_COEX_VSC CONFIG_BT_BLUEDROID_ESP_COEX_VSC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2378,12 +2378,6 @@ The maximum number of payload octets that the local device can receive in a sing
#define BTSNOOP_MEM FALSE
#endif

#if UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST TRUE
#else
#define HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE
#endif

#include "common/bt_trace.h"

#endif /* BT_TARGET_H */
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/hci/hci_hal_h4.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
}
#endif
pkt_size = BT_PKT_LINKED_HDR_SIZE + BT_HDR_SIZE + len;
linked_pkt = (pkt_linked_item_t *) osi_calloc(pkt_size);
linked_pkt = (pkt_linked_item_t *) osi_calloc_base(pkt_size);
if (!linked_pkt) {
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
hci_adv_credits_consumed(1);
Expand Down

0 comments on commit 707dd9b

Please sign in to comment.