Skip to content

Commit ef3a683

Browse files
authored
Don't call start/initialize in child thread's instantiation (bytecodealliance#1967)
The start/initialize functions of wasi module are to do some initialization work during instantiation, which should be only called one time in the instantiation of main instance. For example, they may initialize the data in linear memory, if the data is changed later by the main instance, and re-initialized again by the child instance, unexpected behaviors may occur. And clear a shadow warning in classic interpreter.
1 parent 50650e4 commit ef3a683

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,19 @@ create_exports(AOTModuleInstance *module_inst, AOTModule *module,
901901
return create_export_funcs(module_inst, module, error_buf, error_buf_size);
902902
}
903903

904+
#if WASM_ENABLE_LIBC_WASI != 0
905+
static bool
906+
execute_initialize_function(AOTModuleInstance *module_inst)
907+
{
908+
AOTFunctionInstance *initialize =
909+
aot_lookup_function(module_inst, "_initialize", NULL);
910+
911+
return !initialize
912+
|| aot_create_exec_env_and_call_function(module_inst, initialize, 0,
913+
NULL);
914+
}
915+
#endif
916+
904917
static bool
905918
execute_post_inst_function(AOTModuleInstance *module_inst)
906919
{
@@ -1121,11 +1134,27 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
11211134
}
11221135
#endif
11231136

1124-
/* Execute __post_instantiate function and start function*/
1125-
if (!execute_post_inst_function(module_inst)
1126-
|| !execute_start_function(module_inst)) {
1127-
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
1128-
goto fail;
1137+
if (!is_sub_inst) {
1138+
if (
1139+
#if WASM_ENABLE_LIBC_WASI != 0
1140+
/*
1141+
* reactor instances may assume that _initialize will be called by
1142+
* the environment at most once, and that none of their other
1143+
* exports are accessed before that call.
1144+
*
1145+
* let the loader decide how to act if there is no _initialize
1146+
* in a reactor
1147+
*/
1148+
!execute_initialize_function(module_inst) ||
1149+
#endif
1150+
/* Execute __post_instantiate function */
1151+
!execute_post_inst_function(module_inst)
1152+
/* Execute the function in "start" section */
1153+
|| !execute_start_function(module_inst)) {
1154+
set_error_buf(error_buf, error_buf_size,
1155+
module_inst->cur_exception);
1156+
goto fail;
1157+
}
11291158
}
11301159

11311160
#if WASM_ENABLE_BULK_MEMORY != 0

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
13671367

13681368
HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
13691369
{
1370-
BrTableCache *node =
1370+
BrTableCache *node_cache =
13711371
bh_list_first_elem(module->module->br_table_cache_list);
13721372
BrTableCache *node_next;
13731373

@@ -1376,13 +1376,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
13761376
#endif
13771377
lidx = POP_I32();
13781378

1379-
while (node) {
1380-
node_next = bh_list_elem_next(node);
1381-
if (node->br_table_op_addr == frame_ip - 1) {
1382-
depth = node->br_depths[lidx];
1379+
while (node_cache) {
1380+
node_next = bh_list_elem_next(node_cache);
1381+
if (node_cache->br_table_op_addr == frame_ip - 1) {
1382+
depth = node_cache->br_depths[lidx];
13831383
goto label_pop_csp_n;
13841384
}
1385-
node = node_next;
1385+
node_cache = node_next;
13861386
}
13871387
bh_assert(0);
13881388
HANDLE_OP_END();

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,24 +1999,27 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
19991999
&module_inst->e->functions[module->start_function];
20002000
}
20012001

2002-
if (
2002+
if (!is_sub_inst) {
2003+
if (
20032004
#if WASM_ENABLE_LIBC_WASI != 0
2004-
/*
2005-
* reactor instances may assume that _initialize will be called by
2006-
* the environment at most once, and that none of their other
2007-
* exports are accessed before that call.
2008-
*
2009-
* let the loader decide how to act if there is no _initialize
2010-
* in a reactor
2011-
*/
2012-
!execute_initialize_function(module_inst) ||
2013-
#endif
2014-
/* Execute __post_instantiate function */
2015-
!execute_post_inst_function(module_inst)
2016-
/* Execute the function in "start" section */
2017-
|| !execute_start_function(module_inst)) {
2018-
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
2019-
goto fail;
2005+
/*
2006+
* reactor instances may assume that _initialize will be called by
2007+
* the environment at most once, and that none of their other
2008+
* exports are accessed before that call.
2009+
*
2010+
* let the loader decide how to act if there is no _initialize
2011+
* in a reactor
2012+
*/
2013+
!execute_initialize_function(module_inst) ||
2014+
#endif
2015+
/* Execute __post_instantiate function */
2016+
!execute_post_inst_function(module_inst)
2017+
/* Execute the function in "start" section */
2018+
|| !execute_start_function(module_inst)) {
2019+
set_error_buf(error_buf, error_buf_size,
2020+
module_inst->cur_exception);
2021+
goto fail;
2022+
}
20202023
}
20212024

20222025
#if WASM_ENABLE_BULK_MEMORY != 0

0 commit comments

Comments
 (0)