feat(vm): add reference identity and deterministic value hashing
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
//! Shared borrowed invocation; algorithms receive no host/VM context.
|
||||
use super::*;
|
||||
use crate::{
|
||||
AbiType, IntrinsicExecutionError as E, IntrinsicImplementation as I, IntrinsicMeta,
|
||||
IntrinsicResults,
|
||||
};
|
||||
|
||||
impl VirtualMachine {
|
||||
fn inline_values(
|
||||
&self,
|
||||
values: &[Value],
|
||||
layout: &[AbiType],
|
||||
) -> Result<(), crate::value_contract::ValueError> {
|
||||
use crate::value_contract::ValueError;
|
||||
for (value, abi) in values.iter().zip(layout) {
|
||||
match value {
|
||||
Value::String(content) if self.validated_image.is_some() => {
|
||||
if !self.validated_image.as_ref().unwrap().retains_string(content)
|
||||
&& !self.strings.as_ref().is_some_and(|domain| domain.retains(content))
|
||||
{
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
}
|
||||
Value::HeapRef(reference) if matches!(abi, AbiType::Builtin(key) if key.name == "Random") =>
|
||||
{
|
||||
self.heap.random_state(*reference)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn execute_inline_intrinsic(
|
||||
&mut self,
|
||||
meta: &IntrinsicMeta,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
let op = OpCode::Intrinsic;
|
||||
let consumed = meta.arg_slots();
|
||||
let enough = self.temporary_slots() >= consumed;
|
||||
let args =
|
||||
if enough { &self.operand_stack[self.operand_stack.len() - consumed..] } else { &[] };
|
||||
let Some(cost) = meta.attempted_cost(args) else {
|
||||
self.cycles += meta.cost_cycles;
|
||||
return Err(self.trap(
|
||||
TRAP_RESOURCE_LIMIT,
|
||||
op as u16,
|
||||
"Intrinsic attempted cost overflow".into(),
|
||||
pc,
|
||||
));
|
||||
};
|
||||
self.cycles += cost;
|
||||
meta.validate_descriptor()
|
||||
.map_err(|message| self.trap(TRAP_INVALID_INTRINSIC, op as u16, message.into(), pc))?;
|
||||
if !enough {
|
||||
return Err(self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
op as u16,
|
||||
"Incomplete intrinsic operands".into(),
|
||||
pc,
|
||||
));
|
||||
}
|
||||
let I::BorrowedInline(handler) = meta.implementation else {
|
||||
return Err(self.trap(
|
||||
TRAP_INVALID_INTRINSIC,
|
||||
op as u16,
|
||||
"Wrong invocation profile".into(),
|
||||
pc,
|
||||
));
|
||||
};
|
||||
let base = self.operand_stack.len() - consumed;
|
||||
let args = &self.operand_stack[base..];
|
||||
meta.validate_argument_values(args).map_err(|_| {
|
||||
self.trap(TRAP_TYPE, op as u16, "Invalid intrinsic argument carrier".into(), pc)
|
||||
})?;
|
||||
self.inline_values(args, meta.arg_layout)
|
||||
.map_err(|error| self.value_trap(error, op, pc))?;
|
||||
let mut out = IntrinsicResults::new(meta.ret_slots());
|
||||
handler(args, &mut out).map_err(|error| match error {
|
||||
E::Numeric(error) => {
|
||||
self.trap(error.code, op as u16, format!("Intrinsic numeric fault: {error:?}"), pc)
|
||||
}
|
||||
_ => self.trap(
|
||||
TRAP_INVALID_INTRINSIC,
|
||||
op as u16,
|
||||
format!("Invalid intrinsic handler output: {error:?}"),
|
||||
pc,
|
||||
),
|
||||
})?;
|
||||
out.validate(meta).map_err(|error| {
|
||||
self.trap(
|
||||
TRAP_INVALID_INTRINSIC,
|
||||
op as u16,
|
||||
format!("Invalid intrinsic result: {error:?}"),
|
||||
pc,
|
||||
)
|
||||
})?;
|
||||
self.inline_values(out.as_slice(), meta.ret_layout).map_err(|_| {
|
||||
self.trap(
|
||||
TRAP_INVALID_INTRINSIC,
|
||||
op as u16,
|
||||
"Invalid intrinsic result ownership".into(),
|
||||
pc,
|
||||
)
|
||||
})?;
|
||||
self.operand_stack.truncate(base);
|
||||
for value in out.into_values() {
|
||||
self.operand_stack.push(value).expect("bounded result replaces admitted inputs");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user