Skip to content

Commit c7cdb78

Browse files
authored
Fix issues reported by Coverity (#2053)
Fix the potential dead lock issue reported by Coverity code analysis tool.
1 parent 3977f0b commit c7cdb78

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

core/iwasm/common/wasm_shared_memory.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ acquire_wait_info(void *address, AtomicWaitNode *wait_node)
253253
wait_info->wait_list = &wait_info->wait_list_head;
254254
ret = bh_list_init(wait_info->wait_list);
255255
bh_assert(ret == BH_LIST_SUCCESS);
256+
(void)ret;
256257

257258
if (!bh_hash_map_insert(wait_map, address, (void *)wait_info)) {
258259
wasm_runtime_free(wait_info);

core/iwasm/libraries/thread-mgr/thread_manager.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
382382
return ret;
383383
}
384384

385-
/* The caller should lock cluster->lock for thread safety */
386-
bool
387-
wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
385+
static bool
386+
wasm_cluster_del_exec_env_internal(WASMCluster *cluster, WASMExecEnv *exec_env,
387+
bool can_destroy_cluster)
388388
{
389389
bool ret = true;
390390
bh_assert(exec_env->cluster == cluster);
@@ -407,13 +407,26 @@ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
407407
if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
408408
ret = false;
409409

410-
if (cluster->exec_env_list.len == 0) {
411-
/* exec_env_list empty, destroy the cluster */
412-
wasm_cluster_destroy(cluster);
410+
if (can_destroy_cluster) {
411+
if (cluster->exec_env_list.len == 0) {
412+
/* exec_env_list empty, destroy the cluster */
413+
wasm_cluster_destroy(cluster);
414+
}
415+
}
416+
else {
417+
/* Don't destroy cluster as cluster->lock is being used */
413418
}
419+
414420
return ret;
415421
}
416422

423+
/* The caller should lock cluster->lock for thread safety */
424+
bool
425+
wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
426+
{
427+
return wasm_cluster_del_exec_env_internal(cluster, exec_env, true);
428+
}
429+
417430
static WASMExecEnv *
418431
wasm_cluster_search_exec_env(WASMCluster *cluster,
419432
WASMModuleInstanceCommon *module_inst)
@@ -561,7 +574,7 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
561574
/* Free aux stack space */
562575
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
563576
/* Remove exec_env */
564-
wasm_cluster_del_exec_env(cluster, exec_env);
577+
wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
565578
/* Destroy exec_env */
566579
wasm_exec_env_destroy_internal(exec_env);
567580
/* Routine exit, destroy instance */
@@ -621,7 +634,7 @@ thread_manager_start_routine(void *arg)
621634
/* Free aux stack space */
622635
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
623636
/* Remove exec_env */
624-
wasm_cluster_del_exec_env(cluster, exec_env);
637+
wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
625638
/* Destroy exec_env */
626639
wasm_exec_env_destroy_internal(exec_env);
627640
/* Routine exit, destroy instance */
@@ -707,7 +720,7 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
707720
return 0;
708721

709722
fail4:
710-
wasm_cluster_del_exec_env(cluster, new_exec_env);
723+
wasm_cluster_del_exec_env_internal(cluster, new_exec_env, false);
711724
fail3:
712725
/* free the allocated aux stack space */
713726
if (alloc_aux_stack)
@@ -972,7 +985,7 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
972985
/* Free aux stack space */
973986
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
974987
/* Remove exec_env */
975-
wasm_cluster_del_exec_env(cluster, exec_env);
988+
wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
976989
/* Destroy exec_env */
977990
wasm_exec_env_destroy_internal(exec_env);
978991
/* Routine exit, destroy instance */

0 commit comments

Comments
 (0)