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
@@ -26,6 +26,12 @@ enum Fact {
Dynamic { part: u32, origin: u64 },
}
#[derive(Clone, Copy)]
struct FunctionScope {
id: u32,
locals: usize,
}
pub(crate) fn validate_registry_types(
code: &[u8],
registry: &Registry,
@@ -62,6 +68,7 @@ fn analyze(
let function = &registry.declarations().functions[id as usize];
let layout = registry.function_layout(FunctionId(id)).unwrap();
let locals = layout.parameter_slots as usize + function.local_slots as usize;
let scope = FunctionScope { id, locals };
let mut blocks = basic_blocks::<Fact>(body, budget, at_pc)?;
let mut initial = scratch::<Fact>(locals, budget)?;
budget.work(locals)?;
@@ -101,16 +108,7 @@ fn analyze(
while pc < end {
*at_pc = pc;
let instruction = metered_decode(pc, body, budget)?;
transfer(
&instruction,
&mut working,
locals,
&mut height,
registry,
constants,
id,
budget,
)?;
transfer(&instruction, &mut working, scope, &mut height, registry, constants, budget)?;
let spec = instruction.opcode.spec();
if spec.is_branch {
let target = instruction.imm_u32().unwrap() as usize;
@@ -158,16 +156,14 @@ fn analyze(
*at_pc = pc;
let instruction = metered_decode(pc, body, budget)?;
check(&instruction, &working, locals, height, registry, id, budget)?;
transfer(
&instruction,
&mut working,
locals,
&mut height,
registry,
constants,
id,
budget,
)?;
transfer(&instruction, &mut working, scope, &mut height, registry, constants, budget)?;
if instruction.opcode.spec().is_branch
&& instruction.imm_u32().unwrap() as usize == body.len()
{
// The taken edge returns after consuming a conditional operand.
// Check only stabilized facts, just as for an explicit RET.
check_results(&working[locals..locals + height], registry, id, budget)?;
}
if instruction.opcode.spec().is_terminator {
break;
}
@@ -256,13 +252,13 @@ fn invalidate(
fn transfer(
instruction: &prometeu_bytecode::DecodedInstr<'_>,
working: &mut [Fact],
locals: usize,
scope: FunctionScope,
height: &mut usize,
registry: &Registry,
constants: &[Constant],
id: u32,
budget: &mut LoadBudget,
) -> Result<(), E> {
let FunctionScope { id, locals } = scope;
budget.work(1)?;
let opcode = instruction.opcode;
let spec = opcode.spec();
@@ -670,12 +666,7 @@ fn check(
}
}
Op::Ret => {
let mut base = locals;
for ty in &registry.declarations().functions[id as usize].signature.results {
check_group(&working[base..], *ty, registry, budget)?;
budget.work(1)?;
base += registry.type_layout(*ty).unwrap().storage.slots as usize;
}
check_results(&working[locals..end], registry, id, budget)?;
}
Op::MakeClosure => {
let target = instruction.imm_u32x2().unwrap().0;
@@ -830,5 +821,20 @@ fn check(
Ok(())
}
fn check_results(
values: &[Fact],
registry: &Registry,
id: u32,
budget: &mut LoadBudget,
) -> Result<(), E> {
let mut base = 0;
for ty in &registry.declarations().functions[id as usize].signature.results {
check_group(&values[base..], *ty, registry, budget)?;
budget.work(1)?;
base += registry.type_layout(*ty).unwrap().storage.slots as usize;
}
Ok(())
}
#[cfg(test)]
mod tests;