feat(vm): publish typed fields across multiple structs atomically
Add schema-driven FIELDS_PUBLISH without collection-specific runtime behavior. Validate every nullable destination and complete field group before allocation-free publication, with neutral ownership, malformed-input and quota coverage.
This commit is contained in:
@@ -87,6 +87,17 @@ fn validate_inner(
|
||||
},
|
||||
)?;
|
||||
}
|
||||
if instruction.opcode == Op::FieldsPublish {
|
||||
if let Some(declaration) =
|
||||
registry.declarations().types.get(instruction.imm_u32().unwrap() as usize)
|
||||
{
|
||||
if let prometeu_bytecode::registry::TypeKind::Struct(entries) =
|
||||
&declaration.kind
|
||||
{
|
||||
budget.work(entries.len())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
instruction
|
||||
.registry_stack_effect_in(registry, FunctionId(function_id))
|
||||
.map_err(|e| contextual(e, start + pc))?;
|
||||
|
||||
@@ -709,6 +709,27 @@ fn check(
|
||||
require(working[end - 2], I64, 0, registry, budget)?;
|
||||
require(working[end - 1], I64, 1, registry, budget)?;
|
||||
}
|
||||
Op::FieldsPublish => {
|
||||
let schema = TypeId(instruction.imm_u32().unwrap());
|
||||
let effect = instruction.registry_stack_effect(registry)?.unwrap();
|
||||
let mut at = end - effect.pops as usize;
|
||||
let TypeKind::Struct(entries) = ®istry.declarations().types[schema.0 as usize].kind
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
for entry in entries {
|
||||
budget.work(1)?;
|
||||
check_group(&working[at..], entry.type_id, registry, budget)?;
|
||||
at += 1;
|
||||
for field in
|
||||
®istry.type_layout(entry.type_id).unwrap().instance.as_ref().unwrap().fields
|
||||
{
|
||||
budget.work(1)?;
|
||||
check_group(&working[at..], field.type_id, registry, budget)?;
|
||||
at += registry.type_layout(field.type_id).unwrap().storage.slots as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::ArrayCopy | Op::StoragePublish => {
|
||||
let [a, o, f, s] = if instruction.opcode == Op::StoragePublish {
|
||||
instruction.imm_u32x4().unwrap()
|
||||
|
||||
@@ -1086,6 +1086,9 @@ impl VirtualMachine {
|
||||
let (source, target) = instr.imm_u32x2().unwrap();
|
||||
return self.execute_reference(opcode, source, target, start_pc as u32);
|
||||
}
|
||||
OpCode::FieldsPublish => {
|
||||
return self.execute_fields_publication(instr.imm_u32().unwrap(), start_pc as u32);
|
||||
}
|
||||
OpCode::IndexGuard
|
||||
| OpCode::ArrayCheckLength
|
||||
| OpCode::ArrayCopy
|
||||
|
||||
@@ -180,3 +180,107 @@ impl VirtualMachine {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtualMachine {
|
||||
/// A Struct schema lists destination Struct types, without allocating a request.
|
||||
/// Inputs stay rooted and unconsumed until validation of the entire attempt.
|
||||
pub(super) fn execute_fields_publication(
|
||||
&mut self,
|
||||
schema: u32,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
let opcode = OpCode::FieldsPublish;
|
||||
self.cycles += opcode.cycles();
|
||||
let image = self.validated_image.clone().expect("validated Registry operation");
|
||||
let registry = image.registry();
|
||||
let prometeu_bytecode::registry::TypeKind::Struct(entries) =
|
||||
®istry.declarations().types[schema as usize].kind
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
let consumed: usize = entries
|
||||
.iter()
|
||||
.map(|entry| {
|
||||
1 + registry
|
||||
.type_layout(entry.type_id)
|
||||
.unwrap()
|
||||
.instance
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| registry.type_layout(f.type_id).unwrap().storage.slots as usize)
|
||||
.sum::<usize>()
|
||||
})
|
||||
.sum();
|
||||
if self.temporary_slots() < consumed {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
"Incomplete field publication operands".into(),
|
||||
pc,
|
||||
));
|
||||
}
|
||||
self.cycles += consumed as u64;
|
||||
let base = self.operand_stack.len() - consumed;
|
||||
let mut at = base;
|
||||
for entry in entries {
|
||||
match self.operand_stack[at] {
|
||||
Value::Null => {}
|
||||
Value::HeapRef(receiver) => {
|
||||
if self
|
||||
.heap
|
||||
.concrete_type(receiver, registry)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?
|
||||
!= entry.type_id
|
||||
{
|
||||
return Err(self.value_trap(ValueError::Type, opcode, pc));
|
||||
}
|
||||
}
|
||||
_ => return Err(self.value_trap(ValueError::Type, opcode, pc)),
|
||||
}
|
||||
at += 1;
|
||||
for field in
|
||||
®istry.type_layout(entry.type_id).unwrap().instance.as_ref().unwrap().fields
|
||||
{
|
||||
let width = registry.type_layout(field.type_id).unwrap().storage.slots as usize;
|
||||
value_contract::validate(
|
||||
registry,
|
||||
&self.heap,
|
||||
field.type_id,
|
||||
&self.operand_stack[at..at + width],
|
||||
)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
at += width;
|
||||
}
|
||||
}
|
||||
// No allocation, provenance lookup, validation, callback, GC or scheduler
|
||||
// boundary follows. Repeated destinations use ordered last-write semantics.
|
||||
at = base;
|
||||
for entry in entries {
|
||||
let receiver = match self.operand_stack[at] {
|
||||
Value::HeapRef(r) => Some(r),
|
||||
_ => None,
|
||||
};
|
||||
at += 1;
|
||||
for (index, field) in registry
|
||||
.type_layout(entry.type_id)
|
||||
.unwrap()
|
||||
.instance
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
{
|
||||
let width = registry.type_layout(field.type_id).unwrap().storage.slots as usize;
|
||||
if let Some(receiver) = receiver {
|
||||
self.heap.commit_typed(receiver, index, &self.operand_stack[at..at + width]);
|
||||
}
|
||||
at += width;
|
||||
}
|
||||
}
|
||||
self.operand_stack.truncate(base);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,3 +222,157 @@ fn dead_invalid_operands_and_split_typed_groups_are_rejected() {
|
||||
support::load(&assemble("PUSH_BOOL 1\nPUSH_I64 3\nINDEX_GUARD\nHALT").unwrap()).is_err()
|
||||
);
|
||||
}
|
||||
|
||||
fn fields_machine() -> VirtualMachine {
|
||||
use prometeu_bytecode::registry::{NamedType, TypeDecl, TypeKind};
|
||||
let mut code = String::new();
|
||||
for i in 0..12 {
|
||||
code += &format!("GET_GLOBAL {i}\n");
|
||||
}
|
||||
code += "FIELDS_PUBLISH 27\nHALT";
|
||||
let code = assemble(&code).unwrap();
|
||||
let mut declarations = support::declarations(code.len());
|
||||
assert_eq!(declarations.types.len(), 26);
|
||||
let field = |id| NamedType { type_id: TypeId(id), name: None };
|
||||
declarations.types.push(TypeDecl {
|
||||
name: None,
|
||||
kind: TypeKind::Struct([0, 5, 4, 9].into_iter().map(field).collect()),
|
||||
});
|
||||
declarations.types.push(TypeDecl {
|
||||
name: None,
|
||||
kind: TypeKind::Struct([26, 26].into_iter().map(field).collect()),
|
||||
});
|
||||
let image = crate::ValidatedProgramImage::load(
|
||||
&support::bytes_with(&code, declarations),
|
||||
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
|
||||
prometeu_hal::syscalls::caps::NONE,
|
||||
)
|
||||
.unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, RuntimeLimits::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
vm
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_publication_is_complete_preserves_native_and_reference_groups_and_needs_no_scratch() {
|
||||
for alias in [false, true] {
|
||||
let mut vm = fields_machine();
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let first = vm.heap.allocate_typed_struct(registry.clone(), TypeId(26)).unwrap();
|
||||
let second = if alias {
|
||||
first
|
||||
} else {
|
||||
vm.heap.allocate_typed_struct(registry.clone(), TypeId(26)).unwrap()
|
||||
};
|
||||
let payload = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap();
|
||||
let text: std::sync::Arc<str> = "owned".into();
|
||||
let inputs = [
|
||||
Value::HeapRef(first),
|
||||
Value::Int32(1),
|
||||
Value::Float(2.),
|
||||
Value::Float(3.),
|
||||
Value::String(text.clone()),
|
||||
Value::HeapRef(payload),
|
||||
Value::HeapRef(second),
|
||||
Value::Int32(42),
|
||||
Value::Float(20.),
|
||||
Value::Float(22.),
|
||||
Value::String(text.clone()),
|
||||
Value::HeapRef(payload),
|
||||
];
|
||||
operands(&mut vm, &inputs);
|
||||
for resource in [
|
||||
RuntimeResource::GuestBytes,
|
||||
RuntimeResource::HandleSlots,
|
||||
RuntimeResource::TemporaryBytes,
|
||||
RuntimeResource::AuxiliaryBytes,
|
||||
] {
|
||||
vm.budget.test_lower_limit(resource, vm.budget.used(resource));
|
||||
}
|
||||
let count = vm.heap.allocation_count();
|
||||
let cycles = vm.cycles;
|
||||
step(&mut vm).unwrap();
|
||||
assert_eq!(vm.cycles - cycles, 18);
|
||||
assert_eq!(count, vm.heap.allocation_count());
|
||||
assert!(vm.operand_stack.is_empty());
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, first, 0).unwrap().as_slice(),
|
||||
[Value::Int32(if alias { 42 } else { 1 })]
|
||||
);
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, second, 1).unwrap().as_slice(),
|
||||
[Value::Float(20.), Value::Float(22.)]
|
||||
);
|
||||
let native = vm.heap.load_typed(®istry, first, 2).unwrap();
|
||||
let Value::String(stored) = &native.as_slice()[0] else { panic!() };
|
||||
assert!(std::sync::Arc::ptr_eq(stored, &text));
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, first, 3).unwrap().as_slice(),
|
||||
[Value::HeapRef(payload)]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_publication_checks_late_groups_and_skipped_destinations_before_any_write() {
|
||||
for skipped in [false, true] {
|
||||
let mut vm = fields_machine();
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let first = vm.heap.allocate_typed_struct(registry.clone(), TypeId(26)).unwrap();
|
||||
let second = vm.heap.allocate_typed_struct(registry.clone(), TypeId(26)).unwrap();
|
||||
let inputs = [
|
||||
Value::HeapRef(first),
|
||||
Value::Int32(42),
|
||||
Value::Float(1.),
|
||||
Value::Float(2.),
|
||||
Value::Null,
|
||||
Value::Null,
|
||||
if skipped { Value::Null } else { Value::HeapRef(second) },
|
||||
Value::Int32(7),
|
||||
Value::Float(3.),
|
||||
Value::Float(4.),
|
||||
Value::Boolean(true),
|
||||
Value::Null,
|
||||
];
|
||||
operands(&mut vm, &inputs);
|
||||
fault(step(&mut vm), TRAP_TYPE);
|
||||
assert_eq!(vm.operand_stack.as_slice(), inputs);
|
||||
for owner in [first, second] {
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(),
|
||||
[Value::Int32(0)]
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut vm = fields_machine();
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let first = vm.heap.allocate_typed_struct(registry.clone(), TypeId(26)).unwrap();
|
||||
let inputs = [
|
||||
Value::HeapRef(first),
|
||||
Value::Int32(42),
|
||||
Value::Float(1.),
|
||||
Value::Float(2.),
|
||||
Value::Null,
|
||||
Value::Null,
|
||||
Value::Null,
|
||||
Value::Int32(7),
|
||||
Value::Float(3.),
|
||||
Value::Float(4.),
|
||||
Value::Null,
|
||||
Value::Null,
|
||||
];
|
||||
operands(&mut vm, &inputs);
|
||||
step(&mut vm).unwrap();
|
||||
assert_eq!(vm.heap.load_typed(®istry, first, 0).unwrap().as_slice(), [Value::Int32(42)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_publication_rejects_bad_schemas_dead_operands_and_truncation() {
|
||||
for text in ["HALT\nFIELDS_PUBLISH 0", "HALT\nFIELDS_PUBLISH 99", "HALT\nFIELDS_PUBLISH 9"] {
|
||||
assert!(support::load(&assemble(text).unwrap()).is_err(), "{text}");
|
||||
}
|
||||
let bytes = assemble("FIELDS_PUBLISH 26").unwrap();
|
||||
for end in 2..bytes.len() {
|
||||
assert!(prometeu_bytecode::decode_next(0, &bytes[..end]).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user