Add no_resolve to LoadArgs and wasm_runtime_resolve_symbols (#3790)

Add no_resolve to LoadArgs and wasm_runtime_resolve_symbols so one can
delay resolving of symbols.

This is useful for inspecting the module between loading and instantiating.
This commit is contained in:
Anders Bakken
2024-09-19 17:54:09 -07:00
committed by GitHub
parent 51a71092bf
commit 21330990a8
9 changed files with 337 additions and 241 deletions

View File

@ -5111,3 +5111,125 @@ aot_get_module_name(AOTModule *module)
{
return module->name;
}
bool
aot_resolve_symbols(AOTModule *module)
{
bool ret = true;
uint32 idx;
for (idx = 0; idx < module->import_func_count; ++idx) {
AOTImportFunc *aot_import_func = &module->import_funcs[idx];
if (!aot_import_func->func_ptr_linked) {
if (!aot_resolve_import_func(module, aot_import_func)) {
LOG_WARNING("Failed to link function (%s, %s)",
aot_import_func->module_name,
aot_import_func->func_name);
ret = false;
}
}
}
return ret;
}
#if WASM_ENABLE_MULTI_MODULE != 0
static void *
aot_resolve_function(const AOTModule *module, const char *function_name,
const AOTFuncType *expected_function_type, char *error_buf,
uint32 error_buf_size);
static void *
aot_resolve_function_ex(const char *module_name, const char *function_name,
const AOTFuncType *expected_function_type,
char *error_buf, uint32 error_buf_size)
{
WASMModuleCommon *module_reg;
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;
}
return aot_resolve_function((AOTModule *)module_reg, function_name,
expected_function_type, error_buf,
error_buf_size);
}
static void *
aot_resolve_function(const AOTModule *module, const char *function_name,
const AOTFuncType *expected_function_type, char *error_buf,
uint32 error_buf_size)
{
void *function = NULL;
AOTExport *export = NULL;
AOTFuncType *target_function_type = NULL;
export = loader_find_export((WASMModuleCommon *)module, 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 =
(AOTFuncType *)module
->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((WASMType *)expected_function_type,
(WASMType *)target_function_type, module->types,
module->type_count)) {
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 */
bool
aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func)
{
#if WASM_ENABLE_MULTI_MODULE != 0
char error_buf[128];
AOTModule *sub_module = NULL;
#endif
import_func->func_ptr_linked = wasm_native_resolve_symbol(
import_func->module_name, import_func->func_name,
import_func->func_type, &import_func->signature,
&import_func->attachment, &import_func->call_conv_raw);
#if WASM_ENABLE_MULTI_MODULE != 0
if (!import_func->func_ptr_linked) {
if (!wasm_runtime_is_built_in_module(import_func->module_name)) {
sub_module = (AOTModule *)wasm_runtime_load_depended_module(
(WASMModuleCommon *)module, import_func->module_name, error_buf,
sizeof(error_buf));
if (!sub_module) {
LOG_WARNING("Failed to load sub module: %s", error_buf);
}
if (!sub_module)
import_func->func_ptr_linked = aot_resolve_function_ex(
import_func->module_name, import_func->func_name,
import_func->func_type, error_buf, sizeof(error_buf));
else
import_func->func_ptr_linked = aot_resolve_function(
sub_module, import_func->func_name, import_func->func_type,
error_buf, sizeof(error_buf));
if (!import_func->func_ptr_linked) {
LOG_WARNING("Failed to link function: %s", error_buf);
}
}
}
#endif
return import_func->func_ptr_linked != NULL;
}