feat(pbs): add array-backed Stack and circular Queue
This commit is contained in:
@@ -0,0 +1,570 @@
|
||||
//! Real PBS Stack/Queue qualification against the unchanged production provider.
|
||||
use super::*;
|
||||
use prometeu_bytecode::{TRAP_RESOURCE_LIMIT, decode_next, isa::core::CoreOpCode as Op};
|
||||
use prometeu_vm::{
|
||||
LogicalFrameEndingReason as End,
|
||||
resource_budget::{RuntimeLimits, RuntimeResource as Q},
|
||||
resource_diagnostics::{StorageObject, StorageValue as V},
|
||||
};
|
||||
|
||||
fn load(
|
||||
root: &std::path::Path,
|
||||
collection: &str,
|
||||
name: &str,
|
||||
suffix: &str,
|
||||
) -> std::sync::Arc<ValidatedProgramImage> {
|
||||
let path = root.join(format!("pbs-{collection}-{name}{suffix}.pbx"));
|
||||
let bytes =
|
||||
std::fs::read(&path).unwrap_or_else(|e| panic!("mandatory {}: {e}", path.display()));
|
||||
ValidatedProgramImage::load(
|
||||
&bytes,
|
||||
&mut LoadBudget::new(LoadLimits::default()),
|
||||
caps::NONE,
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{}: {e:?}", path.display()))
|
||||
}
|
||||
fn state(
|
||||
vm: &VirtualMachine,
|
||||
image: &ValidatedProgramImage,
|
||||
collection: &str,
|
||||
) -> Vec<StorageObject> {
|
||||
let objects = vm.heap().diagnostic_typed_storage();
|
||||
let name = format!("{collection}<int>");
|
||||
let owner = objects
|
||||
.iter()
|
||||
.find(|o| {
|
||||
image.registry().declarations().types[o.type_id.0 as usize]
|
||||
.name
|
||||
.as_deref()
|
||||
== Some(&name)
|
||||
})
|
||||
.expect("owner")
|
||||
.clone();
|
||||
let V::Reference(storage) = owner.groups[0][0] else {
|
||||
panic!("backing reference")
|
||||
};
|
||||
let backing = objects
|
||||
.iter()
|
||||
.find(|o| o.reference == storage)
|
||||
.expect("backing")
|
||||
.clone();
|
||||
vec![owner, backing]
|
||||
}
|
||||
fn integer(group: &[V]) -> i64 {
|
||||
let [V::Int64(value)] = group else {
|
||||
panic!("long field {group:?}")
|
||||
};
|
||||
*value
|
||||
}
|
||||
fn validate(
|
||||
vm: &VirtualMachine,
|
||||
image: &ValidatedProgramImage,
|
||||
collection: &str,
|
||||
expected: &[i32],
|
||||
capacity: usize,
|
||||
start: usize,
|
||||
) {
|
||||
let objects = state(vm, image, collection);
|
||||
let owner = &objects[0];
|
||||
let backing = &objects[1];
|
||||
assert_eq!(backing.groups.len(), capacity);
|
||||
assert_eq!(
|
||||
integer(&owner.groups[if collection == "stack" { 1 } else { 2 }]),
|
||||
expected.len() as i64
|
||||
);
|
||||
if collection == "queue" {
|
||||
assert_eq!(integer(&owner.groups[1]), start as i64);
|
||||
}
|
||||
let mut occupied = std::collections::BTreeSet::new();
|
||||
for (offset, expected) in expected.iter().enumerate() {
|
||||
let index = (start + offset) % capacity;
|
||||
occupied.insert(index);
|
||||
let [V::Int64(_), V::Int32(value)] = backing.groups[index].as_slice() else {
|
||||
panic!("present complete int group")
|
||||
};
|
||||
assert_eq!(value, expected);
|
||||
}
|
||||
for (index, group) in backing.groups.iter().enumerate() {
|
||||
if !occupied.contains(&index) {
|
||||
assert_eq!(*group, vec![V::Null, V::Null], "inactive slot {index}");
|
||||
}
|
||||
}
|
||||
}
|
||||
fn release(vm: VirtualMachine) {
|
||||
let observer = vm.resource_observer();
|
||||
drop(vm);
|
||||
for q in [
|
||||
Q::GuestBytes,
|
||||
Q::HandleSlots,
|
||||
Q::AuxiliaryBytes,
|
||||
Q::TemporaryBytes,
|
||||
Q::StringBytes,
|
||||
Q::StringAllocations,
|
||||
] {
|
||||
assert_eq!(observer.used(q), 0, "last owner release {q:?}");
|
||||
}
|
||||
}
|
||||
pub(super) fn verify(root: &std::path::Path) {
|
||||
let successes = [
|
||||
"basic",
|
||||
"capacity",
|
||||
"arithmetic",
|
||||
"cursor-empty",
|
||||
"cursor-single",
|
||||
"snapshot",
|
||||
"cursor-order",
|
||||
"groups",
|
||||
"references",
|
||||
"native",
|
||||
"evaluation",
|
||||
];
|
||||
let mut cases = 0;
|
||||
for collection in ["stack", "queue"] {
|
||||
for (name, fault) in successes
|
||||
.into_iter()
|
||||
.map(|n| (n, None))
|
||||
.chain(
|
||||
["fault-negative", "fault-too-large", "fault-required"]
|
||||
.into_iter()
|
||||
.map(|n| (n, Some(prometeu_bytecode::TRAP_EXPLICIT))),
|
||||
)
|
||||
.chain((collection == "queue").then_some(("wrap", None)))
|
||||
{
|
||||
for suffix in ["", "-optimized"] {
|
||||
let image = load(root, collection, name, suffix);
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let report = vm
|
||||
.run_budget(20_000_000, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap();
|
||||
if let Some(expected) = fault {
|
||||
let End::Trap(info) = report.reason else {
|
||||
panic!("{collection}/{name}{suffix}: {report:?}")
|
||||
};
|
||||
assert_eq!(info.code, expected);
|
||||
} else {
|
||||
assert!(
|
||||
matches!(report.reason, End::Halted | End::EndOfRom),
|
||||
"{collection}/{name}{suffix}: {report:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
vm.operand_stack_top(1),
|
||||
vec![Value::Int32(42)],
|
||||
"{collection}/{name}{suffix}"
|
||||
);
|
||||
}
|
||||
release(vm);
|
||||
cases += 1;
|
||||
println!("STACK QUEUE SOURCE PASS {collection}/{name}{suffix}");
|
||||
}
|
||||
}
|
||||
refusals(root, collection);
|
||||
postwrite_probe(root, collection);
|
||||
gc(root, collection);
|
||||
}
|
||||
distribution(root);
|
||||
println!("STACK QUEUE MANDATORY BEHAVIOR CASES {cases}");
|
||||
}
|
||||
fn marker(image: &ValidatedProgramImage, value: i32) -> usize {
|
||||
let mut pc = 0;
|
||||
let mut found = Vec::new();
|
||||
while pc < image.code().len() {
|
||||
let i = decode_next(pc, image.code()).unwrap();
|
||||
if i.opcode == Op::PushI32 && i.imm_i32().unwrap() == value {
|
||||
found.push(pc);
|
||||
}
|
||||
pc = i.next_pc;
|
||||
}
|
||||
assert_eq!(found.len(), 1);
|
||||
found[0]
|
||||
}
|
||||
fn refusals(root: &std::path::Path, collection: &str) {
|
||||
let mut total = 0;
|
||||
let mut after_writes = 0;
|
||||
let mut backing_refusal = false;
|
||||
let mut snapshot_refusal = false;
|
||||
for name in [
|
||||
"insert", "growth", "zero", "remove", "clear", "peek", "cursor", "empty",
|
||||
]
|
||||
.into_iter()
|
||||
.chain((collection == "queue").then_some("wrapped"))
|
||||
{
|
||||
for suffix in ["", "-optimized"] {
|
||||
let image = load(root, collection, &format!("refusal-{name}"), suffix);
|
||||
let before_pc = marker(&image, 314160);
|
||||
let after_pc = marker(&image, 314161);
|
||||
let prepare = |limits| {
|
||||
let mut vm = VirtualMachine::from_image(image.clone(), limits);
|
||||
vm.prepare_boot_call().unwrap();
|
||||
vm.insert_breakpoint(before_pc);
|
||||
vm.insert_breakpoint(after_pc);
|
||||
vm
|
||||
};
|
||||
let mut baseline = prepare(RuntimeLimits::default());
|
||||
assert!(matches!(
|
||||
baseline
|
||||
.run_budget(20_000_000, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap()
|
||||
.reason,
|
||||
End::Breakpoint
|
||||
));
|
||||
let initial: Vec<i32> = if name == "zero" || name == "empty" {
|
||||
vec![]
|
||||
} else if name == "wrapped" {
|
||||
vec![2, 3, 4, 5]
|
||||
} else {
|
||||
vec![0, 1, 2, 3]
|
||||
};
|
||||
let old_capacity = if name == "zero" || name == "empty" {
|
||||
0
|
||||
} else if name == "growth" || name == "wrapped" {
|
||||
4
|
||||
} else {
|
||||
8
|
||||
};
|
||||
validate(
|
||||
&baseline,
|
||||
&image,
|
||||
collection,
|
||||
&initial,
|
||||
old_capacity,
|
||||
if name == "wrapped" { 2 } else { 0 },
|
||||
);
|
||||
let old = state(&baseline, &image, collection);
|
||||
let before = baseline.resource_snapshot();
|
||||
baseline.remove_breakpoint(before_pc);
|
||||
let mut successful = Vec::new();
|
||||
let mut max_arrays = 0;
|
||||
loop {
|
||||
max_arrays = max_arrays.max(
|
||||
baseline
|
||||
.heap()
|
||||
.diagnostic_typed_storage()
|
||||
.iter()
|
||||
.filter(|o| {
|
||||
matches!(
|
||||
image.registry().declarations().types[o.type_id.0 as usize].kind,
|
||||
prometeu_bytecode::registry::TypeKind::Array(_)
|
||||
)
|
||||
})
|
||||
.count(),
|
||||
);
|
||||
successful.push((baseline.pc(), observe_originals(&baseline, &old)));
|
||||
let report = baseline
|
||||
.run_budget(1, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap();
|
||||
if baseline.pc() == after_pc {
|
||||
break;
|
||||
}
|
||||
assert!(
|
||||
matches!(report.reason, End::BudgetExhausted),
|
||||
"{collection}/{name}: {report:?}"
|
||||
);
|
||||
assert!(successful.len() < 200_000);
|
||||
}
|
||||
let after = baseline.resource_snapshot();
|
||||
let mut expected = initial.clone();
|
||||
match name {
|
||||
"insert" | "growth" | "zero" | "wrapped" => expected.push(42),
|
||||
"remove" => {
|
||||
if collection == "stack" {
|
||||
expected.pop();
|
||||
} else {
|
||||
expected.remove(0);
|
||||
}
|
||||
}
|
||||
"clear" => expected.clear(),
|
||||
_ => (),
|
||||
}
|
||||
let capacity = match name {
|
||||
"zero" => 1,
|
||||
"growth" | "wrapped" => 6,
|
||||
_ => old_capacity,
|
||||
};
|
||||
validate(
|
||||
&baseline,
|
||||
&image,
|
||||
collection,
|
||||
&expected,
|
||||
capacity,
|
||||
if collection == "queue" && name == "remove" {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
},
|
||||
);
|
||||
if ["growth", "zero", "wrapped"].contains(&name) {
|
||||
assert!(max_arrays >= 2, "old/candidate coexistence");
|
||||
} else {
|
||||
assert_eq!(
|
||||
max_arrays, 1,
|
||||
"ordinary update/capture never reconstructs backing"
|
||||
);
|
||||
}
|
||||
if ["peek", "cursor", "empty"].contains(&name) {
|
||||
assert_eq!(state(&baseline, &image, collection), old);
|
||||
}
|
||||
println!(
|
||||
"STACK QUEUE PEAK {collection}/{name}{suffix} arrays={max_arrays} before={:?} after={:?}",
|
||||
before.budget.peaks, after.budget.peaks
|
||||
);
|
||||
let mut count = 0;
|
||||
let mut partial = 0;
|
||||
for q in [
|
||||
Q::GuestBytes,
|
||||
Q::HandleSlots,
|
||||
Q::AuxiliaryBytes,
|
||||
Q::TemporaryBytes,
|
||||
Q::CallDepth,
|
||||
] {
|
||||
let lower = before.budget.peaks[q as usize];
|
||||
let upper = after.budget.peaks[q as usize];
|
||||
if lower >= upper {
|
||||
continue;
|
||||
}
|
||||
let mut limits = vec![lower, lower + (upper - lower) / 2, upper - 1];
|
||||
if q == Q::HandleSlots {
|
||||
limits.extend(lower..upper.min(lower + 24));
|
||||
}
|
||||
limits.sort_unstable();
|
||||
limits.dedup();
|
||||
for limit in limits {
|
||||
let mut vm = prepare(RuntimeLimits::default().lowered(q, limit).unwrap());
|
||||
assert!(
|
||||
matches!(
|
||||
vm.run_budget(20_000_000, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap()
|
||||
.reason,
|
||||
End::Breakpoint
|
||||
),
|
||||
"{collection}/{name}/{q:?}={limit} setup"
|
||||
);
|
||||
let original = state(&vm, &image, collection);
|
||||
vm.remove_breakpoint(before_pc);
|
||||
let (report, prefix, mutated) =
|
||||
trace_collection_attempt(&mut vm, &original, after_pc);
|
||||
assert_mutation_prefix(&prefix, &successful);
|
||||
if let End::Trap(info) = report.reason {
|
||||
assert_eq!(info.code, TRAP_RESOURCE_LIMIT);
|
||||
let current = observe_originals(&vm, &original);
|
||||
if !mutated {
|
||||
assert_eq!(
|
||||
current, original,
|
||||
"before first write preserves full original state"
|
||||
);
|
||||
}
|
||||
if ["peek", "cursor", "empty"].contains(&name) {
|
||||
assert_eq!(current, original);
|
||||
}
|
||||
let instruction = decode_next(info.pc as usize, image.code()).unwrap();
|
||||
backing_refusal |= instruction.opcode == Op::ArrayNew;
|
||||
if instruction.opcode == Op::StructNew {
|
||||
snapshot_refusal |= image.registry().declarations().types
|
||||
[instruction.imm_u32().unwrap() as usize]
|
||||
.name
|
||||
.as_deref()
|
||||
== Some("_SnapshotNode<int>");
|
||||
}
|
||||
count += 1;
|
||||
partial += usize::from(mutated);
|
||||
println!(
|
||||
"STACK QUEUE REFUSAL {collection}/{name}{suffix} mutated={mutated} resource={q:?} limit={limit} opcode={:?}",
|
||||
instruction.opcode
|
||||
);
|
||||
} else {
|
||||
assert!(
|
||||
matches!(report.reason, End::BudgetExhausted | End::Breakpoint)
|
||||
&& vm.pc() == after_pc
|
||||
);
|
||||
}
|
||||
release(vm);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
count > 0,
|
||||
"{collection}/{name}: operation-specific natural refusal required"
|
||||
);
|
||||
total += count;
|
||||
after_writes += partial;
|
||||
println!(
|
||||
"STACK QUEUE PHASE SUMMARY {collection}/{name}{suffix} refusals={count} after_write={partial}"
|
||||
);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
backing_refusal && snapshot_refusal,
|
||||
"backing and snapshot preparation refusals required"
|
||||
);
|
||||
println!("STACK QUEUE NATURAL REFUSALS {collection} total={total} after_write={after_writes}");
|
||||
}
|
||||
fn postwrite_probe(root: &std::path::Path, collection: &str) {
|
||||
// Separate test-only instrumentation: not natural failure frequency evidence.
|
||||
for suffix in ["", "-optimized"] {
|
||||
let image = load(root, collection, "injected-growth", suffix);
|
||||
let mut vm = VirtualMachine::from_image(
|
||||
image.clone(),
|
||||
RuntimeLimits::default()
|
||||
.lowered(Q::GuestBytes, 500_000)
|
||||
.unwrap(),
|
||||
);
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let mut found = false;
|
||||
for _ in 0..100_000 {
|
||||
let instruction = decode_next(vm.pc(), image.code()).unwrap();
|
||||
if instruction.opcode == Op::ArrayNew
|
||||
&& vm.operand_stack_top(1) == vec![Value::Int64(1_000_000)]
|
||||
{
|
||||
let original = vm.heap().diagnostic_typed_storage();
|
||||
let objects = state(&vm, &image, collection);
|
||||
assert_eq!(
|
||||
integer(&objects[0].groups[if collection == "stack" { 1 } else { 2 }]),
|
||||
2
|
||||
);
|
||||
assert_eq!(objects[1].groups.len(), 3);
|
||||
assert!(matches!(
|
||||
objects[1].groups[2].as_slice(),
|
||||
[V::Int64(_), V::Int32(42)]
|
||||
));
|
||||
let operands = vm.operand_stack_top(1).to_vec();
|
||||
let report = vm
|
||||
.run_budget(1, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap();
|
||||
let End::Trap(info) = report.reason else {
|
||||
panic!("post-write probe: {report:?}")
|
||||
};
|
||||
assert_eq!(info.code, TRAP_RESOURCE_LIMIT);
|
||||
assert_eq!(vm.heap().diagnostic_typed_storage(), original);
|
||||
assert_eq!(vm.operand_stack_top(1), operands);
|
||||
println!(
|
||||
"STACK QUEUE TEST-ONLY POST-WRITE PASS {collection}{suffix}: backing=3 old size=2 appended=42 retained"
|
||||
);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
let report = vm
|
||||
.run_budget(1, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap();
|
||||
assert!(matches!(report.reason, End::BudgetExhausted));
|
||||
}
|
||||
assert!(found);
|
||||
release(vm);
|
||||
}
|
||||
}
|
||||
fn gc(root: &std::path::Path, collection: &str) {
|
||||
for suffix in ["", "-optimized"] {
|
||||
for (name, counts) in [
|
||||
("gc-snapshot", vec![1200, 1, 1, 0]),
|
||||
("gc-collection", vec![64, 61, 0, 1, 0]),
|
||||
] {
|
||||
let image = load(root, collection, name, suffix);
|
||||
let mut vm = VirtualMachine::from_image(image.clone(), Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let mut previous = 0;
|
||||
for (round, count) in counts.into_iter().enumerate() {
|
||||
let report = vm
|
||||
.run_budget(100_000_000, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap();
|
||||
assert!(
|
||||
matches!(report.reason, End::FrameSync),
|
||||
"{collection}/{name}/{round}: {report:?}"
|
||||
);
|
||||
let collections = vm.resource_snapshot().gc.collections;
|
||||
assert!(
|
||||
collections > previous,
|
||||
"actual collection, not only safepoint"
|
||||
);
|
||||
previous = collections;
|
||||
let objects = vm.heap().diagnostic_typed_storage();
|
||||
let named = |n: &str| {
|
||||
objects
|
||||
.iter()
|
||||
.filter(|o| {
|
||||
image.registry().declarations().types[o.type_id.0 as usize]
|
||||
.name
|
||||
.as_deref()
|
||||
== Some(n)
|
||||
})
|
||||
.count()
|
||||
};
|
||||
assert_eq!(
|
||||
named("Item"),
|
||||
count,
|
||||
"{collection}/{name}/{round}: payload roots"
|
||||
);
|
||||
let arrays = objects
|
||||
.iter()
|
||||
.filter(|o| {
|
||||
matches!(
|
||||
image.registry().declarations().types[o.type_id.0 as usize].kind,
|
||||
prometeu_bytecode::registry::TypeKind::Array(_)
|
||||
)
|
||||
})
|
||||
.count();
|
||||
if name == "gc-snapshot" {
|
||||
assert_eq!(named("_SnapshotNode<Item>"), count);
|
||||
assert_eq!(arrays, 0, "snapshot retains no source backing");
|
||||
} else {
|
||||
assert_eq!(
|
||||
arrays,
|
||||
usize::from(round < 4),
|
||||
"only retained backing, no replaced arrays"
|
||||
);
|
||||
assert_eq!(named("_SnapshotNode<Item>"), 0);
|
||||
}
|
||||
println!(
|
||||
"STACK QUEUE GC PASS {collection}/{name}{suffix} round={round} items={count} arrays={arrays}"
|
||||
);
|
||||
}
|
||||
let report = vm
|
||||
.run_budget(100_000, &mut NoNative, &mut HostContext::new(None))
|
||||
.unwrap();
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom));
|
||||
assert_eq!(vm.operand_stack_top(1), vec![Value::Int32(42)]);
|
||||
release(vm);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn distribution(root: &std::path::Path) {
|
||||
for name in ["imports", "installed"] {
|
||||
let image = load(root, "stack-queue", name, "");
|
||||
let mut vm = VirtualMachine::from_image(image.clone(), Default::default());
|
||||
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::FrameSync | End::Halted | End::EndOfRom),
|
||||
"{name}: {report:?}"
|
||||
);
|
||||
let objects = vm.heap().diagnostic_typed_storage();
|
||||
let get = |reference| objects.iter().find(|o| o.reference == reference).unwrap();
|
||||
let mut lasts = Vec::new();
|
||||
for cursor in objects.iter().filter(|o| {
|
||||
image.registry().declarations().types[o.type_id.0 as usize]
|
||||
.name
|
||||
.as_deref()
|
||||
== Some("_SnapshotCursor<int>")
|
||||
}) {
|
||||
let V::Reference(carrier) = cursor.groups[0][0] else {
|
||||
panic!("current carrier")
|
||||
};
|
||||
let V::Reference(node) = get(carrier).groups[0][0] else {
|
||||
panic!("node")
|
||||
};
|
||||
let node = get(node);
|
||||
assert_eq!(node.groups[1], vec![V::Null], "completed traversal");
|
||||
let V::Int32(value) = node.groups[0][0] else {
|
||||
panic!("value")
|
||||
};
|
||||
lasts.push(value);
|
||||
}
|
||||
lasts.sort_unstable();
|
||||
assert_eq!(
|
||||
lasts,
|
||||
vec![20, 22],
|
||||
"both order-sensitive snapshots traversed"
|
||||
);
|
||||
release(vm);
|
||||
println!("STACK QUEUE DISTRIBUTION PASS {name}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user