fix(vm): enforce return boundaries and amortize budgeted growth
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Validate and complete function-end branches through the shared return path. Handle integer DIV/MOD overflow with wrapping and charge CALL/RET refusals exactly once. Amortize budgeted vector growth with exact-capacity fallback at quota limits. Add execution and accounting regressions, clarify ISA contracts, and include existing lint cleanups.
This commit is contained in:
@@ -253,6 +253,47 @@ mod tests {
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 8);
|
||||
assert_eq!(queue.pop(), Some(11));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incremental_growth_moves_a_linear_number_of_elements() {
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut values = BudgetVec::<u64>::new(&budget, AuxiliaryBytes);
|
||||
let mut moved = 0;
|
||||
let count = 16_000;
|
||||
for value in 0..count {
|
||||
let capacity = values.capacity();
|
||||
values.push(value as u64).unwrap();
|
||||
if values.capacity() != capacity {
|
||||
moved += value;
|
||||
}
|
||||
}
|
||||
assert!(moved < 2 * count, "moved {moved} elements for {count} pushes");
|
||||
assert_eq!(values.as_slice(), (0..count as u64).collect::<Vec<_>>());
|
||||
assert_eq!(budget.used(AuxiliaryBytes), (values.capacity() * size_of::<u64>()) as u64);
|
||||
assert_eq!(budget.used(TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn growth_falls_back_to_exact_capacity_at_shared_and_coexistence_limits() {
|
||||
for resource in [AuxiliaryBytes, TemporaryBytes] {
|
||||
let limit = if resource == TemporaryBytes { 72 } else { 40 };
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(resource, limit).unwrap());
|
||||
let mut values = BudgetVec::<u64>::new(&budget, resource);
|
||||
for value in 0..5 {
|
||||
values.push(value).unwrap();
|
||||
}
|
||||
assert_eq!(values.as_slice(), &[0, 1, 2, 3, 4]);
|
||||
assert_eq!(values.capacity(), 5);
|
||||
assert_eq!(budget.used(resource), 40);
|
||||
assert!(values.push(5).is_err());
|
||||
assert_eq!(values.as_slice(), &[0, 1, 2, 3, 4]);
|
||||
assert_eq!(budget.used(resource), 40);
|
||||
drop(values);
|
||||
assert_eq!(budget.used(resource), 0);
|
||||
assert_eq!(budget.used(TemporaryBytes), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Contiguous execution storage. The capacity reservation travels with the buffer
|
||||
@@ -280,20 +321,34 @@ impl<T> BudgetVec<T> {
|
||||
if required <= self.data.capacity() {
|
||||
return Ok(None);
|
||||
}
|
||||
let bytes = b.bytes::<T>(required, q)?;
|
||||
// Amortize incremental growth while keeping exact admission available
|
||||
// when spare capacity would exceed a shared or temporary quota.
|
||||
let preferred = required.max(self.data.capacity().saturating_mul(2));
|
||||
if preferred > required
|
||||
&& let Ok(growth) = self.prepare_capacity(preferred)
|
||||
{
|
||||
return Ok(Some(growth));
|
||||
}
|
||||
self.prepare_capacity(required).map(Some)
|
||||
}
|
||||
|
||||
fn prepare_capacity(&self, capacity: usize) -> Result<VecGrowth<T>, ResourceFailure> {
|
||||
let b = &self.charge.budget;
|
||||
let q = self.charge.resource;
|
||||
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)?;
|
||||
let mut candidate = Vec::new();
|
||||
candidate
|
||||
.try_reserve_exact(required)
|
||||
.try_reserve_exact(capacity)
|
||||
.map_err(|_| b.failure(q, ResourceReason::AllocationFailed, bytes))?;
|
||||
extra.resize(b.bytes::<T>(candidate.capacity(), q)? - self.charge.amount)?;
|
||||
Ok(Some(VecGrowth {
|
||||
Ok(VecGrowth {
|
||||
candidate,
|
||||
extra,
|
||||
_temporary: temporary,
|
||||
old_capacity: self.data.capacity(),
|
||||
}))
|
||||
})
|
||||
}
|
||||
pub(crate) fn commit_growth(&mut self, growth: Option<VecGrowth<T>>) {
|
||||
if let Some(mut growth) = growth {
|
||||
|
||||
Reference in New Issue
Block a user