feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good

This commit is contained in:
2026-10-13 15:24:01 +01:00
parent 0af8b2b1fe
commit 6c2a07e579
65 changed files with 6222 additions and 985 deletions
@@ -9,6 +9,7 @@ mod dynamic;
mod gc;
mod lifecycle;
mod loader;
mod native_arrays;
mod random;
mod reference;
mod runtime;
@@ -114,6 +115,9 @@ pub struct VirtualMachine {
halted: bool,
lifecycle: Option<lifecycle::LifecycleCall>,
lifecycle_failure: Option<LogicalFrameEndingReason>,
memcard_boot_started: bool,
memcard_boot_result: Option<i32>,
memcard_boot_required: bool,
/// Set of ROM addresses used for software breakpoints in the debugger.
breakpoints: std::collections::HashSet<usize>,
/// GC: number of newly allocated live objects threshold to trigger a collection at safepoint.
@@ -235,6 +239,9 @@ impl VirtualMachine {
cycles: 0,
halted: true,
lifecycle: None,
memcard_boot_started: false,
memcard_boot_result: None,
memcard_boot_required: false,
lifecycle_failure: None,
breakpoints: std::collections::HashSet::new(),
gc_alloc_threshold: 1024, // conservative default; tests may override
@@ -844,6 +851,11 @@ impl VirtualMachine {
self.operand_stack
.reserve(syscall.results_count())
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
let mut arrays = native_arrays::SessionArrays {
heap: &mut self.heap,
registry: self.validated_image.as_ref().map(|image| image.registry().as_ref()),
work: 0,
};
let mut strings = strings::SessionStringAllocator {
ledger: &mut self.strings,
budget: self.budget.clone(),
@@ -852,10 +864,12 @@ impl VirtualMachine {
self.operand_stack.native_result_buffer(),
syscall.results_count(),
&mut strings,
);
)
.with_arrays(&mut arrays);
let native_result = native.syscall(id, &args, &mut ret, ctx);
let return_overflow = ret.overflowed();
let return_failure = ret.failure().cloned();
self.cycles += arrays.work;
let native_result = return_failure.map(Err).unwrap_or(native_result);
if native_result.is_err() {
self.operand_stack.truncate(stack_height_before);
@@ -885,6 +899,16 @@ impl VirtualMachine {
return_overflow
)));
}
if syscall == prometeu_hal::syscalls::Syscall::MemBootstrap {
let context = self.pop_integer().map_err(LogicalFrameEndingReason::Panic)?;
if !self.begin_memcard_boot(context).map_err(|e| {
LogicalFrameEndingReason::Panic(format!("Invalid memcard bootstrap: {e:?}"))
})? {
return Err(LogicalFrameEndingReason::Panic(
"Missing memcard boot binding".into(),
));
}
}
}
OpCode::Intrinsic => {
let pc_at_intrinsic = start_pc as u32;
@@ -1046,6 +1070,11 @@ impl VirtualMachine {
return self.execute_collection(opcode, ty, field, start_pc as u32);
}
OpCode::FrameSync => {
if self.memcard_boot_required && self.memcard_boot_result != Some(0) {
return Err(LogicalFrameEndingReason::Panic(
"Frame entered before memcard bootstrap admission".into(),
));
}
if self.lifecycle.is_some() {
return Err(self.trap(
TRAP_TYPE,
@@ -3,6 +3,7 @@ use super::*;
use prometeu_bytecode::registry::LifecycleRole;
pub(super) struct LifecycleCall {
role: LifecycleRole,
depth: usize,
pc: usize,
halted: bool,
@@ -28,6 +29,39 @@ impl VirtualMachine {
/// Returns false for an absent role. Entry is transactional with respect to quotas.
pub fn begin_lifecycle(&mut self, role: LifecycleRole) -> Result<bool, VmInitError> {
if role == LifecycleRole::MemcardBoot {
return Err(VmInitError::EntrypointNotFound);
}
self.begin_controlled_call(role, None)
}
pub fn begin_memcard_boot(&mut self, context: i64) -> Result<bool, VmInitError> {
if self.memcard_boot_started {
return Err(VmInitError::EntrypointNotFound);
}
let started = self.begin_controlled_call(LifecycleRole::MemcardBoot, Some(context))?;
self.memcard_boot_started = started;
Ok(started)
}
pub fn memcard_boot_active(&self) -> bool {
self.lifecycle.as_ref().is_some_and(|c| c.role == LifecycleRole::MemcardBoot)
}
pub fn memcard_boot_result(&self) -> Option<i32> {
self.memcard_boot_result
}
pub fn require_memcard_boot(&mut self) -> Result<(), VmInitError> {
if !self.has_lifecycle_hook(LifecycleRole::MemcardBoot) {
return Err(VmInitError::EntrypointNotFound);
}
self.memcard_boot_required = true;
Ok(())
}
fn begin_controlled_call(
&mut self,
role: LifecycleRole,
context: Option<i64>,
) -> Result<bool, VmInitError> {
if self.lifecycle.is_some() || self.lifecycle_failure.is_some() {
return Err(VmInitError::EntrypointNotFound);
}
@@ -41,18 +75,23 @@ impl VirtualMachine {
let locals = callee.local_slots as usize;
let offset = callee.code_offset as usize;
self.budget.check(RuntimeResource::CallDepth, (self.call_stack.len() + 1) as u64)?;
let operands = self.operand_stack.prepare_growth(locals)?;
let operands =
self.operand_stack.prepare_growth(locals + usize::from(context.is_some()))?;
let frames = self.call_stack.prepare_growth(1)?;
self.operand_stack.commit_growth(operands);
self.call_stack.commit_growth(frames);
let base = self.operand_stack.len();
let call = LifecycleCall {
role,
depth: self.call_stack.len(),
pc: self.pc,
halted: self.halted,
remaining: Self::LIFECYCLE_CYCLES,
breakpoint: None,
};
if let Some(context) = context {
self.operand_stack.push(Value::Int64(context))?;
}
for _ in 0..locals {
self.operand_stack.push(Value::Null)?;
}
@@ -80,14 +119,32 @@ impl VirtualMachine {
if let Some(call) = &mut self.lifecycle {
call.breakpoint = None;
}
let had_call = self.lifecycle.is_some();
let before = self.cycles;
let result = self.step_inner(native, ctx);
if !had_call && self.lifecycle.is_some() && result.is_ok() {
return Err(LogicalFrameEndingReason::BudgetExhausted);
}
let Some(call) = self.lifecycle.as_mut() else {
return result;
};
call.remaining = call.remaining.saturating_sub(self.cycles - before);
if result.is_ok() && !self.halted && self.call_stack.len() == call.depth {
let call = self.lifecycle.take().unwrap();
if call.role == LifecycleRole::MemcardBoot {
match self.operand_stack.last() {
Some(Value::Int32(result @ (0 | 1))) => {
self.memcard_boot_result = Some(*result)
}
_ => {
let error = LogicalFrameEndingReason::Panic(
"MemcardBoot must return READY or INCONSISTENT".into(),
);
self.lifecycle_failure = Some(error.clone());
return Err(error);
}
}
}
self.pc = call.pc;
self.halted = call.halted;
return Err(LogicalFrameEndingReason::LifecycleComplete);
@@ -0,0 +1,72 @@
//! Narrow native access to typed arrays; no borrowed guest storage reaches IO workers.
use crate::heap::Heap;
use crate::value_contract::ValueError;
use prometeu_bytecode::registry::{BuiltinId, Registry, TypeKind};
use prometeu_bytecode::{HeapRef, Value};
use prometeu_hal::host_return::GuestByteArrays;
use prometeu_hal::vm_fault::VmFault;
pub(super) struct SessionArrays<'a> {
pub heap: &'a mut Heap,
pub registry: Option<&'a Registry>,
pub work: u64,
}
fn fault(error: ValueError) -> VmFault {
match error {
ValueError::Resource(r) => VmFault::ResourceLimit(r),
other => VmFault::Trap(
super::VirtualMachine::value_error_code(other),
"Invalid native byte-array range/type/value".into(),
),
}
}
impl GuestByteArrays for SessionArrays<'_> {
fn validate(&mut self, handle: HeapRef, offset: usize, count: usize) -> Result<(), VmFault> {
self.work += count.min(4096) as u64;
let registry = self.registry.ok_or_else(|| fault(ValueError::Type))?;
let ty = self.heap.concrete_type(handle, registry).map_err(fault)?;
let array = registry
.type_layout(ty)
.and_then(|t| t.array)
.ok_or_else(|| fault(ValueError::Type))?;
if registry.declarations().types[array.element_type_id.0 as usize].kind
!= TypeKind::Builtin(BuiltinId::INT32)
{
return Err(fault(ValueError::Type));
}
let len = self.heap.typed_array_len(handle, registry).map_err(fault)?;
if count > 4096 || offset.checked_add(count).is_none_or(|end| end > len) {
return Err(fault(ValueError::Bounds));
}
Ok(())
}
fn read(&mut self, handle: HeapRef, offset: usize, target: &mut [u8]) -> Result<(), VmFault> {
self.validate(handle, offset, target.len())?;
let registry = self.registry.unwrap();
for index in offset..offset + target.len() {
if !matches!(
self.heap.load_typed(registry, handle, index).map_err(fault)?.as_slice(),
[Value::Int32(0..=255)]
) {
return Err(fault(ValueError::Type));
}
}
self.work += target.len() as u64;
for (i, byte) in target.iter_mut().enumerate() {
let value = self.heap.load_typed(registry, handle, offset + i).map_err(fault)?;
let [Value::Int32(n)] = value.as_slice() else { unreachable!() };
*byte = *n as u8;
}
Ok(())
}
fn write(&mut self, handle: HeapRef, offset: usize, bytes: &[u8]) -> Result<(), VmFault> {
self.validate(handle, offset, bytes.len())?;
let registry = self.registry.unwrap();
for (i, &byte) in bytes.iter().enumerate() {
self.heap
.store_typed(registry, handle, offset + i, &[Value::Int32(byte as i32)])
.map_err(fault)?;
}
Ok(())
}
}
@@ -946,3 +946,31 @@ mod lifecycle_tests {
assert!(vm.call_stack.is_empty());
}
}
#[test]
fn native_byte_carrier_validates_entire_input_before_copy_and_charges_work() {
use prometeu_hal::host_return::GuestByteArrays;
let mut code = Vec::new();
op(&mut code, OpCode::Halt);
let image = image(&code, vec![function(0, code.len(), &[], &[], 0, 0)]);
let registry = image.registry().clone();
let mut vm = VirtualMachine::default();
let handle = vm.heap.allocate_typed_array(registry.clone(), TypeId(13), 3).unwrap();
vm.heap.store_typed(&registry, handle, 0, &[Value::Int32(255)]).unwrap();
vm.heap.store_typed(&registry, handle, 2, &[Value::Int32(256)]).unwrap();
let wrong = vm.heap.allocate_typed_array(registry.clone(), TypeId(14), 3).unwrap();
let mut access = super::native_arrays::SessionArrays {
heap: &mut vm.heap,
registry: Some(&registry),
work: 0,
};
let mut out = [17; 3];
assert!(access.read(handle, 0, &mut out).is_err());
assert_eq!(out, [17; 3]);
assert!(access.validate(wrong, 0, 3).is_err());
assert!(access.write(handle, 2, &[1, 2]).is_err());
access.write(handle, 2, &[42]).unwrap();
access.read(handle, 0, &mut out).unwrap();
assert_eq!(out, [255, 0, 42]);
assert!(access.work >= 12);
}