Skip to content

Commit f7d2826

Browse files
authored
Allow missing imports in wasm loader and report error in wasm instantiation instead (#3539)
The wasm loader is failing when multi-module support is on and the dependent modules are not found; this enforces the AOT compiler integrations to prepare dependent modules while it isn't necessary. This PR allows allows missing imports in wasm loader and report error in wasm instantiation instead, which enables the integrated AOT compiler to work as if the multi-module support isn't turned on.
1 parent 54b87cb commit f7d2826

File tree

9 files changed

+241
-183
lines changed

9 files changed

+241
-183
lines changed

build-scripts/config_common.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ if (WAMR_BUILD_SPEC_TEST EQUAL 1)
233233
add_definitions (-DWASM_ENABLE_SPEC_TEST=1)
234234
message (" spec test compatible mode is on")
235235
endif ()
236+
if (WAMR_BUILD_WASI_TEST EQUAL 1)
237+
add_definitions (-DWASM_ENABLE_WASI_TEST=1)
238+
message (" wasi test compatible mode is on")
239+
endif ()
236240
if (NOT DEFINED WAMR_BUILD_BULK_MEMORY)
237241
# Enable bulk memory by default
238242
set (WAMR_BUILD_BULK_MEMORY 1)

core/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@
363363
#define WASM_ENABLE_SPEC_TEST 0
364364
#endif
365365

366+
#ifndef WASM_ENABLE_WASI_TEST
367+
#define WASM_ENABLE_WASI_TEST 0
368+
#endif
369+
366370
/* Global heap pool size in bytes */
367371
#ifndef WASM_GLOBAL_HEAP_SIZE
368372
#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024)

core/iwasm/common/wasm_runtime_common.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,9 @@ wasm_runtime_is_built_in_module(const char *module_name)
12671267
|| !strcmp("wasi_snapshot_preview1", module_name)
12681268
#if WASM_ENABLE_SPEC_TEST != 0
12691269
|| !strcmp("spectest", module_name)
1270+
#endif
1271+
#if WASM_ENABLE_WASI_TEST != 0
1272+
|| !strcmp("foo", module_name)
12701273
#endif
12711274
|| !strcmp("", module_name));
12721275
}

core/iwasm/interpreter/wasm_loader.c

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,6 +2533,7 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end,
25332533
WASMFunction *linked_func = NULL;
25342534
#if WASM_ENABLE_MULTI_MODULE != 0
25352535
WASMModule *sub_module = NULL;
2536+
bool is_built_in_module = false;
25362537
#endif
25372538
const char *linked_signature = NULL;
25382539
void *linked_attachment = NULL;
@@ -2568,17 +2569,16 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end,
25682569
}
25692570
#if WASM_ENABLE_MULTI_MODULE != 0
25702571
else {
2571-
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
2572+
if (!(is_built_in_module =
2573+
wasm_runtime_is_built_in_module(sub_module_name))) {
25722574
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
25732575
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
25742576
error_buf_size);
2575-
if (!sub_module) {
2576-
return false;
2577-
}
25782577
}
2579-
linked_func = wasm_loader_resolve_function(
2580-
sub_module_name, function_name, declare_func_type, error_buf,
2581-
error_buf_size);
2578+
if (is_built_in_module || sub_module)
2579+
linked_func = wasm_loader_resolve_function(
2580+
sub_module_name, function_name, declare_func_type, error_buf,
2581+
error_buf_size);
25822582
}
25832583
#endif
25842584

@@ -2691,24 +2691,20 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
26912691
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
26922692
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
26932693
error_buf_size);
2694-
if (!sub_module) {
2695-
return false;
2696-
}
2697-
2698-
linked_table = wasm_loader_resolve_table(
2699-
sub_module_name, table_name, declare_init_size, declare_max_size,
2700-
error_buf, error_buf_size);
2701-
if (!linked_table) {
2702-
return false;
2694+
if (sub_module) {
2695+
linked_table = wasm_loader_resolve_table(
2696+
sub_module_name, table_name, declare_init_size,
2697+
declare_max_size, error_buf, error_buf_size);
2698+
if (linked_table) {
2699+
/* reset with linked table limit */
2700+
declare_elem_type = linked_table->table_type.elem_type;
2701+
declare_init_size = linked_table->table_type.init_size;
2702+
declare_max_size = linked_table->table_type.max_size;
2703+
declare_max_size_flag = linked_table->table_type.flags;
2704+
table->import_table_linked = linked_table;
2705+
table->import_module = sub_module;
2706+
}
27032707
}
2704-
2705-
/* reset with linked table limit */
2706-
declare_elem_type = linked_table->table_type.elem_type;
2707-
declare_init_size = linked_table->table_type.init_size;
2708-
declare_max_size = linked_table->table_type.max_size;
2709-
declare_max_size_flag = linked_table->table_type.flags;
2710-
table->import_table_linked = linked_table;
2711-
table->import_module = sub_module;
27122708
}
27132709
#endif /* WASM_ENABLE_MULTI_MODULE != 0 */
27142710

