Fix wasm-c-api import func link issue in wasm_instance_new (#1787)
When a wasm module is duplicated instantiated with wasm_instance_new, the function import info of the previous instantiation may be overwritten by the later instantiation, which may cause unexpected behavior. Store the function import info into the module instance to fix the issue.
This commit is contained in:
@ -189,7 +189,6 @@ typedef struct WASMFunctionImport {
|
||||
WASMFunction *import_func_linked;
|
||||
#endif
|
||||
bool call_conv_wasm_c_api;
|
||||
bool wasm_c_api_with_env;
|
||||
} WASMFunctionImport;
|
||||
|
||||
typedef struct WASMGlobalImport {
|
||||
|
||||
@ -838,6 +838,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
||||
WASMInterpFrame *prev_frame)
|
||||
{
|
||||
WASMFunctionImport *func_import = cur_func->u.func_import;
|
||||
CApiFuncImport *c_api_func_import = NULL;
|
||||
unsigned local_cell_num = 2;
|
||||
WASMInterpFrame *frame;
|
||||
uint32 argv_ret[2], cur_func_index;
|
||||
@ -858,7 +859,13 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
||||
|
||||
cur_func_index = (uint32)(cur_func - module_inst->e->functions);
|
||||
bh_assert(cur_func_index < module_inst->module->import_function_count);
|
||||
native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
|
||||
if (!func_import->call_conv_wasm_c_api) {
|
||||
native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
|
||||
}
|
||||
else {
|
||||
c_api_func_import = module_inst->e->c_api_func_imports + cur_func_index;
|
||||
native_func_pointer = c_api_func_import->func_ptr_linked;
|
||||
}
|
||||
|
||||
if (!native_func_pointer) {
|
||||
snprintf(buf, sizeof(buf),
|
||||
@ -872,7 +879,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
||||
ret = wasm_runtime_invoke_c_api_native(
|
||||
(WASMModuleInstanceCommon *)module_inst, native_func_pointer,
|
||||
func_import->func_type, cur_func->param_cell_num, frame->lp,
|
||||
func_import->wasm_c_api_with_env, func_import->attachment);
|
||||
c_api_func_import->with_env_arg, c_api_func_import->env_arg);
|
||||
if (ret) {
|
||||
argv_ret[0] = frame->lp[0];
|
||||
argv_ret[1] = frame->lp[1];
|
||||
|
||||
@ -902,6 +902,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
||||
WASMInterpFrame *prev_frame)
|
||||
{
|
||||
WASMFunctionImport *func_import = cur_func->u.func_import;
|
||||
CApiFuncImport *c_api_func_import = NULL;
|
||||
unsigned local_cell_num = 2;
|
||||
WASMInterpFrame *frame;
|
||||
uint32 argv_ret[2], cur_func_index;
|
||||
@ -921,7 +922,13 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
||||
|
||||
cur_func_index = (uint32)(cur_func - module_inst->e->functions);
|
||||
bh_assert(cur_func_index < module_inst->module->import_function_count);
|
||||
native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
|
||||
if (!func_import->call_conv_wasm_c_api) {
|
||||
native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
|
||||
}
|
||||
else {
|
||||
c_api_func_import = module_inst->e->c_api_func_imports + cur_func_index;
|
||||
native_func_pointer = c_api_func_import->func_ptr_linked;
|
||||
}
|
||||
|
||||
if (!native_func_pointer) {
|
||||
char buf[128];
|
||||
@ -936,7 +943,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
|
||||
ret = wasm_runtime_invoke_c_api_native(
|
||||
(WASMModuleInstanceCommon *)module_inst, native_func_pointer,
|
||||
func_import->func_type, cur_func->param_cell_num, frame->lp,
|
||||
func_import->wasm_c_api_with_env, func_import->attachment);
|
||||
c_api_func_import->with_env_arg, c_api_func_import->env_arg);
|
||||
if (ret) {
|
||||
argv_ret[0] = frame->lp[0];
|
||||
argv_ret[1] = frame->lp[1];
|
||||
|
||||
@ -1948,6 +1948,9 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
|
||||
os_mutex_destroy(&module_inst->e->mem_lock);
|
||||
#endif
|
||||
|
||||
if (module_inst->e->c_api_func_imports)
|
||||
wasm_runtime_free(module_inst->e->c_api_func_imports);
|
||||
|
||||
wasm_runtime_free(module_inst);
|
||||
}
|
||||
|
||||
@ -2849,6 +2852,7 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
|
||||
WASMType *func_type;
|
||||
void *func_ptr;
|
||||
WASMFunctionImport *import_func;
|
||||
CApiFuncImport *c_api_func_import = NULL;
|
||||
const char *signature;
|
||||
void *attachment;
|
||||
char buf[96];
|
||||
@ -2870,6 +2874,11 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
|
||||
bh_assert(func_idx < module->import_function_count);
|
||||
|
||||
import_func = &module->import_functions[func_idx].u.function;
|
||||
if (import_func->call_conv_wasm_c_api) {
|
||||
c_api_func_import = module_inst->e->c_api_func_imports + func_idx;
|
||||
func_ptr = c_api_func_import->func_ptr_linked;
|
||||
}
|
||||
|
||||
if (!func_ptr) {
|
||||
snprintf(buf, sizeof(buf),
|
||||
"failed to call unlinked import function (%s, %s)",
|
||||
@ -2882,7 +2891,7 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
|
||||
if (import_func->call_conv_wasm_c_api) {
|
||||
ret = wasm_runtime_invoke_c_api_native(
|
||||
(WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
|
||||
argv, import_func->wasm_c_api_with_env, attachment);
|
||||
argv, c_api_func_import->with_env_arg, c_api_func_import->env_arg);
|
||||
}
|
||||
else if (!import_func->call_conv_raw) {
|
||||
signature = import_func->signature;
|
||||
|
||||
@ -192,6 +192,16 @@ typedef struct WASMExportMemInstance {
|
||||
WASMMemoryInstance *memory;
|
||||
} WASMExportMemInstance;
|
||||
|
||||
/* wasm-c-api import function info */
|
||||
typedef struct CApiFuncImport {
|
||||
/* host func pointer after linked */
|
||||
void *func_ptr_linked;
|
||||
/* whether the host func has env argument */
|
||||
bool with_env_arg;
|
||||
/* the env argument of the host func */
|
||||
void *env_arg;
|
||||
} CApiFuncImport;
|
||||
|
||||
/* Extra info of WASM module instance for interpreter/jit mode */
|
||||
typedef struct WASMModuleInstanceExtra {
|
||||
WASMGlobalInstance *globals;
|
||||
@ -205,6 +215,8 @@ typedef struct WASMModuleInstanceExtra {
|
||||
WASMFunctionInstance *free_function;
|
||||
WASMFunctionInstance *retain_function;
|
||||
|
||||
CApiFuncImport *c_api_func_imports;
|
||||
|
||||
#if WASM_ENABLE_SHARED_MEMORY != 0
|
||||
/* lock for shared memory atomic operations */
|
||||
korp_mutex mem_lock;
|
||||
|
||||
Reference in New Issue
Block a user