feat(vm): add reference identity and deterministic value hashing

This commit is contained in:
2026-10-23 14:29:00 +01:00
parent 57992cf214
commit 69ad1931de
41 changed files with 2521 additions and 121 deletions
+91 -2
View File
@@ -235,12 +235,13 @@ pub type IntrinsicHostImplementation =
pub enum IntrinsicImplementation {
Host(IntrinsicHostImplementation),
VmOwned,
BorrowedInline(crate::IntrinsicInlineImplementation),
}
impl IntrinsicImplementation {
pub fn host_callback(self) -> Option<IntrinsicHostImplementation> {
match self {
Self::Host(f) => Some(f),
Self::VmOwned => None,
Self::VmOwned | Self::BorrowedInline(_) => None,
}
}
}
@@ -249,12 +250,14 @@ impl IntrinsicImplementation {
pub enum IntrinsicCostModel {
Fixed,
RandomRejectionV1,
ArgumentUtf8BytesV1,
}
impl IntrinsicCostModel {
pub const fn name(self) -> &'static str {
match self {
Self::Fixed => "fixed",
Self::RandomRejectionV1 => "random_rejection_v1",
Self::ArgumentUtf8BytesV1 => "argument_utf8_bytes_v1",
}
}
}
@@ -266,11 +269,14 @@ pub struct IntrinsicMeta {
pub name: &'static str,
pub version: u16,
pub arg_layout: &'static [AbiType],
pub arg_nullable: &'static [bool],
pub ret_layout: &'static [AbiType],
pub parameter_names: &'static [&'static str],
/// Full fixed cost, or base cost for a variable model.
pub cost_cycles: u64,
pub cost_model: IntrinsicCostModel,
pub cost_arg_index: Option<usize>,
pub cost_per_byte: u64,
pub attempt_cost_cycles: u64,
pub max_attempts: u32,
pub mutates_guest_state: bool,
@@ -300,7 +306,16 @@ impl IntrinsicMeta {
&self,
values: &[Value],
) -> Result<(), IntrinsicExecutionError> {
Self::validate_values(values, self.arg_layout)
Self::validate_values(values, self.arg_layout)?;
for (index, (value, nullable)) in values.iter().zip(self.arg_nullable).enumerate() {
if matches!(value, Value::Null) && !nullable {
return Err(IntrinsicExecutionError::TypeMismatch {
index,
expected: self.arg_layout[index],
});
}
}
Ok(())
}
fn validate_values(
@@ -563,10 +578,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "dot",
version: 1,
arg_layout: &VEC2_DOT_ARGS,
arg_nullable: &[false; VEC2_DOT_ARGS.len()],
ret_layout: &SINGLE_FLOAT_RET,
parameter_names: &["ax", "ay", "bx", "by"],
cost_cycles: 10,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -580,10 +598,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "length",
version: 1,
arg_layout: &VEC2_LENGTH_ARGS,
arg_nullable: &[false; VEC2_LENGTH_ARGS.len()],
ret_layout: &SINGLE_FLOAT_RET,
parameter_names: &["x", "y"],
cost_cycles: 40,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -597,10 +618,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "from_raw",
version: 1,
arg_layout: &INT_RET,
arg_nullable: &[false; INT_RET.len()],
ret_layout: &COLOR_RET,
parameter_names: &["raw"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -614,10 +638,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "rgb",
version: 1,
arg_layout: &COLOR_RGB_ARGS,
arg_nullable: &[false; COLOR_RGB_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &["r", "g", "b"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -631,10 +658,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "rgba",
version: 1,
arg_layout: &COLOR_RGBA_ARGS,
arg_nullable: &[false; COLOR_RGBA_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &["r", "g", "b", "a"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -648,10 +678,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "html_rgba",
version: 1,
arg_layout: &COLOR_HTML_RGBA_ARGS,
arg_nullable: &[false; COLOR_HTML_RGBA_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &["value"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -665,10 +698,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "gray_scale",
version: 1,
arg_layout: &INT_RET,
arg_nullable: &[false; INT_RET.len()],
ret_layout: &COLOR_RET,
parameter_names: &["value"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -682,10 +718,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "hex",
version: 1,
arg_layout: &COLOR_ARGS,
arg_nullable: &[false; COLOR_ARGS.len()],
ret_layout: &INT_RET,
parameter_names: &["color"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -699,10 +738,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "alpha",
version: 1,
arg_layout: &COLOR_ARGS,
arg_nullable: &[false; COLOR_ARGS.len()],
ret_layout: &INT_RET,
parameter_names: &["color"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -716,10 +758,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "black",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -733,10 +778,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "white",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -750,10 +798,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "red",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -767,10 +818,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "green",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -784,10 +838,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "blue",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -801,10 +858,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "yellow",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -818,10 +878,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "orange",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -835,10 +898,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "indigo",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -852,10 +918,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "gray",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -869,10 +938,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "cyan",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -886,10 +958,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "magenta",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -903,10 +978,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "transparent",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -944,6 +1022,7 @@ pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'stati
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
}
@@ -953,6 +1032,7 @@ pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
.find(|meta| meta.id == id)
}
@@ -1254,9 +1334,17 @@ pub(crate) fn metered_lookup_intrinsic(
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
{
budget.work(1)?;
if meta.id == id {
budget.work(meta.arg_slots() + 1)?;
meta.validate_descriptor().map_err(|_| {
prometeu_bytecode::load_limits::LoadFailure::new(
prometeu_bytecode::load_limits::LoadCode::InvalidCode,
prometeu_bytecode::load_limits::LoadReason::NativeId,
)
})?;
return Ok(Some(meta));
}
}
@@ -1270,6 +1358,7 @@ pub fn intrinsic_catalog() -> impl Iterator<Item = &'static IntrinsicMeta> {
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
}
pub fn builtin_constant_catalog() -> impl Iterator<Item = &'static BuiltinConstMeta> {
BUILTIN_CONSTS.iter()