Skip to content

Commit 2615646

Browse files
authored
Fix on-heap aux stack allocation (bytecodealliance#1865)
Because stack grows from high address towards low address, the value returned by malloc is the end of the stack, not top of the stack. The top of the stack is the end of the allocated space (i.e. address returned by malloc + cluster size). Refer to bytecodealliance#1790.
1 parent 4e5529f commit 2615646

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@ allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
8383
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
8484
WASMModuleInstanceCommon *module_inst =
8585
wasm_exec_env_get_module_inst(exec_env);
86+
uint32 stack_end;
8687

87-
*start = wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL);
88+
stack_end =
89+
wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL);
90+
*start = stack_end + cluster->stack_size;
8891
*size = cluster->stack_size;
8992

90-
return *start != 0;
93+
return stack_end != 0;
9194
#else
9295
uint32 i;
9396

@@ -116,15 +119,18 @@ allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
116119
static bool
117120
free_aux_stack(WASMExecEnv *exec_env, uint32 start)
118121
{
122+
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
123+
119124
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
120125
WASMModuleInstanceCommon *module_inst =
121126
wasm_exec_env_get_module_inst(exec_env);
122127

123-
wasm_runtime_module_free(module_inst, start);
128+
bh_assert(start >= cluster->stack_size);
129+
130+
wasm_runtime_module_free(module_inst, start - cluster->stack_size);
124131

125132
return true;
126133
#else
127-
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
128134
uint32 i;
129135

130136
for (i = 0; i < cluster_max_thread_num; i++) {

0 commit comments

Comments
 (0)