feat(vm)!: implement typed callables and single-context execution
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good

Add structural Fn signatures, typed capture environments, indirect calls and Trait dispatch while preserving logical parameter and result groups, ownership and budgets.

Remove guest coroutine execution and legacy loading. Activate bounded sealed PBX v1 with mandatory capture metadata, preserve frame continuations and root return results, and migrate producers and shipped cartridges.

Publish coordinated specs, handoffs and the completed runtime lesson. Validation: 880 workspace tests passed, 9 existing tests ignored; both cartridges completed six production-host frames.
This commit is contained in:
2026-10-09 05:47:24 +01:00
parent 86bade3e1f
commit ec84a6bba5
96 changed files with 3656 additions and 6075 deletions
@@ -1,6 +1,6 @@
//! Bounded basic-block stack analysis. This does not publish an image or prove
//! semantic argument types. Closure target facts preserve the existing known
//! callee restriction; no guest callable descriptor is invented here.
//! semantic argument types. Fn stack effects come from the expected Registry
//! signature; concrete target facts never gate an indirect call.
use super::*;
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -108,15 +108,14 @@ fn analyze(
4
},
)?;
let effect = instruction.registry_stack_effect(registry)?.unwrap();
let effect =
instruction.registry_stack_effect_in(registry, FunctionId(id))?.unwrap();
(u64::from(effect.pops), u64::from(effect.pushes))
} else {
(u64::from(spec.pops.unwrap()), u64::from(spec.pushes.unwrap()))
};
match instruction.opcode {
Op::PopN => effect = (u64::from(instruction.imm_u32().unwrap()), 0),
Op::MakeClosure => effect = (u64::from(instruction.imm_u32x2().unwrap().1), 1),
Op::Spawn => effect = (u64::from(instruction.imm_u32x2().unwrap().1), 0),
Op::Call => {
budget.work(1)?;
let callee = registry
@@ -124,30 +123,6 @@ fn analyze(
.unwrap();
effect = (u64::from(callee.parameter_slots), u64::from(callee.result_slots));
}
Op::CallClosure => {
let arguments = u64::from(instruction.imm_u32().unwrap());
if (height as u64) < arguments + 1 {
return Err(count_error(R::StackUnderflow, height as u64, arguments + 1));
}
budget.work(2)?;
let target = match working[locals + height - 1] {
Fact::Closure(target) => target,
Fact::NonClosure => return Err(E::new(C::InvalidCode, R::NotClosure)),
Fact::Unknown => {
return Err(E::new(C::InvalidCode, R::UnknownClosureCallee));
}
};
let callee = registry.function_layout(FunctionId(target)).unwrap();
if u64::from(callee.parameter_slots) != arguments + 1 {
return Err(count_error(
R::Signature,
arguments + 1,
u64::from(callee.parameter_slots),
)
.reference(IdDomain::Function, target));
}
effect = (arguments + 1, u64::from(callee.result_slots));
}
Op::Ret => {
if height != results {
return Err(count_error(R::ReturnHeight, height as u64, results as u64));
@@ -169,9 +144,6 @@ fn analyze(
.unwrap();
effect = (native.arg_slots() as u64, native.ret_slots() as u64);
}
Op::Yield if height != 0 => {
return Err(count_error(R::InvalidYield, height as u64, 0));
}
_ => {}
}
if (height as u64) < effect.0 {
@@ -20,6 +20,14 @@ fn registry(code: &[u8], functions: Vec<FunctionDecl>) -> Registry {
TypeDecl { name: None, kind: TypeKind::Builtin(B::VEC2) },
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
TypeDecl { name: None, kind: TypeKind::Trait(vec![]) },
TypeDecl { name: None, kind: TypeKind::Fn(Signature::default()) },
TypeDecl {
name: None,
kind: TypeKind::Fn(Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
results: vec![],
}),
},
],
functions,
implementations: vec![],
@@ -221,48 +229,36 @@ fn stack_analysis_straight_line_storage_is_bounded_by_blocks_not_instruction_fra
}
#[test]
fn stack_analysis_retains_unknown_closure_and_yield_safety_rules() {
let mut code = vec![];
arg(&mut code, Op::GetLocal, 0);
arg(&mut code, Op::CallClosure, 0);
op(&mut code, Op::Halt);
fn stack_analysis_accepts_unknown_fn_targets_and_rejects_removed_opcodes() {
let code = prometeu_bytecode::assemble("GET_LOCAL 0\nCALL_CLOSURE 4\nHALT").unwrap();
let f = FunctionDecl {
signature: Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
parameters: vec![NamedType { type_id: TypeId(4), name: None }],
results: vec![],
},
..function(&code)
};
let r = registry(&code, vec![f]);
assert_eq!(stacks(&code, &r).unwrap_err().reason, R::UnknownClosureCallee);
let mut code = vec![];
arg(&mut code, Op::GetLocal, 0);
arg(&mut code, Op::CallClosure, 0);
let r = registry(&code, vec![FunctionDecl { local_slots: 1, ..function(&code) }]);
assert_eq!(stacks(&code, &r).unwrap_err().reason, R::NotClosure);
let mut code = vec![];
arg(&mut code, Op::PushI32, 0);
op(&mut code, Op::Yield);
let r = registry(&code, vec![function(&code)]);
assert_eq!(stacks(&code, &r).unwrap_err().reason, R::InvalidYield);
stacks(&code, &r).unwrap();
for opcode in [Op::Yield, Op::Sleep, Op::Spawn] {
let mut code = vec![];
op(&mut code, Op::Halt);
op(&mut code, opcode);
code.resize(code.len() + opcode.spec().imm_bytes as usize, 0);
let r = registry(&code, vec![function(&code)]);
assert_eq!(check(&code, &r).unwrap_err().reason, R::RemovedOpcode);
}
}
#[test]
fn stack_analysis_preserves_closure_facts_through_permutations_before_rejecting_bad_arity() {
let mut code = vec![];
arg(&mut code, Op::PushI32, 0);
arg(&mut code, Op::MakeClosure, 1);
code.extend_from_slice(&0u32.to_le_bytes());
op(&mut code, Op::Swap);
op(&mut code, Op::Pop);
op(&mut code, Op::Dup);
op(&mut code, Op::Pop);
arg(&mut code, Op::SetLocal, 0);
arg(&mut code, Op::GetLocal, 0);
arg(&mut code, Op::CallClosure, 0);
op(&mut code, Op::Halt);
fn expected_fn_signature_checks_argument_height_after_permutations() {
let mut code=prometeu_bytecode::assemble("PUSH_I32 0\nMAKE_CLOSURE fn=1,type=5\nSWAP\nPOP\nDUP\nPOP\nSET_LOCAL 0\nGET_LOCAL 0\nCALL_CLOSURE 5\nHALT").unwrap();
let end = code.len();
op(&mut code, Op::Ret);
let signature = Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
results: vec![],
};
let r = registry(
&code,
vec![
@@ -272,18 +268,14 @@ fn stack_analysis_preserves_closure_facts_through_permutations_before_rejecting_
max_stack_slots: 2,
..function(&code)
},
FunctionDecl { code_offset: end as u32, code_len: 2, ..Default::default() },
FunctionDecl { signature, code_offset: end as u32, code_len: 2, ..Default::default() },
],
);
let e = stacks(&code, &r).unwrap_err();
// The callee deliberately lacks hidden arg0: this fixture is rejected,
// and does not invent a typed descriptor for a valid guest closure.
assert_eq!((e.reason, e.referenced_id), (R::Signature, Some(1)));
assert_eq!((e.requested, e.limit), (Some(1), Some(0)));
assert_eq!(stacks(&code, &r).unwrap_err().reason, R::StackUnderflow);
}
#[test]
fn stack_analysis_checks_closure_counts_without_wraparound() {
fn expected_fn_type_ids_never_wrap_or_become_argument_counts() {
for operation in [Op::MakeClosure, Op::CallClosure] {
let mut code = vec![];
if operation == Op::MakeClosure {
@@ -294,9 +286,7 @@ fn stack_analysis_checks_closure_counts_without_wraparound() {
}
op(&mut code, Op::Halt);
let r = registry(&code, vec![function(&code)]);
let e = stacks(&code, &r).unwrap_err();
assert_eq!(e.reason, R::StackUnderflow);
assert_eq!(e.limit, Some(u64::from(u32::MAX) + u64::from(operation == Op::CallClosure)));
assert_eq!(stacks(&code, &r).unwrap_err().reason, R::TypeId);
}
}
@@ -471,7 +461,7 @@ fn unreachable_function_constant_boolean_and_native_operands_are_checked() {
#[test]
fn spawn_arity_does_not_truncate_and_closure_function_ids_are_checked() {
for (operation, function_id, count, reason) in
[(Op::Spawn, 0, 65_536, R::Signature), (Op::MakeClosure, 1, 0, R::FunctionId)]
[(Op::Spawn, 0, 65_536, R::RemovedOpcode), (Op::MakeClosure, 1, 4, R::FunctionId)]
{
let mut code = vec![];
op(&mut code, Op::Halt);
@@ -24,7 +24,6 @@ enum Fact {
Int64(i64),
Group { ty: TypeId, part: u32, origin: u64 },
Dynamic { part: u32, origin: u64 },
Closure(u32),
}
pub(crate) fn validate_registry_types(
@@ -102,7 +101,16 @@ fn analyze(
while pc < end {
*at_pc = pc;
let instruction = metered_decode(pc, body, budget)?;
transfer(&instruction, &mut working, locals, &mut height, registry, constants, budget)?;
transfer(
&instruction,
&mut working,
locals,
&mut height,
registry,
constants,
id,
budget,
)?;
let spec = instruction.opcode.spec();
if spec.is_branch {
let target = instruction.imm_u32().unwrap() as usize;
@@ -150,7 +158,16 @@ 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, budget)?;
transfer(
&instruction,
&mut working,
locals,
&mut height,
registry,
constants,
id,
budget,
)?;
if instruction.opcode.spec().is_terminator {
break;
}
@@ -178,7 +195,7 @@ fn group(
fn component(registry: &Registry, ty: TypeId, part: u32, budget: &mut LoadBudget) -> Result<u8, E> {
budget.work(1)?;
Ok(match &registry.declarations().types[ty.0 as usize].kind {
TypeKind::Struct(_) | TypeKind::Array(_) => REFERENCE | NULL,
TypeKind::Struct(_) | TypeKind::Array(_) | TypeKind::Fn(_) => REFERENCE | NULL,
TypeKind::Trait(_) => {
if part == 0 {
REFERENCE | NULL
@@ -208,7 +225,6 @@ fn mask(fact: Fact, registry: &Registry, budget: &mut LoadBudget) -> Result<u8,
Fact::Null => NULL,
Fact::Carrier(mask) => mask,
Fact::Int64(_) => I64,
Fact::Closure(_) => REFERENCE,
Fact::Group { ty, part, .. } => return component(registry, ty, part, budget),
Fact::Dynamic { part: 0, .. } => I64 | NULL,
Fact::Dynamic { .. } => ANY,
@@ -244,6 +260,7 @@ fn transfer(
height: &mut usize,
registry: &Registry,
constants: &[Constant],
id: u32,
budget: &mut LoadBudget,
) -> Result<(), E> {
budget.work(1)?;
@@ -251,7 +268,7 @@ fn transfer(
let spec = opcode.spec();
let (mut pops, mut pushes) = if opcode.requires_registry() {
budget.work(4)?;
let effect = instruction.registry_stack_effect(registry)?.unwrap();
let effect = instruction.registry_stack_effect_in(registry, FunctionId(id))?.unwrap();
(effect.pops as usize, effect.pushes as usize)
} else {
(spec.pops.unwrap() as usize, spec.pushes.unwrap() as usize)
@@ -260,22 +277,27 @@ fn transfer(
let mut native = None;
match opcode {
Op::PopN => pops = instruction.imm_u32().unwrap() as usize,
Op::MakeClosure => pops = instruction.imm_u32x2().unwrap().1 as usize,
Op::Spawn => pops = instruction.imm_u32x2().unwrap().1 as usize,
Op::Call | Op::CallClosure => {
Op::Call => {
let target = instruction.imm_u32().unwrap();
budget.work(1)?;
let target = if opcode == Op::Call {
instruction.imm_u32().unwrap()
} else {
let Fact::Closure(target) = working[locals + *height - 1] else {
return Err(E::new(C::InvalidCode, R::UnknownClosureCallee));
};
target
};
let layout = registry.function_layout(FunctionId(target)).unwrap();
pops = layout.parameter_slots as usize;
pushes = layout.result_slots as usize;
call = Some(target);
call = Some(&registry.declarations().functions[target as usize].signature);
}
Op::CallClosure => {
let ty = instruction.imm_u32().unwrap();
let TypeKind::Fn(s) = &registry.declarations().types[ty as usize].kind else {
unreachable!()
};
call = Some(s);
}
Op::CallTrait => {
let (ty, method) = instruction.imm_u32x2().unwrap();
let TypeKind::Trait(methods) = &registry.declarations().types[ty as usize].kind else {
unreachable!()
};
call = Some(&methods[method as usize].signature);
}
Op::Ret => {
// Stack analysis has already proved exact height; no continuation.
@@ -387,14 +409,19 @@ fn transfer(
Constant::String(_) => Fact::Carrier(STRING),
};
}
Op::MakeClosure => {
budget.work(1)?;
working[base] = Fact::Closure(instruction.imm_u32x2().unwrap().0);
Op::MakeClosure | Op::GetCapture => {
let ty = if opcode == Op::MakeClosure {
TypeId(instruction.imm_u32x2().unwrap().1)
} else {
registry.function_layout(FunctionId(id)).unwrap().captures
[instruction.imm_u32().unwrap() as usize]
.type_id
};
group(&mut working[base..], registry, ty, (instruction.pc as u64) << 32, budget)?;
}
Op::Call | Op::CallClosure => {
let signature = &registry.declarations().functions[call.unwrap() as usize].signature;
Op::Call | Op::CallClosure | Op::CallTrait => {
let mut offset = base;
for (index, ty) in signature.results.iter().enumerate() {
for (index, ty) in call.unwrap().results.iter().enumerate() {
offset += group(
&mut working[offset..],
registry,
@@ -572,11 +599,6 @@ fn check_group(
budget.work(2)?;
let kind = &registry.declarations().types[ty.0 as usize].kind;
let width = registry.type_layout(ty).unwrap().storage.slots as usize;
if matches!(values[0], Fact::Closure(_))
&& matches!(kind, TypeKind::Struct(_) | TypeKind::Array(_) | TypeKind::Trait(_))
{
return Err(mismatch(0).reference(IdDomain::Type, ty.0));
}
budget.work(1)?;
if let Fact::Group { ty: source, part: 0, origin } = values[0] {
budget.work(1)?;
@@ -634,12 +656,8 @@ fn check(
budget.work(1)?;
let end = locals + height;
match instruction.opcode {
Op::Call | Op::Spawn => {
let target = if instruction.opcode == Op::Call {
instruction.imm_u32().unwrap()
} else {
instruction.imm_u32x2().unwrap().0
};
Op::Call => {
let target = instruction.imm_u32().unwrap();
budget.work(1)?;
let callee = registry.function_layout(FunctionId(target)).unwrap();
let mut base = end - callee.parameter_slots as usize;
@@ -659,21 +677,35 @@ fn check(
base += registry.type_layout(*ty).unwrap().storage.slots as usize;
}
}
Op::CallClosure => {
budget.work(1)?;
let Fact::Closure(target) = working[end - 1] else {
return Err(E::new(C::InvalidCode, R::UnknownClosureCallee));
};
let parameters =
&registry.declarations().functions[target as usize].signature.parameters;
budget.work(1)?;
let receiver = parameters.first().unwrap();
if registry.type_layout(receiver.type_id).unwrap().storage.slots != 1 {
return Err(mismatch(0));
Op::MakeClosure => {
let target = instruction.imm_u32x2().unwrap().0;
let layout = registry.function_layout(FunctionId(target)).unwrap();
let mut base = end - layout.capture_slots as usize;
for capture in &layout.captures {
check_group(&working[base..], capture.type_id, registry, budget)?;
base += capture.slots as usize;
}
check_group(&working[end - 1..end], receiver.type_id, registry, budget)?;
let mut base = end - instruction.imm_u32().unwrap() as usize - 1;
for parameter in parameters.iter().skip(1) {
}
Op::CallClosure | Op::CallTrait => {
let (ty, view_slots, signature) = if instruction.opcode == Op::CallClosure {
let ty = TypeId(instruction.imm_u32().unwrap());
let TypeKind::Fn(signature) = &registry.declarations().types[ty.0 as usize].kind
else {
unreachable!()
};
(ty, 1, signature)
} else {
let (ty, method) = instruction.imm_u32x2().unwrap();
let TypeKind::Trait(methods) = &registry.declarations().types[ty as usize].kind
else {
unreachable!()
};
(TypeId(ty), 2, &methods[method as usize].signature)
};
check_group(&working[end - view_slots..end], ty, registry, budget)?;
let effect = instruction.registry_stack_effect_in(registry, FunctionId(id))?.unwrap();
let mut base = end - effect.pops as usize;
for parameter in &signature.parameters {
check_group(&working[base..], parameter.type_id, registry, budget)?;
budget.work(1)?;
base += registry.type_layout(parameter.type_id).unwrap().storage.slots as usize;
@@ -688,7 +720,7 @@ fn check(
(instruction.imm_u32().unwrap(), 0)
};
let ty = TypeId(id);
let effect = instruction.registry_stack_effect(registry)?.unwrap();
let effect = instruction.registry_stack_effect_in(registry, FunctionId(id))?.unwrap();
let base = end - effect.pops as usize;
check_group(&working[base..base + 1], ty, registry, budget)?;
let array = matches!(instruction.opcode, Op::ArrayGet | Op::ArraySet);
@@ -32,6 +32,7 @@ fn function(
max: u32,
) -> FunctionDecl {
FunctionDecl {
captures: vec![],
code_offset: offset as u32,
code_len: len as u32,
local_slots: locals,
@@ -283,7 +284,7 @@ fn known_closure_receivers_are_not_faked_as_user_struct_types() {
let r =
registry(&code, vec![function(0, end, &[], &[], 0, 1), function(end, 2, &[9], &[], 0, 0)]);
// No provisional callable descriptor: a Closure is never a guest Struct.
assert_eq!(check(&code, &r, &[]).unwrap_err().reason, R::TypeMismatch);
assert_eq!(check(&code, &r, &[]).unwrap_err().reason, R::Category);
}
#[test]