@@ -2870,31 +2866,19 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
28702866
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
28712867
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
28722868
error_buf_size);
2873-
if (!sub_module) {
2874-
#if WASM_ENABLE_LIB_WASI_THREADS != 0
2875-
/* Avoid memory import failure when wasi-threads is enabled
2876-
and the memory is shared */
2877-
if (!(mem_flag & SHARED_MEMORY_FLAG))
2878-
return false;
2879-
#else
2880-
return false;
2881-
#endif /* WASM_ENABLE_LIB_WASI_THREADS */
2882-
}
2883-
else {
2869+
if (sub_module) {
28842870
linked_memory = wasm_loader_resolve_memory(
28852871
sub_module_name, memory_name, declare_init_page_count,
28862872
declare_max_page_count, error_buf, error_buf_size);
2887-
if (!linked_memory) {
2888-
return false;
2873+
if (linked_memory) {
2874+
/**
2875+
* reset with linked memory limit
2876+
*/
2877+
memory->import_module = sub_module;
2878+
memory->import_memory_linked = linked_memory;
2879+
declare_init_page_count = linked_memory->init_page_count;
2880+
declare_max_page_count = linked_memory->max_page_count;
28892881
}
2890-
2891-
/**
2892-
* reset with linked memory limit
2893-
*/
2894-
memory->import_module = sub_module;
2895-
memory->import_memory_linked = linked_memory;
2896-
declare_init_page_count = linked_memory->init_page_count;
2897-
declare_max_page_count = linked_memory->max_page_count;
28982882
}
28992883
}
29002884
#endif
@@ -2920,6 +2904,29 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
29202904
declare_init_page_count = spectest_memory_init_page;
29212905
declare_max_page_count = spectest_memory_max_page;
29222906
}
2907+
#if WASM_ENABLE_WASI_TEST != 0
2908+
/* a case in wasi-testsuite which imports ("foo" "bar") */
2909+
else if (!strcmp("foo", sub_module_name)) {
2910+
uint32 spectest_memory_init_page = 1;
2911+
uint32 spectest_memory_max_page = 1;
2912+
2913+
if (strcmp("bar", memory_name)) {
2914+
set_error_buf(error_buf, error_buf_size,
2915+
"incompatible import type or unknown import");
2916+
return false;
2917+
}
2918+
2919+
if (declare_init_page_count > spectest_memory_init_page
2920+
|| declare_max_page_count < spectest_memory_max_page) {
2921+
set_error_buf(error_buf, error_buf_size,
2922+
"incompatible import type");
2923+
return false;
2924+
}
2925+
2926+
declare_init_page_count = spectest_memory_init_page;
2927+
declare_max_page_count = spectest_memory_max_page;
2928+
}
2929+
#endif
29232930

