diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index c1e237933..b8d326aab 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -964,11 +964,6 @@ fail: 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); } @@ -994,25 +989,17 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, { const uint8 *buf = *p_buf; AOTMemInitData **data_list; - uint64 size; - uint32 i; - - read_uint32(buf, buf_end, module->mem_init_data_count); - - if (module->mem_init_data_count == 0) { - *p_buf = buf; - return true; - } /* Allocate memory */ - size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count; + uint64 size = + sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count; if (!(module->mem_init_data_list = data_list = loader_malloc(size, error_buf, error_buf_size))) { return false; } /* Create each memory data segment */ - for (i = 0; i < module->mem_init_data_count; i++) { + for (uint32 i = 0; i < module->mem_init_data_count; i++) { uint32 byte_count; uint32 is_passive; uint32 memory_index; @@ -1049,19 +1036,12 @@ fail: } static bool -load_import_memory_info(const uint8 **p_buf, const uint8 *buf_end, +load_import_memory_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size) { const uint8 *buf = *p_buf; - read_uint32(buf, buf_end, module->import_memory_count); - - if (module->import_memory_count == 0) { - *p_buf = buf; - return true; - } - uint64 size = sizeof(AOTImportMemory) * (uint64)module->import_memory_count; AOTImportMemory *import_memories = loader_malloc(size, error_buf, error_buf_size); @@ -1094,27 +1074,17 @@ fail: } static bool -load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, +load_memory_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, char *error_buf, uint32 error_buf_size) { - uint32 i; - uint64 total_size; - const uint8 *buf = *p_buf; - - read_uint32(buf, buf_end, module->memory_count); - - if (module->memory_count == 0) { - *p_buf = buf; - return true; - } - - total_size = sizeof(AOTMemory) * (uint64)module->memory_count; + uint64 total_size = sizeof(AOTMemory) * (uint64)module->memory_count; if (!(module->memories = loader_malloc(total_size, error_buf, error_buf_size))) { return false; } - for (i = 0; i < module->memory_count; i++) { + const uint8 *buf = *p_buf; + for (uint32 i = 0; i < module->memory_count; i++) { read_uint32(buf, buf_end, module->memories[i].flags); if (!wasm_memory_check_flags(module->memories[i].flags, error_buf, @@ -1133,6 +1103,44 @@ fail: return false; } +static bool +load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, + bool is_load_from_file_buf, char *error_buf, + uint32 error_buf_size) +{ + const uint8 *buf = *p_buf; + + read_uint32(buf, buf_end, module->import_memory_count); + if (module->import_memory_count > 0) { + if (!load_import_memory_list(&buf, buf_end, module, + is_load_from_file_buf, error_buf, + error_buf_size)) { + return false; + } + } + + read_uint32(buf, buf_end, module->memory_count); + if (module->memory_count > 0) { + if (!load_memory_list(&buf, buf_end, module, error_buf, + error_buf_size)) { + return false; + } + } + + read_uint32(buf, buf_end, module->mem_init_data_count); + if (module->mem_init_data_count > 0) { + if (!load_mem_init_data_list(&buf, buf_end, module, error_buf, + error_buf_size)) { + return false; + } + } + + *p_buf = buf; + return true; +fail: + return false; +} + #if WASM_ENABLE_GC != 0 static void destroy_init_expr(InitializerExpression *expr) @@ -2465,11 +2473,8 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, { const uint8 *p = buf, *p_end = buf_end; - if (!load_import_memory_info(&p, p_end, module, is_load_from_file_buf, - error_buf, error_buf_size) - || !load_memory_info(&p, p_end, module, error_buf, error_buf_size) - || !load_mem_init_data_list(&p, p_end, module, error_buf, - error_buf_size) + if (!load_memory_info(&p, p_end, module, is_load_from_file_buf, error_buf, + error_buf_size) || !load_table_info(&p, p_end, module, error_buf, error_buf_size) || !load_type_info(&p, p_end, module, error_buf, error_buf_size) || !load_import_global_info(&p, p_end, module, is_load_from_file_buf, @@ -3854,7 +3859,6 @@ has_module_memory64(AOTModule *module) { /* TODO: multi-memories for now assuming the memory idx type is consistent * across multi-memories */ - /*FIXME: support import memory */ if (module->import_memory_count > 0) return !!(module->import_memories[0].mem_type.flags & MEMORY64_FLAG); else if (module->memory_count > 0) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index f770e9324..cee2515c7 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -856,11 +856,13 @@ memory_deinstantiate(AOTMemoryInstance *memory) static void memories_deinstantiate(AOTModuleInstance *module_inst) { - /* import memories created by host or linker. released by them too */ - for (uint32 i = ((AOTModule *)module_inst->module)->import_memory_count; - i < module_inst->memory_count; i++) { + if (!module_inst->memories) + return; + + for (uint32 i = 0; i < module_inst->memory_count; i++) { memory_deinstantiate(module_inst->memories[i]); } + wasm_runtime_free(module_inst->memories); } @@ -1938,13 +1940,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, module->import_func_count + module->import_global_count + module->import_memory_count + module->import_table_count; if (total_import_count > 0 && !imports) { - /* - * TODO: might be too strict - * might wasm_runtime_create_imports_with_builtin() here by default - */ set_error_buf(error_buf, error_buf_size, "imports is NULL while module has imports"); - // return NULL; + return NULL; } #endif diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 782e0b657..487d812a9 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1651,12 +1651,21 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, uint32 heap_size, char *error_buf, uint32 error_buf_size) { +#if WASM_ENABLE_MULTI_MODULE == 0 + /* + * TODO: if wasm_runtime_instantiate_with_builtin_linker is not in this + * library what should we do? _weak? + */ + return wasm_runtime_instantiate_with_builtin_linker( + module, stack_size, heap_size, error_buf, error_buf_size); +#else return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size, heap_size, 0, // max_memory_pages NULL, // imports 0, // import_count error_buf, error_buf_size); +#endif } WASMModuleInstanceCommon * @@ -7751,6 +7760,50 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module) } /*TODO: take us(below) out when have a linker */ +WASMModuleInstanceCommon * +wasm_runtime_instantiate_with_builtin_linker(WASMModuleCommon *module, + uint32 stack_size, + uint32 heap_size, char *error_buf, + uint32 error_buf_size) +{ + int32 import_count = 0; + WASMExternInstance *imports = NULL; + + import_count = wasm_runtime_get_import_count(module); + if (import_count < 0) { + return NULL; + } + + if (import_count) { + imports = + runtime_malloc(sizeof(WASMExternInstance) * (uint64)import_count, + NULL, error_buf, error_buf_size); + if (!imports) { + LOG_ERROR("allocate imports failed"); + return NULL; + } + + if (!wasm_runtime_create_imports_with_builtin(module, imports, + import_count)) { + set_error_buf(error_buf, error_buf_size, + "initialize imports failed"); + wasm_runtime_free(imports); + return NULL; + } + } + + WASMModuleInstanceCommon *inst = wasm_runtime_instantiate_internal( + module, NULL, NULL, stack_size, heap_size, + 0, // max_memory_pages + imports, (uint32)import_count, error_buf, error_buf_size); + + if (imports) { + wasm_runtime_free(imports); + } + + return inst; +} + bool wasm_runtime_create_extern_inst(WASMModuleCommon *module, wasm_import_t import_type, @@ -7849,28 +7902,27 @@ wasm_runtime_destroy_imports(WASMModuleCommon *module, i < import_count; i++) { wasm_runtime_destroy_extern_inst(module, extern_inst_list + i); } - - wasm_runtime_free(extern_inst_list); } -WASMExternInstance * +bool wasm_runtime_create_imports(WASMModuleCommon *module, - bool (*module_name_filter)(const char *)) + bool (*module_name_filter)(const char *), + WASMExternInstance *out, int32 out_len) { int32 import_count = wasm_runtime_get_import_count(module); - WASMExternInstance *imports = NULL; + + if (import_count < 0) + return false; if (import_count == 0) - return NULL; + return true; - imports = runtime_malloc(sizeof(WASMExternInstance) * import_count, - NULL, // module_inst - NULL, 0); - if (!imports) { - LOG_ERROR("allocate memory failed"); - return NULL; + if (!out || out_len < import_count) { + LOG_ERROR("invalid arguments"); + return false; } + memset(out, 0, sizeof(WASMExternInstance) * (uint32)import_count); for (int32 i = 0; i < import_count; i++) { wasm_import_t import_type = { 0 }; wasm_runtime_get_import_type(module, i, &import_type); @@ -7882,31 +7934,34 @@ wasm_runtime_create_imports(WASMModuleCommon *module, continue; } - WASMExternInstance *extern_instance = imports + i; + WASMExternInstance *extern_instance = out + i; if (!wasm_runtime_create_extern_inst(module, import_type, extern_instance)) { - wasm_runtime_destroy_imports(module, imports); + wasm_runtime_destroy_imports(module, out); LOG_ERROR("create import failed"); - return NULL; + return false; } } - return imports; + return true; } -WASMExternInstance * -wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module) +bool +wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module, + WASMExternInstance *out, int32 out_len) { LOG_DEBUG("create imports with builtin"); - return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module); + return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module, + out, out_len); } #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) @@ -7927,24 +7982,46 @@ wasm_runtime_inherit_imports(WASMModuleCommon *module, return 0; } -void -wasm_runtime_disinherit_imports(WASMModuleCommon *module, - WASMExternInstance *imports, int32 import_count) +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) { -#if WASM_ENABLE_INTERP != 0 - if (module->module_type == Wasm_Module_Bytecode) { - wasm_disinherit_imports((WASMModule *)module, imports, import_count); - return; + int32 spawned_import_count = wasm_runtime_get_import_count(module); + if (spawned_import_count < 0) { + return NULL; } -#endif -#if WASM_ENABLE_AOT != 0 - if (module->module_type == Wasm_Module_AoT) { - return aot_disinherit_imports((AOTModule *)module, imports, - import_count); + + WASMExternInstance *spawned_imports = runtime_malloc( + sizeof(WASMExternInstance) * (uint64)spawned_import_count, NULL, + error_buf, error_buf_size); + if (spawned_imports == NULL) { + LOG_ERROR("Failed to allocate memory for imports"); + return NULL; } -#endif - LOG_ERROR("disinherit imports failed, invalid module type"); + + 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 + (uint32)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 c1a36746b..0460c5ea8 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -1218,19 +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); - -void -wasm_runtime_disinherit_imports(WASMModuleCommon *module, - WASMExternInstance *imports, - int32 import_count); +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/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 9d4d0b751..d896c42d1 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -2342,18 +2342,28 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr); /*TODO: take me out when have a linker */ +WASM_RUNTIME_API_EXTERN wasm_module_inst_t +wasm_runtime_instantiate_with_builtin_linker(wasm_module_t module, + uint32_t stack_size, + uint32_t heap_size, + char *error_buf, + uint32_t error_buf_size); + /** * @return NULL if failed and if there is no import */ -WASM_RUNTIME_API_EXTERN wasm_extern_inst_t -wasm_runtime_create_imports_with_builtin(wasm_module_t module); +WASM_RUNTIME_API_EXTERN bool +wasm_runtime_create_imports_with_builtin(wasm_module_t module, + wasm_extern_inst_t out, + int32_t out_len); WASM_RUNTIME_API_EXTERN void wasm_runtime_destroy_imports(wasm_module_t module, wasm_extern_inst_t imports); -WASM_RUNTIME_API_EXTERN wasm_extern_inst_t +WASM_RUNTIME_API_EXTERN bool wasm_runtime_create_imports(wasm_module_t module, - bool (*module_name_filter)(const char *)); + bool (*module_name_filter)(const char *), + wasm_extern_inst_t out, int32_t out_len); #ifdef __cplusplus } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 07b32dfef..24fb9a39b 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -266,23 +266,19 @@ static void memories_deinstantiate(WASMModuleInstance *module_inst, WASMMemoryInstance **memories, uint32 count) { - WASMModule *module = module_inst->module; if (!memories) return; - /* - * If created by host or linker, import memories should also be released by - * host or linker - */ for (uint32 i = 0; i < count; i++) { - if (i < module->import_memory_count #if WASM_ENABLE_MULTI_MODULE != 0 - && module->import_memories[i].u.memory.import_module -#endif - ) { + WASMModule *module = module_inst->module; + + if (i < module->import_memory_count + && module->import_memories[i].u.memory.import_module) { continue; } +#endif memory_deinstantiate(memories[i]); } @@ -2502,13 +2498,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, #if WASM_ENABLE_MULTI_MODULE == 0 if (module->import_count > 0 && !imports) { - /* - * TODO: might be too strict - * might wasm_runtime_create_imports_with_builtin() here by default - */ set_error_buf(error_buf, error_buf_size, "argument imports is NULL while module has imports"); - // return NULL; + return NULL; } #endif diff --git a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c index 97f1195ad..5ff467760 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,17 +644,9 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, if (thread) *thread = thread_handle; - wasm_runtime_disinherit_imports(module, spawned_imports, - spawned_import_count); - wasm_runtime_free(spawned_imports); return 0; fail: - if (spawned_imports) { - wasm_runtime_disinherit_imports(module, spawned_imports, - spawned_import_count); - 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 4291ec74f..d5cd0b401 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,22 +129,16 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) goto thread_spawn_fail; } - wasm_runtime_disinherit_imports(module, spawned_imports, - spawned_import_count); - 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_disinherit_imports(module, spawned_imports, - spawned_import_count); - 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 6616bf87e..e3678d957 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 disinherit_imports; + return NULL; } /* Set custom_data to new module instance */ @@ -596,9 +573,6 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) os_mutex_unlock(&cluster->lock); - wasm_runtime_disinherit_imports(module, spawned_imports, - spawned_import_count); - wasm_runtime_free(spawned_imports); return new_exec_env; fail3: @@ -609,11 +583,6 @@ fail2: wasm_cluster_free_aux_stack(exec_env, aux_stack_start); fail1: wasm_runtime_deinstantiate_internal(new_module_inst, true); -disinherit_imports: - wasm_runtime_disinherit_imports(module, spawned_imports, - spawned_import_count); -release_imports: - wasm_runtime_free(spawned_imports); return NULL; } diff --git a/doc/memory_tune.md b/doc/memory_tune.md index 9bb4f6a10..48c641710 100644 --- a/doc/memory_tune.md +++ b/doc/memory_tune.md @@ -32,3 +32,7 @@ Normally there are some methods to tune the memory usage: - use XIP mode, refer to [WAMR XIP (Execution In Place) feature introduction](./xip.md) for more details - when using the Wasm C API in fast interpreter or AOT mode, set `clone_wasm_binary=false` in `LoadArgs` and free the wasm binary buffer (with `wasm_byte_vec_delete`) after module loading; `wasm_module_is_underlying_binary_freeable` can be queried to check if the wasm binary buffer can be safely freed (see [the example](../samples/basic/src/free_buffer_early.c)); after the buffer is freed, `wasm_runtime_get_custom_section` cannot be called anymore - when using the wasm/AOT loader in fast interpreter or AOT mode, set `wasm_binary_freeable=true` in `LoadArgs` and free the wasm binary buffer (with `wasm_byte_vec_delete`) after module loading; `wasm_runtime_is_underlying_binary_freeable` can be queried to check if the wasm binary buffer can be safely freed; after the buffer is freed, `wasm_runtime_get_custom_section` cannot be called anymore + +## With valgrind + +Valgrind doesn't function properly with some WAMR features, so you'll need to turn those off to make checkers work. `-DWAMR_DISABLE_HW_BOUND_CHECK=1 -DWAMR_DISABLE_WRITE_GS_BASE=1`. diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp index b17f0997a..91f03a979 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -377,56 +377,17 @@ handle_cmd_instantiate_module(uint64 *args, uint32 argc) bh_assert(argc == 5); - *(void **)args_org = NULL; - if (!runtime_inited) { + *(void **)args_org = NULL; return; } -#if WASM_ENABLE_MULTI_MODULE == 0 - { - int32_t import_count = - wasm_runtime_get_import_count(enclave_module->module); - WASMExternInstance *imports = NULL; - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = - wasm_runtime_create_imports_with_builtin(enclave_module->module); -#endif - if (import_count > 0 && imports == NULL) { - LOG_WARNING("Need to provide necessary imported objects"); - return; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = (uint32_t)import_count, - }; - - module_inst = wasm_runtime_instantiate_ex( - enclave_module->module, &inst_args, error_buf, error_buf_size); - if (!module_inst) { - wasm_runtime_destroy_imports(enclave_module->module, imports); - return; - } - - /* - * FIXME: how to relese imports. - * if there will be spawned thread, need to release when the thread is - * done. - */ - } -#else /* WASM_ENABLE_MULTI_MODULE == 0 */ if (!(module_inst = wasm_runtime_instantiate(enclave_module->module, stack_size, heap_size, error_buf, error_buf_size))) { + *(void **)args_org = NULL; return; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ *(wasm_module_inst_t *)args_org = module_inst; @@ -802,11 +763,6 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) RuntimeInitArgs init_args; char error_buf[128]; const char *exception; - uint32 stack_size = 16 * 1024; - uint32 heap_size = 16 * 1024; -#if WASM_ENABLE_MULTI_MODULE == 0 - WASMExternInstance *imports = NULL; -#endif /* avoid duplicated init */ if (runtime_inited) { @@ -841,45 +797,13 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) } /* instantiate the module */ -#if WASM_ENABLE_MULTI_MODULE == 0 - { - int32_t import_count = wasm_runtime_get_import_count(wasm_module); - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(wasm_module); -#endif - if (import_count > 0 && imports == NULL) { - enclave_print("Need to provide necessary imported objects"); - enclave_print("\n"); - goto fail2; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = (uint32_t)import_count, - }; - - wasm_module_inst = wasm_runtime_instantiate_ex( - wasm_module, &inst_args, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) { - enclave_print(error_buf); - enclave_print("\n"); - goto fail3; - } - } -#else /* WASM_ENABLE_MULTI_MODULE == 0 */ if (!(wasm_module_inst = - wasm_runtime_instantiate(wasm_module, stack_size, heap_size, + wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024, error_buf, sizeof(error_buf)))) { enclave_print(error_buf); enclave_print("\n"); goto fail2; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ /* execute the main function of wasm app */ wasm_application_execute_main(wasm_module_inst, 0, NULL); @@ -891,12 +815,6 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 -fail3: - /* destory imports */ - wasm_runtime_destroy_imports(wasm_module, imports); -#endif /* WASM_ENABLE_MULTI_MODULE == 0*/ - fail2: /* unload the module */ wasm_runtime_unload(wasm_module); diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 6f7c95e14..af50223a4 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -885,7 +885,7 @@ main(int argc, char *argv[]) /* load WASM byte buffer from WASM bin file */ if (!(wasm_file_buf = (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) - goto unregister_native; + goto fail1; #if WASM_ENABLE_AOT != 0 if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) { @@ -898,7 +898,7 @@ main(int argc, char *argv[]) map_flags, os_get_invalid_handle()))) { printf("mmap memory failed\n"); wasm_runtime_free(wasm_file_buf); - goto unregister_native; + goto fail1; } #if (WASM_MEM_DUAL_BUS_MIRROR != 0) @@ -925,7 +925,7 @@ main(int argc, char *argv[]) if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); - goto unmap_file; + goto fail2; } #if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0 @@ -933,7 +933,7 @@ main(int argc, char *argv[]) sizeof(error_buf))) { printf("set aot module name failed in dynamic aot debug mode, %s\n", error_buf); - goto unload_module; + goto fail3; } #endif @@ -942,42 +942,12 @@ main(int argc, char *argv[]) #endif /* instantiate the module */ -#if WASM_ENABLE_MULTI_MODULE == 0 - int32_t import_count = wasm_runtime_get_import_count(wasm_module); - WASMExternInstance *imports = NULL; - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(wasm_module); -#endif - - if (import_count > 0 && imports == NULL) { - printf("Need to provide %" PRId32 " imported objects:\n", import_count); - goto unload_module; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = import_count, - }; - - wasm_module_inst = wasm_runtime_instantiate_ex( - wasm_module, &inst_args, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) { - printf("%s\n", error_buf); - goto destroy_imports; - } -#else if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); - goto unload_module; + goto fail3; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0 if (disable_bounds_checks) { @@ -1067,27 +1037,21 @@ fail5: #if WASM_ENABLE_DEBUG_INTERP != 0 fail4: #endif - /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 -destroy_imports: - wasm_runtime_destroy_imports(wasm_module, imports); -#endif - -unload_module: +fail3: /* unload the module */ wasm_runtime_unload(wasm_module); -unmap_file: +fail2: /* free the file buffer */ if (!is_xip_file) wasm_runtime_free(wasm_file_buf); else os_munmap(wasm_file_buf, wasm_file_size); -unregister_native: +fail1: #if BH_HAS_DLFCN /* unload the native libraries */ unregister_and_unload_native_libs(native_lib_loaded_count, diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index e932787e9..e85e869c2 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -540,41 +540,12 @@ main(int argc, char *argv[]) #endif /* instantiate the module */ -#if WASM_ENABLE_MULTI_MODULE == 0 - int32_t import_count = wasm_runtime_get_import_count(wasm_module); - WASMExternInstance *imports = NULL; - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(wasm_module); -#endif - if (import_count > 0 && imports == NULL) { - printf("Need to provide %d imported objects:\n", import_count); - goto fail3; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = import_count, - }; - - wasm_module_inst = wasm_runtime_instantiate_ex( - wasm_module, &inst_args, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) { - printf("%s\n", error_buf); - goto fail4; - } -#else if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail3; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ #if WASM_ENABLE_DEBUG_INTERP != 0 if (ip_addr != NULL) { @@ -583,12 +554,12 @@ main(int argc, char *argv[]) uint32_t debug_port; if (exec_env == NULL) { printf("%s\n", wasm_runtime_get_exception(wasm_module_inst)); - goto fail5; + goto fail4; } debug_port = wasm_runtime_start_debug_instance(exec_env); if (debug_port == 0) { printf("Failed to start debug instance\n"); - goto fail5; + goto fail4; } } #endif @@ -624,16 +595,11 @@ main(int argc, char *argv[]) printf("%s\n", exception); #if WASM_ENABLE_DEBUG_INTERP != 0 -fail5: +fail4: #endif /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 -fail4: - wasm_runtime_destroy_imports(wasm_module, imports); -#endif - fail3: /* unload the module */ wasm_runtime_unload(wasm_module); diff --git a/samples/file/src/main.c b/samples/file/src/main.c index f7b1da898..3675ee057 100644 --- a/samples/file/src/main.c +++ b/samples/file/src/main.c @@ -18,7 +18,7 @@ int main(int argc, char *argv_main[]) { static char global_heap_buf[512 * 1024]; - char *buffer, error_buf[128] = { 0 }; + char *buffer, error_buf[128]; const char *wasm_path = NULL, *wasi_dir = NULL; int opt, main_result = 1; @@ -27,10 +27,6 @@ main(int argc, char *argv_main[]) wasm_exec_env_t exec_env = NULL; uint32 buf_size, stack_size = 8092, heap_size = 8092; -#if WASM_ENABLE_MULTI_MODULE == 0 - WASMExternInstance *imports = NULL; -#endif - RuntimeInitArgs init_args; memset(&init_args, 0, sizeof(RuntimeInitArgs)); @@ -80,37 +76,8 @@ main(int argc, char *argv_main[]) wasm_runtime_set_wasi_args_ex(module, &wasi_dir, 1, NULL, 0, NULL, 0, NULL, 0, 0, 1, 2); -#if WASM_ENABLE_MULTI_MODULE == 0 - int32_t import_count = wasm_runtime_get_import_count(module); - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(module); -#endif - - if (import_count > 0 && imports == NULL) { - printf("Need to provide %" PRId32 " imported objects:\n", import_count); - goto fail; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = import_count, - }; - - module_inst = wasm_runtime_instantiate_ex(module, &inst_args, error_buf, - sizeof(error_buf)); - if (!module_inst) { - printf("%s\n", error_buf); - goto fail; - } -#else module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf)); -#endif if (!module_inst) { printf("Instantiate wasm module failed. error: %s\n", error_buf); @@ -137,10 +104,6 @@ fail: wasm_runtime_destroy_exec_env(exec_env); if (module_inst) wasm_runtime_deinstantiate(module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 - if (imports) - wasm_runtime_destroy_imports(module, imports); -#endif if (module) wasm_runtime_unload(module); if (buffer) diff --git a/samples/linking/cmake/FindWASISDK.cmake b/samples/linking/cmake/FindWASISDK.cmake index 0353b7de2..5cdfea41e 100644 --- a/samples/linking/cmake/FindWASISDK.cmake +++ b/samples/linking/cmake/FindWASISDK.cmake @@ -11,7 +11,6 @@ find_path(WASISDK_HOME REQUIRED ) - string(REGEX MATCH [0-9]+\.[0-9]+\.*[0-9]* WASISDK_VERSION ${WASISDK_HOME}) find_package_handle_standard_args(WASISDK REQUIRED_VARS WASISDK_HOME VERSION_VAR WASISDK_VERSION)