Skip to content

Commit 0f637cf

Browse files
authored
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 #1790 for details.
1 parent 8fc6413 commit 0f637cf

7 files changed

Lines changed: 72 additions & 26 deletions

File tree

core/config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@
165165
#define WASM_ENABLE_LIB_WASI_THREADS 0
166166
#endif
167167

168+
#ifndef WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION
169+
#define WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION WASM_ENABLE_LIB_WASI_THREADS
170+
#elif WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0 \
171+
&& WASM_ENABLE_LIB_WASI_THREADS == 1
172+
#error "Heap aux stack allocation must be enabled for WASI threads"
173+
#endif
174+
168175
#ifndef WASM_ENABLE_BASE_LIB
169176
#define WASM_ENABLE_BASE_LIB 0
170177
#endif

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,14 +2443,16 @@ wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
24432443
WASMModuleInstance *module_inst =
24442444
(WASMModuleInstance *)exec_env->module_inst;
24452445
uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
2446+
2447+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
2448+
/* Check the aux stack space */
24462449
uint32 data_end = module_inst->module->aux_data_end;
24472450
uint32 stack_bottom = module_inst->module->aux_stack_bottom;
24482451
bool is_stack_before_data = stack_bottom < data_end ? true : false;
2449-
2450-
/* Check the aux stack space, currently we don't allocate space in heap */
24512452
if ((is_stack_before_data && (size > start_offset))
24522453
|| ((!is_stack_before_data) && (start_offset - data_end < size)))
24532454
return false;
2455+
#endif
24542456

24552457
if (stack_top_idx != (uint32)-1) {
24562458
/* The aux stack top is a wasm global,

core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ pthread_start_routine(void *arg)
504504
info_node->exec_env = exec_env;
505505
info_node->u.thread = exec_env->handle;
506506
if (!append_thread_info_node(info_node)) {
507-
wasm_runtime_deinstantiate_internal(module_inst, true);
508507
delete_thread_info_node(info_node);
509508
os_cond_signal(&parent_exec_env->wait_cond);
510509
os_mutex_unlock(&parent_exec_env->wait_lock);
@@ -527,9 +526,6 @@ pthread_start_routine(void *arg)
527526
/* destroy pthread key values */
528527
call_key_destructor(exec_env);
529528

530-
/* routine exit, destroy instance */
531-
wasm_runtime_deinstantiate_internal(module_inst, true);
532-
533529
wasm_runtime_free(routine_args);
534530

535531
/* if the thread is joinable, store the result in its info node,

core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
set (LIB_WASI_THREADS_DIR ${CMAKE_CURRENT_LIST_DIR})
55

6-
add_definitions (-DWASM_ENABLE_LIB_WASI_THREADS=1)
6+
add_definitions (-DWASM_ENABLE_LIB_WASI_THREADS=1 -DWASM_ENABLE_HEAP_AUX_STACK_ALLOCATION=1)
77

88
include_directories(${LIB_WASI_THREADS_DIR})
99

core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
#include "bh_log.h"
7-
#include "wasmtime_ssp.h"
87
#include "thread_manager.h"
98

109
#if WASM_ENABLE_INTERP != 0
@@ -58,9 +57,6 @@ thread_start(void *arg)
5857
wasm_cluster_spread_exception(exec_env);
5958
}
6059

61-
/* routine exit, destroy instance */
62-
wasm_runtime_deinstantiate_internal(module_inst, true);
63-
6460
wasm_runtime_free(thread_arg);
6561
exec_env->thread_arg = NULL;
6662

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

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,18 @@ traverse_list(bh_list *l, list_visitor visitor, void *user_data)
7777
}
7878

7979
static bool
80-
allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
80+
allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
8181
{
82+
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
83+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
84+
WASMModuleInstanceCommon *module_inst =
85+
wasm_exec_env_get_module_inst(exec_env);
86+
87+
*start = wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL);
88+
*size = cluster->stack_size;
89+
90+
return *start != 0;
91+
#else
8292
uint32 i;
8393

8494
/* If the module doesn't have aux stack info,
@@ -100,11 +110,21 @@ allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
100110
}
101111
os_mutex_unlock(&cluster->lock);
102112
return false;
113+
#endif
103114
}
104115

105116
static bool
106-
free_aux_stack(WASMCluster *cluster, uint32 start)
117+
free_aux_stack(WASMExecEnv *exec_env, uint32 start)
107118
{
119+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
120+
WASMModuleInstanceCommon *module_inst =
121+
wasm_exec_env_get_module_inst(exec_env);
122+
123+
wasm_runtime_module_free(module_inst, start);
124+
125+
return true;
126+
#else
127+
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
108128
uint32 i;
109129

110130
for (i = 0; i < cluster_max_thread_num; i++) {
@@ -116,14 +136,14 @@ free_aux_stack(WASMCluster *cluster, uint32 start)
116136
}
117137
}
118138
return false;
139+
#endif
119140
}
120141

121142
WASMCluster *
122143
wasm_cluster_create(WASMExecEnv *exec_env)
123144
{
124145
WASMCluster *cluster;
125-
uint64 total_size;
126-
uint32 aux_stack_start, aux_stack_size, i;
146+
uint32 aux_stack_start, aux_stack_size;
127147

128148
bh_assert(exec_env->cluster == NULL);
129149
if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
@@ -159,21 +179,27 @@ wasm_cluster_create(WASMExecEnv *exec_env)
159179
return cluster;
160180
}
161181

182+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
183+
cluster->stack_size = aux_stack_size;
184+
#else
162185
cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
163186
if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
164187
goto fail;
165188
}
166189
/* Make stack size 16-byte aligned */
167190
cluster->stack_size = cluster->stack_size & (~15);
191+
#endif
168192

