feat(vm): add reference identity and deterministic value hashing
This commit is contained in:
@@ -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()
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user