feat(vm): execute reference tests and casts for PLN-0176 step 22
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::resource_budget::BudgetVec;
|
||||
mod dynamic;
|
||||
mod gc;
|
||||
mod loader;
|
||||
mod reference;
|
||||
mod runtime;
|
||||
mod stack;
|
||||
mod typed;
|
||||
@@ -1365,15 +1366,9 @@ impl VirtualMachine {
|
||||
return self.execute_dynamic(opcode, target, start_pc as u32);
|
||||
}
|
||||
OpCode::RefIsExact | OpCode::RefImplements | OpCode::RefToTrait | OpCode::RefCast => {
|
||||
// Slot-only ProgramImage cannot authorize typed instructions. The
|
||||
// v1 image/verifier integration supplies their execution context.
|
||||
self.cycles += opcode.cycles();
|
||||
return Err(self.trap(
|
||||
prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION,
|
||||
opcode as u16,
|
||||
"Instruction requires a validated Program Registry".into(),
|
||||
start_pc as u32,
|
||||
));
|
||||
let (source, target) = instr.imm_u32x2().unwrap();
|
||||
return self.execute_reference(opcode, source, target, start_pc as u32);
|
||||
}
|
||||
OpCode::FrameSync => {
|
||||
// Marks the logical end of a frame: consume cycles and signal to the driver
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
use super::*;
|
||||
use crate::value_contract::{self, ValueGroup};
|
||||
use prometeu_bytecode::registry::TypeId;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn execute_reference(
|
||||
&mut self,
|
||||
opcode: OpCode,
|
||||
source: u32,
|
||||
target: u32,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
let image = self.validated_image.clone().ok_or_else(|| {
|
||||
self.trap(
|
||||
prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION,
|
||||
opcode as u16,
|
||||
"Instruction requires a validated Program Registry".into(),
|
||||
pc,
|
||||
)
|
||||
})?;
|
||||
let registry = image.registry();
|
||||
let source = TypeId(source);
|
||||
let target = TypeId(target);
|
||||
let consumed =
|
||||
registry.type_layout(source).expect("validated source TypeId").storage.slots as usize;
|
||||
let produced: usize = if opcode == OpCode::RefToTrait { 2 } else { 1 };
|
||||
if self.temporary_slots() < consumed {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
"Incomplete reference source group".into(),
|
||||
pc,
|
||||
));
|
||||
}
|
||||
let growth = self
|
||||
.operand_stack
|
||||
.prepare_growth(produced.saturating_sub(consumed))
|
||||
.map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
let base = self.operand_stack.len() - consumed;
|
||||
let values = &self.operand_stack[base..];
|
||||
let result = match opcode {
|
||||
OpCode::RefIsExact => {
|
||||
value_contract::ref_is_exact(registry, &self.heap, source, target, values)
|
||||
.map(|yes| ValueGroup::new(&[Value::Boolean(yes)]))
|
||||
}
|
||||
OpCode::RefImplements => {
|
||||
value_contract::ref_implements(registry, &self.heap, source, target, values)
|
||||
.map(|yes| ValueGroup::new(&[Value::Boolean(yes)]))
|
||||
}
|
||||
OpCode::RefToTrait => {
|
||||
value_contract::ref_to_trait(registry, &self.heap, source, target, values)
|
||||
}
|
||||
OpCode::RefCast => {
|
||||
value_contract::ref_cast(registry, &self.heap, source, target, values)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
self.operand_stack.commit_growth(growth);
|
||||
self.operand_stack.truncate(base);
|
||||
self.operand_stack
|
||||
.extend_from_slice(result.as_slice())
|
||||
.expect("result capacity reserved before evaluation");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -290,6 +290,142 @@ fn dynamic_query_consumes_pair_and_reports_fixed_cost_through_budget_report() {
|
||||
assert_eq!(vm.operand_stack.as_slice(), &[Value::Boolean(true)]);
|
||||
}
|
||||
|
||||
fn reference_vm(operation: OpCode, source: u32, target: u32) -> VirtualMachine {
|
||||
let width = if matches!(source, 11 | 12) { 2 } else { 1 };
|
||||
let mut code = vec![];
|
||||
for slot in 0..width {
|
||||
arg(&mut code, OpCode::GetGlobal, slot);
|
||||
}
|
||||
arg(&mut code, operation, source);
|
||||
code.extend_from_slice(&target.to_le_bytes());
|
||||
op(&mut code, OpCode::Halt);
|
||||
let image = image(&code, vec![function(0, code.len(), &[], &[], 0, 2)]);
|
||||
let mut vm = VirtualMachine::from_image(image, RuntimeLimits::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
vm
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoded_cross_trait_conversion_and_concrete_cast_preserve_receiver_without_wrappers() {
|
||||
for (operation, target, expected) in [
|
||||
(OpCode::RefToTrait, 12, 1),
|
||||
(OpCode::RefCast, 9, 2),
|
||||
(OpCode::RefIsExact, 9, 3),
|
||||
(OpCode::RefImplements, 12, 3),
|
||||
] {
|
||||
let mut vm = reference_vm(operation, 11, target);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let receiver = vm.heap.allocate_typed_struct(registry, TypeId(9)).unwrap();
|
||||
let before = vm.heap.len();
|
||||
load_globals_and_step(&mut vm, &[Value::HeapRef(receiver), Value::Int64(0)]).unwrap();
|
||||
let result = match expected {
|
||||
1 => vec![Value::HeapRef(receiver), Value::Int64(1)],
|
||||
2 => vec![Value::HeapRef(receiver)],
|
||||
_ => vec![Value::Boolean(true)],
|
||||
};
|
||||
assert_eq!(vm.operand_stack.as_slice(), result.as_slice());
|
||||
assert_eq!(vm.heap.len(), before);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoded_null_queries_are_false_and_null_conversions_are_absent() {
|
||||
for (operation, target) in [
|
||||
(OpCode::RefIsExact, 9),
|
||||
(OpCode::RefImplements, 11),
|
||||
(OpCode::RefToTrait, 11),
|
||||
(OpCode::RefCast, 9),
|
||||
] {
|
||||
for (source, input) in [(9, vec![Value::Null]), (12, vec![Value::Null, Value::Null])] {
|
||||
let mut vm = reference_vm(operation, source, target);
|
||||
load_globals_and_step(&mut vm, &input).unwrap();
|
||||
let expected = match operation {
|
||||
OpCode::RefIsExact | OpCode::RefImplements => vec![Value::Boolean(false)],
|
||||
OpCode::RefToTrait => vec![Value::Null, Value::Null],
|
||||
_ => vec![Value::Null],
|
||||
};
|
||||
assert_eq!(vm.operand_stack.as_slice(), expected.as_slice());
|
||||
assert_eq!(vm.heap.len(), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoded_reference_malformed_source_never_becomes_false_or_null() {
|
||||
for (operation, target) in [
|
||||
(OpCode::RefIsExact, 9),
|
||||
(OpCode::RefImplements, 11),
|
||||
(OpCode::RefToTrait, 11),
|
||||
(OpCode::RefCast, 9),
|
||||
] {
|
||||
for input in [
|
||||
vec![Value::Null, Value::Int64(0)],
|
||||
vec![Value::HeapRef(HeapRef { index: 99, generation: 1 }), Value::Int64(0)],
|
||||
vec![Value::HeapRef(HeapRef::NULL), Value::Int64(0)],
|
||||
] {
|
||||
let mut vm = reference_vm(operation, 11, target);
|
||||
assert!(
|
||||
matches!(load_globals_and_step(&mut vm,&input),Err(LogicalFrameEndingReason::Trap(info)) if info.code==TRAP_TYPE || info.code==TRAP_INVALID_HANDLE)
|
||||
);
|
||||
assert_eq!(vm.operand_stack.as_slice(), input.as_slice());
|
||||
}
|
||||
let mut vm = reference_vm(operation, 11, target);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let receiver = vm.heap.allocate_typed_struct(registry, TypeId(9)).unwrap();
|
||||
let input = [Value::HeapRef(receiver), Value::Int64(1)]; // Implementation 1 belongs to trait 12, not source trait 11.
|
||||
assert!(
|
||||
matches!(load_globals_and_step(&mut vm,&input),Err(LogicalFrameEndingReason::Trap(info)) if info.code==TRAP_TYPE)
|
||||
);
|
||||
assert_eq!(vm.operand_stack.as_slice(), &input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoded_array_identity_is_invariant_and_arrays_have_no_trait_implementation() {
|
||||
for (operation, target, expected) in [
|
||||
(OpCode::RefIsExact, 13, true),
|
||||
(OpCode::RefIsExact, 14, false),
|
||||
(OpCode::RefImplements, 11, false),
|
||||
] {
|
||||
let mut vm = reference_vm(operation, 13, target);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let array = vm.heap.allocate_typed_array(registry, TypeId(13), 3).unwrap();
|
||||
load_globals_and_step(&mut vm, &[Value::HeapRef(array)]).unwrap();
|
||||
assert_eq!(vm.operand_stack.as_slice(), &[Value::Boolean(expected)]);
|
||||
}
|
||||
for (operation, target) in [(OpCode::RefToTrait, 11), (OpCode::RefCast, 14)] {
|
||||
let mut vm = reference_vm(operation, 13, target);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let array = vm.heap.allocate_typed_array(registry, TypeId(13), 3).unwrap();
|
||||
let input = [Value::HeapRef(array)];
|
||||
assert!(
|
||||
matches!(load_globals_and_step(&mut vm,&input),Err(LogicalFrameEndingReason::Trap(info)) if info.code==TRAP_TYPE)
|
||||
);
|
||||
assert_eq!(vm.operand_stack.as_slice(), &input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoded_stale_handle_is_an_invalid_handle_even_when_target_type_differs() {
|
||||
for (operation, target) in [
|
||||
(OpCode::RefIsExact, 10),
|
||||
(OpCode::RefImplements, 12),
|
||||
(OpCode::RefToTrait, 12),
|
||||
(OpCode::RefCast, 10),
|
||||
] {
|
||||
let mut vm = reference_vm(operation, 9, target);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let receiver = vm.heap.allocate_typed_struct(registry, TypeId(9)).unwrap();
|
||||
vm.heap.mark_from_roots(vm.current_coro).unwrap();
|
||||
vm.heap.sweep();
|
||||
let input = [Value::HeapRef(receiver)];
|
||||
assert!(
|
||||
matches!(load_globals_and_step(&mut vm,&input),Err(LogicalFrameEndingReason::Trap(info)) if info.code==TRAP_INVALID_HANDLE)
|
||||
);
|
||||
assert_eq!(vm.operand_stack.as_slice(), &input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_sessions_share_image_but_have_independent_execution_and_heap() {
|
||||
let image = image(&[1, 0], vec![function(0, 2, &[], &[], 0, 0)]);
|
||||
|
||||
Reference in New Issue
Block a user