fix: add os mempool unregister to prevent mem trampling

This commit is contained in:
cjin
2026-03-17 14:24:34 +08:00
parent d166751d69
commit 27db5fca0e
23 changed files with 105 additions and 453 deletions
+11 -4
View File
@@ -3413,6 +3413,11 @@ ble_att_svr_reset(void)
{
struct ble_att_svr_entry *entry;
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
if (ble_att_svr_ctx == NULL) {
return;
}
#endif
while ((entry = STAILQ_FIRST(&ble_att_svr_list)) != NULL) {
STAILQ_REMOVE_HEAD(&ble_att_svr_list, ha_next);
ble_att_svr_entry_free(entry);
@@ -3442,6 +3447,7 @@ ble_att_svr_free_start_mem(void)
nimble_platform_mem_free(ble_att_svr_entry_mem);
ble_att_svr_entry_mem = NULL;
}
os_mempool_unregister(&ble_att_svr_entry_pool);
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
memset(&ble_att_svr_entry_pool, 0, sizeof(ble_att_svr_entry_pool));
#endif
@@ -3462,7 +3468,7 @@ ble_att_svr_start(void)
ble_att_svr_free_start_mem();
if (ble_hs_max_attrs > 0) {
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
ble_att_svr_entry_mem = nimble_platform_mem_calloc(1,
OS_MEMPOOL_BYTES(ble_hs_max_attrs,
sizeof (struct ble_att_svr_entry)));
@@ -3470,8 +3476,7 @@ ble_att_svr_start(void)
rc = BLE_HS_ENOMEM;
goto err;
}
#endif
#endif
rc = os_mempool_init(&ble_att_svr_entry_pool, ble_hs_max_attrs,
sizeof (struct ble_att_svr_entry),
ble_att_svr_entry_mem, "ble_att_svr_entry_pool");
@@ -3503,6 +3508,7 @@ ble_att_svr_deinit(void)
nimble_platform_mem_free(ble_att_svr_prep_entry_mem);
ble_att_svr_prep_entry_mem = NULL;
}
os_mempool_unregister(&ble_att_svr_prep_entry_pool);
memset(&ble_att_svr_prep_entry_pool, 0, sizeof(ble_att_svr_prep_entry_pool));
ble_att_svr_free_start_mem();
@@ -3538,7 +3544,7 @@ ble_att_svr_init(void)
if (!ble_att_svr_prep_entry_mem) {
return BLE_HS_ENOMEM;
}
#endif
#endif // !MYNEWT_VAL(MP_RUNTIME_ALLOC)
#endif
rc = os_mempool_init(&ble_att_svr_prep_entry_pool,
MYNEWT_VAL(BLE_ATT_SVR_MAX_PREP_ENTRIES),
@@ -3551,6 +3557,7 @@ ble_att_svr_init(void)
nimble_platform_mem_free(ble_att_svr_prep_entry_mem);
ble_att_svr_prep_entry_mem = NULL;
#endif
os_mempool_unregister(&ble_att_svr_prep_entry_pool);
memset(&ble_att_svr_prep_entry_pool, 0,
sizeof(ble_att_svr_prep_entry_pool));
#endif
+2
View File
@@ -681,6 +681,8 @@ void ble_eatt_deinit(void)
ble_eatt_conn_mem = NULL;
}
#endif
os_mempool_unregister(&ble_eatt_sdu_mbuf_mempool);
os_mempool_unregister(&ble_eatt_conn_pool);
nimble_platform_mem_free(ble_eatt_ctx);
ble_eatt_ctx = NULL;
}
+2
View File
@@ -10280,6 +10280,7 @@ err:
ble_gap_update_entry_mem = NULL;
}
#endif
os_mempool_unregister(&ble_gap_update_entry_pool);
nimble_platform_mem_free(ble_gap_vars);
ble_gap_vars = NULL;
}
@@ -10725,6 +10726,7 @@ ble_gap_deinit(void)
ble_gap_update_entry_mem = NULL;
}
#endif
os_mempool_unregister(&ble_gap_update_entry_pool);
nimble_platform_mem_free(ble_gap_vars);
ble_gap_vars = NULL;
}
+2
View File
@@ -6540,6 +6540,7 @@ ble_gattc_init(void)
nimble_platform_mem_free(ble_gattc_proc_mem);
ble_gattc_proc_mem = NULL;
#endif
os_mempool_unregister(&ble_gattc_proc_pool);
memset(&ble_gattc_proc_pool, 0, sizeof(ble_gattc_proc_pool));
return rc;
}
@@ -6600,6 +6601,7 @@ ble_gattc_deinit(void)
ble_gattc_proc_mem = NULL;
}
#endif
os_mempool_unregister(&ble_gattc_proc_pool);
memset(&ble_gattc_proc_pool, 0, sizeof(ble_gattc_proc_pool));
STAILQ_INIT(&ble_gattc_procs);
#if MYNEWT_VAL(BLE_GATTC_AUTO_PAIR)
+5
View File
@@ -2227,28 +2227,33 @@ ble_gattc_cache_conn_free_mem(void)
nimble_platform_mem_free(ble_gattc_cache_conn_mem);
ble_gattc_cache_conn_mem = NULL;
}
os_mempool_unregister(&ble_gattc_cache_conn_pool);
if (ble_gattc_cache_conn_svc_mem) {
nimble_platform_mem_free(ble_gattc_cache_conn_svc_mem);
ble_gattc_cache_conn_svc_mem = NULL;
}
os_mempool_unregister(&ble_gattc_cache_conn_svc_pool);
#if MYNEWT_VAL(BLE_GATT_CACHING_INCLUDE_SERVICES)
if (ble_gattc_cache_conn_incl_svc_mem) {
nimble_platform_mem_free(ble_gattc_cache_conn_incl_svc_mem);
ble_gattc_cache_conn_incl_svc_mem = NULL;
}
os_mempool_unregister(&ble_gattc_cache_conn_incl_svc_pool);
#endif
if (ble_gattc_cache_conn_chr_mem) {
nimble_platform_mem_free(ble_gattc_cache_conn_chr_mem);
ble_gattc_cache_conn_chr_mem = NULL;
}
os_mempool_unregister(&ble_gattc_cache_conn_chr_pool);
if (ble_gattc_cache_conn_dsc_mem) {
nimble_platform_mem_free(ble_gattc_cache_conn_dsc_mem);
ble_gattc_cache_conn_dsc_mem = NULL;
}
os_mempool_unregister(&ble_gattc_cache_conn_dsc_pool);
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
}
+8 -1
View File
@@ -1747,6 +1747,7 @@ ble_gatts_free_mem(void)
#endif
nimble_platform_mem_free(ble_gatts_clt_cfg_mem);
ble_gatts_clt_cfg_mem = NULL;
os_mempool_unregister(&ble_gatts_clt_cfg_pool);
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
}
#endif
@@ -1764,6 +1765,7 @@ ble_gatts_free_mem(void)
nimble_platform_mem_free(ble_gatts_svc_entry_mem);
ble_gatts_svc_entry_mem = NULL;
}
os_mempool_unregister(&ble_gatts_svc_entry_pool);
#else
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
if (ble_hs_max_services > 0) {
@@ -1862,6 +1864,10 @@ ble_gatts_start(void)
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
if (ble_gatts_conn_aware_states == NULL) {
ble_gatts_conn_aware_states = nimble_platform_mem_calloc(1, sizeof(struct ble_gatts_aware_state) * MYNEWT_VAL(BLE_STORE_MAX_BONDS));
if (ble_gatts_conn_aware_states == NULL) {
rc = BLE_HS_ENOMEM;
goto done;
}
}
#else
memset(ble_gatts_conn_aware_states, 0, sizeof ble_gatts_conn_aware_states);
@@ -1925,6 +1931,7 @@ ble_gatts_start(void)
goto done;
}
}
ble_gatts_free_svc_defs();
if (ble_gatts_num_cfgable_chrs == 0) {
@@ -3305,7 +3312,7 @@ ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs)
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
if (ble_gatts_ensure_ctx()) {
rc = BLE_HS_ENOMEM;
rc = BLE_HS_ENOMEM;
goto done;
}
#endif
+2 -1
View File
@@ -1129,7 +1129,6 @@ ble_hs_deinit(void)
#if (MYNEWT_VAL(BLE_HOST_BASED_PRIVACY))
ble_hs_resolv_deinit();
#endif
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
if (ble_hs_ctx) {
ble_hs_ctx->parent_task = NULL;
@@ -1137,6 +1136,8 @@ ble_hs_deinit(void)
nimble_platform_mem_free(ble_hs_ctx->hci_os_event_buf);
ble_hs_ctx->hci_os_event_buf = NULL;
}
os_mempool_unregister(&ble_hs_hci_ev_pool);
nimble_platform_mem_free(ble_hs_ctx);
ble_hs_ctx = NULL;
}
+10 -8
View File
@@ -675,16 +675,17 @@ ble_hs_conn_init(void)
if (rc != 0) {
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
nimble_platform_mem_free(ble_hs_conn_elem_mem);
ble_hs_conn_elem_mem = NULL;
nimble_platform_mem_free(ble_hs_conn_elem_mem);
ble_hs_conn_elem_mem = NULL;
#endif
if (ble_hs_conn_ctx) {
nimble_platform_mem_free(ble_hs_conn_ctx);
ble_hs_conn_ctx = NULL;
}
memset(&ble_hs_conn_pool, 0, sizeof(ble_hs_conn_pool));
os_mempool_unregister(&ble_hs_conn_pool);
if (ble_hs_conn_ctx) {
nimble_platform_mem_free(ble_hs_conn_ctx);
ble_hs_conn_ctx = NULL;
}
memset(&ble_hs_conn_pool, 0, sizeof(ble_hs_conn_pool));
#endif
return BLE_HS_EOS;
return BLE_HS_EOS;
}
SLIST_INIT(&ble_hs_conns);
@@ -706,6 +707,7 @@ ble_hs_conn_deinit(void)
ble_hs_conn_elem_mem = NULL;
}
#endif
os_mempool_unregister(&ble_hs_conn_pool);
memset(&ble_hs_conn_pool, 0, sizeof(ble_hs_conn_pool));
nimble_platform_mem_free(ble_hs_conn_ctx);
ble_hs_conn_ctx = NULL;
+1 -1
View File
@@ -1059,7 +1059,7 @@ void ble_hs_hci_deinit(void)
#endif
/* Clean up mempool first to ensure blocks are free */
os_mempool_clear(&ble_hs_hci_frag_mempool);
os_mempool_unregister(&ble_hs_hci_frag_mempool);
rc = ble_npl_mutex_deinit(&ble_hs_hci_mutex);
BLE_HS_DBG_ASSERT_EVAL(rc == 0);
+4
View File
@@ -102,7 +102,11 @@ ble_hs_hci_cmd_send(uint16_t opcode, uint8_t len, const void *cmddata)
if (rc == 0) {
STATS_INC(ble_hs_stats, hci_cmd);
} else {
#if MYNEWT_VAL(MP_RUNTIME_ALLOC)
ble_transport_free(BLE_HCI_CMD, buf);
#else
ble_transport_free(buf);
#endif
BLE_HS_LOG(DEBUG, "ble_hs_hci_cmd_send failure; rc=%d\n", rc);
}
+2 -1
View File
@@ -393,7 +393,7 @@ void ble_hs_hci_ctx_free(void)
{
if (ble_hci_ctx) {
nimble_platform_mem_free(ble_hci_ctx);
ble_hci_ctx = NULL;
ble_hci_ctx = NULL;
}
}
#else
@@ -1918,6 +1918,7 @@ done:
#else
ble_transport_free((uint8_t *)ev);
#endif
// #endif // CONFIG_SOC_ESP_NIMBLE_CONTROLLER
return rc;
}
+1 -1
View File
@@ -64,7 +64,7 @@ void ble_hs_id_ctx_free(void)
{
if (ble_hs_id_ctx) {
nimble_platform_mem_free(ble_hs_id_ctx);
ble_hs_id_ctx = NULL;
ble_hs_id_ctx = NULL;
}
}
#endif
+9 -7
View File
@@ -206,6 +206,7 @@ ble_hs_periodic_sync_deinit(void)
ble_hs_psync_elem_mem = NULL;
}
#endif
os_mempool_unregister(&ble_hs_periodic_sync_pool);
memset(&ble_hs_periodic_sync_pool, 0, sizeof(ble_hs_periodic_sync_pool));
nimble_platform_mem_free(ble_hs_periodic_ctx);
ble_hs_periodic_ctx = NULL;
@@ -286,15 +287,16 @@ ble_hs_periodic_sync_init(void)
ble_hs_psync_elem_mem, "ble_hs_periodic_disc_pool");
if (rc != 0) {
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
if (ble_hs_psync_elem_mem)
{
nimble_platform_mem_free(ble_hs_psync_elem_mem);
ble_hs_psync_elem_mem = NULL;
}
#endif
if (ble_hs_periodic_ctx)
{
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
if (ble_hs_psync_elem_mem)
{
nimble_platform_mem_free(ble_hs_psync_elem_mem);
ble_hs_psync_elem_mem = NULL;
}
#endif
os_mempool_unregister(&ble_hs_periodic_sync_pool);
nimble_platform_mem_free(ble_hs_periodic_ctx);
ble_hs_periodic_ctx = NULL;
}
+3
View File
@@ -539,6 +539,7 @@ ble_l2cap_init(void)
nimble_platform_mem_free(ble_l2cap_chan_mem);
ble_l2cap_chan_mem = NULL;
#endif
os_mempool_unregister(&ble_l2cap_chan_pool);
memset(&ble_l2cap_chan_pool, 0, sizeof(ble_l2cap_chan_pool));
nimble_platform_mem_free(ble_l2cap_ctx);
@@ -577,6 +578,7 @@ done:
nimble_platform_mem_free(ble_l2cap_chan_mem);
ble_l2cap_chan_mem = NULL;
#endif
os_mempool_unregister(&ble_l2cap_chan_pool);
nimble_platform_mem_free(ble_l2cap_ctx);
ble_l2cap_ctx = NULL;
#endif
@@ -594,6 +596,7 @@ ble_l2cap_deinit(void)
ble_l2cap_chan_mem = NULL;
}
#endif
os_mempool_unregister(&ble_l2cap_chan_pool);
memset(&ble_l2cap_chan_pool, 0, sizeof(ble_l2cap_chan_pool));
ble_l2cap_sig_deinit();
+2
View File
@@ -801,6 +801,7 @@ ble_l2cap_coc_init(void)
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
nimble_platform_mem_free(ble_l2cap_coc_srv_mem);
#endif
os_mempool_unregister(&ble_l2cap_coc_srv_pool);
memset(&ble_l2cap_coc_srv_pool, 0, sizeof(ble_l2cap_coc_srv_pool));
nimble_platform_mem_free(ble_l2cap_coc_ctx);
ble_l2cap_coc_ctx = NULL;
@@ -820,6 +821,7 @@ void ble_l2cap_coc_deinit(void)
ble_l2cap_coc_srv_mem = NULL;
}
#endif
os_mempool_unregister(&ble_l2cap_coc_srv_pool);
memset(&ble_l2cap_coc_srv_pool, 0, sizeof(ble_l2cap_coc_srv_pool));
STAILQ_INIT(&ble_l2cap_coc_srvs);
nimble_platform_mem_free(ble_l2cap_coc_ctx);
+9 -6
View File
@@ -2196,6 +2196,7 @@ ble_l2cap_sig_init(void)
if (!ble_l2cap_sig_proc_mem) {
// free the allocated memory
nimble_platform_mem_free(ble_l2cap_sig_ctx);
ble_l2cap_sig_ctx = NULL;
return BLE_HS_ENOMEM;
}
}
@@ -2215,6 +2216,7 @@ ble_l2cap_sig_init(void)
nimble_platform_mem_free(ble_l2cap_sig_proc_mem);
ble_l2cap_sig_proc_mem = NULL;
#endif
os_mempool_unregister(&ble_l2cap_sig_proc_pool);
nimble_platform_mem_free(ble_l2cap_sig_ctx);
ble_l2cap_sig_ctx = NULL;
#endif
@@ -2228,13 +2230,14 @@ ble_l2cap_sig_init(void)
void
ble_l2cap_sig_deinit(void)
{
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
if (ble_l2cap_sig_proc_mem) {
nimble_platform_mem_free(ble_l2cap_sig_proc_mem);
ble_l2cap_sig_proc_mem = NULL;
}
#endif
if (ble_l2cap_sig_ctx) {
#if !MYNEWT_VAL(MP_RUNTIME_ALLOC)
if (ble_l2cap_sig_proc_mem) {
nimble_platform_mem_free(ble_l2cap_sig_proc_mem);
ble_l2cap_sig_proc_mem = NULL;
}
#endif
os_mempool_unregister(&ble_l2cap_sig_proc_pool);
nimble_platform_mem_free(ble_l2cap_sig_ctx);
ble_l2cap_sig_ctx = NULL;
}
+1
View File
@@ -3345,6 +3345,7 @@ ble_sm_deinit(void)
ble_sm_proc_mem = NULL;
}
#endif
os_mempool_unregister(&ble_sm_proc_pool);
nimble_platform_mem_free(ble_sm_ctx);
ble_sm_ctx = NULL;
}
@@ -71,8 +71,16 @@ ble_transport_alloc_cmd(void)
return r_ble_hci_trans_buf_alloc(ESP_HCI_INTERNAL_BUF_CMD);
}
#if MYNEWT_VAL(MP_RUNTIME_ALLOC)
void
ble_transport_free(uint8_t type, void *buf)
{
r_ble_hci_trans_buf_free(buf);
}
#else
void
ble_transport_free(void *buf)
{
r_ble_hci_trans_buf_free(buf);
}
#endif // MYNEWT_VAL(MP_RUNTIME_ALLOC)
+7 -1
View File
@@ -488,8 +488,14 @@ void ble_buf_free(void)
#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC)
if (ble_trans_ctx) {
os_mempool_unregister(&pool_cmd);
os_mempool_unregister(&pool_evt);
os_mempool_unregister(&pool_evt_lo);
#if POOL_ACL_COUNT > 0
os_mempool_unregister(&pool_acl.mpe_mp);
#endif // POOL_ACL_COUNT > 0
nimble_platform_mem_free(ble_trans_ctx);
ble_trans_ctx = NULL;
ble_trans_ctx = NULL;
}
#endif
}