Fix three multi-threading and wasm-c-api-imports issues (#2173)
Fix issue reported in #2172: wasm-c-api `wasm_func_call` may use a wrong exec_env when multi-threading is enabled, with error "invalid exec env" reported Fix issue reported in #2149: main instance's `c_api_func_imports` are not passed to the counterpart of new thread's instance in wasi-threads mode Fix issue of invalid size calculated to copy `c_api_func_imports` in pthread mode And refactor the code to use `wasm_cluster_dup_c_api_imports` to copy the `c_api_func_imports` to new thread for wasi-threads mode and pthread mode.
This commit is contained in:
@ -733,6 +733,55 @@ fail1:
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
|
||||
const WASMModuleInstanceCommon *module_inst_src)
|
||||
{
|
||||
/* workaround about passing instantiate-linking information */
|
||||
CApiFuncImport **new_c_api_func_imports = NULL;
|
||||
CApiFuncImport *c_api_func_imports;
|
||||
uint32 import_func_count = 0;
|
||||
uint32 size_in_bytes = 0;
|
||||
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst_src->module_type == Wasm_Module_Bytecode) {
|
||||
new_c_api_func_imports =
|
||||
&(((WASMModuleInstance *)module_inst_dst)->e->c_api_func_imports);
|
||||
c_api_func_imports = ((const WASMModuleInstance *)module_inst_src)
|
||||
->e->c_api_func_imports;
|
||||
import_func_count =
|
||||
((WASMModule *)(((const WASMModuleInstance *)module_inst_src)
|
||||
->module))
|
||||
->import_function_count;
|
||||
}
|
||||
#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->c_api_func_imports);
|
||||
|
||||
e = (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_src)->e;
|
||||
c_api_func_imports = e->c_api_func_imports;
|
||||
|
||||
import_func_count =
|
||||
((AOTModule *)(((AOTModuleInstance *)module_inst_src)->module))
|
||||
->import_func_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (import_func_count != 0 && c_api_func_imports) {
|
||||
size_in_bytes = sizeof(CApiFuncImport) * import_func_count;
|
||||
*new_c_api_func_imports = wasm_runtime_malloc(size_in_bytes);
|
||||
if (!(*new_c_api_func_imports))
|
||||
return false;
|
||||
|
||||
bh_memcpy_s(*new_c_api_func_imports, size_in_bytes, c_api_func_imports,
|
||||
size_in_bytes);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||
WASMCurrentEnvStatus *
|
||||
wasm_cluster_create_exenv_status()
|
||||
|
||||
Reference in New Issue
Block a user