Skip to content

Commit 2133099

Browse files
authored
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.
1 parent 51a7109 commit 2133099

File tree

9 files changed

+337
-241
lines changed

9 files changed

+337
-241
lines changed

core/iwasm/aot/aot_loader.c

+21-129
Original file line numberDiff line numberDiff line change
@@ -634,73 +634,6 @@ str2uint32(const char *buf, uint32 *p_res);
634634
static bool
635635
str2uint64(const char *buf, uint64 *p_res);
636636

637-
#if WASM_ENABLE_MULTI_MODULE != 0
638-
static void *
639-
aot_loader_resolve_function(const AOTModule *module, const char *function_name,
640-
const AOTFuncType *expected_function_type,
641-
char *error_buf, uint32 error_buf_size);
642-
643-
static void *
644-
aot_loader_resolve_function_ex(const char *module_name,
645-
const char *function_name,
646-
const AOTFuncType *expected_function_type,
647-
char *error_buf, uint32 error_buf_size)
648-
{
649-
WASMModuleCommon *module_reg;
650-
651-
module_reg = wasm_runtime_find_module_registered(module_name);
652-
if (!module_reg || module_reg->module_type != Wasm_Module_AoT) {
653-
LOG_DEBUG("can not find a module named %s for function %s", module_name,
654-
function_name);
655-
set_error_buf(error_buf, error_buf_size, "unknown import");
656-
return NULL;
657-
}
658-
return aot_loader_resolve_function((AOTModule *)module_reg, function_name,
659-
expected_function_type, error_buf,
660-
error_buf_size);
661-
}
662-
663-
static void *
664-
aot_loader_resolve_function(const AOTModule *module, const char *function_name,
665-
const AOTFuncType *expected_function_type,
666-
char *error_buf, uint32 error_buf_size)
667-
{
668-
void *function = NULL;
669-
AOTExport *export = NULL;
670-
AOTFuncType *target_function_type = NULL;
671-
672-
export = loader_find_export((WASMModuleCommon *)module, module->name,
673-
function_name, EXPORT_KIND_FUNC, error_buf,
674-
error_buf_size);
675-
if (!export) {
676-
return NULL;
677-
}
678-
679-
/* resolve function type and function */
680-
if (export->index < module->import_func_count) {
681-
target_function_type = module->import_funcs[export->index].func_type;
682-
function = module->import_funcs[export->index].func_ptr_linked;
683-
}
684-
else {
685-
target_function_type =
686-
(AOTFuncType *)module
687-
->types[module->func_type_indexes[export->index
688-
- module->import_func_count]];
689-
function =
690-
(module->func_ptrs[export->index - module->import_func_count]);
691-
}
692-
/* check function type */
693-
if (!wasm_type_equal((WASMType *)expected_function_type,
694-
(WASMType *)target_function_type, module->types,
695-
module->type_count)) {
696-
LOG_DEBUG("%s.%s failed the type check", module->name, function_name);
697-
set_error_buf(error_buf, error_buf_size, "incompatible import type");
698-
return NULL;
699-
}
700-
return function;
701-
}
702-
#endif /* end of WASM_ENABLE_MULTI_MODULE */
703-
704637
static bool
705638
load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
706639
AOTModule *module, bool is_load_from_file_buf,
@@ -2285,19 +2218,13 @@ destroy_import_funcs(AOTImportFunc *import_funcs)
22852218

22862219
static bool
22872220
load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
2288-
bool is_load_from_file_buf, char *error_buf,
2221+
bool is_load_from_file_buf, bool no_resolve, char *error_buf,
22892222
uint32 error_buf_size)
22902223
{
2291-
char *module_name, *field_name;
22922224
const uint8 *buf = *p_buf;
22932225
AOTImportFunc *import_funcs;
22942226
uint64 size;
22952227
uint32 i;
2296-
#if WASM_ENABLE_MULTI_MODULE != 0
2297-
AOTModule *sub_module = NULL;
2298-
AOTFunc *linked_func = NULL;
2299-
AOTFuncType *declare_func_type = NULL;
2300-
#endif
23012228

23022229
/* Allocate memory */
23032230
size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
@@ -2314,53 +2241,17 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
23142241
return false;
23152242
}
23162243

