Fix dump call stack issue in interpreter (#1358)

Fix dump call stack issue in interpreter introduced by hw bound check:
the call stack isn't dumped if the exception is thrown and caught by
signal handler.
And restore the wasm stack frame to the original status after calling a
wasm function.
This commit is contained in:
Xu Jun 2022-08-08 11:15:30 +08:00 committed by GitHub
parent 0020b3ae68
commit 4b00432c1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 0 deletions

View File

@ -89,6 +89,18 @@ wasm_interp_call_wasm(struct WASMModuleInstance *module_inst,
struct WASMFunctionInstance *function, uint32 argc, struct WASMFunctionInstance *function, uint32 argc,
uint32 argv[]); uint32 argv[]);
/**
* @brief Restore the wasm stack frame to the last native frame or the begging
* of the whole stack
* @note e.g. for stack "begin --> interp --> interp", it will back to the
* "begin", for stack "begin --> interp --> native --> interp", it will become
* "begin --> interp --> native"
*
* @param exec_env the execution environment
*/
void
wasm_interp_restore_wasm_frame(struct WASMExecEnv *exec_env);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -781,6 +781,26 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
wasm_exec_env_free_wasm_frame(exec_env, frame); wasm_exec_env_free_wasm_frame(exec_env, frame);
} }
void
wasm_interp_restore_wasm_frame(WASMExecEnv *exec_env)
{
WASMInterpFrame *cur_frame, *prev_frame;
cur_frame = wasm_exec_env_get_cur_frame(exec_env);
while (cur_frame) {
prev_frame = cur_frame->prev_frame;
if (cur_frame->ip) {
/* FREE_FRAME just set the wasm_stack.s.top pointer, we only need to
* call it once */
FREE_FRAME(exec_env, cur_frame);
break;
}
cur_frame = prev_frame;
}
wasm_exec_env_set_cur_frame(exec_env, cur_frame);
}
static void static void
wasm_interp_call_func_native(WASMModuleInstance *module_inst, wasm_interp_call_func_native(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, WASMExecEnv *exec_env,

View File

@ -845,6 +845,26 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
wasm_exec_env_free_wasm_frame(exec_env, frame); wasm_exec_env_free_wasm_frame(exec_env, frame);
} }
void
wasm_interp_restore_wasm_frame(WASMExecEnv *exec_env)
{
WASMInterpFrame *cur_frame, *prev_frame;
cur_frame = wasm_exec_env_get_cur_frame(exec_env);
while (cur_frame) {
prev_frame = cur_frame->prev_frame;
if (cur_frame->ip) {
/* FREE_FRAME just set the wasm_stack.s.top pointer, we only need to
* call it once */
FREE_FRAME(exec_env, cur_frame);
break;
}
cur_frame = prev_frame;
}
wasm_exec_env_set_cur_frame(exec_env, cur_frame);
}
static void static void
wasm_interp_call_func_native(WASMModuleInstance *module_inst, wasm_interp_call_func_native(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, WASMExecEnv *exec_env,
@ -3925,6 +3945,7 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
wasm_interp_dump_call_stack(exec_env, true, NULL, 0); wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
} }
#endif #endif
LOG_DEBUG("meet an exception %s", wasm_get_exception(module_inst));
} }
wasm_exec_env_set_cur_frame(exec_env, prev_frame); wasm_exec_env_set_cur_frame(exec_env, prev_frame);

View File

@ -1986,6 +1986,15 @@ call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
ret = false; ret = false;
} }
if (wasm_get_exception(module_inst)) {
#if WASM_ENABLE_DUMP_CALL_STACK != 0
if (wasm_interp_create_call_stack(exec_env)) {
wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
}
#endif
wasm_interp_restore_wasm_frame(exec_env);
}
jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env); jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
bh_assert(&jmpbuf_node == jmpbuf_node_pop); bh_assert(&jmpbuf_node == jmpbuf_node_pop);
if (!exec_env->jmpbuf_stack_top) { if (!exec_env->jmpbuf_stack_top) {