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()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user