252 lines
11 KiB
Rust
252 lines
11 KiB
Rust
//! Registry-aware immediate validation and slot effects for typed instructions.
|
|
use crate::decoder::DecodedInstr;
|
|
use crate::isa::core::{CoreOpCode as Op, CoreOpCodeSpecExt};
|
|
use crate::load_limits::{IdDomain, LoadCode, LoadFailure, LoadReason};
|
|
use crate::registry::{FunctionId, Registry, TypeId, TypeKind};
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct RegistryStackEffect {
|
|
pub pops: u32,
|
|
pub pushes: u32,
|
|
}
|
|
|
|
impl DecodedInstr<'_> {
|
|
/// Capture effects require the enclosing function's resolved environment.
|
|
pub fn registry_stack_effect_in(
|
|
&self,
|
|
registry: &Registry,
|
|
function: FunctionId,
|
|
) -> Result<Option<RegistryStackEffect>, LoadFailure> {
|
|
if self.opcode == Op::GetCapture {
|
|
let index = self
|
|
.imm_u32()
|
|
.map_err(|_| LoadFailure::new(LoadCode::InvalidCode, LoadReason::Truncated))?;
|
|
let capture = registry
|
|
.function_layout(function)
|
|
.and_then(|f| f.captures.get(index as usize))
|
|
.ok_or_else(|| {
|
|
LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex)
|
|
.member(index)
|
|
})?;
|
|
return Ok(Some(RegistryStackEffect { pops: 0, pushes: capture.slots }));
|
|
}
|
|
self.registry_stack_effect(registry)
|
|
}
|
|
/// Resolve an instruction's exact group widths and check immediate domains.
|
|
/// None means this is not a Registry instruction, not a zero stack effect.
|
|
/// This does not prove that the values on the stack satisfy the signature.
|
|
pub fn registry_stack_effect(
|
|
&self,
|
|
registry: &Registry,
|
|
) -> Result<Option<RegistryStackEffect>, LoadFailure> {
|
|
if !self.opcode.requires_registry() {
|
|
return Ok(None);
|
|
}
|
|
let malformed = || {
|
|
let mut e = LoadFailure::new(LoadCode::InvalidCode, LoadReason::Truncated);
|
|
e.pc = u32::try_from(self.pc).ok();
|
|
e
|
|
};
|
|
let type_info = |id: u32| {
|
|
let decl = registry.declarations().types.get(id as usize);
|
|
let layout = registry.type_layout(TypeId(id));
|
|
decl.zip(layout).ok_or_else(|| {
|
|
let mut e = LoadFailure::new(LoadCode::InvalidReference, LoadReason::TypeId)
|
|
.reference(IdDomain::Type, id);
|
|
e.pc = u32::try_from(self.pc).ok();
|
|
e
|
|
})
|
|
};
|
|
let bad_category = |id: u32| {
|
|
let mut e = LoadFailure::new(LoadCode::InvalidReference, LoadReason::Category)
|
|
.reference(IdDomain::Type, id);
|
|
e.pc = u32::try_from(self.pc).ok();
|
|
e
|
|
};
|
|
let (pops, pushes) = match self.opcode {
|
|
Op::MakeClosure => {
|
|
let (function, ty) = self.imm_u32x2().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(ty)?;
|
|
if layout.callable.is_none() {
|
|
return Err(bad_category(ty));
|
|
}
|
|
let target = registry.function_layout(FunctionId(function)).ok_or_else(|| {
|
|
LoadFailure::new(LoadCode::InvalidReference, LoadReason::FunctionId)
|
|
.reference(IdDomain::Function, function)
|
|
})?;
|
|
(target.capture_slots, 1)
|
|
}
|
|
Op::CallClosure => {
|
|
let ty = self.imm_u32().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(ty)?;
|
|
let signature = layout.callable.as_ref().ok_or_else(|| bad_category(ty))?;
|
|
(
|
|
signature.parameter_slots.checked_add(1).ok_or_else(malformed)?,
|
|
signature.result_slots,
|
|
)
|
|
}
|
|
Op::GetCapture => {
|
|
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex));
|
|
}
|
|
Op::CallTrait => {
|
|
let (ty, method) = self.imm_u32x2().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(ty)?;
|
|
let signature = layout
|
|
.methods
|
|
.get(method as usize)
|
|
.ok_or_else(|| bad_category(ty).member(method))?;
|
|
(
|
|
signature.parameter_slots.checked_add(2).ok_or_else(malformed)?,
|
|
signature.result_slots,
|
|
)
|
|
}
|
|
Op::DynPack | Op::DynUnpack | Op::DynCanUnpack => {
|
|
let id = self.imm_u32().map_err(|_| malformed())?;
|
|
let (_, layout) = type_info(id)?;
|
|
match self.opcode {
|
|
Op::DynPack => (layout.storage.slots, 2),
|
|
Op::DynUnpack => (2, layout.storage.slots),
|
|
_ => (2, 1),
|
|
}
|
|
}
|
|
Op::RefIsExact | Op::RefImplements | Op::RefToTrait | Op::RefCast => {
|
|
let (source, target) = self.imm_u32x2().map_err(|_| malformed())?;
|
|
let (source_decl, source_layout) = type_info(source)?;
|
|
let (target_decl, target_layout) = type_info(target)?;
|
|
if !matches!(
|
|
source_decl.kind,
|
|
TypeKind::Struct(_) | TypeKind::Array(_) | TypeKind::Trait(_)
|
|
) {
|
|
return Err(bad_category(source));
|
|
}
|
|
let trait_target = matches!(self.opcode, Op::RefImplements | Op::RefToTrait);
|
|
if (trait_target && !matches!(target_decl.kind, TypeKind::Trait(_)))
|
|
|| (!trait_target
|
|
&& !matches!(target_decl.kind, TypeKind::Struct(_) | TypeKind::Array(_)))
|
|
{
|
|
return Err(bad_category(target));
|
|
}
|
|
let pushes = if matches!(self.opcode, Op::RefIsExact | Op::RefImplements) {
|
|
1
|
|
} else {
|
|
target_layout.storage.slots
|
|
};
|
|
(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)?;
|
|
let TypeKind::Struct(entries) = &decl.kind else {
|
|
return Err(bad_category(schema));
|
|
};
|
|
let mut slots = 0u32;
|
|
for entry in entries {
|
|
let (owner, layout) = type_info(entry.type_id.0)?;
|
|
if !matches!(owner.kind, TypeKind::Struct(_)) {
|
|
return Err(bad_category(entry.type_id.0));
|
|
}
|
|
let fields =
|
|
u32::try_from(layout.instance.as_ref().unwrap().initialization_slots)
|
|
.map_err(|_| malformed())?;
|
|
slots = slots
|
|
.checked_add(1)
|
|
.and_then(|n| n.checked_add(fields))
|
|
.ok_or_else(malformed)?;
|
|
}
|
|
(slots, 0)
|
|
}
|
|
Op::ArrayCheckLength | Op::ArrayCopy | Op::StoragePublish => {
|
|
let (array, owner, first, second) = if self.opcode == Op::StoragePublish {
|
|
let [a, o, f, s] = self.imm_u32x4().map_err(|_| malformed())?;
|
|
(a, o, f, s)
|
|
} else {
|
|
(self.imm_u32().map_err(|_| malformed())?, 0, 0, 0)
|
|
};
|
|
let (_, layout) = type_info(array)?;
|
|
let element = layout.array.ok_or_else(|| bad_category(array))?.element_type_id;
|
|
let width = registry.type_layout(element).unwrap().storage.slots;
|
|
match self.opcode {
|
|
Op::ArrayCheckLength => (1, 0),
|
|
Op::ArrayCopy => (5, 0),
|
|
_ => {
|
|
let (_, layout) = type_info(owner)?;
|
|
let instance =
|
|
layout.instance.as_ref().ok_or_else(|| bad_category(owner))?;
|
|
if first == second {
|
|
return Err(bad_category(owner).member(second));
|
|
}
|
|
let mut slots = 8 + width;
|
|
for field in [first, second] {
|
|
let f = instance
|
|
.fields
|
|
.get(field as usize)
|
|
.ok_or_else(|| bad_category(owner).member(field))?;
|
|
slots += registry.type_layout(f.type_id).unwrap().storage.slots;
|
|
}
|
|
(slots, 0)
|
|
}
|
|
}
|
|
}
|
|
Op::StructNew
|
|
| Op::StructGet
|
|
| Op::StructSet
|
|
| Op::ArrayNew
|
|
| Op::ArrayLen
|
|
| Op::ArrayGet
|
|
| Op::ArraySet => {
|
|
let (id, field) = if matches!(self.opcode, Op::StructGet | Op::StructSet) {
|
|
self.imm_u32x2().map_err(|_| malformed())?
|
|
} else {
|
|
(self.imm_u32().map_err(|_| malformed())?, 0)
|
|
};
|
|
let (_, layout) = type_info(id)?;
|
|
if matches!(self.opcode, Op::StructNew | Op::StructGet | Op::StructSet) {
|
|
let instance = layout.instance.as_ref().ok_or_else(|| bad_category(id))?;
|
|
match self.opcode {
|
|
Op::StructNew => (0, 1),
|
|
op => {
|
|
let f = instance
|
|
.fields
|
|
.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)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
let array = layout.array.as_ref().ok_or_else(|| bad_category(id))?;
|
|
let w = registry.type_layout(array.element_type_id).unwrap().storage.slots;
|
|
match self.opcode {
|
|
Op::ArrayNew | Op::ArrayLen => (1, 1),
|
|
Op::ArrayGet => (2, w),
|
|
_ => (2 + w, 0),
|
|
}
|
|
}
|
|
}
|
|
_ => unreachable!("requires_registry is defined by the typed opcode catalogue"),
|
|
};
|
|
Ok(Some(RegistryStackEffect { pops, pushes }))
|
|
}
|
|
}
|