feat(loader): materialize bounded PBX v1 payloads for PLN-0176 step 09
This commit is contained in:
@@ -470,6 +470,6 @@ fn write_section(
|
||||
}
|
||||
|
||||
mod payloads;
|
||||
pub use payloads::ParsedPbx;
|
||||
pub use payloads::{OwnedPbx, ParsedPbx};
|
||||
mod writer;
|
||||
pub use writer::PbxModule;
|
||||
|
||||
@@ -1,9 +1,44 @@
|
||||
//! Validation of unchanged non-Registry section encodings, without allocating
|
||||
//! guest constants or publishing an executable image.
|
||||
use super::*;
|
||||
use crate::abi::SourceSpan;
|
||||
use crate::load_limits::IdDomain;
|
||||
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// Owned, bounded PBX v1 tables for the executable loader. This is not an
|
||||
/// executable image: bindings and instruction/type verification are still due.
|
||||
#[derive(Debug)]
|
||||
pub struct OwnedPbx {
|
||||
pub registry: Registry,
|
||||
pub constants: Vec<ConstantPoolEntry>,
|
||||
pub code: Vec<u8>,
|
||||
pub debug: Option<DebugInfo>,
|
||||
pub exports: Vec<Export>,
|
||||
pub syscalls: Vec<SyscallDecl>,
|
||||
}
|
||||
impl OwnedPbx {
|
||||
pub fn parse(bytes: &[u8], budget: &mut LoadBudget) -> Result<Self, E> {
|
||||
budget.transaction(|budget| {
|
||||
let parsed = ParsedPbx::parse(bytes, budget)?;
|
||||
let constants = Reader::new(parsed.section(0).unwrap(), budget).owned_constants()?;
|
||||
let exports = Reader::new(parsed.section(4).unwrap(), budget).owned_exports()?;
|
||||
let syscalls = Reader::new(parsed.section(5).unwrap(), budget).owned_syscalls()?;
|
||||
let debug = parsed
|
||||
.section(3)
|
||||
.map(|section| Reader::new(section, budget).owned_debug())
|
||||
.transpose()?;
|
||||
let section = parsed.section(2).unwrap();
|
||||
let mut code = budget
|
||||
.vector(section.bytes.len(), Q::TemporaryBytes)
|
||||
.map_err(|e| e.at(2, section.offset))?;
|
||||
budget.work(section.bytes.len()).map_err(|e| e.at(2, section.offset))?;
|
||||
code.extend_from_slice(section.bytes);
|
||||
Ok(Self { registry: parsed.registry, constants, code, debug, exports, syscalls })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)]
|
||||
@@ -48,6 +83,112 @@ impl<'a> ParsedPbx<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Reader<'a, '_> {
|
||||
fn owned_vector<T>(&mut self, count: usize) -> Result<Vec<T>, E> {
|
||||
self.budget
|
||||
.vector(count, Q::TemporaryBytes)
|
||||
.map_err(|e| e.at(self.section.kind, self.section.offset + self.pos))
|
||||
}
|
||||
fn owned_text(&mut self, len: usize) -> Result<String, E> {
|
||||
let value = self.utf8(len)?;
|
||||
let mut bytes = self.owned_vector(len)?;
|
||||
self.budget.work(len)?;
|
||||
bytes.extend_from_slice(value.as_bytes());
|
||||
Ok(String::from_utf8(bytes).expect("UTF-8 checked before allocation"))
|
||||
}
|
||||
fn owned_constants(mut self) -> Result<Vec<ConstantPoolEntry>, E> {
|
||||
let count = self.raw_count(1)?;
|
||||
let mut out = self.owned_vector(count)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let value = (|| {
|
||||
Ok(match self.byte()? {
|
||||
0 => ConstantPoolEntry::Null,
|
||||
1 => ConstantPoolEntry::Int64(i64::from_le_bytes(
|
||||
self.take(8)?.try_into().unwrap(),
|
||||
)),
|
||||
2 => ConstantPoolEntry::Float64(f64::from_bits(u64::from_le_bytes(
|
||||
self.take(8)?.try_into().unwrap(),
|
||||
))),
|
||||
3 => ConstantPoolEntry::Boolean(self.byte()? == 1),
|
||||
4 => {
|
||||
let len = self.u32()? as usize;
|
||||
ConstantPoolEntry::String(self.owned_text(len)?)
|
||||
}
|
||||
5 => ConstantPoolEntry::Int32(i32::from_le_bytes(
|
||||
self.take(4)?.try_into().unwrap(),
|
||||
)),
|
||||
_ => return Err(self.error(C::UnsupportedFormat, R::ConstantTag)),
|
||||
})
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
out.push(value);
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
fn owned_exports(mut self) -> Result<Vec<Export>, E> {
|
||||
let count = self.raw_count(8)?;
|
||||
let mut out = self.owned_vector(count)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let func_idx = self.u32()?;
|
||||
let len = self.u32()? as usize;
|
||||
let symbol = self.owned_text(len).map_err(|e| e.record(i as u32))?;
|
||||
out.push(Export { func_idx, symbol });
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
fn owned_syscalls(mut self) -> Result<Vec<SyscallDecl>, E> {
|
||||
let count = self.raw_count(10)?;
|
||||
let mut out = self.owned_vector(count)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let record = (|| {
|
||||
let len = self.u16()? as usize;
|
||||
let module = self.owned_text(len)?;
|
||||
let len = self.u16()? as usize;
|
||||
let name = self.owned_text(len)?;
|
||||
Ok(SyscallDecl {
|
||||
module,
|
||||
name,
|
||||
version: self.u16()?,
|
||||
arg_slots: self.u16()?,
|
||||
ret_slots: self.u16()?,
|
||||
})
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
out.push(record);
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(out)
|
||||
}
|
||||
fn owned_debug(mut self) -> Result<DebugInfo, E> {
|
||||
let count = self.raw_count(16)?;
|
||||
let mut pc_to_span = self.owned_vector(count)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let value = (|| {
|
||||
Ok((
|
||||
self.u32()?,
|
||||
SourceSpan { file_id: self.u32()?, start: self.u32()?, end: self.u32()? },
|
||||
))
|
||||
})()
|
||||
.map_err(|e: E| e.record(i as u32))?;
|
||||
pc_to_span.push(value);
|
||||
}
|
||||
let count = self.raw_count(8)?;
|
||||
let mut function_names = self.owned_vector(count)?;
|
||||
for i in 0..count {
|
||||
self.budget.work(1)?;
|
||||
let id = self.u32()?;
|
||||
let len = self.u32()? as usize;
|
||||
let name = self.owned_text(len).map_err(|e| e.record(i as u32))?;
|
||||
function_names.push((id, name));
|
||||
}
|
||||
self.finish()?;
|
||||
Ok(DebugInfo { pc_to_span, function_names })
|
||||
}
|
||||
fn u16(&mut self) -> Result<u16, E> {
|
||||
Ok(u16::from_le_bytes(self.take(2)?.try_into().unwrap()))
|
||||
}
|
||||
|
||||
@@ -84,6 +84,97 @@ fn writer_matches_independent_minimal_envelope() {
|
||||
assert_eq!(registry.declarations().functions[0].max_stack_slots, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_v1_decoder_accepts_independent_bytes_without_publishing_an_image() {
|
||||
let mut b = budget();
|
||||
let owned = OwnedPbx::parse(&minimal_bytes(), &mut b).unwrap();
|
||||
assert_eq!(owned.registry.declarations(), minimal_registry().declarations());
|
||||
assert_eq!(owned.code, [1, 0]);
|
||||
assert!(owned.constants.is_empty());
|
||||
assert!(owned.exports.is_empty());
|
||||
assert!(owned.syscalls.is_empty());
|
||||
assert!(owned.debug.is_none());
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), owned.code.capacity() as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_payloads_preserve_utf8_float_bits_names_and_binding_declarations() {
|
||||
use prometeu_bytecode::model::{ConstantPoolEntry as K, DebugInfo, Export, SyscallDecl};
|
||||
use prometeu_bytecode::SourceSpan;
|
||||
let registry = minimal_registry();
|
||||
let constants = [
|
||||
K::Null,
|
||||
K::Int32(i32::MIN),
|
||||
K::Int64(i64::MAX),
|
||||
K::Float64(f64::from_bits(0x7ff8000000000042)),
|
||||
K::Boolean(true),
|
||||
K::String("é猫".into()),
|
||||
K::String(String::new()),
|
||||
];
|
||||
let debug = DebugInfo {
|
||||
pc_to_span: vec![(0, SourceSpan { file_id: 9, start: 2, end: 7 })],
|
||||
function_names: vec![(0, "débug".into())],
|
||||
};
|
||||
let exports = [Export { func_idx: 0, symbol: "入口".into() }];
|
||||
let syscalls = [SyscallDecl {
|
||||
module: "system".into(),
|
||||
name: "hasCart".into(),
|
||||
version: 1,
|
||||
arg_slots: 0,
|
||||
ret_slots: 1,
|
||||
}];
|
||||
let bytes = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &constants,
|
||||
code: &[1, 0],
|
||||
debug: Some(&debug),
|
||||
exports: &exports,
|
||||
syscalls: &syscalls,
|
||||
}
|
||||
.serialize(&mut budget())
|
||||
.unwrap();
|
||||
let mut b = budget();
|
||||
let module = OwnedPbx::parse(&bytes, &mut b).unwrap();
|
||||
assert_eq!(&module.constants[..3], &constants[..3]);
|
||||
let K::Float64(value) = module.constants[3] else { panic!() };
|
||||
assert_eq!(value.to_bits(), 0x7ff8000000000042);
|
||||
assert_eq!(&module.constants[4..], &constants[4..]);
|
||||
assert_eq!(module.debug.as_ref(), Some(&debug));
|
||||
assert_eq!(module.exports, exports);
|
||||
assert_eq!(module.syscalls, syscalls);
|
||||
assert_eq!(b.used(LoadResource::ConstantEntries), 7);
|
||||
assert_eq!(b.used(LoadResource::ConstantStringBytes), 5);
|
||||
drop(bytes);
|
||||
assert_eq!(module.constants[5], K::String("é猫".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_payload_refusal_rolls_back_all_ownership_and_preserves_prior_budget() {
|
||||
let bytes = minimal_bytes();
|
||||
let mut full = budget();
|
||||
OwnedPbx::parse(&bytes, &mut full).unwrap();
|
||||
let limits = LoadLimits { work: full.used(LoadResource::Work) - 1, ..Default::default() };
|
||||
let mut b = LoadBudget::new(limits);
|
||||
b.charge(LoadResource::TemporaryBytes, 7).unwrap();
|
||||
b.charge(LoadResource::RegistryBytes, 3).unwrap();
|
||||
let failure = OwnedPbx::parse(&bytes, &mut b).unwrap_err();
|
||||
assert_eq!(failure.resource, Some(LoadResource::Work));
|
||||
assert_eq!(failure.section, Some(2));
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 7);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 3);
|
||||
assert_eq!(b.used(LoadResource::Types), 0);
|
||||
assert_eq!(b.used(LoadResource::ConstantEntries), 0);
|
||||
let mut limits = LoadLimits::default();
|
||||
limits.temporary_bytes = 1;
|
||||
let mut b = LoadBudget::new(limits);
|
||||
assert_eq!(
|
||||
OwnedPbx::parse(&bytes, &mut b).unwrap_err().resource,
|
||||
Some(LoadResource::TemporaryBytes)
|
||||
);
|
||||
assert_eq!(b.used(LoadResource::RegistryBytes), 0);
|
||||
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_module_writer_matches_independent_minimal_bytes_and_refunds_scratch() {
|
||||
let registry = minimal_registry();
|
||||
|
||||
Reference in New Issue
Block a user