feat(vm): enforce typed CALL RET and native entry boundaries for PLN-0176 step 26

This commit is contained in:
2026-10-07 20:32:09 +01:00
parent a9d91be39e
commit ba603ce64a
9 changed files with 477 additions and 24 deletions
@@ -70,6 +70,7 @@ fn patch_module_hostcalls(
impl VirtualMachine {
pub fn initialize(&mut self, program_bytes: Vec<u8>) -> Result<(), VmInitError> {
self.program = ProgramImage::default();
self.validated_image = None;
self.pc = 0;
self.operand_stack.clear();
self.call_stack.clear();
@@ -128,26 +129,61 @@ impl VirtualMachine {
self.capabilities = caps;
}
pub fn prepare_boot_call(&mut self) -> Result<(), ResourceFailure> {
self.prepare_call_by_index(0)
pub fn prepare_boot_call(&mut self) -> Result<(), VmInitError> {
self.prepare_call_by_index(0, &[])
}
pub fn prepare_call(&mut self, entrypoint: &str) -> Result<(), ResourceFailure> {
pub fn prepare_call(&mut self, entrypoint: &str) -> Result<(), VmInitError> {
self.prepare_call_with_args(entrypoint, &[])
}
pub fn prepare_call_with_args(
&mut self,
entrypoint: &str,
arguments: &[Value],
) -> Result<(), VmInitError> {
let func_idx = if let Ok(idx) = entrypoint.parse::<usize>() {
idx
} else if let Some(image) = &self.validated_image {
image
.exports()
.iter()
.rfind(|export| export.symbol == entrypoint)
.map(|export| export.func_idx as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else {
self.program.exports.get(entrypoint).map(|&idx| idx as usize).ok_or(()).unwrap_or(0)
};
self.prepare_call_by_index(func_idx)
self.prepare_call_by_index(func_idx, arguments)
}
fn prepare_call_by_index(&mut self, func_idx: usize) -> Result<(), ResourceFailure> {
fn prepare_call_by_index(
&mut self,
func_idx: usize,
arguments: &[Value],
) -> Result<(), VmInitError> {
if self.validated_image.is_some() {
self.check_signature_group(func_idx, false, arguments).map_err(|(slot, error)| {
if self.program.functions.get(func_idx).is_none() {
VmInitError::EntrypointNotFound
} else {
VmInitError::EntrypointArgumentsInvalid {
function: func_idx as u32,
slot: slot as u32,
trap_code: Self::value_error_code(error),
}
}
})?;
}
self.budget.check(RuntimeResource::CallDepth, 1)?;
let callee = self.program.functions.get(func_idx).cloned().unwrap_or_default();
let total_slots = callee.param_slots as usize + callee.local_slots as usize;
self.operand_stack.reserve(total_slots.saturating_sub(self.operand_stack.len()))?;
self.call_stack.reserve(1usize.saturating_sub(self.call_stack.len()))?;
let stack_growth = self
.operand_stack
.prepare_growth(total_slots.saturating_sub(self.operand_stack.len()))?;
let frame_growth =
self.call_stack.prepare_growth(1usize.saturating_sub(self.call_stack.len()))?;
let main_href = self.heap.allocate_coroutine(
callee.code_offset as usize,
CoroutineState::Running,
@@ -155,6 +191,8 @@ impl VirtualMachine {
BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes),
BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes),
)?;
self.operand_stack.commit_growth(stack_growth);
self.call_stack.commit_growth(frame_growth);
self.pc = callee.code_offset as usize;
self.halted = false;
@@ -162,8 +200,13 @@ impl VirtualMachine {
self.call_stack.clear();
if let Some(func) = self.program.functions.get(func_idx) {
let total_slots = func.param_slots as u32 + func.local_slots as u32;
for _ in 0..total_slots {
let typed = self.validated_image.is_some();
if typed {
self.operand_stack.extend_from_slice(arguments)?;
}
let null_slots =
func.local_slots as usize + if typed { 0 } else { func.param_slots as usize };
for _ in 0..null_slots {
self.operand_stack.push(Value::Null)?;
}
}