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:
committed by
GitHub
parent
fff0e2ad1c
commit
79b27c1934
@ -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;
|
||||
}
|
||||
|
||||
@ -51,11 +51,15 @@ set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
|
||||
}
|
||||
|
||||
WASMModule *
|
||||
wasm_load(uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
|
||||
wasm_load(uint8 *buf, uint32 size,
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
bool main_module,
|
||||
#endif
|
||||
char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
return wasm_loader_load(buf, size,
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
true,
|
||||
main_module,
|
||||
#endif
|
||||
error_buf, error_buf_size);
|
||||
}
|
||||
@ -1265,78 +1269,6 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
static bool
|
||||
sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
|
||||
uint32 stack_size, uint32 heap_size, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
{
|
||||
bh_list *sub_module_inst_list = module_inst->e->sub_module_inst_list;
|
||||
WASMRegisteredModule *sub_module_list_node =
|
||||
bh_list_first_elem(module->import_module_list);
|
||||
|
||||
while (sub_module_list_node) {
|
||||
WASMSubModInstNode *sub_module_inst_list_node = NULL;
|
||||
WASMModule *sub_module = (WASMModule *)sub_module_list_node->module;
|
||||
WASMModuleInstance *sub_module_inst = NULL;
|
||||
|
||||
sub_module_inst =
|
||||
wasm_instantiate(sub_module, NULL, NULL, stack_size, heap_size,
|
||||
error_buf, error_buf_size);
|
||||
if (!sub_module_inst) {
|
||||
LOG_DEBUG("instantiate %s failed",
|
||||
sub_module_list_node->module_name);
|
||||
goto failed;
|
||||
}
|
||||
|
||||
sub_module_inst_list_node = runtime_malloc(sizeof(WASMSubModInstNode),
|
||||
error_buf, error_buf_size);
|
||||
if (!sub_module_inst_list_node) {
|
||||
LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
|
||||
sizeof(WASMSubModInstNode));
|
||||
goto failed;
|
||||
}
|
||||
|
||||
sub_module_inst_list_node->module_inst = sub_module_inst;
|
||||
sub_module_inst_list_node->module_name =
|
||||
sub_module_list_node->module_name;
|
||||
bh_list_status ret =
|
||||
bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
|
||||
bh_assert(BH_LIST_SUCCESS == ret);
|
||||
(void)ret;
|
||||
|
||||
sub_module_list_node = bh_list_elem_next(sub_module_list_node);
|
||||
|
||||
continue;
|
||||
failed:
|
||||
if (sub_module_inst_list_node) {
|
||||
bh_list_remove(sub_module_inst_list, sub_module_inst_list_node);
|
||||
wasm_runtime_free(sub_module_inst_list_node);
|
||||
}
|
||||
|
||||
if (sub_module_inst)
|
||||
wasm_deinstantiate(sub_module_inst, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
sub_module_deinstantiate(WASMModuleInstance *module_inst)
|
||||
{
|
||||
bh_list *list = module_inst->e->sub_module_inst_list;
|
||||
WASMSubModInstNode *node = bh_list_first_elem(list);
|
||||
while (node) {
|
||||
WASMSubModInstNode *next_node = bh_list_elem_next(node);
|
||||
bh_list_remove(list, node);
|
||||
wasm_deinstantiate(node->module_inst, false);
|
||||
wasm_runtime_free(node);
|
||||
node = next_node;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool
|
||||
check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
|
||||
uint32 error_buf_size)
|
||||
@ -1713,8 +1645,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
module_inst->e->sub_module_inst_list =
|
||||
&module_inst->e->sub_module_inst_list_head;
|
||||
ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
|
||||
error_buf, error_buf_size);
|
||||
ret = wasm_runtime_sub_module_instantiate(
|
||||
(WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst,
|
||||
stack_size, heap_size, error_buf, error_buf_size);
|
||||
if (!ret) {
|
||||
LOG_DEBUG("build a sub module list failed");
|
||||
goto fail;
|
||||
@ -2197,7 +2130,8 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
sub_module_deinstantiate(module_inst);
|
||||
wasm_runtime_sub_module_deinstantiate(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
|
||||
if (module_inst->memory_count > 0)
|
||||
|
||||
@ -396,7 +396,11 @@ wasm_get_func_code_end(WASMFunctionInstance *func)
|
||||
}
|
||||
|
||||
WASMModule *
|
||||
wasm_load(uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size);
|
||||
wasm_load(uint8 *buf, uint32 size,
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
bool main_module,
|
||||
#endif
|
||||
char *error_buf, uint32 error_buf_size);
|
||||
|
||||
WASMModule *
|
||||
wasm_load_from_sections(WASMSection *section_list, char *error_buf,
|
||||
|
||||
Reference in New Issue
Block a user