Index string provenance with metered image and session tables
This commit is contained in:
@@ -18,3 +18,7 @@ fpmath = { version = "=0.1.1", features = ["soft-float"] }
|
||||
|
||||
[dev-dependencies]
|
||||
prometeu-test-support = { path = "../../dev/prometeu-test-support" }
|
||||
|
||||
[[example]]
|
||||
name = "string_provenance_benchmark"
|
||||
required-features = ["resource-diagnostics"]
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//! Run in release with --features resource-diagnostics; query setup is excluded.
|
||||
#[allow(dead_code)]
|
||||
#[path = "../tests/support/callables.rs"]
|
||||
mod callables;
|
||||
use prometeu_bytecode::model::ConstantPoolEntry;
|
||||
use prometeu_vm::string_provenance_qualification::Fixture;
|
||||
use std::{
|
||||
alloc::{GlobalAlloc, Layout, System},
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
time::Instant,
|
||||
};
|
||||
struct Counting;
|
||||
static ALLOC: AtomicUsize = AtomicUsize::new(0);
|
||||
static REALLOC: AtomicUsize = AtomicUsize::new(0);
|
||||
static FREE: AtomicUsize = AtomicUsize::new(0);
|
||||
unsafe impl GlobalAlloc for Counting {
|
||||
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
|
||||
ALLOC.fetch_add(1, Ordering::Relaxed);
|
||||
unsafe { System.alloc(l) }
|
||||
}
|
||||
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
|
||||
FREE.fetch_add(1, Ordering::Relaxed);
|
||||
unsafe { System.dealloc(p, l) }
|
||||
}
|
||||
unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
|
||||
REALLOC.fetch_add(1, Ordering::Relaxed);
|
||||
unsafe { System.realloc(p, l, n) }
|
||||
}
|
||||
}
|
||||
#[global_allocator]
|
||||
static GLOBAL: Counting = Counting;
|
||||
fn counts() -> [usize; 3] {
|
||||
[ALLOC.load(Ordering::Relaxed), REALLOC.load(Ordering::Relaxed), FREE.load(Ordering::Relaxed)]
|
||||
}
|
||||
fn main() {
|
||||
for n in [1024, 16384, 65536] {
|
||||
let constants = vec![ConstantPoolEntry::String("same".into()); n];
|
||||
let start = Instant::now();
|
||||
let image = callables::image_constants(
|
||||
&[("RET", callables::sig(&[], &[]), vec![])],
|
||||
vec![],
|
||||
&constants,
|
||||
)
|
||||
.unwrap();
|
||||
let image_ns = start.elapsed().as_nanos();
|
||||
let payload = image.payload_bytes();
|
||||
let before = counts();
|
||||
let start = Instant::now();
|
||||
let mut fixture = Fixture::new(image, n);
|
||||
let creation_ns = start.elapsed().as_nanos();
|
||||
let after = counts();
|
||||
println!(
|
||||
"setup n={n} image_load_ns={image_ns} image_payload_bytes={payload} session_ns={creation_ns} session_allocs={} auxiliary_bytes={} work={:?}",
|
||||
after[0] - before[0],
|
||||
fixture.auxiliary_bytes(),
|
||||
fixture.work()
|
||||
);
|
||||
for name in [
|
||||
"image_hit",
|
||||
"image_miss",
|
||||
"session_hit",
|
||||
"session_miss",
|
||||
"import_hit",
|
||||
"snapshot_mixed",
|
||||
"import_miss",
|
||||
] {
|
||||
let before = counts();
|
||||
let start = Instant::now();
|
||||
for _ in 0..1000 {
|
||||
fixture.run(name);
|
||||
}
|
||||
let ns = start.elapsed().as_nanos();
|
||||
let after = counts();
|
||||
let delta = std::array::from_fn::<_, 3, _>(|i| after[i] - before[i]);
|
||||
if name.ends_with("hit") || name == "image_miss" || name == "session_miss" {
|
||||
assert_eq!(delta, [0; 3]);
|
||||
}
|
||||
println!(
|
||||
"query n={n} name={name} iterations=1000 ns={ns} alloc_realloc_free={delta:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ mod program_image;
|
||||
mod registry_verifier;
|
||||
pub use program_image::{ImageStringLease, ValidatedProgramImage};
|
||||
// Image/session creation and native provenance consume this in step 19.
|
||||
mod string_identity_index;
|
||||
#[allow(dead_code)]
|
||||
mod string_ledger;
|
||||
// Registry-backed consumers are integrated with executable images in PLN-0176.
|
||||
@@ -52,3 +53,6 @@ pub use virtual_machine::{BudgetReport, LogicalFrameEndingReason, VirtualMachine
|
||||
pub use vm_init_error::{LoaderPatchError, VmInitError};
|
||||
|
||||
pub use verifier::VerifierError;
|
||||
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub use virtual_machine::string_provenance_qualification;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! PBX v1 publication: immutable metadata and shared payload, after bindings
|
||||
//! and bounded whole-program verification. No public unchecked constructor.
|
||||
use crate::string_identity_index::Identity;
|
||||
use crate::{LoaderPatchError, VmInitError};
|
||||
use prometeu_bytecode::isa::core::CoreOpCode as Op;
|
||||
use prometeu_bytecode::load_limits::{
|
||||
@@ -107,8 +108,30 @@ impl ValidatedProgramImage {
|
||||
Some(ImageStringLease { content: content.clone(), domain: self.strings.clone() })
|
||||
}
|
||||
pub fn retains_string(&self, content: &Arc<str>) -> bool {
|
||||
self.lookup_string(content, || {})
|
||||
}
|
||||
fn lookup_string(&self, content: &Arc<str>, mut compare: impl FnMut()) -> 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)))
|
||||
let target = Identity::of(content);
|
||||
let mut low = 0;
|
||||
let mut high = strings.records.len();
|
||||
while low < high {
|
||||
let mid = low + (high - low) / 2;
|
||||
compare();
|
||||
match record_identity(&strings.records[mid]).cmp(&target) {
|
||||
std::cmp::Ordering::Less => low = mid + 1,
|
||||
std::cmp::Ordering::Greater => high = mid,
|
||||
std::cmp::Ordering::Equal => return true,
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "resource-diagnostics"))]
|
||||
pub(crate) fn lookup_work(&self, content: &Arc<str>) -> (bool, usize) {
|
||||
let mut work = 0;
|
||||
let found = self.lookup_string(content, || work += 1);
|
||||
(found, work)
|
||||
}
|
||||
pub fn constant_content(&self) -> (u64, usize) {
|
||||
let mut strings = self.strings.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
@@ -205,6 +228,7 @@ impl ValidatedProgramImage {
|
||||
};
|
||||
values.push(value);
|
||||
}
|
||||
sort_string_records(&mut records, b)?;
|
||||
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.
|
||||
@@ -256,6 +280,48 @@ impl ValidatedProgramImage {
|
||||
}
|
||||
}
|
||||
|
||||
fn record_identity(record: &(Weak<str>, usize)) -> Identity {
|
||||
Identity { address: record.0.as_ptr() as *const u8 as usize, length: record.1 }
|
||||
}
|
||||
|
||||
// In-place heapsort: every record comparison (four field reads + comparison)
|
||||
// and swap (two record reads + writes) is precharged. No scratch allocation.
|
||||
fn sort_string_records(records: &mut [(Weak<str>, usize)], b: &mut LoadBudget) -> Result<(), E> {
|
||||
fn sift(
|
||||
records: &mut [(Weak<str>, usize)],
|
||||
mut root: usize,
|
||||
end: usize,
|
||||
b: &mut LoadBudget,
|
||||
) -> Result<(), E> {
|
||||
while root < end / 2 {
|
||||
let mut child = root * 2 + 1;
|
||||
if child + 1 < end {
|
||||
b.work(5)?;
|
||||
if record_identity(&records[child]) < record_identity(&records[child + 1]) {
|
||||
child += 1;
|
||||
}
|
||||
}
|
||||
b.work(5)?;
|
||||
if record_identity(&records[root]) >= record_identity(&records[child]) {
|
||||
break;
|
||||
}
|
||||
b.work(4)?;
|
||||
records.swap(root, child);
|
||||
root = child;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
for root in (0..records.len() / 2).rev() {
|
||||
sift(records, root, records.len(), b)?;
|
||||
}
|
||||
for end in (1..records.len()).rev() {
|
||||
b.work(4)?;
|
||||
records.swap(0, end);
|
||||
sift(records, 0, end, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn scratch<T>(count: usize, b: &mut LoadBudget) -> Result<Vec<T>, E> {
|
||||
let bytes = count
|
||||
.checked_mul(size_of::<T>())
|
||||
|
||||
@@ -309,3 +309,65 @@ fn sealed_image_load_probe() {
|
||||
elapsed
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_identity_binary_search_preserves_constant_order_and_lease_indices() {
|
||||
let constants = (0..1024).map(|i| K::String(format!("owner-{i}"))).collect::<Vec<_>>();
|
||||
let bytes = fixture(&[1, 0], 0, &constants, None, &[]);
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut budget(), caps::NONE).unwrap();
|
||||
for (i, value) in image.constants().iter().enumerate() {
|
||||
let Value::String(text) = value else { panic!() };
|
||||
assert_eq!(&**text, format!("owner-{i}"));
|
||||
let (found, work) = image.lookup_work(text);
|
||||
assert!(found && work <= 11, "binary work={work}");
|
||||
assert!(Arc::ptr_eq(text, image.lease_constant(i).unwrap().content()));
|
||||
assert!(!image.lookup_work(&Arc::from(text.as_ref())).0);
|
||||
}
|
||||
let lease = image.lease_constant(1023).unwrap();
|
||||
drop(image);
|
||||
assert_eq!(lease.retained_content(), (10, 1));
|
||||
let domain = lease.domain.lock().unwrap();
|
||||
assert_eq!(record_identity(&domain.records[0]), Identity::of(lease.content()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_heap_sort_precharges_each_comparison_and_swap_without_scratch() {
|
||||
let owners = (0..16).map(|_| Arc::<str>::from("same")).collect::<Vec<_>>();
|
||||
let source = owners.iter().rev().map(|v| (Arc::downgrade(v), v.len())).collect::<Vec<_>>();
|
||||
let mut full = budget();
|
||||
let mut records = source.clone();
|
||||
sort_string_records(&mut records, &mut full).unwrap();
|
||||
assert!(records.windows(2).all(|v| record_identity(&v[0]) < record_identity(&v[1])));
|
||||
assert_eq!(full.used(Q::TemporaryBytes), 0);
|
||||
for limit in 0..full.used(Q::Work) {
|
||||
let mut b = LoadBudget::new(LoadLimits { work: limit, ..Default::default() });
|
||||
let mut records = source.clone();
|
||||
let error = sort_string_records(&mut records, &mut b).unwrap_err();
|
||||
assert_eq!(error.resource, Some(Q::Work));
|
||||
assert!(b.used(Q::Work) <= limit);
|
||||
assert_eq!(b.used(Q::TemporaryBytes), 0);
|
||||
// Failed sort still owns exactly the original Weak identities.
|
||||
records.sort_unstable_by_key(record_identity);
|
||||
let mut expected = source.clone();
|
||||
expected.sort_unstable_by_key(record_identity);
|
||||
assert!(records.iter().zip(&expected).all(|(a, b)| Weak::ptr_eq(&a.0, &b.0)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_sort_exhaustion_rolls_back_sealed_load_domains() {
|
||||
let constants = vec![K::String("same".into()); 2];
|
||||
let bytes = fixture(&[1, 0], 0, &constants, None, &[]);
|
||||
let mut full = budget();
|
||||
ValidatedProgramImage::load(&bytes, &mut full, caps::NONE).unwrap();
|
||||
// Post-sort sharing costs 2 + 2 + 1 work units. Sorting two records
|
||||
// costs 9 or 13 units, depending on their addresses. Removing another
|
||||
// five leaves at most eight: failure is inside sorting for either order.
|
||||
let mut b =
|
||||
LoadBudget::new(LoadLimits { work: full.used(Q::Work) - 5 - 5, ..Default::default() });
|
||||
let error = ValidatedProgramImage::load(&bytes, &mut b, caps::NONE).unwrap_err();
|
||||
assert!(matches!(error,VmInitError::RegistryLoadFailed(e) if e.resource==Some(Q::Work)));
|
||||
for resource in DOMAINS {
|
||||
assert_eq!(b.used(resource), 0, "{resource:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,6 +385,10 @@ impl<T> BudgetVec<T> {
|
||||
let bytes = b.bytes::<T>(capacity, q)?;
|
||||
let mut extra = b.reserve(q, bytes - self.charge.amount)?;
|
||||
let temporary = b.reserve(RuntimeResource::TemporaryBytes, self.charge.amount)?;
|
||||
#[cfg(test)]
|
||||
if FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.replace(false)) {
|
||||
return Err(b.failure(q, ResourceReason::AllocationFailed, bytes));
|
||||
}
|
||||
let mut candidate = Vec::new();
|
||||
candidate
|
||||
.try_reserve_exact(capacity)
|
||||
@@ -405,6 +409,21 @@ impl<T> BudgetVec<T> {
|
||||
self.charge.absorb(growth.extra);
|
||||
}
|
||||
}
|
||||
/// Rebuild a table with all fallible capacity/coexistence admission completed
|
||||
/// before moving any old entry. The relocation callback must not allocate.
|
||||
pub(crate) fn rebuild_with(
|
||||
&mut self,
|
||||
len: usize,
|
||||
vacant: impl FnMut() -> T,
|
||||
relocate: impl FnOnce(&mut [T], &mut [T]),
|
||||
) -> Result<(), ResourceFailure> {
|
||||
let mut growth = self.prepare_capacity(len.max(self.data.capacity()))?;
|
||||
growth.candidate.resize_with(len, vacant);
|
||||
relocate(&mut self.data, &mut growth.candidate);
|
||||
self.data = growth.candidate;
|
||||
self.charge.absorb(growth.extra);
|
||||
Ok(())
|
||||
}
|
||||
pub fn reserve(&mut self, additional: usize) -> Result<(), ResourceFailure> {
|
||||
let growth = self.prepare_growth(additional)?;
|
||||
self.commit_growth(growth);
|
||||
@@ -445,9 +464,6 @@ impl<T> BudgetVec<T> {
|
||||
pub fn clear(&mut self) {
|
||||
self.data.clear();
|
||||
}
|
||||
pub(crate) fn retain(&mut self, keep: impl FnMut(&T) -> bool) {
|
||||
self.data.retain(keep);
|
||||
}
|
||||
pub(crate) fn extend_from_slice(&mut self, values: &[T]) -> Result<(), ResourceFailure>
|
||||
where
|
||||
T: Clone,
|
||||
@@ -509,3 +525,8 @@ pub(crate) struct VecGrowth<T> {
|
||||
_temporary: Reservation,
|
||||
old_capacity: usize,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub(crate) static FAIL_NEXT_VEC_ALLOCATION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
//! Private allocation identity index. Values retain Weak ownership, never content.
|
||||
use crate::resource_budget::{
|
||||
BudgetVec, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub(crate) struct Identity {
|
||||
pub(crate) address: usize,
|
||||
pub(crate) length: usize,
|
||||
}
|
||||
impl Identity {
|
||||
pub(crate) fn of(content: &Arc<str>) -> Self {
|
||||
Self { address: Arc::as_ptr(content) as *const u8 as usize, length: content.len() }
|
||||
}
|
||||
fn hash(self) -> usize {
|
||||
// Mix aligned addresses and lengths; collisions still compare both fields.
|
||||
let mut n = (self.address as u64) ^ (self.length as u64).wrapping_mul(0x9e3779b97f4a7c15);
|
||||
n = (n ^ (n >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
|
||||
n = (n ^ (n >> 27)).wrapping_mul(0x94d049bb133111eb);
|
||||
(n ^ (n >> 31)) as usize
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
enum Bucket<T> {
|
||||
Empty,
|
||||
Tombstone,
|
||||
Occupied(Identity, T),
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct IdentityIndex<T> {
|
||||
buckets: BudgetVec<Bucket<T>>,
|
||||
live: usize,
|
||||
tombstones: usize,
|
||||
}
|
||||
impl<T> IdentityIndex<T> {
|
||||
pub(crate) fn new(budget: &RuntimeBudget) -> Self {
|
||||
Self { buckets: BudgetVec::new(budget, Q::AuxiliaryBytes), live: 0, tombstones: 0 }
|
||||
}
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.live
|
||||
}
|
||||
pub(crate) fn contains(&self, key: Identity, mut probe: impl FnMut()) -> bool {
|
||||
let size = self.buckets.len();
|
||||
if size == 0 {
|
||||
return false;
|
||||
}
|
||||
let mut at = key.hash() & (size - 1);
|
||||
loop {
|
||||
probe();
|
||||
match &self.buckets[at] {
|
||||
Bucket::Empty => return false,
|
||||
Bucket::Occupied(found, _) if *found == key => return true,
|
||||
_ => at = (at + 1) & (size - 1),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn prepare(&mut self, budget: &RuntimeBudget) -> Result<(), ResourceFailure> {
|
||||
let size = self.buckets.len();
|
||||
let next = self.live + 1;
|
||||
let capacity = if size == 0 {
|
||||
8
|
||||
} else if next > size / 2 {
|
||||
size.checked_mul(2).ok_or_else(|| {
|
||||
budget.failure(Q::AuxiliaryBytes, ResourceReason::SizeOverflow, u64::MAX)
|
||||
})?
|
||||
} else if next + self.tombstones > size / 2 {
|
||||
size
|
||||
} else {
|
||||
return Ok(());
|
||||
};
|
||||
self.buckets.rebuild_with(
|
||||
capacity,
|
||||
|| Bucket::Empty,
|
||||
|old, new| {
|
||||
for slot in old {
|
||||
if let Bucket::Occupied(key, value) = std::mem::replace(slot, Bucket::Empty) {
|
||||
Self::place(new, key, value);
|
||||
}
|
||||
}
|
||||
},
|
||||
)?;
|
||||
self.tombstones = 0;
|
||||
Ok(())
|
||||
}
|
||||
fn place(buckets: &mut [Bucket<T>], key: Identity, value: T) -> bool {
|
||||
let mut at = key.hash() & (buckets.len() - 1);
|
||||
loop {
|
||||
match &buckets[at] {
|
||||
Bucket::Empty | Bucket::Tombstone => {
|
||||
let reused = matches!(buckets[at], Bucket::Tombstone);
|
||||
buckets[at] = Bucket::Occupied(key, value);
|
||||
return reused;
|
||||
}
|
||||
Bucket::Occupied(found, _) => {
|
||||
assert_ne!(*found, key, "duplicate allocation identity")
|
||||
}
|
||||
}
|
||||
at = (at + 1) & (buckets.len() - 1);
|
||||
}
|
||||
}
|
||||
pub(crate) fn insert(&mut self, key: Identity, value: T) {
|
||||
assert!(self.live < self.buckets.len() / 2, "prepare must precede publication");
|
||||
if Self::place(&mut self.buckets, key, value) {
|
||||
self.tombstones -= 1;
|
||||
}
|
||||
self.live += 1;
|
||||
}
|
||||
pub(crate) fn retain(&mut self, mut keep: impl FnMut(&T) -> bool) {
|
||||
for slot in &mut *self.buckets {
|
||||
if matches!(slot, Bucket::Occupied(_, value) if !keep(value)) {
|
||||
// Erase the key before releasing the Weak/reservations. Its
|
||||
// allocation address cannot be recycled while Weak survives.
|
||||
let removed = std::mem::replace(slot, Bucket::Tombstone);
|
||||
self.live -= 1;
|
||||
self.tombstones += 1;
|
||||
drop(removed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,72 @@
|
||||
use super::*;
|
||||
#[test]
|
||||
fn collision_chains_distinguish_full_identity_and_survive_tombstones() {
|
||||
let b = RuntimeBudget::default();
|
||||
let mut table = IdentityIndex::new(&b);
|
||||
let keys: Vec<_> = (0..1000)
|
||||
.map(|address| Identity { address, length: 4 })
|
||||
.filter(|k| k.hash() & 7 == 0)
|
||||
.take(4)
|
||||
.collect();
|
||||
for (i, key) in keys.iter().enumerate() {
|
||||
table.prepare(&b).unwrap();
|
||||
table.insert(*key, i);
|
||||
}
|
||||
for key in &keys {
|
||||
assert!(table.contains(*key, || {}));
|
||||
}
|
||||
table.retain(|i| *i != 1);
|
||||
assert!(!table.contains(keys[1], || {}));
|
||||
assert!(table.contains(keys[3], || {}));
|
||||
assert!(!table.contains(Identity { length: 5, ..keys[3] }, || {}));
|
||||
table.prepare(&b).unwrap(); // Rebuild removes tombstones at half occupancy.
|
||||
table.insert(keys[1], 1);
|
||||
assert_eq!(table.len(), 4);
|
||||
assert_eq!(table.tombstones, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tombstone_reuse_keeps_collision_successors_and_length_is_part_of_key() {
|
||||
let b = RuntimeBudget::default();
|
||||
let mut t = IdentityIndex::new(&b);
|
||||
let key = Identity { address: 16, length: 0 };
|
||||
t.prepare(&b).unwrap();
|
||||
t.insert(key, 1);
|
||||
t.prepare(&b).unwrap();
|
||||
t.insert(Identity { length: 1, ..key }, 2);
|
||||
t.retain(|v| *v != 1);
|
||||
assert_eq!(t.tombstones, 1);
|
||||
t.prepare(&b).unwrap();
|
||||
t.insert(key, 3);
|
||||
assert_eq!(t.tombstones, 0);
|
||||
assert!(t.contains(key, || {}));
|
||||
assert!(t.contains(Identity { length: 1, ..key }, || {}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn controlled_probe_counts_and_half_load_survive_growth_and_rebuild() {
|
||||
let b = RuntimeBudget::default();
|
||||
let mut table = IdentityIndex::new(&b);
|
||||
for n in 0..1024 {
|
||||
table.prepare(&b).unwrap();
|
||||
table.insert(Identity { address: n * 16, length: n % 7 }, n);
|
||||
assert!(table.live * 2 <= table.buckets.len());
|
||||
}
|
||||
let mut max_probes = 0;
|
||||
for n in 0..2048 {
|
||||
let mut probes = 0;
|
||||
assert_eq!(
|
||||
table.contains(Identity { address: n * 16, length: n % 7 }, || probes += 1),
|
||||
n < 1024
|
||||
);
|
||||
max_probes = max_probes.max(probes);
|
||||
}
|
||||
// A reproducible synthetic-key bound for this fixture, not a hash-table
|
||||
// worst-case claim or a constraint on allocator-selected real addresses.
|
||||
assert!(max_probes <= 32, "fixture probes={max_probes}");
|
||||
table.retain(|n| n % 2 == 0);
|
||||
table.prepare(&b).unwrap();
|
||||
for n in 0..1024 {
|
||||
assert_eq!(table.contains(Identity { address: n * 16, length: n % 7 }, || {}), n % 2 == 0);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
use crate::resource_budget::{
|
||||
BudgetVec, Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
|
||||
};
|
||||
use crate::string_identity_index::{Identity, IdentityIndex};
|
||||
use prometeu_bytecode::Value;
|
||||
use std::sync::{Arc, Mutex, MutexGuard, Weak};
|
||||
|
||||
@@ -17,7 +18,7 @@ struct Entry {
|
||||
#[derive(Debug)]
|
||||
struct Domain {
|
||||
budget: RuntimeBudget,
|
||||
entries: BudgetVec<Entry>,
|
||||
entries: IdentityIndex<Entry>,
|
||||
_state: Reservation,
|
||||
}
|
||||
|
||||
@@ -49,7 +50,7 @@ impl StringLedger {
|
||||
Q::AuxiliaryBytes,
|
||||
budget.bytes::<Mutex<Domain>>(1, Q::AuxiliaryBytes)? + 2 * size_of::<usize>() as u64,
|
||||
)?;
|
||||
let entries = BudgetVec::new(&budget, Q::AuxiliaryBytes);
|
||||
let entries = IdentityIndex::new(&budget);
|
||||
Ok(Self(Arc::new(Mutex::new(Domain { budget, entries, _state: state }))))
|
||||
}
|
||||
fn domain(&self) -> MutexGuard<'_, Domain> {
|
||||
@@ -57,7 +58,7 @@ impl StringLedger {
|
||||
}
|
||||
|
||||
/// Safepoint reconciliation is bounded by the accepted allocation count.
|
||||
/// Retain compacts in place; unused capacity remains charged until drop.
|
||||
/// Dead buckets become tombstones; retained capacity stays charged until drop.
|
||||
pub(crate) fn reconcile(&self) -> usize {
|
||||
self.domain().reconcile()
|
||||
}
|
||||
@@ -65,9 +66,19 @@ impl StringLedger {
|
||||
self.domain().entries.len()
|
||||
}
|
||||
pub(crate) fn retains(&self, content: &Arc<str>) -> bool {
|
||||
let address = Arc::as_ptr(content);
|
||||
self.domain().entries.iter().any(|entry| std::ptr::eq(entry.content.as_ptr(), address))
|
||||
self.lookup(content, || {})
|
||||
}
|
||||
fn lookup(&self, content: &Arc<str>, mut probe: impl FnMut()) -> bool {
|
||||
self.domain().entries.contains(Identity::of(content), &mut probe)
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "resource-diagnostics"))]
|
||||
pub(crate) fn lookup_work(&self, content: &Arc<str>) -> (bool, usize) {
|
||||
let mut work = 0;
|
||||
let found = self.lookup(content, || work += 1);
|
||||
(found, work)
|
||||
}
|
||||
|
||||
pub(crate) fn lease(&self, content: &Arc<str>) -> Option<StringLease> {
|
||||
self.retains(content)
|
||||
.then(|| StringLease { content: content.clone(), domain: self.clone() })
|
||||
@@ -239,23 +250,24 @@ impl Domain {
|
||||
}
|
||||
Err(failure) => return Err(failure),
|
||||
};
|
||||
if let Err(failure) = self.entries.reserve(1) {
|
||||
if let Err(failure) = self.entries.prepare(&self.budget) {
|
||||
if failure.reason != ResourceReason::Limit {
|
||||
return Err(failure);
|
||||
}
|
||||
self.reconcile();
|
||||
self.entries.reserve(1)?;
|
||||
self.entries.prepare(&self.budget)?;
|
||||
}
|
||||
Ok(charges)
|
||||
}
|
||||
fn publish(&mut self, content: &Arc<str>, charges: Charges) {
|
||||
self.entries
|
||||
.push(Entry {
|
||||
self.entries.insert(
|
||||
Identity::of(content),
|
||||
Entry {
|
||||
content: Arc::downgrade(content),
|
||||
_bytes: charges.bytes,
|
||||
_allocation: charges.allocation,
|
||||
})
|
||||
.expect("prepare reserved one ledger record before materialization");
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -259,3 +259,95 @@ fn formatting_refuses_result_and_temporary_limits_with_no_content_publication()
|
||||
assert_eq!(budget.used(Q::StringAllocations), 0);
|
||||
assert_eq!(ledger.tracked_allocations(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_growth_refuses_auxiliary_temporary_and_allocator_before_materialization() {
|
||||
for failure in [0, 1, 2] {
|
||||
let (budget, ledger) = fixture(&[]);
|
||||
let kept = (0..4).map(|_| ledger.import("kept").unwrap()).collect::<Vec<_>>();
|
||||
let auxiliary = budget.used(Q::AuxiliaryBytes);
|
||||
match failure {
|
||||
0 => budget.test_lower_limit(Q::AuxiliaryBytes, auxiliary),
|
||||
1 => budget.test_lower_limit(Q::TemporaryBytes, 0),
|
||||
_ => crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true)),
|
||||
}
|
||||
let mut called = false;
|
||||
let result = ledger.build(4, &mut |w| {
|
||||
called = true;
|
||||
w.write_str("next")
|
||||
});
|
||||
let Err(MaterializeError::Resource(error)) = result else {
|
||||
panic!("expected prepublication refusal")
|
||||
};
|
||||
assert!(!called);
|
||||
assert_eq!(
|
||||
error.resource,
|
||||
if failure == 1 { Q::TemporaryBytes } else { Q::AuxiliaryBytes }
|
||||
);
|
||||
assert_eq!(
|
||||
error.reason,
|
||||
if failure == 2 { ResourceReason::AllocationFailed } else { ResourceReason::Limit }
|
||||
);
|
||||
assert_eq!(ledger.tracked_allocations(), 4);
|
||||
assert_eq!(budget.used(Q::StringBytes), 16);
|
||||
assert_eq!(budget.used(Q::StringAllocations), 4);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
for value in &kept {
|
||||
assert!(ledger.retains(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_initial_allocation_failure_and_rebuild_failure_refund_reservations() {
|
||||
let (budget, ledger) = fixture(&[]);
|
||||
let initial = budget.used(Q::AuxiliaryBytes);
|
||||
crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true));
|
||||
assert_eq!(ledger.import("x").unwrap_err().reason, ResourceReason::AllocationFailed);
|
||||
assert_eq!(ledger.tracked_allocations(), 0);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), initial);
|
||||
assert_eq!(budget.used(Q::StringBytes), 0);
|
||||
assert_eq!(budget.used(Q::StringAllocations), 0);
|
||||
let mut kept = (0..4).map(|_| ledger.import("same").unwrap()).collect::<Vec<_>>();
|
||||
let old = Arc::downgrade(&kept.pop().unwrap());
|
||||
assert_eq!(ledger.reconcile(), 1);
|
||||
let auxiliary = budget.used(Q::AuxiliaryBytes);
|
||||
budget.test_lower_limit(Q::TemporaryBytes, 0);
|
||||
assert_eq!(ledger.import("new").unwrap_err().resource, Q::TemporaryBytes);
|
||||
assert_eq!(ledger.tracked_allocations(), 3);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert_eq!(budget.used(Q::StringBytes), 12);
|
||||
for value in &kept {
|
||||
assert_eq!(Arc::strong_count(value), 1);
|
||||
assert!(ledger.retains(value));
|
||||
}
|
||||
assert!(old.upgrade().is_none());
|
||||
// Rebuild under a restored test budget; no production ceiling changes.
|
||||
budget.test_lower_limit(Q::TemporaryBytes, 64 * 1024 * 1024);
|
||||
let newer = ledger.import("same").unwrap();
|
||||
assert!(ledger.retains(&newer));
|
||||
assert!(!ledger.retains(&Arc::from("same")));
|
||||
assert!(ledger.lookup_work(&newer).0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tombstone_rebuild_allocator_refusal_preserves_live_keys_and_charges() {
|
||||
let (budget, ledger) = fixture(&[]);
|
||||
let mut owners = (0..4).map(|_| ledger.import("same").unwrap()).collect::<Vec<_>>();
|
||||
owners.pop();
|
||||
assert_eq!(ledger.reconcile(), 1);
|
||||
let auxiliary = budget.used(Q::AuxiliaryBytes);
|
||||
crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true));
|
||||
assert_eq!(ledger.import("new").unwrap_err().reason, ResourceReason::AllocationFailed);
|
||||
assert_eq!(ledger.tracked_allocations(), 3);
|
||||
assert_eq!(budget.used(Q::StringBytes), 12);
|
||||
assert_eq!(budget.used(Q::StringAllocations), 3);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert!(owners.iter().all(|v| ledger.retains(v)));
|
||||
let new = ledger.import("new").unwrap();
|
||||
assert!(ledger.retains(&new));
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
|
||||
}
|
||||
|
||||
@@ -3890,3 +3890,6 @@ mod tests {
|
||||
#[cfg(test)]
|
||||
#[path = "virtual_machine/callable_tests.rs"]
|
||||
mod callable_tests;
|
||||
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub mod string_provenance_qualification;
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
//! Opt-in diagnostics for the actual native provenance boundaries.
|
||||
use super::*;
|
||||
use crate::ValidatedProgramImage;
|
||||
use std::{hint::black_box, sync::Arc};
|
||||
|
||||
pub struct Fixture {
|
||||
vm: VirtualMachine,
|
||||
owners: Vec<Arc<str>>,
|
||||
image_hit: Arc<str>,
|
||||
miss: Arc<str>,
|
||||
}
|
||||
impl Fixture {
|
||||
pub fn new(image: Arc<ValidatedProgramImage>, count: usize) -> Self {
|
||||
let Value::String(image_hit) = image.constants().last().unwrap() else { panic!() };
|
||||
let image_hit = image_hit.clone();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
let ledger = crate::string_ledger::StringLedger::new(vm.budget.clone()).unwrap();
|
||||
let owners = (0..count).map(|_| ledger.import("same").unwrap()).collect::<Vec<_>>();
|
||||
vm.strings = Some(ledger);
|
||||
vm.operand_stack.push(Value::String(image_hit.clone())).unwrap();
|
||||
vm.operand_stack.push(Value::String(owners.last().unwrap().clone())).unwrap();
|
||||
Self { vm, owners, image_hit, miss: Arc::from("same") }
|
||||
}
|
||||
pub fn auxiliary_bytes(&self) -> u64 {
|
||||
self.vm.budget.used(RuntimeResource::AuxiliaryBytes)
|
||||
}
|
||||
pub fn work(&self) -> [usize; 4] {
|
||||
let image = self.vm.validated_image.as_ref().unwrap();
|
||||
let session = self.vm.strings.as_ref().unwrap();
|
||||
let (ih, iw) = image.lookup_work(&self.image_hit);
|
||||
let (im, mw) = image.lookup_work(&self.miss);
|
||||
let (sh, sw) = session.lookup_work(self.owners.last().unwrap());
|
||||
let (sm, tw) = session.lookup_work(&self.miss);
|
||||
assert!(ih && sh && !im && !sm);
|
||||
[iw, mw, sw, tw]
|
||||
}
|
||||
pub fn run(&mut self, workload: &str) {
|
||||
match workload {
|
||||
"image_hit" => {
|
||||
black_box(
|
||||
self.vm
|
||||
.validated_image
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.retains_string(black_box(&self.image_hit)),
|
||||
);
|
||||
}
|
||||
"image_miss" => {
|
||||
black_box(
|
||||
self.vm.validated_image.as_ref().unwrap().retains_string(black_box(&self.miss)),
|
||||
);
|
||||
}
|
||||
"session_hit" => {
|
||||
black_box(
|
||||
self.vm
|
||||
.strings
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.retains(black_box(self.owners.last().unwrap())),
|
||||
);
|
||||
}
|
||||
"session_miss" => {
|
||||
black_box(self.vm.strings.as_ref().unwrap().retains(black_box(&self.miss)));
|
||||
}
|
||||
"import_hit" => {
|
||||
black_box(
|
||||
self.vm
|
||||
.import_string_value(&Value::String(self.owners.last().unwrap().clone()))
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
"import_miss" => {
|
||||
black_box(self.vm.import_string_value(&Value::String(self.miss.clone())).unwrap());
|
||||
}
|
||||
"snapshot_mixed" => {
|
||||
black_box(self.vm.operand_stack_top(2));
|
||||
}
|
||||
_ => panic!("unknown workload"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,3 +97,40 @@ fn warmed_instruction_regions_do_not_allocate() {
|
||||
assert_eq!(counts, (0, 0), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
#[allow(dead_code)]
|
||||
#[path = "support/callables.rs"]
|
||||
mod provenance_callables;
|
||||
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
#[test]
|
||||
fn provenance_queries_allocate_nothing_at_all_qualification_sizes() {
|
||||
use prometeu_bytecode::model::ConstantPoolEntry;
|
||||
use prometeu_vm::string_provenance_qualification::Fixture;
|
||||
for count in [1024_usize, 16384, 65536] {
|
||||
let constants = vec![ConstantPoolEntry::String("same".into()); count];
|
||||
let image = provenance_callables::image_constants(
|
||||
&[("RET", provenance_callables::sig(&[], &[]), vec![])],
|
||||
vec![],
|
||||
&constants,
|
||||
)
|
||||
.unwrap();
|
||||
let mut fixture = Fixture::new(image, count);
|
||||
let work = fixture.work();
|
||||
let bound = count.ilog2() as usize + 1;
|
||||
assert!(work[0] <= bound && work[1] <= bound);
|
||||
for workload in ["image_hit", "image_miss", "session_hit", "session_miss", "import_hit"] {
|
||||
COUNTS.with(|c| c.set(Some((0, 0))));
|
||||
for _ in 0..100 {
|
||||
fixture.run(workload);
|
||||
}
|
||||
let counts = COUNTS.with(|c| c.replace(None)).unwrap();
|
||||
assert_eq!(counts, (0, 0), "{workload}, owners={count}");
|
||||
}
|
||||
COUNTS.with(|c| c.set(Some((0, 0))));
|
||||
fixture.run("snapshot_mixed");
|
||||
let counts = COUNTS.with(|c| c.replace(None)).unwrap();
|
||||
assert_eq!(counts, (1, 1), "only snapshot Value storage allocates");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user