fix: CREATE_CLOSURE 越界改运行时错误防进程中断

This commit is contained in:
2026-08-07 00:42:33 +08:00
parent f10010869e
commit 98b07687f0
2 changed files with 12 additions and 3 deletions
+11 -2
View File
@@ -86,10 +86,18 @@ impl Vm {
} }
impl Vm { impl Vm {
pub(crate) fn dispatch_create_closure(&mut self, rd: usize, instr: u32) { pub(crate) fn dispatch_create_closure(&mut self, rd: usize, instr: u32) -> Result<(), String> {
let sub_idx = opcode::imm16(instr) as u32; let sub_idx = opcode::imm16(instr) as u32;
vm_trace!("CREATE_CLOSURE rd={} sub_idx={}", rd, sub_idx); vm_trace!("CREATE_CLOSURE rd={} sub_idx={}", rd, sub_idx);
debug_assert!(sub_idx > 0 && (sub_idx as usize) <= self.sub_modules.len()); if sub_idx == 0 || (sub_idx as usize) > self.sub_modules.len() {
// 逃逸闭包(函数对象在定义模块之外被创建)时 sub_idx 相对定义模块,
// 超出当前 sub_modules 上下文——按运行时错误处理而非索引越界 panic。
return Err(format!(
"CREATE_CLOSURE: sub_module_index {} out of bounds (max {})",
sub_idx,
self.sub_modules.len()
));
}
let sub = &self.sub_modules[sub_idx as usize - 1]; let sub = &self.sub_modules[sub_idx as usize - 1];
let is_arrow = sub.is_arrow; let is_arrow = sub.is_arrow;
let is_class_constructor = sub.is_class_constructor; let is_class_constructor = sub.is_class_constructor;
@@ -119,6 +127,7 @@ impl Vm {
} }
} }
self.regs[rd] = result; self.regs[rd] = result;
Ok(())
} }
#[allow(dead_code)] #[allow(dead_code)]
+1 -1
View File
@@ -869,7 +869,7 @@ impl Vm {
} }
OpCode::CREATE_CLOSURE => { OpCode::CREATE_CLOSURE => {
self.dispatch_create_closure(rd, instr); self.dispatch_create_closure(rd, instr)?;
} }
OpCode::MAKE_CELL => { OpCode::MAKE_CELL => {
self.dispatch_make_cell(rd, instr)?; self.dispatch_make_cell(rd, instr)?;