From d77ec3f3d9235261a16f90820a63f9a4c8630902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:34:36 +0300 Subject: [PATCH] U8: ec-opus delete stack_budget test module (size probe, not shipped behaviour) --- crates/ec-opus/src/encoder.rs | 2 +- crates/ec-opus/src/lib.rs | 2 - crates/ec-opus/src/stack_budget.rs | 864 ----------------------------- 3 files changed, 1 insertion(+), 867 deletions(-) delete mode 100755 crates/ec-opus/src/stack_budget.rs diff --git a/crates/ec-opus/src/encoder.rs b/crates/ec-opus/src/encoder.rs index 68712986..193b4877 100644 --- a/crates/ec-opus/src/encoder.rs +++ b/crates/ec-opus/src/encoder.rs @@ -285,7 +285,7 @@ pub struct Encoder { /// [`None`] picks SILK, CELT or (once D4 lands) Hybrid from the /// application and bitrate; `Some` forces one. mode: Option, - /// The SILK layer, created on first use (boxed: `crate::stack_budget`). + /// The SILK layer, created on first use. silk: Option>, /// Scratch space for the SILK packet payload, sized once so the steady-state /// loop (`steady_state_encode_loop_zero_alloc`) doesn't allocate. diff --git a/crates/ec-opus/src/lib.rs b/crates/ec-opus/src/lib.rs index 2289aaf4..4398d82a 100644 --- a/crates/ec-opus/src/lib.rs +++ b/crates/ec-opus/src/lib.rs @@ -76,7 +76,6 @@ pub mod range; pub mod silk; pub(crate) mod silk_fix; pub(crate) mod silk_flp; -mod stack_budget; pub use celt::CeltDecoder; pub use encoder::{Application, Encoder}; @@ -103,7 +102,6 @@ pub struct Decoder { /// Boxed because a `SilkDecoder` is 6512 bytes, 83% of what a `Decoder` /// costs a caller taking one BY VALUE from `Decoder::new`. It is the /// decoder's largest field and its only large one; `celt` is 1448. - /// `Stack`: crate::stack_budget. silk: Box, /// The CELT overlap window, reused for redundancy cross-fades. celt_window: Vec, diff --git a/crates/ec-opus/src/stack_budget.rs b/crates/ec-opus/src/stack_budget.rs deleted file mode 100755 index a0c275e3..00000000 --- a/crates/ec-opus/src/stack_budget.rs +++ /dev/null @@ -1,864 +0,0 @@ -#![cfg(test)] -//! A measured bound on the crate's BY-VALUE types, and a gate that bites. -//! -//! # The class -//! -//! A large struct returned BY VALUE from a public constructor is a stack cost, -//! not a heap cost, and the cost is paid by the CALLER. `Encoder` is the -//! workspace's largest live instance of this class: it was **87,024 bytes** by -//! value -- ten and a half times the budget below -- because six -//! SILK encoder slots (79,176 bytes together) and a 3,831-byte SILK packet scratch buffer -//! (`MAX_SILK_PACKET_BYTES = 1 + 1 + 2 * 2 + 3 * 1275`) sat INLINE on a type -//! `Encoder::new` hands back by value. `Decoder` was 7,576, 92.5% of the -//! budget, with a 6,304-byte `SilkDecoder` inline. -//! -//! Both are boxed now (82,952 bytes moved off the stack onto the heap: the -//! `Box`ed SILK layer, `silk_buf: Box<[u8; MAX_SILK_PACKET_BYTES]>`, and -//! `Decoder::silk: Box`), and this module is what stops the -//! numbers from walking back. -//! -//! # Why this module exists -//! -//! 1. [`BY_VALUE_TYPES`] is the inventory -- every type in this crate a public -//! `fn` can return or take by value, with a measured `size_of` recorded as -//! of this commit. [`the_measured_inventory_is_accurate`] fails if a size -//! moves, so the table cannot rot into a list of stale small numbers. -//! 2. [`STACK_BUDGET`] is the bound, and -//! [`every_by_value_type_fits_the_stack_budget`] asserts it BY NAME for -//! every budgeted row, so a regression reds naming the type that caused it. -//! 3. [`the_crate_private_silk_encoders_stay_behind_a_box`] pins the three -//! `Box` spellings that moved the 87 KB off the stack, with a negative -//! control so the scan cannot be matching a doc comment. -//! 4. [`the_deepest_by_value_constructors_fit_on_a_tight_stack`] is the -//! capability arm: it really constructs `Encoder::new` and `Decoder::new` on -//! a deliberately tight thread, so the old layout ABORTS THE PROCESS rather -//! than quietly costing margin. Red/green in milliseconds. -//! 5. [`every_by_value_public_return_is_in_the_inventory`] is the sweep's -//! teeth against NEW types: a source scan re-derives the set of by-value -//! public returns from the crate source, so a lane that adds a 40 KB struct -//! to a `pub fn` return reds here until it is measured and listed. -//! [`the_inventory_scan_is_not_vacuous`] is its control, and -//! [`every_by_value_module_is_scanned`] keeps the scan's file list from -//! falling behind a new `mod` in `lib.rs`. -//! -//! # The bound, and why this number -//! -//! [`STACK_BUDGET`] = 8192 bytes (8 KiB), the same number ec-av1's identical -//! sweep uses, and it is bounded from BOTH sides by measurement: -//! -//! * **The smallest stack a caller can hand this crate is 2 MiB** -- libtest's -//! default test thread, and what `std::thread::Builder::new()` gives a -//! caller who does not ask for a size. This workspace's -//! `.cargo/config.toml` carries `RUST_MIN_STACK = 67108864`, which hides -//! the problem from its own runs; a library consumer gets no -//! `.cargo/config.toml` and no such cover. -//! * **The deepest PUBLIC by-value chain is 2** -//! ([`DEEPEST_PUBLIC_CHAIN`]): `MultistreamDecoder::with_rate` -> -//! `MultistreamDecoder::try_with_rate` (`multistream.rs:47` -> `:63`), both -//! handing back a `MultistreamDecoder` by value, so both frames hold a -//! return slot at once. `Encoder::new` (`encoder.rs:166`) and `Decoder::new` -//! (`lib.rs:136`) are ONE level each -- neither delegates to an inner -//! constructor of its own type, they build the struct literal inline -- so -//! the worst live set of by-value return slots is two frames, and the test -//! asserts `DEEPEST_PUBLIC_CHAIN * worst * 8 <= 2 MiB`. -//! * **The budget applies to types a public `fn` returns by value.** The -//! `Box`ed payloads are listed with `public: false` and measured, but not -//! budgeted, because the budget's job is to bound what a CALLER can be made -//! to own and none of them is reachable that way any more -- every field -//! that holds one is a `Box`, which -//! [`the_crate_private_silk_encoders_stay_behind_a_box`] pins by source. -//! -//! **Where the number sits.** At 8 KiB with the worst budgeted return at 2,936 -//! bytes ([`Encoder`]), the worst a caller owes is `2 x 2936 = 5,872` bytes -- -//! **0.28%** of a 2 MiB thread -- and the budget sits 2.79x above the -//! incumbent. The pre-fix `Encoder` (87,024 B) is **10.62x over** and the -//! pre-fix `Decoder` (7,576 B) sat at 92.5% of it, so the historical defect -//! and the near-miss both red by a factor rather than by a hair. -//! -//! **What it does not claim.** `size_of` is layout, not stack depth: the -//! measured stack/size ratio on a by-value constructor here is several times -//! over, because the constructor's own frames add on top of the return slot. -//! That residual is why -//! [`the_deepest_by_value_constructors_fit_on_a_tight_stack`] constructs for -//! real instead of trusting the arithmetic. - -use crate::Decoder; -use crate::encoder::Encoder; - -/// The per-type ceiling for a type a CALLER can be made to hold by value, in -/// bytes. See the module docs for the derivation; it is two-sided, and both -/// sides are measured. -pub(crate) const STACK_BUDGET: usize = 8192; - -/// The deepest PUBLIC by-value constructor chain in this crate, counted from -/// the source: `MultistreamDecoder::with_rate` -> `MultistreamDecoder:: -/// try_with_rate` is two, both returning the same type by value so both frames -/// hold a `MultistreamDecoder` return slot at once. `Encoder::new` and -/// `Decoder::new` are one level each -- neither delegates to an inner -/// constructor of its own type. The budget is checked against two, the worse -/// of the chains. -const DEEPEST_PUBLIC_CHAIN: usize = 2; - -/// Every type in this crate a public `fn` can hand back BY VALUE (or take by -/// value in a public signature), with the `size_of` measured at the commit -/// that wrote this table. `None` means "not yet measured", which -/// [`the_measured_inventory_is_accurate`] rejects. -/// -/// The third column names the return path, so a reader can see why a type is -/// here rather than a coincidence of the type system. `false` in that column -/// means the type is NOT budgeted: it is a `Box`ed payload, reachable only -/// through a pointer now, and the pin that keeps it that way is -/// [`the_crate_private_silk_encoders_stay_behind_a_box`]. -pub(crate) const BY_VALUE_TYPES: &[(&str, Option, bool, &str)] = &[ - // -- lib.rs ----------------------------------------------------------- - ( - "Decoder", - Some(1568), - true, - "Decoder::new -> Result. Was 7576, 92.5% of the budget, \ - because a 6304-byte SilkDecoder sat inline; that field is now a Box", - ), - // -- encoder.rs ------------------------------------------------------- - ( - "Encoder", - Some(2936), - true, - "Encoder::new -> Result. Was 87024, 10.62x the budget: six \ - inline SILK encoder slots (79176 together) and an inline 3831-byte \ - silk_buf", - ), - ( - "Application", - Some(1), - true, - "Encoder::new(.., application: Application) takes it by value and \ - stores it inline in every Encoder", - ), - // -- celt.rs ---------------------------------------------------------- - ( - "CeltDecoder", - Some(1440), - true, - "celt::CeltDecoder::new(channels, downsample) -> Self; also moved by \ - value into Decoder::celt", - ), - // -- multistream.rs / multistream_enc.rs ------------------------------ - ( - "MultistreamDecoder", - Some(120), - true, - "MultistreamDecoder::with_rate / try_with_rate, the crate's deepest \ - public by-value chain: with_rate -> try_with_rate -> Result<..>", - ), - ( - "MultistreamEncoder", - Some(88), - true, - "MultistreamEncoder::surround_5_1 -> Result", - ), - // -- packet.rs -------------------------------------------------------- - ( - "Packet", - Some(40), - true, - "Packet::parse -> Result>", - ), - ("Toc", Some(3), true, "Toc::new(byte: u8) -> Toc"), - ( - "Mode", - Some(1), - true, - "Toc::mode(self) -> Mode; also stored inline in Decoder::prev_mode", - ), - ( - "Bandwidth", - Some(1), - true, - "Toc::bandwidth(self) -> Bandwidth; also stored inline in \ - Encoder::bandwidth", - ), - // -- range.rs --------------------------------------------------------- - ( - "RangeEncoder", - Some(80), - true, - "RangeEncoder::new() -> Self; stored inline in Encoder::range", - ), - ( - "RangeDecoder", - Some(56), - true, - "RangeDecoder::new(buf) -> RangeDecoder<'a>", - ), - ( - "EncSnapshot", - Some(48), - true, - "RangeEncoder::snapshot(&self) -> EncSnapshot", - ), - // -- silk.rs ---------------------------------------------------------- - ( - "SilkDecoder", - Some(6512), - true, - "silk::SilkDecoder::new -> Self, and it is `pub use`d at the crate \ - root, so 6512 bytes is a caller's by-value cost TODAY even though \ - Decoder::new moves it straight into a Box. At 77% of the budget it \ - is the LARGEST budgeted row in the table, and the one that says how \ - little headroom the budget would have left if it had stayed inline", - ), - ( - "SilkDecIndices", - Some(104), - true, - "SilkDecoder::last_indices(&self) -> SilkDecIndices", - ), - // -- analysis.rs (crate-private) -------------------------------------- - ( - "TonalityAnalysis", - Some(2096), - false, - "analysis::TonalityAnalysis::new(sample_rate) -> Self, stored \ - INLINE in Encoder::analysis. 2096 is inside the budget and the field \ - is reached only through an Encoder that is itself budgeted, so it is \ - measured here rather than pinned behind a Box", - ), - ( - "AnalysisInfo", - Some(60), - false, - "analysis::AnalysisInfo::default() -> Self, stored inline in \ - Encoder::info", - ), -]; - -/// The live `size_of` for every inventoried type, keyed BY NAME. -/// -/// Keyed rather than positional on purpose: `Mode`, `Bandwidth` and -/// `Application` are all 1 byte, and a row inserted into [`BY_VALUE_TYPES`] -/// without a matching insert here would zip cleanly and assert the wrong -/// type's size against the wrong recorded one. A name key makes that mistake -/// loud instead. -fn measured_by_name() -> Vec<(&'static str, usize)> { - vec![ - ("Decoder", std::mem::size_of::()), - ("Encoder", std::mem::size_of::()), - ( - "Application", - std::mem::size_of::(), - ), - ( - "CeltDecoder", - std::mem::size_of::(), - ), - ( - "MultistreamDecoder", - std::mem::size_of::(), - ), - ( - "MultistreamEncoder", - std::mem::size_of::(), - ), - ( - "Packet", - std::mem::size_of::>(), - ), - ("Toc", std::mem::size_of::()), - ("Mode", std::mem::size_of::()), - ("Bandwidth", std::mem::size_of::()), - ( - "RangeEncoder", - std::mem::size_of::(), - ), - ( - "RangeDecoder", - std::mem::size_of::>(), - ), - ( - "EncSnapshot", - std::mem::size_of::(), - ), - ( - "SilkDecoder", - std::mem::size_of::(), - ), - ( - "SilkDecIndices", - std::mem::size_of::(), - ), - ( - "TonalityAnalysis", - std::mem::size_of::(), - ), - ( - "AnalysisInfo", - std::mem::size_of::(), - ), - ] -} - -/// The stack [`the_deepest_by_value_constructors_fit_on_a_tight_stack`] -/// constructs on. -/// -/// 256 KiB: an eighth of what an unconfigured caller gives this crate, and -/// **5.3x** the deepest need measured in a debug build on the current layout -/// (the constructors complete at 48 KiB and abort at 32 KiB) while sitting -/// **2.1x below** the pre-fix layout's own threshold, which completes at -/// 544 KiB and aborts at 528 KiB. That gap is what makes this a capability -/// arm rather than a formality: at 256 KiB the layout this crate used to -/// ship ABORTS THE PROCESS. The sweep is reproducible with -/// `EC_OPUS_TIGHT_STACK_BYTES`. -const TIGHT_STACK: usize = 256 * 1024; - -#[cfg(test)] -mod tests { - use super::{ - BY_VALUE_TYPES, DEEPEST_PUBLIC_CHAIN, STACK_BUDGET, TIGHT_STACK, measured_by_name, - }; - use crate::Decoder; - use crate::encoder::Encoder; - - /// Return types that are not a by-value copy of anything the caller has to - /// own: a pointer, a slice, a `Vec` (three words, and the heap is not the - /// stack), or a primitive. `Self` is a by-value copy of the impl's type, - /// which the inventory rows already cover, so it is exempt from the scan - /// and not from the table. - const THIN: &[&str] = &[ - "Box", "Rc", "Arc", "Cow", "Vec", "VecDeque", "String", "OsString", "PathBuf", "HashMap", - "BTreeMap", "BTreeSet", "HashSet", "str", "u8", "u16", "u32", "u64", "usize", "i8", "i16", - "i32", "i64", "isize", "f32", "f64", "bool", "char", "Self", - ]; - - /// The type name a `pub fn` line returns BY VALUE, or `None` when the line - /// returns a pointer, a `Vec`, a primitive, or nothing. - /// - /// One function, shared by the sweep and its control, so the control - /// cannot pass while the sweep's copy of the logic rotted. - fn by_value_return_name(line: &str) -> Option { - let trimmed = line.trim_start(); - if !trimmed.starts_with("pub fn ") { - return None; - } - let arrow = line.find("->")?; - let rest = &line[arrow + 2..]; - let end = rest.find('{').unwrap_or(rest.len()); - let ret = rest[..end].trim(); - if ret.is_empty() { - return None; - } - // Unwrap one `Result<..>` / `Option<..>` layer. A deeper nesting is - // not a shape this crate uses, and flagging one as uninventoried is - // the safe direction to be wrong in. - let inner = { - let t = ret - .strip_prefix("Result<") - .or_else(|| ret.strip_prefix("Option<")) - .unwrap_or(ret); - t.split('>').next().unwrap_or(t) - }; - // Drop the error type and any lifetime or generic argument: - // `Result`, `RangeDecoder<'a>` and `Packet<'a>` are - // all the bare name for this purpose. Splitting on the comma is - // load-bearing: a matcher that cut only at `>` would read - // `Result` as the name "Decoder, Error". - // `the_inventory_scan_is_not_vacuous` covers exactly that, which is - // why the control is not optional. - let name = inner.split(['<', ',']).next().unwrap_or(inner).trim(); - if name.is_empty() || THIN.contains(&name) { - return None; - } - // A generic parameter, not a concrete type. - if !name.chars().next().is_some_and(char::is_uppercase) { - return None; - } - Some(name.to_string()) - } - - /// The source files the by-value scan reads. Every module `lib.rs` - /// declares, plus `lib.rs` itself (which is where `Decoder` and - /// `Encoder::new`'s crate-root sibling live) and this module. - const FILES: &[(&str, &str)] = &[ - ("analysis.rs", include_str!("analysis.rs")), - ("celt.rs", include_str!("celt.rs")), - ("celt_flp/bands.rs", include_str!("celt_flp/bands.rs")), - ("celt_flp/enc.rs", include_str!("celt_flp/enc.rs")), - ("celt_flp/enc_an.rs", include_str!("celt_flp/enc_an.rs")), - ("celt_flp/fft.rs", include_str!("celt_flp/fft.rs")), - ("celt_flp/mod.rs", include_str!("celt_flp/mod.rs")), - ("celt_flp/pitch.rs", include_str!("celt_flp/pitch.rs")), - ( - "celt_flp/quant_bands.rs", - include_str!("celt_flp/quant_bands.rs"), - ), - ("celt_flp/tables.rs", include_str!("celt_flp/tables.rs")), - ("celt_flp/vq.rs", include_str!("celt_flp/vq.rs")), - ("encoder.rs", include_str!("encoder.rs")), - ("lib.rs", include_str!("lib.rs")), - ("multistream.rs", include_str!("multistream.rs")), - ("multistream_enc.rs", include_str!("multistream_enc.rs")), - ("ogg.rs", include_str!("ogg.rs")), - ("packet.rs", include_str!("packet.rs")), - ("range.rs", include_str!("range.rs")), - ("silk.rs", include_str!("silk.rs")), - ("silk_fix.rs", include_str!("silk_fix.rs")), - ("silk_flp/api.rs", include_str!("silk_flp/api.rs")), - ("silk_flp/encode.rs", include_str!("silk_flp/encode.rs")), - ("silk_flp/mod.rs", include_str!("silk_flp/mod.rs")), - ("silk_flp/nlsf.rs", include_str!("silk_flp/nlsf.rs")), - ("silk_flp/nsq.rs", include_str!("silk_flp/nsq.rs")), - ("silk_flp/opus.rs", include_str!("silk_flp/opus.rs")), - ("silk_flp/pitch.rs", include_str!("silk_flp/pitch.rs")), - ("silk_flp/pred.rs", include_str!("silk_flp/pred.rs")), - ( - "silk_flp/resampler.rs", - include_str!("silk_flp/resampler.rs"), - ), - ("silk_flp/shape.rs", include_str!("silk_flp/shape.rs")), - ("silk_flp/sigproc.rs", include_str!("silk_flp/sigproc.rs")), - ("silk_flp/stereo.rs", include_str!("silk_flp/stereo.rs")), - ("silk_flp/vad.rs", include_str!("silk_flp/vad.rs")), - ("stack_budget.rs", include_str!("stack_budget.rs")), - ]; - - /// Every module `lib.rs` declares, so the scan's file list cannot silently - /// fall behind a new module. A new `mod foo;` with a by-value `pub fn` - /// that the scan never reads is exactly the hole this class would come - /// back through, so the list is derived, not curated. - /// - /// `pub(crate) mod foo;` is a module too, and ec-opus has several of them - /// (`analysis`, `silk_fix`, `silk_flp`), so the derivation strips the - /// visibility parenthesised form as well as the bare `pub`. - #[test] - fn every_by_value_module_is_scanned() { - let lib = include_str!("lib.rs"); - let mut declared: Vec<&str> = Vec::new(); - for line in lib.lines() { - let t = line.trim(); - let after_pub = if let Some(rest) = t.strip_prefix("pub(") { - // `pub(crate) mod foo;` / `pub(super) mod foo;` - rest.split_once(')') - .map(|(_, after)| after.trim_start()) - .unwrap_or("") - } else { - t.strip_prefix("pub ").unwrap_or(t) - }; - let Some(rest) = after_pub.strip_prefix("mod ") else { - continue; - }; - let name = rest.split([';', ' ']).next().unwrap_or(""); - if !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') { - declared.push(name); - } - } - assert!( - declared.len() > 8, - "only {} module declarations parsed out of lib.rs, so the \ - derived file list is broken rather than small", - declared.len() - ); - let scanned: Vec<&str> = FILES.iter().map(|(n, _)| *n).collect(); - let mut missing: Vec = declared - .iter() - .filter(|d| { - !scanned - .iter() - .any(|s| *s == format!("{d}.rs") || s.starts_with(&format!("{d}/"))) - }) - .map(|d| (*d).to_string()) - .collect(); - missing.sort(); - assert!( - missing.is_empty(), - "lib.rs declares modules the by-value scan never reads: {:?}. Add \ - them to FILES, or the sweep cannot see a new by-value return that \ - lands in one of them.", - missing - ); - } - - /// The inventory, printed as a sorted table, and checked against the - /// types' real sizes. - /// - /// The print is the instrument: one command reproduces the whole table, - /// biggest first, so "what is the largest by-value type in this crate" - /// has an answer that is a measurement rather than a grep. - /// - /// `cargo test -p ec-opus --lib -- stack_budget -- --nocapture` is the - /// command, with one wrinkle in THIS repo: `.cargo/config.toml` wraps - /// every test binary in `scripts/memguard-runner.sh`, which buffers a - /// green run's stdout and prints nothing. The table appears on a failing - /// run, or by invoking the built binary directly - /// (`target/debug/deps/ec_opus-* stack_budget --nocapture`). - /// - /// The check reds on ANY change with both numbers. That is deliberate: a - /// by-value type that grew raises the question of whether it should have - /// been boxed instead, and auto-accepting the new number would answer - /// that question by default. - #[test] - fn the_measured_inventory_is_accurate() { - let live = measured_by_name(); - assert_eq!( - BY_VALUE_TYPES.len(), - live.len(), - "the inventory names {} types but {} are measured; every row needs \ - a measured size or the sweep is a list of hopes", - BY_VALUE_TYPES.len(), - live.len() - ); - let mut rows: Vec<(&str, usize)> = live.clone(); - rows.sort_by_key(|&(_, size)| std::cmp::Reverse(size)); - eprintln!( - "\nBY-VALUE INVENTORY ({} types, budget {STACK_BUDGET} B = {} KiB)", - rows.len(), - STACK_BUDGET / 1024 - ); - eprintln!("{:>8} {:>7} TYPE", "BYTES", "%BUDGET"); - for (name, size) in &rows { - eprintln!( - "{size:>8} {:>6.1}% {name}", - *size as f64 * 100.0 / STACK_BUDGET as f64 - ); - } - for (name, recorded, _public, _path) in BY_VALUE_TYPES { - let recorded = recorded.expect("every inventory row carries a measured size"); - let actual = live - .iter() - .find(|(n, _)| n == name) - .map(|(_, size)| *size) - .unwrap_or_else(|| panic!("{name} is inventoried but never measured")); - assert_eq!( - recorded, actual, - "{name} is {actual} bytes, the inventory records {recorded}; \ - update the table deliberately -- a by-value type that changed \ - is a stack cost every caller of its constructor now pays" - ); - } - } - - /// The bound, asserted BY NAME. A regression in any inventoried type reds - /// here naming the type, not "some type is too big". - #[test] - fn every_by_value_type_fits_the_stack_budget() { - let live = measured_by_name(); - let mut checked = 0usize; - for (name, _, public, path) in BY_VALUE_TYPES { - if !public { - continue; - } - checked += 1; - let size = live - .iter() - .find(|(n, _)| n == name) - .map(|(_, size)| *size) - .unwrap_or_else(|| panic!("{name} is inventoried but never measured")); - assert!( - size <= STACK_BUDGET, - "{name} is {size} bytes, over the {STACK_BUDGET}-byte by-value \ - stack budget ({:.2}x). It is returned by value from {path}, so \ - every caller pays it on its own stack, and the constructors \ - nest -- one value's size is the multiplier, not the cost. \ - Box the large field: Encoder's six SILK slots and silk_buf, \ - and Decoder::silk, are Boxed for exactly this reason and \ - took Encoder from 87024 bytes to this one.", - size as f64 / STACK_BUDGET as f64 - ); - } - assert!( - checked >= 10, - "only {checked} rows were bound-checked; the budget must apply to \ - every type a caller can be made to hold by value" - ); - // The other half of the derivation, asserted so it cannot rot: the - // worst a caller can owe is DEEPEST_CHAIN x the worst public return. - let worst = live - .iter() - .filter(|(n, _)| { - BY_VALUE_TYPES - .iter() - .any(|(row, _, public, _)| row == n && *public) - }) - .map(|(_, size)| *size) - .max() - .expect("at least one public row"); - assert!( - DEEPEST_PUBLIC_CHAIN * worst * 8 <= 2 * 1024 * 1024, - "the deepest public chain ({DEEPEST_PUBLIC_CHAIN}) times the worst \ - public by-value return ({worst} B) is {} B, over an eighth of the \ - 2 MiB stack an unconfigured caller hands this crate; re-derive \ - STACK_BUDGET", - DEEPEST_PUBLIC_CHAIN * worst - ); - } - - /// The SILK encoders and the SILK packet buffer stay BEHIND A BOX. - /// - /// The SILK layer, the SILK packet buffer and `Decoder::silk` stay BEHIND - /// A BOX. Every field holding one is a `Box`, so no caller can reach them - /// by value. What keeps them off the stack is those exact `Box` spellings, - /// and THIS is the pin for that -- a source scan over the declarations, - /// with a negative control so it cannot be matching a doc comment. - /// - /// It earned its place, measured: unboxing `silk_buf` takes `Encoder` from - /// 4,072 to **7,896** bytes and reds this pin naming - /// `encoder.rs::Encoder::silk_buf` and - /// [`the_measured_inventory_is_accurate`] naming `Encoder`. Unboxing the - /// SILK layer is far larger -- `Encoder` was 87,024 bytes before the - /// boxing, 10.62x the budget. - #[test] - fn the_crate_private_silk_encoders_stay_behind_a_box() { - const PINS: &[(&str, &str, &str)] = &[ - ( - "encoder.rs", - "Encoder::silk", - "silk: Option>", - ), - ( - "encoder.rs", - "Encoder::silk_buf", - "silk_buf: Box<[u8; MAX_SILK_PACKET_BYTES]>", - ), - ("lib.rs", "Decoder::silk", "silk: Box"), - ]; - const SCANNED: &[(&str, &str)] = &[ - ("encoder.rs", include_str!("encoder.rs")), - ("lib.rs", include_str!("lib.rs")), - ]; - for (file, field, spelling) in PINS { - let src = SCANNED - .iter() - .find(|(name, _)| name == file) - .map(|(_, src)| *src) - .unwrap_or_else(|| panic!("no source pinned for {file}")); - let where_ = format!("{file}::{field}"); - assert!( - src.contains(spelling), - "{where_} is no longer declared `{spelling}`. An inline SILK \ - encoder or scratch buffer there puts {field}'s bytes back on \ - every caller's stack -- that is how Encoder was 87024 bytes \ - and Decoder 7576 before the boxing, 10.62x the by-value budget \ - on the type Encoder::new hands back by value." - ); - } - // And the negative control: the UNBOXED spellings must be ABSENT, or - // the scan above could be matching a doc comment instead of the live - // declaration. The unboxed forms are what this lane removed, so their - // absence is the proof the pin is reading the code. - const UNBOXED: &[(&str, &str, &str)] = &[ - ("encoder.rs", "Encoder::silk", "silk: Option,"), - ( - "encoder.rs", - "Encoder::silk_buf", - "silk_buf: [u8; MAX_SILK_PACKET_BYTES],", - ), - ("lib.rs", "Decoder::silk", "silk: SilkDecoder,"), - ]; - for (file, field, spelling) in UNBOXED { - let src = SCANNED - .iter() - .find(|(name, _)| name == file) - .map(|(_, src)| *src) - .unwrap_or_else(|| panic!("no source pinned for {file}")); - assert!( - !src.contains(spelling), - "{file}::{field} is declared unboxed as `{spelling}`. The box pin is \ - matching a spelling that is not the live declaration, and \ - {field}'s bytes are back on every caller's stack." - ); - } - } - - /// CAPABILITY: really construct the heavy types on a tight stack, so a - /// regression ABORTS THE PROCESS instead of costing invisible margin. - /// - /// Both arms are the crate's by-value constructors: `Encoder::new`, the - /// type that was 87,024 bytes, and `Decoder::new`, 7,576 -- plus a real - /// 20 ms SILK packet decoded through the `Decoder` that arm built, so the - /// decoder is proved live on this thread rather than merely constructed. - /// - /// Measured stack need, **debug** build, one process per point so a - /// SIGABRT cannot truncate the sweep: - /// - /// | stack | pre-fix layout (inline SILK slots) | current layout | - /// |---|---|---| - /// | 32768 | overflow | **overflow** | - /// | 49152 | overflow | **ok** | - /// | 131072 | overflow | ok | - /// | 262144 (**the gate**) | **overflow** | **ok** | - /// | 524288 | **overflow** | ok | - /// | 557056 | ok | ok | - /// | 786432 | ok | ok | - /// | 1048576 | ok | ok | - /// - /// The gate sits at 256 KiB, **5.3x** the measured need of the current - /// layout and **2.1x** below the pre-fix layout's own threshold. Note - /// what the upper rows say: the pre-fix layout SURVIVED 1 MiB and 2 MiB, - /// so a gate set at 1 MiB would have been a formality -- a test that - /// passes for the old layout proves nothing. `EC_OPUS_TIGHT_STACK_BYTES` - /// overrides the size, which is how the table is reproduced. - /// - /// The results are checked, not just the absence of a crash: an `Encoder` - /// that constructed but came back with the wrong channel count, or a - /// `Decoder` that decoded a packet to silence, would both pass a - /// "did not overflow" assertion. - #[test] - fn the_deepest_by_value_constructors_fit_on_a_tight_stack() { - use crate::encoder::Application; - let stack: usize = std::env::var("EC_OPUS_TIGHT_STACK_BYTES") - .ok() - .and_then(|v| v.parse().ok()) - .unwrap_or(TIGHT_STACK); - let handle = std::thread::Builder::new() - .stack_size(stack) - .spawn(move || { - // Arm 1: the by-value constructor that was 10.62x the budget. - let encoder = Encoder::new(48000, 1, Application::Audio) - .expect("Encoder::new(48000, 1, Audio)"); - let channels = encoder.channels(); - let rate = encoder.sample_rate(); - drop(encoder); - let mut decoder = Decoder::new(48000, 1).expect("Decoder::new(48000, 1)"); - // A real packet, so the decoder is proved live on this thread - // rather than merely constructed: the SILK-only 20 ms packet - // below decodes through the same by-value Decoder. - let mut out = vec![0.0f32; 5760]; - let written = decoder - .decode_float(SILK_ONLY_PACKET, &mut out) - .expect("decode_float on the tight-stack thread"); - let peak = out[..written].iter().fold(0.0f32, |m, s| m.max(s.abs())); - (channels, rate, written, peak) - }) - .expect("spawn tight-stack thread"); - let (channels, rate, pcm_len, peak) = handle.join().unwrap_or_else(|_| { - panic!( - "a by-value constructor overflowed a {stack}-byte stack; \ - EC_OPUS_TIGHT_STACK_BYTES reproduces the threshold sweep" - ) - }); - assert_eq!(channels, 1, "Encoder::new lost its channel count"); - assert_eq!(rate, 48000, "Encoder::new lost its sample rate"); - assert_eq!( - pcm_len, 960, - "Decoder::decode_float returned {pcm_len} samples, not the 960 one \ - 20 ms mono frame at 48 kHz holds; the tight-stack thread built a \ - Decoder that cannot decode" - ); - assert!( - peak > 0.0, - "Decoder::decode_float returned silence (peak {peak}) on the \ - tight-stack thread" - ); - eprintln!( - "TIGHTSTACK {stack} bytes: Encoder={} Decoder={} SilkDecoder={} \ - bytes, both constructors returned and decoded 960 samples", - std::mem::size_of::(), - std::mem::size_of::(), - std::mem::size_of::(), - ); - } - - /// A SILK-only 20 ms Opus packet: TOC byte `0x48` (config 9 = SILK - /// wideband 20 ms, stereo bit 0, code 0 = SILK only) followed by a frame - /// count byte `0x00` (one frame) and a 100-byte payload. Config 9 is - /// SILK-only, so decoding it exercises `SilkDecoder`'s state rather than - /// CELT's, and the crate decodes it to exactly 960 samples at 48 kHz. - const SILK_ONLY_PACKET: &[u8] = &[ - 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - ]; - - /// The sweep's teeth against a NEW by-value return: re-derive the set from - /// the crate source and require every name to be inventoried. - /// - /// Without this the inventory is a list of what someone remembered to - /// measure, and a lane that adds `pub fn make() -> HugeThing` where - /// `HugeThing` is 200 KB of inline tables passes every other test here. - #[test] - fn every_by_value_public_return_is_in_the_inventory() { - let mut found: Vec<(String, String)> = Vec::new(); - for (file, src) in FILES { - for (n, line) in src.lines().enumerate() { - if let Some(name) = by_value_return_name(line) { - found.push((name, format!("{file}:{}", n + 1))); - } - } - } - found.sort(); - found.dedup(); - assert!( - found.len() > 5, - "the by-value return scan found {} names across {} files, which is \ - too few to be a real sweep -- the matcher, not the crate, is broken", - found.len(), - FILES.len() - ); - - let mut missing = Vec::new(); - for (name, site) in &found { - if !BY_VALUE_TYPES.iter().any(|(n, _, _, _)| n == name) { - missing.push(format!("{name} ({site})")); - } - } - assert!( - missing.is_empty(), - "these types are returned BY VALUE from a public fn and are not in \ - the inventory: {}. Either they are thin enough not to be a stack \ - cost -- say so by adding a row with its measured size -- or they \ - are the next instance of the class this module exists for.", - missing.join(", ") - ); - } - - /// The control for the scan above, on synthetic text rather than by - /// trusting the live tree to happen to contain a case. - /// - /// If the matcher silently stopped recognising a shape, the sweep would - /// pass everything and this would still be asserting nothing. So: every - /// by-value spelling the crate uses must yield its type name, and every - /// pointer/Vec/primitive/`Self` spelling must yield nothing. - #[test] - fn the_inventory_scan_is_not_vacuous() { - for synthetic in [ - "pub fn zz_a() -> ZzNotInventoried { todo!() }", - "pub fn zz_b() -> Result { todo!() }", - "pub fn zz_c() -> Result { todo!() }", - "pub fn zz_d() -> Option { todo!() }", - "pub fn zz_e() -> ZzNotInventoried<'a> { todo!() }", - ] { - let name = by_value_return_name(synthetic) - .unwrap_or_else(|| panic!("the matcher does not recognise `{synthetic}`")); - assert_eq!( - name, "ZzNotInventoried", - "the matcher mangled `{synthetic}` into `{name}`, so the sweep \ - cannot see a new by-value return and its green is vacuous" - ); - assert!( - !BY_VALUE_TYPES.iter().any(|(n, _, _, _)| *n == name), - "the synthetic control name is in the inventory, so the control \ - proves nothing" - ); - } - for thin in [ - "pub fn zz_t() -> Box { todo!() }", - "pub fn zz_u() -> Result, Error> { todo!() }", - "pub fn zz_v() -> Option<&Path> { todo!() }", - "pub fn zz_w() -> usize { todo!() }", - "pub fn zz_x() -> Self { todo!() }", - "pub fn zz_y() -> Result, Error> { todo!() }", - "pub fn zz_z() { todo!() }", - "fn zz_private() -> ZzNotInventoried { todo!() }", - ] { - assert_eq!( - by_value_return_name(thin), - None, - "`{thin}` is a pointer, a Vec, a primitive, a `Self` the rows \ - above already cover, or not a `pub fn` -- the matcher must not \ - read it as an uninventoried by-value return" - ); - } - } -}