From 800f360cc3e76faa3875b4076c013f2656af8d74 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Thu, 20 Jun 2024 12:22:37 +0800 Subject: [PATCH] feat(bt/bluedroid): Add print and assert when failed to alloc memory --- components/bt/common/include/bt_common.h | 14 +++++ components/bt/common/include/bt_user_config.h | 15 +++++- components/bt/common/osi/allocator.c | 51 +++++++------------ .../bt/common/osi/include/osi/allocator.h | 13 +++-- components/bt/host/bluedroid/Kconfig.in | 7 +++ .../include/common/bluedroid_user_config.h | 14 ----- .../common/include/common/bt_target.h | 6 --- components/bt/host/bluedroid/hci/hci_hal_h4.c | 2 +- 8 files changed, 63 insertions(+), 59 deletions(-) diff --git a/components/bt/common/include/bt_common.h b/components/bt/common/include/bt_common.h index f3c5ba0e0d..09e7fa50d0 100644 --- a/components/bt/common/include/bt_common.h +++ b/components/bt/common/include/bt_common.h @@ -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 @@ -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 diff --git a/components/bt/common/include/bt_user_config.h b/components/bt/common/include/bt_user_config.h index 029766b7ee..da153405ed 100644 --- a/components/bt/common/include/bt_user_config.h +++ b/components/bt/common/include/bt_user_config.h @@ -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 diff --git a/components/bt/common/osi/allocator.c b/components/bt/common/osi/allocator.c index 4d10e10f47..fba9cf0a9e 100644 --- a/components/bt/common/osi/allocator.c +++ b/components/bt/common/osi/allocator.c @@ -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); } diff --git a/components/bt/common/osi/include/osi/allocator.h b/components/bt/common/osi/include/osi/allocator.h index 579f2b2bb6..25eca3431b 100644 --- a/components/bt/common/osi/include/osi/allocator.h +++ b/components/bt/common/osi/include/osi/allocator.h @@ -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 */ diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index 1346fc5d47..16985a7dff 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -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 diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index ee1f548db3..15d2c55f09 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -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 diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 34ff48bc92..a18c7aadb1 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -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 */ diff --git a/components/bt/host/bluedroid/hci/hci_hal_h4.c b/components/bt/host/bluedroid/hci/hci_hal_h4.c index 47717803ae..da3be90707 100644 --- a/components/bt/host/bluedroid/hci/hci_hal_h4.c +++ b/components/bt/host/bluedroid/hci/hci_hal_h4.c @@ -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);