feat: retain read-only execution frames across instruction slices
This commit is contained in:
+147
-4
@@ -1,4 +1,5 @@
|
||||
use crate::frame::{NativeReturn, StackEntry};
|
||||
use crate::ExecutionProgress;
|
||||
use crate::frame::{NativeReturn, StackEntry, SuspendedCall};
|
||||
use crate::{
|
||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
|
||||
@@ -69,6 +70,11 @@ impl Vm {
|
||||
arguments: &[Value],
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<Value, VmError> {
|
||||
if self.suspended_call.is_some() {
|
||||
return Err(VmError::UnsupportedOperation(
|
||||
"call while read-only execution is suspended",
|
||||
));
|
||||
}
|
||||
self.last_traceback.clear();
|
||||
let mut executed = 0;
|
||||
let frame = match self.prepare_call(function, arguments.to_vec(), &mut executed, limits)? {
|
||||
@@ -80,7 +86,9 @@ impl Vm {
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
}
|
||||
let mut frames = vec![frame];
|
||||
let result = self.eval_frames(&mut frames, limits, &mut executed);
|
||||
let result = self
|
||||
.eval_frames(&mut frames, limits, &mut executed, usize::MAX)
|
||||
.map(|value| value.expect("unbounded slice completes or fails"));
|
||||
if result.is_err() {
|
||||
self.last_traceback = frames
|
||||
.iter()
|
||||
@@ -98,6 +106,137 @@ impl Vm {
|
||||
result
|
||||
}
|
||||
|
||||
/// Begin a resumable read-only call. One VM owns at most one continuation.
|
||||
/// A slice is a soft instruction quantum: native callbacks and delegation
|
||||
/// complete atomically and remain subject to the cumulative hard limits.
|
||||
pub fn start_read_only_call(
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
arguments: &[Value],
|
||||
limits: ExecutionLimits,
|
||||
slice: usize,
|
||||
) -> Result<ExecutionProgress, VmError> {
|
||||
if slice == 0 {
|
||||
return Err(VmError::InvalidArgument("execution slice must be positive"));
|
||||
}
|
||||
if self.suspended_call.is_some() || self.objects.is_read_only() {
|
||||
return Err(VmError::UnsupportedOperation(
|
||||
"start suspended call inside another execution",
|
||||
));
|
||||
}
|
||||
self.last_traceback.clear();
|
||||
let scope = self.objects.begin_read_only();
|
||||
let mut executed = 0;
|
||||
let prepared = self.prepare_call(function, arguments.to_vec(), &mut executed, limits);
|
||||
match prepared {
|
||||
Ok(PreparedCall::Value(value)) => {
|
||||
let reads = match self.objects.end_read_only(scope) {
|
||||
Ok(reads) => reads,
|
||||
Err(error) => {
|
||||
self.drop_root(value);
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
Ok(ExecutionProgress::Complete {
|
||||
value,
|
||||
reads,
|
||||
instructions: executed,
|
||||
})
|
||||
}
|
||||
Ok(PreparedCall::Frame(frame)) => {
|
||||
if limits.max_frames == 0 {
|
||||
self.clear_frame(frame);
|
||||
self.objects.end_read_only(scope)?;
|
||||
return Err(VmError::ExecutionLimit("frames"));
|
||||
}
|
||||
self.suspended_call = Some(SuspendedCall {
|
||||
frames: vec![frame],
|
||||
scope,
|
||||
limits,
|
||||
executed,
|
||||
});
|
||||
// Preparation counts toward the first quantum as well.
|
||||
self.resume_read_only_to(slice)
|
||||
}
|
||||
Err(error) => {
|
||||
self.objects.end_read_only(scope)?;
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_suspended_call(&self) -> bool {
|
||||
self.suspended_call.is_some()
|
||||
}
|
||||
|
||||
/// Resume existing frames, never replay earlier instructions. The read-only
|
||||
/// scope remains active across Pending; preexisting heap writes stay barred.
|
||||
pub fn resume_read_only_call(&mut self, slice: usize) -> Result<ExecutionProgress, VmError> {
|
||||
if slice == 0 {
|
||||
return Err(VmError::InvalidArgument("execution slice must be positive"));
|
||||
}
|
||||
let executed = self
|
||||
.suspended_call
|
||||
.as_ref()
|
||||
.ok_or(VmError::InvalidArgument("no suspended call"))?
|
||||
.executed;
|
||||
self.resume_read_only_to(executed.saturating_add(slice))
|
||||
}
|
||||
|
||||
fn resume_read_only_to(&mut self, slice_end: usize) -> Result<ExecutionProgress, VmError> {
|
||||
let mut call = self
|
||||
.suspended_call
|
||||
.take()
|
||||
.ok_or(VmError::InvalidArgument("no suspended call"))?;
|
||||
let result = self.eval_frames(&mut call.frames, call.limits, &mut call.executed, slice_end);
|
||||
if let Ok(None) = result {
|
||||
let instructions = call.executed;
|
||||
self.suspended_call = Some(call);
|
||||
return Ok(ExecutionProgress::Pending { instructions });
|
||||
}
|
||||
if result.is_err() {
|
||||
self.last_traceback = call
|
||||
.frames
|
||||
.iter()
|
||||
.map(|frame| ExecutionLocation {
|
||||
function: frame.name.clone(),
|
||||
filename: frame.executable.filename().into(),
|
||||
first_line: frame.executable.first_line(),
|
||||
instruction_offset: frame.current_offset,
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
while let Some(frame) = call.frames.pop() {
|
||||
self.clear_frame(frame);
|
||||
}
|
||||
let reads = self.objects.end_read_only(call.scope);
|
||||
match (result, reads) {
|
||||
(Ok(Some(value)), Ok(reads)) => Ok(ExecutionProgress::Complete {
|
||||
value,
|
||||
reads,
|
||||
instructions: call.executed,
|
||||
}),
|
||||
(Ok(Some(value)), Err(error)) => {
|
||||
self.drop_root(value);
|
||||
Err(error)
|
||||
}
|
||||
(Err(error), _) => Err(error),
|
||||
(Ok(None), _) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Release all retained execution roots and close the read-only scope.
|
||||
pub fn cancel_read_only_call(&mut self) -> Result<bool, VmError> {
|
||||
let Some(mut call) = self.suspended_call.take() else {
|
||||
return Ok(false);
|
||||
};
|
||||
while let Some(frame) = call.frames.pop() {
|
||||
self.clear_frame(frame);
|
||||
}
|
||||
self.objects.end_read_only(call.scope)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Outer-to-inner execution locations from the last failed call/execute.
|
||||
/// These are copied metadata, not heap references or language exceptions.
|
||||
pub fn last_traceback(&self) -> &[ExecutionLocation] {
|
||||
@@ -613,11 +752,15 @@ impl Vm {
|
||||
frames: &mut Vec<EndorFrame>,
|
||||
limits: ExecutionLimits,
|
||||
executed: &mut usize,
|
||||
) -> Result<Value, VmError> {
|
||||
slice_end: usize,
|
||||
) -> Result<Option<Value>, VmError> {
|
||||
loop {
|
||||
if *executed >= limits.max_instructions {
|
||||
return Err(VmError::ExecutionLimit("instructions"));
|
||||
}
|
||||
if *executed >= slice_end {
|
||||
return Ok(None);
|
||||
}
|
||||
*executed += 1;
|
||||
let frame = frames.last_mut().expect("an evaluation frame is active");
|
||||
let decoded = frame.executable.instruction_at(frame.instruction_offset)?;
|
||||
@@ -782,7 +925,7 @@ impl Vm {
|
||||
if let Some(caller) = frames.last_mut() {
|
||||
caller.stack.push(StackEntry::Value(result));
|
||||
} else {
|
||||
return Ok(result);
|
||||
return Ok(Some(result));
|
||||
}
|
||||
}
|
||||
Opcode::JumpForward | Opcode::JumpBackward => {
|
||||
|
||||
Reference in New Issue
Block a user