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.
334 lines
13 KiB
Rust
334 lines
13 KiB
Rust
use super::*;
|
|
#[path = "../../tests/support/callables.rs"]
|
|
mod support;
|
|
use support::*;
|
|
|
|
fn session(
|
|
bodies: &[(
|
|
&str,
|
|
prometeu_bytecode::registry::Signature,
|
|
Vec<prometeu_bytecode::registry::TypeId>,
|
|
)],
|
|
limits: RuntimeLimits,
|
|
) -> VirtualMachine {
|
|
let mut v = VirtualMachine::from_image(image(bodies, vec![]).unwrap(), limits);
|
|
v.prepare_boot_call().unwrap();
|
|
v
|
|
}
|
|
|
|
#[test]
|
|
fn fn_creation_identity_aliasing_and_capture_order() {
|
|
let mut v = vm(&[
|
|
(
|
|
"PUSH_I32 11\nPUSH_I32 22\nMAKE_CLOSURE fn=1,type=4\nDUP\nSET_GLOBAL 0\nSET_GLOBAL 1\nPUSH_I32 11\nPUSH_I32 22\nMAKE_CLOSURE fn=1,type=4\nSET_GLOBAL 2\nGET_GLOBAL 0\nCALL_CLOSURE 4\nRET",
|
|
sig(&[], &[0]),
|
|
vec![],
|
|
),
|
|
(
|
|
"GET_CAPTURE 0\nGET_CAPTURE 1\nSUB\nRET",
|
|
sig(&[], &[0]),
|
|
vec![prometeu_bytecode::registry::TypeId(0); 2],
|
|
),
|
|
]);
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
|
assert_eq!(v.operand_stack_top(1), vec![Value::Int32(-11)]);
|
|
assert_eq!(v.globals[0], v.globals[1]);
|
|
assert_ne!(v.globals[0], v.globals[2]);
|
|
}
|
|
|
|
#[test]
|
|
fn multi_slot_capture_is_one_logical_group() {
|
|
let mut v = vm(&[
|
|
(
|
|
"PUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nMAKE_CLOSURE fn=1,type=13\nCALL_CLOSURE 13\nRET",
|
|
sig(&[], &[2]),
|
|
vec![],
|
|
),
|
|
("GET_CAPTURE 0\nRET", sig(&[], &[2]), vec![prometeu_bytecode::registry::TypeId(2)]),
|
|
]);
|
|
let r = run(&mut v, 1000);
|
|
assert_eq!(r.reason, LogicalFrameEndingReason::EndOfRom);
|
|
assert_eq!(v.operand_stack_top(2), vec![Value::Float(2.), Value::Float(1.)]);
|
|
// 2 pushes + (8+2) creation + 6 call + (2+2) capture + two returns.
|
|
assert_eq!(r.cycles_used, 2 * OpCode::PushF64.cycles() + 10 + 6 + 4 + 8);
|
|
}
|
|
|
|
#[test]
|
|
fn callable_budget_zero_preserves_state_and_positive_slice_finishes_one_instruction() {
|
|
let mut v = vm(&[
|
|
(
|
|
"PUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nMAKE_CLOSURE fn=1,type=13\nCALL_CLOSURE 13\nRET",
|
|
sig(&[], &[2]),
|
|
vec![],
|
|
),
|
|
("GET_CAPTURE 0\nRET", sig(&[], &[2]), vec![prometeu_bytecode::registry::TypeId(2)]),
|
|
]);
|
|
for _ in 0..2 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
// Creation, indirect call and complete-group capture are each atomic even
|
|
// when their cost exceeds the remaining positive slice.
|
|
for cost in [10, 6, 4] {
|
|
let before = (
|
|
v.pc,
|
|
v.cycles,
|
|
v.operand_stack.as_slice().to_vec(),
|
|
v.call_stack.len(),
|
|
v.heap.allocation_count(),
|
|
);
|
|
let zero = run(&mut v, 0);
|
|
assert_eq!((zero.cycles_used, zero.steps_executed), (0, 0));
|
|
assert_eq!(zero.reason, LogicalFrameEndingReason::BudgetExhausted);
|
|
assert_eq!(
|
|
(
|
|
v.pc,
|
|
v.cycles,
|
|
v.operand_stack.as_slice().to_vec(),
|
|
v.call_stack.len(),
|
|
v.heap.allocation_count()
|
|
),
|
|
before
|
|
);
|
|
let one = run(&mut v, 1);
|
|
assert_eq!((one.cycles_used, one.steps_executed), (cost, 1));
|
|
assert_eq!(one.reason, LogicalFrameEndingReason::BudgetExhausted);
|
|
}
|
|
assert_eq!(run(&mut v, 100).reason, LogicalFrameEndingReason::EndOfRom);
|
|
assert_eq!(v.operand_stack_top(2), vec![Value::Float(2.), Value::Float(1.)]);
|
|
}
|
|
|
|
#[test]
|
|
fn closure_call_depth_failure_preserves_fn_and_charges_once() {
|
|
let mut v = session(
|
|
&[
|
|
("MAKE_CLOSURE fn=1,type=9\nCALL_CLOSURE 9\nRET", sig(&[], &[]), vec![]),
|
|
("RET", sig(&[], &[]), vec![]),
|
|
],
|
|
RuntimeLimits::default().lowered(RuntimeResource::CallDepth, 1).unwrap(),
|
|
);
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
let before = v.cycles;
|
|
let result = v.step(&mut Native, &mut HostContext::new(None));
|
|
assert!(matches!(result,Err(LogicalFrameEndingReason::Trap(t)) if t.code==TRAP_RESOURCE_LIMIT));
|
|
assert_eq!(v.cycles - before, 6);
|
|
assert_eq!(v.call_stack.len(), 1);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
}
|
|
|
|
#[test]
|
|
fn null_and_stale_fn_calls_trap_without_consuming_source() {
|
|
for value in [Value::Null, Value::HeapRef(HeapRef { index: 99, generation: 1 })] {
|
|
let mut v = vm(&[("GET_GLOBAL 0\nCALL_CLOSURE 4\nRET", sig(&[], &[0]), vec![])]);
|
|
v.globals.push(value.clone()).unwrap();
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let before = v.cycles;
|
|
assert!(
|
|
matches!(v.step(&mut Native,&mut HostContext::new(None)),Err(LogicalFrameEndingReason::Trap(t)) if t.code==TRAP_NULL_REFERENCE || t.code==TRAP_INVALID_HANDLE)
|
|
);
|
|
assert_eq!(v.cycles - before, 6);
|
|
assert_eq!(v.operand_stack.last(), Some(&value));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn closure_quota_refusal_keeps_captures_and_owners() {
|
|
for resource in [RuntimeResource::HandleSlots, RuntimeResource::GuestBytes] {
|
|
let mut v = session(
|
|
&[
|
|
("PUSH_I32 7\nMAKE_CLOSURE fn=1,type=4\nPOP\nRET", sig(&[], &[]), vec![]),
|
|
(
|
|
"GET_CAPTURE 0\nRET",
|
|
sig(&[], &[0]),
|
|
vec![prometeu_bytecode::registry::TypeId(0)],
|
|
),
|
|
],
|
|
RuntimeLimits::default().lowered(resource, 0).unwrap(),
|
|
);
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let before = v.cycles;
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
assert!(
|
|
matches!(v.step(&mut Native,&mut HostContext::new(None)),Err(LogicalFrameEndingReason::Trap(t)) if t.code==TRAP_RESOURCE_LIMIT)
|
|
);
|
|
assert_eq!(v.cycles - before, 9);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
assert_eq!(v.heap.allocation_count(), 0);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn closure_temporary_growth_refusal_preserves_capture() {
|
|
let mut v = vm(&[
|
|
(
|
|
"MAKE_CLOSURE fn=2,type=4\nPOP\nPUSH_I32 7\nMAKE_CLOSURE fn=1,type=4\nPOP\nRET",
|
|
sig(&[], &[]),
|
|
vec![],
|
|
),
|
|
("GET_CAPTURE 0\nRET", sig(&[], &[0]), vec![prometeu_bytecode::registry::TypeId(0)]),
|
|
("PUSH_I32 0\nRET", sig(&[], &[0]), vec![]),
|
|
]);
|
|
for _ in 0..3 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
v.budget.test_lower_limit(RuntimeResource::TemporaryBytes, 0);
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
let count = v.heap.allocation_count();
|
|
let before = v.cycles;
|
|
assert!(
|
|
matches!(v.step(&mut Native,&mut HostContext::new(None)),Err(LogicalFrameEndingReason::Trap(t)) if t.code==TRAP_RESOURCE_LIMIT)
|
|
);
|
|
assert_eq!(v.cycles - before, 9);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
assert_eq!(v.heap.allocation_count(), count);
|
|
assert_eq!(v.budget.used(RuntimeResource::TemporaryBytes), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn root_return_gc_roots_fn_result_before_collecting() {
|
|
let mut v = vm(&[
|
|
("PUSH_I32 55\nMAKE_CLOSURE fn=1,type=4\nRET", sig(&[], &[4]), vec![]),
|
|
("GET_CAPTURE 0\nRET", sig(&[], &[0]), vec![prometeu_bytecode::registry::TypeId(0)]),
|
|
]);
|
|
v.gc_alloc_threshold = 1;
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
|
assert_eq!(v.heap.len(), 1);
|
|
assert_eq!(v.call_stack.len(), 0);
|
|
let Value::HeapRef(owner) = v.operand_stack[0] else { panic!() };
|
|
assert!(v.heap.is_valid(owner));
|
|
assert_eq!(v.heap.closure_env_slice(owner).unwrap(), &[Value::Int32(55)]);
|
|
}
|
|
|
|
#[test]
|
|
fn unreachable_closure_cell_cycle_is_collected_at_sync() {
|
|
let mut v = vm(&[
|
|
(
|
|
"STRUCT_NEW 15\nDUP\nMAKE_CLOSURE fn=1,type=4\nSTRUCT_SET type=15,field=0\nFRAME_SYNC\nRET",
|
|
sig(&[], &[]),
|
|
vec![],
|
|
),
|
|
("PUSH_I32 1\nRET", sig(&[], &[0]), vec![prometeu_bytecode::registry::TypeId(15)]),
|
|
]);
|
|
v.gc_alloc_threshold = 1;
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::FrameSync);
|
|
assert_eq!(v.heap.allocation_count(), 2);
|
|
assert_eq!(v.heap.len(), 0);
|
|
assert_eq!(v.budget.used(RuntimeResource::GuestBytes), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn mixed_trait_dynamic_string_vec2_captures_survive_sync_and_root_return() {
|
|
use prometeu_bytecode::registry::*;
|
|
let root = "STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nPUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nDYN_PACK 2\nPUSH_CONST 0\nPUSH_F64 f64:0x4008000000000000\nPUSH_F64 f64:0x4010000000000000\nMAKE_CLOSURE fn=1,type=17\nCALL_CLOSURE 17\nRET";
|
|
let bodies = [
|
|
(root, sig(&[], &[7, 11, 3, 2]), vec![]),
|
|
(
|
|
"FRAME_SYNC\nGET_CAPTURE 0\nGET_CAPTURE 1\nGET_CAPTURE 2\nGET_CAPTURE 3\nRET",
|
|
sig(&[], &[7, 11, 3, 2]),
|
|
vec![TypeId(7), TypeId(11), TypeId(3), TypeId(2)],
|
|
),
|
|
("PUSH_I32 0\nRET", sig(&[5], &[0]), vec![]),
|
|
];
|
|
let implementation =
|
|
ImplementationDecl { owner: TypeId(5), trait_id: TypeId(7), methods: vec![FunctionId(2)] };
|
|
let image = image_constants(
|
|
&bodies,
|
|
vec![implementation],
|
|
&[prometeu_bytecode::model::ConstantPoolEntry::String("native capture".into())],
|
|
)
|
|
.unwrap();
|
|
let mut v = VirtualMachine::from_image(image, RuntimeLimits::default());
|
|
v.prepare_boot_call().unwrap();
|
|
v.gc_alloc_threshold = 1;
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::FrameSync);
|
|
assert_eq!(v.heap.len(), 3);
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
|
let values = v.operand_stack.as_slice();
|
|
assert_eq!(values.len(), 7);
|
|
assert_eq!(values[4], Value::string("native capture"));
|
|
assert_eq!(values[5..], [Value::Float(3.), Value::Float(4.)]);
|
|
let registry = v.validated_image.as_ref().unwrap().registry();
|
|
crate::value_contract::validate(registry, &v.heap, TypeId(7), &values[..2]).unwrap();
|
|
crate::value_contract::validate(registry, &v.heap, TypeId(11), &values[2..4]).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn fn_snapshot_is_not_a_guest_root_or_an_import_authority() {
|
|
use prometeu_bytecode::registry::TypeId;
|
|
let mut v = vm(&[
|
|
("MAKE_CLOSURE fn=1,type=4\nRET", sig(&[], &[4]), vec![]),
|
|
("PUSH_I32 73\nRET", sig(&[], &[0]), vec![]),
|
|
("RET", sig(&[], &[]), vec![]),
|
|
]);
|
|
v.gc_alloc_threshold = 1;
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
|
let snapshot = v.operand_stack_top(1);
|
|
let Value::HeapRef(owner) = snapshot[0] else { panic!() };
|
|
v.prepare_call("2").unwrap();
|
|
v.heap.alloc_typed_closure(1, TypeId(4), &[]).unwrap();
|
|
assert_eq!(run(&mut v, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
|
assert!(!v.heap.is_valid(owner));
|
|
let other =
|
|
image(&[("GET_LOCAL 0\nCALL_CLOSURE 4\nRET", sig(&[4], &[0]), vec![])], vec![]).unwrap();
|
|
let mut other = VirtualMachine::from_image(other, RuntimeLimits::default());
|
|
assert!(matches!(
|
|
other.prepare_call_with_args("0", &snapshot),
|
|
Err(VmInitError::EntrypointArgumentsInvalid { trap_code: TRAP_INVALID_HANDLE, .. })
|
|
));
|
|
assert_eq!(other.call_stack.len(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_trait_views_trap_once_and_preserve_sources() {
|
|
use prometeu_bytecode::registry::*;
|
|
let bodies = [
|
|
("GET_GLOBAL 0\nGET_GLOBAL 1\nCALL_TRAIT type=7,method=0\nRET", sig(&[], &[0]), vec![]),
|
|
("PUSH_I32 1\nRET", sig(&[5], &[0]), vec![]),
|
|
];
|
|
let implementation =
|
|
ImplementationDecl { owner: TypeId(5), trait_id: TypeId(7), methods: vec![FunctionId(1)] };
|
|
let image = image(&bodies, vec![implementation]).unwrap();
|
|
for case in 0..5 {
|
|
let mut v = VirtualMachine::from_image(image.clone(), RuntimeLimits::default());
|
|
v.prepare_boot_call().unwrap();
|
|
let wrong = v.heap.allocate_typed_struct(image.registry().clone(), TypeId(8)).unwrap();
|
|
let (values, expected) = match case {
|
|
0 => ([Value::Null, Value::Null], TRAP_NULL_REFERENCE),
|
|
1 => (
|
|
[Value::HeapRef(HeapRef { index: 99, generation: 1 }), Value::Int64(0)],
|
|
TRAP_INVALID_HANDLE,
|
|
),
|
|
2 => ([Value::HeapRef(wrong), Value::Int64(0)], TRAP_TYPE),
|
|
3 => ([Value::Null, Value::Int64(99)], TRAP_TYPE),
|
|
_ => ([Value::HeapRef(wrong), Value::Int32(0)], TRAP_TYPE),
|
|
};
|
|
v.globals.extend_from_slice(&values).unwrap();
|
|
for _ in 0..2 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
let before = v.cycles;
|
|
assert!(
|
|
matches!(v.step(&mut Native,&mut HostContext::new(None)),Err(LogicalFrameEndingReason::Trap(t)) if t.code==expected)
|
|
);
|
|
assert_eq!(v.cycles - before, 6);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
assert_eq!(v.call_stack.len(), 1);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn recursive_fn_calls_use_bounded_ordinary_frames() {
|
|
let mut v = session(
|
|
&[
|
|
("MAKE_CLOSURE fn=1,type=9\nCALL_CLOSURE 9\nRET", sig(&[], &[]), vec![]),
|
|
("MAKE_CLOSURE fn=1,type=9\nCALL_CLOSURE 9\nRET", sig(&[], &[]), vec![]),
|
|
],
|
|
RuntimeLimits::default().lowered(RuntimeResource::CallDepth, 4).unwrap(),
|
|
);
|
|
assert!(
|
|
matches!(run(&mut v,1000).reason,LogicalFrameEndingReason::Trap(t) if t.code==TRAP_RESOURCE_LIMIT && t.opcode==0x53)
|
|
);
|
|
assert_eq!(v.call_stack.len(), 4);
|
|
}
|