2317-
#if WASM_ENABLE_MULTI_MODULE != 0
2318-
declare_func_type =
2319-
(AOTFuncType *)module->types[import_funcs[i].func_type_index];
2320-
read_string(buf, buf_end, module_name);
2321-
read_string(buf, buf_end, field_name);
2322-
2323-
import_funcs[i].module_name = module_name;
2324-
import_funcs[i].func_name = field_name;
2325-
linked_func = wasm_native_resolve_symbol(
2326-
module_name, field_name, declare_func_type,
2327-
&import_funcs[i].signature, &import_funcs[i].attachment,
2328-
&import_funcs[i].call_conv_raw);
2329-
if (!linked_func) {
2330-
sub_module = NULL;
2331-
if (!wasm_runtime_is_built_in_module(module_name)) {
2332-
sub_module = (AOTModule *)wasm_runtime_load_depended_module(
2333-
(WASMModuleCommon *)module, module_name, error_buf,
2334-
error_buf_size);
2335-
if (!sub_module) {
2336-
LOG_ERROR("failed to load sub module: %s", error_buf);
2337-
return false;
2338-
}
2339-
}
2340-
if (!sub_module)
2341-
linked_func = aot_loader_resolve_function_ex(
2342-
module_name, field_name, declare_func_type, error_buf,
2343-
error_buf_size);
2344-
else
2345-
linked_func = aot_loader_resolve_function(
2346-
sub_module, field_name, declare_func_type, error_buf,
2347-
error_buf_size);
2348-
}
2349-
import_funcs[i].func_ptr_linked = linked_func;
2350-
import_funcs[i].func_type = declare_func_type;
2351-
2352-
#else
23532244
import_funcs[i].func_type =
23542245
(AOTFuncType *)module->types[import_funcs[i].func_type_index];
23552246
read_string(buf, buf_end, import_funcs[i].module_name);
23562247
read_string(buf, buf_end, import_funcs[i].func_name);
2357-
module_name = import_funcs[i].module_name;
2358-
field_name = import_funcs[i].func_name;
2359-
import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol(
2360-
module_name, field_name, import_funcs[i].func_type,
2361-
&import_funcs[i].signature, &import_funcs[i].attachment,
2362-
&import_funcs[i].call_conv_raw);
2363-
#endif
2248+
import_funcs[i].attachment = NULL;
2249+
import_funcs[i].signature = NULL;
2250+
import_funcs[i].call_conv_raw = false;
2251+
2252+
if (!no_resolve) {
2253+
aot_resolve_import_func(module, &import_funcs[i]);
2254+
}
23642255

23652256
#if WASM_ENABLE_LIBC_WASI != 0
23662257
if (!strcmp(import_funcs[i].module_name, "wasi_unstable")
@@ -2378,7 +2269,7 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
23782269
static bool
23792270
load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
23802271
AOTModule *module, bool is_load_from_file_buf,
2381-
char *error_buf, uint32 error_buf_size)
2272+
bool no_resolve, char *error_buf, uint32 error_buf_size)
23822273
{
23832274
const uint8 *buf = *p_buf;
23842275

@@ -2387,7 +2278,7 @@ load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
23872278
/* load import funcs */
23882279
if (module->import_func_count > 0
23892280
&& !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf,
2390-
error_buf, error_buf_size))
2281+
no_resolve, error_buf, error_buf_size))
23912282
return false;
23922283

23932284
*p_buf = buf;
@@ -2514,7 +2405,7 @@ load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
25142405
static bool
25152406
load_init_data_section(const uint8 *buf, const uint8 *buf_end,
25162407
AOTModule *module, bool is_load_from_file_buf,
2517-
char *error_buf, uint32 error_buf_size)
2408+
bool no_resolve, char *error_buf, uint32 error_buf_size)
25182409
{
25192410
const uint8 *p = buf, *p_end = buf_end;
25202411

@@ -2525,7 +2416,7 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end,
25252416
error_buf, error_buf_size)
25262417
|| !load_global_info(&p, p_end, module, error_buf, error_buf_size)
25272418
|| !load_import_func_info(&p, p_end, module, is_load_from_file_buf,
2528-
error_buf, error_buf_size))
2419+
no_resolve, error_buf, error_buf_size))
25292420
return false;
25302421

25312422
/* load function count and start function index */
@@ -3819,7 +3710,7 @@ has_module_memory64(AOTModule *module)
38193710

