Add generic logical guards and atomic storage publication
This commit is contained in:
@@ -336,6 +336,22 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
||||
emit_u32(parse_u32_any(ops)?, &mut out);
|
||||
}
|
||||
|
||||
"INDEX_GUARD" => {
|
||||
if !ops.is_empty() {
|
||||
return Err(AsmError::InvalidOperand(line.into()));
|
||||
}
|
||||
emit_u16(CoreOpCode::IndexGuard as u16, &mut out);
|
||||
}
|
||||
"STORAGE_PUBLISH" => {
|
||||
let parts: Vec<_> = ops.split(',').map(str::trim).collect();
|
||||
if parts.len() != 4 {
|
||||
return Err(AsmError::InvalidOperand(line.into()));
|
||||
}
|
||||
emit_u16(CoreOpCode::StoragePublish as u16, &mut out);
|
||||
for part in parts {
|
||||
emit_u32(parse_u32_any(part)?, &mut out);
|
||||
}
|
||||
}
|
||||
"STRUCT_GET" | "STRUCT_SET" => {
|
||||
let (a, b) = parse_keyvals(ops)?;
|
||||
let (ty, field) = parse_pair(a, "type", b, "field")?;
|
||||
@@ -345,7 +361,8 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
||||
emit_u32(ty, &mut out);
|
||||
emit_u32(field, &mut out);
|
||||
}
|
||||
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" => {
|
||||
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET"
|
||||
| "ARRAY_CHECK_LENGTH" | "ARRAY_COPY" => {
|
||||
if ops.is_empty() {
|
||||
return Err(AsmError::MissingOperand(line.into()));
|
||||
}
|
||||
@@ -354,6 +371,8 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
||||
"ARRAY_NEW" => CoreOpCode::ArrayNew,
|
||||
"ARRAY_LEN" => CoreOpCode::ArrayLen,
|
||||
"ARRAY_GET" => CoreOpCode::ArrayGet,
|
||||
"ARRAY_CHECK_LENGTH" => CoreOpCode::ArrayCheckLength,
|
||||
"ARRAY_COPY" => CoreOpCode::ArrayCopy,
|
||||
_ => CoreOpCode::ArraySet,
|
||||
};
|
||||
emit_u16(op as u16, &mut out);
|
||||
|
||||
@@ -76,6 +76,14 @@ impl<'a> DecodedInstr<'a> {
|
||||
Ok(f64::from_le_bytes(self.imm.try_into().unwrap()))
|
||||
}
|
||||
|
||||
/// Four independent u32 operands; widths are resolved through the Registry.
|
||||
pub fn imm_u32x4(&self) -> Result<[u32; 4], DecodeError> {
|
||||
self.ensure_len(16)?;
|
||||
Ok(std::array::from_fn(|i| {
|
||||
u32::from_le_bytes(self.imm[i * 4..i * 4 + 4].try_into().unwrap())
|
||||
}))
|
||||
}
|
||||
|
||||
/// Helper for opcodes carrying two u32 values packed in 8 bytes (e.g., ALLOC meta).
|
||||
#[inline]
|
||||
pub fn imm_u32x2(&self) -> Result<(u32, u32), DecodeError> {
|
||||
|
||||
@@ -70,10 +70,18 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
|
||||
| CoreOpCode::ArrayNew
|
||||
| CoreOpCode::ArrayLen
|
||||
| CoreOpCode::ArrayGet
|
||||
| CoreOpCode::ArraySet => {
|
||||
| CoreOpCode::ArraySet
|
||||
| CoreOpCode::ArrayCheckLength
|
||||
| CoreOpCode::ArrayCopy => {
|
||||
let v = u32::from_le_bytes(imm.try_into().unwrap());
|
||||
format!("{}", v)
|
||||
}
|
||||
CoreOpCode::StoragePublish => {
|
||||
let fields: [u32; 4] = std::array::from_fn(|i| {
|
||||
u32::from_le_bytes(imm[i * 4..i * 4 + 4].try_into().unwrap())
|
||||
});
|
||||
format!("{}, {}, {}, {}", fields[0], fields[1], fields[2], fields[3])
|
||||
}
|
||||
CoreOpCode::StructGet | CoreOpCode::StructSet => {
|
||||
let ty = u32::from_le_bytes(imm[..4].try_into().unwrap());
|
||||
let field = u32::from_le_bytes(imm[4..].try_into().unwrap());
|
||||
@@ -144,3 +152,21 @@ pub fn disassemble(bytes: &[u8]) -> Result<String, DecodeError> {
|
||||
}
|
||||
Ok(out.join("\n"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod storage_instruction_tests {
|
||||
#[test]
|
||||
fn storage_operands_roundtrip_and_quad_immediates_never_decode_truncated() {
|
||||
let code = crate::assemble(
|
||||
"INDEX_GUARD\nARRAY_CHECK_LENGTH 7\nARRAY_COPY 8\nSTORAGE_PUBLISH 8, 9, 0, 1",
|
||||
)
|
||||
.unwrap();
|
||||
let text = super::disassemble(&code).unwrap();
|
||||
assert_eq!(crate::assemble(&text).unwrap(), code);
|
||||
let pc = 14;
|
||||
for end in pc + 2..code.len() {
|
||||
assert!(crate::decode_next(pc, &code[..end]).is_err());
|
||||
}
|
||||
assert_eq!(crate::decode_next(pc, &code).unwrap().imm_u32x4().unwrap(), [8, 9, 0, 1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,6 +201,15 @@ pub enum OpCode {
|
||||
ArrayLen = 0xA4,
|
||||
ArrayGet = 0xA5,
|
||||
ArraySet = 0xA6,
|
||||
/// Signed logical index guard; stack [index:i64, exclusive_limit:i64] -> [].
|
||||
IndexGuard = 0xA7,
|
||||
/// Structural Array length check without allocation; TypeId:u32.
|
||||
ArrayCheckLength = 0xA8,
|
||||
/// Complete stored groups, overlap-safe; Array TypeId:u32.
|
||||
ArrayCopy = 0xA9,
|
||||
/// Move/fill array ranges and two selected Struct fields in one attempt.
|
||||
/// Array TypeId, Struct TypeId, first field, second field (four u32).
|
||||
StoragePublish = 0xAA,
|
||||
}
|
||||
|
||||
impl TryFrom<u16> for OpCode {
|
||||
@@ -274,6 +283,10 @@ impl TryFrom<u16> for OpCode {
|
||||
0xA4 => Ok(OpCode::ArrayLen),
|
||||
0xA5 => Ok(OpCode::ArrayGet),
|
||||
0xA6 => Ok(OpCode::ArraySet),
|
||||
0xA7 => Ok(OpCode::IndexGuard),
|
||||
0xA8 => Ok(OpCode::ArrayCheckLength),
|
||||
0xA9 => Ok(OpCode::ArrayCopy),
|
||||
0xAA => Ok(OpCode::StoragePublish),
|
||||
|
||||
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
|
||||
}
|
||||
@@ -351,6 +364,9 @@ impl OpCode {
|
||||
OpCode::ArrayLen => 3,
|
||||
OpCode::ArrayGet => 3,
|
||||
OpCode::ArraySet => 4,
|
||||
OpCode::IndexGuard | OpCode::ArrayCheckLength => 2,
|
||||
OpCode::ArrayCopy => 4,
|
||||
OpCode::StoragePublish => 6,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -666,6 +666,30 @@ impl OpCodeSpecExt for OpCode {
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::IndexGuard => OpcodeSpec {
|
||||
name: "INDEX_GUARD",
|
||||
imm_bytes: 0,
|
||||
pops: Some(2),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::ArrayCheckLength | OpCode::ArrayCopy | OpCode::StoragePublish => OpcodeSpec {
|
||||
name: match self {
|
||||
OpCode::ArrayCheckLength => "ARRAY_CHECK_LENGTH",
|
||||
OpCode::ArrayCopy => "ARRAY_COPY",
|
||||
_ => "STORAGE_PUBLISH",
|
||||
},
|
||||
imm_bytes: if *self == OpCode::StoragePublish { 16 } else { 4 },
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::FrameSync => OpcodeSpec {
|
||||
name: "FRAME_SYNC",
|
||||
imm_bytes: 0,
|
||||
|
||||
@@ -133,6 +133,38 @@ impl DecodedInstr<'_> {
|
||||
};
|
||||
(source_layout.storage.slots, pushes)
|
||||
}
|
||||
Op::ArrayCheckLength | Op::ArrayCopy | Op::StoragePublish => {
|
||||
let (array, owner, first, second) = if self.opcode == Op::StoragePublish {
|
||||
let [a, o, f, s] = self.imm_u32x4().map_err(|_| malformed())?;
|
||||
(a, o, f, s)
|
||||
} else {
|
||||
(self.imm_u32().map_err(|_| malformed())?, 0, 0, 0)
|
||||
};
|
||||
let (_, layout) = type_info(array)?;
|
||||
let element = layout.array.ok_or_else(|| bad_category(array))?.element_type_id;
|
||||
let width = registry.type_layout(element).unwrap().storage.slots;
|
||||
match self.opcode {
|
||||
Op::ArrayCheckLength => (1, 0),
|
||||
Op::ArrayCopy => (5, 0),
|
||||
_ => {
|
||||
let (_, layout) = type_info(owner)?;
|
||||
let instance =
|
||||
layout.instance.as_ref().ok_or_else(|| bad_category(owner))?;
|
||||
if first == second {
|
||||
return Err(bad_category(owner).member(second));
|
||||
}
|
||||
let mut slots = 8 + width;
|
||||
for field in [first, second] {
|
||||
let f = instance
|
||||
.fields
|
||||
.get(field as usize)
|
||||
.ok_or_else(|| bad_category(owner).member(field))?;
|
||||
slots += registry.type_layout(f.type_id).unwrap().storage.slots;
|
||||
}
|
||||
(slots, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
Op::StructNew
|
||||
| Op::StructGet
|
||||
| Op::StructSet
|
||||
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
@@ -161,3 +161,61 @@ Typed callable instructions are MAKE_CLOSURE `(FunctionId,FnTypeId)`, CALL_CLOSU
|
||||
## Random core integration
|
||||
|
||||
INTRINSIC also dispatches the [VM-owned random operations](02e-vm-owned-random-core.md), IDs 0x4000–0x4003. NEW cannot construct Random. TRAP_RANDOM_REJECTION_LIMIT is 0x00000015.
|
||||
|
||||
|
||||
## Logical guards and atomic storage publication
|
||||
|
||||
The core includes the following generic operations, with u16 opcodes and
|
||||
little-endian u32 immediates. They introduce no new Registry category, syscall,
|
||||
source-language collection or ambient transaction. Operand order below is bottom
|
||||
to top; all integer operands are Int64.
|
||||
|
||||
| Opcode | Immediate | Stack operands consumed |
|
||||
| --- | --- | --- |
|
||||
| INDEX_GUARD `0xA7` | none | index, exclusive limit |
|
||||
| ARRAY_CHECK_LENGTH `0xA8` | Array TypeId | length |
|
||||
| ARRAY_COPY `0xA9` | Array TypeId | source, sourceStart, target, targetStart, count |
|
||||
| STORAGE_PUBLISH `0xAA` | Array TypeId, Struct TypeId, first field, second field | source, sourceStart, target, targetStart, count, fillStart, fillCount, element group, owner, first field group, second field group |
|
||||
|
||||
All operations return no values. INDEX_GUARD accepts exactly
|
||||
`0 <= index < limit`, otherwise TRAP_OOB. It does not use physical storage length.
|
||||
ARRAY_CHECK_LENGTH accepts zero through u32::MAX without allocating. Negative
|
||||
lengths produce TRAP_OOB; values above u32::MAX produce the same resource
|
||||
SizeOverflow failure as ARRAY_NEW. This check does not reserve future allocation.
|
||||
|
||||
ARRAY_COPY and STORAGE_PUBLISH require exact Array receiver identities and complete
|
||||
stored groups. Every range must have nonnegative start/count and fit its physical
|
||||
array; an empty range may start at length. Invalid ranges produce TRAP_OOB, never
|
||||
clamping or truncation. Copy reads the pre-attempt source sequence, including for
|
||||
overlapping source/target ranges. Presence tags, immutable boxes, native owners and
|
||||
object identities are preserved without payload unpacking or repacking.
|
||||
|
||||
STORAGE_PUBLISH first copies the selected source range, then fills the selected
|
||||
target range with the supplied element group, then writes the two selected fields
|
||||
on the supplied Struct owner. Fields must be distinct valid indices on that Struct;
|
||||
their types and widths come from the immutable Registry. Source code/producer owns
|
||||
selection of every range, value, owner and field. The VM implements these generic
|
||||
ordered effects and does not select collection algorithms.
|
||||
|
||||
Loading validates all immediate identities/categories/fields even in unreachable
|
||||
code. Verification requires complete typed operands. ARRAY_COPY consumes five
|
||||
slots; STORAGE_PUBLISH consumes `8 + elementWidth + firstWidth + secondWidth`.
|
||||
INDEX_GUARD consumes two slots and ARRAY_CHECK_LENGTH consumes one.
|
||||
|
||||
Execution validates all receivers, lengths/ranges, supplied groups and source
|
||||
groups, and reserves the complete charged copy scratch before writing. Validation,
|
||||
quota or host allocation refusal leaves all input operands and destinations
|
||||
unchanged, including both fields and native owners. Scratch uses TemporaryBytes,
|
||||
is refunded on success/refusal and does not relax any resource ceiling. Existing
|
||||
operands remain roots during preparation. There is no GC, callback, scheduler or
|
||||
other interleaving inside this single instruction. Commit uses already validated
|
||||
complete groups, performs no fallible allocation and consumes operands only after
|
||||
all selected effects have been published. No prepared token escapes the attempt.
|
||||
|
||||
Base cycle costs are 2 for either guard, 4 for ARRAY_COPY and 6 for STORAGE_PUBLISH.
|
||||
Copy adds `2 * count * elementWidth`; publication additionally adds
|
||||
`fillCount * elementWidth + firstWidth + secondWidth`. Work arithmetic is checked
|
||||
before mutation. A refused preparation still charges the attempted work. Neither
|
||||
operation is a safepoint. Consumers must prepare any potentially failing return
|
||||
carrier and all call operands before publication; a series of ordinary stores is
|
||||
not a substitute for whole-operation safety.
|
||||
|
||||
@@ -306,3 +306,12 @@ precedence, conversions, catalog metadata and producer examples.
|
||||
## Random core integration
|
||||
|
||||
[Random](02e-vm-owned-random-core.md) adds BuiltinId 0x0016 and intrinsic IDs 0x4000–0x4003 using existing PBX v0 encoding. Registry categories remain unchanged; unsupported identities are rejected, without fallback.
|
||||
|
||||
|
||||
## Logical guards and atomic storage publication
|
||||
|
||||
INDEX_GUARD (0xA7), ARRAY_CHECK_LENGTH (0xA8), ARRAY_COPY (0xA9) and
|
||||
STORAGE_PUBLISH (0xAA) provide generic logical validation, complete stored-group
|
||||
transfer and prepared atomic publication. Their encoding, typed operands, exact
|
||||
faults, overlap semantics, costs and preflight/commit guarantees are defined by
|
||||
the [canonical instruction contract](02-vm-instruction-set.md#logical-guards-and-atomic-storage-publication). No new Registry category, syscall, collection algorithm or safepoint is introduced.
|
||||
|
||||
@@ -221,3 +221,14 @@ Threshold GC opportunities are FRAME_SYNC and successful RET of the last root, a
|
||||
## Random core integration
|
||||
|
||||
[Random objects](02e-vm-owned-random-core.md) use ObjectKind tag 9 (tag 6 remains reserved), 32 logical payload bytes and quota-accounted headers/handles/auxiliary storage. They are opaque GC leaves; Dynamic holds their direct reference and exact TypeId.
|
||||
|
||||
|
||||
## Prepared storage publication
|
||||
|
||||
Generic complete-group transfer/publication reserves its copy scratch against
|
||||
TemporaryBytes before changing any destination. Source groups preserve Dynamic
|
||||
presence tags and immutable box/native owner/reference identities. Operands remain
|
||||
roots throughout preparation; the commit phase has no allocation, GC, scheduler or
|
||||
callback boundary. Refusal changes neither destinations nor input groups, and
|
||||
releases temporary reservations. Consumer return carriers must be prepared before
|
||||
publication. See [the instruction contract](02c-program-registry-and-pbx-v0.md#logical-guards-and-atomic-storage-publication).
|
||||
|
||||
@@ -189,3 +189,12 @@ STRUCT_NEW (0x00A0), STRUCT_GET (0x00A1), STRUCT_SET (0x00A2), ARRAY_NEW (0x00A3
|
||||
## VM-owned random core
|
||||
|
||||
See the [random contract](../specs/runtime/02e-vm-owned-random-core.md) for Random BuiltinId 0x0016, ObjectKind tag 9, intrinsics 0x4000–0x4003, atomic state mutation, bounded 4+8*N costs and rejection-limit trap 0x00000015. Random is managed by the VM heap and existing GC, without host entropy or PBX changes.
|
||||
|
||||
|
||||
## Logical guards and atomic storage publication
|
||||
|
||||
INDEX_GUARD (0xA7), ARRAY_CHECK_LENGTH (0xA8), ARRAY_COPY (0xA9) and
|
||||
STORAGE_PUBLISH (0xAA) provide generic logical validation, complete stored-group
|
||||
transfer and prepared atomic publication. Their encoding, typed operands, exact
|
||||
faults, overlap semantics, costs and preflight/commit guarantees are defined by
|
||||
the [canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#logical-guards-and-atomic-storage-publication). No new Registry category, syscall, collection algorithm or safepoint is introduced.
|
||||
|
||||
Reference in New Issue
Block a user