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
@@ -379,18 +379,26 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
emit_u16(op as u16, &mut out);
emit_u32(parse_u32_any(ops)?, &mut out);
}
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" => {
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" | "REF_IDENTITY_HASH" => {
if ops.is_empty() {
return Err(AsmError::MissingOperand(line.into()));
}
let opcode = match mn {
"DYN_PACK" => CoreOpCode::DynPack,
"DYN_UNPACK" => CoreOpCode::DynUnpack,
"REF_IDENTITY_HASH" => CoreOpCode::RefIdentityHash,
_ => CoreOpCode::DynCanUnpack,
};
emit_u16(opcode as u16, &mut out);
emit_u32(parse_u32_any(ops)?, &mut out);
}
"REF_IDENTITY_EQ" => {
let (a, b) = parse_keyvals(ops)?;
let (left, right) = parse_pair(a, "left", b, "right")?;
emit_u16(CoreOpCode::RefIdentityEq as u16, &mut out);
emit_u32(left, &mut out);
emit_u32(right, &mut out);
}
"REF_IS_EXACT" | "REF_IMPLEMENTS" | "REF_TO_TRAIT" | "REF_CAST" => {
if ops.is_empty() {
return Err(AsmError::MissingOperand(line.into()));
@@ -21,9 +21,9 @@
//! - Floats use exact bit-pattern format to prevent locale/rounding differences.
//! - Ordering is the canonical decode order; no address prefixes are emitted.
use crate::DecodeError;
use crate::decode_next;
use crate::isa::core::{CoreOpCode, CoreOpCodeSpecExt};
use crate::DecodeError;
fn fmt_f64_bits(bits: u64) -> String {
// Fixed-width 16 hex digits, lowercase.
@@ -65,6 +65,7 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
| CoreOpCode::Hostcall
| CoreOpCode::DynPack
| CoreOpCode::DynUnpack
| CoreOpCode::RefIdentityHash
| CoreOpCode::DynCanUnpack
| CoreOpCode::StructNew
| CoreOpCode::ArrayNew
@@ -88,6 +89,11 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
let field = u32::from_le_bytes(imm[4..].try_into().unwrap());
format!("type={}, field={}", ty, field)
}
CoreOpCode::RefIdentityEq => {
let left = u32::from_le_bytes(imm[..4].try_into().unwrap());
let right = u32::from_le_bytes(imm[4..].try_into().unwrap());
format!("left={}, right={}", left, right)
}
CoreOpCode::RefIsExact
| CoreOpCode::RefImplements
| CoreOpCode::RefToTrait
@@ -192,6 +192,10 @@ pub enum OpCode {
RefToTrait = 0x95,
/// REF_CAST: source:TypeId, target:TypeId; requires validated Registry metadata.
RefCast = 0x96,
/// Query a complete typed reference identity; source TypeId.
RefIdentityHash = 0x97,
/// Compare two complete typed references; left/right TypeIds.
RefIdentityEq = 0x98,
// Registry-backed collection operations. cycles() is their base component.
StructNew = 0xA0,
@@ -278,6 +282,8 @@ impl TryFrom<u16> for OpCode {
0x94 => Ok(OpCode::RefImplements),
0x95 => Ok(OpCode::RefToTrait),
0x96 => Ok(OpCode::RefCast),
0x97 => Ok(OpCode::RefIdentityHash),
0x98 => Ok(OpCode::RefIdentityEq),
0xA0 => Ok(OpCode::StructNew),
0xA1 => Ok(OpCode::StructGet),
0xA2 => Ok(OpCode::StructSet),
@@ -360,6 +366,8 @@ impl OpCode {
OpCode::RefImplements => 4,
OpCode::RefToTrait => 5,
OpCode::RefCast => 4,
OpCode::RefIdentityHash => 8,
OpCode::RefIdentityEq => 4,
OpCode::StructNew => 8,
OpCode::StructGet => 3,
OpCode::StructSet => 4,
@@ -596,6 +596,26 @@ impl OpCodeSpecExt for OpCode {
may_trap: true,
is_safepoint: false,
},
OpCode::RefIdentityHash => OpcodeSpec {
name: "REF_IDENTITY_HASH",
imm_bytes: 4,
pops: None,
pushes: None,
is_branch: false,
is_terminator: false,
may_trap: true,
is_safepoint: false,
},
OpCode::RefIdentityEq => OpcodeSpec {
name: "REF_IDENTITY_EQ",
imm_bytes: 8,
pops: None,
pushes: None,
is_branch: false,
is_terminator: false,
may_trap: true,
is_safepoint: false,
},
OpCode::StructNew => OpcodeSpec {
name: "STRUCT_NEW",
imm_bytes: 4,
@@ -52,6 +52,19 @@ pub enum TypeKind {
Trait(Vec<MethodDecl>),
Fn(Signature),
}
impl TypeKind {
/// Categories supported by identity queries, not by legacy REF_* conversions.
pub fn is_identity_reference(&self) -> bool {
matches!(
self,
Self::Struct(_)
| Self::Array(_)
| Self::Fn(_)
| Self::Trait(_)
| Self::Builtin(BuiltinId::RANDOM)
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeDecl {
pub name: Option<String>,
@@ -133,6 +133,23 @@ impl DecodedInstr<'_> {
};
(source_layout.storage.slots, pushes)
}
Op::RefIdentityHash | Op::RefIdentityEq => {
let (left, right) = if self.opcode == Op::RefIdentityHash {
(self.imm_u32().map_err(|_| malformed())?, None)
} else {
let (a, b) = self.imm_u32x2().map_err(|_| malformed())?;
(a, Some(b))
};
let mut pops = 0;
for id in std::iter::once(left).chain(right) {
let (decl, layout) = type_info(id)?;
if !decl.kind.is_identity_reference() {
return Err(bad_category(id));
}
pops += layout.storage.slots;
}
(pops, 1)
}
Op::FieldsPublish => {
let schema = self.imm_u32().map_err(|_| malformed())?;
let (decl, _) = type_info(schema)?;
@@ -210,7 +227,11 @@ impl DecodedInstr<'_> {
.get(field as usize)
.ok_or_else(|| bad_category(id).member(field))?;
let w = registry.type_layout(f.type_id).unwrap().storage.slots;
if op == Op::StructGet { (1, w) } else { (1 + w, 0) }
if op == Op::StructGet {
(1, w)
} else {
(1 + w, 0)
}
}
}
} else {
@@ -34,6 +34,8 @@ fn all_typed_instructions_match_independent_bytes_and_costs() {
("REF_IMPLEMENTS source=16909060, target=84281096", &[0x94, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
("REF_TO_TRAIT source=16909060, target=84281096", &[0x95, 0, 4, 3, 2, 1, 8, 7, 6, 5], 5),
("REF_CAST source=16909060, target=84281096", &[0x96, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
("REF_IDENTITY_HASH 16909060", &[0x97, 0, 4, 3, 2, 1], 8),
("REF_IDENTITY_EQ left=16909060, right=84281096", &[0x98, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
];
for &(text, bytes, cycles) in cases {
assert_eq!(assemble(text).unwrap(), bytes);
@@ -49,8 +51,8 @@ fn all_typed_instructions_match_independent_bytes_and_costs() {
}
}
assert!(matches!(
decode_next(0, &[0x97, 0]),
Err(DecodeError::UnknownOpcode { opcode: 0x97, .. })
decode_next(0, &[0x99, 0]),
Err(DecodeError::UnknownOpcode { opcode: 0x99, .. })
));
}
#[test]
@@ -68,6 +70,8 @@ fn slot_effects_resolve_semantic_widths_and_reference_categories() {
("REF_TO_TRAIT source=4, target=6", 1, 2),
("REF_TO_TRAIT source=6, target=6", 2, 2),
("REF_CAST source=6, target=4", 2, 1),
("REF_IDENTITY_HASH 6", 2, 1),
("REF_IDENTITY_EQ left=6, right=4", 3, 1),
] {
let bytes = assemble(text).unwrap();
assert_eq!(
@@ -94,6 +98,9 @@ fn invalid_type_immediates_are_contextual_load_errors() {
"REF_IS_EXACT source=4, target=6",
"REF_TO_TRAIT source=4, target=0",
"REF_IMPLEMENTS source=0, target=6",
"REF_IDENTITY_HASH 0",
"REF_IDENTITY_HASH 3",
"REF_IDENTITY_EQ left=4, right=1",
] {
let bytes = assemble(text).unwrap();
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(&registry).unwrap_err();
@@ -114,6 +121,10 @@ fn assembler_rejects_missing_overflowing_or_ambiguous_type_ids() {
"REF_CAST source=4",
"REF_CAST source=4, target=5, target=6",
"REF_CAST source=4, trait=6",
"REF_IDENTITY_HASH",
"REF_IDENTITY_HASH -1",
"REF_IDENTITY_EQ left=4",
"REF_IDENTITY_EQ left=4, target=5",
] {
assert!(assemble(text).is_err(), "{text}");
}
+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()
+10 -3
View File
@@ -16,14 +16,15 @@ fn atom(t: AbiType) -> String {
}
pub fn intrinsic_catalog_csv() -> String {
let mut out = String::from(
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts\n",
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts,schema_version,arg_nullable,invocation_model,scratch_model,context_access,cost_arg_index,cost_per_byte\n",
);
let mut entries: Vec<_> = intrinsic_catalog().collect();
entries.sort_by_key(|m| m.id);
for m in entries {
m.validate_descriptor().expect("valid canonical intrinsic descriptor");
writeln!(
&mut out,
"0x{:04X},{},{}.{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
"0x{:04X},{},{}.{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},2,{},{},{},{},{},{}",
m.id,
m.id,
m.owner,
@@ -42,7 +43,13 @@ pub fn intrinsic_catalog_csv() -> String {
m.mutates_guest_state,
m.cost_model.name(),
m.attempt_cost_cycles,
m.max_attempts
m.max_attempts,
m.arg_nullable.iter().map(bool::to_string).collect::<Vec<_>>().join("|"),
m.invocation_model(),
m.scratch_model(),
m.context_access(),
m.cost_arg_index.map_or_else(String::new, |i| i.to_string()),
m.cost_per_byte
)
.unwrap();
}
@@ -0,0 +1,66 @@
use crate::{
AbiType, BuiltinScalarType as S, IntrinsicCostModel as C, IntrinsicExecutionError as E,
IntrinsicImplementation as I, IntrinsicMeta, IntrinsicResults,
};
use prometeu_bytecode::Value;
const fn entry(
id: u32,
name: &'static str,
abi: AbiType,
nullable: bool,
handler: crate::IntrinsicInlineImplementation,
) -> IntrinsicMeta {
IntrinsicMeta {
id,
owner: "hash",
name,
version: 1,
arg_layout: match abi {
AbiType::Scalar(S::Int64) => &[AbiType::Scalar(S::Int64)],
AbiType::Scalar(S::Float) => &[AbiType::Scalar(S::Float)],
AbiType::Scalar(S::String) => &[AbiType::Scalar(S::String)],
_ => panic!("unsupported hash input ABI"),
},
ret_layout: &[AbiType::Scalar(S::Int64)],
arg_nullable: if nullable { &[true] } else { &[false] },
parameter_names: &["value"],
cost_cycles: 8,
cost_model: if matches!(abi, AbiType::Scalar(S::String)) {
C::ArgumentUtf8BytesV1
} else {
C::Fixed
},
cost_arg_index: if matches!(abi, AbiType::Scalar(S::String)) { Some(0) } else { None },
cost_per_byte: if matches!(abi, AbiType::Scalar(S::String)) { 1 } else { 0 },
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
deterministic: true,
may_allocate: false,
implementation: I::BorrowedInline(handler),
}
}
pub(crate) const HASH_INTRINSICS: [IntrinsicMeta; 3] = [
entry(0x4100, "i64", AbiType::Scalar(S::Int64), false, hash_i64),
entry(0x4101, "float", AbiType::Scalar(S::Float), false, hash_float),
entry(0x4102, "string", AbiType::Scalar(S::String), true, hash_string),
];
fn hash_i64(args: &[Value], out: &mut IntrinsicResults) -> Result<(), E> {
let Value::Int64(value) = args[0] else { unreachable!("exact validated Int64") };
out.push(Value::Int64(crate::hashing::xxh64(&value.to_le_bytes()) as i64))
}
fn hash_float(args: &[Value], out: &mut IntrinsicResults) -> Result<(), E> {
let Value::Float(value) = args[0] else { unreachable!("exact validated Float") };
crate::numeric::check_inputs(&[value])?;
let bits = if value == 0.0 { 0 } else { value.to_bits() };
out.push(Value::Int64(crate::hashing::xxh64(&bits.to_le_bytes()) as i64))
}
fn hash_string(args: &[Value], out: &mut IntrinsicResults) -> Result<(), E> {
let hash = match &args[0] {
Value::Null => 0,
Value::String(s) => crate::hashing::xxh64(s.as_bytes()) as i64,
_ => unreachable!("exact validated nullable String"),
};
out.push(Value::Int64(hash))
}
+131
View File
@@ -0,0 +1,131 @@
//! Hash-v1: portable XXH64, seed zero, over explicit machine byte encodings.
use prometeu_bytecode::HeapRef;
const P1: u64 = 0x9E3779B185EBCA87;
const P2: u64 = 0xC2B2AE3D27D4EB4F;
const P3: u64 = 0x165667B19E3779F9;
const P4: u64 = 0x85EBCA77C2B2AE63;
const P5: u64 = 0x27D4EB2F165667C5;
fn word(bytes: &[u8]) -> u64 {
u64::from_le_bytes(bytes[..8].try_into().expect("complete XXH64 lane"))
}
fn round(acc: u64, lane: u64) -> u64 {
acc.wrapping_add(lane.wrapping_mul(P2)).rotate_left(31).wrapping_mul(P1)
}
fn merge(acc: u64, lane: u64) -> u64 {
(acc ^ round(0, lane)).wrapping_mul(P1).wrapping_add(P4)
}
pub(crate) fn xxh64(mut bytes: &[u8]) -> u64 {
let length = bytes.len() as u64;
let mut hash = if bytes.len() >= 32 {
let mut acc = [P1.wrapping_add(P2), P2, 0, 0u64.wrapping_sub(P1)];
while bytes.len() >= 32 {
for (index, value) in acc.iter_mut().enumerate() {
*value = round(*value, word(&bytes[index * 8..]));
}
bytes = &bytes[32..];
}
let mut hash = acc[0]
.rotate_left(1)
.wrapping_add(acc[1].rotate_left(7))
.wrapping_add(acc[2].rotate_left(12))
.wrapping_add(acc[3].rotate_left(18));
for value in acc {
hash = merge(hash, value);
}
hash
} else {
P5
};
hash = hash.wrapping_add(length);
while bytes.len() >= 8 {
hash ^= round(0, word(bytes));
hash = hash.rotate_left(27).wrapping_mul(P1).wrapping_add(P4);
bytes = &bytes[8..];
}
if bytes.len() >= 4 {
hash ^= u64::from(u32::from_le_bytes(bytes[..4].try_into().unwrap())).wrapping_mul(P1);
hash = hash.rotate_left(23).wrapping_mul(P2).wrapping_add(P3);
bytes = &bytes[4..];
}
for byte in bytes {
hash ^= u64::from(*byte).wrapping_mul(P5);
hash = hash.rotate_left(11).wrapping_mul(P1);
}
hash ^= hash >> 33;
hash = hash.wrapping_mul(P2);
hash ^= hash >> 29;
hash = hash.wrapping_mul(P3);
hash ^ (hash >> 32)
}
pub(crate) fn reference(value: Option<HeapRef>) -> i64 {
value.map_or(0, |reference| {
let mut bytes = [0; 8];
bytes[..4].copy_from_slice(&reference.index.to_le_bytes());
bytes[4..].copy_from_slice(&reference.generation.to_le_bytes());
xxh64(&bytes) as i64
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_all_independent_c_reference_vectors() {
let mut count = 0;
for row in include_str!("../tests/fixtures/hash-conformance.tsv").lines().skip(1) {
let f: Vec<_> = row.split('\t').collect();
let expected = u64::from_str_radix(f[2], 16).unwrap();
let actual = match f[0] {
"bytes" | "string" => {
let bytes: Vec<_> = (0..f[1].len())
.step_by(2)
.map(|i| u8::from_str_radix(&f[1][i..i + 2], 16).unwrap())
.collect();
xxh64(&bytes)
}
"i64" => xxh64(&f[1].parse::<i64>().unwrap().to_le_bytes()),
"float" => {
let bits = u64::from_str_radix(f[1], 16).unwrap();
let bits = if f64::from_bits(bits) == 0.0 { 0 } else { bits };
xxh64(&bits.to_le_bytes())
}
"reference" => {
let (index, generation) = f[1].split_once(',').unwrap();
reference(Some(HeapRef {
index: index.parse().unwrap(),
generation: generation.parse().unwrap(),
})) as u64
}
"null" => reference(None) as u64,
kind => panic!("unknown vector {kind}"),
};
assert_eq!(actual, expected, "{row}");
count += 1;
}
assert_eq!(count, 118);
}
#[test]
fn sequential_handles_spread_and_bucket_collisions_do_not_imply_identity() {
let mut buckets = [0; 256];
let mut first = [None; 256];
let mut collisions = 0;
for index in 1..=16384 {
let handle = HeapRef { index, generation: 1 };
let bucket = reference(Some(handle)) as u64 as usize & 255;
buckets[bucket] += 1;
if let Some(previous) = first[bucket] {
assert_ne!(previous, handle);
collisions += 1;
} else {
first[bucket] = Some(handle);
}
}
assert!(buckets.iter().all(|&count| (32..=100).contains(&count)), "{buckets:?}");
assert_eq!(collisions, 16384 - 256);
}
}
@@ -20,10 +20,13 @@ const fn query(
name,
version: 1,
arg_layout: &[],
arg_nullable: &[],
ret_layout,
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,
@@ -0,0 +1,143 @@
//! The native handler variant and descriptor jointly define the invocation contract.
use crate::{
AbiType, BuiltinScalarType, IntrinsicCostModel, IntrinsicExecutionError as E,
IntrinsicImplementation as I, IntrinsicMeta,
};
use prometeu_bytecode::Value;
pub type IntrinsicInlineImplementation = fn(&[Value], &mut IntrinsicResults) -> Result<(), E>;
const INLINE_SLOTS: usize = 3;
/// Fixed native staging only; no heap buffer or mutable execution context.
#[derive(Debug)]
pub struct IntrinsicResults {
values: [Value; INLINE_SLOTS],
len: usize,
limit: usize,
overflowed: bool,
}
impl IntrinsicResults {
pub(crate) fn new(limit: usize) -> Self {
assert!(limit <= INLINE_SLOTS);
Self { values: [const { Value::Null }; INLINE_SLOTS], len: 0, limit, overflowed: false }
}
pub fn push(&mut self, value: Value) -> Result<(), E> {
if self.len >= self.limit {
self.overflowed = true;
return Err(E::ArityMismatch { expected: self.limit, got: self.len + 1 });
}
self.values[self.len] = value;
self.len += 1;
Ok(())
}
pub(crate) fn validate(&self, meta: &IntrinsicMeta) -> Result<(), E> {
if self.overflowed {
return Err(E::ArityMismatch { expected: self.limit, got: self.limit + 1 });
}
meta.validate_result_values(&self.values[..self.len])
}
pub(crate) fn as_slice(&self) -> &[Value] {
&self.values[..self.len]
}
pub(crate) fn into_values(self) -> impl Iterator<Item = Value> {
self.values.into_iter().take(self.len)
}
}
impl IntrinsicMeta {
pub fn invocation_model(&self) -> &'static str {
match self.implementation {
I::Host(_) => "host_buffered_v1",
I::VmOwned => "vm_owned_v1",
I::BorrowedInline(_) => "borrowed_inline_v1",
}
}
pub fn scratch_model(&self) -> &'static str {
match self.implementation {
I::BorrowedInline(_) => "none",
_ => "existing_contract_v1",
}
}
pub fn context_access(&self) -> &'static str {
match self.implementation {
I::Host(_) => "host",
I::VmOwned => "vm_state",
I::BorrowedInline(_) => "none",
}
}
/// Derivation from the handler variant makes incompatible profile claims unrepresentable.
pub fn validate_descriptor(&self) -> Result<(), &'static str> {
if self.arg_nullable.len() != self.arg_slots()
|| self.parameter_names.len() != self.arg_slots()
{
return Err("argument metadata widths disagree");
}
for abi in self.arg_layout.iter().chain(self.ret_layout) {
if !matches!(abi, AbiType::Scalar(_))
&& !matches!(abi, AbiType::Builtin(key) if (key.name == "Random" || key.name == "color") && key.version == 1)
{
return Err("unsupported intrinsic ABI layout");
}
}
for (abi, nullable) in self.arg_layout.iter().zip(self.arg_nullable) {
let permits_null = matches!(abi, AbiType::Scalar(BuiltinScalarType::String))
|| matches!(abi, AbiType::Builtin(key) if key.name == "Random" && key.version == 1);
if *nullable && !permits_null {
return Err("non-nullable ABI carrier declared nullable");
}
}
match self.cost_model {
IntrinsicCostModel::Fixed => {
if self.cost_arg_index.is_some()
|| self.cost_per_byte != 0
|| self.attempt_cost_cycles != 0
|| self.max_attempts != 0
{
return Err("fixed cost has variable parameters");
}
}
IntrinsicCostModel::RandomRejectionV1 => {
if !matches!(self.implementation, I::VmOwned)
|| self.cost_arg_index.is_some()
|| self.cost_per_byte != 0
|| self.attempt_cost_cycles == 0
|| self.max_attempts == 0
{
return Err("invalid random rejection cost parameters");
}
}
IntrinsicCostModel::ArgumentUtf8BytesV1 => {
if !matches!(self.implementation, I::BorrowedInline(_))
|| self.cost_arg_index.and_then(|i| self.arg_layout.get(i))
!= Some(&AbiType::Scalar(BuiltinScalarType::String))
|| self.cost_per_byte == 0
|| self.attempt_cost_cycles != 0
|| self.max_attempts != 0
{
return Err("invalid String byte-cost parameters");
}
}
}
if matches!(self.implementation, I::BorrowedInline(_))
&& (self.ret_slots() > INLINE_SLOTS
|| self.ret_slots() > self.arg_slots()
|| self.may_allocate
|| self.mutates_guest_state
|| !self.deterministic)
{
return Err("handler violates bounded invocation capabilities");
}
Ok(())
}
/// None is checked cost overflow. Wrong/missing carriers have no inferred length.
pub(crate) fn attempted_cost(&self, args: &[Value]) -> Option<u64> {
if self.cost_model != IntrinsicCostModel::ArgumentUtf8BytesV1 {
return Some(self.cost_cycles);
}
let bytes = match self.cost_arg_index.and_then(|i| args.get(i)) {
Some(Value::String(value)) => value.len() as u64,
_ => 0,
};
bytes.checked_mul(self.cost_per_byte)?.checked_add(self.cost_cycles)
}
}
+4
View File
@@ -2,7 +2,11 @@
extern crate self as prometeu_vm;
mod builtins;
mod catalog;
mod hash_intrinsics;
mod hashing;
mod intrinsic_contract;
pub use catalog::{builtin_constant_catalog_csv, intrinsic_catalog_csv};
pub use intrinsic_contract::{IntrinsicInlineImplementation, IntrinsicResults};
mod call_frame;
mod input_intrinsics;
mod math_intrinsics;
@@ -20,10 +20,17 @@ const fn entry<const ID: u32>(
name,
version: 1,
arg_layout,
arg_nullable: match arg_layout.len() {
1 => &[false],
2 => &[false, false],
_ => &[false, false, false],
},
ret_layout,
parameter_names,
cost_cycles,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -21,6 +21,13 @@ const fn entry(
name,
version: 1,
arg_layout: args,
arg_nullable: if id == 0x4000 {
&[false]
} else if rejection {
&[true, false, false]
} else {
&[true]
},
ret_layout: ret,
parameter_names: names,
cost_cycles: cost,
@@ -29,6 +36,8 @@ const fn entry(
} else {
IntrinsicCostModel::Fixed
},
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: if rejection { 8 } else { 0 },
max_attempts: if rejection { crate::random::MAX_ATTEMPTS } else { 0 },
deterministic: true,
@@ -485,6 +485,10 @@ fn transfer(
group(&mut working[base..], registry, ty, (instruction.pc as u64) << 32, budget)?;
}
}
Op::RefIdentityHash => {
budget.work(1)?;
working[base] = Fact::Carrier(I64);
}
Op::RefCast | Op::RefToTrait => {
group(
&mut working[base..],
@@ -503,6 +507,7 @@ fn transfer(
| Op::And
| Op::Or
| Op::Not
| Op::RefIdentityEq
| Op::RefIsExact
| Op::RefImplements
| Op::DynCanUnpack => {
@@ -818,6 +823,21 @@ fn check(
return Err(mismatch(1));
}
}
Op::RefIdentityHash | Op::RefIdentityEq => {
let (left, right) = if instruction.opcode == Op::RefIdentityHash {
(instruction.imm_u32().unwrap(), None)
} else {
let (a, b) = instruction.imm_u32x2().unwrap();
(a, Some(b))
};
let lw = registry.type_layout(TypeId(left)).unwrap().storage.slots as usize;
let rw = right
.map_or(0, |id| registry.type_layout(TypeId(id)).unwrap().storage.slots as usize);
check_group(&working[end - lw - rw..end - rw], TypeId(left), registry, budget)?;
if let Some(id) = right {
check_group(&working[end - rw..end], TypeId(id), registry, budget)?;
}
}
Op::RefCast | Op::RefToTrait | Op::RefIsExact | Op::RefImplements => {
budget.work(1)?;
let ty = TypeId(instruction.imm_u32x2().unwrap().0);
@@ -830,7 +850,12 @@ fn check(
.unwrap();
let base = end - native.arg_slots();
for (part, abi) in native.arg_layout.iter().enumerate() {
require(working[base + part], abi_mask(*abi), part, registry, budget)?;
let accepted = if native.arg_nullable[part] {
abi_mask(*abi) | NULL
} else {
abi_mask(*abi) & !NULL
};
require(working[base + part], accepted, part, registry, budget)?;
}
}
Op::JmpIfTrue | Op::JmpIfFalse | Op::Not => {
@@ -221,6 +221,24 @@ pub(crate) fn unpack_dynamic(
// All reference operations validate complete source values before querying the
// underlying concrete object. Invalid handles/pairs never become false or null.
pub(crate) fn identity_reference(
registry: &Registry,
heap: &Heap,
source: TypeId,
values: &[Value],
) -> Result<Option<HeapRef>, ValueError> {
let kind = registry.declarations().types.get(source.0 as usize).ok_or(ValueError::Type)?;
if !kind.kind.is_identity_reference() {
return Err(ValueError::Type);
}
validate(registry, heap, source, values)?;
match values.first() {
Some(Value::Null) => Ok(None),
Some(Value::HeapRef(reference)) => Ok(Some(*reference)),
_ => Err(ValueError::Type),
}
}
fn reference(
registry: &Registry,
heap: &Heap,
@@ -7,6 +7,8 @@ mod collections_tests;
mod control;
mod dynamic;
mod gc;
mod identity;
mod intrinsic;
mod lifecycle;
mod loader;
mod native_arrays;
@@ -330,8 +332,10 @@ impl VirtualMachine {
}
let charged_math = if opcode == OpCode::Intrinsic {
lookup_intrinsic_by_id(instr.imm_u32().unwrap())
.filter(|m| m.owner == "vec2" || m.owner == "mathf" || m.owner.starts_with("math."))
lookup_intrinsic_by_id(instr.imm_u32().unwrap()).filter(|m| {
matches!(m.implementation, crate::IntrinsicImplementation::Host(_))
&& (m.owner == "vec2" || m.owner == "mathf" || m.owner.starts_with("math."))
})
} else {
None
};
@@ -955,6 +959,12 @@ impl VirtualMachine {
)
})?;
if matches!(
intrinsic.implementation,
crate::IntrinsicImplementation::BorrowedInline(_)
) {
return self.execute_inline_intrinsic(intrinsic, pc_at_intrinsic);
}
if matches!(intrinsic.implementation, crate::IntrinsicImplementation::VmOwned) {
return self.execute_random(intrinsic, pc_at_intrinsic);
}
@@ -1086,6 +1096,15 @@ impl VirtualMachine {
let (source, target) = instr.imm_u32x2().unwrap();
return self.execute_reference(opcode, source, target, start_pc as u32);
}
OpCode::RefIdentityHash | OpCode::RefIdentityEq => {
let (left, right) = if opcode == OpCode::RefIdentityHash {
(instr.imm_u32().unwrap(), None)
} else {
let (left, right) = instr.imm_u32x2().unwrap();
(left, Some(right))
};
return self.execute_identity(opcode, left, right, start_pc as u32);
}
OpCode::FieldsPublish => {
return self.execute_fields_publication(instr.imm_u32().unwrap(), start_pc as u32);
}
@@ -3910,3 +3929,7 @@ mod callable_tests;
#[cfg(feature = "resource-diagnostics")]
pub mod string_provenance_qualification;
#[cfg(test)]
#[path = "virtual_machine/identity_tests.rs"]
mod identity_tests;
@@ -0,0 +1,59 @@
use super::*;
use crate::value_contract;
use prometeu_bytecode::registry::TypeId;
impl VirtualMachine {
pub(super) fn execute_identity(
&mut self,
opcode: OpCode,
left: u32,
right: Option<u32>,
pc: u32,
) -> Result<(), LogicalFrameEndingReason> {
let image = self.validated_image.clone().ok_or_else(|| {
self.trap(
prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION,
opcode as u16,
"Identity instruction requires a validated Registry".into(),
pc,
)
})?;
let registry = image.registry();
let left = TypeId(left);
let right = right.map(TypeId);
let lw = registry.type_layout(left).unwrap().storage.slots as usize;
let rw = right.map_or(0, |ty| registry.type_layout(ty).unwrap().storage.slots as usize);
self.cycles += opcode.cycles() + (lw + rw) as u64;
if self.temporary_slots() < lw + rw {
return Err(self.trap(
TRAP_STACK_UNDERFLOW,
opcode as u16,
"Incomplete identity operands".into(),
pc,
));
}
let base = self.operand_stack.len() - lw - rw;
let first = value_contract::identity_reference(
registry,
&self.heap,
left,
&self.operand_stack[base..base + lw],
)
.map_err(|e| self.value_trap(e, opcode, pc))?;
let result = if let Some(ty) = right {
let second = value_contract::identity_reference(
registry,
&self.heap,
ty,
&self.operand_stack[base + lw..],
)
.map_err(|e| self.value_trap(e, opcode, pc))?;
Value::Boolean(first == second)
} else {
Value::Int64(crate::hashing::reference(first))
};
self.operand_stack.truncate(base);
self.operand_stack.push(result).expect("identity result replaces existing inputs");
Ok(())
}
}
@@ -0,0 +1,236 @@
use super::*;
#[path = "../../tests/support/identity.rs"]
mod support;
use crate::{
IntrinsicExecutionError, IntrinsicImplementation, IntrinsicMeta, IntrinsicResults,
hash_intrinsics::HASH_INTRINSICS,
};
use prometeu_bytecode::{HeapRef, RuntimeResource as Q, registry::TypeId};
use std::sync::Arc;
use support::*;
fn empty() -> VirtualMachine {
vm(&[("RET", sig(&[], &[]))], &[])
}
fn push(v: &mut VirtualMachine, values: &[Value]) {
for value in values {
v.operand_stack.push(value.clone()).unwrap();
}
}
fn usage(v: &VirtualMachine) -> Vec<u64> {
[
Q::GuestBytes,
Q::HandleSlots,
Q::AuxiliaryBytes,
Q::TemporaryBytes,
Q::StringBytes,
Q::StringAllocations,
]
.map(|q| v.budget.used(q))
.to_vec()
}
fn fault(result: Result<(), LogicalFrameEndingReason>, expected: u32) {
assert!(
matches!(result,Err(LogicalFrameEndingReason::Trap(ref t)) if t.code==expected),
"{result:?}"
);
}
fn hash(v: &mut VirtualMachine, ty: u32, values: &[Value]) -> i64 {
push(v, values);
v.execute_identity(OpCode::RefIdentityHash, ty, None, 0).unwrap();
let Value::Int64(value) = v.operand_stack.pop().unwrap() else { panic!() };
value
}
#[test]
fn identity_faults_preserve_complete_groups_and_charge_once_in_left_to_right_order() {
let mut v = empty();
let registry = v.validated_image.as_ref().unwrap().registry().clone();
let live = v.heap.allocate_typed_struct(registry.clone(), TypeId(5)).unwrap();
let wrong = v.heap.allocate_typed_struct(registry, TypeId(11)).unwrap();
let stale = HeapRef { index: 999999, generation: 1 };
for (left, right, values, code) in [
(5, None, vec![Value::HeapRef(stale)], TRAP_INVALID_HANDLE),
(5, None, vec![Value::HeapRef(wrong)], TRAP_TYPE),
(5, None, vec![Value::HeapRef(HeapRef::NULL)], TRAP_TYPE),
(6, None, vec![Value::HeapRef(live)], TRAP_TYPE),
(9, None, vec![Value::HeapRef(HeapRef::NULL)], TRAP_TYPE),
(10, None, vec![Value::HeapRef(HeapRef::NULL)], TRAP_INVALID_HANDLE),
(7, None, vec![Value::Null, Value::Int64(0)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Null], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Int64(1)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Int64(-1)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Int64(999)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(stale), Value::Int64(0)], TRAP_INVALID_HANDLE),
// A null left side still validates the complete right group.
(5, Some(7), vec![Value::Null, Value::HeapRef(live), Value::Int64(1)], TRAP_TYPE),
(5, Some(11), vec![Value::Null, Value::HeapRef(stale)], TRAP_INVALID_HANDLE),
// Both invalid: left TYPE takes precedence over right INVALID_HANDLE.
(5, Some(5), vec![Value::HeapRef(wrong), Value::HeapRef(stale)], TRAP_TYPE),
] {
v.operand_stack.truncate(8); // Entry's eight reserved local slots.
push(&mut v, &values);
let operands = v.operand_stack.as_slice().to_vec();
let before = usage(&v);
let cycles = v.cycles;
let op = if right.is_some() { OpCode::RefIdentityEq } else { OpCode::RefIdentityHash };
let width = if left == 7 { 2 } else { 1 } + right.map_or(0, |r| if r == 7 { 2 } else { 1 });
fault(v.execute_identity(op, left, right, 0), code);
assert_eq!(v.operand_stack.as_slice(), operands);
assert_eq!(usage(&v), before);
assert_eq!(v.cycles - cycles, op.cycles() + width);
}
v.operand_stack.truncate(8);
let before = v.cycles;
fault(v.execute_identity(OpCode::RefIdentityEq, 7, Some(7), 0), TRAP_STACK_UNDERFLOW);
assert_eq!(v.cycles - before, 8);
}
#[test]
fn gc_preserves_live_aliases_but_hashes_do_not_root_collected_handles() {
let mut v = empty();
let registry = v.validated_image.as_ref().unwrap().registry().clone();
let live = v.heap.allocate_typed_struct(registry.clone(), TypeId(5)).unwrap();
let dead = v.heap.allocate_typed_struct(registry.clone(), TypeId(5)).unwrap();
let live_hash = hash(&mut v, 5, &[Value::HeapRef(live)]);
let dead_hash = hash(&mut v, 5, &[Value::HeapRef(dead)]);
v.globals.push(Value::HeapRef(live)).unwrap();
v.globals.push(Value::Int64(dead_hash)).unwrap();
v.gc_alloc_threshold = 1;
v.handle_safepoint().unwrap();
assert!(v.heap.is_valid(live));
assert!(!v.heap.is_valid(dead));
assert_eq!(hash(&mut v, 7, &[Value::HeapRef(live), Value::Int64(0)]), live_hash);
let replacement = v.heap.allocate_typed_struct(registry, TypeId(5)).unwrap();
assert_eq!(replacement.index, dead.index);
assert_ne!(replacement.generation, dead.generation);
push(&mut v, &[Value::HeapRef(dead), Value::HeapRef(replacement)]);
fault(v.execute_identity(OpCode::RefIdentityEq, 5, Some(5), 0), TRAP_INVALID_HANDLE);
}
#[test]
fn new_queries_ignore_exhausted_guest_and_scratch_quotas_and_do_not_collect() {
let mut v = empty();
let registry = v.validated_image.as_ref().unwrap().registry().clone();
let live = v.heap.allocate_typed_struct(registry, TypeId(5)).unwrap();
push(&mut v, &[Value::HeapRef(live)]);
for q in [
Q::GuestBytes,
Q::HandleSlots,
Q::AuxiliaryBytes,
Q::TemporaryBytes,
Q::StringBytes,
Q::StringAllocations,
] {
v.budget.test_lower_limit(q, v.budget.used(q));
}
let before = usage(&v);
let allocations = v.heap.allocation_count();
v.gc_alloc_threshold = 1;
v.execute_identity(OpCode::RefIdentityHash, 5, None, 0).unwrap();
v.execute_inline_intrinsic(&HASH_INTRINSICS[0], 0).unwrap();
assert_eq!(v.heap.allocation_count(), allocations);
assert!(v.heap.is_valid(live)); // No root remains, but neither operation is a safepoint.
assert_eq!(usage(&v), before);
}
#[test]
fn inline_faults_preserve_float_bits_owners_and_attempted_cost() {
let mut v = empty();
for bits in [f64::INFINITY.to_bits(), f64::NEG_INFINITY.to_bits(), 0x7ff8000000001234] {
push(&mut v, &[Value::Float(f64::from_bits(bits))]);
let before = usage(&v);
let cycles = v.cycles;
fault(
v.execute_inline_intrinsic(&HASH_INTRINSICS[1], 0),
prometeu_bytecode::TRAP_NON_FINITE,
);
assert!(matches!(v.operand_stack.last(),Some(Value::Float(f)) if f.to_bits()==bits));
assert_eq!(usage(&v), before);
assert_eq!(v.cycles - cycles, 8);
v.operand_stack.pop();
}
let foreign: Arc<str> = Arc::from("owner outside image and session");
push(&mut v, &[Value::String(foreign.clone())]);
let refs = Arc::strong_count(&foreign);
let before = usage(&v);
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0), TRAP_TYPE);
assert_eq!(Arc::strong_count(&foreign), refs);
assert_eq!(usage(&v), before);
assert_eq!(v.cycles - cycles, 8 + foreign.len() as u64);
let mut overflow = HASH_INTRINSICS[2].clone();
overflow.cost_per_byte = u64::MAX;
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&overflow, 0), TRAP_RESOURCE_LIMIT);
assert_eq!(v.cycles - cycles, 8);
assert_eq!(Arc::strong_count(&foreign), refs);
v.operand_stack.pop();
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0), TRAP_STACK_UNDERFLOW);
assert_eq!(v.cycles - cycles, 8);
push(&mut v, &[Value::Int64(7)]);
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0), TRAP_TYPE);
assert_eq!(v.cycles - cycles, 8);
assert_eq!(v.operand_stack.last(), Some(&Value::Int64(7)));
}
#[test]
fn equal_string_content_hashes_across_image_session_and_import_owners() {
use prometeu_bytecode::model::ConstantPoolEntry as K;
let mut v = vm(
&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))],
&[K::String("same\0π".into())],
);
step(&mut v).unwrap();
let Value::String(image_owner) = v.operand_stack.last().unwrap().clone() else { panic!() };
v.strings = Some(crate::string_ledger::StringLedger::new(v.budget.clone()).unwrap());
let session = v.strings.as_ref().unwrap().concat("same", "\0π").unwrap();
let imported = v.strings.as_ref().unwrap().import("same\0π").unwrap();
assert!(!Arc::ptr_eq(&session, &imported));
let mut hashes = vec![];
v.operand_stack.pop();
for owner in [&image_owner, &session, &imported] {
push(&mut v, &[Value::String(owner.clone())]);
let before = usage(&v);
let count = Arc::strong_count(owner);
v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0).unwrap();
assert_eq!(usage(&v), before);
assert_eq!(Arc::strong_count(owner), count - 1);
hashes.push(v.operand_stack.pop().unwrap());
}
assert!(hashes.windows(2).all(|pair| pair[0] == pair[1]));
}
fn missing(_: &[Value], _: &mut IntrinsicResults) -> Result<(), IntrinsicExecutionError> {
Ok(())
}
fn wrong(_: &[Value], out: &mut IntrinsicResults) -> Result<(), IntrinsicExecutionError> {
out.push(Value::Boolean(true))
}
fn excess(_: &[Value], out: &mut IntrinsicResults) -> Result<(), IntrinsicExecutionError> {
out.push(Value::Int64(1))?;
let _ = out.push(Value::Int64(2)); // Ignoring the error cannot publish a partial result.
Ok(())
}
#[test]
fn shared_invocation_rejects_bad_outputs_without_consuming_original_string_owner() {
let mut v = empty();
let Value::String(content) = v.import_string_value(&Value::String(Arc::from("kept"))).unwrap()
else {
panic!()
};
push(&mut v, &[Value::String(content.clone())]);
let count = Arc::strong_count(&content);
let before = usage(&v);
for handler in [missing, wrong, excess] {
let mut meta: IntrinsicMeta = HASH_INTRINSICS[2].clone();
meta.owner = "test"; // The shared executor is independent of the hash namespace.
meta.implementation = IntrinsicImplementation::BorrowedInline(handler);
fault(v.execute_inline_intrinsic(&meta, 0), TRAP_INVALID_INTRINSIC);
assert_eq!(Arc::strong_count(&content), count);
assert_eq!(usage(&v), before);
assert!(matches!(v.operand_stack.last(),Some(Value::String(s)) if Arc::ptr_eq(s,&content)));
}
}
@@ -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(())
}
}
@@ -134,3 +134,96 @@ fn provenance_queries_allocate_nothing_at_all_qualification_sizes() {
assert_eq!(counts, (1, 1), "only snapshot Value storage allocates");
}
}
#[path = "support/identity.rs"]
mod identity_support;
#[test]
fn identity_and_borrowed_hash_instruction_regions_do_not_allocate() {
use prometeu_bytecode::model::ConstantPoolEntry as K;
for (name, initialize, operand, operation, steps, closure) in [
("hash_i64", "PUSH_I64 -73\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4100", 4, false),
("hash_float", "PUSH_CONST 0\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4101", 4, false),
("hash_string", "PUSH_CONST 1\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4102", 4, false),
(
"struct_hash",
"STRUCT_NEW 5\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 5",
4,
false,
),
(
"array_hash",
"PUSH_I64 0\nARRAY_NEW 6\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 6",
4,
false,
),
(
"fn_hash",
"MAKE_CLOSURE fn=1,type=9\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 9",
4,
true,
),
(
"random_hash",
"PUSH_I64 1\nINTRINSIC 0x4000\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 10",
4,
false,
),
(
"trait_hash",
"STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nSET_LOCAL 1\nSET_LOCAL 0",
"GET_LOCAL 0\nGET_LOCAL 1",
"REF_IDENTITY_HASH 7",
5,
false,
),
(
"struct_eq",
"STRUCT_NEW 5\nSET_LOCAL 0",
"GET_LOCAL 0\nGET_LOCAL 0",
"REF_IDENTITY_EQ left=5,right=5",
5,
false,
),
(
"trait_eq",
"STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nSET_LOCAL 1\nSET_LOCAL 0",
"GET_LOCAL 0\nGET_LOCAL 1\nGET_LOCAL 0\nGET_LOCAL 1",
"REF_IDENTITY_EQ left=7,right=7",
7,
false,
),
] {
let start = assemble(initialize).unwrap().len();
let text = format!("{initialize}\n{operand}\n{operation}\nPOP\nJMP {start}");
let mut bodies = vec![(text.as_str(), identity_support::sig(&[], &[]))];
if closure {
bodies.push(("RET", identity_support::sig(&[], &[])));
}
let mut vm =
identity_support::vm(&bodies, &[K::Float64(-0.0), K::String("π\0".repeat(512))]);
let mut host = HostContext::new(None);
while vm.pc() != start {
vm.step(&mut identity_support::Native, &mut host).unwrap();
}
for _ in 0..steps * 10 {
vm.step(&mut identity_support::Native, &mut host).unwrap();
}
let heap_allocations = vm.heap().allocation_count();
COUNTS.with(|c| c.set(Some((0, 0))));
for _ in 0..steps * 1000 {
vm.step(&mut identity_support::Native, &mut host).unwrap();
}
let counts = COUNTS.with(|c| c.replace(None).unwrap());
assert_eq!(counts, (0, 0), "{name}: allocations and deallocations");
assert_eq!(vm.heap().allocation_count(), heap_allocations, "{name}: guest allocations");
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,23 @@
# Hash-v1 oracle provenance
`hash-conformance.tsv` is generated by `hash-oracle.py` against the official C
XXH64 reference, not the Rust VM implementation. Ordinary tests read the checked-in
vectors and need no library, network or Python dependency.
Reference: https://github.com/Cyan4973/xxHash/blob/v0.8.3/xxhash.h
Header SHA-256: `17973c0dc49d9854ca26caa191f0e12f7a424b68858d9a78de3860d959d85e4b`.
The generator verifies both the header digest and `XXH_versionNumber()==803`.
Example reproduction on macOS, after downloading that pinned header:
```sh
clang -dynamiclib -O2 -x c -DXXH_IMPLEMENTATION -DXXH_STATIC_LINKING_ONLY xxhash.h -o libxxhash-oracle.dylib
python3 hash-oracle.py --header xxhash.h --library ./libxxhash-oracle.dylib
```
On Linux, use `-shared -fPIC` instead of `-dynamiclib`. The generator explicitly
encodes little-endian integers/reference components and finite Float bit patterns,
canonicalizes signed zeros, and uses seed zero. The null row is the machine
contract's direct zero result, not a hash of an empty message. Arbitrary `bytes`
rows exercise the internal algorithm; they do not expose guest byte strings.
@@ -0,0 +1,51 @@
"""Regenerate hash-v1 vectors using upstream XXH64, never the Rust executor."""
import argparse
import ctypes
import hashlib
from pathlib import Path
import struct
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--library", required=True, type=Path)
parser.add_argument("--header", required=True, type=Path)
parser.add_argument("--output", type=Path, default=Path(__file__).with_name("hash-conformance.tsv"))
args = parser.parse_args()
expected = "17973c0dc49d9854ca26caa191f0e12f7a424b68858d9a78de3860d959d85e4b"
assert hashlib.sha256(args.header.read_bytes()).hexdigest() == expected
library = ctypes.CDLL(str(args.library.resolve()))
assert library.XXH_versionNumber() == 803
digest = library.XXH64
digest.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_uint64]
digest.restype = ctypes.c_uint64
rows = ["kind\tinput\texpected_hash_hex"]
def emit(kind, value, data):
buffer = ctypes.create_string_buffer(data)
result = digest(ctypes.cast(buffer, ctypes.c_void_p), len(data), 0)
rows.append(f"{kind}\t{value}\t{result:016X}")
for size in list(range(71)) + [95, 96, 97, 127, 128, 129, 255, 256, 257, 1024, 4097]:
data = bytes((i * 131 + 17) & 255 for i in range(size))
emit("bytes", data.hex(), data)
for value in [0, 1, -1, 2, -2, -(2**63), 2**63 - 1, 2**53 + 1, 2**32]:
emit("i64", value, struct.pack("<q", value))
for bits in [0, 0x8000000000000000, 0x3FF0000000000000, 0xBFF0000000000000,
0x3FE0000000000000, 0xBFE0000000000000, 1, 0x8000000000000001,
0x000FFFFFFFFFFFFF, 0x0010000000000000,
0x7FEFFFFFFFFFFFFF, 0xFFEFFFFFFFFFFFFF, 0x4340000000000000]:
canonical = 0 if bits & 0x7FFFFFFFFFFFFFFF == 0 else bits
emit("float", f"{bits:016X}", struct.pack("<Q", canonical))
for value in ["", "hello", "\0", "ação 😀", "a" * 31, "a" * 32, "a" * 33, "z" * 1025]:
data = value.encode("utf-8")
emit("string", data.hex(), data)
for index, generation in [(0, 1), (1, 1), (1, 2), (1024, 1), (2**32-1, 2**32-1)]:
emit("reference", f"{index},{generation}", struct.pack("<II", index, generation))
rows.append("null\tnull\t0000000000000000")
args.output.write_text("\n".join(rows) + "\n", encoding="utf-8")
print(f"Generated {len(rows)-1} vectors with upstream XXH64 v0.8.3, seed 0.")
if __name__ == "__main__":
main()
@@ -0,0 +1,152 @@
#[path = "support/identity.rs"]
mod support;
use prometeu_bytecode::{Value, model::ConstantPoolEntry as K};
use prometeu_vm::{HostContext, LogicalFrameEndingReason as End};
use support::*;
#[test]
fn scalar_pbx_execution_matches_independent_c_reference_vectors() {
let mut count = 0;
for row in include_str!("fixtures/hash-conformance.tsv").lines().skip(1) {
let fields: Vec<_> = row.split('\t').collect();
let expected = u64::from_str_radix(fields[2], 16).unwrap() as i64;
let (value, id, bytes) = match fields[0] {
"i64" => (K::Int64(fields[1].parse().unwrap()), 0x4100, 0),
"float" => {
(K::Float64(f64::from_bits(u64::from_str_radix(fields[1], 16).unwrap())), 0x4101, 0)
}
"string" => {
let bytes = hex_bytes(fields[1]);
let size = bytes.len();
(K::String(String::from_utf8(bytes).unwrap()), 0x4102, size)
}
"null" => (K::Null, 0x4102, 0),
_ => continue,
};
let source = format!("PUSH_CONST 0\nINTRINSIC {id}\nRET");
let mut vm = vm(&[(&source, sig(&[], &[0]))], &[value]);
step(&mut vm).unwrap();
let report = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(report.reason, End::BudgetExhausted);
assert_eq!(report.cycles_used, 8 + bytes as u64, "{row}");
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int64(expected)], "{row}");
assert_eq!(run(&mut vm).reason, End::EndOfRom);
count += 1;
}
assert_eq!(count, 31);
}
#[test]
fn identities_survive_mutation_and_views_validate_implementation_zero() {
let source = "STRUCT_NEW 5\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 5\nSET_LOCAL 1\nGET_LOCAL 0\nPUSH_I64 73\nSTRUCT_SET type=5,field=0\nGET_LOCAL 0\nREF_IDENTITY_HASH 5\nGET_LOCAL 1\nEQ\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=7\nREF_IDENTITY_HASH 7\nGET_LOCAL 1\nEQ\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=7\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=8\nREF_IDENTITY_EQ left=7,right=8\nGET_LOCAL 0\nSTRUCT_NEW 5\nREF_IDENTITY_EQ left=5,right=5\nRET";
let mut vm = vm(&[(source, sig(&[], &[2, 2, 2, 2]))], &[]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(
&*vm.operand_stack_top(4),
&[Value::Boolean(false), Value::Boolean(true), Value::Boolean(true), Value::Boolean(true)]
);
}
#[test]
fn array_closure_and_random_have_instance_identity() {
for (source, bodies) in [
(
"PUSH_I64 1\nARRAY_NEW 6\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 6\nSET_LOCAL 1\nGET_LOCAL 0\nPUSH_I64 0\nPUSH_I64 42\nARRAY_SET 6\nGET_LOCAL 0\nREF_IDENTITY_HASH 6\nGET_LOCAL 1\nEQ\nRET",
false,
),
(
"PUSH_I64 42\nINTRINSIC 0x4000\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 10\nSET_LOCAL 1\nGET_LOCAL 0\nINTRINSIC 0x4001\nPOP\nGET_LOCAL 0\nREF_IDENTITY_HASH 10\nGET_LOCAL 1\nEQ\nRET",
false,
),
(
"MAKE_CLOSURE fn=1,type=9\nSET_LOCAL 0\nGET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_EQ left=9,right=9\nGET_LOCAL 0\nMAKE_CLOSURE fn=1,type=9\nREF_IDENTITY_EQ left=9,right=9\nNOT\nAND\nRET",
true,
),
] {
let mut functions = vec![(source, sig(&[], &[2]))];
if bodies {
functions.push(("RET", sig(&[], &[])));
}
let mut vm = vm(&functions, &[]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(&*vm.operand_stack_top(1), &[Value::Boolean(true)]);
}
}
#[test]
fn canonical_null_groups_compare_and_hash_without_instance_faults() {
let source = "PUSH_CONST 0\nPUSH_CONST 0\nREF_IDENTITY_HASH 7\nPUSH_CONST 0\nPUSH_CONST 0\nPUSH_CONST 0\nREF_IDENTITY_EQ left=6,right=7\nRET";
let mut vm = vm(&[(source, sig(&[], &[0, 2]))], &[K::Null]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(&*vm.operand_stack_top(2), &[Value::Boolean(true), Value::Int64(0)]);
}
#[test]
fn admission_rejects_ineligible_sources_and_bad_native_ids_even_when_dead() {
for instruction in [
"REF_IDENTITY_HASH 0",
"REF_IDENTITY_HASH 3",
"REF_IDENTITY_HASH 4",
"REF_IDENTITY_HASH 12",
"REF_IDENTITY_HASH 13",
"REF_IDENTITY_HASH 14",
"REF_IDENTITY_HASH 9999",
"REF_IDENTITY_EQ left=5,right=3",
"INTRINSIC 0x41FF",
] {
let text = format!("HALT\n{instruction}");
assert!(image(&[(&text, sig(&[], &[]))], &[]).is_err(), "{instruction}");
}
assert!(image(&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))], &[K::Null]).is_ok());
for id in [0x1103, 0x4100, 0x4101] {
let text = format!("PUSH_CONST 0\nINTRINSIC {id}\nPOP\nRET");
assert!(image(&[(&text, sig(&[], &[]))], &[K::Null]).is_err(), "{id}");
}
assert!(image(&[("PUSH_I64 3\nREF_IDENTITY_HASH 5\nRET", sig(&[], &[0]))], &[]).is_err());
}
#[test]
fn whole_instruction_budget_admission_preserves_zero_and_overshoot() {
let mut vm = vm(
&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))],
&[K::String("x".repeat(65))],
);
step(&mut vm).unwrap();
let before = vm.pc();
let zero = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(zero.cycles_used, 0);
assert_eq!(vm.pc(), before);
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(one.reason, End::BudgetExhausted);
assert_eq!(one.cycles_used, 73);
assert_eq!(one.steps_executed, 1);
}
#[test]
fn every_eligible_absence_has_zero_hash_and_exact_identity_instruction_cost() {
for (ty, width) in [(5, 1), (6, 1), (7, 2), (9, 1), (10, 1)] {
let source = format!("{}REF_IDENTITY_HASH {ty}\nRET", "PUSH_CONST 0\n".repeat(width));
let mut vm = vm(&[(&source, sig(&[], &[0]))], &[K::Null]);
for _ in 0..width {
step(&mut vm).unwrap();
}
let before = vm.pc();
let zero = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((zero.cycles_used, zero.steps_executed), (0, 0));
assert_eq!(vm.pc(), before);
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((one.cycles_used, one.steps_executed), (8 + width as u64, 1));
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int64(0)]);
}
for source in [
"PUSH_CONST 0\nSTRUCT_NEW 5\nREF_IDENTITY_EQ left=6,right=5\nRET",
"STRUCT_NEW 5\nSTRUCT_NEW 11\nREF_IDENTITY_EQ left=5,right=11\nRET",
] {
let mut vm = vm(&[(source, sig(&[], &[2]))], &[K::Null]);
step(&mut vm).unwrap();
step(&mut vm).unwrap();
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((one.cycles_used, one.steps_executed), (6, 1));
assert_eq!(&*vm.operand_stack_top(1), &[Value::Boolean(false)]);
}
}
@@ -0,0 +1,105 @@
use prometeu_bytecode::Value;
use prometeu_vm::{
AbiType, BuiltinScalarType as S, IntrinsicCostModel as C, intrinsic_catalog,
intrinsic_catalog_csv, lookup_intrinsic_by_id,
};
use std::collections::BTreeSet;
#[test]
fn schema_two_has_exact_columns_profiles_and_per_slot_nullable_lists() {
let csv = intrinsic_catalog_csv();
assert_eq!(csv, include_str!("../../../../docs/vm-arch/INTRINSICS.csv"));
let mut rows = csv.lines();
assert_eq!(
rows.next().unwrap(),
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts,schema_version,arg_nullable,invocation_model,scratch_model,context_access,cost_arg_index,cost_per_byte"
);
let mut ids = BTreeSet::new();
let mut keys = BTreeSet::new();
let mut previous = 0;
for row in rows {
let f: Vec<_> = row.split(',').collect();
assert_eq!(f.len(), 25);
assert_eq!(f[18], "2");
let id = f[1].parse::<u32>().unwrap();
assert!(id > previous);
previous = id;
assert!(ids.insert(id));
assert!(keys.insert((f[2], f[3])));
let meta = lookup_intrinsic_by_id(id).unwrap();
meta.validate_descriptor().unwrap();
assert_eq!(f[19].split('|').filter(|s| !s.is_empty()).count(), meta.arg_slots());
assert_eq!(f[20], meta.invocation_model());
assert_eq!(f[21], meta.scratch_model());
assert_eq!(f[22], meta.context_access());
if id < 0x4100 {
assert_eq!(f[21], "existing_contract_v1");
assert_eq!(f[23], "");
assert_eq!(f[24], "0");
assert_eq!(f[20], if f[4] == "random" { "vm_owned_v1" } else { "host_buffered_v1" });
for (abi, nullable) in meta.arg_layout.iter().zip(meta.arg_nullable) {
assert_eq!(*nullable, matches!(abi,AbiType::Builtin(key) if key.name=="Random"));
}
} else {
assert_eq!(&f[20..23], &["borrowed_inline_v1", "none", "none"]);
assert_eq!(meta.cost_cycles, 8);
assert_eq!(meta.ret_layout, &[AbiType::Scalar(S::Int64)]);
assert_eq!(meta.arg_nullable, &[id == 0x4102]);
}
}
assert_eq!(ids.len(), 108);
assert_eq!(intrinsic_catalog().count(), 108);
}
#[test]
fn incompatible_descriptors_fail_instead_of_gaining_implicit_capabilities() {
let original = *lookup_intrinsic_by_id(0x4102).unwrap();
for variant in 0..14 {
let mut meta = original;
match variant {
0 => meta.arg_nullable = &[],
1 => meta.parameter_names = &[],
2 => meta.cost_arg_index = None,
3 => meta.cost_arg_index = Some(1),
4 => meta.cost_per_byte = 0,
5 => meta.attempt_cost_cycles = 1,
6 => meta.max_attempts = 1,
7 => meta.may_allocate = true,
8 => meta.mutates_guest_state = true,
9 => meta.deterministic = false,
10 => meta.ret_layout = &[AbiType::Scalar(S::Int64); 4],
11 => meta.ret_layout = &[AbiType::Scalar(S::Int64); 2],
12 => meta.ret_layout = &[AbiType::DynamicPayload],
13 => meta.arg_layout = &[AbiType::DynamicTag],
_ => unreachable!(),
}
assert!(meta.validate_descriptor().is_err(), "variant {variant}");
}
let mut meta = *lookup_intrinsic_by_id(0x4100).unwrap();
meta.arg_nullable = &[true];
assert!(meta.validate_descriptor().is_err());
meta.arg_nullable = &[false];
meta.cost_arg_index = Some(0);
assert!(meta.validate_descriptor().is_err());
meta.cost_model = C::ArgumentUtf8BytesV1;
meta.cost_per_byte = 1;
assert!(meta.validate_descriptor().is_err());
meta.cost_model = C::RandomRejectionV1;
meta.cost_arg_index = None;
meta.cost_per_byte = 0;
meta.attempt_cost_cycles = 8;
meta.max_attempts = 64;
assert!(meta.validate_descriptor().is_err());
}
#[test]
fn nullability_does_not_relax_existing_string_or_numeric_arguments() {
for id in [0x4100, 0x4101, 0x1103] {
assert!(
lookup_intrinsic_by_id(id).unwrap().validate_argument_values(&[Value::Null]).is_err()
);
}
assert!(
lookup_intrinsic_by_id(0x4102).unwrap().validate_argument_values(&[Value::Null]).is_ok()
);
}
@@ -0,0 +1,114 @@
#![allow(dead_code)]
use prometeu_bytecode::{
assemble, load_limits::LoadBudget, model::ConstantPoolEntry, registry::*,
registry_codec::PbxModule,
};
use prometeu_vm::{
HostContext, HostReturn, LogicalFrameEndingReason, NativeInterface, ValidatedProgramImage,
VirtualMachine, VmInitError,
};
use std::sync::Arc;
pub struct Native;
impl NativeInterface for Native {
fn syscall(
&mut self,
_: u32,
_: &[prometeu_bytecode::Value],
_: &mut HostReturn,
_: &mut HostContext,
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
panic!("identity corpus uses no host syscall")
}
}
pub fn sig(parameters: &[u32], results: &[u32]) -> Signature {
Signature {
parameters: parameters
.iter()
.map(|&id| NamedType { type_id: TypeId(id), name: None })
.collect(),
results: results.iter().copied().map(TypeId).collect(),
}
}
pub fn types() -> Vec<TypeDecl> {
let field = || NamedType { type_id: TypeId(0), name: None };
vec![
TypeKind::Builtin(BuiltinId::INT64),
TypeKind::Builtin(BuiltinId::FLOAT),
TypeKind::Builtin(BuiltinId::BOOL),
TypeKind::Builtin(BuiltinId::STRING),
TypeKind::Builtin(BuiltinId::DYNAMIC),
TypeKind::Struct(vec![field()]),
TypeKind::Array(TypeId(0)),
TypeKind::Trait(vec![]),
TypeKind::Trait(vec![]),
TypeKind::Fn(sig(&[], &[])),
TypeKind::Builtin(BuiltinId::RANDOM),
TypeKind::Struct(vec![field()]),
TypeKind::Builtin(BuiltinId::COLOR),
TypeKind::Builtin(BuiltinId::VEC2),
TypeKind::Builtin(BuiltinId::PIXEL),
TypeKind::Builtin(BuiltinId::INT32),
]
.into_iter()
.map(|kind| TypeDecl { name: None, kind })
.collect()
}
pub fn image(
bodies: &[(&str, Signature)],
constants: &[ConstantPoolEntry],
) -> Result<Arc<ValidatedProgramImage>, VmInitError> {
let mut code = vec![];
let mut functions = vec![];
for (source, signature) in bodies {
let body = assemble(source).unwrap();
functions.push(FunctionDecl {
signature: signature.clone(),
local_slots: 8,
max_stack_slots: 64,
code_offset: code.len() as u32,
code_len: body.len() as u32,
..Default::default()
});
code.extend(body);
}
let mut budget = LoadBudget::new(Default::default());
let registry = Registry::resolve(
RegistryDeclarations {
types: types(),
functions,
implementations: vec![
ImplementationDecl { owner: TypeId(5), trait_id: TypeId(7), methods: vec![] },
ImplementationDecl { owner: TypeId(5), trait_id: TypeId(8), methods: vec![] },
],
},
code.len(),
&mut budget,
)
.unwrap();
let bytes = PbxModule {
registry: &registry,
code: &code,
constants,
debug: None,
exports: &[],
syscalls: &[],
}
.serialize(&mut budget)
.unwrap();
ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(Default::default()), 0)
}
pub fn vm(bodies: &[(&str, Signature)], constants: &[ConstantPoolEntry]) -> VirtualMachine {
let mut vm = VirtualMachine::from_image(image(bodies, constants).unwrap(), Default::default());
vm.prepare_boot_call().unwrap();
vm
}
pub fn run(vm: &mut VirtualMachine) -> prometeu_vm::BudgetReport {
vm.run_budget(1_000_000, &mut Native, &mut HostContext::new(None)).unwrap()
}
pub fn step(vm: &mut VirtualMachine) -> Result<(), LogicalFrameEndingReason> {
vm.step(&mut Native, &mut HostContext::new(None))
}
pub fn hex_bytes(value: &str) -> Vec<u8> {
(0..value.len()).step_by(2).map(|i| u8::from_str_radix(&value[i..i + 2], 16).unwrap()).collect()
}