diff --git a/crates/console/prometeu-vm/tests/registry_conformance.rs b/crates/console/prometeu-vm/tests/registry_conformance.rs new file mode 100644 index 0000000..f08477a --- /dev/null +++ b/crates/console/prometeu-vm/tests/registry_conformance.rs @@ -0,0 +1,112 @@ +use prometeu_bytecode::Value; +use prometeu_bytecode::load_limits::{LoadBudget, LoadCode, LoadLimits, LoadReason, LoadResource}; +use prometeu_hal::{HostContext, HostReturn, NativeInterface, syscalls::caps, vm_fault::VmFault}; +use prometeu_vm::{ + LogicalFrameEndingReason, ValidatedProgramImage, VirtualMachine, VmInitError, + resource_budget::{RuntimeLimits, RuntimeResource}, +}; + +// Independent executable oracle: 32-byte header, seven directory entries, +// payload at 116, one Bool import, one zero-slot HALT function; total 173. +fn minimal() -> Vec { + "50 42 58 00 01 00 00 00 07 00 00 00 +00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +00 00 00 00 74 00 00 00 04 00 00 00 +01 00 00 00 78 00 00 00 1d 00 00 00 +02 00 00 00 95 00 00 00 02 00 00 00 +04 00 00 00 97 00 00 00 04 00 00 00 +05 00 00 00 9b 00 00 00 04 00 00 00 +06 00 00 00 9f 00 00 00 0a 00 00 00 +07 00 00 00 a9 00 00 00 04 00 00 00 +00 00 00 00 +01 00 00 00 00 00 00 00 00 00 00 00 00 +00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 +01 00 +00 00 00 00 +00 00 00 00 +01 00 00 00 00 00 13 00 00 00 +00 00 00 00" + .split_whitespace() + .map(|byte| u8::from_str_radix(byte, 16).unwrap()) + .collect() +} +struct Native; +impl NativeInterface for Native { + fn syscall( + &mut self, + _id: u32, + _args: &[Value], + _ret: &mut HostReturn, + _ctx: &mut HostContext, + ) -> Result<(), VmFault> { + panic!("HALT oracle has no native call") + } +} + +#[test] +fn independent_v1_bytes_load_publish_and_execute_through_the_public_sealed_boundary() { + let bytes = minimal(); + assert_eq!(bytes.len(), 173); + let mut load = LoadBudget::new(Default::default()); + let image = ValidatedProgramImage::load(&bytes, &mut load, caps::NONE).unwrap(); + assert_eq!(load.used(LoadResource::TemporaryBytes), 0); + assert_eq!(image.registry().declarations().functions[0].max_stack_slots, 0); + let mut vm = VirtualMachine::from_image(image, RuntimeLimits::default()); + vm.prepare_boot_call().unwrap(); + let report = vm.run_budget(100, &mut Native, &mut HostContext::new(None)).unwrap(); + assert_eq!(report.reason, LogicalFrameEndingReason::Halted); + assert_eq!(report.steps_executed, 1); + assert!(vm.operand_stack_top(1).is_empty()); +} + +#[test] +fn single_violation_oracles_reject_before_image_publication_with_typed_context() { + for (offset, replacement, code, reason, pc) in [ + (4, 0, LoadCode::UnsupportedFormat, LoadReason::Version, None), + (165, 0, LoadCode::UnsupportedFormat, LoadReason::Builtin, None), + (149, 0xff, LoadCode::InvalidCode, LoadReason::UnknownOpcode, Some(0)), + (149, 0x11, LoadCode::InvalidCode, LoadReason::StackUnderflow, Some(0)), + ] { + let mut bytes = minimal(); + bytes[offset] = replacement; + let mut load = LoadBudget::new(Default::default()); + load.charge(LoadResource::TemporaryBytes, 7).unwrap(); + load.charge(LoadResource::RegistryBytes, 3).unwrap(); + let VmInitError::RegistryLoadFailed(error) = + ValidatedProgramImage::load(&bytes, &mut load, caps::NONE).unwrap_err() + else { + panic!() + }; + assert_eq!((error.code, error.reason, error.pc), (code, reason, pc)); + if pc.is_some() { + assert_eq!(error.function, Some(0)); + assert_eq!(error.section, Some(2)); + } + assert_eq!(load.used(LoadResource::TemporaryBytes), 7); + assert_eq!(load.used(LoadResource::RegistryBytes), 3); + assert_eq!(load.used(LoadResource::Functions), 0); + assert_eq!(load.used(LoadResource::PbxBytes), 0); + } +} + +#[test] +fn load_scratch_and_session_depth_are_separate_admission_failures() { + let mut load = LoadBudget::new(LoadLimits { temporary_bytes: 0, ..Default::default() }); + assert!( + matches!(ValidatedProgramImage::load(&minimal(),&mut load,caps::NONE),Err(VmInitError::RegistryLoadFailed(error)) if error.resource==Some(LoadResource::TemporaryBytes)) + ); + assert_eq!(load.used(LoadResource::RegistryBytes), 0); + let image = ValidatedProgramImage::load( + &minimal(), + &mut LoadBudget::new(Default::default()), + caps::NONE, + ) + .unwrap(); + let limits = RuntimeLimits::default().lowered(RuntimeResource::CallDepth, 0).unwrap(); + let mut vm = VirtualMachine::from_image(image, limits); + assert!( + matches!(vm.prepare_boot_call(),Err(VmInitError::RuntimeResourceLimit(error)) if error.resource==RuntimeResource::CallDepth && error.requested==1 && error.limit==0) + ); + assert!(vm.call_stack_is_empty()); + assert_eq!(vm.heap().len(), 0); +}