feat(vm): integrate metered strings and native retention for PLN-0176 step 19
This commit is contained in:
@@ -5,6 +5,7 @@ mod loader;
|
||||
mod reference;
|
||||
mod runtime;
|
||||
mod stack;
|
||||
mod strings;
|
||||
mod typed;
|
||||
#[cfg(test)]
|
||||
mod typed_tests;
|
||||
@@ -87,6 +88,7 @@ pub struct VirtualMachine {
|
||||
/// The loaded executable (Bytecode + Constant Pool), that is the ROM translated.
|
||||
program: ProgramImage,
|
||||
validated_image: Option<std::sync::Arc<crate::ValidatedProgramImage>>,
|
||||
strings: Option<crate::string_ledger::StringLedger>,
|
||||
/// Heap Memory: Dynamic allocation pool.
|
||||
heap: Heap,
|
||||
/// Total virtual cycles consumed since the VM started.
|
||||
@@ -144,10 +146,26 @@ impl VirtualMachine {
|
||||
}
|
||||
|
||||
/// Returns up to `n` values from the top of the operand stack (top-first order).
|
||||
pub fn operand_stack_top(&self, n: usize) -> Vec<Value> {
|
||||
pub fn operand_stack_top(&self, n: usize) -> prometeu_hal::ValueSnapshot {
|
||||
let len = self.operand_stack.len();
|
||||
let start = len.saturating_sub(n);
|
||||
self.operand_stack[start..].iter().rev().cloned().collect()
|
||||
let values: Vec<_> = self.operand_stack[start..].iter().rev().cloned().collect();
|
||||
let mut owners = [None, None];
|
||||
for value in &values {
|
||||
if let Value::String(content) = value {
|
||||
if let Some(image) = &self.validated_image {
|
||||
if owners[0].is_none() && image.retains_string(content) {
|
||||
owners[0] = Some(image.string_retention());
|
||||
}
|
||||
}
|
||||
if let Some(strings) = &self.strings {
|
||||
if owners[1].is_none() && strings.retains(content) {
|
||||
owners[1] = Some(strings.retention());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
prometeu_hal::ValueSnapshot::with_owners(values, owners)
|
||||
}
|
||||
|
||||
/// Returns a reference to the VM's heap.
|
||||
@@ -204,6 +222,7 @@ impl VirtualMachine {
|
||||
std::collections::HashMap::new(),
|
||||
),
|
||||
validated_image: None,
|
||||
strings: None,
|
||||
heap: Heap::with_budget(&budget),
|
||||
cycles: 0,
|
||||
halted: false,
|
||||
@@ -723,26 +742,32 @@ impl VirtualMachine {
|
||||
self.push_trap(a, opcode, start_pc as u32)?;
|
||||
self.push_trap(b, opcode, start_pc as u32)?;
|
||||
}
|
||||
OpCode::Add => self.binary_op(opcode, start_pc as u32, |a, b| match (&a, &b) {
|
||||
(Value::String(_), _) | (_, Value::String(_)) => {
|
||||
let mut out = String::with_capacity(a.string_len_hint() + b.string_len_hint());
|
||||
a.append_to_string(&mut out);
|
||||
b.append_to_string(&mut out);
|
||||
Ok(Value::string(out))
|
||||
OpCode::Add => {
|
||||
let len = self.operand_stack.len();
|
||||
if self.temporary_slots() >= 2
|
||||
&& (matches!(self.operand_stack[len - 2], Value::String(_))
|
||||
|| matches!(self.operand_stack[len - 1], Value::String(_)))
|
||||
{
|
||||
self.execute_string_add(opcode, start_pc as u32)?;
|
||||
} else {
|
||||
self.binary_op(opcode, start_pc as u32, |a, b| match (&a, &b) {
|
||||
(Value::Int32(a), Value::Int32(b)) => Ok(Value::Int32(a.wrapping_add(*b))),
|
||||
(Value::Int64(a), Value::Int64(b)) => Ok(Value::Int64(a.wrapping_add(*b))),
|
||||
(Value::Int32(a), Value::Int64(b)) => {
|
||||
Ok(Value::Int64((*a as i64).wrapping_add(*b)))
|
||||
}
|
||||
(Value::Int64(a), Value::Int32(b)) => {
|
||||
Ok(Value::Int64(a.wrapping_add(*b as i64)))
|
||||
}
|
||||
(Value::Float(a), Value::Float(b)) => Ok(Value::Float(a + b)),
|
||||
(Value::Int32(a), Value::Float(b)) => Ok(Value::Float(*a as f64 + b)),
|
||||
(Value::Float(a), Value::Int32(b)) => Ok(Value::Float(a + *b as f64)),
|
||||
(Value::Int64(a), Value::Float(b)) => Ok(Value::Float(*a as f64 + b)),
|
||||
(Value::Float(a), Value::Int64(b)) => Ok(Value::Float(a + *b as f64)),
|
||||
_ => Err(OpError::Trap(TRAP_TYPE, "Invalid types for ADD".into())),
|
||||
})?;
|
||||
}
|
||||
(Value::Int32(a), Value::Int32(b)) => Ok(Value::Int32(a.wrapping_add(*b))),
|
||||
(Value::Int64(a), Value::Int64(b)) => Ok(Value::Int64(a.wrapping_add(*b))),
|
||||
(Value::Int32(a), Value::Int64(b)) => {
|
||||
Ok(Value::Int64((*a as i64).wrapping_add(*b)))
|
||||
}
|
||||
(Value::Int64(a), Value::Int32(b)) => Ok(Value::Int64(a.wrapping_add(*b as i64))),
|
||||
(Value::Float(a), Value::Float(b)) => Ok(Value::Float(a + b)),
|
||||
(Value::Int32(a), Value::Float(b)) => Ok(Value::Float(*a as f64 + b)),
|
||||
(Value::Float(a), Value::Int32(b)) => Ok(Value::Float(a + *b as f64)),
|
||||
(Value::Int64(a), Value::Float(b)) => Ok(Value::Float(*a as f64 + b)),
|
||||
(Value::Float(a), Value::Int64(b)) => Ok(Value::Float(a + *b as f64)),
|
||||
_ => Err(OpError::Trap(TRAP_TYPE, "Invalid types for ADD".into())),
|
||||
})?,
|
||||
}
|
||||
OpCode::Sub => self.binary_op(opcode, start_pc as u32, |a, b| match (a, b) {
|
||||
(Value::Int32(a), Value::Int32(b)) => Ok(Value::Int32(a.wrapping_sub(b))),
|
||||
(Value::Int64(a), Value::Int64(b)) => Ok(Value::Int64(a.wrapping_sub(b))),
|
||||
@@ -1222,12 +1247,22 @@ impl VirtualMachine {
|
||||
self.operand_stack
|
||||
.reserve(syscall.results_count())
|
||||
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
|
||||
let mut strings = strings::SessionStringAllocator {
|
||||
ledger: &mut self.strings,
|
||||
budget: self.budget.clone(),
|
||||
};
|
||||
let mut ret = crate::HostReturn::new(
|
||||
self.operand_stack.native_result_buffer(),
|
||||
syscall.results_count(),
|
||||
&mut strings,
|
||||
);
|
||||
let native_result = native.syscall(id, &args, &mut ret, ctx);
|
||||
let return_overflow = ret.overflowed();
|
||||
let return_failure = ret.failure().cloned();
|
||||
let native_result = return_failure.map(Err).unwrap_or(native_result);
|
||||
if native_result.is_err() {
|
||||
self.operand_stack.truncate(stack_height_before);
|
||||
}
|
||||
native_result.map_err(|fault| match fault {
|
||||
VmFault::Trap(code, msg) => {
|
||||
self.trap(code, OpCode::Syscall as u16, msg, pc_at_syscall)
|
||||
|
||||
Reference in New Issue
Block a user