audio-dsp: validate stored remix plans so pieces cannot stack on one output (#4104)

* audio-dsp: validate stored remix plans so pieces cannot stack on one output

* address review: remix plans are budgeted in output samples, above what the longest project holds

* Merge origin/main; address review: limit a stored remix plan in ticks, not output samples
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-11 12:04:20 -05:00
1 parent 617e0aa7ba
commit 4c0d2ad79b
2 files changed
+104 -2

No files matched your search

+87 -1
View File
@@ -44,6 +44,16 @@ const MAX_SIMILARITY_CELLS: usize = 4_000_000;
/// Remix analysis supports ordinary production and high-resolution audio rates, not arbitrary
/// integers supplied through a public library API.
pub const MAX_ANALYSIS_SAMPLE_RATE: u32 = 384_000;
/// Most pieces a plan may have. A planned remix has a handful of joints (a few hundred for a very
/// long extension); a stored plan with more is damaged or hostile.
pub const MAX_PIECES: usize = 65_536;
/// The longest a project may run, in seconds, rounded up: `Tick::MAX` (`i64::MAX / 4` ticks at 254 016 000 000
/// ticks/s) is 9.08 million seconds. (This crate has no dependencies, so the figure is repeated here.)
const MAX_PROJECT_SECONDS: i64 = 9_100_000;
/// Most output samples a plan may describe: the longest project at the highest rate remix supports, so no
/// valid plan reaches it. A render reads at most two pieces per output sample it produces, so the work of
/// rendering a playable plan is bounded by its output samples, whatever the piece count.
pub const MAX_OUTPUT_SAMPLES: i64 = MAX_ANALYSIS_SAMPLE_RATE as i64 * MAX_PROJECT_SECONDS;
/// One piece of the output: `len` samples read from the source at `src`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -85,6 +95,37 @@ impl Plan {
pub fn cuts(&self) -> Vec<(i64, i64)> {
self.pieces.windows(2).map(|w| (w[0].src + w[0].len, w[1].src)).collect()
}
/// Whether playback can follow this plan with bounded work: at most [`MAX_PIECES`] pieces and [`MAX_OUTPUT_SAMPLES`] output samples, a
/// non-negative crossfade, non-negative lengths, and, when there are joints, every piece at
/// least as long as the crossfade and non-empty. Then no more than two pieces contribute to any
/// output sample, and every position [`render`] computes fits in `i64`.
pub fn is_playable(&self) -> bool {
self.is_playable_within(MAX_OUTPUT_SAMPLES)
}
/// [`Self::is_playable`] with the output limit in the plan's own units: a plan stored in ticks
/// (see `filmcraft-render`'s `Remix`) passes the project's longest duration in ticks instead.
pub fn is_playable_within(&self, max_output: i64) -> bool {
if self.pieces.is_empty() || self.pieces.len() > MAX_PIECES || self.xfade < 0 {
return false;
}
let joined = self.pieces.len() > 1;
let x = self.xfade;
let mut total = 0i64;
for p in &self.pieces {
if p.len < 0 || (joined && (p.len == 0 || p.len < x)) {
return false;
}
// render reads from `src − xfade/2` to `src + len + xfade/2`
if p.src.checked_sub(x).is_none() || p.src.checked_add(p.len).and_then(|e| e.checked_add(x)).is_none() {
return false;
}
match total.checked_add(p.len) {
Some(t) if t <= max_output => total = t,
_ => return false,
}
}
total.checked_add(x).is_some()
}
}
/// Remix sliders (see the module documentation).
@@ -634,6 +675,10 @@ pub fn plan(a: &Analysis, target: i64, params: Params) -> Result<Plan, RemixErro
/// gains; the result does not depend on how the output is cut into requests.
pub fn render(plan: &Plan, out0: i64, n: usize, channels: usize, read: &mut dyn FnMut(i64, usize) -> Vec<Vec<f32>>) -> Vec<Vec<f32>> {
let mut out = vec![vec![0f32; n]; channels];
// a plan that would stack many pieces on the same output (or overflow its positions) plays silence
if !plan.is_playable() || out0.checked_add(n as i64).is_none() {
return out;
}
let x = plan.xfade.max(0);
let h0 = x / 2;
let h1 = x - h0;
@@ -686,15 +731,24 @@ impl Plan {
}
s
}
/// Parses [`Plan::to_text`]; `None` unless the plan [is playable](Plan::is_playable).
pub fn from_text(s: &str) -> Option<Plan> {
Plan::from_text_within(s, MAX_OUTPUT_SAMPLES)
}
/// [`Plan::from_text`] with the output limit in the stored plan's units (see [`Plan::is_playable_within`]).
pub fn from_text_within(s: &str, max_output: i64) -> Option<Plan> {
let mut it = s.split(';');
let xfade = it.next()?.trim().parse().ok()?;
let mut pieces = Vec::new();
for p in it {
if pieces.len() >= MAX_PIECES {
return None;
}
let (a, b) = p.split_once(',')?;
pieces.push(Piece { src: a.trim().parse().ok()?, len: b.trim().parse().ok()? });
}
(!pieces.is_empty()).then_some(Plan { pieces, xfade })
let plan = Plan { pieces, xfade };
plan.is_playable_within(max_output).then_some(plan)
}
}
@@ -829,6 +883,7 @@ mod tests {
assert!(a.beats.contains(&out_end), "cut out at {out_end} is not a beat");
assert!(a.beats.contains(&in_start), "cut in at {in_start} is not a beat");
}
assert!(p.is_playable(), "planned remix must survive a save and reload: {p:?}");
for pc in &p.pieces {
assert!(pc.len >= (MIN_PIECE_BEATS as f64 * a.period) as i64 - 2, "piece too short: {pc:?}");
}
@@ -1039,4 +1094,35 @@ mod tests {
assert_eq!(Plan::from_text(&p.to_text()), Some(p));
assert_eq!(Plan::from_text("x"), None);
}
#[test]
fn stored_plans_cannot_stack_pieces() {
// thousands of empty pieces under one huge crossfade would each read and mix the same window
let stacked = format!("1000000000{}", ";0,0".repeat(5000));
assert_eq!(Plan::from_text(&stacked), None);
// a crossfade longer than a piece lets three pieces overlap
assert_eq!(Plan::from_text("10;0,100;200,5;300,100"), None);
assert_eq!(Plan::from_text("-1;0,100;200,100"), None);
assert_eq!(Plan::from_text("0;0,-5"), None);
assert_eq!(Plan::from_text(&format!("0;0,{};0,{}", i64::MAX, i64::MAX)), None);
// the output budget counts samples: the longest project at the highest rate fits, one sample more does not
assert!(Plan::from_text(&format!("0;0,{}", MAX_OUTPUT_SAMPLES)).is_some());
assert_eq!(Plan::from_text(&format!("0;0,{};0,1", MAX_OUTPUT_SAMPLES)), None);
assert!(MAX_OUTPUT_SAMPLES as i128 >= 384_000 * (i64::MAX as i128 / 4 / 254_016_000_000));
assert_eq!(Plan::from_text(&format!("0;{},1", i64::MAX)), None);
let many = format!("0{}", ";0,1".repeat(MAX_PIECES + 1));
assert_eq!(Plan::from_text(&many), None);
// the identity plan of an empty clip is still a plan
assert!(Plan::identity(0, 960).is_playable());
// render refuses an unvalidated hostile plan instead of reading once per piece
let hostile = Plan { pieces: vec![Piece { src: 0, len: 0 }; 5000], xfade: 1_000_000_000 };
let mut reads = 0usize;
let out = render(&hostile, 0, 512, 2, &mut |_, len| {
reads += 1;
vec![vec![1.0; len]; 2]
});
assert_eq!(reads, 0);
assert!(out.iter().all(|c| c.len() == 512 && c.iter().all(|&v| v == 0.0)));
}
}
+17 -1
View File
@@ -59,7 +59,8 @@ impl Remix {
let e = item.effects.iter().find(|e| e.effect == EFFECT)?;
let f = |k: &str| e.param(k).and_then(|p| p.value.as_f64());
let plan = match e.param("plan").map(|p| &p.value) {
Some(ParamValue::Text(s)) => dsp::Plan::from_text(s),
// stored in ticks: the limit is the longest project, not the sample-domain one
Some(ParamValue::Text(s)) => dsp::Plan::from_text_within(s, Tick::MAX.0),
_ => None,
};
let (xfade, pieces) = match plan {
@@ -198,6 +199,21 @@ mod tests {
assert!(it.effects.is_empty());
}
#[test]
fn a_stored_plan_longer_than_the_sample_limit_round_trips() {
// 20 s pieces are 5e12 ticks each: past the sample-domain limit, far inside the project's
let s = |x: f64| Tick::from_seconds_f64(x);
let r = Remix { pieces: vec![TickPiece { src: Tick::ZERO, len: s(20.0) }, TickPiece { src: s(30.0), len: s(20.0) }], xfade: s(0.1), ..remix() };
let mut it = item();
r.store(&mut it);
assert_eq!(Remix::of(&it), Some(r));
// a stored plan past the longest project is still refused
let hostile =
Remix { pieces: vec![TickPiece { src: Tick::ZERO, len: Tick::MAX }, TickPiece { src: Tick::ZERO, len: Tick(1) }], xfade: Tick::ZERO, ..remix() };
hostile.store(&mut it);
assert!(Remix::of(&it).is_some_and(|r| r.pieces.is_empty()));
}
#[test]
fn plays_the_pieces_and_ignores_request_cuts() {
let mut it = item();