Add generic logical guards and atomic storage publication
This commit is contained in:
@@ -428,6 +428,34 @@ impl Heap {
|
||||
self.allocation_count.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Host-side observations only, outside guest execution and atomic commits.
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub fn diagnostic_typed_storage(&self) -> Vec<crate::resource_diagnostics::StorageObject> {
|
||||
use crate::resource_diagnostics::{StorageObject, StorageValue};
|
||||
self.objects
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, object)| {
|
||||
let storage = object.as_ref()?.typed.as_ref()?;
|
||||
let groups = (0..storage.len())
|
||||
.map(|i| {
|
||||
storage
|
||||
.read(i)
|
||||
.expect("initialized typed storage")
|
||||
.as_slice()
|
||||
.iter()
|
||||
.map(StorageValue::observe)
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
Some(StorageObject {
|
||||
reference: HeapRef { index: index as u32, generation: self.generations[index] },
|
||||
type_id: storage.type_id(),
|
||||
groups,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub(crate) fn diagnostic_handles(&self) -> (usize, usize) {
|
||||
(self.free_list.len(), self.objects.len() - self.free_list.len() - self.len())
|
||||
@@ -520,6 +548,18 @@ impl Heap {
|
||||
value_contract::validate(registry, self, storage.slot_type(index)?, values.as_slice())?;
|
||||
Ok(values)
|
||||
}
|
||||
/// Caller has checked all receivers, bounds and groups for the entire attempt.
|
||||
/// This commit path performs no allocation, provenance lookup or guest callback.
|
||||
pub(crate) fn commit_typed(&mut self, r: HeapRef, index: usize, values: &[Value]) {
|
||||
self.objects[r.index as usize]
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.typed
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.write_validated(index, values)
|
||||
.expect("prevalidated storage publication");
|
||||
}
|
||||
pub(crate) fn store_typed(
|
||||
&mut self,
|
||||
registry: &Registry,
|
||||
|
||||
@@ -704,7 +704,44 @@ fn check(
|
||||
base += registry.type_layout(parameter.type_id).unwrap().storage.slots as usize;
|
||||
}
|
||||
}
|
||||
Op::ArrayNew => require(working[end - 1], I64, 0, registry, budget)?,
|
||||
Op::ArrayNew | Op::ArrayCheckLength => require(working[end - 1], I64, 0, registry, budget)?,
|
||||
Op::IndexGuard => {
|
||||
require(working[end - 2], I64, 0, registry, budget)?;
|
||||
require(working[end - 1], I64, 1, registry, budget)?;
|
||||
}
|
||||
Op::ArrayCopy | Op::StoragePublish => {
|
||||
let [a, o, f, s] = if instruction.opcode == Op::StoragePublish {
|
||||
instruction.imm_u32x4().unwrap()
|
||||
} else {
|
||||
[instruction.imm_u32().unwrap(), 0, 0, 0]
|
||||
};
|
||||
let effect = instruction.registry_stack_effect_in(registry, FunctionId(id))?.unwrap();
|
||||
let base = end - effect.pops as usize;
|
||||
check_group(&working[base..base + 1], TypeId(a), registry, budget)?;
|
||||
check_group(&working[base + 2..base + 3], TypeId(a), registry, budget)?;
|
||||
for i in [1, 3, 4] {
|
||||
require(working[base + i], I64, i, registry, budget)?;
|
||||
}
|
||||
if instruction.opcode == Op::StoragePublish {
|
||||
for i in [5, 6] {
|
||||
require(working[base + i], I64, i, registry, budget)?;
|
||||
}
|
||||
let element =
|
||||
registry.type_layout(TypeId(a)).unwrap().array.unwrap().element_type_id;
|
||||
let mut at = base + 7;
|
||||
check_group(&working[at..], element, registry, budget)?;
|
||||
at += registry.type_layout(element).unwrap().storage.slots as usize;
|
||||
check_group(&working[at..], TypeId(o), registry, budget)?;
|
||||
at += 1;
|
||||
let fields =
|
||||
®istry.type_layout(TypeId(o)).unwrap().instance.as_ref().unwrap().fields;
|
||||
for field in [f, s] {
|
||||
let ty = fields[field as usize].type_id;
|
||||
check_group(&working[at..], ty, registry, budget)?;
|
||||
at += registry.type_layout(ty).unwrap().storage.slots as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::StructGet | Op::StructSet | Op::ArrayLen | Op::ArrayGet | Op::ArraySet => {
|
||||
budget.work(3)?;
|
||||
let (id, field) = if matches!(instruction.opcode, Op::StructGet | Op::StructSet) {
|
||||
|
||||
@@ -87,3 +87,39 @@ mod tests {
|
||||
assert_eq!(h.max_ns, 99);
|
||||
}
|
||||
}
|
||||
|
||||
/// Non-owning storage observations for engineering qualification. These values
|
||||
/// never retain a guest object or a native string owner and cannot write storage.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum StorageValue {
|
||||
Null,
|
||||
Int32(i32),
|
||||
Int64(i64),
|
||||
FloatBits(u64),
|
||||
Boolean(bool),
|
||||
Reference(prometeu_bytecode::HeapRef),
|
||||
StringIdentity { address: usize, bytes: usize },
|
||||
}
|
||||
impl StorageValue {
|
||||
pub(crate) fn observe(value: &prometeu_bytecode::Value) -> Self {
|
||||
use prometeu_bytecode::Value;
|
||||
match value {
|
||||
Value::Null => Self::Null,
|
||||
Value::Int32(n) => Self::Int32(*n),
|
||||
Value::Int64(n) => Self::Int64(*n),
|
||||
Value::Float(n) => Self::FloatBits(n.to_bits()),
|
||||
Value::Boolean(n) => Self::Boolean(*n),
|
||||
Value::HeapRef(r) => Self::Reference(*r),
|
||||
Value::String(s) => Self::StringIdentity {
|
||||
address: std::sync::Arc::as_ptr(s) as *const () as usize,
|
||||
bytes: s.len(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StorageObject {
|
||||
pub reference: prometeu_bytecode::HeapRef,
|
||||
pub type_id: prometeu_bytecode::registry::TypeId,
|
||||
pub groups: Vec<Vec<StorageValue>>,
|
||||
}
|
||||
|
||||
@@ -14,6 +14,9 @@ mod random;
|
||||
mod reference;
|
||||
mod runtime;
|
||||
mod stack;
|
||||
mod storage_publication;
|
||||
#[cfg(test)]
|
||||
mod storage_publication_tests;
|
||||
mod strings;
|
||||
mod typed;
|
||||
#[cfg(test)]
|
||||
@@ -1083,6 +1086,17 @@ impl VirtualMachine {
|
||||
let (source, target) = instr.imm_u32x2().unwrap();
|
||||
return self.execute_reference(opcode, source, target, start_pc as u32);
|
||||
}
|
||||
OpCode::IndexGuard
|
||||
| OpCode::ArrayCheckLength
|
||||
| OpCode::ArrayCopy
|
||||
| OpCode::StoragePublish => {
|
||||
let operands = match opcode {
|
||||
OpCode::StoragePublish => instr.imm_u32x4().unwrap(),
|
||||
OpCode::IndexGuard => [0; 4],
|
||||
_ => [instr.imm_u32().unwrap(), 0, 0, 0],
|
||||
};
|
||||
return self.execute_storage_publication(opcode, operands, start_pc as u32);
|
||||
}
|
||||
OpCode::StructNew
|
||||
| OpCode::StructGet
|
||||
| OpCode::StructSet
|
||||
|
||||
@@ -175,7 +175,7 @@ impl VirtualMachine {
|
||||
.expect("result capacity reserved before publication");
|
||||
Ok(())
|
||||
}
|
||||
fn collection_size_trap(
|
||||
pub(super) fn collection_size_trap(
|
||||
&self,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
//! Generic complete-group range operations. All admission precedes shared writes.
|
||||
use super::*;
|
||||
use crate::value_contract::{self, ValueError, ValueGroup};
|
||||
use prometeu_bytecode::registry::TypeId;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn execute_storage_publication(
|
||||
&mut self,
|
||||
opcode: OpCode,
|
||||
operands: [u32; 4],
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
self.cycles += opcode.cycles();
|
||||
if opcode == OpCode::IndexGuard {
|
||||
if self.temporary_slots() < 2 {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
"Incomplete guard operands".into(),
|
||||
pc,
|
||||
));
|
||||
}
|
||||
let base = self.operand_stack.len() - 2;
|
||||
match (&self.operand_stack[base], &self.operand_stack[base + 1]) {
|
||||
(Value::Int64(index), Value::Int64(limit)) if *index >= 0 && index < limit => {}
|
||||
(Value::Int64(_), Value::Int64(_)) => {
|
||||
return Err(self.value_trap(ValueError::Bounds, opcode, pc));
|
||||
}
|
||||
_ => return Err(self.value_trap(ValueError::Type, opcode, pc)),
|
||||
}
|
||||
self.operand_stack.truncate(base);
|
||||
return Ok(());
|
||||
}
|
||||
let image = self.validated_image.clone().ok_or_else(|| {
|
||||
self.trap(
|
||||
prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION,
|
||||
opcode as u16,
|
||||
"Storage operation requires a validated Registry".into(),
|
||||
pc,
|
||||
)
|
||||
})?;
|
||||
let registry = image.registry();
|
||||
let [array_id, owner_id, first, second] = operands;
|
||||
let array_ty = TypeId(array_id);
|
||||
let element = registry.type_layout(array_ty).unwrap().array.unwrap().element_type_id;
|
||||
let width = registry.type_layout(element).unwrap().storage.slots as usize;
|
||||
let fields = if opcode == OpCode::StoragePublish {
|
||||
Some(®istry.type_layout(TypeId(owner_id)).unwrap().instance.as_ref().unwrap().fields)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let field_width = |field: u32| {
|
||||
registry.type_layout(fields.unwrap()[field as usize].type_id).unwrap().storage.slots
|
||||
as usize
|
||||
};
|
||||
let consumed = match opcode {
|
||||
OpCode::ArrayCheckLength => 1,
|
||||
OpCode::ArrayCopy => 5,
|
||||
_ => 8 + width + field_width(first) + field_width(second),
|
||||
};
|
||||
if self.temporary_slots() < consumed {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
"Incomplete storage operands".into(),
|
||||
pc,
|
||||
));
|
||||
}
|
||||
let base = self.operand_stack.len() - consumed;
|
||||
let integer = |offset: usize| match self.operand_stack[base + offset] {
|
||||
Value::Int64(n) => Ok(n),
|
||||
_ => Err(self.value_trap(ValueError::Type, opcode, pc)),
|
||||
};
|
||||
if opcode == OpCode::ArrayCheckLength {
|
||||
let n = integer(0)?;
|
||||
if n < 0 {
|
||||
return Err(self.value_trap(ValueError::Bounds, opcode, pc));
|
||||
}
|
||||
if u32::try_from(n).is_err() {
|
||||
return Err(self.collection_size_trap(opcode, pc, n as u64));
|
||||
}
|
||||
self.operand_stack.truncate(base);
|
||||
return Ok(());
|
||||
}
|
||||
let receiver = |offset: usize, ty: TypeId| {
|
||||
let r = match self.operand_stack[base + offset] {
|
||||
Value::HeapRef(r) => r,
|
||||
Value::Null => return Err(self.value_trap(ValueError::NullReference, opcode, pc)),
|
||||
_ => return Err(self.value_trap(ValueError::Type, opcode, pc)),
|
||||
};
|
||||
if self.heap.concrete_type(r, registry).map_err(|e| self.value_trap(e, opcode, pc))?
|
||||
!= ty
|
||||
{
|
||||
return Err(self.value_trap(ValueError::Type, opcode, pc));
|
||||
}
|
||||
Ok(r)
|
||||
};
|
||||
let source = receiver(0, array_ty)?;
|
||||
let target = receiver(2, array_ty)?;
|
||||
let source_start = integer(1)?;
|
||||
let target_start = integer(3)?;
|
||||
let count = integer(4)?;
|
||||
let range = |r, start: i64, count: i64| {
|
||||
let len = self
|
||||
.heap
|
||||
.typed_array_len(r, registry)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
if start < 0 || count < 0 || start > len as i64 || count > len as i64 - start {
|
||||
return Err(self.value_trap(ValueError::Bounds, opcode, pc));
|
||||
}
|
||||
Ok((start as usize, count as usize))
|
||||
};
|
||||
let (source_start, count) = range(source, source_start, count)?;
|
||||
let (target_start, _) = range(target, target_start, count as i64)?;
|
||||
let (fill_start, fill_count, owner) = if fields.is_some() {
|
||||
let (start, count) = range(target, integer(5)?, integer(6)?)?;
|
||||
let owner = receiver(7 + width, TypeId(owner_id))?;
|
||||
value_contract::validate(
|
||||
registry,
|
||||
&self.heap,
|
||||
element,
|
||||
&self.operand_stack[base + 7..base + 7 + width],
|
||||
)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
let mut at = base + 8 + width;
|
||||
for field in [first, second] {
|
||||
let ty = fields.unwrap()[field as usize].type_id;
|
||||
let w = field_width(field);
|
||||
value_contract::validate(registry, &self.heap, ty, &self.operand_stack[at..at + w])
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
at += w;
|
||||
}
|
||||
(start, count, Some(owner))
|
||||
} else {
|
||||
(0, 0, None)
|
||||
};
|
||||
let work = (count as u64)
|
||||
.checked_mul(2)
|
||||
.and_then(|n| n.checked_add(fill_count as u64))
|
||||
.and_then(|n| n.checked_mul(width as u64))
|
||||
.and_then(|n| {
|
||||
n.checked_add(if fields.is_some() {
|
||||
(field_width(first) + field_width(second)) as u64
|
||||
} else {
|
||||
0
|
||||
})
|
||||
})
|
||||
.ok_or_else(|| self.collection_size_trap(opcode, pc, u64::MAX))?;
|
||||
self.cycles += work;
|
||||
// This reservation and all reads/validation can fail. No destination has
|
||||
// changed yet. The original operands remain roots throughout preparation.
|
||||
let mut prepared =
|
||||
BudgetVec::<ValueGroup>::new(&self.budget, RuntimeResource::TemporaryBytes);
|
||||
prepared.reserve(count).map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
for i in 0..count {
|
||||
let group = self
|
||||
.heap
|
||||
.load_typed(registry, source, source_start + i)
|
||||
.map_err(|e| self.value_trap(e, opcode, pc))?;
|
||||
prepared.push(group).expect("complete scratch reservation");
|
||||
}
|
||||
// No allocation, GC, callback, scheduler or fallible operation follows.
|
||||
// Capturing every source group first gives memmove semantics for overlap.
|
||||
for (i, group) in prepared.iter().enumerate() {
|
||||
self.heap.commit_typed(target, target_start + i, group.as_slice());
|
||||
}
|
||||
if let Some(owner) = owner {
|
||||
let fill = &self.operand_stack[base + 7..base + 7 + width];
|
||||
for i in 0..fill_count {
|
||||
self.heap.commit_typed(target, fill_start + i, fill);
|
||||
}
|
||||
let mut at = base + 8 + width;
|
||||
for field in [first, second] {
|
||||
let w = field_width(field);
|
||||
self.heap.commit_typed(owner, field as usize, &self.operand_stack[at..at + w]);
|
||||
at += w;
|
||||
}
|
||||
}
|
||||
self.operand_stack.truncate(base);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
use super::*;
|
||||
#[path = "../../tests/support/collections.rs"]
|
||||
mod support;
|
||||
use prometeu_bytecode::{assemble, registry::TypeId};
|
||||
use support::Native;
|
||||
|
||||
fn machine(instruction: &str, count: usize) -> VirtualMachine {
|
||||
let mut text = String::new();
|
||||
for i in 0..count {
|
||||
text += &format!("GET_GLOBAL {i}\n");
|
||||
}
|
||||
text += instruction;
|
||||
text += "\nHALT";
|
||||
let mut vm = VirtualMachine::from_image(
|
||||
support::load(&assemble(&text).unwrap()).unwrap(),
|
||||
RuntimeLimits::default(),
|
||||
);
|
||||
vm.prepare_boot_call().unwrap();
|
||||
vm
|
||||
}
|
||||
fn operands(vm: &mut VirtualMachine, values: &[Value]) {
|
||||
vm.globals.extend_from_slice(values).unwrap();
|
||||
for _ in values {
|
||||
vm.step(&mut Native, &mut HostContext::new(None)).unwrap();
|
||||
}
|
||||
}
|
||||
fn step(vm: &mut VirtualMachine) -> Result<(), LogicalFrameEndingReason> {
|
||||
vm.step(&mut Native, &mut HostContext::new(None))
|
||||
}
|
||||
fn fault(result: Result<(), LogicalFrameEndingReason>, code: u32) {
|
||||
let LogicalFrameEndingReason::Trap(t) = result.unwrap_err() else { panic!() };
|
||||
assert_eq!(t.code, code);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logical_guard_and_length_validation_preserve_exact_faults_and_inputs() {
|
||||
for (index, limit, valid) in
|
||||
[(0, 1, true), (1, 1, false), (-1, 3, false), (0, 0, false), (i64::MAX - 1, i64::MAX, true)]
|
||||
{
|
||||
let mut vm = machine("INDEX_GUARD", 2);
|
||||
let values = [Value::Int64(index), Value::Int64(limit)];
|
||||
operands(&mut vm, &values);
|
||||
if valid {
|
||||
step(&mut vm).unwrap();
|
||||
assert!(vm.operand_stack.is_empty());
|
||||
} else {
|
||||
fault(step(&mut vm), TRAP_OOB);
|
||||
assert_eq!(vm.operand_stack.as_slice(), values);
|
||||
}
|
||||
}
|
||||
for (n, code) in [
|
||||
(-1, Some(TRAP_OOB)),
|
||||
(0, None),
|
||||
(u32::MAX as i64, None),
|
||||
(u32::MAX as i64 + 1, Some(TRAP_RESOURCE_LIMIT)),
|
||||
] {
|
||||
let mut vm = machine("ARRAY_CHECK_LENGTH 15", 1);
|
||||
operands(&mut vm, &[Value::Int64(n)]);
|
||||
let before = vm.heap.allocation_count();
|
||||
if let Some(code) = code {
|
||||
fault(step(&mut vm), code);
|
||||
assert_eq!(vm.operand_stack.as_slice(), [Value::Int64(n)]);
|
||||
} else {
|
||||
step(&mut vm).unwrap();
|
||||
assert!(vm.operand_stack.is_empty());
|
||||
}
|
||||
assert_eq!(vm.heap.allocation_count(), before);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_publication_preserves_complete_groups_and_publishes_both_fields() {
|
||||
for quota in [None, Some(RuntimeResource::TemporaryBytes)] {
|
||||
let mut vm = machine("STORAGE_PUBLISH 12,21,0,1", 13);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(12), 4).unwrap();
|
||||
let owner = vm.heap.allocate_typed_struct(registry.clone(), TypeId(21)).unwrap();
|
||||
for i in 0..3 {
|
||||
vm.heap
|
||||
.store_typed(
|
||||
®istry,
|
||||
a,
|
||||
i,
|
||||
&[Value::Float(i as f64 + 1.), Value::Float(i as f64 + 10.)],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let before: Vec<_> = (0..4)
|
||||
.map(|i| vm.heap.load_typed(®istry, a, i).unwrap().as_slice().to_vec())
|
||||
.collect();
|
||||
let inputs = [
|
||||
Value::HeapRef(a),
|
||||
Value::Int64(0),
|
||||
Value::HeapRef(a),
|
||||
Value::Int64(1),
|
||||
Value::Int64(3),
|
||||
Value::Int64(0),
|
||||
Value::Int64(1),
|
||||
Value::Float(9.),
|
||||
Value::Float(8.),
|
||||
Value::HeapRef(owner),
|
||||
Value::Int32(7),
|
||||
Value::Float(6.),
|
||||
Value::Float(5.),
|
||||
];
|
||||
operands(&mut vm, &inputs);
|
||||
let old_usage = vm.budget.used(RuntimeResource::TemporaryBytes);
|
||||
if let Some(q) = quota {
|
||||
vm.budget.test_lower_limit(q, vm.budget.used(q));
|
||||
}
|
||||
if quota.is_some() {
|
||||
fault(step(&mut vm), TRAP_RESOURCE_LIMIT);
|
||||
assert_eq!(vm.operand_stack.as_slice(), inputs);
|
||||
for (i, group) in before.iter().enumerate() {
|
||||
assert_eq!(vm.heap.load_typed(®istry, a, i).unwrap().as_slice(), group);
|
||||
}
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(),
|
||||
[Value::Int32(0)]
|
||||
);
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, owner, 1).unwrap().as_slice(),
|
||||
[Value::Float(0.), Value::Float(0.)]
|
||||
);
|
||||
} else {
|
||||
step(&mut vm).unwrap();
|
||||
assert!(vm.operand_stack.is_empty());
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, a, 0).unwrap().as_slice(),
|
||||
[Value::Float(9.), Value::Float(8.)]
|
||||
);
|
||||
for i in 1..4 {
|
||||
assert_eq!(vm.heap.load_typed(®istry, a, i).unwrap().as_slice(), before[i - 1]);
|
||||
}
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(),
|
||||
[Value::Int32(7)]
|
||||
);
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, owner, 1).unwrap().as_slice(),
|
||||
[Value::Float(6.), Value::Float(5.)]
|
||||
);
|
||||
}
|
||||
assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), old_usage);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn late_invalid_field_or_range_prevents_every_write() {
|
||||
for bad_range in [false, true] {
|
||||
let mut vm = machine("STORAGE_PUBLISH 11,9,0,1", 11);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(11), 2).unwrap();
|
||||
let owner = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap();
|
||||
vm.heap.store_typed(®istry, a, 0, &[Value::Int32(3)]).unwrap();
|
||||
let inputs = [
|
||||
Value::HeapRef(a),
|
||||
Value::Int64(0),
|
||||
Value::HeapRef(a),
|
||||
Value::Int64(1),
|
||||
Value::Int64(1),
|
||||
Value::Int64(if bad_range { 2 } else { 0 }),
|
||||
Value::Int64(1),
|
||||
Value::Int32(8),
|
||||
Value::HeapRef(owner),
|
||||
Value::Int32(4),
|
||||
if bad_range { Value::Int64(7) } else { Value::Boolean(true) },
|
||||
];
|
||||
operands(&mut vm, &inputs);
|
||||
fault(step(&mut vm), if bad_range { TRAP_OOB } else { TRAP_TYPE });
|
||||
assert_eq!(vm.operand_stack.as_slice(), inputs);
|
||||
assert_eq!(vm.heap.load_typed(®istry, a, 0).unwrap().as_slice(), [Value::Int32(3)]);
|
||||
assert_eq!(vm.heap.load_typed(®istry, a, 1).unwrap().as_slice(), [Value::Int32(0)]);
|
||||
assert_eq!(vm.heap.load_typed(®istry, owner, 0).unwrap().as_slice(), [Value::Int32(0)]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_copy_preserves_absence_and_box_identity_without_heap_allocations() {
|
||||
let mut vm = machine("ARRAY_COPY 15", 5);
|
||||
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
|
||||
let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(15), 4).unwrap();
|
||||
let boxed = vm
|
||||
.heap
|
||||
.allocate_dynamic_box(registry.clone(), TypeId(5), &[Value::Float(1.), Value::Float(2.)])
|
||||
.unwrap();
|
||||
let present = [Value::Int64(5), Value::HeapRef(boxed)];
|
||||
vm.heap.store_typed(®istry, a, 0, &present).unwrap();
|
||||
operands(
|
||||
&mut vm,
|
||||
&[Value::HeapRef(a), Value::Int64(0), Value::HeapRef(a), Value::Int64(1), Value::Int64(3)],
|
||||
);
|
||||
let before = vm.heap.allocation_count();
|
||||
vm.budget
|
||||
.test_lower_limit(RuntimeResource::GuestBytes, vm.budget.used(RuntimeResource::GuestBytes));
|
||||
step(&mut vm).unwrap();
|
||||
assert_eq!(vm.heap.allocation_count(), before);
|
||||
assert_eq!(vm.heap.load_typed(®istry, a, 1).unwrap().as_slice(), present);
|
||||
for i in [2, 3] {
|
||||
assert_eq!(
|
||||
vm.heap.load_typed(®istry, a, i).unwrap().as_slice(),
|
||||
[Value::Null, Value::Null]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dead_invalid_operands_and_split_typed_groups_are_rejected() {
|
||||
for instruction in [
|
||||
"ARRAY_COPY 9",
|
||||
"ARRAY_CHECK_LENGTH 99",
|
||||
"STORAGE_PUBLISH 11,9,0,0",
|
||||
"STORAGE_PUBLISH 11,9,0,99",
|
||||
"STORAGE_PUBLISH 11,11,0,1",
|
||||
] {
|
||||
assert!(
|
||||
support::load(&assemble(&format!("HALT\n{instruction}")).unwrap()).is_err(),
|
||||
"{instruction}"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
support::load(&assemble("PUSH_BOOL 1\nPUSH_I64 3\nINDEX_GUARD\nHALT").unwrap()).is_err()
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user