Skip to content

Commit 676c3c7

Browse files
authored
Fix failure about preopen of reactor modules (#1816)
Support modes: - run a commander module only - run a reactor module only - run a commander module and a/multiple reactor modules together commander propagates WASIArguments to reactors
1 parent 5d006ad commit 676c3c7

5 files changed

Lines changed: 132 additions & 52 deletions

File tree

core/iwasm/common/wasm_c_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,8 @@ wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, uint8 val_type_rt,
16281628
ret =
16291629
wasm_externref_obj2ref(inst_comm_rt, v->of.ref, (uint32 *)data);
16301630
break;
1631+
#else
1632+
(void)inst_comm_rt;
16311633
#endif
16321634
default:
16331635
LOG_WARNING("unexpected value type %d", val_type_rt);
@@ -1907,6 +1909,9 @@ wasm_trap_new_internal(wasm_store_t *store,
19071909
frame_instance;
19081910
}
19091911
}
1912+
#else
1913+
(void)store;
1914+
(void)inst_comm_rt;
19101915
#endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
19111916

19121917
return trap;
@@ -2034,6 +2039,7 @@ wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt,
20342039
}
20352040

20362041
foreign->ref_cnt++;
2042+
(void)inst_comm_rt;
20372043
return foreign;
20382044
}
20392045

@@ -4291,6 +4297,7 @@ interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
42914297
imported_func_interp->u.function.func_ptr_linked = import->u.cb;
42924298
import->func_idx_rt = func_idx_rt;
42934299

4300+
(void)inst;
42944301
return true;
42954302
}
42964303

core/iwasm/common/wasm_runtime_common.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,19 +2359,27 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
23592359
{
23602360
WASIArguments *wasi_args = get_wasi_args_from_module(module);
23612361

2362-
if (wasi_args) {
2363-
wasi_args->dir_list = dir_list;
2364-
wasi_args->dir_count = dir_count;
2365-
wasi_args->map_dir_list = map_dir_list;
2366-
wasi_args->map_dir_count = map_dir_count;
2367-
wasi_args->env = env_list;
2368-
wasi_args->env_count = env_count;
2369-
wasi_args->argv = argv;
2370-
wasi_args->argc = (uint32)argc;
2371-
wasi_args->stdio[0] = stdinfd;
2372-
wasi_args->stdio[1] = stdoutfd;
2373-
wasi_args->stdio[2] = stderrfd;
2362+
bh_assert(wasi_args);
2363+
2364+
wasi_args->dir_list = dir_list;
2365+
wasi_args->dir_count = dir_count;
2366+
wasi_args->map_dir_list = map_dir_list;
2367+
wasi_args->map_dir_count = map_dir_count;
2368+
wasi_args->env = env_list;
2369+
wasi_args->env_count = env_count;
2370+
wasi_args->argv = argv;
2371+
wasi_args->argc = (uint32)argc;
2372+
wasi_args->stdio[0] = stdinfd;
2373+
wasi_args->stdio[1] = stdoutfd;
2374+
wasi_args->stdio[2] = stderrfd;
2375+
2376+
#if WASM_ENABLE_MULTI_MODULE != 0
2377+
#if WASM_ENABLE_INTERP != 0
2378+
if (module->module_type == Wasm_Module_Bytecode) {
2379+
wasm_propagate_wasi_args((WASMModule *)module);
23742380
}
2381+
#endif
2382+
#endif
23752383
}
23762384

23772385
void
@@ -2488,7 +2496,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
24882496

24892497
wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
24902498

2491-
/* process argv[0], trip the path and suffix, only keep the program name */
2499+
/* process argv[0], trip the path and suffix, only keep the program name
2500+
*/
24922501
if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
24932502
&argv_buf_size)) {
24942503
set_error_buf(error_buf, error_buf_size,
@@ -3188,7 +3197,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
31883197
uint32 *argv_ret)
31893198
{
31903199
WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
3191-
/* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
3200+
/* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args
3201+
*/
31923202
uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
31933203
uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
31943204
uint32 arg_i32, ptr_len;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
684684
}
685685
}
686686

687-
#if WASM_ENABLE_MULTI_MODULE != 0
687+
#if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
688688
/**
689689
* Find export item of a module with export info:
690690
* module name, field name and export kind
@@ -718,11 +718,15 @@ wasm_loader_find_export(const WASMModule *module, const char *module_name,
718718
return NULL;
719719
}
720720

721+
(void)module_name;
722+
721723
/* since there is a validation in load_export_section(), it is for sure
722724
* export->index is valid*/
723725
return export;
724726
}
727+
#endif
725728

