feat(compiler): implement typed registry and production PBX conformance (AGD76)
This commit is contained in:
@@ -0,0 +1,357 @@
|
||||
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"));
|
||||
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 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
|
||||
}
|
||||
Reference in New Issue
Block a user