Fix wasm_cluster_create_thread issue (#2004)

In wasm_cluster_create_thread, the new_exec_env is added into the cluster's
exec_env list before the thread is created, so other threads can access the
fields of new_exec_env once the cluster->lock is unlocked, while the
new_exec_env's handle is set later inside the thread routine. This may result
in the new_exec_env's handle be invalidly accessed by other threads.
This commit is contained in:
Wenyong Huang 2023-03-06 18:51:13 +08:00 committed by GitHub
parent e6a0184797
commit 04616d398d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -580,7 +580,10 @@ thread_manager_start_routine(void *arg)
os_mutex_lock(&exec_env->wait_lock); os_mutex_lock(&exec_env->wait_lock);
exec_env->handle = os_self_thread(); exec_env->handle = os_self_thread();
/* Notify the parent thread to continue running */
os_cond_signal(&exec_env->wait_cond);
os_mutex_unlock(&exec_env->wait_lock); os_mutex_unlock(&exec_env->wait_lock);
ret = exec_env->thread_start_routine(exec_env); ret = exec_env->thread_start_routine(exec_env);
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
@ -664,13 +667,21 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
new_exec_env->thread_start_routine = thread_routine; new_exec_env->thread_start_routine = thread_routine;
new_exec_env->thread_arg = arg; new_exec_env->thread_arg = arg;
os_mutex_lock(&new_exec_env->wait_lock);
if (0 if (0
!= os_thread_create(&tid, thread_manager_start_routine, != os_thread_create(&tid, thread_manager_start_routine,
(void *)new_exec_env, (void *)new_exec_env,
APP_THREAD_STACK_SIZE_DEFAULT)) { APP_THREAD_STACK_SIZE_DEFAULT)) {
os_mutex_unlock(&new_exec_env->wait_lock);
goto fail4; goto fail4;
} }
/* Wait until the new_exec_env->handle is set to avoid it is
illegally accessed after unlocking cluster->lock */
os_cond_wait(&new_exec_env->wait_cond, &new_exec_env->wait_lock);
os_mutex_unlock(&new_exec_env->wait_lock);
os_mutex_unlock(&cluster->lock); os_mutex_unlock(&cluster->lock);
return 0; return 0;