169193
/* Set initial aux stack top to the instance and
170194
aux stack boundary to the main exec_env */
171195
if (!wasm_exec_env_set_aux_stack(exec_env, aux_stack_start,
172196
cluster->stack_size))
173197
goto fail;
174198

199+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
175200
if (cluster_max_thread_num != 0) {
176-
total_size = cluster_max_thread_num * sizeof(uint32);
201+
uint64 total_size = cluster_max_thread_num * sizeof(uint32);
202+
uint32 i;
177203
if (total_size >= UINT32_MAX
178204
|| !(cluster->stack_tops =
179205
wasm_runtime_malloc((uint32)total_size))) {
@@ -195,6 +221,7 @@ wasm_cluster_create(WASMExecEnv *exec_env)
195221
cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
196222
}
197223
}
224+
#endif
198225

199226
os_mutex_lock(&cluster_list_lock);
200227
if (bh_list_insert(cluster_list, cluster) != 0) {
@@ -234,10 +261,12 @@ wasm_cluster_destroy(WASMCluster *cluster)
234261

235262
os_mutex_destroy(&cluster->lock);
236263

264+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
237265
if (cluster->stack_tops)
238266
wasm_runtime_free(cluster->stack_tops);
239267
if (cluster->stack_segment_occupied)
240268
wasm_runtime_free(cluster->stack_segment_occupied);
269+
#endif
241270

242271
#if WASM_ENABLE_DEBUG_INTERP != 0
243272
wasm_debug_instance_destroy(cluster);
@@ -273,7 +302,13 @@ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
273302
exec_env->cluster = cluster;
274303

275304
os_mutex_lock(&cluster->lock);
276-
if (bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
305+
if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
306+
LOG_ERROR("thread manager error: "
307+
"maximum number of threads exceeded");
308+
ret = false;
309+
}
310+
311+
if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
277312
ret = false;
278313
os_mutex_unlock(&cluster->lock);
279314
return ret;
@@ -407,7 +442,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
407442
if (!new_exec_env)
408443
goto fail1;
409444

410-
if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
445+
if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
411446
LOG_ERROR("thread manager error: "
412447
"failed to allocate aux stack space for new thread");
413448
goto fail2;
@@ -426,7 +461,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
426461

427462
fail3:
428463
/* free the allocated aux stack space */
429-
free_aux_stack(cluster, aux_stack_start);
464+
free_aux_stack(exec_env, aux_stack_start);
430465
fail2:
431466
wasm_exec_env_destroy(new_exec_env);
432467
fail1:
@@ -443,7 +478,7 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
443478
bh_assert(cluster != NULL);
444479

445480
/* Free aux stack space */
446-
free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
481+
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
447482
wasm_cluster_del_exec_env(cluster, exec_env);
448483
wasm_exec_env_destroy_internal(exec_env);
449484

@@ -457,7 +492,11 @@ thread_manager_start_routine(void *arg)
457492
void *ret;
458493
WASMExecEnv *exec_env = (WASMExecEnv *)arg;
459494
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
495+
WASMModuleInstanceCommon *module_inst =
496+
wasm_exec_env_get_module_inst(exec_env);
497+
460498
bh_assert(cluster != NULL);
499+
bh_assert(module_inst != NULL);
461500

462501
exec_env->handle = os_self_thread();
463502
ret = exec_env->thread_start_routine(exec_env);
@@ -469,7 +508,11 @@ thread_manager_start_routine(void *arg)
469508

470509
/* Routine exit */
471510
/* Free aux stack space */
472-
free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
511+
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
512+
513+
/* routine exit, destroy instance */
514+
wasm_runtime_deinstantiate_internal(module_inst, true);
515+
473516
/* Detach the native thread here to ensure the resources are freed */
474517
wasm_cluster_detach_thread(exec_env);
475518
#if WASM_ENABLE_DEBUG_INTERP != 0
@@ -501,7 +544,7 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
501544
if (!new_exec_env)
502545
return -1;
503546

504-
if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
547+
if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
505548
LOG_ERROR("thread manager error: "
506549
"failed to allocate aux stack space for new thread");
507550
goto fail1;
@@ -532,7 +575,7 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
532575
wasm_cluster_del_exec_env(cluster, new_exec_env);
533576
fail2:
534577
/* free the allocated aux stack space */
535-
free_aux_stack(cluster, aux_stack_start);
578+
free_aux_stack(exec_env, aux_stack_start);
536579
fail1:
537580
wasm_exec_env_destroy(new_exec_env);
538581
return -1;
@@ -762,7 +805,7 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
762805
#endif
763806
/* App exit the thread, free the resources before exit native thread */
764807
/* Free aux stack space */
765-
free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
808+
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
766809
/* Detach the native thread here to ensure the resources are freed */
767810
wasm_cluster_detach_thread(exec_env);
768811
/* Remove and destroy exec_env */

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ struct WASMCluster {
2626
korp_mutex lock;
2727
bh_list exec_env_list;
2828

29+
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
2930
/* The aux stack of a module with shared memory will be
3031
divided into several segments. This array store the
3132
stack top of different segments */
3233
uint32 *stack_tops;
33-
/* Size of every stack segment */
34-
uint32 stack_size;
3534
/* Record which segments are occupied */
3635
bool *stack_segment_occupied;
36+
#endif
37+
/* Size of every stack segment */
38+
uint32 stack_size;
3739
#if WASM_ENABLE_DEBUG_INTERP != 0
3840
WASMDebugInstance *debug_inst;
3941
#endif

0 commit comments

Comments
 (0)