Support muti-module for AOT mode (#2482)

Support muti-module for AOT mode, currently only implement the
multi-module's function import feature for AOT, the memory/table/
global import are not implemented yet.

And update wamr-test-suites scripts, multi-module sample and some
CIs accordingly.
This commit is contained in:
dongsheng28849455
2023-09-28 08:56:11 +08:00
committed by GitHub
parent fff0e2ad1c
commit 79b27c1934
20 changed files with 1018 additions and 343 deletions

View File

@ -558,6 +558,56 @@ str2uint32(const char *buf, uint32 *p_res);
static bool
str2uint64(const char *buf, uint64 *p_res);
#if WASM_ENABLE_MULTI_MODULE != 0
static void *
aot_loader_resolve_function(const char *module_name, const char *function_name,
const AOTFuncType *expected_function_type,
char *error_buf, uint32 error_buf_size)
{
WASMModuleCommon *module_reg;
void *function = NULL;
AOTExport *export = NULL;
AOTModule *module = NULL;
AOTFuncType *target_function_type = NULL;
module_reg = wasm_runtime_find_module_registered(module_name);
if (!module_reg || module_reg->module_type != Wasm_Module_AoT) {
LOG_DEBUG("can not find a module named %s for function %s", module_name,
function_name);
set_error_buf(error_buf, error_buf_size, "unknown import");
return NULL;
}
module = (AOTModule *)module_reg;
export = loader_find_export(module_reg, module_name, function_name,
EXPORT_KIND_FUNC, error_buf, error_buf_size);
if (!export) {
return NULL;
}
/* resolve function type and function */
if (export->index < module->import_func_count) {
target_function_type = module->import_funcs[export->index].func_type;
function = module->import_funcs[export->index].func_ptr_linked;
}
else {
target_function_type =
module->func_types[module->func_type_indexes
[export->index - module->import_func_count]];
function =
(module->func_ptrs[export->index - module->import_func_count]);
}
/* check function type */
if (!wasm_type_equal(expected_function_type, target_function_type)) {
LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
set_error_buf(error_buf, error_buf_size, "incompatible import type");
return NULL;
}
return function;
}
#endif /* end of WASM_ENABLE_MULTI_MODULE */
static bool
load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, bool is_load_from_file_buf,
@ -1357,11 +1407,16 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
bool is_load_from_file_buf, char *error_buf,
uint32 error_buf_size)
{
const char *module_name, *field_name;
char *module_name, *field_name;
const uint8 *buf = *p_buf;
AOTImportFunc *import_funcs;
uint64 size;
uint32 i;
#if WASM_ENABLE_MULTI_MODULE != 0
AOTModule *sub_module = NULL;
AOTFunc *linked_func = NULL;
WASMType *declare_func_type = NULL;
#endif
/* Allocate memory */
size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
@ -1377,17 +1432,46 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
set_error_buf(error_buf, error_buf_size, "unknown type");
return false;
}
#if WASM_ENABLE_MULTI_MODULE != 0
declare_func_type = module->func_types[import_funcs[i].func_type_index];
read_string(buf, buf_end, module_name);
read_string(buf, buf_end, field_name);
import_funcs[i].module_name = module_name;
import_funcs[i].func_name = field_name;
linked_func = wasm_native_resolve_symbol(
module_name, field_name, declare_func_type,
&import_funcs[i].signature, &import_funcs[i].attachment,
&import_funcs[i].call_conv_raw);
if (!linked_func) {
if (!wasm_runtime_is_built_in_module(module_name)) {
sub_module = (AOTModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)module, module_name, error_buf,
error_buf_size);
if (!sub_module) {
return false;
}
}
linked_func = aot_loader_resolve_function(
module_name, field_name, declare_func_type, error_buf,
error_buf_size);
}
import_funcs[i].func_ptr_linked = linked_func;
import_funcs[i].func_type = declare_func_type;
#else
import_funcs[i].func_type =
module->func_types[import_funcs[i].func_type_index];
read_string(buf, buf_end, import_funcs[i].module_name);
read_string(buf, buf_end, import_funcs[i].func_name);
module_name = import_funcs[i].module_name;
field_name = import_funcs[i].func_name;
import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol(
module_name, field_name, import_funcs[i].func_type,
&import_funcs[i].signature, &import_funcs[i].attachment,
&import_funcs[i].call_conv_raw);
#endif
#if WASM_ENABLE_LIBC_WASI != 0
if (!strcmp(import_funcs[i].module_name, "wasi_unstable")
@ -2872,12 +2956,17 @@ create_module(char *error_buf, uint32 error_buf_size)
AOTModule *module =
loader_malloc(sizeof(AOTModule), error_buf, error_buf_size);
bh_list_status ret;
if (!module) {
return NULL;
}
module->module_type = Wasm_Module_AoT;
#if WASM_ENABLE_MULTI_MODULE != 0
module->import_module_list = &module->import_module_list_head;
ret = bh_list_init(module->import_module_list);
bh_assert(ret == BH_LIST_SUCCESS);
#endif
(void)ret;
return module;
}
@ -3201,6 +3290,19 @@ aot_unload(AOTModule *module)
if (module->const_str_set)
bh_hash_map_destroy(module->const_str_set);
#if WASM_ENABLE_MULTI_MODULE != 0
/* just release the sub module list */
if (module->import_module_list) {
WASMRegisteredModule *node =
bh_list_first_elem(module->import_module_list);
while (node) {
WASMRegisteredModule *next = bh_list_elem_next(node);
bh_list_remove(module->import_module_list, node);
wasm_runtime_free(node);
node = next;
}
}
#endif
if (module->code && !module->is_indirect_mode) {
/* The layout is: literal size + literal + code (with plt table) */