Skip to content

Commit 7401718

Browse files
authored
Report error in instantiation when meeting unlinked import globals (#1859)
1 parent 0c44022 commit 7401718

5 files changed

Lines changed: 33 additions & 2 deletions

File tree

core/iwasm/aot/aot_loader.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,10 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
12261226
}
12271227
import_globals[i].global_data_linked =
12281228
tmp_global.global_data_linked;
1229+
import_globals[i].is_linked = true;
12291230
}
1231+
#else
1232+
import_globals[i].is_linked = false;
12301233
#endif
12311234

12321235
import_globals[i].size = wasm_value_type_size(import_globals[i].type);

core/iwasm/aot/aot_runtime.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,26 @@ execute_memory_init_function(AOTModuleInstance *module_inst)
976976
}
977977
#endif
978978

979+
static bool
980+
check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size)
981+
{
982+
uint32 i;
983+
984+
/* init_func_ptrs() will go through import functions */
985+
986+
for (i = 0; i < module->import_global_count; i++) {
987+
AOTImportGlobal *global = module->import_globals + i;
988+
if (!global->is_linked) {
989+
set_error_buf_v(error_buf, error_buf_size,
990+
"warning: failed to link import global (%s, %s)",
991+
global->module_name, global->global_name);
992+
return false;
993+
}
994+
}
995+
996+
return true;
997+
}
998+
979999
AOTModuleInstance *
9801000
aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
9811001
uint32 heap_size, char *error_buf, uint32 error_buf_size)
@@ -1059,6 +1079,9 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
10591079
if (!init_func_type_indexes(module_inst, module, error_buf, error_buf_size))
10601080
goto fail;
10611081

1082+
if (!check_linked_symbol(module, error_buf, error_buf_size))
1083+
goto fail;
1084+
10621085
if (!create_exports(module_inst, module, error_buf, error_buf_size))
10631086
goto fail;
10641087

core/iwasm/common/wasm_c_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,6 +4555,7 @@ aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
45554555
}
45564556

45574557
import->global_idx_rt = global_idx_rt;
4558+
import_aot_global->is_linked = true;
45584559
return true;
45594560

45604561
failed:

core/iwasm/compilation/aot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef struct AOTImportGlobal {
149149
uint32 data_offset;
150150
/* global data after linked */
151151
WASMValue global_data_linked;
152+
bool is_linked;
152153
} AOTImportGlobal;
153154

154155
/**

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,7 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
12351235
#if WASM_ENABLE_WAMR_COMPILER == 0
12361236
LOG_WARNING("warning: failed to link import function (%s, %s)",
12371237
func->module_name, func->field_name);
1238+
/* will throw exception only if calling */
12381239
#else
12391240
/* do nothing to avoid confused message */
12401241
#endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
@@ -1250,8 +1251,10 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
12501251
return false;
12511252
#else
12521253
#if WASM_ENABLE_WAMR_COMPILER == 0
1253-
LOG_DEBUG("warning: failed to link import global (%s, %s)",
1254-
global->module_name, global->field_name);
1254+
set_error_buf_v(error_buf, error_buf_size,
1255+
"warning: failed to link import global (%s, %s)",
1256+
global->module_name, global->field_name);
1257+
return false;
12551258
#else
12561259
/* do nothing to avoid confused message */
12571260
#endif /* WASM_ENABLE_WAMR_COMPILER == 0 */

0 commit comments

Comments
 (0)