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

@ -694,34 +694,10 @@ wasm_loader_find_export(const WASMModule *module, const char *module_name,
const char *field_name, uint8 export_kind,
char *error_buf, uint32 error_buf_size)
{
WASMExport *export;
uint32 i;
for (i = 0, export = module->exports; i < module->export_count;
++i, ++export) {
/**
* need to consider a scenario that different kinds of exports
* may have the same name, like
* (table (export "m1" "exported") 10 funcref)
* (memory (export "m1" "exported") 10)
**/
if (export->kind == export_kind && !strcmp(field_name, export->name)) {
break;
}
}
if (i == module->export_count) {
LOG_DEBUG("can not find an export %d named %s in the module %s",
export_kind, field_name, module_name);
set_error_buf(error_buf, error_buf_size,
"unknown import or incompatible import type");
return NULL;
}
(void)module_name;
/* since there is a validation in load_export_section(), it is for sure
* export->index is valid*/
WASMExport *export =
loader_find_export((WASMModuleCommon *)module, module_name, field_name,
export_kind, error_buf, error_buf_size);
;
return export;
}
#endif
@ -912,152 +888,6 @@ wasm_loader_resolve_global(const char *module_name, const char *global_name,
return global;
}
static WASMModule *
search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
{
WASMRegisteredModule *node =
bh_list_first_elem(parent_module->import_module_list);
while (node && strcmp(sub_module_name, node->module_name)) {
node = bh_list_elem_next(node);
}
return node ? (WASMModule *)node->module : NULL;
}
static bool
register_sub_module(const WASMModule *parent_module,
const char *sub_module_name, WASMModule *sub_module)
{
/* register sub_module into its parent sub module list */
WASMRegisteredModule *node = NULL;
bh_list_status ret;
if (search_sub_module(parent_module, sub_module_name)) {
LOG_DEBUG("%s has been registered in its parent", sub_module_name);
return true;
}
node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
if (!node) {
return false;
}
node->module_name = sub_module_name;
node->module = (WASMModuleCommon *)sub_module;
ret = bh_list_insert(parent_module->import_module_list, node);
bh_assert(BH_LIST_SUCCESS == ret);
(void)ret;
return true;
}
static WASMModule *
load_depended_module(const WASMModule *parent_module,
const char *sub_module_name, char *error_buf,
uint32 error_buf_size)
{
WASMModule *sub_module = NULL;
bool ret = false;
uint8 *buffer = NULL;
uint32 buffer_size = 0;
const module_reader reader = wasm_runtime_get_module_reader();
const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
/* check the registered module list of the parent */
sub_module = search_sub_module(parent_module, sub_module_name);
if (sub_module) {
LOG_DEBUG("%s has been loaded before", sub_module_name);
return sub_module;
}
/* check the global registered module list */
sub_module =
(WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
if (sub_module) {
LOG_DEBUG("%s has been loaded", sub_module_name);
goto register_sub_module;
}
LOG_VERBOSE("loading %s", sub_module_name);
if (!reader) {
set_error_buf_v(error_buf, error_buf_size,
"no sub module reader to load %s", sub_module_name);
return NULL;
}
/* start to maintain a loading module list */
ret = wasm_runtime_is_loading_module(sub_module_name);
if (ret) {
set_error_buf_v(error_buf, error_buf_size,
"found circular dependency on %s", sub_module_name);
return NULL;
}
ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
error_buf_size);
if (!ret) {
LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
return NULL;
}
ret = reader(sub_module_name, &buffer, &buffer_size);
if (!ret) {
LOG_DEBUG("read the file of %s failed", sub_module_name);
set_error_buf_v(error_buf, error_buf_size, "unknown import",
sub_module_name);
goto delete_loading_module;
}
sub_module =
wasm_loader_load(buffer, buffer_size, false, error_buf, error_buf_size);
if (!sub_module) {
LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
/* others will be destroyed in runtime_destroy() */
goto destroy_file_buffer;
}
wasm_runtime_delete_loading_module(sub_module_name);
/* register on a global list */
ret = wasm_runtime_register_module_internal(
sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
error_buf, error_buf_size);
if (!ret) {
LOG_DEBUG("error: can not register module %s globally\n",
sub_module_name);
/* others will be unloaded in runtime_destroy() */
goto unload_module;
}
/* register into its parent list */
register_sub_module:
ret = register_sub_module(parent_module, sub_module_name, sub_module);
if (!ret) {
set_error_buf_v(error_buf, error_buf_size,
"failed to register sub module %s", sub_module_name);
/* since it is in the global module list, no need to
* unload the module. the runtime_destroy() will do it
*/
return NULL;
}
return sub_module;
unload_module:
wasm_loader_unload(sub_module);
destroy_file_buffer:
if (destroyer) {
destroyer(buffer, buffer_size);
}
else {
LOG_WARNING("need to release the reading buffer of %s manually",
sub_module_name);
}
delete_loading_module:
wasm_runtime_delete_loading_module(sub_module_name);
return NULL;
}
#endif /* end of WASM_ENABLE_MULTI_MODULE */
static bool
@ -1104,8 +934,9 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end,
#if WASM_ENABLE_MULTI_MODULE != 0
else {
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
sub_module = load_depended_module(parent_module, sub_module_name,
error_buf, error_buf_size);
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
error_buf_size);
if (!sub_module) {
return false;
}
@ -1193,8 +1024,9 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
#if WASM_ENABLE_MULTI_MODULE != 0
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
sub_module = load_depended_module(parent_module, sub_module_name,
error_buf, error_buf_size);
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
error_buf_size);
if (!sub_module) {
return false;
}
@ -1327,8 +1159,9 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
#if WASM_ENABLE_MULTI_MODULE != 0
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
sub_module = load_depended_module(parent_module, sub_module_name,
error_buf, error_buf_size);
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
error_buf_size);
if (!sub_module) {
return false;
}
@ -1427,8 +1260,9 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
#if WASM_ENABLE_MULTI_MODULE != 0
if (!global->is_linked
&& !wasm_runtime_is_built_in_module(sub_module_name)) {
sub_module = load_depended_module(parent_module, sub_module_name,
error_buf, error_buf_size);
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
error_buf_size);
if (!sub_module) {
return false;
}