Skip to content

Commit 370d321

Browse files
committed
Allow HW OOB exception propagate from callee to caller when multi-module is enabled
1 parent c065004 commit 370d321

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

core/iwasm/common/wasm_exec_env.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ typedef struct WASMCurrentEnvStatus WASMCurrentEnvStatus;
3030
typedef struct WASMJmpBuf {
3131
struct WASMJmpBuf *prev;
3232
korp_jmpbuf jmpbuf;
33+
#if WASM_ENABLE_MULTI_MODULE != 0
34+
/* The owner module instance associated with the current jmpbuf. Used in multi-module to propagate the exception */
35+
struct WASMModuleInstanceCommon *module_inst;
36+
#endif
3337
} WASMJmpBuf;
3438
#endif
3539

core/iwasm/common/wasm_runtime_common.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ runtime_signal_handler(void *sig_addr)
248248

249249
if (is_sig_addr_in_guard_pages(sig_addr, module_inst)) {
250250
wasm_set_exception(module_inst, "out of bounds memory access");
251+
#if WASM_ENABLE_MULTI_MODULE != 0
252+
if (jmpbuf_node->module_inst
253+
&& jmpbuf_node->module_inst
254+
!= (WASMModuleInstanceCommon *)module_inst) {
255+
wasm_runtime_propagate_exception_from_import(
256+
(WASMModuleInstance *)jmpbuf_node->module_inst,
257+
module_inst);
258+
}
259+
#endif
251260
os_longjmp(jmpbuf_node->jmpbuf, 1);
252261
}
253262
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
@@ -375,6 +384,15 @@ runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
375384
the wasm func returns, the caller will check whether the
376385
exception is thrown and return to runtime. */
377386
wasm_set_exception(module_inst, "out of bounds memory access");
387+
#if WASM_ENABLE_MULTI_MODULE != 0
388+
if (jmpbuf_node->module_inst
389+
&& jmpbuf_node->module_inst
390+
!= (WASMModuleInstanceCommon *)module_inst) {
391+
wasm_runtime_propagate_exception_from_import(
392+
(WASMModuleInstance *)jmpbuf_node->module_inst,
393+
module_inst);
394+
}
395+
#endif
378396
ret = next_action(module_inst, exce_info);
379397
if (ret == EXCEPTION_CONTINUE_SEARCH
380398
|| ret == EXCEPTION_CONTINUE_EXECUTION)

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ wasm_resolve_symbols(WASMModule *module)
103103
return ret;
104104
}
105105

106+
#if WASM_ENABLE_MULTI_MODULE != 0
107+
void
108+
wasm_runtime_propagate_exception_from_import(WASMModuleInstance *parent,
109+
WASMModuleInstance *sub_module)
110+
{
111+
static const char exception_prefix[] = "Exception: ";
112+
const char *message = NULL;
113+
114+
if (!parent || !sub_module)
115+
return;
116+
117+
message = wasm_get_exception(sub_module);
118+
if (message && message[0] != '\0') {
119+
if (!strncmp(message, exception_prefix, sizeof(exception_prefix) - 1)) {
120+
message += sizeof(exception_prefix) - 1;
121+
}
122+
123+
wasm_set_exception(parent, message);
124+
wasm_set_exception(sub_module, NULL);
125+
}
126+
}
127+
#endif
128+
106129
#if WASM_ENABLE_MULTI_MODULE != 0
107130
static WASMFunction *
108131
wasm_resolve_function(const char *module_name, const char *function_name,
@@ -3613,6 +3636,10 @@ call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
36133636
return;
36143637
}
36153638

3639+
#if WASM_ENABLE_MULTI_MODULE != 0
3640+
jmpbuf_node.module_inst = (WASMModuleInstanceCommon *)module_inst;
3641+
#endif
3642+
36163643
wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
36173644

36183645
if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ wasm_lookup_tag(const WASMModuleInstance *module_inst, const char *name,
593593
const char *signature);
594594
#endif
595595

596+
void
597+
wasm_runtime_propagate_exception_from_import(WASMModuleInstance *parent,
598+
WASMModuleInstance *sub_module);
599+
596600
#endif
597601

598602
bool

0 commit comments

Comments
 (0)