Support explicit contract type parameters, canonical nominal applications, recursive substitution, concrete implementations and instantiated editor views. Complete parametric Array syntax and contextual construction integration. Preserve type origins across imports and reexports, fix trait argument stack order, and relocate global slots before lifecycle assembly. Publish specs, installed examples and consolidated discussion lessons. Validate frontend, common pipeline, architecture, LSP and installed knowledge gates, plus the full production VM conformance corpus with 14 generic fixtures.
949 lines
51 KiB
Rust
949 lines
51 KiB
Rust
use prometeu_bytecode::Value;
|
|
use prometeu_bytecode::load_limits::{LoadBudget, LoadLimits};
|
|
use prometeu_hal::syscalls::caps;
|
|
use prometeu_vm::{
|
|
HostContext, HostReturn, NativeInterface, ValidatedProgramImage, VirtualMachine,
|
|
};
|
|
|
|
struct HasCart(usize);
|
|
impl NativeInterface for HasCart {
|
|
fn syscall(
|
|
&mut self,
|
|
id: u32,
|
|
args: &[Value],
|
|
ret: &mut HostReturn,
|
|
_: &mut HostContext,
|
|
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
|
|
assert_eq!(id, 1);
|
|
assert!(args.is_empty());
|
|
self.0 += 1;
|
|
ret.push_bool(false);
|
|
Ok(())
|
|
}
|
|
}
|
|
struct NoNative;
|
|
impl NativeInterface for NoNative {
|
|
fn syscall(
|
|
&mut self,
|
|
_: u32,
|
|
_: &[Value],
|
|
_: &mut HostReturn,
|
|
_: &mut HostContext,
|
|
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
|
|
panic!("fixture unexpectedly invoked native code")
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let root = std::path::PathBuf::from(std::env::args().nth(1).expect("fixture directory"));
|
|
if std::env::args().any(|arg| arg == "--generic-contracts-only") { verify_pbs_generic_contract_fixtures(&root); return; }
|
|
if std::env::args().any(|arg| arg == "--random-only") { verify_pbs_random_fixtures(&root); return; }
|
|
verify_pbs_math_fixtures(&root);
|
|
if std::env::args().any(|arg| arg == "--math-only") { return; }
|
|
verify_pbs_random_fixtures(&root);
|
|
verify_catalog_fixtures(&root);
|
|
verify_pbs_fixtures(&root);
|
|
verify_pbs_structured_fixtures(&root);
|
|
verify_pbs_generic_contract_fixtures(&root);
|
|
verify_pbs_resource_refusals(&root);
|
|
verify_pbs_closure_frames(&root);
|
|
for name in [
|
|
"minimal",
|
|
"jump-end",
|
|
"vec2-signature",
|
|
"closure-capture",
|
|
"trait-call",
|
|
"array-dynamic",
|
|
"common-pipeline",
|
|
"roles",
|
|
"join",
|
|
"memcard-role",
|
|
"intrinsic-dot",
|
|
] {
|
|
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
|
|
let image = ValidatedProgramImage::load(
|
|
&bytes,
|
|
&mut LoadBudget::new(LoadLimits::default()),
|
|
caps::NONE,
|
|
)
|
|
.unwrap_or_else(|e| panic!("{name}: {e:?}"));
|
|
println!("LOAD PASS {name}");
|
|
if name != "vec2-signature" {
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm
|
|
.run_budget(1000, &mut NoNative, &mut HostContext::new(None))
|
|
.unwrap();
|
|
let reason = format!("{:?}", report.reason);
|
|
assert!(
|
|
reason == "Halted" || reason == "EndOfRom",
|
|
"{name}: {report:?}"
|
|
);
|
|
if name == "closure-capture" || name == "trait-call" {
|
|
assert!(matches!(vm.operand_stack_top(1)[0], Value::Int32(42)));
|
|
}
|
|
if name == "intrinsic-dot" {
|
|
assert!(
|
|
matches!(vm.operand_stack_top(1)[0], Value::Float(value) if value.to_bits() == 11f64.to_bits())
|
|
);
|
|
}
|
|
if name == "array-dynamic" {
|
|
assert!(matches!(vm.operand_stack_top(1)[0], Value::Int32(0)));
|
|
}
|
|
println!(
|
|
"EXEC PASS {name}: {reason}, {} steps",
|
|
report.steps_executed
|
|
);
|
|
}
|
|
}
|
|
for name in ["native", "unknown-native", "native-abi", "unused-native"] {
|
|
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
|
|
if name == "native" {
|
|
let image = ValidatedProgramImage::load(
|
|
&bytes,
|
|
&mut LoadBudget::new(LoadLimits::default()),
|
|
caps::SYSTEM,
|
|
)
|
|
.unwrap();
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let mut native = HasCart(0);
|
|
let report = vm
|
|
.run_budget(1000, &mut native, &mut HostContext::new(None))
|
|
.unwrap();
|
|
assert_eq!(native.0, 1);
|
|
assert_eq!(format!("{:?}", report.reason), "Halted");
|
|
println!("BIND + EXEC PASS native with SYSTEM");
|
|
}
|
|
let capabilities = if name == "native" {
|
|
caps::NONE
|
|
} else {
|
|
caps::SYSTEM
|
|
};
|
|
let error = ValidatedProgramImage::load(
|
|
&bytes,
|
|
&mut LoadBudget::new(LoadLimits::default()),
|
|
capabilities,
|
|
)
|
|
.unwrap_err();
|
|
record_fault(&root, name, error);
|
|
}
|
|
let baseline = std::fs::read(root.join("minimal.pbx")).unwrap();
|
|
for (name, offset, value) in [
|
|
("version", 4, 1),
|
|
("unsupported-section", 116, 9),
|
|
("unknown-builtin", 178, 0),
|
|
("invalid-role", 136, 4),
|
|
("retired-opcode", 162, 0x54),
|
|
] {
|
|
let mut bytes = baseline.clone();
|
|
bytes[offset] = value;
|
|
reject(&root, name, &bytes, LoadLimits::default());
|
|
}
|
|
for (name, code, max) in [
|
|
("raw-syscall", vec![0x70, 0, 1, 0, 0, 0, 1, 0], 0),
|
|
("hostcall-index", vec![0x71, 0, 0, 0, 0, 0, 1, 0], 0),
|
|
(
|
|
"retired-spawn",
|
|
vec![0x54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
|
0,
|
|
),
|
|
("retired-yield", vec![0x55, 0, 1, 0], 0),
|
|
("retired-sleep", vec![0x56, 0, 0, 0, 0, 0, 1, 0], 0),
|
|
(
|
|
"unreachable-reference",
|
|
vec![1, 0, 0x50, 0, 255, 255, 255, 255],
|
|
0,
|
|
),
|
|
(
|
|
"nonfinite",
|
|
vec![1, 0, 0x15, 0, 0, 0, 0, 0, 0, 0, 0xf0, 0x7f],
|
|
0,
|
|
),
|
|
("jump-immediate", vec![2, 0, 3, 0, 0, 0, 1, 0], 0),
|
|
("zero-max", vec![0x17, 0, 0, 0, 0, 0, 1, 0], 0),
|
|
("return-height", vec![0x17, 0, 0, 0, 0, 0, 0x51, 0], 1),
|
|
("fallthrough", vec![0, 0], 0),
|
|
] {
|
|
let mut functions = section(&baseline, 1).to_vec();
|
|
functions[18..22].copy_from_slice(&(code.len() as u32).to_le_bytes());
|
|
functions[26..30].copy_from_slice(&(max as u32).to_le_bytes());
|
|
let bytes = replace(&replace(&baseline, 1, &functions), 2, &code);
|
|
reject(&root, name, &bytes, LoadLimits::default());
|
|
}
|
|
// Two mutually recursive Array declarations: a structural cycle, with valid IDs.
|
|
reject(
|
|
&root,
|
|
"array-cycle",
|
|
&replace(
|
|
&baseline,
|
|
6,
|
|
&[2, 0, 0, 0, 2, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0],
|
|
),
|
|
LoadLimits::default(),
|
|
);
|
|
reject(
|
|
&root,
|
|
"structural-cycle",
|
|
&replace(
|
|
&baseline,
|
|
6,
|
|
&[
|
|
2, 0, 0, 0, 4, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0,
|
|
],
|
|
),
|
|
LoadLimits::default(),
|
|
);
|
|
reject(
|
|
&root,
|
|
"invalid-type-reference",
|
|
&replace(&baseline, 6, &[1, 0, 0, 0, 2, 0, 99, 0, 0, 0]),
|
|
LoadLimits::default(),
|
|
);
|
|
reject(
|
|
&root,
|
|
"invalid-utf8",
|
|
&replace(
|
|
&baseline,
|
|
6,
|
|
&[1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 255, 0x13, 0, 0, 0],
|
|
),
|
|
LoadLimits::default(),
|
|
);
|
|
reject(
|
|
&root,
|
|
"removed-input",
|
|
&replace(&baseline, 6, &[1, 0, 0, 0, 0, 0, 0, 1, 0, 0]),
|
|
LoadLimits::default(),
|
|
);
|
|
let mut overlap = baseline.clone();
|
|
overlap[36..40].copy_from_slice(&0u32.to_le_bytes());
|
|
reject(&root, "overlap", &overlap, LoadLimits::default());
|
|
reject(
|
|
&root,
|
|
"truncated",
|
|
&baseline[..baseline.len() - 1],
|
|
LoadLimits::default(),
|
|
);
|
|
let mut duplicate = baseline.clone();
|
|
duplicate[44..48].copy_from_slice(&0u32.to_le_bytes());
|
|
reject(
|
|
&root,
|
|
"duplicate-section",
|
|
&duplicate,
|
|
LoadLimits::default(),
|
|
);
|
|
reject(
|
|
&root,
|
|
"empty-entry",
|
|
&replace(&replace(&baseline, 1, &[0, 0, 0, 0]), 8, &[0, 0, 0, 0]),
|
|
LoadLimits::default(),
|
|
);
|
|
reject(
|
|
&root,
|
|
"resource-types",
|
|
&baseline,
|
|
LoadLimits {
|
|
types: 0,
|
|
..Default::default()
|
|
},
|
|
);
|
|
reject(
|
|
&root,
|
|
"resource-scratch",
|
|
&baseline,
|
|
LoadLimits {
|
|
temporary_bytes: 0,
|
|
..Default::default()
|
|
},
|
|
);
|
|
reject(
|
|
&root,
|
|
"resource",
|
|
&baseline,
|
|
LoadLimits {
|
|
work: 0,
|
|
..Default::default()
|
|
},
|
|
);
|
|
}
|
|
|
|
fn verify_catalog_fixtures(root: &std::path::Path) {
|
|
use prometeu_vm::LogicalFrameEndingReason as End;
|
|
let mut random_cycles = None;
|
|
for name in ["catalog-random-off", "catalog-random-on", "catalog-constants", "catalog-vec2-zero", "catalog-input", "catalog-string"] {
|
|
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
|
.unwrap_or_else(|error| panic!("{name}: {error:?}"));
|
|
println!("LOAD PASS {name}");
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
if name == "catalog-input" {
|
|
assert!(matches!(report.reason, End::Trap(ref trap) if trap.code == prometeu_bytecode::TRAP_UNAVAILABLE));
|
|
println!("EXEC PASS {name}: TRAP_UNAVAILABLE (known query, no hardware)");
|
|
continue;
|
|
}
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
|
if name.starts_with("catalog-random-") {
|
|
// Independent seed-42 vector and unit-range consumption from the provider handoff/tests.
|
|
assert_eq!(vm.operand_stack_top(3), vec![
|
|
Value::Int64(0xF9090E529A7DAE00u64 as i64), Value::Int64(-7),
|
|
Value::Float(f64::from_bits(0x3FB304AA46C98538)),
|
|
], "{name}: aliasing, discarded first next, mixed operations and exact order");
|
|
if let Some(cycles) = random_cycles { assert_eq!(report.cycles_used, cycles, "optimization changed charged work"); }
|
|
random_cycles = Some(report.cycles_used);
|
|
} else if name == "catalog-vec2-zero" {
|
|
assert_eq!(vm.operand_stack_top(2), vec![Value::Float(0.0), Value::Float(0.0)]);
|
|
} else if name == "catalog-string" {
|
|
assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(0xAABBCCDD)]);
|
|
} else {
|
|
let mut constants: Vec<_> = prometeu_vm::builtin_constant_catalog().collect();
|
|
constants.sort_by_key(|constant| constant.key.key());
|
|
let mut expected = Vec::new();
|
|
for constant in constants { expected.extend(constant.materialize().unwrap()); }
|
|
expected.reverse();
|
|
let actual = vm.operand_stack_top(expected.len());
|
|
assert_eq!(actual.len(), 16);
|
|
for (actual, expected) in actual.iter().zip(expected.iter()) {
|
|
match (actual, expected) {
|
|
(Value::Float(a), Value::Float(b)) => assert_eq!(a.to_bits(), b.to_bits()),
|
|
_ => assert_eq!(actual, expected),
|
|
}
|
|
}
|
|
}
|
|
println!("EXEC PASS {name}: {} cycles, {} steps", report.cycles_used, report.steps_executed);
|
|
}
|
|
let removed = std::fs::read(root.join("catalog-removed.pbx")).unwrap();
|
|
reject(root, "catalog-removed", &removed, LoadLimits::default());
|
|
let null_string = std::fs::read(root.join("catalog-null-string.pbx")).unwrap();
|
|
reject(root, "catalog-null-string", &null_string, LoadLimits::default());
|
|
}
|
|
|
|
fn reject(root: &std::path::Path, name: &str, bytes: &[u8], limits: LoadLimits) {
|
|
let error =
|
|
ValidatedProgramImage::load(bytes, &mut LoadBudget::new(limits), caps::NONE).unwrap_err();
|
|
record_fault(root, name, error);
|
|
}
|
|
fn record_fault(root: &std::path::Path, name: &str, error: prometeu_vm::VmInitError) {
|
|
if let prometeu_vm::VmInitError::RegistryLoadFailed(ref failure) = error {
|
|
let expected = match name {
|
|
"array-cycle" => Some("ArrayCycle"),
|
|
"structural-cycle" => Some("StructuralTypeCycle"),
|
|
"invalid-type-reference" => Some("TypeId"),
|
|
"invalid-utf8" => Some("Utf8"),
|
|
"removed-input" => Some("Builtin"),
|
|
"retired-spawn" | "retired-yield" | "retired-sleep" => Some("RemovedOpcode"),
|
|
"nonfinite" => Some("NonFiniteValue"),
|
|
"unreachable-reference" => Some("FunctionId"),
|
|
"return-height" => Some("ReturnHeight"),
|
|
"fallthrough" => Some("UnterminatedPath"),
|
|
"zero-max" => Some("StackOverflow"),
|
|
"duplicate-section" => Some("DuplicateSection"),
|
|
_ => None,
|
|
};
|
|
if let Some(reason) = expected {
|
|
assert_eq!(format!("{:?}", failure.reason), reason, "{name}");
|
|
}
|
|
}
|
|
let report = prometeu_system::CrashReport::VmInit { error };
|
|
assert_eq!(report.pc(), None);
|
|
let fault = serde_json::json!({"kind":"vm_init", "pc":null, "summary":"Conformance fixture", "initError":report.init_diagnostic().unwrap()});
|
|
let full: serde_json::Value = serde_json::from_slice(
|
|
&std::fs::read(std::env::args().nth(2).expect("schema path")).unwrap(),
|
|
)
|
|
.unwrap();
|
|
let schema = jsonschema::validator_for(
|
|
&serde_json::json!({"$defs":full["$defs"],"$ref":"#/$defs/fault"}),
|
|
)
|
|
.unwrap();
|
|
if !schema.is_valid(&fault) {
|
|
let reason = fault["initError"]["load"]["reason"].as_str().unwrap();
|
|
assert!(
|
|
["LifecycleRole", "NonFiniteValue"].contains(&reason),
|
|
"unexpected schema failure: {fault}"
|
|
);
|
|
// Validate the ORIGINAL payload with only the observed missing enum value added.
|
|
// This is an explicit upstream defect report, not a claim of published-schema conformance.
|
|
let mut corrected = full.clone();
|
|
corrected["$defs"]["loadFailure"]["properties"]["reason"]["enum"]
|
|
.as_array_mut()
|
|
.unwrap()
|
|
.push(serde_json::json!(reason));
|
|
jsonschema::validator_for(
|
|
&serde_json::json!({"$defs":corrected["$defs"],"$ref":"#/$defs/fault"}),
|
|
)
|
|
.unwrap()
|
|
.validate(&fault)
|
|
.unwrap();
|
|
println!(
|
|
"UPSTREAM SCHEMA GAP: {reason} missing from published enum; original payload preserved"
|
|
);
|
|
}
|
|
std::fs::write(
|
|
root.join(format!("fault-{name}.json")),
|
|
serde_json::to_vec_pretty(&fault).unwrap(),
|
|
)
|
|
.unwrap();
|
|
println!("REJECT PASS {name}: {}", fault["initError"]);
|
|
}
|
|
|
|
fn word(bytes: &[u8], at: usize) -> usize {
|
|
u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap()) as usize
|
|
}
|
|
fn section(bytes: &[u8], kind: usize) -> &[u8] {
|
|
for i in 0..word(bytes, 8) {
|
|
let at = 32 + 12 * i;
|
|
if word(bytes, at) == kind {
|
|
let offset = word(bytes, at + 4);
|
|
return &bytes[offset..offset + word(bytes, at + 8)];
|
|
}
|
|
}
|
|
panic!("missing section {kind}")
|
|
}
|
|
fn replace(bytes: &[u8], kind: usize, payload: &[u8]) -> Vec<u8> {
|
|
let count = word(bytes, 8);
|
|
let mut output = bytes[..32 + 12 * count].to_vec();
|
|
for i in 0..count {
|
|
let at = 32 + 12 * i;
|
|
let body = if word(bytes, at) == kind {
|
|
payload
|
|
} else {
|
|
section(bytes, word(bytes, at))
|
|
};
|
|
let offset = output.len() as u32;
|
|
output[at + 4..at + 8].copy_from_slice(&offset.to_le_bytes());
|
|
output[at + 8..at + 12].copy_from_slice(&(body.len() as u32).to_le_bytes());
|
|
output.extend_from_slice(body);
|
|
}
|
|
output
|
|
}
|
|
|
|
// Controlled provider boundary: every input read checks its identity and is counted.
|
|
struct PbsInput {
|
|
expected: usize,
|
|
reads: std::cell::Cell<usize>,
|
|
button: prometeu_hal::button::Button,
|
|
}
|
|
impl PbsInput {
|
|
fn read(&self, index: usize) -> &prometeu_hal::button::Button {
|
|
assert_eq!(index, self.expected, "PBS queried an unselected endpoint");
|
|
self.reads.set(self.reads.get() + 1);
|
|
&self.button
|
|
}
|
|
}
|
|
impl prometeu_hal::PadBridge for PbsInput {
|
|
fn begin_frame(&mut self, _: &prometeu_hal::InputSignals) { unreachable!() }
|
|
fn any(&self) -> bool { unreachable!() }
|
|
fn up(&self) -> &prometeu_hal::button::Button { self.read(0) }
|
|
fn down(&self) -> &prometeu_hal::button::Button { self.read(1) }
|
|
fn left(&self) -> &prometeu_hal::button::Button { self.read(2) }
|
|
fn right(&self) -> &prometeu_hal::button::Button { self.read(3) }
|
|
fn a(&self) -> &prometeu_hal::button::Button { self.read(4) }
|
|
fn b(&self) -> &prometeu_hal::button::Button { self.read(5) }
|
|
fn x(&self) -> &prometeu_hal::button::Button { self.read(6) }
|
|
fn y(&self) -> &prometeu_hal::button::Button { self.read(7) }
|
|
fn l(&self) -> &prometeu_hal::button::Button { self.read(8) }
|
|
fn r(&self) -> &prometeu_hal::button::Button { self.read(9) }
|
|
fn start(&self) -> &prometeu_hal::button::Button { self.read(10) }
|
|
fn select(&self) -> &prometeu_hal::button::Button { self.read(11) }
|
|
}
|
|
impl prometeu_hal::TouchBridge for PbsInput {
|
|
fn begin_frame(&mut self, _: &prometeu_hal::InputSignals) { unreachable!() }
|
|
fn f(&self) -> &prometeu_hal::button::Button { self.read(12) }
|
|
fn x(&self) -> i32 { self.read(13); -123 }
|
|
fn y(&self) -> i32 { self.read(14); 456 }
|
|
}
|
|
impl prometeu_hal::InputPlatform for PbsInput {
|
|
fn pad(&self) -> &dyn prometeu_hal::PadBridge { self }
|
|
fn pad_mut(&mut self) -> &mut dyn prometeu_hal::PadBridge { self }
|
|
fn touch(&self) -> &dyn prometeu_hal::TouchBridge { self }
|
|
fn touch_mut(&mut self) -> &mut dyn prometeu_hal::TouchBridge { self }
|
|
}
|
|
impl prometeu_hal::RuntimePlatform for PbsInput {
|
|
fn input(&self) -> &dyn prometeu_hal::InputPlatform { self }
|
|
fn input_mut(&mut self) -> &mut dyn prometeu_hal::InputPlatform { self }
|
|
fn wall_clock(&self) -> &dyn prometeu_hal::WallClock { unreachable!("non-input platform access") }
|
|
fn render_submission_sink(&mut self) -> &mut dyn prometeu_hal::RenderSubmissionSink { unreachable!("non-input platform access") }
|
|
fn render_backend(&mut self) -> &mut dyn prometeu_hal::RenderBackend { unreachable!("non-input platform access") }
|
|
fn game2d_frame_composer(&mut self) -> &mut dyn prometeu_hal::Game2DFrameComposer { unreachable!("non-input platform access") }
|
|
fn audio(&self) -> &dyn prometeu_hal::AudioBridge { unreachable!("non-input platform access") }
|
|
fn audio_mut(&mut self) -> &mut dyn prometeu_hal::AudioBridge { unreachable!("non-input platform access") }
|
|
fn assets(&self) -> &dyn prometeu_hal::AssetBridge { unreachable!("non-input platform access") }
|
|
fn assets_mut(&mut self) -> &mut dyn prometeu_hal::AssetBridge { unreachable!("non-input platform access") }
|
|
fn telemetry(&self) -> &dyn prometeu_hal::TelemetryPlatform { unreachable!("non-input platform access") }
|
|
}
|
|
fn verify_pbs_fixtures(root: &std::path::Path) {
|
|
use prometeu_vm::LogicalFrameEndingReason as End;
|
|
let buttons = ["up","down","left","right","a","b","x","y","l","r","start","select"];
|
|
let mut count = 0;
|
|
let mut defaults_checked = std::collections::BTreeSet::new();
|
|
for entry in std::fs::read_dir(root).unwrap() {
|
|
let path = entry.unwrap().path();
|
|
let name = path.file_stem().unwrap().to_str().unwrap();
|
|
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || name.starts_with("pbs-generic-contract-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
|
count += 1;
|
|
let bytes = std::fs::read(&path).unwrap();
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
|
.unwrap_or_else(|e| panic!("{name}: {e:?}"));
|
|
for (index, ty) in image.registry().declarations().types.iter().enumerate() {
|
|
if let prometeu_bytecode::registry::TypeKind::Builtin(id) = ty.kind {
|
|
let expected = match id.0 {
|
|
1 | 0x11 => vec![Value::Int64(0)],
|
|
2 => vec![Value::Float(0.0), Value::Float(0.0)],
|
|
3 => vec![Value::Int64(0), Value::Int64(0), Value::Int64(0)],
|
|
0x10 => vec![Value::Int32(0)], 0x12 => vec![Value::Float(0.0)],
|
|
0x13 => vec![Value::Boolean(false)], 0x14 => vec![Value::Null],
|
|
_ => panic!("unexpected PBS builtin {}", id.0),
|
|
};
|
|
assert_eq!(image.registry().type_layout(prometeu_bytecode::registry::TypeId(index as u32)).unwrap().storage.default_values(), expected);
|
|
defaults_checked.insert(id.0);
|
|
}
|
|
}
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let parts: Vec<_> = name.split('-').collect();
|
|
if parts[1] == "pad" || parts[1] == "touch" {
|
|
let query = if parts[1] == "pad" { &parts[3] } else { parts.last().unwrap() };
|
|
let expected = if parts[1] == "pad" { buttons.iter().position(|b| *b == parts[2]).unwrap() }
|
|
else { match *query { "x" => 13, "y" => 14, _ => 12 } };
|
|
let button = prometeu_hal::button::Button { pressed: expected % 2 == 0, released: expected % 3 == 0, down: expected % 5 == 0, hold_frames: 100 + expected as u16 };
|
|
let value = match *query {
|
|
"pressed" => Value::Boolean(button.pressed), "released" => Value::Boolean(button.released), "down" => Value::Boolean(button.down),
|
|
"hold" => Value::Int64(button.hold_frames.into()), "x" => Value::Int64(-123), "y" => Value::Int64(456), _ => unreachable!()
|
|
};
|
|
let mut input = PbsInput { expected, reads: std::cell::Cell::new(0), button };
|
|
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(Some(&mut input))).unwrap();
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
|
assert_eq!(vm.operand_stack_top(1), vec![value], "{name}");
|
|
assert_eq!(input.reads.get(), 1, "{name}: exactly one input query");
|
|
let unavailable_image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let mut unavailable_vm = VirtualMachine::from_image(unavailable_image, Default::default());
|
|
unavailable_vm.prepare_boot_call().unwrap();
|
|
let unavailable = unavailable_vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(unavailable.reason, End::Trap(ref trap) if trap.code == prometeu_bytecode::TRAP_UNAVAILABLE));
|
|
} else {
|
|
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
match parts[1] {
|
|
"numeric" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{report:?}"); assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(2147483655)]); }
|
|
"vec2" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert_eq!(vm.operand_stack_top(2), vec![Value::Float(0.0),Value::Float(0.0)]); }
|
|
"tuple" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert_eq!(vm.operand_stack_top(2), vec![Value::Int64(8),Value::Int32(7)]); }
|
|
"named" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert_eq!(vm.operand_stack_top(1), vec![Value::Int32(211020)]); }
|
|
"color" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(0xFF0000FF)]); }
|
|
"pixel" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{report:?}"); assert_eq!(vm.operand_stack_top(3), vec![Value::Int64(0xFF0000FF),Value::Int64(-34),Value::Int64(12)]); }
|
|
"string" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert!(matches!(&vm.operand_stack_top(1)[0], Value::String(value) if value.as_ref() == "hello")); }
|
|
"division" => assert!(matches!(report.reason, End::Trap(ref trap) if trap.code == prometeu_bytecode::TRAP_DIV_ZERO)),
|
|
"minimum" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(i64::MIN)]); }
|
|
"float" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{report:?}"); assert_eq!(vm.operand_stack_top(1), vec![Value::Float(3.75)]); }
|
|
"overflow" => { assert!(matches!(report.reason, End::Halted | End::EndOfRom)); assert_eq!(vm.operand_stack_top(1), vec![Value::Int32(i32::MIN)]); }
|
|
"narrow" => assert!(matches!(report.reason, End::Trap(ref trap) if trap.code == prometeu_bytecode::TRAP_NUMERIC_RANGE), "{name}: {report:?}"),
|
|
_ => panic!("unasserted PBS fixture: {name}"),
|
|
}
|
|
}
|
|
println!("PBS SOURCE/LOAD/EXEC PASS {name}");
|
|
}
|
|
assert_eq!(defaults_checked, [1,2,3,0x10,0x11,0x12,0x13,0x14].into_iter().collect());
|
|
println!("PBS Registry defaults PASS: all eight existing value types");
|
|
assert_eq!(count, 114, "The complete PBS qualification corpus must be present");
|
|
}
|
|
|
|
fn verify_pbs_structured_fixtures(root: &std::path::Path) {
|
|
use prometeu_vm::LogicalFrameEndingReason as End;
|
|
let success = ["closure-import", "closure-installed", "closure-groups", "closure-isolation", "closure-fields", "closure-functions", "closure-snapshot", "closure-objects", "closure-nested", "array-cycle", "array-nested", "array-parametric", "optional-flow", "struct-contract", "service-contract",
|
|
"optional-tuple", "tuple-storage", "optional-intrinsics", "optional-lazy", "array-context",
|
|
"binding-scope", "overloads", "single-evaluation", "long-boundaries", "gc-roots", "native-groups", "contract-elements", "imports", "global-array", "installed-example", "return-overloads", "inferred-optional-tuple", "inline-integers"];
|
|
for name in success.into_iter().chain(["negative-index", "past-end", "negative-length", "large-length"]) {
|
|
let bytes = std::fs::read(root.join(format!("pbs-structured-{name}.pbx")))
|
|
.unwrap_or_else(|e| panic!("missing mandatory PBS structured fixture {name}: {e}"));
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
|
.unwrap_or_else(|e| panic!("{name}: {e:?}"));
|
|
let limits = if name == "inline-integers" {
|
|
prometeu_vm::resource_budget::RuntimeLimits::default()
|
|
.lowered(prometeu_vm::resource_budget::RuntimeResource::HandleSlots, 1).unwrap()
|
|
} else { Default::default() };
|
|
let mut vm = VirtualMachine::from_image(image, limits);
|
|
vm.prepare_boot_call().unwrap();
|
|
let mut report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
if name == "gc-roots" {
|
|
assert!(matches!(report.reason, End::FrameSync), "{report:?}");
|
|
let snapshot = vm.resource_snapshot();
|
|
assert_eq!(snapshot.gc.collections, 1);
|
|
assert!(snapshot.gc.reclaimed >= 1100, "{:?}", snapshot.gc);
|
|
report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
match name {
|
|
"negative-index" | "past-end" | "negative-length" => assert!(matches!(report.reason,
|
|
End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_OOB), "{name}: {report:?}"),
|
|
"large-length" => assert!(matches!(report.reason,
|
|
End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_RESOURCE_LIMIT), "{name}: {report:?}"),
|
|
_ => {
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
|
assert_eq!(vm.operand_stack_top(1), vec![Value::Int32(42)], "{name}");
|
|
}
|
|
}
|
|
if name == "inline-integers" { assert_eq!(vm.heap().len(), 1, "single-slot integers must not allocate boxes"); }
|
|
let ownership = vm.resource_observer();
|
|
drop(vm);
|
|
for resource in [prometeu_vm::resource_budget::RuntimeResource::StringBytes,
|
|
prometeu_vm::resource_budget::RuntimeResource::StringAllocations] {
|
|
assert_eq!(ownership.used(resource), 0, "{name}: string ownership released");
|
|
}
|
|
println!("PBS STRUCTURED SOURCE/LOAD/EXEC PASS {name}");
|
|
}
|
|
}
|
|
|
|
fn verify_pbs_generic_contract_fixtures(root: &std::path::Path) {
|
|
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits, RuntimeResource}};
|
|
use prometeu_bytecode::{registry::{TypeId, TypeKind}, isa::core::CoreOpCode};
|
|
let mut reference_heap = None;
|
|
for name in ["identity", "manual", "multiple", "tuple", "optional", "function", "contract", "native", "recursive", "phantom", "receiver", "gc", "installed", "imports"] {
|
|
let bytes = std::fs::read(root.join(format!("pbs-generic-contract-{name}.pbx")))
|
|
.unwrap_or_else(|error| panic!("missing mandatory generic-contract fixture {name}: {error}"));
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
|
.unwrap_or_else(|error| panic!("generic contract {name}: {error:?}"));
|
|
let declarations = image.registry().declarations();
|
|
let traits: Vec<_> = declarations.types.iter().enumerate().filter(|(_, t)| matches!(t.kind, TypeKind::Trait(_))).collect();
|
|
assert!(!traits.is_empty(), "{name}: source must emit concrete traits");
|
|
for (index, _) in &traits {
|
|
assert_eq!(image.registry().type_layout(TypeId(*index as u32)).unwrap().storage.default_values(), &[Value::Null, Value::Null], "{name}: absent concrete trait");
|
|
}
|
|
assert!(!declarations.implementations.is_empty());
|
|
// The first dense implementation is valid; zero must never act as an absence sentinel.
|
|
assert_eq!(image.registry().implementation(declarations.implementations[0].owner, declarations.implementations[0].trait_id).unwrap().0, 0);
|
|
if name == "multiple" || name == "phantom" {
|
|
assert_eq!(traits.len(), 2);
|
|
assert_eq!(declarations.implementations.len(), 2);
|
|
assert_eq!(declarations.implementations[0].owner, declarations.implementations[1].owner);
|
|
assert_ne!(declarations.implementations[0].trait_id, declarations.implementations[1].trait_id);
|
|
}
|
|
if name == "imports" { assert_eq!(traits.len(), 3); assert_eq!(declarations.implementations.len(), 3); }
|
|
if name == "phantom" {
|
|
let mut code = image.code().to_vec();
|
|
let mut pc = 0;
|
|
let mut changed = false;
|
|
while pc < code.len() {
|
|
let instruction = prometeu_bytecode::decode_next(pc, &code).unwrap();
|
|
if instruction.opcode == CoreOpCode::CallTrait {
|
|
let (original, _) = instruction.imm_u32x2().unwrap();
|
|
let other = traits.iter().map(|(index, _)| *index as u32).find(|id| *id != original).unwrap();
|
|
code[pc+2..pc+6].copy_from_slice(&other.to_le_bytes());
|
|
changed = true; break;
|
|
}
|
|
pc = instruction.next_pc;
|
|
}
|
|
assert!(changed);
|
|
let corrupt = replace(&bytes, 2, &code);
|
|
match ValidatedProgramImage::load(&corrupt, &mut LoadBudget::new(LoadLimits::default()), caps::NONE) {
|
|
Err(error) => record_fault(root, "generic-contract-wrong-view", error),
|
|
Ok(image) => {
|
|
// Unknown receiver facts may defer rejection to the mandatory runtime check.
|
|
let mut invalid = VirtualMachine::from_image(image, RuntimeLimits::default());
|
|
invalid.prepare_boot_call().unwrap();
|
|
let report = invalid.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason, End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_TYPE), "mismatched concrete view: {report:?}");
|
|
}
|
|
}
|
|
println!("PBS GENERIC WRONG-VIEW REJECTION PASS");
|
|
}
|
|
println!("PBS GENERIC METADATA {name}: types={} implementations={} bytes={}", declarations.types.len(), declarations.implementations.len(), bytes.len());
|
|
let limits = if name == "identity" || name == "manual" {
|
|
RuntimeLimits::default().lowered(RuntimeResource::HandleSlots, 1).unwrap()
|
|
} else { RuntimeLimits::default() };
|
|
let mut vm = VirtualMachine::from_image(image, limits);
|
|
let observer = vm.resource_observer();
|
|
vm.prepare_boot_call().unwrap();
|
|
let mut report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
if name == "gc" {
|
|
assert!(matches!(report.reason, End::FrameSync), "{name}: {report:?}");
|
|
let gc = vm.resource_snapshot().gc;
|
|
assert!(gc.collections >= 1 && gc.reclaimed >= 1100, "{gc:?}");
|
|
report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
if name == "imports" {
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
|
} else {
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
|
assert_eq!(vm.operand_stack_top(1), vec![Value::Int32(42)], "{name}");
|
|
}
|
|
if name == "identity" || name == "manual" {
|
|
assert_eq!(vm.heap().len(), 1, "no generic argument box or view wrapper");
|
|
let heap = observer.used(RuntimeResource::GuestBytes);
|
|
if let Some(reference) = reference_heap { assert_eq!(heap, reference, "generic and manual contract heap footprints"); }
|
|
reference_heap = Some(heap);
|
|
}
|
|
drop(vm);
|
|
for resource in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots, RuntimeResource::AuxiliaryBytes,
|
|
RuntimeResource::TemporaryBytes, RuntimeResource::StringBytes, RuntimeResource::StringAllocations] {
|
|
assert_eq!(observer.used(resource), 0, "{name}: release {resource:?}");
|
|
}
|
|
if name == "identity" || name == "receiver" {
|
|
for (resource, limit) in [(RuntimeResource::GuestBytes, 0), (RuntimeResource::GuestBytes, 1), (RuntimeResource::GuestBytes, 2), (RuntimeResource::HandleSlots, 0)] {
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let mut refused = VirtualMachine::from_image(image, RuntimeLimits::default().lowered(resource, limit).unwrap());
|
|
let observed = refused.resource_observer();
|
|
refused.prepare_boot_call().unwrap();
|
|
let report = refused.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason, End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_RESOURCE_LIMIT), "{name}/{resource:?}: {report:?}");
|
|
assert_eq!(refused.heap().len(), 0, "refusal cannot publish partial objects");
|
|
assert!(observed.used(resource) <= limit);
|
|
drop(refused);
|
|
for domain in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots, RuntimeResource::AuxiliaryBytes, RuntimeResource::TemporaryBytes] {
|
|
assert_eq!(observed.used(domain), 0);
|
|
}
|
|
}
|
|
reject(root, &format!("generic-contract-{name}-load-quota"), &bytes, LoadLimits { implementations: 0, ..LoadLimits::default() });
|
|
}
|
|
println!("PBS GENERIC SOURCE/LOAD/EXEC PASS {name}");
|
|
}
|
|
}
|
|
|
|
fn verify_pbs_resource_refusals(root: &std::path::Path) {
|
|
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits, RuntimeResource}};
|
|
for name in ["closure-objects", "array-cycle", "tuple-storage", "struct-contract"] {
|
|
for resource in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots] {
|
|
for limit in [0, 1, 2] {
|
|
let bytes = std::fs::read(root.join(format!("pbs-structured-{name}.pbx"))).unwrap();
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let limits = RuntimeLimits::default().lowered(resource, limit).unwrap();
|
|
let mut vm = VirtualMachine::from_image(image, limits);
|
|
let observer = vm.resource_observer();
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason, End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_RESOURCE_LIMIT), "{name}/{resource:?}/{limit}: {report:?}");
|
|
assert!(observer.used(resource) <= limit);
|
|
if limit == 0 { assert_eq!(vm.heap().len(), 0, "no partially published object"); }
|
|
drop(vm);
|
|
for domain in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots,
|
|
RuntimeResource::AuxiliaryBytes, RuntimeResource::TemporaryBytes] {
|
|
assert_eq!(observer.used(domain), 0, "{name}: session ownership released after refusal: {domain:?}");
|
|
}
|
|
println!("PBS QUOTA/OWNERSHIP PASS {name}/{resource:?}/{limit}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn verify_pbs_closure_frames(root: &std::path::Path) {
|
|
use prometeu_vm::LogicalFrameEndingReason as End;
|
|
let bytes = std::fs::read(root.join("closure-frame-lifecycle.pbx")).expect("mandatory closure frame fixture");
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
for frame in 0..3 {
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm.run_budget(2_000_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "frame {frame}: {report:?}");
|
|
assert!(vm.operand_stack_top(1).is_empty(), "frame root returns void");
|
|
let snapshot = vm.resource_snapshot();
|
|
assert!(snapshot.gc.collections >= frame + 1, "frame {frame}: {:?}", snapshot.gc);
|
|
assert!(snapshot.gc.reclaimed >= (frame + 1) * 1200, "frame {frame}: {:?}", snapshot.gc);
|
|
if frame == 0 { assert!(vm.heap().len() >= 3, "array, closure and capture survive root return"); }
|
|
else { assert_eq!(vm.heap().len(), 1, "only the empty global array remains after closure release"); }
|
|
}
|
|
println!("PBS CLOSURE FRAME LIFECYCLE/RETENTION/GC PASS");
|
|
}
|
|
|
|
// The expected results/call costs are independent provider fixtures. Step costs
|
|
// use the published opcode contract, not observed timings or compiler estimates.
|
|
fn verify_pbs_math_fixtures(root: &std::path::Path) {
|
|
use prometeu_vm::LogicalFrameEndingReason as End;
|
|
use prometeu_bytecode::isa::core::CoreOpCode as OpCode;
|
|
let corpus = std::fs::read_to_string(root.join("math-vectors.tsv")).expect("mandatory math corpus");
|
|
let mut covered = std::collections::BTreeSet::new();
|
|
let mut count = 0;
|
|
let mut original_cycles = std::collections::BTreeMap::new();
|
|
let mut names = std::collections::BTreeSet::new();
|
|
for line in corpus.lines() {
|
|
let parts: Vec<_> = line.split_whitespace().collect();
|
|
let name = parts[0];
|
|
assert!(names.insert(parts[0]), "duplicate math fixture");
|
|
let id = u32::from_str_radix(parts[1], 16).unwrap();
|
|
let expected = parts[parts.len()-2];
|
|
let cost: u64 = parts.last().unwrap().parse().unwrap();
|
|
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let code = image.code().to_vec();
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let mut total = 0;
|
|
let mut calls = 0;
|
|
let ending = loop {
|
|
let pc = vm.pc();
|
|
let opcode = OpCode::try_from(u16::from_le_bytes(code[pc..pc+2].try_into().unwrap())).unwrap();
|
|
let cycles = if opcode == OpCode::Intrinsic {
|
|
let actual = u32::from_le_bytes(code[pc+2..pc+6].try_into().unwrap());
|
|
assert_eq!(actual, id, "{name}: wrong overload or unexpected conversion");
|
|
calls += 1;
|
|
cost
|
|
} else { opcode.cycles() };
|
|
let report = vm.run_budget(1, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert_eq!(report.cycles_used, cycles, "{name}: cycle charge at {pc}, {opcode:?}");
|
|
total += cycles;
|
|
if matches!(report.reason, End::Halted | End::EndOfRom | End::Trap(_)) { break report.reason; }
|
|
assert!(total < 10000, "{name}: nonterminating source fixture");
|
|
};
|
|
assert_eq!(calls, 1, "{name}: preserve literal calls and no hidden conversions");
|
|
if let Some(code) = expected.strip_prefix("trap:") {
|
|
let code = u32::from_str_radix(code,16).unwrap();
|
|
assert!(matches!(ending, End::Trap(ref trap) if trap.code == code), "{name}: {ending:?}");
|
|
} else {
|
|
assert!(matches!(ending, End::Halted | End::EndOfRom), "{name}: {ending:?}");
|
|
let value = vm.operand_stack_top(1);
|
|
let actual = match &value[0] {
|
|
Value::Float(value) => format!("f:{:016x}",value.to_bits()),
|
|
Value::Int32(value) => format!("i:{value}"),
|
|
Value::Int64(value) => format!("l:{value}"),
|
|
value => panic!("{name}: unexpected value {value:?}"),
|
|
};
|
|
assert_eq!(actual, expected, "{name}: exact result bits");
|
|
}
|
|
// A full-budget execution must charge exactly the independently checked steps.
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert_eq!(report.cycles_used, total, "{name}: aggregate cost");
|
|
if let Some(original) = name.strip_suffix("-optimized") {
|
|
assert_eq!(original_cycles.get(original), Some(&total), "{name}: optimization changed cycles");
|
|
} else { original_cycles.insert(name.to_string(), total); }
|
|
covered.insert(id);
|
|
count += 1;
|
|
}
|
|
assert_eq!(covered, (0x3000..=0x3019).collect());
|
|
assert_eq!(count, 522, "mandatory boundary corpus missing");
|
|
for (name, expected) in [("pi", Value::Float(f64::from_bits(0x400921fb54442d18))),
|
|
("tau", Value::Float(f64::from_bits(0x401921fb54442d18))),
|
|
("literal", Value::Float(-0.0)), ("dependency", Value::Int32(42)), ("long", Value::Int64(2147483648)),
|
|
("service", Value::Int32(42)), ("min", Value::Int64(42))] {
|
|
let bytes = std::fs::read(root.join(format!("pbs-math-{name}.pbx"))).unwrap();
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
|
let actual = vm.operand_stack_top(1);
|
|
match (&actual[0], &expected) {
|
|
(Value::Float(a), Value::Float(b)) => assert_eq!(a.to_bits(), b.to_bits(), "{name}"),
|
|
_ => assert_eq!(actual, vec![expected]),
|
|
}
|
|
if name == "pi" || name == "tau" || name == "service" { assert_eq!(report.cycles_used, 6, "constant load + root RET only"); }
|
|
}
|
|
let bytes = std::fs::read(root.join("pbs-math-import.pbx")).expect("real SDK imports fixture");
|
|
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
|
let mut vm = VirtualMachine::from_image(image, Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "import: {report:?}");
|
|
println!("PBS MATH PASS: {count} exact bits/faults/cycle vectors; all 26 signatures; service constants");
|
|
}
|
|
|
|
|
|
fn verify_pbs_random_fixtures(root: &std::path::Path) {
|
|
use prometeu_vm::LogicalFrameEndingReason as End;
|
|
use prometeu_bytecode::isa::core::CoreOpCode as OpCode;
|
|
let corpus = std::fs::read_to_string(root.join("random-vectors.tsv")).expect("mandatory random seed corpus");
|
|
let mut cases: Vec<(String, Vec<String>, Vec<u64>, Option<u32>)> = Vec::new();
|
|
let mut names = std::collections::BTreeSet::new();
|
|
for row in corpus.lines() {
|
|
let p: Vec<_> = row.split_whitespace().collect();
|
|
assert!(names.insert(p[0]), "duplicate random fixture");
|
|
let draw: usize = p[2].parse().unwrap();
|
|
assert!((1..=8).contains(&draw));
|
|
let mut costs = vec![32];
|
|
costs.extend(std::iter::repeat_n(8, draw-1));
|
|
costs.push(if p[1].starts_with("f:") { 12 } else { 8 });
|
|
cases.push((p[0].to_string(), vec![p[1].to_string()], costs, None));
|
|
}
|
|
assert_eq!(cases.len(), 192, "six seeds, eight draws, two outputs, two optimizer variants required");
|
|
let words = [0x86B1DA1D72062B68u64, 0x1304AA46C9853D39, 0xA3670E9E0DD50358, 0xF9090E529A7DAE00];
|
|
let long = |word: u64| format!("l:{}",word as i64);
|
|
let mut add = |name: &str, expected: Vec<String>, costs: Vec<u64>, fault: Option<u32>| {
|
|
for suffix in ["", "-optimized"] {
|
|
cases.push((format!("pbs-random-{name}{suffix}"), expected.clone(), costs.clone(), fault));
|
|
}
|
|
};
|
|
add("mixed", vec![long(words[3]), "l:-7".into(), "f:3fb304aa46c98538".into()], vec![32,8,12,12,8], None);
|
|
add("storage", vec![long(words[0]),long(words[3]),long(words[2]),long(words[1]),long(words[0])], vec![32,8,8,8,8,32,8], None);
|
|
for (name, trace) in [("named",312),("positional",123)] {
|
|
add(name,vec![long(words[3]),"l:-7".into(),format!("i:{trace}")],vec![32,8,8,12,8],None);
|
|
}
|
|
let bounds = [(0,1),(-10,-3),(-10,10),(0,16),(i64::MIN,0),(-1,i64::MAX),(i64::MIN,i64::MAX)];
|
|
for (index,(min,max)) in bounds.into_iter().enumerate() {
|
|
let span = (i128::from(max)-i128::from(min)) as u64;
|
|
// Independent provider fixture word, accepted for each of these spans.
|
|
let expected = i128::from(min)+i128::from(words[0]%span);
|
|
assert!(expected>=i128::from(min) && expected<i128::from(max));
|
|
add(&format!("range-{index}"),vec![format!("l:{expected}")],vec![32,12],None);
|
|
}
|
|
for index in [7,8] { add(&format!("range-{index}"),vec![],vec![32,4],Some(prometeu_bytecode::TRAP_NUMERIC_DOMAIN)); }
|
|
let rejected_then_accepted = i128::from(i64::MIN)+i128::from(words[2]%((1u64<<63)+1));
|
|
add("rejection",vec![long(words[3]),format!("l:{rejected_then_accepted}")],vec![32,8,20,8],None);
|
|
for name in ["null","typed-null"] { add(name,vec![],vec![8],Some(prometeu_bytecode::TRAP_NULL_REFERENCE)); }
|
|
let mut gc_costs=vec![32;1101]; gc_costs.extend([8,8,8]);
|
|
add("gc",vec![long(words[2]),long(words[1]),long(words[0])],gc_costs,None);
|
|
let mut cycles_by_name = std::collections::BTreeMap::new();
|
|
for (name,expected,costs,fault) in cases {
|
|
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap_or_else(|e|panic!("{name}: {e}"));
|
|
let mut reference = None;
|
|
for budget in [1,100000,100000] {
|
|
let image = ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE)
|
|
.unwrap_or_else(|e|panic!("{name}: {e:?}"));
|
|
let code = image.code().to_vec();
|
|
let mut vm = VirtualMachine::from_image(image,Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let mut total=0;
|
|
let mut calls=0;
|
|
let ending = loop {
|
|
let pc=vm.pc();
|
|
let opcode=OpCode::try_from(u16::from_le_bytes(code[pc..pc+2].try_into().unwrap())).unwrap();
|
|
let report=vm.run_budget(budget,&mut NoNative,&mut HostContext::new(None)).unwrap();
|
|
if budget==1 && opcode==OpCode::Intrinsic {
|
|
let id=u32::from_le_bytes(code[pc+2..pc+6].try_into().unwrap());
|
|
if id == 0x3014 {
|
|
// Explicit source `as long` conversion; not a Random call.
|
|
assert_eq!(report.cycles_used,2,"{name}: explicit Int32 to Int64");
|
|
} else {
|
|
assert!((0x4000..=0x4003).contains(&id),"{name}: wrong intrinsic {id:x}");
|
|
assert_eq!(report.cycles_used,costs[calls],"{name}: call {calls} id={id:x}");
|
|
calls+=1;
|
|
}
|
|
}
|
|
total+=report.cycles_used;
|
|
if matches!(report.reason,End::Halted|End::EndOfRom|End::Trap(_)) { break report.reason; }
|
|
if matches!(report.reason,End::FrameSync) {
|
|
assert!(name.starts_with("pbs-random-gc"));
|
|
let snapshot=vm.resource_snapshot();
|
|
assert_eq!(snapshot.gc.collections,1);
|
|
assert!(snapshot.gc.reclaimed>=1100,"{name}: {:?}",snapshot.gc);
|
|
}
|
|
assert!(total<1000000,"{name}: nontermination");
|
|
};
|
|
if budget==1 { assert_eq!(calls,costs.len(),"{name}: missing or extra calls"); }
|
|
if let Some(fault)=fault {
|
|
assert!(matches!(ending,End::Trap(ref t) if t.code==fault),"{name}: {ending:?}");
|
|
} else {
|
|
assert!(matches!(ending,End::Halted|End::EndOfRom),"{name}: {ending:?}");
|
|
let actual:Vec<_>=vm.operand_stack_top(expected.len()).iter().map(|v|match v {
|
|
Value::Int64(v)=>format!("l:{v}"),Value::Int32(v)=>format!("i:{v}"),
|
|
Value::Float(v)=>format!("f:{:016x}",v.to_bits()),v=>panic!("{name}: {v:?}")
|
|
}).collect();
|
|
assert_eq!(actual,expected,"{name}: output bits/replay");
|
|
}
|
|
if let Some(reference)=reference { assert_eq!(total,reference,"{name}: replay/budget cycles"); }
|
|
reference=Some(total);
|
|
}
|
|
let total=reference.unwrap();
|
|
if let Some(original)=name.strip_suffix("-optimized") {
|
|
assert_eq!(cycles_by_name.get(original),Some(&total),"{name}: optimizer cycles");
|
|
} else { cycles_by_name.insert(name.clone(),total); }
|
|
println!("RANDOM PASS {name}: {total} cycles, exact bits/fault and replay");
|
|
}
|
|
let bytes=std::fs::read(root.join("pbs-random-import.pbx")).expect("mandatory real Random SDK import");
|
|
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
|
|
let mut vm=VirtualMachine::from_image(image,Default::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
let report=vm.run_budget(10000,&mut NoNative,&mut HostContext::new(None)).unwrap();
|
|
assert!(matches!(report.reason,End::Halted|End::EndOfRom),"Random import: {report:?}");
|
|
println!("RANDOM PASS real SDK imports and aliases");
|
|
|
|
}
|