Skip to content

Commit 91e616b

Browse files
committed
only handle memory OOB exception
1 parent 8ea918a commit 91e616b

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,77 @@ void
9090
wasm_runtime_propagate_exception_from_import(
9191
WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *sub_module)
9292
{
93-
static const char exception_prefix[] = "Exception: ";
93+
static const uint32 exception_prefix_len = sizeof("Exception: ") - 1;
94+
static const char memory_oob_exception[] = "out of bounds memory access";
95+
char exception_buf[EXCEPTION_BUF_LEN] = { 0 };
9496
const char *message = NULL;
97+
bool has_exception = false;
9598

9699
if (!parent || !sub_module)
97100
return;
98101

99-
message = wasm_get_exception(sub_module);
100-
if (message && message[0] != '\0') {
101-
if (!strncmp(message, exception_prefix, sizeof(exception_prefix) - 1)) {
102-
message += sizeof(exception_prefix) - 1;
102+
switch (sub_module->module_type) {
103+
#if WASM_ENABLE_INTERP != 0
104+
case Wasm_Module_Bytecode:
105+
has_exception = wasm_copy_exception(
106+
(WASMModuleInstance *)sub_module, exception_buf);
107+
break;
108+
#endif
109+
#if WASM_ENABLE_AOT != 0
110+
case Wasm_Module_AoT:
111+
has_exception = aot_copy_exception((AOTModuleInstance *)sub_module,
112+
exception_buf);
113+
break;
114+
#endif
115+
default:
116+
return;
117+
}
118+
119+
if (has_exception) {
120+
message = exception_buf;
121+
if (strlen(message) >= exception_prefix_len) {
122+
message += exception_prefix_len;
123+
}
124+
else {
125+
LOG_WARNING("sub-module exception format unexpected: %s", message);
126+
return;
127+
}
128+
129+
if (strcmp(message, memory_oob_exception) != 0) {
130+
LOG_WARNING("skip propagating non-memory-OOB exception: %s",
131+
message);
132+
return;
133+
}
134+
135+
switch (parent->module_type) {
136+
#if WASM_ENABLE_INTERP != 0
137+
case Wasm_Module_Bytecode:
138+
wasm_set_exception((WASMModuleInstance *)parent, message);
139+
break;
140+
#endif
141+
#if WASM_ENABLE_AOT != 0
142+
case Wasm_Module_AoT:
143+
aot_set_exception((AOTModuleInstance *)parent, message);
144+
break;
145+
#endif
146+
default:
147+
break;
103148
}
104149

105-
wasm_set_exception(parent, message);
106-
wasm_set_exception(sub_module, NULL);
150+
switch (sub_module->module_type) {
151+
#if WASM_ENABLE_INTERP != 0
152+
case Wasm_Module_Bytecode:
153+
wasm_set_exception((WASMModuleInstance *)sub_module, NULL);
154+
break;
155+
#endif
156+
#if WASM_ENABLE_AOT != 0
157+
case Wasm_Module_AoT:
158+
aot_set_exception((AOTModuleInstance *)sub_module, NULL);
159+
break;
160+
#endif
161+
default:
162+
break;
163+
}
107164
}
108165
}
109166

@@ -275,8 +332,8 @@ runtime_signal_handler(void *sig_addr)
275332
&& jmpbuf_node->module_inst
276333
!= (WASMModuleInstanceCommon *)module_inst) {
277334
wasm_runtime_propagate_exception_from_import(
278-
(WASMModuleInstance *)jmpbuf_node->module_inst,
279-
module_inst);
335+
(WASMModuleInstanceCommon *)jmpbuf_node->module_inst,
336+
(WASMModuleInstanceCommon *)module_inst);
280337
}
281338
#endif
282339
os_longjmp(jmpbuf_node->jmpbuf, 1);

core/iwasm/common/wasm_runtime_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ typedef struct WASMRegisteredModule {
579579
uint32 orig_file_buf_size;
580580
} WASMRegisteredModule;
581581

582+
/* Propagate callee's memory OOB exception to caller module in hardware memory
583+
* exception handler */
582584
void
583585
wasm_runtime_propagate_exception_from_import(
584586
WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *sub_module);

0 commit comments

Comments
 (0)