feat(runtime): add optional pause and resume lifecycle hooks
Intrepid/Prometeu/Runtime/pipeline/pr-master Build started...
Intrepid/Prometeu/Runtime/pipeline/pr-master Build started...
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
//! Bounded calls that retain the suspended guest continuation and its GC roots.
|
||||
use super::*;
|
||||
use prometeu_bytecode::registry::LifecycleRole;
|
||||
|
||||
pub(super) struct LifecycleCall {
|
||||
depth: usize,
|
||||
pc: usize,
|
||||
halted: bool,
|
||||
pub(super) remaining: u64,
|
||||
pub(super) breakpoint: Option<usize>,
|
||||
}
|
||||
|
||||
impl VirtualMachine {
|
||||
pub const LIFECYCLE_CYCLES: u64 = 1_500_000;
|
||||
pub const LIFECYCLE_SLICE: u64 = 250_000;
|
||||
|
||||
pub fn has_lifecycle_hook(&self, role: LifecycleRole) -> bool {
|
||||
self.validated_image.as_ref().is_some_and(|i| i.registry().lifecycle_hook(role).is_some())
|
||||
}
|
||||
|
||||
pub fn lifecycle_active(&self) -> bool {
|
||||
self.lifecycle.is_some()
|
||||
}
|
||||
|
||||
pub fn lifecycle_remaining_cycles(&self) -> Option<u64> {
|
||||
self.lifecycle.as_ref().map(|call| call.remaining)
|
||||
}
|
||||
|
||||
/// 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 self.lifecycle.is_some() || self.lifecycle_failure.is_some() {
|
||||
return Err(VmInitError::EntrypointNotFound);
|
||||
}
|
||||
let Some(id) =
|
||||
self.validated_image.as_ref().and_then(|i| i.registry().lifecycle_hook(role))
|
||||
else {
|
||||
return Ok(false);
|
||||
};
|
||||
let func_idx = id.0 as usize;
|
||||
let callee = &self.program.functions[func_idx];
|
||||
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 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 {
|
||||
depth: self.call_stack.len(),
|
||||
pc: self.pc,
|
||||
halted: self.halted,
|
||||
remaining: Self::LIFECYCLE_CYCLES,
|
||||
breakpoint: None,
|
||||
};
|
||||
for _ in 0..locals {
|
||||
self.operand_stack.push(Value::Null)?;
|
||||
}
|
||||
self.call_stack.push(CallFrame {
|
||||
return_pc: self.pc as u32,
|
||||
stack_base: base,
|
||||
func_idx,
|
||||
closure: None,
|
||||
})?;
|
||||
self.lifecycle = Some(call);
|
||||
self.pc = offset;
|
||||
self.halted = false;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Both the debugger and normal slices pass through the same accounting boundary.
|
||||
pub fn step(
|
||||
&mut self,
|
||||
native: &mut dyn NativeInterface,
|
||||
ctx: &mut HostContext,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
if let Some(reason) = &self.lifecycle_failure {
|
||||
return Err(reason.clone());
|
||||
}
|
||||
if let Some(call) = &mut self.lifecycle {
|
||||
call.breakpoint = None;
|
||||
}
|
||||
let before = self.cycles;
|
||||
let result = self.step_inner(native, ctx);
|
||||
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();
|
||||
self.pc = call.pc;
|
||||
self.halted = call.halted;
|
||||
return Err(LogicalFrameEndingReason::LifecycleComplete);
|
||||
}
|
||||
let failure = match result {
|
||||
Err(reason) => Some(reason),
|
||||
Ok(()) if self.halted => Some(LogicalFrameEndingReason::Halted),
|
||||
Ok(()) if call.remaining == 0 => Some(LogicalFrameEndingReason::Panic(
|
||||
"lifecycle total cycle budget exhausted".into(),
|
||||
)),
|
||||
Ok(()) => None,
|
||||
};
|
||||
if let Some(reason) = failure {
|
||||
self.lifecycle_failure = Some(reason.clone());
|
||||
return Err(reason);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user