Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Remove INDEX_GUARD, ARRAY_CHECK_LENGTH, STORAGE_PUBLISH and FIELDS_PUBLISH from decoding, metadata, tooling, verification and execution. Keep ARRAY_LEN and overlap-safe ARRAY_COPY with complete-group ownership and early charging of calculable copy work, including refused attempts. Update specifications, producer handoff, qualification evidence and lessons. Share collection test support and remove redundant Copy clones for Clippy. Validation: make ci-domains passed with 1,095 workspace tests, five separately executed debugger socket tests and all 15 unchanged coverage domain gates. VM release, resource diagnostics and layer/quality suites also passed. BREAKING CHANGE: PBX using opcodes 0xA7, 0xA8, 0xAA or 0xAB is rejected, including unreachable instructions. Consumers must adapt their emission and collection algorithms or operational guarantees.
503 lines
22 KiB
Rust
503 lines
22 KiB
Rust
use super::collection_test_support as support;
|
|
use super::*;
|
|
use crate::value_contract::ValueError;
|
|
use prometeu_bytecode::assemble;
|
|
use support::Native;
|
|
fn vm(text: &str, limits: RuntimeLimits) -> VirtualMachine {
|
|
let mut vm =
|
|
VirtualMachine::from_image(support::load(&assemble(text).unwrap()).unwrap(), limits);
|
|
vm.prepare_boot_call().unwrap();
|
|
vm
|
|
}
|
|
fn attempt(
|
|
text: &str,
|
|
values: &[Value],
|
|
limits: RuntimeLimits,
|
|
cost: u64,
|
|
) -> (VirtualMachine, Result<(), LogicalFrameEndingReason>) {
|
|
let mut prefix = String::new();
|
|
for i in 0..values.len() {
|
|
prefix += &format!("GET_GLOBAL {i}\n");
|
|
}
|
|
let mut vm = vm(&(prefix + text + "\nHALT"), limits);
|
|
vm.globals.extend_from_slice(values).unwrap();
|
|
for _ in values {
|
|
vm.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
let before = vm.cycles;
|
|
let result = vm.step(&mut Native, &mut HostContext::new(None));
|
|
assert_eq!(vm.cycles - before, cost);
|
|
(vm, result)
|
|
}
|
|
fn trap(result: Result<(), LogicalFrameEndingReason>, code: u32) {
|
|
match result.unwrap_err() {
|
|
LogicalFrameEndingReason::Trap(info) => assert_eq!(info.code, code),
|
|
other => panic!("{other:?}"),
|
|
}
|
|
}
|
|
#[test]
|
|
fn new_attempt_costs_budget_and_transactional_refusal() {
|
|
for (length, cost, fault) in [
|
|
(Value::Int64(-1), 8, TRAP_OOB),
|
|
(Value::Int32(10), 8, TRAP_TYPE),
|
|
(Value::Null, 8, TRAP_TYPE),
|
|
(Value::Int64(u32::MAX as i64 + 1), 8, TRAP_RESOURCE_LIMIT),
|
|
(Value::Int64(10), 28, TRAP_RESOURCE_LIMIT),
|
|
] {
|
|
let limits = RuntimeLimits::default()
|
|
.lowered(RuntimeResource::GuestBytes, size_of::<crate::heap::StoredObject>() as u64)
|
|
.unwrap();
|
|
let (vm, result) = attempt("ARRAY_NEW 12", std::slice::from_ref(&length), limits, cost);
|
|
trap(result, fault);
|
|
assert_eq!(vm.operand_stack.as_slice(), &[length]);
|
|
assert_eq!(vm.budget.used(RuntimeResource::GuestBytes), 0);
|
|
assert_eq!(vm.budget.used(RuntimeResource::HandleSlots), 0);
|
|
}
|
|
let mut vm = vm("PUSH_I64 10\nARRAY_NEW 12\nHALT", RuntimeLimits::default());
|
|
vm.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let pc = vm.pc;
|
|
let cycles = vm.cycles;
|
|
let r = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
|
|
assert_eq!((r.cycles_used, r.steps_executed), (0, 0));
|
|
assert_eq!((vm.pc, vm.cycles), (pc, cycles));
|
|
let r = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
|
|
assert_eq!(
|
|
(r.cycles_used, r.steps_executed, r.reason),
|
|
(28, 1, LogicalFrameEndingReason::BudgetExhausted)
|
|
);
|
|
let limits = RuntimeLimits::default()
|
|
.lowered(RuntimeResource::GuestBytes, size_of::<crate::heap::StoredObject>() as u64)
|
|
.unwrap();
|
|
let mut vm = vm_for_refusal(limits);
|
|
let r = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
|
|
assert_eq!((r.cycles_used, r.steps_executed), (28, 0));
|
|
assert!(matches!(r.reason, LogicalFrameEndingReason::Trap(_)));
|
|
}
|
|
fn vm_for_refusal(limits: RuntimeLimits) -> VirtualMachine {
|
|
let mut v = vm("PUSH_I64 10\nARRAY_NEW 12\nHALT", limits);
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
v
|
|
}
|
|
#[test]
|
|
fn underflow_is_charged_without_consuming_frame_storage() {
|
|
let mut vm = vm("GET_GLOBAL 0\nARRAY_NEW 12\nHALT", RuntimeLimits::default());
|
|
vm.pc = 6;
|
|
let before = vm.cycles;
|
|
trap(vm.step(&mut Native, &mut HostContext::new(None)), TRAP_STACK_UNDERFLOW);
|
|
assert_eq!(vm.cycles - before, 8);
|
|
assert!(vm.operand_stack.is_empty());
|
|
}
|
|
#[test]
|
|
fn receiver_then_index_then_group_checks_preserve_operands_and_storage() {
|
|
let mut vm = vm(
|
|
"GET_GLOBAL 0\nGET_GLOBAL 1\nGET_GLOBAL 2\nGET_GLOBAL 3\nGET_GLOBAL 4\nARRAY_SET 13\nHALT",
|
|
RuntimeLimits::default(),
|
|
);
|
|
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
|
let r = vm
|
|
.heap
|
|
.allocate_typed_array(registry.clone(), prometeu_bytecode::registry::TypeId(13), 1)
|
|
.unwrap();
|
|
let old = [Value::Int64(1), Value::Int64(2), Value::Int64(3)];
|
|
vm.heap.store_typed(®istry, r, 0, &old).unwrap();
|
|
for (receiver, index, fault) in [
|
|
(Value::Null, Value::Int32(0), TRAP_NULL_REFERENCE),
|
|
(Value::HeapRef(r), Value::Int32(0), TRAP_TYPE),
|
|
(Value::HeapRef(r), Value::Int64(1), TRAP_OOB),
|
|
(Value::HeapRef(r), Value::Int64(0), TRAP_TYPE),
|
|
] {
|
|
vm.pc = 0;
|
|
vm.operand_stack.clear();
|
|
vm.globals.clear();
|
|
let inputs = [receiver, index, Value::Int64(7), Value::Int64(8), Value::Int64(-1)];
|
|
vm.globals.extend_from_slice(&inputs).unwrap();
|
|
for _ in 0..5 {
|
|
vm.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
let before = vm.cycles;
|
|
trap(vm.step(&mut Native, &mut HostContext::new(None)), fault);
|
|
assert_eq!(vm.cycles - before, 7);
|
|
assert_eq!(vm.operand_stack.as_slice(), &inputs);
|
|
assert_eq!(vm.heap.load_typed(®istry, r, 0).unwrap().as_slice(), &old);
|
|
}
|
|
assert_eq!(vm.heap.concrete_type(r, ®istry), Ok(prometeu_bytecode::registry::TypeId(13)));
|
|
let foreign = support::load(&assemble("HALT").unwrap()).unwrap();
|
|
assert_eq!(vm.heap.concrete_type(r, foreign.registry()), Err(ValueError::Type));
|
|
}
|
|
#[test]
|
|
fn default_groups_and_roundtrip_all_storage_types_keep_identity_and_owners() {
|
|
let mut vm = vm("STRUCT_NEW 9\nSET_GLOBAL 0\nHALT", RuntimeLimits::default());
|
|
vm.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let Value::HeapRef(r) = vm.operand_stack[0] else { panic!() };
|
|
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
|
let defaults = [
|
|
vec![Value::Int32(0)],
|
|
vec![Value::Int64(0)],
|
|
vec![Value::Float(0.)],
|
|
vec![Value::Boolean(false)],
|
|
vec![Value::Null],
|
|
vec![Value::Float(0.), Value::Float(0.)],
|
|
vec![Value::Int64(0)],
|
|
vec![Value::Int64(0), Value::Int64(0), Value::Int64(0)],
|
|
vec![Value::Null, Value::Null],
|
|
vec![Value::Null],
|
|
vec![Value::Null, Value::Null],
|
|
vec![Value::Null],
|
|
];
|
|
for (field, group) in defaults.iter().enumerate() {
|
|
assert_eq!(vm.heap.load_typed(®istry, r, field).unwrap().as_slice(), group);
|
|
}
|
|
// Present Trait implementation zero and shared String/Dynamic/reference values.
|
|
let string = Value::String(std::sync::Arc::from("owned"));
|
|
for (field, group) in [
|
|
(4, vec![string.clone()]),
|
|
(8, vec![Value::Int64(4), string]),
|
|
(9, vec![Value::HeapRef(r)]),
|
|
(10, vec![Value::HeapRef(r), Value::Int64(0)]),
|
|
] {
|
|
vm.heap.store_typed(®istry, r, field, &group).unwrap();
|
|
assert_eq!(vm.heap.load_typed(®istry, r, field).unwrap().as_slice(), group);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn decoded_stores_and_reads_preserve_complete_native_and_reference_groups() {
|
|
for (field, group) in [
|
|
(0, vec![Value::Int32(7)]),
|
|
(1, vec![Value::Int64(8)]),
|
|
(2, vec![Value::Float(1.)]),
|
|
(3, vec![Value::Boolean(true)]),
|
|
(4, vec![Value::String(std::sync::Arc::from(""))]),
|
|
(5, vec![Value::Float(1.), Value::Float(2.)]),
|
|
(6, vec![Value::Int64(u32::MAX.into())]),
|
|
(7, vec![Value::Int64(1), Value::Int64(2), Value::Int64(3)]),
|
|
(8, vec![Value::Null, Value::Null]),
|
|
(9, vec![Value::Null]),
|
|
(10, vec![Value::Null, Value::Null]),
|
|
(11, vec![Value::Null]),
|
|
] {
|
|
let mut text = "STRUCT_NEW 9\nSET_GLOBAL 0\nGET_GLOBAL 0\n".to_string();
|
|
for i in 0..group.len() {
|
|
text += &format!("GET_GLOBAL {}\n", i + 1);
|
|
}
|
|
text += &format!(
|
|
"STRUCT_SET type=9, field={field}\nGET_GLOBAL 0\nSTRUCT_GET type=9, field={field}\nHALT"
|
|
);
|
|
let mut v = vm(&text, RuntimeLimits::default());
|
|
v.globals.push(Value::Null).unwrap();
|
|
v.globals.extend_from_slice(&group).unwrap();
|
|
assert_eq!(
|
|
v.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap().reason,
|
|
LogicalFrameEndingReason::Halted
|
|
);
|
|
assert_eq!(v.operand_stack.as_slice(), group);
|
|
}
|
|
}
|
|
#[test]
|
|
fn provisional_allocation_and_handle_refusals_publish_nothing() {
|
|
for quota in [
|
|
RuntimeResource::HandleSlots,
|
|
RuntimeResource::AuxiliaryBytes,
|
|
RuntimeResource::TemporaryBytes,
|
|
] {
|
|
let mut v = vm("PUSH_I64 10\nARRAY_NEW 12\nHALT", RuntimeLimits::default());
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
// Test-only accounting state: lower the resource ceiling to current usage
|
|
// after boot, without changing any production constructor/check.
|
|
if quota == RuntimeResource::TemporaryBytes {
|
|
v.heap.allocate_object(crate::object::ObjectKind::Bytes, &[]).unwrap();
|
|
}
|
|
let current = v.budget.used(quota);
|
|
v.budget.test_lower_limit(quota, current);
|
|
let guest = v.budget.used(RuntimeResource::GuestBytes);
|
|
let live = v.heap.len();
|
|
let allocations = v.heap.allocation_count();
|
|
let before = v.cycles;
|
|
trap(v.step(&mut Native, &mut HostContext::new(None)), TRAP_RESOURCE_LIMIT);
|
|
assert_eq!(v.cycles - before, 28);
|
|
assert_eq!(v.heap.len(), live);
|
|
assert_eq!(v.heap.allocation_count(), allocations);
|
|
assert_eq!(v.operand_stack.as_slice(), &[Value::Int64(10)]);
|
|
assert_eq!(v.budget.used(RuntimeResource::GuestBytes), guest);
|
|
}
|
|
let mut v = vm("PUSH_I64 10\nARRAY_NEW 12\nHALT", RuntimeLimits::default());
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let guest = v.budget.used(RuntimeResource::GuestBytes);
|
|
let guard = crate::typed_storage::refuse_next_allocation();
|
|
let failure = v.step(&mut Native, &mut HostContext::new(None));
|
|
drop(guard);
|
|
let LogicalFrameEndingReason::Trap(t) = failure.unwrap_err() else { panic!() };
|
|
assert_eq!(
|
|
t.resource.unwrap().reason,
|
|
crate::resource_budget::ResourceReason::AllocationFailed
|
|
);
|
|
assert_eq!(v.budget.used(RuntimeResource::GuestBytes), guest);
|
|
assert_eq!(v.operand_stack.as_slice(), &[Value::Int64(10)]);
|
|
assert_eq!(v.heap.len(), 0);
|
|
}
|
|
#[test]
|
|
fn rooted_native_owners_boxes_traits_and_cycles_survive_then_reclaim_at_real_safepoints() {
|
|
let text = "STRUCT_NEW 9\nSET_GLOBAL 0\nGET_GLOBAL 0\nSTRUCT_GET type=9, field=4\nSET_GLOBAL 9\nPUSH_I64 1\nARRAY_NEW 17\nSET_GLOBAL 1\nGET_GLOBAL 1\nPUSH_I64 0\nGET_GLOBAL 0\nARRAY_SET 17\nGET_GLOBAL 0\nGET_GLOBAL 0\nSTRUCT_SET type=9, field=9\nGET_GLOBAL 0\nGET_GLOBAL 0\nREF_TO_TRAIT source=9, target=10\nSTRUCT_SET type=9, field=10\nGET_GLOBAL 0\nPUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nDYN_PACK 5\nSTRUCT_SET type=9, field=8\nGET_GLOBAL 0\nGET_GLOBAL 2\nSTRUCT_SET type=9, field=4\nPUSH_I64 1\nARRAY_NEW 14\nSET_GLOBAL 3\nGET_GLOBAL 3\nPUSH_I64 0\nGET_GLOBAL 2\nARRAY_SET 14\nPUSH_I64 1\nARRAY_NEW 15\nSET_GLOBAL 4\nGET_GLOBAL 4\nPUSH_I64 0\nGET_GLOBAL 2\nDYN_PACK 4\nARRAY_SET 15\nFRAME_SYNC\nGET_GLOBAL 9\nSET_GLOBAL 2\nGET_GLOBAL 9\nSET_GLOBAL 3\nFRAME_SYNC\nGET_GLOBAL 9\nSET_GLOBAL 0\nGET_GLOBAL 9\nSET_GLOBAL 1\nGET_GLOBAL 9\nSET_GLOBAL 4\nSTRUCT_NEW 19\nPOP_N 1\nFRAME_SYNC\nHALT";
|
|
let mut v = vm(text, RuntimeLimits::default());
|
|
v.gc_alloc_threshold = 1;
|
|
v.globals.resize(3, Value::Null).unwrap();
|
|
v.push(Value::String(std::sync::Arc::from("one content"))).unwrap();
|
|
let string = v.pop().unwrap();
|
|
v.globals[2] = string;
|
|
assert_eq!(
|
|
v.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap().reason,
|
|
LogicalFrameEndingReason::FrameSync
|
|
);
|
|
let Value::HeapRef(node) = v.globals[0] else { panic!() };
|
|
let registry = v.validated_image.as_ref().unwrap().registry().clone();
|
|
assert_eq!(
|
|
v.heap.load_typed(®istry, node, 10).unwrap().as_slice(),
|
|
&[Value::HeapRef(node), Value::Int64(0)]
|
|
);
|
|
assert_eq!(v.budget.used(RuntimeResource::StringBytes), 11);
|
|
assert_eq!(v.budget.used(RuntimeResource::StringAllocations), 1);
|
|
assert_eq!(
|
|
v.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap().reason,
|
|
LogicalFrameEndingReason::FrameSync
|
|
);
|
|
assert!(v.heap.is_valid(node));
|
|
assert_eq!(v.budget.used(RuntimeResource::StringAllocations), 1);
|
|
assert_eq!(
|
|
v.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap().reason,
|
|
LogicalFrameEndingReason::FrameSync
|
|
);
|
|
assert!(!v.heap.is_valid(node));
|
|
assert_eq!(v.heap.len(), 0);
|
|
assert_eq!(v.budget.used(RuntimeResource::StringBytes), 0);
|
|
assert_eq!(v.budget.used(RuntimeResource::StringAllocations), 0);
|
|
assert_eq!(v.heap.concrete_type(node, ®istry), Err(ValueError::InvalidHandle));
|
|
}
|
|
|
|
#[test]
|
|
fn exact_struct_weights_and_empty_construction_charge_once() {
|
|
for (ty, cost) in [(19, 8), (21, 13)] {
|
|
let (v, result) = attempt(&format!("STRUCT_NEW {ty}"), &[], RuntimeLimits::default(), cost);
|
|
result.unwrap();
|
|
assert!(matches!(v.operand_stack[0], Value::HeapRef(_)));
|
|
}
|
|
let (v, result) = attempt("ARRAY_NEW 12", &[Value::Int64(0)], RuntimeLimits::default(), 8);
|
|
result.unwrap();
|
|
assert!(matches!(v.operand_stack[0], Value::HeapRef(_)));
|
|
}
|
|
#[test]
|
|
fn arrays_of_every_builtin_and_reference_view_default_and_roundtrip_by_decoded_ops() {
|
|
for (ty, default, value) in [
|
|
(11, vec![Value::Int32(0)], vec![Value::Int32(7)]),
|
|
(22, vec![Value::Int64(0)], vec![Value::Int64(8)]),
|
|
(23, vec![Value::Float(0.)], vec![Value::Float(1.)]),
|
|
(24, vec![Value::Boolean(false)], vec![Value::Boolean(true)]),
|
|
(14, vec![Value::Null], vec![Value::String(std::sync::Arc::from("shared"))]),
|
|
(12, vec![Value::Float(0.), Value::Float(0.)], vec![Value::Float(1.), Value::Float(2.)]),
|
|
(25, vec![Value::Int64(0)], vec![Value::Int64(42)]),
|
|
(
|
|
13,
|
|
vec![Value::Int64(0), Value::Int64(0), Value::Int64(0)],
|
|
vec![Value::Int64(1), Value::Int64(2), Value::Int64(3)],
|
|
),
|
|
(15, vec![Value::Null, Value::Null], vec![Value::Int64(4), Value::Null]),
|
|
(16, vec![Value::Null, Value::Null], vec![Value::Null, Value::Null]),
|
|
(17, vec![Value::Null], vec![Value::Null]),
|
|
(18, vec![Value::Null], vec![Value::Null]),
|
|
] {
|
|
let mut text = format!(
|
|
"PUSH_I64 1\nARRAY_NEW {ty}\nSET_GLOBAL 0\nGET_GLOBAL 0\nPUSH_I64 0\nARRAY_GET {ty}\nPOP_N {}\nGET_GLOBAL 0\nPUSH_I64 0\n",
|
|
value.len()
|
|
);
|
|
for i in 0..value.len() {
|
|
text += &format!("GET_GLOBAL {}\n", i + 1);
|
|
}
|
|
text += &format!("ARRAY_SET {ty}\nGET_GLOBAL 0\nPUSH_I64 0\nARRAY_GET {ty}\nHALT");
|
|
let mut v = vm(&text, RuntimeLimits::default());
|
|
v.globals.push(Value::Null).unwrap();
|
|
v.globals.extend_from_slice(&value).unwrap();
|
|
for _ in 0..6 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
assert_eq!(v.operand_stack.as_slice(), default);
|
|
assert_eq!(
|
|
v.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap().reason,
|
|
LogicalFrameEndingReason::Halted
|
|
);
|
|
assert_eq!(v.operand_stack.as_slice(), value);
|
|
}
|
|
}
|
|
#[test]
|
|
fn invalid_stale_wrong_nominal_and_foreign_registry_receivers_are_charged_and_preserved() {
|
|
let mut v = vm("GET_GLOBAL 0\nARRAY_LEN 11\nHALT", RuntimeLimits::default());
|
|
let registry = v.validated_image.as_ref().unwrap().registry().clone();
|
|
let wrong = v
|
|
.heap
|
|
.allocate_typed_struct(registry.clone(), prometeu_bytecode::registry::TypeId(19))
|
|
.unwrap();
|
|
let foreign = support::load(&assemble("HALT").unwrap()).unwrap();
|
|
let foreign_ref = v
|
|
.heap
|
|
.allocate_typed_array(
|
|
foreign.registry().clone(),
|
|
prometeu_bytecode::registry::TypeId(11),
|
|
0,
|
|
)
|
|
.unwrap();
|
|
for (value, code) in [
|
|
(Value::Int32(0), TRAP_TYPE),
|
|
(Value::Null, TRAP_NULL_REFERENCE),
|
|
(Value::HeapRef(HeapRef { index: u32::MAX, generation: 1 }), TRAP_INVALID_HANDLE),
|
|
(
|
|
Value::HeapRef(HeapRef { index: wrong.index, generation: wrong.generation + 1 }),
|
|
TRAP_INVALID_HANDLE,
|
|
),
|
|
(Value::HeapRef(wrong), TRAP_TYPE),
|
|
(Value::HeapRef(foreign_ref), TRAP_TYPE),
|
|
] {
|
|
v.pc = 0;
|
|
v.operand_stack.clear();
|
|
v.globals.clear();
|
|
v.globals.push(value.clone()).unwrap();
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
let before = v.cycles;
|
|
trap(v.step(&mut Native, &mut HostContext::new(None)), code);
|
|
assert_eq!(v.cycles - before, 3);
|
|
assert_eq!(v.operand_stack.as_slice(), &[value]);
|
|
}
|
|
}
|
|
#[test]
|
|
fn malformed_trait_and_dynamic_stores_reject_the_whole_group() {
|
|
for (ty, group) in [
|
|
(16, vec![Value::Null, Value::Int64(0)]),
|
|
(16, vec![Value::Int32(0), Value::Null]),
|
|
(15, vec![Value::Null, Value::Int32(0)]),
|
|
(15, vec![Value::Int64(5), Value::Int32(0)]),
|
|
(15, vec![Value::Int64(999), Value::Null]),
|
|
] {
|
|
let text = format!(
|
|
"PUSH_I64 1\nARRAY_NEW {ty}\nSET_GLOBAL 0\nGET_GLOBAL 0\nPUSH_I64 0\nGET_GLOBAL 1\nGET_GLOBAL 2\nARRAY_SET {ty}\nHALT"
|
|
);
|
|
let mut v = vm(&text, RuntimeLimits::default());
|
|
v.globals.push(Value::Null).unwrap();
|
|
v.globals.extend_from_slice(&group).unwrap();
|
|
for _ in 0..7 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
let before = v.cycles;
|
|
trap(v.step(&mut Native, &mut HostContext::new(None)), TRAP_TYPE);
|
|
assert_eq!(v.cycles - before, 6);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
let Value::HeapRef(r) = v.globals[0] else { panic!() };
|
|
let registry = v.validated_image.as_ref().unwrap().registry();
|
|
assert_eq!(
|
|
v.heap.load_typed(registry, r, 0).unwrap().as_slice(),
|
|
&[Value::Null, Value::Null]
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn typed_loader_rejects_out_of_registry_collections_even_when_unreachable() {
|
|
for text in [
|
|
"STRUCT_NEW 99",
|
|
"STRUCT_GET type=99,field=0",
|
|
"STRUCT_SET type=99,field=0",
|
|
"ARRAY_NEW 99",
|
|
"ARRAY_LEN 99",
|
|
"ARRAY_GET 99",
|
|
"ARRAY_SET 99",
|
|
] {
|
|
let code = assemble(&format!("HALT\n{text}")).unwrap();
|
|
assert!(support::load(&code).is_err());
|
|
}
|
|
}
|
|
#[test]
|
|
fn invalid_string_overwrite_keeps_the_previous_native_owner() {
|
|
let text = "STRUCT_NEW 9\nSET_GLOBAL 0\nGET_GLOBAL 0\nGET_GLOBAL 1\nSTRUCT_SET type=9, field=4\nGET_GLOBAL 0\nGET_GLOBAL 2\nSTRUCT_SET type=9, field=4\nHALT";
|
|
let mut v = vm(text, RuntimeLimits::default());
|
|
let text: std::sync::Arc<str> = std::sync::Arc::from("old owner");
|
|
v.globals
|
|
.extend_from_slice(&[Value::Null, Value::String(text.clone()), Value::Int32(3)])
|
|
.unwrap();
|
|
for _ in 0..7 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
let before = v.cycles;
|
|
trap(v.step(&mut Native, &mut HostContext::new(None)), TRAP_TYPE);
|
|
assert_eq!(v.cycles - before, 5);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
let Value::HeapRef(r) = v.globals[0] else { panic!() };
|
|
let registry = v.validated_image.as_ref().unwrap().registry();
|
|
let group = v.heap.load_typed(registry, r, 4).unwrap();
|
|
let Value::String(stored) = &group.as_slice()[0] else { panic!() };
|
|
assert!(std::sync::Arc::ptr_eq(stored, &text));
|
|
}
|
|
|
|
#[test]
|
|
fn pixel_get_cost_bounds_and_result_reservation_failures_preserve_sources() {
|
|
for lowered in
|
|
[None, Some(RuntimeResource::AuxiliaryBytes), Some(RuntimeResource::TemporaryBytes)]
|
|
{
|
|
let mut v = vm("GET_GLOBAL 0\nGET_GLOBAL 1\nARRAY_GET 13\nHALT", RuntimeLimits::default());
|
|
let registry = v.validated_image.as_ref().unwrap().registry().clone();
|
|
let r = v
|
|
.heap
|
|
.allocate_typed_array(registry.clone(), prometeu_bytecode::registry::TypeId(13), 1)
|
|
.unwrap();
|
|
v.globals
|
|
.extend_from_slice(&[
|
|
Value::HeapRef(r),
|
|
Value::Int64(if lowered.is_none() { 1 } else { 0 }),
|
|
])
|
|
.unwrap();
|
|
for _ in 0..2 {
|
|
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
|
}
|
|
if let Some(q) = lowered {
|
|
v.budget.test_lower_limit(q, v.budget.used(q));
|
|
}
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
let before = v.cycles;
|
|
trap(
|
|
v.step(&mut Native, &mut HostContext::new(None)),
|
|
if lowered.is_none() { TRAP_OOB } else { TRAP_RESOURCE_LIMIT },
|
|
);
|
|
assert_eq!(v.cycles - before, 6);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
assert_eq!(
|
|
v.heap.load_typed(®istry, r, 0).unwrap().as_slice(),
|
|
&[Value::Int64(0), Value::Int64(0), Value::Int64(0)]
|
|
);
|
|
}
|
|
}
|
|
#[test]
|
|
fn incomplete_set_group_does_not_consume_a_frame_local_and_still_charges_static_cost() {
|
|
let code = assemble(
|
|
"GET_GLOBAL 0\nGET_GLOBAL 1\nGET_GLOBAL 2\nGET_GLOBAL 3\nGET_GLOBAL 4\nARRAY_SET 13\nHALT",
|
|
)
|
|
.unwrap();
|
|
let mut declarations = support::declarations(code.len());
|
|
declarations.functions[0].local_slots = 1;
|
|
let image = crate::ValidatedProgramImage::load(
|
|
&support::bytes_with(&code, declarations),
|
|
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
|
|
NONE,
|
|
)
|
|
.unwrap();
|
|
let mut v = VirtualMachine::from_image(image, RuntimeLimits::default());
|
|
v.prepare_boot_call().unwrap();
|
|
v.operand_stack[0] = Value::Int64(123);
|
|
v.pc = 30;
|
|
v.operand_stack
|
|
.extend_from_slice(&[Value::Null, Value::Int64(0), Value::Int64(1), Value::Int64(2)])
|
|
.unwrap();
|
|
let before = v.cycles;
|
|
let sources = v.operand_stack.as_slice().to_vec();
|
|
trap(v.step(&mut Native, &mut HostContext::new(None)), TRAP_STACK_UNDERFLOW);
|
|
assert_eq!(v.cycles - before, 7);
|
|
assert_eq!(v.operand_stack.as_slice(), sources);
|
|
assert_eq!(v.operand_stack[0], Value::Int64(123));
|
|
}
|