fix: try 内 return 泄漏 TryHandler,异常跳回死函数 catch 死循环

This commit is contained in:
爱特每羊队
2026-08-09 07:41:34 +08:00
parent 2f8f1c3b71
commit d4fffcdcc6
6 changed files with 143 additions and 0 deletions
@@ -14,15 +14,34 @@ impl Emitter {
match &ret.argument {
Some(expr) => {
let r = self.emit_expression(expr, ctx)?;
// return 逃出本函数全部 try 域:值求值后、发 RETURN 前,先从栈顶
// 弹出连续纯 catch handler(finally handler 走运行时完成穿越逐个
// 执行)。否则 return 跳过 TRY_END 会让 handler 残留在 try_stack,
// 后续异常 unwind 会跳回已返回函数的 catch 形成死循环。
self.emit_return_try_cleanup(ctx);
ctx.inst(Inst::new(OpCode::RETURN, Operand::Reg(r), Operand::None, Operand::None));
}
None => {
self.emit_return_try_cleanup(ctx);
ctx.inst(Inst::new(OpCode::RETURN, Operand::None, Operand::None, Operand::None));
}
}
Ok(None)
}
/// 在 RETURN 前弹出栈顶连续打开的纯 catch handler。
///
/// # 边界与前提
/// - 只处理位于栈顶的连续纯 catch handler;finally handler 及其下方的 catch
/// handler 不由 TRY_END 弹出(LIFO 无法跨 finally 弹出),由运行时
/// dispatch_return 扫描兜底清理。
fn emit_return_try_cleanup(&self, ctx: &mut CompileCtx) {
let open_catches = ctx.top_open_catch_handlers();
for _ in 0..open_catches {
ctx.inst(Inst::new(OpCode::TRY_END, Operand::None, Operand::None, Operand::None));
}
}
pub(crate) fn emit_basic_return(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u32>, String> {
self.emit_return_statement(stmt, ctx)
}
@@ -22,9 +22,11 @@ impl Emitter {
// 逃出语句时须穿越本 finally,词法上记录域深度供跨越计数。
ctx.push_finally_domain();
ctx.inst(Inst::try_finally_begin(finally_label));
ctx.push_open_finally_handler();
}
if has_catch {
ctx.inst(Inst::try_begin(catch_label));
ctx.push_open_catch_handler();
}
let mut last_try_result: Option<u32> = None;
for s in &ts.block.body {
@@ -40,6 +42,7 @@ impl Emitter {
));
if has_catch {
ctx.inst(Inst::new(OpCode::TRY_END, Operand::None, Operand::None, Operand::None));
ctx.pop_open_try_handler();
}
let jmp_needed = has_catch || has_finally;
if jmp_needed {
@@ -84,6 +87,7 @@ impl Emitter {
Operand::None,
));
ctx.inst(Inst::new(OpCode::TRY_FINALLY_END, Operand::None, Operand::None, Operand::None));
ctx.pop_open_try_handler();
ctx.pop_finally_domain();
}
ctx.labels.set_label_pos(try_end_label, ctx.insts.len());