Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ref.func forward-declared function check #2099

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -8222,12 +8222,13 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
goto fail;
}

if (func_idx == cur_func_idx + module->import_function_count) {
/* Refer to a forward-declared function */
if (func_idx >= cur_func_idx + module->import_function_count) {
WASMTableSeg *table_seg = module->table_segments;
bool func_declared = false;
uint32 j;

/* Check whether current function is declared */
/* Check whether the function is declared in table segs */
for (i = 0; i < module->table_seg_count; i++, table_seg++) {
if (table_seg->elem_type == VALUE_TYPE_FUNCREF
&& wasm_elem_is_declarative(table_seg->mode)) {
Expand All @@ -8239,6 +8240,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
}
}
}
if (!func_declared) {
/* Check whether the function is exported */
for (i = 0; i < module->export_count; i++) {
if (module->exports[i].kind == EXPORT_KIND_FUNC
&& module->exports[i].index == func_idx) {
func_declared = true;
break;
}
}
}

if (!func_declared) {
set_error_buf(error_buf, error_buf_size,
"undeclared function reference");
Expand Down
18 changes: 13 additions & 5 deletions core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -6384,12 +6384,13 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
goto fail;
}

if (func_idx == cur_func_idx + module->import_function_count) {
/* Refer to a forward-declared function */
if (func_idx >= cur_func_idx + module->import_function_count) {
WASMTableSeg *table_seg = module->table_segments;
bool func_declared = false;
uint32 j;

/* Check whether current function is declared */
/* Check whether the function is declared in table segs */
for (i = 0; i < module->table_seg_count; i++, table_seg++) {
if (table_seg->elem_type == VALUE_TYPE_FUNCREF
&& wasm_elem_is_declarative(table_seg->mode)) {
Expand All @@ -6402,10 +6403,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
}
}
if (!func_declared) {
set_error_buf(error_buf, error_buf_size,
"undeclared function reference");
goto fail;
/* Check whether the function is exported */
for (i = 0; i < module->export_count; i++) {
if (module->exports[i].kind == EXPORT_KIND_FUNC
&& module->exports[i].index == func_idx) {
func_declared = true;
break;
}
}
}
bh_assert(func_declared);
(void)func_declared;
}

#if WASM_ENABLE_FAST_INTERP != 0
Expand Down