From b8463822eb7a830caf3cdcbe620c16ff68ae12d9 Mon Sep 17 00:00:00 2001 From: TL Date: Mon, 24 Nov 2025 14:33:32 +0800 Subject: [PATCH] some refactor and add regression test case --- core/iwasm/common/wasm_runtime_common.c | 156 +++++++++--------- tests/regression/ba-issues/build_wamr.sh | 12 +- .../issues/issue-4703/double_page_memory.wasm | Bin 0 -> 132 bytes .../issues/issue-4703/single_page_memory.wasm | Bin 0 -> 88 bytes .../regression/ba-issues/running_config.json | 18 +- 5 files changed, 106 insertions(+), 80 deletions(-) create mode 100644 tests/regression/ba-issues/issues/issue-4703/double_page_memory.wasm create mode 100644 tests/regression/ba-issues/issues/issue-4703/single_page_memory.wasm diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 4fcfd231d..d75d45cf3 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -86,84 +86,6 @@ static bh_list registered_module_list_head; static bh_list *const registered_module_list = ®istered_module_list_head; static korp_mutex registered_module_list_lock; -void -wasm_runtime_propagate_exception_from_import( - WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *sub_module) -{ - static const uint32 exception_prefix_len = sizeof("Exception: ") - 1; - static const char memory_oob_exception[] = "out of bounds memory access"; - char exception_buf[EXCEPTION_BUF_LEN] = { 0 }; - const char *message = NULL; - bool has_exception = false; - - if (!parent || !sub_module) - return; - - switch (sub_module->module_type) { -#if WASM_ENABLE_INTERP != 0 - case Wasm_Module_Bytecode: - has_exception = wasm_copy_exception( - (WASMModuleInstance *)sub_module, exception_buf); - break; -#endif -#if WASM_ENABLE_AOT != 0 - case Wasm_Module_AoT: - has_exception = aot_copy_exception((AOTModuleInstance *)sub_module, - exception_buf); - break; -#endif - default: - return; - } - - if (has_exception) { - message = exception_buf; - if (strlen(message) >= exception_prefix_len) { - message += exception_prefix_len; - } - else { - LOG_WARNING("sub-module exception format unexpected: %s", message); - return; - } - - if (strcmp(message, memory_oob_exception) != 0) { - LOG_WARNING("skip propagating non-memory-OOB exception: %s", - message); - return; - } - - switch (parent->module_type) { -#if WASM_ENABLE_INTERP != 0 - case Wasm_Module_Bytecode: - wasm_set_exception((WASMModuleInstance *)parent, message); - break; -#endif -#if WASM_ENABLE_AOT != 0 - case Wasm_Module_AoT: - aot_set_exception((AOTModuleInstance *)parent, message); - break; -#endif - default: - break; - } - - switch (sub_module->module_type) { -#if WASM_ENABLE_INTERP != 0 - case Wasm_Module_Bytecode: - wasm_set_exception((WASMModuleInstance *)sub_module, NULL); - break; -#endif -#if WASM_ENABLE_AOT != 0 - case Wasm_Module_AoT: - aot_set_exception((AOTModuleInstance *)sub_module, NULL); - break; -#endif - default: - break; - } - } -} - static void wasm_runtime_destroy_registered_module_list(void); #endif /* WASM_ENABLE_MULTI_MODULE */ @@ -1449,6 +1371,84 @@ wasm_runtime_destroy_loading_module_list() os_mutex_unlock(&loading_module_list_lock); } + +void +wasm_runtime_propagate_exception_from_import( + WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *sub_module) +{ + static const uint32 exception_prefix_len = sizeof("Exception: ") - 1; + static const char memory_oob_exception[] = "out of bounds memory access"; + char exception_buf[EXCEPTION_BUF_LEN] = { 0 }; + const char *message = NULL; + bool has_exception = false; + + if (!parent || !sub_module) + return; + + switch (sub_module->module_type) { +#if WASM_ENABLE_INTERP != 0 + case Wasm_Module_Bytecode: + has_exception = wasm_copy_exception( + (WASMModuleInstance *)sub_module, exception_buf); + break; +#endif +#if WASM_ENABLE_AOT != 0 + case Wasm_Module_AoT: + has_exception = aot_copy_exception((AOTModuleInstance *)sub_module, + exception_buf); + break; +#endif + default: + return; + } + + if (has_exception) { + message = exception_buf; + if (strlen(message) >= exception_prefix_len) { + message += exception_prefix_len; + } + else { + LOG_WARNING("sub-module exception format unexpected: %s", message); + return; + } + + if (strcmp(message, memory_oob_exception) != 0) { + LOG_WARNING("skip propagating non-memory-OOB exception: %s", + message); + return; + } + + switch (parent->module_type) { +#if WASM_ENABLE_INTERP != 0 + case Wasm_Module_Bytecode: + wasm_set_exception((WASMModuleInstance *)parent, message); + break; +#endif +#if WASM_ENABLE_AOT != 0 + case Wasm_Module_AoT: + aot_set_exception((AOTModuleInstance *)parent, message); + break; +#endif + default: + break; + } + + switch (sub_module->module_type) { +#if WASM_ENABLE_INTERP != 0 + case Wasm_Module_Bytecode: + wasm_set_exception((WASMModuleInstance *)sub_module, NULL); + break; +#endif +#if WASM_ENABLE_AOT != 0 + case Wasm_Module_AoT: + aot_set_exception((AOTModuleInstance *)sub_module, NULL); + break; +#endif + default: + break; + } + } +} #endif /* WASM_ENABLE_MULTI_MODULE */ bool diff --git a/tests/regression/ba-issues/build_wamr.sh b/tests/regression/ba-issues/build_wamr.sh index 76562c5a9..3f0fd82e1 100755 --- a/tests/regression/ba-issues/build_wamr.sh +++ b/tests/regression/ba-issues/build_wamr.sh @@ -25,8 +25,15 @@ function build_iwasm() { cd ${WORK_DIR}/build && if [ -d build-iwasm-$2 ]; then rm -rf build-iwasm-$2; else mkdir build-iwasm-$2; fi && cd build-iwasm-$2 && + + # default: enable asan, when pass false, disable asan + SANITIZER_FLAG="-DWAMR_BUILD_SANITIZER=asan" + if [ "$3" = "false" ]; then + SANITIZER_FLAG="" + fi + cmake ${WAMR_DIR}/product-mini/platforms/${PLATFORM} $1 \ - -DCMAKE_BUILD_TYPE=Debug -DWAMR_BUILD_SANITIZER=asan && + -DCMAKE_BUILD_TYPE=Debug ${SANITIZER_FLAG} && make -j 4 if [ "$?" != 0 ]; then echo -e "build iwasm failed" @@ -60,4 +67,7 @@ build_iwasm "-DWAMR_BUILD_REF_TYPES=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LIBC_WASI= # build multi-tier-jit iwasm for testing classic-interp, fast-jit, llvm-jit and multi-tier-jit with libc-wasi disabled build_iwasm "-DWAMR_BUILD_REF_TYPES=1 -DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LIBC_WASI=0" "multi-tier-jit-wasi-disabled" +# build default iwasm for testing multi-module +build_iwasm "-DWAMR_BUILD_MULTI_MODULE=1 -DWAMR_BUILD_MULTI_MEMORY=1 -DWAMR_BUILD_AOT=0 -DWAMR_BUILD_FAST_INTERP=0" "multi-memory-multi-module" "false" + # TODO: add more version of iwasm, for example, sgx version diff --git a/tests/regression/ba-issues/issues/issue-4703/double_page_memory.wasm b/tests/regression/ba-issues/issues/issue-4703/double_page_memory.wasm new file mode 100644 index 0000000000000000000000000000000000000000..40b600133c55e015734cf518299c2a6eb93b5e0e GIT binary patch literal 132 zcmZQbEY4+QU|?WmWlUgTtY>CoWME}uVn|^)!N{(_%#oX#n_pBJZ^Xm^V;VCtvgIab z=EWN^FmNd{$}>1VJX{q literal 0 HcmV?d00001 diff --git a/tests/regression/ba-issues/running_config.json b/tests/regression/ba-issues/running_config.json index e7a4a3117..3bb9a7a9a 100644 --- a/tests/regression/ba-issues/running_config.json +++ b/tests/regression/ba-issues/running_config.json @@ -1818,6 +1818,22 @@ "stdout content": "Exception: unsupported opcode", "description": "classic-interp will exit gracefully when meeting simd opcodes" } + }, + { + "deprecated": false, + "ids": [ + 4703 + ], + "runtime": "iwasm-multi-memory-multi-module", + "file": "single_page_memory.wasm", + "mode": "classic-interp", + "options": "--module-path=./issues/issue-4703", + "argument": "", + "expected return": { + "ret code": 1, + "stdout content": "Exception: out of bounds memory access", + "description": "when encounter memory OOB in sub-module when hardware bondary check is enabled, report exception correctly instead of silent exit" + } } ] -} +} \ No newline at end of file