Repository navigation
Description
Found by a memory-accounting audit of 8b3c813. The tests are on branch audit/memory-tests and fail on main today; check out that branch to run them. File and line links point at that commit.
What goes wrong.
try_update_memory_usagesubtracts with a barefetch_sub(src/core/src/cache/budget.rs:57-71), and so doesrelease_disk(budget.rs:99-101). Neither checks for underflow.- A release larger than the charge wraps the counter to near
usize::MAX. The tier then refuses every request smaller than the deficit. try_reserve_*also addsused + requestunchecked. In debug that panics; in release it wraps silently (inferred, not run).- This amplifies cache: try_insert settles memory from a stale index read #67 and cache: reset() leaves objects, policy entries and in-flight charges #73.
Evidence.
release_disk(1)on an empty budget left disk usage at 18,446,744,073,709,551,615, and the nexttry_reserve_disk(1)panicked with "attempt to add with overflow" (budget.rs:83).- The memory path behaves the same (
budget.rs:40).
Tests.
| Test | File | Command | Flags |
|---|---|---|---|
audit_release_disk_beyond_usage_does_not_wrap |
src/core/src/cache/budget.rs:240 |
cargo +1.96.0 test -p liquid-cache --lib audit_release_disk_beyond_usage_does_not_wrap -- --nocapture |
|
audit_memory_shrink_beyond_usage_does_not_wrap |
src/core/src/cache/budget.rs:264 |
cargo +1.96.0 test -p liquid-cache --lib audit_memory_shrink_beyond_usage_does_not_wrap -- --nocapture |
Inline |
Inline test (append inside mod tests in budget.rs; it uses the existing test_budget):
fn describe_next_reservation(reserve: impl FnOnce() -> Result<(), ()>) -> String {
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(reserve)) {
Ok(result) => format!("returned {result:?}"),
Err(panic) => format!(
"panicked: {}",
panic
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| panic.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "<non-string panic>".to_string())
),
}
}
#[test]
fn audit_memory_shrink_beyond_usage_does_not_wrap() {
let max_memory = 1000;
let budget = test_budget(max_memory, usize::MAX);
budget
.try_update_memory_usage(1, 0)
.expect("a release cannot fail");
let usage = budget.memory_usage_bytes();
let next = describe_next_reservation(|| budget.try_reserve_memory(1));
assert!(
usage <= max_memory,
"try_update_memory_usage(1, 0) on an empty budget left memory usage at \
{usage}, over the limit of {max_memory}; the next try_reserve_memory(1) {next}"
);
assert_eq!(usage, 0);
}Heads-up: the existing test a_spill_installs_its_record_when_the_memory_budget_reads_full (core.rs:2215) relies on this wrap. It calls try_update_memory_usage(1, 0) to push usage over the limit, so its setup needs changing once this is fixed. Related: #30.
Flags: P probabilistic (a pass after a fix only means the race wasn't hit), F interleaving forced by a test hook, L Linux only, D needs Docker, M measurement only, C control (passes by design).
Activity
Merge origin/main into fix/eviction-accounting