#[path = "support/collections.rs"] mod support; use prometeu_bytecode::{Value, assemble}; use prometeu_hal::HostContext; use prometeu_vm::{LogicalFrameEndingReason, VirtualMachine, resource_budget::RuntimeLimits}; use support::{Native, load}; fn run(code: &[u8]) -> Vec { let mut vm = VirtualMachine::from_image(load(code).unwrap(), RuntimeLimits::default()); vm.prepare_boot_call().unwrap(); let r = vm.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap(); assert_eq!(r.reason, LogicalFrameEndingReason::Halted); vm.operand_stack_top(32).to_vec() } #[test] fn independent_bytes_execute_default_array_and_len_on_sealed_image() { let code = [0x14, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0xa3, 0, 11, 0, 0, 0, 0xa4, 0, 11, 0, 0, 0, 1, 0]; assert_eq!(run(&code), [Value::Int64(2)]); } #[test] fn guest_aliases_mutate_same_instance_and_groups_roundtrip_without_boxing() { let code=assemble("STRUCT_NEW 9\nSET_GLOBAL 0\nGET_GLOBAL 0\nPUSH_F64 f64:0x3ff8000000000000\nPUSH_F64 f64:0x4004000000000000\nSTRUCT_SET type=9, field=5\nGET_GLOBAL 0\nSTRUCT_GET type=9, field=5\nPUSH_I64 2\nARRAY_NEW 17\nSET_GLOBAL 1\nGET_GLOBAL 1\nPUSH_I64 0\nGET_GLOBAL 0\nARRAY_SET 17\nGET_GLOBAL 1\nPUSH_I64 0\nARRAY_GET 17\nPUSH_I32 42\nSTRUCT_SET type=9, field=0\nGET_GLOBAL 0\nSTRUCT_GET type=9, field=0\nGET_GLOBAL 1\nARRAY_LEN 17\nHALT").unwrap(); assert_eq!( run(&code), [Value::Int64(2), Value::Int32(42), Value::Float(2.5), Value::Float(1.5)] ); } #[test] fn every_builtin_group_can_be_stored_and_loaded_by_decoded_struct_operations() { for (field, push, expected) in [ (0, "PUSH_I32 23", vec![Value::Int32(23)]), (1, "PUSH_I64 23", vec![Value::Int64(23)]), (2, "PUSH_F64 f64:0x4004000000000000", vec![Value::Float(2.5)]), (3, "PUSH_BOOL 1", vec![Value::Boolean(true)]), (4, "GET_GLOBAL 0\nSTRUCT_GET type=9, field=4", vec![Value::Null]), ( 5, "PUSH_F64 f64:0x3ff8000000000000\nPUSH_F64 f64:0x4004000000000000", vec![Value::Float(2.5), Value::Float(1.5)], ), (6, "PUSH_I64 4294967295", vec![Value::Int64(4294967295)]), ( 7, "PUSH_I64 1\nPUSH_I64 2\nPUSH_I64 3", vec![Value::Int64(3), Value::Int64(2), Value::Int64(1)], ), (8, "PUSH_F64 f64:0x3ff8000000000000\nPUSH_F64 f64:0x4004000000000000\nDYN_PACK 5", vec![]), ] { let code=assemble(&format!("STRUCT_NEW 9\nSET_GLOBAL 0\nGET_GLOBAL 0\n{push}\nSTRUCT_SET type=9, field={field}\nGET_GLOBAL 0\nSTRUCT_GET type=9, field={field}\n{}HALT",if field==8 {"DYN_UNPACK 5\n"}else{""})).unwrap(); let actual = run(&code); assert_eq!( actual, if field == 8 { vec![Value::Float(2.5), Value::Float(1.5)] } else { expected } ); } } #[test] fn verifier_rejects_proven_types_metadata_and_unreachable_invalid_fields() { for text in [ "ARRAY_NEW 0\nHALT", "STRUCT_NEW 11\nHALT", "HALT\nSTRUCT_GET type=9, field=12", "PUSH_I32 1\nARRAY_NEW 11\nHALT", "STRUCT_NEW 19\nSTRUCT_GET type=9, field=0\nHALT", "STRUCT_NEW 9\nPUSH_BOOL 1\nSTRUCT_SET type=9, field=0\nHALT", "PUSH_I64 1\nARRAY_NEW 13\nPUSH_F64 f64:0x3ff0000000000000\nARRAY_GET 13\nHALT", ] { assert!(load(&assemble(text).unwrap()).is_err(), "{text}"); } } #[test] fn empty_instances_have_identity_and_nested_array_references_preserve_aliases() { let code=assemble("STRUCT_NEW 19\nSTRUCT_NEW 19\nEQ\nPUSH_I64 0\nARRAY_NEW 11\nARRAY_LEN 11\nPUSH_I64 1\nARRAY_NEW 18\nSET_GLOBAL 0\nGET_GLOBAL 0\nPUSH_I64 0\nPUSH_I64 2\nARRAY_NEW 11\nARRAY_SET 18\nGET_GLOBAL 0\nPUSH_I64 0\nARRAY_GET 18\nARRAY_LEN 11\nHALT").unwrap(); assert_eq!(run(&code), [Value::Int64(2), Value::Int64(0), Value::Boolean(false)]); } #[test] fn converged_unknown_receiver_join_keeps_runtime_checks_and_valid_branch_executes() { let text = "PUSH_BOOL 1\nJMP_IF_FALSE 31\nPUSH_I64 1\nARRAY_NEW 11\nJMP 47\nPUSH_I64 1\nARRAY_NEW 14\nARRAY_LEN 11\nHALT"; assert_eq!(run(&assemble(text).unwrap()), [Value::Int64(1)]); let mut vm = VirtualMachine::from_image( load(&assemble(&text.replace("PUSH_BOOL 1", "PUSH_BOOL 0")).unwrap()).unwrap(), RuntimeLimits::default(), ); vm.prepare_boot_call().unwrap(); let r = vm.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap(); let LogicalFrameEndingReason::Trap(t) = r.reason else { panic!() }; assert_eq!(t.code, prometeu_bytecode::TRAP_TYPE); assert_eq!(t.opcode, 0xa4); assert_eq!(t.pc, 47); } #[test] fn verifier_rejects_group_tearing_jumps_into_immediates_and_unequal_height_joins() { for text in [ "STRUCT_NEW 9\nSTRUCT_GET type=9, field=5\nPOP_N 1\nPUSH_I32 1\nSTRUCT_NEW 9\nSWAP\nSTRUCT_SET type=9, field=5\nHALT", "JMP 8\nSTRUCT_NEW 9\nHALT", "PUSH_BOOL 1\nJMP_IF_FALSE 21\nSTRUCT_NEW 9\nJMP 21\nHALT", ] { assert!(load(&assemble(text).unwrap()).is_err(), "{text}"); } } #[test] fn collection_load_limits_and_analysis_refusals_keep_context_and_refund_scratch() { use prometeu_bytecode::load_limits::{LoadBudget, LoadCode, LoadLimits, LoadResource}; use prometeu_hal::syscalls::caps::NONE; use prometeu_vm::{ValidatedProgramImage, VmInitError}; let code = assemble("PUSH_I64 1\nARRAY_NEW 13\nPUSH_I64 0\nARRAY_GET 13\nHALT").unwrap(); let mut d = support::declarations(code.len()); d.functions[0].max_stack_slots = 2; assert!(load(&code).is_ok()); let bytes = support::bytes_with(&code, d); let e = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(Default::default()), NONE) .unwrap_err(); let VmInitError::RegistryLoadFailed(e) = e else { panic!() }; assert_eq!(e.code, LoadCode::InvalidCode); assert_eq!(e.function, Some(0)); assert_eq!(e.pc, Some(26)); let bytes = support::bytes(&code); for limits in [ LoadLimits { work: 1, ..Default::default() }, LoadLimits { temporary_bytes: 1, ..Default::default() }, LoadLimits { registry_bytes: 1, ..Default::default() }, ] { let mut budget = LoadBudget::new(limits); let VmInitError::RegistryLoadFailed(e) = ValidatedProgramImage::load(&bytes, &mut budget, NONE).unwrap_err() else { panic!() }; assert_eq!(e.code, LoadCode::ResourceLimit); assert_eq!(budget.used(LoadResource::TemporaryBytes), 0); 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}"); } }