From 70af781bded14ef79f42a1626017b933adfeb4c5 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 17 Mar 2025 22:47:19 +0100 Subject: [PATCH] fix: improve instruction limit checks and enhance readability of HANDLE_OP macros --- core/iwasm/interpreter/wasm_interp_classic.c | 22 +++++++++++++------- core/iwasm/interpreter/wasm_interp_fast.c | 13 +++++++----- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 96601c585..94fa72c0c 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -1569,11 +1569,13 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global) } #if WASM_INSTRUCTION_METERING != 0 -#define CHECK_INSTRUCTION_LIMIT() \ - if (instructions_left == 0) { \ - goto return_func; \ - } \ - instructions_left--; +#define CHECK_INSTRUCTION_LIMIT() \ + if (instructions_left == 0) { \ + wasm_set_exception(module, "instruction limit exceeded"); \ + goto got_exception; \ + } \ + else if (instructions_left > 0) \ + instructions_left--; \ #else #define CHECK_INSTRUCTION_LIMIT() (void)0 #endif @@ -1721,7 +1723,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, goto got_exception; } - HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); } + HANDLE_OP(WASM_OP_NOP) + { + HANDLE_OP_END(); + } #if WASM_ENABLE_EXCE_HANDLING != 0 HANDLE_OP(WASM_OP_RETHROW) @@ -5658,7 +5663,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_I32_REINTERPRET_F32) HANDLE_OP(WASM_OP_I64_REINTERPRET_F64) HANDLE_OP(WASM_OP_F32_REINTERPRET_I32) - HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); } + HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) + { + HANDLE_OP_END(); + } HANDLE_OP(WASM_OP_I32_EXTEND8_S) { diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 5a793d04b..676e4e74b 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -102,11 +102,14 @@ typedef float64 CellType_F64; } while (0) #if WASM_INSTRUCTION_METERING != 0 -#define CHECK_INSTRUCTION_LIMIT() \ - if (instructions_left == 0) { \ - goto return_func; \ - } \ - instructions_left--; +#define CHECK_INSTRUCTION_LIMIT() \ + if (instructions_left == 0) { \ + wasm_set_exception(module_inst, "instruction limit exceeded"); \ + goto got_exception; \ + } \ + else if (instructions_left > 0) { \ + instructions_left--; \ + } \ #else #define CHECK_INSTRUCTION_LIMIT() (void)0 #endif