//! PBX v1 publication: immutable metadata and shared payload, after bindings //! and bounded whole-program verification. No public unchecked constructor. use crate::{LoaderPatchError, VmInitError}; use prometeu_bytecode::isa::core::CoreOpCode as Op; use prometeu_bytecode::load_limits::{ LoadBudget, LoadCode as C, LoadFailure as E, LoadReason as R, LoadResource as Q, }; use prometeu_bytecode::model::{ConstantPoolEntry as K, DebugInfo, Export}; use prometeu_bytecode::registry::{FunctionId, Registry}; use prometeu_bytecode::registry_codec::OwnedPbx; use prometeu_bytecode::{FunctionMeta, ProgramImage, Value}; use prometeu_hal::syscalls::{CapFlags, DeclaredLoadError, SyscallResolved}; use std::collections::HashMap; use std::sync::{Arc, Mutex, Weak}; const DOMAINS: [Q; 11] = [ Q::PbxBytes, Q::CodeBytes, Q::Types, Q::Functions, Q::Implementations, Q::NestedRecords, Q::NameBytes, Q::RegistryBytes, Q::TemporaryBytes, Q::ConstantEntries, Q::ConstantStringBytes, ]; #[derive(Debug)] struct ImageStrings { records: Vec<(Weak, usize)>, bytes: u64, } impl ImageStrings { fn reconcile(&mut self) { self.records.retain(|(weak, len)| { if weak.strong_count() == 0 { self.bytes -= *len as u64; false } else { true } }); } } /// Native retention carries the image accounting domain with the content. /// This is provenance metadata, not a new guest string representation. #[derive(Debug, Clone)] pub struct ImageStringLease { content: Arc, domain: Arc>, } impl ImageStringLease { pub fn content(&self) -> &Arc { &self.content } pub fn retained_content(&self) -> (u64, usize) { let mut domain = self.domain.lock().unwrap_or_else(std::sync::PoisonError::into_inner); domain.reconcile(); (domain.bytes, domain.records.len()) } } #[derive(Debug)] pub struct ValidatedProgramImage { registry: Arc, pub(crate) execution: ProgramImage, exports: Vec, strings: Arc>, registry_bytes: u64, payload_bytes: u64, capabilities: CapFlags, } impl ValidatedProgramImage { pub fn registry(&self) -> &Arc { &self.registry } pub fn code(&self) -> &[u8] { &self.execution.rom } pub fn constants(&self) -> &[Value] { &self.execution.constant_pool } pub fn debug(&self) -> Option<&DebugInfo> { self.execution.debug_info.as_deref() } pub fn exports(&self) -> &[Export] { &self.exports } pub fn capabilities(&self) -> CapFlags { self.capabilities } /// Domain measurements are native capacities/content, not process RSS. pub fn registry_bytes(&self) -> u64 { self.registry_bytes } pub fn payload_bytes(&self) -> u64 { self.payload_bytes } pub fn lease_constant(&self, index: usize) -> Option { let Value::String(content) = self.constants().get(index)? else { return None }; Some(ImageStringLease { content: content.clone(), domain: self.strings.clone() }) } pub fn retains_string(&self, content: &Arc) -> bool { let strings = self.strings.lock().unwrap_or_else(std::sync::PoisonError::into_inner); strings.records.iter().any(|(weak, _)| std::ptr::eq(weak.as_ptr(), Arc::as_ptr(content))) } pub fn constant_content(&self) -> (u64, usize) { let mut strings = self.strings.lock().unwrap_or_else(std::sync::PoisonError::into_inner); strings.reconcile(); (strings.bytes, strings.records.len()) } /// Only this factory can publish an image. Unsupported versions have no /// fallback. The caller's input domain is separate from load scratch. pub fn load( bytes: &[u8], budget: &mut LoadBudget, capabilities: CapFlags, ) -> Result, VmInitError> { let before = DOMAINS.map(|resource| budget.used(resource)); let result = Self::load_inner(bytes, budget, capabilities); // Failure drops partial owners before this rollback; success transfers // scratch-owned payload into the measured immutable image domain. for (resource, old) in DOMAINS.into_iter().zip(before) { if result.is_err() || resource == Q::TemporaryBytes { budget.refund(resource, budget.used(resource) - old); } } result } fn load_inner( bytes: &[u8], b: &mut LoadBudget, capabilities: CapFlags, ) -> Result, VmInitError> { let payload_before = b.used(Q::TemporaryBytes); let registry_before = b.used(Q::RegistryBytes); let mut module = OwnedPbx::parse(bytes, b)?; if module.registry.declarations().functions.is_empty() { return Err(E::new(C::InvalidEntrypoint, R::FunctionId).in_function(0, None).into()); } bind(&mut module, b, capabilities)?; crate::registry_verifier::validate_registry_types( &module.code, &module.registry, &module.constants, b, )?; let mut functions = scratch::(module.registry.declarations().functions.len(), b)?; for (index, f) in module.registry.declarations().functions.iter().enumerate() { b.work(1)?; let slots = module.registry.function_layout(FunctionId(index as u32)).unwrap(); // The accepted 16,384-slot frame/1,024-slot result ceilings prove // these cached execution widths fit; no serialized field narrows. let narrow = |n| { u16::try_from(n).map_err(|_| { E::new(C::InvalidCode, R::FrameSlots) .in_function(index as u32, Some(f.code_offset)) }) }; functions.push(FunctionMeta { code_offset: f.code_offset, code_len: f.code_len, param_slots: narrow(slots.parameter_slots)?, local_slots: narrow(f.local_slots)?, return_slots: narrow(slots.result_slots)?, max_stack_slots: narrow(f.max_stack_slots)?, }); } let constants_capacity = (module.constants.capacity() * size_of::()) as u64; b.work(module.constants.len())?; let string_count = module.constants.iter().filter(|c| matches!(c, K::String(_))).count(); let mut records = scratch::<(Weak, usize)>(string_count, b)?; let mut values = scratch::(module.constants.len(), b)?; let mut content_bytes = 0; for constant in module.constants { b.work(1)?; let value = match constant { K::Null => Value::Null, K::Int32(v) => Value::Int32(v), K::Int64(v) => Value::Int64(v), K::Float64(v) => Value::Float(v), K::Boolean(v) => Value::Boolean(v), K::String(text) => { arc_charge::(text.len(), b, Q::TemporaryBytes)?; b.work(text.len())?; let content: Arc = Arc::from(text.as_str()); content_bytes += text.len() as u64; records.push((Arc::downgrade(&content), text.len())); b.refund(Q::TemporaryBytes, text.capacity() as u64); Value::String(content) } }; values.push(value); } b.refund(Q::TemporaryBytes, constants_capacity); // Binding no longer needs source declarations. They were validated, // used exactly, and can be released before final payload publication. b.work(module.syscalls.len())?; let syscall_bytes = (module.syscalls.capacity() * size_of::()) as u64 + module .syscalls .iter() .map(|d| (d.module.capacity() + d.name.capacity()) as u64) .sum::(); drop(module.syscalls); b.refund(Q::TemporaryBytes, syscall_bytes); let rom = shared_slice(module.code, b)?; let constant_pool = shared_slice(values, b)?; let functions = shared_slice(functions, b)?; let debug_info = if let Some(debug) = module.debug { arc_charge::(1, b, Q::TemporaryBytes)?; Some(Arc::new(debug)) } else { None }; // Export vectors retain source order; lookup can preserve the existing // last-declaration-wins native entry behavior without another HashMap. arc_charge::>(1, b, Q::TemporaryBytes)?; let strings = Arc::new(Mutex::new(ImageStrings { records, bytes: content_bytes })); arc_charge::(1, b, Q::RegistryBytes)?; let registry = Arc::new(module.registry); arc_charge::(1, b, Q::TemporaryBytes)?; arc_charge::>(1, b, Q::TemporaryBytes)?; let registry_bytes = b.used(Q::RegistryBytes) - registry_before; let payload_bytes = b.used(Q::TemporaryBytes) - payload_before; let execution = ProgramImage { rom, constant_pool, functions, debug_info, exports: Arc::new(HashMap::new()), }; Ok(Arc::new(Self { registry, execution, exports: module.exports, strings, registry_bytes, payload_bytes, capabilities, })) } } pub(crate) fn scratch(count: usize, b: &mut LoadBudget) -> Result, E> { let bytes = count .checked_mul(size_of::()) .ok_or_else(|| E::new(C::ResourceLimit, R::Overflow))? as u64; b.charge(Q::TemporaryBytes, bytes)?; let mut out = Vec::new(); out.try_reserve_exact(count).map_err(|_| { let mut e = E::resource(Q::TemporaryBytes, bytes, b.limit(Q::TemporaryBytes)); e.reason = R::Allocation; e })?; b.charge(Q::TemporaryBytes, (out.capacity() - count) as u64 * size_of::() as u64)?; Ok(out) } fn arc_charge(count: usize, b: &mut LoadBudget, resource: Q) -> Result<(), E> { let align = align_of::().max(align_of::()); let bytes = count .checked_mul(size_of::()) .and_then(|n| n.checked_add(2 * size_of::() + align - 1)) .map(|n| n / align * align) .ok_or_else(|| E::new(C::ResourceLimit, R::Overflow))?; b.charge(resource, bytes as u64) } fn shared_slice(items: Vec, b: &mut LoadBudget) -> Result, E> { arc_charge::(items.len(), b, Q::TemporaryBytes)?; b.work(items.len())?; let capacity = (items.capacity() * size_of::()) as u64; let shared = Arc::from(items); b.refund(Q::TemporaryBytes, capacity); Ok(shared) } fn same_text(left: &str, right: &str, b: &mut LoadBudget) -> Result { b.work(1)?; if left.len() != right.len() { return Ok(false); } for (a, c) in left.bytes().zip(right.bytes()) { b.work(2)?; if a != c { return Ok(false); } } Ok(true) } fn diagnostic_text(text: &str, b: &mut LoadBudget) -> Result { let mut bytes = scratch(text.len(), b)?; b.work(text.len())?; bytes.extend_from_slice(text.as_bytes()); Ok(String::from_utf8(bytes).expect("declaration text is UTF-8")) } fn patch_error(error: LoaderPatchError) -> VmInitError { VmInitError::LoaderPatchFailed(error) } fn bind(module: &mut OwnedPbx, b: &mut LoadBudget, caps: CapFlags) -> Result<(), VmInitError> { let before = b.used(Q::TemporaryBytes); let result = (|| { let mut resolved = scratch::(module.syscalls.len(), b)?; let mut used = scratch::(module.syscalls.len(), b)?; b.work(module.syscalls.len())?; used.resize(module.syscalls.len(), false); for decl in &module.syscalls { let mut found = None; for entry in prometeu_hal::syscalls::catalogue() { b.work(1)?; if entry.meta.version == decl.version && same_text(entry.meta.module, &decl.module, b)? && same_text(entry.meta.name, &decl.name, b)? { found = Some(SyscallResolved { id: entry.meta.id, meta: entry.meta }); break; } } let native_error = if let Some(value) = found { if value.meta.caps & !caps != 0 { Some(DeclaredLoadError::MissingCapability { required: value.meta.caps, provided: caps, module: diagnostic_text(&decl.module, b)?, name: diagnostic_text(&decl.name, b)?, version: decl.version, }) } else if decl.arg_slots != value.meta.arg_slots as u16 || decl.ret_slots != value.meta.ret_slots { Some(DeclaredLoadError::AbiMismatch { module: diagnostic_text(&decl.module, b)?, name: diagnostic_text(&decl.name, b)?, version: decl.version, declared_arg_slots: decl.arg_slots, declared_ret_slots: decl.ret_slots, expected_arg_slots: value.meta.arg_slots as u16, expected_ret_slots: value.meta.ret_slots, }) } else { resolved.push(value); None } } else { Some(DeclaredLoadError::UnknownSyscall { module: diagnostic_text(&decl.module, b)?, name: diagnostic_text(&decl.name, b)?, version: decl.version, }) }; if let Some(error) = native_error { return Err(patch_error(LoaderPatchError::ResolveFailed(error))); } } for (id, function) in module.registry.declarations().functions.iter().enumerate() { let mut pc = function.code_offset as usize; let end = pc + function.code_len as usize; while pc < end { let instruction = (|| { b.work(2)?; let instruction = prometeu_bytecode::decode_next(pc, &module.code[..end]) .map_err(|e| { E::new( C::InvalidCode, if matches!(e, prometeu_bytecode::DecodeError::UnknownOpcode { .. }) { R::UnknownOpcode } else { R::Truncated }, ) })?; b.work(instruction.imm.len() + 1)?; Ok(instruction) })() .map_err(|e: E| e.in_section(2).in_function(id as u32, Some(pc as u32)))?; let next = instruction.next_pc; match instruction.opcode { Op::Hostcall => { let index = instruction.imm_u32().unwrap(); let syscall = resolved.get(index as usize).ok_or_else(|| { patch_error(LoaderPatchError::HostcallIndexOutOfBounds { pc, sysc_index: index, syscalls_len: resolved.len(), }) })?; b.work(7)?; used[index as usize] = true; module.code[pc..pc + 2] .copy_from_slice(&(Op::Syscall as u16).to_le_bytes()); module.code[pc + 2..pc + 6].copy_from_slice(&syscall.id.to_le_bytes()); } Op::Syscall => { return Err(patch_error(LoaderPatchError::RawSyscallInPreloadArtifact { pc, syscall_id: instruction.imm_u32().unwrap(), })); } _ => {} } pc = next; } } for (index, (decl, used)) in module.syscalls.iter().zip(&used).enumerate() { b.work(1)?; if !used { return Err(patch_error(LoaderPatchError::UnusedSyscallDecl { sysc_index: index as u32, module: diagnostic_text(&decl.module, b)?, name: diagnostic_text(&decl.name, b)?, version: decl.version, })); } } Ok(()) })(); b.refund(Q::TemporaryBytes, b.used(Q::TemporaryBytes) - before); result } #[cfg(test)] mod tests;