feat(vm): publish typed fields across multiple structs atomically

Add schema-driven FIELDS_PUBLISH without collection-specific runtime behavior. Validate every nullable destination and complete field group before allocation-free publication, with neutral ownership, malformed-input and quota coverage.
This commit is contained in:
Nilton Constantino
2026-10-23 09:53:40 +01:00
committed by bquarkz
parent d9b9eeb171
commit 57992cf214
13 changed files with 384 additions and 12 deletions
@@ -179,7 +179,7 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
emit_u16(CoreOpCode::Ret as u16, &mut out);
}
"YIELD" | "SLEEP" | "SPAWN" => {
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()))
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()));
}
"FRAME_SYNC" => {
emit_u16(CoreOpCode::FrameSync as u16, &mut out);
@@ -362,11 +362,12 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
emit_u32(field, &mut out);
}
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET"
| "ARRAY_CHECK_LENGTH" | "ARRAY_COPY" => {
| "ARRAY_CHECK_LENGTH" | "ARRAY_COPY" | "FIELDS_PUBLISH" => {
if ops.is_empty() {
return Err(AsmError::MissingOperand(line.into()));
}
let op = match mn {
"FIELDS_PUBLISH" => CoreOpCode::FieldsPublish,
"STRUCT_NEW" => CoreOpCode::StructNew,
"ARRAY_NEW" => CoreOpCode::ArrayNew,
"ARRAY_LEN" => CoreOpCode::ArrayLen,
@@ -21,9 +21,9 @@
//! - Floats use exact bit-pattern format to prevent locale/rounding differences.
//! - Ordering is the canonical decode order; no address prefixes are emitted.
use crate::DecodeError;
use crate::decode_next;
use crate::isa::core::{CoreOpCode, CoreOpCodeSpecExt};
use crate::DecodeError;
fn fmt_f64_bits(bits: u64) -> String {
// Fixed-width 16 hex digits, lowercase.
@@ -72,7 +72,8 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
| CoreOpCode::ArrayGet
| CoreOpCode::ArraySet
| CoreOpCode::ArrayCheckLength
| CoreOpCode::ArrayCopy => {
| CoreOpCode::ArrayCopy
| CoreOpCode::FieldsPublish => {
let v = u32::from_le_bytes(imm.try_into().unwrap());
format!("{}", v)
}
@@ -210,6 +210,8 @@ pub enum OpCode {
/// Move/fill array ranges and two selected Struct fields in one attempt.
/// Array TypeId, Struct TypeId, first field, second field (four u32).
StoragePublish = 0xAA,
/// Atomic full-field writes to schema-selected nullable Struct destinations.
FieldsPublish = 0xAB,
}
impl TryFrom<u16> for OpCode {
@@ -287,6 +289,7 @@ impl TryFrom<u16> for OpCode {
0xA8 => Ok(OpCode::ArrayCheckLength),
0xA9 => Ok(OpCode::ArrayCopy),
0xAA => Ok(OpCode::StoragePublish),
0xAB => Ok(OpCode::FieldsPublish),
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
}
@@ -366,7 +369,7 @@ impl OpCode {
OpCode::ArraySet => 4,
OpCode::IndexGuard | OpCode::ArrayCheckLength => 2,
OpCode::ArrayCopy => 4,
OpCode::StoragePublish => 6,
OpCode::StoragePublish | OpCode::FieldsPublish => 6,
}
}
}
@@ -676,10 +676,14 @@ impl OpCodeSpecExt for OpCode {
may_trap: true,
is_safepoint: false,
},
OpCode::ArrayCheckLength | OpCode::ArrayCopy | OpCode::StoragePublish => OpcodeSpec {
OpCode::ArrayCheckLength
| OpCode::ArrayCopy
| OpCode::StoragePublish
| OpCode::FieldsPublish => OpcodeSpec {
name: match self {
OpCode::ArrayCheckLength => "ARRAY_CHECK_LENGTH",
OpCode::ArrayCopy => "ARRAY_COPY",
OpCode::FieldsPublish => "FIELDS_PUBLISH",
_ => "STORAGE_PUBLISH",
},
imm_bytes: if *self == OpCode::StoragePublish { 16 } else { 4 },
@@ -86,7 +86,7 @@ impl DecodedInstr<'_> {
)
}
Op::GetCapture => {
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex))
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex));
}
Op::CallTrait => {
let (ty, method) = self.imm_u32x2().map_err(|_| malformed())?;
@@ -133,6 +133,28 @@ impl DecodedInstr<'_> {
};
(source_layout.storage.slots, pushes)
}
Op::FieldsPublish => {
let schema = self.imm_u32().map_err(|_| malformed())?;
let (decl, _) = type_info(schema)?;
let TypeKind::Struct(entries) = &decl.kind else {
return Err(bad_category(schema));
};
let mut slots = 0u32;
for entry in entries {
let (owner, layout) = type_info(entry.type_id.0)?;
if !matches!(owner.kind, TypeKind::Struct(_)) {
return Err(bad_category(entry.type_id.0));
}
let fields =
u32::try_from(layout.instance.as_ref().unwrap().initialization_slots)
.map_err(|_| malformed())?;
slots = slots
.checked_add(1)
.and_then(|n| n.checked_add(fields))
.ok_or_else(malformed)?;
}
(slots, 0)
}
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())?;
@@ -188,11 +210,7 @@ impl DecodedInstr<'_> {
.get(field as usize)
.ok_or_else(|| bad_category(id).member(field))?;
let w = registry.type_layout(f.type_id).unwrap().storage.slots;
if op == Op::StructGet {
(1, w)
} else {
(1 + w, 0)
}
if op == Op::StructGet { (1, w) } else { (1 + w, 0) }
}
}
} else {