feat(registry): checkpoint PLN-0176 foundations and validated partial integrations
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
use super::*;
|
||||
use prometeu_bytecode::load_limits::LoadLimits;
|
||||
use prometeu_bytecode::registry::{
|
||||
BuiltinId as B, FunctionDecl, NamedType, RegistryDeclarations, Signature, TypeDecl, TypeId,
|
||||
TypeKind,
|
||||
};
|
||||
|
||||
fn op(code: &mut Vec<u8>, op: Op) {
|
||||
code.extend_from_slice(&(op as u16).to_le_bytes());
|
||||
}
|
||||
fn arg(code: &mut Vec<u8>, op_code: Op, n: u32) {
|
||||
op(code, op_code);
|
||||
code.extend_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
fn registry(code: &[u8], functions: Vec<FunctionDecl>) -> Registry {
|
||||
Registry::resolve(
|
||||
RegistryDeclarations {
|
||||
types: vec![
|
||||
TypeDecl { name: None, kind: TypeKind::Builtin(B::INT32) },
|
||||
TypeDecl { name: None, kind: TypeKind::Builtin(B::VEC2) },
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Trait(vec![]) },
|
||||
],
|
||||
functions,
|
||||
implementations: vec![],
|
||||
},
|
||||
code.len(),
|
||||
&mut LoadBudget::new(Default::default()),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
fn function(code: &[u8]) -> FunctionDecl {
|
||||
FunctionDecl { code_len: code.len() as u32, max_stack_slots: 4, ..Default::default() }
|
||||
}
|
||||
fn check(code: &[u8], registry: &Registry) -> Result<(), E> {
|
||||
validate_registry_instructions(code, registry, 1, &mut LoadBudget::new(Default::default()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_opcodes_and_relative_jumps_are_resolved_in_declared_ranges() {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, Op::DynPack, 1);
|
||||
arg(&mut code, Op::DynUnpack, 1);
|
||||
arg(&mut code, Op::DynCanUnpack, 1);
|
||||
for (operation, target) in
|
||||
[(Op::RefIsExact, 2), (Op::RefCast, 2), (Op::RefImplements, 3), (Op::RefToTrait, 3)]
|
||||
{
|
||||
arg(&mut code, operation, 2);
|
||||
code.extend_from_slice(&(target as u32).to_le_bytes());
|
||||
}
|
||||
let start = code.len();
|
||||
arg(&mut code, Op::Jmp, 6);
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: start as u32, ..function(&code) },
|
||||
FunctionDecl {
|
||||
code_offset: start as u32,
|
||||
code_len: 6,
|
||||
max_stack_slots: 0,
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
);
|
||||
assert!(check(&code, &r).is_ok());
|
||||
// This phase is structural: stack heights/type groups still need the next analysis.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_bad_type_ids_and_categories_preserve_function_and_pc() {
|
||||
for (operation, id) in [(Op::DynPack, 99), (Op::RefCast, 0)] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, operation, id);
|
||||
if operation == Op::RefCast {
|
||||
code.extend_from_slice(&2_u32.to_le_bytes());
|
||||
}
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!(e.code, C::InvalidReference);
|
||||
assert_eq!(e.function, Some(0));
|
||||
assert_eq!(e.pc, Some(2));
|
||||
assert_eq!(e.section, Some(2));
|
||||
assert_eq!(e.referenced_id, Some(id));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jumps_reject_cross_range_and_mid_instruction_even_when_unreachable() {
|
||||
for (target, reason) in [(7, R::JumpTarget), (3, R::InstructionBoundary)] {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, Op::Jmp, target);
|
||||
op(&mut code, Op::Halt);
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: 6, ..function(&code) },
|
||||
FunctionDecl { code_offset: 6, code_len: 2, ..function(&code) },
|
||||
],
|
||||
);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!((e.code, e.reason, e.pc), (C::InvalidCode, reason, Some(0)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_bounds_use_derived_parameter_widths_and_u32_local_counts() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, Op::GetLocal, 70);
|
||||
let mut f = function(&code);
|
||||
f.signature = Signature {
|
||||
parameters: vec![NamedType { type_id: TypeId(1), name: None }],
|
||||
results: vec![],
|
||||
};
|
||||
f.local_slots = 69; // vec2 contributes two parameter slots, total 71.
|
||||
let r = registry(&code, vec![f.clone()]);
|
||||
assert!(check(&code, &r).is_ok());
|
||||
for slot in [71, 65_536, u32::MAX] {
|
||||
let mut bad = vec![];
|
||||
arg(&mut bad, Op::SetLocal, slot);
|
||||
let e = check(&bad, &r).unwrap_err();
|
||||
assert_eq!((e.reason, e.member), (R::LocalSlot, Some(slot)));
|
||||
}
|
||||
let mut limits = LoadLimits::default();
|
||||
limits.frame_slots = 74;
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
let e = validate_registry_instructions(&code, &r, 1, &mut budget).unwrap_err();
|
||||
assert_eq!(e.reason, R::FrameSlots); // 2 parameters + 69 locals + 4 temporaries = 75.
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_function_constant_boolean_and_native_operands_are_checked() {
|
||||
for (operation, id, reason) in [
|
||||
(Op::Call, u32::MAX, R::FunctionId),
|
||||
(Op::PushConst, 1, R::ConstantIndex),
|
||||
(Op::Intrinsic, u32::MAX, R::NativeId),
|
||||
(Op::Syscall, u32::MAX, R::NativeId),
|
||||
(Op::Hostcall, 0, R::UnpatchedHostcall),
|
||||
] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, operation, id);
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!((e.reason, e.pc, e.referenced_id), (reason, Some(2), Some(id)));
|
||||
assert_eq!(
|
||||
e.code,
|
||||
if operation == Op::Hostcall { C::InvalidCode } else { C::InvalidReference }
|
||||
);
|
||||
}
|
||||
let code = [
|
||||
(Op::Halt as u16).to_le_bytes().as_slice(),
|
||||
(Op::PushBool as u16).to_le_bytes().as_slice(),
|
||||
&[2],
|
||||
]
|
||||
.concat();
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert_eq!(check(&code, &r).unwrap_err().reason, R::BooleanImmediate);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_arity_does_not_truncate_and_closure_function_ids_are_checked() {
|
||||
for (operation, function_id, count, reason) in
|
||||
[(Op::Spawn, 0, 65_536, R::Signature), (Op::MakeClosure, 1, 0, R::FunctionId)]
|
||||
{
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, operation, function_id);
|
||||
code.extend_from_slice(&(count as u32).to_le_bytes());
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert_eq!(check(&code, &r).unwrap_err().reason, reason);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declared_range_is_not_extended_into_gaps_or_neighbor_functions() {
|
||||
let code = vec![Op::Halt as u8, 0, 0xff, 0xff];
|
||||
let r = registry(&code, vec![FunctionDecl { code_len: 2, ..function(&code) }]);
|
||||
assert!(check(&code, &r).is_ok()); // Unowned gap is outside this phase's ranges.
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert_eq!(check(&code, &r).unwrap_err().reason, R::UnknownOpcode);
|
||||
let short = &code[..1];
|
||||
assert_eq!(check(short, &r).unwrap_err().reason, R::CodeRange);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scratch_is_refunded_per_function_and_preserves_prior_load_ownership() {
|
||||
let code = vec![Op::Halt as u8, 0, Op::Halt as u8, 0];
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: 2, ..function(&code) },
|
||||
FunctionDecl { code_offset: 2, code_len: 2, ..function(&code) },
|
||||
],
|
||||
);
|
||||
let mut limits = LoadLimits::default();
|
||||
limits.temporary_bytes = 8;
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
budget.charge(Q::TemporaryBytes, 5).unwrap();
|
||||
budget.charge(Q::RegistryBytes, 10).unwrap();
|
||||
validate_registry_instructions(&code, &r, 1, &mut budget).unwrap();
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 5); // Both 3-byte maps fit separately.
|
||||
assert_eq!(budget.used(Q::RegistryBytes), 10);
|
||||
assert!(budget.used(Q::Work) > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scratch_and_work_exhaustion_fail_with_context_and_no_counter_leak() {
|
||||
let code = vec![Op::Halt as u8, 0];
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
for limits in [
|
||||
LoadLimits { temporary_bytes: 2, ..Default::default() },
|
||||
LoadLimits { work: 4, ..Default::default() },
|
||||
] {
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
let e = validate_registry_instructions(&code, &r, 1, &mut budget).unwrap_err();
|
||||
assert_eq!(e.code, C::ResourceLimit);
|
||||
assert_eq!(e.function, Some(0));
|
||||
assert_eq!(e.pc, Some(0));
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert!(budget.used(Q::Work) <= budget.limit(Q::Work));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_immediates_cannot_borrow_bytes_from_the_next_function() {
|
||||
let code = vec![Op::PushI64 as u8, 0, Op::Halt as u8, 0];
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: 2, ..function(&code) },
|
||||
FunctionDecl { code_offset: 2, code_len: 2, ..function(&code) },
|
||||
],
|
||||
);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!((e.code, e.reason, e.pc), (C::InvalidCode, R::Truncated, Some(0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_lookup_work_refusal_retains_the_actual_instruction_pc() {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, Op::Intrinsic, 0x2100);
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert!(check(&code, &r).is_ok()); // Known input query, even in unreachable code.
|
||||
// One function + nine initialized map bytes + two decoding passes and
|
||||
// boundary writes consume 33 units before the first catalogue record.
|
||||
let limits = LoadLimits { work: 33, ..Default::default() };
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
let e = validate_registry_instructions(&code, &r, 1, &mut budget).unwrap_err();
|
||||
assert_eq!(
|
||||
(e.code, e.resource, e.function, e.pc),
|
||||
(C::ResourceLimit, Some(Q::Work), Some(0), Some(2))
|
||||
);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert_eq!(budget.used(Q::Work), 33);
|
||||
}
|
||||
Reference in New Issue
Block a user