Skip to content

Commit 97c95a2

Browse files
authored
Fix table idx resolving in op call_indirect/return_call_indirect (#3726)
The table index in the call_indirect/return_call_indirect opcode should be one byte 0x00 when ref-types/GC isn't enabled, and should be treated as leb u32 when ref-types/GC is enabled. And make aot compiler bail out if ref-types/GC is disabled by command line argument while ref-types instructions are used.
1 parent 88caa0c commit 97c95a2

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

core/iwasm/compilation/aot_llvm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,16 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
31083108
goto fail;
31093109
}
31103110

3111+
/* Return error if ref-types and GC are disabled by command line but
3112+
ref-types instructions are used */
3113+
if (!option->enable_ref_types && !option->enable_gc
3114+
&& wasm_module->is_ref_types_used) {
3115+
aot_set_last_error("ref-types instruction was found, "
3116+
"try removing --disable-ref-types option "
3117+
"or adding --enable-gc option.");
3118+
goto fail;
3119+
}
3120+
31113121
/* Disable features when they are not actually used */
31123122
if (!wasm_module->is_simd_used) {
31133123
option->enable_simd = comp_ctx->enable_simd = false;

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,8 +2281,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22812281
bh_assert(tidx < module->module->type_count);
22822282
cur_type = wasm_types[tidx];
22832283

2284+
/* clang-format off */
2285+
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
22842286
read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
2287+
#else
2288+
frame_ip++;
2289+
tbl_idx = 0;
2290+
#endif
22852291
bh_assert(tbl_idx < module->table_count);
2292+
/* clang-format on */
22862293

22872294
tbl_inst = wasm_get_table_inst(module, tbl_idx);
22882295

core/iwasm/interpreter/wasm_loader.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7149,10 +7149,10 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
71497149
case WASM_OP_RETURN_CALL_INDIRECT:
71507150
#endif
71517151
skip_leb_uint32(p, p_end); /* typeidx */
7152-
#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
7153-
u8 = read_uint8(p); /* 0x00 */
7154-
#else
7152+
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
71557153
skip_leb_uint32(p, p_end); /* tableidx */
7154+
#else
7155+
u8 = read_uint8(p); /* 0x00 */
71567156
#endif
71577157
break;
71587158

@@ -12005,10 +12005,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1200512005
read_leb_uint32(p, p_end, type_idx);
1200612006
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
1200712007
#if WASM_ENABLE_WAMR_COMPILER != 0
12008-
if (*p != 0x00) {
12009-
// Any non-0x00 byte requires the ref types proposal.
12010-
// This is different from checking the table_idx value
12011-
// since `0x80 0x00` etc. are all valid encodings of zero.
12008+
if (p + 1 < p_end && *p != 0x00) {
12009+
/*
12010+
* Any non-0x00 byte requires the ref types proposal.
12011+
* This is different from checking the table_idx value
12012+
* since `0x80 0x00` etc. are all valid encodings of zero.
12013+
*/
1201212014
module->is_ref_types_used = true;
1201312015
}
1201412016
#endif

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3501,8 +3501,11 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
35013501
case WASM_OP_RETURN_CALL_INDIRECT:
35023502
#endif
35033503
skip_leb_uint32(p, p_end); /* typeidx */
3504-
CHECK_BUF(p, p_end, 1);
3504+
#if WASM_ENABLE_REF_TYPES != 0
3505+
skip_leb_uint32(p, p_end); /* tableidx */
3506+
#else
35053507
u8 = read_uint8(p); /* 0x00 */
3508+
#endif
35063509
break;
35073510

35083511
#if WASM_ENABLE_EXCE_HANDLING != 0

0 commit comments

Comments
 (0)