diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index e01af5f178..308733e8fb 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -963,11 +963,6 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, static void destroy_import_memories(AOTImportMemory *import_memories) { - if (!import_memories) - return; - - import_memories->module_name = NULL; - import_memories->memory_name = NULL; wasm_runtime_free(import_memories); } diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index edc9bf5b43..64d48cb386 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -7947,11 +7947,12 @@ wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module, } #if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 + /* * The function is used to create a new WASMExternInstance list * for a spawned thread. */ -int32 +static int32 wasm_runtime_inherit_imports(WASMModuleCommon *module, WASMModuleInstanceCommon *inst, WASMExternInstance *out, int32 out_len) @@ -7971,6 +7972,43 @@ wasm_runtime_inherit_imports(WASMModuleCommon *module, LOG_ERROR("inherit imports failed, invalid module type"); return 0; } + +WASMModuleInstanceCommon * +wasm_runtime_instantiate_with_inheritance( + WASMModuleCommon *module, WASMModuleInstanceCommon *parent_inst, + WASMExecEnv *exec_env, uint32 stack_size, uint32 heap_size, + uint32 max_memory_pages, char *error_buf, uint32 error_buf_size) +{ + int spawned_import_count = wasm_runtime_get_import_count(module); + WASMExternInstance *spawned_imports = + runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count, NULL, + error_buf, error_buf_size); + if (spawned_imports == NULL) { + LOG_ERROR("Failed to allocate memory for imports"); + return NULL; + } + + int ret = wasm_runtime_inherit_imports(module, parent_inst, spawned_imports, + spawned_import_count); + if (ret != 0) { + LOG_ERROR("Failed to inherit imports"); + wasm_runtime_free(spawned_imports); + return NULL; + } + + wasm_module_inst_t child_inst = wasm_runtime_instantiate_internal( + module, parent_inst, exec_env, stack_size, + 0, // heap_size + 0, // max_memory_pages + spawned_imports, // imports + spawned_import_count, // import_count + error_buf, error_buf_size); + + wasm_runtime_free(spawned_imports); + + return child_inst; +} + #endif /* WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 */ const WASMExternInstance * diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index eedcf598de..0460c5ea87 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -1218,14 +1218,11 @@ wasm_runtime_set_linux_perf(bool flag); #endif #if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 -/* - * The function is used to create a new WASMExternInstance list - * for a spawned thread. - */ -int32 -wasm_runtime_inherit_imports(WASMModuleCommon *module, - WASMModuleInstanceCommon *inst, - WASMExternInstance *out, int32 out_len); +WASMModuleInstanceCommon * +wasm_runtime_instantiate_with_inheritance( + WASMModuleCommon *module, WASMModuleInstanceCommon *parent_inst, + WASMExecEnv *exec_env, uint32 stack_size, uint32 heap_size, + uint32 max_memory_pages, char *error_buf, uint32 error_buf_size); #endif /* WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 */ const WASMExternInstance * diff --git a/core/iwasm/compilation/aot.c b/core/iwasm/compilation/aot.c index 9c05f72b51..f42c27488d 100644 --- a/core/iwasm/compilation/aot.c +++ b/core/iwasm/compilation/aot.c @@ -47,7 +47,7 @@ aot_create_import_memories(const WASMModule *module, uint32 import_memory_count) } import_memories = wasm_runtime_malloc((uint32)size); - if (!module->import_memories) { + if (!import_memories) { aot_set_last_error("allocate memory failed."); return NULL; } diff --git a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c index f67a481ee0..5ff467760f 100644 --- a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c +++ b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c @@ -561,8 +561,6 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, uint32 aux_stack_size; uint64 aux_stack_start = 0; int32 ret = -1; - int32 spawned_import_count = 0; - WASMExternInstance *spawned_imports = NULL; bh_assert(module); bh_assert(module_inst); @@ -581,32 +579,12 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, } #endif - /* - * build a imports list(WASMExternInstance[]) from parent's imports - */ - spawned_import_count = wasm_runtime_get_import_count(module); - spawned_imports = - wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count); - if (spawned_imports == NULL) { - LOG_ERROR("Failed to allocate memory for imports"); - goto fail; - } - - ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports, - spawned_import_count); - if (ret != 0) { - LOG_ERROR("Failed to inherit imports"); - goto fail; - } - - if (!(new_module_inst = wasm_runtime_instantiate_internal( + if (!(new_module_inst = wasm_runtime_instantiate_with_inheritance( module, module_inst, exec_env, stack_size, - 0, // heap_size - 0, // max_memory_pages - spawned_imports, // imports - spawned_import_count, // import_count + 0, // heap_size + 0, // max_memory_pages NULL, 0))) - goto fail; + return -1; /* Set custom_data to new module instance */ wasm_runtime_set_custom_data_internal( @@ -666,13 +644,9 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, if (thread) *thread = thread_handle; - wasm_runtime_free(spawned_imports); return 0; fail: - if (spawned_imports) { - wasm_runtime_free(spawned_imports); - } if (new_module_inst) wasm_runtime_deinstantiate_internal(new_module_inst, true); if (info_node) diff --git a/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c b/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c index 97937cd7bc..d5cd0b4018 100644 --- a/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c +++ b/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c @@ -77,46 +77,21 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) wasm_module_inst_t new_module_inst = NULL; ThreadStartArg *thread_start_arg = NULL; wasm_function_inst_t start_func; - int32 thread_id = -1; + int32 thread_id; uint32 stack_size = 8192; int32 ret = -1; - int32 spawned_import_count = 0; - WASMExternInstance *spawned_imports = NULL; bh_assert(module); bh_assert(module_inst); stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size; - /* - * build a imports list(WASMExternInstance[]) from parent's imports - */ - spawned_import_count = wasm_runtime_get_import_count(module); - spawned_imports = - wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count); - if (spawned_imports == NULL) { - LOG_ERROR("Failed to allocate memory for imports"); + if (!(new_module_inst = wasm_runtime_instantiate_with_inheritance( + module, module_inst, exec_env, stack_size, + 0, // heap_size + 0, // max_memory_pages + NULL, 0))) return -1; - } - - ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports, - spawned_import_count); - if (ret != 0) { - LOG_ERROR("Failed to inherit imports"); - goto free_imports; - } - - new_module_inst = wasm_runtime_instantiate_internal( - module, module_inst, exec_env, stack_size, - 0, // heap_size - 0, // max_memory_pages - spawned_imports, // imports - spawned_import_count, // import_count - NULL, 0); - if (new_module_inst == NULL) { - LOG_ERROR("Failed to instantiate new module"); - goto free_imports; - } wasm_runtime_set_custom_data_internal( new_module_inst, wasm_runtime_get_custom_data(module_inst)); @@ -154,18 +129,16 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) goto thread_spawn_fail; } - wasm_runtime_free(spawned_imports); return thread_id; thread_spawn_fail: deallocate_thread_id(thread_id); + thread_preparation_fail: if (new_module_inst) wasm_runtime_deinstantiate_internal(new_module_inst, true); if (thread_start_arg) wasm_runtime_free(thread_start_arg); -free_imports: - wasm_runtime_free(spawned_imports); return -1; } diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 719ece7735..e3678d9571 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -500,40 +500,17 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) uint32 aux_stack_size; uint64 aux_stack_start; uint32 stack_size = 8192; - int32 ret = -1; - int32 spawned_import_count = 0; - WASMExternInstance *spawned_imports = NULL; if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) { return NULL; } - /* - * build a imports list(WASMExternInstance[]) from parent's imports - */ - spawned_import_count = ((WASMModule *)module)->import_count; - spawned_imports = - wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count); - if (spawned_imports == NULL) { - LOG_ERROR("Failed to allocate memory for imports"); - return NULL; - } - - ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports, - spawned_import_count); - if (ret != 0) { - LOG_ERROR("Failed to inherit imports"); - goto release_imports; - } - - if (!(new_module_inst = wasm_runtime_instantiate_internal( + if (!(new_module_inst = wasm_runtime_instantiate_with_inheritance( module, module_inst, exec_env, stack_size, - 0, // heap_size - 0, // max_memory_pages - spawned_imports, // imports - spawned_import_count, // import_count + 0, // heap_size + 0, // max_memory_pages NULL, 0))) { - goto release_imports; + return NULL; } /* Set custom_data to new module instance */ @@ -596,7 +573,6 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) os_mutex_unlock(&cluster->lock); - wasm_runtime_free(spawned_imports); return new_exec_env; fail3: @@ -607,8 +583,6 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) wasm_cluster_free_aux_stack(exec_env, aux_stack_start); fail1: wasm_runtime_deinstantiate_internal(new_module_inst, true); -release_imports: - wasm_runtime_free(spawned_imports); return NULL; }