729+
#if WASM_ENABLE_MULTI_MODULE != 0
726730
static WASMFunction *
727731
wasm_loader_resolve_function(const char *module_name, const char *function_name,
728732
const WASMType *expected_function_type,
@@ -1240,6 +1244,8 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
12401244
table->init_size = declare_init_size;
12411245
table->flags = declare_max_size_flag;
12421246
table->max_size = declare_max_size;
1247+
1248+
(void)parent_module;
12431249
return true;
12441250
fail:
12451251
return false;
@@ -1373,6 +1379,8 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
13731379
memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
13741380

13751381
*p_buf = p;
1382+
1383+
(void)parent_module;
13761384
return true;
13771385
fail:
13781386
return false;
@@ -1439,6 +1447,8 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
14391447
global->field_name = global_name;
14401448
global->type = declare_type;
14411449
global->is_mutable = (declare_mutable == 1);
1450+
1451+
(void)parent_module;
14421452
return true;
14431453
fail:
14441454
return false;
@@ -2381,6 +2391,7 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
23812391
}
23822392
#else
23832393
read_leb_uint32(p, p_end, function_index);
2394+
(void)use_init_expr;
23842395
#endif
23852396

23862397
/* since we are using -1 to indicate ref.null */
@@ -2690,6 +2701,7 @@ load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
26902701
}
26912702

26922703
LOG_VERBOSE("Load code segment section success.\n");
2704+
(void)module;
26932705
return true;
26942706
fail:
26952707
return false;
@@ -2900,6 +2912,8 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
29002912

29012913
LOG_VERBOSE("Ignore custom section [%s].", section_name);
29022914

2915+
(void)is_load_from_file_buf;
2916+
(void)module;
29032917
return true;
29042918
fail:
29052919
return false;
@@ -4054,24 +4068,19 @@ load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
40544068
return false;
40554069
}
40564070

4057-
#if (WASM_ENABLE_MULTI_MODULE != 0) && (WASM_ENABLE_LIBC_WASI != 0)
4071+
#if WASM_ENABLE_LIBC_WASI != 0
40584072
/**
40594073
* refer to
40604074
* https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
40614075
*/
40624076
static bool
4063-
check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
4077+
check_wasi_abi_compatibility(const WASMModule *module,
4078+
#if WASM_ENABLE_MULTI_MODULE != 0
4079+
bool main_module,
4080+
#endif
40644081
char *error_buf, uint32 error_buf_size)
40654082
{
40664083
/**
4067-
* need to handle:
4068-
* - non-wasi compatiable modules
4069-
* - a fake wasi compatiable module
4070-
* - a command acts as a main_module
4071-
* - a command acts as a sub_module
4072-
* - a reactor acts as a main_module
4073-
* - a reactor acts as a sub_module
4074-
*
40754084
* be careful with:
40764085
* wasi compatiable modules(command/reactor) which don't import any wasi
40774086
* APIs. Usually, a command has to import a "prox_exit" at least, but a
@@ -4087,7 +4096,19 @@ check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
40874096
* - no one will define either `_start` or `_initialize` on purpose
40884097
* - `_start` should always be `void _start(void)`
40894098
* - `_initialize` should always be `void _initialize(void)`
4099+
*
4100+
*/
4101+
4102+
/* clang-format off */
4103+
/**
4104+
*
4105+
* | | import_wasi_api True | | import_wasi_api False | |
4106+
* | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
4107+
* | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
4108+
* | \_start() Y | N | COMMANDER | N | COMMANDER |
4109+
* | \_start() N | REACTOR | N | REACTOR | OTHERS |
40904110
*/
4111+
/* clang-format on */
40914112

40924113
WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
40934114

@@ -4147,13 +4168,15 @@ check_wasi_abi_compatibility(const WASMModule *module, bool main_module,
41474168
return false;
41484169
}
41494170

4171+
#if WASM_ENABLE_MULTI_MODULE != 0
41504172
/* filter out commands (with `_start`) cases */
41514173
if (start && !main_module) {
41524174
set_error_buf(
41534175
error_buf, error_buf_size,
41544176
"a command (with _start function) can not be a sub-module");
41554177
return false;
41564178
}
4179+
#endif
41574180

41584181
/*
41594182
* it is ok a reactor acts as a main module,
@@ -4193,10 +4216,13 @@ wasm_loader_load(uint8 *buf, uint32 size,
41934216
goto fail;
41944217
}
41954218

4196-
#if (WASM_ENABLE_MULTI_MODULE != 0) && (WASM_ENABLE_LIBC_WASI != 0)
4219+
#if WASM_ENABLE_LIBC_WASI != 0
41974220
/* Check the WASI application ABI */
4198-
if (!check_wasi_abi_compatibility(module, main_module, error_buf,
4199-
error_buf_size)) {
4221+
if (!check_wasi_abi_compatibility(module,
4222+
#if WASM_ENABLE_MULTI_MODULE != 0
4223+
main_module,
4224+
#endif
4225+
error_buf, error_buf_size)) {
42004226
goto fail;
42014227
}
42024228
#endif
@@ -4971,6 +4997,7 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
49714997
}
49724998

49734999
(void)u8;
5000+
(void)exec_env;
49745001
return false;
49755002
fail:
49765003
return false;
@@ -5834,6 +5861,8 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
58345861
i += 2;
58355862
}
58365863

