From a02cc6626d0806bc784d6cba2c666245629b7584 Mon Sep 17 00:00:00 2001 From: Hanged Fish <94267867+hangedfish@users.noreply.github.com> Date: Tue, 22 Mar 2022 12:17:14 +0800 Subject: [PATCH] Fix libc-wasi not working in spawned exec_env (#1053) In thread_manager.c, `wasm_cluster_spawn_exec_env` creates a new module instance but not sets wasi_ctx, so when the new exec_env calls wasm function which uses WASI API, the WASI API functions in `libc_wasi_wrapper.c` will get null result in calling `get_wasi_ctx` and then return `wasi_errno`. Signed-off-by: HangedFish --- core/iwasm/libraries/thread-mgr/thread_manager.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 0a25c11b..768e0d7d 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -362,6 +362,9 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_t module; wasm_module_inst_t new_module_inst; +#if WASM_ENABLE_LIBC_WASI != 0 + WASIContext *wasi_ctx; +#endif WASMExecEnv *new_exec_env; uint32 aux_stack_start, aux_stack_size; uint32 stack_size = 8192; @@ -393,6 +396,11 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) wasm_runtime_set_custom_data_internal( new_module_inst, wasm_runtime_get_custom_data(module_inst)); +#if WASM_ENABLE_LIBC_WASI != 0 + wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst); + wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx); +#endif + new_exec_env = wasm_exec_env_create_internal(new_module_inst, exec_env->wasm_stack_size); if (!new_exec_env)