perf(translations): a stored entry keeps its recovery rows packed in one allocation, decoded when a fault rebases them: 614 to 488 bytes an entry of real code
This commit is contained in:
1 parent
33ec18cddc
commit
325274f05d
6 files changed
+457
-110
No files matched your search
@@ -110,7 +110,7 @@ impl Polluter {
|
||||
/// (`FETCH_PROFILE` names the file: one hex address a line, less the
|
||||
/// binary's load address).
|
||||
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
|
||||
mod sampler {
|
||||
pub(super) mod sampler {
|
||||
use core::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
const SAMPLES: usize = 1 << 20;
|
||||
static TAKEN: AtomicUsize = AtomicUsize::new(0);
|
||||
@@ -456,6 +456,81 @@ fn pack_record_compression() {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a stored entry of real code holds in memory: every block of the
|
||||
/// first `FETCH_IMAGE` file (from `FETCH_OFFSET`, `FETCH_BYTES`, default 8
|
||||
/// MiB) compiled and stored, the mean bytes an entry retains, its recovery
|
||||
/// rows' packed bytes and what the rows' own vectors took before
|
||||
/// ([`crate::translations::Rows`]).
|
||||
#[test]
|
||||
#[ignore = "manual: opt-in"]
|
||||
fn stored_entry_bytes() {
|
||||
use stela_compiler::block::{RecoveryBinding, UnqualifiedFaultRecovery};
|
||||
let number = |name: &str, default: usize| {
|
||||
std::env::var(name)
|
||||
.ok()
|
||||
.and_then(|value| value.parse().ok())
|
||||
.unwrap_or(default)
|
||||
};
|
||||
let path = std::env::var("FETCH_IMAGE").expect("FETCH_IMAGE");
|
||||
let path = path.split(',').next().unwrap_or_default();
|
||||
let (offset, bytes) = (
|
||||
number("FETCH_OFFSET", 1 << 20),
|
||||
number("FETCH_BYTES", 8 << 20),
|
||||
);
|
||||
let file = std::fs::read(path).expect("the code image");
|
||||
let image = &file[offset.min(file.len())..(offset + bytes).min(file.len())];
|
||||
let preparation = Arc::new(system_preparation());
|
||||
let options = system_options();
|
||||
let base = 0x1_8040_0000u64;
|
||||
let mut staging = [0u8; super::MAX_BLOCK_BYTES as usize];
|
||||
let (mut entries, mut retained, mut packed, mut unpacked, mut rows) = (0u64, 0, 0, 0, 0);
|
||||
let mut at = 0usize;
|
||||
while at + 4 <= image.len() {
|
||||
let pc = base + at as u64;
|
||||
let page = pc & !(FETCH_PAGE - 1);
|
||||
let page_end = (at as u64 | (FETCH_PAGE - 1)) + 1;
|
||||
let end = (page_end as usize).min(image.len()).min(at + window(pc));
|
||||
let length = end - at;
|
||||
staging[..length].copy_from_slice(&image[at..end]);
|
||||
preparation.admit(&mut staging[..length]);
|
||||
let Ok((analysis, Ok(plan))) =
|
||||
block::analyze_and_plan(&staging[..length], GuestAddress(pc), &options)
|
||||
else {
|
||||
at += 4;
|
||||
continue;
|
||||
};
|
||||
let Ok(compiled) = plan.compile() else {
|
||||
at += 4;
|
||||
continue;
|
||||
};
|
||||
let step = compiled.source_bytes.max(4);
|
||||
let input = analysis.source_bytes;
|
||||
let emission = Emission::new(staging[..input].to_vec(), compiled, preparation.clone());
|
||||
let context = preparation.context(page);
|
||||
if let Some(entry) = Entry::stored(&emission.block, &emission.input, page, pc, context) {
|
||||
entries += 1;
|
||||
retained += entry.retained_bytes() as u64;
|
||||
packed += entry.recovery.capacity() as u64;
|
||||
rows += entry.recovery.len() as u64;
|
||||
unpacked += (entry.recovery.len() * size_of::<UnqualifiedFaultRecovery>()) as u64;
|
||||
for row in entry.recovery.iter() {
|
||||
unpacked += (row.bindings().len() * size_of::<RecoveryBinding>()) as u64;
|
||||
}
|
||||
}
|
||||
at += step;
|
||||
}
|
||||
let per = |bytes: u64| bytes as f64 / entries.max(1) as f64;
|
||||
std::println!(
|
||||
"entries={entries} rows_per_entry={:.1} retained_per_entry={:.0} \
|
||||
recovery_packed={:.0} recovery_as_vectors={:.0} retained_as_vectors={:.0}",
|
||||
per(rows),
|
||||
per(retained),
|
||||
per(packed),
|
||||
per(unpacked),
|
||||
per(retained - packed + unpacked + 8 * entries),
|
||||
);
|
||||
}
|
||||
|
||||
/// The first window's bytes: the engine's, or `FETCH_FIRST` instructions'.
|
||||
fn first_window() -> usize {
|
||||
std::env::var("FETCH_FIRST")
|
||||
|
||||
@@ -282,7 +282,7 @@ mod tests {
|
||||
relocations: alloc::vec![],
|
||||
source_map: alloc::vec![(offset, 0)],
|
||||
exits: alloc::vec![],
|
||||
recovery: alloc::vec![],
|
||||
recovery: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,27 +201,28 @@ impl<'a> Template<'a> {
|
||||
/// of tags never grants private/APX admission: that must be derived from an
|
||||
/// compiler/profile ABI permit before future eager invocation.
|
||||
pub(super) fn portable_requires_unavailable_capture(&self) -> bool {
|
||||
use stela_compiler::block::{
|
||||
RecoveryBinding, RecoverySource, RetirementPhase, RetirementPoint,
|
||||
};
|
||||
let requires = |retirement: RetirementPoint, bindings: &[RecoveryBinding]| {
|
||||
retirement.phase() != RetirementPhase::Before
|
||||
|| bindings.iter().any(|binding| match binding.source {
|
||||
use stela_compiler::block::{RecoveryBinding, RecoverySource, RetirementPhase};
|
||||
fn requires(
|
||||
phase: RetirementPhase,
|
||||
mut bindings: impl Iterator<Item = RecoveryBinding>,
|
||||
) -> bool {
|
||||
phase != RetirementPhase::Before
|
||||
|| bindings.any(|binding| match binding.source {
|
||||
RecoverySource::HostRegister(register) => register >= 16,
|
||||
RecoverySource::NativeSpill(_) => true,
|
||||
})
|
||||
};
|
||||
}
|
||||
// Rebasing a row moves its PC alone, not its phase or bindings.
|
||||
match self {
|
||||
Self::Stored { entry, .. } => entry
|
||||
.recovery
|
||||
.iter()
|
||||
.any(|row| requires(row.retirement, &row.bindings)),
|
||||
.any(|row| requires(row.phase, row.bindings())),
|
||||
Self::Compiled(emission) => emission
|
||||
.block
|
||||
.fault_recovery
|
||||
.iter()
|
||||
.any(|row| requires(row.retirement, &row.bindings)),
|
||||
.any(|row| requires(row.retirement.phase(), row.bindings.iter().copied())),
|
||||
}
|
||||
}
|
||||
pub(super) fn matches_source(&self, source: &[u8]) -> bool {
|
||||
|
||||
@@ -16,7 +16,7 @@ pub mod writer;
|
||||
|
||||
pub use stored::StoredRecord;
|
||||
|
||||
use alloc::{string::String, vec::Vec};
|
||||
use alloc::{boxed::Box, string::String, vec::Vec};
|
||||
use stela_compiler::block::Features;
|
||||
use stela_compiler::{
|
||||
GuestAddress, NativeBlock,
|
||||
@@ -102,6 +102,209 @@ pub struct Exit {
|
||||
pub skipped: u16,
|
||||
}
|
||||
|
||||
/// A recovery binding's source as a kind and a value: the record's and
|
||||
/// [`Rows`]' encoding of it.
|
||||
fn binding_word(source: RecoverySource) -> (u8, u32) {
|
||||
match source {
|
||||
RecoverySource::HostRegister(host) => (0, u32::from(host)),
|
||||
RecoverySource::NativeSpill(offset) => (1, u32::from(offset.bytes())),
|
||||
}
|
||||
}
|
||||
|
||||
/// The source a kind and a value name, if they name one.
|
||||
fn binding_of(kind: u8, value: u32) -> Option<RecoverySource> {
|
||||
match kind {
|
||||
0 => u8::try_from(value).ok().map(RecoverySource::HostRegister),
|
||||
1 => SpillOffset::new(u16::try_from(value).ok()?)
|
||||
.ok()
|
||||
.map(RecoverySource::NativeSpill),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn put(out: &mut Vec<u8>, mut value: u64) {
|
||||
while value >= 0x80 {
|
||||
out.push(value as u8 | 0x80);
|
||||
value >>= 7;
|
||||
}
|
||||
out.push(value as u8);
|
||||
}
|
||||
|
||||
/// An entry's recovery rows packed in one allocation: what the decoded
|
||||
/// records and the kept forms hold of them, about a tenth of the rows'
|
||||
/// own vectors. Each value is LEB128: the row count, then per row its
|
||||
/// start, its end less its start (zigzag), its retirement's page-relative
|
||||
/// PC and count, a byte of phase (bit 0) and site (the rest), and its
|
||||
/// bindings: their count, and each its guest register, its source's kind
|
||||
/// and value ([`binding_word`]). Only [`Rows::push`] writes one.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Rows(Box<[u8]>);
|
||||
|
||||
impl Default for Rows {
|
||||
/// No rows.
|
||||
fn default() -> Self {
|
||||
Self::build(0, |_| {})
|
||||
}
|
||||
}
|
||||
|
||||
/// One row of [`Rows`], its retirement page-relative as stored.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Row<'a> {
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
pub pc: u64,
|
||||
pub retired_before: u16,
|
||||
pub phase: RetirementPhase,
|
||||
pub site: RecoverySiteKind,
|
||||
bindings: (usize, &'a [u8]),
|
||||
}
|
||||
|
||||
/// A reader of what [`Rows`] wrote.
|
||||
struct Packed<'a>(&'a [u8]);
|
||||
|
||||
impl Packed<'_> {
|
||||
fn byte(&mut self) -> u8 {
|
||||
let (&first, rest) = self.0.split_first().expect("written by Rows::push");
|
||||
self.0 = rest;
|
||||
first
|
||||
}
|
||||
|
||||
fn value(&mut self) -> u64 {
|
||||
let (mut value, mut shift) = (0, 0);
|
||||
loop {
|
||||
let byte = self.byte();
|
||||
value |= u64::from(byte & 0x7f) << shift;
|
||||
if byte & 0x80 == 0 {
|
||||
return value;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
}
|
||||
|
||||
fn binding(&mut self) -> RecoveryBinding {
|
||||
let guest = self.byte();
|
||||
let kind = self.byte();
|
||||
let value = self.value() as u32;
|
||||
RecoveryBinding {
|
||||
guest,
|
||||
source: binding_of(kind, value).expect("written by Rows::push"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Rows {
|
||||
/// Rows of `count` that `write` pushes ([`Self::push`]).
|
||||
fn build(count: usize, write: impl FnOnce(&mut Vec<u8>)) -> Self {
|
||||
let mut out = Vec::new();
|
||||
put(&mut out, count as u64);
|
||||
write(&mut out);
|
||||
Self(out.into_boxed_slice())
|
||||
}
|
||||
|
||||
/// Write one row of `bindings` into `out`.
|
||||
fn push(
|
||||
out: &mut Vec<u8>,
|
||||
start: usize,
|
||||
end: usize,
|
||||
retirement: (u64, u16, RetirementPhase),
|
||||
site: RecoverySiteKind,
|
||||
bindings: &[RecoveryBinding],
|
||||
) {
|
||||
let (pc, retired_before, phase) = retirement;
|
||||
put(out, start as u64);
|
||||
let length = (end as i64).wrapping_sub(start as i64);
|
||||
put(out, ((length << 1) ^ (length >> 63)) as u64);
|
||||
put(out, pc);
|
||||
put(out, u64::from(retired_before));
|
||||
let phase = match phase {
|
||||
RetirementPhase::Before => 0,
|
||||
RetirementPhase::AfterFallthrough => 1,
|
||||
};
|
||||
out.push(phase | (site as u8) << 1);
|
||||
put(out, bindings.len() as u64);
|
||||
for binding in bindings {
|
||||
let (kind, value) = binding_word(binding.source);
|
||||
out.push(binding.guest);
|
||||
out.push(kind);
|
||||
put(out, u64::from(value));
|
||||
}
|
||||
}
|
||||
|
||||
/// `rows` packed, their retirements as they are.
|
||||
pub fn pack(rows: &[UnqualifiedFaultRecovery]) -> Self {
|
||||
Self::build(rows.len(), |out| {
|
||||
for row in rows {
|
||||
let point = row.retirement;
|
||||
Self::push(
|
||||
out,
|
||||
row.start,
|
||||
row.end,
|
||||
(
|
||||
point.instruction_pc().0,
|
||||
point.retired_before(),
|
||||
point.phase(),
|
||||
),
|
||||
row.site,
|
||||
&row.bindings,
|
||||
);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
Packed(&self.0).value() as usize
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// The heap bytes it owns.
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl ExactSizeIterator<Item = Row<'_>> + '_ {
|
||||
let mut packed = Packed(&self.0);
|
||||
let count = packed.value() as usize;
|
||||
(0..count).map(move |_| {
|
||||
let start = packed.value() as usize;
|
||||
let length = packed.value();
|
||||
let length = (length >> 1) as i64 ^ -((length & 1) as i64);
|
||||
let pc = packed.value();
|
||||
let retired_before = packed.value() as u16;
|
||||
let flags = packed.byte();
|
||||
let phase = match flags & 1 {
|
||||
0 => RetirementPhase::Before,
|
||||
_ => RetirementPhase::AfterFallthrough,
|
||||
};
|
||||
let site = RecoverySiteKind::try_from(flags >> 1).expect("written by Rows::push");
|
||||
let bindings = packed.value() as usize;
|
||||
let from = packed.0;
|
||||
for _ in 0..bindings {
|
||||
packed.binding();
|
||||
}
|
||||
let used = from.len() - packed.0.len();
|
||||
Row {
|
||||
start,
|
||||
end: (start as i64).wrapping_add(length) as usize,
|
||||
pc,
|
||||
retired_before,
|
||||
phase,
|
||||
site,
|
||||
bindings: (bindings, &from[..used]),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Row<'a> {
|
||||
pub fn bindings(&self) -> impl ExactSizeIterator<Item = RecoveryBinding> + 'a {
|
||||
let mut packed = Packed(self.bindings.1);
|
||||
(0..self.bindings.0).map(move |_| packed.binding())
|
||||
}
|
||||
}
|
||||
|
||||
/// One stored block translation.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Entry {
|
||||
@@ -122,7 +325,8 @@ pub struct Entry {
|
||||
/// Page offset of each guest instruction and its native offset.
|
||||
pub source_map: Vec<(u16, u32)>,
|
||||
pub exits: Vec<Exit>,
|
||||
pub recovery: Vec<UnqualifiedFaultRecovery>,
|
||||
/// Its recovery rows, their retirements page-relative ([`Rows`]).
|
||||
pub recovery: Rows,
|
||||
}
|
||||
|
||||
/// A rebased immutable template. Persisted fault tags remain unchecked until
|
||||
@@ -151,11 +355,7 @@ impl Entry {
|
||||
bytes += self.relocations.capacity() * core::mem::size_of::<u32>();
|
||||
bytes += self.source_map.capacity() * core::mem::size_of::<(u16, u32)>();
|
||||
bytes += self.exits.capacity() * core::mem::size_of::<Exit>();
|
||||
bytes += self.recovery.capacity() * core::mem::size_of::<UnqualifiedFaultRecovery>();
|
||||
for row in &self.recovery {
|
||||
bytes += row.bindings.capacity() * core::mem::size_of::<RecoveryBinding>();
|
||||
}
|
||||
bytes
|
||||
bytes + self.recovery.capacity()
|
||||
}
|
||||
|
||||
/// What it costs in a record: its slot and its buffers.
|
||||
@@ -205,18 +405,24 @@ impl Entry {
|
||||
},
|
||||
source_bytes: usize::from(self.source_bytes),
|
||||
direct_exits,
|
||||
fault_recovery: self
|
||||
.recovery
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let mut row = row.clone();
|
||||
row.retirement = self.retirement_at(row.retirement, page)?;
|
||||
Some(row)
|
||||
})
|
||||
.collect::<Option<_>>()?,
|
||||
fault_recovery: self.recovery_at(page)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Its recovery rows, their retirements at the page whose address is
|
||||
/// `page` (0: page-relative, as stored).
|
||||
pub fn recovery_at(&self, page: u64) -> Option<Vec<UnqualifiedFaultRecovery>> {
|
||||
exact(self.recovery.iter().map(|row| {
|
||||
Some(UnqualifiedFaultRecovery {
|
||||
start: row.start,
|
||||
end: row.end,
|
||||
bindings: row.bindings().collect(),
|
||||
retirement: self.retirement_at(row.pc, row.retired_before, row.phase, page)?,
|
||||
site: row.site,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
/// What installing the block at the page whose address is `page` needs
|
||||
/// of it, written into `into`, whose buffers keep their capacity for the
|
||||
/// next: its code, rebased, and its direct exits. Its source map and
|
||||
@@ -238,15 +444,13 @@ impl Entry {
|
||||
}
|
||||
page.checked_add(u64::from(self.offset))?
|
||||
.checked_add(u64::from(self.source_bytes))?;
|
||||
if !self
|
||||
.recovery
|
||||
.iter()
|
||||
.all(|row| row.start < row.end && row.end <= self.code.len())
|
||||
|| !self
|
||||
.recovery
|
||||
.iter()
|
||||
.all(|row| self.retirement_at(row.retirement, page).is_some())
|
||||
{
|
||||
if !self.recovery.iter().all(|row| {
|
||||
row.start < row.end
|
||||
&& row.end <= self.code.len()
|
||||
&& self
|
||||
.retirement_at(row.pc, row.retired_before, row.phase, page)
|
||||
.is_some()
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
into.code.clear();
|
||||
@@ -271,23 +475,28 @@ impl Entry {
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// The retirement at page-relative `relative_pc` after `retired_before`
|
||||
/// instructions in `phase`, at the page whose address is `page`.
|
||||
/// Reconstruct only a source-backed retirement shape. This never mints a
|
||||
/// guest-access site or grants memory-effect permission from a portable tag.
|
||||
fn retirement_at(&self, point: RetirementPoint, page: u64) -> Option<RetirementPoint> {
|
||||
fn retirement_at(
|
||||
&self,
|
||||
relative_pc: u64,
|
||||
retired_before: u16,
|
||||
phase: RetirementPhase,
|
||||
page: u64,
|
||||
) -> Option<RetirementPoint> {
|
||||
use stela_compiler::Lifter as _;
|
||||
let relative = point
|
||||
.instruction_pc()
|
||||
.0
|
||||
.checked_sub(u64::from(self.offset))?;
|
||||
let relative = relative_pc.checked_sub(u64::from(self.offset))?;
|
||||
if !relative.is_multiple_of(4)
|
||||
|| relative > u64::from(self.source_bytes)
|
||||
|| u64::from(point.retired_before()) != relative / 4
|
||||
|| u64::from(retired_before) != relative / 4
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let pc = GuestAddress(page.checked_add(point.instruction_pc().0)?);
|
||||
match point.phase() {
|
||||
RetirementPhase::Before => RetirementPoint::before(pc, point.retired_before()).ok(),
|
||||
let pc = GuestAddress(page.checked_add(relative_pc)?);
|
||||
match phase {
|
||||
RetirementPhase::Before => RetirementPoint::before(pc, retired_before).ok(),
|
||||
RetirementPhase::AfterFallthrough => {
|
||||
let at = usize::try_from(relative).ok()?;
|
||||
if at.checked_add(4)? > usize::from(self.source_bytes) {
|
||||
@@ -295,12 +504,7 @@ impl Entry {
|
||||
}
|
||||
let word = u32::from_le_bytes(self.input.get(at..at + 4)?.try_into().ok()?);
|
||||
let instruction = stela_compiler::aarch64::Decoder.lift(pc, word).ok()?;
|
||||
RetirementPoint::from_instruction(
|
||||
&instruction,
|
||||
point.retired_before(),
|
||||
point.phase(),
|
||||
)
|
||||
.ok()
|
||||
RetirementPoint::from_instruction(&instruction, retired_before, phase).ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -350,13 +554,15 @@ impl Entry {
|
||||
&& self.exits.iter().all(|exit| {
|
||||
in_code(exit.offset, 0) && ExitKind::try_from(u64::from(exit.kind)).is_ok()
|
||||
})
|
||||
&& self.recovery.iter().all(|range| {
|
||||
range.start < range.end
|
||||
&& range.end <= code
|
||||
&& self.retirement_at(range.retirement, 0).is_some()
|
||||
&& (range.site == RecoverySiteKind::NativeInternal
|
||||
|| range.retirement.phase() == RetirementPhase::Before)
|
||||
&& range.bindings.iter().all(RecoveryBinding::is_valid)
|
||||
&& self.recovery.iter().all(|row| {
|
||||
row.start < row.end
|
||||
&& row.end <= code
|
||||
&& self
|
||||
.retirement_at(row.pc, row.retired_before, row.phase, 0)
|
||||
.is_some()
|
||||
&& (row.site == RecoverySiteKind::NativeInternal
|
||||
|| row.phase == RetirementPhase::Before)
|
||||
&& row.bindings().all(|binding| binding.is_valid())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -481,8 +687,8 @@ impl Record {
|
||||
+ entry.relocations.len() * RELOCATION_BYTES
|
||||
+ entry.source_map.len() * SOURCE_MAP_BYTES
|
||||
+ entry.exits.len() * EXIT_BYTES;
|
||||
for row in &entry.recovery {
|
||||
encoded_bytes += RECOVERY_ROW_BYTES + row.bindings.len() * RECOVERY_BINDING_BYTES;
|
||||
for row in entry.recovery.iter() {
|
||||
encoded_bytes += RECOVERY_ROW_BYTES + row.bindings.0 * RECOVERY_BINDING_BYTES;
|
||||
}
|
||||
}
|
||||
let mut out = Vec::with_capacity(encoded_bytes);
|
||||
@@ -518,25 +724,22 @@ impl Record {
|
||||
out.extend(exit.skipped.to_le_bytes());
|
||||
}
|
||||
out.extend((entry.recovery.len() as u32).to_le_bytes());
|
||||
for range in &entry.recovery {
|
||||
out.extend((range.start as u32).to_le_bytes());
|
||||
out.extend((range.end as u32).to_le_bytes());
|
||||
out.extend(range.retirement.instruction_pc().0.to_le_bytes());
|
||||
out.extend(range.retirement.retired_before().to_le_bytes());
|
||||
out.push(match range.retirement.phase() {
|
||||
for row in entry.recovery.iter() {
|
||||
out.extend((row.start as u32).to_le_bytes());
|
||||
out.extend((row.end as u32).to_le_bytes());
|
||||
out.extend(row.pc.to_le_bytes());
|
||||
out.extend(row.retired_before.to_le_bytes());
|
||||
out.push(match row.phase {
|
||||
RetirementPhase::Before => 0,
|
||||
RetirementPhase::AfterFallthrough => 1,
|
||||
});
|
||||
out.push(range.site as u8);
|
||||
out.extend((range.bindings.len() as u32).to_le_bytes());
|
||||
for binding in &range.bindings {
|
||||
out.push(row.site as u8);
|
||||
out.extend((row.bindings.0 as u32).to_le_bytes());
|
||||
for binding in row.bindings() {
|
||||
out.push(binding.guest);
|
||||
let (kind, value) = match binding.source {
|
||||
RecoverySource::HostRegister(host) => (0, u16::from(host)),
|
||||
RecoverySource::NativeSpill(offset) => (1, offset.bytes()),
|
||||
};
|
||||
let (kind, value) = binding_word(binding.source);
|
||||
out.push(kind);
|
||||
out.extend(value.to_le_bytes());
|
||||
out.extend((value as u16).to_le_bytes());
|
||||
}
|
||||
}
|
||||
out.extend(&entry.code);
|
||||
@@ -701,7 +904,10 @@ impl<'a> Reader<'a> {
|
||||
skipped: self.u16()?,
|
||||
})
|
||||
})?;
|
||||
let recovery = decode_items(self.count(RECOVERY_ROW_BYTES)?, || {
|
||||
let rows = self.count(RECOVERY_ROW_BYTES)?;
|
||||
let mut packed = Vec::new();
|
||||
let mut bindings = Vec::new();
|
||||
for _ in 0..rows {
|
||||
let start = self.u32()? as usize;
|
||||
let end = self.u32()? as usize;
|
||||
let pc = GuestAddress(self.u64()?);
|
||||
@@ -712,25 +918,18 @@ impl<'a> Reader<'a> {
|
||||
_ => return Err(FormatError::Entry),
|
||||
};
|
||||
let site = RecoverySiteKind::try_from(self.u8()?).map_err(|_| FormatError::Entry)?;
|
||||
let bindings = decode_items(self.count(RECOVERY_BINDING_BYTES)?, || {
|
||||
bindings.clear();
|
||||
for _ in 0..self.count(RECOVERY_BINDING_BYTES)? {
|
||||
let guest = self.u8()?;
|
||||
let kind = self.u8()?;
|
||||
let value = self.u16()?;
|
||||
let source = match kind {
|
||||
0 => RecoverySource::HostRegister(
|
||||
u8::try_from(value).map_err(|_| FormatError::Entry)?,
|
||||
),
|
||||
1 => RecoverySource::NativeSpill(
|
||||
SpillOffset::new(value).map_err(|_| FormatError::Entry)?,
|
||||
),
|
||||
_ => return Err(FormatError::Entry),
|
||||
};
|
||||
Ok(RecoveryBinding { guest, source })
|
||||
})?;
|
||||
let source = binding_of(kind, u32::from(value)).ok_or(FormatError::Entry)?;
|
||||
bindings.push(RecoveryBinding { guest, source });
|
||||
}
|
||||
// Build a structurally valid source-backed shape using an
|
||||
// entry view below; no raw After constructor is exposed.
|
||||
let before = RetirementPoint::before(pc, count).map_err(|_| FormatError::Entry)?;
|
||||
let retirement = match phase {
|
||||
match phase {
|
||||
RetirementPhase::Before => before,
|
||||
RetirementPhase::AfterFallthrough => {
|
||||
use stela_compiler::Lifter as _;
|
||||
@@ -756,14 +955,16 @@ impl<'a> Reader<'a> {
|
||||
.map_err(|_| FormatError::Entry)?
|
||||
}
|
||||
};
|
||||
Ok(UnqualifiedFaultRecovery {
|
||||
Rows::push(
|
||||
&mut packed,
|
||||
start,
|
||||
end,
|
||||
bindings,
|
||||
retirement,
|
||||
(pc.0, count, phase),
|
||||
site,
|
||||
})
|
||||
})?;
|
||||
&bindings,
|
||||
);
|
||||
}
|
||||
let recovery = Rows::build(rows, |out| out.extend_from_slice(&packed));
|
||||
let code = self.take(code_bytes)?.to_vec();
|
||||
Ok(Entry {
|
||||
context,
|
||||
@@ -956,16 +1157,25 @@ impl Entry {
|
||||
skipped: exit.skipped,
|
||||
})
|
||||
}))?,
|
||||
recovery: exact(block.fault_recovery.iter().map(|row| {
|
||||
let mut row = UnqualifiedFaultRecovery::from(row);
|
||||
row.retirement = row
|
||||
.retirement
|
||||
.relocated(GuestAddress(
|
||||
row.retirement.instruction_pc().0.checked_sub(page)?,
|
||||
))
|
||||
.ok()?;
|
||||
Some(row)
|
||||
}))?,
|
||||
recovery: {
|
||||
let mut out = Vec::new();
|
||||
for row in &block.fault_recovery {
|
||||
let point = row.retirement;
|
||||
Rows::push(
|
||||
&mut out,
|
||||
row.start,
|
||||
row.end,
|
||||
(
|
||||
point.instruction_pc().0.checked_sub(page)?,
|
||||
point.retired_before(),
|
||||
point.phase(),
|
||||
),
|
||||
row.site.kind(),
|
||||
&row.bindings,
|
||||
);
|
||||
}
|
||||
Rows::build(block.fault_recovery.len(), |head| head.extend(out))
|
||||
},
|
||||
};
|
||||
entry.consistent().then_some(entry)
|
||||
}
|
||||
|
||||
@@ -210,6 +210,63 @@ fn source_witness_geometry_is_checked_even_with_a_valid_record_checksum() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Packed rows give back every row as it was: its range, its retirement
|
||||
/// and each binding of either source, in a tenth of the rows' vectors.
|
||||
#[test]
|
||||
fn packed_recovery_rows_give_back_the_rows_they_hold() {
|
||||
use stela_compiler::block::RecoveryBinding;
|
||||
let source = words(&[0xd503201f, 0xd503201f, 0xd503201f, 0xd4000001]);
|
||||
let mut entry = proved(&source, 0x40).unwrap();
|
||||
let rows: Vec<UnqualifiedFaultRecovery> = (0..3u16)
|
||||
.map(|at| UnqualifiedFaultRecovery {
|
||||
start: usize::from(at) * 300,
|
||||
end: usize::from(at) * 300 + 1 + usize::from(at),
|
||||
bindings: (0..u8::try_from(at).unwrap() * 3)
|
||||
.map(|guest| RecoveryBinding {
|
||||
guest,
|
||||
source: match guest % 2 {
|
||||
0 => RecoverySource::HostRegister(guest % 16),
|
||||
_ => RecoverySource::NativeSpill(
|
||||
SpillOffset::new(8 * u16::from(guest)).unwrap(),
|
||||
),
|
||||
},
|
||||
})
|
||||
.collect(),
|
||||
retirement: RetirementPoint::before(GuestAddress(0x40 + 4 * u64::from(at)), at)
|
||||
.unwrap(),
|
||||
site: RecoverySiteKind::NativeInternal,
|
||||
})
|
||||
.collect();
|
||||
entry.code.resize(1000, 0x90);
|
||||
entry.recovery = Rows::pack(&rows);
|
||||
assert_eq!(entry.recovery.len(), 3);
|
||||
assert_eq!(entry.recovery_at(0).unwrap(), rows);
|
||||
let moved = entry.recovery_at(PAGE).unwrap();
|
||||
for (moved, row) in moved.iter().zip(&rows) {
|
||||
assert_eq!(
|
||||
moved.retirement.instruction_pc().0,
|
||||
PAGE + row.retirement.instruction_pc().0
|
||||
);
|
||||
assert_eq!(moved.bindings, row.bindings);
|
||||
}
|
||||
let vectors = rows.len() * size_of::<UnqualifiedFaultRecovery>()
|
||||
+ rows
|
||||
.iter()
|
||||
.map(|row| row.bindings.len() * size_of::<RecoveryBinding>())
|
||||
.sum::<usize>();
|
||||
assert!(
|
||||
entry.recovery.capacity() * 4 < vectors,
|
||||
"{}",
|
||||
entry.recovery.capacity()
|
||||
);
|
||||
// A record keeps them through its encoding.
|
||||
let (translator, key) = (translator_id(), Digest::of(b"packed rows page"));
|
||||
let mut record = Record::new(translator, key);
|
||||
record.insert(entry.clone());
|
||||
let decoded = Record::decode(&record.encode(), &translator, &key).unwrap();
|
||||
assert_eq!(decoded.entries()[0], entry);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn portable_after_phase_requires_the_kept_fallthrough_instruction() {
|
||||
use stela_compiler::Lifter as _;
|
||||
@@ -218,13 +275,13 @@ fn portable_after_phase_requires_the_kept_fallthrough_instruction() {
|
||||
let instruction = stela_compiler::aarch64::Decoder
|
||||
.lift(GuestAddress(0x40), 0xd503201f)
|
||||
.unwrap();
|
||||
entry.recovery = vec![UnqualifiedFaultRecovery {
|
||||
entry.recovery = Rows::pack(&[UnqualifiedFaultRecovery {
|
||||
start: 0,
|
||||
end: 1,
|
||||
bindings: Vec::new(),
|
||||
retirement: RetirementPoint::after_fallthrough(&instruction, 0).unwrap(),
|
||||
site: RecoverySiteKind::NativeInternal,
|
||||
}];
|
||||
}]);
|
||||
let (translator, key) = (translator_id(), Digest::of(b"phase fixture raw page"));
|
||||
let encode = |entry| {
|
||||
let mut record = Record::new(translator, key);
|
||||
@@ -250,7 +307,9 @@ fn portable_after_phase_requires_the_kept_fallthrough_instruction() {
|
||||
"serialized After does not authorize RET fallthrough"
|
||||
);
|
||||
let mut guest_after = entry;
|
||||
guest_after.recovery[0].site = RecoverySiteKind::GuestAccess;
|
||||
let mut rows = guest_after.recovery_at(0).unwrap();
|
||||
rows[0].site = RecoverySiteKind::GuestAccess;
|
||||
guest_after.recovery = Rows::pack(&rows);
|
||||
assert_eq!(
|
||||
Record::decode(&encode(guest_after), &translator, &key),
|
||||
Err(FormatError::Entry)
|
||||
@@ -263,13 +322,13 @@ fn portable_before_endpoint_keeps_exact_prefix_count_and_checked_relocation() {
|
||||
let mut entry = proved(&source, 0x40).unwrap();
|
||||
let end = u64::from(entry.offset) + u64::from(entry.source_bytes);
|
||||
let count = entry.source_bytes / 4;
|
||||
entry.recovery = vec![UnqualifiedFaultRecovery {
|
||||
entry.recovery = Rows::pack(&[UnqualifiedFaultRecovery {
|
||||
start: 0,
|
||||
end: 1,
|
||||
bindings: Vec::new(),
|
||||
retirement: RetirementPoint::before(GuestAddress(end), count).unwrap(),
|
||||
site: RecoverySiteKind::NativeInternal,
|
||||
}];
|
||||
}]);
|
||||
assert!(entry.consistent());
|
||||
let rebased = entry.rebase(PAGE, &source).unwrap();
|
||||
assert_eq!(
|
||||
@@ -296,7 +355,9 @@ fn portable_before_endpoint_keeps_exact_prefix_count_and_checked_relocation() {
|
||||
Record::decode(&record.encode(), &translator, &key),
|
||||
Ok(record)
|
||||
);
|
||||
entry.recovery[0].retirement = RetirementPoint::before(GuestAddress(end), count - 1).unwrap();
|
||||
let mut rows = entry.recovery_at(0).unwrap();
|
||||
rows[0].retirement = RetirementPoint::before(GuestAddress(end), count - 1).unwrap();
|
||||
entry.recovery = Rows::pack(&rows);
|
||||
let mut bad = Record::new(translator, key);
|
||||
bad.insert(entry);
|
||||
assert_eq!(
|
||||
|
||||
@@ -227,7 +227,7 @@ fn entry(offset: u16, fill: u8, bytes: usize) -> crate::translations::Entry {
|
||||
// A merge decodes what two segments hold with every check.
|
||||
source_map: vec![(offset, 0)],
|
||||
exits: Vec::new(),
|
||||
recovery: Vec::new(),
|
||||
recovery: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user