Skip to content

Commit 2b07e8f

Browse files
committed
SPP: Add mutex protection in spp_command_uninit to prevent concurrent access
v/80850 Rootcause: uv_loop_close() marks internal data structures as invalid (e.g., set to -1), but pending callbacks in the queue still reference these invalidated structures. When uv_run() is called later, it processes the queue using corrupted pointers, leading to segmentation faults and crashes. Solution:Introduce a global mutex 'spp_lock' to protect the entire cleanup process in spp_command_uninit. The lock is initialized in spp_command_init and acquired at the beginning of spp_command_uninit. It is released and destroyed at the end of the function to ensure atomic execution of critical operations and prevent data races. Signed-off-by: v-chenghuijin <v-chenghuijin@xiaomi.com>
1 parent 7bbb16b commit 2b07e8f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tools/spp.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "bt_uuid.h"
2525
#include "euv_pipe.h"
2626
#include "uv_thread_loop.h"
27+
#include <inttypes.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include <syslog.h>
2731

2832
typedef struct {
2933
struct list_node node;
@@ -87,6 +91,7 @@ static sem_t spp_send_sem;
8791
static void* spp_app_handle = NULL;
8892
static uv_loop_t spp_thread_loop = { 0 };
8993
static transmit_context_t trans_ctx = { 0 };
94+
static uv_mutex_t spp_lock;
9095

9196
static struct option spp_ping_options[] = {
9297
{ "port", required_argument, 0, 'p' },
@@ -720,15 +725,24 @@ int spp_command_init(void* handle)
720725
thread_loop_run(&spp_thread_loop, true, "spp_client");
721726
spp_app_handle = bt_spp_register_app_with_name(handle, "btool", &spp_cbs);
722727

728+
int ret = uv_mutex_init(&spp_lock);
729+
if (ret != 0) {
730+
syslog(LOG_ERR, "%s mutex error: %d", __func__, ret);
731+
return ret;
732+
}
733+
723734
return 0;
724735
}
725736

726737
void spp_command_uninit(void* handle)
727738
{
739+
uv_mutex_lock(&spp_lock);
728740
bt_spp_unregister_app(handle, spp_app_handle);
729741
sem_destroy(&spp_send_sem);
730742
thread_loop_exit(&spp_thread_loop);
731743
memset(&spp_thread_loop, 0, sizeof(spp_thread_loop));
744+
uv_mutex_unlock(&spp_lock);
745+
uv_mutex_destroy(&spp_lock);
732746
}
733747

734748
int spp_command_exec(void* handle, int argc, char* argv[])

0 commit comments

Comments
 (0)