Skip to content

Commit a653746

Browse files
Check whether related table has funcref elem in opcode call_indirect (#3999)
* check whether table has funcref elem in call_indirect * check whether table has funcref elem in call_indirect when gc is enabled
1 parent 9989b1c commit a653746

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

core/iwasm/interpreter/wasm_loader.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12103,6 +12103,10 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1210312103
{
1210412104
int32 idx;
1210512105
WASMFuncType *func_type;
12106+
uint32 tbl_elem_type;
12107+
#if WASM_ENABLE_GC != 0
12108+
WASMRefType *elem_ref_type = NULL;
12109+
#endif
1210612110

1210712111
read_leb_uint32(p, p_end, type_idx);
1210812112
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
@@ -12125,6 +12129,43 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1212512129
error_buf_size)) {
1212612130
goto fail;
1212712131
}
12132+
tbl_elem_type =
12133+
table_idx < module->import_table_count
12134+
? module->import_tables[table_idx]
12135+
.u.table.table_type.elem_type
12136+
: module->tables[table_idx - module->import_table_count]
12137+
.table_type.elem_type;
12138+
12139+
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
12140+
if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
12141+
set_error_buf_v(error_buf, error_buf_size,
12142+
"type mismatch: instruction requires table "
12143+
"of functions but table %u has externref",
12144+
table_idx);
12145+
goto fail;
12146+
}
12147+
#elif WASM_ENABLE_GC != 0
12148+
/* Table element must match type ref null func */
12149+
elem_ref_type =
12150+
table_idx < module->import_table_count
12151+
? module->import_tables[table_idx]
12152+
.u.table.table_type.elem_ref_type
12153+
: module->tables[table_idx - module->import_table_count]
12154+
.table_type.elem_ref_type;
12155+
12156+
if (!wasm_reftype_is_subtype_of(
12157+
tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
12158+
module->types, module->type_count)) {
12159+
set_error_buf_v(error_buf, error_buf_size,
12160+
"type mismatch: instruction requires "
12161+
"reference type t match type ref null func"
12162+
"in table %u",
12163+
table_idx);
12164+
goto fail;
12165+
}
12166+
#else
12167+
(void)tbl_elem_type;
12168+
#endif
1212812169

1212912170
#if WASM_ENABLE_FAST_INTERP != 0
1213012171
/* we need to emit before arguments */

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6700,6 +6700,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
67006700
goto fail;
67016701
}
67026702

6703+
bh_assert(
6704+
(table_idx < module->import_table_count
6705+
? module->import_tables[table_idx]
6706+
.u.table.table_type.elem_type
6707+
: module
6708+
->tables[table_idx - module->import_table_count]
6709+
.table_type.elem_type)
6710+
== VALUE_TYPE_FUNCREF);
6711+
67036712
#if WASM_ENABLE_FAST_INTERP != 0
67046713
/* we need to emit before arguments */
67056714
emit_uint32(loader_ctx, type_idx);

0 commit comments

Comments
 (0)