38203711
static bool
38213712
load_from_sections(AOTModule *module, AOTSection *sections,
3822-
bool is_load_from_file_buf, char *error_buf,
3713+
bool is_load_from_file_buf, bool no_resolve, char *error_buf,
38233714
uint32 error_buf_size)
38243715
{
38253716
AOTSection *section = sections;
@@ -3852,8 +3743,8 @@ load_from_sections(AOTModule *module, AOTSection *sections,
38523743
break;
38533744
case AOT_SECTION_TYPE_INIT_DATA:
38543745
if (!load_init_data_section(buf, buf_end, module,
3855-
is_load_from_file_buf, error_buf,
3856-
error_buf_size))
3746+
is_load_from_file_buf, no_resolve,
3747+
error_buf, error_buf_size))
38573748
return false;
38583749
break;
38593750
case AOT_SECTION_TYPE_TEXT:
@@ -4076,7 +3967,7 @@ aot_load_from_sections(AOTSection *section_list, char *error_buf,
40763967
if (!module)
40773968
return NULL;
40783969

4079-
if (!load_from_sections(module, section_list, false, error_buf,
3970+
if (!load_from_sections(module, section_list, false, false, error_buf,
40803971
error_buf_size)) {
40813972
aot_unload(module);
40823973
return NULL;
@@ -4246,7 +4137,8 @@ create_sections(AOTModule *module, const uint8 *buf, uint32 size,
42464137

42474138
static bool
42484139
load(const uint8 *buf, uint32 size, AOTModule *module,
4249-
bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
4140+
bool wasm_binary_freeable, bool no_resolve, char *error_buf,
4141+
uint32 error_buf_size)
42504142
{
42514143
const uint8 *buf_end = buf + size;
42524144
const uint8 *p = buf, *p_end = buf_end;
@@ -4273,7 +4165,7 @@ load(const uint8 *buf, uint32 size, AOTModule *module,
42734165
return false;
42744166

42754167
ret = load_from_sections(module, section_list, !wasm_binary_freeable,
4276-
error_buf, error_buf_size);
4168+
no_resolve, error_buf, error_buf_size);
42774169
if (!ret) {
42784170
/* If load_from_sections() fails, then aot text is destroyed
42794171
in destroy_sections() */
@@ -4321,8 +4213,8 @@ aot_load_from_aot_file(const uint8 *buf, uint32 size, const LoadArgs *args,
43214213
return NULL;
43224214

43234215
os_thread_jit_write_protect_np(false); /* Make memory writable */
4324-
if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
4325-
error_buf_size)) {
4216+
if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
4217+
error_buf, error_buf_size)) {
43264218
aot_unload(module);
43274219
return NULL;
43284220
}

core/iwasm/aot/aot_runtime.c

+122
Original file line numberDiff line numberDiff line change
@@ -5111,3 +5111,125 @@ aot_get_module_name(AOTModule *module)
51115111
{
51125112
return module->name;
51135113
}
5114+
5115+
bool
5116+
aot_resolve_symbols(AOTModule *module)
5117+
{
5118+
bool ret = true;
5119+
uint32 idx;
5120+
for (idx = 0; idx < module->import_func_count; ++idx) {
5121+
AOTImportFunc *aot_import_func = &module->import_funcs[idx];
5122+
if (!aot_import_func->func_ptr_linked) {
5123+
if (!aot_resolve_import_func(module, aot_import_func)) {
5124+
LOG_WARNING("Failed to link function (%s, %s)",
5125+
aot_import_func->module_name,
5126+
aot_import_func->func_name);
5127+
ret = false;
5128+
}
5129+
}
5130+
}
5131+
return ret;
5132+
}
5133+
5134+
#if WASM_ENABLE_MULTI_MODULE != 0
5135+
static void *
5136+
aot_resolve_function(const AOTModule *module, const char *function_name,
5137+
const AOTFuncType *expected_function_type, char *error_buf,
5138+
uint32 error_buf_size);
5139+
5140+
static void *
5141+
aot_resolve_function_ex(const char *module_name, const char *function_name,
5142+
const AOTFuncType *expected_function_type,
5143+
char *error_buf, uint32 error_buf_size)
5144+
{
5145+
WASMModuleCommon *module_reg;
5146+
5147+
module_reg = wasm_runtime_find_module_registered(module_name);
5148+
if (!module_reg || module_reg->module_type != Wasm_Module_AoT) {
5149+
LOG_DEBUG("can not find a module named %s for function %s", module_name,
5150+
function_name);
5151+
set_error_buf(error_buf, error_buf_size, "unknown import");
5152+
return NULL;
5153+
}
5154+
return aot_resolve_function((AOTModule *)module_reg, function_name,
5155+
expected_function_type, error_buf,
5156+
error_buf_size);
5157+
}
5158+
5159+
static void *
5160+
aot_resolve_function(const AOTModule *module, const char *function_name,
5161+
const AOTFuncType *expected_function_type, char *error_buf,
5162+
uint32 error_buf_size)
5163+
{
5164+
void *function = NULL;
5165+
AOTExport *export = NULL;
5166+
AOTFuncType *target_function_type = NULL;
5167+
5168+
export = loader_find_export((WASMModuleCommon *)module, module->name,
5169+
function_name, EXPORT_KIND_FUNC, error_buf,
5170+
error_buf_size);
5171+
if (!export) {
5172+
return NULL;
5173+
}
5174+
5175+
/* resolve function type and function */
5176+
if (export->index < module->import_func_count) {
5177+
target_function_type = module->import_funcs[export->index].func_type;
5178+
function = module->import_funcs[export->index].func_ptr_linked;
5179+
}
5180+
else {
5181+
target_function_type =
5182+
(AOTFuncType *)module
5183+
->types[module->func_type_indexes[export->index
5184+
- module->import_func_count]];
5185+
function =
5186+
(module->func_ptrs[export->index - module->import_func_count]);
5187+
}
5188+
/* check function type */
5189+
if (!wasm_type_equal((WASMType *)expected_function_type,
5190+
(WASMType *)target_function_type, module->types,
5191+
module->type_count)) {
5192+
LOG_DEBUG("%s.%s failed the type check", module->name, function_name);
5193+
set_error_buf(error_buf, error_buf_size, "incompatible import type");
5194+
return NULL;
5195+
}
5196+
return function;
5197+
}
5198+
#endif /* end of WASM_ENABLE_MULTI_MODULE */
5199+
5200+
bool
5201+
aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func)
5202+
{
5203+
#if WASM_ENABLE_MULTI_MODULE != 0
5204+
char error_buf[128];
5205+
AOTModule *sub_module = NULL;
5206+
#endif
5207+
import_func->func_ptr_linked = wasm_native_resolve_symbol(
5208+
import_func->module_name, import_func->func_name,
5209+
import_func->func_type, &import_func->signature,
5210+
&import_func->attachment, &import_func->call_conv_raw);
5211+
#if WASM_ENABLE_MULTI_MODULE != 0
5212+
if (!import_func->func_ptr_linked) {
5213+
if (!wasm_runtime_is_built_in_module(import_func->module_name)) {
5214+
sub_module = (AOTModule *)wasm_runtime_load_depended_module(
5215+
(WASMModuleCommon *)module, import_func->module_name, error_buf,
5216+
sizeof(error_buf));
5217+
if (!sub_module) {
5218+
LOG_WARNING("Failed to load sub module: %s", error_buf);
5219+
}
5220+
if (!sub_module)
5221+
import_func->func_ptr_linked = aot_resolve_function_ex(
5222+
import_func->module_name, import_func->func_name,
5223+
import_func->func_type, error_buf, sizeof(error_buf));
5224+
else
5225+
import_func->func_ptr_linked = aot_resolve_function(
5226+
sub_module, import_func->func_name, import_func->func_type,
5227+
error_buf, sizeof(error_buf));
5228+
if (!import_func->func_ptr_linked) {
5229+
LOG_WARNING("Failed to link function: %s", error_buf);
5230+
}
5231+
}
5232+
}
5233+
#endif
5234+
return import_func->func_ptr_linked != NULL;
5235+
}

core/iwasm/aot/aot_runtime.h

+12
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,18 @@ aot_load_from_sections(AOTSection *section_list, char *error_buf,
492492
void
493493
aot_unload(AOTModule *module);
494494

495+
/**
496+
* Resolve symbols for an AOT module
497+
*/
498+
bool
499+
aot_resolve_symbols(AOTModule *module);
500+
501+
/**
502+
* Helper function to resolve a single function
503+
*/
504+
bool
505+
aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func);
506+
495507
/**
496508
* Instantiate a AOT module.
497509
*

0 commit comments

Comments
 (0)