diff --git a/crates/console/prometeu-bytecode/src/abi.rs b/crates/console/prometeu-bytecode/src/abi.rs index 9cd65b3..2746ae2 100644 --- a/crates/console/prometeu-bytecode/src/abi.rs +++ b/crates/console/prometeu-bytecode/src/abi.rs @@ -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. diff --git a/crates/console/prometeu-bytecode/src/assembler.rs b/crates/console/prometeu-bytecode/src/assembler.rs index dd98869..b1e50c8 100644 --- a/crates/console/prometeu-bytecode/src/assembler.rs +++ b/crates/console/prometeu-bytecode/src/assembler.rs @@ -360,6 +360,34 @@ pub fn assemble(src: &str) -> Result, 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())), } } diff --git a/crates/console/prometeu-bytecode/src/decoder.rs b/crates/console/prometeu-bytecode/src/decoder.rs index 4366c48..26e8293 100644 --- a/crates/console/prometeu-bytecode/src/decoder.rs +++ b/crates/console/prometeu-bytecode/src/decoder.rs @@ -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, 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, 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 { diff --git a/crates/console/prometeu-bytecode/src/disassembler.rs b/crates/console/prometeu-bytecode/src/disassembler.rs index b91ebfd..d888eaa 100644 --- a/crates/console/prometeu-bytecode/src/disassembler.rs +++ b/crates/console/prometeu-bytecode/src/disassembler.rs @@ -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()); diff --git a/crates/console/prometeu-bytecode/src/isa/core.rs b/crates/console/prometeu-bytecode/src/isa/core.rs index d1018e4..9a24fcd 100644 --- a/crates/console/prometeu-bytecode/src/isa/core.rs +++ b/crates/console/prometeu-bytecode/src/isa/core.rs @@ -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: diff --git a/crates/console/prometeu-bytecode/src/lib.rs b/crates/console/prometeu-bytecode/src/lib.rs index 9b316d9..ab087b1 100644 --- a/crates/console/prometeu-bytecode/src/lib.rs +++ b/crates/console/prometeu-bytecode/src/lib.rs @@ -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; diff --git a/crates/console/prometeu-bytecode/src/load_limits.rs b/crates/console/prometeu-bytecode/src/load_limits.rs new file mode 100644 index 0000000..0f6aa36 --- /dev/null +++ b/crates/console/prometeu-bytecode/src/load_limits.rs @@ -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, + pub offset: Option, + pub record: Option, + pub member: Option, + pub id_domain: Option, + pub referenced_id: Option, + pub function: Option, + pub pc: Option, + pub resource: Option, + pub requested: Option, + pub limit: Option, +} +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) -> 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( + &mut self, + count: usize, + r: LoadResource, + ) -> Result, LoadFailure> { + let bytes = count + .checked_mul(std::mem::size_of::()) + .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::() 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( + &mut self, + f: impl FnOnce(&mut Self) -> Result, + ) -> Result { + 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 + } +} diff --git a/crates/console/prometeu-bytecode/src/opcode.rs b/crates/console/prometeu-bytecode/src/opcode.rs index 9aedf2d..afa85d8 100644 --- a/crates/console/prometeu-bytecode/src/opcode.rs +++ b/crates/console/prometeu-bytecode/src/opcode.rs @@ -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 for OpCode { @@ -259,6 +275,14 @@ impl TryFrom 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, } } } diff --git a/crates/console/prometeu-bytecode/src/opcode_spec.rs b/crates/console/prometeu-bytecode/src/opcode_spec.rs index 43a4997..1ce7e0f 100644 --- a/crates/console/prometeu-bytecode/src/opcode_spec.rs +++ b/crates/console/prometeu-bytecode/src/opcode_spec.rs @@ -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, // slots popped + /// None requires Registry resolution; it never means zero slots. + pub pushes: Option, // 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, diff --git a/crates/console/prometeu-bytecode/src/registry.rs b/crates/console/prometeu-bytecode/src/registry.rs new file mode 100644 index 0000000..71b5c0d --- /dev/null +++ b/crates/console/prometeu-bytecode/src/registry.rs @@ -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, +} +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Signature { + pub parameters: Vec, + pub results: Vec, +} +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct MethodDecl { + pub name: Option, + pub signature: Signature, +} +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TypeKind { + Builtin(BuiltinId), + Struct(Vec), + Array(TypeId), + Trait(Vec), +} +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TypeDecl { + pub name: Option, + pub kind: TypeKind, +} +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct FunctionDecl { + pub name: Option, + 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, +} +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct RegistryDeclarations { + pub types: Vec, + pub functions: Vec, + pub implementations: Vec, +} + +pub type StringSlot = Option>; +#[derive(Debug, Clone)] +pub struct DynamicSlot { + pub type_id: Option, + 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 { + 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::(), align_of::(), 1, O::NativeString, D::Null) + } + Self::DYNAMIC => { + (size_of::(), align_of::(), 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, + /// Sorted offsets for fields requiring guest tracing or native ownership handling. + pub owned_fields: Vec, +} +#[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, + pub array: Option, +} +#[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, + functions: Vec, + // 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 { + 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 { + budget.transaction(|budget| Self::resolve_inner(declarations, code_len, budget)) + } + fn resolve_inner( + d: RegistryDeclarations, + code_len: usize, + b: &mut LoadBudget, + ) -> Result { + 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 { + n.checked_add(a - 1).map(|v| v & !(a - 1)).ok_or_else(overflow) +} +fn width( + ids: impl Iterator, + layouts: &[TypeLayout], + b: &mut LoadBudget, +) -> Result { + 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, 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::(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(v: &Vec, b: &mut LoadBudget) -> Result<(), E> { + b.charge( + Q::RegistryBytes, + (v.capacity() as u64).checked_mul(size_of::() as u64).ok_or_else(overflow)?, + ) +} +fn charge_name(n: &Option, b: &mut LoadBudget) -> Result<(), E> { + if let Some(n) = n { + b.charge(Q::RegistryBytes, n.capacity() as u64)?; + } + Ok(()) +} +fn charge_named(v: &Vec, 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::() 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(()) +} diff --git a/crates/console/prometeu-bytecode/src/registry_codec.rs b/crates/console/prometeu-bytecode/src/registry_codec.rs new file mode 100644 index 0000000..ba31da4 --- /dev/null +++ b/crates/console/prometeu-bytecode/src/registry_codec.rs @@ -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>; 8], +} +impl<'a> PbxEnvelope<'a> { + pub fn section(&self, kind: u32) -> Option> { + self.sections.get(kind as usize).copied().flatten() + } + pub fn parse(bytes: &'a [u8], b: &mut LoadBudget) -> Result { + b.transaction(|b| Self::parse_inner(bytes, b)) + } + fn parse_inner(bytes: &'a [u8], b: &mut LoadBudget) -> Result { + 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 { + 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 { + Ok(self.take(1)?[0]) + } + fn u32(&mut self) -> Result { + 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(&mut self, resource: Q, min_bytes: usize) -> Result<(Vec, 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, 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::(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, 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 { + 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, 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, 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, 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, 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, + pub functions: Vec, + pub implementations: Vec, +} +/// 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 { + 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>, + 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) -> 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, 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; diff --git a/crates/console/prometeu-bytecode/src/registry_codec/payloads.rs b/crates/console/prometeu-bytecode/src/registry_codec/payloads.rs new file mode 100644 index 0000000..4b51b2c --- /dev/null +++ b/crates/console/prometeu-bytecode/src/registry_codec/payloads.rs @@ -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 { + 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> { + self.envelope.section(kind) + } + /// Re-encode validated tables in canonical directory order; debug absence is preserved. + pub fn encode(&self, budget: &mut LoadBudget) -> Result, 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 { + Ok(u16::from_le_bytes(self.take(2)?.try_into().unwrap())) + } + fn raw_count(&mut self, minimum: usize) -> Result { + 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 { + 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( + items: &mut [T], + mut cmp: impl FnMut(&T, &T) -> Result, +) -> Result<(), E> { + fn sift( + items: &mut [T], + mut root: usize, + cmp: &mut impl FnMut(&T, &T) -> Result, + ) -> 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(()) +} diff --git a/crates/console/prometeu-bytecode/src/registry_instruction.rs b/crates/console/prometeu-bytecode/src/registry_instruction.rs new file mode 100644 index 0000000..1a871de --- /dev/null +++ b/crates/console/prometeu-bytecode/src/registry_instruction.rs @@ -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, 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 })) + } +} diff --git a/crates/console/prometeu-bytecode/src/value.rs b/crates/console/prometeu-bytecode/src/value.rs index 40ca770..486fb63 100644 --- a/crates/console/prometeu-bytecode/src/value.rs +++ b/crates/console/prometeu-bytecode/src/value.rs @@ -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(), } } diff --git a/crates/console/prometeu-bytecode/tests/registry_isa.rs b/crates/console/prometeu-bytecode/tests/registry_isa.rs new file mode 100644 index 0000000..f0c4a3e --- /dev/null +++ b/crates/console/prometeu-bytecode/tests/registry_isa.rs @@ -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); +} diff --git a/crates/console/prometeu-bytecode/tests/registry_v1.rs b/crates/console/prometeu-bytecode/tests/registry_v1.rs new file mode 100644 index 0000000..b7c97d5 --- /dev/null +++ b/crates/console/prometeu-bytecode/tests/registry_v1.rs @@ -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::resolve(d, 100, &mut budget()) +} +fn hex(s: &str) -> Vec { + 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 { + 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 { + 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![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::(), align_of::())); + assert!(s.nullable()); + let d = BuiltinId::DYNAMIC.layout().unwrap(); + assert_eq!((d.size, d.alignment), (size_of::(), align_of::())); + 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 { + 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::(),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); +} diff --git a/crates/console/prometeu-hal/src/host_return.rs b/crates/console/prometeu-hal/src/host_return.rs index b38bd7f..f8a0847 100644 --- a/crates/console/prometeu-hal/src/host_return.rs +++ b/crates/console/prometeu-hal/src/host_return.rs @@ -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, + remaining: usize, + overflowed: bool, } impl<'a> HostReturn<'a> { - pub fn new(stack: &'a mut Vec) -> Self { - Self { stack } + pub fn new(stack: &'a mut Vec, 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); } } diff --git a/crates/console/prometeu-system/src/services/vm_runtime/tick.rs b/crates/console/prometeu-system/src/services/vm_runtime/tick.rs index ac2104e..4026801 100644 --- a/crates/console/prometeu-system/src/services/vm_runtime/tick.rs +++ b/crates/console/prometeu-system/src/services/vm_runtime/tick.rs @@ -77,7 +77,13 @@ impl VirtualMachineRuntime { platform: &mut dyn RuntimePlatform, ) -> Option { 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; } diff --git a/crates/console/prometeu-vm/src/builtins.rs b/crates/console/prometeu-vm/src/builtins.rs index 5bafab6..2fd457c 100644 --- a/crates/console/prometeu-vm/src/builtins.rs +++ b/crates/console/prometeu-vm/src/builtins.rs @@ -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 { + 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) { 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; @@ -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) { @@ -967,20 +769,26 @@ fn expect_float_arg(args: &[Value], index: usize) -> Result Ok(*value), + _ => Err(IntrinsicExecutionError::TypeMismatch { + index, + expected: AbiType::Scalar(BuiltinScalarType::Float), + }), + } } fn expect_int_arg(args: &[Value], index: usize) -> Result { 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, -) -> Result, IntrinsicExecutionError> { - Ok(vec![Value::Int64(PAD_HANDLE)]) -} - -fn input_touch( - _args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - Ok(vec![Value::Int64(TOUCH_HANDLE)]) -} - -fn input_pad_up( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "up")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE)]) -} - -fn input_pad_down( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "down")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 1)]) -} - -fn input_pad_left( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "left")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 2)]) -} - -fn input_pad_right( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "right")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 3)]) -} - -fn input_pad_a( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "a")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 4)]) -} - -fn input_pad_b( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "b")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 5)]) -} - -fn input_pad_x( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "x")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 6)]) -} - -fn input_pad_y( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "y")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 7)]) -} - -fn input_pad_l( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "l")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 8)]) -} - -fn input_pad_r( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "r")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 9)]) -} - -fn input_pad_start( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "start")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 10)]) -} - -fn input_pad_select( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_pad_handle(args, "select")?; - Ok(vec![Value::Int64(PAD_BUTTON_BASE + 11)]) -} - -fn input_touch_button( - args: &[Value], - _ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - expect_touch_handle(args, "button")?; - Ok(vec![Value::Int64(TOUCH_BUTTON_CARRIER)]) -} - -fn input_touch_x( - args: &[Value], - ctx: &mut HostContext<'_>, -) -> Result, 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, 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, IntrinsicExecutionError> { - let button = resolve_button(args, "pressed", ctx)?; - Ok(vec![Value::Boolean(button.pressed)]) -} - -fn input_button_released( - args: &[Value], - ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - let button = resolve_button(args, "released", ctx)?; - Ok(vec![Value::Boolean(button.released)]) -} - -fn input_button_down( - args: &[Value], - ctx: &mut HostContext<'_>, -) -> Result, IntrinsicExecutionError> { - let button = resolve_button(args, "down", ctx)?; - Ok(vec![Value::Boolean(button.down)]) -} - -fn input_button_hold( - args: &[Value], - ctx: &mut HostContext<'_>, -) -> Result, 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 { - 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 { - 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, 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) +} diff --git a/crates/console/prometeu-vm/src/heap.rs b/crates/console/prometeu-vm/src/heap.rs index bc361fa..b55477d 100644 --- a/crates/console/prometeu-vm/src/heap.rs +++ b/crates/console/prometeu-vm/src/heap.rs @@ -1,51 +1,64 @@ +mod dynamic_box; use crate::call_frame::CallFrame; use crate::object::{ObjectHeader, ObjectKind}; +use crate::resource_budget::{ + BudgetVec, Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q, +}; +use crate::typed_storage::TypedStorage; +use crate::value_contract::{self, ValueError, ValueGroup}; +use dynamic_box::DynamicBox; +use prometeu_bytecode::registry::{BuiltinId, Registry, TypeId, TypeKind}; use prometeu_bytecode::{HeapRef, Value}; use std::sync::Arc; use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; /// Internal stored object: header plus opaque payload bytes. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct StoredObject { pub header: ObjectHeader, /// Raw payload bytes for byte-oriented kinds (e.g., String, Bytes). - pub payload: Vec, + pub payload: BudgetVec, /// Optional typed elements for `ObjectKind::Array`. /// When present, `header.payload_len` must equal `array_elems.len() as u32`. - pub array_elems: Option>, + pub array_elems: Option>, /// Optional captured environment for `ObjectKind::Closure`. /// Invariants for closures: /// - `header.payload_len == 8` and `payload` bytes are `[fn_id: u32][env_len: u32]` (LE). /// - The actual `env_len` Value slots are stored here (not in `payload`) so /// they stay directly GC-visible. The GC must traverse exactly `env_len` /// entries from this slice, in order. - pub closure_env: Option>, + pub closure_env: Option>, /// Optional coroutine data for `ObjectKind::Coroutine`. pub coroutine: Option, + /// Registry-backed compact Struct/Array fields, never raw Value elements. + typed: Option, + /// Immutable native Value slots; not accessible through collection helpers. + dynamic_box: Option, } -impl StoredObject { - /// Returns the approximate memory footprint of this object in bytes. - pub fn bytes(&self) -> usize { - let mut total = std::mem::size_of::(); - total += self.payload.capacity(); - - if let Some(elems) = &self.array_elems { - total += std::mem::size_of::>(); - total += elems.capacity() * std::mem::size_of::(); - } - if let Some(env) = &self.closure_env { - total += std::mem::size_of::>(); - total += env.capacity() * std::mem::size_of::(); - } - if let Some(coro) = &self.coroutine { - total += std::mem::size_of::(); - total += coro.stack.capacity() * std::mem::size_of::(); - total += coro.frames.capacity() * std::mem::size_of::(); - } - - total +/// One native object allocation, distinct from auxiliary handle-table storage. +#[derive(Debug)] +pub struct StoredAllocation(BudgetVec); +impl std::ops::Deref for StoredAllocation { + type Target = StoredObject; + fn deref(&self) -> &StoredObject { + &self.0[0] + } +} +impl std::ops::DerefMut for StoredAllocation { + fn deref_mut(&mut self) -> &mut StoredObject { + &mut self.0[0] + } +} +impl StoredAllocation { + fn bytes(&self) -> usize { + (self.0.accounted_bytes() + + self.payload.accounted_bytes() + + self.array_elems.as_ref().map_or(0, BudgetVec::accounted_bytes) + + self.closure_env.as_ref().map_or(0, BudgetVec::accounted_bytes) + + self.typed.as_ref().map_or(0, TypedStorage::bytes) + + self.dynamic_box.as_ref().map_or(0, DynamicBox::bytes)) as usize } } @@ -60,22 +73,27 @@ pub enum CoroutineState { } /// Stored payload for coroutine objects. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct CoroutineData { pub pc: usize, pub state: CoroutineState, pub wake_tick: u64, - pub stack: Vec, - pub frames: Vec, + pub stack: BudgetVec, + pub frames: BudgetVec, } -/// Simple vector-backed heap. No GC or compaction. -#[derive(Debug, Default, Clone)] +/// Non-moving, generation-checked heap with safepoint mark/sweep. +#[derive(Debug)] pub struct Heap { // Tombstone-aware store: Some(obj) = live allocation; None = freed slot. - objects: Vec>, + objects: BudgetVec>, + // Same index as objects. Maximum-generation reclaimed slots are never reused. + generations: BudgetVec, // Reclaimed slots available for deterministic reuse (LIFO). - free_list: Vec, + free_list: BudgetVec, + + budget: RuntimeBudget, + slots_charge: Reservation, /// Total bytes currently used by all objects in the heap. pub used_bytes: Arc, @@ -84,120 +102,203 @@ pub struct Heap { } impl Heap { + #[cfg(test)] + fn set_test_array(&mut self, r: HeapRef, values: &[Value]) { + let data = BudgetVec::from_slice(&self.budget, Q::GuestBytes, values).unwrap(); + let obj = self.objects[r.index as usize].as_mut().unwrap(); + let old = obj.bytes(); + obj.array_elems = Some(data); + obj.header.payload_len = values.len() as u32; + let new = obj.bytes(); + self.used_bytes.fetch_sub(old, Ordering::Relaxed); + self.used_bytes.fetch_add(new, Ordering::Relaxed); + } + #[cfg(test)] + pub(crate) fn table_capacity_bytes(&self) -> u64 { + self.objects.accounted_bytes() + + self.generations.accounted_bytes() + + self.free_list.accounted_bytes() + } + + #[cfg(test)] + pub(crate) fn execution_capacity_bytes(&self) -> u64 { + self.objects + .iter() + .flatten() + .filter_map(|object| object.coroutine.as_ref()) + .map(|co| { + (co.stack.capacity() * size_of::() + + co.frames.capacity() * size_of::()) as u64 + }) + .sum() + } + + #[cfg(test)] pub fn new() -> Self { + Self::with_budget(&RuntimeBudget::default()) + } + + pub fn with_budget(budget: &RuntimeBudget) -> Self { Self { - objects: Vec::new(), - free_list: Vec::new(), + objects: BudgetVec::new(budget, Q::AuxiliaryBytes), + generations: BudgetVec::new(budget, Q::AuxiliaryBytes), + free_list: BudgetVec::new(budget, Q::AuxiliaryBytes), + budget: budget.clone(), + slots_charge: budget.empty_charge(Q::HandleSlots), used_bytes: Arc::new(AtomicUsize::new(0)), allocation_count: Arc::new(AtomicU64::new(0)), } } - fn insert_object(&mut self, obj: StoredObject) -> HeapRef { - self.used_bytes.fetch_add(obj.bytes(), Ordering::Relaxed); - self.allocation_count.fetch_add(1, Ordering::Relaxed); - if let Some(idx) = self.free_list.pop() { - debug_assert!(self.objects.get(idx).is_some_and(|slot| slot.is_none())); - self.objects[idx] = Some(obj); - HeapRef(idx as u32) - } else { - let idx = self.objects.len(); - self.objects.push(Some(obj)); - HeapRef(idx as u32) + fn check_slot(&self) -> Result<(), ResourceFailure> { + if self.free_list.is_empty() { + self.budget.check(Q::HandleSlots, self.objects.len() as u64 + 1)?; } + Ok(()) } - /// Allocate a new object with the given kind and raw payload bytes. - /// Returns an opaque `HeapRef` handle. - #[cfg(test)] - pub fn allocate_object(&mut self, kind: ObjectKind, payload: &[u8]) -> HeapRef { - let header = ObjectHeader::new(kind, payload.len() as u32); - let obj = StoredObject { - header, - payload: payload.to_vec(), - array_elems: None, - closure_env: None, - coroutine: None, + fn insert_object(&mut self, obj: StoredObject) -> Result { + let mut storage = BudgetVec::new(&self.budget, Q::GuestBytes); + storage.push(obj)?; + let obj = StoredAllocation(storage); + let bytes = obj.bytes(); + let handle = if let Some(&idx) = self.free_list.last() { + self.free_list.pop(); + debug_assert!(self.objects[idx].is_none()); + self.objects[idx] = Some(obj); + HeapRef { index: idx as u32, generation: self.generations[idx] } + } else { + let slot_charge = self.budget.reserve(Q::HandleSlots, 1)?; + // Stage all three capacities. Failure drops the candidate buffers and + // object, without publishing a slot or retaining partial table growth. + let objects = self.objects.prepare_growth(1)?; + let generations = self.generations.prepare_growth(1)?; + let free = + self.free_list.prepare_growth(self.objects.len() + 1 - self.free_list.len())?; + self.objects.commit_growth(objects); + self.generations.commit_growth(generations); + self.free_list.commit_growth(free); + let idx = self.objects.len(); + self.objects.push(Some(obj)).expect("reserved handle slot"); + self.generations.push(1).expect("reserved generation slot"); + self.slots_charge.absorb(slot_charge); + HeapRef { index: idx as u32, generation: 1 } }; - self.insert_object(obj) + self.used_bytes.fetch_add(bytes, Ordering::Relaxed); + self.allocation_count.fetch_add(1, Ordering::Relaxed); + Ok(handle) } - /// Allocate a new `Array` object with the given `Value` elements. - /// `payload_len` stores the element count; raw `payload` bytes are empty. #[cfg(test)] - pub fn allocate_array(&mut self, elements: Vec) -> HeapRef { - let header = ObjectHeader::new(ObjectKind::Array, elements.len() as u32); - let obj = StoredObject { - header, - payload: Vec::new(), - array_elems: Some(elements), - closure_env: None, - coroutine: None, - }; - self.insert_object(obj) - } - - /// Allocate a new `Closure` object with the given function id and captured environment. - /// Layout: - /// payload bytes: [fn_id: u32][env_len: u32] - /// env slots: stored out-of-line in `closure_env` for GC visibility - pub fn alloc_closure(&mut self, fn_id: u32, env_values: &[Value]) -> HeapRef { - let mut payload = Vec::with_capacity(8); - payload.extend_from_slice(&fn_id.to_le_bytes()); - let env_len = env_values.len() as u32; - payload.extend_from_slice(&env_len.to_le_bytes()); - - let header = ObjectHeader::new(ObjectKind::Closure, payload.len() as u32); - let obj = StoredObject { - header, + pub fn allocate_object( + &mut self, + kind: ObjectKind, + payload: &[u8], + ) -> Result { + self.check_slot()?; + let payload_len = self.payload_len(payload.len())?; + let payload = BudgetVec::from_slice(&self.budget, Q::GuestBytes, payload)?; + self.insert_object(StoredObject { + header: ObjectHeader::new(kind, payload_len), payload, array_elems: None, - closure_env: Some(env_values.to_vec()), + closure_env: None, coroutine: None, - }; - self.insert_object(obj) + typed: None, + dynamic_box: None, + }) + } + + #[cfg(test)] + pub fn allocate_array(&mut self, elements: Vec) -> Result { + self.check_slot()?; + let payload_len = self.payload_len(elements.len())?; + let elems = BudgetVec::from_slice(&self.budget, Q::GuestBytes, &elements)?; + self.insert_object(StoredObject { + header: ObjectHeader::new(ObjectKind::Array, payload_len), + payload: BudgetVec::new(&self.budget, Q::GuestBytes), + array_elems: Some(elems), + closure_env: None, + coroutine: None, + typed: None, + dynamic_box: None, + }) + } + + fn payload_len(&self, len: usize) -> Result { + u32::try_from(len).map_err(|_| { + self.budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, len as u64) + }) + } + + pub fn alloc_closure( + &mut self, + fn_id: u32, + env_values: &[Value], + ) -> Result { + self.check_slot()?; + let env_len = self.payload_len(env_values.len())?; + let mut bytes = [0; 8]; + bytes[..4].copy_from_slice(&fn_id.to_le_bytes()); + bytes[4..].copy_from_slice(&env_len.to_le_bytes()); + let payload = BudgetVec::from_slice(&self.budget, Q::GuestBytes, &bytes)?; + let env = BudgetVec::from_slice(&self.budget, Q::GuestBytes, env_values)?; + self.insert_object(StoredObject { + header: ObjectHeader::new(ObjectKind::Closure, 8), + payload, + array_elems: None, + closure_env: Some(env), + coroutine: None, + typed: None, + dynamic_box: None, + }) } - /// Allocate a new `Coroutine` object with provided initial data. - /// `payload_len` is 0; stack and frames are stored out-of-line for GC visibility. pub fn allocate_coroutine( &mut self, pc: usize, state: CoroutineState, wake_tick: u64, - stack: Vec, - frames: Vec, - ) -> HeapRef { - let header = ObjectHeader::new(ObjectKind::Coroutine, 0); - let obj = StoredObject { - header, - payload: Vec::new(), + stack: BudgetVec, + frames: BudgetVec, + ) -> Result { + self.check_slot()?; + self.insert_object(StoredObject { + header: ObjectHeader::new(ObjectKind::Coroutine, 0), + payload: BudgetVec::new(&self.budget, Q::GuestBytes), array_elems: None, closure_env: None, coroutine: Some(CoroutineData { pc, state, wake_tick, stack, frames }), - }; - self.insert_object(obj) + typed: None, + dynamic_box: None, + }) } /// Returns true if this handle refers to an allocated object. pub fn is_valid(&self, r: HeapRef) -> bool { - let idx = r.0 as usize; + let idx = r.index as usize; if idx >= self.objects.len() { return false; } - self.objects[idx].is_some() + r.generation != 0 && self.generations[idx] == r.generation && self.objects[idx].is_some() } /// Returns a shared reference to the coroutine data for the given handle, if it is a Coroutine. #[cfg(test)] pub fn coroutine_data(&self, r: HeapRef) -> Option<&CoroutineData> { - let idx = r.0 as usize; + if !self.is_valid(r) { + return None; + } + let idx = r.index as usize; self.objects.get(idx).and_then(|slot| slot.as_ref()).and_then(|obj| obj.coroutine.as_ref()) } /// Returns a mutable reference to the coroutine data for the given handle, if it is a Coroutine. pub fn coroutine_data_mut(&mut self, r: HeapRef) -> Option<&mut CoroutineData> { - let idx = r.0 as usize; + if !self.is_valid(r) { + return None; + } + let idx = r.index as usize; self.objects .get_mut(idx) .and_then(|slot| slot.as_mut()) @@ -206,67 +307,26 @@ impl Heap { /// Get immutable access to an object's header by handle. pub fn header(&self, r: HeapRef) -> Option<&ObjectHeader> { - self.objects.get(r.0 as usize).and_then(|slot| slot.as_ref()).map(|o| &o.header) + if !self.is_valid(r) { + return None; + } + self.objects.get(r.index as usize).and_then(|slot| slot.as_ref()).map(|o| &o.header) } /// Internal: get mutable access to an object's header by handle. fn header_mut(&mut self, r: HeapRef) -> Option<&mut ObjectHeader> { - self.objects.get_mut(r.0 as usize).and_then(|slot| slot.as_mut()).map(|o| &mut o.header) + if !self.is_valid(r) { + return None; + } + self.objects.get_mut(r.index as usize).and_then(|slot| slot.as_mut()).map(|o| &mut o.header) } - // Internal: list inner `HeapRef` children of an object without allocating. - // Note: GC mark no longer uses this helper; kept for potential diagnostics. - // fn children_of(&self, r: HeapRef) -> Box + '_> { - // let idx = r.0 as usize; - // if let Some(Some(o)) = self.objects.get(idx) { - // match o.header.kind { - // ObjectKind::Array => { - // let it = o - // .array_elems - // .as_deref() - // .into_iter() - // .flat_map(|slice| slice.iter()) - // .filter_map(|val| if let Value::HeapRef(h) = val { Some(*h) } else { None }); - // return Box::new(it); - // } - // ObjectKind::Closure => { - // // Read env_len from payload; traverse exactly that many entries. - // debug_assert_eq!(o.header.kind, ObjectKind::Closure); - // debug_assert_eq!(o.payload.len(), 8, "closure payload metadata must be 8 bytes"); - // let mut nbytes = [0u8; 4]; - // nbytes.copy_from_slice(&o.payload[4..8]); - // let env_len = u32::from_le_bytes(nbytes) as usize; - // let it = o - // .closure_env - // .as_deref() - // .map(|slice| { - // debug_assert_eq!(slice.len(), env_len, "closure env length must match encoded env_len"); - // &slice[..env_len] - // }) - // .into_iter() - // .flat_map(|slice| slice.iter()) - // .filter_map(|val| if let Value::HeapRef(h) = val { Some(*h) } else { None }); - // return Box::new(it); - // } - // ObjectKind::Coroutine => { - // if let Some(co) = o.coroutine.as_ref() { - // let it = co - // .stack - // .iter() - // .filter_map(|v| if let Value::HeapRef(h) = v { Some(*h) } else { None }); - // return Box::new(it); - // } - // return Box::new(std::iter::empty()); - // } - // _ => return Box::new(std::iter::empty()), - // } - // } - // Box::new(std::iter::empty()) - // } - /// Read the `fn_id` stored in a closure object. Returns None if kind mismatch or invalid ref. pub fn closure_fn_id(&self, r: HeapRef) -> Option { - let idx = r.0 as usize; + if !self.is_valid(r) { + return None; + } + let idx = r.index as usize; let slot = self.objects.get(idx)?.as_ref()?; if slot.header.kind != ObjectKind::Closure { return None; @@ -283,7 +343,10 @@ impl Heap { /// Get the captured environment slice of a closure. Returns None if kind mismatch or invalid ref. #[cfg(test)] pub fn closure_env_slice(&self, r: HeapRef) -> Option<&[Value]> { - let idx = r.0 as usize; + if !self.is_valid(r) { + return None; + } + let idx = r.index as usize; let slot = self.objects.get(idx)?.as_ref()?; if slot.header.kind != ObjectKind::Closure { return None; @@ -301,94 +364,93 @@ impl Heap { /// Mark phase: starting from the given roots, traverse and set mark bits /// on all reachable objects. Uses an explicit stack to avoid recursion. - pub fn mark_from_roots>(&mut self, roots: I) { - let mut stack: Vec = roots.into_iter().collect(); + #[cfg(test)] + pub fn mark_from_roots>( + &mut self, + roots: I, + ) -> Result<(), ResourceFailure> { + self.mark(roots, false) + } - while let Some(r) = stack.pop() { - if !self.is_valid(r) { - continue; + pub fn mark_session_roots>( + &mut self, + roots: I, + ) -> Result<(), ResourceFailure> { + self.mark(roots, true) + } + + fn enqueue_mark(&mut self, r: HeapRef, stack: &mut BudgetVec) { + if let Some(header) = self.header_mut(r) { + if !header.is_marked() { + // Each live object enters at most once; reserve(len()) bounds the entire traversal. + header.set_marked(true); + stack.push(r).expect("mark worklist reserved for every live object"); } + } + } - // If already marked, skip. - let already_marked = - self.header(r).map(|h: &ObjectHeader| h.is_marked()).unwrap_or(false); - if already_marked { - continue; - } + fn children(&self, r: HeapRef) -> &[Value] { + let obj = self.objects[r.index as usize].as_ref().expect("validated worklist handle"); + match obj.header.kind { + ObjectKind::Array => obj.array_elems.as_deref().unwrap_or(&[]), + ObjectKind::Closure => obj.closure_env.as_deref().unwrap_or(&[]), + ObjectKind::DynamicBox => obj.dynamic_box.as_ref().map_or(&[], DynamicBox::values), + ObjectKind::Coroutine => obj.coroutine.as_ref().map_or(&[], |co| co.stack.as_slice()), + _ => &[], + } + } - // Set mark bit. - if let Some(h) = self.header_mut(r) { - h.set_marked(true); - } - - // Push children by scanning payload directly (no intermediate Vec allocs). - let idx = r.0 as usize; - if let Some(Some(obj)) = self.objects.get(idx) { - match obj.header.kind { - ObjectKind::Array => { - if let Some(elems) = obj.array_elems.as_ref() { - for val in elems.iter() { - if let Value::HeapRef(child) = val - && self.is_valid(*child) - { - let marked = self - .header(*child) - .map(|h: &ObjectHeader| h.is_marked()) - .unwrap_or(false); - if !marked { - stack.push(*child); - } - } - } - } - } - ObjectKind::Closure => { - debug_assert_eq!(obj.payload.len(), 8, "closure payload must be 8 bytes"); - let mut nbytes = [0u8; 4]; - nbytes.copy_from_slice(&obj.payload[4..8]); - let env_len = u32::from_le_bytes(nbytes) as usize; - if let Some(env) = obj.closure_env.as_ref() { - debug_assert_eq!( - env.len(), - env_len, - "closure env len must match encoded env_len" - ); - for val in env[..env_len].iter() { - if let Value::HeapRef(child) = val - && self.is_valid(*child) - { - let marked = self - .header(*child) - .map(|h: &ObjectHeader| h.is_marked()) - .unwrap_or(false); - if !marked { - stack.push(*child); - } - } - } - } - } - ObjectKind::Coroutine => { - if let Some(co) = obj.coroutine.as_ref() { - for val in co.stack.iter() { - if let Value::HeapRef(child) = val - && self.is_valid(*child) - { - let marked = self - .header(*child) - .map(|h: &ObjectHeader| h.is_marked()) - .unwrap_or(false); - if !marked { - stack.push(*child); - } - } - } - } - } - _ => {} + fn mark>( + &mut self, + roots: I, + suspended: bool, + ) -> Result<(), ResourceFailure> { + let mut stack = BudgetVec::new(&self.budget, Q::TemporaryBytes); + stack.reserve(self.len())?; // Fail before changing any mark bit. + for root in roots { + self.enqueue_mark(root, &mut stack); + } + if suspended { + for idx in 0..self.objects.len() { + let root = + self.objects[idx].as_ref().and_then(|o| o.coroutine.as_ref()).is_some_and( + |co| matches!(co.state, CoroutineState::Ready | CoroutineState::Sleeping), + ); + if root { + self.enqueue_mark( + HeapRef { index: idx as u32, generation: self.generations[idx] }, + &mut stack, + ); } } } + while let Some(r) = stack.pop() { + let typed_count = self.objects[r.index as usize] + .as_ref() + .unwrap() + .typed + .as_ref() + .map_or(0, TypedStorage::root_count); + for i in 0..typed_count { + let child = self.objects[r.index as usize] + .as_ref() + .unwrap() + .typed + .as_ref() + .unwrap() + .root_at(i); + if let Some(child) = child { + self.enqueue_mark(child, &mut stack); + } + } + for i in 0..self.children(r).len() { + let child = crate::roots::value_heap_ref(&self.children(r)[i]); + if let Some(child) = child { + self.enqueue_mark(child, &mut stack); + } + } + } + Ok(()) } /// Sweep phase: reclaim unmarked objects by turning their slots into @@ -404,7 +466,12 @@ impl Heap { // Unreachable: reclaim by dropping and turning into tombstone. self.used_bytes.fetch_sub(obj.bytes(), Ordering::Relaxed); *slot = None; - self.free_list.push(idx); + if let Some(next) = self.generations[idx].checked_add(1) { + self.generations[idx] = next; + self.free_list + .push(idx) + .expect("recycling capacity reserved with handle slot"); + } } } } @@ -422,32 +489,260 @@ impl Heap { /// Enumerate handles of coroutines that are currently suspended (i.e., not running): /// Ready or Sleeping. These must be treated as GC roots by the runtime so their /// stacks/frames are scanned during mark. - pub fn suspended_coroutine_handles(&self) -> Vec { - let mut out = Vec::new(); - for (idx, slot) in self.objects.iter().enumerate() { - if let Some(obj) = slot - && obj.header.kind == ObjectKind::Coroutine - && let Some(co) = &obj.coroutine - && matches!(co.state, CoroutineState::Ready | CoroutineState::Sleeping) - { - out.push(HeapRef(idx as u32)); - } - } - out + #[cfg(test)] + pub fn suspended_coroutine_handles(&self) -> impl Iterator + '_ { + self.objects.iter().enumerate().filter_map(|(idx, slot)| { + let co = slot.as_ref()?.coroutine.as_ref()?; + matches!(co.state, CoroutineState::Ready | CoroutineState::Sleeping) + .then_some(HeapRef { index: idx as u32, generation: self.generations[idx] }) + }) } } +// Internal consumer seam for PLN-0176 step 18 / AGD-0054. Executable image +// publication and collection instructions are separate integration work. +#[allow(dead_code)] +impl Heap { + fn typed_object(&self, r: HeapRef) -> Result<&TypedStorage, ValueError> { + if r.is_null() { + return Err(ValueError::NullReference); + } + if !self.is_valid(r) { + return Err(ValueError::InvalidHandle); + } + self.objects[r.index as usize].as_ref().unwrap().typed.as_ref().ok_or(ValueError::Type) + } + pub(crate) fn concrete_type( + &self, + r: HeapRef, + registry: &Registry, + ) -> Result { + let storage = self.typed_object(r)?; + if !std::ptr::eq(storage.registry(), registry) { + return Err(ValueError::Type); + } + Ok(storage.type_id()) + } + pub(crate) fn allocate_typed_struct( + &mut self, + registry: Arc, + type_id: TypeId, + ) -> Result { + self.check_slot()?; + let storage = TypedStorage::new_struct(registry, type_id, &self.budget)?; + self.insert_typed(ObjectKind::Struct, 0, storage) + } + pub(crate) fn allocate_typed_array( + &mut self, + registry: Arc, + type_id: TypeId, + count: u32, + ) -> Result { + self.check_slot()?; + let storage = TypedStorage::new_array(registry, type_id, count, &self.budget)?; + self.insert_typed(ObjectKind::Array, count, storage) + } + fn insert_typed( + &mut self, + kind: ObjectKind, + count: u32, + storage: TypedStorage, + ) -> Result { + Ok(self.insert_object(StoredObject { + header: ObjectHeader::new(kind, count), + payload: BudgetVec::new(&self.budget, Q::GuestBytes), + array_elems: None, + closure_env: None, + coroutine: None, + typed: Some(storage), + dynamic_box: None, + })?) + } + pub(crate) fn load_typed( + &self, + registry: &Registry, + r: HeapRef, + index: usize, + ) -> Result { + self.concrete_type(r, registry)?; + let storage = self.typed_object(r)?; + let values = storage.read(index)?; + value_contract::validate(registry, self, storage.slot_type(index)?, values.as_slice())?; + Ok(values) + } + pub(crate) fn store_typed( + &mut self, + registry: &Registry, + r: HeapRef, + index: usize, + values: &[Value], + ) -> Result<(), ValueError> { + self.concrete_type(r, registry)?; + let ty = self.typed_object(r)?.slot_type(index)?; + value_contract::validate(registry, self, ty, values)?; + // No mutation/publication or owner cloning occurs before complete validation. + self.objects[r.index as usize] + .as_mut() + .unwrap() + .typed + .as_mut() + .unwrap() + .write_validated(index, values) + } +} + +#[allow(dead_code)] +impl Heap { + pub(crate) fn allocate_dynamic_box( + &mut self, + registry: Arc, + type_id: TypeId, + values: &[Value], + ) -> Result { + // Validate before reserving anything. The source remains live and borrowed + // through publication, and this path never invokes GC or the scheduler. + validate_box_group(®istry, self, type_id, values)?; + self.check_slot()?; + let payload_len = self.payload_len(values.len())?; + let dynamic_box = DynamicBox::new(registry, type_id, values, &self.budget)?; + Ok(self.insert_object(StoredObject { + header: ObjectHeader::new(ObjectKind::DynamicBox, payload_len), + payload: BudgetVec::new(&self.budget, Q::GuestBytes), + array_elems: None, + closure_env: None, + coroutine: None, + typed: None, + dynamic_box: Some(dynamic_box), + })?) + } + pub(crate) fn dynamic_box_values( + &self, + registry: &Registry, + r: HeapRef, + type_id: TypeId, + ) -> Result<&[Value], ValueError> { + if r.is_null() { + return Err(ValueError::Type); + } + if !self.is_valid(r) { + return Err(ValueError::InvalidHandle); + } + let object = self.objects[r.index as usize].as_ref().unwrap(); + if object.header.kind != ObjectKind::DynamicBox { + return Err(ValueError::Type); + } + let values = + object.dynamic_box.as_ref().ok_or(ValueError::Type)?.typed_values(registry, type_id)?; + if object.header.payload_len as usize != values.len() { + return Err(ValueError::Type); + } + validate_box_group(registry, self, type_id, values)?; + Ok(values) + } +} +fn validate_box_group( + registry: &Registry, + heap: &Heap, + type_id: TypeId, + values: &[Value], +) -> Result<(), ValueError> { + let layout = registry.type_layout(type_id).ok_or(ValueError::Type)?; + if layout.storage.slots <= 1 + || matches!( + registry.declarations().types[type_id.0 as usize].kind, + TypeKind::Builtin(BuiltinId::DYNAMIC) + ) + || values.iter().all(|value| matches!(value, Value::Null)) + { + return Err(ValueError::Type); + } + // Only vec2, pixel and present trait views reach this path. In particular, + // validating a malicious Dynamic box cannot recurse into another box. + value_contract::validate(registry, heap, type_id, values) +} + +#[cfg(test)] +mod dynamic_tests; + #[cfg(test)] mod tests { use super::*; + #[test] + fn stale_references_cannot_access_a_reused_incarnation() { + let mut heap = Heap::new(); + let old = heap.alloc_closure(7, &[]).unwrap(); + assert_eq!((old.index, old.generation), (0, 1)); + heap.sweep(); + let live = heap.alloc_closure(9, &[]).unwrap(); + assert_eq!(live.index, old.index); + assert_eq!(live.generation, 2); + assert!(!heap.is_valid(old)); + assert!(heap.header(old).is_none()); + assert!(heap.closure_fn_id(old).is_none()); + assert!(heap.closure_env_slice(old).is_none()); + assert_eq!(heap.closure_fn_id(live), Some(9)); + // Stale roots never retain the newer object at the same index. + heap.mark_from_roots([old]).unwrap(); + heap.sweep(); + assert!(!heap.is_valid(live)); + } + + #[test] + fn maximum_generation_is_retired_without_wraparound() { + let mut heap = Heap::new(); + let first = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + heap.generations[first.index as usize] = u32::MAX; + let last = HeapRef { index: first.index, generation: u32::MAX }; + assert!(heap.is_valid(last)); + heap.sweep(); + assert!(!heap.is_valid(last)); + assert!(heap.free_list.is_empty()); + let next = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + assert_ne!(next.index, last.index); + assert_eq!(next.generation, 1); + assert_eq!(heap.generations[last.index as usize], u32::MAX); + } + + #[test] + fn null_zero_generation_and_dead_coroutine_handles_are_rejected() { + let mut heap = Heap::new(); + let old = heap + .allocate_coroutine( + 0, + CoroutineState::Ready, + 0, + BudgetVec::fixture(vec![]), + BudgetVec::fixture(vec![]), + ) + .unwrap(); + assert!(!heap.is_valid(HeapRef::NULL)); + assert!(!heap.is_valid(HeapRef { index: 1, generation: 0 })); + assert!(!heap.is_valid(HeapRef { index: u32::MAX, generation: 1 })); + heap.sweep(); + let next = heap + .allocate_coroutine( + 2, + CoroutineState::Ready, + 0, + BudgetVec::fixture(vec![]), + BudgetVec::fixture(vec![]), + ) + .unwrap(); + assert!(heap.coroutine_data(old).is_none()); + assert!(heap.coroutine_data_mut(old).is_none()); + assert_eq!(heap.suspended_coroutine_handles().collect::>(), vec![next]); + assert_eq!(size_of::(), 8); + assert_eq!(align_of::(), 8); + } + #[test] fn basic_allocation_returns_valid_refs() { let mut heap = Heap::new(); - let r1 = heap.allocate_object(ObjectKind::String, b"hello"); - let r2 = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3, 4]); - let r3 = heap.allocate_array(vec![]); + let r1 = heap.allocate_object(ObjectKind::String, b"hello").unwrap(); + let r2 = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3, 4]).unwrap(); + let r3 = heap.allocate_array(vec![]).unwrap(); assert!(heap.is_valid(r1)); assert!(heap.is_valid(r2)); @@ -472,14 +767,16 @@ mod tests { let mut heap = Heap::new(); // Create a coroutine with a small stack containing a HeapRef to verify GC traversal later. - let obj_ref = heap.allocate_object(ObjectKind::Bytes, &[4, 5, 6]); - let coro = heap.allocate_coroutine( - 0, - CoroutineState::Ready, - 0, - vec![Value::Int32(1), Value::HeapRef(obj_ref)], - vec![CallFrame { return_pc: 0, stack_base: 0, func_idx: 0 }], - ); + let obj_ref = heap.allocate_object(ObjectKind::Bytes, &[4, 5, 6]).unwrap(); + let coro = heap + .allocate_coroutine( + 0, + CoroutineState::Ready, + 0, + BudgetVec::fixture(vec![Value::Int32(1), Value::HeapRef(obj_ref)]), + BudgetVec::fixture(vec![CallFrame { return_pc: 0, stack_base: 0, func_idx: 0 }]), + ) + .unwrap(); let hdr = heap.header(coro).unwrap(); assert_eq!(hdr.kind, ObjectKind::Coroutine); @@ -487,7 +784,7 @@ mod tests { // Manually mutate state transitions via access to inner data. { - let slot = heap.objects.get_mut(coro.0 as usize).and_then(|s| s.as_mut()).unwrap(); + let slot = heap.objects.get_mut(coro.index as usize).and_then(|s| s.as_mut()).unwrap(); let co = slot.coroutine.as_mut().unwrap(); assert_eq!(co.state, CoroutineState::Ready); co.state = CoroutineState::Running; @@ -500,7 +797,7 @@ mod tests { } // GC should mark the object referenced from the coroutine stack when the coroutine is a root. - heap.mark_from_roots([coro]); + heap.mark_from_roots([coro]).unwrap(); assert!(heap.header(obj_ref).unwrap().is_marked()); } @@ -509,13 +806,14 @@ mod tests { let mut heap = Heap::new(); // Target object B (unreferenced yet) - let b = heap.allocate_object(ObjectKind::Bytes, &[9, 9, 9]); + let b = heap.allocate_object(ObjectKind::Bytes, &[9, 9, 9]).unwrap(); // Array A that contains a reference to B among other primitives - let a = - heap.allocate_array(vec![Value::Int32(1), Value::HeapRef(b), Value::Boolean(false)]); + let a = heap + .allocate_array(vec![Value::Int32(1), Value::HeapRef(b), Value::Boolean(false)]) + .unwrap(); // Mark starting from root A - heap.mark_from_roots([a]); + heap.mark_from_roots([a]).unwrap(); // Both A and B must be marked; random other objects are not allocated assert!(heap.header(a).unwrap().is_marked()); @@ -526,10 +824,10 @@ mod tests { fn mark_does_not_mark_unreachable() { let mut heap = Heap::new(); - let unreachable = heap.allocate_object(ObjectKind::String, b"orphan"); - let root = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]); + let unreachable = heap.allocate_object(ObjectKind::String, b"orphan").unwrap(); + let root = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]).unwrap(); - heap.mark_from_roots([root]); + heap.mark_from_roots([root]).unwrap(); assert!(heap.header(root).unwrap().is_marked()); assert!(!heap.header(unreachable).unwrap().is_marked()); @@ -541,28 +839,18 @@ mod tests { // Create two arrays that reference each other: A -> B, B -> A // Allocate empty arrays first to get handles - let a = heap.allocate_array(vec![]); - let b = heap.allocate_array(vec![]); + let a = heap.allocate_array(vec![]).unwrap(); + let b = heap.allocate_array(vec![]).unwrap(); // Now mutate their internal vectors via re-allocation pattern: // replace with arrays containing cross-references. Since our simple // heap doesn't support in-place element edits via API, simulate by // directly editing stored objects. - if let Some(slot) = heap.objects.get_mut(a.0 as usize) - && let Some(obj) = slot.as_mut() - { - obj.array_elems = Some(vec![Value::HeapRef(b)]); - obj.header.payload_len = 1; - } - if let Some(slot) = heap.objects.get_mut(b.0 as usize) - && let Some(obj) = slot.as_mut() - { - obj.array_elems = Some(vec![Value::HeapRef(a)]); - obj.header.payload_len = 1; - } + heap.set_test_array(a, &[Value::HeapRef(b)]); + heap.set_test_array(b, &[Value::HeapRef(a)]); // Mark from A; should terminate and mark both. - heap.mark_from_roots([a]); + heap.mark_from_roots([a]).unwrap(); assert!(heap.header(a).unwrap().is_marked()); assert!(heap.header(b).unwrap().is_marked()); @@ -571,7 +859,7 @@ mod tests { #[test] fn closure_allocation_with_empty_env() { let mut heap = Heap::new(); - let c = heap.alloc_closure(42, &[]); + let c = heap.alloc_closure(42, &[]).unwrap(); assert!(heap.is_valid(c)); let h = heap.header(c).unwrap(); assert_eq!(h.kind, ObjectKind::Closure); @@ -585,9 +873,9 @@ mod tests { #[test] fn closure_allocation_with_env_and_access() { let mut heap = Heap::new(); - let a = heap.allocate_object(ObjectKind::String, b"a"); + let a = heap.allocate_object(ObjectKind::String, b"a").unwrap(); let env_vals = vec![Value::Int32(7), Value::HeapRef(a), Value::Boolean(true)]; - let c = heap.alloc_closure(7, &env_vals); + let c = heap.alloc_closure(7, &env_vals).unwrap(); let h = heap.header(c).unwrap(); assert_eq!(h.kind, ObjectKind::Closure); @@ -597,7 +885,7 @@ mod tests { assert_eq!(env, &env_vals[..]); // GC traversal should see the inner HeapRef in closure env when marking. - heap.mark_from_roots([c]); + heap.mark_from_roots([c]).unwrap(); assert!(heap.header(c).unwrap().is_marked()); assert!(heap.header(a).unwrap().is_marked()); } @@ -607,11 +895,11 @@ mod tests { let mut heap = Heap::new(); // Allocate two objects; only one will be a root. - let unreachable = heap.allocate_object(ObjectKind::String, b"orphan"); - let root = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]); + let unreachable = heap.allocate_object(ObjectKind::String, b"orphan").unwrap(); + let root = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]).unwrap(); // Mark from root and then sweep. - heap.mark_from_roots([root]); + heap.mark_from_roots([root]).unwrap(); // Precondition: root marked, unreachable not marked. assert!(heap.header(root).unwrap().is_marked()); assert!(!heap.header(unreachable).unwrap().is_marked()); @@ -631,12 +919,12 @@ mod tests { fn sweep_keeps_indices_stable_and_len_counts_live() { let mut heap = Heap::new(); - let a = heap.allocate_object(ObjectKind::String, b"a"); - let b = heap.allocate_object(ObjectKind::String, b"b"); - let c = heap.allocate_object(ObjectKind::String, b"c"); + let a = heap.allocate_object(ObjectKind::String, b"a").unwrap(); + let b = heap.allocate_object(ObjectKind::String, b"b").unwrap(); + let c = heap.allocate_object(ObjectKind::String, b"c").unwrap(); // Only keep A live. - heap.mark_from_roots([a]); + heap.mark_from_roots([a]).unwrap(); heap.sweep(); // B and C are now invalidated, A remains valid. @@ -649,24 +937,26 @@ mod tests { // Indices are stable: A's index is still within the backing store bounds. // We can't access internal vector here, but stability is implied by handle not changing. - assert_eq!(a.0, a.0); // placeholder sanity check + assert_eq!(heap.header(a).unwrap().kind, ObjectKind::String); } #[test] fn sweep_reuses_freed_slot_on_next_allocation() { let mut heap = Heap::new(); - let dead = heap.allocate_object(ObjectKind::String, b"dead"); - let live = heap.allocate_object(ObjectKind::String, b"live"); + let dead = heap.allocate_object(ObjectKind::String, b"dead").unwrap(); + let live = heap.allocate_object(ObjectKind::String, b"live").unwrap(); - heap.mark_from_roots([live]); + heap.mark_from_roots([live]).unwrap(); heap.sweep(); assert!(!heap.is_valid(dead)); - assert_eq!(heap.free_list, vec![dead.0 as usize]); + assert_eq!(heap.free_list, vec![dead.index as usize]); - let reused = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]); - assert_eq!(reused, dead); + let reused = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]).unwrap(); + assert_eq!(reused.index, dead.index); + assert_eq!(reused.generation, dead.generation + 1); + assert!(!heap.is_valid(dead)); assert!(heap.is_valid(reused)); assert!(heap.is_valid(live)); } @@ -675,40 +965,45 @@ mod tests { fn live_handles_remain_stable_when_freelist_is_reused() { let mut heap = Heap::new(); - let live = heap.allocate_object(ObjectKind::String, b"live"); - let dead = heap.allocate_object(ObjectKind::String, b"dead"); + let live = heap.allocate_object(ObjectKind::String, b"live").unwrap(); + let dead = heap.allocate_object(ObjectKind::String, b"dead").unwrap(); - heap.mark_from_roots([live]); + heap.mark_from_roots([live]).unwrap(); heap.sweep(); - let replacement = heap.allocate_object(ObjectKind::Bytes, &[9]); + let replacement = heap.allocate_object(ObjectKind::Bytes, &[9]).unwrap(); - assert_eq!(replacement, dead); + assert_eq!(replacement.index, dead.index); + assert_ne!(replacement, dead); + assert!(!heap.is_valid(dead)); assert_eq!(heap.header(live).unwrap().kind, ObjectKind::String); assert_eq!(heap.header(replacement).unwrap().kind, ObjectKind::Bytes); - assert_eq!(live.0, 0); + assert_eq!(live.index, 0); } #[test] fn freelist_reuse_is_deterministic_lifo() { let mut heap = Heap::new(); - let a = heap.allocate_object(ObjectKind::String, b"a"); - let b = heap.allocate_object(ObjectKind::String, b"b"); - let c = heap.allocate_object(ObjectKind::String, b"c"); + let a = heap.allocate_object(ObjectKind::String, b"a").unwrap(); + let b = heap.allocate_object(ObjectKind::String, b"b").unwrap(); + let c = heap.allocate_object(ObjectKind::String, b"c").unwrap(); - heap.mark_from_roots([]); + heap.mark_from_roots([]).unwrap(); heap.sweep(); - assert_eq!(heap.free_list, vec![a.0 as usize, b.0 as usize, c.0 as usize]); + assert_eq!(heap.free_list, vec![a.index as usize, b.index as usize, c.index as usize]); - let r1 = heap.allocate_object(ObjectKind::Bytes, &[1]); - let r2 = heap.allocate_object(ObjectKind::Bytes, &[2]); - let r3 = heap.allocate_object(ObjectKind::Bytes, &[3]); + let r1 = heap.allocate_object(ObjectKind::Bytes, &[1]).unwrap(); + let r2 = heap.allocate_object(ObjectKind::Bytes, &[2]).unwrap(); + let r3 = heap.allocate_object(ObjectKind::Bytes, &[3]).unwrap(); - assert_eq!(r1, c); - assert_eq!(r2, b); - assert_eq!(r3, a); + assert_eq!(r1.index, c.index); + assert_eq!(r1.generation, c.generation + 1); + assert_eq!(r2.index, b.index); + assert_eq!(r2.generation, b.generation + 1); + assert_eq!(r3.index, a.index); + assert_eq!(r3.generation, a.generation + 1); } #[test] @@ -716,22 +1011,12 @@ mod tests { let mut heap = Heap::new(); // Build a 2-node cycle A <-> B using internal mutation (module-private access). - let a = heap.allocate_array(vec![]); - let b = heap.allocate_array(vec![]); + let a = heap.allocate_array(vec![]).unwrap(); + let b = heap.allocate_array(vec![]).unwrap(); // Make A point to B and B point to A. - if let Some(slot) = heap.objects.get_mut(a.0 as usize) - && let Some(obj) = slot.as_mut() - { - obj.array_elems = Some(vec![Value::HeapRef(b)]); - obj.header.payload_len = 1; - } - if let Some(slot) = heap.objects.get_mut(b.0 as usize) - && let Some(obj) = slot.as_mut() - { - obj.array_elems = Some(vec![Value::HeapRef(a)]); - obj.header.payload_len = 1; - } + heap.set_test_array(a, &[Value::HeapRef(b)]); + heap.set_test_array(b, &[Value::HeapRef(a)]); // No roots: perform sweep directly; both should be reclaimed. heap.sweep(); @@ -746,14 +1031,14 @@ mod tests { let mut heap = Heap::new(); // Captured heap object. - let obj = heap.allocate_object(ObjectKind::Bytes, &[4, 5, 6]); + let obj = heap.allocate_object(ObjectKind::Bytes, &[4, 5, 6]).unwrap(); // Closure capturing the heap object among other primitive values. let env = [Value::Boolean(true), Value::HeapRef(obj), Value::Int32(123)]; - let clo = heap.alloc_closure(1, &env); + let clo = heap.alloc_closure(1, &env).unwrap(); // Mark from closure root: both closure and captured object must be marked. - heap.mark_from_roots([clo]); + heap.mark_from_roots([clo]).unwrap(); assert!(heap.header(clo).unwrap().is_marked()); assert!(heap.header(obj).unwrap().is_marked()); @@ -771,13 +1056,13 @@ mod tests { let mut heap = Heap::new(); // Inner closure (no env). - let inner = heap.alloc_closure(2, &[]); + let inner = heap.alloc_closure(2, &[]).unwrap(); // Outer closure captures the inner closure as a Value::HeapRef. - let outer = heap.alloc_closure(3, &[Value::HeapRef(inner)]); + let outer = heap.alloc_closure(3, &[Value::HeapRef(inner)]).unwrap(); // Root only the outer closure. - heap.mark_from_roots([outer]); + heap.mark_from_roots([outer]).unwrap(); // Both must be marked reachable. assert!(heap.header(outer).unwrap().is_marked()); @@ -796,8 +1081,8 @@ mod tests { let mut heap = Heap::new(); // Captured heap object and a closure capturing it. - let captured = heap.allocate_object(ObjectKind::String, b"dead"); - let clo = heap.alloc_closure(9, &[Value::HeapRef(captured)]); + let captured = heap.allocate_object(ObjectKind::String, b"dead").unwrap(); + let clo = heap.alloc_closure(9, &[Value::HeapRef(captured)]).unwrap(); // No roots are provided; sweeping should reclaim both. heap.sweep(); @@ -806,4 +1091,115 @@ mod tests { assert!(!heap.is_valid(captured)); assert_eq!(heap.len(), 0); } + fn tiny_heap(resource: Q, limit: u64) -> (Heap, RuntimeBudget) { + let budget = RuntimeBudget::new( + crate::resource_budget::RuntimeLimits::default().lowered(resource, limit).unwrap(), + ); + (Heap::with_budget(&budget), budget) + } + + #[test] + fn guest_header_and_payload_limits_refund_failed_construction() { + let required = (size_of::() + 8 + size_of::()) as u64; + let (mut heap, budget) = tiny_heap(Q::GuestBytes, required - 1); + assert_eq!(heap.alloc_closure(0, &[Value::Int64(9)]).unwrap_err().resource, Q::GuestBytes); + for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] { + assert_eq!(budget.used(q), 0, "{q:?}"); + } + assert_eq!(heap.allocation_count(), 0); + let (mut heap, budget) = tiny_heap(Q::GuestBytes, required); + let r = heap.alloc_closure(0, &[Value::Int64(9)]).unwrap(); + assert!(heap.is_valid(r)); + assert_eq!(budget.used(Q::GuestBytes), required); + assert_eq!(heap.used_bytes.load(Ordering::Relaxed) as u64, required); + assert!(heap.alloc_closure(0, &[]).is_err()); + assert_eq!(heap.len(), 1); + heap.sweep(); + assert_eq!(budget.used(Q::GuestBytes), 0); + assert_eq!(heap.used_bytes.load(Ordering::Relaxed), 0); + } + + #[test] + fn table_growth_is_atomic_when_a_later_table_exceeds_auxiliary_quota() { + let per_slot = + (size_of::>() + size_of::() + size_of::()) as u64; + let (mut heap, budget) = tiny_heap(Q::AuxiliaryBytes, 2 * per_slot - 1); + let first = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + let guest = budget.used(Q::GuestBytes); + let error = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap_err(); + assert_eq!(error.resource, Q::AuxiliaryBytes); + assert_eq!(budget.used(Q::AuxiliaryBytes), per_slot); + assert_eq!(budget.used(Q::GuestBytes), guest); + assert_eq!(budget.used(Q::HandleSlots), 1); + assert_eq!(budget.used(Q::TemporaryBytes), 0); + assert_eq!(heap.objects.len(), 1); + assert_eq!(heap.generations.len(), 1); + assert_eq!(heap.allocation_count(), 1); + assert!(heap.is_valid(first)); + } + + #[test] + fn handle_limit_counts_free_and_retired_slots_and_reuse_does_not_charge_twice() { + let (mut heap, budget) = tiny_heap(Q::HandleSlots, 1); + let first = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + assert_eq!( + heap.allocate_object(ObjectKind::Bytes, &[]).unwrap_err().resource, + Q::HandleSlots + ); + heap.sweep(); + assert_eq!(budget.used(Q::HandleSlots), 1); + let reused = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + assert_eq!((reused.index, reused.generation), (first.index, 2)); + assert_eq!(budget.used(Q::HandleSlots), 1); + heap.generations[reused.index as usize] = u32::MAX; + heap.sweep(); + assert!(heap.free_list.is_empty()); + assert_eq!(budget.used(Q::GuestBytes), 0); + assert_eq!(budget.used(Q::HandleSlots), 1); + assert_eq!( + heap.allocate_object(ObjectKind::Bytes, &[]).unwrap_err().resource, + Q::HandleSlots + ); + drop(heap); + assert_eq!(budget.used(Q::HandleSlots), 0); + assert_eq!(budget.used(Q::AuxiliaryBytes), 0); + } + + #[test] + fn mark_worklist_refusal_changes_no_marks_or_objects() { + let (mut heap, budget) = tiny_heap(Q::TemporaryBytes, 0); + let root = heap.allocate_array(vec![Value::Int64(8)]).unwrap(); + let bytes = budget.used(Q::GuestBytes); + let error = heap.mark_from_roots([root]).unwrap_err(); + assert_eq!(error.resource, Q::TemporaryBytes); + assert!(!heap.header(root).unwrap().is_marked()); + assert!(heap.is_valid(root)); + assert_eq!(budget.used(Q::GuestBytes), bytes); + assert_eq!(budget.used(Q::TemporaryBytes), 0); + } + + #[test] + fn duplicate_edges_and_roots_need_one_work_slot_per_live_object() { + let (mut heap, budget) = tiny_heap(Q::TemporaryBytes, size_of::() as u64); + let root = HeapRef { index: 0, generation: 1 }; + assert_eq!(heap.allocate_array(vec![Value::HeapRef(root); 1024]).unwrap(), root); + heap.mark_from_roots(std::iter::repeat_n(root, 1024)).unwrap(); + assert!(heap.header(root).unwrap().is_marked()); + assert_eq!(budget.used(Q::TemporaryBytes), 0); + heap.sweep(); + assert!(heap.is_valid(root)); + } + + #[test] + fn quota_pressure_does_not_collect_outside_a_safepoint() { + let (mut heap, budget) = tiny_heap(Q::GuestBytes, size_of::() as u64); + let unreachable = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + assert!(heap.allocate_object(ObjectKind::Bytes, &[]).is_err()); + assert!(heap.is_valid(unreachable)); + heap.sweep(); + assert_eq!(budget.used(Q::GuestBytes), 0); + let replacement = heap.allocate_object(ObjectKind::Bytes, &[]).unwrap(); + assert_eq!(replacement.index, unreachable.index); + assert_ne!(replacement.generation, unreachable.generation); + } } diff --git a/crates/console/prometeu-vm/src/heap/dynamic_box.rs b/crates/console/prometeu-vm/src/heap/dynamic_box.rs new file mode 100644 index 0000000..1514cf2 --- /dev/null +++ b/crates/console/prometeu-vm/src/heap/dynamic_box.rs @@ -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, + 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, + type_id: TypeId, + values: &[Value], + budget: &RuntimeBudget, + ) -> Result { + let bytes = budget.bytes::(values.len(), Q::GuestBytes)?; + let layout = Layout::array::(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::()).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, + 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 = Arc::from("partially initialized"); + let result = std::panic::catch_unwind(|| { + let layout = Layout::array::(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::()).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 + ); + } +} diff --git a/crates/console/prometeu-vm/src/heap/dynamic_tests.rs b/crates/console/prometeu-vm/src/heap/dynamic_tests.rs new file mode 100644 index 0000000..a928ffb --- /dev/null +++ b/crates/console/prometeu-vm/src/heap/dynamic_tests.rs @@ -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, 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::() + input.len() * size_of::()) 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 = 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::() + 2 * size_of::()) 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::() + 2 * size_of::()) 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 = 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 = 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::>() + size_of::() + size_of::()) 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)); +} diff --git a/crates/console/prometeu-vm/src/input_intrinsics.rs b/crates/console/prometeu-vm/src/input_intrinsics.rs new file mode 100644 index 0000000..1ee6d17 --- /dev/null +++ b/crates/console/prometeu-vm/src/input_intrinsics.rs @@ -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(button: &Button) -> Vec { + 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( + args: &[Value], ctx: &mut HostContext<'_>, + ) -> Result, IntrinsicExecutionError> { + Ok(read_button::(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, IntrinsicExecutionError> { + Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().x()))]) +} + +fn touch_y( + args: &[Value], + ctx: &mut HostContext<'_>, +) -> Result, IntrinsicExecutionError> { + Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().y()))]) +} diff --git a/crates/console/prometeu-vm/src/lib.rs b/crates/console/prometeu-vm/src/lib.rs index 22489a4..8b28381 100644 --- a/crates/console/prometeu-vm/src/lib.rs +++ b/crates/console/prometeu-vm/src/lib.rs @@ -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; diff --git a/crates/console/prometeu-vm/src/object.rs b/crates/console/prometeu-vm/src/object.rs index 7f0260f..6e6cb92 100644 --- a/crates/console/prometeu-vm/src/object.rs +++ b/crates/console/prometeu-vm/src/object.rs @@ -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. } diff --git a/crates/console/prometeu-vm/src/registry_verifier.rs b/crates/console/prometeu-vm/src/registry_verifier.rs new file mode 100644 index 0000000..83e9659 --- /dev/null +++ b/crates/console/prometeu-vm/src/registry_verifier.rs @@ -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, 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, 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 +} diff --git a/crates/console/prometeu-vm/src/registry_verifier/tests.rs b/crates/console/prometeu-vm/src/registry_verifier/tests.rs new file mode 100644 index 0000000..9c42763 --- /dev/null +++ b/crates/console/prometeu-vm/src/registry_verifier/tests.rs @@ -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, op: Op) { + code.extend_from_slice(&(op as u16).to_le_bytes()); +} +fn arg(code: &mut Vec, op_code: Op, n: u32) { + op(code, op_code); + code.extend_from_slice(&n.to_le_bytes()); +} +fn registry(code: &[u8], functions: Vec) -> 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); +} diff --git a/crates/console/prometeu-vm/src/resource_budget.rs b/crates/console/prometeu-vm/src/resource_budget.rs new file mode 100644 index 0000000..91d120f --- /dev/null +++ b/crates/console/prometeu-vm/src/resource_budget.rs @@ -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 { + 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>); +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( + &self, + count: usize, + resource: RuntimeResource, + ) -> Result { + count + .checked_mul(size_of::()) + .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 { + 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 { + data: VecDeque, + charge: Reservation, +} +impl BudgetQueue { + #[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 { + 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::(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::(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::::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::::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::(usize::MAX, AuxiliaryBytes).unwrap_err().reason, + ResourceReason::SizeOverflow + ); + let mut queue = BudgetQueue::::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 { + data: Vec, + charge: Reservation, +} +impl BudgetVec { + 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>, 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::(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::(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>) { + 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 + 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 { + 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) -> 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 { + &mut self.data + } + #[cfg(test)] + pub(crate) fn fixture(data: Vec) -> Self { + let budget = RuntimeBudget::default(); + let bytes = budget.bytes::(data.capacity(), RuntimeResource::AuxiliaryBytes).unwrap(); + let charge = budget.reserve(RuntimeResource::AuxiliaryBytes, bytes).unwrap(); + Self { data, charge } + } +} +impl std::ops::Deref for BudgetVec { + type Target = [T]; + fn deref(&self) -> &[T] { + &self.data + } +} +impl std::ops::DerefMut for BudgetVec { + fn deref_mut(&mut self) -> &mut [T] { + &mut self.data + } +} +impl<'a, T> IntoIterator for &'a BudgetVec { + type Item = &'a T; + type IntoIter = std::slice::Iter<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.data.iter() + } +} +#[cfg(test)] +impl PartialEq> for BudgetVec { + fn eq(&self, other: &Vec) -> bool { + &self.data == other + } +} + +/// Unpublished growth can be dropped to refund every provisional reservation. +pub(crate) struct VecGrowth { + candidate: Vec, + extra: Reservation, + _temporary: Reservation, + old_capacity: usize, +} diff --git a/crates/console/prometeu-vm/src/roots.rs b/crates/console/prometeu-vm/src/roots.rs index 4a69cd3..a7e8dfc 100644 --- a/crates/console/prometeu-vm/src/roots.rs +++ b/crates/console/prometeu-vm/src/roots.rs @@ -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 { + if let Value::HeapRef(r) = value { Some(*r) } else { None } +} + pub fn visit_value_for_roots(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 }]); } } diff --git a/crates/console/prometeu-vm/src/scheduler.rs b/crates/console/prometeu-vm/src/scheduler.rs index 4fdde45..7ab6cd9 100644 --- a/crates/console/prometeu-vm/src/scheduler.rs +++ b/crates/console/prometeu-vm/src/scheduler.rs @@ -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, + ready_queue: BudgetQueue, /// Sleeping list kept sorted by (wake_tick, insertion_order) - sleeping: Vec, + sleeping: BudgetQueue, /// Currently selected coroutine (purely informational here) current: Option, /// 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 = 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::()) 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); + } } diff --git a/crates/console/prometeu-vm/src/typed_storage.rs b/crates/console/prometeu-vm/src/typed_storage.rs new file mode 100644 index 0000000..4c962c3 --- /dev/null +++ b/crates/console/prometeu-vm/src/typed_storage.rs @@ -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, + type_id: TypeId, + // None for a Struct instance, Some(count) for an Array instance. + count: Option, + initialized: usize, + data: Allocation, +} +impl TypedStorage { + pub fn new_struct( + registry: Arc, + type_id: TypeId, + budget: &RuntimeBudget, + ) -> Result { + 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, + type_id: TypeId, + count: u32, + budget: &RuntimeBudget, + ) -> Result { + 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 { + 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::(offset, None), + B::DYNAMIC => self.data.init(offset, DynamicSlot::default()), + _ => unreachable!("resolved builtin"), + }, + } + } + self.initialized += 1; + } + } + pub fn read(&self, index: usize) -> Result { + 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::(offset); + ValueGroup::new(&[if r.is_null() { Value::Null } else { Value::HeapRef(r) }]) + } + TypeKind::Trait(_) => { + let t = self.data.get::(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::(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::(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::(offset); + ValueGroup::new(&[ + Value::Int64(p.x), + Value::Int64(p.y), + Value::Int64(p.color.into()), + ]) + } + B::STRING => ValueGroup::new(&[self + .data + .get::(offset) + .as_ref() + .map_or(Value::Null, |s| Value::String(s.clone()))]), + B::DYNAMIC => { + let d = self.data.get::(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::(offset, None) + } + (TypeKind::Builtin(B::STRING), [Value::String(s)]) => { + self.data.replace::(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 { + 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::(offset)), + Ownership::TraitReceiver => Some(self.data.get::(offset).receiver), + Ownership::Dynamic => { + crate::roots::value_heap_ref(&self.data.get::(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::(offset), + Ownership::Dynamic => data.drop_at::(offset), + _ => {} + } + } +} + +#[cfg(test)] +pub(crate) mod tests; diff --git a/crates/console/prometeu-vm/src/typed_storage/allocation.rs b/crates/console/prometeu-vm/src/typed_storage/allocation.rs new file mode 100644 index 0000000..5ed92c7 --- /dev/null +++ b/crates/console/prometeu-vm/src/typed_storage/allocation.rs @@ -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, + 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 { + 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(&self, offset: usize) -> *mut T { + assert!(offset.checked_add(size_of::()).is_some_and(|end| end <= self.layout.size())); + assert!(self.layout.align() >= align_of::()); + assert!(offset.is_multiple_of(align_of::())); + // 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(&self, offset: usize) -> &T { + // SAFETY: caller's typed initialization invariant and checked address. + unsafe { &*self.at::(offset) } + } + /// Caller guarantees exclusive access and a valid initialized T at offset. + pub unsafe fn replace(&mut self, offset: usize, value: T) { + // SAFETY: caller guarantees initialized matching type; assignment drops old owner. + unsafe { + *self.at::(offset) = value; + } + } + /// Caller guarantees the destination is uninitialized storage for exactly T. + pub unsafe fn init(&mut self, offset: usize, value: T) { + // SAFETY: checked address, ownership is moved into previously unused storage. + unsafe { + self.at::(offset).write(value); + } + } + /// Caller guarantees an initialized T which will never be used again. + pub unsafe fn drop_at(&mut self, offset: usize) { + // SAFETY: caller walks each initialized native slot exactly once. + unsafe { + std::ptr::drop_in_place(self.at::(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); + } + } + } +} diff --git a/crates/console/prometeu-vm/src/typed_storage/tests.rs b/crates/console/prometeu-vm/src/typed_storage/tests.rs new file mode 100644 index 0000000..4ae486f --- /dev/null +++ b/crates/console/prometeu-vm/src/typed_storage/tests.rs @@ -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 { + 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 { + 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::() + 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 = 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 = 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 = 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::(offset) } as *const _ as usize; + assert_eq!(addr % align_of::(), 0); + let offset = mixed.slot(4).unwrap().1; + let addr = unsafe { mixed.data.get::(offset) } as *const _ as usize; + assert_eq!(addr % align_of::(), 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::(), 16); + assert_eq!(size_of::(), 24); +} + +#[test] +fn unpublished_native_prefix_cleanup_drops_only_initialized_slots() { + let registry = fixture_registry(); + let budget = RuntimeBudget::default(); + let text: Arc = 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::(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 = 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::() 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); +} diff --git a/crates/console/prometeu-vm/src/value_contract.rs b/crates/console/prometeu-vm/src/value_contract.rs new file mode 100644 index 0000000..329705b --- /dev/null +++ b/crates/console/prometeu-vm/src/value_contract.rs @@ -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 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 { + 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. + } + 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, + heap: &mut Heap, + type_id: TypeId, + values: &[Value], +) -> Result { + 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 { + 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 { + 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)?)) +} diff --git a/crates/console/prometeu-vm/src/verifier.rs b/crates/console/prometeu-vm/src/verifier.rs index 1c098ae..56adbf2 100644 --- a/crates/console/prometeu-vm/src/verifier.rs +++ b/crates/console/prometeu-vm/src/verifier.rs @@ -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, opcode: OpCode) { + code.extend_from_slice(&(opcode as u16).to_le_bytes()); + } + fn arg(code: &mut Vec, opcode: OpCode, n: u32) { + op(code, opcode); + code.extend_from_slice(&n.to_le_bytes()); + } + fn one(code: &[u8]) -> Vec { + 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]); + } +} diff --git a/crates/console/prometeu-vm/src/virtual_machine.rs b/crates/console/prometeu-vm/src/virtual_machine.rs index 7f2de71..b7bad66 100644 --- a/crates/console/prometeu-vm/src/virtual_machine.rs +++ b/crates/console/prometeu-vm/src/virtual_machine.rs @@ -1,3 +1,4 @@ +use crate::resource_budget::BudgetVec; mod gc; mod loader; mod runtime; @@ -7,6 +8,7 @@ use crate::call_frame::CallFrame; use crate::heap::{CoroutineState, Heap}; use crate::lookup_intrinsic_by_id; use crate::object::ObjectKind; +use crate::resource_budget::{ResourceFailure, RuntimeBudget, RuntimeLimits, RuntimeResource}; use crate::roots::{RootVisitor, visit_value_for_roots}; use crate::scheduler::Scheduler; use crate::verifier::Verifier; @@ -19,8 +21,9 @@ use prometeu_bytecode::decode_next; use prometeu_bytecode::isa::core::CoreOpCode as OpCode; use prometeu_bytecode::model::BytecodeModule; use prometeu_bytecode::{ - TRAP_BAD_RET_SLOTS, TRAP_DIV_ZERO, TRAP_EXPLICIT, TRAP_INVALID_FUNC, TRAP_INVALID_INTRINSIC, - TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_STACK_UNDERFLOW, TRAP_TYPE, TrapInfo, + TRAP_BAD_RET_SLOTS, TRAP_DIV_ZERO, TRAP_EXPLICIT, TRAP_INVALID_FUNC, TRAP_INVALID_HANDLE, + TRAP_INVALID_INTRINSIC, TRAP_INVALID_SYSCALL, TRAP_NULL_REFERENCE, TRAP_OOB, + TRAP_RESOURCE_LIMIT, TRAP_STACK_UNDERFLOW, TRAP_TYPE, TRAP_UNAVAILABLE, TrapInfo, }; use prometeu_hal::syscalls::caps::NONE; use prometeu_hal::vm_fault::VmFault; @@ -71,11 +74,11 @@ pub struct VirtualMachine { /// Program Counter (PC): The absolute byte offset in ROM for the next instruction. pc: usize, /// Operand Stack: The primary workspace for all mathematical and logical operations. - operand_stack: Vec, + operand_stack: BudgetVec, /// Call Stack: Manages function call context (return addresses, frame limits). - call_stack: Vec, + call_stack: BudgetVec, /// Global Variable Store: Variables that persist for the lifetime of the program. - globals: Vec, + globals: BudgetVec, /// The loaded executable (Bytecode + Constant Pool), that is the ROM translated. program: ProgramImage, /// Heap Memory: Dynamic allocation pool. @@ -112,6 +115,7 @@ pub struct VirtualMachine { current_tick: u64, /// Cooperative scheduler instance managing ready/sleeping queues. scheduler: Scheduler, + budget: RuntimeBudget, /// Handle to the currently running coroutine (owns the active VM context). current_coro: Option, } @@ -168,15 +172,24 @@ impl VirtualMachine { // Test-only helpers for internal unit tests within this crate. #[cfg(test)] pub(crate) fn push_operand_for_test(&mut self, v: Value) { - self.operand_stack.push(v); + self.operand_stack.push(v).unwrap(); } /// Creates a new VM instance with the provided bytecode and constants. pub fn new(rom: Vec, constant_pool: Vec) -> Self { + Self::with_runtime_limits(rom, constant_pool, RuntimeLimits::default()) + } + + pub fn with_runtime_limits( + rom: Vec, + constant_pool: Vec, + limits: RuntimeLimits, + ) -> Self { + let budget = RuntimeBudget::new(limits); Self { pc: 0, - operand_stack: Vec::new(), - call_stack: Vec::new(), - globals: Vec::new(), + operand_stack: BudgetVec::new(&budget, RuntimeResource::AuxiliaryBytes), + call_stack: BudgetVec::new(&budget, RuntimeResource::AuxiliaryBytes), + globals: BudgetVec::new(&budget, RuntimeResource::AuxiliaryBytes), program: ProgramImage::new( rom, constant_pool, @@ -184,7 +197,7 @@ impl VirtualMachine { None, std::collections::HashMap::new(), ), - heap: Heap::new(), + heap: Heap::with_budget(&budget), cycles: 0, halted: false, breakpoints: std::collections::HashSet::new(), @@ -194,7 +207,8 @@ impl VirtualMachine { yield_requested: false, sleep_requested_until: None, current_tick: 0, - scheduler: Scheduler::new(), + scheduler: Scheduler::with_budget(&budget), + budget, current_coro: None, } } @@ -215,7 +229,8 @@ impl VirtualMachine { // with a safepoint to advance tick and potentially wake sleepers. if self.current_coro.is_none() { self.cycles += OpCode::FrameSync.cycles(); - self.handle_safepoint(); + self.handle_safepoint() + .map_err(|error| self.resource_trap(error, OpCode::FrameSync, self.pc as u32))?; return Err(LogicalFrameEndingReason::FrameSync); } if self.halted || self.pc >= self.program.rom.len() { @@ -334,7 +349,7 @@ impl VirtualMachine { let local_slots: u16 = callee.local_slots; let entry_pc = callee.code_offset as usize; - if arg_count as u16 != param_slots { + if arg_count != param_slots as usize { return Err(self.trap( TRAP_TYPE, opcode as u16, @@ -360,44 +375,62 @@ impl VirtualMachine { )); } + self.budget + .check(RuntimeResource::CallDepth, 1) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; + self.scheduler + .reserve_ready() + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; // Pop args top-first, then reverse to logical order arg1..argN - let mut args: Vec = Vec::with_capacity(arg_count); + let mut args: BudgetVec = + BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes); + args.reserve(arg_count) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; for _ in 0..arg_count { args.push(self.pop_trap( opcode, start_pc as u32, format!("SPAWN to func {} argument stack underflow", fn_id), - )?); + )?) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } args.reverse(); // Build operand stack for the new coroutine: params followed by zeroed locals - let mut new_stack: Vec = - Vec::with_capacity((param_slots + local_slots) as usize); + let mut new_stack = BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes); + new_stack + .reserve(param_slots as usize + local_slots as usize) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; // Place user args as parameters - for v in args { - new_stack.push(v); + for v in args.drain(..) { + new_stack + .push(v) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } // Zero-init locals for _ in 0..local_slots { - new_stack.push(Value::Null); + new_stack + .push(Value::Null) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } // Initial frame for the coroutine (sentinel-like return to end-of-rom) - let frames = vec![CallFrame { - return_pc: self.program.rom.len() as u32, - stack_base: 0, - func_idx: fn_id, - }]; + let mut frames = BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes); + frames + .push(CallFrame { + return_pc: self.program.rom.len() as u32, + stack_base: 0, + func_idx: fn_id, + }) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; - let href = self.heap.allocate_coroutine( - entry_pc, - CoroutineState::Ready, - 0, - new_stack, - frames, - ); - self.scheduler.enqueue_ready(href); + let href = self + .heap + .allocate_coroutine(entry_pc, CoroutineState::Ready, 0, new_stack, frames) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + self.scheduler + .enqueue_ready(href) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; } OpCode::Yield => { // Cooperative yield: record intent; actual switching only at FRAME_SYNC. @@ -424,7 +457,8 @@ impl VirtualMachine { // End the logical frame right after the instruction completes // to ensure no further instructions run until at least next tick. self.cycles += OpCode::FrameSync.cycles(); - self.handle_safepoint(); + self.handle_safepoint() + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; return Err(LogicalFrameEndingReason::FrameSync); } OpCode::Hostcall => { @@ -443,24 +477,35 @@ impl VirtualMachine { .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?; // Pop cap_count values from the operand stack, top-first. - let mut temp: Vec = Vec::with_capacity(cap_count as usize); + let mut temp: BudgetVec = + BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes); + temp.reserve(cap_count as usize) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; for _ in 0..cap_count { let v = self.pop_trap( opcode, start_pc as u32, "MAKE_CLOSURE capture stack underflow", )?; - temp.push(v); + temp.push(v).map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } // Preserve order so that env[0] corresponds to captured_1 (the bottom-most // among the popped values): reverse the temp vector. temp.reverse(); // Allocate closure on heap and push its reference. - let href = self.heap.alloc_closure(fn_id, &temp); - self.push(Value::HeapRef(href)); + self.operand_stack + .reserve(1) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + let href = self + .heap + .alloc_closure(fn_id, &temp) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + self.push_trap(Value::HeapRef(href), opcode, start_pc as u32)?; } OpCode::CallClosure => { + self.check_call_depth() + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; // Operand carries the number of user-supplied arguments (arg1..argN). let user_arg_count = instr .imm_u32() @@ -474,7 +519,15 @@ impl VirtualMachine { "CALL_CLOSURE requires a closure handle on the stack", )?; let href = match clos_val { - Value::HeapRef(h) => h, + Value::HeapRef(h) if !h.is_null() => h, + Value::Null | Value::HeapRef(HeapRef { index: 0, generation: 0 }) => { + return Err(self.trap( + TRAP_NULL_REFERENCE, + opcode as u16, + "CALL_CLOSURE requires a present instance".into(), + start_pc as u32, + )); + } other => { return Err(self.trap( TRAP_TYPE, @@ -491,7 +544,7 @@ impl VirtualMachine { // Validate that the heap object is indeed a Closure. let header = self.heap.header(href).ok_or_else(|| { self.trap( - TRAP_OOB, + TRAP_INVALID_HANDLE, opcode as u16, format!("Invalid heap handle in CALL_CLOSURE: {:?}", href), start_pc as u32, @@ -507,13 +560,19 @@ impl VirtualMachine { } // Pop user arguments from the operand stack (top-first), then fix order. - let mut user_args: Vec = Vec::with_capacity(user_arg_count); + let mut user_args: BudgetVec = + BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes); + user_args + .reserve(user_arg_count) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; for _ in 0..user_arg_count { - user_args.push(self.pop_trap( - opcode, - start_pc as u32, - "CALL_CLOSURE argument stack underflow", - )?); + user_args + .push(self.pop_trap( + opcode, + start_pc as u32, + "CALL_CLOSURE argument stack underflow", + )?) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } user_args.reverse(); // Now in logical order: arg1..argN @@ -553,9 +612,9 @@ impl VirtualMachine { // Prepare the operand stack to match the direct CALL convention: // push hidden arg0 (closure_ref) followed by arg1..argN. - self.push(Value::HeapRef(href)); - for v in user_args.into_iter() { - self.push(v); + self.push_trap(Value::HeapRef(href), opcode, start_pc as u32)?; + for v in user_args.drain(..) { + self.push_trap(v, opcode, start_pc as u32)?; } let stack_base = self @@ -564,16 +623,22 @@ impl VirtualMachine { .checked_sub(callee_param_slots) .ok_or_else(|| LogicalFrameEndingReason::Panic("Stack underflow".into()))?; + self.operand_stack + .reserve(callee_local_slots) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + self.call_stack + .reserve(1) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; // Allocate and zero-init local slots for _ in 0..callee_local_slots { - self.operand_stack.push(Value::Null); + self.operand_stack + .push(Value::Null) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; } - self.call_stack.push(CallFrame { - return_pc: self.pc as u32, - stack_base, - func_idx: fn_id, - }); + self.call_stack + .push(CallFrame { return_pc: self.pc as u32, stack_base, func_idx: fn_id }) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; self.pc = callee_code_offset; } OpCode::PushConst => { @@ -589,31 +654,31 @@ impl VirtualMachine { start_pc as u32, ) })?; - self.push(val); + self.push_trap(val, opcode, start_pc as u32)?; } OpCode::PushI64 => { let val = instr .imm_i64() .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?; - self.push(Value::Int64(val)); + self.push_trap(Value::Int64(val), opcode, start_pc as u32)?; } OpCode::PushI32 => { let val = instr .imm_i32() .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?; - self.push(Value::Int32(val)); + self.push_trap(Value::Int32(val), opcode, start_pc as u32)?; } OpCode::PushF64 => { let val = instr .imm_f64() .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?; - self.push(Value::Float(val)); + self.push_trap(Value::Float(val), opcode, start_pc as u32)?; } OpCode::PushBool => { let val = instr .imm_u8() .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?; - self.push(Value::Boolean(val != 0)); + self.push_trap(Value::Boolean(val != 0), opcode, start_pc as u32)?; } OpCode::Pop => { self.pop_trap(opcode, start_pc as u32, "POP requires one operand")?; @@ -629,13 +694,13 @@ impl VirtualMachine { OpCode::Dup => { let val = self.peek_trap(opcode, start_pc as u32, "DUP requires one operand")?.clone(); - self.push(val); + self.push_trap(val, opcode, start_pc as u32)?; } OpCode::Swap => { let a = self.pop_trap(opcode, start_pc as u32, "SWAP requires two operands")?; let b = self.pop_trap(opcode, start_pc as u32, "SWAP requires two operands")?; - self.push(a); - self.push(b); + self.push_trap(a, opcode, start_pc as u32)?; + self.push_trap(b, opcode, start_pc as u32)?; } OpCode::Add => self.binary_op(opcode, start_pc as u32, |a, b| match (&a, &b) { (Value::String(_), _) | (_, Value::String(_)) => { @@ -802,7 +867,7 @@ impl VirtualMachine { OpCode::Not => { let val = self.pop_trap(opcode, start_pc as u32, "NOT requires one operand")?; if let Value::Boolean(b) = val { - self.push(Value::Boolean(!b)); + self.push_trap(Value::Boolean(!b), opcode, start_pc as u32)?; } else { return Err(self.trap( TRAP_TYPE, @@ -854,9 +919,13 @@ impl VirtualMachine { OpCode::Neg => { let val = self.pop_trap(opcode, start_pc as u32, "NEG requires one operand")?; match val { - Value::Int32(a) => self.push(Value::Int32(a.wrapping_neg())), - Value::Int64(a) => self.push(Value::Int64(a.wrapping_neg())), - Value::Float(a) => self.push(Value::Float(-a)), + Value::Int32(a) => { + self.push_trap(Value::Int32(a.wrapping_neg()), opcode, start_pc as u32)? + } + Value::Int64(a) => { + self.push_trap(Value::Int64(a.wrapping_neg()), opcode, start_pc as u32)? + } + Value::Float(a) => self.push_trap(Value::Float(-a), opcode, start_pc as u32)?, _ => { return Err(self.trap( TRAP_TYPE, @@ -873,10 +942,25 @@ impl VirtualMachine { .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? as usize; if idx >= self.globals.len() { - self.globals.resize(idx + 1, Value::Int32(0)); + self.globals + .resize( + idx.checked_add(1).ok_or_else(|| { + self.resource_trap( + self.budget.failure( + RuntimeResource::AuxiliaryBytes, + crate::resource_budget::ResourceReason::SizeOverflow, + u64::MAX, + ), + opcode, + start_pc as u32, + ) + })?, + Value::Int32(0), + ) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; } let val = self.globals[idx].clone(); - self.push(val); + self.push_trap(val, opcode, start_pc as u32)?; } OpCode::SetGlobal => { let idx = instr @@ -886,7 +970,22 @@ impl VirtualMachine { let val = self.pop_trap(opcode, start_pc as u32, "SET_GLOBAL requires one operand")?; if idx >= self.globals.len() { - self.globals.resize(idx + 1, Value::Int32(0)); + self.globals + .resize( + idx.checked_add(1).ok_or_else(|| { + self.resource_trap( + self.budget.failure( + RuntimeResource::AuxiliaryBytes, + crate::resource_budget::ResourceReason::SizeOverflow, + u64::MAX, + ), + opcode, + start_pc as u32, + ) + })?, + Value::Int32(0), + ) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; } self.globals[idx] = val; } @@ -915,7 +1014,7 @@ impl VirtualMachine { "Internal error: validated local slot not found in stack".into(), ) })?; - self.push(val); + self.push_trap(val, opcode, start_pc as u32)?; } OpCode::SetLocal => { let slot = instr @@ -942,6 +1041,8 @@ impl VirtualMachine { self.operand_stack[stack_idx] = val; } OpCode::Call => { + self.check_call_depth() + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; let func_id = instr .imm_u32() .map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))? @@ -971,17 +1072,25 @@ impl VirtualMachine { let stack_base = self.operand_stack.len() - callee.param_slots as usize; + let callee_local_slots = callee.local_slots; + let callee_offset = callee.code_offset; + self.operand_stack + .reserve(callee_local_slots as usize) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + self.call_stack + .reserve(1) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; // Allocate and zero-init local_slots - for _ in 0..callee.local_slots { - self.operand_stack.push(Value::Null); + for _ in 0..callee_local_slots { + self.operand_stack + .push(Value::Null) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; } - self.call_stack.push(CallFrame { - return_pc: self.pc as u32, - stack_base, - func_idx: func_id, - }); - self.pc = callee.code_offset as usize; + self.call_stack + .push(CallFrame { return_pc: self.pc as u32, stack_base, func_idx: func_id }) + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; + self.pc = callee_offset as usize; } OpCode::Ret => { let frame = self.call_stack.pop().ok_or_else(|| { @@ -1009,15 +1118,20 @@ impl VirtualMachine { } // Copy return values (preserving order: pop return_slots values, then reverse to push back) - let mut return_vals = Vec::with_capacity(return_slots); + let mut return_vals = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes); + return_vals + .reserve(return_slots) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; for _ in 0..return_slots { - return_vals.push(self.pop().map_err(LogicalFrameEndingReason::Panic)?); + return_vals + .push(self.pop().map_err(LogicalFrameEndingReason::Panic)?) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } return_vals.reverse(); self.operand_stack.truncate(frame.stack_base); - for val in return_vals { - self.push(val); + for val in return_vals.drain(..) { + self.push_trap(val, opcode, start_pc as u32)?; } self.pc = frame.return_pc as usize; } @@ -1052,7 +1166,9 @@ impl VirtualMachine { let args_count = syscall.args_count(); - let mut args = Vec::with_capacity(args_count); + let mut args = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes); + args.reserve(args_count) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; for _ in 0..args_count { let v = self.pop().map_err(|_e| { self.trap( @@ -1062,13 +1178,21 @@ impl VirtualMachine { pc_at_syscall, ) })?; - args.push(v); + args.push(v).map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } args.reverse(); let stack_height_before = self.operand_stack.len(); - let mut ret = crate::HostReturn::new(&mut self.operand_stack); - native.syscall(id, &args, &mut ret, ctx).map_err(|fault| match fault { + self.operand_stack + .reserve(syscall.results_count()) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + let mut ret = crate::HostReturn::new( + self.operand_stack.native_result_buffer(), + syscall.results_count(), + ); + let native_result = native.syscall(id, &args, &mut ret, ctx); + let return_overflow = ret.overflowed(); + native_result.map_err(|fault| match fault { VmFault::Trap(code, msg) => { self.trap(code, OpCode::Syscall as u16, msg, pc_at_syscall) } @@ -1080,13 +1204,14 @@ impl VirtualMachine { let stack_height_after = self.operand_stack.len(); let results_pushed = stack_height_after - stack_height_before; - if results_pushed != syscall.results_count() { + if return_overflow || results_pushed != syscall.results_count() { return Err(LogicalFrameEndingReason::Panic(format!( - "Syscall {} (0x{:08X}) results mismatch: expected {}, got {}", + "Syscall {} (0x{:08X}) results mismatch: expected {}, got {} (overflow={})", syscall.name(), id, syscall.results_count(), - results_pushed + results_pushed, + return_overflow ))); } } @@ -1105,7 +1230,9 @@ impl VirtualMachine { })?; let args_count = intrinsic.arg_slots(); - let mut args = Vec::with_capacity(args_count); + let mut args = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes); + args.reserve(args_count) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; for _ in 0..args_count { let value = self.pop().map_err(|_e| { self.trap( @@ -1115,46 +1242,56 @@ impl VirtualMachine { pc_at_intrinsic, ) })?; - args.push(value); + args.push(value).map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; } args.reverse(); - let results = (intrinsic.implementation)(&args, ctx).map_err(|err| match err { - crate::IntrinsicExecutionError::ArityMismatch { expected, got } => self.trap( - TRAP_INVALID_INTRINSIC, - OpCode::Intrinsic as u16, - format!( - "Intrinsic {}.{} argument mismatch: expected {}, got {}", - intrinsic.owner, intrinsic.name, expected, got + let _result_storage = self + .budget + .reserve( + RuntimeResource::TemporaryBytes, + self.budget + .bytes::(intrinsic.ret_slots(), RuntimeResource::TemporaryBytes) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?, + ) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + self.operand_stack + .reserve(intrinsic.ret_slots()) + .map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?; + let results = intrinsic + .validate_argument_values(&args) + .and_then(|()| (intrinsic.implementation)(&args, ctx)) + .map_err(|err| match err { + crate::IntrinsicExecutionError::ArityMismatch { expected, got } => self + .trap( + TRAP_INVALID_INTRINSIC, + OpCode::Intrinsic as u16, + format!( + "Intrinsic {}.{} argument mismatch: expected {}, got {}", + intrinsic.owner, intrinsic.name, expected, got + ), + pc_at_intrinsic, + ), + crate::IntrinsicExecutionError::TypeMismatch { index, expected } => self + .trap( + TRAP_TYPE, + OpCode::Intrinsic as u16, + format!( + "Intrinsic {}.{} argument {} type mismatch: expected {:?}", + intrinsic.owner, intrinsic.name, index, expected + ), + pc_at_intrinsic, + ), + crate::IntrinsicExecutionError::HardwareUnavailable => self.trap( + TRAP_UNAVAILABLE, + OpCode::Intrinsic as u16, + format!( + "Input query {}.{} unavailable", + intrinsic.owner, intrinsic.name + ), + pc_at_intrinsic, ), - pc_at_intrinsic, - ), - crate::IntrinsicExecutionError::TypeMismatch { index, expected } => self.trap( - TRAP_TYPE, - OpCode::Intrinsic as u16, - format!( - "Intrinsic {}.{} argument {} type mismatch: expected {:?}", - intrinsic.owner, intrinsic.name, index, expected - ), - pc_at_intrinsic, - ), - crate::IntrinsicExecutionError::InvalidBuiltinCarrier { - owner, - name, - carrier, - } => self.trap( - TRAP_INVALID_INTRINSIC, - OpCode::Intrinsic as u16, - format!( - "Intrinsic {}.{} received invalid builtin carrier {} for {}.{}", - intrinsic.owner, intrinsic.name, carrier, owner, name - ), - pc_at_intrinsic, - ), - crate::IntrinsicExecutionError::HardwareUnavailable => { - LogicalFrameEndingReason::Panic("Host feature unavailable".into()) - } - })?; + })?; intrinsic.validate_result_values(&results).map_err(|err| match err { crate::IntrinsicExecutionError::ArityMismatch { expected, got } => self.trap( @@ -1175,34 +1312,40 @@ impl VirtualMachine { ), pc_at_intrinsic, ), - crate::IntrinsicExecutionError::InvalidBuiltinCarrier { - owner, - name, - carrier, - } => self.trap( - TRAP_INVALID_INTRINSIC, + crate::IntrinsicExecutionError::HardwareUnavailable => self.trap( + TRAP_UNAVAILABLE, OpCode::Intrinsic as u16, - format!( - "Intrinsic {}.{} produced invalid builtin carrier {} for {}.{}", - intrinsic.owner, intrinsic.name, carrier, owner, name - ), + format!("Input query {}.{} unavailable", intrinsic.owner, intrinsic.name), pc_at_intrinsic, ), - crate::IntrinsicExecutionError::HardwareUnavailable => { - LogicalFrameEndingReason::Panic( - "Host feature unavailable while validating intrinsic results".into(), - ) - } })?; for value in results { - self.push(value); + self.push_trap(value, opcode, start_pc as u32)?; } } + OpCode::DynPack + | OpCode::DynUnpack + | OpCode::DynCanUnpack + | OpCode::RefIsExact + | OpCode::RefImplements + | OpCode::RefToTrait + | OpCode::RefCast => { + // Slot-only ProgramImage cannot authorize typed instructions. The + // v1 image/verifier integration supplies their execution context. + self.cycles += opcode.cycles(); + return Err(self.trap( + prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION, + opcode as u16, + "Instruction requires a validated Program Registry".into(), + start_pc as u32, + )); + } OpCode::FrameSync => { // Marks the logical end of a frame: consume cycles and signal to the driver self.cycles += OpCode::FrameSync.cycles(); - self.handle_safepoint(); + self.handle_safepoint() + .map_err(|error| self.resource_trap(error, opcode, start_pc as u32))?; return Err(LogicalFrameEndingReason::FrameSync); } } @@ -1218,6 +1361,13 @@ mod tests { use super::*; use std::sync::Arc; + fn entry_auxiliary_bytes() -> u64 { + (size_of::() + + size_of::>() + + size_of::() + + size_of::()) as u64 + } + fn new_test_vm(rom: Vec, constant_pool: Vec) -> VirtualMachine { let rom_len = rom.len() as u32; let mut vm = VirtualMachine::new(rom, constant_pool); @@ -1227,7 +1377,7 @@ mod tests { ..Default::default() }]); // Ensure tests start with a properly initialized main coroutine at func 0 - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); vm } use crate::HostReturn; @@ -1473,9 +1623,18 @@ mod tests { for i in 0..coro_count { let state = if i % 2 == 0 { CoroutineState::Ready } else { CoroutineState::Sleeping }; let wake = if state == CoroutineState::Sleeping { (i / 2) as u64 } else { 0 }; - let _c = vm.heap.allocate_coroutine(0, state, wake, vec![], vec![]); + let _c = vm + .heap + .allocate_coroutine( + 0, + state, + wake, + BudgetVec::fixture(vec![]), + BudgetVec::fixture(vec![]), + ) + .unwrap(); // Also allocate a tiny byte object to increase GC pressure. - let _b = vm.heap.allocate_object(ObjectKind::Bytes, &[i as u8]); + let _b = vm.heap.allocate_object(ObjectKind::Bytes, &[i as u8]).unwrap(); } // Sanity: allocations present @@ -1651,7 +1810,7 @@ mod tests { fn test_shared_string_clone_reuses_materialized_payload() { let mut vm = VirtualMachine::default(); let payload: Arc = "shared-global".into(); - vm.globals.push(Value::String(payload.clone())); + vm.globals.push(Value::String(payload.clone())).unwrap(); let cloned = vm.globals[0].clone(); match cloned { @@ -1738,7 +1897,7 @@ mod tests { ), ..Default::default() }; - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let mut native = MockNative; let mut ctx = HostContext::new(None); @@ -1789,7 +1948,7 @@ mod tests { ), ..Default::default() }; - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let mut native = MockNative; let mut ctx = HostContext::new(None); @@ -1834,7 +1993,7 @@ mod tests { ), ..Default::default() }; - vm2.prepare_call("0"); + vm2.prepare_call("0").expect("prepare entry call"); vm2.step(&mut native, &mut ctx).unwrap(); // CALL vm2.step(&mut native, &mut ctx).unwrap(); // PUSH_I64 vm2.step(&mut native, &mut ctx).unwrap(); // RET @@ -2083,7 +2242,7 @@ mod tests { } let mut native = TestNative; - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let result = vm.run_budget(100, &mut native, &mut ctx).expect("VM run failed"); assert_eq!(result.reason, LogicalFrameEndingReason::EndOfRom); } @@ -2120,7 +2279,7 @@ mod tests { let mut native = MultiReturnNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); // Ensure we have SYSTEM capability so we pass capability gate vm.set_capabilities(prometeu_hal::syscalls::caps::SYSTEM); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); @@ -2163,8 +2322,8 @@ mod tests { let mut native = VoidReturnNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); - vm.operand_stack.push(Value::Int32(100)); + vm.prepare_call("0").expect("prepare entry call"); + vm.operand_stack.push(Value::Int32(100)).unwrap(); vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(vm.pop().unwrap(), Value::Int32(100)); @@ -2204,7 +2363,7 @@ mod tests { let mut native = ArgCheckNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); // Ensure we have GFX capability so we reach type checking inside native handler vm.set_capabilities(prometeu_hal::syscalls::caps::GFX); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); @@ -2344,7 +2503,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); // Grant GFX capability so arg underflow is checked (capability gate is first) vm.set_capabilities(prometeu_hal::syscalls::caps::GFX); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); @@ -2399,30 +2558,34 @@ mod tests { } #[test] - fn test_intrinsic_input_pad_returns_builtin_carrier() { + fn test_removed_input_endpoint_is_an_unknown_intrinsic() { let rom = assemble("INTRINSIC 0x2000\nHALT").expect("assemble"); let mut vm = new_test_vm(rom, vec![]); let mut native = MockNative; let mut ctx = HostContext::new(None); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); - assert!(matches!(report.reason, LogicalFrameEndingReason::Halted)); - assert_eq!(vm.operand_stack, vec![Value::Int64(1)]); + assert!( + matches!(report.reason, LogicalFrameEndingReason::Trap(trap) if trap.code == TRAP_INVALID_INTRINSIC) + ); + assert!(vm.operand_stack.is_empty()); } #[test] fn test_intrinsic_input_touch_x_requires_hardware_context() { - let rom = assemble("INTRINSIC 0x2001\nINTRINSIC 0x2021\nHALT").expect("assemble"); + let rom = assemble("INTRINSIC 0x2134\nHALT").expect("assemble"); let mut vm = new_test_vm(rom, vec![]); let mut native = MockNative; let mut ctx = HostContext::new(None); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); match report.reason { - LogicalFrameEndingReason::Panic(msg) => { - assert!(msg.contains("Host feature unavailable")); + LogicalFrameEndingReason::Trap(trap) => { + assert_eq!(trap.code, TRAP_UNAVAILABLE); + assert_eq!(trap.pc, 0); + assert_eq!(trap.opcode, OpCode::Intrinsic as u16); } - other => panic!("Expected Panic due to missing hardware context, got {:?}", other), + other => panic!("Expected unavailable input trap, got {:?}", other), } } @@ -2475,7 +2638,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); // Grant GFX capability so arg underflow is checked (capability gate is first) vm.set_capabilities(prometeu_hal::syscalls::caps::GFX); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); @@ -2505,7 +2668,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); match report.reason { @@ -2534,7 +2697,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); // Any non-trap outcome is considered success here @@ -2574,7 +2737,7 @@ mod tests { let mut native = BadNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); match report.reason { LogicalFrameEndingReason::Panic(msg) => assert!(msg.contains("results mismatch")), @@ -2606,7 +2769,7 @@ mod tests { let mut native = BadNativeNoReturn; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).expect("run"); match report.reason { LogicalFrameEndingReason::Panic(msg) => assert!(msg.contains("results mismatch")), @@ -2696,7 +2859,7 @@ mod tests { assert_eq!(vm.program.rom.len(), 2); assert_eq!(vm.cycles, 0); - vm.prepare_boot_call(); + vm.prepare_boot_call().expect("prepare entry call"); let mut native = MockNative; let mut ctx = HostContext::new(None); let report = vm.run_budget(100, &mut native, &mut ctx).expect("run loaded PBX"); @@ -3189,7 +3352,7 @@ mod tests { }, ]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); assert_eq!(vm.operand_stack.last().unwrap(), &Value::Int32(30)); @@ -3241,7 +3404,7 @@ mod tests { }, ]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); // Stack should be [100, 200] @@ -3292,7 +3455,7 @@ mod tests { }, ]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); assert_eq!(vm.operand_stack.len(), 0); @@ -3357,7 +3520,7 @@ mod tests { }, ]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); match report.reason { @@ -3400,7 +3563,7 @@ mod tests { ..Default::default() }]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::EndOfRom); // RET pops return values and pushes them back on the caller stack (which is the sentinel frame's stack here). @@ -3461,7 +3624,7 @@ mod tests { }, ]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); @@ -3490,7 +3653,7 @@ mod tests { ..Default::default() }]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); match report.reason { @@ -3564,7 +3727,7 @@ mod tests { code_len: 50, ..Default::default() }]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(vm.pop().unwrap(), Value::Int32(2)); @@ -3602,7 +3765,7 @@ mod tests { code_len: 17, ..Default::default() }]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); @@ -3630,7 +3793,7 @@ mod tests { code_len: 14, ..Default::default() }]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); match report.reason { @@ -3664,7 +3827,7 @@ mod tests { vm.gc_alloc_threshold = 1; // Allocate an unreachable object (no roots referencing it) - let _orphan = vm.heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]); + let _orphan = vm.heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]).unwrap(); // +1 for the main coroutine allocated by new_test_vm assert_eq!(vm.heap.len(), 2); @@ -3704,11 +3867,11 @@ mod tests { vm.gc_alloc_threshold = 1; // Allocate two objects; make one a root by placing it on the operand stack - let rooted = vm.heap.allocate_object(ObjectKind::Bytes, &[9, 9]); - let unreachable = vm.heap.allocate_object(ObjectKind::Bytes, &[8, 8, 8]); + let rooted = vm.heap.allocate_object(ObjectKind::Bytes, &[9, 9]).unwrap(); + let unreachable = vm.heap.allocate_object(ObjectKind::Bytes, &[8, 8, 8]).unwrap(); // +1 for main coroutine assert_eq!(vm.heap.len(), 3); - vm.operand_stack.push(Value::HeapRef(rooted)); + vm.operand_stack.push(Value::HeapRef(rooted)).unwrap(); let mut native = MockNative; let mut ctx = HostContext::new(None); @@ -3746,7 +3909,7 @@ mod tests { vm.gc_alloc_threshold = 1; // Cycle 1: allocate one unreachable object - let _h1 = vm.heap.allocate_object(ObjectKind::Bytes, &[1]); + let _h1 = vm.heap.allocate_object(ObjectKind::Bytes, &[1]).unwrap(); // +1 for main coroutine assert_eq!(vm.heap.len(), 2); @@ -3762,7 +3925,7 @@ mod tests { assert_eq!(vm.heap.len(), 1); // Cycle 2: allocate again and collect again deterministically - let _h2 = vm.heap.allocate_object(ObjectKind::Bytes, &[2]); + let _h2 = vm.heap.allocate_object(ObjectKind::Bytes, &[2]).unwrap(); assert_eq!(vm.heap.len(), 2); // Second FRAME_SYNC should also be reached deterministically match vm.step(&mut native, &mut ctx) { @@ -3795,7 +3958,7 @@ mod tests { let count = 2048usize; // stress but still quick for i in 0..count { let byte = (i & 0xFF) as u8; - let _ = vm.heap.allocate_object(ObjectKind::Bytes, &[byte]); + let _ = vm.heap.allocate_object(ObjectKind::Bytes, &[byte]).unwrap(); } // +1 for main coroutine assert_eq!(vm.heap.len(), count + 1); @@ -3837,14 +4000,17 @@ mod tests { vm.gc_alloc_threshold = 1; // Allocate a heap object and a suspended coroutine that captures it on its stack - let captured = vm.heap.allocate_object(ObjectKind::Bytes, &[0xAA, 0xBB]); - let _coro = vm.heap.allocate_coroutine( - 0, - CoroutineState::Ready, - 0, - vec![Value::HeapRef(captured)], - vec![], - ); + let captured = vm.heap.allocate_object(ObjectKind::Bytes, &[0xAA, 0xBB]).unwrap(); + let _coro = vm + .heap + .allocate_coroutine( + 0, + CoroutineState::Ready, + 0, + BudgetVec::fixture(vec![Value::HeapRef(captured)]), + BudgetVec::fixture(vec![]), + ) + .unwrap(); assert_eq!( vm.heap.len(), @@ -3885,7 +4051,16 @@ mod tests { vm.gc_alloc_threshold = 1; // Allocate a finished coroutine with no external references - let finished = vm.heap.allocate_coroutine(0, CoroutineState::Finished, 0, vec![], vec![]); + let finished = vm + .heap + .allocate_coroutine( + 0, + CoroutineState::Finished, + 0, + BudgetVec::fixture(vec![]), + BudgetVec::fixture(vec![]), + ) + .unwrap(); assert!(vm.heap.is_valid(finished)); let mut native = MockNative; @@ -4379,8 +4554,8 @@ mod tests { vm.gc_alloc_threshold = 1; // Allocate a heap object and preload it onto main's operand stack so SPAWN consumes it as arg. - let captured = vm.heap.allocate_object(ObjectKind::Bytes, &[0xAB, 0xCD, 0xEF]); - vm.operand_stack.push(Value::HeapRef(captured)); + let captured = vm.heap.allocate_object(ObjectKind::Bytes, &[0xAB, 0xCD, 0xEF]).unwrap(); + vm.operand_stack.push(Value::HeapRef(captured)).unwrap(); let mut native = MockNative; let mut ctx = HostContext::new(None); @@ -4396,6 +4571,25 @@ mod tests { ); } + #[test] + fn call_closure_reports_invalid_generation_before_object_access() { + let mut rom = (OpCode::CallClosure as u16).to_le_bytes().to_vec(); + rom.extend_from_slice(&0u32.to_le_bytes()); + let mut vm = new_test_vm(rom, vec![]); + let old = vm.heap.alloc_closure(0, &[]).unwrap(); + vm.heap.sweep(); + let current = vm.heap.alloc_closure(0, &[]).unwrap(); + assert_eq!(old.index, current.index); + vm.operand_stack.push(Value::HeapRef(old)).unwrap(); + let mut native = MockNative; + let mut ctx = HostContext::new(None); + let result = vm.step(&mut native, &mut ctx).unwrap_err(); + let LogicalFrameEndingReason::Trap(trap) = result else { panic!("expected trap") }; + assert_eq!(trap.code, TRAP_INVALID_HANDLE); + assert_eq!(trap.pc, 0); + assert_eq!(trap.opcode, OpCode::CallClosure as u16); + } + #[test] fn test_make_closure_zero_captures() { use prometeu_bytecode::{FunctionMeta, Value}; @@ -4523,7 +4717,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); assert_eq!(vm.operand_stack.len(), 1); @@ -4572,7 +4766,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(100, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); assert_eq!(vm.operand_stack, vec![Value::Int32(42)]); @@ -4602,7 +4796,7 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(10, &mut native, &mut ctx).unwrap(); match report.reason { LogicalFrameEndingReason::Trap(info) => { @@ -4675,9 +4869,198 @@ mod tests { let mut native = MockNative; let mut ctx = HostContext::new(None); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let report = vm.run_budget(200, &mut native, &mut ctx).unwrap(); assert_eq!(report.reason, LogicalFrameEndingReason::Halted); assert_eq!(vm.operand_stack, vec![Value::Int32(55)]); } + #[test] + fn call_depth_includes_entry_and_traps_before_frame_growth() { + for limit in [1, 3, 1024] { + let limits = + RuntimeLimits::default().lowered(RuntimeResource::CallDepth, limit).unwrap(); + let mut vm = + VirtualMachine::with_runtime_limits(assemble("CALL 0").unwrap(), vec![], limits); + vm.program.functions = + Arc::from(vec![FunctionMeta { code_len: 6, ..Default::default() }]); + vm.prepare_call("0").unwrap(); + let report = + vm.run_budget(100_000, &mut MockNative, &mut HostContext::new(None)).unwrap(); + assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_RESOURCE_LIMIT && trap.pc == 0 && trap.opcode == OpCode::Call as u16)); + assert_eq!(vm.call_stack.len(), limit as usize); + } + } + + #[test] + fn entry_depth_zero_returns_typed_setup_failure_without_allocating() { + let limits = RuntimeLimits::default().lowered(RuntimeResource::CallDepth, 0).unwrap(); + let mut vm = VirtualMachine::with_runtime_limits(assemble("HALT").unwrap(), vec![], limits); + let error = vm.prepare_boot_call().unwrap_err(); + assert_eq!( + (error.resource, error.requested, error.limit), + (RuntimeResource::CallDepth, 1, 0) + ); + assert!(vm.call_stack.is_empty()); + assert!(vm.current_coro.is_none()); + assert_eq!(vm.heap.len(), 0); + } + + #[test] + fn scheduler_quota_failure_traps_at_sleep_instruction_without_switching_out() { + let limits = RuntimeLimits::default() + .lowered(RuntimeResource::AuxiliaryBytes, entry_auxiliary_bytes()) + .unwrap(); + let mut vm = + VirtualMachine::with_runtime_limits(assemble("SLEEP 1").unwrap(), vec![], limits); + vm.prepare_call("0").unwrap(); + let current = vm.current_coro; + let report = vm.run_budget(100, &mut MockNative, &mut HostContext::new(None)).unwrap(); + assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_RESOURCE_LIMIT && trap.pc == 0 && trap.opcode == OpCode::Sleep as u16)); + assert_eq!(vm.current_coro, current); + assert_eq!(vm.call_stack.len(), 1); + assert_eq!( + vm.heap.coroutine_data(current.unwrap()).unwrap().state, + CoroutineState::Running + ); + assert_eq!(vm.budget.used(RuntimeResource::AuxiliaryBytes), entry_auxiliary_bytes()); + } + + #[test] + fn closure_calls_obey_the_same_per_flow_depth_limit() { + let limits = RuntimeLimits::default().lowered(RuntimeResource::CallDepth, 1).unwrap(); + let mut vm = VirtualMachine::with_runtime_limits( + assemble("MAKE_CLOSURE fn=0, captures=0\nCALL_CLOSURE argc=0").unwrap(), + vec![], + limits, + ); + vm.prepare_call("0").unwrap(); + let report = vm.run_budget(100, &mut MockNative, &mut HostContext::new(None)).unwrap(); + assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_RESOURCE_LIMIT && trap.pc == 10 && trap.opcode == OpCode::CallClosure as u16)); + assert_eq!(vm.call_stack.len(), 1); + assert!(matches!(vm.operand_stack.as_slice(), [Value::HeapRef(_)])); + } + #[test] + fn operand_globals_and_frames_share_one_auxiliary_limit() { + let frame = entry_auxiliary_bytes(); + let slot = size_of::() as u64; + for (source, extra, expected_pc, expected_op) in [ + ("PUSH_I32 1\nPUSH_I32 2", slot, 6, OpCode::PushI32), + ("GET_GLOBAL 0\nGET_GLOBAL 1", 2 * slot, 6, OpCode::GetGlobal), + ("GET_GLOBAL 4294967295", 0, 0, OpCode::GetGlobal), + ("CALL 0", 0, 0, OpCode::Call), + ] { + let limits = RuntimeLimits::default() + .lowered(RuntimeResource::AuxiliaryBytes, frame + extra) + .unwrap(); + let code = assemble(source).unwrap(); + let mut vm = VirtualMachine::with_runtime_limits(code.clone(), vec![], limits); + vm.program.functions = + Arc::from(vec![FunctionMeta { code_len: code.len() as u32, ..Default::default() }]); + vm.prepare_boot_call().unwrap(); + let report = vm.run_budget(100, &mut MockNative, &mut HostContext::new(None)).unwrap(); + assert!( + matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_RESOURCE_LIMIT && trap.pc == expected_pc && trap.opcode == expected_op as u16), + "{source}" + ); + assert!(vm.budget.used(RuntimeResource::AuxiliaryBytes) <= frame + extra); + assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), 0); + assert_eq!(vm.call_stack.len(), 1); + } + } + + #[test] + fn suspended_buffers_keep_one_charge_and_are_not_guest_payload_bytes() { + let main = assemble("SPAWN fn=1, argc=0\nYIELD\nFRAME_SYNC\nHALT").unwrap(); + let child = assemble("PUSH_I32 9\nYIELD\nFRAME_SYNC\nHALT").unwrap(); + let mut code = main.clone(); + code.extend(&child); + let mut vm = VirtualMachine::new(code, vec![]); + vm.program.functions = Arc::from(vec![ + FunctionMeta { code_len: main.len() as u32, ..Default::default() }, + FunctionMeta { + code_offset: main.len() as u32, + code_len: child.len() as u32, + ..Default::default() + }, + ]); + vm.prepare_boot_call().unwrap(); + let budget = vm.budget.clone(); + let mut heap_bytes_after_spawn = 0; + for step in 0..6 { + let outcome = vm.step(&mut MockNative, &mut HostContext::new(None)); + assert!(matches!(outcome, Ok(()) | Err(LogicalFrameEndingReason::FrameSync))); + let active = vm.operand_stack.capacity() * size_of::() + + vm.call_stack.capacity() * size_of::() + + vm.globals.capacity() * size_of::(); + let expected = active as u64 + + vm.heap.execution_capacity_bytes() + + vm.scheduler.capacity_bytes() + + vm.heap.table_capacity_bytes(); + assert_eq!(budget.used(RuntimeResource::AuxiliaryBytes), expected, "step {step}"); + assert_eq!(budget.used(RuntimeResource::TemporaryBytes), 0); + let heap_bytes = vm.heap.used_bytes.load(std::sync::atomic::Ordering::Relaxed); + if step == 0 { + heap_bytes_after_spawn = heap_bytes; + } + assert_eq!( + heap_bytes, heap_bytes_after_spawn, + "execution buffers must not change guest object telemetry" + ); + } + assert!(vm.heap.execution_capacity_bytes() > size_of::() as u64); + drop(vm); + assert_eq!(budget.used(RuntimeResource::AuxiliaryBytes), 0); + } + + #[test] + fn operation_temporary_limit_fails_before_consuming_closure_captures() { + let limits = RuntimeLimits::default().lowered(RuntimeResource::TemporaryBytes, 0).unwrap(); + let mut vm = VirtualMachine::with_runtime_limits( + assemble("PUSH_I32 7\nMAKE_CLOSURE fn=0, captures=1").unwrap(), + vec![], + limits, + ); + vm.prepare_boot_call().unwrap(); + let before = vm.heap.len(); + let report = vm.run_budget(100, &mut MockNative, &mut HostContext::new(None)).unwrap(); + assert!( + matches!(report.reason, LogicalFrameEndingReason::Trap(trap) if trap.code == TRAP_RESOURCE_LIMIT && trap.pc == 6) + ); + assert_eq!(vm.operand_stack.as_slice(), &[Value::Int32(7)]); + assert_eq!(vm.heap.len(), before); + assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), 0); + } + #[test] + fn gc_scratch_failure_traps_before_sweep_and_preserves_current_coroutine() { + let limits = RuntimeLimits::default().lowered(RuntimeResource::TemporaryBytes, 0).unwrap(); + let mut vm = + VirtualMachine::with_runtime_limits(assemble("FRAME_SYNC").unwrap(), vec![], limits); + vm.prepare_boot_call().unwrap(); + vm.gc_alloc_threshold = 1; + let current = vm.current_coro.unwrap(); + let guest = vm.budget.used(RuntimeResource::GuestBytes); + let report = vm.run_budget(100, &mut MockNative, &mut HostContext::new(None)).unwrap(); + assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_RESOURCE_LIMIT && trap.pc == 0 && trap.opcode == OpCode::FrameSync as u16)); + assert!(vm.heap.is_valid(current)); + assert!(!vm.heap.header(current).unwrap().is_marked()); + assert_eq!(vm.budget.used(RuntimeResource::GuestBytes), guest); + assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), 0); + } + + #[test] + fn guest_quota_failure_does_not_publish_an_entry_context() { + let limits = RuntimeLimits::default().lowered(RuntimeResource::GuestBytes, 0).unwrap(); + let mut vm = VirtualMachine::with_runtime_limits(assemble("HALT").unwrap(), vec![], limits); + assert_eq!(vm.prepare_boot_call().unwrap_err().resource, RuntimeResource::GuestBytes); + assert!(vm.current_coro.is_none()); + assert!(vm.call_stack.is_empty()); + assert_eq!(vm.heap.len(), 0); + assert_eq!(vm.budget.used(RuntimeResource::GuestBytes), 0); + assert_eq!(vm.budget.used(RuntimeResource::HandleSlots), 0); + } } diff --git a/crates/console/prometeu-vm/src/virtual_machine/gc.rs b/crates/console/prometeu-vm/src/virtual_machine/gc.rs index 53b5bad..63b2cf7 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/gc.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/gc.rs @@ -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); - 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(&self, visitor: &mut V) { diff --git a/crates/console/prometeu-vm/src/virtual_machine/loader.rs b/crates/console/prometeu-vm/src/virtual_machine/loader.rs index 192b44f..440951e 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/loader.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/loader.rs @@ -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::() { 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(()) } } diff --git a/crates/console/prometeu-vm/src/virtual_machine/stack.rs b/crates/console/prometeu-vm/src/virtual_machine/stack.rs index 2a098d6..a1f0519 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/stack.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/stack.rs @@ -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 { @@ -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)), diff --git a/crates/console/prometeu-vm/src/vm_init_error.rs b/crates/console/prometeu-vm/src/vm_init_error.rs index c43da03..550c614 100644 --- a/crates/console/prometeu-vm/src/vm_init_error.rs +++ b/crates/console/prometeu-vm/src/vm_init_error.rs @@ -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), } diff --git a/crates/console/prometeu-vm/tests/builtin_contract.rs b/crates/console/prometeu-vm/tests/builtin_contract.rs new file mode 100644 index 0000000..c205ab7 --- /dev/null +++ b/crates/console/prometeu-vm/tests/builtin_contract.rs @@ -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::(), align_of::(), 1, true), + ("Dynamic", 0x15, size_of::(), align_of::(), 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()); +} diff --git a/crates/console/prometeu-vm/tests/syscall_input_removed.rs b/crates/console/prometeu-vm/tests/syscall_input_removed.rs index 9b54dbd..7467525 100644 --- a/crates/console/prometeu-vm/tests/syscall_input_removed.rs +++ b/crates/console/prometeu-vm/tests/syscall_input_removed.rs @@ -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"); diff --git a/crates/dev/prometeu-layer-tests/tests/scheduler_determinism.rs b/crates/dev/prometeu-layer-tests/tests/scheduler_determinism.rs index 950bee9..47259c6 100644 --- a/crates/dev/prometeu-layer-tests/tests/scheduler_determinism.rs +++ b/crates/dev/prometeu-layer-tests/tests/scheduler_determinism.rs @@ -40,7 +40,7 @@ fn scheduler_wake_and_ready_order_is_deterministic() { let run_once = || -> Vec { let mut vm = VirtualMachine::new(rom.clone(), vec![]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let mut native = NoopNative; let mut ctx = HostContext::new(None); let mut reasons = Vec::new(); diff --git a/crates/dev/prometeu-layer-tests/tests/vm_exec_valid.rs b/crates/dev/prometeu-layer-tests/tests/vm_exec_valid.rs index 0311d8e..cfa2933 100644 --- a/crates/dev/prometeu-layer-tests/tests/vm_exec_valid.rs +++ b/crates/dev/prometeu-layer-tests/tests/vm_exec_valid.rs @@ -41,7 +41,7 @@ fn vm_executes_valid_program_in_slices() { // Construct VM directly; assume input is already verified. let mut vm = VirtualMachine::new(rom, vec![]); - vm.prepare_call("0"); + vm.prepare_call("0").expect("prepare entry call"); let mut native = NoopNative; let mut ctx = HostContext::new(None); diff --git a/crates/host/prometeu-host-headless/src/automation/wire.rs b/crates/host/prometeu-host-headless/src/automation/wire.rs index c67e74a..f58c0bd 100644 --- a/crates/host/prometeu-host-headless/src/automation/wire.rs +++ b/crates/host/prometeu-host-headless/src/automation/wire.rs @@ -264,7 +264,9 @@ fn stack_value(value: &VmValue) -> Value { VmValue::Float(v) => json!({"kind":"float64","value":v.to_string()}), VmValue::Boolean(v) => json!({"kind":"boolean","value":v}), VmValue::String(v) => json!({"kind":"string","value":v.as_ref()}), - VmValue::HeapRef(v) => json!({"kind":"heap_ref","value":v.0}), + VmValue::HeapRef(v) => { + json!({"kind":"heap_ref","value":{"index":v.index,"generation":v.generation}}) + } VmValue::Null => json!({"kind":"null","value":null}), } } @@ -287,6 +289,7 @@ pub(super) fn fault(report: &CrashReport) -> Value { } CrashReport::VmInit { error } => { let code = match error { + VmInitError::RuntimeResourceLimit(_) => "runtime_resource_limit", VmInitError::InvalidFormat => "invalid_format", VmInitError::UnsupportedFormat => "unsupported_format", VmInitError::ImageLoadFailed(_) => "image_load_failed", diff --git a/crates/host/prometeu-host-headless/tests/deterministic_control.rs b/crates/host/prometeu-host-headless/tests/deterministic_control.rs index 8372f17..320d946 100644 --- a/crates/host/prometeu-host-headless/tests/deterministic_control.rs +++ b/crates/host/prometeu-host-headless/tests/deterministic_control.rs @@ -481,9 +481,7 @@ fn color_program() -> Vec { PUSH_I32 0\n\ HOSTCALL 1\n\ POP_N 1\n\ - INTRINSIC 0x2000\n\ - INTRINSIC 0x2013\n\ - INTRINSIC 0x2032\n")); + INTRINSIC 0x210E\n")); let jump_to_blue = rom.len() + 2; rom.extend(asm("JMP_IF_FALSE 0")); rom.extend(asm(&format!("PUSH_I64 {RED}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); @@ -495,17 +493,17 @@ fn color_program() -> Vec { fn input_edge_program() -> Vec { let mut rom = Vec::new(); - let pressed_jump = emit_right_predicate(&mut rom, 0x2030); + let pressed_jump = emit_right_predicate(&mut rom, 0x210C); rom.extend(asm(&format!("PUSH_I64 {RED}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); let released_at = rom.len() as u32; rom[pressed_jump..pressed_jump + 4].copy_from_slice(&released_at.to_le_bytes()); - let released_jump = emit_right_predicate(&mut rom, 0x2031); + let released_jump = emit_right_predicate(&mut rom, 0x210D); rom.extend(asm(&format!("PUSH_I64 {GREEN}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); let down_at = rom.len() as u32; rom[released_jump..released_jump + 4].copy_from_slice(&down_at.to_le_bytes()); - let down_jump = emit_right_predicate(&mut rom, 0x2032); + let down_jump = emit_right_predicate(&mut rom, 0x210E); rom.extend(asm(&format!("PUSH_I64 {BLUE}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); let idle_at = rom.len() as u32; rom[down_jump..down_jump + 4].copy_from_slice(&idle_at.to_le_bytes()); @@ -515,7 +513,7 @@ fn input_edge_program() -> Vec { } fn emit_right_predicate(rom: &mut Vec, predicate: u32) -> usize { - rom.extend(asm(&format!("INTRINSIC 0x2000\nINTRINSIC 0x2013\nINTRINSIC {predicate:#x}\n"))); + rom.extend(asm(&format!("INTRINSIC {predicate:#x}\n"))); let target = rom.len() + 2; rom.extend(asm("JMP_IF_FALSE 0\n")); target @@ -523,17 +521,17 @@ fn emit_right_predicate(rom: &mut Vec, predicate: u32) -> usize { fn touch_edge_program() -> Vec { let mut rom = Vec::new(); - let pressed_jump = emit_touch_predicate(&mut rom, 0x2030); + let pressed_jump = emit_touch_predicate(&mut rom, 0x2130); rom.extend(asm(&format!("PUSH_I64 {RED}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); let released_at = rom.len() as u32; rom[pressed_jump..pressed_jump + 4].copy_from_slice(&released_at.to_le_bytes()); - let released_jump = emit_touch_predicate(&mut rom, 0x2031); + let released_jump = emit_touch_predicate(&mut rom, 0x2131); rom.extend(asm(&format!("PUSH_I64 {GREEN}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); let down_at = rom.len() as u32; rom[released_jump..released_jump + 4].copy_from_slice(&down_at.to_le_bytes()); - let down_jump = emit_touch_predicate(&mut rom, 0x2032); + let down_jump = emit_touch_predicate(&mut rom, 0x2132); rom.extend(asm(&format!("PUSH_I64 {BLUE}\nHOSTCALL 0\nFRAME_SYNC\nRET\n"))); let idle_at = rom.len() as u32; rom[down_jump..down_jump + 4].copy_from_slice(&idle_at.to_le_bytes()); @@ -544,9 +542,9 @@ fn touch_edge_program() -> Vec { fn emit_touch_predicate(rom: &mut Vec, predicate: u32) -> usize { rom.extend(asm(&format!( - "INTRINSIC 0x2001\nINTRINSIC 0x2020\nINTRINSIC {predicate:#x}\n\ - INTRINSIC 0x2001\nINTRINSIC 0x2021\nPUSH_I64 123\nEQ\nAND\n\ - INTRINSIC 0x2001\nINTRINSIC 0x2022\nPUSH_I64 45\nEQ\nAND\n" + "INTRINSIC {predicate:#x}\n\ + INTRINSIC 0x2134\nPUSH_I64 123\nEQ\nAND\n\ + INTRINSIC 0x2135\nPUSH_I64 45\nEQ\nAND\n" ))); let target = rom.len() + 2; rom.extend(asm("JMP_IF_FALSE 0\n")); diff --git a/crates/host/prometeu-host-headless/tests/input_queries.rs b/crates/host/prometeu-host-headless/tests/input_queries.rs new file mode 100644 index 0000000..c6ff37a --- /dev/null +++ b/crates/host/prometeu-host-headless/tests/input_queries.rs @@ -0,0 +1,231 @@ +//! Independent catalogue and execution oracles for the fixed input ABI. +use prometeu_bytecode::{TRAP_INVALID_INTRINSIC, TRAP_UNAVAILABLE, Value, assembler::assemble}; +use prometeu_drivers::TestPlatform; +use prometeu_hal::{InputSignals, RuntimePlatform, vm_fault::VmFault}; +use prometeu_vm::{ + AbiType, BuiltinScalarType, HostContext, HostReturn, IntrinsicExecutionError, + LogicalFrameEndingReason, NativeInterface, VirtualMachine, lookup_builtin_type, + lookup_intrinsic, lookup_intrinsic_by_id, +}; + +const BUTTONS: [(&str, u32); 13] = [ + ("input.pad.up", 0x2100), + ("input.pad.down", 0x2104), + ("input.pad.left", 0x2108), + ("input.pad.right", 0x210C), + ("input.pad.a", 0x2110), + ("input.pad.b", 0x2114), + ("input.pad.x", 0x2118), + ("input.pad.y", 0x211C), + ("input.pad.l", 0x2120), + ("input.pad.r", 0x2124), + ("input.pad.start", 0x2128), + ("input.pad.select", 0x212C), + ("input.touch.button", 0x2130), +]; + +struct NoSyscalls; +impl NativeInterface for NoSyscalls { + fn syscall( + &mut self, + _: u32, + _: &[Value], + _: &mut HostReturn, + _: &mut HostContext, + ) -> Result<(), VmFault> { + panic!("input must not invoke a syscall") + } +} + +#[test] +fn catalogue_has_exact_names_ids_signatures_and_no_endpoint_aliases() { + let mut ids = std::collections::BTreeSet::new(); + for (owner, base) in BUTTONS { + for (offset, property) in ["pressed", "released", "down", "hold"].into_iter().enumerate() { + let meta = lookup_intrinsic(owner, property, 1).unwrap(); + assert_eq!(meta.id, base + offset as u32); + assert!(ids.insert(meta.id)); + assert_eq!(lookup_intrinsic_by_id(meta.id).unwrap().key(), meta.key()); + assert!(meta.arg_layout.is_empty()); + assert_eq!( + meta.ret_layout, + &[AbiType::Scalar(if offset == 3 { + BuiltinScalarType::Int64 + } else { + BuiltinScalarType::Bool + })] + ); + assert!(meta.deterministic); + assert!(!meta.may_allocate); + assert!(lookup_intrinsic(owner, property, 2).is_none()); + } + } + for (name, id) in [("x", 0x2134), ("y", 0x2135)] { + let meta = lookup_intrinsic("input.touch", name, 1).unwrap(); + assert_eq!(meta.id, id); + assert!(ids.insert(id)); + assert!(meta.arg_layout.is_empty()); + assert_eq!(meta.ret_layout, &[AbiType::Scalar(BuiltinScalarType::Int64)]); + } + assert_eq!(ids.len(), 54); + assert!(lookup_intrinsic_by_id(0x2136).is_none()); + for id in [0x2000, 0x2001] + .into_iter() + .chain(0x2010..=0x201B) + .chain(0x2020..=0x2022) + .chain(0x2030..=0x2033) + { + assert!(lookup_intrinsic_by_id(id).is_none(), "removed ID {id:#x}"); + } + for name in ["input.pad", "input.touch", "input.button", "input.touch.button"] { + assert!(lookup_builtin_type(name, 1).is_none()); + } + for (owner, name) in [ + ("input", "pad"), + ("input", "touch"), + ("input.pad", "up"), + ("input.touch", "button"), + ("input.button", "down"), + ] { + assert!(lookup_intrinsic(owner, name, 1).is_none()); + } + let csv = include_str!("../../../../docs/vm-arch/INTRINSICS.csv"); + let mut documented = std::collections::BTreeSet::new(); + for row in csv.lines().skip(1) { + let columns: Vec<_> = row.split(',').collect(); + if !columns[2].starts_with("input.") { + continue; + } + let id = columns[1].parse::().unwrap(); + let meta = lookup_intrinsic(columns[4], columns[5], 1).unwrap(); + assert_eq!(id, meta.id); + assert_eq!(columns[2], format!("{}.{}", meta.owner, meta.name)); + assert_eq!(&columns[6..9], &["0", "1", ""]); + assert_eq!( + columns[9], + if meta.name == "hold" || meta.owner == "input.touch" { "Int64" } else { "Bool" } + ); + assert!(documented.insert(id)); + } + assert_eq!(ids, documented); +} + +fn set_frame(platform: &mut TestPlatform, active: Option) { + let mut signals = InputSignals { x_pos: -123, y_pos: i32::MAX, ..Default::default() }; + if let Some(index) = active { + let fields = [ + &mut signals.up_signal, + &mut signals.down_signal, + &mut signals.left_signal, + &mut signals.right_signal, + &mut signals.a_signal, + &mut signals.b_signal, + &mut signals.x_signal, + &mut signals.y_signal, + &mut signals.l_signal, + &mut signals.r_signal, + &mut signals.start_signal, + &mut signals.select_signal, + &mut signals.f_signal, + ]; + *fields[index] = true; + } + platform.input_mut().pad_mut().begin_frame(&signals); + platform.input_mut().touch_mut().begin_frame(&signals); +} + +fn query_all(platform: &mut TestPlatform) -> Vec { + let mut ctx = HostContext::new(Some(platform)); + (0x2100..=0x2135) + .map(|id| { + let meta = lookup_intrinsic_by_id(id).unwrap(); + let result = (meta.implementation)(&[], &mut ctx).unwrap(); + meta.validate_result_values(&result).unwrap(); + result.into_iter().next().unwrap() + }) + .collect() +} + +#[test] +fn every_button_uses_its_own_snapshot_and_queries_do_not_advance_frames() { + let mut platform = TestPlatform::new(); + for active in 0..13 { + set_frame(&mut platform, None); + for (pressed, released, down, hold) in [ + (true, false, true, 1), + (false, false, true, 2), + (false, true, false, 0), + (false, false, false, 0), + ] { + set_frame(&mut platform, down.then_some(active)); + let result = query_all(&mut platform); + assert_eq!(query_all(&mut platform), result, "same-frame queries changed state"); + for index in 0..13 { + let values = if index == active { + (pressed, released, down, hold) + } else { + (false, false, false, 0) + }; + assert_eq!( + &result[index * 4..index * 4 + 4], + &[ + Value::Boolean(values.0), + Value::Boolean(values.1), + Value::Boolean(values.2), + Value::Int64(values.3), + ], + "button {index}, active {active}" + ); + } + assert_eq!(&result[52..], &[Value::Int64(-123), Value::Int64(i32::MAX as i64)]); + } + } +} + +#[test] +fn vm_queries_charge_one_cycle_each_and_allocate_no_guest_objects() { + let mut source = String::new(); + for id in 0x2100..=0x2135 { + source.push_str(&format!("INTRINSIC {id}\n")); + } + let mut vm = VirtualMachine::new(assemble(&source).unwrap(), vec![]); + vm.prepare_call("0").expect("prepare entry call"); + let objects = vm.heap().len(); + let mut platform = TestPlatform::new(); + set_frame(&mut platform, Some(3)); + let expected = query_all(&mut platform); + let report = + vm.run_budget(54, &mut NoSyscalls, &mut HostContext::new(Some(&mut platform))).unwrap(); + assert_eq!(report.cycles_used, 54); + assert_eq!(report.steps_executed, 54); + assert_eq!(vm.heap().len(), objects); + // Inspection returns the topmost slot first. + assert_eq!(vm.operand_stack_top(54), expected.into_iter().rev().collect::>()); +} + +#[test] +fn every_known_query_faults_when_unavailable_and_rejects_arguments() { + for id in 0x2100..=0x2135 { + let meta = lookup_intrinsic_by_id(id).unwrap(); + let mut ctx = HostContext::new(None); + assert_eq!( + (meta.implementation)(&[], &mut ctx), + Err(IntrinsicExecutionError::HardwareUnavailable) + ); + assert_eq!( + (meta.implementation)(&[Value::Int64(0)], &mut ctx), + Err(IntrinsicExecutionError::ArityMismatch { expected: 0, got: 1 }) + ); + let mut vm = VirtualMachine::new(assemble(&format!("INTRINSIC {id}\n")).unwrap(), vec![]); + vm.prepare_call("0").expect("prepare entry call"); + let report = vm.run_budget(10, &mut NoSyscalls, &mut ctx).unwrap(); + assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_UNAVAILABLE && trap.pc == 0)); + assert!(vm.operand_stack_top(1).is_empty()); + } + let mut vm = VirtualMachine::new(assemble("INTRINSIC 0x2136\n").unwrap(), vec![]); + vm.prepare_call("0").expect("prepare entry call"); + let report = vm.run_budget(10, &mut NoSyscalls, &mut HostContext::new(None)).unwrap(); + assert!(matches!(report.reason, LogicalFrameEndingReason::Trap(trap) + if trap.code == TRAP_INVALID_INTRINSIC)); +} diff --git a/crates/tools/prometeu-cli/tests/automation_process.rs b/crates/tools/prometeu-cli/tests/automation_process.rs index 1bdcd37..3c36650 100644 --- a/crates/tools/prometeu-cli/tests/automation_process.rs +++ b/crates/tools/prometeu-cli/tests/automation_process.rs @@ -340,7 +340,7 @@ fn lifecycle_and_capture_match_the_in_process_canonical_oracle() { #[test] fn independent_processes_match_pixels_ids_state_and_fault() { // Color is selected by the real normalized RIGHT-down intrinsic. - let mut code = assemble("INTRINSIC 0x2000\nINTRINSIC 0x2013\nINTRINSIC 0x2032\n").unwrap(); + let mut code = assemble("INTRINSIC 0x210E\n").unwrap(); let jump = code.len() + 2; code.extend( assemble("JMP_IF_FALSE 0\nPUSH_I64 4278190335\nHOSTCALL 0\nFRAME_SYNC\nRET\n").unwrap(), diff --git a/docs/specs/runtime/01-time-model-and-cycles.md b/docs/specs/runtime/01-time-model-and-cycles.md index 6b6dc33..97d06cd 100644 --- a/docs/specs/runtime/01-time-model-and-cycles.md +++ b/docs/specs/runtime/01-time-model-and-cycles.md @@ -154,3 +154,7 @@ Used: 5,612 cycles Primary cause: enemy.updateAI(): 1,012 cycles ``` + +## Program Registry v1 integration + +The [Dynamic/reference instruction table](02c-program-registry-and-pbx-v1.md#dynamic-and-reference-instructions) specifies fixed costs of 8/4/3/4/4/5/4 cycles for opcodes 0x0090–0x0096. Each decoded attempt charges once, including a trap. Input retains the existing one-cycle INTRINSIC rule. Load work quotas are deterministic analysis units, not execution cycles or a wall-clock timeout. diff --git a/docs/specs/runtime/02-vm-instruction-set.md b/docs/specs/runtime/02-vm-instruction-set.md index 649de59..5bc30ae 100644 --- a/docs/specs/runtime/02-vm-instruction-set.md +++ b/docs/specs/runtime/02-vm-instruction-set.md @@ -127,3 +127,7 @@ The PVM is: - closure-capable; - coroutine-capable; - embedded inside the larger PROMETEU console model, not equal to it. + +## Program Registry v1 integration + +[Program Registry and PBX v1](02c-program-registry-and-pbx-v1.md) defines typed immediate domains, opcodes 0x0090–0x0096, complete multi-slot groups and conservative type-flow verification. Unknown facts require runtime validation and do not alone reject an otherwise valid join. Opcode bytes remain u16; TypeId immediates are u32 little-endian. diff --git a/docs/specs/runtime/02a-vm-values-and-calling-convention.md b/docs/specs/runtime/02a-vm-values-and-calling-convention.md index ae23586..e8280b5 100644 --- a/docs/specs/runtime/02a-vm-values-and-calling-convention.md +++ b/docs/specs/runtime/02a-vm-values-and-calling-convention.md @@ -13,7 +13,8 @@ All runtime values are stored in VM slots as `Value`. | Type | Description | | ------- | --------------------- | -| `int` | 64-bit signed integer | +| `Int32` | 32-bit signed integer | +| `Int64` | 64-bit signed integer | | `bool` | Boolean value | | `float` | 64-bit floating point | @@ -114,3 +115,17 @@ There is no separate public ISA for manual frame stack manipulation. - [`02b-vm-function-values-and-closures.md`](02b-vm-function-values-and-closures.md) defines first-class function values. - [`03-memory-stack-heap-and-allocation.md`](03-memory-stack-heap-and-allocation.md) defines stack/heap and handle-backed objects. - [`16-host-abi-and-syscalls.md`](16-host-abi-and-syscalls.md) reuses the same slot-oriented argument/return philosophy for syscalls. + +## Typed signatures and frames + + +Derive P=sum(parameter widths), L=local_slots and R=sum(result widths). A frame contains parameters, L extra locals then temporaries. GET/SET_LOCAL address only P+L; operand operations cannot consume below the temporary base. CALL consumes P caller temporaries in signature/component order and validates complete groups before publishing the frame. Concrete method receiver is the first declared parameter. No hidden closure receiver is invented by this rule. + +RET requires exactly R temporaries, validates ordered types, releases frame owners and returns R slots. Wrong height uses TRAP_BAD_RET_SLOTS; unproved runtime type mismatch uses TRAP_TYPE. Locals begin as Value::Null, not numeric defaults or Uninitialized. Parameter/local slots can be reused after entry; SET_LOCAL updates abstract knowledge. Definite assignment/source variable types remain frontend concerns. Do not introduce silent Int32/Int64, concrete/trait or concrete/Dynamic conversions at boundaries. + +Verifier decodes every function range, validates instruction boundaries/IDs/jump destinations, and analyzes structurally reachable control flow. Track temporary height and local/temporary facts in a finite domain: Unknown, known null, carrier facts and proven semantic/multi-slot groups. Initial parameters follow signatures; other locals start null. GET copies facts; SET replaces. Equal facts survive joins; conflicting facts become Unknown, while unequal heights are rejected. Do not require one permanent local type or infer unbounded unions/branch conditions. + +Use a bounded worklist to fixed point, reprocessing widened states; decide state-dependent type errors after convergence so visitation order cannot cause rejection. Signature contracts suffice for calls; no interprocedural body inference is required. Partial group mutation invalidates relational facts. Unknown is not proof of compatibility and does not alone reject code: retain complete runtime checks. Type knowledge alone does not eliminate null/generation/bounds checks. Retain current UnknownClosureCallee protection until the separate callable/closure/coroutine contract provides a replacement contract. Exceeding memory/work budget rejects loading, never skips verification. + + +Builtin IDs, exact slot carriers, nullable defaults and stored layouts are defined in [Program Registry and PBX v1](02c-program-registry-and-pbx-v1.md#values-storage-and-initialization). A fixed list of results does not imply a tuple object or boxing. Parameter names are optional signature metadata; frontend argument reordering must preserve source evaluation order. diff --git a/docs/specs/runtime/02c-program-registry-and-pbx-v1.md b/docs/specs/runtime/02c-program-registry-and-pbx-v1.md new file mode 100644 index 0000000..926c8ef --- /dev/null +++ b/docs/specs/runtime/02c-program-registry-and-pbx-v1.md @@ -0,0 +1,234 @@ +# Program Registry, Typed Values and PBX v1 + +Domain: VM program metadata, binary format, typed storage and resource boundaries +Function: normative + +This chapter publishes DEC-0045 through PLN-0176. It defines the required v1 contract; implementation is being migrated and this publication is not a claim that all execution paths already satisfy it. The callable/closure/coroutine type boundary remains separately specified: no provisional untyped reference type is authorized. Existing closure/coroutine behavior must be preserved until its approved replacement can be integrated. + +The requirements below use MUST/MUST NOT semantics. The Registry owns type/function identity, storage shapes and signatures; other chapters retain authority over scheduling, collection operations, cartridge boot and host ABI behavior outside these requirements. + + +## Registry, identities and signatures + +- Registry logically joins Type Registry and Function Registry. It belongs to ProgramImage, is immutable after successful resolution, and is shared across sessions. Metadata is native, outside the guest GC heap; it has no HeapRef and remains alive while any image consumer needs it. Code, constants, exports and debug are separate responsibilities. +- TypeId, FunctionId and ImplementationId are distinct u32 newtypes, dense zero-based table indices local to the image. TraitId is a TypeId checked as Trait, not a separate namespace. Field and method indices are owner-local. BuiltinId, IntrinsicId and constant-pool tags are separate domains. Names never establish identity. +- Struct/Trait are nominal; identical names/layouts do not merge them. Producers link references and deduplicate the same declaration across modules. Array identity is structural by element TypeId, invariant and mutable, with length on instances; nested arrays store array references. +- One descriptor per imported BuiltinId and per Array element TypeId; duplicate records are rejected without loader remapping. Forward references and nominal reference recursion are legal. Array-only cycles and infinite inline layouts, overflow and wrong-category references are rejected with bounded graph processing. Runtime object cycles remain legal. +- Four descriptor categories: Builtin, Struct, Array, Trait. No Service or input endpoint category. Services lower to a nominal Struct and one rooted global instance per session; singleton language rules stay in the frontend. Builtin-value trait adapters are explicit Struct allocations with separate nominal identity. +- Function signatures contain ordered typed parameters/results. Results have fixed semantic arity; zero results means zero slots, not null, and multiple results do not require tuple objects. A vec2 is one semantic result occupying two slots. Widths are derived, not duplicated in PBX. +- Parameter names are optional signature metadata independent of debug. Frontends reorder named arguments to signature order while preserving source evaluation order, using temporaries. Mixing/default-argument syntax and diagnostics stay with frontends; optional/default parameters are not implicitly introduced. +- Trait implementations are unique per (Struct, Trait), complete, immutable and ordered by method index. Explicit parameter/result TypeIds, order and arity match exactly; the implementation function has its concrete receiver as first parameter. No variance or implicit adaptation: frontend adapter functions may satisfy the exact signature. Actual trait dispatch belongs to the separate callable/closure/coroutine contract. + +## PBX v1 encoding + +PBX v1 only: magic `PBX\0`, little-endian, 32-byte header, zero reserved bytes and directory triples `(kind:u32, offset:u32, length:u32)`. Preserve existing non-Registry section encodings except expressly required changes. Reject unsupported versions, unknown/duplicate/missing sections, overlap, overflow, truncation and trailing table bytes. Section Code is raw instructions without a count prefix. + +| Kind | Section | Presence | +| --- | --- | --- | +| 0 | constants | exactly once | +| 1 | functions | exactly once | +| 2 | code | exactly once | +| 3 | debug | optional, at most once | +| 4 | exports | exactly once | +| 5 | syscalls | exactly once | +| 6 | types | exactly once | +| 7 | implementations | exactly once | + +The fixed header is: + +| Byte offset | Encoding | Meaning | +| --- | --- | --- | +| 0 | four bytes | `PBX\0` | +| 4 | u16 | version 1 | +| 6 | u8 | endianness 0 (little-endian) | +| 7 | u8 | reserved, zero | +| 8 | u32 | section count | +| 12 | 20 bytes | reserved, all zero | +| 32 | section_count × 12 bytes | directory triples | + +Directory offsets are file-relative. Section ranges must not overlap the header, +directory or other nonempty sections; empty Code does not make an executable entry. + +Unchanged section payload encodings are also field-by-field and little-endian: + +- Constants: count:u32, then tag:u8 plus payload. Tags are 0 Null (no payload), + 1 Int64 (i64), 2 Float64 (f64 bits), 3 Boolean (u8 0/1), 4 String + (byte_len:u32 then UTF-8), 5 Int32 (i32). UTF-8 must be valid, never repaired + with replacement characters. Constant quotas apply before materialization. +- Exports: count:u32, then FunctionId:u32, byte_len:u32 and UTF-8 symbol. +- Debug: span_count:u32, then records (pc:u32, file_id:u32, start:u32, end:u32); + name_count:u32, then records (FunctionId:u32, byte_len:u32, UTF-8 name). + Function references and code/source ranges must be valid. Debug names do not + replace optional signature names. +- Syscalls: count:u32, then module_byte_len:u16, UTF-8 module, + name_byte_len:u16, UTF-8 name, version:u16, arg_slots:u16, ret_slots:u16. + Canonical (module, name, version) identities are unique; binding resolution + remains a separate load phase. + +Empty tables use count zero where legal; section presence alone does not establish a valid boot entry. Preserve the existing boot-entry contract and validate it before execution. + +Encoding primitives: list=`count:u32` followed by records; optional name=`present:u8` (0/1), then `byte_len:u32 + UTF-8` if present. Absent and empty are distinct. Parameter/field=`type_id:u32 + optional name`; result=`type_id:u32`. Records have no padding. + +| Record | Exact field order | +| --- | --- | +| Type | kind:u8, optional name, category payload; tags 0 Builtin / 1 Struct / 2 Array / 3 Trait | +| Builtin payload | builtin_id:u32 | +| Struct payload | ordered field list | +| Array payload | element_type_id:u32 | +| Trait payload | ordered method list; each method has optional name, explicit-parameter list, result list; concrete receiver is not repeated here | +| Function | optional name, parameter list, result list, code_offset:u32, code_len:u32, local_slots:u32, max_stack_slots:u32 | +| Implementation | owner_type_id:u32, trait_type_id:u32, method_count:u32, FunctionIds:u32 in trait-method order; count must match | + +Code offsets are relative to Code. Local and temporary counts widen from current u16 without truncation. `max_stack_slots` counts temporaries only; zero means zero, never infer. No guest-controlled builtin layouts or raw Rust structures/pointers are serialized. Remove old/external compatibility; regeneration is mandatory. + +## Values, storage and initialization + +Value remains the generic stack/local representation. Structs/arrays are references; primitive and builtin-value array elements are stored directly. Layout descriptors distinguish value-slot width from heap-instance layout and distinguish GC references from native ownership. Derive offsets/defaults/maps once; access by indices/offsets rather than repeatedly traversing semantic metadata. Measure bitmap versus offset/block lists; changing the map encoding must not change the contract. + +| BuiltinId | Type | Value slots | Stored size / alignment | +| --- | --- | --- | --- | +| 0x0001 | color | 1 | 4 / 4 bytes | +| 0x0002 | vec2 | 2 | 16 / 8 bytes | +| 0x0003 | pixel | 3 | 24 / 8 bytes | +| 0x0010 | Int32 | 1 | 4 / 4 bytes | +| 0x0011 | Int64 | 1 | 8 / 8 bytes | +| 0x0012 | Float | 1 | 8 / 8 bytes, f64 | +| 0x0013 | Bool | 1 | 1 / 1 byte; canonical 0/1 | +| 0x0014 | String | 1 | native StringSlot size_of/align_of | +| 0x0015 | Dynamic | 2 | native DynamicSlot size_of/align_of | + +Unlisted BuiltinIds, including zero and removed input IDs 0x0100–0x0102, cannot be imported. No implicit Int/Number/Void/Null aliases. `color` is numeric RGBA8888 `0xRRGGBBAA`; its stack carrier is Int64 constrained to u32 range. `vec2` stores f64 x/y at offsets 0/8. `pixel` stores i64 x/y at 0/8 and u32 color at 16 plus four padding bytes, with stack carriers [Int64,Int64,Int64]. Do not narrow coordinates or infer framebuffer byte order from the numeric color representation. These builtins contain no guest references/native ownership. + +Preserve declared field order; round each offset to field alignment and final payload size to maximum alignment. Empty Struct has payload zero, alignment one, but retains object identity/header. Array stride is aligned stored-element size; Array stores references, not Struct payloads. Checked arithmetic precedes allocation. Padding is not data or a GC reference. Allocate truly aligned storage or use appropriate accesses; Vec does not justify unsafe casts. Native Arc/Value layouts are target-dependent and never a portable PBX ABI. + +Defaults apply before publication/GC, including reused storage: Int32/Int64 zero, Float +0.0, Bool false, Struct/Array/String references null, vec2 (+0,+0), color transparent raw zero, pixel (0,0,transparent), trait absent, Dynamic untyped null. Do not instantiate referenced objects/subarrays or invoke source constructors. Empty arrays require no element values. Catalog defaults are resolved without arbitrary per-element callbacks. Native values must be constructed correctly rather than assumed valid after memset. + +Heap references: index:u32 + generation:u32, stored size/alignment 8, still one Value::HeapRef slot. Generation starts at one; zero is reserved. Compact null=(0,0), while index zero remains valid with a live generation. Other generation-zero pairs are invalid. Validate index/liveness/generation/type before dereference. Increment generation on reclamation; retire the slot after its maximum-generation incarnation rather than wrap. Handles belong to their heap/session; native boundaries preserve provenance without introducing a heap ID into this encoding. + +StringSlot is native Option>: None is null, Some(empty) is distinct. Reads/copies creating owners clone Arc; moves transfer; overwrite/drop/GC releases the old owner. Validate before overwrite and clean up only initialized native slots after failure. GC must not follow Arc as HeapRef. + +Trait stack form: [HeapRef(receiver), Int64(ImplementationId)] with validated u32 ID and a coherent owner/trait pair; absence=[Null,Null]. Stored trait occupies 16 bytes, alignment 8: receiver at 0 (8 bytes), ImplementationId at 8 (4), padding at 12 (4). Stored absence uses null receiver plus ID zero; receiver discriminates absence, so implementation zero remains valid for present values. Null receiver with nonzero ID is malformed. GC follows only receiver. Copying/converting preserves object identity without a wrapper; methods invoked on absent views fault. + +## Dynamic and reference instructions + +Dynamic stack form is [type,payload], with Int64(u32 TypeId) or Null in the type slot. Native DynamicSlot={type_id:Option,payload:Value}. Untyped null=[Null,Null]; typed null=[Int64(T),Null] only for nullable T, including absent traits without boxing. Unknown type with present payload is invalid. Preserve exact semantic type despite shared physical carriers. + +Single-slot values use direct Value payload (including strings/references). Present multi-slot values use a HeapRef to an internal immutable box containing TypeId and exactly width(T) Values in Box<[Value]>. Its category is distinct from user Struct/Array/Closure. The box is not a public nominal type and cannot be accessed as a guest object/array. Root source values across allocation. Extraction checks category/type/width and copies complete slots, retaining contained aliases/native ownership. Dynamic copies share the box; repacking never nests or allocates another layer. + +Present extraction and typed-null extraction require exact TypeId. Untyped null extracts only into nullable types. Compatibility query differs from object-instance tests: null can be extractable while never an instance. Numeric/trait conversions are separate. Destination/source Dynamic identity validates and preserves its pair. + +| Opcode | Code | Immediate(s) | Stack effect | Cycles | +| --- | --- | --- | --- | --- | +| DYN_PACK T | 0x0090 | TypeId:u32 | slots(T) -> type,payload | 8 | +| DYN_UNPACK T | 0x0091 | TypeId:u32 | type,payload -> slots(T) | 4 | +| DYN_CAN_UNPACK T | 0x0092 | TypeId:u32 | type,payload -> Bool | 3 | +| REF_IS_EXACT | 0x0093 | source:TypeId, target:TypeId | slots(source) -> Bool | 4 | +| REF_IMPLEMENTS | 0x0094 | source:TypeId, trait:TypeId | slots(source) -> Bool | 4 | +| REF_TO_TRAIT | 0x0095 | source:TypeId, trait:TypeId | slots(source) -> slots(trait) | 5 | +| REF_CAST | 0x0096 | source:TypeId, target:TypeId | slots(source) -> HeapRef or Null | 4 | + +Opcodes are u16 and immediates little-endian u32: Dynamic instructions occupy 6 bytes, reference instructions 10. Types are immediate metadata validated by the loader, never arbitrary selector arguments. Queries consume their operands; producers preserve a copy in locals before querying then extracting. Compatibility returns false only for ordinary mismatch of a well-formed Dynamic; malformed carriers/boxes/handles fault. It does not allocate or copy the extracted value. + +Reference source must be Struct, Array or Trait; exact-test/cast target Struct or Array; implementation-test/conversion target Trait. Reject invalid immediate categories. Validate source type or complete trait pair before examining its underlying object. Null tests return false; conversions preserve null. Present incompatible casts/conversions fault. REF_TO_TRAIT resolves the underlying concrete type directly across traits; REF_CAST requires exact concrete type. No hierarchy, array covariance, structural reinterpretation, implicit numeric conversion or Dynamic-as-source shortcut. Arrays cannot implement traits because owners are Struct only. No operation allocates wrappers; tests do not create views. + +New Dynamic/reference instructions charge exactly once per decoded execution attempt, including traps. Costs are fixed for direct/box/null/identity and independent of host timing, optimizer, Arc/allocator or GC behavior. Do not double-charge in common VM dispatch. Load rejection executes no instruction. No intra-instruction preemption or global GC-cycle redesign is introduced. + +## Calls, locals and verifier + +Derive P=sum(parameter widths), L=local_slots and R=sum(result widths). A frame contains parameters, L extra locals then temporaries. GET/SET_LOCAL address only P+L; operand operations cannot consume below the temporary base. CALL consumes P caller temporaries in signature/component order and validates complete groups before publishing the frame. Concrete method receiver is the first declared parameter. No hidden closure receiver is invented by this rule. + +RET requires exactly R temporaries, validates ordered types, releases frame owners and returns R slots. Wrong height uses TRAP_BAD_RET_SLOTS; unproved runtime type mismatch uses TRAP_TYPE. Locals begin as Value::Null, not numeric defaults or Uninitialized. Parameter/local slots can be reused after entry; SET_LOCAL updates abstract knowledge. Definite assignment/source variable types remain frontend concerns. Do not introduce silent Int32/Int64, concrete/trait or concrete/Dynamic conversions at boundaries. + +Verifier decodes every function range, validates instruction boundaries/IDs/jump destinations, and analyzes structurally reachable control flow. Track temporary height and local/temporary facts in a finite domain: Unknown, known null, carrier facts and proven semantic/multi-slot groups. Initial parameters follow signatures; other locals start null. GET copies facts; SET replaces. Equal facts survive joins; conflicting facts become Unknown, while unequal heights are rejected. Do not require one permanent local type or infer unbounded unions/branch conditions. + +Use a bounded worklist to fixed point, reprocessing widened states; decide state-dependent type errors after convergence so visitation order cannot cause rejection. Signature contracts suffice for calls; no interprocedural body inference is required. Partial group mutation invalidates relational facts. Unknown is not proof of compatibility and does not alone reject code: retain complete runtime checks. Type knowledge alone does not eliminate null/generation/bounds checks. Retain current UnknownClosureCallee protection until the separate callable/closure/coroutine contract provides a replacement contract. Exceeding memory/work budget rejects loading, never skips verification. + +## Input + +Input endpoints are not guest values, even transiently: no locals/globals/fields/arrays/arguments/results/Dynamic/traits. Frontends lower names directly to INTRINSIC (u16 0x0072 + u32 ID). Each query takes zero arguments; states return Bool, hold/x/y Int64. Each reads the immutable logical-frame snapshot; hold counts frames. No guest allocation, no per-query hardware polling, no stack selector. Use the existing 1-cycle intrinsic charge without redesigning global intrinsic accounting. A known unavailable query faults, never fabricates false/zero. + +| Name prefix | pressed | released | down | hold | +| --- | --- | --- | --- | --- | +| input.pad.up | 0x2100 | 0x2101 | 0x2102 | 0x2103 | +| input.pad.down | 0x2104 | 0x2105 | 0x2106 | 0x2107 | +| input.pad.left | 0x2108 | 0x2109 | 0x210A | 0x210B | +| input.pad.right | 0x210C | 0x210D | 0x210E | 0x210F | +| input.pad.a | 0x2110 | 0x2111 | 0x2112 | 0x2113 | +| input.pad.b | 0x2114 | 0x2115 | 0x2116 | 0x2117 | +| input.pad.x | 0x2118 | 0x2119 | 0x211A | 0x211B | +| input.pad.y | 0x211C | 0x211D | 0x211E | 0x211F | +| input.pad.l | 0x2120 | 0x2121 | 0x2122 | 0x2123 | +| input.pad.r | 0x2124 | 0x2125 | 0x2126 | 0x2127 | +| input.pad.start | 0x2128 | 0x2129 | 0x212A | 0x212B | +| input.pad.select | 0x212C | 0x212D | 0x212E | 0x212F | +| input.touch.button | 0x2130 | 0x2131 | 0x2132 | 0x2133 | + +Full name=prefix.property; input.touch.x=0x2134 and y=0x2135. Total 54 entries. Prefixes are catalogue names, not guest types. Remove old endpoint intrinsics 0x2000/0x2001, 0x2010–0x201B, 0x2020–0x2022, 0x2030–0x2033 without aliases. + +## Errors and publication + +| Stable load tag | Meaning | +| --- | --- | +| LOAD_INVALID_FORMAT | Binary structure, flags/UTF-8, missing/duplicate/overlapping sections, truncation or trailing bytes | +| LOAD_UNSUPPORTED_FORMAT | Unsupported PBX version, section/category or BuiltinId; no fallback | +| LOAD_INVALID_REFERENCE | Out-of-range IDs or wrong referenced categories, including immediates | +| LOAD_INVALID_REGISTRY | Forbidden duplicates/cycles, incompatible implementation signatures or inconsistent layouts | +| LOAD_INVALID_CODE | Invalid instruction/control flow/heights/proven types or declared per-function limits | +| LOAD_RESOURCE_LIMIT | Load/verification quota, unrepresentable computed size or recoverable allocation failure | +| LOAD_INVALID_ENTRYPOINT | Missing or incompatible entry under the existing boot contract | + +Preserve typed reasons and available section/file offset, ID domain/record/member/parameter, function/PC and resource/requested/limit fields. Names/debug are optional; text is for humans, not parsing. Keep VerifierError structured inside VmInitError and preserve specific native/syscall resolution errors. Validate structure, records/references, layouts/signatures/implementations, bindings and code before publishing an executable image. Check budgets before dependent loops/allocations throughout. Report one error per attempt with deterministic traversal, but do not promise the same first error for multi-invalid inputs across revisions. Release partial state; never fabricate runtime PC/traps for load failures. + +| Runtime fault | Code | Meaning | +| --- | --- | --- | +| TRAP_OOB | 0x00000003 | Collection/field bounds; retain specific existing local errors | +| TRAP_TYPE | 0x00000004 | Mismatch, malformed carrier/pair, wrong box type/category or incompatible implementation | +| TRAP_NULL_REFERENCE | 0x0000000E | Valid absence used where an instance is required | +| TRAP_INVALID_HANDLE | 0x0000000F | Invalid index, dead slot, stale/invalid generation | +| TRAP_RESOURCE_LIMIT | 0x00000010 | Runtime quota/size overflow/recoverable allocation failure | +| TRAP_UNAVAILABLE | 0x00000011 | Known input query unavailable on the platform | + +Keep TrapInfo(code,opcode,pc,message,optional span) and current trap interruption behavior; no guest-catchable exception system. Untyped null to Int32 is TRAP_TYPE, not null dereference. Invalid handles never become false/null and are validated before following them. Preserve existing stack/function/return/intrinsic error categories. No promise to recover process/allocator abort; use fallible new allocation paths rather than operational panics. + +## Memory ownership and quotas + +Image constants and session-created strings are separate accounting domains; charge once per allocation identity, not text equality or reference count. Clone/pass/store shares Arc without recopying content; duplicate independent equal strings count separately. No global interning requirement. Native slots/containers pay their own storage; Registry names are not also guest constants. + +Each domain tracks UTF-8 bytes and allocation count using a bounded Weak ledger without a strong accounting owner. Reads remain direct Arc accesses, not handle-table lookups. Reconcile dead entries at safepoints and before quota refusal, release Weak plus charge then; Weak may retain native allocation after last strong drop, so no immediate-release promise. Keep accounting domains alive while tracked allocations survive, including host/tool retention. Import untracked native strings as charged new allocations; explicit session transfer copies into the destination domain, while shared-image constants remain shared. + +Check result sizes/overflow and reserve before materializing; roll back reservations and initialized state on failure. Charge transient buffer/result coexistence. No hidden unmetered concatenation/native result paths. Do not introduce reentrant GC or scheduling at arbitrary allocation points to satisfy quotas; dead guest objects stay charged until safe sweep. Layout headers, metadata, payload capacities, native ownership, auxiliary buffers and temporary storage must have explicit nonduplicated domains. Limits are not RSS or upfront reservations. + +LoadLimits uses the same defaults on hosts; cartridges cannot raise them. Test configurations may lower limits. MiB=1,048,576 bytes. + +| Load resource | Accepted limit | +| --- | --- | +| PBX file | 64 MiB | +| Code section | 32 MiB | +| Type records / function records | 65,536 each | +| Implementation records | 262,144 | +| Aggregate nested Registry records | 1,048,576 | +| Individual UTF-8 name | 4,096 bytes | +| Aggregate Registry names | 4 MiB | +| Retained resolved Registry | 64 MiB per image | +| Temporary load/verifier storage | 128 MiB | +| Parameters + locals + declared maximum temporaries per frame | 16,384 slots | +| Results per function | 1,024 slots | +| Load/verifier work | 268,435,456 units | + +Nested records count fields, methods, parameters/results and implementation FunctionIds. Registry charge includes capacities, names, resolution indices and reference maps, once per image, not each session. Input buffer is bounded separately from scratch. Temporary copies simultaneously alive count in their domains. Work charges one unit per input byte examined/copied/validated, record/edge processed, instruction transfer or abstract slot read/written/compared; reprocessing charges again. Charge proportional helpers, not only iterations; this is deterministic work, not wall time. Reject excess before work/reservation, never truncate/remap/infer away limits. + +| Runtime/image resource | Accepted limit | +| --- | --- | +| Retained guest objects | 256 MiB per session | +| Handle-table slots including free/retired | 1,048,576 per session | +| Runtime-created/imported UTF-8 content | 64 MiB per session | +| Tracked runtime string allocations | 262,144 per session | +| Constant-string UTF-8 content | 64 MiB per image | +| Constant-pool entries, all types | 1,048,576 per image | +| Individual UTF-8 string | 4 MiB | +| Persistent auxiliary execution state | 64 MiB per session | +| Runtime operation temporary buffers | 64 MiB per session | +| Call depth including entry frame | 1,024 frames per execution flow | + +Auxiliary state includes capacities of active/suspended Value stacks, frames, globals, handles/free lists, scheduler queues, string ledgers and persistent GC structures. Moving stacks between VM and coroutine transfers charges; never bill the same stack in both auxiliary and heap domains. Coroutine header/own payload remains heap-owned. Per-flow depth and per-frame cap coexist with aggregate session auxiliary cap. No separate task quota/lifecycle design is added here. Constants are checked at load; runtime excess traps. Growth must not geometrically reserve beyond available quota. Cycle/frame budgets remain separate. Asset and host admission limits are outside these totals. + diff --git a/docs/specs/runtime/03-memory-stack-heap-and-allocation.md b/docs/specs/runtime/03-memory-stack-heap-and-allocation.md index 92a755e..00c1f36 100644 --- a/docs/specs/runtime/03-memory-stack-heap-and-allocation.md +++ b/docs/specs/runtime/03-memory-stack-heap-and-allocation.md @@ -78,7 +78,7 @@ GateEntry { When an object is freed: - `alive` becomes false; -- `generation` is incremented. +- `generation` is incremented, or the slot is permanently retired after its maximum-generation incarnation; generation must never wrap. When a handle is used: @@ -109,7 +109,7 @@ Object { Properties: -- fields are stored in slot order; +- fields use declared-order typed offsets with alignment and padding; stack Value slots are not the heap layout; - there is no raw pointer arithmetic in the VM model; - layout rules are explicit and verifier/runtime-friendly. @@ -200,3 +200,7 @@ Any violation results in a trap or rejected program image. - [`15-asset-management.md`](15-asset-management.md) defines asset-bank surfaces outside VM heap ownership. - [`08-save-memory-and-memcard.md`](08-save-memory-and-memcard.md) defines save storage outside VM heap ownership. - [`16a-syscall-policies.md`](16a-syscall-policies.md) defines host-root rules and host/heap interaction policy. + +## Program Registry v1 integration + +[Program Registry and PBX v1](02c-program-registry-and-pbx-v1.md#values-storage-and-initialization) specifies compact typed layouts, defaults, native String/Dynamic ownership and generational handles. Its [memory quotas](02c-program-registry-and-pbx-v1.md#memory-ownership-and-quotas) apply jointly before allocation. Registry metadata is immutable image-owned native memory, outside guest GC. Stack/local execution retains Value. diff --git a/docs/specs/runtime/06-input-peripheral.md b/docs/specs/runtime/06-input-peripheral.md index b91b9dd..e8de7ec 100644 --- a/docs/specs/runtime/06-input-peripheral.md +++ b/docs/specs/runtime/06-input-peripheral.md @@ -53,7 +53,7 @@ Within the same frame: - `pressed: bool` (true only on transition up -> down in this frame) - `released: bool` (true only on transition down -> up in this frame) - `down: bool` (true while physically pressed in this frame snapshot) -- `hold: int` (count of consecutive pressed frames) +- `hold: Int64` (count of consecutive pressed frames) `pad` is a fixed set of buttons: @@ -63,8 +63,8 @@ Within the same frame: `touch` exposes: -- `x: int` -- `y: int` +- `x: Int64` +- `y: Int64` - `button` with the same `pressed/released/down/hold` semantics PROMETEU handheld v1 uses single-touch active pointer semantics. @@ -84,7 +84,7 @@ Input.pad().up().hold() Input.touch().x() ``` -The normative contract is intrinsic identity/version and semantics, not source syntax. +Source syntax MUST lower directly to one fixed zero-argument query; pad/touch/button are catalogue prefixes, not guest values. Endpoints cannot be stored or passed, even transiently. The complete 54-query table and removed IDs are specified in [Program Registry input](02c-program-registry-and-pbx-v1.md#input). Unknown IDs are invalid intrinsics; a known unavailable query raises TRAP_UNAVAILABLE (0x11), never a fabricated false/zero. ## 6 Determinism and Replay diff --git a/docs/specs/runtime/07-touch-peripheral.md b/docs/specs/runtime/07-touch-peripheral.md index 1999964..b39ff3b 100644 --- a/docs/specs/runtime/07-touch-peripheral.md +++ b/docs/specs/runtime/07-touch-peripheral.md @@ -37,19 +37,19 @@ TOUCH is **absolute**: ``` TOUCH: - present : bool down : bool pressed : bool released : bool - x : int - y : int + hold : Int64 + x : Int64 + y : Int64 ``` ### 3.2 Field Semantics -- **present** - - `true` if the TOUCH peripheral is available - - `false` if there is no physical touch (desktop, hardware without touch) +- **availability** + - is not a guest field or endpoint value + - a known query without a platform source raises `TRAP_UNAVAILABLE` - **down** - `true` while the active pointer is pressed - **pressed** @@ -169,3 +169,7 @@ Every PROMETEU game that uses TOUCH: - behaves identically on all platforms - does not depend on host-specific capabilities - does not suffer semantic variation between desktop, mobile, and dedicated hardware + +## Fixed query catalogue + +Touch button queries occupy 0x2130–0x2133 (pressed/released/down/hold); x/y use 0x2134/0x2135. All have zero arguments and read the frame snapshot. Button states return Bool; hold and coordinates return Int64. See the [complete input contract](02c-program-registry-and-pbx-v1.md#input). diff --git a/docs/specs/runtime/10-debug-inspection-and-profiling.md b/docs/specs/runtime/10-debug-inspection-and-profiling.md index 3bf6d29..137beca 100644 --- a/docs/specs/runtime/10-debug-inspection-and-profiling.md +++ b/docs/specs/runtime/10-debug-inspection-and-profiling.md @@ -351,3 +351,7 @@ All machine diagnostics and profiling data: - are collected deterministically; - do not require a guest-visible debug ABI; - are consistent regardless of whether host diagnostics are active or not. + +## Program Registry v1 integration + +Loader diagnostics MUST preserve the [structured load tags, reasons and context](02c-program-registry-and-pbx-v1.md#errors-and-publication). Signature names remain available independently of debug data; names do not establish identity. Tools must distinguish image Registry, guest heap, native strings, auxiliary execution state and temporary quotas rather than reporting their sum as RSS. diff --git a/docs/specs/runtime/11a-headless-automation-protocol.md b/docs/specs/runtime/11a-headless-automation-protocol.md index ef2725f..a1ff959 100644 --- a/docs/specs/runtime/11a-headless-automation-protocol.md +++ b/docs/specs/runtime/11a-headless-automation-protocol.md @@ -159,3 +159,9 @@ PNG pixel equality, independent logical/render identity, repeat/reset determinis structured faults, progress on partial failure and retained desktop run/debug. Neither Studio UI nor an AI model is required. See `tests/automation_process.rs` in prometeu-cli and the in-process deterministic-control tests in the headless crate. + +## Program Registry v1 integration + +When exposing initialization failures, retain the [structured load error](02c-program-registry-and-pbx-v1.md#errors-and-publication) code, typed reason and available context. Human-readable messages are not a parsing contract; a load error has no fabricated runtime PC. Failed loads must not publish executable sessions. Existing protocol request names and transport behavior are unchanged. + +Heap reference inspection values use `{"kind":"heap_ref","value":{"index":u32,"generation":u32}}`. Both components are required to distinguish a live incarnation from a reused slot; the wire representation must not truncate handles to indices. Inspection does not grant cross-session transfer authority. diff --git a/docs/specs/runtime/13-cartridge.md b/docs/specs/runtime/13-cartridge.md index a53181b..f2cd2ff 100644 --- a/docs/specs/runtime/13-cartridge.md +++ b/docs/specs/runtime/13-cartridge.md @@ -217,3 +217,7 @@ The cartridge spec therefore distinguishes: - [`12-firmware-pos-and-prometeuhub.md`](12-firmware-pos-and-prometeuhub.md) defines how firmware consumes cartridge metadata. - [`14-boot-profiles.md`](14-boot-profiles.md) defines cartridge selection at boot. - [`15-asset-management.md`](15-asset-management.md) defines the `assets.pa` envelope, asset table semantics, preload lifecycle, and residency semantics. + +## Program Registry v1 integration + +Program payloads MUST use the PBX v1 envelope and mandatory sections defined in [Program Registry and PBX v1](02c-program-registry-and-pbx-v1.md#pbx-v1-encoding). The file limit is 64 MiB and Code is limited to 32 MiB. Unsupported versions are rejected; runtime examples are regenerated rather than converted by a compatibility loader. The existing func_id = 0 boot authority is unchanged. diff --git a/docs/specs/runtime/16-host-abi-and-syscalls.md b/docs/specs/runtime/16-host-abi-and-syscalls.md index 5e341db..206bab0 100644 --- a/docs/specs/runtime/16-host-abi-and-syscalls.md +++ b/docs/specs/runtime/16-host-abi-and-syscalls.md @@ -380,3 +380,7 @@ Only user-defined functions and closures are first-class. - [`15-asset-management.md`](15-asset-management.md) defines asset-domain semantics behind some syscall families. - [`08-save-memory-and-memcard.md`](08-save-memory-and-memcard.md) defines persistent save-domain semantics. - [`02a-vm-values-and-calling-convention.md`](02a-vm-values-and-calling-convention.md) defines the slot/call philosophy reused by the syscall ABI. + +## Program Registry v1 integration + +Native boundaries MUST respect the [Registry typed-value and ownership contract](02c-program-registry-and-pbx-v1.md): exact carriers and complete groups, generational reference validation, string provenance and preallocation quotas. Existing SYSC binding errors remain structured and separate from Registry errors. Input consists of 54 VM-owned fixed queries, never endpoint objects or host syscalls. diff --git a/docs/specs/runtime/README.md b/docs/specs/runtime/README.md index 3952c80..5be2e26 100644 --- a/docs/specs/runtime/README.md +++ b/docs/specs/runtime/README.md @@ -8,7 +8,7 @@ The organization here is intentionally flat. The taxonomy is conceptual, not str ## Scope Boundary -Belongs in `docs/runtime/specs/`: +Belongs in `docs/specs/runtime/`: - the technical contract of the PROMETEU machine; - virtual hardware and its peripherals; @@ -16,7 +16,7 @@ Belongs in `docs/runtime/specs/`: - cartridge, asset, and load surfaces; - the VM-to-host ABI. -Does not belong in `docs/runtime/specs/` as the primary canonical source: +Does not belong in `docs/specs/runtime/` as the primary canonical source: - internal architectural invariants of the VM/runtime: [`../virtual-machine/ARCHITECTURE.md`](../../vm-arch/ARCHITECTURE.md); @@ -25,7 +25,7 @@ Does not belong in `docs/runtime/specs/` as the primary canonical source: - pedagogical guides and learning material: [`../learn/README.md`](../runtime/learn/README.md); - agendas, proposals, and plans: - `docs/runtime/agendas/` and `docs/runtime/pull-requests/`. + `discussion/workflow/agendas/`, `discussion/workflow/decisions/` and `discussion/workflow/plans/`. ## Organization Rules @@ -70,7 +70,7 @@ worker-owned RGBA8888 frame production and host-owned native upload/present. - `historical`: archived material; not the authority for new implementation work. - `pedagogical`: teaching material; does not define the technical contract on its own. -In the current state, no chapter under the active `docs/runtime/specs/` root is purely `historical`. If historical material appears, it must not compete with the primary normative surface. +In the current state, no chapter under the active `docs/specs/runtime/` root is purely `historical`. If historical material appears, it must not compete with the primary normative surface. ## Current Chapter Map @@ -80,6 +80,7 @@ In the current state, no chapter under the active `docs/runtime/specs/` root is | [`02-vm-instruction-set.md`](02-vm-instruction-set.md) | VM subsystem overview | normative | | [`02a-vm-values-and-calling-convention.md`](02a-vm-values-and-calling-convention.md) | VM values and calling convention | normative | | [`02b-vm-function-values-and-closures.md`](02b-vm-function-values-and-closures.md) | VM closures and function values | normative | +| [`02c-program-registry-and-pbx-v1.md`](02c-program-registry-and-pbx-v1.md) | Registry, typed storage, PBX v1 and quotas | normative | | [`03-memory-stack-heap-and-allocation.md`](03-memory-stack-heap-and-allocation.md) | VM memory model | normative | | [`04-gfx-peripheral.md`](04-gfx-peripheral.md) | virtual hardware: graphics | normative | | [`05-audio-peripheral.md`](05-audio-peripheral.md) | virtual hardware: audio | normative | @@ -100,7 +101,7 @@ In the current state, no chapter under the active `docs/runtime/specs/` root is ## Authority Model -- `docs/runtime/specs/` defines the PROMETEU machine and its external execution surfaces. +- `docs/specs/runtime/` defines the PROMETEU machine and its external execution surfaces. - [`../virtual-machine/ARCHITECTURE.md`](../../vm-arch/ARCHITECTURE.md) remains the canonical authority for internal VM/runtime invariants. - [`../virtual-machine/ISA_CORE.md`](../../vm-arch/ISA_CORE.md) remains the authority for the bytecode ISA. - When a chapter here touches the VM, it may complement but must not contradict the canonical VM/runtime architecture. @@ -124,7 +125,7 @@ A topic should be created as a `spec` + `learn` pair when: In those cases: -- `docs/runtime/specs/` defines the contract; +- `docs/specs/runtime/` defines the contract; - `docs/runtime/learn/` explains the why, the intuition, and the examples; - the spec should point to the pedagogical companion near the top; - the pedagogical guide should point back to the canonical spec. diff --git a/docs/vm-arch/ARCHITECTURE.md b/docs/vm-arch/ARCHITECTURE.md index 4f81f55..3038894 100644 --- a/docs/vm-arch/ARCHITECTURE.md +++ b/docs/vm-arch/ARCHITECTURE.md @@ -48,13 +48,13 @@ Maintenance rule: - The VM’s current operand stack and frames are GC roots. - Heap - - The heap stores runtime objects that require identity and reachability tracking. Handles are `HeapRef` indices into an internal object store. + - The heap stores runtime objects that require identity and reachability tracking. Handles are `HeapRef { index: u32, generation: u32 }` values into an internal object store. Every access checks the live generation; reclaimed maximum-generation slots are retired permanently. - The collector is mark–sweep, non‑moving: it marks from roots, then reclaims unreachable objects without relocating survivors. Indices for live objects remain stable across collections. 2.2 Heap Object Kinds (as used today) -- Arrays of `Value` - - Variable‑length arrays whose elements may contain further `HeapRef`s. +- Typed arrays + - Variable-length arrays use the Registry element layout and defaults. Primitive/builtin values are stored directly; Struct/Array elements contain generational references. Native String/Dynamic slots retain their ownership rules. Stack/local execution continues to use `Value`. - Closures - Carry a function identifier and a captured environment (a slice/vector of `Value`s stored with the closure). Captured `HeapRef`s are traversed by the GC. - Coroutines @@ -183,3 +183,7 @@ The verifier statically checks bytecode for structural safety and stack‑shape - Do not assume implicit safepoints; schedule and GC only at `FRAME_SYNC`. - When adding new opcodes or object kinds, extend the verifier and GC traversal accordingly (children enumeration, environment scanning, root sets). - Update this document alongside any architectural change that affects runtime invariants. + +## Program Registry v1 + +The [Program Registry contract](../specs/runtime/02c-program-registry-and-pbx-v1.md) defines immutable image-owned Type/Function metadata linked by dense IDs. Descriptors are native metadata outside guest GC; nominal identity does not depend on names. Stack/local execution retains Value, while typed storage uses resolved aligned layouts. PBX v1 is the sole target format; publication requires full bounded validation. Callable/closure/coroutine typed integration awaits its separate contract and must preserve existing behavior until then. diff --git a/docs/vm-arch/INTRINSICS.csv b/docs/vm-arch/INTRINSICS.csv index 33779d1..947c732 100644 --- a/docs/vm-arch/INTRINSICS.csv +++ b/docs/vm-arch/INTRINSICS.csv @@ -1,13 +1,13 @@ final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate -0x1000,4096,vec2.dot,1,vec2,dot,4,1,float|float|float|float,float,true,false -0x1001,4097,vec2.length,1,vec2,length,2,1,float|float,float,true,false -0x1100,4352,color.from_raw,1,color,from_raw,1,1,int,builtin:color,true,false -0x1101,4353,color.rgb,1,color,rgb,3,1,int|int|int,builtin:color,true,false -0x1102,4354,color.rgba,1,color,rgba,4,1,int|int|int|int,builtin:color,true,false -0x1103,4355,color.html_rgba,1,color,html_rgba,1,1,str,builtin:color,true,false -0x1104,4356,color.gray_scale,1,color,gray_scale,1,1,int,builtin:color,true,false -0x1105,4357,color.hex,1,color,hex,1,1,builtin:color,int,true,false -0x1106,4358,color.alpha,1,color,alpha,1,1,builtin:color,int,true,false +0x1000,4096,vec2.dot,1,vec2,dot,4,1,Float|Float|Float|Float,Float,true,false +0x1001,4097,vec2.length,1,vec2,length,2,1,Float|Float,Float,true,false +0x1100,4352,color.from_raw,1,color,from_raw,1,1,Int64,builtin:color,true,false +0x1101,4353,color.rgb,1,color,rgb,3,1,Int64|Int64|Int64,builtin:color,true,false +0x1102,4354,color.rgba,1,color,rgba,4,1,Int64|Int64|Int64|Int64,builtin:color,true,false +0x1103,4355,color.html_rgba,1,color,html_rgba,1,1,String,builtin:color,true,false +0x1104,4356,color.gray_scale,1,color,gray_scale,1,1,Int64,builtin:color,true,false +0x1105,4357,color.hex,1,color,hex,1,1,builtin:color,Int64,true,false +0x1106,4358,color.alpha,1,color,alpha,1,1,builtin:color,Int64,true,false 0x1120,4384,color.black,1,color,black,0,1,,builtin:color,true,false 0x1121,4385,color.white,1,color,white,0,1,,builtin:color,true,false 0x1122,4386,color.red,1,color,red,0,1,,builtin:color,true,false @@ -20,24 +20,57 @@ final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots, 0x1129,4393,color.cyan,1,color,cyan,0,1,,builtin:color,true,false 0x112A,4394,color.magenta,1,color,magenta,0,1,,builtin:color,true,false 0x112B,4395,color.transparent,1,color,transparent,0,1,,builtin:color,true,false -0x2000,8192,input.pad,1,input,pad,0,1,,builtin:input.pad,true,false -0x2001,8193,input.touch,1,input,touch,0,1,,builtin:input.touch,true,false -0x2010,8208,input.pad.up,1,input.pad,up,1,1,builtin:input.pad,builtin:input.button,true,false -0x2011,8209,input.pad.down,1,input.pad,down,1,1,builtin:input.pad,builtin:input.button,true,false -0x2012,8210,input.pad.left,1,input.pad,left,1,1,builtin:input.pad,builtin:input.button,true,false -0x2013,8211,input.pad.right,1,input.pad,right,1,1,builtin:input.pad,builtin:input.button,true,false -0x2014,8212,input.pad.a,1,input.pad,a,1,1,builtin:input.pad,builtin:input.button,true,false -0x2015,8213,input.pad.b,1,input.pad,b,1,1,builtin:input.pad,builtin:input.button,true,false -0x2016,8214,input.pad.x,1,input.pad,x,1,1,builtin:input.pad,builtin:input.button,true,false -0x2017,8215,input.pad.y,1,input.pad,y,1,1,builtin:input.pad,builtin:input.button,true,false -0x2018,8216,input.pad.l,1,input.pad,l,1,1,builtin:input.pad,builtin:input.button,true,false -0x2019,8217,input.pad.r,1,input.pad,r,1,1,builtin:input.pad,builtin:input.button,true,false -0x201A,8218,input.pad.start,1,input.pad,start,1,1,builtin:input.pad,builtin:input.button,true,false -0x201B,8219,input.pad.select,1,input.pad,select,1,1,builtin:input.pad,builtin:input.button,true,false -0x2020,8224,input.touch.button,1,input.touch,button,1,1,builtin:input.touch,builtin:input.button,true,false -0x2021,8225,input.touch.x,1,input.touch,x,1,1,builtin:input.touch,int,true,false -0x2022,8226,input.touch.y,1,input.touch,y,1,1,builtin:input.touch,int,true,false -0x2030,8240,input.button.pressed,1,input.button,pressed,1,1,builtin:input.button,bool,true,false -0x2031,8241,input.button.released,1,input.button,released,1,1,builtin:input.button,bool,true,false -0x2032,8242,input.button.down,1,input.button,down,1,1,builtin:input.button,bool,true,false -0x2033,8243,input.button.hold,1,input.button,hold,1,1,builtin:input.button,int,true,false +0x2100,8448,input.pad.up.pressed,1,input.pad.up,pressed,0,1,,Bool,true,false +0x2101,8449,input.pad.up.released,1,input.pad.up,released,0,1,,Bool,true,false +0x2102,8450,input.pad.up.down,1,input.pad.up,down,0,1,,Bool,true,false +0x2103,8451,input.pad.up.hold,1,input.pad.up,hold,0,1,,Int64,true,false +0x2104,8452,input.pad.down.pressed,1,input.pad.down,pressed,0,1,,Bool,true,false +0x2105,8453,input.pad.down.released,1,input.pad.down,released,0,1,,Bool,true,false +0x2106,8454,input.pad.down.down,1,input.pad.down,down,0,1,,Bool,true,false +0x2107,8455,input.pad.down.hold,1,input.pad.down,hold,0,1,,Int64,true,false +0x2108,8456,input.pad.left.pressed,1,input.pad.left,pressed,0,1,,Bool,true,false +0x2109,8457,input.pad.left.released,1,input.pad.left,released,0,1,,Bool,true,false +0x210A,8458,input.pad.left.down,1,input.pad.left,down,0,1,,Bool,true,false +0x210B,8459,input.pad.left.hold,1,input.pad.left,hold,0,1,,Int64,true,false +0x210C,8460,input.pad.right.pressed,1,input.pad.right,pressed,0,1,,Bool,true,false +0x210D,8461,input.pad.right.released,1,input.pad.right,released,0,1,,Bool,true,false +0x210E,8462,input.pad.right.down,1,input.pad.right,down,0,1,,Bool,true,false +0x210F,8463,input.pad.right.hold,1,input.pad.right,hold,0,1,,Int64,true,false +0x2110,8464,input.pad.a.pressed,1,input.pad.a,pressed,0,1,,Bool,true,false +0x2111,8465,input.pad.a.released,1,input.pad.a,released,0,1,,Bool,true,false +0x2112,8466,input.pad.a.down,1,input.pad.a,down,0,1,,Bool,true,false +0x2113,8467,input.pad.a.hold,1,input.pad.a,hold,0,1,,Int64,true,false +0x2114,8468,input.pad.b.pressed,1,input.pad.b,pressed,0,1,,Bool,true,false +0x2115,8469,input.pad.b.released,1,input.pad.b,released,0,1,,Bool,true,false +0x2116,8470,input.pad.b.down,1,input.pad.b,down,0,1,,Bool,true,false +0x2117,8471,input.pad.b.hold,1,input.pad.b,hold,0,1,,Int64,true,false +0x2118,8472,input.pad.x.pressed,1,input.pad.x,pressed,0,1,,Bool,true,false +0x2119,8473,input.pad.x.released,1,input.pad.x,released,0,1,,Bool,true,false +0x211A,8474,input.pad.x.down,1,input.pad.x,down,0,1,,Bool,true,false +0x211B,8475,input.pad.x.hold,1,input.pad.x,hold,0,1,,Int64,true,false +0x211C,8476,input.pad.y.pressed,1,input.pad.y,pressed,0,1,,Bool,true,false +0x211D,8477,input.pad.y.released,1,input.pad.y,released,0,1,,Bool,true,false +0x211E,8478,input.pad.y.down,1,input.pad.y,down,0,1,,Bool,true,false +0x211F,8479,input.pad.y.hold,1,input.pad.y,hold,0,1,,Int64,true,false +0x2120,8480,input.pad.l.pressed,1,input.pad.l,pressed,0,1,,Bool,true,false +0x2121,8481,input.pad.l.released,1,input.pad.l,released,0,1,,Bool,true,false +0x2122,8482,input.pad.l.down,1,input.pad.l,down,0,1,,Bool,true,false +0x2123,8483,input.pad.l.hold,1,input.pad.l,hold,0,1,,Int64,true,false +0x2124,8484,input.pad.r.pressed,1,input.pad.r,pressed,0,1,,Bool,true,false +0x2125,8485,input.pad.r.released,1,input.pad.r,released,0,1,,Bool,true,false +0x2126,8486,input.pad.r.down,1,input.pad.r,down,0,1,,Bool,true,false +0x2127,8487,input.pad.r.hold,1,input.pad.r,hold,0,1,,Int64,true,false +0x2128,8488,input.pad.start.pressed,1,input.pad.start,pressed,0,1,,Bool,true,false +0x2129,8489,input.pad.start.released,1,input.pad.start,released,0,1,,Bool,true,false +0x212A,8490,input.pad.start.down,1,input.pad.start,down,0,1,,Bool,true,false +0x212B,8491,input.pad.start.hold,1,input.pad.start,hold,0,1,,Int64,true,false +0x212C,8492,input.pad.select.pressed,1,input.pad.select,pressed,0,1,,Bool,true,false +0x212D,8493,input.pad.select.released,1,input.pad.select,released,0,1,,Bool,true,false +0x212E,8494,input.pad.select.down,1,input.pad.select,down,0,1,,Bool,true,false +0x212F,8495,input.pad.select.hold,1,input.pad.select,hold,0,1,,Int64,true,false +0x2130,8496,input.touch.button.pressed,1,input.touch.button,pressed,0,1,,Bool,true,false +0x2131,8497,input.touch.button.released,1,input.touch.button,released,0,1,,Bool,true,false +0x2132,8498,input.touch.button.down,1,input.touch.button,down,0,1,,Bool,true,false +0x2133,8499,input.touch.button.hold,1,input.touch.button,hold,0,1,,Int64,true,false +0x2134,8500,input.touch.x,1,input.touch,x,0,1,,Int64,true,false +0x2135,8501,input.touch.y,1,input.touch,y,0,1,,Int64,true,false diff --git a/docs/vm-arch/ISA_CORE.md b/docs/vm-arch/ISA_CORE.md index d6f704d..55bf46e 100644 --- a/docs/vm-arch/ISA_CORE.md +++ b/docs/vm-arch/ISA_CORE.md @@ -143,3 +143,24 @@ For exact immediates and stack effects, see `CoreOpCode::spec()` which is the si - Verification notes: - The bytecode verifier treats `FRAME_SYNC` as a normal instruction with no stack effect and no control-flow targets. It is permitted before `RET`, between basic blocks, and as the last instruction of a function. Jumps targeting the function end (`pc == end`) remain valid under the end-exclusive rule. + +## Registry instruction encodings + +The [Registry contract](../specs/runtime/02c-program-registry-and-pbx-v1.md#dynamic-and-reference-instructions) +defines opcodes 0x0090–0x0096. Tooling recognizes their u32 TypeId immediates: + +```text +DYN_PACK 0 +DYN_UNPACK 0 +DYN_CAN_UNPACK 0 +REF_IS_EXACT source=4, target=5 +REF_IMPLEMENTS source=4, target=6 +REF_TO_TRAIT source=4, target=6 +REF_CAST source=6, target=4 +``` + +Dynamic instructions occupy six bytes; reference instructions occupy ten bytes. +Static opcode metadata must represent Registry-dependent slot effects as unresolved, +never as zero. Resolve the semantic widths and immediate categories from the validated +Registry. Encoding/decoding support alone does not authorize execution: a slot-only +image/verifier must reject these instructions until typed-image integration is present.