#![cfg(feature = "resource-diagnostics")] #[allow(dead_code)] #[path = "support/callables.rs"] mod callables; #[path = "support/qualification.rs"] mod qualification; use prometeu_bytecode::{Value, model::ConstantPoolEntry}; use prometeu_vm::{ LogicalFrameEndingReason, VirtualMachine, resource_budget::{RuntimeLimits, RuntimeResource as Q}, }; #[test] fn depth_diagnostics_include_an_entry_that_returns_immediately() { let mut vm = callables::vm(&[("RET", callables::sig(&[], &[]), vec![])]); assert_eq!(callables::run(&mut vm, 100).reason, LogicalFrameEndingReason::EndOfRom); let snapshot = vm.resource_snapshot(); assert_eq!(snapshot.call_depth, 0); assert_eq!(snapshot.peak_call_depth, 1); } #[test] fn stationary_workloads_repeat_cycles_and_release_all_session_reservations() { for name in qualification::NAMES { let mut first = qualification::Workload::new(name); let mut second = qualification::Workload::new(name); for _ in 0..32 { first.frame(); second.frame(); } first.check_stationary(); second.check_stationary(); assert_eq!(first.digest, second.digest); assert_eq!(first.vm.resource_snapshot().budget, second.vm.resource_snapshot().budget); assert_eq!(first.vm.resource_snapshot().cycles, second.vm.resource_snapshot().cycles); first.finish(); second.finish(); } } #[test] fn no_sync_loop_returns_each_slice_without_gc_frames_or_allocation() { let mut vm = callables::vm(&[("JMP 0", callables::sig(&[], &[]), vec![])]); let before = vm.resource_snapshot(); for _ in 0..1_000 { let r = callables::run(&mut vm, 100); assert_eq!(r.reason, LogicalFrameEndingReason::BudgetExhausted); assert!(r.steps_executed > 0); assert_eq!(vm.pc(), 0); } let after = vm.resource_snapshot(); assert_eq!(after.budget.used, before.budget.used); assert_eq!(after.gc.collections, 0); assert_eq!(after.call_depth, 1); vm.insert_breakpoint(0); assert_eq!(callables::run(&mut vm, 100).reason, LogicalFrameEndingReason::Breakpoint); let observer = vm.resource_observer(); drop(vm); assert_eq!(observer.diagnostic_snapshot().used, [0; 8]); } #[test] fn host_string_snapshot_keeps_its_domain_alive_then_final_drop_refunds_it() { let image = callables::image_constants( &[("PUSH_CONST 0\nPUSH_CONST 1\nADD\nRET", callables::sig(&[], &[3]), vec![])], vec![], &[ConstantPoolEntry::String("abc".into()), ConstantPoolEntry::String("def".into())], ) .unwrap(); let mut vm = VirtualMachine::from_image(image, Default::default()); vm.prepare_boot_call().unwrap(); assert_eq!(callables::run(&mut vm, 1000).reason, LogicalFrameEndingReason::EndOfRom); let snapshot = vm.operand_stack_top(1); assert_eq!(&*snapshot, &[Value::String("abcdef".into())]); let observer = vm.resource_observer(); drop(vm); assert_eq!(observer.used(Q::StringBytes), 6); assert_eq!(observer.used(Q::StringAllocations), 1); drop(snapshot); assert_eq!(observer.diagnostic_snapshot().used, [0; 8]); } #[test] fn refused_allocation_keeps_operands_and_charges_attempt_without_rescue_gc() { let image = callables::image( &[ ("PUSH_I32 73\nMAKE_CLOSURE fn=1,type=4\nPOP\nRET", callables::sig(&[], &[]), vec![]), ( "GET_CAPTURE 0\nRET", callables::sig(&[], &[0]), vec![prometeu_bytecode::registry::TypeId(0)], ), ], vec![], ) .unwrap(); let mut vm = VirtualMachine::from_image( image, RuntimeLimits::default().lowered(Q::GuestBytes, 0).unwrap(), ); vm.prepare_boot_call().unwrap(); let before = vm.resource_snapshot(); let result = callables::run(&mut vm, 1000); let LogicalFrameEndingReason::Trap(trap) = result.reason else { panic!("quota must trap") }; assert_eq!(trap.code, prometeu_bytecode::TRAP_RESOURCE_LIMIT); assert_eq!(&*vm.operand_stack_top(1), &[Value::Int32(73)]); let after = vm.resource_snapshot(); assert!(after.cycles > before.cycles); assert_eq!(after.gc.collections, 0); assert_eq!(after.live_objects, 0); assert_eq!(after.budget.used[0], 0); assert!(after.budget.refusals[0] > 0); let observer = vm.resource_observer(); drop(vm); assert_eq!(observer.diagnostic_snapshot().used, [0; 8]); }