Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
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.
259 lines
9.4 KiB
Rust
259 lines
9.4 KiB
Rust
use prometeu_test_support::program::{TestProgram, int64_signature};
|
|
use prometeu_vm::ValidatedProgramImage;
|
|
// Independent catalogue and execution oracles for the fixed input ABI.
|
|
use prometeu_bytecode::{TRAP_UNAVAILABLE, Value, assembler::assemble};
|
|
use prometeu_drivers::TestPlatform;
|
|
use prometeu_hal::{InputSignals, RuntimePlatform, vm_fault::VmFault};
|
|
use prometeu_vm::{
|
|
AbiType, BuiltinScalarType, HostContext, HostReturn, IntrinsicExecutionError,
|
|
LogicalFrameEndingReason, NativeInterface, VirtualMachine, lookup_builtin_type,
|
|
lookup_intrinsic, lookup_intrinsic_by_id,
|
|
};
|
|
|
|
const BUTTONS: [(&str, u32); 13] = [
|
|
("input.pad.up", 0x2100),
|
|
("input.pad.down", 0x2104),
|
|
("input.pad.left", 0x2108),
|
|
("input.pad.right", 0x210C),
|
|
("input.pad.a", 0x2110),
|
|
("input.pad.b", 0x2114),
|
|
("input.pad.x", 0x2118),
|
|
("input.pad.y", 0x211C),
|
|
("input.pad.l", 0x2120),
|
|
("input.pad.r", 0x2124),
|
|
("input.pad.start", 0x2128),
|
|
("input.pad.select", 0x212C),
|
|
("input.touch.button", 0x2130),
|
|
];
|
|
|
|
struct NoSyscalls;
|
|
impl NativeInterface for NoSyscalls {
|
|
fn syscall(
|
|
&mut self,
|
|
_: u32,
|
|
_: &[Value],
|
|
_: &mut HostReturn,
|
|
_: &mut HostContext,
|
|
) -> Result<(), VmFault> {
|
|
panic!("input must not invoke a syscall")
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn catalogue_has_exact_names_ids_signatures_and_no_endpoint_aliases() {
|
|
let mut ids = std::collections::BTreeSet::new();
|
|
for (owner, base) in BUTTONS {
|
|
for (offset, property) in ["pressed", "released", "down", "hold"].into_iter().enumerate() {
|
|
let meta = lookup_intrinsic(owner, property, 1).unwrap();
|
|
assert_eq!(meta.id, base + offset as u32);
|
|
assert!(ids.insert(meta.id));
|
|
assert_eq!(lookup_intrinsic_by_id(meta.id).unwrap().key(), meta.key());
|
|
assert!(meta.arg_layout.is_empty());
|
|
assert_eq!(
|
|
meta.ret_layout,
|
|
&[AbiType::Scalar(if offset == 3 {
|
|
BuiltinScalarType::Int64
|
|
} else {
|
|
BuiltinScalarType::Bool
|
|
})]
|
|
);
|
|
assert!(meta.deterministic);
|
|
assert!(!meta.may_allocate);
|
|
assert!(lookup_intrinsic(owner, property, 2).is_none());
|
|
}
|
|
}
|
|
for (name, id) in [("x", 0x2134), ("y", 0x2135)] {
|
|
let meta = lookup_intrinsic("input.touch", name, 1).unwrap();
|
|
assert_eq!(meta.id, id);
|
|
assert!(ids.insert(id));
|
|
assert!(meta.arg_layout.is_empty());
|
|
assert_eq!(meta.ret_layout, &[AbiType::Scalar(BuiltinScalarType::Int64)]);
|
|
}
|
|
assert_eq!(ids.len(), 54);
|
|
assert!(lookup_intrinsic_by_id(0x2136).is_none());
|
|
for id in [0x2000, 0x2001]
|
|
.into_iter()
|
|
.chain(0x2010..=0x201B)
|
|
.chain(0x2020..=0x2022)
|
|
.chain(0x2030..=0x2033)
|
|
{
|
|
assert!(lookup_intrinsic_by_id(id).is_none(), "removed ID {id:#x}");
|
|
}
|
|
for name in ["input.pad", "input.touch", "input.button", "input.touch.button"] {
|
|
assert!(lookup_builtin_type(name, 1).is_none());
|
|
}
|
|
for (owner, name) in [
|
|
("input", "pad"),
|
|
("input", "touch"),
|
|
("input.pad", "up"),
|
|
("input.touch", "button"),
|
|
("input.button", "down"),
|
|
] {
|
|
assert!(lookup_intrinsic(owner, name, 1).is_none());
|
|
}
|
|
let csv = include_str!("../../../../docs/vm-arch/INTRINSICS.csv");
|
|
let mut documented = std::collections::BTreeSet::new();
|
|
for row in csv.lines().skip(1) {
|
|
let columns: Vec<_> = row.split(',').collect();
|
|
if !columns[2].starts_with("input.") {
|
|
continue;
|
|
}
|
|
let id = columns[1].parse::<u32>().unwrap();
|
|
let meta = lookup_intrinsic(columns[4], columns[5], 1).unwrap();
|
|
assert_eq!(id, meta.id);
|
|
assert_eq!(columns[2], format!("{}.{}", meta.owner, meta.name));
|
|
assert_eq!(&columns[6..9], &["0", "1", ""]);
|
|
assert_eq!(
|
|
columns[9],
|
|
if meta.name == "hold" || meta.owner == "input.touch" { "Int64" } else { "Bool" }
|
|
);
|
|
assert!(documented.insert(id));
|
|
}
|
|
assert_eq!(ids, documented);
|
|
}
|
|
|
|
fn set_frame(platform: &mut TestPlatform, active: Option<usize>) {
|
|
let mut signals = InputSignals { x_pos: -123, y_pos: i32::MAX, ..Default::default() };
|
|
if let Some(index) = active {
|
|
let fields = [
|
|
&mut signals.up_signal,
|
|
&mut signals.down_signal,
|
|
&mut signals.left_signal,
|
|
&mut signals.right_signal,
|
|
&mut signals.a_signal,
|
|
&mut signals.b_signal,
|
|
&mut signals.x_signal,
|
|
&mut signals.y_signal,
|
|
&mut signals.l_signal,
|
|
&mut signals.r_signal,
|
|
&mut signals.start_signal,
|
|
&mut signals.select_signal,
|
|
&mut signals.f_signal,
|
|
];
|
|
*fields[index] = true;
|
|
}
|
|
platform.input_mut().pad_mut().begin_frame(&signals);
|
|
platform.input_mut().touch_mut().begin_frame(&signals);
|
|
}
|
|
|
|
fn query_all(platform: &mut TestPlatform) -> Vec<Value> {
|
|
let mut ctx = HostContext::new(Some(platform));
|
|
(0x2100..=0x2135)
|
|
.map(|id| {
|
|
let meta = lookup_intrinsic_by_id(id).unwrap();
|
|
let result =
|
|
(meta.implementation.host_callback().expect("host intrinsic"))(&[], &mut ctx)
|
|
.unwrap();
|
|
meta.validate_result_values(&result).unwrap();
|
|
result.into_iter().next().unwrap()
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn every_button_uses_its_own_snapshot_and_queries_do_not_advance_frames() {
|
|
let mut platform = TestPlatform::new();
|
|
for active in 0..13 {
|
|
set_frame(&mut platform, None);
|
|
for (pressed, released, down, hold) in [
|
|
(true, false, true, 1),
|
|
(false, false, true, 2),
|
|
(false, true, false, 0),
|
|
(false, false, false, 0),
|
|
] {
|
|
set_frame(&mut platform, down.then_some(active));
|
|
let result = query_all(&mut platform);
|
|
assert_eq!(query_all(&mut platform), result, "same-frame queries changed state");
|
|
for index in 0..13 {
|
|
let values = if index == active {
|
|
(pressed, released, down, hold)
|
|
} else {
|
|
(false, false, false, 0)
|
|
};
|
|
assert_eq!(
|
|
&result[index * 4..index * 4 + 4],
|
|
&[
|
|
Value::Boolean(values.0),
|
|
Value::Boolean(values.1),
|
|
Value::Boolean(values.2),
|
|
Value::Int64(values.3),
|
|
],
|
|
"button {index}, active {active}"
|
|
);
|
|
}
|
|
assert_eq!(&result[52..], &[Value::Int64(-123), Value::Int64(i32::MAX as i64)]);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn vm_queries_charge_one_cycle_each_and_allocate_no_guest_objects() {
|
|
let mut source = String::new();
|
|
for id in 0x2100..=0x2135 {
|
|
source.push_str(&format!("INTRINSIC {id}\n"));
|
|
}
|
|
let mut vm = query_vm(assemble(&(source + "HALT\n")).unwrap());
|
|
vm.prepare_call("0").expect("prepare entry call");
|
|
let objects = vm.heap().len();
|
|
let mut platform = TestPlatform::new();
|
|
set_frame(&mut platform, Some(3));
|
|
let expected = query_all(&mut platform);
|
|
let report =
|
|
vm.run_budget(54, &mut NoSyscalls, &mut HostContext::new(Some(&mut platform))).unwrap();
|
|
assert_eq!(report.cycles_used, 54);
|
|
assert_eq!(report.steps_executed, 54);
|
|
assert_eq!(vm.heap().len(), objects);
|
|
// Inspection returns the topmost slot first.
|
|
assert_eq!(vm.operand_stack_top(54), expected.into_iter().rev().collect::<Vec<_>>());
|
|
}
|
|
|
|
#[test]
|
|
fn every_known_query_faults_when_unavailable_and_rejects_arguments() {
|
|
for id in 0x2100..=0x2135 {
|
|
let meta = lookup_intrinsic_by_id(id).unwrap();
|
|
let mut ctx = HostContext::new(None);
|
|
assert_eq!(
|
|
(meta.implementation.host_callback().expect("host intrinsic"))(&[], &mut ctx),
|
|
Err(IntrinsicExecutionError::HardwareUnavailable)
|
|
);
|
|
assert_eq!(
|
|
(meta.implementation.host_callback().expect("host intrinsic"))(
|
|
&[Value::Int64(0)],
|
|
&mut ctx
|
|
),
|
|
Err(IntrinsicExecutionError::ArityMismatch { expected: 0, got: 1 })
|
|
);
|
|
let mut vm = query_vm(assemble(&format!("INTRINSIC {id}\nHALT")).unwrap());
|
|
vm.prepare_call("0").expect("prepare entry call");
|
|
let report = vm.run_budget(10, &mut NoSyscalls, &mut ctx).unwrap();
|
|
assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap)
|
|
if trap.code == TRAP_UNAVAILABLE && trap.pc == 0));
|
|
assert!(vm.operand_stack_top(1).is_empty());
|
|
}
|
|
let bytes = TestProgram::single(
|
|
assemble("INTRINSIC 0x2136\nHALT").unwrap(),
|
|
int64_signature(0, 0),
|
|
0,
|
|
64,
|
|
)
|
|
.serialize();
|
|
let error = ValidatedProgramImage::load(
|
|
&bytes,
|
|
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
|
|
0,
|
|
)
|
|
.unwrap_err();
|
|
assert!(matches!(error, prometeu_vm::VmInitError::RegistryLoadFailed(_)));
|
|
}
|
|
|
|
fn query_vm(code: Vec<u8>) -> VirtualMachine {
|
|
let bytes = TestProgram::single(code, int64_signature(0, 0), 0, 64).serialize();
|
|
let image = ValidatedProgramImage::load(
|
|
&bytes,
|
|
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
|
|
0,
|
|
)
|
|
.unwrap();
|
|
VirtualMachine::from_image(image, Default::default())
|
|
}
|