Files
prometeu-runtime/crates/console/prometeu-vm/tests/builtin_contract.rs
T
bquarkz 3e39a90083
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
feat(vm): add VM-owned PCG64 random and coverage headroom
Implement GC-managed random generators, next/nextf intrinsics, bounded ranges and deterministic conformance coverage. Publish the runtime contract and producer guidance.

Give coverage domains at least five percentage points of measured headroom while preserving sufficient floors. Record local qualification: make ci-domains passes with 939 tests and all domain gates green.
2026-10-09 12:51:05 +01:00

199 lines
7.6 KiB
Rust

use prometeu_bytecode::registry::{BuiltinId, DynamicSlot, StringSlot};
use prometeu_bytecode::{TRAP_TYPE, Value, assembler::assemble};
use prometeu_hal::{color::Color, vm_fault::VmFault};
use prometeu_vm::{
AbiType, BuiltinScalarType, BuiltinValueError, HostContext, HostReturn,
LogicalFrameEndingReason, NativeInterface, VirtualMachine, lookup_builtin_type,
lookup_intrinsic, materialize_builtin_constant,
};
#[test]
fn all_ten_descriptors_match_registry_storage_and_canonical_defaults() {
for (name, id, size, alignment, width, nullable) in [
("color", 1, 4, 4, 1, false),
("vec2", 2, 16, 8, 2, false),
("pixel", 3, 24, 8, 3, false),
("Random", 0x16, 8, 8, 1, true),
("Int32", 0x10, 4, 4, 1, false),
("Int64", 0x11, 8, 8, 1, false),
("Float", 0x12, 8, 8, 1, false),
("Bool", 0x13, 1, 1, 1, false),
("String", 0x14, size_of::<StringSlot>(), align_of::<StringSlot>(), 1, true),
("Dynamic", 0x15, size_of::<DynamicSlot>(), align_of::<DynamicSlot>(), 2, true),
] {
let meta = lookup_builtin_type(name, 1).unwrap();
assert_eq!(meta.id, id);
let layout = meta.storage_layout().unwrap();
assert_eq!(layout, BuiltinId(id).layout().unwrap());
assert_eq!(
(layout.size, layout.alignment, layout.slots, layout.nullable()),
(size, alignment, width, nullable)
);
assert_eq!(u32::from(meta.flat_slot_width), width);
assert_eq!(meta.flatten_layout().len(), width as usize);
meta.validate_flat_values(layout.default_values()).unwrap();
assert!(lookup_builtin_type(name, 2).is_none());
}
for alias in [
"Int",
"Number",
"Void",
"Null",
"int",
"float",
"bool",
"str",
"input.pad",
"input.touch",
"input.button",
] {
assert!(lookup_builtin_type(alias, 1).is_none(), "{alias}");
}
}
#[test]
fn primitive_carriers_are_exact_and_only_string_accepts_scalar_null() {
let cases = [
("Int32", Value::Int32(8)),
("Int64", Value::Int64(8)),
("Float", Value::Float(8.0)),
("Bool", Value::Boolean(true)),
("String", Value::String("eight".into())),
];
for (name, valid) in &cases {
let meta = lookup_builtin_type(name, 1).unwrap();
meta.validate_flat_values(std::slice::from_ref(valid)).unwrap();
for (other_name, invalid) in &cases {
if other_name != name {
assert!(
meta.validate_flat_values(std::slice::from_ref(invalid)).is_err(),
"{name} <- {other_name}"
);
}
}
assert_eq!(meta.validate_flat_values(&[Value::Null]).is_ok(), *name == "String");
}
let dynamic = lookup_builtin_type("Dynamic", 1).unwrap();
dynamic.validate_flat_values(&[Value::Null, Value::Null]).unwrap();
assert!(dynamic.validate_flat_values(&[Value::Null]).is_err());
assert_eq!(
dynamic.validate_flat_values(&[Value::Int32(0), Value::Int32(8)]),
Err(BuiltinValueError::RegistryRequired)
);
assert_eq!(
dynamic.validate_flat_values(&[Value::Null, Value::Int32(8)]),
Err(BuiltinValueError::RegistryRequired)
);
}
#[test]
fn color_range_and_aggregate_component_carriers_do_not_narrow() {
let color = lookup_builtin_type("color", 1).unwrap();
for raw in [0, 0x11223344, u32::MAX as i64] {
color.validate_flat_values(&[Value::Int64(raw)]).unwrap();
}
for value in [
Value::Int32(1),
Value::Int64(-1),
Value::Int64(u32::MAX as i64 + 1),
Value::Float(1.0),
Value::Null,
] {
assert!(color.validate_flat_values(&[value]).is_err());
}
let pixel = lookup_builtin_type("pixel", 1).unwrap();
let wide = [Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(u32::MAX as i64)];
pixel.validate_flat_values(&wide).unwrap();
assert!(
pixel.validate_flat_values(&[wide[2].clone(), wide[1].clone(), wide[0].clone()]).is_err()
);
for (field, slot) in [("x", 0), ("y", 1), ("color", 2)] {
assert_eq!(pixel.field(field).unwrap().start_slot, slot);
}
let vec2 = lookup_builtin_type("vec2", 1).unwrap();
vec2.validate_flat_values(&[Value::Float(1.25), Value::Float(-2.5)]).unwrap();
assert!(vec2.validate_flat_values(&[Value::Int64(1), Value::Float(2.0)]).is_err());
assert_eq!(
materialize_builtin_constant("color", "red", 1).unwrap().unwrap(),
vec![Value::Int64(Color::RED.raw() as i64)]
);
}
struct NoSyscalls;
impl NativeInterface for NoSyscalls {
fn syscall(
&mut self,
_: u32,
_: &[Value],
_: &mut HostReturn,
_: &mut HostContext,
) -> Result<(), VmFault> {
panic!("unexpected syscall")
}
}
#[test]
fn intrinsic_boundaries_reject_implicit_carrier_conversions_in_execution() {
for source in [
"PUSH_I32 1\nINTRINSIC 0x1100", // color.from_raw requires Int64
"PUSH_I32 1\nINTRINSIC 0x1106", // color.alpha requires the color carrier
"PUSH_I64 1\nPUSH_F64 f64:0x4000000000000000\nINTRINSIC 0x1001", // Float/Float required
"PUSH_I64 -1\nINTRINSIC 0x1106", // out-of-range color
] {
let bytes = prometeu_test_support::program::TestProgram::single(
assemble(&(source.to_string() + "\nHALT")).unwrap(),
Default::default(),
0,
8,
)
.serialize();
let known = prometeu_vm::ValidatedProgramImage::load(
&bytes,
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
0,
);
if let Err(error) = known {
assert!(
matches!(error,prometeu_vm::VmInitError::RegistryLoadFailed(e) if e.code==prometeu_bytecode::load_limits::LoadCode::InvalidCode && e.reason==prometeu_bytecode::load_limits::LoadReason::TypeMismatch)
);
}
// Globals deliberately hide the concrete carrier from static facts;
// the same rejected values must still fault at the runtime boundary.
let lines: Vec<_> = source.lines().collect();
let mut unknown = String::new();
for (i, line) in lines[..lines.len() - 1].iter().enumerate() {
unknown.push_str(&format!("{line}\nSET_GLOBAL {i}\n"));
}
for i in 0..lines.len() - 1 {
unknown.push_str(&format!("GET_GLOBAL {i}\n"));
}
unknown.push_str(lines.last().unwrap());
unknown.push_str("\nHALT");
let bytes = prometeu_test_support::program::TestProgram::single(
assemble(&unknown).unwrap(),
Default::default(),
0,
8,
)
.serialize();
let image = prometeu_vm::ValidatedProgramImage::load(
&bytes,
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
0,
)
.unwrap();
let mut vm = VirtualMachine::from_image(image, Default::default());
vm.prepare_call("0").expect("prepare entry call");
let report = vm.run_budget(100, &mut NoSyscalls, &mut HostContext::new(None)).unwrap();
assert!(
matches!(report.reason, LogicalFrameEndingReason::Trap(trap) if trap.code == TRAP_TYPE),
"{source}"
);
}
let hold = lookup_intrinsic("input.pad.up", "hold", 1).unwrap();
assert_eq!(hold.ret_layout, &[AbiType::Scalar(BuiltinScalarType::Int64)]);
assert!(hold.validate_result_values(&[Value::Int32(1)]).is_err());
let color = lookup_intrinsic("color", "red", 1).unwrap();
assert!(color.validate_result_values(&[Value::Int64(-1)]).is_err());
}