From 94c803dd173870dc9fd76306ae12d22d3197cfea Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 16 Aug 2024 13:01:05 +0800 Subject: [PATCH 1/2] Fix table idx handling in opcode call_indirect --- core/iwasm/compilation/aot_llvm.c | 10 ++++++++++ core/iwasm/interpreter/wasm_interp_classic.c | 7 +++++++ core/iwasm/interpreter/wasm_loader.c | 6 +++--- core/iwasm/interpreter/wasm_mini_loader.c | 5 ++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 8c989ed6e9..2e1e82e564 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -3108,6 +3108,16 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) goto fail; } + /* Return error if ref-types and GC are disabled by command line but + ref-types instructions are used */ + if (!option->enable_ref_types && !option->enable_gc + && wasm_module->is_ref_types_used) { + aot_set_last_error("ref-types instruction was found, " + "try removing --disable-ref-types option " + "or adding --enable-gc option."); + goto fail; + } + /* Disable features when they are not actually used */ if (!wasm_module->is_simd_used) { option->enable_simd = comp_ctx->enable_simd = false; diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index f2d6b7e7bd..766d2f52f0 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -2281,8 +2281,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, bh_assert(tidx < module->module->type_count); cur_type = wasm_types[tidx]; + /* clang-format off */ +#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); +#else + frame_ip++; + tbl_idx = 0; +#endif bh_assert(tbl_idx < module->table_count); + /* clang-format on */ tbl_inst = wasm_get_table_inst(module, tbl_idx); diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index d2bcf71763..14086cb746 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -7149,10 +7149,10 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache, case WASM_OP_RETURN_CALL_INDIRECT: #endif skip_leb_uint32(p, p_end); /* typeidx */ -#if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0 - u8 = read_uint8(p); /* 0x00 */ -#else +#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 skip_leb_uint32(p, p_end); /* tableidx */ +#else + u8 = read_uint8(p); /* 0x00 */ #endif break; diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 6e70203fa5..0bb2f34ebc 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -3501,8 +3501,11 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache, case WASM_OP_RETURN_CALL_INDIRECT: #endif skip_leb_uint32(p, p_end); /* typeidx */ - CHECK_BUF(p, p_end, 1); +#if WASM_ENABLE_REF_TYPES != 0 + skip_leb_uint32(p, p_end); /* tableidx */ +#else u8 = read_uint8(p); /* 0x00 */ +#endif break; #if WASM_ENABLE_EXCE_HANDLING != 0 From 7a7d4310340a8d5c38dbb34c214e5cd43dce3fbd Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 16 Aug 2024 13:12:02 +0800 Subject: [PATCH 2/2] check boundary of p, fix coding style --- core/iwasm/interpreter/wasm_loader.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 14086cb746..0891598d14 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -12005,10 +12005,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, read_leb_uint32(p, p_end, type_idx); #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 #if WASM_ENABLE_WAMR_COMPILER != 0 - if (*p != 0x00) { - // Any non-0x00 byte requires the ref types proposal. - // This is different from checking the table_idx value - // since `0x80 0x00` etc. are all valid encodings of zero. + if (p + 1 < p_end && *p != 0x00) { + /* + * Any non-0x00 byte requires the ref types proposal. + * This is different from checking the table_idx value + * since `0x80 0x00` etc. are all valid encodings of zero. + */ module->is_ref_types_used = true; } #endif