diff --git a/crates/console/prometeu-bytecode/src/assembler.rs b/crates/console/prometeu-bytecode/src/assembler.rs index 97282e6..44f0fbf 100644 --- a/crates/console/prometeu-bytecode/src/assembler.rs +++ b/crates/console/prometeu-bytecode/src/assembler.rs @@ -336,22 +336,6 @@ pub fn assemble(src: &str) -> Result, AsmError> { emit_u32(parse_u32_any(ops)?, &mut out); } - "INDEX_GUARD" => { - if !ops.is_empty() { - return Err(AsmError::InvalidOperand(line.into())); - } - emit_u16(CoreOpCode::IndexGuard as u16, &mut out); - } - "STORAGE_PUBLISH" => { - let parts: Vec<_> = ops.split(',').map(str::trim).collect(); - if parts.len() != 4 { - return Err(AsmError::InvalidOperand(line.into())); - } - emit_u16(CoreOpCode::StoragePublish as u16, &mut out); - for part in parts { - emit_u32(parse_u32_any(part)?, &mut out); - } - } "STRUCT_GET" | "STRUCT_SET" => { let (a, b) = parse_keyvals(ops)?; let (ty, field) = parse_pair(a, "type", b, "field")?; @@ -361,18 +345,15 @@ pub fn assemble(src: &str) -> Result, AsmError> { emit_u32(ty, &mut out); emit_u32(field, &mut out); } - "STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" - | "ARRAY_CHECK_LENGTH" | "ARRAY_COPY" | "FIELDS_PUBLISH" => { + "STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" | "ARRAY_COPY" => { if ops.is_empty() { return Err(AsmError::MissingOperand(line.into())); } let op = match mn { - "FIELDS_PUBLISH" => CoreOpCode::FieldsPublish, "STRUCT_NEW" => CoreOpCode::StructNew, "ARRAY_NEW" => CoreOpCode::ArrayNew, "ARRAY_LEN" => CoreOpCode::ArrayLen, "ARRAY_GET" => CoreOpCode::ArrayGet, - "ARRAY_CHECK_LENGTH" => CoreOpCode::ArrayCheckLength, "ARRAY_COPY" => CoreOpCode::ArrayCopy, _ => CoreOpCode::ArraySet, }; diff --git a/crates/console/prometeu-bytecode/src/decoder.rs b/crates/console/prometeu-bytecode/src/decoder.rs index 3b8d12f..26e8293 100644 --- a/crates/console/prometeu-bytecode/src/decoder.rs +++ b/crates/console/prometeu-bytecode/src/decoder.rs @@ -76,14 +76,6 @@ impl<'a> DecodedInstr<'a> { Ok(f64::from_le_bytes(self.imm.try_into().unwrap())) } - /// Four independent u32 operands; widths are resolved through the Registry. - pub fn imm_u32x4(&self) -> Result<[u32; 4], DecodeError> { - self.ensure_len(16)?; - Ok(std::array::from_fn(|i| { - u32::from_le_bytes(self.imm[i * 4..i * 4 + 4].try_into().unwrap()) - })) - } - /// Helper for opcodes carrying two u32 values packed in 8 bytes (e.g., ALLOC meta). #[inline] pub fn imm_u32x2(&self) -> Result<(u32, u32), DecodeError> { diff --git a/crates/console/prometeu-bytecode/src/disassembler.rs b/crates/console/prometeu-bytecode/src/disassembler.rs index f8ffecb..65f5ff6 100644 --- a/crates/console/prometeu-bytecode/src/disassembler.rs +++ b/crates/console/prometeu-bytecode/src/disassembler.rs @@ -72,18 +72,10 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String { | CoreOpCode::ArrayLen | CoreOpCode::ArrayGet | CoreOpCode::ArraySet - | CoreOpCode::ArrayCheckLength - | CoreOpCode::ArrayCopy - | CoreOpCode::FieldsPublish => { + | CoreOpCode::ArrayCopy => { let v = u32::from_le_bytes(imm.try_into().unwrap()); format!("{}", v) } - CoreOpCode::StoragePublish => { - let fields: [u32; 4] = std::array::from_fn(|i| { - u32::from_le_bytes(imm[i * 4..i * 4 + 4].try_into().unwrap()) - }); - format!("{}, {}, {}, {}", fields[0], fields[1], fields[2], fields[3]) - } CoreOpCode::StructGet | CoreOpCode::StructSet => { let ty = u32::from_le_bytes(imm[..4].try_into().unwrap()); let field = u32::from_le_bytes(imm[4..].try_into().unwrap()); @@ -159,21 +151,3 @@ pub fn disassemble(bytes: &[u8]) -> Result { } Ok(out.join("\n")) } - -#[cfg(test)] -mod storage_instruction_tests { - #[test] - fn storage_operands_roundtrip_and_quad_immediates_never_decode_truncated() { - let code = crate::assemble( - "INDEX_GUARD\nARRAY_CHECK_LENGTH 7\nARRAY_COPY 8\nSTORAGE_PUBLISH 8, 9, 0, 1", - ) - .unwrap(); - let text = super::disassemble(&code).unwrap(); - assert_eq!(crate::assemble(&text).unwrap(), code); - let pc = 14; - for end in pc + 2..code.len() { - assert!(crate::decode_next(pc, &code[..end]).is_err()); - } - assert_eq!(crate::decode_next(pc, &code).unwrap().imm_u32x4().unwrap(), [8, 9, 0, 1]); - } -} diff --git a/crates/console/prometeu-bytecode/src/opcode.rs b/crates/console/prometeu-bytecode/src/opcode.rs index 9706952..bebe4d1 100644 --- a/crates/console/prometeu-bytecode/src/opcode.rs +++ b/crates/console/prometeu-bytecode/src/opcode.rs @@ -205,17 +205,8 @@ pub enum OpCode { ArrayLen = 0xA4, ArrayGet = 0xA5, ArraySet = 0xA6, - /// Signed logical index guard; stack [index:i64, exclusive_limit:i64] -> []. - IndexGuard = 0xA7, - /// Structural Array length check without allocation; TypeId:u32. - ArrayCheckLength = 0xA8, /// Complete stored groups, overlap-safe; Array TypeId:u32. ArrayCopy = 0xA9, - /// Move/fill array ranges and two selected Struct fields in one attempt. - /// Array TypeId, Struct TypeId, first field, second field (four u32). - StoragePublish = 0xAA, - /// Atomic full-field writes to schema-selected nullable Struct destinations. - FieldsPublish = 0xAB, } impl TryFrom for OpCode { @@ -291,11 +282,7 @@ impl TryFrom for OpCode { 0xA4 => Ok(OpCode::ArrayLen), 0xA5 => Ok(OpCode::ArrayGet), 0xA6 => Ok(OpCode::ArraySet), - 0xA7 => Ok(OpCode::IndexGuard), - 0xA8 => Ok(OpCode::ArrayCheckLength), 0xA9 => Ok(OpCode::ArrayCopy), - 0xAA => Ok(OpCode::StoragePublish), - 0xAB => Ok(OpCode::FieldsPublish), _ => Err(format!("Invalid OpCode: 0x{:04X}", value)), } @@ -375,9 +362,7 @@ impl OpCode { OpCode::ArrayLen => 3, OpCode::ArrayGet => 3, OpCode::ArraySet => 4, - OpCode::IndexGuard | OpCode::ArrayCheckLength => 2, OpCode::ArrayCopy => 4, - OpCode::StoragePublish | OpCode::FieldsPublish => 6, } } } diff --git a/crates/console/prometeu-bytecode/src/opcode_spec.rs b/crates/console/prometeu-bytecode/src/opcode_spec.rs index f98292a..8f757c9 100644 --- a/crates/console/prometeu-bytecode/src/opcode_spec.rs +++ b/crates/console/prometeu-bytecode/src/opcode_spec.rs @@ -686,27 +686,9 @@ impl OpCodeSpecExt for OpCode { may_trap: true, is_safepoint: false, }, - OpCode::IndexGuard => OpcodeSpec { - name: "INDEX_GUARD", - imm_bytes: 0, - pops: Some(2), - pushes: Some(0), - is_branch: false, - is_terminator: false, - may_trap: true, - is_safepoint: false, - }, - OpCode::ArrayCheckLength - | OpCode::ArrayCopy - | OpCode::StoragePublish - | OpCode::FieldsPublish => OpcodeSpec { - name: match self { - OpCode::ArrayCheckLength => "ARRAY_CHECK_LENGTH", - OpCode::ArrayCopy => "ARRAY_COPY", - OpCode::FieldsPublish => "FIELDS_PUBLISH", - _ => "STORAGE_PUBLISH", - }, - imm_bytes: if *self == OpCode::StoragePublish { 16 } else { 4 }, + OpCode::ArrayCopy => OpcodeSpec { + name: "ARRAY_COPY", + imm_bytes: 4, pops: None, pushes: None, is_branch: false, diff --git a/crates/console/prometeu-bytecode/src/registry_instruction.rs b/crates/console/prometeu-bytecode/src/registry_instruction.rs index 4433bd5..a751525 100644 --- a/crates/console/prometeu-bytecode/src/registry_instruction.rs +++ b/crates/console/prometeu-bytecode/src/registry_instruction.rs @@ -150,59 +150,11 @@ impl DecodedInstr<'_> { } (pops, 1) } - Op::FieldsPublish => { - let schema = self.imm_u32().map_err(|_| malformed())?; - let (decl, _) = type_info(schema)?; - let TypeKind::Struct(entries) = &decl.kind else { - return Err(bad_category(schema)); - }; - let mut slots = 0u32; - for entry in entries { - let (owner, layout) = type_info(entry.type_id.0)?; - if !matches!(owner.kind, TypeKind::Struct(_)) { - return Err(bad_category(entry.type_id.0)); - } - let fields = - u32::try_from(layout.instance.as_ref().unwrap().initialization_slots) - .map_err(|_| malformed())?; - slots = slots - .checked_add(1) - .and_then(|n| n.checked_add(fields)) - .ok_or_else(malformed)?; - } - (slots, 0) - } - Op::ArrayCheckLength | Op::ArrayCopy | Op::StoragePublish => { - let (array, owner, first, second) = if self.opcode == Op::StoragePublish { - let [a, o, f, s] = self.imm_u32x4().map_err(|_| malformed())?; - (a, o, f, s) - } else { - (self.imm_u32().map_err(|_| malformed())?, 0, 0, 0) - }; + Op::ArrayCopy => { + let array = self.imm_u32().map_err(|_| malformed())?; let (_, layout) = type_info(array)?; - let element = layout.array.ok_or_else(|| bad_category(array))?.element_type_id; - let width = registry.type_layout(element).unwrap().storage.slots; - match self.opcode { - Op::ArrayCheckLength => (1, 0), - Op::ArrayCopy => (5, 0), - _ => { - let (_, layout) = type_info(owner)?; - let instance = - layout.instance.as_ref().ok_or_else(|| bad_category(owner))?; - if first == second { - return Err(bad_category(owner).member(second)); - } - let mut slots = 8 + width; - for field in [first, second] { - let f = instance - .fields - .get(field as usize) - .ok_or_else(|| bad_category(owner).member(field))?; - slots += registry.type_layout(f.type_id).unwrap().storage.slots; - } - (slots, 0) - } - } + layout.array.ok_or_else(|| bad_category(array))?; + (5, 0) } Op::StructNew | Op::StructGet diff --git a/crates/console/prometeu-bytecode/tests/registry_isa.rs b/crates/console/prometeu-bytecode/tests/registry_isa.rs index 4a14ee2..db4d21e 100644 --- a/crates/console/prometeu-bytecode/tests/registry_isa.rs +++ b/crates/console/prometeu-bytecode/tests/registry_isa.rs @@ -215,3 +215,62 @@ fn collection_effects_and_cached_initialization_width_are_exact() { ); } } + +#[test] +fn removed_storage_encodings_and_mnemonics_are_unsupported() { + for opcode in [0xa7u16, 0xa8, 0xaa, 0xab] { + assert!(Op::try_from(opcode).is_err()); + let bytes = opcode.to_le_bytes(); + assert!( + matches!(decode_next(0,&bytes),Err(DecodeError::UnknownOpcode{pc:0,opcode: actual}) if actual == opcode) + ); + assert!(disassemble(&bytes).is_err()); + } + for text in + ["INDEX_GUARD", "ARRAY_CHECK_LENGTH 1", "STORAGE_PUBLISH 1,2,0,1", "FIELDS_PUBLISH 1"] + { + assert!(assemble(text).is_err(), "{text}"); + } +} + +#[test] +fn range_copy_encoding_effects_and_registry_categories_are_independent_oracles() { + let bytes = [0xa9, 0, 4, 3, 2, 1]; + assert_eq!(assemble("ARRAY_COPY 16909060").unwrap(), bytes); + assert_eq!(disassemble(&bytes).unwrap(), "ARRAY_COPY 16909060"); + let instruction = decode_next(0, &bytes).unwrap(); + assert_eq!(instruction.opcode, Op::ArrayCopy); + assert_eq!(instruction.opcode.cycles(), 4); + let spec = instruction.opcode.spec(); + assert_eq!((spec.imm_bytes, spec.pops, spec.pushes), (4, None, None)); + assert!(instruction.opcode.requires_registry() && spec.may_trap); + assert!(!spec.is_safepoint && !spec.is_branch && !spec.is_terminator); + for end in 0..6 { + assert!(decode_next(0, &bytes[..end]).is_err()); + } + let registry = registry(); + let bytes = assemble("ARRAY_COPY 5").unwrap(); + assert_eq!( + decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap(), + Some(RegistryStackEffect { pops: 5, pushes: 0 }) + ); + for (id, reason) in [ + (0, LoadReason::Category), + (4, LoadReason::Category), + (6, LoadReason::Category), + (99, LoadReason::TypeId), + ] { + let bytes = assemble(&format!("ARRAY_COPY {id}")).unwrap(); + let error = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err(); + assert_eq!( + (error.code, error.reason, error.pc), + (LoadCode::InvalidReference, reason, Some(0)) + ); + } + for text in ["ARRAY_COPY", "ARRAY_COPY -1", "ARRAY_COPY 4294967296", "ARRAY_COPY 1 2"] { + assert!(assemble(text).is_err()); + } + // These are legal immediate bytes, not instruction boundaries. + let bytes = assemble("PUSH_I64 18735214094965159\nHALT").unwrap(); + assert!(disassemble(&bytes).is_ok()); +} diff --git a/crates/console/prometeu-vm/src/registry_verifier.rs b/crates/console/prometeu-vm/src/registry_verifier.rs index d6a0943..5531874 100644 --- a/crates/console/prometeu-vm/src/registry_verifier.rs +++ b/crates/console/prometeu-vm/src/registry_verifier.rs @@ -87,17 +87,6 @@ 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))?; diff --git a/crates/console/prometeu-vm/src/registry_verifier/types.rs b/crates/console/prometeu-vm/src/registry_verifier/types.rs index de32346..e2b577c 100644 --- a/crates/console/prometeu-vm/src/registry_verifier/types.rs +++ b/crates/console/prometeu-vm/src/registry_verifier/types.rs @@ -709,64 +709,15 @@ fn check( base += registry.type_layout(parameter.type_id).unwrap().storage.slots as usize; } } - Op::ArrayNew | Op::ArrayCheckLength => require(working[end - 1], I64, 0, registry, budget)?, - Op::IndexGuard => { - 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() - } else { - [instruction.imm_u32().unwrap(), 0, 0, 0] - }; - let effect = instruction.registry_stack_effect_in(registry, FunctionId(id))?.unwrap(); - let base = end - effect.pops as usize; - check_group(&working[base..base + 1], TypeId(a), registry, budget)?; - check_group(&working[base + 2..base + 3], TypeId(a), registry, budget)?; + Op::ArrayNew => require(working[end - 1], I64, 0, registry, budget)?, + Op::ArrayCopy => { + let array = TypeId(instruction.imm_u32().unwrap()); + let base = end - 5; + check_group(&working[base..base + 1], array, registry, budget)?; + check_group(&working[base + 2..base + 3], array, registry, budget)?; for i in [1, 3, 4] { require(working[base + i], I64, i, registry, budget)?; } - if instruction.opcode == Op::StoragePublish { - for i in [5, 6] { - require(working[base + i], I64, i, registry, budget)?; - } - let element = - registry.type_layout(TypeId(a)).unwrap().array.unwrap().element_type_id; - let mut at = base + 7; - check_group(&working[at..], element, registry, budget)?; - at += registry.type_layout(element).unwrap().storage.slots as usize; - check_group(&working[at..], TypeId(o), registry, budget)?; - at += 1; - let fields = - ®istry.type_layout(TypeId(o)).unwrap().instance.as_ref().unwrap().fields; - for field in [f, s] { - let ty = fields[field as usize].type_id; - check_group(&working[at..], ty, registry, budget)?; - at += registry.type_layout(ty).unwrap().storage.slots as usize; - } - } } Op::StructGet | Op::StructSet | Op::ArrayLen | Op::ArrayGet | Op::ArraySet => { budget.work(3)?; diff --git a/crates/console/prometeu-vm/src/virtual_machine.rs b/crates/console/prometeu-vm/src/virtual_machine.rs index fb267a0..589a660 100644 --- a/crates/console/prometeu-vm/src/virtual_machine.rs +++ b/crates/console/prometeu-vm/src/virtual_machine.rs @@ -1,6 +1,12 @@ use crate::numeric; use crate::resource_budget::BudgetVec; +mod array_copy; +#[cfg(test)] +mod array_copy_tests; mod callable; +#[cfg(test)] +#[path = "../tests/support/collections.rs"] +mod collection_test_support; mod collections; #[cfg(test)] mod collections_tests; @@ -16,9 +22,6 @@ mod random; mod reference; mod runtime; mod stack; -mod storage_publication; -#[cfg(test)] -mod storage_publication_tests; mod strings; mod typed; #[cfg(test)] @@ -1105,19 +1108,8 @@ impl VirtualMachine { }; return self.execute_identity(opcode, left, right, 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 - | OpCode::StoragePublish => { - let operands = match opcode { - OpCode::StoragePublish => instr.imm_u32x4().unwrap(), - OpCode::IndexGuard => [0; 4], - _ => [instr.imm_u32().unwrap(), 0, 0, 0], - }; - return self.execute_storage_publication(opcode, operands, start_pc as u32); + OpCode::ArrayCopy => { + return self.execute_array_copy(instr.imm_u32().unwrap(), start_pc as u32); } OpCode::StructNew | OpCode::StructGet diff --git a/crates/console/prometeu-vm/src/virtual_machine/array_copy.rs b/crates/console/prometeu-vm/src/virtual_machine/array_copy.rs new file mode 100644 index 0000000..39c3d80 --- /dev/null +++ b/crates/console/prometeu-vm/src/virtual_machine/array_copy.rs @@ -0,0 +1,110 @@ +//! Complete-group snapshot copy: charge, validate, prepare, then commit. +use super::*; +use crate::value_contract::{ValueError, ValueGroup}; +use prometeu_bytecode::registry::TypeId; + +fn planned_cost(count: u32, width: u64) -> Option { + u64::from(count).checked_mul(2)?.checked_mul(width)?.checked_add(4) +} + +impl VirtualMachine { + pub(super) fn execute_array_copy( + &mut self, + array_id: u32, + pc: u32, + ) -> Result<(), LogicalFrameEndingReason> { + let opcode = OpCode::ArrayCopy; + self.cycles += opcode.cycles(); + let image = self.validated_image.clone().expect("validated Array TypeId"); + let registry = image.registry(); + let array_ty = TypeId(array_id); + let element = registry.type_layout(array_ty).unwrap().array.unwrap().element_type_id; + let width = registry.type_layout(element).unwrap().storage.slots; + if self.temporary_slots() < 5 { + return Err(self.trap( + TRAP_STACK_UNDERFLOW, + opcode as u16, + "Incomplete array copy operands".into(), + pc, + )); + } + let base = self.operand_stack.len() - 5; + // Inspect count only to select cost, without changing ordinary fault order. + if let Value::Int64(n) = self.operand_stack[base + 4] + && let Ok(n) = u32::try_from(n) + { + let cost = planned_cost(n, u64::from(width)) + .ok_or_else(|| self.collection_size_trap(opcode, pc, u64::MAX))?; + self.cycles += cost - opcode.cycles(); + } + let receiver = |offset: usize| { + let r = match self.operand_stack[base + offset] { + Value::HeapRef(r) => r, + Value::Null => return Err(self.value_trap(ValueError::NullReference, opcode, pc)), + _ => return Err(self.value_trap(ValueError::Type, opcode, pc)), + }; + if self.heap.concrete_type(r, registry).map_err(|e| self.value_trap(e, opcode, pc))? + != array_ty + { + return Err(self.value_trap(ValueError::Type, opcode, pc)); + } + Ok(r) + }; + let source = receiver(0)?; + let target = receiver(2)?; + let integer = |offset: usize| match self.operand_stack[base + offset] { + Value::Int64(n) => Ok(n), + _ => Err(self.value_trap(ValueError::Type, opcode, pc)), + }; + let source_start = integer(1)?; + let target_start = integer(3)?; + let count = integer(4)?; + let range = |r, start: i64| { + let len = self + .heap + .typed_array_len(r, registry) + .map_err(|e| self.value_trap(e, opcode, pc))?; + if start < 0 || count < 0 || start > len as i64 || count > len as i64 - start { + return Err(self.value_trap(ValueError::Bounds, opcode, pc)); + } + Ok(start as usize) + }; + let source_start = range(source, source_start)?; + let target_start = range(target, target_start)?; + let count = count as usize; + // Operands remain roots; every fallible operation precedes shared writes. + let mut prepared = + BudgetVec::::new(&self.budget, RuntimeResource::TemporaryBytes); + prepared.reserve(count).map_err(|e| self.resource_trap(e, opcode, pc))?; + for i in 0..count { + let group = self + .heap + .load_typed(registry, source, source_start + i) + .map_err(|e| self.value_trap(e, opcode, pc))?; + prepared.push(group).expect("complete scratch reservation"); + } + // Snapshot captured. No allocation, validation, GC, callback or scheduler. + for (i, group) in prepared.iter().enumerate() { + self.heap.commit_typed(target, target_start + i, group.as_slice()); + } + self.operand_stack.truncate(base); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::planned_cost; + + #[test] + fn checked_cost_covers_the_full_sum_without_wrap_or_saturation() { + assert_eq!(planned_cost(10, 1), Some(24)); + assert_eq!(planned_cost(10, 3), Some(64)); + assert_eq!(planned_cost(0, u64::MAX), Some(4)); + assert_eq!(planned_cost(u32::MAX, 3), Some(25_769_803_774)); + // Synthetic widths exercise defensive overflow, not valid guest layouts. + assert_eq!(planned_cost(1, u64::MAX), None); + assert_eq!(planned_cost(1, u64::MAX / 2), None); + assert_eq!(planned_cost(u32::MAX, u64::MAX), None); + } +} diff --git a/crates/console/prometeu-vm/src/virtual_machine/array_copy_tests.rs b/crates/console/prometeu-vm/src/virtual_machine/array_copy_tests.rs new file mode 100644 index 0000000..4aedcaa --- /dev/null +++ b/crates/console/prometeu-vm/src/virtual_machine/array_copy_tests.rs @@ -0,0 +1,487 @@ +use super::collection_test_support as support; +use super::*; +use crate::ValidatedProgramImage; +use prometeu_bytecode::{assemble, registry::TypeId}; +use support::Native; + +fn machine(instruction: &str, count: usize) -> VirtualMachine { + let mut text = String::new(); + for i in 0..count { + text += &format!("GET_GLOBAL {i}\n"); + } + text += instruction; + text += "\nHALT"; + let mut vm = VirtualMachine::from_image( + support::load(&assemble(&text).unwrap()).unwrap(), + RuntimeLimits::default(), + ); + vm.prepare_boot_call().unwrap(); + vm +} +fn operands(vm: &mut VirtualMachine, values: &[Value]) { + vm.globals.extend_from_slice(values).unwrap(); + for _ in values { + vm.step(&mut Native, &mut HostContext::new(None)).unwrap(); + } +} +fn step(vm: &mut VirtualMachine) -> Result<(), LogicalFrameEndingReason> { + vm.step(&mut Native, &mut HostContext::new(None)) +} +#[test] +fn raw_copy_preserves_absence_and_box_identity_without_heap_allocations() { + let mut vm = machine("ARRAY_COPY 15", 5); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(15), 4).unwrap(); + let boxed = vm + .heap + .allocate_dynamic_box(registry.clone(), TypeId(5), &[Value::Float(1.), Value::Float(2.)]) + .unwrap(); + let present = [Value::Int64(5), Value::HeapRef(boxed)]; + vm.heap.store_typed(®istry, a, 0, &present).unwrap(); + operands( + &mut vm, + &[Value::HeapRef(a), Value::Int64(0), Value::HeapRef(a), Value::Int64(1), Value::Int64(3)], + ); + let before = vm.heap.allocation_count(); + vm.budget + .test_lower_limit(RuntimeResource::GuestBytes, vm.budget.used(RuntimeResource::GuestBytes)); + step(&mut vm).unwrap(); + assert_eq!(vm.heap.allocation_count(), before); + assert_eq!(vm.heap.load_typed(®istry, a, 1).unwrap().as_slice(), present); + for i in [2, 3] { + assert_eq!( + vm.heap.load_typed(®istry, a, i).unwrap().as_slice(), + [Value::Null, Value::Null] + ); + } +} + +fn pair(ty: u32, len: u32) -> (VirtualMachine, HeapRef, HeapRef) { + let mut vm = machine(&format!("ARRAY_COPY {ty}"), 5); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + let source = vm.heap.allocate_typed_array(registry.clone(), TypeId(ty), len).unwrap(); + let target = vm.heap.allocate_typed_array(registry, TypeId(ty), len).unwrap(); + (vm, source, target) +} +fn copy_inputs(source: HeapRef, target: HeapRef, count: i64) -> [Value; 5] { + [ + Value::HeapRef(source), + Value::Int64(0), + Value::HeapRef(target), + Value::Int64(0), + Value::Int64(count), + ] +} +fn refused(vm: &mut VirtualMachine, code: u32, cost: u64) { + let inputs = vm.operand_stack.as_slice().to_vec(); + let (pc, cycles, temporary, objects) = + (vm.pc, vm.cycles, vm.budget.used(RuntimeResource::TemporaryBytes), vm.heap.len()); + let LogicalFrameEndingReason::Trap(info) = step(vm).unwrap_err() else { panic!() }; + assert_eq!((info.code, info.opcode, info.pc), (code, 0xa9, pc as u32)); + assert_eq!(vm.cycles - cycles, cost); + assert_eq!(vm.operand_stack.as_slice(), inputs); + assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), temporary); + assert_eq!(vm.heap.len(), objects); +} + +#[test] +fn calculable_work_is_precharged_on_receivers_carriers_and_ranges_in_fault_order() { + for case in 0..20 { + let (mut vm, source, target) = pair(11, 10); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + let wrong = vm.heap.allocate_typed_struct(registry.clone(), TypeId(19)).unwrap(); + let foreign = support::load(&assemble("HALT").unwrap()).unwrap(); + let foreign_ref = + vm.heap.allocate_typed_array(foreign.registry().clone(), TypeId(11), 10).unwrap(); + let stale = HeapRef { index: source.index, generation: source.generation + 1 }; + let mut inputs = copy_inputs(source, target, 10); + let (offset, value, code) = match case { + 0 => (0, Value::Null, TRAP_NULL_REFERENCE), + 1 => (2, Value::Null, TRAP_NULL_REFERENCE), + 2 => (0, Value::HeapRef(stale), TRAP_INVALID_HANDLE), + 3 => (2, Value::HeapRef(stale), TRAP_INVALID_HANDLE), + 4 => (0, Value::HeapRef(wrong), TRAP_TYPE), + 5 => (2, Value::HeapRef(wrong), TRAP_TYPE), + 6 => (0, Value::HeapRef(foreign_ref), TRAP_TYPE), + 7 => (2, Value::Int32(0), TRAP_TYPE), + 8 => (1, Value::Int32(0), TRAP_TYPE), + 9 => (3, Value::Null, TRAP_TYPE), + 10 => (1, Value::Int64(-1), TRAP_OOB), + 11 => (3, Value::Int64(-1), TRAP_OOB), + 12 => (1, Value::Int64(1), TRAP_OOB), + 13 => (3, Value::Int64(1), TRAP_OOB), + 14 => { + inputs[1] = Value::Int32(0); // Receiver fault precedes offset carrier. + (2, Value::Null, TRAP_NULL_REFERENCE) + } + 15 => { + inputs[1] = Value::Int64(-1); // All carriers precede source range. + (3, Value::Int32(0), TRAP_TYPE) + } + 16 => (0, Value::Int32(0), TRAP_TYPE), + 17 => { + let wrong_array = + vm.heap.allocate_typed_array(registry.clone(), TypeId(22), 10).unwrap(); + (2, Value::HeapRef(wrong_array), TRAP_TYPE) + } + 18 => (1, Value::Int64(i64::MAX), TRAP_OOB), + _ => (3, Value::Int64(i64::MAX), TRAP_OOB), + }; + inputs[offset] = value; + operands(&mut vm, &inputs); + // Quota refusal must not mask earlier faults, even with full cost selected. + vm.budget.test_lower_limit( + RuntimeResource::TemporaryBytes, + vm.budget.used(RuntimeResource::TemporaryBytes), + ); + refused(&mut vm, code, 24); + for i in 0..10 { + assert_eq!( + vm.heap.load_typed(®istry, target, i).unwrap().as_slice(), + [Value::Int32(0)] + ); + } + } +} + +#[test] +fn invalid_count_selects_only_base_without_reordering_ordinary_faults() { + for count in [ + Value::Int32(10), + Value::Null, + Value::Int64(-1), + Value::Int64(u32::MAX as i64 + 1), + Value::Int64(i64::MAX), + ] { + for bad_receiver in [false, true] { + let (mut vm, source, target) = pair(11, 10); + let mut inputs = copy_inputs(source, target, 0); + inputs[4] = count.clone(); + if bad_receiver { + inputs[0] = Value::Null; + } + operands(&mut vm, &inputs); + let code = if bad_receiver { + TRAP_NULL_REFERENCE + } else if matches!(count, Value::Int64(_)) { + TRAP_OOB + } else { + TRAP_TYPE + }; + refused(&mut vm, code, 4); + } + } +} + +#[test] +fn incomplete_temporary_operands_never_read_frame_locals_for_cost() { + let code = assemble( + "GET_GLOBAL 0\nGET_GLOBAL 1\nGET_GLOBAL 2\nGET_GLOBAL 3\nGET_GLOBAL 4\nARRAY_COPY 11\nHALT", + ) + .unwrap(); + for available in 0..5 { + let mut declarations = support::declarations(code.len()); + declarations.functions[0].local_slots = 5; + let image = 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(); + // Internal defense: skip the verified producers, leaving only locals and + // an incomplete temporary group. This is not an admitted bad program. + vm.operand_stack[4] = Value::Int64(10); + vm.operand_stack.extend_from_slice(&vec![Value::Int64(10); available]).unwrap(); + vm.pc = 30; + refused(&mut vm, TRAP_STACK_UNDERFLOW, 4); + } +} + +#[test] +fn overlap_snapshots_complete_multislot_groups_and_has_no_identity_discount() { + for (source_start, target_start, count) in [(0, 1, 3), (1, 0, 3), (0, 0, 4)] { + for refuse in [false, true] { + let mut vm = machine("ARRAY_COPY 12", 5); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + let array = vm.heap.allocate_typed_array(registry.clone(), TypeId(12), 4).unwrap(); + for i in 0..4 { + vm.heap + .store_typed( + ®istry, + array, + i, + &[Value::Float(i as f64), Value::Float(i as f64 + 10.)], + ) + .unwrap(); + } + let before: Vec<_> = (0..4) + .map(|i| vm.heap.load_typed(®istry, array, i).unwrap().as_slice().to_vec()) + .collect(); + operands( + &mut vm, + &[ + Value::HeapRef(array), + Value::Int64(source_start), + Value::HeapRef(array), + Value::Int64(target_start), + Value::Int64(count), + ], + ); + let temporary = vm.budget.used(RuntimeResource::TemporaryBytes); + if refuse { + vm.budget.test_lower_limit(RuntimeResource::TemporaryBytes, temporary); + refused(&mut vm, TRAP_RESOURCE_LIMIT, if count == 3 { 16 } else { 20 }); + } else { + let cycles = vm.cycles; + step(&mut vm).unwrap(); + assert_eq!(vm.cycles - cycles, if count == 3 { 16 } else { 20 }); + assert!(vm.operand_stack.is_empty()); + } + for i in 0..4 { + let expected = + if !refuse && i >= target_start as usize && i < (target_start + count) as usize + { + &before[(i as i64 - target_start + source_start) as usize] + } else { + &before[i] + }; + assert_eq!(vm.heap.load_typed(®istry, array, i).unwrap().as_slice(), expected); + } + assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), temporary); + } + } +} + +#[test] +fn scratch_quota_and_allocator_refusals_preserve_all_destinations_and_work() { + for allocation_failure in [false, true] { + let (mut vm, source, target) = pair(11, 10); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + for i in 0..10 { + vm.heap.store_typed(®istry, source, i, &[Value::Int32(i as i32 + 1)]).unwrap(); + } + operands(&mut vm, ©_inputs(source, target, 10)); + if allocation_failure { + crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true)); + } else { + vm.budget.test_lower_limit( + RuntimeResource::TemporaryBytes, + vm.budget.used(RuntimeResource::TemporaryBytes), + ); + } + let (cycles, temporary) = (vm.cycles, vm.budget.used(RuntimeResource::TemporaryBytes)); + let LogicalFrameEndingReason::Trap(info) = step(&mut vm).unwrap_err() else { panic!() }; + assert_eq!((info.code, info.opcode, info.pc), (TRAP_RESOURCE_LIMIT, 0xa9, 30)); + let resource = info.resource.unwrap(); + assert_eq!(resource.resource, RuntimeResource::TemporaryBytes); + assert_eq!( + resource.reason, + if allocation_failure { + crate::resource_budget::ResourceReason::AllocationFailed + } else { + crate::resource_budget::ResourceReason::Limit + } + ); + assert_eq!(vm.cycles - cycles, 24); + assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), temporary); + assert_eq!(vm.operand_stack.as_slice(), copy_inputs(source, target, 10)); + for i in 0..10 { + assert_eq!( + vm.heap.load_typed(®istry, target, i).unwrap().as_slice(), + [Value::Int32(0)] + ); + } + } +} + +#[test] +fn late_invalid_source_group_releases_partial_preparation_without_any_write() { + let (mut vm, source, target) = pair(17, 10); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + let node = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap(); + vm.heap.store_typed(®istry, source, 0, &[Value::HeapRef(node)]).unwrap(); + // Internal corruption of the last stored handle exercises a late read fault. + vm.heap.commit_typed( + source, + 9, + &[Value::HeapRef(HeapRef { index: node.index, generation: node.generation + 1 })], + ); + operands(&mut vm, ©_inputs(source, target, 10)); + refused(&mut vm, TRAP_INVALID_HANDLE, 24); + for i in 0..10 { + assert_eq!(vm.heap.load_typed(®istry, target, i).unwrap().as_slice(), [Value::Null]); + } +} + +#[test] +fn empty_copy_validates_both_receivers_and_strict_ranges_without_scratch() { + for case in 0..5 { + let (mut vm, source, target) = pair(11, 2); + let mut inputs = copy_inputs(source, target, 0); + inputs[1] = Value::Int64(2); + inputs[3] = Value::Int64(2); + let code = match case { + 0 => None, + 1 => { + inputs[0] = Value::Null; + Some(TRAP_NULL_REFERENCE) + } + 2 => { + inputs[2] = Value::Null; + Some(TRAP_NULL_REFERENCE) + } + 3 => { + inputs[1] = Value::Int64(3); + Some(TRAP_OOB) + } + _ => { + inputs[3] = Value::Int64(-1); + Some(TRAP_OOB) + } + }; + operands(&mut vm, &inputs); + vm.budget.test_lower_limit( + RuntimeResource::TemporaryBytes, + vm.budget.used(RuntimeResource::TemporaryBytes), + ); + if let Some(code) = code { + refused(&mut vm, code, 4); + } else { + let cycles = vm.cycles; + step(&mut vm).unwrap(); + assert_eq!(vm.cycles - cycles, 4); + assert!(vm.operand_stack.is_empty()); + } + } +} + +#[test] +fn zero_budget_and_positive_overshoot_preserve_trap_priority_and_step_count() { + for fail in [false, true] { + let (mut vm, source, target) = pair(11, 10); + operands(&mut vm, ©_inputs(source, target, 10)); + if fail { + vm.budget.test_lower_limit( + RuntimeResource::TemporaryBytes, + vm.budget.used(RuntimeResource::TemporaryBytes), + ); + } + let (pc, cycles, inputs) = (vm.pc, vm.cycles, vm.operand_stack.as_slice().to_vec()); + let report = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap(); + assert_eq!((report.cycles_used, report.steps_executed), (0, 0)); + assert_eq!((vm.pc, vm.cycles), (pc, cycles)); + assert_eq!(vm.operand_stack.as_slice(), inputs); + let report = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap(); + assert_eq!((report.cycles_used, report.steps_executed), (24, if fail { 0 } else { 1 })); + if fail { + let LogicalFrameEndingReason::Trap(info) = report.reason else { panic!() }; + assert_eq!((info.code, info.opcode, info.pc), (TRAP_RESOURCE_LIMIT, 0xa9, pc as u32)); + assert_eq!(vm.operand_stack.as_slice(), inputs); + } else { + assert_eq!(report.reason, LogicalFrameEndingReason::BudgetExhausted); + } + } +} + +#[test] +fn copied_groups_preserve_owners_and_references_through_collection_without_guest_growth() { + for ty in [11, 12, 13, 14, 15, 16, 17] { + let (mut vm, source, target) = pair(ty, 1); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + let node = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap(); + vm.push(Value::String(std::sync::Arc::from("copy owner"))).unwrap(); + let string = vm.pop().unwrap(); + let group = match ty { + 11 => vec![Value::Int32(7)], + 12 => vec![Value::Float(1.), Value::Float(2.)], + 13 => vec![Value::Int64(1), Value::Int64(2), Value::Int64(3)], + 14 => vec![string.clone()], + 15 => vec![Value::Int64(4), string.clone()], + 16 => vec![Value::HeapRef(node), Value::Int64(0)], + _ => vec![Value::HeapRef(node)], + }; + vm.heap.store_typed(®istry, source, 0, &group).unwrap(); + operands(&mut vm, ©_inputs(source, target, 1)); + let objects = vm.heap.allocation_count(); + let guest = vm.budget.used(RuntimeResource::GuestBytes); + for q in [ + RuntimeResource::GuestBytes, + RuntimeResource::HandleSlots, + RuntimeResource::AuxiliaryBytes, + ] { + vm.budget.test_lower_limit(q, vm.budget.used(q)); + } + let cycles = vm.cycles; + step(&mut vm).unwrap(); + assert_eq!( + vm.cycles - cycles, + match ty { + 12 | 15 | 16 => 8, + 13 => 10, + _ => 6, + } + ); + assert_eq!(vm.heap.allocation_count(), objects); + assert_eq!(vm.budget.used(RuntimeResource::GuestBytes), guest); + assert_eq!(vm.heap.load_typed(®istry, target, 0).unwrap().as_slice(), group); + drop(group); + drop(string); + vm.globals.clear(); + vm.globals.push(Value::HeapRef(target)).unwrap(); + vm.gc_alloc_threshold = 1; + vm.handle_safepoint().unwrap(); + assert!(!vm.heap.is_valid(source)); + assert!(vm.heap.is_valid(target)); + if [16, 17].contains(&ty) { + assert!(vm.heap.is_valid(node)); + } + if [14, 15].contains(&ty) { + assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 1); + } + // Remove all roots and collect using the actual session root traversal. + vm.globals.clear(); + vm.last_gc_live_count = 0; + vm.handle_safepoint().unwrap(); + assert_eq!(vm.heap.len(), 0); + assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 0); + } +} + +#[test] +fn refused_copy_keeps_source_and_previous_target_native_owners_rooted() { + let (mut vm, source, target) = pair(14, 1); + let registry = vm.validated_image.as_ref().unwrap().registry().clone(); + for (array, text) in [(source, "candidate owner"), (target, "previous owner")] { + vm.push(Value::String(std::sync::Arc::from(text))).unwrap(); + let value = vm.pop().unwrap(); + vm.heap.store_typed(®istry, array, 0, &[value]).unwrap(); + } + operands(&mut vm, ©_inputs(source, target, 1)); + let temporary = vm.budget.used(RuntimeResource::TemporaryBytes); + vm.budget.test_lower_limit(RuntimeResource::TemporaryBytes, temporary); + refused(&mut vm, TRAP_RESOURCE_LIMIT, 6); + assert_eq!( + vm.heap.load_typed(®istry, source, 0).unwrap().as_slice(), + [Value::String(std::sync::Arc::from("candidate owner"))] + ); + assert_eq!( + vm.heap.load_typed(®istry, target, 0).unwrap().as_slice(), + [Value::String(std::sync::Arc::from("previous owner"))] + ); + vm.budget.test_lower_limit( + RuntimeResource::TemporaryBytes, + RuntimeLimits::default().get(RuntimeResource::TemporaryBytes), + ); + vm.globals.clear(); // Failed operands are now the only session roots. + vm.gc_alloc_threshold = 1; + vm.handle_safepoint().unwrap(); + assert!(vm.heap.is_valid(source) && vm.heap.is_valid(target)); + assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 2); + vm.operand_stack.clear(); + vm.last_gc_live_count = 0; + vm.handle_safepoint().unwrap(); + assert_eq!(vm.heap.len(), 0); + assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 0); +} diff --git a/crates/console/prometeu-vm/src/virtual_machine/collections_tests.rs b/crates/console/prometeu-vm/src/virtual_machine/collections_tests.rs index de062c6..4231e1a 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/collections_tests.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/collections_tests.rs @@ -1,6 +1,5 @@ +use super::collection_test_support as support; use super::*; -#[path = "../../tests/support/collections.rs"] -mod support; use crate::value_contract::ValueError; use prometeu_bytecode::assemble; use support::Native; diff --git a/crates/console/prometeu-vm/src/virtual_machine/identity_tests.rs b/crates/console/prometeu-vm/src/virtual_machine/identity_tests.rs index 2f17a41..8ea14d4 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/identity_tests.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/identity_tests.rs @@ -159,7 +159,7 @@ fn inline_faults_preserve_float_bits_owners_and_attempted_cost() { assert_eq!(Arc::strong_count(&foreign), refs); assert_eq!(usage(&v), before); assert_eq!(v.cycles - cycles, 8 + foreign.len() as u64); - let mut overflow = HASH_INTRINSICS[2].clone(); + let mut overflow = HASH_INTRINSICS[2]; overflow.cost_per_byte = u64::MAX; let cycles = v.cycles; fault(v.execute_inline_intrinsic(&overflow, 0), TRAP_RESOURCE_LIMIT); @@ -225,7 +225,7 @@ fn shared_invocation_rejects_bad_outputs_without_consuming_original_string_owner let count = Arc::strong_count(&content); let before = usage(&v); for handler in [missing, wrong, excess] { - let mut meta: IntrinsicMeta = HASH_INTRINSICS[2].clone(); + let mut meta: IntrinsicMeta = HASH_INTRINSICS[2]; meta.owner = "test"; // The shared executor is independent of the hash namespace. meta.implementation = IntrinsicImplementation::BorrowedInline(handler); fault(v.execute_inline_intrinsic(&meta, 0), TRAP_INVALID_INTRINSIC); diff --git a/crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs b/crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs deleted file mode 100644 index 0abf28e..0000000 --- a/crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs +++ /dev/null @@ -1,286 +0,0 @@ -//! Generic complete-group range operations. All admission precedes shared writes. -use super::*; -use crate::value_contract::{self, ValueError, ValueGroup}; -use prometeu_bytecode::registry::TypeId; - -impl VirtualMachine { - pub(super) fn execute_storage_publication( - &mut self, - opcode: OpCode, - operands: [u32; 4], - pc: u32, - ) -> Result<(), LogicalFrameEndingReason> { - self.cycles += opcode.cycles(); - if opcode == OpCode::IndexGuard { - if self.temporary_slots() < 2 { - return Err(self.trap( - TRAP_STACK_UNDERFLOW, - opcode as u16, - "Incomplete guard operands".into(), - pc, - )); - } - let base = self.operand_stack.len() - 2; - match (&self.operand_stack[base], &self.operand_stack[base + 1]) { - (Value::Int64(index), Value::Int64(limit)) if *index >= 0 && index < limit => {} - (Value::Int64(_), Value::Int64(_)) => { - return Err(self.value_trap(ValueError::Bounds, opcode, pc)); - } - _ => return Err(self.value_trap(ValueError::Type, opcode, pc)), - } - self.operand_stack.truncate(base); - return Ok(()); - } - let image = self.validated_image.clone().ok_or_else(|| { - self.trap( - prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION, - opcode as u16, - "Storage operation requires a validated Registry".into(), - pc, - ) - })?; - let registry = image.registry(); - let [array_id, owner_id, first, second] = operands; - let array_ty = TypeId(array_id); - let element = registry.type_layout(array_ty).unwrap().array.unwrap().element_type_id; - let width = registry.type_layout(element).unwrap().storage.slots as usize; - let fields = if opcode == OpCode::StoragePublish { - Some(®istry.type_layout(TypeId(owner_id)).unwrap().instance.as_ref().unwrap().fields) - } else { - None - }; - let field_width = |field: u32| { - registry.type_layout(fields.unwrap()[field as usize].type_id).unwrap().storage.slots - as usize - }; - let consumed = match opcode { - OpCode::ArrayCheckLength => 1, - OpCode::ArrayCopy => 5, - _ => 8 + width + field_width(first) + field_width(second), - }; - if self.temporary_slots() < consumed { - return Err(self.trap( - TRAP_STACK_UNDERFLOW, - opcode as u16, - "Incomplete storage operands".into(), - pc, - )); - } - let base = self.operand_stack.len() - consumed; - let integer = |offset: usize| match self.operand_stack[base + offset] { - Value::Int64(n) => Ok(n), - _ => Err(self.value_trap(ValueError::Type, opcode, pc)), - }; - if opcode == OpCode::ArrayCheckLength { - let n = integer(0)?; - if n < 0 { - return Err(self.value_trap(ValueError::Bounds, opcode, pc)); - } - if u32::try_from(n).is_err() { - return Err(self.collection_size_trap(opcode, pc, n as u64)); - } - self.operand_stack.truncate(base); - return Ok(()); - } - let receiver = |offset: usize, ty: TypeId| { - let r = match self.operand_stack[base + offset] { - Value::HeapRef(r) => r, - Value::Null => return Err(self.value_trap(ValueError::NullReference, opcode, pc)), - _ => return Err(self.value_trap(ValueError::Type, opcode, pc)), - }; - if self.heap.concrete_type(r, registry).map_err(|e| self.value_trap(e, opcode, pc))? - != ty - { - return Err(self.value_trap(ValueError::Type, opcode, pc)); - } - Ok(r) - }; - let source = receiver(0, array_ty)?; - let target = receiver(2, array_ty)?; - let source_start = integer(1)?; - let target_start = integer(3)?; - let count = integer(4)?; - let range = |r, start: i64, count: i64| { - let len = self - .heap - .typed_array_len(r, registry) - .map_err(|e| self.value_trap(e, opcode, pc))?; - if start < 0 || count < 0 || start > len as i64 || count > len as i64 - start { - return Err(self.value_trap(ValueError::Bounds, opcode, pc)); - } - Ok((start as usize, count as usize)) - }; - let (source_start, count) = range(source, source_start, count)?; - let (target_start, _) = range(target, target_start, count as i64)?; - let (fill_start, fill_count, owner) = if fields.is_some() { - let (start, count) = range(target, integer(5)?, integer(6)?)?; - let owner = receiver(7 + width, TypeId(owner_id))?; - value_contract::validate( - registry, - &self.heap, - element, - &self.operand_stack[base + 7..base + 7 + width], - ) - .map_err(|e| self.value_trap(e, opcode, pc))?; - let mut at = base + 8 + width; - for field in [first, second] { - let ty = fields.unwrap()[field as usize].type_id; - let w = field_width(field); - value_contract::validate(registry, &self.heap, ty, &self.operand_stack[at..at + w]) - .map_err(|e| self.value_trap(e, opcode, pc))?; - at += w; - } - (start, count, Some(owner)) - } else { - (0, 0, None) - }; - let work = (count as u64) - .checked_mul(2) - .and_then(|n| n.checked_add(fill_count as u64)) - .and_then(|n| n.checked_mul(width as u64)) - .and_then(|n| { - n.checked_add(if fields.is_some() { - (field_width(first) + field_width(second)) as u64 - } else { - 0 - }) - }) - .ok_or_else(|| self.collection_size_trap(opcode, pc, u64::MAX))?; - self.cycles += work; - // This reservation and all reads/validation can fail. No destination has - // changed yet. The original operands remain roots throughout preparation. - let mut prepared = - BudgetVec::::new(&self.budget, RuntimeResource::TemporaryBytes); - prepared.reserve(count).map_err(|e| self.resource_trap(e, opcode, pc))?; - for i in 0..count { - let group = self - .heap - .load_typed(registry, source, source_start + i) - .map_err(|e| self.value_trap(e, opcode, pc))?; - prepared.push(group).expect("complete scratch reservation"); - } - // No allocation, GC, callback, scheduler or fallible operation follows. - // Capturing every source group first gives memmove semantics for overlap. - for (i, group) in prepared.iter().enumerate() { - self.heap.commit_typed(target, target_start + i, group.as_slice()); - } - if let Some(owner) = owner { - let fill = &self.operand_stack[base + 7..base + 7 + width]; - for i in 0..fill_count { - self.heap.commit_typed(target, fill_start + i, fill); - } - let mut at = base + 8 + width; - for field in [first, second] { - let w = field_width(field); - self.heap.commit_typed(owner, field as usize, &self.operand_stack[at..at + w]); - at += w; - } - } - self.operand_stack.truncate(base); - 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::() - }) - .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(()) - } -} diff --git a/crates/console/prometeu-vm/src/virtual_machine/storage_publication_tests.rs b/crates/console/prometeu-vm/src/virtual_machine/storage_publication_tests.rs deleted file mode 100644 index 4799a31..0000000 --- a/crates/console/prometeu-vm/src/virtual_machine/storage_publication_tests.rs +++ /dev/null @@ -1,378 +0,0 @@ -use super::*; -#[path = "../../tests/support/collections.rs"] -mod support; -use prometeu_bytecode::{assemble, registry::TypeId}; -use support::Native; - -fn machine(instruction: &str, count: usize) -> VirtualMachine { - let mut text = String::new(); - for i in 0..count { - text += &format!("GET_GLOBAL {i}\n"); - } - text += instruction; - text += "\nHALT"; - let mut vm = VirtualMachine::from_image( - support::load(&assemble(&text).unwrap()).unwrap(), - RuntimeLimits::default(), - ); - vm.prepare_boot_call().unwrap(); - vm -} -fn operands(vm: &mut VirtualMachine, values: &[Value]) { - vm.globals.extend_from_slice(values).unwrap(); - for _ in values { - vm.step(&mut Native, &mut HostContext::new(None)).unwrap(); - } -} -fn step(vm: &mut VirtualMachine) -> Result<(), LogicalFrameEndingReason> { - vm.step(&mut Native, &mut HostContext::new(None)) -} -fn fault(result: Result<(), LogicalFrameEndingReason>, code: u32) { - let LogicalFrameEndingReason::Trap(t) = result.unwrap_err() else { panic!() }; - assert_eq!(t.code, code); -} - -#[test] -fn logical_guard_and_length_validation_preserve_exact_faults_and_inputs() { - for (index, limit, valid) in - [(0, 1, true), (1, 1, false), (-1, 3, false), (0, 0, false), (i64::MAX - 1, i64::MAX, true)] - { - let mut vm = machine("INDEX_GUARD", 2); - let values = [Value::Int64(index), Value::Int64(limit)]; - operands(&mut vm, &values); - if valid { - step(&mut vm).unwrap(); - assert!(vm.operand_stack.is_empty()); - } else { - fault(step(&mut vm), TRAP_OOB); - assert_eq!(vm.operand_stack.as_slice(), values); - } - } - for (n, code) in [ - (-1, Some(TRAP_OOB)), - (0, None), - (u32::MAX as i64, None), - (u32::MAX as i64 + 1, Some(TRAP_RESOURCE_LIMIT)), - ] { - let mut vm = machine("ARRAY_CHECK_LENGTH 15", 1); - operands(&mut vm, &[Value::Int64(n)]); - let before = vm.heap.allocation_count(); - if let Some(code) = code { - fault(step(&mut vm), code); - assert_eq!(vm.operand_stack.as_slice(), [Value::Int64(n)]); - } else { - step(&mut vm).unwrap(); - assert!(vm.operand_stack.is_empty()); - } - assert_eq!(vm.heap.allocation_count(), before); - } -} - -#[test] -fn overlapping_publication_preserves_complete_groups_and_publishes_both_fields() { - for quota in [None, Some(RuntimeResource::TemporaryBytes)] { - let mut vm = machine("STORAGE_PUBLISH 12,21,0,1", 13); - let registry = vm.validated_image.as_ref().unwrap().registry().clone(); - let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(12), 4).unwrap(); - let owner = vm.heap.allocate_typed_struct(registry.clone(), TypeId(21)).unwrap(); - for i in 0..3 { - vm.heap - .store_typed( - ®istry, - a, - i, - &[Value::Float(i as f64 + 1.), Value::Float(i as f64 + 10.)], - ) - .unwrap(); - } - let before: Vec<_> = (0..4) - .map(|i| vm.heap.load_typed(®istry, a, i).unwrap().as_slice().to_vec()) - .collect(); - let inputs = [ - Value::HeapRef(a), - Value::Int64(0), - Value::HeapRef(a), - Value::Int64(1), - Value::Int64(3), - Value::Int64(0), - Value::Int64(1), - Value::Float(9.), - Value::Float(8.), - Value::HeapRef(owner), - Value::Int32(7), - Value::Float(6.), - Value::Float(5.), - ]; - operands(&mut vm, &inputs); - let old_usage = vm.budget.used(RuntimeResource::TemporaryBytes); - if let Some(q) = quota { - vm.budget.test_lower_limit(q, vm.budget.used(q)); - } - if quota.is_some() { - fault(step(&mut vm), TRAP_RESOURCE_LIMIT); - assert_eq!(vm.operand_stack.as_slice(), inputs); - for (i, group) in before.iter().enumerate() { - assert_eq!(vm.heap.load_typed(®istry, a, i).unwrap().as_slice(), group); - } - assert_eq!( - vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(), - [Value::Int32(0)] - ); - assert_eq!( - vm.heap.load_typed(®istry, owner, 1).unwrap().as_slice(), - [Value::Float(0.), Value::Float(0.)] - ); - } else { - step(&mut vm).unwrap(); - assert!(vm.operand_stack.is_empty()); - assert_eq!( - vm.heap.load_typed(®istry, a, 0).unwrap().as_slice(), - [Value::Float(9.), Value::Float(8.)] - ); - for i in 1..4 { - assert_eq!(vm.heap.load_typed(®istry, a, i).unwrap().as_slice(), before[i - 1]); - } - assert_eq!( - vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(), - [Value::Int32(7)] - ); - assert_eq!( - vm.heap.load_typed(®istry, owner, 1).unwrap().as_slice(), - [Value::Float(6.), Value::Float(5.)] - ); - } - assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), old_usage); - } -} - -#[test] -fn late_invalid_field_or_range_prevents_every_write() { - for bad_range in [false, true] { - let mut vm = machine("STORAGE_PUBLISH 11,9,0,1", 11); - let registry = vm.validated_image.as_ref().unwrap().registry().clone(); - let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(11), 2).unwrap(); - let owner = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap(); - vm.heap.store_typed(®istry, a, 0, &[Value::Int32(3)]).unwrap(); - let inputs = [ - Value::HeapRef(a), - Value::Int64(0), - Value::HeapRef(a), - Value::Int64(1), - Value::Int64(1), - Value::Int64(if bad_range { 2 } else { 0 }), - Value::Int64(1), - Value::Int32(8), - Value::HeapRef(owner), - Value::Int32(4), - if bad_range { Value::Int64(7) } else { Value::Boolean(true) }, - ]; - operands(&mut vm, &inputs); - fault(step(&mut vm), if bad_range { TRAP_OOB } else { TRAP_TYPE }); - assert_eq!(vm.operand_stack.as_slice(), inputs); - assert_eq!(vm.heap.load_typed(®istry, a, 0).unwrap().as_slice(), [Value::Int32(3)]); - assert_eq!(vm.heap.load_typed(®istry, a, 1).unwrap().as_slice(), [Value::Int32(0)]); - assert_eq!(vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(), [Value::Int32(0)]); - } -} - -#[test] -fn raw_copy_preserves_absence_and_box_identity_without_heap_allocations() { - let mut vm = machine("ARRAY_COPY 15", 5); - let registry = vm.validated_image.as_ref().unwrap().registry().clone(); - let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(15), 4).unwrap(); - let boxed = vm - .heap - .allocate_dynamic_box(registry.clone(), TypeId(5), &[Value::Float(1.), Value::Float(2.)]) - .unwrap(); - let present = [Value::Int64(5), Value::HeapRef(boxed)]; - vm.heap.store_typed(®istry, a, 0, &present).unwrap(); - operands( - &mut vm, - &[Value::HeapRef(a), Value::Int64(0), Value::HeapRef(a), Value::Int64(1), Value::Int64(3)], - ); - let before = vm.heap.allocation_count(); - vm.budget - .test_lower_limit(RuntimeResource::GuestBytes, vm.budget.used(RuntimeResource::GuestBytes)); - step(&mut vm).unwrap(); - assert_eq!(vm.heap.allocation_count(), before); - assert_eq!(vm.heap.load_typed(®istry, a, 1).unwrap().as_slice(), present); - for i in [2, 3] { - assert_eq!( - vm.heap.load_typed(®istry, a, i).unwrap().as_slice(), - [Value::Null, Value::Null] - ); - } -} - -#[test] -fn dead_invalid_operands_and_split_typed_groups_are_rejected() { - for instruction in [ - "ARRAY_COPY 9", - "ARRAY_CHECK_LENGTH 99", - "STORAGE_PUBLISH 11,9,0,0", - "STORAGE_PUBLISH 11,9,0,99", - "STORAGE_PUBLISH 11,11,0,1", - ] { - assert!( - support::load(&assemble(&format!("HALT\n{instruction}")).unwrap()).is_err(), - "{instruction}" - ); - } - assert!( - 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 = "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()); - } -} diff --git a/crates/console/prometeu-vm/tests/collection_conformance.rs b/crates/console/prometeu-vm/tests/collection_conformance.rs index 04f62e4..3b2d6f0 100644 --- a/crates/console/prometeu-vm/tests/collection_conformance.rs +++ b/crates/console/prometeu-vm/tests/collection_conformance.rs @@ -132,3 +132,39 @@ fn collection_load_limits_and_analysis_refusals_keep_context_and_refund_scratch( assert_eq!(budget.used(LoadResource::RegistryBytes), 0); } } + +#[test] +fn removed_storage_opcodes_reject_even_in_unreachable_instruction_positions() { + use prometeu_bytecode::load_limits::{LoadCode, LoadReason}; + use prometeu_vm::VmInitError; + for opcode in [0xa7u16, 0xa8, 0xaa, 0xab] { + for dead in [false, true] { + let mut code = if dead { vec![1, 0] } else { vec![] }; + let pc = code.len() as u32; + code.extend_from_slice(&opcode.to_le_bytes()); + let VmInitError::RegistryLoadFailed(error) = load(&code).unwrap_err() else { panic!() }; + assert_eq!(error.code, LoadCode::InvalidCode); + assert_eq!(error.reason, LoadReason::UnknownOpcode); + assert_eq!((error.function, error.pc), (Some(0), Some(pc))); + } + } + // The same bytes are accepted as an Int64 immediate, never decoded as opcodes. + let code = [0x14, 0, 0xa7, 0, 0xa8, 0, 0xaa, 0, 0xab, 0, 1, 0]; + assert_eq!( + run(&code), + [Value::Int64(i64::from_le_bytes([0xa7, 0, 0xa8, 0, 0xaa, 0, 0xab, 0]))] + ); +} + +#[test] +fn neutral_pbx_copy_moves_a_range_and_preserves_physical_length() { + let code=assemble("PUSH_I64 2\nARRAY_NEW 11\nSET_GLOBAL 0\nGET_GLOBAL 0\nPUSH_I64 0\nPUSH_I32 42\nARRAY_SET 11\nGET_GLOBAL 0\nPUSH_I64 0\nGET_GLOBAL 0\nPUSH_I64 1\nPUSH_I64 1\nARRAY_COPY 11\nGET_GLOBAL 0\nPUSH_I64 1\nARRAY_GET 11\nGET_GLOBAL 0\nARRAY_LEN 11\nHALT").unwrap(); + assert_eq!(run(&code), [Value::Int64(2), Value::Int32(42)]); + for text in [ + "HALT\nARRAY_COPY 9", + "HALT\nARRAY_COPY 99", + "PUSH_I64 1\nARRAY_NEW 11\nPUSH_I64 0\nPUSH_I64 1\nARRAY_NEW 11\nPUSH_I64 0\nPUSH_BOOL 1\nARRAY_COPY 11\nHALT", + ] { + assert!(load(&assemble(text).unwrap()).is_err(), "{text}"); + } +} diff --git a/discussion/lessons/DSC-0065-vm-storage-publication-neutrality/LSN-0072-primitives-need-independent-semantics-and-failed-attempts-still-cost-work.md b/discussion/lessons/DSC-0065-vm-storage-publication-neutrality/LSN-0072-primitives-need-independent-semantics-and-failed-attempts-still-cost-work.md new file mode 100644 index 0000000..867a9bc --- /dev/null +++ b/discussion/lessons/DSC-0065-vm-storage-publication-neutrality/LSN-0072-primitives-need-independent-semantics-and-failed-attempts-still-cost-work.md @@ -0,0 +1,135 @@ +--- +id: LSN-0072 +ticket: vm-storage-publication-neutrality +title: Primitives Need Independent Semantics and Failed Attempts Still Cost Work +created: 2026-10-23 +tags: [vm, isa, storage, atomicity, ownership, cycles, frontend-neutrality] +--- + +## Context and Published Result + +DEC-0067, accepted from AGD-0068 and implemented by PLN-0198, reduced the storage +ISA after reviewing additions requested by a consumer. The runtime removed +INDEX_GUARD (0xA7), ARRAY_CHECK_LENGTH (0xA8), STORAGE_PUBLISH (0xAA) and +FIELDS_PUBLISH (0xAB). ARRAY_COPY remains at 0xA9 and ARRAY_LEN at 0xA4. The +supported encodings and fault/cost contracts live in the +[canonical ISA](../../../docs/specs/runtime/02-vm-instruction-set.md#typed-array-range-copy), +[time model](../../../docs/specs/runtime/01-time-model-and-cycles.md#collection-attempt-charging) +and [memory specification](../../../docs/specs/runtime/03-memory-stack-heap-and-allocation.md#prepared-array-range-copy). +The [producer handoff](../../../docs/vm-arch/collection-producer-handoff.md) and +[qualification record](../../../docs/vm-arch/STORAGE_QUALIFICATION.md) describe +integration obligations and executable evidence. This lesson explains the result; +it does not establish a separate contract. + +## A Consumer Request Is Evidence, Not a Permanent ISA Commitment + +A real compiler or SDK can expose missing capabilities. That makes its use cases +valuable evidence, but delivery in one consumer does not make every added helper +an enduring machine primitive. Re-evaluate the operation's own semantics, failure +boundary and reuse across representations. Backward compatibility was explicitly +not a requirement for this revision. + +The removed guards expressed logical limits and allocation-free validation that +the producer can organize through its existing control flow. ARRAY_LEN remains +useful as a direct physical query; removing logical helpers does not remove +physical validation from allocation and array access. A collection's logical +length can differ from its backing array's physical length. + +The removed publications coupled several mutations into fixed or schema-selected +batches. Supporting one current collection algorithm did not justify maintaining +those compound machine guarantees. ARRAY_COPY survives because strict typed +range transfer, snapshot overlap and complete-group ownership provide independent +machine semantics without identifying a source collection. + +Learning from Java, C#, Rust and Go means examining principles and tradeoffs, +not selecting a mandatory frontend. JavaScript and Lua were philosophical tests +of the architecture's independence. No engine, frontend or object from either +language was needed to implement or qualify this change. + +## An Instruction Boundary Does Not Roll Back a Source Operation + +ARRAY_COPY starts with five prepared operands. It reserves full scratch, captures +all source groups and then commits prevalidated writes. Failure preserves its +operands and destinations while releasing provisional scratch. Earlier source +calculations, allocations or callbacks keep their effects. + +Snapshot preparation explains overlap: when copying indices 0..3 to 1..4 of the +same array, the source sequence is captured before any destination store. Native +String owners, HeapRefs, Dynamic tags/boxes and Trait groups travel together. +Splitting carriers or cloning payload graphs would change the operation. + +A series of individually validated STRUCT_SET or ARRAY_SET instructions does not +inherit a shared rollback boundary. A consumer that previously relied on a batch +must establish its invariant through an appropriate algorithm/representation or +explicitly revise its operational guarantee. A renamed helper emitting sequential +stores does not prove equivalence. Consumer migration remains an independent +delivery; it does not block publication of the runtime's own supported surface. + +## Select Cost Early Without Selecting the First Fault Early + +Let N be count and W the immutable Registry element-slot width. ARRAY_COPY charges +4+2*N*W when all five temporary operands are available and N is an exact Int64 in +0..u32::MAX. For ten one-slot elements the attempt costs 24 even if a receiver is +null, a range is invalid or scratch reservation fails. That charge records the +planned work of an admitted attempt, independently of published state. + +Inspecting N early only selects cost. It does not move an invalid count ahead of +an invalid receiver in ordinary fault precedence. If count has a wrong carrier or +is outside the calculable domain, charge base 4 and follow normal validation. +With null source and negative count, the receiver fault still wins. Frame locals +must never stand in for missing temporary operands. + +Zero count still validates both receivers and ranges and costs 4. Identity and +aliasing do not discount a copy. Checked total-cost overflow has its explicit +base-charge/SizeOverflow path; synthetic widths test this defense without claiming +that such widths are admitted guest layouts. + +This extends the reasoning in +[Collection Attempts Preserve Groups and Charge Work Before Publication](../DSC-0051-game-state-collections-and-objects/LSN-0060-collection-attempts-preserve-groups-and-charge-work-before-publication.md): +unchanged state and consumed cycles are compatible outcomes. Positive budget +admits an entire instruction with overshoot; zero admits none. A failed attempt +reports its trap and charged cycles, but contributes no successful target step. + +## Remove Support at Every Boundary + +Removing dispatch alone leaves assemblers, decoder metadata or dead-code +verification advertising obsolete capabilities. This implementation removes all +four variants, encodings, metadata, assembler/disassembler cases, Registry effects, +verifier paths and executors. Unsupported instruction positions reject during +admission, including after HALT. The same byte values inside a legitimate immediate +remain data, so a raw byte search is not an admission rule. + +The retained copy now has a dedicated executor. Tests inherited from the compound +publication paths were migrated to copy-specific overlap, quota and group cases. +That preserves relevant evidence while eliminating tests of obsolete successes. + +## Evidence and Practical Limits + +The qualification record identifies the source baseline and working-tree +fingerprints rather than attributing uncommitted code to a new provider SHA. +Tests use independent literal encodings and serialized neutral PBX, the production +loader/VM, exact cycle/fault oracles, quota and allocation-failure injection, +late invalid source groups, overlap in both directions, frame temporary floors, +zero/positive budgets, and native/guest owners across collection. + +Internal state injection is labeled separately from admitted-program evidence. +The checked-cost overflow test uses synthetic widths; it does not relax Registry +layout rules. Test-only cleanup also shares the collection fixture module and +removes redundant Copy clones to satisfy Clippy without changing identity/hash +semantics. Full gate results and remaining ignored tests are recorded with their +actual execution status. + +These results do not establish the Studio SDK's migration, its old compound +atomicity guarantees, new collection algorithms, or JavaScript/Lua conformance. +Those claims require the consumer's own accepted contract and new evidence on +an identified provider. + +## Pitfalls + +- Keeping a primitive solely because a delivered SDK already emits it. +- Removing a logical guard while accidentally weakening physical bounds checks. +- Charging only after receiver/range validation, which changes failed-attempt cost. +- Letting cost inspection reorder value faults or read below the temporary floor. +- Dropping ownership/overlap coverage when deleting the old compound executor. +- Treating stores as a transaction, or claiming old consumer evidence for a new provider. +- Searching arbitrary payload bytes instead of actual instruction boundaries. diff --git a/docs/specs/runtime/01-time-model-and-cycles.md b/docs/specs/runtime/01-time-model-and-cycles.md index 6027c3f..ebc2d8b 100644 --- a/docs/specs/runtime/01-time-model-and-cycles.md +++ b/docs/specs/runtime/01-time-model-and-cycles.md @@ -169,6 +169,16 @@ The [Dynamic/reference instruction table](02c-program-registry-and-pbx-v0.md#dyn The [collection ISA table](02-vm-instruction-set.md#typed-collection-instructions) defines complete costs. Metadata widths determine costs independently of native sizeof, allocator, timing, optimizer and GC. Each decoded collection attempt is charged exactly once before validation/refusal. Fixed costs apply even to underflow, invalid receiver, bounds and rejected value groups. ARRAY_NEW charges 8 + N*w(E) whenever an available exact Int64 length is in 0..u32::MAX, including quota/allocation refusal; absent, wrong-carrier, negative or above-u32 length charges eight. Rejected loads execute and charge no guest instruction. +ARRAY_COPY charges C=4+2*N*W when all five temporary operands are available and +count is exact Int64 in 0..u32::MAX. W comes from immutable Registry element +storage. Charge C before receiver/range/group validation and scratch reservation, +retaining it on refusal. Otherwise charge base 4 and preserve ordinary fault +precedence. Checked cost overflow charges base 4 and traps SizeOverflow before +ordinary validation. Never inspect frame locals as missing operands. Empty copies +still validate and cost 4; identity/overlap give no discount. Ten one-slot elements +cost 24 on success, invalid receiver/range or scratch refusal. Full validation +order is defined in the [copy contract](02-vm-instruction-set.md#typed-array-range-copy). + Examples: empty Struct/Array costs eight; a Struct containing Int32, vec2 and Trait costs thirteen; ten vec2 elements cost twenty-eight even on quota refusal; pixel GET costs six including bounds failure; pixel SET costs seven including group rejection; LEN costs three including receiver failure. Charged cycles are never refunded by rollback. Budget admission remains between instructions. Zero executes nothing. A positive remaining budget admits the entire next instruction, which may overshoot; continuation then returns BudgetExhausted before the following instruction. Thus ARRAY_NEW of ten vec2 under run_budget(1) consumes twenty-eight cycles. Trap takes priority on refusal, with the same cycle delta and no successful target step. steps_executed counts only successful steps; cycles_used includes failed attempts. Collection operations add no implicit frame sync, tick, GC or coroutine switch, no extra length cap and no cycle pre-admission rule. diff --git a/docs/specs/runtime/02-vm-instruction-set.md b/docs/specs/runtime/02-vm-instruction-set.md index 0adefd7..5b28dac 100644 --- a/docs/specs/runtime/02-vm-instruction-set.md +++ b/docs/specs/runtime/02-vm-instruction-set.md @@ -163,96 +163,58 @@ Typed callable instructions are MAKE_CLOSURE `(FunctionId,FnTypeId)`, CALL_CLOSU INTRINSIC also dispatches the [VM-owned random operations](02e-vm-owned-random-core.md), IDs 0x4000–0x4003. NEW cannot construct Random. TRAP_RANDOM_REJECTION_LIMIT is 0x00000015. -## Logical guards and atomic storage publication +## Typed array range copy -The core includes the following generic operations, with u16 opcodes and -little-endian u32 immediates. They introduce no new Registry category, syscall, -source-language collection or ambient transaction. Operand order below is bottom -to top; all integer operands are Int64. +ARRAY_COPY uses little-endian u16 opcode 0xA9 followed by one little-endian u32 +Array TypeId. It consumes five operands, bottom to top: source, sourceStart, +target, targetStart, count, and produces no result. Receivers require the exact +Array type; sourceStart, targetStart and count require exact Int64 carriers. +Ranges must be nonnegative and fit each physical array, without clamping, +truncation or coercion. An empty range may start at the physical length. -| Opcode | Immediate | Stack operands consumed | -| --- | --- | --- | -| INDEX_GUARD `0xA7` | none | index, exclusive limit | -| ARRAY_CHECK_LENGTH `0xA8` | Array TypeId | length | -| ARRAY_COPY `0xA9` | Array TypeId | source, sourceStart, target, targetStart, count | -| STORAGE_PUBLISH `0xAA` | Array TypeId, Struct TypeId, first field, second field | source, sourceStart, target, targetStart, count, fillStart, fillCount, element group, owner, first field group, second field group | +Loading validates Array identity/category even in unreachable code. Registry +verification requires five complete operands. The instruction may trap, but is +neither a branch, terminator nor safepoint. ARRAY_LEN (0xA4) remains the physical +length query. Logical collection limits and error paths belong to producers. +The encodings 0xA7, 0xA8, 0xAA and 0xAB are unsupported and reject at admission, +including in unreachable code. Bytes inside immediates or data are not opcodes. +There is no multi-owner publication, generic batch or atomic-region replacement. -All operations return no values. INDEX_GUARD accepts exactly -`0 <= index < limit`, otherwise TRAP_OOB. It does not use physical storage length. -ARRAY_CHECK_LENGTH accepts zero through u32::MAX without allocating. Negative -lengths produce TRAP_OOB; values above u32::MAX produce the same resource -SizeOverflow failure as ARRAY_NEW. This check does not reserve future allocation. +Copy preserves complete stored groups, including presence tags, immutable boxes, +native String owners, HeapRefs, traits and identities without boxing or payload +repacking. All source groups are captured before the first destination write, +so overlap observes the pre-attempt source sequence. All copy scratch is reserved +under TemporaryBytes before writing and released on success or failure. Original +operands remain roots throughout preparation. No guest object or request token +is allocated. Quota refusal does not trigger rescue GC. -ARRAY_COPY and STORAGE_PUBLISH require exact Array receiver identities and complete -stored groups. Every range must have nonnegative start/count and fit its physical -array; an empty range may start at length. Invalid ranges produce TRAP_OOB, never -clamping or truncation. Copy reads the pre-attempt source sequence, including for -overlapping source/target ranges. Presence tags, immutable boxes, native owners and -object identities are preserved without payload unpacking or repacking. +Validation, quota or recoverable allocation failure preserves operands and all +destinations/owners. Commit uses prevalidated infallible writes; no callback, +allocation, GC or scheduler boundary occurs inside the instruction. Consume the +five operands only after commit. Cycles and diagnostics retain the attempt. +This guarantee begins with prepared operands; it does not undo earlier source +calculations, allocations or callbacks, or make a sequence of stores transactional. -STORAGE_PUBLISH first copies the selected source range, then fills the selected -target range with the supplied element group, then writes the two selected fields -on the supplied Struct owner. Fields must be distinct valid indices on that Struct; -their types and widths come from the immutable Registry. Source code/producer owns -selection of every range, value, owner and field. The VM implements these generic -ordered effects and does not select collection algorithms. +Let N=count and W=element storage slots from the immutable validated Registry. +The planned cost is C=4+2*N*W. Charge base 4 for every admitted attempt. Only inspect +count for cost selection when all five operands are available in the temporary +stack region; frame locals are not operands. If N is exact Int64 in 0..u32::MAX, +charge representable C before validating receivers, offsets, ranges, source +groups or reserving scratch. Retain C on any later refusal. Otherwise retain base +4 and follow ordinary validation; count is neither converted nor accepted. +Checked overflow of C retains only base 4 and produces structured RESOURCE_LIMIT +with SizeOverflow before ordinary validation, without wrapping or saturation. -Loading validates all immediate identities/categories/fields even in unreachable -code. Verification requires complete typed operands. ARRAY_COPY consumes five -slots; STORAGE_PUBLISH consumes `8 + elementWidth + firstWidth + secondWidth`. -INDEX_GUARD consumes two slots and ARRAY_CHECK_LENGTH consumes one. - -Execution validates all receivers, lengths/ranges, supplied groups and source -groups, and reserves the complete charged copy scratch before writing. Validation, -quota or host allocation refusal leaves all input operands and destinations -unchanged, including both fields and native owners. Scratch uses TemporaryBytes, -is refunded on success/refusal and does not relax any resource ceiling. Existing -operands remain roots during preparation. There is no GC, callback, scheduler or -other interleaving inside this single instruction. Commit uses already validated -complete groups, performs no fallible allocation and consumes operands only after -all selected effects have been published. No prepared token escapes the attempt. - -Base cycle costs are 2 for either guard, 4 for ARRAY_COPY and 6 for STORAGE_PUBLISH. -Copy adds `2 * count * elementWidth`; publication additionally adds -`fillCount * elementWidth + firstWidth + secondWidth`. Work arithmetic is checked -before mutation. A refused preparation still charges the attempted work. Neither -operation is a safepoint. Consumers must prepare any potentially failing return -carrier and all call operands before publication; a series of ordinary stores is -not a substitute for whole-operation safety. - - -## Atomic publication of multiple Struct instances - -FIELDS_PUBLISH (`0xAB`) has one little-endian u32 schema TypeId immediate. The -schema MUST be a Registry Struct whose ordered field types are all Struct types. -Names are descriptive only. The schema is declarative metadata; no request object, -new Registry category or preparation token is allocated or exposed. - -For each schema entry, the operand stack supplies one nullable destination -reference followed by every destination field's complete typed value group, in -field declaration order. The stack effect is the sum, over entries, of one plus -the destination instance's initialization-slot count; no results are produced. -Null skips that destination, but every supplied field group is still validated. -Non-null destinations require exact concrete Struct identity and a live reference -of the current session/generation. Array, Trait, Fn and builtin schema entries are -invalid, including in unreachable instructions. The full schema and exact stack -height/groups are checked under normal Registry admission/work limits. - -Validate all destinations and value groups before any shared write or operand -consumption. The original operands remain roots throughout preparation. Then write -all fields in schema order, without allocation, fallible validation, callbacks, -GC or scheduler interleaving, and consume the entire input. Input values are -captured before publication; repeated destinations use ordered last-write -semantics. Complete multi-slot/native/reference groups retain identity and normal -ownership. A failed attempt preserves every destination and the operand stack. - -The operation allocates no publication scratch or guest object. Operand/frame and -source value preparation remain normally charged and may fail before publication. -Base cost is 6 cycles. After the stack-height check, precharge one cycle per schema -entry and one per destination field execution slot for the complete declared -attempt, including skipped destinations. Quotas are unchanged. -This is a generic typed storage mechanism; collection algorithms and node topology -remain source-owned, with no collection-specific VM instruction or syscall. +Early count inspection selects cost only. Apart from cost overflow, fault order +is temporary height, source receiver, target receiver, carriers of sourceStart, +targetStart and count, source range, target range, scratch reservation, then source +group reads/validation in order. Thus above-u32 count with otherwise valid inputs +fails physical bounds, not an allocation-size check. N=0 still validates receivers +and ranges and costs 4. Identity and overlap grant no discount. N=10/W=1 costs 24 +on success, invalid receiver/range or scratch refusal. Zero budget executes no +attempt; a positive budget admits the whole instruction with overshoot. Trap +prevails over BudgetExhausted on failure, with no successful target step. Rejected +images execute no guest instruction and charge zero guest cycles. ## Identity and value hashing diff --git a/docs/specs/runtime/02c-program-registry-and-pbx-v0.md b/docs/specs/runtime/02c-program-registry-and-pbx-v0.md index 187838d..2794785 100644 --- a/docs/specs/runtime/02c-program-registry-and-pbx-v0.md +++ b/docs/specs/runtime/02c-program-registry-and-pbx-v0.md @@ -308,13 +308,15 @@ precedence, conversions, catalog metadata and producer examples. [Random](02e-vm-owned-random-core.md) adds BuiltinId 0x0016 and intrinsic IDs 0x4000–0x4003 using existing PBX v0 encoding. Registry categories remain unchanged; unsupported identities are rejected, without fallback. -## Logical guards and atomic storage publication +## Typed array range copy -INDEX_GUARD (0xA7), ARRAY_CHECK_LENGTH (0xA8), ARRAY_COPY (0xA9) and -STORAGE_PUBLISH (0xAA) provide generic logical validation, complete stored-group -transfer and prepared atomic publication. Their encoding, typed operands, exact -faults, overlap semantics, costs and preflight/commit guarantees are defined by -the [canonical instruction contract](02-vm-instruction-set.md#logical-guards-and-atomic-storage-publication). No new Registry category, syscall, collection algorithm or safepoint is introduced. +ARRAY_COPY (0xA9) uses one Array TypeId and five operands for complete stored-group +range transfer, with no new Registry category or PBX section. Encoding, exact +receivers, overlap, preparation, fault order and precharged attempt costs follow +the [instruction contract](02-vm-instruction-set.md#typed-array-range-copy). +Unsupported opcodes 0xA7, 0xA8, 0xAA and 0xAB reject during bounded instruction +admission even after a terminator. Immediate/data bytes are not instructions. +ARRAY_LEN remains 0xA4; physical allocation/access checks remain mandatory. ## Identity queries diff --git a/docs/specs/runtime/03-memory-stack-heap-and-allocation.md b/docs/specs/runtime/03-memory-stack-heap-and-allocation.md index 76158be..02df49f 100644 --- a/docs/specs/runtime/03-memory-stack-heap-and-allocation.md +++ b/docs/specs/runtime/03-memory-stack-heap-and-allocation.md @@ -223,15 +223,18 @@ Threshold GC opportunities are FRAME_SYNC and successful RET of the last root, a [Random objects](02e-vm-owned-random-core.md) use ObjectKind tag 9 (tag 6 remains reserved), 32 logical payload bytes and quota-accounted headers/handles/auxiliary storage. They are opaque GC leaves; Dynamic holds their direct reference and exact TypeId. -## Prepared storage publication +## Prepared array range copy -Generic complete-group transfer/publication reserves its copy scratch against -TemporaryBytes before changing any destination. Source groups preserve Dynamic -presence tags and immutable box/native owner/reference identities. Operands remain -roots throughout preparation; the commit phase has no allocation, GC, scheduler or -callback boundary. Refusal changes neither destinations nor input groups, and -releases temporary reservations. Consumer return carriers must be prepared before -publication. See [the instruction contract](02c-program-registry-and-pbx-v0.md#logical-guards-and-atomic-storage-publication). +ARRAY_COPY reserves all copy scratch against TemporaryBytes and captures complete +source groups before changing any destination. Preserve Dynamic tags, immutable +boxes, native String owners, HeapRefs and trait groups without boxing or new guest +objects/request tokens. Operands remain roots; commit has no allocation, fallible +validation, callback, GC or scheduler boundary. No rescue GC is performed. +Failure preserves destinations and operands and releases provisional scratch; +success also releases scratch, then consumes the five operands. Charged cycles +are never refunded. Overlap reads the pre-attempt source sequence, without an +identity discount. Earlier instructions retain their effects; multiple ordinary +stores do not form a transaction. See [the instruction contract](02-vm-instruction-set.md#typed-array-range-copy). ## Borrowed intrinsic invocation diff --git a/docs/vm-arch/ARCHITECTURE.md b/docs/vm-arch/ARCHITECTURE.md index e14fa72..6b8308d 100644 --- a/docs/vm-arch/ARCHITECTURE.md +++ b/docs/vm-arch/ARCHITECTURE.md @@ -204,10 +204,14 @@ The [Program Registry contract](../specs/runtime/02c-program-registry-and-pbx-v0 See the [random contract](../specs/runtime/02e-vm-owned-random-core.md) for Random BuiltinId 0x0016, ObjectKind tag 9, intrinsics 0x4000–0x4003, atomic state mutation, bounded 4+8*N costs and rejection-limit trap 0x00000015. Random is managed by the VM heap and existing GC, without host entropy or PBX changes. -## Atomic publication of multiple Struct instances +## Typed array range copy -FIELDS_PUBLISH (0xAB) atomically publishes complete typed fields to multiple -source-selected Struct destinations. Its schema is an ordinary Registry Struct; -no new category, request object, syscall or safepoint is introduced. Exact nullable -destination rules, stack effects, validation, costs and commit semantics are in the -[canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#atomic-publication-of-multiple-struct-instances). +ARRAY_COPY (0xA9) copies complete groups between exact typed arrays with strict +physical ranges and pre-attempt overlap semantics. Preparation reserves scratch +before infallible commit; failures preserve operands/destinations and retain +charged work. No callbacks, GC or scheduling occur inside the instruction. +The [instruction contract](../specs/runtime/02-vm-instruction-set.md#typed-array-range-copy) +defines encoding, fault order and early cost selection. ARRAY_LEN remains the +physical query; producer algorithms own logical limits and multi-owner invariants. +There is no transactional sequence of stores. Encodings 0xA7/0xA8/0xAA/0xAB are +unsupported, including in dead code. diff --git a/docs/vm-arch/ISA_CORE.md b/docs/vm-arch/ISA_CORE.md index c43d187..9f62663 100644 --- a/docs/vm-arch/ISA_CORE.md +++ b/docs/vm-arch/ISA_CORE.md @@ -192,22 +192,17 @@ STRUCT_NEW (0x00A0), STRUCT_GET (0x00A1), STRUCT_SET (0x00A2), ARRAY_NEW (0x00A3 See the [random contract](../specs/runtime/02e-vm-owned-random-core.md) for Random BuiltinId 0x0016, ObjectKind tag 9, intrinsics 0x4000–0x4003, atomic state mutation, bounded 4+8*N costs and rejection-limit trap 0x00000015. Random is managed by the VM heap and existing GC, without host entropy or PBX changes. -## Logical guards and atomic storage publication +## Typed array range copy -INDEX_GUARD (0xA7), ARRAY_CHECK_LENGTH (0xA8), ARRAY_COPY (0xA9) and -STORAGE_PUBLISH (0xAA) provide generic logical validation, complete stored-group -transfer and prepared atomic publication. Their encoding, typed operands, exact -faults, overlap semantics, costs and preflight/commit guarantees are defined by -the [canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#logical-guards-and-atomic-storage-publication). No new Registry category, syscall, collection algorithm or safepoint is introduced. - - -## Atomic publication of multiple Struct instances - -FIELDS_PUBLISH (0xAB) atomically publishes complete typed fields to multiple -source-selected Struct destinations. Its schema is an ordinary Registry Struct; -no new category, request object, syscall or safepoint is introduced. Exact nullable -destination rules, stack effects, validation, costs and commit semantics are in the -[canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#atomic-publication-of-multiple-struct-instances). +ARRAY_COPY (0xA9, Array TypeId u32) consumes source, sourceStart, target, +targetStart, count without a result. Complete groups and owners survive overlap +and failed preparation. Charge 4+2*N*W before ordinary validation for available +Int64 count in 0..u32::MAX; other counts/underflow charge base 4. Checked cost +overflow charges base and traps SizeOverflow. The +[canonical contract](../specs/runtime/02-vm-instruction-set.md#typed-array-range-copy) +defines fault precedence, scratch and commit. ARRAY_LEN (0xA4) and physical array +checks remain. Encodings 0xA7/0xA8/0xAA/0xAB reject at load even in dead code; +no replacement publication/guard or transaction is provided. ## Identity/hash and catalog schema 2 diff --git a/docs/vm-arch/STORAGE_QUALIFICATION.md b/docs/vm-arch/STORAGE_QUALIFICATION.md new file mode 100644 index 0000000..5331d9e --- /dev/null +++ b/docs/vm-arch/STORAGE_QUALIFICATION.md @@ -0,0 +1,63 @@ +# Storage ISA qualification + +This record qualifies the working-tree delivery over baseline `69ad1931de1e12e59c2268f94aa906aed114fb6d` on +macOS-27.0.1-arm64-arm-64bit-Mach-O with `rustc 1.98.1 (48a229cea 2026-09-01)`. The baseline commit alone does +not contain the changes. No new provider revision is claimed. Source/test/spec +SHA-256 fingerprints and exact test commands are stored in +[the qualification manifest](storage-qualification.json). + +## Delivered behavior + +Encodings 0xA7/0xA8/0xAA/0xAB are unsupported at real instruction boundaries, +including dead code. ARRAY_LEN stays 0xA4 and ARRAY_COPY stays 0xA9. The +[copy contract](../specs/runtime/02-vm-instruction-set.md#typed-array-range-copy) +defines strict ranges, complete groups/owners, snapshot overlap and precharged +4+2*N*W work for calculable counts, including receiver/range/scratch refusal. +No replacement publication, guard or transaction was introduced. + +## Verification + +`make ci-domains` passed: formatting, workspace Clippy with warnings denied, +workspace tests/all features, all five debugger socket tests, LLVM coverage, +coverage ownership audit and all 15 unchanged domain gates. + +| Run | Passed | Ignored | +| --- | ---: | ---: | +| Workspace/all targets/all features | 1095 | 13 | +| Debugger sockets (explicit ignored selection) | 5 | 0 | +| LLVM coverage/workspace/all features | 1095 | 13 | +| VM release | 400 | 2 | +| VM diagnostics checkpoint | 407 | 2 | +| Layer and quality suites | 19 | 0 | + +The diagnostics checkpoint preceded test-only fixture import/Copy cleanup; the +final all-features workspace and coverage runs include diagnostics on final code. +No identity/hash semantic change was made. Default ignored measurement probes, +audio probes and packaged-installation smoke remain explicit exclusions. The +five debugger socket tests were executed separately and passed; sandbox-local +binding had first been denied, so the complete successful gate ran with approval +outside the sandbox. Coverage gates were not lowered or skipped. + +Independent bytecode and serialized PBX tests prove decoder/admission rejection +without treating immediate/data bytes as instructions. VM tests cover cost/fault +precedence, frame temporary floors, empty and overlapping ranges, native and +multislot groups, aliases, scratch quota and allocation refusal, late source-group +failure, operand/destination preservation, native owners and roots through GC, +zero budget, overshoot and successful-step accounting. + +Overflow defense uses synthetic widths to exercise checked multiplication and +addition. It does not imply such widths are accepted Registry layouts. Internal +corruption tests are labeled separately from admitted-program evidence. There +are no JavaScript/Lua engines or frontend fixtures in this qualification. + +Local full logs and the domain audit are under `target/qualification/storage/`; +the manifest records their digests. Generated logs/LLVM output are reproducible +local evidence, not a runtime or SDK API. + +## Consumer boundary + +The [producer handoff](collection-producer-handoff.md) describes required compiler +emission and SDK algorithm/contract changes. Existing consumer evidence still +qualifies its original provider only. Compiler/SDK migration, provider pin/catalog +import, installed resources and normal/optimized source conformance require +consumer-owned delivery and are not claimed here. diff --git a/docs/vm-arch/collection-producer-handoff.md b/docs/vm-arch/collection-producer-handoff.md index 8e30f01..4d67570 100644 --- a/docs/vm-arch/collection-producer-handoff.md +++ b/docs/vm-arch/collection-producer-handoff.md @@ -15,6 +15,7 @@ Opcodes are little-endian u16, followed by little-endian u32 immediates. TypeId | ARRAY_LEN | 0xA4 | TypeId | ref → Int64 length | | ARRAY_GET | 0xA5 | TypeId | ref, Int64 index → complete element group | | ARRAY_SET | 0xA6 | TypeId | ref, Int64 index, complete element group → [] | +| ARRAY_COPY | 0xA9 | TypeId | source, sourceStart, target, targetStart, count → [] | The single-immediate forms are six bytes; field forms are ten. Width is one for scalar/String/color/concrete references, two for vec2/Trait/Dynamic, three for pixel. A Trait contains receiver then ImplementationId; Dynamic contains TypeId then payload. Preserve the whole group, including absent [Null, Null]. ImplementationId zero is valid. GET does not implicitly box; Dynamic copies share immutable boxes. NEW initializes canonical defaults without constructing referenced instances. Arrays have fixed lengths. SDKs implement List, Set and Map algorithms using these runtime capabilities. @@ -58,7 +59,7 @@ Static bad IDs/categories/fields reject load with typed LOAD_INVALID_REFERENCE c ## Consumer integration and evidence -Consumers must recognize all seven encodings, invoke Registry-derived effects in bounded structural/height/type verification and use validated-image execution. The metadata-free verifier and legacy decoder have been removed. Reuse compact typed storage, default initialization, native ownership maps and generational handles; do not replace them with raw Value arrays or SDK container implementations. +Consumers must recognize all eight encodings, invoke Registry-derived effects in bounded structural/height/type verification and use validated-image execution. The metadata-free verifier and legacy decoder have been removed. Reuse compact typed storage, default initialization, native ownership maps and generational handles; do not replace them with raw Value arrays or SDK container implementations. Executable oracles live in bytecode `tests/registry_isa.rs`, VM `tests/collection_conformance.rs`, VM `virtual_machine/collections_tests.rs` and layer `tests/typed_collections.rs`. They cover independent bytes, default/alias/group operations, attempt costs and refusals, malformed/proven types, ownership and actual FRAME_SYNC graph reclamation. VM unit tests inject otherwise unreachable invalid state only inside tests. Optimized ownership/conformance runs complement debug evidence. @@ -70,3 +71,40 @@ never invokes callbacks. Threshold GC runs at FRAME_SYNC and successful last-roo RET after rooting results. Quota refusal cannot invoke rescue GC. The published patterns and validation limits are explained in [Collection Attempts Preserve Groups and Charge Work Before Publication](../../discussion/lessons/DSC-0051-game-state-collections-and-objects/LSN-0060-collection-attempts-preserve-groups-and-charge-work-before-publication.md). + +## Range-copy migration contract + +ARRAY_LEN stays 0xA4. ARRAY_COPY is six bytes: `A9 00 04 03 02 01` for encoding +oracle TypeId 0x01020304. Actual execution needs an existing exact Array descriptor. +The five operands use Int64 offsets/count and exact Array receivers. Physical +ranges are strict; an empty range may start at length. Copy snapshots complete +source groups before writing, preserving tags, native owners and identity under +overlap without boxing. Reserve complete TemporaryBytes scratch first; release it +on success/failure. Failed attempts preserve operands/storage; no request token, +guest object, callback, GC or scheduler occurs inside the instruction. + +For available five temporary operands and exact Int64 count N in 0..u32::MAX, +precharge representable C=4+2*N*W before receiver/range/group validation or scratch +reservation. Retain it on refusal: N=10/W=1 costs 24 for success, bad receiver, +bad range or scratch quota. Other counts/underflow cost base 4 with normal fault +order. Checked cost overflow costs 4 and traps SizeOverflow before ordinary +validation. Empty copies validate receivers/ranges and cost 4; aliases give no +discount. See the [copy contract](../specs/runtime/02-vm-instruction-set.md#typed-array-range-copy) +for exact precedence. Positive budget admits the full attempt with overshoot; +Trap wins on refusal and only successful steps count. Zero budget does no work. + +Encodings 0xA7, 0xA8, 0xAA and 0xAB are unsupported in actual instruction +positions, including dead code. They are not remapped or emulated. Producers +must remove their emission, adapt logical checks/error paths and re-evaluate +algorithms, representations and collection guarantees. Sequential stores do not +preserve the former multi-owner publication guarantee. Prepare fallible values +before mutation and explicitly define the remaining source contract. + +Compiler/SDK migration, provider pinning/catalog import and source/optimized/ +installed-resource conformance belong to the consumer. Prior List/Linked evidence +qualifies earlier providers only. Identity/hash catalog schema 2 import is a +separate consumer dependency, not a storage or new-collection implementation here. +See `virtual_machine/array_copy_tests.rs` and collection/Registry conformance for +current executable oracles. Qualification provenance is recorded in +[storage qualification](STORAGE_QUALIFICATION.md); an uncommitted implementation +must not be attributed to an earlier committed provider SHA. diff --git a/docs/vm-arch/storage-qualification.json b/docs/vm-arch/storage-qualification.json new file mode 100644 index 0000000..64a7705 --- /dev/null +++ b/docs/vm-arch/storage-qualification.json @@ -0,0 +1,357 @@ +{ + "schema_version": 1, + "qualified_at_utc": "2026-10-23T22:32:12.156934+00:00", + "base_revision": "69ad1931de1e12e59c2268f94aa906aed114fb6d", + "delivery_state": "uncommitted working tree; fingerprints identify this delivery, not base_revision alone", + "platform": "macOS-27.0.1-arm64-arm-64bit-Mach-O", + "rustc": "rustc 1.98.1 (48a229cea 2026-09-01)", + "gate": "make ci-domains", + "gate_status": "passed", + "execution": "Approved outside sandbox: debugger localhost sockets are blocked in sandbox.", + "checks": [ + { + "command": "cargo test --workspace --all-targets --all-features --no-fail-fast", + "status": "passed", + "passed": 1095, + "failed": 0, + "ignored": 13 + }, + { + "command": "cargo test -p prometeu-host-desktop-winit --lib runner::tests -- --ignored", + "status": "passed", + "passed": 5, + "failed": 0, + "ignored": 0 + }, + { + "command": "cargo llvm-cov --workspace --all-features --html --output-dir target/llvm-cov", + "status": "passed", + "passed": 1095, + "failed": 0, + "ignored": 13 + }, + { + "command": "cargo test --locked --offline -p prometeu-vm --release --no-fail-fast", + "status": "passed", + "passed": 400, + "failed": 0, + "ignored": 2 + }, + { + "command": "cargo test --locked --offline -p prometeu-vm --features resource-diagnostics --no-fail-fast", + "status": "passed", + "phase": "Before Clippy-only fixture import/Copy cleanup; final all-features workspace and coverage also include diagnostics.", + "passed": 407, + "failed": 0, + "ignored": 2 + }, + { + "command": "cargo test --locked --offline -p prometeu-layer-tests -p prometeu-quality-checks --no-fail-fast", + "status": "passed", + "passed": 19, + "failed": 0, + "ignored": 0 + } + ], + "coverage": { + "system/os": { + "source_files": 19, + "report_files": 10, + "lines_count": 1711, + "lines_covered": 1410, + "functions": { + "count": 239, + "covered": 184 + }, + "regions": { + "count": 2976, + "covered": 2514 + }, + "baseline_percent": 77.24, + "line_percent": 82.4, + "gate_passed": true + }, + "system/runtime": { + "source_files": 14, + "report_files": 12, + "lines_count": 3309, + "lines_covered": 2762, + "functions": { + "count": 250, + "covered": 224 + }, + "regions": { + "count": 5722, + "covered": 4655 + }, + "baseline_percent": 76.19, + "line_percent": 83.46, + "gate_passed": true + }, + "system/fs": { + "source_files": 13, + "report_files": 9, + "lines_count": 2469, + "lines_covered": 1944, + "functions": { + "count": 249, + "covered": 204 + }, + "regions": { + "count": 4450, + "covered": 3401 + }, + "baseline_percent": 76.08, + "line_percent": 78.73, + "gate_passed": true + }, + "system/programs": { + "source_files": 3, + "report_files": 1, + "lines_count": 567, + "lines_covered": 556, + "functions": { + "count": 54, + "covered": 52 + }, + "regions": { + "count": 854, + "covered": 834 + }, + "baseline_percent": 93.05, + "line_percent": 98.05, + "gate_passed": true + }, + "asset/bank": { + "source_files": 8, + "report_files": 7, + "lines_count": 3434, + "lines_covered": 3176, + "functions": { + "count": 303, + "covered": 269 + }, + "regions": { + "count": 5718, + "covered": 5306 + }, + "baseline_percent": 86.76, + "line_percent": 92.48, + "gate_passed": true + }, + "firmware": { + "source_files": 16, + "report_files": 11, + "lines_count": 2314, + "lines_covered": 1990, + "functions": { + "count": 139, + "covered": 127 + }, + "regions": { + "count": 4255, + "covered": 3791 + }, + "baseline_percent": 83.2, + "line_percent": 85.99, + "gate_passed": true + }, + "host-dependent": { + "source_files": 13, + "report_files": 13, + "lines_count": 2393, + "lines_covered": 1531, + "functions": { + "count": 203, + "covered": 136 + }, + "regions": { + "count": 4011, + "covered": 2599 + }, + "baseline_percent": 40.05, + "line_percent": 63.97, + "gate_passed": true + }, + "host/headless": { + "source_files": 6, + "report_files": 3, + "lines_count": 855, + "lines_covered": 739, + "functions": { + "count": 93, + "covered": 69 + }, + "regions": { + "count": 1451, + "covered": 1246 + }, + "baseline_percent": 83.39, + "line_percent": 86.43, + "gate_passed": true + }, + "vm/core": { + "source_files": 64, + "report_files": 47, + "lines_count": 10137, + "lines_covered": 9028, + "functions": { + "count": 945, + "covered": 774 + }, + "regions": { + "count": 19226, + "covered": 17054 + }, + "baseline_percent": 84.29, + "line_percent": 89.05, + "gate_passed": true + }, + "bytecode": { + "source_files": 19, + "report_files": 14, + "lines_count": 3374, + "lines_covered": 3157, + "functions": { + "count": 299, + "covered": 243 + }, + "regions": { + "count": 5909, + "covered": 5070 + }, + "baseline_percent": 88.26, + "line_percent": 93.56, + "gate_passed": true + }, + "hal/core": { + "source_files": 59, + "report_files": 27, + "lines_count": 2369, + "lines_covered": 2093, + "functions": { + "count": 264, + "covered": 227 + }, + "regions": { + "count": 3341, + "covered": 3035 + }, + "baseline_percent": 83.56, + "line_percent": 88.34, + "gate_passed": true + }, + "drivers/core": { + "source_files": 13, + "report_files": 12, + "lines_count": 3107, + "lines_covered": 2775, + "functions": { + "count": 347, + "covered": 307 + }, + "regions": { + "count": 4756, + "covered": 4123 + }, + "baseline_percent": 79.51, + "line_percent": 89.31, + "gate_passed": true + }, + "system/core": { + "source_files": 12, + "report_files": 7, + "lines_count": 1444, + "lines_covered": 1353, + "functions": { + "count": 171, + "covered": 163 + }, + "regions": { + "count": 2318, + "covered": 2184 + }, + "baseline_percent": 87.71, + "line_percent": 93.69, + "gate_passed": true + }, + "tooling": { + "source_files": 8, + "report_files": 8, + "lines_count": 612, + "lines_covered": 396, + "functions": { + "count": 41, + "covered": 31 + }, + "regions": { + "count": 947, + "covered": 621 + }, + "baseline_percent": 59.75, + "line_percent": 64.7, + "gate_passed": true + }, + "dev/test-support": { + "source_files": 4, + "report_files": 2, + "lines_count": 115, + "lines_covered": 99, + "functions": { + "count": 17, + "covered": 13 + }, + "regions": { + "count": 131, + "covered": 112 + }, + "baseline_percent": 80.96, + "line_percent": 86.08, + "gate_passed": true + } + }, + "fingerprints_sha256": { + "crates/console/prometeu-bytecode/src/assembler.rs": "ae718b0881fef0b10bb8a0f5e56c348e13796b1133e2639b7fbd8683faff0792", + "crates/console/prometeu-bytecode/src/decoder.rs": "032e9e7606b68c4d722518e7d4e97eac7fa5feecc78453a41ee8e2e14f8b87d9", + "crates/console/prometeu-bytecode/src/disassembler.rs": "f5e47a2b0f0595b628f17816ffd2728d48ef92c65aa926ea1aaf7c38fe7a63ec", + "crates/console/prometeu-bytecode/src/opcode.rs": "3392129a50e365445439c9c76e379650a474f492df6f4878fe87fee7172630e2", + "crates/console/prometeu-bytecode/src/opcode_spec.rs": "d2e74d88d68a223e4411ccc159f8003db21d05428511add13016aaa862a66883", + "crates/console/prometeu-bytecode/src/registry_instruction.rs": "8bd4d3928f4e85674877be5398279988641da17be203a323edce8df795639f47", + "crates/console/prometeu-bytecode/tests/registry_isa.rs": "0a61b8e1ba6054dab542acc6b2c320e8ab9140375fc12780350a75395b10d87a", + "crates/console/prometeu-vm/src/registry_verifier.rs": "985446f71017625266483be539514f8a74c61ffcf3a21e5676bd67620ae3575f", + "crates/console/prometeu-vm/src/registry_verifier/types.rs": "6776bf04c49f8f9738a1be293d775493557b0d8ba035bf525a1e9aec1323047b", + "crates/console/prometeu-vm/src/virtual_machine.rs": "cfa594d54109bfcdca6e138bb287db6d20a003ea3248cf1970eb6c926def9729", + "crates/console/prometeu-vm/src/virtual_machine/array_copy.rs": "122bb20f9041f22a46d7e23398d016fbe365fe93834878a9d6fcc4f4c4b36c1b", + "crates/console/prometeu-vm/src/virtual_machine/array_copy_tests.rs": "e0afba27cee5bd94772c1a53498dc7a4185f9a0642b7f029378ed2c0f3be5d12", + "crates/console/prometeu-vm/src/virtual_machine/collections_tests.rs": "90ba1cba0d37b2213f0fa13de2b16c04515a5c1d33e858118768a448b06b6c8b", + "crates/console/prometeu-vm/src/virtual_machine/identity_tests.rs": "ce3501539df309f154bb1f46f7d72c450cb87c71b9d21a7ca86fc3fcc63a6c44", + "crates/console/prometeu-vm/tests/collection_conformance.rs": "bcb40dcd9dc650ffd1234fc4eeb6799742dc7a8f60536126543d6a2db6f37128", + "discussion/lessons/DSC-0065-vm-storage-publication-neutrality/LSN-0072-primitives-need-independent-semantics-and-failed-attempts-still-cost-work.md": "a55d2d5f2487301f9b3d18c81c9112a4ad4f104a967e20df849fd3c722aadf3c", + "docs/specs/runtime/01-time-model-and-cycles.md": "6c1ba3e023521d5593edb8aefd20f3d6d09fdec5be23d89487871df339f0359f", + "docs/specs/runtime/02-vm-instruction-set.md": "26bf012681f6e96ff793e0541f6685c3e15df899ac2b04b62edbe70a4a116726", + "docs/specs/runtime/02c-program-registry-and-pbx-v0.md": "e5a42d1aab637467dacde624f2662a615b74fbb3b5111fc437ef1bd0aeffb149", + "docs/specs/runtime/03-memory-stack-heap-and-allocation.md": "96bf0859ecfc949390a159166673a7bfa0ad83c651138c5b8d28c87229a5669a", + "docs/vm-arch/ARCHITECTURE.md": "d7e3487bb2d9718bba810cf4cfaec175dcbf9b079f7df12a4fad537bd1606146", + "docs/vm-arch/ISA_CORE.md": "6b3207f81d3bd782e4f9b445b19c80b64bc599d8aebc5f01d8627a477bd09c8b", + "docs/vm-arch/collection-producer-handoff.md": "47e6117fd7585b10927090519c72c8476ea5f4800e5777d201f536fd9a05dc91" + }, + "removed_files": [ + "crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs", + "crates/console/prometeu-vm/src/virtual_machine/storage_publication_tests.rs" + ], + "local_evidence_sha256": { + "target/qualification/storage/ci.log": "36adf2b1407ada9101d0b1b94011d870116964dd44655a0694221a4a541ae91d", + "target/qualification/storage/diagnostics.log": "8b51fbfd18083b0b49831109c61e29687e799ce0d654711be74caad95f4c7762", + "target/qualification/storage/domain-audit.json": "4a08f40ff6d73d5e1b3f405e3cb5556117fb3a6667c58a067e6db71274649189", + "target/qualification/storage/layers.log": "6d2a15c2fd90b86050ccb9b92b2f74670fe73f0165ff7b2cef01fdc3afcf12b6", + "target/qualification/storage/release.log": "1d9e946e90dfc3d2f3e64af0a1099437cdf85cf9a2ef149ab80189082ca4da25" + }, + "limits": [ + "No new provider commit is claimed; consumers must select and qualify a revision containing the delivery.", + "Consumer compiler/SDK migration is not executed by runtime qualification.", + "Cost arithmetic overflow uses synthetic-width defense tests, not an admitted wide Registry.", + "Malformed internal-state injection tests are distinct from admitted PBX conformance.", + "Default ignored probes/packaged-installation smoke remain explicitly ignored; five debugger socket tests were separately executed.", + "Clippy cleanup shares collection fixture imports and removes redundant Copy clones in identity tests without changing identity/hash contracts." + ] +}