fix(vm): enforce return boundaries and amortize budgeted growth
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Validate and complete function-end branches through the shared return path. Handle integer DIV/MOD overflow with wrapping and charge CALL/RET refusals exactly once. Amortize budgeted vector growth with exact-capacity fallback at quota limits. Add execution and accounting regressions, clarify ISA contracts, and include existing lint cleanups.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
//! Function-local branches and validated return boundaries share one completion path.
|
||||
use super::*;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn jump_to(
|
||||
&mut self,
|
||||
target: usize,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
let function = self.call_stack.last().map(|f| &self.program.functions[f.func_idx]);
|
||||
if self.validated_image.is_some() && function.is_some_and(|f| target == f.code_len as usize)
|
||||
{
|
||||
return self.finish_return(opcode, pc);
|
||||
}
|
||||
self.pc = function.map_or(0, |f| f.code_offset as usize) + target;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn finish_return(
|
||||
&mut self,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
let frame = self.call_stack.last().cloned().ok_or_else(|| {
|
||||
self.trap(TRAP_STACK_UNDERFLOW, opcode as u16, "RET with empty call stack".into(), pc)
|
||||
})?;
|
||||
let func = &self.program.functions[frame.func_idx];
|
||||
let return_slots = func.return_slots as usize;
|
||||
|
||||
let current_height = self.operand_stack.len();
|
||||
let expected_height =
|
||||
frame.stack_base + func.param_slots as usize + func.local_slots as usize + return_slots;
|
||||
|
||||
if current_height != expected_height {
|
||||
return Err(self.trap(TRAP_BAD_RET_SLOTS, opcode as u16, format!(
|
||||
"Incorrect stack height at RET in func {}: expected {} slots (stack_base={} + params={} + locals={} + returns={}), got {}",
|
||||
frame.func_idx, expected_height, frame.stack_base, func.param_slots, func.local_slots, return_slots, current_height
|
||||
), pc));
|
||||
}
|
||||
|
||||
self.check_signature_group(
|
||||
frame.func_idx,
|
||||
true,
|
||||
&self.operand_stack[current_height - return_slots..],
|
||||
)
|
||||
.map_err(|(_, error)| self.value_trap(error, opcode, pc))?;
|
||||
|
||||
// Copy return values (preserving order: pop return_slots values, then reverse to push back)
|
||||
let mut return_vals = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes);
|
||||
return_vals.reserve(return_slots).map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
for _ in 0..return_slots {
|
||||
return_vals
|
||||
.push(self.pop().map_err(LogicalFrameEndingReason::Panic)?)
|
||||
.map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
}
|
||||
return_vals.reverse();
|
||||
|
||||
self.call_stack.pop();
|
||||
self.operand_stack.truncate(frame.stack_base);
|
||||
for val in return_vals.drain(..) {
|
||||
self.push_trap(val, opcode, pc)?;
|
||||
}
|
||||
self.pc = frame.return_pc as usize;
|
||||
if self.call_stack.is_empty() {
|
||||
self.handle_safepoint().map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user