29242931
/* now we believe all declaration are ok */
29252932
memory->mem_type.flags = mem_flag;
@@ -2983,20 +2990,19 @@ load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
29832990
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
29842991
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
29852992
error_buf_size);
2986-
if (!sub_module) {
2987-
return false;
2988-
}
2989-
/* wasm_loader_resolve_tag checks, that the imported tag
2990-
* and the declared tag have the same type
2991-
*/
2992-
uint32 linked_tag_index = 0;
2993-
WASMTag *linked_tag = wasm_loader_resolve_tag(
2994-
sub_module_name, tag_name, declare_tag_type,
2995-
&linked_tag_index /* out */, error_buf, error_buf_size);
2996-
if (linked_tag) {
2997-
tag->import_module = sub_module;
2998-
tag->import_tag_linked = linked_tag;
2999-
tag->import_tag_index_linked = linked_tag_index;
2993+
if (sub_module) {
2994+
/* wasm_loader_resolve_tag checks, that the imported tag
2995+
* and the declared tag have the same type
2996+
*/
2997+
uint32 linked_tag_index = 0;
2998+
WASMTag *linked_tag = wasm_loader_resolve_tag(
2999+
sub_module_name, tag_name, declare_tag_type,
3000+
&linked_tag_index /* out */, error_buf, error_buf_size);
3001+
if (linked_tag) {
3002+
tag->import_module = sub_module;
3003+
tag->import_tag_linked = linked_tag;
3004+
tag->import_tag_index_linked = linked_tag_index;
3005+
}
30003006
}
30013007
}
30023008
#endif
@@ -3095,18 +3101,16 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
30953101
sub_module = (WASMModule *)wasm_runtime_load_depended_module(
30963102
(WASMModuleCommon *)parent_module, sub_module_name, error_buf,
30973103
error_buf_size);
3098-
if (!sub_module) {
3099-
return false;
3100-
}
3101-
3102-
/* check sub modules */
3103-
linked_global = wasm_loader_resolve_global(
3104-
sub_module_name, global_name, declare_type, declare_mutable,
3105-
error_buf, error_buf_size);
3106-
if (linked_global) {
3107-
global->import_module = sub_module;
3108-
global->import_global_linked = linked_global;
3109-
global->is_linked = true;
3104+
if (sub_module) {
3105+
/* check sub modules */
3106+
linked_global = wasm_loader_resolve_global(
3107+
sub_module_name, global_name, declare_type, declare_mutable,
3108+
error_buf, error_buf_size);
3109+
if (linked_global) {
3110+
global->import_module = sub_module;
3111+
global->import_global_linked = linked_global;
3112+
global->is_linked = true;
3113+
}
31103114
}
31113115
}
31123116
#endif

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,36 +1710,73 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
17101710
&& !func->import_func_linked
17111711
#endif
17121712
) {
1713-
#if WASM_ENABLE_WAMR_COMPILER == 0
17141713
LOG_WARNING("warning: failed to link import function (%s, %s)",
17151714
func->module_name, func->field_name);
1716-
/* will throw exception only if calling */
1717-
#else
1718-
/* do nothing to avoid confused message */
1719-
#endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
17201715
}
17211716
}
17221717

17231718
for (i = 0; i < module->import_global_count; i++) {
17241719
WASMGlobalImport *global = &((module->import_globals + i)->u.global);
1720+
17251721
if (!global->is_linked) {
17261722
#if WASM_ENABLE_SPEC_TEST != 0
17271723
set_error_buf(error_buf, error_buf_size,
17281724
"unknown import or incompatible import type");
17291725
return false;
17301726
#else
1731-
#if WASM_ENABLE_WAMR_COMPILER == 0
17321727
set_error_buf_v(error_buf, error_buf_size,
17331728
"failed to link import global (%s, %s)",
17341729
global->module_name, global->field_name);
17351730
return false;
1736-
#else
1737-
/* do nothing to avoid confused message */
1738-
#endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
17391731
#endif /* WASM_ENABLE_SPEC_TEST != 0 */
17401732
}
17411733
}
17421734

1735+
for (i = 0; i < module->import_table_count; i++) {
1736+
WASMTableImport *table = &((module->import_tables + i)->u.table);
1737+
1738+
if (!wasm_runtime_is_built_in_module(table->module_name)
1739+
#if WASM_ENABLE_MULTI_MODULE != 0
1740+
&& !table->import_table_linked
1741+
#endif
1742+
) {
1743+
set_error_buf_v(error_buf, error_buf_size,
1744+
"failed to link import table (%s, %s)",
1745+
table->module_name, table->field_name);
1746+
return false;
1747+
}
1748+
}
1749+
1750+
for (i = 0; i < module->import_memory_count; i++) {
1751+
WASMMemoryImport *memory = &((module->import_memories + i)->u.memory);
1752+
1753+
if (!wasm_runtime_is_built_in_module(memory->module_name)
1754+
#if WASM_ENABLE_MULTI_MODULE != 0
1755+
&& !memory->import_memory_linked
1756+
#endif
1757+
) {
1758+
set_error_buf_v(error_buf, error_buf_size,
1759+
"failed to link import memory (%s, %s)",
1760+
memory->module_name, memory->field_name);
1761+
return false;
1762+
}
1763+
}
1764+
1765+
#if WASM_ENABLE_MULTI_MODULE != 0
1766+
#if WASM_ENABLE_TAGS != 0
1767+
for (i = 0; i < module->import_tag_count; i++) {
1768+
WASMTagImport *tag = &((module->import_tags + i)->u.tag);
1769+
1770+
if (!tag->import_tag_linked) {
1771+
set_error_buf_v(error_buf, error_buf_size,
1772+
"failed to link import tag (%s, %s)",
1773+
tag->module_name, tag->field_name);
1774+
return false;
1775+
}
1776+
}
1777+
#endif /* WASM_ENABLE_TAGS != 0 */
1778+
#endif
1779+
17431780
return true;
17441781
}
17451782

0 commit comments

Comments
 (0)