feat(compiler): implement typed registry and production PBX conformance (AGD76)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
# Studio PBX producer conformance
|
||||
|
||||
Owner: compiler/general. This harness uses the production runtime loader and VM;
|
||||
it does not implement a Java approximation of runtime admission.
|
||||
|
||||
From the Studio root:
|
||||
|
||||
```sh
|
||||
./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*LinkedRegistryTest' --tests '*PbxRegistryConformanceTest' --tests '*TypedCommonPipelineTest'
|
||||
python3 tools/pbx-conformance/run.py --runtime ../runtime
|
||||
```
|
||||
|
||||
The Java tests generate PBX in the build-pipeline module's
|
||||
`build/registry-conformance`. `run.py` creates a temporary Cargo project, copies
|
||||
the runtime lockfile and references its bytecode/hal/vm/system crates by path.
|
||||
It never modifies the runtime checkout. Add `--online` for an uncached machine;
|
||||
by default Cargo uses only cached dependencies. Java 25, Rust and the runtime
|
||||
checkout's dependencies are required.
|
||||
|
||||
The output identifies runtime revision, OS/architecture and load configuration.
|
||||
Positive fixtures distinguish loading from execution. The vec2 signature fixture
|
||||
is deliberately load-only because its entry requires arguments. Native binding
|
||||
uses SYSTEM for the positive case and NONE for missing-capability rejection;
|
||||
all other cases use NONE. Resource tests explicitly lower default limits.
|
||||
|
||||
Negative fixtures mutate Java-produced bytes independently of the writer to
|
||||
exercise malformed input that the writer correctly refuses to construct.
|
||||
Initialization JSON comes from `prometeu_system::CrashReport`, and is checked
|
||||
against the runtime's published headless schema. Saved JSON files remain under
|
||||
the generated fixture directory. Studio process tests replay selected observed
|
||||
payloads from `prometeu-app/src/test/resources/runtime` and assert unchanged MCP
|
||||
projection, including null executed PC and nested verifier context.
|
||||
|
||||
## Known upstream schema defect
|
||||
|
||||
At runtime `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`, the LoadReason Rust enum
|
||||
includes LifecycleRole, LifecycleSignature, DuplicateLifecycleRole and
|
||||
NonFiniteValue, absent from the published JSON schema enum. This corpus
|
||||
observes LifecycleRole and NonFiniteValue. The harness prints an explicit
|
||||
`UPSTREAM SCHEMA GAP` for those two reasons and verifies that adding exactly
|
||||
the missing enum value makes the original payload valid. It never alters the
|
||||
payload or reports those cases as conforming to the unmodified schema. Any
|
||||
other schema failure remains fatal. Track the runtime correction in AGD75;
|
||||
remove the exception once the schema is corrected.
|
||||
|
||||
This corpus qualifies common backend production, not PBS lowering, lifecycle
|
||||
selection, memcard persistence or installed-runtime deployment.
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Build a production-loader harness without writing into the runtime checkout."""
|
||||
import argparse
|
||||
import json
|
||||
import pathlib
|
||||
import platform
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--runtime", type=pathlib.Path, default=pathlib.Path("../runtime"))
|
||||
parser.add_argument("--fixtures", type=pathlib.Path, default=pathlib.Path("prometeu-compiler/prometeu-build-pipeline/build/registry-conformance"))
|
||||
parser.add_argument("--online", action="store_true", help="Allow Cargo to fetch uncached dependencies")
|
||||
args = parser.parse_args()
|
||||
runtime = args.runtime.resolve()
|
||||
print("runtime", subprocess.check_output(["git", "-C", str(runtime), "rev-parse", "HEAD"], text=True).strip(), flush=True)
|
||||
print("platform", platform.platform(), "limits=default capabilities=NONE", flush=True)
|
||||
with tempfile.TemporaryDirectory(prefix="pbx-conformance-") as directory:
|
||||
root = pathlib.Path(directory)
|
||||
manifest = '[package]\nname="studio-pbx-conformance"\nversion="0.1.0"\nedition="2024"\n[dependencies]\n'
|
||||
for crate in ["prometeu-bytecode", "prometeu-hal", "prometeu-vm", "prometeu-system"]:
|
||||
manifest += f'{crate}={{path={json.dumps(str(runtime / "crates/console" / crate))}}}\n'
|
||||
manifest += 'serde_json="1.0.149"\njsonschema={version="0.33",default-features=false}\n'
|
||||
(root / "Cargo.toml").write_text(manifest)
|
||||
(root / "Cargo.lock").write_text((runtime / "Cargo.lock").read_text())
|
||||
(root / "src").mkdir()
|
||||
(root / "src/main.rs").write_text(pathlib.Path(__file__).with_name("main.rs").read_text())
|
||||
subprocess.run(["cargo", "run", *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json")], check=True)
|
||||
Reference in New Issue
Block a user