fix(vm): enforce return boundaries and amortize budgeted growth
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:
2026-10-09 06:35:51 +01:00
parent ec84a6bba5
commit 2e041266f1
10 changed files with 430 additions and 136 deletions
@@ -3,6 +3,7 @@ mod callable;
mod collections;
#[cfg(test)]
mod collections_tests;
mod control;
mod dynamic;
mod gc;
mod loader;
@@ -266,6 +267,15 @@ impl VirtualMachine {
let opcode = instr.opcode;
self.pc = instr.next_pc;
// Call/return and branch attempts pay once before any runtime refusal.
let charged_control = matches!(
opcode,
OpCode::Call | OpCode::Ret | OpCode::Jmp | OpCode::JmpIfTrue | OpCode::JmpIfFalse
);
if charged_control {
self.cycles += opcode.cycles();
}
// Execute
match opcode {
OpCode::Nop => {}
@@ -277,12 +287,7 @@ impl VirtualMachine {
.imm_u32()
.map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?
as usize;
let func_start = self
.call_stack
.last()
.map(|f| self.program.functions[f.func_idx].code_offset as usize)
.unwrap_or(0);
self.pc = func_start + target;
self.jump_to(target, opcode, start_pc as u32)?;
}
OpCode::JmpIfFalse => {
let target = instr
@@ -293,12 +298,7 @@ impl VirtualMachine {
self.pop_trap(opcode, start_pc as u32, "JMP_IF_FALSE requires one operand")?;
match val {
Value::Boolean(false) => {
let func_start = self
.call_stack
.last()
.map(|f| self.program.functions[f.func_idx].code_offset as usize)
.unwrap_or(0);
self.pc = func_start + target;
self.jump_to(target, opcode, start_pc as u32)?;
}
Value::Boolean(true) => {}
_ => {
@@ -320,12 +320,7 @@ impl VirtualMachine {
self.pop_trap(opcode, start_pc as u32, "JMP_IF_TRUE requires one operand")?;
match val {
Value::Boolean(true) => {
let func_start = self
.call_stack
.last()
.map(|f| self.program.functions[f.func_idx].code_offset as usize)
.unwrap_or(0);
self.pc = func_start + target;
self.jump_to(target, opcode, start_pc as u32)?;
}
Value::Boolean(false) => {}
_ => {
@@ -491,7 +486,7 @@ impl VirtualMachine {
"Integer division by zero".into(),
));
}
Ok(Value::Int32(a / b))
Ok(Value::Int32(a.wrapping_div(b)))
}
(Value::Int64(a), Value::Int64(b)) => {
if b == 0 {
@@ -500,7 +495,7 @@ impl VirtualMachine {
"Integer division by zero".into(),
));
}
Ok(Value::Int64(a / b))
Ok(Value::Int64(a.wrapping_div(b)))
}
(Value::Int32(a), Value::Int64(b)) => {
if b == 0 {
@@ -509,7 +504,7 @@ impl VirtualMachine {
"Integer division by zero".into(),
));
}
Ok(Value::Int64(a as i64 / b))
Ok(Value::Int64((a as i64).wrapping_div(b)))
}
(Value::Int64(a), Value::Int32(b)) => {
if b == 0 {
@@ -518,7 +513,7 @@ impl VirtualMachine {
"Integer division by zero".into(),
));
}
Ok(Value::Int64(a / b as i64))
Ok(Value::Int64(a.wrapping_div(b as i64)))
}
(Value::Float(a), Value::Float(b)) => {
if b == 0.0 {
@@ -557,13 +552,13 @@ impl VirtualMachine {
if b == 0 {
return Err(OpError::Trap(TRAP_DIV_ZERO, "Integer modulo by zero".into()));
}
Ok(Value::Int32(a % b))
Ok(Value::Int32(a.wrapping_rem(b)))
}
(Value::Int64(a), Value::Int64(b)) => {
if b == 0 {
return Err(OpError::Trap(TRAP_DIV_ZERO, "Integer modulo by zero".into()));
}
Ok(Value::Int64(a % b))
Ok(Value::Int64(a.wrapping_rem(b)))
}
_ => Err(OpError::Trap(TRAP_TYPE, "Invalid types for MOD".into())),
})?,
@@ -841,61 +836,7 @@ impl VirtualMachine {
.map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?;
self.pc = callee_offset as usize;
}
OpCode::Ret => {
let frame = self.call_stack.last().cloned().ok_or_else(|| {
self.trap(
TRAP_STACK_UNDERFLOW,
opcode as u16,
"RET with empty call stack".into(),
start_pc as u32,
)
})?;
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
), start_pc as u32));
}
self.check_signature_group(
frame.func_idx,
true,
&self.operand_stack[current_height - return_slots..],
)
.map_err(|(_, error)| self.value_trap(error, opcode, start_pc as u32))?;
// 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, start_pc as u32))?;
for _ in 0..return_slots {
return_vals
.push(self.pop().map_err(LogicalFrameEndingReason::Panic)?)
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
}
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, start_pc as u32)?;
}
self.pc = frame.return_pc as usize;
if self.call_stack.is_empty() {
self.handle_safepoint()
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
}
}
OpCode::Ret => self.finish_return(opcode, start_pc as u32)?,
OpCode::Syscall => {
let pc_at_syscall = start_pc as u32;
let id = instr
@@ -1131,8 +1072,10 @@ impl VirtualMachine {
}
}
// Apply the instruction cost to the cycle counter
self.cycles += opcode.cycles();
// Control attempts were already charged before validation.
if !charged_control {
self.cycles += opcode.cycles();
}
Ok(())
}
}