tests/alloc_budget_peak.rs
//! **A 36-byte frame cannot make OpenZL allocate a gigabyte under a budget.**
//! Its own test binary, with one test, so the process peak (VmHWM) is its own.
//! Without the budget the same frame peaks at about 1 GiB (measured on oden,
//! lane gatwasm, 2026-10-31).
use openzl_sys_rs::*;
fn peak_rss_mib() -> u64 {
let s = std::fs::read_to_string("/proc/self/status").unwrap_or_default();
s.lines()
.find(|l| l.starts_with("VmHWM:"))
.and_then(|l| l.split_whitespace().nth(1))
.and_then(|v| v.parse::<u64>().ok())
.map(|kib| kib / 1024)
.unwrap_or(0)
}
#[test]
fn a_gigabyte_constant_stream_is_refused_without_growing_memory() {
let mut frame = include_bytes!("fixtures/regress-constant-stream-alloc.zl").to_vec();
let mut v: u64 = 1 << 30;
for i in 0..10 {
frame[0x11 + i] = (v & 0x7f) as u8 | if i < 9 { 0x80 } else { 0 };
v >>= 7;
}
let before = peak_rss_mib();
let mut out = [0u8; 16];
let (got, spent) = with_alloc_budget(16 << 20, || zl_decompress(&mut out, &frame));
let grew = peak_rss_mib().saturating_sub(before);
assert!(got.is_err());
assert!(spent.exceeded, "{spent:?}");
assert!(grew < 32, "a 36-byte frame grew the process peak by {grew} MiB under a 16 MiB budget");
}