From 9492cabfa3ee8feee6154f299190d83a8dfbdbda Mon Sep 17 00:00:00 2001 From: TL Date: Mon, 24 Nov 2025 11:55:32 +0800 Subject: [PATCH] only handle memory OOB exception --- core/iwasm/common/wasm_runtime_common.c | 75 ++++++++++++++++++++++--- core/iwasm/common/wasm_runtime_common.h | 2 + 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index d50db068d..4fcfd231d 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -90,20 +90,77 @@ void wasm_runtime_propagate_exception_from_import( WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *sub_module) { - static const char exception_prefix[] = "Exception: "; + 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; - message = wasm_get_exception(sub_module); - if (message && message[0] != '\0') { - if (!strncmp(message, exception_prefix, sizeof(exception_prefix) - 1)) { - message += sizeof(exception_prefix) - 1; + 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; } - wasm_set_exception(parent, message); - wasm_set_exception(sub_module, NULL); + 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; + } } } @@ -275,8 +332,8 @@ runtime_signal_handler(void *sig_addr) && jmpbuf_node->module_inst != (WASMModuleInstanceCommon *)module_inst) { wasm_runtime_propagate_exception_from_import( - (WASMModuleInstance *)jmpbuf_node->module_inst, - module_inst); + (WASMModuleInstanceCommon *)jmpbuf_node->module_inst, + (WASMModuleInstanceCommon *)module_inst); } #endif os_longjmp(jmpbuf_node->jmpbuf, 1); diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 995f7236c..96cc3e19b 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -579,6 +579,8 @@ typedef struct WASMRegisteredModule { uint32 orig_file_buf_size; } WASMRegisteredModule; +/* Propagate callee's memory OOB exception to caller module in hardware memory + * exception handler */ void wasm_runtime_propagate_exception_from_import( WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *sub_module);