feat(registry): checkpoint PLN-0176 foundations and validated partial integrations
This commit is contained in:
@@ -24,6 +24,15 @@ pub const TRAP_BAD_RET_SLOTS: u32 = 0x0000_000C;
|
||||
/// The intrinsic ID provided is not recognized by the runtime, or its metadata is invalid.
|
||||
pub const TRAP_INVALID_INTRINSIC: u32 = 0x0000_000D;
|
||||
|
||||
/// Valid absence used where an instance is required.
|
||||
pub const TRAP_NULL_REFERENCE: u32 = 0x0000_000E;
|
||||
/// Invalid, dead or stale generational heap reference.
|
||||
pub const TRAP_INVALID_HANDLE: u32 = 0x0000_000F;
|
||||
/// Quota, checked size overflow or recoverable allocation failure.
|
||||
pub const TRAP_RESOURCE_LIMIT: u32 = 0x0000_0010;
|
||||
/// Known input query unavailable on this platform.
|
||||
pub const TRAP_UNAVAILABLE: u32 = 0x0000_0011;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Detailed information about a source code span.
|
||||
|
||||
@@ -360,6 +360,34 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
||||
emit_u32(parse_u32_any(ops)?, &mut out);
|
||||
}
|
||||
|
||||
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" => {
|
||||
if ops.is_empty() {
|
||||
return Err(AsmError::MissingOperand(line.into()));
|
||||
}
|
||||
let opcode = match mn {
|
||||
"DYN_PACK" => CoreOpCode::DynPack,
|
||||
"DYN_UNPACK" => CoreOpCode::DynUnpack,
|
||||
_ => CoreOpCode::DynCanUnpack,
|
||||
};
|
||||
emit_u16(opcode as u16, &mut out);
|
||||
emit_u32(parse_u32_any(ops)?, &mut out);
|
||||
}
|
||||
"REF_IS_EXACT" | "REF_IMPLEMENTS" | "REF_TO_TRAIT" | "REF_CAST" => {
|
||||
if ops.is_empty() {
|
||||
return Err(AsmError::MissingOperand(line.into()));
|
||||
}
|
||||
let (a, b) = parse_keyvals(ops)?;
|
||||
let (source, target) = parse_pair(a, "source", b, "target")?;
|
||||
let opcode = match mn {
|
||||
"REF_IS_EXACT" => CoreOpCode::RefIsExact,
|
||||
"REF_IMPLEMENTS" => CoreOpCode::RefImplements,
|
||||
"REF_TO_TRAIT" => CoreOpCode::RefToTrait,
|
||||
_ => CoreOpCode::RefCast,
|
||||
};
|
||||
emit_u16(opcode as u16, &mut out);
|
||||
emit_u32(source, &mut out);
|
||||
emit_u32(target, &mut out);
|
||||
}
|
||||
other => return Err(AsmError::UnknownMnemonic(other.into())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ impl<'a> DecodedInstr<'a> {
|
||||
/// Returns the decoded instruction with canonical `next_pc`.
|
||||
#[inline]
|
||||
pub fn decode_next(pc: usize, bytes: &'_ [u8]) -> Result<DecodedInstr<'_>, DecodeError> {
|
||||
if pc + 2 > bytes.len() {
|
||||
if pc.checked_add(2).is_none_or(|end| end > bytes.len()) {
|
||||
return Err(DecodeError::TruncatedOpcode { pc });
|
||||
}
|
||||
|
||||
@@ -100,7 +100,13 @@ pub fn decode_next(pc: usize, bytes: &'_ [u8]) -> Result<DecodedInstr<'_>, Decod
|
||||
|
||||
let spec: CoreOpcodeSpec = opcode.spec();
|
||||
let imm_start = pc + 2;
|
||||
let imm_end = imm_start + (spec.imm_bytes as usize);
|
||||
let imm_end =
|
||||
imm_start.checked_add(spec.imm_bytes as usize).ok_or(DecodeError::TruncatedImmediate {
|
||||
pc,
|
||||
opcode,
|
||||
need: spec.imm_bytes as usize,
|
||||
have: bytes.len().saturating_sub(imm_start),
|
||||
})?;
|
||||
|
||||
if imm_end > bytes.len() {
|
||||
return Err(DecodeError::TruncatedImmediate {
|
||||
|
||||
@@ -60,10 +60,21 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
|
||||
| CoreOpCode::SetLocal
|
||||
| CoreOpCode::Call
|
||||
| CoreOpCode::Sleep
|
||||
| CoreOpCode::Hostcall => {
|
||||
| CoreOpCode::Hostcall
|
||||
| CoreOpCode::DynPack
|
||||
| CoreOpCode::DynUnpack
|
||||
| CoreOpCode::DynCanUnpack => {
|
||||
let v = u32::from_le_bytes(imm.try_into().unwrap());
|
||||
format!("{}", v)
|
||||
}
|
||||
CoreOpCode::RefIsExact
|
||||
| CoreOpCode::RefImplements
|
||||
| CoreOpCode::RefToTrait
|
||||
| CoreOpCode::RefCast => {
|
||||
let source = u32::from_le_bytes(imm[..4].try_into().unwrap());
|
||||
let target = u32::from_le_bytes(imm[4..].try_into().unwrap());
|
||||
format!("source={}, target={}", source, target)
|
||||
}
|
||||
CoreOpCode::MakeClosure => {
|
||||
let fn_id = u32::from_le_bytes(imm[0..4].try_into().unwrap());
|
||||
let cap = u32::from_le_bytes(imm[4..8].try_into().unwrap());
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! Core ISA profile (bytecode-level, stable, canonical).
|
||||
//!
|
||||
//! This profile is the canonical opcode surface used by decoder/disasm and
|
||||
//! mirrors the runtime-visible instruction set implemented today, including
|
||||
//! closures, coroutines, intrinsics, and hostcall patching semantics.
|
||||
//! exposes the canonical encoding for closures, coroutines, intrinsics,
|
||||
//! hostcall patching and Registry instructions. Recognition by tooling alone
|
||||
//! does not authorize execution without validated program metadata.
|
||||
|
||||
// For PR-1.4 we define the core profile as the current `OpCode` set. We re-export
|
||||
// the opcode type and spec so downstream tools can import from a stable path:
|
||||
|
||||
@@ -12,13 +12,21 @@ mod value;
|
||||
|
||||
pub use abi::{
|
||||
SourceSpan, TrapInfo, TRAP_BAD_RET_SLOTS, TRAP_DIV_ZERO, TRAP_EXPLICIT,
|
||||
TRAP_ILLEGAL_INSTRUCTION, TRAP_INVALID_FUNC, TRAP_INVALID_INTRINSIC, TRAP_INVALID_LOCAL,
|
||||
TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_STACK_UNDERFLOW, TRAP_TYPE,
|
||||
TRAP_ILLEGAL_INSTRUCTION, TRAP_INVALID_FUNC, TRAP_INVALID_HANDLE, TRAP_INVALID_INTRINSIC,
|
||||
TRAP_INVALID_LOCAL, TRAP_INVALID_SYSCALL, TRAP_NULL_REFERENCE, TRAP_OOB, TRAP_RESOURCE_LIMIT,
|
||||
TRAP_STACK_UNDERFLOW, TRAP_TYPE, TRAP_UNAVAILABLE,
|
||||
};
|
||||
pub use assembler::{assemble, AsmError};
|
||||
pub use decoder::{decode_next, DecodeError};
|
||||
pub use decoder::{decode_next, DecodeError, DecodedInstr};
|
||||
pub use disassembler::disassemble;
|
||||
pub use layout::{compute_function_layouts, FunctionLayout};
|
||||
pub use model::{BytecodeLoader, FunctionMeta, LoadError, SyscallDecl, PBX_MAGIC};
|
||||
pub use program_image::ProgramImage;
|
||||
pub use value::{string_materialization_count, HeapRef, Value};
|
||||
|
||||
pub mod load_limits;
|
||||
pub mod registry;
|
||||
pub mod registry_codec;
|
||||
|
||||
mod registry_instruction;
|
||||
pub use registry_instruction::RegistryStackEffect;
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
//! Shared PBX v1 resource limits and structured validation failures.
|
||||
//! Counters are deterministic; a failed reservation never changes usage.
|
||||
use std::fmt;
|
||||
|
||||
const MIB: u64 = 1_048_576;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LoadCode {
|
||||
InvalidFormat,
|
||||
UnsupportedFormat,
|
||||
InvalidReference,
|
||||
InvalidRegistry,
|
||||
InvalidCode,
|
||||
ResourceLimit,
|
||||
InvalidEntrypoint,
|
||||
}
|
||||
impl LoadCode {
|
||||
pub const fn tag(self) -> &'static str {
|
||||
match self {
|
||||
Self::InvalidFormat => "LOAD_INVALID_FORMAT",
|
||||
Self::UnsupportedFormat => "LOAD_UNSUPPORTED_FORMAT",
|
||||
Self::InvalidReference => "LOAD_INVALID_REFERENCE",
|
||||
Self::InvalidRegistry => "LOAD_INVALID_REGISTRY",
|
||||
Self::InvalidCode => "LOAD_INVALID_CODE",
|
||||
Self::ResourceLimit => "LOAD_RESOURCE_LIMIT",
|
||||
Self::InvalidEntrypoint => "LOAD_INVALID_ENTRYPOINT",
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LoadReason {
|
||||
Magic,
|
||||
Version,
|
||||
Endianness,
|
||||
Reserved,
|
||||
SectionKind,
|
||||
MissingSection,
|
||||
DuplicateSection,
|
||||
Overlap,
|
||||
Truncated,
|
||||
TrailingBytes,
|
||||
InvalidFlag,
|
||||
Utf8,
|
||||
Category,
|
||||
Builtin,
|
||||
TypeId,
|
||||
FunctionId,
|
||||
OwnerCategory,
|
||||
TraitCategory,
|
||||
DuplicateBuiltin,
|
||||
DuplicateArray,
|
||||
ArrayCycle,
|
||||
DuplicateImplementation,
|
||||
MethodCount,
|
||||
Signature,
|
||||
CodeRange,
|
||||
FrameSlots,
|
||||
ReturnSlots,
|
||||
Overflow,
|
||||
Limit,
|
||||
Allocation,
|
||||
ConstantTag,
|
||||
DebugRange,
|
||||
DuplicateSyscall,
|
||||
UnknownOpcode,
|
||||
LocalSlot,
|
||||
JumpTarget,
|
||||
InstructionBoundary,
|
||||
BooleanImmediate,
|
||||
ConstantIndex,
|
||||
NativeId,
|
||||
UnpatchedHostcall,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(usize)]
|
||||
pub enum LoadResource {
|
||||
PbxBytes,
|
||||
CodeBytes,
|
||||
Types,
|
||||
Functions,
|
||||
Implementations,
|
||||
NestedRecords,
|
||||
NameBytes,
|
||||
RegistryBytes,
|
||||
TemporaryBytes,
|
||||
Work,
|
||||
ConstantEntries,
|
||||
ConstantStringBytes,
|
||||
}
|
||||
const RESOURCE_COUNT: usize = 12;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum IdDomain {
|
||||
Type,
|
||||
Function,
|
||||
Implementation,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LoadFailure {
|
||||
pub code: LoadCode,
|
||||
pub reason: LoadReason,
|
||||
pub section: Option<u32>,
|
||||
pub offset: Option<usize>,
|
||||
pub record: Option<u32>,
|
||||
pub member: Option<u32>,
|
||||
pub id_domain: Option<IdDomain>,
|
||||
pub referenced_id: Option<u32>,
|
||||
pub function: Option<u32>,
|
||||
pub pc: Option<u32>,
|
||||
pub resource: Option<LoadResource>,
|
||||
pub requested: Option<u64>,
|
||||
pub limit: Option<u64>,
|
||||
}
|
||||
impl LoadFailure {
|
||||
pub fn new(code: LoadCode, reason: LoadReason) -> Self {
|
||||
Self {
|
||||
code,
|
||||
reason,
|
||||
section: None,
|
||||
offset: None,
|
||||
record: None,
|
||||
member: None,
|
||||
id_domain: None,
|
||||
referenced_id: None,
|
||||
function: None,
|
||||
pc: None,
|
||||
resource: None,
|
||||
requested: None,
|
||||
limit: None,
|
||||
}
|
||||
}
|
||||
pub fn at(mut self, section: u32, offset: usize) -> Self {
|
||||
self.section = Some(section);
|
||||
self.offset = Some(offset);
|
||||
self
|
||||
}
|
||||
pub fn record(mut self, record: u32) -> Self {
|
||||
self.record = Some(record);
|
||||
self
|
||||
}
|
||||
pub fn member(mut self, member: u32) -> Self {
|
||||
self.member = Some(member);
|
||||
self
|
||||
}
|
||||
pub fn in_section(mut self, section: u32) -> Self {
|
||||
self.section = Some(section);
|
||||
self
|
||||
}
|
||||
pub fn reference(mut self, domain: IdDomain, id: u32) -> Self {
|
||||
self.id_domain = Some(domain);
|
||||
self.referenced_id = Some(id);
|
||||
self
|
||||
}
|
||||
pub fn in_function(mut self, function: u32, pc: Option<u32>) -> Self {
|
||||
self.function = Some(function);
|
||||
self.pc = pc;
|
||||
self
|
||||
}
|
||||
pub fn resource(resource: LoadResource, requested: u64, limit: u64) -> Self {
|
||||
Self {
|
||||
resource: Some(resource),
|
||||
requested: Some(requested),
|
||||
limit: Some(limit),
|
||||
..Self::new(LoadCode::ResourceLimit, LoadReason::Limit)
|
||||
}
|
||||
}
|
||||
}
|
||||
impl fmt::Display for LoadFailure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}: {:?}", self.code.tag(), self.reason)?;
|
||||
if let Some(section) = self.section {
|
||||
write!(f, " in section {section}")?;
|
||||
}
|
||||
if let Some(offset) = self.offset {
|
||||
write!(f, " at byte {offset}")?;
|
||||
}
|
||||
if let Some(record) = self.record {
|
||||
write!(f, " (record {record})")?;
|
||||
}
|
||||
if let Some(resource) = self.resource {
|
||||
write!(f, " ({resource:?}: {:?} / {:?})", self.requested, self.limit)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl std::error::Error for LoadFailure {}
|
||||
|
||||
/// Product maxima; callers may lower limits but cannot raise the accepted ceilings.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct LoadLimits {
|
||||
pub pbx_bytes: u64,
|
||||
pub code_bytes: u64,
|
||||
pub types: u64,
|
||||
pub functions: u64,
|
||||
pub implementations: u64,
|
||||
pub nested_records: u64,
|
||||
pub name_bytes: u64,
|
||||
pub individual_name_bytes: u64,
|
||||
pub registry_bytes: u64,
|
||||
pub temporary_bytes: u64,
|
||||
pub work: u64,
|
||||
pub frame_slots: u64,
|
||||
pub result_slots: u64,
|
||||
pub constant_entries: u64,
|
||||
pub constant_string_bytes: u64,
|
||||
pub individual_string_bytes: u64,
|
||||
}
|
||||
impl Default for LoadLimits {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
pbx_bytes: 64 * MIB,
|
||||
code_bytes: 32 * MIB,
|
||||
types: 65_536,
|
||||
functions: 65_536,
|
||||
implementations: 262_144,
|
||||
nested_records: 1_048_576,
|
||||
name_bytes: 4 * MIB,
|
||||
individual_name_bytes: 4096,
|
||||
registry_bytes: 64 * MIB,
|
||||
temporary_bytes: 128 * MIB,
|
||||
work: 268_435_456,
|
||||
frame_slots: 16_384,
|
||||
result_slots: 1024,
|
||||
constant_entries: 1_048_576,
|
||||
constant_string_bytes: 64 * MIB,
|
||||
individual_string_bytes: 4 * MIB,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl LoadLimits {
|
||||
fn ceiling(self, r: LoadResource) -> u64 {
|
||||
match r {
|
||||
LoadResource::PbxBytes => self.pbx_bytes,
|
||||
LoadResource::CodeBytes => self.code_bytes,
|
||||
LoadResource::Types => self.types,
|
||||
LoadResource::Functions => self.functions,
|
||||
LoadResource::Implementations => self.implementations,
|
||||
LoadResource::NestedRecords => self.nested_records,
|
||||
LoadResource::NameBytes => self.name_bytes,
|
||||
LoadResource::RegistryBytes => self.registry_bytes,
|
||||
LoadResource::TemporaryBytes => self.temporary_bytes,
|
||||
LoadResource::Work => self.work,
|
||||
LoadResource::ConstantEntries => self.constant_entries,
|
||||
LoadResource::ConstantStringBytes => self.constant_string_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// One load attempt's counters. Successful retained allocations stay charged
|
||||
/// for the remainder of this attempt; executable-image lifetime accounting is
|
||||
/// a separate owner. Reuse a budget only when its earlier allocations still live.
|
||||
#[derive(Debug)]
|
||||
pub struct LoadBudget {
|
||||
limits: LoadLimits,
|
||||
used: [u64; RESOURCE_COUNT],
|
||||
}
|
||||
impl LoadBudget {
|
||||
pub fn new(limits: LoadLimits) -> Self {
|
||||
Self { limits, used: [0; RESOURCE_COUNT] }
|
||||
}
|
||||
pub fn limit(&self, r: LoadResource) -> u64 {
|
||||
self.limits.ceiling(r).min(LoadLimits::default().ceiling(r))
|
||||
}
|
||||
pub fn individual_string_limit(&self) -> u64 {
|
||||
self.limits.individual_string_bytes.min(4 * MIB)
|
||||
}
|
||||
pub fn individual_name_limit(&self) -> u64 {
|
||||
self.limits.individual_name_bytes.min(4096)
|
||||
}
|
||||
pub fn frame_limit(&self) -> u64 {
|
||||
self.limits.frame_slots.min(16_384)
|
||||
}
|
||||
pub fn result_limit(&self) -> u64 {
|
||||
self.limits.result_slots.min(1024)
|
||||
}
|
||||
pub fn used(&self, r: LoadResource) -> u64 {
|
||||
self.used[r as usize]
|
||||
}
|
||||
pub fn charge(&mut self, r: LoadResource, amount: u64) -> Result<(), LoadFailure> {
|
||||
let next = self
|
||||
.used(r)
|
||||
.checked_add(amount)
|
||||
.ok_or_else(|| LoadFailure::new(LoadCode::ResourceLimit, LoadReason::Overflow))?;
|
||||
self.check(r, next)?;
|
||||
self.used[r as usize] = next;
|
||||
Ok(())
|
||||
}
|
||||
pub fn check(&self, r: LoadResource, amount: u64) -> Result<(), LoadFailure> {
|
||||
if amount > self.limit(r) {
|
||||
Err(LoadFailure::resource(r, amount, self.limit(r)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub fn refund(&mut self, r: LoadResource, amount: u64) {
|
||||
self.used[r as usize] = self.used[r as usize]
|
||||
.checked_sub(amount)
|
||||
.expect("internal load budget accounting imbalance");
|
||||
}
|
||||
pub fn work(&mut self, amount: usize) -> Result<(), LoadFailure> {
|
||||
self.charge(LoadResource::Work, amount as u64)
|
||||
}
|
||||
pub(crate) fn vector<T>(
|
||||
&mut self,
|
||||
count: usize,
|
||||
r: LoadResource,
|
||||
) -> Result<Vec<T>, LoadFailure> {
|
||||
let bytes = count
|
||||
.checked_mul(std::mem::size_of::<T>())
|
||||
.ok_or_else(|| LoadFailure::new(LoadCode::ResourceLimit, LoadReason::Overflow))?
|
||||
as u64;
|
||||
self.charge(r, bytes)?;
|
||||
let mut result = Vec::new();
|
||||
if result.try_reserve_exact(count).is_err() {
|
||||
self.refund(r, bytes);
|
||||
return Err(LoadFailure::new(LoadCode::ResourceLimit, LoadReason::Allocation));
|
||||
}
|
||||
let actual = (result.capacity() as u64)
|
||||
.checked_mul(std::mem::size_of::<T>() as u64)
|
||||
.ok_or_else(|| LoadFailure::new(LoadCode::ResourceLimit, LoadReason::Overflow))?;
|
||||
if let Err(err) = self.charge(r, actual - bytes) {
|
||||
self.refund(r, bytes);
|
||||
return Err(err);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
/// Roll back retained counters on failure; attempted work remains charged.
|
||||
pub(crate) fn transaction<T>(
|
||||
&mut self,
|
||||
f: impl FnOnce(&mut Self) -> Result<T, LoadFailure>,
|
||||
) -> Result<T, LoadFailure> {
|
||||
let before = self.used;
|
||||
let result = f(self);
|
||||
if result.is_err() {
|
||||
let work = self.used(LoadResource::Work);
|
||||
self.used = before;
|
||||
self.used[LoadResource::Work as usize] = work;
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
@@ -202,6 +202,22 @@ pub enum OpCode {
|
||||
/// Synchronizes the VM with the hardware frame (usually 60Hz).
|
||||
/// Execution pauses until the next VSync.
|
||||
FrameSync = 0x80,
|
||||
|
||||
// Registry-dependent typed operations (DEC-0045).
|
||||
/// DYN_PACK: TypeId:u32; requires validated Registry metadata.
|
||||
DynPack = 0x90,
|
||||
/// DYN_UNPACK: TypeId:u32; requires validated Registry metadata.
|
||||
DynUnpack = 0x91,
|
||||
/// DYN_CAN_UNPACK: TypeId:u32; requires validated Registry metadata.
|
||||
DynCanUnpack = 0x92,
|
||||
/// REF_IS_EXACT: source:TypeId, target:TypeId; requires validated Registry metadata.
|
||||
RefIsExact = 0x93,
|
||||
/// REF_IMPLEMENTS: source:TypeId, target:TypeId; requires validated Registry metadata.
|
||||
RefImplements = 0x94,
|
||||
/// REF_TO_TRAIT: source:TypeId, target:TypeId; requires validated Registry metadata.
|
||||
RefToTrait = 0x95,
|
||||
/// REF_CAST: source:TypeId, target:TypeId; requires validated Registry metadata.
|
||||
RefCast = 0x96,
|
||||
}
|
||||
|
||||
impl TryFrom<u16> for OpCode {
|
||||
@@ -259,6 +275,14 @@ impl TryFrom<u16> for OpCode {
|
||||
0x71 => Ok(OpCode::Hostcall),
|
||||
0x72 => Ok(OpCode::Intrinsic),
|
||||
0x80 => Ok(OpCode::FrameSync),
|
||||
0x90 => Ok(OpCode::DynPack),
|
||||
0x91 => Ok(OpCode::DynUnpack),
|
||||
0x92 => Ok(OpCode::DynCanUnpack),
|
||||
0x93 => Ok(OpCode::RefIsExact),
|
||||
0x94 => Ok(OpCode::RefImplements),
|
||||
0x95 => Ok(OpCode::RefToTrait),
|
||||
0x96 => Ok(OpCode::RefCast),
|
||||
|
||||
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
|
||||
}
|
||||
}
|
||||
@@ -319,6 +343,13 @@ impl OpCode {
|
||||
OpCode::Hostcall => 1,
|
||||
OpCode::Intrinsic => 1,
|
||||
OpCode::FrameSync => 1,
|
||||
OpCode::DynPack => 8,
|
||||
OpCode::DynUnpack => 4,
|
||||
OpCode::DynCanUnpack => 3,
|
||||
OpCode::RefIsExact => 4,
|
||||
OpCode::RefImplements => 4,
|
||||
OpCode::RefToTrait => 5,
|
||||
OpCode::RefCast => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,24 @@ use crate::opcode::OpCode;
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct OpcodeSpec {
|
||||
pub name: &'static str,
|
||||
pub imm_bytes: u8, // immediate payload size (decode)
|
||||
pub pops: u16, // slots popped
|
||||
pub pushes: u16, // slots pushed
|
||||
pub is_branch: bool, // has a control-flow target
|
||||
pub imm_bytes: u8, // immediate payload size (decode)
|
||||
/// None requires Registry resolution; it never means zero slots.
|
||||
pub pops: Option<u16>, // slots popped
|
||||
/// None requires Registry resolution; it never means zero slots.
|
||||
pub pushes: Option<u16>, // slots pushed
|
||||
pub is_branch: bool, // has a control-flow target
|
||||
pub is_terminator: bool, // ends basic block: JMP/RET/TRAP/HALT
|
||||
pub may_trap: bool, // runtime trap possible
|
||||
pub may_trap: bool, // runtime trap possible
|
||||
/// Marks this opcode as a VM safepoint. Used by GC/scheduler policies.
|
||||
pub is_safepoint: bool,
|
||||
}
|
||||
|
||||
pub trait OpCodeSpecExt {
|
||||
fn spec(&self) -> OpcodeSpec;
|
||||
/// These instructions cannot be verified from slot-only FunctionMeta.
|
||||
fn requires_registry(&self) -> bool {
|
||||
self.spec().pops.is_none() || self.spec().pushes.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl OpCodeSpecExt for OpCode {
|
||||
@@ -25,8 +31,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Nop => OpcodeSpec {
|
||||
name: "NOP",
|
||||
imm_bytes: 0,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -35,8 +41,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Halt => OpcodeSpec {
|
||||
name: "HALT",
|
||||
imm_bytes: 0,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: true,
|
||||
may_trap: false,
|
||||
@@ -45,8 +51,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Jmp => OpcodeSpec {
|
||||
name: "JMP",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: true,
|
||||
is_terminator: true,
|
||||
may_trap: false,
|
||||
@@ -55,8 +61,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::JmpIfFalse => OpcodeSpec {
|
||||
name: "JMP_IF_FALSE",
|
||||
imm_bytes: 4,
|
||||
pops: 1,
|
||||
pushes: 0,
|
||||
pops: Some(1),
|
||||
pushes: Some(0),
|
||||
is_branch: true,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -65,8 +71,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::JmpIfTrue => OpcodeSpec {
|
||||
name: "JMP_IF_TRUE",
|
||||
imm_bytes: 4,
|
||||
pops: 1,
|
||||
pushes: 0,
|
||||
pops: Some(1),
|
||||
pushes: Some(0),
|
||||
is_branch: true,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -75,8 +81,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Trap => OpcodeSpec {
|
||||
name: "TRAP",
|
||||
imm_bytes: 0,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: true,
|
||||
may_trap: true,
|
||||
@@ -85,8 +91,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::PushConst => OpcodeSpec {
|
||||
name: "PUSH_CONST",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -95,8 +101,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Pop => OpcodeSpec {
|
||||
name: "POP",
|
||||
imm_bytes: 0,
|
||||
pops: 1,
|
||||
pushes: 0,
|
||||
pops: Some(1),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -105,8 +111,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::PopN => OpcodeSpec {
|
||||
name: "POP_N",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -115,8 +121,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Dup => OpcodeSpec {
|
||||
name: "DUP",
|
||||
imm_bytes: 0,
|
||||
pops: 1,
|
||||
pushes: 2,
|
||||
pops: Some(1),
|
||||
pushes: Some(2),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -125,8 +131,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Swap => OpcodeSpec {
|
||||
name: "SWAP",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 2,
|
||||
pops: Some(2),
|
||||
pushes: Some(2),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -135,8 +141,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::PushI64 => OpcodeSpec {
|
||||
name: "PUSH_I64",
|
||||
imm_bytes: 8,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -145,8 +151,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::PushF64 => OpcodeSpec {
|
||||
name: "PUSH_F64",
|
||||
imm_bytes: 8,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -155,8 +161,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::PushBool => OpcodeSpec {
|
||||
name: "PUSH_BOOL",
|
||||
imm_bytes: 1,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -165,8 +171,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::PushI32 => OpcodeSpec {
|
||||
name: "PUSH_I32",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -175,8 +181,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Add => OpcodeSpec {
|
||||
name: "ADD",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -185,8 +191,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Sub => OpcodeSpec {
|
||||
name: "SUB",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -195,8 +201,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Mul => OpcodeSpec {
|
||||
name: "MUL",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -205,8 +211,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Div => OpcodeSpec {
|
||||
name: "DIV",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -215,8 +221,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Mod => OpcodeSpec {
|
||||
name: "MOD",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -225,8 +231,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Eq => OpcodeSpec {
|
||||
name: "EQ",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -235,8 +241,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Neq => OpcodeSpec {
|
||||
name: "NEQ",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -245,8 +251,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Lt => OpcodeSpec {
|
||||
name: "LT",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -255,8 +261,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Gt => OpcodeSpec {
|
||||
name: "GT",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -265,8 +271,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::And => OpcodeSpec {
|
||||
name: "AND",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -275,8 +281,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Or => OpcodeSpec {
|
||||
name: "OR",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -285,8 +291,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Not => OpcodeSpec {
|
||||
name: "NOT",
|
||||
imm_bytes: 0,
|
||||
pops: 1,
|
||||
pushes: 1,
|
||||
pops: Some(1),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -295,8 +301,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::BitAnd => OpcodeSpec {
|
||||
name: "BIT_AND",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -305,8 +311,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::BitOr => OpcodeSpec {
|
||||
name: "BIT_OR",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -315,8 +321,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::BitXor => OpcodeSpec {
|
||||
name: "BIT_XOR",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -325,8 +331,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Shl => OpcodeSpec {
|
||||
name: "SHL",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -335,8 +341,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Shr => OpcodeSpec {
|
||||
name: "SHR",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -345,8 +351,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Lte => OpcodeSpec {
|
||||
name: "LTE",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -355,8 +361,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Gte => OpcodeSpec {
|
||||
name: "GTE",
|
||||
imm_bytes: 0,
|
||||
pops: 2,
|
||||
pushes: 1,
|
||||
pops: Some(2),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -365,8 +371,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Neg => OpcodeSpec {
|
||||
name: "NEG",
|
||||
imm_bytes: 0,
|
||||
pops: 1,
|
||||
pushes: 1,
|
||||
pops: Some(1),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -375,8 +381,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::GetGlobal => OpcodeSpec {
|
||||
name: "GET_GLOBAL",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -385,8 +391,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::SetGlobal => OpcodeSpec {
|
||||
name: "SET_GLOBAL",
|
||||
imm_bytes: 4,
|
||||
pops: 1,
|
||||
pushes: 0,
|
||||
pops: Some(1),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -395,8 +401,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::GetLocal => OpcodeSpec {
|
||||
name: "GET_LOCAL",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -405,8 +411,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::SetLocal => OpcodeSpec {
|
||||
name: "SET_LOCAL",
|
||||
imm_bytes: 4,
|
||||
pops: 1,
|
||||
pushes: 0,
|
||||
pops: Some(1),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -415,8 +421,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Call => OpcodeSpec {
|
||||
name: "CALL",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -425,8 +431,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Ret => OpcodeSpec {
|
||||
name: "RET",
|
||||
imm_bytes: 0,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: true,
|
||||
may_trap: false,
|
||||
@@ -437,8 +443,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
// Two u32 immediates: fn_id and capture_count
|
||||
imm_bytes: 8,
|
||||
// Dynamic, depends on capture_count; keep 0 here for verifier-free spec
|
||||
pops: 0,
|
||||
pushes: 1,
|
||||
pops: Some(0),
|
||||
pushes: Some(1),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -449,8 +455,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
// One u32 immediate: arg_count (user args, excludes hidden arg0)
|
||||
imm_bytes: 4,
|
||||
// Dynamic: pops closure_ref + arg_count; keep 0 in spec layer
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -461,8 +467,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
// Two u32 immediates: fn_id and arg_count
|
||||
imm_bytes: 8,
|
||||
// Dynamic pops depends on arg_count; keep 0 here in spec layer
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -471,8 +477,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Yield => OpcodeSpec {
|
||||
name: "YIELD",
|
||||
imm_bytes: 0,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
// Not a block terminator; effect realized at safepoint
|
||||
is_terminator: false,
|
||||
@@ -484,8 +490,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
name: "SLEEP",
|
||||
// One u32 immediate: duration_ticks
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
// Ends execution at safepoint after instruction completes
|
||||
is_terminator: false,
|
||||
@@ -496,8 +502,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Hostcall => OpcodeSpec {
|
||||
name: "HOSTCALL",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
@@ -506,8 +512,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Syscall => OpcodeSpec {
|
||||
name: "SYSCALL",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -516,8 +522,78 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::Intrinsic => OpcodeSpec {
|
||||
name: "INTRINSIC",
|
||||
imm_bytes: 4,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::DynPack => OpcodeSpec {
|
||||
name: "DYN_PACK",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::DynUnpack => OpcodeSpec {
|
||||
name: "DYN_UNPACK",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::DynCanUnpack => OpcodeSpec {
|
||||
name: "DYN_CAN_UNPACK",
|
||||
imm_bytes: 4,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::RefIsExact => OpcodeSpec {
|
||||
name: "REF_IS_EXACT",
|
||||
imm_bytes: 8,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::RefImplements => OpcodeSpec {
|
||||
name: "REF_IMPLEMENTS",
|
||||
imm_bytes: 8,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::RefToTrait => OpcodeSpec {
|
||||
name: "REF_TO_TRAIT",
|
||||
imm_bytes: 8,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
is_safepoint: false,
|
||||
},
|
||||
OpCode::RefCast => OpcodeSpec {
|
||||
name: "REF_CAST",
|
||||
imm_bytes: 8,
|
||||
pops: None,
|
||||
pushes: None,
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: true,
|
||||
@@ -526,8 +602,8 @@ impl OpCodeSpecExt for OpCode {
|
||||
OpCode::FrameSync => OpcodeSpec {
|
||||
name: "FRAME_SYNC",
|
||||
imm_bytes: 0,
|
||||
pops: 0,
|
||||
pushes: 0,
|
||||
pops: Some(0),
|
||||
pushes: Some(0),
|
||||
is_branch: false,
|
||||
is_terminator: false,
|
||||
may_trap: false,
|
||||
|
||||
@@ -0,0 +1,607 @@
|
||||
//! Typed PBX declarations and bounded immutable Registry resolution.
|
||||
//! These declarations do not authorize execution: code and bindings still require verification.
|
||||
use crate::load_limits::IdDomain;
|
||||
use crate::load_limits::{
|
||||
LoadBudget, LoadCode as C, LoadFailure as E, LoadReason as R, LoadResource as Q,
|
||||
};
|
||||
use crate::Value;
|
||||
use std::sync::Arc;
|
||||
|
||||
macro_rules! id {
|
||||
($name:ident) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct $name(pub u32);
|
||||
};
|
||||
}
|
||||
id!(TypeId);
|
||||
id!(FunctionId);
|
||||
id!(ImplementationId);
|
||||
id!(BuiltinId);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct NamedType {
|
||||
pub type_id: TypeId,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct Signature {
|
||||
pub parameters: Vec<NamedType>,
|
||||
pub results: Vec<TypeId>,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct MethodDecl {
|
||||
pub name: Option<String>,
|
||||
pub signature: Signature,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TypeKind {
|
||||
Builtin(BuiltinId),
|
||||
Struct(Vec<NamedType>),
|
||||
Array(TypeId),
|
||||
Trait(Vec<MethodDecl>),
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TypeDecl {
|
||||
pub name: Option<String>,
|
||||
pub kind: TypeKind,
|
||||
}
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct FunctionDecl {
|
||||
pub name: Option<String>,
|
||||
pub signature: Signature,
|
||||
pub code_offset: u32,
|
||||
pub code_len: u32,
|
||||
pub local_slots: u32,
|
||||
pub max_stack_slots: u32,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ImplementationDecl {
|
||||
pub owner: TypeId,
|
||||
pub trait_id: TypeId,
|
||||
pub methods: Vec<FunctionId>,
|
||||
}
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct RegistryDeclarations {
|
||||
pub types: Vec<TypeDecl>,
|
||||
pub functions: Vec<FunctionDecl>,
|
||||
pub implementations: Vec<ImplementationDecl>,
|
||||
}
|
||||
|
||||
pub type StringSlot = Option<Arc<str>>;
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DynamicSlot {
|
||||
pub type_id: Option<TypeId>,
|
||||
pub payload: Value,
|
||||
}
|
||||
impl Default for DynamicSlot {
|
||||
fn default() -> Self {
|
||||
Self { type_id: None, payload: Value::Null }
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum DefaultKind {
|
||||
Int32,
|
||||
Int64,
|
||||
Float,
|
||||
Bool,
|
||||
Null,
|
||||
Vec2,
|
||||
Pixel,
|
||||
Trait,
|
||||
Dynamic,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Ownership {
|
||||
Plain,
|
||||
GuestReference,
|
||||
NativeString,
|
||||
TraitReceiver,
|
||||
Dynamic,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StorageLayout {
|
||||
pub size: usize,
|
||||
pub alignment: usize,
|
||||
pub slots: u32,
|
||||
pub ownership: Ownership,
|
||||
pub default: DefaultKind,
|
||||
}
|
||||
impl StorageLayout {
|
||||
fn new(
|
||||
size: usize,
|
||||
alignment: usize,
|
||||
slots: u32,
|
||||
ownership: Ownership,
|
||||
default: DefaultKind,
|
||||
) -> Self {
|
||||
Self { size, alignment, slots, ownership, default }
|
||||
}
|
||||
pub fn nullable(self) -> bool {
|
||||
!matches!(self.ownership, Ownership::Plain)
|
||||
}
|
||||
/// Canonical stack-side defaults, with no allocation or source constructor.
|
||||
/// Native typed storage still requires its own initialized-slot construction.
|
||||
pub fn default_values(self) -> &'static [Value] {
|
||||
match self.default {
|
||||
DefaultKind::Int32 => &[Value::Int32(0)],
|
||||
DefaultKind::Int64 => &[Value::Int64(0)],
|
||||
DefaultKind::Float => &[Value::Float(0.0)],
|
||||
DefaultKind::Bool => &[Value::Boolean(false)],
|
||||
DefaultKind::Null => &[Value::Null],
|
||||
DefaultKind::Vec2 => &[Value::Float(0.0), Value::Float(0.0)],
|
||||
DefaultKind::Pixel => &[Value::Int64(0), Value::Int64(0), Value::Int64(0)],
|
||||
DefaultKind::Trait | DefaultKind::Dynamic => &[Value::Null, Value::Null],
|
||||
}
|
||||
}
|
||||
}
|
||||
impl BuiltinId {
|
||||
pub const COLOR: Self = Self(1);
|
||||
pub const VEC2: Self = Self(2);
|
||||
pub const PIXEL: Self = Self(3);
|
||||
pub const INT32: Self = Self(0x10);
|
||||
pub const INT64: Self = Self(0x11);
|
||||
pub const FLOAT: Self = Self(0x12);
|
||||
pub const BOOL: Self = Self(0x13);
|
||||
pub const STRING: Self = Self(0x14);
|
||||
pub const DYNAMIC: Self = Self(0x15);
|
||||
pub fn layout(self) -> Result<StorageLayout, E> {
|
||||
use DefaultKind as D;
|
||||
use Ownership as O;
|
||||
let (size, alignment, slots, ownership, default) = match self {
|
||||
Self::COLOR => (4, 4, 1, O::Plain, D::Int64),
|
||||
Self::VEC2 => (16, 8, 2, O::Plain, D::Vec2),
|
||||
Self::PIXEL => (24, 8, 3, O::Plain, D::Pixel),
|
||||
Self::INT32 => (4, 4, 1, O::Plain, D::Int32),
|
||||
Self::INT64 => (8, 8, 1, O::Plain, D::Int64),
|
||||
Self::FLOAT => (8, 8, 1, O::Plain, D::Float),
|
||||
Self::BOOL => (1, 1, 1, O::Plain, D::Bool),
|
||||
Self::STRING => {
|
||||
(size_of::<StringSlot>(), align_of::<StringSlot>(), 1, O::NativeString, D::Null)
|
||||
}
|
||||
Self::DYNAMIC => {
|
||||
(size_of::<DynamicSlot>(), align_of::<DynamicSlot>(), 2, O::Dynamic, D::Dynamic)
|
||||
}
|
||||
_ => return Err(E::new(C::UnsupportedFormat, R::Builtin)),
|
||||
};
|
||||
Ok(StorageLayout::new(size, alignment, slots, ownership, default))
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct FieldLayout {
|
||||
pub type_id: TypeId,
|
||||
pub offset: usize,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct OwnedField {
|
||||
pub offset: usize,
|
||||
pub ownership: Ownership,
|
||||
}
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct StructLayout {
|
||||
pub size: usize,
|
||||
pub alignment: usize,
|
||||
pub fields: Vec<FieldLayout>,
|
||||
/// Sorted offsets for fields requiring guest tracing or native ownership handling.
|
||||
pub owned_fields: Vec<OwnedField>,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ArrayLayout {
|
||||
pub element_type_id: TypeId,
|
||||
pub stride: usize,
|
||||
pub element_alignment: usize,
|
||||
}
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TypeLayout {
|
||||
pub storage: StorageLayout,
|
||||
pub instance: Option<StructLayout>,
|
||||
pub array: Option<ArrayLayout>,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct FunctionLayout {
|
||||
pub parameter_slots: u32,
|
||||
pub result_slots: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Registry {
|
||||
declarations: RegistryDeclarations,
|
||||
layouts: Vec<TypeLayout>,
|
||||
functions: Vec<FunctionLayout>,
|
||||
// Sorted by owner/trait; original implementation IDs are never remapped.
|
||||
implementations: Vec<(TypeId, TypeId, ImplementationId)>,
|
||||
}
|
||||
impl Registry {
|
||||
pub fn declarations(&self) -> &RegistryDeclarations {
|
||||
&self.declarations
|
||||
}
|
||||
pub fn type_layout(&self, id: TypeId) -> Option<&TypeLayout> {
|
||||
self.layouts.get(id.0 as usize)
|
||||
}
|
||||
pub fn function_layout(&self, id: FunctionId) -> Option<&FunctionLayout> {
|
||||
self.functions.get(id.0 as usize)
|
||||
}
|
||||
pub fn implementation(&self, owner: TypeId, trait_id: TypeId) -> Option<ImplementationId> {
|
||||
self.implementations
|
||||
.binary_search_by_key(&(owner, trait_id), |&(a, b, _)| (a, b))
|
||||
.ok()
|
||||
.map(|i| self.implementations[i].2)
|
||||
}
|
||||
/// Resolves declarations, not code/bindings. The caller must still verify before publication.
|
||||
/// Declaration allocations are retained and charged here, even for non-PBX producers.
|
||||
pub fn resolve(
|
||||
declarations: RegistryDeclarations,
|
||||
code_len: usize,
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<Self, E> {
|
||||
budget.transaction(|budget| Self::resolve_inner(declarations, code_len, budget))
|
||||
}
|
||||
fn resolve_inner(
|
||||
d: RegistryDeclarations,
|
||||
code_len: usize,
|
||||
b: &mut LoadBudget,
|
||||
) -> Result<Self, E> {
|
||||
b.check(Q::Types, d.types.len() as u64)?;
|
||||
b.check(Q::Functions, d.functions.len() as u64)?;
|
||||
b.check(Q::Implementations, d.implementations.len() as u64)?;
|
||||
b.check(Q::CodeBytes, code_len as u64)?;
|
||||
charge_declarations(&d, b)?;
|
||||
validate_declarations(&d, b)?;
|
||||
let mut layouts = b.vector(d.types.len(), Q::RegistryBytes)?;
|
||||
for (i, t) in d.types.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
let storage = match t.kind {
|
||||
TypeKind::Builtin(id) => id.layout().map_err(|e| e.record(i as u32))?,
|
||||
TypeKind::Trait(_) => {
|
||||
StorageLayout::new(16, 8, 2, Ownership::TraitReceiver, DefaultKind::Trait)
|
||||
}
|
||||
_ => StorageLayout::new(8, 8, 1, Ownership::GuestReference, DefaultKind::Null),
|
||||
};
|
||||
layouts.push(TypeLayout { storage, instance: None, array: None });
|
||||
}
|
||||
for (i, t) in d.types.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
if let TypeKind::Array(element_type_id) = t.kind {
|
||||
let element = layouts[element_type_id.0 as usize].storage;
|
||||
layouts[i].array = Some(ArrayLayout {
|
||||
element_type_id,
|
||||
stride: align(element.size, element.alignment)?,
|
||||
element_alignment: element.alignment,
|
||||
});
|
||||
}
|
||||
if let TypeKind::Struct(fields) = &t.kind {
|
||||
let mut offsets = b.vector(fields.len(), Q::RegistryBytes)?;
|
||||
b.work(fields.len())?;
|
||||
let owned_count = fields
|
||||
.iter()
|
||||
.filter(|f| layouts[f.type_id.0 as usize].storage.ownership != Ownership::Plain)
|
||||
.count();
|
||||
let mut owned = b.vector(owned_count, Q::RegistryBytes)?;
|
||||
let (mut size, mut alignment) = (0, 1);
|
||||
for field in fields {
|
||||
b.work(1)?;
|
||||
let layout = layouts[field.type_id.0 as usize].storage;
|
||||
size = align(size, layout.alignment)?;
|
||||
offsets.push(FieldLayout { type_id: field.type_id, offset: size });
|
||||
if layout.ownership != Ownership::Plain {
|
||||
owned.push(OwnedField { offset: size, ownership: layout.ownership });
|
||||
}
|
||||
size = size.checked_add(layout.size).ok_or_else(overflow)?;
|
||||
alignment = alignment.max(layout.alignment);
|
||||
}
|
||||
layouts[i].instance = Some(StructLayout {
|
||||
size: align(size, alignment)?,
|
||||
alignment,
|
||||
fields: offsets,
|
||||
owned_fields: owned,
|
||||
});
|
||||
}
|
||||
}
|
||||
let mut functions = b.vector(d.functions.len(), Q::RegistryBytes)?;
|
||||
let mut ranges = b.vector(d.functions.len(), Q::TemporaryBytes)?;
|
||||
for (i, f) in d.functions.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
let params = width(f.signature.parameters.iter().map(|p| p.type_id), &layouts, b)?;
|
||||
let results = width(f.signature.results.iter().copied(), &layouts, b)?;
|
||||
let frame = u64::from(params) + u64::from(f.local_slots) + u64::from(f.max_stack_slots);
|
||||
if frame > b.frame_limit() {
|
||||
return Err(E::new(C::InvalidCode, R::FrameSlots)
|
||||
.in_section(1)
|
||||
.record(i as u32)
|
||||
.in_function(i as u32, None));
|
||||
}
|
||||
if u64::from(results) > b.result_limit() {
|
||||
return Err(E::new(C::InvalidCode, R::ReturnSlots)
|
||||
.in_section(1)
|
||||
.record(i as u32)
|
||||
.in_function(i as u32, None));
|
||||
}
|
||||
let end = f.code_offset.checked_add(f.code_len).ok_or_else(|| {
|
||||
E::new(C::InvalidCode, R::CodeRange)
|
||||
.in_section(1)
|
||||
.record(i as u32)
|
||||
.in_function(i as u32, Some(f.code_offset))
|
||||
})?;
|
||||
if end as usize > code_len || f.code_len == 0 {
|
||||
return Err(E::new(C::InvalidCode, R::CodeRange)
|
||||
.in_section(1)
|
||||
.record(i as u32)
|
||||
.in_function(i as u32, Some(f.code_offset)));
|
||||
}
|
||||
ranges.push((f.code_offset, end, i as u32));
|
||||
functions.push(FunctionLayout { parameter_slots: params, result_slots: results });
|
||||
}
|
||||
sort_work(ranges.len(), b)?;
|
||||
ranges.sort_unstable();
|
||||
for pair in ranges.windows(2) {
|
||||
b.work(1)?;
|
||||
if pair[0].1 > pair[1].0 {
|
||||
return Err(E::new(C::InvalidCode, R::CodeRange).record(pair[1].2));
|
||||
}
|
||||
}
|
||||
b.refund(Q::TemporaryBytes, (ranges.capacity() * size_of::<(u32, u32, u32)>()) as u64);
|
||||
drop(ranges);
|
||||
let mut implementations = b.vector(d.implementations.len(), Q::RegistryBytes)?;
|
||||
for (i, imp) in d.implementations.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
if !matches!(d.types[imp.owner.0 as usize].kind, TypeKind::Struct(_)) {
|
||||
return Err(E::new(C::InvalidReference, R::OwnerCategory)
|
||||
.in_section(7)
|
||||
.record(i as u32)
|
||||
.reference(IdDomain::Type, imp.owner.0));
|
||||
}
|
||||
let TypeKind::Trait(methods) = &d.types[imp.trait_id.0 as usize].kind else {
|
||||
return Err(E::new(C::InvalidReference, R::TraitCategory)
|
||||
.in_section(7)
|
||||
.record(i as u32)
|
||||
.reference(IdDomain::Type, imp.trait_id.0));
|
||||
};
|
||||
if methods.len() != imp.methods.len() {
|
||||
return Err(E::new(C::InvalidRegistry, R::MethodCount)
|
||||
.in_section(7)
|
||||
.record(i as u32));
|
||||
}
|
||||
for (j, (method, id)) in methods.iter().zip(&imp.methods).enumerate() {
|
||||
let signature = &d.functions[id.0 as usize].signature;
|
||||
b.work(signature.parameters.len() + signature.results.len() + 1)?;
|
||||
if signature.parameters.first().map(|p| p.type_id) != Some(imp.owner)
|
||||
|| signature.parameters.len() != method.signature.parameters.len() + 1
|
||||
|| !signature.parameters.iter().skip(1).map(|p| p.type_id).eq(method
|
||||
.signature
|
||||
.parameters
|
||||
.iter()
|
||||
.map(|p| p.type_id))
|
||||
|| signature.results != method.signature.results
|
||||
{
|
||||
return Err(E::new(C::InvalidRegistry, R::Signature)
|
||||
.in_section(7)
|
||||
.record(i as u32)
|
||||
.member(j as u32));
|
||||
}
|
||||
}
|
||||
implementations.push((imp.owner, imp.trait_id, ImplementationId(i as u32)));
|
||||
}
|
||||
sort_work(implementations.len(), b)?;
|
||||
implementations.sort_unstable();
|
||||
for pair in implementations.windows(2) {
|
||||
b.work(1)?;
|
||||
if (pair[0].0, pair[0].1) == (pair[1].0, pair[1].1) {
|
||||
return Err(E::new(C::InvalidRegistry, R::DuplicateImplementation)
|
||||
.in_section(7)
|
||||
.record(pair[1].2 .0));
|
||||
}
|
||||
}
|
||||
Ok(Self { declarations: d, layouts, functions, implementations })
|
||||
}
|
||||
}
|
||||
fn overflow() -> E {
|
||||
E::new(C::ResourceLimit, R::Overflow)
|
||||
}
|
||||
fn align(n: usize, a: usize) -> Result<usize, E> {
|
||||
n.checked_add(a - 1).map(|v| v & !(a - 1)).ok_or_else(overflow)
|
||||
}
|
||||
fn width(
|
||||
ids: impl Iterator<Item = TypeId>,
|
||||
layouts: &[TypeLayout],
|
||||
b: &mut LoadBudget,
|
||||
) -> Result<u32, E> {
|
||||
let mut sum = 0u32;
|
||||
for id in ids {
|
||||
b.work(1)?;
|
||||
sum = sum.checked_add(layouts[id.0 as usize].storage.slots).ok_or_else(overflow)?;
|
||||
}
|
||||
Ok(sum)
|
||||
}
|
||||
fn sort_work(n: usize, b: &mut LoadBudget) -> Result<(), E> {
|
||||
let bits = usize::BITS - n.leading_zeros();
|
||||
b.work(n.checked_mul(bits as usize + 1).ok_or_else(overflow)?)
|
||||
}
|
||||
fn type_ref(id: TypeId, d: &RegistryDeclarations) -> Result<(), E> {
|
||||
if (id.0 as usize) < d.types.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(E::new(C::InvalidReference, R::TypeId).reference(IdDomain::Type, id.0))
|
||||
}
|
||||
}
|
||||
fn name(name: &Option<String>, b: &mut LoadBudget) -> Result<(), E> {
|
||||
if let Some(name) = name {
|
||||
if name.len() as u64 > b.individual_name_limit() {
|
||||
return Err(E::resource(Q::NameBytes, name.len() as u64, b.individual_name_limit()));
|
||||
}
|
||||
b.charge(Q::NameBytes, name.len() as u64)?;
|
||||
b.work(name.len())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn named(items: &[NamedType], d: &RegistryDeclarations, b: &mut LoadBudget) -> Result<(), E> {
|
||||
b.charge(Q::NestedRecords, items.len() as u64)?;
|
||||
for (i, item) in items.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
type_ref(item.type_id, d).map_err(|e| e.member(i as u32))?;
|
||||
name(&item.name, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn signature(s: &Signature, d: &RegistryDeclarations, b: &mut LoadBudget) -> Result<(), E> {
|
||||
named(&s.parameters, d, b)?;
|
||||
b.charge(Q::NestedRecords, s.results.len() as u64)?;
|
||||
for (i, id) in s.results.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
type_ref(*id, d).map_err(|e| e.member(i as u32))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Validate all edges before indexed layout resolution; IDs remain table positions.
|
||||
pub(crate) fn validate_declarations(d: &RegistryDeclarations, b: &mut LoadBudget) -> Result<(), E> {
|
||||
b.charge(Q::Types, d.types.len() as u64)?;
|
||||
b.charge(Q::Functions, d.functions.len() as u64)?;
|
||||
b.charge(Q::Implementations, d.implementations.len() as u64)?;
|
||||
let mut constructors = b.vector(d.types.len(), Q::TemporaryBytes)?;
|
||||
for (i, t) in d.types.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
name(&t.name, b).map_err(|e| e.in_section(6).record(i as u32))?;
|
||||
let result = (|| match &t.kind {
|
||||
TypeKind::Builtin(id) => {
|
||||
id.layout()?;
|
||||
constructors.push((0, id.0, i as u32));
|
||||
Ok(())
|
||||
}
|
||||
TypeKind::Array(id) => {
|
||||
type_ref(*id, d)?;
|
||||
constructors.push((1, id.0, i as u32));
|
||||
Ok(())
|
||||
}
|
||||
TypeKind::Struct(fields) => named(fields, d, b),
|
||||
TypeKind::Trait(methods) => {
|
||||
b.charge(Q::NestedRecords, methods.len() as u64)?;
|
||||
for m in methods {
|
||||
b.work(1)?;
|
||||
name(&m.name, b)?;
|
||||
signature(&m.signature, d, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
})();
|
||||
result.map_err(|e| e.in_section(6).record(i as u32))?;
|
||||
}
|
||||
sort_work(constructors.len(), b)?;
|
||||
constructors.sort_unstable();
|
||||
for pair in constructors.windows(2) {
|
||||
b.work(1)?;
|
||||
if (pair[0].0, pair[0].1) == (pair[1].0, pair[1].1) {
|
||||
return Err(E::new(
|
||||
C::InvalidRegistry,
|
||||
if pair[1].0 == 0 { R::DuplicateBuiltin } else { R::DuplicateArray },
|
||||
)
|
||||
.record(pair[1].2));
|
||||
}
|
||||
}
|
||||
b.refund(Q::TemporaryBytes, (constructors.capacity() * size_of::<(i32, u32, u32)>()) as u64);
|
||||
drop(constructors);
|
||||
// Iterative tri-color walk: every Array has one successor; nominal nodes terminate walks.
|
||||
let mut colors = b.vector::<u8>(d.types.len(), Q::TemporaryBytes)?;
|
||||
colors.resize(d.types.len(), 0);
|
||||
for start in 0..d.types.len() {
|
||||
b.work(1)?;
|
||||
let mut node = start;
|
||||
while colors[node] == 0 {
|
||||
b.work(1)?;
|
||||
colors[node] = 1;
|
||||
match d.types[node].kind {
|
||||
TypeKind::Array(id) => node = id.0 as usize,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
if colors[node] == 1 && matches!(d.types[node].kind, TypeKind::Array(_)) {
|
||||
return Err(E::new(C::InvalidRegistry, R::ArrayCycle).record(node as u32));
|
||||
}
|
||||
node = start;
|
||||
while colors[node] == 1 {
|
||||
b.work(1)?;
|
||||
colors[node] = 2;
|
||||
match d.types[node].kind {
|
||||
TypeKind::Array(id) => node = id.0 as usize,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
b.refund(Q::TemporaryBytes, colors.capacity() as u64);
|
||||
drop(colors);
|
||||
for (i, f) in d.functions.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
name(&f.name, b).map_err(|e| e.in_section(1).record(i as u32))?;
|
||||
signature(&f.signature, d, b).map_err(|e| e.in_section(1).record(i as u32))?;
|
||||
}
|
||||
for (i, imp) in d.implementations.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
type_ref(imp.owner, d).map_err(|e| e.in_section(7).record(i as u32))?;
|
||||
type_ref(imp.trait_id, d).map_err(|e| e.in_section(7).record(i as u32))?;
|
||||
b.charge(Q::NestedRecords, imp.methods.len() as u64)?;
|
||||
for (j, id) in imp.methods.iter().enumerate() {
|
||||
b.work(1)?;
|
||||
if id.0 as usize >= d.functions.len() {
|
||||
return Err(E::new(C::InvalidReference, R::FunctionId)
|
||||
.in_section(7)
|
||||
.reference(IdDomain::Function, id.0)
|
||||
.record(i as u32)
|
||||
.member(j as u32));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn charge_vec<T>(v: &Vec<T>, b: &mut LoadBudget) -> Result<(), E> {
|
||||
b.charge(
|
||||
Q::RegistryBytes,
|
||||
(v.capacity() as u64).checked_mul(size_of::<T>() as u64).ok_or_else(overflow)?,
|
||||
)
|
||||
}
|
||||
fn charge_name(n: &Option<String>, b: &mut LoadBudget) -> Result<(), E> {
|
||||
if let Some(n) = n {
|
||||
b.charge(Q::RegistryBytes, n.capacity() as u64)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn charge_named(v: &Vec<NamedType>, b: &mut LoadBudget) -> Result<(), E> {
|
||||
charge_vec(v, b)?;
|
||||
for p in v {
|
||||
b.work(1)?;
|
||||
charge_name(&p.name, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn charge_signature(s: &Signature, b: &mut LoadBudget) -> Result<(), E> {
|
||||
charge_named(&s.parameters, b)?;
|
||||
charge_vec(&s.results, b)
|
||||
}
|
||||
fn charge_declarations(d: &RegistryDeclarations, b: &mut LoadBudget) -> Result<(), E> {
|
||||
b.charge(Q::RegistryBytes, size_of::<Registry>() as u64)?;
|
||||
charge_vec(&d.types, b)?;
|
||||
charge_vec(&d.functions, b)?;
|
||||
charge_vec(&d.implementations, b)?;
|
||||
for t in &d.types {
|
||||
b.work(1)?;
|
||||
charge_name(&t.name, b)?;
|
||||
match &t.kind {
|
||||
TypeKind::Struct(fields) => charge_named(fields, b)?,
|
||||
TypeKind::Trait(methods) => {
|
||||
charge_vec(methods, b)?;
|
||||
for m in methods {
|
||||
b.work(1)?;
|
||||
charge_name(&m.name, b)?;
|
||||
charge_signature(&m.signature, b)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for f in &d.functions {
|
||||
b.work(1)?;
|
||||
charge_name(&f.name, b)?;
|
||||
charge_signature(&f.signature, b)?;
|
||||
}
|
||||
for imp in &d.implementations {
|
||||
b.work(1)?;
|
||||
charge_vec(&imp.methods, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,473 @@
|
||||
//! Strict PBX v1 envelope and Registry sections. No legacy fallback.
|
||||
//! Envelope parsing is structural only: non-Registry sections and instructions
|
||||
//! must be validated by their consumers before an executable image is published.
|
||||
use crate::load_limits::{
|
||||
LoadBudget, LoadCode as C, LoadFailure as E, LoadReason as R, LoadResource as Q,
|
||||
};
|
||||
use crate::registry::*;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Section<'a> {
|
||||
pub kind: u32,
|
||||
pub offset: usize,
|
||||
pub bytes: &'a [u8],
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct PbxEnvelope<'a> {
|
||||
sections: [Option<Section<'a>>; 8],
|
||||
}
|
||||
impl<'a> PbxEnvelope<'a> {
|
||||
pub fn section(&self, kind: u32) -> Option<Section<'a>> {
|
||||
self.sections.get(kind as usize).copied().flatten()
|
||||
}
|
||||
pub fn parse(bytes: &'a [u8], b: &mut LoadBudget) -> Result<Self, E> {
|
||||
b.transaction(|b| Self::parse_inner(bytes, b))
|
||||
}
|
||||
fn parse_inner(bytes: &'a [u8], b: &mut LoadBudget) -> Result<Self, E> {
|
||||
b.check(Q::PbxBytes, bytes.len() as u64)?;
|
||||
if bytes.len() < 32 {
|
||||
return Err(E::new(C::InvalidFormat, R::Truncated));
|
||||
}
|
||||
b.work(32)?;
|
||||
if &bytes[..4] != b"PBX\0" {
|
||||
return Err(E::new(C::InvalidFormat, R::Magic));
|
||||
}
|
||||
if bytes[4..6] != [1, 0] {
|
||||
return Err(E::new(C::UnsupportedFormat, R::Version));
|
||||
}
|
||||
if bytes[6] != 0 {
|
||||
return Err(E::new(C::InvalidFormat, R::Endianness));
|
||||
}
|
||||
if bytes[7] != 0 || bytes[12..32].iter().any(|&x| x != 0) {
|
||||
return Err(E::new(C::InvalidFormat, R::Reserved));
|
||||
}
|
||||
let count = u32::from_le_bytes(bytes[8..12].try_into().unwrap()) as usize;
|
||||
let directory_end =
|
||||
count.checked_mul(12).and_then(|n| n.checked_add(32)).ok_or_else(overflow)?;
|
||||
if directory_end > bytes.len() {
|
||||
return Err(E::new(C::InvalidFormat, R::Truncated));
|
||||
}
|
||||
let mut result = Self { sections: [None; 8] };
|
||||
for i in 0..count {
|
||||
b.work(13)?;
|
||||
let pos = 32 + i * 12;
|
||||
let kind = u32::from_le_bytes(bytes[pos..pos + 4].try_into().unwrap());
|
||||
if kind > 7 {
|
||||
return Err(E::new(C::UnsupportedFormat, R::SectionKind).at(kind, pos));
|
||||
}
|
||||
if result.sections[kind as usize].is_some() {
|
||||
return Err(E::new(C::InvalidFormat, R::DuplicateSection).at(kind, pos));
|
||||
}
|
||||
let start = u32::from_le_bytes(bytes[pos + 4..pos + 8].try_into().unwrap()) as usize;
|
||||
let len = u32::from_le_bytes(bytes[pos + 8..pos + 12].try_into().unwrap()) as usize;
|
||||
let end = start.checked_add(len).ok_or_else(overflow)?;
|
||||
if end > bytes.len() {
|
||||
return Err(E::new(C::InvalidFormat, R::Truncated).at(kind, start));
|
||||
}
|
||||
if start < directory_end {
|
||||
return Err(E::new(C::InvalidFormat, R::Overlap).at(kind, start));
|
||||
}
|
||||
if kind == 2 {
|
||||
b.check(Q::CodeBytes, len as u64)?;
|
||||
}
|
||||
for other in result.sections.iter().flatten() {
|
||||
b.work(1)?;
|
||||
if len > 0
|
||||
&& !other.bytes.is_empty()
|
||||
&& start < other.offset + other.bytes.len()
|
||||
&& other.offset < end
|
||||
{
|
||||
return Err(E::new(C::InvalidFormat, R::Overlap).at(kind, start));
|
||||
}
|
||||
}
|
||||
result.sections[kind as usize] =
|
||||
Some(Section { kind, offset: start, bytes: &bytes[start..end] });
|
||||
}
|
||||
for kind in [0, 1, 2, 4, 5, 6, 7] {
|
||||
if result.sections[kind].is_none() {
|
||||
return Err(E::new(C::InvalidFormat, R::MissingSection).at(kind as u32, 8));
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
/// Decode and resolve Registry metadata only. This is not an executable loader.
|
||||
pub fn resolve_registry(&self, b: &mut LoadBudget) -> Result<Registry, E> {
|
||||
b.transaction(|b| {
|
||||
let resources = [
|
||||
Q::TemporaryBytes,
|
||||
Q::Types,
|
||||
Q::Functions,
|
||||
Q::Implementations,
|
||||
Q::NestedRecords,
|
||||
Q::NameBytes,
|
||||
];
|
||||
let before = resources.map(|r| b.used(r));
|
||||
let types = Reader::new(self.section(6).unwrap(), b).types()?;
|
||||
let functions = Reader::new(self.section(1).unwrap(), b).functions()?;
|
||||
let implementations = Reader::new(self.section(7).unwrap(), b).implementations()?;
|
||||
// Ownership moves from parse scratch to retained Registry. No clone or
|
||||
// simultaneous second declaration allocation occurs at this boundary.
|
||||
for (r, used) in resources.into_iter().zip(before) {
|
||||
b.refund(r, b.used(r) - used);
|
||||
}
|
||||
Registry::resolve(
|
||||
RegistryDeclarations { types, functions, implementations },
|
||||
self.section(2).unwrap().bytes.len(),
|
||||
b,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
fn overflow() -> E {
|
||||
E::new(C::ResourceLimit, R::Overflow)
|
||||
}
|
||||
struct Reader<'a, 'b> {
|
||||
section: Section<'a>,
|
||||
pos: usize,
|
||||
budget: &'b mut LoadBudget,
|
||||
}
|
||||
impl<'a, 'b> Reader<'a, 'b> {
|
||||
fn new(section: Section<'a>, budget: &'b mut LoadBudget) -> Self {
|
||||
Self { section, pos: 0, budget }
|
||||
}
|
||||
fn error(&self, code: C, reason: R) -> E {
|
||||
E::new(code, reason).at(self.section.kind, self.section.offset + self.pos)
|
||||
}
|
||||
fn take(&mut self, n: usize) -> Result<&'a [u8], E> {
|
||||
let end = self.pos.checked_add(n).ok_or_else(overflow)?;
|
||||
let value = self
|
||||
.section
|
||||
.bytes
|
||||
.get(self.pos..end)
|
||||
.ok_or_else(|| self.error(C::InvalidFormat, R::Truncated))?;
|
||||
self.budget.work(n)?;
|
||||
self.pos = end;
|
||||
Ok(value)
|
||||
}
|
||||
fn byte(&mut self) -> Result<u8, E> {
|
||||
Ok(self.take(1)?[0])
|
||||
}
|
||||
fn u32(&mut self) -> Result<u32, E> {
|
||||
Ok(u32::from_le_bytes(self.take(4)?.try_into().unwrap()))
|
||||
}
|
||||
fn finish(&self) -> Result<(), E> {
|
||||
if self.pos == self.section.bytes.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.error(C::InvalidFormat, R::TrailingBytes))
|
||||
}
|
||||
}
|
||||
fn list<T>(&mut self, resource: Q, min_bytes: usize) -> Result<(Vec<T>, usize), E> {
|
||||
let count = self.u32()? as usize;
|
||||
self.budget.charge(resource, count as u64)?;
|
||||
let min = count.checked_mul(min_bytes).ok_or_else(overflow)?;
|
||||
if min > self.section.bytes.len() - self.pos {
|
||||
return Err(self.error(C::InvalidFormat, R::Truncated));
|
||||
}
|
||||
Ok((self.budget.vector(count, Q::TemporaryBytes)?, count))
|
||||
}
|
||||
fn name(&mut self) -> Result<Option<String>, E> {
|
||||
match self.byte()? {
|
||||
0 => Ok(None),
|
||||
1 => {
|
||||
let len = self.u32()? as usize;
|
||||
if len as u64 > self.budget.individual_name_limit() {
|
||||
return Err(E::resource(
|
||||
Q::NameBytes,
|
||||
len as u64,
|
||||
self.budget.individual_name_limit(),
|
||||
)
|
||||
.at(self.section.kind, self.section.offset + self.pos));
|
||||
}
|
||||
self.budget.charge(Q::NameBytes, len as u64)?;
|
||||
let bytes = self.take(len)?;
|
||||
self.budget.work(len)?;
|
||||
let value = std::str::from_utf8(bytes)
|
||||
.map_err(|_| self.error(C::InvalidFormat, R::Utf8))?;
|
||||
self.budget.work(len)?;
|
||||
let mut storage = self.budget.vector::<u8>(len, Q::TemporaryBytes)?;
|
||||
storage.extend_from_slice(value.as_bytes());
|
||||
// UTF-8 was checked above; moving Vec into String performs no allocation.
|
||||
Ok(Some(
|
||||
String::from_utf8(storage)
|
||||
.map_err(|_| self.error(C::InvalidFormat, R::Utf8))?,
|
||||
))
|
||||
}
|
||||
_ => Err(self.error(C::InvalidFormat, R::InvalidFlag)),
|
||||
}
|
||||
}
|
||||
fn named(&mut self) -> Result<Vec<NamedType>, E> {
|
||||
let (mut out, n) = self.list(Q::NestedRecords, 5)?;
|
||||
for i in 0..n {
|
||||
self.budget.work(1)?;
|
||||
let type_id = TypeId(self.u32()?);
|
||||
let name = self.name().map_err(|e| e.member(i as u32))?;
|
||||
out.push(NamedType { type_id, name });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
fn signature(&mut self) -> Result<Signature, E> {
|
||||
let parameters = self.named()?;
|
||||
let (mut results, n) = self.list(Q::NestedRecords, 4)?;
|
||||
for _ in 0..n {
|
||||
self.budget.work(1)?;
|
||||
results.push(TypeId(self.u32()?));
|
||||
}
|
||||
Ok(Signature { parameters, results })
|
||||
}
|
||||
fn types(mut self) -> Result<Vec<TypeDecl>, E> {
|
||||
let (mut out, n) = self.list(Q::Types, 6)?;
|
||||
for i in 0..n {
|
||||
self.budget.work(1)?;
|
||||
let record = (|| {
|
||||
let kind = self.byte()?;
|
||||
let name = self.name()?;
|
||||
let kind = match kind {
|
||||
0 => TypeKind::Builtin(BuiltinId(self.u32()?)),
|
||||
1 => TypeKind::Struct(self.named()?),
|
||||
2 => TypeKind::Array(TypeId(self.u32()?)),
|
||||
3 => {
|
||||
let (mut methods, n) = self.list(Q::NestedRecords, 9)?;
|
||||
for j in 0..n {
|
||||
self.budget.work(1)?;
|
||||
let name = self.name()?;
|
||||
let signature = self.signature().map_err(|e| e.member(j as u32))?;
|
||||
methods.push(MethodDecl { name, signature });
|
||||
}
|
||||
TypeKind::Trait(methods)
|
||||
}
|
||||
_ => return Err(self.error(C::UnsupportedFormat, R::Category)),
|
||||
};
|
||||
Ok(TypeDecl { name, kind })
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
out.push(record);
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
fn functions(mut self) -> Result<Vec<FunctionDecl>, E> {
|
||||
let (mut out, n) = self.list(Q::Functions, 25)?;
|
||||
for i in 0..n {
|
||||
self.budget.work(1)?;
|
||||
let f = (|| {
|
||||
Ok(FunctionDecl {
|
||||
name: self.name()?,
|
||||
signature: self.signature()?,
|
||||
code_offset: self.u32()?,
|
||||
code_len: self.u32()?,
|
||||
local_slots: self.u32()?,
|
||||
max_stack_slots: self.u32()?,
|
||||
})
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
out.push(f);
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
fn implementations(mut self) -> Result<Vec<ImplementationDecl>, E> {
|
||||
let (mut out, n) = self.list(Q::Implementations, 12)?;
|
||||
for i in 0..n {
|
||||
self.budget.work(1)?;
|
||||
let imp = (|| {
|
||||
let owner = TypeId(self.u32()?);
|
||||
let trait_id = TypeId(self.u32()?);
|
||||
let (mut methods, n) = self.list(Q::NestedRecords, 4)?;
|
||||
for _ in 0..n {
|
||||
self.budget.work(1)?;
|
||||
methods.push(FunctionId(self.u32()?));
|
||||
}
|
||||
Ok(ImplementationDecl { owner, trait_id, methods })
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
out.push(imp);
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw section input for a v1 envelope writer. Non-Registry section bytes must
|
||||
/// come from their own validated writers; this API never invents defaults.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct SectionData<'a> {
|
||||
pub kind: u32,
|
||||
pub bytes: &'a [u8],
|
||||
}
|
||||
/// Assemble strict v1 sections. The output buffer is charged to temporary load storage.
|
||||
pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
||||
b.transaction(|b| {
|
||||
let mut by_kind = [None; 8];
|
||||
for section in sections {
|
||||
b.work(1)?;
|
||||
if section.kind > 7 {
|
||||
return Err(E::new(C::UnsupportedFormat, R::SectionKind));
|
||||
}
|
||||
if by_kind[section.kind as usize].replace(section.bytes).is_some() {
|
||||
return Err(E::new(C::InvalidFormat, R::DuplicateSection));
|
||||
}
|
||||
}
|
||||
for k in [0, 1, 2, 4, 5, 6, 7] {
|
||||
if by_kind[k].is_none() {
|
||||
return Err(E::new(C::InvalidFormat, R::MissingSection).at(k as u32, 8));
|
||||
}
|
||||
}
|
||||
b.check(Q::CodeBytes, by_kind[2].unwrap().len() as u64)?;
|
||||
let count = sections.len();
|
||||
let directory_end = 32 + 12 * count;
|
||||
let total = sections
|
||||
.iter()
|
||||
.try_fold(directory_end, |n, s| n.checked_add(s.bytes.len()).ok_or_else(overflow))?;
|
||||
b.check(Q::PbxBytes, total as u64)?;
|
||||
b.work(total)?;
|
||||
let mut out = b.vector(total, Q::TemporaryBytes)?;
|
||||
out.extend_from_slice(b"PBX\0");
|
||||
out.extend_from_slice(&[1, 0, 0, 0]);
|
||||
out.extend_from_slice(&(count as u32).to_le_bytes());
|
||||
out.extend_from_slice(&[0; 20]);
|
||||
let mut offset = directory_end;
|
||||
for (kind, data) in by_kind.iter().enumerate() {
|
||||
if let Some(data) = data {
|
||||
out.extend_from_slice(&(kind as u32).to_le_bytes());
|
||||
out.extend_from_slice(&(offset as u32).to_le_bytes());
|
||||
out.extend_from_slice(&(data.len() as u32).to_le_bytes());
|
||||
offset += data.len();
|
||||
}
|
||||
}
|
||||
for data in by_kind.into_iter().flatten() {
|
||||
out.extend_from_slice(data);
|
||||
}
|
||||
Ok(out)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RegistrySections {
|
||||
pub types: Vec<u8>,
|
||||
pub functions: Vec<u8>,
|
||||
pub implementations: Vec<u8>,
|
||||
}
|
||||
/// Field-by-field serialization of already resolved Registry declarations.
|
||||
/// There is no source-language inference or unsupported-value-to-null fallback.
|
||||
pub fn encode_registry(registry: &Registry, b: &mut LoadBudget) -> Result<RegistrySections, E> {
|
||||
b.transaction(|b| {
|
||||
let d = registry.declarations();
|
||||
let types = write_section(b, |w| {
|
||||
w.count(d.types.len())?;
|
||||
for t in &d.types {
|
||||
w.byte(match t.kind {
|
||||
TypeKind::Builtin(_) => 0,
|
||||
TypeKind::Struct(_) => 1,
|
||||
TypeKind::Array(_) => 2,
|
||||
TypeKind::Trait(_) => 3,
|
||||
})?;
|
||||
w.name(&t.name)?;
|
||||
match &t.kind {
|
||||
TypeKind::Builtin(id) => w.u32(id.0)?,
|
||||
TypeKind::Struct(fields) => w.named(fields)?,
|
||||
TypeKind::Array(id) => w.u32(id.0)?,
|
||||
TypeKind::Trait(methods) => {
|
||||
w.count(methods.len())?;
|
||||
for m in methods {
|
||||
w.name(&m.name)?;
|
||||
w.signature(&m.signature)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
let functions = write_section(b, |w| {
|
||||
w.count(d.functions.len())?;
|
||||
for f in &d.functions {
|
||||
w.name(&f.name)?;
|
||||
w.signature(&f.signature)?;
|
||||
w.u32(f.code_offset)?;
|
||||
w.u32(f.code_len)?;
|
||||
w.u32(f.local_slots)?;
|
||||
w.u32(f.max_stack_slots)?;
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
let implementations = write_section(b, |w| {
|
||||
w.count(d.implementations.len())?;
|
||||
for imp in &d.implementations {
|
||||
w.u32(imp.owner.0)?;
|
||||
w.u32(imp.trait_id.0)?;
|
||||
w.count(imp.methods.len())?;
|
||||
for id in &imp.methods {
|
||||
w.u32(id.0)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(RegistrySections { types, functions, implementations })
|
||||
})
|
||||
}
|
||||
// Count first so a malicious producer cannot trigger unbounded geometric growth.
|
||||
struct Writer<'a> {
|
||||
out: Option<&'a mut Vec<u8>>,
|
||||
len: usize,
|
||||
budget: &'a mut LoadBudget,
|
||||
}
|
||||
impl Writer<'_> {
|
||||
fn bytes(&mut self, bytes: &[u8]) -> Result<(), E> {
|
||||
self.budget.work(bytes.len())?;
|
||||
self.len = self.len.checked_add(bytes.len()).ok_or_else(overflow)?;
|
||||
self.budget.check(Q::PbxBytes, self.len as u64)?;
|
||||
if let Some(out) = self.out.as_mut() {
|
||||
out.extend_from_slice(bytes);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn byte(&mut self, n: u8) -> Result<(), E> {
|
||||
self.bytes(&[n])
|
||||
}
|
||||
fn u32(&mut self, n: u32) -> Result<(), E> {
|
||||
self.bytes(&n.to_le_bytes())
|
||||
}
|
||||
fn count(&mut self, n: usize) -> Result<(), E> {
|
||||
self.u32(u32::try_from(n).map_err(|_| overflow())?)
|
||||
}
|
||||
fn name(&mut self, n: &Option<String>) -> Result<(), E> {
|
||||
match n {
|
||||
None => self.byte(0),
|
||||
Some(n) => {
|
||||
self.byte(1)?;
|
||||
self.count(n.len())?;
|
||||
self.bytes(n.as_bytes())
|
||||
}
|
||||
}
|
||||
}
|
||||
fn named(&mut self, items: &[NamedType]) -> Result<(), E> {
|
||||
self.count(items.len())?;
|
||||
for p in items {
|
||||
self.u32(p.type_id.0)?;
|
||||
self.name(&p.name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn signature(&mut self, s: &Signature) -> Result<(), E> {
|
||||
self.named(&s.parameters)?;
|
||||
self.count(s.results.len())?;
|
||||
for id in &s.results {
|
||||
self.u32(id.0)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn write_section(
|
||||
b: &mut LoadBudget,
|
||||
write: impl Fn(&mut Writer<'_>) -> Result<(), E>,
|
||||
) -> Result<Vec<u8>, E> {
|
||||
let mut counter = Writer { out: None, len: 0, budget: b };
|
||||
write(&mut counter)?;
|
||||
let len = counter.len;
|
||||
let mut out = b.vector(len, Q::TemporaryBytes)?;
|
||||
write(&mut Writer { out: Some(&mut out), len: 0, budget: b })?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
mod payloads;
|
||||
pub use payloads::ParsedPbx;
|
||||
@@ -0,0 +1,244 @@
|
||||
//! Validation of unchanged non-Registry section encodings, without allocating
|
||||
//! guest constants or publishing an executable image.
|
||||
use super::*;
|
||||
use crate::load_limits::IdDomain;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// All PBX tables have passed binary and reference validation. Code has NOT
|
||||
/// passed instruction/type-flow verification and host calls are NOT bound.
|
||||
#[derive(Debug)]
|
||||
pub struct ParsedPbx<'a> {
|
||||
envelope: PbxEnvelope<'a>,
|
||||
registry: Registry,
|
||||
}
|
||||
impl<'a> ParsedPbx<'a> {
|
||||
pub fn parse(bytes: &'a [u8], budget: &mut LoadBudget) -> Result<Self, E> {
|
||||
budget.transaction(|budget| {
|
||||
let envelope = PbxEnvelope::parse(bytes, budget)?;
|
||||
let registry = envelope.resolve_registry(budget)?;
|
||||
let functions = registry.declarations().functions.len();
|
||||
let code_len = envelope.section(2).unwrap().bytes.len();
|
||||
Reader::new(envelope.section(0).unwrap(), budget).constants()?;
|
||||
Reader::new(envelope.section(4).unwrap(), budget).exports(functions)?;
|
||||
Reader::new(envelope.section(5).unwrap(), budget).syscalls()?;
|
||||
if let Some(debug) = envelope.section(3) {
|
||||
Reader::new(debug, budget).debug(functions, code_len)?;
|
||||
}
|
||||
Ok(Self { envelope, registry })
|
||||
})
|
||||
}
|
||||
pub fn registry(&self) -> &Registry {
|
||||
&self.registry
|
||||
}
|
||||
pub fn section(&self, kind: u32) -> Option<Section<'a>> {
|
||||
self.envelope.section(kind)
|
||||
}
|
||||
/// Re-encode validated tables in canonical directory order; debug absence is preserved.
|
||||
pub fn encode(&self, budget: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
||||
let mut sections = [SectionData { kind: 0, bytes: &[] }; 8];
|
||||
let mut count = 0;
|
||||
for kind in 0..8 {
|
||||
if let Some(section) = self.section(kind) {
|
||||
sections[count] = SectionData { kind, bytes: section.bytes };
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
encode_envelope(§ions[..count], budget)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Reader<'a, '_> {
|
||||
fn u16(&mut self) -> Result<u16, E> {
|
||||
Ok(u16::from_le_bytes(self.take(2)?.try_into().unwrap()))
|
||||
}
|
||||
fn raw_count(&mut self, minimum: usize) -> Result<usize, E> {
|
||||
let count = self.u32()? as usize;
|
||||
if count.checked_mul(minimum).ok_or_else(overflow)? > self.section.bytes.len() - self.pos {
|
||||
return Err(self.error(C::InvalidFormat, R::Truncated));
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
fn utf8(&mut self, len: usize) -> Result<&'a str, E> {
|
||||
let bytes = self.take(len)?;
|
||||
self.budget.work(len)?;
|
||||
std::str::from_utf8(bytes).map_err(|_| self.error(C::InvalidFormat, R::Utf8))
|
||||
}
|
||||
fn text(&mut self, len: usize) -> Result<&'a str, E> {
|
||||
if len as u64 > self.budget.individual_name_limit() {
|
||||
return Err(E::resource(Q::NameBytes, len as u64, self.budget.individual_name_limit())
|
||||
.at(self.section.kind, self.section.offset + self.pos));
|
||||
}
|
||||
self.utf8(len)
|
||||
}
|
||||
fn function(&self, id: u32, count: usize) -> Result<(), E> {
|
||||
if id as usize >= count {
|
||||
Err(self.error(C::InvalidReference, R::FunctionId).reference(IdDomain::Function, id))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn constants(mut self) -> Result<(), E> {
|
||||
let count = self.u32()? as usize;
|
||||
self.budget.charge(Q::ConstantEntries, count as u64)?;
|
||||
if count > self.section.bytes.len() - self.pos {
|
||||
return Err(self.error(C::InvalidFormat, R::Truncated));
|
||||
}
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
(|| {
|
||||
match self.byte()? {
|
||||
0 => {}
|
||||
1 | 2 => {
|
||||
self.take(8)?;
|
||||
}
|
||||
3 => {
|
||||
if self.byte()? > 1 {
|
||||
return Err(self.error(C::InvalidFormat, R::InvalidFlag));
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
let len = self.u32()? as usize;
|
||||
if len as u64 > self.budget.individual_string_limit() {
|
||||
return Err(E::resource(
|
||||
Q::ConstantStringBytes,
|
||||
len as u64,
|
||||
self.budget.individual_string_limit(),
|
||||
)
|
||||
.at(0, self.section.offset + self.pos));
|
||||
}
|
||||
self.budget.charge(Q::ConstantStringBytes, len as u64)?;
|
||||
self.utf8(len)?;
|
||||
}
|
||||
5 => {
|
||||
self.take(4)?;
|
||||
}
|
||||
_ => return Err(self.error(C::UnsupportedFormat, R::ConstantTag)),
|
||||
}
|
||||
Ok(())
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
}
|
||||
self.finish()
|
||||
}
|
||||
fn exports(mut self, functions: usize) -> Result<(), E> {
|
||||
let count = self.raw_count(8)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let id = self.u32()?;
|
||||
self.function(id, functions).map_err(|e| e.record(i as u32))?;
|
||||
let len = self.u32()? as usize;
|
||||
self.text(len).map_err(|e| e.record(i as u32))?;
|
||||
}
|
||||
self.finish()
|
||||
}
|
||||
fn debug(mut self, functions: usize, code_len: usize) -> Result<(), E> {
|
||||
let count = self.raw_count(16)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let pc = self.u32()?;
|
||||
self.u32()?; // source file ID, local to debug information
|
||||
let start = self.u32()?;
|
||||
let end = self.u32()?;
|
||||
if pc as usize >= code_len || end < start {
|
||||
return Err(self.error(C::InvalidFormat, R::DebugRange).record(i as u32));
|
||||
}
|
||||
}
|
||||
let names = self.raw_count(8)?;
|
||||
for i in 0..names {
|
||||
self.budget.work(1)?;
|
||||
let id = self.u32()?;
|
||||
self.function(id, functions).map_err(|e| e.record(i as u32))?;
|
||||
let len = self.u32()? as usize;
|
||||
self.text(len).map_err(|e| e.record(i as u32))?;
|
||||
}
|
||||
self.finish()
|
||||
}
|
||||
fn syscalls(mut self) -> Result<(), E> {
|
||||
let count = self.raw_count(10)?;
|
||||
let mut identities = self.budget.vector(count, Q::TemporaryBytes)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let len = self.u16()? as usize;
|
||||
let module = self.text(len).map_err(|e| e.record(i as u32))?;
|
||||
let len = self.u16()? as usize;
|
||||
let name = self.text(len).map_err(|e| e.record(i as u32))?;
|
||||
let version = self.u16()?;
|
||||
self.u16()?; // argument slots, unchanged SYSC encoding
|
||||
self.u16()?; // result slots
|
||||
identities.push((module, name, version, i as u32));
|
||||
}
|
||||
self.finish()?;
|
||||
// Fallible heapsort bounds both auxiliary storage and comparison work;
|
||||
// unlike sorting with an error flag it stops immediately at quota refusal.
|
||||
fallible_sort(&mut identities, |a, b| {
|
||||
let module = text_cmp(a.0, b.0, self.budget)?;
|
||||
if module != Ordering::Equal {
|
||||
return Ok(module);
|
||||
}
|
||||
let name = text_cmp(a.1, b.1, self.budget)?;
|
||||
if name != Ordering::Equal {
|
||||
return Ok(name);
|
||||
}
|
||||
self.budget.work(1)?;
|
||||
Ok((a.2, a.3).cmp(&(b.2, b.3)))
|
||||
})?;
|
||||
for pair in identities.windows(2) {
|
||||
self.budget.work(1)?;
|
||||
if text_cmp(pair[0].0, pair[1].0, self.budget)? == Ordering::Equal
|
||||
&& text_cmp(pair[0].1, pair[1].1, self.budget)? == Ordering::Equal
|
||||
&& pair[0].2 == pair[1].2
|
||||
{
|
||||
return Err(self.error(C::InvalidFormat, R::DuplicateSyscall).record(pair[1].3));
|
||||
}
|
||||
}
|
||||
self.budget.refund(
|
||||
Q::TemporaryBytes,
|
||||
(identities.capacity() * size_of::<(&str, &str, u16, u32)>()) as u64,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn text_cmp(a: &str, b: &str, budget: &mut LoadBudget) -> Result<Ordering, E> {
|
||||
for (a, b) in a.bytes().zip(b.bytes()) {
|
||||
budget.work(2)?;
|
||||
match a.cmp(&b) {
|
||||
Ordering::Equal => {}
|
||||
other => return Ok(other),
|
||||
}
|
||||
}
|
||||
budget.work(1)?;
|
||||
Ok(a.len().cmp(&b.len()))
|
||||
}
|
||||
fn fallible_sort<T>(
|
||||
items: &mut [T],
|
||||
mut cmp: impl FnMut(&T, &T) -> Result<Ordering, E>,
|
||||
) -> Result<(), E> {
|
||||
fn sift<T>(
|
||||
items: &mut [T],
|
||||
mut root: usize,
|
||||
cmp: &mut impl FnMut(&T, &T) -> Result<Ordering, E>,
|
||||
) -> Result<(), E> {
|
||||
while let Some(child) =
|
||||
root.checked_mul(2).and_then(|n| n.checked_add(1)).filter(|&i| i < items.len())
|
||||
{
|
||||
let mut child = child;
|
||||
if child + 1 < items.len() && cmp(&items[child], &items[child + 1])? == Ordering::Less {
|
||||
child += 1;
|
||||
}
|
||||
if cmp(&items[root], &items[child])? != Ordering::Less {
|
||||
break;
|
||||
}
|
||||
items.swap(root, child);
|
||||
root = child;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
for root in (0..items.len() / 2).rev() {
|
||||
sift(items, root, &mut cmp)?;
|
||||
}
|
||||
for end in (1..items.len()).rev() {
|
||||
items.swap(0, end);
|
||||
sift(&mut items[..end], 0, &mut cmp)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
//! 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::{Registry, TypeId, TypeKind};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct RegistryStackEffect {
|
||||
pub pops: u32,
|
||||
pub pushes: u32,
|
||||
}
|
||||
|
||||
impl DecodedInstr<'_> {
|
||||
/// 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::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)
|
||||
}
|
||||
_ => unreachable!("requires_registry is defined by the typed opcode catalogue"),
|
||||
};
|
||||
Ok(Some(RegistryStackEffect { pops, pushes }))
|
||||
}
|
||||
}
|
||||
@@ -10,17 +10,28 @@ thread_local! {
|
||||
|
||||
/// Opaque handle that references an object stored in the VM heap.
|
||||
///
|
||||
/// This is an index-based handle. It does not imply ownership and carries
|
||||
/// no lifetime information. GC/allocator integration will come in later PRs.
|
||||
/// Handles belong to the heap/session that issued them. Native boundaries must
|
||||
/// preserve that provenance. Generation zero is reserved; compact null is (0, 0).
|
||||
#[repr(C, align(8))]
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub struct HeapRef(pub u32);
|
||||
pub struct HeapRef {
|
||||
pub index: u32,
|
||||
pub generation: u32,
|
||||
}
|
||||
|
||||
impl HeapRef {
|
||||
pub const NULL: Self = Self { index: 0, generation: 0 };
|
||||
pub const fn is_null(self) -> bool {
|
||||
self.index == 0 && self.generation == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents any piece of data that can be stored on the VM stack or in globals.
|
||||
///
|
||||
/// The PVM is "dynamically typed" at the bytecode level, meaning a single
|
||||
/// `Value` enum can hold different primitive types. The VM performs
|
||||
/// automatic type promotion (e.g., adding an Int32 to a Float64 results
|
||||
/// in a Float64) to ensure mathematical correctness.
|
||||
/// This enum is a physical carrier, not a semantic type declaration. Registry
|
||||
/// boundaries validate exact types and complete multi-slot groups. Arithmetic
|
||||
/// instructions may have their own numeric promotion rules; those rules do not
|
||||
/// authorize implicit conversions at typed storage or call boundaries.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Value {
|
||||
@@ -123,7 +134,7 @@ impl Value {
|
||||
}
|
||||
Value::String(s) => out.push_str(s),
|
||||
Value::HeapRef(r) => {
|
||||
let _ = write!(out, "[HeapRef {}]", r.0);
|
||||
let _ = write!(out, "[HeapRef {}:{}]", r.index, r.generation);
|
||||
}
|
||||
Value::Null => out.push_str("null"),
|
||||
}
|
||||
@@ -138,7 +149,7 @@ impl Value {
|
||||
Value::Int32(i) => i.to_string().len(),
|
||||
Value::Int64(i) => i.to_string().len(),
|
||||
Value::Float(f) => f.to_string().len(),
|
||||
Value::HeapRef(r) => 11 + r.0.to_string().len(),
|
||||
Value::HeapRef(r) => 11 + r.index.to_string().len() + r.generation.to_string().len(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
use prometeu_bytecode::isa::core::{CoreOpCode as Op, CoreOpCodeSpecExt};
|
||||
use prometeu_bytecode::load_limits::{LoadBudget, LoadCode, LoadLimits, LoadReason};
|
||||
use prometeu_bytecode::registry::*;
|
||||
use prometeu_bytecode::{assemble, decode_next, disassemble, DecodeError, RegistryStackEffect};
|
||||
|
||||
fn registry() -> Registry {
|
||||
let types = vec![
|
||||
TypeKind::Builtin(BuiltinId::INT32),
|
||||
TypeKind::Builtin(BuiltinId::VEC2),
|
||||
TypeKind::Builtin(BuiltinId::PIXEL),
|
||||
TypeKind::Builtin(BuiltinId::DYNAMIC),
|
||||
TypeKind::Struct(vec![]),
|
||||
TypeKind::Array(TypeId(0)),
|
||||
TypeKind::Trait(vec![]),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|kind| TypeDecl { name: None, kind })
|
||||
.collect();
|
||||
Registry::resolve(
|
||||
RegistryDeclarations { types, ..Default::default() },
|
||||
0,
|
||||
&mut LoadBudget::new(LoadLimits::default()),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_typed_instructions_match_independent_bytes_and_costs() {
|
||||
let cases: &[(&str, &[u8], u64)] = &[
|
||||
("DYN_PACK 16909060", &[0x90, 0, 4, 3, 2, 1], 8),
|
||||
("DYN_UNPACK 16909060", &[0x91, 0, 4, 3, 2, 1], 4),
|
||||
("DYN_CAN_UNPACK 16909060", &[0x92, 0, 4, 3, 2, 1], 3),
|
||||
("REF_IS_EXACT source=16909060, target=84281096", &[0x93, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
|
||||
("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),
|
||||
];
|
||||
for &(text, bytes, cycles) in cases {
|
||||
assert_eq!(assemble(text).unwrap(), bytes);
|
||||
assert_eq!(disassemble(bytes).unwrap(), text);
|
||||
let decoded = decode_next(0, bytes).unwrap();
|
||||
assert_eq!(decoded.next_pc, bytes.len());
|
||||
assert_eq!(decoded.opcode.cycles(), cycles);
|
||||
assert_eq!(decoded.opcode.spec().pops, None);
|
||||
assert_eq!(decoded.opcode.spec().pushes, None);
|
||||
assert!(decoded.opcode.requires_registry());
|
||||
for n in 0..bytes.len() {
|
||||
assert!(decode_next(0, &bytes[..n]).is_err(), "{text}, prefix {n}");
|
||||
}
|
||||
}
|
||||
assert!(matches!(
|
||||
decode_next(0, &[0x97, 0]),
|
||||
Err(DecodeError::UnknownOpcode { opcode: 0x97, .. })
|
||||
));
|
||||
}
|
||||
#[test]
|
||||
fn slot_effects_resolve_semantic_widths_and_reference_categories() {
|
||||
let registry = registry();
|
||||
for (text, pops, pushes) in [
|
||||
("DYN_PACK 0", 1, 2),
|
||||
("DYN_PACK 1", 2, 2),
|
||||
("DYN_PACK 2", 3, 2),
|
||||
("DYN_PACK 3", 2, 2),
|
||||
("DYN_UNPACK 2", 2, 3),
|
||||
("DYN_CAN_UNPACK 2", 2, 1),
|
||||
("REF_IS_EXACT source=6, target=4", 2, 1),
|
||||
("REF_IMPLEMENTS source=5, target=6", 1, 1),
|
||||
("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),
|
||||
] {
|
||||
let bytes = assemble(text).unwrap();
|
||||
assert_eq!(
|
||||
decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap(),
|
||||
Some(RegistryStackEffect { pops, pushes }),
|
||||
"{text}"
|
||||
);
|
||||
}
|
||||
assert_eq!(decode_next(0, &[0, 0]).unwrap().registry_stack_effect(®istry).unwrap(), None);
|
||||
}
|
||||
#[test]
|
||||
fn invalid_type_immediates_are_contextual_load_errors() {
|
||||
let registry = registry();
|
||||
for text in ["DYN_PACK 7", "DYN_UNPACK 4294967295", "REF_CAST source=7, target=4"] {
|
||||
let bytes = assemble(text).unwrap();
|
||||
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err();
|
||||
assert_eq!(
|
||||
(e.code, e.reason, e.pc),
|
||||
(LoadCode::InvalidReference, LoadReason::TypeId, Some(0))
|
||||
);
|
||||
}
|
||||
for text in [
|
||||
"REF_CAST source=3, target=4",
|
||||
"REF_IS_EXACT source=4, target=6",
|
||||
"REF_TO_TRAIT source=4, target=0",
|
||||
"REF_IMPLEMENTS source=0, target=6",
|
||||
] {
|
||||
let bytes = assemble(text).unwrap();
|
||||
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err();
|
||||
assert_eq!(
|
||||
(e.code, e.reason),
|
||||
(LoadCode::InvalidReference, LoadReason::Category),
|
||||
"{text}"
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn assembler_rejects_missing_overflowing_or_ambiguous_type_ids() {
|
||||
for text in [
|
||||
"DYN_PACK",
|
||||
"DYN_PACK -1",
|
||||
"DYN_PACK 4294967296",
|
||||
"DYN_PACK 1 2",
|
||||
"REF_CAST source=4",
|
||||
"REF_CAST source=4, target=5, target=6",
|
||||
"REF_CAST source=4, trait=6",
|
||||
] {
|
||||
assert!(assemble(text).is_err(), "{text}");
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn decoder_extreme_pc_does_not_overflow() {
|
||||
assert!(matches!(
|
||||
decode_next(usize::MAX, &[]),
|
||||
Err(DecodeError::TruncatedOpcode { pc: usize::MAX })
|
||||
));
|
||||
assert_eq!(Op::DynPack as u16, 0x0090);
|
||||
}
|
||||
@@ -0,0 +1,631 @@
|
||||
use prometeu_bytecode::load_limits::{
|
||||
LoadBudget, LoadCode, LoadFailure, LoadLimits, LoadReason, LoadResource,
|
||||
};
|
||||
use prometeu_bytecode::registry::*;
|
||||
use prometeu_bytecode::registry_codec::*;
|
||||
|
||||
fn budget() -> LoadBudget {
|
||||
LoadBudget::new(LoadLimits::default())
|
||||
}
|
||||
fn named(id: u32) -> NamedType {
|
||||
NamedType { type_id: TypeId(id), name: None }
|
||||
}
|
||||
fn builtin(id: BuiltinId) -> TypeDecl {
|
||||
TypeDecl { name: None, kind: TypeKind::Builtin(id) }
|
||||
}
|
||||
fn resolve(d: RegistryDeclarations) -> Result<Registry, LoadFailure> {
|
||||
Registry::resolve(d, 100, &mut budget())
|
||||
}
|
||||
fn hex(s: &str) -> Vec<u8> {
|
||||
s.split_whitespace().map(|b| u8::from_str_radix(b, 16).unwrap()).collect()
|
||||
}
|
||||
|
||||
// Independently specified PBX, not generated by any production serializer.
|
||||
// Header 32 bytes, seven directory entries (84 bytes), payload starts at 116.
|
||||
// constants=116/4, functions=120/29, code=149/2, exports=151/4,
|
||||
// syscalls=155/4, types=159/10, implementations=169/4. Total=173.
|
||||
fn minimal_bytes() -> Vec<u8> {
|
||||
hex("
|
||||
50 42 58 00 01 00 00 00 07 00 00 00
|
||||
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
00 00 00 00 74 00 00 00 04 00 00 00
|
||||
01 00 00 00 78 00 00 00 1d 00 00 00
|
||||
02 00 00 00 95 00 00 00 02 00 00 00
|
||||
04 00 00 00 97 00 00 00 04 00 00 00
|
||||
05 00 00 00 9b 00 00 00 04 00 00 00
|
||||
06 00 00 00 9f 00 00 00 0a 00 00 00
|
||||
07 00 00 00 a9 00 00 00 04 00 00 00
|
||||
00 00 00 00
|
||||
01 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00
|
||||
01 00
|
||||
00 00 00 00
|
||||
00 00 00 00
|
||||
01 00 00 00 00 00 13 00 00 00
|
||||
00 00 00 00")
|
||||
}
|
||||
fn minimal_registry() -> Registry {
|
||||
resolve(RegistryDeclarations {
|
||||
types: vec![builtin(BuiltinId::BOOL)],
|
||||
functions: vec![FunctionDecl { code_len: 2, ..Default::default() }],
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
fn envelope(types: &[u8], functions: &[u8], implementations: &[u8]) -> Vec<u8> {
|
||||
let empty = [0; 4];
|
||||
let code = [1, 0];
|
||||
encode_envelope(
|
||||
&[
|
||||
SectionData { kind: 0, bytes: &empty },
|
||||
SectionData { kind: 1, bytes: functions },
|
||||
SectionData { kind: 2, bytes: &code },
|
||||
SectionData { kind: 4, bytes: &empty },
|
||||
SectionData { kind: 5, bytes: &empty },
|
||||
SectionData { kind: 6, bytes: types },
|
||||
SectionData { kind: 7, bytes: implementations },
|
||||
],
|
||||
&mut budget(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
#[test]
|
||||
fn writer_matches_independent_minimal_envelope() {
|
||||
let sections = encode_registry(&minimal_registry(), &mut budget()).unwrap();
|
||||
let bytes = envelope(§ions.types, §ions.functions, §ions.implementations);
|
||||
assert_eq!(bytes, minimal_bytes());
|
||||
assert_eq!(bytes.len(), 173);
|
||||
let mut b = budget();
|
||||
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
|
||||
assert_eq!(env.section(2).unwrap().offset, 149);
|
||||
let registry = env.resolve_registry(&mut b).unwrap();
|
||||
assert_eq!(registry.declarations(), minimal_registry().declarations());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
assert_eq!(registry.declarations().functions[0].max_stack_slots, 0);
|
||||
}
|
||||
#[test]
|
||||
fn representative_records_match_fixed_bytes_and_preserve_names_without_debug() {
|
||||
let types = hex("
|
||||
04 00 00 00
|
||||
00 00 13 00 00 00
|
||||
01 01 00 00 00 00 01 00 00 00 00 00 00 00 00
|
||||
02 00 01 00 00 00
|
||||
03 00 01 00 00 00 01 01 00 00 00 6d
|
||||
01 00 00 00 00 00 00 00 01 01 00 00 00 70
|
||||
01 00 00 00 00 00 00 00");
|
||||
let functions = hex("
|
||||
01 00 00 00 01 01 00 00 00 66
|
||||
02 00 00 00 01 00 00 00 00 00 00 00 00 01 01 00 00 00 70
|
||||
01 00 00 00 00 00 00 00
|
||||
00 00 00 00 02 00 00 00 00 00 00 00 01 00 00 00");
|
||||
let implementations = hex("01 00 00 00 01 00 00 00 03 00 00 00 01 00 00 00 00 00 00 00");
|
||||
let bytes = envelope(&types, &functions, &implementations);
|
||||
let mut b = budget();
|
||||
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
|
||||
assert!(env.section(3).is_none());
|
||||
let r = env.resolve_registry(&mut b).unwrap();
|
||||
assert_eq!(r.declarations().types[0].name, None);
|
||||
assert_eq!(r.declarations().types[1].name, Some(String::new()));
|
||||
assert_eq!(r.declarations().functions[0].signature.parameters[1].name.as_deref(), Some("p"));
|
||||
assert_eq!(r.implementation(TypeId(1), TypeId(3)), Some(ImplementationId(0)));
|
||||
assert_eq!(r.type_layout(TypeId(2)).unwrap().storage.size, 8);
|
||||
let encoded = encode_registry(&r, &mut budget()).unwrap();
|
||||
assert_eq!(encoded.types, types);
|
||||
assert_eq!(encoded.functions, functions);
|
||||
assert_eq!(encoded.implementations, implementations);
|
||||
}
|
||||
#[test]
|
||||
fn every_truncated_envelope_is_rejected_without_panic() {
|
||||
let bytes = minimal_bytes();
|
||||
for n in 0..bytes.len() {
|
||||
let mut b = budget();
|
||||
assert!(PbxEnvelope::parse(&bytes[..n], &mut b).is_err(), "prefix {n}");
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn envelope_mutations_have_typed_reasons() {
|
||||
for (offset, value, code, reason) in [
|
||||
(0, 0, LoadCode::InvalidFormat, LoadReason::Magic),
|
||||
(4, 0, LoadCode::UnsupportedFormat, LoadReason::Version),
|
||||
(6, 1, LoadCode::InvalidFormat, LoadReason::Endianness),
|
||||
(7, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
|
||||
(31, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
|
||||
(32, 8, LoadCode::UnsupportedFormat, LoadReason::SectionKind),
|
||||
(44, 0, LoadCode::InvalidFormat, LoadReason::DuplicateSection),
|
||||
(36, 0, LoadCode::InvalidFormat, LoadReason::Overlap),
|
||||
(48, 116, LoadCode::InvalidFormat, LoadReason::Overlap),
|
||||
] {
|
||||
let mut bytes = minimal_bytes();
|
||||
bytes[offset] = value;
|
||||
let err = PbxEnvelope::parse(&bytes, &mut budget()).unwrap_err();
|
||||
assert_eq!((err.code, err.reason), (code, reason), "offset {offset}");
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn mandatory_empty_sections_are_required() {
|
||||
let mut bytes = minimal_bytes();
|
||||
bytes[32] = 3; // replaces constants with optional debug
|
||||
let err = PbxEnvelope::parse(&bytes, &mut budget()).unwrap_err();
|
||||
assert_eq!(err.reason, LoadReason::MissingSection);
|
||||
assert_eq!(err.section, Some(0));
|
||||
}
|
||||
#[test]
|
||||
fn record_flags_utf8_trailing_bytes_and_unknown_categories_fail() {
|
||||
let valid = minimal_bytes();
|
||||
let mut b = budget();
|
||||
let env = PbxEnvelope::parse(&valid, &mut b).unwrap();
|
||||
let functions = env.section(1).unwrap().bytes;
|
||||
let implementations = env.section(7).unwrap().bytes;
|
||||
for (types, reason) in [
|
||||
(hex("01 00 00 00 00 02 13 00 00 00"), LoadReason::InvalidFlag),
|
||||
(hex("01 00 00 00 04 00 13 00 00 00"), LoadReason::Category),
|
||||
(hex("01 00 00 00 00 01 01 00 00 00 ff 13 00 00 00"), LoadReason::Utf8),
|
||||
(hex("01 00 00 00 00 00 13 00 00 00 ff"), LoadReason::TrailingBytes),
|
||||
(hex("01 00 00 00 00 00 00 01 00 00"), LoadReason::Builtin),
|
||||
] {
|
||||
let bytes = envelope(&types, functions, implementations);
|
||||
let mut b = budget();
|
||||
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
|
||||
let err = env.resolve_registry(&mut b).unwrap_err();
|
||||
assert_eq!(err.reason, reason);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn oversized_counts_fail_before_dependent_allocation() {
|
||||
let bytes = envelope(&[255; 4], &[0; 4], &[0; 4]);
|
||||
let mut b = budget();
|
||||
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
|
||||
let err = env.resolve_registry(&mut b).unwrap_err();
|
||||
assert_eq!(err.resource, Some(LoadResource::Types));
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
#[test]
|
||||
fn logical_scalar_layout_and_empty_nominal_identity() {
|
||||
let d = RegistryDeclarations {
|
||||
types: vec![
|
||||
builtin(BuiltinId::BOOL),
|
||||
builtin(BuiltinId::INT32),
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![named(0), named(1), named(0)]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(2)) },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
let r = resolve(d).unwrap();
|
||||
let s = r.type_layout(TypeId(2)).unwrap();
|
||||
assert_eq!(s.storage.size, 8);
|
||||
let p = s.instance.as_ref().unwrap();
|
||||
assert_eq!((p.size, p.alignment), (12, 4));
|
||||
assert_eq!(p.fields.iter().map(|f| f.offset).collect::<Vec<_>>(), vec![0, 4, 8]);
|
||||
assert!(p.owned_fields.is_empty());
|
||||
for id in [3, 4] {
|
||||
let l = r.type_layout(TypeId(id)).unwrap().instance.as_ref().unwrap();
|
||||
assert_eq!((l.size, l.alignment), (0, 1));
|
||||
}
|
||||
assert_eq!(r.type_layout(TypeId(5)).unwrap().storage.size, 8);
|
||||
assert_eq!(r.declarations().types.len(), 6);
|
||||
}
|
||||
#[test]
|
||||
fn nominal_recursion_and_forward_references_terminate() {
|
||||
let r = resolve(RegistryDeclarations {
|
||||
types: vec![
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![named(0), named(1)]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
||||
],
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
let layout = r.type_layout(TypeId(0)).unwrap().instance.as_ref().unwrap();
|
||||
assert_eq!(layout.size, 16);
|
||||
assert_eq!(layout.owned_fields.len(), 2);
|
||||
}
|
||||
#[test]
|
||||
fn duplicate_imports_and_arrays_and_structural_cycles_fail() {
|
||||
let cases = [
|
||||
(vec![builtin(BuiltinId::BOOL), builtin(BuiltinId::BOOL)], LoadReason::DuplicateBuiltin),
|
||||
(
|
||||
vec![
|
||||
builtin(BuiltinId::BOOL),
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
||||
],
|
||||
LoadReason::DuplicateArray,
|
||||
),
|
||||
(vec![TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) }], LoadReason::ArrayCycle),
|
||||
(
|
||||
vec![
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(1)) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
||||
],
|
||||
LoadReason::ArrayCycle,
|
||||
),
|
||||
];
|
||||
for (types, reason) in cases {
|
||||
assert_eq!(
|
||||
resolve(RegistryDeclarations { types, ..Default::default() }).unwrap_err().reason,
|
||||
reason
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn long_array_chain_uses_bounded_iteration() {
|
||||
let mut types: Vec<_> = (0..10_000)
|
||||
.map(|i| TypeDecl { name: None, kind: TypeKind::Array(TypeId(i + 1)) })
|
||||
.collect();
|
||||
types.push(builtin(BuiltinId::INT32));
|
||||
assert!(resolve(RegistryDeclarations { types, ..Default::default() }).is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn invalid_references_are_not_indexed() {
|
||||
let d = RegistryDeclarations {
|
||||
types: vec![TypeDecl { name: None, kind: TypeKind::Struct(vec![named(u32::MAX)]) }],
|
||||
..Default::default()
|
||||
};
|
||||
let e = resolve(d).unwrap_err();
|
||||
assert_eq!(e.code, LoadCode::InvalidReference);
|
||||
assert_eq!(e.member, Some(0));
|
||||
}
|
||||
#[test]
|
||||
fn all_builtin_widths_native_layout_and_unknown_imports() {
|
||||
for (id, size, alignment, slots) in [
|
||||
(1, 4, 4, 1),
|
||||
(2, 16, 8, 2),
|
||||
(3, 24, 8, 3),
|
||||
(0x10, 4, 4, 1),
|
||||
(0x11, 8, 8, 1),
|
||||
(0x12, 8, 8, 1),
|
||||
(0x13, 1, 1, 1),
|
||||
] {
|
||||
let l = BuiltinId(id).layout().unwrap();
|
||||
assert_eq!((l.size, l.alignment, l.slots), (size, alignment, slots));
|
||||
assert!(!l.nullable());
|
||||
}
|
||||
let s = BuiltinId::STRING.layout().unwrap();
|
||||
assert_eq!((s.size, s.alignment), (size_of::<StringSlot>(), align_of::<StringSlot>()));
|
||||
assert!(s.nullable());
|
||||
let d = BuiltinId::DYNAMIC.layout().unwrap();
|
||||
assert_eq!((d.size, d.alignment), (size_of::<DynamicSlot>(), align_of::<DynamicSlot>()));
|
||||
for id in [0, 4, 0x100, 0x101, 0x102, u32::MAX] {
|
||||
assert_eq!(BuiltinId(id).layout().unwrap_err().reason, LoadReason::Builtin);
|
||||
}
|
||||
}
|
||||
fn implemented() -> RegistryDeclarations {
|
||||
RegistryDeclarations {
|
||||
types: vec![
|
||||
builtin(BuiltinId::VEC2),
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
||||
TypeDecl {
|
||||
name: None,
|
||||
kind: TypeKind::Trait(vec![MethodDecl {
|
||||
name: None,
|
||||
signature: Signature { parameters: vec![named(0)], results: vec![TypeId(0)] },
|
||||
}]),
|
||||
},
|
||||
],
|
||||
functions: vec![FunctionDecl {
|
||||
signature: Signature { parameters: vec![named(1), named(0)], results: vec![TypeId(0)] },
|
||||
code_len: 2,
|
||||
max_stack_slots: 2,
|
||||
..Default::default()
|
||||
}],
|
||||
implementations: vec![ImplementationDecl {
|
||||
owner: TypeId(1),
|
||||
trait_id: TypeId(2),
|
||||
methods: vec![FunctionId(0)],
|
||||
}],
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn multi_slot_signatures_derive_counts_and_validate_exact_trait_pair() {
|
||||
let r = resolve(implemented()).unwrap();
|
||||
assert_eq!(
|
||||
r.function_layout(FunctionId(0)),
|
||||
Some(&FunctionLayout { parameter_slots: 3, result_slots: 2 })
|
||||
);
|
||||
let mut d = implemented();
|
||||
d.functions[0].signature.parameters.swap(0, 1);
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::Signature);
|
||||
let mut d = implemented();
|
||||
d.functions[0].signature.results.clear();
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::Signature);
|
||||
let mut d = implemented();
|
||||
d.implementations.push(d.implementations[0].clone());
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::DuplicateImplementation);
|
||||
let mut d = implemented();
|
||||
d.implementations[0].methods.clear();
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::MethodCount);
|
||||
let mut d = implemented();
|
||||
d.implementations[0].owner = TypeId(0);
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::OwnerCategory);
|
||||
}
|
||||
#[test]
|
||||
fn literal_zero_max_stack_is_preserved_and_frame_limits_are_not_truncated() {
|
||||
let mut d = implemented();
|
||||
d.functions[0].max_stack_slots = 0;
|
||||
assert!(resolve(d).is_ok());
|
||||
let mut d = implemented();
|
||||
d.functions[0].local_slots = 65_536;
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::FrameSlots);
|
||||
let mut d = implemented();
|
||||
d.functions[0].code_offset = u32::MAX;
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::CodeRange);
|
||||
let mut d = implemented();
|
||||
d.functions.push(d.functions[0].clone());
|
||||
assert_eq!(resolve(d).unwrap_err().reason, LoadReason::CodeRange);
|
||||
}
|
||||
#[test]
|
||||
fn work_retained_and_temporary_limits_refuse_before_publication() {
|
||||
for limits in [
|
||||
LoadLimits { work: 1, ..Default::default() },
|
||||
LoadLimits { registry_bytes: 1, ..Default::default() },
|
||||
LoadLimits { temporary_bytes: 1, ..Default::default() },
|
||||
] {
|
||||
let mut b = LoadBudget::new(limits);
|
||||
let e = Registry::resolve(implemented(), 100, &mut b).unwrap_err();
|
||||
assert_eq!(e.code, LoadCode::ResourceLimit);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn budget_boundaries_are_transactional_and_product_ceiling_cannot_be_raised() {
|
||||
let mut b = LoadBudget::new(LoadLimits { temporary_bytes: 8, ..Default::default() });
|
||||
b.charge(LoadResource::TemporaryBytes, 8).unwrap();
|
||||
assert!(b.charge(LoadResource::TemporaryBytes, 1).is_err());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 8);
|
||||
b.refund(LoadResource::TemporaryBytes, 8);
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
let b = LoadBudget::new(LoadLimits { pbx_bytes: u64::MAX, ..Default::default() });
|
||||
assert_eq!(b.limit(LoadResource::PbxBytes), 64 * 1_048_576);
|
||||
}
|
||||
#[test]
|
||||
fn name_limits_include_aggregate_and_empty_is_not_absent() {
|
||||
let mut d = implemented();
|
||||
d.types[0].name = Some("x".repeat(4097));
|
||||
assert_eq!(resolve(d).unwrap_err().resource, Some(LoadResource::NameBytes));
|
||||
let mut d = implemented();
|
||||
d.types[0].name = Some("ab".into());
|
||||
d.types[1].name = Some("cd".into());
|
||||
let mut b = LoadBudget::new(LoadLimits { name_bytes: 3, ..Default::default() });
|
||||
assert_eq!(
|
||||
Registry::resolve(d, 100, &mut b).unwrap_err().resource,
|
||||
Some(LoadResource::NameBytes)
|
||||
);
|
||||
}
|
||||
|
||||
fn with_section(kind: u32, payload: &[u8]) -> Vec<u8> {
|
||||
let bytes = minimal_bytes();
|
||||
let mut b = budget();
|
||||
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
|
||||
let mut sections = Vec::new();
|
||||
for k in 0..8 {
|
||||
if k == kind {
|
||||
sections.push(SectionData { kind: k, bytes: payload });
|
||||
} else if let Some(section) = env.section(k) {
|
||||
sections.push(SectionData { kind: k, bytes: section.bytes });
|
||||
}
|
||||
}
|
||||
encode_envelope(§ions, &mut budget()).unwrap()
|
||||
}
|
||||
#[test]
|
||||
fn full_pbx_validation_accepts_independent_minimal_fixture() {
|
||||
let bytes = minimal_bytes();
|
||||
let mut b = budget();
|
||||
let parsed = ParsedPbx::parse(&bytes, &mut b).unwrap();
|
||||
assert_eq!(parsed.encode(&mut budget()).unwrap(), bytes);
|
||||
assert_eq!(parsed.registry().declarations(), minimal_registry().declarations());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
#[test]
|
||||
fn constants_are_strict_and_validate_every_tag_without_lossy_utf8() {
|
||||
let constants=hex("06 00 00 00 00 01 ff ff ff ff ff ff ff ff 02 00 00 00 00 00 00 00 00 03 01 04 03 00 00 00 61 62 63 05 ff ff ff ff");
|
||||
let bytes = with_section(0, &constants);
|
||||
let mut b = budget();
|
||||
let parsed = ParsedPbx::parse(&bytes, &mut b).unwrap();
|
||||
assert_eq!(parsed.section(0).unwrap().bytes, constants);
|
||||
assert_eq!(b.used(LoadResource::ConstantEntries), 6);
|
||||
assert_eq!(b.used(LoadResource::ConstantStringBytes), 3);
|
||||
for (cp, reason) in [
|
||||
(hex("01 00 00 00 03 02"), LoadReason::InvalidFlag),
|
||||
(hex("01 00 00 00 04 01 00 00 00 ff"), LoadReason::Utf8),
|
||||
(hex("01 00 00 00 06"), LoadReason::ConstantTag),
|
||||
(hex("01 00 00 00 01 ff"), LoadReason::Truncated),
|
||||
(hex("00 00 00 00 ff"), LoadReason::TrailingBytes),
|
||||
] {
|
||||
let bytes = with_section(0, &cp);
|
||||
let mut b = budget();
|
||||
let e = ParsedPbx::parse(&bytes, &mut b).unwrap_err();
|
||||
assert_eq!(e.reason, reason);
|
||||
assert_eq!(e.section, Some(0));
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn constant_string_limits_are_checked_before_materialization() {
|
||||
let bytes = with_section(0, &hex("01 00 00 00 04 03 00 00 00 61 62 63"));
|
||||
for limits in [
|
||||
LoadLimits { individual_string_bytes: 2, ..Default::default() },
|
||||
LoadLimits { constant_string_bytes: 2, ..Default::default() },
|
||||
LoadLimits { constant_entries: 0, ..Default::default() },
|
||||
] {
|
||||
let mut b = LoadBudget::new(limits);
|
||||
let e = ParsedPbx::parse(&bytes, &mut b).unwrap_err();
|
||||
assert_eq!(e.code, LoadCode::ResourceLimit);
|
||||
assert_eq!(b.used(LoadResource::ConstantStringBytes), 0);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn export_and_debug_references_are_checked_and_table_tails_rejected() {
|
||||
let export = hex("01 00 00 00 00 00 00 00 04 00 00 00 6d 61 69 6e");
|
||||
assert!(ParsedPbx::parse(&with_section(4, &export), &mut budget()).is_ok());
|
||||
let mut bad = export;
|
||||
bad[4] = 1;
|
||||
let e = ParsedPbx::parse(&with_section(4, &bad), &mut budget()).unwrap_err();
|
||||
assert_eq!(e.code, LoadCode::InvalidReference);
|
||||
assert_eq!(e.referenced_id, Some(1));
|
||||
// One pc/span record; one function name. These are independent of signature names.
|
||||
let debug=hex("01 00 00 00 00 00 00 00 07 00 00 00 0a 00 00 00 0c 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 66");
|
||||
let bytes = with_section(3, &debug);
|
||||
assert!(ParsedPbx::parse(&bytes, &mut budget()).is_ok());
|
||||
let mut bad = debug;
|
||||
bad[4] = 2;
|
||||
assert_eq!(
|
||||
ParsedPbx::parse(&with_section(3, &bad), &mut budget()).unwrap_err().reason,
|
||||
LoadReason::DebugRange
|
||||
);
|
||||
for kind in [3, 4, 5] {
|
||||
let payload =
|
||||
if kind == 3 { hex("00 00 00 00 00 00 00 00 ff") } else { hex("00 00 00 00 ff") };
|
||||
assert_eq!(
|
||||
ParsedPbx::parse(&with_section(kind, &payload), &mut budget()).unwrap_err().reason,
|
||||
LoadReason::TrailingBytes
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn syscall_identity_validation_keeps_versions_distinct_and_rejects_duplicates() {
|
||||
// module m, name n, version 1, zero arguments/results.
|
||||
let entry = hex("01 00 6d 01 00 6e 01 00 00 00 00 00");
|
||||
let mut payload = vec![2, 0, 0, 0];
|
||||
payload.extend_from_slice(&entry);
|
||||
payload.extend_from_slice(&entry);
|
||||
let e = ParsedPbx::parse(&with_section(5, &payload), &mut budget()).unwrap_err();
|
||||
assert_eq!((e.reason, e.section, e.record), (LoadReason::DuplicateSyscall, Some(5), Some(1)));
|
||||
payload[22] = 2; // version in second 12-byte entry
|
||||
let bytes = with_section(5, &payload);
|
||||
let mut b = budget();
|
||||
assert!(ParsedPbx::parse(&bytes, &mut b).is_ok());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_stride_uses_stored_element_layout_and_defaults_are_typed() {
|
||||
use prometeu_bytecode::Value;
|
||||
let registry = resolve(RegistryDeclarations {
|
||||
types: vec![
|
||||
builtin(BuiltinId::INT32),
|
||||
builtin(BuiltinId::BOOL),
|
||||
builtin(BuiltinId::VEC2),
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![named(0), named(1)]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(1)) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(2)) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(3)) },
|
||||
TypeDecl { name: None, kind: TypeKind::Array(TypeId(4)) },
|
||||
],
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
for (id, stride, alignment) in [(4, 4, 4), (5, 1, 1), (6, 16, 8), (7, 8, 8), (8, 8, 8)] {
|
||||
let a = registry.type_layout(TypeId(id)).unwrap().array.unwrap();
|
||||
assert_eq!((a.stride, a.element_alignment), (stride, alignment));
|
||||
}
|
||||
assert!(matches!(BuiltinId::INT32.layout().unwrap().default_values(), [Value::Int32(0)]));
|
||||
assert!(matches!(BuiltinId::BOOL.layout().unwrap().default_values(), [Value::Boolean(false)]));
|
||||
assert!(matches!(BuiltinId::COLOR.layout().unwrap().default_values(), [Value::Int64(0)]));
|
||||
assert!(matches!(BuiltinId::STRING.layout().unwrap().default_values(), [Value::Null]));
|
||||
assert!(matches!(
|
||||
BuiltinId::DYNAMIC.layout().unwrap().default_values(),
|
||||
[Value::Null, Value::Null]
|
||||
));
|
||||
for value in BuiltinId::VEC2.layout().unwrap().default_values() {
|
||||
let Value::Float(value) = value else { panic!("expected float default") };
|
||||
assert_eq!(value.to_bits(), 0);
|
||||
}
|
||||
assert_eq!(registry.type_layout(TypeId(3)).unwrap().storage.default_values(), &[Value::Null]);
|
||||
}
|
||||
|
||||
/// Run explicitly in release mode; records a map-only comparison, not VM/GC throughput.
|
||||
#[test]
|
||||
#[ignore = "explicit layout measurement; not a timing assertion"]
|
||||
fn reference_map_probe() {
|
||||
use std::hint::black_box;
|
||||
use std::time::Instant;
|
||||
for period in [1, 16, 256] {
|
||||
let d = RegistryDeclarations {
|
||||
types: vec![
|
||||
builtin(BuiltinId::INT64),
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
||||
TypeDecl {
|
||||
name: None,
|
||||
kind: TypeKind::Struct(
|
||||
(0..4096).map(|i| named(if i % period == 0 { 1 } else { 0 })).collect(),
|
||||
),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
let mut b = budget();
|
||||
let load_start = Instant::now();
|
||||
let r = Registry::resolve(d, 0, &mut b).unwrap();
|
||||
let load_ns = load_start.elapsed().as_nanos();
|
||||
let layout = r.type_layout(TypeId(2)).unwrap().instance.as_ref().unwrap();
|
||||
let mut bitmap = vec![0u64; 64];
|
||||
for field in &layout.owned_fields {
|
||||
let bit = field.offset / 8;
|
||||
bitmap[bit / 64] |= 1u64 << (bit % 64);
|
||||
}
|
||||
let runs = 10_000;
|
||||
let start = Instant::now();
|
||||
let mut list_checksum = 0usize;
|
||||
for _ in 0..runs {
|
||||
for field in black_box(&layout.owned_fields) {
|
||||
list_checksum = list_checksum.wrapping_add(field.offset);
|
||||
}
|
||||
}
|
||||
black_box(list_checksum);
|
||||
let list_ns = start.elapsed().as_nanos();
|
||||
let start = Instant::now();
|
||||
let mut bitmap_checksum = 0usize;
|
||||
for _ in 0..runs {
|
||||
for (word_index, &word) in black_box(&bitmap).iter().enumerate() {
|
||||
let mut word = word;
|
||||
while word != 0 {
|
||||
bitmap_checksum = bitmap_checksum
|
||||
.wrapping_add((word_index * 64 + word.trailing_zeros() as usize) * 8);
|
||||
word &= word - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
black_box(bitmap_checksum);
|
||||
let bitmap_ns = start.elapsed().as_nanos();
|
||||
assert_eq!(list_checksum, bitmap_checksum);
|
||||
println!("arch={} fields=4096 period={} roots={} payload={} registry={} load_ns={} list_bytes={} bitmap_bytes={} runs={} list_ns={} bitmap_ns={}",
|
||||
std::env::consts::ARCH,period,layout.owned_fields.len(),layout.size,b.used(LoadResource::RegistryBytes),load_ns,
|
||||
layout.owned_fields.capacity()*size_of::<OwnedField>(),bitmap.capacity()*8,runs,list_ns,bitmap_ns);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn independent_multi_slot_signature_fixture_has_no_serialized_widths() {
|
||||
// vec2 import; fn(vec2)->vec2; GET_LOCAL 0; GET_LOCAL 1; RET.
|
||||
let types = hex("01 00 00 00 00 00 02 00 00 00");
|
||||
let functions=hex("01 00 00 00 00 01 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 02 00 00 00");
|
||||
let code = hex("42 00 00 00 00 00 42 00 01 00 00 00 51 00");
|
||||
let empty = [0; 4];
|
||||
let bytes = encode_envelope(
|
||||
&[
|
||||
SectionData { kind: 0, bytes: &empty },
|
||||
SectionData { kind: 1, bytes: &functions },
|
||||
SectionData { kind: 2, bytes: &code },
|
||||
SectionData { kind: 4, bytes: &empty },
|
||||
SectionData { kind: 5, bytes: &empty },
|
||||
SectionData { kind: 6, bytes: &types },
|
||||
SectionData { kind: 7, bytes: &empty },
|
||||
],
|
||||
&mut budget(),
|
||||
)
|
||||
.unwrap();
|
||||
let parsed = ParsedPbx::parse(&bytes, &mut budget()).unwrap();
|
||||
assert_eq!(
|
||||
parsed.registry().function_layout(FunctionId(0)),
|
||||
Some(&FunctionLayout { parameter_slots: 2, result_slots: 2 })
|
||||
);
|
||||
let sections = encode_registry(parsed.registry(), &mut budget()).unwrap();
|
||||
assert_eq!(sections.functions, functions);
|
||||
assert_eq!(sections.types, types);
|
||||
}
|
||||
@@ -1,27 +1,75 @@
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
|
||||
/// Writes only into result slots reserved by the VM. A native handler cannot
|
||||
/// grow the operand buffer or exceed its declared result count.
|
||||
pub struct HostReturn<'a> {
|
||||
stack: &'a mut Vec<Value>,
|
||||
remaining: usize,
|
||||
overflowed: bool,
|
||||
}
|
||||
|
||||
impl<'a> HostReturn<'a> {
|
||||
pub fn new(stack: &'a mut Vec<Value>) -> Self {
|
||||
Self { stack }
|
||||
pub fn new(stack: &'a mut Vec<Value>, result_slots: usize) -> Self {
|
||||
Self { stack, remaining: result_slots, overflowed: false }
|
||||
}
|
||||
pub fn overflowed(&self) -> bool {
|
||||
self.overflowed
|
||||
}
|
||||
fn admit(&mut self) -> bool {
|
||||
if self.remaining == 0 || self.stack.len() == self.stack.capacity() {
|
||||
self.overflowed = true;
|
||||
false
|
||||
} else {
|
||||
self.remaining -= 1;
|
||||
true
|
||||
}
|
||||
}
|
||||
pub fn push_bool(&mut self, v: bool) {
|
||||
self.stack.push(Value::Boolean(v));
|
||||
if self.admit() {
|
||||
self.stack.push(Value::Boolean(v));
|
||||
}
|
||||
}
|
||||
pub fn push_int(&mut self, v: i64) {
|
||||
self.stack.push(Value::Int64(v));
|
||||
if self.admit() {
|
||||
self.stack.push(Value::Int64(v));
|
||||
}
|
||||
}
|
||||
pub fn push_null(&mut self) {
|
||||
self.stack.push(Value::Null);
|
||||
if self.admit() {
|
||||
self.stack.push(Value::Null);
|
||||
}
|
||||
}
|
||||
pub fn push_gate(&mut self, g: usize) {
|
||||
// Temporary: cast incoming handle/HeapRef index. Real allocator will provide proper handles.
|
||||
self.stack.push(Value::HeapRef(HeapRef(g as u32)));
|
||||
/// Return a handle issued by the current session's heap.
|
||||
pub fn push_gate(&mut self, handle: HeapRef) {
|
||||
if self.admit() {
|
||||
self.stack.push(if handle.is_null() { Value::Null } else { Value::HeapRef(handle) });
|
||||
}
|
||||
}
|
||||
pub fn push_string(&mut self, s: String) {
|
||||
self.stack.push(Value::string(s));
|
||||
if self.admit() {
|
||||
self.stack.push(Value::string(s));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn native_results_cannot_grow_the_reserved_buffer_or_exceed_the_signature() {
|
||||
let mut stack = Vec::with_capacity(8);
|
||||
let capacity = stack.capacity();
|
||||
let mut ret = HostReturn::new(&mut stack, 1);
|
||||
ret.push_int(7);
|
||||
ret.push_int(8);
|
||||
assert!(ret.overflowed());
|
||||
assert_eq!(stack, vec![Value::Int64(7)]);
|
||||
assert_eq!(stack.capacity(), capacity);
|
||||
let mut empty = Vec::new();
|
||||
let mut ret = HostReturn::new(&mut empty, 1);
|
||||
ret.push_null();
|
||||
assert!(ret.overflowed());
|
||||
assert!(empty.is_empty());
|
||||
assert_eq!(empty.capacity(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,13 @@ impl VirtualMachineRuntime {
|
||||
platform: &mut dyn RuntimePlatform,
|
||||
) -> Option<CrashReport> {
|
||||
if vm.call_stack_is_empty() {
|
||||
vm.prepare_boot_call();
|
||||
if let Err(error) = vm.prepare_boot_call() {
|
||||
let report = CrashReport::VmInit {
|
||||
error: prometeu_vm::VmInitError::RuntimeResourceLimit(error),
|
||||
};
|
||||
self.last_crash_report = Some(report.clone());
|
||||
return Some(report);
|
||||
}
|
||||
}
|
||||
let step_result = {
|
||||
let mut ctx = HostContext::new_platform(Some(platform));
|
||||
@@ -168,7 +174,13 @@ impl VirtualMachineRuntime {
|
||||
self.begin_logical_frame(signals, platform);
|
||||
|
||||
if self.needs_prepare_entry_call || vm.call_stack_is_empty() {
|
||||
vm.prepare_boot_call();
|
||||
if let Err(error) = vm.prepare_boot_call() {
|
||||
let report = CrashReport::VmInit {
|
||||
error: prometeu_vm::VmInitError::RuntimeResourceLimit(error),
|
||||
};
|
||||
self.last_crash_report = Some(report.clone());
|
||||
return Some(report);
|
||||
}
|
||||
self.needs_prepare_entry_call = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::{
|
||||
Value,
|
||||
registry::{BuiltinId, StorageLayout},
|
||||
};
|
||||
use prometeu_hal::{HostContext, color::Color};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@@ -53,14 +56,18 @@ impl IntrinsicKey {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinScalarType {
|
||||
Int,
|
||||
Int32,
|
||||
Int64,
|
||||
Float,
|
||||
Bool,
|
||||
Str,
|
||||
String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum AbiType {
|
||||
/// Internal group components; neither can establish a valid Dynamic alone.
|
||||
DynamicTag,
|
||||
DynamicPayload,
|
||||
Scalar(BuiltinScalarType),
|
||||
Builtin(BuiltinTypeKey),
|
||||
}
|
||||
@@ -75,6 +82,7 @@ pub enum BuiltinLayoutType {
|
||||
pub enum BuiltinTypeShape {
|
||||
Scalar,
|
||||
Aggregate,
|
||||
Dynamic,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -111,14 +119,33 @@ impl BuiltinTypeMeta {
|
||||
out
|
||||
}
|
||||
|
||||
pub fn storage_layout(
|
||||
&self,
|
||||
) -> Result<StorageLayout, prometeu_bytecode::load_limits::LoadFailure> {
|
||||
BuiltinId(self.id).layout()
|
||||
}
|
||||
|
||||
pub fn validate_flat_values(&self, values: &[Value]) -> Result<(), BuiltinValueError> {
|
||||
if self.shape == BuiltinTypeShape::Dynamic {
|
||||
if values.len() != 2 {
|
||||
return Err(BuiltinValueError::ArityMismatch { expected: 2, got: values.len() });
|
||||
}
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
// A non-null tag is a program TypeId, and its payload may be a box.
|
||||
// This context-free catalogue must never claim to validate it.
|
||||
return Err(BuiltinValueError::RegistryRequired);
|
||||
}
|
||||
let layout = self.flatten_layout();
|
||||
validate_values_against_layout(values, &layout)
|
||||
}
|
||||
|
||||
fn flatten_into(&self, out: &mut Vec<AbiType>) {
|
||||
match self.shape {
|
||||
BuiltinTypeShape::Scalar => out.extend_from_slice(self.flat_slot_layout),
|
||||
BuiltinTypeShape::Scalar | BuiltinTypeShape::Dynamic => {
|
||||
out.extend_from_slice(self.flat_slot_layout)
|
||||
}
|
||||
BuiltinTypeShape::Aggregate => {
|
||||
for field in self.fields {
|
||||
flatten_layout_type(field.field_type, out);
|
||||
@@ -134,7 +161,7 @@ pub enum BuiltinConstSlotValue {
|
||||
Int64(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: i32 },
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: u32 },
|
||||
}
|
||||
|
||||
pub type BuiltinConstHook = fn() -> Vec<Value>;
|
||||
@@ -178,12 +205,12 @@ impl BuiltinConstMeta {
|
||||
pub enum IntrinsicExecutionError {
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
InvalidBuiltinCarrier { owner: &'static str, name: &'static str, carrier: i64 },
|
||||
HardwareUnavailable,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum BuiltinValueError {
|
||||
RegistryRequired,
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
}
|
||||
@@ -218,14 +245,28 @@ impl IntrinsicMeta {
|
||||
}
|
||||
|
||||
pub fn validate_result_values(&self, values: &[Value]) -> Result<(), IntrinsicExecutionError> {
|
||||
if values.len() != self.ret_slots() {
|
||||
Self::validate_values(values, self.ret_layout)
|
||||
}
|
||||
|
||||
pub fn validate_argument_values(
|
||||
&self,
|
||||
values: &[Value],
|
||||
) -> Result<(), IntrinsicExecutionError> {
|
||||
Self::validate_values(values, self.arg_layout)
|
||||
}
|
||||
|
||||
fn validate_values(
|
||||
values: &[Value],
|
||||
layout: &[AbiType],
|
||||
) -> Result<(), IntrinsicExecutionError> {
|
||||
if values.len() != layout.len() {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch {
|
||||
expected: self.ret_slots(),
|
||||
expected: layout.len(),
|
||||
got: values.len(),
|
||||
});
|
||||
}
|
||||
|
||||
for (index, (value, expected)) in values.iter().zip(self.ret_layout.iter()).enumerate() {
|
||||
for (index, (value, expected)) in values.iter().zip(layout.iter()).enumerate() {
|
||||
if !value_matches_abi_type(value, *expected) {
|
||||
return Err(IntrinsicExecutionError::TypeMismatch { index, expected: *expected });
|
||||
}
|
||||
@@ -242,7 +283,9 @@ impl BuiltinConstSlotValue {
|
||||
BuiltinConstSlotValue::Int64(value) => Value::Int64(value),
|
||||
BuiltinConstSlotValue::Float(value) => Value::Float(value),
|
||||
BuiltinConstSlotValue::Bool(value) => Value::Boolean(value),
|
||||
BuiltinConstSlotValue::NominalBuiltin { carrier, .. } => Value::Int32(carrier),
|
||||
BuiltinConstSlotValue::NominalBuiltin { carrier, .. } => {
|
||||
Value::Int64(i64::from(carrier))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -250,21 +293,15 @@ impl BuiltinConstSlotValue {
|
||||
const COLOR: BuiltinTypeKey = BuiltinTypeKey::new("color", 1);
|
||||
const VEC2: BuiltinTypeKey = BuiltinTypeKey::new("vec2", 1);
|
||||
const PIXEL: BuiltinTypeKey = BuiltinTypeKey::new("pixel", 1);
|
||||
const INPUT_PAD: BuiltinTypeKey = BuiltinTypeKey::new("input.pad", 1);
|
||||
const INPUT_TOUCH: BuiltinTypeKey = BuiltinTypeKey::new("input.touch", 1);
|
||||
const INPUT_BUTTON: BuiltinTypeKey = BuiltinTypeKey::new("input.button", 1);
|
||||
|
||||
const COLOR_LAYOUT: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const VEC2_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const PIXEL_LAYOUT: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Builtin(COLOR),
|
||||
];
|
||||
const INPUT_PAD_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const INPUT_TOUCH_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const INPUT_BUTTON_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
|
||||
const VEC2_FIELDS: [BuiltinFieldMeta; 2] = [
|
||||
BuiltinFieldMeta {
|
||||
@@ -285,13 +322,13 @@ const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
BuiltinFieldMeta {
|
||||
name: "x",
|
||||
start_slot: 0,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int64),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
name: "y",
|
||||
start_slot: 1,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int64),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
@@ -302,7 +339,7 @@ const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
},
|
||||
];
|
||||
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 6] = [
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 9] = [
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0001,
|
||||
name: COLOR.name,
|
||||
@@ -330,35 +367,34 @@ const BUILTIN_TYPES: [BuiltinTypeMeta; 6] = [
|
||||
flat_slot_layout: &PIXEL_LAYOUT,
|
||||
flat_slot_width: 3,
|
||||
},
|
||||
scalar_meta(0x10, "Int32", &[AbiType::Scalar(BuiltinScalarType::Int32)]),
|
||||
scalar_meta(0x11, "Int64", &[AbiType::Scalar(BuiltinScalarType::Int64)]),
|
||||
scalar_meta(0x12, "Float", &[AbiType::Scalar(BuiltinScalarType::Float)]),
|
||||
scalar_meta(0x13, "Bool", &[AbiType::Scalar(BuiltinScalarType::Bool)]),
|
||||
scalar_meta(0x14, "String", &[AbiType::Scalar(BuiltinScalarType::String)]),
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0100,
|
||||
name: INPUT_PAD.name,
|
||||
version: INPUT_PAD.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
id: 0x15,
|
||||
name: "Dynamic",
|
||||
version: 1,
|
||||
shape: BuiltinTypeShape::Dynamic,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_PAD_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0101,
|
||||
name: INPUT_TOUCH.name,
|
||||
version: INPUT_TOUCH.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_TOUCH_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0102,
|
||||
name: INPUT_BUTTON.name,
|
||||
version: INPUT_BUTTON.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_BUTTON_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
flat_slot_layout: &[AbiType::DynamicTag, AbiType::DynamicPayload],
|
||||
flat_slot_width: 2,
|
||||
},
|
||||
];
|
||||
|
||||
const fn scalar_meta(id: u32, name: &'static str, layout: &'static [AbiType]) -> BuiltinTypeMeta {
|
||||
BuiltinTypeMeta {
|
||||
id,
|
||||
name,
|
||||
version: 1,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: layout,
|
||||
flat_slot_width: 1,
|
||||
}
|
||||
}
|
||||
|
||||
const VEC2_ZERO_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const VEC2_ZERO_SLOTS: [BuiltinConstSlotValue; 2] =
|
||||
@@ -434,27 +470,20 @@ const NO_ARGS: [AbiType; 0] = [];
|
||||
const COLOR_ARGS: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const COLOR_RET: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const COLOR_RGB_ARGS: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
];
|
||||
const COLOR_RGBA_ARGS: [AbiType; 4] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
];
|
||||
const COLOR_HTML_RGBA_ARGS: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Str)];
|
||||
const PAD_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const TOUCH_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const BUTTON_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
const PAD_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const TOUCH_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const BUTTON_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
const BOOL_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Bool)];
|
||||
const INT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Int)];
|
||||
const COLOR_HTML_RGBA_ARGS: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::String)];
|
||||
const INT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Int64)];
|
||||
|
||||
const INTRINSICS: [IntrinsicMeta; 42] = [
|
||||
const INTRINSICS: [IntrinsicMeta; 21] = [
|
||||
IntrinsicMeta {
|
||||
id: 0x1000,
|
||||
owner: "vec2",
|
||||
@@ -686,237 +715,6 @@ const INTRINSICS: [IntrinsicMeta; 42] = [
|
||||
may_allocate: false,
|
||||
implementation: color_transparent,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2000,
|
||||
owner: "input",
|
||||
name: "pad",
|
||||
version: 1,
|
||||
arg_layout: &NO_ARGS,
|
||||
ret_layout: &PAD_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2001,
|
||||
owner: "input",
|
||||
name: "touch",
|
||||
version: 1,
|
||||
arg_layout: &NO_ARGS,
|
||||
ret_layout: &TOUCH_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2010,
|
||||
owner: "input.pad",
|
||||
name: "up",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_up,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2011,
|
||||
owner: "input.pad",
|
||||
name: "down",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_down,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2012,
|
||||
owner: "input.pad",
|
||||
name: "left",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_left,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2013,
|
||||
owner: "input.pad",
|
||||
name: "right",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_right,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2014,
|
||||
owner: "input.pad",
|
||||
name: "a",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_a,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2015,
|
||||
owner: "input.pad",
|
||||
name: "b",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_b,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2016,
|
||||
owner: "input.pad",
|
||||
name: "x",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_x,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2017,
|
||||
owner: "input.pad",
|
||||
name: "y",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_y,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2018,
|
||||
owner: "input.pad",
|
||||
name: "l",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_l,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2019,
|
||||
owner: "input.pad",
|
||||
name: "r",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_r,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x201A,
|
||||
owner: "input.pad",
|
||||
name: "start",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_start,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x201B,
|
||||
owner: "input.pad",
|
||||
name: "select",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_select,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2020,
|
||||
owner: "input.touch",
|
||||
name: "button",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_button,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2021,
|
||||
owner: "input.touch",
|
||||
name: "x",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_x,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2022,
|
||||
owner: "input.touch",
|
||||
name: "y",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_y,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2030,
|
||||
owner: "input.button",
|
||||
name: "pressed",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_pressed,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2031,
|
||||
owner: "input.button",
|
||||
name: "released",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_released,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2032,
|
||||
owner: "input.button",
|
||||
name: "down",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_down,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2033,
|
||||
owner: "input.button",
|
||||
name: "hold",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_hold,
|
||||
},
|
||||
];
|
||||
|
||||
pub fn lookup_builtin_type(name: &str, version: u16) -> Option<&'static BuiltinTypeMeta> {
|
||||
@@ -944,11 +742,15 @@ pub fn materialize_builtin_constant(
|
||||
pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
|
||||
}
|
||||
|
||||
pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS.iter().find(|meta| meta.id == id)
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||
.find(|meta| meta.id == id)
|
||||
}
|
||||
|
||||
fn flatten_layout_type(layout_type: BuiltinLayoutType, out: &mut Vec<AbiType>) {
|
||||
@@ -967,20 +769,26 @@ fn expect_float_arg(args: &[Value], index: usize) -> Result<f64, IntrinsicExecut
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_float().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
})
|
||||
match value {
|
||||
Value::Float(value) => Ok(*value),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_int_arg(args: &[Value], index: usize) -> Result<i64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_integer().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
})
|
||||
match value {
|
||||
Value::Int64(value) => Ok(*value),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_string_arg(args: &[Value], index: usize) -> Result<&str, IntrinsicExecutionError> {
|
||||
@@ -991,18 +799,24 @@ fn expect_string_arg(args: &[Value], index: usize) -> Result<&str, IntrinsicExec
|
||||
Value::String(value) => Ok(value.as_ref()),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Str),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::String),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn value_matches_abi_type(value: &Value, expected: AbiType) -> bool {
|
||||
match expected {
|
||||
AbiType::Scalar(BuiltinScalarType::Int) => value.as_integer().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::Float) => value.as_float().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::Int32) => matches!(value, Value::Int32(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64) => matches!(value, Value::Int64(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Float) => matches!(value, Value::Float(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Bool) => matches!(value, Value::Boolean(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Str) => matches!(value, Value::String(_)),
|
||||
AbiType::Builtin(_) => value.as_integer().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::String) => {
|
||||
matches!(value, Value::String(_) | Value::Null)
|
||||
}
|
||||
AbiType::Builtin(key) if key == COLOR => {
|
||||
matches!(value, Value::Int64(raw) if u32::try_from(*raw).is_ok())
|
||||
}
|
||||
AbiType::Builtin(_) | AbiType::DynamicTag | AbiType::DynamicPayload => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1023,11 +837,6 @@ fn validate_values_against_layout(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const PAD_HANDLE: i64 = 1;
|
||||
const TOUCH_HANDLE: i64 = 1;
|
||||
const PAD_BUTTON_BASE: i64 = 0x100;
|
||||
const TOUCH_BUTTON_CARRIER: i64 = 0x200;
|
||||
|
||||
fn vec2_dot(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
@@ -1069,7 +878,7 @@ fn expect_channel(args: &[Value], index: usize) -> Result<u8, IntrinsicExecution
|
||||
let value = expect_int_arg(args, index)?;
|
||||
u8::try_from(value).map_err(|_| IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1088,7 +897,7 @@ fn color_from_raw(
|
||||
let raw = u32::try_from(expect_int_arg(args, 0)?).map_err(|_| {
|
||||
IntrinsicExecutionError::TypeMismatch {
|
||||
index: 0,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
}
|
||||
})?;
|
||||
Ok(color_value(Color::from_raw(raw)))
|
||||
@@ -1124,7 +933,7 @@ fn color_html_rgba(
|
||||
let color = parse_html_rgba(expect_string_arg(args, 0)?).ok_or({
|
||||
IntrinsicExecutionError::TypeMismatch {
|
||||
index: 0,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Str),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::String),
|
||||
}
|
||||
})?;
|
||||
Ok(color_value(color))
|
||||
@@ -1227,254 +1036,6 @@ fn color_transparent(
|
||||
color_const(Color::TRANSPARENT)
|
||||
}
|
||||
|
||||
fn input_pad(
|
||||
_args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(PAD_HANDLE)])
|
||||
}
|
||||
|
||||
fn input_touch(
|
||||
_args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(TOUCH_HANDLE)])
|
||||
}
|
||||
|
||||
fn input_pad_up(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "up")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE)])
|
||||
}
|
||||
|
||||
fn input_pad_down(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "down")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 1)])
|
||||
}
|
||||
|
||||
fn input_pad_left(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "left")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 2)])
|
||||
}
|
||||
|
||||
fn input_pad_right(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "right")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 3)])
|
||||
}
|
||||
|
||||
fn input_pad_a(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "a")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 4)])
|
||||
}
|
||||
|
||||
fn input_pad_b(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "b")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 5)])
|
||||
}
|
||||
|
||||
fn input_pad_x(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "x")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 6)])
|
||||
}
|
||||
|
||||
fn input_pad_y(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "y")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 7)])
|
||||
}
|
||||
|
||||
fn input_pad_l(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "l")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 8)])
|
||||
}
|
||||
|
||||
fn input_pad_r(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "r")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 9)])
|
||||
}
|
||||
|
||||
fn input_pad_start(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "start")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 10)])
|
||||
}
|
||||
|
||||
fn input_pad_select(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "select")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 11)])
|
||||
}
|
||||
|
||||
fn input_touch_button(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "button")?;
|
||||
Ok(vec![Value::Int64(TOUCH_BUTTON_CARRIER)])
|
||||
}
|
||||
|
||||
fn input_touch_x(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "x")?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(vec![Value::Int64(platform.input().touch().x() as i64)])
|
||||
}
|
||||
|
||||
fn input_touch_y(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "y")?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(vec![Value::Int64(platform.input().touch().y() as i64)])
|
||||
}
|
||||
|
||||
fn input_button_pressed(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "pressed", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.pressed)])
|
||||
}
|
||||
|
||||
fn input_button_released(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "released", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.released)])
|
||||
}
|
||||
|
||||
fn input_button_down(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "down", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.down)])
|
||||
}
|
||||
|
||||
fn input_button_hold(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "hold", ctx)?;
|
||||
Ok(vec![Value::Int64(button.hold_frames as i64)])
|
||||
}
|
||||
|
||||
fn expect_pad_handle(args: &[Value], name: &'static str) -> Result<(), IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
if carrier != PAD_HANDLE {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.pad",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expect_touch_handle(args: &[Value], name: &'static str) -> Result<(), IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
if carrier != TOUCH_HANDLE {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.touch",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expect_builtin_carrier(args: &[Value], index: usize) -> Result<i64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_integer().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_button(
|
||||
args: &[Value],
|
||||
name: &'static str,
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<prometeu_hal::button::Button, IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
let input = platform.input();
|
||||
if carrier == TOUCH_BUTTON_CARRIER {
|
||||
return Ok(*input.touch().f());
|
||||
}
|
||||
|
||||
let Some(index) = carrier.checked_sub(PAD_BUTTON_BASE) else {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.button",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
};
|
||||
let button = match index {
|
||||
0 => *input.pad().up(),
|
||||
1 => *input.pad().down(),
|
||||
2 => *input.pad().left(),
|
||||
3 => *input.pad().right(),
|
||||
4 => *input.pad().a(),
|
||||
5 => *input.pad().b(),
|
||||
6 => *input.pad().x(),
|
||||
7 => *input.pad().y(),
|
||||
8 => *input.pad().l(),
|
||||
9 => *input.pad().r(),
|
||||
10 => *input.pad().start(),
|
||||
11 => *input.pad().select(),
|
||||
_ => {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.button",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
};
|
||||
Ok(button)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1501,8 +1062,8 @@ mod tests {
|
||||
assert_eq!(
|
||||
pixel.flatten_layout(),
|
||||
vec![
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Builtin(COLOR),
|
||||
]
|
||||
);
|
||||
@@ -1570,8 +1131,8 @@ mod tests {
|
||||
assert_eq!(lookup_intrinsic_by_id(length.id).map(|meta| meta.key()), Some(length.key()));
|
||||
|
||||
let hold =
|
||||
lookup_intrinsic("input.button", "hold", 1).expect("input.button.hold must exist");
|
||||
assert_eq!(hold.arg_layout.len(), 1);
|
||||
lookup_intrinsic("input.pad.up", "hold", 1).expect("input.pad.up.hold must exist");
|
||||
assert_eq!(hold.arg_layout.len(), 0);
|
||||
assert_eq!(hold.ret_layout.len(), 1);
|
||||
assert!(hold.deterministic);
|
||||
assert!(!hold.may_allocate);
|
||||
@@ -1621,7 +1182,7 @@ mod tests {
|
||||
fn input_intrinsic_requires_hardware_context() {
|
||||
let mut ctx = HostContext::new(None);
|
||||
let touch_x = lookup_intrinsic("input.touch", "x", 1).expect("input.touch.x must exist");
|
||||
let err = (touch_x.implementation)(&[Value::Int64(TOUCH_HANDLE)], &mut ctx)
|
||||
let err = (touch_x.implementation)(&[], &mut ctx)
|
||||
.expect_err("touch.x must fail without hardware context");
|
||||
assert_eq!(err, IntrinsicExecutionError::HardwareUnavailable);
|
||||
}
|
||||
@@ -1629,9 +1190,23 @@ mod tests {
|
||||
#[test]
|
||||
fn pixel_flat_values_validate_with_color_carrier_slot() {
|
||||
let pixel = lookup_builtin_type("pixel", 1).expect("pixel builtin must exist");
|
||||
let values = vec![Value::Int32(1), Value::Int32(2), Value::Int32(0xF800)];
|
||||
let values = vec![Value::Int64(1), Value::Int64(2), Value::Int64(0xF800)];
|
||||
pixel.validate_flat_values(&values).expect("pixel flat values must validate");
|
||||
assert_eq!(pixel.flat_slot_width, 3);
|
||||
assert_eq!(pixel.field("color").map(|field| field.start_slot), Some(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// Loader-side lookup charges each catalogue record before examining it.
|
||||
pub(crate) fn metered_lookup_intrinsic(
|
||||
id: u32,
|
||||
budget: &mut prometeu_bytecode::load_limits::LoadBudget,
|
||||
) -> Result<Option<&'static IntrinsicMeta>, prometeu_bytecode::load_limits::LoadFailure> {
|
||||
for meta in INTRINSICS.iter().chain(crate::input_intrinsics::INPUT_INTRINSICS.iter()) {
|
||||
budget.work(1)?;
|
||||
if meta.id == id {
|
||||
return Ok(Some(meta));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,147 @@
|
||||
//! Exact-width immutable native storage for a present multi-slot Dynamic.
|
||||
use crate::resource_budget::{
|
||||
Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
|
||||
};
|
||||
use crate::value_contract::ValueError;
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::registry::{Registry, TypeId};
|
||||
use std::alloc::{Layout, alloc, dealloc};
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct DynamicBox {
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
values: Box<[Value]>,
|
||||
charge: Reservation,
|
||||
}
|
||||
impl DynamicBox {
|
||||
/// Heap validates the exact non-Dynamic multi-slot group before construction.
|
||||
/// Source owners remain borrowed until publication; allocation never invokes GC.
|
||||
pub fn new(
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
values: &[Value],
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ResourceFailure> {
|
||||
let bytes = budget.bytes::<Value>(values.len(), Q::GuestBytes)?;
|
||||
let layout = Layout::array::<Value>(values.len())
|
||||
.map_err(|_| budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, bytes))?;
|
||||
let charge = budget.reserve(Q::GuestBytes, bytes)?;
|
||||
let ptr = if values.is_empty() {
|
||||
NonNull::dangling()
|
||||
} else {
|
||||
// SAFETY: checked nonzero array layout; a null allocator result is a
|
||||
// recoverable resource failure and drops the provisional reservation.
|
||||
NonNull::new(unsafe { alloc(layout) }.cast::<Value>()).ok_or_else(|| {
|
||||
budget.failure(Q::GuestBytes, ResourceReason::AllocationFailed, bytes)
|
||||
})?
|
||||
};
|
||||
let mut pending = PendingValues { ptr, layout, initialized: 0 };
|
||||
for value in values {
|
||||
let cloned = value.clone();
|
||||
// SAFETY: initialized advances within the exactly allocated Value array;
|
||||
// ptr::write moves each owner into an unused, aligned native slot.
|
||||
unsafe {
|
||||
ptr.as_ptr().add(pending.initialized).write(cloned);
|
||||
}
|
||||
pending.initialized += 1;
|
||||
}
|
||||
// SAFETY: allocation came from the global allocator with precisely the
|
||||
// Box<[Value]> layout; all elements are initialized. Transfer ownership
|
||||
// without Vec capacity/shrink reallocations or a second allocation.
|
||||
let boxed = unsafe {
|
||||
Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr.as_ptr(), values.len()))
|
||||
};
|
||||
std::mem::forget(pending);
|
||||
Ok(Self { registry, type_id, values: boxed, charge })
|
||||
}
|
||||
pub fn bytes(&self) -> u64 {
|
||||
self.charge.amount()
|
||||
}
|
||||
pub fn values(&self) -> &[Value] {
|
||||
&self.values
|
||||
}
|
||||
pub fn typed_values(
|
||||
&self,
|
||||
registry: &Registry,
|
||||
type_id: TypeId,
|
||||
) -> Result<&[Value], ValueError> {
|
||||
if !std::ptr::eq(self.registry.as_ref(), registry) || self.type_id != type_id {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
Ok(&self.values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Owns the partially initialized allocation until Box takes over. A panic
|
||||
/// while constructing an owner drops only the initialized prefix, then frees it.
|
||||
struct PendingValues {
|
||||
ptr: NonNull<Value>,
|
||||
layout: Layout,
|
||||
initialized: usize,
|
||||
}
|
||||
impl Drop for PendingValues {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: initialized counts constructed Values only, all uniquely owned.
|
||||
unsafe {
|
||||
std::ptr::drop_in_place(std::ptr::slice_from_raw_parts_mut(
|
||||
self.ptr.as_ptr(),
|
||||
self.initialized,
|
||||
));
|
||||
if self.layout.size() != 0 {
|
||||
dealloc(self.ptr.as_ptr().cast(), self.layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn interrupted_native_initialization_drops_the_constructed_prefix_and_refunds() {
|
||||
let budget = RuntimeBudget::default();
|
||||
let text: Arc<str> = Arc::from("partially initialized");
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
let layout = Layout::array::<Value>(3).unwrap();
|
||||
let _charge = budget.reserve(Q::GuestBytes, layout.size() as u64).unwrap();
|
||||
// SAFETY: checked nonzero layout; only the first slot is initialized.
|
||||
let ptr = NonNull::new(unsafe { alloc(layout) }.cast::<Value>()).unwrap();
|
||||
let mut pending = PendingValues { ptr, layout, initialized: 0 };
|
||||
unsafe {
|
||||
ptr.as_ptr().write(Value::String(text.clone()));
|
||||
}
|
||||
pending.initialized = 1;
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
panic!("simulate interrupted construction before the next native slot");
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_width_storage_owns_no_allocation_and_is_not_a_valid_guest_box() {
|
||||
let registry = crate::typed_storage::tests::fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let empty = DynamicBox::new(registry, TypeId(5), &[], &budget).unwrap();
|
||||
assert!(empty.values().is_empty());
|
||||
assert_eq!(empty.bytes(), 0);
|
||||
drop(empty);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
// Heap's group validator rejects empty values before invoking this storage constructor.
|
||||
let mut heap = crate::heap::Heap::with_budget(&budget);
|
||||
assert_eq!(
|
||||
heap.allocate_dynamic_box(
|
||||
crate::typed_storage::tests::fixture_registry(),
|
||||
TypeId(5),
|
||||
&[]
|
||||
)
|
||||
.unwrap_err(),
|
||||
ValueError::Type
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,436 @@
|
||||
use super::*;
|
||||
use crate::resource_budget::{ResourceReason, RuntimeLimits};
|
||||
use crate::typed_storage::tests::fixture_registry;
|
||||
use crate::value_contract::{
|
||||
ValueError as E, can_unpack_dynamic as can_unpack, pack_dynamic as pack,
|
||||
unpack_dynamic as unpack,
|
||||
};
|
||||
use prometeu_bytecode::load_limits::LoadBudget;
|
||||
use prometeu_bytecode::registry::ImplementationDecl;
|
||||
|
||||
const I32: TypeId = TypeId(0);
|
||||
const I64: TypeId = TypeId(1);
|
||||
const COLOR: TypeId = TypeId(4);
|
||||
const VEC: TypeId = TypeId(5);
|
||||
const PIXEL: TypeId = TypeId(6);
|
||||
const STRING: TypeId = TypeId(7);
|
||||
const DYNAMIC: TypeId = TypeId(8);
|
||||
const ENEMY: TypeId = TypeId(9);
|
||||
const OTHER: TypeId = TypeId(10);
|
||||
const MIXED: TypeId = TypeId(11);
|
||||
const TRAIT: TypeId = TypeId(12);
|
||||
const INTS: TypeId = TypeId(13);
|
||||
const DYNAMICS: TypeId = TypeId(18);
|
||||
|
||||
fn payload_handle(values: &[Value]) -> HeapRef {
|
||||
let [Value::Int64(_), Value::HeapRef(r)] = values else { panic!("boxed Dynamic pair") };
|
||||
*r
|
||||
}
|
||||
fn forge_box(heap: &mut Heap, registry: Arc<Registry>, ty: TypeId, values: &[Value]) -> HeapRef {
|
||||
let dynamic_box = DynamicBox::new(registry, ty, values, &heap.budget).unwrap();
|
||||
heap.insert_object(StoredObject {
|
||||
header: ObjectHeader::new(ObjectKind::DynamicBox, values.len() as u32),
|
||||
payload: BudgetVec::new(&heap.budget, Q::GuestBytes),
|
||||
array_elems: None,
|
||||
closure_env: None,
|
||||
coroutine: None,
|
||||
typed: None,
|
||||
dynamic_box: Some(dynamic_box),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_slot_pack_has_one_exact_charged_box_and_immutable_copied_values() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
for (ty, mut input) in [
|
||||
(VEC, vec![Value::Float(1.25), Value::Float(-2.5)]),
|
||||
(
|
||||
PIXEL,
|
||||
vec![Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(u32::MAX.into())],
|
||||
),
|
||||
] {
|
||||
let before = budget.used(Q::GuestBytes);
|
||||
let original = input.clone();
|
||||
let dynamic = pack(®istry, &mut heap, ty, &input).unwrap();
|
||||
assert_eq!(
|
||||
budget.used(Q::GuestBytes) - before,
|
||||
(size_of::<StoredObject>() + input.len() * size_of::<Value>()) as u64
|
||||
);
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
assert_eq!(heap.header(r).unwrap().kind, ObjectKind::DynamicBox);
|
||||
assert_eq!(heap.dynamic_box_values(®istry, r, ty).unwrap().len(), input.len());
|
||||
input.fill(Value::Null);
|
||||
assert_eq!(unpack(®istry, &heap, ty, dynamic.as_slice()).unwrap().as_slice(), original);
|
||||
assert!(can_unpack(®istry, &heap, ty, dynamic.as_slice()).unwrap());
|
||||
assert!(
|
||||
!can_unpack(®istry, &heap, if ty == VEC { PIXEL } else { VEC }, dynamic.as_slice())
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(heap.len(), 2);
|
||||
assert_eq!(budget.used(Q::GuestBytes), heap.used_bytes.load(Ordering::Relaxed) as u64);
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repack_and_dynamic_extraction_preserve_the_pair_without_another_box() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let first = pack(®istry, &mut heap, VEC, &[Value::Float(3.0), Value::Float(4.0)]).unwrap();
|
||||
for _ in 0..100 {
|
||||
let again = pack(®istry, &mut heap, DYNAMIC, first.as_slice()).unwrap();
|
||||
let extracted = unpack(®istry, &heap, DYNAMIC, again.as_slice()).unwrap();
|
||||
assert_eq!(extracted.as_slice(), first.as_slice());
|
||||
assert!(can_unpack(®istry, &heap, DYNAMIC, again.as_slice()).unwrap());
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), 1);
|
||||
assert_eq!(heap.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_string_and_reference_payloads_are_direct_and_exact() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let enemy = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let text: Arc<str> = Arc::from("shared");
|
||||
let before = heap.allocation_count();
|
||||
for (ty, input) in [
|
||||
(I32, Value::Int32(7)),
|
||||
(I64, Value::Int64(7)),
|
||||
(COLOR, Value::Int64(7)),
|
||||
(STRING, Value::String(text.clone())),
|
||||
(ENEMY, Value::HeapRef(enemy)),
|
||||
] {
|
||||
let dynamic = pack(®istry, &mut heap, ty, std::slice::from_ref(&input)).unwrap();
|
||||
let result = unpack(®istry, &heap, ty, dynamic.as_slice()).unwrap();
|
||||
assert_eq!(result.as_slice(), [input]);
|
||||
if let [Value::String(alias)] = result.as_slice() {
|
||||
assert!(Arc::ptr_eq(alias, &text));
|
||||
}
|
||||
if ty == I64 {
|
||||
assert!(!can_unpack(®istry, &heap, COLOR, dynamic.as_slice()).unwrap());
|
||||
}
|
||||
if ty == COLOR {
|
||||
assert!(!can_unpack(®istry, &heap, I64, dynamic.as_slice()).unwrap());
|
||||
}
|
||||
if ty == ENEMY {
|
||||
assert!(!can_unpack(®istry, &heap, TRAIT, dynamic.as_slice()).unwrap());
|
||||
}
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), before);
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_compatibility_preserves_typed_identity_and_rebuilds_absent_trait() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let untyped = [Value::Null, Value::Null];
|
||||
for ty in [STRING, ENEMY, OTHER, TRAIT, INTS] {
|
||||
assert!(can_unpack(®istry, &heap, ty, &untyped).unwrap());
|
||||
let absent = registry.type_layout(ty).unwrap().storage.default_values();
|
||||
assert_eq!(unpack(®istry, &heap, ty, &untyped).unwrap().as_slice(), absent);
|
||||
let typed = pack(®istry, &mut heap, ty, absent).unwrap();
|
||||
assert_eq!(typed.as_slice(), [Value::Int64(ty.0.into()), Value::Null]);
|
||||
assert_eq!(unpack(®istry, &heap, ty, typed.as_slice()).unwrap().as_slice(), absent);
|
||||
let other = if ty == STRING { ENEMY } else { STRING };
|
||||
assert!(!can_unpack(®istry, &heap, other, typed.as_slice()).unwrap());
|
||||
assert_eq!(unpack(®istry, &heap, other, typed.as_slice()).unwrap_err(), E::Type);
|
||||
}
|
||||
for ty in [I32, VEC, PIXEL] {
|
||||
assert!(!can_unpack(®istry, &heap, ty, &untyped).unwrap());
|
||||
assert_eq!(unpack(®istry, &heap, ty, &untyped).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(pack(®istry, &mut heap, DYNAMIC, &untyped).unwrap().as_slice(), untyped);
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_box_keeps_receiver_identity_and_gc_reaches_through_typed_dynamic_storage() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let source = [Value::HeapRef(receiver), Value::Int64(0)];
|
||||
// A safepoint before pack sees the original source reference.
|
||||
heap.mark_from_roots([receiver]).unwrap();
|
||||
heap.sweep();
|
||||
let dynamic = pack(®istry, &mut heap, TRAIT, &source).unwrap();
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
let holder = heap.allocate_typed_array(registry.clone(), DYNAMICS, 2).unwrap();
|
||||
heap.store_typed(®istry, holder, 0, dynamic.as_slice()).unwrap();
|
||||
heap.store_typed(®istry, holder, 1, dynamic.as_slice()).unwrap();
|
||||
assert_eq!(heap.len(), 3); // Receiver, one shared box, holder.
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(receiver));
|
||||
assert!(heap.is_valid(r));
|
||||
assert_eq!(unpack(®istry, &heap, TRAIT, dynamic.as_slice()).unwrap().as_slice(), source);
|
||||
heap.store_typed(®istry, holder, 0, &[Value::Null, Value::Null]).unwrap();
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(r)); // Second alias still owns reachability.
|
||||
heap.store_typed(®istry, holder, 1, &[Value::Null, Value::Null]).unwrap();
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(!heap.is_valid(r));
|
||||
assert!(!heap.is_valid(receiver));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_is_not_a_guest_instance_or_collection_and_cannot_be_a_trait_receiver() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let dynamic = pack(®istry, &mut heap, VEC, &[Value::Float(0.0), Value::Float(0.0)]).unwrap();
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
assert_eq!(heap.load_typed(®istry, r, 0).unwrap_err(), E::Type);
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &[Value::Float(1.0)]).unwrap_err(), E::Type);
|
||||
assert_eq!(
|
||||
value_contract::validate(®istry, &heap, ENEMY, &[Value::HeapRef(r)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(
|
||||
value_contract::validate(®istry, &heap, TRAIT, &[Value::HeapRef(r), Value::Int64(0)])
|
||||
.unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(heap.concrete_type(r, ®istry).unwrap_err(), E::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_dynamic_queries_fault_before_reporting_ordinary_mismatch() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
for bad in [
|
||||
vec![],
|
||||
vec![Value::Null],
|
||||
vec![Value::Null, Value::Int64(0)],
|
||||
vec![Value::Int64(DYNAMIC.0.into()), Value::Null],
|
||||
vec![Value::Int64(VEC.0.into()), Value::HeapRef(receiver)],
|
||||
vec![Value::Int64(VEC.0.into()), Value::HeapRef(HeapRef::NULL)],
|
||||
vec![Value::Int64(u32::MAX as i64 + 1), Value::Null],
|
||||
vec![Value::Int64(999), Value::Null],
|
||||
vec![Value::Int64(I32.0.into()), Value::Int64(0)],
|
||||
] {
|
||||
assert_eq!(can_unpack(®istry, &heap, STRING, &bad).unwrap_err(), E::Type);
|
||||
assert_eq!(unpack(®istry, &heap, STRING, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
let stale = heap
|
||||
.allocate_dynamic_box(registry.clone(), VEC, &[Value::Float(1.0), Value::Float(2.0)])
|
||||
.unwrap();
|
||||
heap.sweep();
|
||||
let live = heap
|
||||
.allocate_dynamic_box(registry.clone(), VEC, &[Value::Float(3.0), Value::Float(4.0)])
|
||||
.unwrap();
|
||||
assert_eq!(stale.index, live.index);
|
||||
assert_ne!(stale.generation, live.generation);
|
||||
for handle in [
|
||||
stale,
|
||||
HeapRef { index: live.index, generation: 0 },
|
||||
HeapRef { index: u32::MAX, generation: 1 },
|
||||
] {
|
||||
let bad = [Value::Int64(VEC.0.into()), Value::HeapRef(handle)];
|
||||
assert_eq!(can_unpack(®istry, &heap, STRING, &bad).unwrap_err(), E::InvalidHandle);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forged_box_category_type_width_group_and_registry_are_rejected() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let wrong_boxes = [
|
||||
forge_box(&mut heap, registry.clone(), VEC, &[Value::Float(1.0)]),
|
||||
forge_box(&mut heap, registry.clone(), VEC, &[Value::Float(1.0), Value::Int64(2)]),
|
||||
forge_box(&mut heap, registry.clone(), TRAIT, &[Value::HeapRef(receiver), Value::Int64(0)]),
|
||||
forge_box(&mut heap, fixture_registry(), VEC, &[Value::Float(1.0), Value::Float(2.0)]),
|
||||
heap.allocate_object(ObjectKind::DynamicBox, &[]).unwrap(),
|
||||
];
|
||||
for r in wrong_boxes {
|
||||
assert_eq!(
|
||||
can_unpack(®istry, &heap, STRING, &[Value::Int64(VEC.0.into()), Value::HeapRef(r)])
|
||||
.unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
}
|
||||
for contents in
|
||||
[vec![Value::Null, Value::Null], vec![Value::HeapRef(receiver), Value::Int64(1)]]
|
||||
{
|
||||
let r = forge_box(&mut heap, registry.clone(), TRAIT, &contents);
|
||||
assert_eq!(
|
||||
can_unpack(®istry, &heap, TRAIT, &[Value::Int64(TRAIT.0.into()), Value::HeapRef(r)])
|
||||
.unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
}
|
||||
let r = forge_box(&mut heap, registry.clone(), VEC, &[Value::Float(1.0), Value::Float(2.0)]);
|
||||
heap.objects[r.index as usize].as_mut().unwrap().header.payload_len = 3;
|
||||
assert_eq!(heap.dynamic_box_values(®istry, r, VEC).unwrap_err(), E::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_pack_never_allocates_or_consumes_source_values() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
for (ty, input) in [
|
||||
(VEC, vec![Value::Float(1.0)]),
|
||||
(PIXEL, vec![Value::Int64(0), Value::Int64(0), Value::Int64(-1)]),
|
||||
(TRAIT, vec![Value::Null, Value::Int64(0)]),
|
||||
(DYNAMIC, vec![Value::Null, Value::Int64(0)]),
|
||||
] {
|
||||
let copy = input.clone();
|
||||
assert_eq!(pack(®istry, &mut heap, ty, &input).unwrap_err(), E::Type);
|
||||
assert_eq!(input, copy);
|
||||
assert_eq!(heap.len(), 0);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
// The lower-level allocator also rejects categories that must never box.
|
||||
for (ty, input) in [
|
||||
(I32, vec![Value::Int32(0)]),
|
||||
(TRAIT, vec![Value::Null, Value::Null]),
|
||||
(DYNAMIC, vec![Value::Null, Value::Null]),
|
||||
(DYNAMIC, vec![Value::Int64(I32.0.into()), Value::Int32(1)]),
|
||||
] {
|
||||
assert_eq!(heap.allocate_dynamic_box(registry.clone(), ty, &input).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_guest_quota_boundary_and_late_table_failure_refund_all_reservations() {
|
||||
let registry = fixture_registry();
|
||||
let bytes = (size_of::<StoredObject>() + 2 * size_of::<Value>()) as u64;
|
||||
for limit in [bytes - 1, bytes] {
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::GuestBytes, limit).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let source = [Value::Float(1.0), Value::Float(2.0)];
|
||||
let result = pack(®istry, &mut heap, VEC, &source);
|
||||
if limit == bytes {
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(budget.used(Q::GuestBytes), bytes);
|
||||
} else {
|
||||
assert!(
|
||||
matches!(result, Err(E::Resource(e)) if e.resource == Q::GuestBytes && e.reason == ResourceReason::Limit)
|
||||
);
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] {
|
||||
assert_eq!(budget.used(q), 0);
|
||||
}
|
||||
}
|
||||
assert_eq!(source, [Value::Float(1.0), Value::Float(2.0)]);
|
||||
}
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::AuxiliaryBytes, 0).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
assert!(
|
||||
matches!(pack(®istry, &mut heap, VEC, &[Value::Float(1.0), Value::Float(2.0)]), Err(E::Resource(e)) if e.resource == Q::AuxiliaryBytes)
|
||||
);
|
||||
assert_eq!(heap.len(), 0);
|
||||
for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] {
|
||||
assert_eq!(budget.used(q), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_and_unpack_succeed_at_full_heap_quota_without_allocating() {
|
||||
let registry = fixture_registry();
|
||||
let bytes = (size_of::<StoredObject>() + 2 * size_of::<Value>()) as u64;
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::GuestBytes, bytes).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let dynamic = pack(®istry, &mut heap, VEC, &[Value::Float(1.0), Value::Float(2.0)]).unwrap();
|
||||
for _ in 0..100 {
|
||||
assert!(can_unpack(®istry, &heap, VEC, dynamic.as_slice()).unwrap());
|
||||
assert_eq!(
|
||||
unpack(®istry, &heap, VEC, dynamic.as_slice()).unwrap().as_slice(),
|
||||
[Value::Float(1.0), Value::Float(2.0)]
|
||||
);
|
||||
assert_eq!(
|
||||
pack(®istry, &mut heap, DYNAMIC, dynamic.as_slice()).unwrap().as_slice(),
|
||||
dynamic.as_slice()
|
||||
);
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), bytes);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collecting_a_trait_box_drops_transitive_native_owners_once() {
|
||||
let mut declarations = fixture_registry().declarations().clone();
|
||||
declarations.implementations.push(ImplementationDecl {
|
||||
owner: MIXED,
|
||||
trait_id: TRAIT,
|
||||
methods: vec![],
|
||||
});
|
||||
let registry = Arc::new(
|
||||
Registry::resolve(declarations, 0, &mut LoadBudget::new(Default::default())).unwrap(),
|
||||
);
|
||||
let mut heap = Heap::new();
|
||||
let text: Arc<str> = Arc::from("retained through boxed trait");
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
heap.store_typed(®istry, receiver, 3, &[Value::String(text.clone())]).unwrap();
|
||||
let dynamic =
|
||||
pack(®istry, &mut heap, TRAIT, &[Value::HeapRef(receiver), Value::Int64(2)]).unwrap();
|
||||
// Receiver -> Dynamic box -> receiver is a real typed heap cycle.
|
||||
heap.store_typed(®istry, receiver, 4, dynamic.as_slice()).unwrap();
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
heap.mark_from_roots([r]).unwrap();
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(heap.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_box_storage_drops_native_owners_even_for_rejected_groups() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let text: Arc<str> = Arc::from("malformed box fixture");
|
||||
let r = forge_box(
|
||||
&mut heap,
|
||||
registry.clone(),
|
||||
VEC,
|
||||
&[Value::String(text.clone()), Value::Float(1.0)],
|
||||
);
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
assert_eq!(heap.dynamic_box_values(®istry, r, VEC).unwrap_err(), E::Type);
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_trait_box_publication_preserves_source_roots_and_existing_table_capacities() {
|
||||
let registry = fixture_registry();
|
||||
let one_slot_aux =
|
||||
(size_of::<Option<StoredAllocation>>() + size_of::<u32>() + size_of::<usize>()) as u64;
|
||||
let budget = RuntimeBudget::new(
|
||||
RuntimeLimits::default().lowered(Q::AuxiliaryBytes, one_slot_aux).unwrap(),
|
||||
);
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let before = budget.used(Q::GuestBytes);
|
||||
let source = [Value::HeapRef(receiver), Value::Int64(0)];
|
||||
assert!(
|
||||
matches!(pack(®istry, &mut heap, TRAIT, &source), Err(E::Resource(e)) if e.resource == Q::AuxiliaryBytes)
|
||||
);
|
||||
assert_eq!(heap.len(), 1);
|
||||
assert_eq!(heap.allocation_count(), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), before);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), one_slot_aux);
|
||||
assert_eq!(budget.used(Q::HandleSlots), 1);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert_eq!(source, [Value::HeapRef(receiver), Value::Int64(0)]);
|
||||
heap.mark_from_roots(source.iter().filter_map(crate::roots::value_heap_ref)).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(receiver));
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
//! Fixed queries over the logical-frame input snapshot. No endpoint or selector
|
||||
//! crosses the guest boundary; names below identify operations, not value types.
|
||||
use crate::builtins::{AbiType, BuiltinScalarType, IntrinsicExecutionError, IntrinsicMeta};
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_hal::{HostContext, InputPlatform, button::Button};
|
||||
|
||||
const BOOL: &[AbiType] = &[AbiType::Scalar(BuiltinScalarType::Bool)];
|
||||
const INT: &[AbiType] = &[AbiType::Scalar(BuiltinScalarType::Int64)];
|
||||
|
||||
const fn query(
|
||||
id: u32,
|
||||
owner: &'static str,
|
||||
name: &'static str,
|
||||
ret_layout: &'static [AbiType],
|
||||
implementation: crate::IntrinsicImplementation,
|
||||
) -> IntrinsicMeta {
|
||||
IntrinsicMeta {
|
||||
id,
|
||||
owner,
|
||||
name,
|
||||
version: 1,
|
||||
arg_layout: &[],
|
||||
ret_layout,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation,
|
||||
}
|
||||
}
|
||||
|
||||
fn snapshot<'a>(
|
||||
args: &[Value],
|
||||
ctx: &'a HostContext<'_>,
|
||||
) -> Result<&'a dyn InputPlatform, IntrinsicExecutionError> {
|
||||
if !args.is_empty() {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch { expected: 0, got: args.len() });
|
||||
}
|
||||
let platform = ctx.platform.as_deref().ok_or(IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(platform.input())
|
||||
}
|
||||
|
||||
fn read_button<const PROPERTY: u8>(button: &Button) -> Vec<Value> {
|
||||
vec![match PROPERTY {
|
||||
0 => Value::Boolean(button.pressed),
|
||||
1 => Value::Boolean(button.released),
|
||||
2 => Value::Boolean(button.down),
|
||||
3 => Value::Int64(i64::from(button.hold_frames)),
|
||||
_ => unreachable!("private input property outside the fixed catalogue"),
|
||||
}]
|
||||
}
|
||||
|
||||
// This single mapping defines IDs, canonical names and their snapshot readers.
|
||||
macro_rules! input_queries {
|
||||
($(($base:literal, $prefix:literal, $reader:ident, $device:ident, $button:ident)),* $(,)?) => {
|
||||
pub(crate) const INPUT_INTRINSICS: [IntrinsicMeta; 54] = [
|
||||
$(
|
||||
query($base, $prefix, "pressed", BOOL, $reader::<0>),
|
||||
query($base + 1, $prefix, "released", BOOL, $reader::<1>),
|
||||
query($base + 2, $prefix, "down", BOOL, $reader::<2>),
|
||||
query($base + 3, $prefix, "hold", INT, $reader::<3>),
|
||||
)*
|
||||
query(0x2134, "input.touch", "x", INT, touch_x),
|
||||
query(0x2135, "input.touch", "y", INT, touch_y),
|
||||
];
|
||||
$(
|
||||
fn $reader<const PROPERTY: u8>(
|
||||
args: &[Value], ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(read_button::<PROPERTY>(snapshot(args, ctx)?.$device().$button()))
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
input_queries!(
|
||||
(0x2100, "input.pad.up", up, pad, up),
|
||||
(0x2104, "input.pad.down", down, pad, down),
|
||||
(0x2108, "input.pad.left", left, pad, left),
|
||||
(0x210C, "input.pad.right", right, pad, right),
|
||||
(0x2110, "input.pad.a", a, pad, a),
|
||||
(0x2114, "input.pad.b", b, pad, b),
|
||||
(0x2118, "input.pad.x", x, pad, x),
|
||||
(0x211C, "input.pad.y", y, pad, y),
|
||||
(0x2120, "input.pad.l", l, pad, l),
|
||||
(0x2124, "input.pad.r", r, pad, r),
|
||||
(0x2128, "input.pad.start", start, pad, start),
|
||||
(0x212C, "input.pad.select", select, pad, select),
|
||||
(0x2130, "input.touch.button", touch_button, touch, f),
|
||||
);
|
||||
|
||||
fn touch_x(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().x()))])
|
||||
}
|
||||
|
||||
fn touch_y(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().y()))])
|
||||
}
|
||||
@@ -1,12 +1,22 @@
|
||||
mod builtins;
|
||||
mod call_frame;
|
||||
mod input_intrinsics;
|
||||
mod local_addressing;
|
||||
// Keep the verifier internal in production builds, but expose it for integration tests
|
||||
// so the golden verifier suite can exercise it without widening the public API in releases.
|
||||
mod heap;
|
||||
mod object;
|
||||
pub mod resource_budget;
|
||||
mod roots;
|
||||
// Bounded Registry preflight is consumed by the upcoming v1 image loader.
|
||||
#[allow(dead_code)]
|
||||
mod registry_verifier;
|
||||
mod scheduler;
|
||||
// Registry-backed consumers are integrated with executable images in PLN-0176.
|
||||
#[allow(dead_code)]
|
||||
mod typed_storage;
|
||||
#[allow(dead_code)]
|
||||
mod value_contract;
|
||||
#[cfg(not(test))]
|
||||
mod verifier;
|
||||
#[cfg(test)]
|
||||
@@ -24,3 +34,5 @@ pub use builtins::{
|
||||
pub use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
pub use virtual_machine::{BudgetReport, LogicalFrameEndingReason, VirtualMachine};
|
||||
pub use vm_init_error::{LoaderPatchError, VmInitError};
|
||||
|
||||
pub use verifier::VerifierError;
|
||||
|
||||
@@ -88,6 +88,12 @@ pub enum ObjectKind {
|
||||
/// contained `HeapRef`s directly.
|
||||
/// - `payload_len` is 0 for this fixed-layout object.
|
||||
Coroutine = 6,
|
||||
|
||||
/// Registry-defined Struct with a compact typed instance payload.
|
||||
Struct = 7,
|
||||
|
||||
/// Internal immutable multi-slot Dynamic payload, never a nominal guest type.
|
||||
DynamicBox = 8,
|
||||
// Future kinds must be appended here to keep tag numbers stable.
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
//! Bounded structural preflight of declared Registry code ranges. This phase
|
||||
//! checks every instruction, including unreachable code. Stack/type-flow,
|
||||
//! bindings and entry validation must still succeed before image publication.
|
||||
use prometeu_bytecode::isa::core::{CoreOpCode as Op, CoreOpCodeSpecExt};
|
||||
use prometeu_bytecode::load_limits::{
|
||||
IdDomain, LoadBudget, LoadCode as C, LoadFailure as E, LoadReason as R, LoadResource as Q,
|
||||
};
|
||||
use prometeu_bytecode::registry::{FunctionId, Registry};
|
||||
use prometeu_bytecode::{DecodeError, decode_next};
|
||||
|
||||
pub(crate) fn validate_registry_instructions(
|
||||
code: &[u8],
|
||||
registry: &Registry,
|
||||
constant_entries: usize,
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<(), E> {
|
||||
let temporary_before = budget.used(Q::TemporaryBytes);
|
||||
let result = validate_inner(code, registry, constant_entries, budget);
|
||||
// All maps live inside validate_inner and are already dropped here. Failure
|
||||
// refunds scratch while preserving attempted work and prior load ownership.
|
||||
budget.refund(Q::TemporaryBytes, budget.used(Q::TemporaryBytes) - temporary_before);
|
||||
result
|
||||
}
|
||||
fn validate_inner(
|
||||
code: &[u8],
|
||||
registry: &Registry,
|
||||
constants: usize,
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<(), E> {
|
||||
budget.check(Q::CodeBytes, code.len() as u64)?;
|
||||
budget.check(Q::ConstantEntries, constants as u64)?;
|
||||
budget.check(Q::Functions, registry.declarations().functions.len() as u64)?;
|
||||
for (index, function) in registry.declarations().functions.iter().enumerate() {
|
||||
let function_id = index as u32;
|
||||
budget
|
||||
.work(1)
|
||||
.map_err(|e| e.in_section(2).in_function(function_id, Some(function.code_offset)))?;
|
||||
let start = function.code_offset as usize;
|
||||
let end = start
|
||||
.checked_add(function.code_len as usize)
|
||||
.ok_or_else(|| error(R::CodeRange, function_id, start))?;
|
||||
let contextual =
|
||||
|e: E, pc: usize| e.in_section(2).in_function(function_id, u32::try_from(pc).ok());
|
||||
let body = code.get(start..end).ok_or_else(|| error(R::CodeRange, function_id, start))?;
|
||||
let widths = registry.function_layout(FunctionId(function_id)).unwrap();
|
||||
let local_slots = u64::from(widths.parameter_slots) + u64::from(function.local_slots);
|
||||
let total = local_slots + u64::from(function.max_stack_slots);
|
||||
if total > budget.frame_limit() {
|
||||
return Err(error(R::FrameSlots, function_id, start));
|
||||
}
|
||||
if u64::from(widths.result_slots) > budget.result_limit() {
|
||||
return Err(error(R::ReturnSlots, function_id, start));
|
||||
}
|
||||
let function_scratch_before = budget.used(Q::TemporaryBytes);
|
||||
let mut at_pc = start;
|
||||
let result = (|| {
|
||||
let mut boundaries = boundary_map(
|
||||
body.len().checked_add(1).ok_or_else(|| E::new(C::ResourceLimit, R::Overflow))?,
|
||||
budget,
|
||||
)?;
|
||||
let mut pc = 0;
|
||||
while pc < body.len() {
|
||||
at_pc = start + pc;
|
||||
let instruction =
|
||||
metered_decode(pc, body, budget).map_err(|e| contextual(e, start + pc))?;
|
||||
budget.work(1)?; // Boundary write.
|
||||
boundaries[pc] = 1;
|
||||
pc = instruction.next_pc;
|
||||
}
|
||||
budget.work(1)?;
|
||||
boundaries[body.len()] = 1; // Existing jump-to-function-end return boundary.
|
||||
pc = 0;
|
||||
while pc < body.len() {
|
||||
at_pc = start + pc;
|
||||
let instruction =
|
||||
metered_decode(pc, body, budget).map_err(|e| contextual(e, start + pc))?;
|
||||
let fail = |reason| error(reason, function_id, start + pc);
|
||||
if instruction.opcode.requires_registry() {
|
||||
budget.work(
|
||||
if matches!(
|
||||
instruction.opcode,
|
||||
Op::DynPack | Op::DynUnpack | Op::DynCanUnpack
|
||||
) {
|
||||
2
|
||||
} else {
|
||||
4
|
||||
},
|
||||
)?;
|
||||
}
|
||||
instruction
|
||||
.registry_stack_effect(registry)
|
||||
.map_err(|e| contextual(e, start + pc))?;
|
||||
match instruction.opcode {
|
||||
Op::Jmp | Op::JmpIfTrue | Op::JmpIfFalse => {
|
||||
budget.work(1)?; // Branch edge.
|
||||
let target = instruction.imm_u32().unwrap() as usize;
|
||||
if target > body.len() {
|
||||
return Err(fail(R::JumpTarget));
|
||||
}
|
||||
budget.work(1)?; // Boundary read.
|
||||
if boundaries[target] == 0 {
|
||||
return Err(fail(R::InstructionBoundary));
|
||||
}
|
||||
}
|
||||
Op::GetLocal | Op::SetLocal => {
|
||||
let slot = instruction.imm_u32().unwrap();
|
||||
if u64::from(slot) >= local_slots {
|
||||
return Err(fail(R::LocalSlot).member(slot));
|
||||
}
|
||||
}
|
||||
Op::PushBool if instruction.imm_u8().unwrap() > 1 => {
|
||||
return Err(fail(R::BooleanImmediate));
|
||||
}
|
||||
Op::PushConst => {
|
||||
let id = instruction.imm_u32().unwrap();
|
||||
if id as usize >= constants {
|
||||
return Err(with_id(reference_error(fail(R::ConstantIndex)), id));
|
||||
}
|
||||
}
|
||||
Op::Call | Op::MakeClosure | Op::Spawn => {
|
||||
let id = if instruction.opcode == Op::Call {
|
||||
instruction.imm_u32().unwrap()
|
||||
} else {
|
||||
instruction.imm_u32x2().unwrap().0
|
||||
};
|
||||
let callee = registry.function_layout(FunctionId(id)).ok_or_else(|| {
|
||||
reference_error(fail(R::FunctionId)).reference(IdDomain::Function, id)
|
||||
})?;
|
||||
if instruction.opcode == Op::Spawn
|
||||
&& instruction.imm_u32x2().unwrap().1 != callee.parameter_slots
|
||||
{
|
||||
return Err(fail(R::Signature).reference(IdDomain::Function, id));
|
||||
}
|
||||
}
|
||||
Op::Hostcall => {
|
||||
return Err(with_id(
|
||||
fail(R::UnpatchedHostcall),
|
||||
instruction.imm_u32().unwrap(),
|
||||
));
|
||||
}
|
||||
Op::Syscall => {
|
||||
let id = instruction.imm_u32().unwrap();
|
||||
if prometeu_hal::syscalls::Syscall::from_u32(id).is_none() {
|
||||
return Err(with_id(reference_error(fail(R::NativeId)), id));
|
||||
}
|
||||
}
|
||||
Op::Intrinsic => {
|
||||
let id = instruction.imm_u32().unwrap();
|
||||
if crate::builtins::metered_lookup_intrinsic(id, budget)?.is_none() {
|
||||
return Err(with_id(reference_error(fail(R::NativeId)), id));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
pc = instruction.next_pc;
|
||||
}
|
||||
Ok(())
|
||||
})()
|
||||
.map_err(|e: E| if e.function.is_none() { contextual(e, at_pc) } else { e });
|
||||
// Free each function's map before the next. No aggregate unmetered CFG.
|
||||
budget.refund(Q::TemporaryBytes, budget.used(Q::TemporaryBytes) - function_scratch_before);
|
||||
result?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn boundary_map(count: usize, budget: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
||||
budget.charge(Q::TemporaryBytes, count as u64)?;
|
||||
let mut values = Vec::new();
|
||||
values.try_reserve_exact(count).map_err(|_| {
|
||||
let mut e = E::resource(Q::TemporaryBytes, count as u64, budget.limit(Q::TemporaryBytes));
|
||||
e.reason = R::Allocation;
|
||||
e
|
||||
})?;
|
||||
budget.charge(Q::TemporaryBytes, (values.capacity() - count) as u64)?;
|
||||
budget.work(count)?; // Initialize every map byte before it can be read.
|
||||
values.resize(count, 0);
|
||||
Ok(values)
|
||||
}
|
||||
fn metered_decode<'a>(
|
||||
pc: usize,
|
||||
bytes: &'a [u8],
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<prometeu_bytecode::DecodedInstr<'a>, E> {
|
||||
budget.work(bytes.len().saturating_sub(pc).min(2))?;
|
||||
let result = decode_next(pc, bytes).map_err(|e| {
|
||||
E::new(
|
||||
C::InvalidCode,
|
||||
match e {
|
||||
DecodeError::UnknownOpcode { .. } => R::UnknownOpcode,
|
||||
_ => R::Truncated,
|
||||
},
|
||||
)
|
||||
})?;
|
||||
budget.work(result.imm.len() + 1)?; // Immediate examination and instruction.
|
||||
Ok(result)
|
||||
}
|
||||
fn error(reason: R, function: u32, pc: usize) -> E {
|
||||
E::new(C::InvalidCode, reason).in_section(2).in_function(function, u32::try_from(pc).ok())
|
||||
}
|
||||
fn with_id(mut e: E, id: u32) -> E {
|
||||
e.referenced_id = Some(id);
|
||||
e
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
fn reference_error(mut e: E) -> E {
|
||||
e.code = C::InvalidReference;
|
||||
e
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,503 @@
|
||||
//! Session accounting shared by owners, with reservations refunded on failure/drop.
|
||||
//! These quotas account VM domains, not process RSS or allocator metadata.
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(usize)]
|
||||
pub enum RuntimeResource {
|
||||
GuestBytes,
|
||||
HandleSlots,
|
||||
AuxiliaryBytes,
|
||||
TemporaryBytes,
|
||||
StringBytes,
|
||||
StringAllocations,
|
||||
IndividualStringBytes,
|
||||
CallDepth,
|
||||
}
|
||||
const COUNT: usize = 8;
|
||||
const MIB: u64 = 1_048_576;
|
||||
const CEILINGS: [u64; COUNT] =
|
||||
[256 * MIB, 1_048_576, 64 * MIB, 64 * MIB, 64 * MIB, 262_144, 4 * MIB, 1_024];
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct RuntimeLimits([u64; COUNT]);
|
||||
impl Default for RuntimeLimits {
|
||||
fn default() -> Self {
|
||||
Self(CEILINGS)
|
||||
}
|
||||
}
|
||||
impl RuntimeLimits {
|
||||
pub fn get(&self, resource: RuntimeResource) -> u64 {
|
||||
self.0[resource as usize]
|
||||
}
|
||||
/// Test/host configurations may lower ceilings; guest code cannot raise them.
|
||||
pub fn lowered(
|
||||
mut self,
|
||||
resource: RuntimeResource,
|
||||
limit: u64,
|
||||
) -> Result<Self, ResourceFailure> {
|
||||
if limit > self.get(resource) {
|
||||
return Err(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::Limit,
|
||||
requested: limit,
|
||||
limit: self.get(resource),
|
||||
});
|
||||
}
|
||||
self.0[resource as usize] = limit;
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ResourceReason {
|
||||
Limit,
|
||||
SizeOverflow,
|
||||
AllocationFailed,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ResourceFailure {
|
||||
pub resource: RuntimeResource,
|
||||
pub reason: ResourceReason,
|
||||
pub requested: u64,
|
||||
pub limit: u64,
|
||||
}
|
||||
impl fmt::Display for ResourceFailure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{:?}: {:?}, requested {}, limit {}",
|
||||
self.resource, self.reason, self.requested, self.limit
|
||||
)
|
||||
}
|
||||
}
|
||||
impl std::error::Error for ResourceFailure {}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct State {
|
||||
limits: RuntimeLimits,
|
||||
used: [u64; COUNT],
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RuntimeBudget(Arc<Mutex<State>>);
|
||||
impl Default for RuntimeBudget {
|
||||
fn default() -> Self {
|
||||
Self::new(RuntimeLimits::default())
|
||||
}
|
||||
}
|
||||
impl RuntimeBudget {
|
||||
pub fn new(limits: RuntimeLimits) -> Self {
|
||||
Self(Arc::new(Mutex::new(State { limits, used: [0; COUNT] })))
|
||||
}
|
||||
fn state(&self) -> MutexGuard<'_, State> {
|
||||
self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
pub fn used(&self, resource: RuntimeResource) -> u64 {
|
||||
self.state().used[resource as usize]
|
||||
}
|
||||
pub fn limit(&self, resource: RuntimeResource) -> u64 {
|
||||
self.state().limits.get(resource)
|
||||
}
|
||||
pub fn check(&self, resource: RuntimeResource, requested: u64) -> Result<(), ResourceFailure> {
|
||||
let limit = self.limit(resource);
|
||||
if requested > limit {
|
||||
Err(ResourceFailure { resource, reason: ResourceReason::Limit, requested, limit })
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub fn failure(
|
||||
&self,
|
||||
resource: RuntimeResource,
|
||||
reason: ResourceReason,
|
||||
requested: u64,
|
||||
) -> ResourceFailure {
|
||||
ResourceFailure { resource, reason, requested, limit: self.limit(resource) }
|
||||
}
|
||||
pub fn bytes<T>(
|
||||
&self,
|
||||
count: usize,
|
||||
resource: RuntimeResource,
|
||||
) -> Result<u64, ResourceFailure> {
|
||||
count
|
||||
.checked_mul(size_of::<T>())
|
||||
.and_then(|bytes| u64::try_from(bytes).ok())
|
||||
.ok_or_else(|| self.failure(resource, ResourceReason::SizeOverflow, u64::MAX))
|
||||
}
|
||||
pub fn reserve(
|
||||
&self,
|
||||
resource: RuntimeResource,
|
||||
amount: u64,
|
||||
) -> Result<Reservation, ResourceFailure> {
|
||||
let mut state = self.state();
|
||||
let current = state.used[resource as usize];
|
||||
let limit = state.limits.get(resource);
|
||||
let requested = current.checked_add(amount).ok_or(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::SizeOverflow,
|
||||
requested: u64::MAX,
|
||||
limit,
|
||||
})?;
|
||||
if requested > limit {
|
||||
return Err(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::Limit,
|
||||
requested,
|
||||
limit,
|
||||
});
|
||||
}
|
||||
state.used[resource as usize] = requested;
|
||||
Ok(Reservation { budget: self.clone(), resource, amount })
|
||||
}
|
||||
pub(crate) fn empty_charge(&self, resource: RuntimeResource) -> Reservation {
|
||||
Reservation { budget: self.clone(), resource, amount: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Unique accounting ownership; moving this token never charges a second time.
|
||||
#[derive(Debug)]
|
||||
pub struct Reservation {
|
||||
budget: RuntimeBudget,
|
||||
resource: RuntimeResource,
|
||||
amount: u64,
|
||||
}
|
||||
impl Reservation {
|
||||
pub fn amount(&self) -> u64 {
|
||||
self.amount
|
||||
}
|
||||
pub(crate) fn absorb(&mut self, mut other: Self) {
|
||||
assert!(Arc::ptr_eq(&self.budget.0, &other.budget.0));
|
||||
assert_eq!(self.resource, other.resource);
|
||||
self.amount += other.amount;
|
||||
other.amount = 0;
|
||||
}
|
||||
fn resize(&mut self, amount: u64) -> Result<(), ResourceFailure> {
|
||||
if amount > self.amount {
|
||||
self.absorb(self.budget.reserve(self.resource, amount - self.amount)?);
|
||||
} else {
|
||||
self.budget.state().used[self.resource as usize] -= self.amount - amount;
|
||||
self.amount = amount;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl Drop for Reservation {
|
||||
fn drop(&mut self) {
|
||||
self.budget.state().used[self.resource as usize] -= self.amount;
|
||||
}
|
||||
}
|
||||
|
||||
/// Scheduler storage owns its capacity charge, including unused capacity.
|
||||
/// During growth, the old allocation is also covered by a temporary reservation.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct BudgetQueue<T> {
|
||||
data: VecDeque<T>,
|
||||
charge: Reservation,
|
||||
}
|
||||
impl<T> BudgetQueue<T> {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn capacity_bytes(&self) -> u64 {
|
||||
self.charge.amount
|
||||
}
|
||||
|
||||
pub fn new(budget: &RuntimeBudget) -> Self {
|
||||
Self { data: VecDeque::new(), charge: budget.empty_charge(RuntimeResource::AuxiliaryBytes) }
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.data.is_empty()
|
||||
}
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
self.data.pop_front()
|
||||
}
|
||||
pub fn reserve(&mut self, additional: usize) -> Result<(), ResourceFailure> {
|
||||
let b = &self.charge.budget;
|
||||
let q = RuntimeResource::AuxiliaryBytes;
|
||||
let required = self
|
||||
.data
|
||||
.len()
|
||||
.checked_add(additional)
|
||||
.ok_or_else(|| b.failure(q, ResourceReason::SizeOverflow, u64::MAX))?;
|
||||
if required <= self.data.capacity() {
|
||||
return Ok(());
|
||||
}
|
||||
// Exact growth prevents a geometric allocation from exceeding the available quota.
|
||||
let bytes = b.bytes::<T>(required, q)?;
|
||||
let mut extra = b.reserve(q, bytes - self.charge.amount)?;
|
||||
let _temporary = b.reserve(RuntimeResource::TemporaryBytes, self.charge.amount)?;
|
||||
let mut candidate = VecDeque::new();
|
||||
candidate
|
||||
.try_reserve_exact(required)
|
||||
.map_err(|_| b.failure(q, ResourceReason::AllocationFailed, bytes))?;
|
||||
let actual = b.bytes::<T>(candidate.capacity(), q)?;
|
||||
// If the allocator reports excess capacity, reject before publishing the new buffer.
|
||||
extra.resize(actual - self.charge.amount)?;
|
||||
candidate.append(&mut self.data);
|
||||
self.data = candidate;
|
||||
self.charge.absorb(extra);
|
||||
Ok(())
|
||||
}
|
||||
pub fn push_back(&mut self, value: T) -> Result<(), ResourceFailure> {
|
||||
self.reserve(1)?;
|
||||
self.data.push_back(value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn insert(&mut self, index: usize, value: T) -> Result<(), ResourceFailure> {
|
||||
self.reserve(1)?;
|
||||
self.data.insert(index, value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn partition_point(&self, predicate: impl FnMut(&T) -> bool) -> usize {
|
||||
self.data.partition_point(predicate)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use RuntimeResource::*;
|
||||
|
||||
#[test]
|
||||
fn accepted_limits_are_fixed_and_can_only_be_lowered() {
|
||||
let limits = RuntimeLimits::default();
|
||||
for (resource, expected) in [
|
||||
(GuestBytes, 268_435_456),
|
||||
(HandleSlots, 1_048_576),
|
||||
(AuxiliaryBytes, 67_108_864),
|
||||
(TemporaryBytes, 67_108_864),
|
||||
(StringBytes, 67_108_864),
|
||||
(StringAllocations, 262_144),
|
||||
(IndividualStringBytes, 4_194_304),
|
||||
(CallDepth, 1_024),
|
||||
] {
|
||||
assert_eq!(limits.get(resource), expected);
|
||||
assert!(limits.clone().lowered(resource, expected + 1).is_err());
|
||||
assert_eq!(limits.clone().lowered(resource, 0).unwrap().get(resource), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_reservations_refund_on_failure_and_follow_their_owner() {
|
||||
let budget = RuntimeBudget::new(RuntimeLimits::default().lowered(GuestBytes, 10).unwrap());
|
||||
let charge = budget.reserve(GuestBytes, 7).unwrap();
|
||||
let shared = budget.clone();
|
||||
let failure = shared.reserve(GuestBytes, 4).unwrap_err();
|
||||
assert_eq!((failure.resource, failure.requested, failure.limit), (GuestBytes, 11, 10));
|
||||
assert_eq!(budget.used(GuestBytes), 7);
|
||||
let transferred = charge;
|
||||
assert_eq!(shared.used(GuestBytes), 7);
|
||||
let tail = shared.reserve(GuestBytes, 3).unwrap();
|
||||
assert_eq!(shared.used(GuestBytes), 10);
|
||||
drop(transferred);
|
||||
assert_eq!(shared.used(GuestBytes), 3);
|
||||
drop(tail);
|
||||
assert_eq!(shared.used(GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_capacity_is_retained_until_drop_and_transfer_does_not_charge_twice() {
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(AuxiliaryBytes, 16).unwrap());
|
||||
let mut queue = BudgetQueue::<u64>::new(&budget);
|
||||
queue.push_back(11).unwrap();
|
||||
queue.push_back(22).unwrap();
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 16);
|
||||
assert!(queue.push_back(33).is_err());
|
||||
assert_eq!(queue.pop_front(), Some(11));
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 16);
|
||||
queue.push_back(44).unwrap(); // reuse retained capacity
|
||||
let mut moved = queue;
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 16);
|
||||
assert_eq!(moved.pop_front(), Some(22));
|
||||
assert_eq!(moved.pop_front(), Some(44));
|
||||
drop(moved);
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 0);
|
||||
assert_eq!(budget.used(TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn temporary_growth_refusal_keeps_old_buffer_and_refunds_provisional_charge() {
|
||||
let limits = RuntimeLimits::default()
|
||||
.lowered(AuxiliaryBytes, 16)
|
||||
.unwrap()
|
||||
.lowered(TemporaryBytes, 0)
|
||||
.unwrap();
|
||||
let budget = RuntimeBudget::new(limits);
|
||||
let mut queue = BudgetQueue::<u64>::new(&budget);
|
||||
queue.push_back(11).unwrap();
|
||||
let error = queue.push_back(22).unwrap_err();
|
||||
assert_eq!((error.resource, error.requested), (TemporaryBytes, 8));
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 8);
|
||||
assert_eq!(queue.pop_front(), Some(11));
|
||||
assert_eq!(queue.pop_front(), None);
|
||||
assert_eq!(budget.used(TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_and_length_overflow_fail_before_growth() {
|
||||
let budget = RuntimeBudget::default();
|
||||
assert_eq!(
|
||||
budget.bytes::<u64>(usize::MAX, AuxiliaryBytes).unwrap_err().reason,
|
||||
ResourceReason::SizeOverflow
|
||||
);
|
||||
let mut queue = BudgetQueue::<u64>::new(&budget);
|
||||
queue.push_back(11).unwrap();
|
||||
assert_eq!(queue.reserve(usize::MAX).unwrap_err().reason, ResourceReason::SizeOverflow);
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 8);
|
||||
assert_eq!(queue.pop_front(), Some(11));
|
||||
}
|
||||
}
|
||||
|
||||
/// Contiguous execution storage. The capacity reservation travels with the buffer
|
||||
/// when a coroutine suspends/resumes, and is refunded only when storage is dropped.
|
||||
#[derive(Debug)]
|
||||
pub struct BudgetVec<T> {
|
||||
data: Vec<T>,
|
||||
charge: Reservation,
|
||||
}
|
||||
impl<T> BudgetVec<T> {
|
||||
pub fn new(budget: &RuntimeBudget, resource: RuntimeResource) -> Self {
|
||||
Self { data: Vec::new(), charge: budget.empty_charge(resource) }
|
||||
}
|
||||
pub(crate) fn prepare_growth(
|
||||
&self,
|
||||
additional: usize,
|
||||
) -> Result<Option<VecGrowth<T>>, ResourceFailure> {
|
||||
let b = &self.charge.budget;
|
||||
let q = self.charge.resource;
|
||||
let required = self
|
||||
.data
|
||||
.len()
|
||||
.checked_add(additional)
|
||||
.ok_or_else(|| b.failure(q, ResourceReason::SizeOverflow, u64::MAX))?;
|
||||
if required <= self.data.capacity() {
|
||||
return Ok(None);
|
||||
}
|
||||
let bytes = b.bytes::<T>(required, q)?;
|
||||
let mut extra = b.reserve(q, bytes - self.charge.amount)?;
|
||||
let temporary = b.reserve(RuntimeResource::TemporaryBytes, self.charge.amount)?;
|
||||
let mut candidate = Vec::new();
|
||||
candidate
|
||||
.try_reserve_exact(required)
|
||||
.map_err(|_| b.failure(q, ResourceReason::AllocationFailed, bytes))?;
|
||||
extra.resize(b.bytes::<T>(candidate.capacity(), q)? - self.charge.amount)?;
|
||||
Ok(Some(VecGrowth {
|
||||
candidate,
|
||||
extra,
|
||||
_temporary: temporary,
|
||||
old_capacity: self.data.capacity(),
|
||||
}))
|
||||
}
|
||||
pub(crate) fn commit_growth(&mut self, growth: Option<VecGrowth<T>>) {
|
||||
if let Some(mut growth) = growth {
|
||||
assert_eq!(self.data.capacity(), growth.old_capacity);
|
||||
growth.candidate.append(&mut self.data);
|
||||
self.data = growth.candidate;
|
||||
self.charge.absorb(growth.extra);
|
||||
}
|
||||
}
|
||||
pub fn reserve(&mut self, additional: usize) -> Result<(), ResourceFailure> {
|
||||
let growth = self.prepare_growth(additional)?;
|
||||
self.commit_growth(growth);
|
||||
Ok(())
|
||||
}
|
||||
pub(crate) fn accounted_bytes(&self) -> u64 {
|
||||
self.charge.amount
|
||||
}
|
||||
pub(crate) fn from_slice(
|
||||
budget: &RuntimeBudget,
|
||||
resource: RuntimeResource,
|
||||
values: &[T],
|
||||
) -> Result<Self, ResourceFailure>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
let mut result = Self::new(budget, resource);
|
||||
result.reserve(values.len())?;
|
||||
result.data.extend_from_slice(values);
|
||||
Ok(result)
|
||||
}
|
||||
pub fn push(&mut self, value: T) -> Result<(), ResourceFailure> {
|
||||
self.reserve(1)?;
|
||||
self.data.push(value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn resize(&mut self, len: usize, value: T) -> Result<(), ResourceFailure>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
self.reserve(len.saturating_sub(self.data.len()))?;
|
||||
self.data.resize(len, value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.data.pop()
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.data.clear();
|
||||
}
|
||||
pub fn truncate(&mut self, len: usize) {
|
||||
self.data.truncate(len);
|
||||
}
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.data.capacity()
|
||||
}
|
||||
pub fn as_slice(&self) -> &[T] {
|
||||
&self.data
|
||||
}
|
||||
pub fn drain(&mut self, range: impl std::ops::RangeBounds<usize>) -> std::vec::Drain<'_, T> {
|
||||
self.data.drain(range)
|
||||
}
|
||||
pub fn take(&mut self) -> Self {
|
||||
let empty = Self::new(&self.charge.budget, self.charge.resource);
|
||||
std::mem::replace(self, empty)
|
||||
}
|
||||
/// Only used with the bounded native result writer after reserving its result slots.
|
||||
pub(crate) fn native_result_buffer(&mut self) -> &mut Vec<T> {
|
||||
&mut self.data
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub(crate) fn fixture(data: Vec<T>) -> Self {
|
||||
let budget = RuntimeBudget::default();
|
||||
let bytes = budget.bytes::<T>(data.capacity(), RuntimeResource::AuxiliaryBytes).unwrap();
|
||||
let charge = budget.reserve(RuntimeResource::AuxiliaryBytes, bytes).unwrap();
|
||||
Self { data, charge }
|
||||
}
|
||||
}
|
||||
impl<T> std::ops::Deref for BudgetVec<T> {
|
||||
type Target = [T];
|
||||
fn deref(&self) -> &[T] {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
impl<T> std::ops::DerefMut for BudgetVec<T> {
|
||||
fn deref_mut(&mut self) -> &mut [T] {
|
||||
&mut self.data
|
||||
}
|
||||
}
|
||||
impl<'a, T> IntoIterator for &'a BudgetVec<T> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = std::slice::Iter<'a, T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.data.iter()
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
impl<T: PartialEq> PartialEq<Vec<T>> for BudgetVec<T> {
|
||||
fn eq(&self, other: &Vec<T>) -> bool {
|
||||
&self.data == other
|
||||
}
|
||||
}
|
||||
|
||||
/// Unpublished growth can be dropped to refund every provisional reservation.
|
||||
pub(crate) struct VecGrowth<T> {
|
||||
candidate: Vec<T>,
|
||||
extra: Reservation,
|
||||
_temporary: Reservation,
|
||||
old_capacity: usize,
|
||||
}
|
||||
@@ -7,9 +7,13 @@ pub trait RootVisitor {
|
||||
}
|
||||
|
||||
/// Helper: if `val` is a `Value::HeapRef`, call the visitor.
|
||||
pub(crate) fn value_heap_ref(value: &Value) -> Option<HeapRef> {
|
||||
if let Value::HeapRef(r) = value { Some(*r) } else { None }
|
||||
}
|
||||
|
||||
pub fn visit_value_for_roots<V: RootVisitor + ?Sized>(val: &Value, visitor: &mut V) {
|
||||
if let Value::HeapRef(r) = val {
|
||||
visitor.visit_heap_ref(*r);
|
||||
if let Some(r) = value_heap_ref(val) {
|
||||
visitor.visit_heap_ref(r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +35,11 @@ mod tests {
|
||||
fn visits_heapref_on_operand_stack() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
// Place a HeapRef on the operand stack
|
||||
vm.push_operand_for_test(Value::HeapRef(HeapRef(123)));
|
||||
vm.push_operand_for_test(Value::HeapRef(HeapRef { index: 123, generation: 1 }));
|
||||
|
||||
let mut v = CollectVisitor { seen: vec![] };
|
||||
vm.visit_roots(&mut v);
|
||||
|
||||
assert_eq!(v.seen, vec![HeapRef(123)]);
|
||||
assert_eq!(v.seen, vec![HeapRef { index: 123, generation: 1 }]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use crate::resource_budget::{BudgetQueue, ResourceFailure, RuntimeBudget};
|
||||
|
||||
use prometeu_bytecode::HeapRef;
|
||||
|
||||
@@ -8,12 +8,12 @@ use prometeu_bytecode::HeapRef;
|
||||
/// - FIFO for ready coroutines.
|
||||
/// - Sleeping coroutines are ordered by `wake_tick` and moved to ready when `wake_tick <= current_tick`.
|
||||
/// - No randomness, no preemption, no context switching here.
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct Scheduler {
|
||||
/// Queue of runnable coroutines (FIFO)
|
||||
ready_queue: VecDeque<HeapRef>,
|
||||
ready_queue: BudgetQueue<HeapRef>,
|
||||
/// Sleeping list kept sorted by (wake_tick, insertion_order)
|
||||
sleeping: Vec<SleepEntry>,
|
||||
sleeping: BudgetQueue<SleepEntry>,
|
||||
/// Currently selected coroutine (purely informational here)
|
||||
current: Option<HeapRef>,
|
||||
/// Monotonic counter to preserve deterministic order for same wake_tick
|
||||
@@ -28,15 +28,34 @@ struct SleepEntry {
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn capacity_bytes(&self) -> u64 {
|
||||
self.ready_queue.capacity_bytes() + self.sleeping.capacity_bytes()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
Self::with_budget(&RuntimeBudget::default())
|
||||
}
|
||||
|
||||
pub fn with_budget(budget: &RuntimeBudget) -> Self {
|
||||
Self {
|
||||
ready_queue: BudgetQueue::new(budget),
|
||||
sleeping: BudgetQueue::new(budget),
|
||||
current: None,
|
||||
next_seq: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reserve_ready(&mut self) -> Result<(), ResourceFailure> {
|
||||
self.ready_queue.reserve(1)
|
||||
}
|
||||
|
||||
// ---------- Ready queue operations ----------
|
||||
|
||||
/// Enqueue a coroutine to the ready FIFO.
|
||||
pub fn enqueue_ready(&mut self, coro: HeapRef) {
|
||||
self.ready_queue.push_back(coro);
|
||||
pub fn enqueue_ready(&mut self, coro: HeapRef) -> Result<(), ResourceFailure> {
|
||||
self.ready_queue.push_back(coro)
|
||||
}
|
||||
|
||||
/// Dequeue next ready coroutine (front of FIFO).
|
||||
@@ -70,36 +89,25 @@ impl Scheduler {
|
||||
/// Put a coroutine to sleep until `wake_tick`.
|
||||
/// A coroutine is woken when `current_tick >= wake_tick`.
|
||||
/// The sleeping list is kept stably ordered to guarantee determinism.
|
||||
pub fn sleep_until(&mut self, coro: HeapRef, wake_tick: u64) {
|
||||
pub fn sleep_until(&mut self, coro: HeapRef, wake_tick: u64) -> Result<(), ResourceFailure> {
|
||||
let entry = SleepEntry { wake_tick, seq: self.next_seq, coro };
|
||||
let idx =
|
||||
self.sleeping.partition_point(|e| (e.wake_tick, e.seq) <= (entry.wake_tick, entry.seq));
|
||||
self.sleeping.insert(idx, entry)?;
|
||||
self.next_seq = self.next_seq.wrapping_add(1);
|
||||
|
||||
// Binary search insertion point by wake_tick, then by seq to keep total order deterministic
|
||||
let idx = match self.sleeping.binary_search_by(|e| {
|
||||
if e.wake_tick == entry.wake_tick {
|
||||
e.seq.cmp(&entry.seq)
|
||||
} else {
|
||||
e.wake_tick.cmp(&entry.wake_tick)
|
||||
}
|
||||
}) {
|
||||
Ok(i) => i, // equal element position; insert after to preserve FIFO among equals
|
||||
Err(i) => i,
|
||||
};
|
||||
self.sleeping.insert(idx, entry);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Move all sleeping coroutines with `wake_tick <= current_tick` to ready queue (FIFO by wake order).
|
||||
pub fn wake_ready(&mut self, current_tick: u64) {
|
||||
// Find split point where wake_tick > current_tick
|
||||
/// Reserve the destination before removing any sleeping entries. A failed
|
||||
/// transfer preserves both queues and never loses a runnable coroutine.
|
||||
pub fn wake_ready(&mut self, current_tick: u64) -> Result<(), ResourceFailure> {
|
||||
let split = self.sleeping.partition_point(|e| e.wake_tick <= current_tick);
|
||||
if split == 0 {
|
||||
return;
|
||||
}
|
||||
let mut ready_slice: Vec<SleepEntry> = self.sleeping.drain(0..split).collect();
|
||||
// Already in order by (wake_tick, seq); push in that order to preserve determinism
|
||||
for e in ready_slice.drain(..) {
|
||||
self.ready_queue.push_back(e.coro);
|
||||
self.ready_queue.reserve(split)?;
|
||||
for _ in 0..split {
|
||||
let entry = self.sleeping.pop_front().expect("validated sleeping prefix");
|
||||
self.ready_queue.push_back(entry.coro)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns true if there are any sleeping coroutines.
|
||||
@@ -113,15 +121,15 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hr(id: u32) -> HeapRef {
|
||||
HeapRef(id)
|
||||
HeapRef { index: id, generation: 1 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fifo_ready_queue_is_deterministic() {
|
||||
let mut s = Scheduler::new();
|
||||
s.enqueue_ready(hr(1));
|
||||
s.enqueue_ready(hr(2));
|
||||
s.enqueue_ready(hr(3));
|
||||
s.enqueue_ready(hr(1)).unwrap();
|
||||
s.enqueue_ready(hr(2)).unwrap();
|
||||
s.enqueue_ready(hr(3)).unwrap();
|
||||
|
||||
assert_eq!(s.ready_len(), 3);
|
||||
assert_eq!(s.dequeue_next(), Some(hr(1)));
|
||||
@@ -133,23 +141,44 @@ mod tests {
|
||||
#[test]
|
||||
fn sleeping_wake_is_stable_and_fifo_by_insertion() {
|
||||
let mut s = Scheduler::new();
|
||||
s.sleep_until(hr(10), 5); // first with wake at 5
|
||||
s.sleep_until(hr(11), 5); // second with same wake
|
||||
s.sleep_until(hr(12), 6); // later wake
|
||||
s.sleep_until(hr(10), 5).unwrap(); // first with wake at 5
|
||||
s.sleep_until(hr(11), 5).unwrap(); // second with same wake
|
||||
s.sleep_until(hr(12), 6).unwrap(); // later wake
|
||||
|
||||
// Before tick 5: nothing wakes
|
||||
s.wake_ready(4);
|
||||
s.wake_ready(4).unwrap();
|
||||
assert!(s.is_ready_empty());
|
||||
|
||||
// At tick 5: first two wake in insertion order
|
||||
s.wake_ready(5);
|
||||
s.wake_ready(5).unwrap();
|
||||
assert_eq!(s.dequeue_next(), Some(hr(10)));
|
||||
assert_eq!(s.dequeue_next(), Some(hr(11)));
|
||||
assert!(s.is_ready_empty());
|
||||
|
||||
// At tick 6: last one wakes
|
||||
s.wake_ready(6);
|
||||
s.wake_ready(6).unwrap();
|
||||
assert_eq!(s.dequeue_next(), Some(hr(12)));
|
||||
assert!(s.is_ready_empty());
|
||||
}
|
||||
#[test]
|
||||
fn wake_refusal_preserves_sleepers_and_fifo_order() {
|
||||
use crate::resource_budget::{RuntimeLimits, RuntimeResource};
|
||||
let budget = RuntimeBudget::new(
|
||||
RuntimeLimits::default()
|
||||
.lowered(RuntimeResource::AuxiliaryBytes, (2 * size_of::<SleepEntry>()) as u64)
|
||||
.unwrap(),
|
||||
);
|
||||
let mut s = Scheduler::with_budget(&budget);
|
||||
s.sleep_until(hr(1), 4).unwrap();
|
||||
s.sleep_until(hr(2), 4).unwrap();
|
||||
let charged = budget.used(RuntimeResource::AuxiliaryBytes);
|
||||
assert!(s.wake_ready(4).is_err());
|
||||
assert_eq!(budget.used(RuntimeResource::AuxiliaryBytes), charged);
|
||||
assert_eq!(budget.used(RuntimeResource::TemporaryBytes), 0);
|
||||
assert!(s.is_ready_empty());
|
||||
assert_eq!(s.sleeping.pop_front().unwrap().coro, hr(1));
|
||||
assert_eq!(s.sleeping.pop_front().unwrap().coro, hr(2));
|
||||
drop(s);
|
||||
assert_eq!(budget.used(RuntimeResource::AuxiliaryBytes), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
//! Compact native field/element storage backed by resolved Registry layouts.
|
||||
//! No collection opcodes: consumers use indices and the shared value contract.
|
||||
mod allocation;
|
||||
use crate::resource_budget::{ResourceReason, RuntimeBudget, RuntimeResource as Q};
|
||||
use crate::value_contract::{ValueError, ValueGroup, u32_carrier};
|
||||
use allocation::Allocation;
|
||||
use prometeu_bytecode::registry::{
|
||||
BuiltinId as B, DynamicSlot, Ownership, Registry, StringSlot, TypeId, TypeKind,
|
||||
};
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct TraitSlot {
|
||||
receiver: HeapRef,
|
||||
implementation: u32,
|
||||
padding: u32,
|
||||
}
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct PixelSlot {
|
||||
x: i64,
|
||||
y: i64,
|
||||
color: u32,
|
||||
padding: u32,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TypedStorage {
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
// None for a Struct instance, Some(count) for an Array instance.
|
||||
count: Option<usize>,
|
||||
initialized: usize,
|
||||
data: Allocation,
|
||||
}
|
||||
impl TypedStorage {
|
||||
pub fn new_struct(
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ValueError> {
|
||||
let layout = registry
|
||||
.type_layout(type_id)
|
||||
.and_then(|l| l.instance.as_ref())
|
||||
.ok_or(ValueError::Type)?;
|
||||
let data = Allocation::new(layout.size, layout.alignment, budget)?;
|
||||
let mut storage = Self { registry, type_id, count: None, initialized: 0, data };
|
||||
storage.initialize();
|
||||
Ok(storage)
|
||||
}
|
||||
pub fn new_array(
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
count: u32,
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ValueError> {
|
||||
let layout = registry.type_layout(type_id).and_then(|l| l.array).ok_or(ValueError::Type)?;
|
||||
let size = layout
|
||||
.stride
|
||||
.checked_mul(count as usize)
|
||||
.ok_or_else(|| budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, u64::MAX))?;
|
||||
let data = Allocation::new(size, layout.element_alignment, budget)?;
|
||||
let mut storage =
|
||||
Self { registry, type_id, count: Some(count as usize), initialized: 0, data };
|
||||
storage.initialize();
|
||||
Ok(storage)
|
||||
}
|
||||
pub fn registry(&self) -> &Registry {
|
||||
&self.registry
|
||||
}
|
||||
pub fn type_id(&self) -> TypeId {
|
||||
self.type_id
|
||||
}
|
||||
pub fn bytes(&self) -> u64 {
|
||||
self.data.bytes()
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.count.unwrap_or_else(|| {
|
||||
self.registry.type_layout(self.type_id).unwrap().instance.as_ref().unwrap().fields.len()
|
||||
})
|
||||
}
|
||||
fn slot(&self, index: usize) -> Result<(TypeId, usize), ValueError> {
|
||||
if index >= self.len() {
|
||||
return Err(ValueError::Bounds);
|
||||
}
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
if let Some(array) = layout.array {
|
||||
Ok((array.element_type_id, array.stride * index))
|
||||
} else {
|
||||
let field = layout.instance.as_ref().unwrap().fields[index];
|
||||
Ok((field.type_id, field.offset))
|
||||
}
|
||||
}
|
||||
pub fn slot_type(&self, index: usize) -> Result<TypeId, ValueError> {
|
||||
self.slot(index).map(|s| s.0)
|
||||
}
|
||||
|
||||
fn initialize(&mut self) {
|
||||
for index in 0..self.len() {
|
||||
let (ty, offset) = self.slot(index).unwrap();
|
||||
// SAFETY: fresh resolved nonoverlapping field/element, no publication
|
||||
// until every native value is constructed. Count advances only after init.
|
||||
unsafe {
|
||||
match self.registry.declarations().types[ty.0 as usize].kind {
|
||||
TypeKind::Struct(_) | TypeKind::Array(_) => {
|
||||
self.data.init(offset, HeapRef::NULL)
|
||||
}
|
||||
TypeKind::Trait(_) => self.data.init(
|
||||
offset,
|
||||
TraitSlot { receiver: HeapRef::NULL, implementation: 0, padding: 0 },
|
||||
),
|
||||
TypeKind::Builtin(id) => match id {
|
||||
B::INT32 => self.data.init(offset, 0_i32),
|
||||
B::INT64 => self.data.init(offset, 0_i64),
|
||||
B::FLOAT => self.data.init(offset, 0_f64),
|
||||
B::BOOL => self.data.init(offset, 0_u8),
|
||||
B::COLOR => self.data.init(offset, 0_u32),
|
||||
B::VEC2 => self.data.init(offset, [0_f64; 2]),
|
||||
B::PIXEL => {
|
||||
self.data.init(offset, PixelSlot { x: 0, y: 0, color: 0, padding: 0 })
|
||||
}
|
||||
B::STRING => self.data.init::<StringSlot>(offset, None),
|
||||
B::DYNAMIC => self.data.init(offset, DynamicSlot::default()),
|
||||
_ => unreachable!("resolved builtin"),
|
||||
},
|
||||
}
|
||||
}
|
||||
self.initialized += 1;
|
||||
}
|
||||
}
|
||||
pub fn read(&self, index: usize) -> Result<ValueGroup, ValueError> {
|
||||
let (ty, offset) = self.slot(index)?;
|
||||
// SAFETY: private backing storage was initialized according to this exact
|
||||
// immutable Registry. Only matching typed writes can change its contents.
|
||||
let result = unsafe {
|
||||
match self.registry.declarations().types[ty.0 as usize].kind {
|
||||
TypeKind::Struct(_) | TypeKind::Array(_) => {
|
||||
let r = *self.data.get::<HeapRef>(offset);
|
||||
ValueGroup::new(&[if r.is_null() { Value::Null } else { Value::HeapRef(r) }])
|
||||
}
|
||||
TypeKind::Trait(_) => {
|
||||
let t = self.data.get::<TraitSlot>(offset);
|
||||
if t.receiver.is_null() {
|
||||
if t.implementation != 0 {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
ValueGroup::new(&[Value::Null, Value::Null])
|
||||
} else {
|
||||
ValueGroup::new(&[
|
||||
Value::HeapRef(t.receiver),
|
||||
Value::Int64(t.implementation.into()),
|
||||
])
|
||||
}
|
||||
}
|
||||
TypeKind::Builtin(id) => match id {
|
||||
B::INT32 => ValueGroup::new(&[Value::Int32(*self.data.get(offset))]),
|
||||
B::INT64 => ValueGroup::new(&[Value::Int64(*self.data.get(offset))]),
|
||||
B::FLOAT => ValueGroup::new(&[Value::Float(*self.data.get(offset))]),
|
||||
B::BOOL => match *self.data.get::<u8>(offset) {
|
||||
0 => ValueGroup::new(&[Value::Boolean(false)]),
|
||||
1 => ValueGroup::new(&[Value::Boolean(true)]),
|
||||
_ => return Err(ValueError::Type),
|
||||
},
|
||||
B::COLOR => {
|
||||
ValueGroup::new(&[Value::Int64((*self.data.get::<u32>(offset)).into())])
|
||||
}
|
||||
B::VEC2 => {
|
||||
let [x, y] = *self.data.get::<[f64; 2]>(offset);
|
||||
ValueGroup::new(&[Value::Float(x), Value::Float(y)])
|
||||
}
|
||||
B::PIXEL => {
|
||||
let p = self.data.get::<PixelSlot>(offset);
|
||||
ValueGroup::new(&[
|
||||
Value::Int64(p.x),
|
||||
Value::Int64(p.y),
|
||||
Value::Int64(p.color.into()),
|
||||
])
|
||||
}
|
||||
B::STRING => ValueGroup::new(&[self
|
||||
.data
|
||||
.get::<StringSlot>(offset)
|
||||
.as_ref()
|
||||
.map_or(Value::Null, |s| Value::String(s.clone()))]),
|
||||
B::DYNAMIC => {
|
||||
let d = self.data.get::<DynamicSlot>(offset);
|
||||
ValueGroup::new(&[
|
||||
d.type_id.map_or(Value::Null, |t| Value::Int64(t.0.into())),
|
||||
d.payload.clone(),
|
||||
])
|
||||
}
|
||||
_ => unreachable!("resolved builtin"),
|
||||
},
|
||||
}
|
||||
};
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Heap validates the complete group and provenance before calling this.
|
||||
/// All matching checks/conversions happen before replacement, so an error
|
||||
/// preserves both the old value and its native owners.
|
||||
pub fn write_validated(&mut self, index: usize, values: &[Value]) -> Result<(), ValueError> {
|
||||
let (ty, offset) = self.slot(index)?;
|
||||
// SAFETY: each arm uses the same resident type as initialize/read/drop;
|
||||
// &mut self gives exclusive access. Native assignment drops exactly once.
|
||||
unsafe {
|
||||
match (&self.registry.declarations().types[ty.0 as usize].kind, values) {
|
||||
(TypeKind::Struct(_) | TypeKind::Array(_), [Value::Null]) => {
|
||||
self.data.replace(offset, HeapRef::NULL)
|
||||
}
|
||||
(TypeKind::Struct(_) | TypeKind::Array(_), [Value::HeapRef(r)]) => {
|
||||
self.data.replace(offset, *r)
|
||||
}
|
||||
(TypeKind::Trait(_), [Value::Null, Value::Null]) => self.data.replace(
|
||||
offset,
|
||||
TraitSlot { receiver: HeapRef::NULL, implementation: 0, padding: 0 },
|
||||
),
|
||||
(TypeKind::Trait(_), [Value::HeapRef(r), id]) => self.data.replace(
|
||||
offset,
|
||||
TraitSlot { receiver: *r, implementation: u32_carrier(id)?, padding: 0 },
|
||||
),
|
||||
(TypeKind::Builtin(B::INT32), [Value::Int32(v)]) => self.data.replace(offset, *v),
|
||||
(TypeKind::Builtin(B::INT64), [Value::Int64(v)]) => self.data.replace(offset, *v),
|
||||
(TypeKind::Builtin(B::FLOAT), [Value::Float(v)]) => self.data.replace(offset, *v),
|
||||
(TypeKind::Builtin(B::BOOL), [Value::Boolean(v)]) => {
|
||||
self.data.replace(offset, u8::from(*v))
|
||||
}
|
||||
(TypeKind::Builtin(B::COLOR), [v]) => self.data.replace(offset, u32_carrier(v)?),
|
||||
(TypeKind::Builtin(B::VEC2), [Value::Float(x), Value::Float(y)]) => {
|
||||
self.data.replace(offset, [*x, *y])
|
||||
}
|
||||
(TypeKind::Builtin(B::PIXEL), [Value::Int64(x), Value::Int64(y), color]) => {
|
||||
self.data.replace(
|
||||
offset,
|
||||
PixelSlot { x: *x, y: *y, color: u32_carrier(color)?, padding: 0 },
|
||||
)
|
||||
}
|
||||
(TypeKind::Builtin(B::STRING), [Value::Null]) => {
|
||||
self.data.replace::<StringSlot>(offset, None)
|
||||
}
|
||||
(TypeKind::Builtin(B::STRING), [Value::String(s)]) => {
|
||||
self.data.replace::<StringSlot>(offset, Some(s.clone()))
|
||||
}
|
||||
(TypeKind::Builtin(B::DYNAMIC), [tag, payload]) => {
|
||||
let type_id = if matches!(tag, Value::Null) {
|
||||
None
|
||||
} else {
|
||||
Some(TypeId(u32_carrier(tag)?))
|
||||
};
|
||||
self.data.replace(offset, DynamicSlot { type_id, payload: payload.clone() });
|
||||
}
|
||||
_ => return Err(ValueError::Type),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Iterates only resolved ownership-map entries; padding/scalar fields are
|
||||
/// never interpreted as references. No temporary root buffer is allocated.
|
||||
pub fn root_count(&self) -> usize {
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
if let Some(array) = layout.array {
|
||||
let owner = self.registry.type_layout(array.element_type_id).unwrap().storage.ownership;
|
||||
if matches!(
|
||||
owner,
|
||||
Ownership::GuestReference | Ownership::TraitReceiver | Ownership::Dynamic
|
||||
) {
|
||||
self.len()
|
||||
} else {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
layout.instance.as_ref().unwrap().owned_fields.len()
|
||||
}
|
||||
}
|
||||
pub fn root_at(&self, index: usize) -> Option<HeapRef> {
|
||||
assert!(index < self.root_count());
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
let (offset, ownership) = if let Some(array) = layout.array {
|
||||
(
|
||||
index * array.stride,
|
||||
self.registry.type_layout(array.element_type_id).unwrap().storage.ownership,
|
||||
)
|
||||
} else {
|
||||
let field = layout.instance.as_ref().unwrap().owned_fields[index];
|
||||
(field.offset, field.ownership)
|
||||
};
|
||||
// SAFETY: private caller indexes the resolved map within root_count();
|
||||
// resident native values were all constructed before heap publication.
|
||||
let reference = unsafe {
|
||||
match ownership {
|
||||
Ownership::GuestReference => Some(*self.data.get::<HeapRef>(offset)),
|
||||
Ownership::TraitReceiver => Some(self.data.get::<TraitSlot>(offset).receiver),
|
||||
Ownership::Dynamic => {
|
||||
crate::roots::value_heap_ref(&self.data.get::<DynamicSlot>(offset).payload)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
reference.filter(|r| !r.is_null())
|
||||
}
|
||||
}
|
||||
impl Drop for TypedStorage {
|
||||
fn drop(&mut self) {
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
if let Some(array) = layout.array {
|
||||
let ownership =
|
||||
self.registry.type_layout(array.element_type_id).unwrap().storage.ownership;
|
||||
if !matches!(ownership, Ownership::NativeString | Ownership::Dynamic) {
|
||||
return;
|
||||
}
|
||||
for index in 0..self.initialized {
|
||||
drop_native(&mut self.data, index * array.stride, ownership);
|
||||
}
|
||||
} else {
|
||||
let instance = layout.instance.as_ref().unwrap();
|
||||
// An interrupted constructor owns only the prefix before this offset.
|
||||
let end = instance.fields.get(self.initialized).map_or(instance.size, |f| f.offset);
|
||||
for field in &instance.owned_fields {
|
||||
if field.offset >= end {
|
||||
break;
|
||||
}
|
||||
drop_native(&mut self.data, field.offset, field.ownership);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn drop_native(data: &mut Allocation, offset: usize, ownership: Ownership) {
|
||||
// SAFETY: caller visits each initialized native ownership-map entry once.
|
||||
// Allocation::drop deallocates the backing memory only after this traversal.
|
||||
unsafe {
|
||||
match ownership {
|
||||
Ownership::NativeString => data.drop_at::<StringSlot>(offset),
|
||||
Ownership::Dynamic => data.drop_at::<DynamicSlot>(offset),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests;
|
||||
@@ -0,0 +1,87 @@
|
||||
//! Private aligned allocation. TypedStorage constructs/drops native owners;
|
||||
//! this owner only manages the backing allocation and its quota reservation.
|
||||
use crate::resource_budget::{
|
||||
Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
|
||||
};
|
||||
use std::alloc::{Layout, alloc_zeroed, dealloc};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct Allocation {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
charge: Reservation,
|
||||
}
|
||||
// The allocation is uniquely owned. Typed access is private to TypedStorage,
|
||||
// whose resident StringSlot/DynamicSlot/plain carriers are all Send + Sync.
|
||||
unsafe impl Send for Allocation {}
|
||||
unsafe impl Sync for Allocation {}
|
||||
impl Allocation {
|
||||
pub fn new(
|
||||
size: usize,
|
||||
alignment: usize,
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ResourceFailure> {
|
||||
let layout = Layout::from_size_align(size, alignment).map_err(|_| {
|
||||
budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, size as u64)
|
||||
})?;
|
||||
let charge = budget.reserve(Q::GuestBytes, size as u64)?;
|
||||
let ptr = if size == 0 {
|
||||
// Never dereferenced/deallocated; no element exists in an empty payload.
|
||||
NonNull::dangling()
|
||||
} else {
|
||||
// SAFETY: nonzero checked Layout; null is handled as a resource failure.
|
||||
NonNull::new(unsafe { alloc_zeroed(layout) }).ok_or_else(|| {
|
||||
budget.failure(Q::GuestBytes, ResourceReason::AllocationFailed, size as u64)
|
||||
})?
|
||||
};
|
||||
Ok(Self { ptr, layout, charge })
|
||||
}
|
||||
pub fn bytes(&self) -> u64 {
|
||||
self.charge.amount()
|
||||
}
|
||||
|
||||
fn at<T>(&self, offset: usize) -> *mut T {
|
||||
assert!(offset.checked_add(size_of::<T>()).is_some_and(|end| end <= self.layout.size()));
|
||||
assert!(self.layout.align() >= align_of::<T>());
|
||||
assert!(offset.is_multiple_of(align_of::<T>()));
|
||||
// SAFETY: checked bounds/alignment, nonzero accessed type lies in allocation.
|
||||
unsafe { self.ptr.as_ptr().add(offset).cast() }
|
||||
}
|
||||
/// Caller guarantees T is initialized and is the resident type at offset.
|
||||
pub unsafe fn get<T>(&self, offset: usize) -> &T {
|
||||
// SAFETY: caller's typed initialization invariant and checked address.
|
||||
unsafe { &*self.at::<T>(offset) }
|
||||
}
|
||||
/// Caller guarantees exclusive access and a valid initialized T at offset.
|
||||
pub unsafe fn replace<T>(&mut self, offset: usize, value: T) {
|
||||
// SAFETY: caller guarantees initialized matching type; assignment drops old owner.
|
||||
unsafe {
|
||||
*self.at::<T>(offset) = value;
|
||||
}
|
||||
}
|
||||
/// Caller guarantees the destination is uninitialized storage for exactly T.
|
||||
pub unsafe fn init<T>(&mut self, offset: usize, value: T) {
|
||||
// SAFETY: checked address, ownership is moved into previously unused storage.
|
||||
unsafe {
|
||||
self.at::<T>(offset).write(value);
|
||||
}
|
||||
}
|
||||
/// Caller guarantees an initialized T which will never be used again.
|
||||
pub unsafe fn drop_at<T>(&mut self, offset: usize) {
|
||||
// SAFETY: caller walks each initialized native slot exactly once.
|
||||
unsafe {
|
||||
std::ptr::drop_in_place(self.at::<T>(offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Drop for Allocation {
|
||||
fn drop(&mut self) {
|
||||
if self.layout.size() != 0 {
|
||||
// SAFETY: same pointer/Layout returned by alloc_zeroed, unique owner.
|
||||
unsafe {
|
||||
dealloc(self.ptr.as_ptr(), self.layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,528 @@
|
||||
use super::*;
|
||||
use crate::heap::{Heap, StoredObject};
|
||||
use crate::resource_budget::{ResourceReason, RuntimeLimits};
|
||||
use crate::value_contract::{self, ValueError as E};
|
||||
use prometeu_bytecode::load_limits::LoadBudget;
|
||||
use prometeu_bytecode::registry::{ImplementationDecl, NamedType, RegistryDeclarations, TypeDecl};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
const I32: TypeId = TypeId(0);
|
||||
const I64: TypeId = TypeId(1);
|
||||
const FLOAT: TypeId = TypeId(2);
|
||||
const BOOL: TypeId = TypeId(3);
|
||||
const COLOR: TypeId = TypeId(4);
|
||||
const VEC: TypeId = TypeId(5);
|
||||
const PIXEL: TypeId = TypeId(6);
|
||||
const STRING: TypeId = TypeId(7);
|
||||
const DYNAMIC: TypeId = TypeId(8);
|
||||
const ENEMY: TypeId = TypeId(9);
|
||||
const OTHER: TypeId = TypeId(10);
|
||||
const MIXED: TypeId = TypeId(11);
|
||||
const TRAIT: TypeId = TypeId(12);
|
||||
const INTS: TypeId = TypeId(13);
|
||||
const VECS: TypeId = TypeId(14);
|
||||
const REFS: TypeId = TypeId(15);
|
||||
const STRINGS: TypeId = TypeId(16);
|
||||
const TRAITS: TypeId = TypeId(17);
|
||||
const DYNAMICS: TypeId = TypeId(18);
|
||||
const BOOLS: TypeId = TypeId(19);
|
||||
const PIXELS: TypeId = TypeId(20);
|
||||
const FLOATS: TypeId = TypeId(21);
|
||||
const LONGS: TypeId = TypeId(22);
|
||||
const COLORS: TypeId = TypeId(23);
|
||||
const ARRAYS: TypeId = TypeId(24);
|
||||
|
||||
pub(crate) fn fixture_registry() -> Arc<Registry> {
|
||||
let mut types: Vec<_> =
|
||||
[B::INT32, B::INT64, B::FLOAT, B::BOOL, B::COLOR, B::VEC2, B::PIXEL, B::STRING, B::DYNAMIC]
|
||||
.into_iter()
|
||||
.map(TypeKind::Builtin)
|
||||
.collect();
|
||||
types.extend([
|
||||
TypeKind::Struct(vec![]),
|
||||
TypeKind::Struct(vec![]),
|
||||
TypeKind::Struct(
|
||||
[BOOL, I32, BOOL, STRING, DYNAMIC, TRAIT, ENEMY, VEC, PIXEL, INTS]
|
||||
.into_iter()
|
||||
.map(|type_id| NamedType { type_id, name: None })
|
||||
.collect(),
|
||||
),
|
||||
TypeKind::Trait(vec![]),
|
||||
]);
|
||||
types.extend(
|
||||
[I32, VEC, ENEMY, STRING, TRAIT, DYNAMIC, BOOL, PIXEL, FLOAT, I64, COLOR, INTS]
|
||||
.into_iter()
|
||||
.map(TypeKind::Array),
|
||||
);
|
||||
Arc::new(
|
||||
Registry::resolve(
|
||||
RegistryDeclarations {
|
||||
types: types.into_iter().map(|kind| TypeDecl { kind, name: None }).collect(),
|
||||
functions: vec![],
|
||||
implementations: vec![
|
||||
ImplementationDecl { owner: ENEMY, trait_id: TRAIT, methods: vec![] },
|
||||
ImplementationDecl { owner: OTHER, trait_id: TRAIT, methods: vec![] },
|
||||
],
|
||||
},
|
||||
0,
|
||||
&mut LoadBudget::new(Default::default()),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
fn values(heap: &Heap, registry: &Registry, r: HeapRef, i: usize) -> Vec<Value> {
|
||||
heap.load_typed(registry, r, i).unwrap().as_slice().to_vec()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_are_typed_compact_and_construct_no_referenced_objects() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
for array in
|
||||
[INTS, VECS, REFS, STRINGS, TRAITS, DYNAMICS, BOOLS, PIXELS, FLOATS, LONGS, COLORS, ARRAYS]
|
||||
{
|
||||
let before = budget.used(Q::GuestBytes);
|
||||
let r = heap.allocate_typed_array(registry.clone(), array, 3).unwrap();
|
||||
let layout = registry.type_layout(array).unwrap().array.unwrap();
|
||||
assert_eq!(
|
||||
budget.used(Q::GuestBytes) - before,
|
||||
(size_of::<StoredObject>() + 3 * layout.stride) as u64
|
||||
);
|
||||
let default =
|
||||
registry.type_layout(layout.element_type_id).unwrap().storage.default_values();
|
||||
for i in 0..3 {
|
||||
assert_eq!(values(&heap, ®istry, r, i), default);
|
||||
}
|
||||
}
|
||||
assert_eq!(heap.len(), 12); // No children, subarrays, boxes or wrappers.
|
||||
let mixed = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
let fields = ®istry.type_layout(MIXED).unwrap().instance.as_ref().unwrap().fields;
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
assert_eq!(
|
||||
values(&heap, ®istry, mixed, i),
|
||||
registry.type_layout(field.type_id).unwrap().storage.default_values()
|
||||
);
|
||||
}
|
||||
let empty = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
assert!(heap.is_valid(empty));
|
||||
assert_eq!(heap.load_typed(®istry, empty, 0).unwrap_err(), E::Bounds);
|
||||
assert_eq!(heap.used_bytes.load(Ordering::Relaxed) as u64, budget.used(Q::GuestBytes));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_scalar_roundtrips_preserve_float_bits_pixel_width_and_color_range() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
for (ty, input) in [
|
||||
(INTS, vec![Value::Int32(i32::MIN)]),
|
||||
(LONGS, vec![Value::Int64(i64::MAX)]),
|
||||
(BOOLS, vec![Value::Boolean(true)]),
|
||||
(COLORS, vec![Value::Int64(u32::MAX.into())]),
|
||||
(VECS, vec![Value::Float(-0.0), Value::Float(f64::INFINITY)]),
|
||||
(PIXELS, vec![Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(0x11223344)]),
|
||||
] {
|
||||
let r = heap.allocate_typed_array(registry.clone(), ty, 1).unwrap();
|
||||
heap.store_typed(®istry, r, 0, &input).unwrap();
|
||||
assert_eq!(values(&heap, ®istry, r, 0), input);
|
||||
}
|
||||
let r = heap.allocate_typed_array(registry.clone(), FLOATS, 1).unwrap();
|
||||
for bits in [0_u64, (-0.0_f64).to_bits(), 0x7ff8000000000123] {
|
||||
heap.store_typed(®istry, r, 0, &[Value::Float(f64::from_bits(bits))]).unwrap();
|
||||
let output = values(&heap, ®istry, r, 0);
|
||||
let Value::Float(v) = output[0] else { panic!("exact Float carrier") };
|
||||
assert_eq!(v.to_bits(), bits);
|
||||
}
|
||||
let r = heap.allocate_typed_array(registry.clone(), COLORS, 1).unwrap();
|
||||
for value in [
|
||||
Value::Int64(-1),
|
||||
Value::Int64(u32::MAX as i64 + 1),
|
||||
Value::Int32(0),
|
||||
Value::Float(0.0),
|
||||
Value::Null,
|
||||
] {
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &[value]).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(values(&heap, ®istry, r, 0), [Value::Int64(0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_reads_share_identity_and_failed_overwrite_keeps_the_old_owner() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let r = heap.allocate_typed_array(registry.clone(), STRINGS, 2).unwrap();
|
||||
let text: Arc<str> = Arc::from("hello");
|
||||
let input = [Value::String(text.clone())];
|
||||
heap.store_typed(®istry, r, 0, &input).unwrap();
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &[Value::Int64(1)]).unwrap_err(), E::Type);
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
let copy = heap.load_typed(®istry, r, 0).unwrap();
|
||||
let [Value::String(alias)] = copy.as_slice() else { panic!("String carrier") };
|
||||
assert!(Arc::ptr_eq(alias, &text));
|
||||
assert_eq!(Arc::strong_count(&text), 4);
|
||||
heap.store_typed(®istry, r, 0, &[Value::Null]).unwrap();
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
heap.store_typed(®istry, r, 1, &[Value::String(Arc::from(""))]).unwrap();
|
||||
assert!(
|
||||
matches!(values(&heap, ®istry, r, 1).as_slice(), [Value::String(s)] if s.is_empty())
|
||||
);
|
||||
drop(copy);
|
||||
drop(input);
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
heap.sweep();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_trait_pairs_accept_implementation_zero_and_preserve_receiver_aliases() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let enemy = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let r = heap.allocate_typed_array(registry.clone(), TRAITS, 2).unwrap();
|
||||
let pair = [Value::HeapRef(enemy), Value::Int64(0)];
|
||||
heap.store_typed(®istry, r, 0, &pair).unwrap();
|
||||
assert_eq!(values(&heap, ®istry, r, 0), pair);
|
||||
for bad in [
|
||||
vec![Value::HeapRef(enemy), Value::Int64(1)],
|
||||
vec![Value::HeapRef(enemy), Value::Null],
|
||||
vec![Value::Null, Value::Int64(0)],
|
||||
vec![Value::HeapRef(HeapRef::NULL), Value::Int64(0)],
|
||||
vec![Value::HeapRef(enemy), Value::Int32(0)],
|
||||
vec![Value::HeapRef(enemy), Value::Int64(-1)],
|
||||
vec![Value::HeapRef(enemy)],
|
||||
] {
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(values(&heap, ®istry, r, 0), pair);
|
||||
heap.mark_from_roots([r]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(enemy));
|
||||
heap.store_typed(®istry, r, 0, &[Value::Null, Value::Null]).unwrap();
|
||||
heap.mark_from_roots([r]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(!heap.is_valid(enemy));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_checks_semantic_ids_and_direct_or_typed_null_payloads() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let r = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
let text: Arc<str> = Arc::from("shared");
|
||||
for input in [
|
||||
vec![Value::Int64(I32.0.into()), Value::Int32(17)],
|
||||
vec![Value::Int64(COLOR.0.into()), Value::Int64(0x11223344)],
|
||||
vec![Value::Int64(STRING.0.into()), Value::String(text.clone())],
|
||||
vec![Value::Int64(STRING.0.into()), Value::Null],
|
||||
vec![Value::Int64(TRAIT.0.into()), Value::Null],
|
||||
vec![Value::Int64(ENEMY.0.into()), Value::Null],
|
||||
vec![Value::Null, Value::Null],
|
||||
] {
|
||||
heap.store_typed(®istry, r, 0, &input).unwrap();
|
||||
assert_eq!(values(&heap, ®istry, r, 0), input);
|
||||
}
|
||||
for bad in [
|
||||
vec![Value::Null, Value::Int32(0)],
|
||||
vec![Value::Int64(I32.0.into()), Value::Int64(0)],
|
||||
vec![Value::Int64(I32.0.into()), Value::Null],
|
||||
vec![Value::Int64(DYNAMIC.0.into()), Value::Null],
|
||||
vec![Value::Int64(999), Value::Null],
|
||||
vec![Value::Int64(-1), Value::Null],
|
||||
vec![Value::Int32(0), Value::Int32(0)],
|
||||
vec![Value::Int64(VEC.0.into()), Value::Float(0.0)],
|
||||
] {
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(heap.len(), 1); // No box for typed null, no direct-value boxing.
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compact_reference_arrays_require_exact_type_live_generation_and_registry() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let refs = heap.allocate_typed_array(registry.clone(), REFS, 1).unwrap();
|
||||
let enemy = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let other = heap.allocate_typed_struct(registry.clone(), OTHER).unwrap();
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(enemy)]).unwrap();
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(other)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
refs,
|
||||
0,
|
||||
&[Value::HeapRef(HeapRef { index: enemy.index, generation: enemy.generation + 1 })]
|
||||
)
|
||||
.unwrap_err(),
|
||||
E::InvalidHandle
|
||||
);
|
||||
heap.mark_from_roots([refs]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(enemy));
|
||||
assert!(!heap.is_valid(other));
|
||||
let next = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
assert_eq!(next.index, other.index);
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(other)]).unwrap_err(),
|
||||
E::InvalidHandle
|
||||
);
|
||||
let foreign = fixture_registry();
|
||||
assert_eq!(heap.load_typed(&foreign, refs, 0).unwrap_err(), E::Type);
|
||||
let foreign_enemy = heap.allocate_typed_struct(foreign, ENEMY).unwrap();
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(foreign_enemy)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(values(&heap, ®istry, refs, 0), [Value::HeapRef(enemy)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_struct_gc_uses_reference_maps_and_drops_native_dynamic_strings() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let holder = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
let child = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let dead = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let text: Arc<str> = Arc::from("owner");
|
||||
heap.store_typed(®istry, holder, 3, &[Value::String(text.clone())]).unwrap();
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
holder,
|
||||
4,
|
||||
&[Value::Int64(STRING.0.into()), Value::String(text.clone())],
|
||||
)
|
||||
.unwrap();
|
||||
heap.store_typed(®istry, holder, 6, &[Value::HeapRef(child)]).unwrap();
|
||||
// Scalar bits resembling a handle must never retain the unrelated object.
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
holder,
|
||||
8,
|
||||
&[
|
||||
Value::Int64(((dead.generation as u64) << 32 | dead.index as u64) as i64),
|
||||
Value::Int64(0),
|
||||
Value::Int64(0),
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(child));
|
||||
assert!(!heap.is_valid(dead));
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_reference_roots_and_array_references_preserve_identity() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let outer = heap.allocate_typed_array(registry.clone(), ARRAYS, 1).unwrap();
|
||||
let child = heap.allocate_typed_array(registry.clone(), INTS, 1).unwrap();
|
||||
let root = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
heap.store_typed(®istry, outer, 0, &[Value::HeapRef(child)]).unwrap();
|
||||
heap.store_typed(®istry, root, 0, &[Value::Int64(ARRAYS.0.into()), Value::HeapRef(outer)])
|
||||
.unwrap();
|
||||
heap.mark_from_roots([root]).unwrap();
|
||||
heap.sweep();
|
||||
assert_eq!(heap.len(), 3);
|
||||
heap.store_typed(®istry, child, 0, &[Value::Int32(42)]).unwrap();
|
||||
let [Value::HeapRef(alias)] = values(&heap, ®istry, outer, 0)[..] else {
|
||||
panic!("reference")
|
||||
};
|
||||
assert_eq!(values(&heap, ®istry, alias, 0), [Value::Int32(42)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_bounds_null_and_wrong_category_are_distinct() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let empty = heap.allocate_typed_array(registry.clone(), STRINGS, 0).unwrap();
|
||||
assert_eq!(heap.load_typed(®istry, empty, 0).unwrap_err(), E::Bounds);
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, empty, usize::MAX, &[Value::Null]).unwrap_err(),
|
||||
E::Bounds
|
||||
);
|
||||
assert_eq!(heap.load_typed(®istry, HeapRef::NULL, 0).unwrap_err(), E::NullReference);
|
||||
let closure = heap.alloc_closure(0, &[]).unwrap();
|
||||
assert_eq!(
|
||||
value_contract::validate(®istry, &heap, ENEMY, &[Value::HeapRef(closure)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert!(matches!(heap.allocate_typed_struct(registry.clone(), STRING), Err(E::Type)));
|
||||
assert!(matches!(heap.allocate_typed_array(registry, TRAIT, 1), Err(E::Type)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_slots_are_aligned_and_corrupt_compact_pairs_are_rejected() {
|
||||
let registry = fixture_registry();
|
||||
let mut mixed =
|
||||
TypedStorage::new_struct(registry.clone(), MIXED, &RuntimeBudget::default()).unwrap();
|
||||
let offset = mixed.slot(3).unwrap().1;
|
||||
// SAFETY: exact initialized StringSlot, as established by the immutable layout.
|
||||
let addr = unsafe { mixed.data.get::<StringSlot>(offset) } as *const _ as usize;
|
||||
assert_eq!(addr % align_of::<StringSlot>(), 0);
|
||||
let offset = mixed.slot(4).unwrap().1;
|
||||
let addr = unsafe { mixed.data.get::<DynamicSlot>(offset) } as *const _ as usize;
|
||||
assert_eq!(addr % align_of::<DynamicSlot>(), 0);
|
||||
let offset = mixed.slot(5).unwrap().1;
|
||||
unsafe {
|
||||
mixed
|
||||
.data
|
||||
.replace(offset, TraitSlot { receiver: HeapRef::NULL, implementation: 1, padding: 0 });
|
||||
}
|
||||
assert_eq!(mixed.read(5).unwrap_err(), E::Type);
|
||||
let offset = mixed.slot(0).unwrap().1;
|
||||
unsafe {
|
||||
mixed.data.replace(offset, 2_u8);
|
||||
}
|
||||
assert_eq!(mixed.read(0).unwrap_err(), E::Type);
|
||||
assert_eq!(size_of::<TraitSlot>(), 16);
|
||||
assert_eq!(size_of::<PixelSlot>(), 24);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unpublished_native_prefix_cleanup_drops_only_initialized_slots() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let text: Arc<str> = Arc::from("prefix");
|
||||
let layout = registry.type_layout(STRINGS).unwrap().array.unwrap();
|
||||
let data = Allocation::new(layout.stride * 3, layout.element_alignment, &budget).unwrap();
|
||||
let mut storage =
|
||||
TypedStorage { registry, type_id: STRINGS, count: Some(3), initialized: 0, data };
|
||||
// Simulate cancellation during initialization. Remaining native slots were
|
||||
// never constructed; cleanup must not interpret their zero backing bytes.
|
||||
unsafe {
|
||||
storage.data.init::<StringSlot>(0, Some(text.clone()));
|
||||
}
|
||||
storage.initialized = 1;
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
drop(storage);
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allocation_failure_after_native_initialization_refunds_every_domain() {
|
||||
let registry = fixture_registry();
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::AuxiliaryBytes, 0).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
assert!(
|
||||
matches!(heap.allocate_typed_struct(registry, MIXED), Err(E::Resource(e)) if e.resource == Q::AuxiliaryBytes)
|
||||
);
|
||||
assert_eq!(heap.len(), 0);
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] {
|
||||
assert_eq!(budget.used(q), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_array_fails_before_initialization_and_reused_storage_gets_defaults() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
assert!(
|
||||
matches!(heap.allocate_typed_array(registry.clone(), PIXELS, u32::MAX), Err(E::Resource(e)) if matches!(e.reason, ResourceReason::Limit | ResourceReason::SizeOverflow))
|
||||
);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
let first = heap.allocate_typed_array(registry.clone(), VECS, 1).unwrap();
|
||||
heap.store_typed(®istry, first, 0, &[Value::Float(1.0), Value::Float(2.0)]).unwrap();
|
||||
heap.sweep();
|
||||
let reused = heap.allocate_typed_array(registry.clone(), VECS, 1).unwrap();
|
||||
assert_eq!(first.index, reused.index);
|
||||
assert_ne!(first.generation, reused.generation);
|
||||
assert_eq!(values(&heap, ®istry, reused, 0), [Value::Float(0.0), Value::Float(0.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_multislot_writes_are_atomic_and_do_not_drop_native_owners() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let r = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
let text: Arc<str> = Arc::from("keep");
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
r,
|
||||
0,
|
||||
&[Value::Int64(STRING.0.into()), Value::String(text.clone())],
|
||||
)
|
||||
.unwrap();
|
||||
let dead = HeapRef { index: 999, generation: 1 };
|
||||
for (bad, error) in [
|
||||
(vec![Value::Int64(STRING.0.into())], E::Type),
|
||||
(vec![Value::Null, Value::String(text.clone())], E::Type),
|
||||
(vec![Value::Int64(ENEMY.0.into()), Value::HeapRef(dead)], E::InvalidHandle),
|
||||
(vec![Value::Int64(VEC.0.into()), Value::HeapRef(dead)], E::InvalidHandle),
|
||||
(vec![Value::Int64(VEC.0.into()), Value::HeapRef(r)], E::Type),
|
||||
(vec![Value::Int64(TRAIT.0.into()), Value::HeapRef(r)], E::Type),
|
||||
] {
|
||||
let before = Arc::strong_count(&text);
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &bad).unwrap_err(), error);
|
||||
assert_eq!(Arc::strong_count(&text), before);
|
||||
let output = heap.load_typed(®istry, r, 0).unwrap();
|
||||
let [Value::Int64(tag), Value::String(alias)] = output.as_slice() else {
|
||||
panic!("original group")
|
||||
};
|
||||
assert_eq!(*tag, STRING.0 as i64);
|
||||
assert!(Arc::ptr_eq(alias, &text));
|
||||
}
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_boundaries_never_use_cross_numeric_value_equality_or_partial_groups() {
|
||||
let registry = fixture_registry();
|
||||
let heap = Heap::new();
|
||||
for (ty, bad) in [
|
||||
(I32, vec![Value::Int64(0)]),
|
||||
(I64, vec![Value::Int32(0)]),
|
||||
(FLOAT, vec![Value::Int64(0)]),
|
||||
(BOOL, vec![Value::Int32(0)]),
|
||||
(VEC, vec![Value::Float(1.0)]),
|
||||
(VEC, vec![Value::Int64(1), Value::Float(1.0)]),
|
||||
(PIXEL, vec![Value::Int32(0), Value::Int64(0), Value::Int64(0)]),
|
||||
(PIXEL, vec![Value::Int64(0), Value::Int64(0), Value::Int64(u32::MAX as i64 + 1)]),
|
||||
(ENEMY, vec![Value::HeapRef(HeapRef::NULL)]),
|
||||
(TypeId(u32::MAX), vec![Value::Null]),
|
||||
] {
|
||||
assert_eq!(value_contract::validate(®istry, &heap, ty, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(
|
||||
value_contract::validate(
|
||||
®istry,
|
||||
&heap,
|
||||
ENEMY,
|
||||
&[Value::HeapRef(HeapRef { index: 1, generation: 0 })]
|
||||
)
|
||||
.unwrap_err(),
|
||||
E::InvalidHandle
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_dynamic_self_cycle_uses_one_mark_worklist_entry() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::new(
|
||||
RuntimeLimits::default().lowered(Q::TemporaryBytes, size_of::<HeapRef>() as u64).unwrap(),
|
||||
);
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let r = heap.allocate_typed_array(registry.clone(), DYNAMICS, 128).unwrap();
|
||||
for i in 0..128 {
|
||||
heap.store_typed(®istry, r, i, &[Value::Int64(DYNAMICS.0.into()), Value::HeapRef(r)])
|
||||
.unwrap();
|
||||
}
|
||||
heap.mark_from_roots(std::iter::repeat_n(r, 128)).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(r));
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
//! Exact Registry-aware boundary checks. No numeric coercion or object-graph walk.
|
||||
use crate::heap::Heap;
|
||||
use crate::resource_budget::ResourceFailure;
|
||||
use prometeu_bytecode::registry::{BuiltinId as B, Registry, TypeId, TypeKind};
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum ValueError {
|
||||
Type,
|
||||
InvalidHandle,
|
||||
NullReference,
|
||||
Bounds,
|
||||
Resource(ResourceFailure),
|
||||
}
|
||||
impl From<ResourceFailure> for ValueError {
|
||||
fn from(value: ResourceFailure) -> Self {
|
||||
Self::Resource(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// The accepted catalogue has at most three slots per stored value. This buffer
|
||||
/// clones native owners, but never allocates a temporary Vec for a read.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ValueGroup {
|
||||
values: [Value; 3],
|
||||
len: usize,
|
||||
}
|
||||
impl ValueGroup {
|
||||
pub fn new(values: &[Value]) -> Self {
|
||||
assert!(values.len() <= 3);
|
||||
let mut result =
|
||||
Self { values: [Value::Null, Value::Null, Value::Null], len: values.len() };
|
||||
result.values[..values.len()].clone_from_slice(values);
|
||||
result
|
||||
}
|
||||
pub fn as_slice(&self) -> &[Value] {
|
||||
&self.values[..self.len]
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn u32_carrier(value: &Value) -> Result<u32, ValueError> {
|
||||
match value {
|
||||
Value::Int64(n) => u32::try_from(*n).map_err(|_| ValueError::Type),
|
||||
_ => Err(ValueError::Type),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn validate(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
type_id: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<(), ValueError> {
|
||||
let layout = registry.type_layout(type_id).ok_or(ValueError::Type)?;
|
||||
if values.len() != layout.storage.slots as usize {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
let kind = ®istry.declarations().types[type_id.0 as usize].kind;
|
||||
let valid = match kind {
|
||||
TypeKind::Struct(_) | TypeKind::Array(_) => match &values[0] {
|
||||
Value::Null => true,
|
||||
Value::HeapRef(r) => {
|
||||
if r.is_null() {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
heap.concrete_type(*r, registry)? == type_id
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
TypeKind::Trait(_) => return validate_trait(registry, heap, type_id, values),
|
||||
TypeKind::Builtin(B::DYNAMIC) => return validate_dynamic(registry, heap, values),
|
||||
TypeKind::Builtin(id) => match (*id, values) {
|
||||
(B::INT32, [Value::Int32(_)])
|
||||
| (B::INT64, [Value::Int64(_)])
|
||||
| (B::FLOAT, [Value::Float(_)])
|
||||
| (B::BOOL, [Value::Boolean(_)])
|
||||
| (B::STRING, [Value::String(_) | Value::Null])
|
||||
| (B::VEC2, [Value::Float(_), Value::Float(_)]) => true,
|
||||
(B::COLOR, [color]) => u32_carrier(color).is_ok(),
|
||||
(B::PIXEL, [Value::Int64(_), Value::Int64(_), color]) => u32_carrier(color).is_ok(),
|
||||
_ => false,
|
||||
},
|
||||
};
|
||||
if valid { Ok(()) } else { Err(ValueError::Type) }
|
||||
}
|
||||
|
||||
fn validate_trait(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
trait_id: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<(), ValueError> {
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
let [Value::HeapRef(receiver), implementation] = values else { return Err(ValueError::Type) };
|
||||
if receiver.is_null() {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
let owner = heap.concrete_type(*receiver, registry)?;
|
||||
let id = u32_carrier(implementation)?;
|
||||
let implementation =
|
||||
registry.declarations().implementations.get(id as usize).ok_or(ValueError::Type)?;
|
||||
if implementation.owner == owner && implementation.trait_id == trait_id {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ValueError::Type)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_dynamic(registry: &Registry, heap: &Heap, values: &[Value]) -> Result<(), ValueError> {
|
||||
if values.len() != 2 {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
let type_id = TypeId(u32_carrier(&values[0])?);
|
||||
let layout = registry.type_layout(type_id).ok_or(ValueError::Type)?;
|
||||
if matches!(
|
||||
registry.declarations().types[type_id.0 as usize].kind,
|
||||
TypeKind::Builtin(B::DYNAMIC)
|
||||
) {
|
||||
return Err(ValueError::Type); // Repacking is identity, never Dynamic<Dynamic>.
|
||||
}
|
||||
if matches!(values[1], Value::Null) {
|
||||
return if layout.storage.nullable() { Ok(()) } else { Err(ValueError::Type) };
|
||||
}
|
||||
if layout.storage.slots == 1 {
|
||||
return validate(registry, heap, type_id, &values[1..]);
|
||||
}
|
||||
let Value::HeapRef(handle) = values[1] else { return Err(ValueError::Type) };
|
||||
if handle == HeapRef::NULL {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
heap.dynamic_box_values(registry, handle, type_id)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Semantic pack helper. The interpreter must retain source stack roots until
|
||||
/// this returns and publish/consume the complete group atomically.
|
||||
pub(crate) fn pack_dynamic(
|
||||
registry: &std::sync::Arc<Registry>,
|
||||
heap: &mut Heap,
|
||||
type_id: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<ValueGroup, ValueError> {
|
||||
validate(registry, heap, type_id, values)?;
|
||||
if is_dynamic(registry, type_id) {
|
||||
return Ok(ValueGroup::new(values));
|
||||
}
|
||||
let payload = if values.iter().all(|v| matches!(v, Value::Null)) {
|
||||
Value::Null // Includes absent traits: typed null does not need a box.
|
||||
} else if values.len() == 1 {
|
||||
values[0].clone()
|
||||
} else {
|
||||
Value::HeapRef(heap.allocate_dynamic_box(registry.clone(), type_id, values)?)
|
||||
};
|
||||
Ok(ValueGroup::new(&[Value::Int64(type_id.0.into()), payload]))
|
||||
}
|
||||
|
||||
fn is_dynamic(registry: &Registry, type_id: TypeId) -> bool {
|
||||
matches!(registry.declarations().types[type_id.0 as usize].kind, TypeKind::Builtin(B::DYNAMIC))
|
||||
}
|
||||
|
||||
/// Compatibility validates malformed input even for a mismatched destination.
|
||||
/// No allocation, extraction, or Arc/box clone occurs in a query.
|
||||
pub(crate) fn can_unpack_dynamic(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
target: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<bool, ValueError> {
|
||||
let target_layout = registry.type_layout(target).ok_or(ValueError::Type)?;
|
||||
validate_dynamic(registry, heap, values)?;
|
||||
if is_dynamic(registry, target) {
|
||||
return Ok(true);
|
||||
}
|
||||
match &values[0] {
|
||||
Value::Null => Ok(target_layout.storage.nullable()),
|
||||
tag => Ok(TypeId(u32_carrier(tag)?) == target),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unpack_dynamic(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
target: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<ValueGroup, ValueError> {
|
||||
if !can_unpack_dynamic(registry, heap, target, values)? {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
if is_dynamic(registry, target) {
|
||||
return Ok(ValueGroup::new(values));
|
||||
}
|
||||
let layout = registry.type_layout(target).unwrap().storage;
|
||||
if matches!(values[1], Value::Null) {
|
||||
return Ok(ValueGroup::new(layout.default_values()));
|
||||
}
|
||||
if layout.slots == 1 {
|
||||
return Ok(ValueGroup::new(&values[1..]));
|
||||
}
|
||||
let Value::HeapRef(r) = values[1] else { return Err(ValueError::Type) };
|
||||
Ok(ValueGroup::new(heap.dynamic_box_values(registry, r, target)?))
|
||||
}
|
||||
@@ -9,6 +9,18 @@ use std::collections::{HashMap, HashSet, VecDeque};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VerifierError {
|
||||
InvalidLocalSlot {
|
||||
pc: usize,
|
||||
slot: u32,
|
||||
slots: u32,
|
||||
},
|
||||
InvalidBoolImmediate {
|
||||
pc: usize,
|
||||
},
|
||||
RegistryRequired {
|
||||
pc: usize,
|
||||
opcode: OpCode,
|
||||
},
|
||||
UnknownOpcode {
|
||||
pc: usize,
|
||||
opcode: u16,
|
||||
@@ -36,19 +48,19 @@ pub enum VerifierError {
|
||||
},
|
||||
StackOverflow {
|
||||
pc: usize,
|
||||
height: u16,
|
||||
limit: u16,
|
||||
height: u32,
|
||||
limit: u32,
|
||||
},
|
||||
StackMismatchJoin {
|
||||
pc: usize,
|
||||
target: usize,
|
||||
height_in: u16,
|
||||
height_target: u16,
|
||||
height_in: u32,
|
||||
height_target: u32,
|
||||
},
|
||||
BadRetStackHeight {
|
||||
pc: usize,
|
||||
height: u16,
|
||||
expected: u16,
|
||||
height: u32,
|
||||
expected: u32,
|
||||
},
|
||||
FunctionOutOfBounds {
|
||||
func_idx: usize,
|
||||
@@ -95,19 +107,19 @@ pub enum VerifierError {
|
||||
/// User-provided arg_count for CALL_CLOSURE does not match callee signature
|
||||
BadClosureArgCount {
|
||||
pc: usize,
|
||||
expected: u16,
|
||||
got: u16,
|
||||
expected: u32,
|
||||
got: u32,
|
||||
},
|
||||
/// YIELD executed in an invalid context (minimal safety rule violation)
|
||||
InvalidYieldContext {
|
||||
pc: usize,
|
||||
height: u16,
|
||||
height: u32,
|
||||
},
|
||||
/// SPAWN arg_count does not match callee param_slots
|
||||
BadSpawnArgCount {
|
||||
pc: usize,
|
||||
expected: u16,
|
||||
got: u16,
|
||||
expected: u32,
|
||||
got: u32,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -178,6 +190,67 @@ impl Verifier {
|
||||
}
|
||||
}
|
||||
})?;
|
||||
if instr.opcode.requires_registry() {
|
||||
return Err(VerifierError::RegistryRequired {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
});
|
||||
}
|
||||
match instr.opcode {
|
||||
OpCode::Call | OpCode::MakeClosure | OpCode::Spawn => {
|
||||
let id = if instr.opcode == OpCode::Call {
|
||||
instr.imm_u32().unwrap()
|
||||
} else {
|
||||
instr.imm_u32x2().unwrap().0
|
||||
};
|
||||
let callee = all_functions
|
||||
.get(id as usize)
|
||||
.ok_or(VerifierError::InvalidFuncId { pc: func_start + pc, id })?;
|
||||
if instr.opcode == OpCode::Spawn {
|
||||
let got = instr.imm_u32x2().unwrap().1;
|
||||
if got != u32::from(callee.param_slots) {
|
||||
return Err(VerifierError::BadSpawnArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: u32::from(callee.param_slots),
|
||||
got,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
OpCode::GetLocal | OpCode::SetLocal => {
|
||||
let slot = instr.imm_u32().unwrap();
|
||||
let slots = u32::from(func.param_slots) + u32::from(func.local_slots);
|
||||
if slot >= slots {
|
||||
return Err(VerifierError::InvalidLocalSlot {
|
||||
pc: func_start + pc,
|
||||
slot,
|
||||
slots,
|
||||
});
|
||||
}
|
||||
}
|
||||
OpCode::Hostcall => {
|
||||
return Err(VerifierError::HostcallNotPatched {
|
||||
pc: func_start + pc,
|
||||
sysc_index: instr.imm_u32().unwrap(),
|
||||
});
|
||||
}
|
||||
OpCode::Syscall => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
if Syscall::from_u32(id).is_none() {
|
||||
return Err(VerifierError::InvalidSyscallId { pc: func_start + pc, id });
|
||||
}
|
||||
}
|
||||
OpCode::Intrinsic => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
if lookup_intrinsic_by_id(id).is_none() {
|
||||
return Err(VerifierError::InvalidIntrinsicId { pc: func_start + pc, id });
|
||||
}
|
||||
}
|
||||
OpCode::PushBool if instr.imm_u8().unwrap() > 1 => {
|
||||
return Err(VerifierError::InvalidBoolImmediate { pc: func_start + pc });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
pc = instr.next_pc;
|
||||
}
|
||||
|
||||
@@ -185,6 +258,29 @@ impl Verifier {
|
||||
return Err(VerifierError::TrailingBytes { func_idx, at_pc: func_start + pc });
|
||||
}
|
||||
|
||||
// Check branch immediates over the whole decoded range, before reachability
|
||||
// analysis, so dead code cannot hide an invalid target or boundary.
|
||||
let mut pc = 0;
|
||||
while pc < func_code.len() {
|
||||
let instr = decode_next(pc, func_code).unwrap();
|
||||
if instr.opcode.spec().is_branch {
|
||||
let target = instr.imm_u32().unwrap() as usize;
|
||||
if target > func_code.len() {
|
||||
return Err(VerifierError::InvalidJumpTarget {
|
||||
pc: func_start + pc,
|
||||
target: func_start + target,
|
||||
});
|
||||
}
|
||||
if target < func_code.len() && !valid_pc.contains(&target) {
|
||||
return Err(VerifierError::JumpToMidInstruction {
|
||||
pc: func_start + pc,
|
||||
target: func_start + target,
|
||||
});
|
||||
}
|
||||
}
|
||||
pc = instr.next_pc;
|
||||
}
|
||||
|
||||
// Minimal stack type lattice to validate closure semantics
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum SlotTy {
|
||||
@@ -215,14 +311,22 @@ impl Verifier {
|
||||
|
||||
while let Some(pc) = worklist.pop_front() {
|
||||
let in_types = stack_types_in.get(&pc).cloned().unwrap();
|
||||
let in_height = in_types.len() as u16;
|
||||
let in_height = in_types.len() as u64;
|
||||
let instr = decode_next(pc, func_code).unwrap(); // Guaranteed to succeed due to first pass
|
||||
let spec = instr.opcode.spec();
|
||||
let static_pops = spec.pops.ok_or(VerifierError::RegistryRequired {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
})?;
|
||||
let static_pushes = spec.pushes.ok_or(VerifierError::RegistryRequired {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
})?;
|
||||
|
||||
// Resolve dynamic pops/_pushes
|
||||
let (pops, _pushes) = match instr.opcode {
|
||||
OpCode::PopN => {
|
||||
let n = instr.imm_u32().unwrap() as u16;
|
||||
let n = instr.imm_u32().unwrap() as u64;
|
||||
(n, 0)
|
||||
}
|
||||
OpCode::Call => {
|
||||
@@ -230,18 +334,18 @@ impl Verifier {
|
||||
let callee = all_functions.get(func_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: func_id }
|
||||
})?;
|
||||
(callee.param_slots, callee.return_slots)
|
||||
(u64::from(callee.param_slots), u64::from(callee.return_slots))
|
||||
}
|
||||
OpCode::MakeClosure => {
|
||||
// imm: fn_id (u32), capture_count (u32)
|
||||
let (_fn_id, capture_count_u32) = instr.imm_u32x2().unwrap();
|
||||
let capture_count = capture_count_u32 as u16;
|
||||
let capture_count = capture_count_u32 as u64;
|
||||
(capture_count, 1)
|
||||
}
|
||||
OpCode::CallClosure => {
|
||||
// imm: arg_count (u32). Pops closure_ref + arg_count, _pushes callee returns.
|
||||
// We can't determine _pushes here without looking at TOS type; will validate below.
|
||||
let arg_count = instr.imm_u32().unwrap() as u16;
|
||||
let arg_count = instr.imm_u32().unwrap() as u64;
|
||||
// Temporarily set _pushes=0; we'll compute real _pushes after type checks.
|
||||
(arg_count + 1, 0)
|
||||
}
|
||||
@@ -251,18 +355,18 @@ impl Verifier {
|
||||
let callee = all_functions.get(fn_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: fn_id }
|
||||
})?;
|
||||
let arg_count = arg_count_u32 as u16;
|
||||
let arg_count = arg_count_u32 as u64;
|
||||
// Enforce exact arity match for safety and determinism
|
||||
if callee.param_slots != arg_count {
|
||||
if u64::from(callee.param_slots) != arg_count {
|
||||
return Err(VerifierError::BadSpawnArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: callee.param_slots,
|
||||
got: arg_count,
|
||||
expected: u32::from(callee.param_slots),
|
||||
got: arg_count as u32,
|
||||
});
|
||||
}
|
||||
(arg_count, 0)
|
||||
}
|
||||
OpCode::Ret => (func.return_slots, 0),
|
||||
OpCode::Ret => (u64::from(func.return_slots), 0),
|
||||
OpCode::Hostcall => {
|
||||
return Err(VerifierError::HostcallNotPatched {
|
||||
pc: func_start + pc,
|
||||
@@ -274,16 +378,16 @@ impl Verifier {
|
||||
let syscall = Syscall::from_u32(id).ok_or_else(|| {
|
||||
VerifierError::InvalidSyscallId { pc: func_start + pc, id }
|
||||
})?;
|
||||
(syscall.args_count() as u16, syscall.results_count() as u16)
|
||||
(syscall.args_count() as u64, syscall.results_count() as u64)
|
||||
}
|
||||
OpCode::Intrinsic => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let intrinsic = lookup_intrinsic_by_id(id).ok_or_else(|| {
|
||||
VerifierError::InvalidIntrinsicId { pc: func_start + pc, id }
|
||||
})?;
|
||||
(intrinsic.arg_slots() as u16, intrinsic.ret_slots() as u16)
|
||||
(intrinsic.arg_slots() as u64, intrinsic.ret_slots() as u64)
|
||||
}
|
||||
_ => (spec.pops, spec.pushes),
|
||||
_ => (u64::from(static_pops), u64::from(static_pushes)),
|
||||
};
|
||||
|
||||
if in_height < pops {
|
||||
@@ -299,7 +403,7 @@ impl Verifier {
|
||||
{
|
||||
return Err(VerifierError::InvalidYieldContext {
|
||||
pc: func_start + pc,
|
||||
height: in_height,
|
||||
height: in_height as u32,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -326,21 +430,21 @@ impl Verifier {
|
||||
let callee = all_functions.get(fn_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: fn_id }
|
||||
})?;
|
||||
let user_argc = instr.imm_u32().unwrap() as u16;
|
||||
let user_argc = instr.imm_u32().unwrap() as u64;
|
||||
if callee.param_slots == 0 {
|
||||
// Hidden arg0 must exist in callee signature
|
||||
return Err(VerifierError::BadClosureArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: 0,
|
||||
got: user_argc,
|
||||
got: user_argc as u32,
|
||||
});
|
||||
}
|
||||
let expected_user = callee.param_slots - 1; // exclude hidden arg0
|
||||
if expected_user != user_argc {
|
||||
if u64::from(expected_user) != user_argc {
|
||||
return Err(VerifierError::BadClosureArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: expected_user,
|
||||
got: user_argc,
|
||||
expected: u32::from(expected_user),
|
||||
got: user_argc as u32,
|
||||
});
|
||||
}
|
||||
// Push callee returns as Unknown types
|
||||
@@ -380,6 +484,8 @@ impl Verifier {
|
||||
opcode: OpCode::Dup,
|
||||
});
|
||||
}
|
||||
// Generic transfer popped the original; restore it and its copy.
|
||||
out_types.push(tos);
|
||||
out_types.push(tos);
|
||||
}
|
||||
// Swap swaps top-2 types; ensure enough stack
|
||||
@@ -390,8 +496,10 @@ impl Verifier {
|
||||
opcode: OpCode::Swap,
|
||||
});
|
||||
}
|
||||
let len = out_types.len();
|
||||
out_types.swap(len - 1, len - 2);
|
||||
// Generic transfer popped both values; rebuild the swapped pair.
|
||||
let len = in_types.len();
|
||||
out_types.push(in_types[len - 1]);
|
||||
out_types.push(in_types[len - 2]);
|
||||
}
|
||||
// Arithmetic/logic produce NonClosure but we don't strictly need it; mark Unknown conservatively
|
||||
// Calls and syscalls return Unknown
|
||||
@@ -405,21 +513,21 @@ impl Verifier {
|
||||
OpCode::Syscall => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let syscall = Syscall::from_u32(id).unwrap();
|
||||
for _ in 0..(syscall.results_count() as u16) {
|
||||
for _ in 0..syscall.results_count() {
|
||||
out_types.push(Unknown);
|
||||
}
|
||||
}
|
||||
OpCode::Intrinsic => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let intrinsic = lookup_intrinsic_by_id(id).unwrap();
|
||||
for _ in 0..(intrinsic.ret_slots() as u16) {
|
||||
for _ in 0..intrinsic.ret_slots() {
|
||||
out_types.push(Unknown);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Default: push Unknown for any declared _pushes
|
||||
if spec.pushes > 0 {
|
||||
for _ in 0..spec.pushes {
|
||||
if static_pushes > 0 {
|
||||
for _ in 0..static_pushes {
|
||||
out_types.push(Unknown);
|
||||
}
|
||||
}
|
||||
@@ -432,29 +540,36 @@ impl Verifier {
|
||||
// If we reached here, we handled it above and set dynamic_pushes
|
||||
0
|
||||
}
|
||||
OpCode::Syscall => spec.pushes, // already added Unknowns above
|
||||
OpCode::Intrinsic => spec.pushes, // already added Unknowns above
|
||||
OpCode::Call => spec.pushes, // already added Unknowns above
|
||||
_ => spec.pushes,
|
||||
OpCode::Syscall => static_pushes, // already added Unknowns above
|
||||
OpCode::Intrinsic => static_pushes, // already added Unknowns above
|
||||
OpCode::Call => static_pushes, // already added Unknowns above
|
||||
_ => static_pushes,
|
||||
});
|
||||
|
||||
let out_height = (out_types.len()) as u16;
|
||||
max_stack = max_stack.max(out_height);
|
||||
|
||||
// Enforce declared max stack if provided (0 means "no explicit limit")
|
||||
if func.max_stack_slots != 0 && out_height > func.max_stack_slots {
|
||||
let out_height = out_types.len() as u32;
|
||||
if out_height > u32::from(u16::MAX) {
|
||||
return Err(VerifierError::StackOverflow {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
limit: func.max_stack_slots,
|
||||
limit: u32::from(u16::MAX),
|
||||
});
|
||||
}
|
||||
max_stack = max_stack.max(out_height as u16);
|
||||
|
||||
// Enforce declared max stack if provided (0 means "no explicit limit")
|
||||
if func.max_stack_slots != 0 && out_height > u32::from(func.max_stack_slots) {
|
||||
return Err(VerifierError::StackOverflow {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
limit: u32::from(func.max_stack_slots),
|
||||
});
|
||||
}
|
||||
|
||||
if instr.opcode == OpCode::Ret && in_height != func.return_slots {
|
||||
if instr.opcode == OpCode::Ret && in_height != u64::from(func.return_slots) {
|
||||
return Err(VerifierError::BadRetStackHeight {
|
||||
pc: func_start + pc,
|
||||
height: in_height,
|
||||
expected: func.return_slots,
|
||||
height: in_height as u32,
|
||||
expected: u32::from(func.return_slots),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -496,11 +611,11 @@ impl Verifier {
|
||||
|
||||
if target_rel == func_len {
|
||||
// jump to the end of function
|
||||
if out_height != func.return_slots {
|
||||
if out_height != u32::from(func.return_slots) {
|
||||
return Err(VerifierError::BadRetStackHeight {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
expected: func.return_slots,
|
||||
expected: u32::from(func.return_slots),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -512,7 +627,7 @@ impl Verifier {
|
||||
}
|
||||
|
||||
if let Some(existing_types) = stack_types_in.get_mut(&target_rel) {
|
||||
let existing_height = existing_types.len() as u16;
|
||||
let existing_height = existing_types.len() as u32;
|
||||
if existing_height != out_height {
|
||||
return Err(VerifierError::StackMismatchJoin {
|
||||
pc: func_start + pc,
|
||||
@@ -546,7 +661,7 @@ impl Verifier {
|
||||
let func_len = layouts.get(func_idx).map(|l| l.end - l.start).unwrap_or_else(|| 0);
|
||||
if next_pc < func_len {
|
||||
if let Some(existing_types) = stack_types_in.get_mut(&next_pc) {
|
||||
let existing_height = existing_types.len() as u16;
|
||||
let existing_height = existing_types.len() as u32;
|
||||
if existing_height != out_height {
|
||||
return Err(VerifierError::StackMismatchJoin {
|
||||
pc: func_start + pc,
|
||||
@@ -951,6 +1066,18 @@ mod golden_ext {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn slot_only_verifier_rejects_registry_instruction_even_when_unreachable() {
|
||||
use super::{Verifier, VerifierError};
|
||||
use prometeu_bytecode::{FunctionMeta, assemble};
|
||||
let code = assemble("HALT\nDYN_PACK 0").unwrap();
|
||||
let f = FunctionMeta { code_len: code.len() as u32, ..Default::default() };
|
||||
assert!(matches!(
|
||||
Verifier::verify(&code, &[f]),
|
||||
Err(VerifierError::RegistryRequired { pc: 2, .. })
|
||||
));
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -1388,3 +1515,179 @@ mod tests {
|
||||
assert_eq!(res, Err(VerifierError::BadRetStackHeight { pc: 6, height: 2, expected: 1 }));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod width_regressions {
|
||||
use super::*;
|
||||
fn op(code: &mut Vec<u8>, opcode: OpCode) {
|
||||
code.extend_from_slice(&(opcode as u16).to_le_bytes());
|
||||
}
|
||||
fn arg(code: &mut Vec<u8>, opcode: OpCode, n: u32) {
|
||||
op(code, opcode);
|
||||
code.extend_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
fn one(code: &[u8]) -> Vec<FunctionMeta> {
|
||||
vec![FunctionMeta { code_len: code.len() as u32, ..Default::default() }]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn popn_u32_counts_do_not_wrap_to_zero() {
|
||||
for n in [65_536, u32::MAX] {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::PopN, n);
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::StackUnderflow { pc: 0, opcode: OpCode::PopN }
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn closure_capture_u32_counts_do_not_wrap_to_zero() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 0);
|
||||
code.extend_from_slice(&65_536_u32.to_le_bytes());
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::StackUnderflow { pc: 0, opcode: OpCode::MakeClosure }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn closure_argument_plus_receiver_cannot_overflow() {
|
||||
for n in [65_536, u32::MAX] {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 0);
|
||||
code.extend_from_slice(&0_u32.to_le_bytes());
|
||||
arg(&mut code, OpCode::CallClosure, n);
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::StackUnderflow { pc: 10, opcode: OpCode::CallClosure }
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn spawn_diagnostic_preserves_the_full_u32_argument_count() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::Spawn, 0);
|
||||
code.extend_from_slice(&65_536_u32.to_le_bytes());
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::BadSpawnArgCount { pc: 0, expected: 0, got: 65_536 }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn inferred_stack_height_cannot_wrap_when_exported_to_current_function_metadata() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::Call, 1);
|
||||
op(&mut code, OpCode::Dup);
|
||||
op(&mut code, OpCode::Halt);
|
||||
let functions = vec![
|
||||
FunctionMeta { code_len: code.len() as u32, ..Default::default() },
|
||||
FunctionMeta {
|
||||
code_offset: code.len() as u32,
|
||||
return_slots: u16::MAX,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &functions).unwrap_err(),
|
||||
VerifierError::StackOverflow { pc: 6, height: 65_536, limit: 65_535 }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn local_indices_cannot_address_temporary_slots_even_in_dead_code() {
|
||||
for operation in [OpCode::GetLocal, OpCode::SetLocal] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
arg(&mut code, operation, 1);
|
||||
let mut functions = one(&code);
|
||||
functions[0].local_slots = 1;
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &functions).unwrap_err(),
|
||||
VerifierError::InvalidLocalSlot { pc: 2, slot: 1, slots: 1 }
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn unreachable_bad_functions_natives_booleans_and_jumps_are_rejected() {
|
||||
for operation in [OpCode::Call, OpCode::Intrinsic, OpCode::Syscall] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
arg(&mut code, operation, u32::MAX);
|
||||
assert!(Verifier::verify(&code, &one(&code)).is_err());
|
||||
}
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
op(&mut code, OpCode::PushBool);
|
||||
code.push(2);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::InvalidBoolImmediate { pc: 2 }
|
||||
);
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
arg(&mut code, OpCode::Jmp, 3);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::JumpToMidInstruction { pc: 2, target: 3 }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn dup_increases_height_and_restores_both_copies_for_arithmetic() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::PushI32, 7);
|
||||
op(&mut code, OpCode::Dup);
|
||||
op(&mut code, OpCode::Add);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let mut functions = one(&code);
|
||||
functions[0].return_slots = 1;
|
||||
assert_eq!(Verifier::verify(&code, &functions).unwrap(), vec![2]);
|
||||
}
|
||||
#[test]
|
||||
fn swap_preserves_height_and_known_closure_in_the_correct_position() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 1);
|
||||
code.extend_from_slice(&0_u32.to_le_bytes());
|
||||
arg(&mut code, OpCode::PushI32, 5);
|
||||
op(&mut code, OpCode::Swap);
|
||||
arg(&mut code, OpCode::CallClosure, 1);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let start = code.len();
|
||||
op(&mut code, OpCode::Ret);
|
||||
let functions = vec![
|
||||
FunctionMeta { code_len: start as u32, ..Default::default() },
|
||||
FunctionMeta {
|
||||
code_offset: start as u32,
|
||||
code_len: 2,
|
||||
param_slots: 2,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
assert_eq!(Verifier::verify(&code, &functions).unwrap(), vec![2, 0]);
|
||||
}
|
||||
#[test]
|
||||
fn dup_preserves_the_known_callee_in_each_closure_copy() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 1);
|
||||
code.extend_from_slice(&0_u32.to_le_bytes());
|
||||
op(&mut code, OpCode::Dup);
|
||||
arg(&mut code, OpCode::CallClosure, 0);
|
||||
arg(&mut code, OpCode::CallClosure, 0);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let start = code.len();
|
||||
op(&mut code, OpCode::Ret);
|
||||
let functions = vec![
|
||||
FunctionMeta { code_len: start as u32, ..Default::default() },
|
||||
FunctionMeta {
|
||||
code_offset: start as u32,
|
||||
code_len: 2,
|
||||
param_slots: 1,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
assert_eq!(Verifier::verify(&code, &functions).unwrap(), vec![2, 0]);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,63 +1,59 @@
|
||||
use super::*;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn handle_safepoint(&mut self) {
|
||||
pub(super) fn handle_safepoint(&mut self) -> Result<(), ResourceFailure> {
|
||||
if self.gc_alloc_threshold > 0 {
|
||||
let live_now = self.heap.len();
|
||||
let since_last = live_now.saturating_sub(self.last_gc_live_count);
|
||||
if since_last >= self.gc_alloc_threshold {
|
||||
struct CollectRoots(Vec<prometeu_bytecode::HeapRef>);
|
||||
impl crate::roots::RootVisitor for CollectRoots {
|
||||
fn visit_heap_ref(&mut self, r: prometeu_bytecode::HeapRef) {
|
||||
self.0.push(r);
|
||||
}
|
||||
}
|
||||
let mut collector = CollectRoots(Vec::new());
|
||||
self.visit_roots(&mut collector);
|
||||
if let Some(cur) = self.current_coro {
|
||||
collector.0.push(cur);
|
||||
}
|
||||
let mut coro_roots = self.heap.suspended_coroutine_handles();
|
||||
collector.0.append(&mut coro_roots);
|
||||
self.heap.mark_from_roots(collector.0);
|
||||
// Parameters/locals are already in the operand buffer. Stream roots
|
||||
// without a duplicate collection; suspended contexts are scanned by Heap.
|
||||
let roots = self
|
||||
.operand_stack
|
||||
.iter()
|
||||
.chain(self.globals.iter())
|
||||
.filter_map(crate::roots::value_heap_ref)
|
||||
.chain(self.current_coro);
|
||||
self.heap.mark_session_roots(roots)?;
|
||||
self.heap.sweep();
|
||||
self.last_gc_live_count = self.heap.len();
|
||||
}
|
||||
}
|
||||
|
||||
self.current_tick = self.current_tick.wrapping_add(1);
|
||||
self.scheduler.wake_ready(self.current_tick);
|
||||
self.scheduler.wake_ready(self.current_tick)?;
|
||||
|
||||
let mut switched_out = false;
|
||||
if let Some(cur) = self.current_coro {
|
||||
if let Some(wake) = self.sleep_requested_until.take() {
|
||||
if let Some(wake) = self.sleep_requested_until {
|
||||
self.scheduler.sleep_until(cur, wake)?;
|
||||
self.sleep_requested_until = None;
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Sleeping;
|
||||
co.wake_tick = wake;
|
||||
}
|
||||
self.scheduler.sleep_until(cur, wake);
|
||||
self.current_coro = None;
|
||||
self.scheduler.clear_current();
|
||||
switched_out = true;
|
||||
} else if self.yield_requested {
|
||||
self.scheduler.enqueue_ready(cur)?;
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Ready;
|
||||
}
|
||||
self.scheduler.enqueue_ready(cur);
|
||||
self.current_coro = None;
|
||||
self.scheduler.clear_current();
|
||||
switched_out = true;
|
||||
} else if self.halted || self.pc >= self.program.rom.len() {
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Finished;
|
||||
}
|
||||
self.current_coro = None;
|
||||
@@ -70,8 +66,8 @@ impl VirtualMachine {
|
||||
if let Some(next) = self.scheduler.dequeue_next() {
|
||||
if let Some(co) = self.heap.coroutine_data_mut(next) {
|
||||
self.pc = co.pc;
|
||||
self.operand_stack = std::mem::take(&mut co.stack);
|
||||
self.call_stack = std::mem::take(&mut co.frames);
|
||||
self.operand_stack = co.stack.take();
|
||||
self.call_stack = co.frames.take();
|
||||
co.state = CoroutineState::Running;
|
||||
}
|
||||
self.current_coro = Some(next);
|
||||
@@ -84,6 +80,7 @@ impl VirtualMachine {
|
||||
}
|
||||
|
||||
self.yield_requested = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn visit_roots<V: RootVisitor + ?Sized>(&self, visitor: &mut V) {
|
||||
|
||||
@@ -74,13 +74,13 @@ impl VirtualMachine {
|
||||
self.operand_stack.clear();
|
||||
self.call_stack.clear();
|
||||
self.globals.clear();
|
||||
self.heap = Heap::new();
|
||||
self.heap = Heap::with_budget(&self.budget);
|
||||
self.cycles = 0;
|
||||
self.halted = true;
|
||||
self.last_gc_live_count = 0;
|
||||
self.current_tick = 0;
|
||||
self.sleep_requested_until = None;
|
||||
self.scheduler = Scheduler::new();
|
||||
self.scheduler = Scheduler::with_budget(&self.budget);
|
||||
self.current_coro = None;
|
||||
|
||||
let program = match prometeu_bytecode::BytecodeLoader::load(&program_bytes) {
|
||||
@@ -89,7 +89,7 @@ impl VirtualMachine {
|
||||
.map_err(VmInitError::LoaderPatchFailed)?;
|
||||
|
||||
let max_stacks = Verifier::verify(&module.code, &module.functions)
|
||||
.map_err(|e| VmInitError::VerificationFailed(format!("{:?}", e)))?;
|
||||
.map_err(VmInitError::VerificationFailed)?;
|
||||
|
||||
let mut program = ProgramImage::from(module);
|
||||
let mut functions = program.functions.as_ref().to_vec();
|
||||
@@ -128,22 +128,33 @@ impl VirtualMachine {
|
||||
self.capabilities = caps;
|
||||
}
|
||||
|
||||
pub fn prepare_boot_call(&mut self) {
|
||||
self.prepare_call_by_index(0);
|
||||
pub fn prepare_boot_call(&mut self) -> Result<(), ResourceFailure> {
|
||||
self.prepare_call_by_index(0)
|
||||
}
|
||||
|
||||
pub fn prepare_call(&mut self, entrypoint: &str) {
|
||||
pub fn prepare_call(&mut self, entrypoint: &str) -> Result<(), ResourceFailure> {
|
||||
let func_idx = if let Ok(idx) = entrypoint.parse::<usize>() {
|
||||
idx
|
||||
} else {
|
||||
self.program.exports.get(entrypoint).map(|&idx| idx as usize).ok_or(()).unwrap_or(0)
|
||||
};
|
||||
|
||||
self.prepare_call_by_index(func_idx);
|
||||
self.prepare_call_by_index(func_idx)
|
||||
}
|
||||
|
||||
fn prepare_call_by_index(&mut self, func_idx: usize) {
|
||||
fn prepare_call_by_index(&mut self, func_idx: usize) -> Result<(), ResourceFailure> {
|
||||
self.budget.check(RuntimeResource::CallDepth, 1)?;
|
||||
let callee = self.program.functions.get(func_idx).cloned().unwrap_or_default();
|
||||
let total_slots = callee.param_slots as usize + callee.local_slots as usize;
|
||||
self.operand_stack.reserve(total_slots.saturating_sub(self.operand_stack.len()))?;
|
||||
self.call_stack.reserve(1usize.saturating_sub(self.call_stack.len()))?;
|
||||
let main_href = self.heap.allocate_coroutine(
|
||||
callee.code_offset as usize,
|
||||
CoroutineState::Running,
|
||||
0,
|
||||
BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes),
|
||||
BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes),
|
||||
)?;
|
||||
self.pc = callee.code_offset as usize;
|
||||
self.halted = false;
|
||||
|
||||
@@ -153,7 +164,7 @@ impl VirtualMachine {
|
||||
if let Some(func) = self.program.functions.get(func_idx) {
|
||||
let total_slots = func.param_slots as u32 + func.local_slots as u32;
|
||||
for _ in 0..total_slots {
|
||||
self.operand_stack.push(Value::Null);
|
||||
self.operand_stack.push(Value::Null)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,16 +172,10 @@ impl VirtualMachine {
|
||||
return_pc: self.program.rom.len() as u32,
|
||||
stack_base: 0,
|
||||
func_idx,
|
||||
});
|
||||
})?;
|
||||
|
||||
let main_href = self.heap.allocate_coroutine(
|
||||
self.pc,
|
||||
CoroutineState::Running,
|
||||
0,
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
);
|
||||
self.current_coro = Some(main_href);
|
||||
self.scheduler.set_current(self.current_coro);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,26 @@
|
||||
use super::*;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn check_call_depth(&self) -> Result<(), ResourceFailure> {
|
||||
let depth = self.call_stack.len().checked_add(1).ok_or_else(|| {
|
||||
self.budget.failure(
|
||||
RuntimeResource::CallDepth,
|
||||
crate::resource_budget::ResourceReason::SizeOverflow,
|
||||
u64::MAX,
|
||||
)
|
||||
})?;
|
||||
self.budget.check(RuntimeResource::CallDepth, depth as u64)
|
||||
}
|
||||
|
||||
pub(super) fn resource_trap(
|
||||
&self,
|
||||
error: ResourceFailure,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
) -> LogicalFrameEndingReason {
|
||||
self.trap(TRAP_RESOURCE_LIMIT, opcode as u16, error.to_string(), pc)
|
||||
}
|
||||
|
||||
pub fn trap(
|
||||
&self,
|
||||
code: u32,
|
||||
@@ -11,8 +31,17 @@ impl VirtualMachine {
|
||||
LogicalFrameEndingReason::Trap(self.program.create_trap(code, opcode, message, pc))
|
||||
}
|
||||
|
||||
pub fn push(&mut self, val: Value) {
|
||||
self.operand_stack.push(val);
|
||||
pub fn push(&mut self, val: Value) -> Result<(), ResourceFailure> {
|
||||
self.operand_stack.push(val)
|
||||
}
|
||||
|
||||
pub(super) fn push_trap(
|
||||
&mut self,
|
||||
val: Value,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
self.push(val).map_err(|e| self.resource_trap(e, opcode, pc))
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) -> Result<Value, String> {
|
||||
@@ -67,7 +96,7 @@ impl VirtualMachine {
|
||||
let a = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
|
||||
match f(a, b) {
|
||||
Ok(res) => {
|
||||
self.push(res);
|
||||
self.push_trap(res, opcode, start_pc)?;
|
||||
Ok(())
|
||||
}
|
||||
Err(OpError::Trap(code, msg)) => Err(self.trap(code, opcode as u16, msg, start_pc)),
|
||||
|
||||
@@ -10,10 +10,11 @@ pub enum LoaderPatchError {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VmInitError {
|
||||
RuntimeResourceLimit(crate::resource_budget::ResourceFailure),
|
||||
InvalidFormat,
|
||||
UnsupportedFormat,
|
||||
ImageLoadFailed(prometeu_bytecode::LoadError),
|
||||
LoaderPatchFailed(LoaderPatchError),
|
||||
EntrypointNotFound,
|
||||
VerificationFailed(String),
|
||||
VerificationFailed(crate::verifier::VerifierError),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
use prometeu_bytecode::registry::{BuiltinId, DynamicSlot, StringSlot};
|
||||
use prometeu_bytecode::{TRAP_TYPE, Value, assembler::assemble};
|
||||
use prometeu_hal::{color::Color, vm_fault::VmFault};
|
||||
use prometeu_vm::{
|
||||
AbiType, BuiltinScalarType, BuiltinValueError, HostContext, HostReturn,
|
||||
LogicalFrameEndingReason, NativeInterface, VirtualMachine, lookup_builtin_type,
|
||||
lookup_intrinsic, materialize_builtin_constant,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn all_nine_descriptors_match_registry_storage_and_canonical_defaults() {
|
||||
for (name, id, size, alignment, width, nullable) in [
|
||||
("color", 1, 4, 4, 1, false),
|
||||
("vec2", 2, 16, 8, 2, false),
|
||||
("pixel", 3, 24, 8, 3, false),
|
||||
("Int32", 0x10, 4, 4, 1, false),
|
||||
("Int64", 0x11, 8, 8, 1, false),
|
||||
("Float", 0x12, 8, 8, 1, false),
|
||||
("Bool", 0x13, 1, 1, 1, false),
|
||||
("String", 0x14, size_of::<StringSlot>(), align_of::<StringSlot>(), 1, true),
|
||||
("Dynamic", 0x15, size_of::<DynamicSlot>(), align_of::<DynamicSlot>(), 2, true),
|
||||
] {
|
||||
let meta = lookup_builtin_type(name, 1).unwrap();
|
||||
assert_eq!(meta.id, id);
|
||||
let layout = meta.storage_layout().unwrap();
|
||||
assert_eq!(layout, BuiltinId(id).layout().unwrap());
|
||||
assert_eq!(
|
||||
(layout.size, layout.alignment, layout.slots, layout.nullable()),
|
||||
(size, alignment, width, nullable)
|
||||
);
|
||||
assert_eq!(u32::from(meta.flat_slot_width), width);
|
||||
assert_eq!(meta.flatten_layout().len(), width as usize);
|
||||
meta.validate_flat_values(layout.default_values()).unwrap();
|
||||
assert!(lookup_builtin_type(name, 2).is_none());
|
||||
}
|
||||
for alias in [
|
||||
"Int",
|
||||
"Number",
|
||||
"Void",
|
||||
"Null",
|
||||
"int",
|
||||
"float",
|
||||
"bool",
|
||||
"str",
|
||||
"input.pad",
|
||||
"input.touch",
|
||||
"input.button",
|
||||
] {
|
||||
assert!(lookup_builtin_type(alias, 1).is_none(), "{alias}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn primitive_carriers_are_exact_and_only_string_accepts_scalar_null() {
|
||||
let cases = [
|
||||
("Int32", Value::Int32(8)),
|
||||
("Int64", Value::Int64(8)),
|
||||
("Float", Value::Float(8.0)),
|
||||
("Bool", Value::Boolean(true)),
|
||||
("String", Value::String("eight".into())),
|
||||
];
|
||||
for (name, valid) in &cases {
|
||||
let meta = lookup_builtin_type(name, 1).unwrap();
|
||||
meta.validate_flat_values(std::slice::from_ref(valid)).unwrap();
|
||||
for (other_name, invalid) in &cases {
|
||||
if other_name != name {
|
||||
assert!(
|
||||
meta.validate_flat_values(std::slice::from_ref(invalid)).is_err(),
|
||||
"{name} <- {other_name}"
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(meta.validate_flat_values(&[Value::Null]).is_ok(), *name == "String");
|
||||
}
|
||||
let dynamic = lookup_builtin_type("Dynamic", 1).unwrap();
|
||||
dynamic.validate_flat_values(&[Value::Null, Value::Null]).unwrap();
|
||||
assert!(dynamic.validate_flat_values(&[Value::Null]).is_err());
|
||||
assert_eq!(
|
||||
dynamic.validate_flat_values(&[Value::Int32(0), Value::Int32(8)]),
|
||||
Err(BuiltinValueError::RegistryRequired)
|
||||
);
|
||||
assert_eq!(
|
||||
dynamic.validate_flat_values(&[Value::Null, Value::Int32(8)]),
|
||||
Err(BuiltinValueError::RegistryRequired)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_range_and_aggregate_component_carriers_do_not_narrow() {
|
||||
let color = lookup_builtin_type("color", 1).unwrap();
|
||||
for raw in [0, 0x11223344, u32::MAX as i64] {
|
||||
color.validate_flat_values(&[Value::Int64(raw)]).unwrap();
|
||||
}
|
||||
for value in [
|
||||
Value::Int32(1),
|
||||
Value::Int64(-1),
|
||||
Value::Int64(u32::MAX as i64 + 1),
|
||||
Value::Float(1.0),
|
||||
Value::Null,
|
||||
] {
|
||||
assert!(color.validate_flat_values(&[value]).is_err());
|
||||
}
|
||||
let pixel = lookup_builtin_type("pixel", 1).unwrap();
|
||||
let wide = [Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(u32::MAX as i64)];
|
||||
pixel.validate_flat_values(&wide).unwrap();
|
||||
assert!(
|
||||
pixel.validate_flat_values(&[wide[2].clone(), wide[1].clone(), wide[0].clone()]).is_err()
|
||||
);
|
||||
for (field, slot) in [("x", 0), ("y", 1), ("color", 2)] {
|
||||
assert_eq!(pixel.field(field).unwrap().start_slot, slot);
|
||||
}
|
||||
let vec2 = lookup_builtin_type("vec2", 1).unwrap();
|
||||
vec2.validate_flat_values(&[Value::Float(1.25), Value::Float(-2.5)]).unwrap();
|
||||
assert!(vec2.validate_flat_values(&[Value::Int64(1), Value::Float(2.0)]).is_err());
|
||||
assert_eq!(
|
||||
materialize_builtin_constant("color", "red", 1).unwrap().unwrap(),
|
||||
vec![Value::Int64(Color::RED.raw() as i64)]
|
||||
);
|
||||
}
|
||||
|
||||
struct NoSyscalls;
|
||||
impl NativeInterface for NoSyscalls {
|
||||
fn syscall(
|
||||
&mut self,
|
||||
_: u32,
|
||||
_: &[Value],
|
||||
_: &mut HostReturn,
|
||||
_: &mut HostContext,
|
||||
) -> Result<(), VmFault> {
|
||||
panic!("unexpected syscall")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intrinsic_boundaries_reject_implicit_carrier_conversions_in_execution() {
|
||||
for source in [
|
||||
"PUSH_I32 1\nINTRINSIC 0x1100", // color.from_raw requires Int64
|
||||
"PUSH_I32 1\nINTRINSIC 0x1106", // color.alpha requires the color carrier
|
||||
"PUSH_I64 1\nPUSH_F64 f64:0x4000000000000000\nINTRINSIC 0x1001", // Float/Float required
|
||||
"PUSH_I64 -1\nINTRINSIC 0x1106", // out-of-range color
|
||||
] {
|
||||
let mut vm = VirtualMachine::new(assemble(source).unwrap(), vec![]);
|
||||
vm.prepare_call("0").expect("prepare entry call");
|
||||
let report = vm.run_budget(100, &mut NoSyscalls, &mut HostContext::new(None)).unwrap();
|
||||
assert!(
|
||||
matches!(report.reason, LogicalFrameEndingReason::Trap(trap) if trap.code == TRAP_TYPE),
|
||||
"{source}"
|
||||
);
|
||||
}
|
||||
let hold = lookup_intrinsic("input.pad.up", "hold", 1).unwrap();
|
||||
assert_eq!(hold.ret_layout, &[AbiType::Scalar(BuiltinScalarType::Int64)]);
|
||||
assert!(hold.validate_result_values(&[Value::Int32(1)]).is_err());
|
||||
let color = lookup_intrinsic("color", "red", 1).unwrap();
|
||||
assert!(color.validate_result_values(&[Value::Int64(-1)]).is_err());
|
||||
}
|
||||
@@ -31,7 +31,7 @@ fn vm_rejects_legacy_input_syscall_id() {
|
||||
let mut vm = VirtualMachine::new(rom, vec![]);
|
||||
let mut native = NoopNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
vm.prepare_call("0");
|
||||
vm.prepare_call("0").expect("prepare entry call");
|
||||
vm.set_capabilities(prometeu_hal::syscalls::caps::ALL);
|
||||
|
||||
let report = vm.run_budget(100, &mut native, &mut ctx).expect("VM run failed");
|
||||
|
||||
Reference in New Issue
Block a user