feat(vm): enforce typed CALL RET and native entry boundaries for PLN-0176 step 26
This commit is contained in:
@@ -3,6 +3,9 @@ mod gc;
|
||||
mod loader;
|
||||
mod runtime;
|
||||
mod stack;
|
||||
mod typed;
|
||||
#[cfg(test)]
|
||||
mod typed_tests;
|
||||
|
||||
use crate::call_frame::CallFrame;
|
||||
use crate::heap::{CoroutineState, Heap};
|
||||
@@ -81,6 +84,7 @@ pub struct VirtualMachine {
|
||||
globals: BudgetVec<Value>,
|
||||
/// The loaded executable (Bytecode + Constant Pool), that is the ROM translated.
|
||||
program: ProgramImage,
|
||||
validated_image: Option<std::sync::Arc<crate::ValidatedProgramImage>>,
|
||||
/// Heap Memory: Dynamic allocation pool.
|
||||
heap: Heap,
|
||||
/// Total virtual cycles consumed since the VM started.
|
||||
@@ -197,6 +201,7 @@ impl VirtualMachine {
|
||||
None,
|
||||
std::collections::HashMap::new(),
|
||||
),
|
||||
validated_image: None,
|
||||
heap: Heap::with_budget(&budget),
|
||||
cycles: 0,
|
||||
halted: false,
|
||||
@@ -213,6 +218,20 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sessions share validated code, constants, debug and Registry ownership.
|
||||
/// Entry setup validates supplied groups before publishing an active frame.
|
||||
pub fn from_image(
|
||||
image: std::sync::Arc<crate::ValidatedProgramImage>,
|
||||
limits: RuntimeLimits,
|
||||
) -> Self {
|
||||
let mut vm = Self::with_runtime_limits(Vec::new(), Vec::new(), limits);
|
||||
vm.program = image.execution.clone();
|
||||
vm.capabilities = image.capabilities();
|
||||
vm.validated_image = Some(image);
|
||||
vm.halted = true;
|
||||
vm
|
||||
}
|
||||
|
||||
/// Executes a single instruction at the current Program Counter (PC).
|
||||
///
|
||||
/// This follows the classic CPU cycle:
|
||||
@@ -1056,7 +1075,7 @@ impl VirtualMachine {
|
||||
)
|
||||
})?;
|
||||
|
||||
if self.operand_stack.len() < callee.param_slots as usize {
|
||||
if self.temporary_slots() < callee.param_slots as usize {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
@@ -1072,14 +1091,21 @@ impl VirtualMachine {
|
||||
|
||||
let stack_base = self.operand_stack.len() - callee.param_slots as usize;
|
||||
|
||||
self.check_signature_group(func_id, false, &self.operand_stack[stack_base..])
|
||||
.map_err(|(_, error)| self.value_trap(error, opcode, start_pc as u32))?;
|
||||
|
||||
let callee_local_slots = callee.local_slots;
|
||||
let callee_offset = callee.code_offset;
|
||||
self.operand_stack
|
||||
.reserve(callee_local_slots as usize)
|
||||
let stack_growth = self
|
||||
.operand_stack
|
||||
.prepare_growth(callee_local_slots as usize)
|
||||
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
|
||||
self.call_stack
|
||||
.reserve(1)
|
||||
let frame_growth = self
|
||||
.call_stack
|
||||
.prepare_growth(1)
|
||||
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
|
||||
self.operand_stack.commit_growth(stack_growth);
|
||||
self.call_stack.commit_growth(frame_growth);
|
||||
// Allocate and zero-init local_slots
|
||||
for _ in 0..callee_local_slots {
|
||||
self.operand_stack
|
||||
@@ -1093,7 +1119,7 @@ impl VirtualMachine {
|
||||
self.pc = callee_offset as usize;
|
||||
}
|
||||
OpCode::Ret => {
|
||||
let frame = self.call_stack.pop().ok_or_else(|| {
|
||||
let frame = self.call_stack.last().cloned().ok_or_else(|| {
|
||||
self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
@@ -1117,6 +1143,13 @@ impl VirtualMachine {
|
||||
), start_pc as u32));
|
||||
}
|
||||
|
||||
self.check_signature_group(
|
||||
frame.func_idx,
|
||||
true,
|
||||
&self.operand_stack[current_height - return_slots..],
|
||||
)
|
||||
.map_err(|(_, error)| self.value_trap(error, opcode, start_pc as u32))?;
|
||||
|
||||
// Copy return values (preserving order: pop return_slots values, then reverse to push back)
|
||||
let mut return_vals = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes);
|
||||
return_vals
|
||||
@@ -1129,6 +1162,7 @@ impl VirtualMachine {
|
||||
}
|
||||
return_vals.reverse();
|
||||
|
||||
self.call_stack.pop();
|
||||
self.operand_stack.truncate(frame.stack_base);
|
||||
for val in return_vals.drain(..) {
|
||||
self.push_trap(val, opcode, start_pc as u32)?;
|
||||
@@ -4897,6 +4931,7 @@ mod tests {
|
||||
let limits = RuntimeLimits::default().lowered(RuntimeResource::CallDepth, 0).unwrap();
|
||||
let mut vm = VirtualMachine::with_runtime_limits(assemble("HALT").unwrap(), vec![], limits);
|
||||
let error = vm.prepare_boot_call().unwrap_err();
|
||||
let VmInitError::RuntimeResourceLimit(error) = error else { panic!("{error:?}") };
|
||||
assert_eq!(
|
||||
(error.resource, error.requested, error.limit),
|
||||
(RuntimeResource::CallDepth, 1, 0)
|
||||
@@ -5056,7 +5091,9 @@ mod tests {
|
||||
fn guest_quota_failure_does_not_publish_an_entry_context() {
|
||||
let limits = RuntimeLimits::default().lowered(RuntimeResource::GuestBytes, 0).unwrap();
|
||||
let mut vm = VirtualMachine::with_runtime_limits(assemble("HALT").unwrap(), vec![], limits);
|
||||
assert_eq!(vm.prepare_boot_call().unwrap_err().resource, RuntimeResource::GuestBytes);
|
||||
assert!(
|
||||
matches!(vm.prepare_boot_call().unwrap_err(), VmInitError::RuntimeResourceLimit(error) if error.resource == RuntimeResource::GuestBytes)
|
||||
);
|
||||
assert!(vm.current_coro.is_none());
|
||||
assert!(vm.call_stack.is_empty());
|
||||
assert_eq!(vm.heap.len(), 0);
|
||||
|
||||
Reference in New Issue
Block a user