5864+
(void)error_buf;
5865+
(void)error_buf_size;
58375866
return true;
58385867
#if WASM_ENABLE_LABELS_AS_VALUES != 0
58395868
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
@@ -6098,6 +6127,9 @@ wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
60986127
ctx->dynamic_offset -= 2;
60996128
}
61006129
emit_operand(ctx, *(ctx->frame_offset));
6130+
6131+
(void)error_buf;
6132+
(void)error_buf_size;
61016133
return true;
61026134
}
61036135

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,18 @@ export_globals_instantiate(const WASMModule *module,
983983
}
984984
#endif
985985

986+
#if WASM_ENABLE_LIBC_WASI != 0
987+
static bool
988+
execute_initialize_function(WASMModuleInstance *module_inst)
989+
{
990+
WASMFunctionInstance *initialize =
991+
wasm_lookup_function(module_inst, "_initialize", NULL);
992+
return !initialize
993+
|| wasm_create_exec_env_and_call_function(module_inst, initialize, 0,
994+
NULL);
995+
}
996+
#endif
997+
986998
static bool
987999
execute_post_inst_function(WASMModuleInstance *module_inst)
9881000
{
@@ -1175,28 +1187,6 @@ sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
11751187

11761188
sub_module_list_node = bh_list_elem_next(sub_module_list_node);
11771189

1178-
#if WASM_ENABLE_LIBC_WASI != 0
1179-
{
1180-
/*
1181-
* reactor instances may assume that _initialize will be called by
1182-
* the environment at most once, and that none of their other
1183-
* exports are accessed before that call.
1184-
*
1185-
* let the loader decide how to act if there is no _initialize
1186-
* in a reactor
1187-
*/
1188-
WASMFunctionInstance *initialize =
1189-
wasm_lookup_function(sub_module_inst, "_initialize", NULL);
1190-
if (initialize
1191-
&& !wasm_create_exec_env_and_call_function(
1192-
sub_module_inst, initialize, 0, NULL)) {
1193-
set_error_buf(error_buf, error_buf_size,
1194-
"Call _initialize failed ");
1195-
goto failed;
1196-
}
1197-
}
1198-
#endif
1199-
12001190
continue;
12011191
failed:
12021192
if (sub_module_inst_list_node) {
@@ -1844,8 +1834,21 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
18441834
&module_inst->e->functions[module->start_function];
18451835
}
18461836

1847-
/* Execute __post_instantiate function */
1848-
if (!execute_post_inst_function(module_inst)
1837+
if (
1838+
#if WASM_ENABLE_LIBC_WASI != 0
1839+
/*
1840+
* reactor instances may assume that _initialize will be called by
1841+
* the environment at most once, and that none of their other
1842+
* exports are accessed before that call.
1843+
*
1844+
* let the loader decide how to act if there is no _initialize
1845+
* in a reactor
1846+
*/
1847+
!execute_initialize_function(module_inst) ||
1848+
#endif
1849+
/* Execute __post_instantiate function */
1850+
!execute_post_inst_function(module_inst)
1851+
/* Execute the function in "start" section */
18491852
|| !execute_start_function(module_inst)) {
18501853
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
18511854
goto fail;
@@ -3231,3 +3234,26 @@ llvm_jit_free_frame(WASMExecEnv *exec_env)
32313234
|| WASM_ENABLE_PERF_PROFILING != 0 */
32323235

32333236
#endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
3237+
3238+
#if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
3239+
void
3240+
wasm_propagate_wasi_args(WASMModule *module)
3241+
{
3242+
if (!module->import_count)
3243+
return;
3244+
3245+
bh_assert(&module->import_module_list_head);
3246+
3247+
WASMRegisteredModule *node =
3248+
bh_list_first_elem(&module->import_module_list_head);
3249+
while (node) {
3250+
WASIArguments *wasi_args_impt_mod =
3251+
&((WASMModule *)(node->module))->wasi_args;
3252+
bh_assert(wasi_args_impt_mod);
3253+
3254+
bh_memcpy_s(wasi_args_impt_mod, sizeof(WASIArguments),
3255+
&module->wasi_args, sizeof(WASIArguments));
3256+
node = bh_list_elem_next(node);
3257+
}
3258+
}
3259+
#endif

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ llvm_jit_free_frame(WASMExecEnv *exec_env);
626626
#endif
627627
#endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
628628

629+
#if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
630+
void
631+
wasm_propagate_wasi_args(WASMModule *module);
632+
#endif
633+
629634
#ifdef __cplusplus
630635
}
631636
#endif

0 commit comments

Comments
 (0)