Enable aux stack allocations on application heap (#1799)

This is necessary for WASI threads as the aux stack should be managed by the application.
See https://github.com/bytecodealliance/wasm-micro-runtime/issues/1790 for details.
This commit is contained in:
Marcin Kolny
2022-12-19 02:58:12 +00:00
committed by GitHub
parent 8fc641377a
commit 0f637cff07
7 changed files with 72 additions and 26 deletions

View File

@ -77,8 +77,18 @@ traverse_list(bh_list *l, list_visitor visitor, void *user_data)
}
static bool
allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
{
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
WASMModuleInstanceCommon *module_inst =
wasm_exec_env_get_module_inst(exec_env);
*start = wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL);
*size = cluster->stack_size;
return *start != 0;
#else
uint32 i;
/* If the module doesn't have aux stack info,
@ -100,11 +110,21 @@ allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
}
os_mutex_unlock(&cluster->lock);
return false;
#endif
}
static bool
free_aux_stack(WASMCluster *cluster, uint32 start)
free_aux_stack(WASMExecEnv *exec_env, uint32 start)
{
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
WASMModuleInstanceCommon *module_inst =
wasm_exec_env_get_module_inst(exec_env);
wasm_runtime_module_free(module_inst, start);
return true;
#else
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
uint32 i;
for (i = 0; i < cluster_max_thread_num; i++) {
@ -116,14 +136,14 @@ free_aux_stack(WASMCluster *cluster, uint32 start)
}
}
return false;
#endif
}
WASMCluster *
wasm_cluster_create(WASMExecEnv *exec_env)
{
WASMCluster *cluster;
uint64 total_size;
uint32 aux_stack_start, aux_stack_size, i;
uint32 aux_stack_start, aux_stack_size;
bh_assert(exec_env->cluster == NULL);
if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
@ -159,12 +179,16 @@ wasm_cluster_create(WASMExecEnv *exec_env)
return cluster;
}
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
cluster->stack_size = aux_stack_size;
#else
cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
goto fail;
}
/* Make stack size 16-byte aligned */
cluster->stack_size = cluster->stack_size & (~15);
#endif
/* Set initial aux stack top to the instance and
aux stack boundary to the main exec_env */
@ -172,8 +196,10 @@ wasm_cluster_create(WASMExecEnv *exec_env)
cluster->stack_size))
goto fail;
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
if (cluster_max_thread_num != 0) {
total_size = cluster_max_thread_num * sizeof(uint32);
uint64 total_size = cluster_max_thread_num * sizeof(uint32);
uint32 i;
if (total_size >= UINT32_MAX
|| !(cluster->stack_tops =
wasm_runtime_malloc((uint32)total_size))) {
@ -195,6 +221,7 @@ wasm_cluster_create(WASMExecEnv *exec_env)
cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
}
}
#endif
os_mutex_lock(&cluster_list_lock);
if (bh_list_insert(cluster_list, cluster) != 0) {
@ -234,10 +261,12 @@ wasm_cluster_destroy(WASMCluster *cluster)
os_mutex_destroy(&cluster->lock);
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
if (cluster->stack_tops)
wasm_runtime_free(cluster->stack_tops);
if (cluster->stack_segment_occupied)
wasm_runtime_free(cluster->stack_segment_occupied);
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0
wasm_debug_instance_destroy(cluster);
@ -273,7 +302,13 @@ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
exec_env->cluster = cluster;
os_mutex_lock(&cluster->lock);
if (bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
LOG_ERROR("thread manager error: "
"maximum number of threads exceeded");
ret = false;
}
if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
ret = false;
os_mutex_unlock(&cluster->lock);
return ret;
@ -407,7 +442,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
if (!new_exec_env)
goto fail1;
if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
LOG_ERROR("thread manager error: "
"failed to allocate aux stack space for new thread");
goto fail2;
@ -426,7 +461,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
fail3:
/* free the allocated aux stack space */
free_aux_stack(cluster, aux_stack_start);
free_aux_stack(exec_env, aux_stack_start);
fail2:
wasm_exec_env_destroy(new_exec_env);
fail1:
@ -443,7 +478,7 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
bh_assert(cluster != NULL);
/* Free aux stack space */
free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
wasm_cluster_del_exec_env(cluster, exec_env);
wasm_exec_env_destroy_internal(exec_env);
@ -457,7 +492,11 @@ thread_manager_start_routine(void *arg)
void *ret;
WASMExecEnv *exec_env = (WASMExecEnv *)arg;
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
WASMModuleInstanceCommon *module_inst =
wasm_exec_env_get_module_inst(exec_env);
bh_assert(cluster != NULL);
bh_assert(module_inst != NULL);
exec_env->handle = os_self_thread();
ret = exec_env->thread_start_routine(exec_env);
@ -469,7 +508,11 @@ thread_manager_start_routine(void *arg)
/* Routine exit */
/* Free aux stack space */
free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
/* routine exit, destroy instance */
wasm_runtime_deinstantiate_internal(module_inst, true);
/* Detach the native thread here to ensure the resources are freed */
wasm_cluster_detach_thread(exec_env);
#if WASM_ENABLE_DEBUG_INTERP != 0
@ -501,7 +544,7 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
if (!new_exec_env)
return -1;
if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
LOG_ERROR("thread manager error: "
"failed to allocate aux stack space for new thread");
goto fail1;
@ -532,7 +575,7 @@ fail3:
wasm_cluster_del_exec_env(cluster, new_exec_env);
fail2:
/* free the allocated aux stack space */
free_aux_stack(cluster, aux_stack_start);
free_aux_stack(exec_env, aux_stack_start);
fail1:
wasm_exec_env_destroy(new_exec_env);
return -1;
@ -762,7 +805,7 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
#endif
/* App exit the thread, free the resources before exit native thread */
/* Free aux stack space */
free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
/* Detach the native thread here to ensure the resources are freed */
wasm_cluster_detach_thread(exec_env);
/* Remove and destroy exec_env */