From c3e33a96eaef15b086d2d0115d916f276476b749 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Wed, 13 Mar 2024 12:28:45 +0800 Subject: [PATCH] Remove unused argument in wasm_runtime_lookup_function and refactor WASMModuleInstance (#3218) Remove the unused parameter `signature` from `wasm_runtime_lookup_function`. Refactor the layout of WASMModuleInstance structure: - move common data members `c_api_func_imports` and `cur_exec_env` from `WASMModuleInstanceExtraCommon` to `WASMModuleInstance` - In `WASMModuleInstance`, enlarge `reserved[3]` to `reserved[5]` in case that we need to add more fields in the future ps. https://github.com/bytecodealliance/wasm-micro-runtime/issues/2530 https://github.com/bytecodealliance/wasm-micro-runtime/issues/3202 --- core/iwasm/aot/aot_runtime.c | 42 +++++++------------ core/iwasm/aot/aot_runtime.h | 6 +-- core/iwasm/common/wasm_application.c | 11 +++-- core/iwasm/common/wasm_c_api.c | 16 ++++--- core/iwasm/common/wasm_memory.c | 6 +-- core/iwasm/common/wasm_runtime_common.c | 6 +-- core/iwasm/common/wasm_runtime_common.h | 2 +- core/iwasm/compilation/aot_emit_function.c | 10 ++--- core/iwasm/compilation/aot_llvm.c | 2 +- core/iwasm/include/wasm_export.h | 3 +- core/iwasm/interpreter/wasm_interp_classic.c | 14 +++---- core/iwasm/interpreter/wasm_interp_fast.c | 5 +-- core/iwasm/interpreter/wasm_runtime.c | 21 ++++------ core/iwasm/interpreter/wasm_runtime.h | 13 +++--- .../lib_wasi_threads_wrapper.c | 4 +- .../libraries/thread-mgr/thread_manager.c | 19 ++++----- doc/embed_wamr.md | 2 +- doc/multi_module.md | 3 +- doc/perf_tune.md | 2 +- language-bindings/go/wamr/instance.go | 6 +-- .../python/src/wamr/wamrapi/wamr.py | 2 +- .../platforms/zephyr/simple/src/main.c | 8 ++-- samples/basic/src/main.c | 9 ++-- samples/inst-context/src/main.c | 2 +- samples/ref-types/src/hello.c | 6 +-- samples/shared-module/src/main.c | 8 ++-- samples/spawn-thread/src/main.c | 6 +-- samples/terminate/src/main.c | 2 +- 28 files changed, 100 insertions(+), 136 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 7d5f5b9059..9ecdf2af08 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -47,15 +47,15 @@ bh_static_assert(offsetof(AOTModuleInstance, func_type_indexes) == 6 * sizeof(uint64)); bh_static_assert(offsetof(AOTModuleInstance, cur_exception) == 13 * sizeof(uint64)); +bh_static_assert(offsetof(AOTModuleInstance, c_api_func_imports) + == 13 * sizeof(uint64) + 128 + 8 * sizeof(uint64)); bh_static_assert(offsetof(AOTModuleInstance, global_table_data) - == 13 * sizeof(uint64) + 128 + 11 * sizeof(uint64)); + == 13 * sizeof(uint64) + 128 + 14 * sizeof(uint64)); bh_static_assert(sizeof(AOTMemoryInstance) == 112); bh_static_assert(offsetof(AOTTableInstance, elems) == 24); bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0); -bh_static_assert(offsetof(AOTModuleInstanceExtra, common.c_api_func_imports) - == sizeof(uint64)); bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3); @@ -1273,7 +1273,7 @@ lookup_post_instantiate_func(AOTModuleInstance *module_inst, AOTFunctionInstance *func; AOTFuncType *func_type; - if (!(func = aot_lookup_function(module_inst, func_name, NULL))) + if (!(func = aot_lookup_function(module_inst, func_name))) /* Not found */ return NULL; @@ -1908,9 +1908,8 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) if (module_inst->func_type_indexes) wasm_runtime_free(module_inst->func_type_indexes); - if (common->c_api_func_imports) - wasm_runtime_free(((AOTModuleInstanceExtra *)module_inst->e) - ->common.c_api_func_imports); + if (module_inst->c_api_func_imports) + wasm_runtime_free(module_inst->c_api_func_imports); #if WASM_ENABLE_GC != 0 if (!is_sub_inst) { @@ -1941,8 +1940,7 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) } AOTFunctionInstance * -aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, - const char *signature) +aot_lookup_function(const AOTModuleInstance *module_inst, const char *name) { uint32 i; AOTFunctionInstance *export_funcs = @@ -1951,7 +1949,6 @@ aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, for (i = 0; i < module_inst->export_func_count; i++) if (!strcmp(export_funcs[i].func_name, name)) return &export_funcs[i]; - (void)signature; return NULL; } @@ -2157,8 +2154,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, hw bound check is enabled */ #endif - /* Set exec env so it can be later retrieved from instance */ - ((AOTModuleInstanceExtra *)module_inst->e)->common.cur_exec_env = exec_env; + /* Set exec env, so it can be later retrieved from instance */ + module_inst->cur_exec_env = exec_env; if (ext_ret_count > 0) { uint32 cell_num = 0, i; @@ -2497,22 +2494,18 @@ aot_module_malloc_internal(AOTModuleInstance *module_inst, && module->free_func_index != (uint32)-1) { AOTFunctionInstance *malloc_func, *retain_func = NULL; char *malloc_func_name; - char *malloc_func_sig; if (module->retain_func_index != (uint32)-1) { malloc_func_name = "__new"; - malloc_func_sig = "(ii)i"; - retain_func = aot_lookup_function(module_inst, "__retain", "(i)i"); + retain_func = aot_lookup_function(module_inst, "__retain"); if (!retain_func) - retain_func = aot_lookup_function(module_inst, "__pin", "(i)i"); + retain_func = aot_lookup_function(module_inst, "__pin"); bh_assert(retain_func); } else { malloc_func_name = "malloc"; - malloc_func_sig = "(i)i"; } - malloc_func = - aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig); + malloc_func = aot_lookup_function(module_inst, malloc_func_name); if (!malloc_func || !execute_malloc_function(module_inst, exec_env, malloc_func, @@ -2621,10 +2614,9 @@ aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env, else { free_func_name = "free"; } - free_func = - aot_lookup_function(module_inst, free_func_name, "(i)i"); + free_func = aot_lookup_function(module_inst, free_func_name); if (!free_func && module->retain_func_index != (uint32)-1) - free_func = aot_lookup_function(module_inst, "__unpin", "(i)i"); + free_func = aot_lookup_function(module_inst, "__unpin"); if (free_func) execute_free_function(module_inst, exec_env, free_func, @@ -2687,11 +2679,9 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, AOTModuleInstance *module_inst = (AOTModuleInstance *)wasm_runtime_get_module_inst(exec_env); AOTModule *aot_module = (AOTModule *)module_inst->module; - AOTModuleInstanceExtra *module_inst_extra = - (AOTModuleInstanceExtra *)module_inst->e; CApiFuncImport *c_api_func_import = - module_inst_extra->common.c_api_func_imports - ? module_inst_extra->common.c_api_func_imports + func_idx + module_inst->c_api_func_imports + ? module_inst->c_api_func_imports + func_idx : NULL; uint32 *func_type_indexes = module_inst->func_type_indexes; uint32 func_type_idx = func_type_indexes[func_idx]; diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 95f81b20eb..1b5b610e17 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -499,14 +499,12 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst); * * @param module_inst the module instance * @param name the name of the function - * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64" - * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32" * * @return the function instance found */ AOTFunctionInstance * -aot_lookup_function(const AOTModuleInstance *module_inst, const char *name, - const char *signature); +aot_lookup_function(const AOTModuleInstance *module_inst, const char *name); + /** * Call the given AOT function of a AOT module instance with * arguments. diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index ffc1873391..7aa4a563ad 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -147,10 +147,10 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[]) } #endif /* end of WASM_ENABLE_LIBC_WASI */ - if (!(func = wasm_runtime_lookup_function(module_inst, "main", NULL)) - && !(func = wasm_runtime_lookup_function(module_inst, - "__main_argc_argv", NULL)) - && !(func = wasm_runtime_lookup_function(module_inst, "_main", NULL))) { + if (!(func = wasm_runtime_lookup_function(module_inst, "main")) + && !(func = + wasm_runtime_lookup_function(module_inst, "__main_argc_argv")) + && !(func = wasm_runtime_lookup_function(module_inst, "_main"))) { #if WASM_ENABLE_LIBC_WASI != 0 wasm_runtime_set_exception( module_inst, "lookup the entry point symbol (like _start, main, " @@ -337,8 +337,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name, bh_assert(argc >= 0); LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc); - if (!(target_func = - wasm_runtime_lookup_function(module_inst, name, NULL))) { + if (!(target_func = wasm_runtime_lookup_function(module_inst, name))) { snprintf(buf, sizeof(buf), "lookup function %s failed", name); wasm_runtime_set_exception(module_inst, buf); goto fail; diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index fcd06b3a7f..39073984f0 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -4939,19 +4939,17 @@ wasm_instance_new_with_args_ex(wasm_store_t *store, const wasm_module_t *module, /* create the c-api func import list */ #if WASM_ENABLE_INTERP != 0 if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) { - WASMModuleInstanceExtra *e = - ((WASMModuleInstance *)instance->inst_comm_rt)->e; - p_func_imports = &(e->common.c_api_func_imports); + WASMModuleInstance *wasm_module_inst = + (WASMModuleInstance *)instance->inst_comm_rt; + p_func_imports = &(wasm_module_inst->c_api_func_imports); import_func_count = MODULE_INTERP(module)->import_function_count; } #endif #if WASM_ENABLE_AOT != 0 if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) { - AOTModuleInstanceExtra *e = - (AOTModuleInstanceExtra *)((AOTModuleInstance *) - instance->inst_comm_rt) - ->e; - p_func_imports = &(e->common.c_api_func_imports); + AOTModuleInstance *aot_module_inst = + (AOTModuleInstance *)instance->inst_comm_rt; + p_func_imports = &(aot_module_inst->c_api_func_imports); import_func_count = MODULE_AOT(module)->import_func_count; } #endif @@ -4965,7 +4963,7 @@ wasm_instance_new_with_args_ex(wasm_store_t *store, const wasm_module_t *module, goto failed; } - /* fill in module_inst->e->c_api_func_imports */ + /* fill in module_inst->c_api_func_imports */ for (i = 0; imports && i < imports->num_elems; i++) { wasm_func_t *func_host = NULL; wasm_extern_t *in = imports->data[i]; diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index 61ab74928e..381e6b4473 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -823,13 +823,11 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count) #if WASM_ENABLE_INTERP != 0 if (module->module_type == Wasm_Module_Bytecode) - exec_env = - ((WASMModuleInstanceExtra *)module->e)->common.cur_exec_env; + exec_env = ((WASMModuleInstance *)module)->cur_exec_env; #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) - exec_env = - ((AOTModuleInstanceExtra *)module->e)->common.cur_exec_env; + exec_env = ((AOTModuleInstance *)module)->cur_exec_env; #endif enlarge_memory_error_cb(inc_page_count, total_size_old, 0, diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index f7553d2cd4..4191a67240 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1853,17 +1853,17 @@ wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function, WASMFunctionInstanceCommon * wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst, - const char *name, const char *signature) + const char *name) { #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) return (WASMFunctionInstanceCommon *)wasm_lookup_function( - (const WASMModuleInstance *)module_inst, name, signature); + (const WASMModuleInstance *)module_inst, name); #endif #if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) return (WASMFunctionInstanceCommon *)aot_lookup_function( - (const AOTModuleInstance *)module_inst, name, signature); + (const AOTModuleInstance *)module_inst, name); #endif return NULL; } diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index c682b4944c..aaf04ec2fd 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -611,7 +611,7 @@ wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon * wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst, - const char *name, const char *signature); + const char *name); /* Internal API */ WASMFuncType * diff --git a/core/iwasm/compilation/aot_emit_function.c b/core/iwasm/compilation/aot_emit_function.c index 0a67d37b2b..2241731632 100644 --- a/core/iwasm/compilation/aot_emit_function.c +++ b/core/iwasm/compilation/aot_emit_function.c @@ -329,13 +329,9 @@ call_aot_invoke_c_api_native(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, param_values[0] = func_ctx->aot_inst; - /* Get module_inst->e->common.c_api_func_imports */ - offset_c_api_func_imports = - get_module_inst_extra_offset(comp_ctx) - + (comp_ctx->is_jit_mode - ? offsetof(WASMModuleInstanceExtra, common.c_api_func_imports) - /* offsetof(AOTModuleInstanceExtra, common.c_api_func_imports) */ - : sizeof(uint64)); + /* Get module_inst->c_api_func_imports, jit mode WASMModuleInstance is the + * same layout with AOTModuleInstance */ + offset_c_api_func_imports = offsetof(AOTModuleInstance, c_api_func_imports); offset = I32_CONST(offset_c_api_func_imports); CHECK_LLVM_CONST(offset); c_api_func_imports = diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 64c12d7e11..79a39d06ab 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -124,7 +124,7 @@ create_basic_func_context(const AOTCompContext *comp_ctx, { LLVMValueRef aot_inst_offset = I32_TWO, aot_inst_addr; - /* Save the pameters for fast access */ + /* Save the parameters for fast access */ func_ctx->exec_env = LLVMGetParam(func_ctx->func, 0); /* Get aot inst address, the layout of exec_env is: diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index a1e897aa30..f20993424a 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -616,13 +616,12 @@ wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst); * * @param module_inst the module instance * @param name the name of the function - * @param signature the signature of the function, ignored currently * * @return the function instance found, NULL if not found */ WASM_RUNTIME_API_EXTERN wasm_function_inst_t wasm_runtime_lookup_function(wasm_module_inst_t const module_inst, - const char *name, const char *signature); + const char *name); /** * Get parameter count of the function instance diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index a968d4a965..d8fd2d708c 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -1106,9 +1106,8 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, if (!func_import->call_conv_wasm_c_api) { native_func_pointer = module_inst->import_func_ptrs[cur_func_index]; } - else if (module_inst->e->common.c_api_func_imports) { - c_api_func_import = - module_inst->e->common.c_api_func_imports + cur_func_index; + else if (module_inst->c_api_func_imports) { + c_api_func_import = module_inst->c_api_func_imports + cur_func_index; native_func_pointer = c_api_func_import->func_ptr_linked; } @@ -5427,8 +5426,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, #ifndef OS_ENABLE_HW_BOUND_CHECK CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr); #else - if ((uint64)(uint32)addr + bytes - > (uint64)linear_mem_size) + if ((uint64)(uint32)addr + bytes > linear_mem_size) goto out_of_bounds; maddr = memory->memory_data + (uint32)addr; #endif @@ -5447,7 +5445,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, if (offset + bytes > seg_len) goto out_of_bounds; - bh_memcpy_s(maddr, linear_mem_size - addr, + bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr), data + offset, (uint32)bytes); break; } @@ -5479,11 +5477,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc); CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); #else - if ((uint64)(uint32)src + len > (uint64)linear_mem_size) + if ((uint64)(uint32)src + len > linear_mem_size) goto out_of_bounds; msrc = memory->memory_data + (uint32)src; - if ((uint64)(uint32)dst + len > (uint64)linear_mem_size) + if ((uint64)(uint32)dst + len > linear_mem_size) goto out_of_bounds; mdst = memory->memory_data + (uint32)dst; #endif diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index edc38cf8c4..21412046e1 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -1187,9 +1187,8 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, if (!func_import->call_conv_wasm_c_api) { native_func_pointer = module_inst->import_func_ptrs[cur_func_index]; } - else if (module_inst->e->common.c_api_func_imports) { - c_api_func_import = - module_inst->e->common.c_api_func_imports + cur_func_index; + else if (module_inst->c_api_func_imports) { + c_api_func_import = module_inst->c_api_func_imports + cur_func_index; native_func_pointer = c_api_func_import->func_ptr_linked; } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index d0f4164cea..187a44f49c 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -656,7 +656,7 @@ functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, if (function->import_module_inst) { function->import_func_inst = wasm_lookup_function(function->import_module_inst, - import->u.function.field_name, NULL); + import->u.function.field_name); } } #endif /* WASM_ENABLE_MULTI_MODULE */ @@ -1220,7 +1220,7 @@ lookup_post_instantiate_func(WASMModuleInstance *module_inst, WASMFunctionInstance *func; WASMFuncType *func_type; - if (!(func = wasm_lookup_function(module_inst, func_name, NULL))) + if (!(func = wasm_lookup_function(module_inst, func_name))) /* Not found */ return NULL; @@ -2967,8 +2967,8 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) } #endif - if (module_inst->e->common.c_api_func_imports) - wasm_runtime_free(module_inst->e->common.c_api_func_imports); + if (module_inst->c_api_func_imports) + wasm_runtime_free(module_inst->c_api_func_imports); if (!is_sub_inst) { #if WASM_ENABLE_WASI_NN != 0 @@ -2988,14 +2988,12 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) } WASMFunctionInstance * -wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name, - const char *signature) +wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name) { uint32 i; for (i = 0; i < module_inst->export_func_count; i++) if (!strcmp(module_inst->export_functions[i].name, name)) return module_inst->export_functions[i].function; - (void)signature; return NULL; } @@ -3169,8 +3167,8 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function, hw bound check is enabled */ #endif - /* Set exec env so it can be later retrieved from instance */ - module_inst->e->common.cur_exec_env = exec_env; + /* Set exec env, so it can be later retrieved from instance */ + module_inst->cur_exec_env = exec_env; interp_call_wasm(module_inst, exec_env, function, argc, argv); return !wasm_copy_exception(module_inst, NULL); @@ -4050,9 +4048,8 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc, import_func = &module->import_functions[func_idx].u.function; if (import_func->call_conv_wasm_c_api) { - if (module_inst->e->common.c_api_func_imports) { - c_api_func_import = - module_inst->e->common.c_api_func_imports + func_idx; + if (module_inst->c_api_func_imports) { + c_api_func_import = module_inst->c_api_func_imports + func_idx; func_ptr = c_api_func_import->func_ptr_linked; } else { diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index e38a9d5894..4b6899909d 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -297,10 +297,9 @@ typedef struct CApiFuncImport { /* The common part of WASMModuleInstanceExtra and AOTModuleInstanceExtra */ typedef struct WASMModuleInstanceExtraCommon { - CApiFuncImport *c_api_func_imports; +#if WASM_ENABLE_MODULE_INST_CONTEXT != 0 void *contexts[WASM_MAX_INSTANCE_CONTEXTS]; - /* pointer to the exec env currently used */ - WASMExecEnv *cur_exec_env; +#endif #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0 /* Disable bounds checks or not */ bool disable_bounds_checks; @@ -426,13 +425,16 @@ struct WASMModuleInstance { /* Function performance profiling info list, only available in AOTModuleInstance */ DefPointer(struct AOTFuncPerfProfInfo *, func_perf_profilings); + DefPointer(CApiFuncImport *, c_api_func_imports); + /* Pointer to the exec env currently used */ + DefPointer(WASMExecEnv *, cur_exec_env); /* WASM/AOT module extra info, for AOTModuleInstance, it denotes `AOTModuleInstanceExtra *` */ DefPointer(WASMModuleInstanceExtra *, e); /* Default WASM operand stack size */ uint32 default_wasm_stack_size; - uint32 reserved[3]; + uint32 reserved[5]; /* * +------------------------------+ <-- memories @@ -539,8 +541,7 @@ wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode); WASMFunctionInstance * -wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name, - const char *signature); +wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name); #if WASM_ENABLE_MULTI_MODULE != 0 WASMGlobalInstance * 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 f0ebaa457e..aeaafced75 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 @@ -98,8 +98,8 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) wasm_native_inherit_contexts(new_module_inst, module_inst); - start_func = wasm_runtime_lookup_function(new_module_inst, - THREAD_START_FUNCTION, NULL); + start_func = + wasm_runtime_lookup_function(new_module_inst, THREAD_START_FUNCTION); if (!start_func) { LOG_ERROR("Failed to find thread start function %s", THREAD_START_FUNCTION); diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 4b633ec84c..ac89575010 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -781,10 +781,10 @@ wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst, #if WASM_ENABLE_INTERP != 0 if (module_inst_src->module_type == Wasm_Module_Bytecode) { - new_c_api_func_imports = &(((WASMModuleInstance *)module_inst_dst) - ->e->common.c_api_func_imports); - c_api_func_imports = ((const WASMModuleInstance *)module_inst_src) - ->e->common.c_api_func_imports; + new_c_api_func_imports = + &(((WASMModuleInstance *)module_inst_dst)->c_api_func_imports); + c_api_func_imports = + ((const WASMModuleInstance *)module_inst_src)->c_api_func_imports; import_func_count = ((WASMModule *)(((const WASMModuleInstance *)module_inst_src) ->module)) @@ -793,13 +793,10 @@ wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst, #endif #if WASM_ENABLE_AOT != 0 if (module_inst_src->module_type == Wasm_Module_AoT) { - AOTModuleInstanceExtra *e = - (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_dst)->e; - new_c_api_func_imports = &(e->common.c_api_func_imports); - - e = (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_src)->e; - c_api_func_imports = e->common.c_api_func_imports; - + new_c_api_func_imports = + &(((AOTModuleInstance *)module_inst_dst)->c_api_func_imports); + c_api_func_imports = + ((const AOTModuleInstance *)module_inst_src)->c_api_func_imports; import_func_count = ((AOTModule *)(((AOTModuleInstance *)module_inst_src)->module)) ->import_func_count; diff --git a/doc/embed_wamr.md b/doc/embed_wamr.md index 0bb9fb0328..5e4e3a5122 100644 --- a/doc/embed_wamr.md +++ b/doc/embed_wamr.md @@ -100,7 +100,7 @@ After a module is instantiated, the runtime embedder can lookup the target WASM ```c /* lookup a WASM function by its name The function signature can NULL here */ - func = wasm_runtime_lookup_function(module_inst, "fib", NULL); + func = wasm_runtime_lookup_function(module_inst, "fib"); /* creat an execution environment to execute the WASM functions */ exec_env = wasm_runtime_create_exec_env(module_inst, stack_size); diff --git a/doc/multi_module.md b/doc/multi_module.md index 9ab26673e2..ac9e3bbde4 100644 --- a/doc/multi_module.md +++ b/doc/multi_module.md @@ -62,8 +62,7 @@ WAMR hopes that the native host or embedding environment loads/unloads the modul ```c wasm_function_inst_t wasm_runtime_lookup_function(wasm_module_inst_t const module_inst, - const char *name, - const char *signature); + const char *name); ``` Multi-module allows one to look up an exported function of a submodule. There are two ways to indicate the function _name_: diff --git a/doc/perf_tune.md b/doc/perf_tune.md index b366f09c0c..bb463d7024 100644 --- a/doc/perf_tune.md +++ b/doc/perf_tune.md @@ -288,7 +288,7 @@ And in the host embedder: bool ret; argv[0] = *(uint32 *)&arg_f32; - func = wasm_runtime_lookup_function(module_inst, "foo1", NULL); + func = wasm_runtime_lookup_function(module_inst, "foo1"); ret = wasm_runtime_call_wasm(exec_env, func, 1, argv); if (!ret) { /* handle exception */ diff --git a/language-bindings/go/wamr/instance.go b/language-bindings/go/wamr/instance.go index 86a1fe77ba..111d795bc7 100644 --- a/language-bindings/go/wamr/instance.go +++ b/language-bindings/go/wamr/instance.go @@ -129,8 +129,7 @@ func (self *Instance) CallFunc(funcName string, cName := C.CString(funcName) defer C.free(unsafe.Pointer(cName)) - _func = C.wasm_runtime_lookup_function(self._instance, - cName, (*C.char)(C.NULL)) + _func = C.wasm_runtime_lookup_function(self._instance, cName) if _func == nil { return fmt.Errorf("CallFunc error: lookup function failed") } @@ -170,8 +169,7 @@ func (self *Instance) CallFuncV(funcName string, cName := C.CString(funcName) defer C.free(unsafe.Pointer(cName)) - _func = C.wasm_runtime_lookup_function(self._instance, - cName, (*C.char)(C.NULL)) + _func = C.wasm_runtime_lookup_function(self._instance, cName) if _func == nil { return fmt.Errorf("CallFunc error: lookup function failed") } diff --git a/language-bindings/python/src/wamr/wamrapi/wamr.py b/language-bindings/python/src/wamr/wamrapi/wamr.py index b433ffc17d..9727faa37b 100644 --- a/language-bindings/python/src/wamr/wamrapi/wamr.py +++ b/language-bindings/python/src/wamr/wamrapi/wamr.py @@ -175,7 +175,7 @@ def free(self, wasm_handler) -> None: wasm_runtime_module_free(self.module_inst, wasm_handler) def lookup_function(self, name: str) -> wasm_function_inst_t: - func = wasm_runtime_lookup_function(self.module_inst, name, None) + func = wasm_runtime_lookup_function(self.module_inst, name) if not func: raise Exception("Error while looking-up function") return func diff --git a/product-mini/platforms/zephyr/simple/src/main.c b/product-mini/platforms/zephyr/simple/src/main.c index e70bb5caba..3b389826f1 100644 --- a/product-mini/platforms/zephyr/simple/src/main.c +++ b/product-mini/platforms/zephyr/simple/src/main.c @@ -65,14 +65,12 @@ app_instance_main(wasm_module_inst_t module_inst) wasm_exec_env_t exec_env; unsigned argv[2] = { 0 }; - if (wasm_runtime_lookup_function(module_inst, "main", NULL) - || wasm_runtime_lookup_function(module_inst, "__main_argc_argv", - NULL)) { + if (wasm_runtime_lookup_function(module_inst, "main") + || wasm_runtime_lookup_function(module_inst, "__main_argc_argv")) { LOG_VERBOSE("Calling main function\n"); wasm_application_execute_main(module_inst, app_argc, app_argv); } - else if ((func = wasm_runtime_lookup_function(module_inst, "app_main", - NULL))) { + else if ((func = wasm_runtime_lookup_function(module_inst, "app_main"))) { exec_env = wasm_runtime_create_exec_env(module_inst, CONFIG_APP_HEAP_SIZE); if (!exec_env) { diff --git a/samples/basic/src/main.c b/samples/basic/src/main.c index 445d4e4d50..406b2427cd 100644 --- a/samples/basic/src/main.c +++ b/samples/basic/src/main.c @@ -149,8 +149,7 @@ main(int argc, char *argv_main[]) goto fail; } - if (!(func = wasm_runtime_lookup_function(module_inst, "generate_float", - NULL))) { + if (!(func = wasm_runtime_lookup_function(module_inst, "generate_float"))) { printf("The generate_float wasm function is not found.\n"); goto fail; } @@ -189,8 +188,8 @@ main(int argc, char *argv_main[]) argv2[4] = 3; // the last argument is the digits after decimal point for // converting float to string - if (!(func2 = wasm_runtime_lookup_function(module_inst, "float_to_string", - NULL))) { + if (!(func2 = + wasm_runtime_lookup_function(module_inst, "float_to_string"))) { printf( "The wasm function float_to_string wasm function is not found.\n"); goto fail; @@ -208,7 +207,7 @@ main(int argc, char *argv_main[]) } wasm_function_inst_t func3 = - wasm_runtime_lookup_function(module_inst, "calculate", NULL); + wasm_runtime_lookup_function(module_inst, "calculate"); if (!func3) { printf("The wasm function calculate is not found.\n"); goto fail; diff --git a/samples/inst-context/src/main.c b/samples/inst-context/src/main.c index 0d774735e4..f5068deffc 100644 --- a/samples/inst-context/src/main.c +++ b/samples/inst-context/src/main.c @@ -128,7 +128,7 @@ main(int argc, char *argv_main[]) } wasm_function_inst_t func3 = - wasm_runtime_lookup_function(module_inst, "calculate", NULL); + wasm_runtime_lookup_function(module_inst, "calculate"); if (!func3) { printf("The wasm function calculate is not found.\n"); goto fail; diff --git a/samples/ref-types/src/hello.c b/samples/ref-types/src/hello.c index 0ee1aee88e..8a6f968c3a 100644 --- a/samples/ref-types/src/hello.c +++ b/samples/ref-types/src/hello.c @@ -234,19 +234,19 @@ main(int argc, char *argv[]) /* lookup function instance */ if (!(wasm_cmp_externref_ptr = wasm_runtime_lookup_function( - wasm_module_inst, "cmp-externref", NULL))) { + wasm_module_inst, "cmp-externref"))) { printf("%s\n", "lookup function cmp-externref failed"); goto fail; } if (!(wasm_get_externref_ptr = wasm_runtime_lookup_function( - wasm_module_inst, "get-externref", NULL))) { + wasm_module_inst, "get-externref"))) { printf("%s\n", "lookup function get-externref failed"); goto fail; } if (!(wasm_set_externref_ptr = wasm_runtime_lookup_function( - wasm_module_inst, "set-externref", NULL))) { + wasm_module_inst, "set-externref"))) { printf("%s\n", "lookup function set-externref failed"); goto fail; } diff --git a/samples/shared-module/src/main.c b/samples/shared-module/src/main.c index ebea0c6bfd..7efbc4d0b9 100644 --- a/samples/shared-module/src/main.c +++ b/samples/shared-module/src/main.c @@ -104,15 +104,15 @@ main(int argc, char *argv_main[]) goto fail; } - func_test_data_drop[i] = wasm_runtime_lookup_function( - module_inst[i], name_test_data_drop, NULL); + func_test_data_drop[i] = + wasm_runtime_lookup_function(module_inst[i], name_test_data_drop); if (!func_test_data_drop[i]) { printf("The wasm function %s is not found.\n", name_test_data_drop); goto fail; } - func_test_elem_drop[i] = wasm_runtime_lookup_function( - module_inst[i], name_test_elem_drop, NULL); + func_test_elem_drop[i] = + wasm_runtime_lookup_function(module_inst[i], name_test_elem_drop); if (!func_test_elem_drop[i]) { printf("The wasm function %s is not found.\n", name_test_elem_drop); goto fail; diff --git a/samples/spawn-thread/src/main.c b/samples/spawn-thread/src/main.c index 9501ae4c03..ecdf679a2b 100644 --- a/samples/spawn-thread/src/main.c +++ b/samples/spawn-thread/src/main.c @@ -29,7 +29,7 @@ thread(void *arg) return NULL; } - func = wasm_runtime_lookup_function(module_inst, "sum", NULL); + func = wasm_runtime_lookup_function(module_inst, "sum"); if (!func) { printf("failed to lookup function sum"); wasm_runtime_destroy_thread_env(); @@ -57,7 +57,7 @@ wamr_thread_cb(wasm_exec_env_t exec_env, void *arg) wasm_function_inst_t func; uint32 argv[2]; - func = wasm_runtime_lookup_function(module_inst, "sum", NULL); + func = wasm_runtime_lookup_function(module_inst, "sum"); if (!func) { printf("failed to lookup function sum"); return NULL; @@ -133,7 +133,7 @@ main(int argc, char *argv[]) goto fail4; } - func = wasm_runtime_lookup_function(wasm_module_inst, "sum", NULL); + func = wasm_runtime_lookup_function(wasm_module_inst, "sum"); if (!func) { printf("failed to lookup function sum"); goto fail5; diff --git a/samples/terminate/src/main.c b/samples/terminate/src/main.c index 4885b0b11b..fb0842a2b5 100644 --- a/samples/terminate/src/main.c +++ b/samples/terminate/src/main.c @@ -39,7 +39,7 @@ runner_with_spawn_exec_env(void *vp) wasm_function_inst_t func; bool ok = wasm_runtime_init_thread_env(); assert(ok); - func = wasm_runtime_lookup_function(inst, "block_forever", NULL); + func = wasm_runtime_lookup_function(inst, "block_forever"); assert(func != NULL); wasm_runtime_call_wasm(env, func, 0, NULL); wasm_runtime_destroy_spawned_exec_env(env);