feat(vm): add typed array and struct guest operations
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Add seven Registry-backed collection opcodes with bounded verification, complete value groups, transactional publication and exact attempt costs. Publish ISA, memory and cycle contracts, producer handoff and the completed lesson. Validate with 925 workspace tests, optimized collection suites and all eight coverage domain gates. Make headless fixture directory names unique for parallel tests.
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
//! Whole-group collection attempts: charge, validate, reserve, prepare, commit.
|
||||
use super::*;
|
||||
use crate::resource_budget::ResourceReason;
|
||||
use crate::value_contract::{self, ValueError, ValueGroup};
|
||||
use prometeu_bytecode::registry::TypeId;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn execute_collection(
|
||||
&mut self,
|
||||
opcode: OpCode,
|
||||
id: u32,
|
||||
field: 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 ty = TypeId(id);
|
||||
let layout = registry.type_layout(ty).expect("validated collection type");
|
||||
let is_array = layout.array.is_some();
|
||||
let value_ty = if let Some(array) = layout.array {
|
||||
Some(array.element_type_id)
|
||||
} else if matches!(opcode, OpCode::StructGet | OpCode::StructSet) {
|
||||
Some(layout.instance.as_ref().unwrap().fields[field as usize].type_id)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let width =
|
||||
value_ty.map(|t| registry.type_layout(t).unwrap().storage.slots as usize).unwrap_or(0);
|
||||
let (consumed, produced) = match opcode {
|
||||
OpCode::StructNew => (0, 1),
|
||||
OpCode::ArrayNew | OpCode::ArrayLen => (1, 1),
|
||||
OpCode::StructGet => (1, width),
|
||||
OpCode::ArrayGet => (2, width),
|
||||
OpCode::StructSet => (1 + width, 0),
|
||||
OpCode::ArraySet => (2 + width, 0),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let available = self.temporary_slots() >= consumed;
|
||||
let base = self.operand_stack.len().saturating_sub(consumed);
|
||||
let length = if opcode == OpCode::ArrayNew && available {
|
||||
match self.operand_stack[base] {
|
||||
Value::Int64(n) => Some(n),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let work = match opcode {
|
||||
OpCode::StructNew => layout.instance.as_ref().unwrap().initialization_slots,
|
||||
OpCode::ArrayNew => length
|
||||
.and_then(|n| u32::try_from(n).ok())
|
||||
.map(|n| {
|
||||
u64::from(n)
|
||||
.checked_mul(width as u64)
|
||||
.ok_or_else(|| self.collection_size_trap(opcode, pc, u64::MAX))
|
||||
})
|
||||
.transpose()?
|
||||
.unwrap_or(0),
|
||||
OpCode::StructGet | OpCode::ArrayGet | OpCode::StructSet | OpCode::ArraySet => {
|
||||
width as u64
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
let cost = opcode
|
||||
.cycles()
|
||||
.checked_add(work)
|
||||
.ok_or_else(|| self.collection_size_trap(opcode, pc, u64::MAX))?;
|
||||
self.cycles += cost;
|
||||
if !available {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
"Incomplete collection operands".into(),
|
||||
pc,
|
||||
));
|
||||
}
|
||||
let mut receiver = None;
|
||||
let mut index = field as usize;
|
||||
if !matches!(opcode, OpCode::StructNew | OpCode::ArrayNew) {
|
||||
let r = match self.operand_stack[base] {
|
||||
Value::Null => return Err(self.value_trap(ValueError::NullReference, opcode, pc)),
|
||||
Value::HeapRef(r) => r,
|
||||
_ => return Err(self.value_trap(ValueError::Type, opcode, pc)),
|
||||
};
|
||||
let actual =
|
||||
self.heap.concrete_type(r, registry).map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
if actual != ty {
|
||||
return Err(self.value_trap(ValueError::Type, opcode, pc));
|
||||
}
|
||||
receiver = Some(r);
|
||||
if matches!(opcode, OpCode::ArrayGet | OpCode::ArraySet) {
|
||||
let n = match self.operand_stack[base + 1] {
|
||||
Value::Int64(n) => n,
|
||||
_ => return Err(self.value_trap(ValueError::Type, opcode, pc)),
|
||||
};
|
||||
let len = self
|
||||
.heap
|
||||
.typed_array_len(r, registry)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
index = usize::try_from(n)
|
||||
.ok()
|
||||
.filter(|n| *n < len)
|
||||
.ok_or_else(|| self.value_trap(ValueError::Bounds, opcode, pc))?;
|
||||
}
|
||||
}
|
||||
let count = if opcode == OpCode::ArrayNew {
|
||||
let n = length.ok_or_else(|| self.value_trap(ValueError::Type, opcode, pc))?;
|
||||
if n < 0 {
|
||||
return Err(self.value_trap(ValueError::Bounds, opcode, pc));
|
||||
}
|
||||
u32::try_from(n).map_err(|_| self.collection_size_trap(opcode, pc, n as u64))?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let set = matches!(opcode, OpCode::StructSet | OpCode::ArraySet);
|
||||
let source = base + 1 + usize::from(is_array);
|
||||
if set {
|
||||
value_contract::validate(
|
||||
registry,
|
||||
&self.heap,
|
||||
value_ty.unwrap(),
|
||||
&self.operand_stack[source..],
|
||||
)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
}
|
||||
let growth = self
|
||||
.operand_stack
|
||||
.prepare_growth(produced.saturating_sub(consumed))
|
||||
.map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
let result = match opcode {
|
||||
OpCode::StructNew => {
|
||||
let r = self
|
||||
.heap
|
||||
.allocate_typed_struct(registry.clone(), ty)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
ValueGroup::new(&[Value::HeapRef(r)])
|
||||
}
|
||||
OpCode::ArrayNew => {
|
||||
let r = self
|
||||
.heap
|
||||
.allocate_typed_array(registry.clone(), ty, count)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
ValueGroup::new(&[Value::HeapRef(r)])
|
||||
}
|
||||
OpCode::ArrayLen => {
|
||||
let len = self
|
||||
.heap
|
||||
.typed_array_len(receiver.unwrap(), registry)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
ValueGroup::new(&[Value::Int64(len as i64)])
|
||||
}
|
||||
OpCode::StructGet | OpCode::ArrayGet => self
|
||||
.heap
|
||||
.load_typed(registry, receiver.unwrap(), index)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?,
|
||||
OpCode::StructSet | OpCode::ArraySet => {
|
||||
self.heap
|
||||
.store_typed(registry, receiver.unwrap(), index, &self.operand_stack[source..])
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
ValueGroup::new(&[])
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
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 publication");
|
||||
Ok(())
|
||||
}
|
||||
fn collection_size_trap(
|
||||
&self,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
requested: u64,
|
||||
) -> LogicalFrameEndingReason {
|
||||
self.resource_trap(
|
||||
self.budget.failure(
|
||||
RuntimeResource::GuestBytes,
|
||||
ResourceReason::SizeOverflow,
|
||||
requested,
|
||||
),
|
||||
opcode,
|
||||
pc,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user