From f32506d48c71e1339f395104cebb99ace60dba2b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 23:10:13 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m1/fmp4-metadata-limits.md Quest-Claim: eb8ceb94362cb1b8 From 3917bd8583e777663a5706f92fc4197a646956c1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 00:18:23 -0800 Subject: [PATCH 2/3] fix(mux): bound what an fMP4 moov or moof costs to parse and plan mp4-atom expands a parsed body up to tenfold, so a moov or moof body over fmp4::MAX_METADATA_BYTES (2 MiB) is now refused at its header, and MAX_HELD_BYTES (32 MiB) bounds only a text sample. MAX_FRAGMENT_SAMPLES drops to 2^17 and the sample plan is presized with a smaller per-sample queue, so a fragment at the bounds plans within MAX_HELD_BYTES. A moov declaring more than MAX_TRACKS (256) tracks is refused before any is created. Declaring a catalog rendition no longer clones and serializes the whole catalog, which made a moov of N tracks cost N^2. Co-Authored-By: Claude Opus 5.5 --- doc/lib/rs/moq-mux.md | 2 +- quest/m1/README.md | 1 - quest/m1/fmp4-metadata-limits.md | 66 ------- quest/m2/import-metadata-policy.md | 10 +- rs/moq-mux/CHANGELOG.md | 2 + rs/moq-mux/src/allocation.rs | 12 ++ rs/moq-mux/src/catalog/tracks.rs | 27 +-- rs/moq-mux/src/container/fmp4/import.rs | 249 +++++++++++++++++++----- rs/moq-mux/src/container/fmp4/mod.rs | 41 ++-- rs/moq-mux/src/container/fmp4/sample.rs | 18 +- rs/moq-mux/src/container/fmp4/text.rs | 2 +- 11 files changed, 279 insertions(+), 151 deletions(-) delete mode 100644 quest/m1/fmp4-metadata-limits.md diff --git a/doc/lib/rs/moq-mux.md b/doc/lib/rs/moq-mux.md index ed86acb59bc..ee3fbda293c 100644 --- a/doc/lib/rs/moq-mux.md +++ b/doc/lib/rs/moq-mux.md @@ -13,7 +13,7 @@ back. This is what `moq import` / `export` and the gateways are built on. | Format | Import | Export | Notes | | --- | --- | --- | --- | -| fMP4 / CMAF | yes | yes | Audio/video unwrapped to `legacy` by default, or passed through as `cmaf` with `with_passthrough`; `wvtt` subtitles import as timed WebVTT text and `stpp` TTML (IMSC timing profile) as `ttml` documents on the broadcast clock, preserving source language and label when present; other subtitle codecs are skipped. Import streams media samples as they arrive; each track's samples must be in forward byte order, and a `moov`, `moof`, or text sample over 32 MiB is refused. | +| fMP4 / CMAF | yes | yes | Audio/video unwrapped to `legacy` by default, or passed through as `cmaf` with `with_passthrough`; `wvtt` subtitles import as timed WebVTT text and `stpp` TTML (IMSC timing profile) as `ttml` documents on the broadcast clock, preserving source language and label when present; other subtitle codecs are skipped. Import streams media samples as they arrive; each track's samples must be in forward byte order, and a `moov` or `moof` over 2 MiB, a text sample over 32 MiB, a `moof` of more than 131,072 samples, or a `moov` of more than 256 tracks is refused. | | MPEG-TS | yes | yes | H.264/H.265; AAC, MP2, AC-3, E-AC-3, Opus up to 7.1; SCTE-35 sections and every other undecoded PES stream (KLV, subtitles, DVB AC-3) as tracks; service tables round-trip. | | FLV / RTMP | yes | yes | Legacy H.264 + AAC + MP3, plus enhanced-RTMP HEVC, AV1, VP9, Opus, AC-3, E-AC-3, and multitrack. | | Matroska / WebM | yes | yes | Import streams each block into its frame in one pass, retaining no declared size. It refuses subtitle tracks, CMAF output, video `BlockGroup`s with fields after their `Block`, a `CodecPrivate` before its `CodecID`, and decoder configs carrying extra metadata (any FLAC metadata block after STREAMINFO, OpusHead extension fields, av1C OBUs beyond the sequence header and HDR metadata, AAC AudioSpecificConfig bytes past its last field or with a non-AAC object type's config): remux video as `SimpleBlock`s, extract subtitles separately, and remux FLAC so its `CodecPrivate` holds only STREAMINFO. Stripping tags is not enough, since seek tables and padding are refused too. ffmpeg's `-c:a copy` keeps only STREAMINFO, except for channel layouts FLAC does not define (such as 5.1 with back surrounds, ffmpeg's `-ac 6` default, or 4.0): there ffmpeg adds a Vorbis comment holding the channel mask, so those are refused. An hvcC keeps only its parameter sets, dropping SEI such as x265's encoder settings. | diff --git a/quest/m1/README.md b/quest/m1/README.md index ef568f55338..932668af4ee 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -137,7 +137,6 @@ per-frame CPU growth since #5926. - [Pre-cut audit findings](/quest/m1/cut-audit/README.md) - fix the crashes, hostile-input costs, and correctness bugs the 2026-10-24 audit of main found - [Vendored code has every upstream fix it needs](/quest/m1/vendor-sync/README.md) - quinn, libv4l-rs, nvidia-video-codec-sdk, rust-media-libs, cros-codecs, and UniFFI generator fixes are ported or planned, once per release cycle - [Media fuzz findings](/quest/m1/fuzz-findings/README.md) - the nightly media fuzz targets run clean: mp4-atom, MKV block, TS step, SPS overflow, and catalog encoder bugs they found are fixed -- [fMP4 sample plans, trun counts, and trak counts are bounded](/quest/m1/fmp4-metadata-limits.md) - a `moof` or `moov` within the importer's bounds cannot plan hundreds of megabytes or take quadratic time; 116 bytes cost 584 MB today - [Mesh dials recover from a stuck flow](/quest/m1/mesh-fresh-port.md) - a relay redials a failing cluster peer from a fresh UDP port, so one dropped 5-tuple can't strand a link - [Cluster routing](/quest/m1/cluster-routing/README.md) - prove shared routing contracts, implement per-origin routes, and connect outbound local clusters - [Routing guide](/quest/m1/routing-guide.md) - concepts and practical outbound federation setup diff --git a/quest/m1/fmp4-metadata-limits.md b/quest/m1/fmp4-metadata-limits.md deleted file mode 100644 index fa4497653c7..00000000000 --- a/quest/m1/fmp4-metadata-limits.md +++ /dev/null @@ -1,66 +0,0 @@ -# [M] fMP4 sample plans, trun counts, and trak counts are bounded - -## Goal - -An fMP4 `moov` or `moof` that fits the importer's bounds -(`fmp4::MAX_HELD_BYTES`, `fmp4::MAX_FRAGMENT_SAMPLES`) parses and plans in -memory near `MAX_HELD_BYTES` and in time linear in its size, rather than -hundreds of megabytes or minutes of CPU: a `moof`'s sample plan is bounded -in bytes, a `trun`'s sample count is refused before mp4-atom expands it, and -a `moov`'s tracks are bounded and cost linear time. Each case below has a -test that asserts its bound. - -## Plan - -Decided 2026-11-03 (maintainer, on -[moq-dev/moq#7313](https://github.com/moq-dev/moq/pull/7313)): the fixes land -here, in m1, not in #7313, because 116 bytes cost 584 MB on any fMP4 or HLS -ingest. - -- ✅ Land #7313 as-is, with the fixes in an m1 quest -- Land #7313 as-is, with the fixes staying in m2 -- Fix all three in #7313 -- Accept the numbers - -Earlier, decided 2026-10-31 (maintainer, from -[moq-dev/moq#7232](https://github.com/moq-dev/moq/pull/7232)): measure the -parse amplification #7232's bounds allow, and fix each case or accept it with -the number. [moq-dev/moq#7313](https://github.com/moq-dev/moq/pull/7313) -added the media fuzz targets but cannot measure this: libFuzzer inputs stay a -few KiB, far below the 32 MiB bound. It measured the cases below by hand -instead, through `Import::decode` with moq-mux's counting allocator, as peak -live heap. Every one needs a fix: - -| Input | Size | Peak | Note | -|---|---|---|---| -| `moof` with field-less `trun`s declaring 2^21 samples (`MAX_FRAGMENT_SAMPLES`), then an `mdat` header | 116 B | 584 MB | the plan alone, before any `mdat` byte | -| `moof` of 2^21 4-byte `trun` entries (`sample_size` only), then an `mdat` header | 8 MiB | 667 MB (80x) | 684 MB with passthrough | -| `moof` of 4-byte entries filling `MAX_HELD_BYTES` (8.4M entries) | 32 MiB | 369 MB (11x) | refused as `TooManySamples` only after mp4-atom parses it | -| `moov` of repeated audio `trak`s (bbb.mp4's, 477 B each) | 0.24 / 0.48 / 0.95 MB | 35x | CPU quadratic: 0.25 s, 0.9 s, 3.9 s; allocations 96 MB, 360 MB, 1.4 GB | - -- Planning costs about 280 B per sample: a `Sample` in each `Run`, a - `Chunk` in the `mdat`'s queue, and the growth of both unsized collections. - Bound the plan's memory, not just the sample count: for example lower - `MAX_FRAGMENT_SAMPLES` until the plan fits near `MAX_HELD_BYTES`, - presize the collections, and shrink `Chunk` (its `Text` variant sets its - size). -- mp4-atom expands each explicit `trun` entry to a 40-byte `TrunEntry` - before the sample count is checked. Refuse the count before parsing it: - read the `trun` sample counts from the held body, or bound a `moof` body - separately from a `moov` one. -- Each `trak` added to a `moov` costs time and allocation in proportion to - the tracks already added. Extrapolated to 32 MiB (about 70,000 `trak`s), - that is over an hour of CPU and a peak above 1 GB. Find the step that - revisits every earlier track (catalog renditions, track creation, or - enrollment), make it linear, and bound the tracks a `moov` declares. - -The bounds are moq-mux's public consts. This quest sets their values; -[one policy for oversized container metadata](/quest/m2/import-metadata-policy.md) -documents them later with MKV's. - -Public API: possibly new or changed `pub const` bounds in moq-mux. Wire: none. - -## Related - -- [One policy for oversized container metadata](/quest/m2/import-metadata-policy.md) - documents the importer bounds this quest sets -- [Media fuzz findings](/quest/m1/fuzz-findings/README.md) - the bugs the media fuzz targets found diff --git a/quest/m2/import-metadata-policy.md b/quest/m2/import-metadata-policy.md index 6ca5f60f316..cf69b1a3584 100644 --- a/quest/m2/import-metadata-policy.md +++ b/quest/m2/import-metadata-policy.md @@ -8,18 +8,14 @@ The fMP4 and MKV importers follow one documented policy for container metadata t The two importers settled this separately during the 2026-10-27 security audit: -- fMP4 ([moq-dev/moq#7232](https://github.com/moq-dev/moq/pull/7232), merged): what must be held whole, a `moov` or `moof` body or one text sample, is refused at its header when it declares more than `fmp4::MAX_HELD_BYTES` (32 MiB, the transport's per-group `MAX_CACHE_BYTES`), and a `moof` declaring more than `fmp4::MAX_FRAGMENT_SAMPLES` (2^21) is refused when it parses. That reversed the 2026-10-28 incremental plan, because incremental parsing could not bound what it kept. +- fMP4 ([moq-dev/moq#7232](https://github.com/moq-dev/moq/pull/7232), merged): what must be held whole is refused at its header, which reversed the 2026-10-28 incremental plan, because incremental parsing could not bound what it kept. [moq-dev/moq#7687](https://github.com/moq-dev/moq/pull/7687) then measured that mp4-atom expands a parsed body up to tenfold, so a `moov` or `moof` body is bounded by `fmp4::MAX_METADATA_BYTES` (2 MiB) and a text sample by `fmp4::MAX_HELD_BYTES` (32 MiB, the transport's per-group `MAX_CACHE_BYTES`). A `moof` declaring more than `fmp4::MAX_FRAGMENT_SAMPLES` (2^17) is refused when it parses, and a `moov` declaring more than `fmp4::MAX_TRACKS` (256) before any track is created. - MKV ([moq-dev/moq#7234](https://github.com/moq-dev/moq/pull/7234), merged): metadata parses incrementally with no blanket size cap (maintainer, 2026-10-28). Each collected master keeps one value per field, so repeats replace rather than accumulate. [moq-dev/moq#7629](https://github.com/moq-dev/moq/pull/7629) bounded the fields read whole: `DocType` and `CodecID` by the longest supported value, and `CodecPrivate` by each codec's supported configuration, refused at its header (FLAC STREAMINFO alone, the RFC 7845 OpusHead, av1C with a sequence header and HDR metadata, avcC by its count fields at about 34 MiB, and an AudioSpecificConfig by its syntax maximum). An hvcC is canonicalized as it arrives instead: its parameter sets are kept, at most one per id, and SEI is discarded unread. These are private constants in `rs/moq-mux/src/container/mkv/import.rs`, `codec::h264::AVCC_MAX_LEN`, and `codec::aac::CONFIG_MAX_LEN`. Decided 2026-11-01 (maintainer, follow-up planning after the 2026-11-01 /quest-afk run): m2. Pick one policy for both importers, rather than leaving each at the decision its own audit PR made. Starting point: a byte bound at the header for whatever must be held whole, since #7232 found that is what actually bounds memory, while MKV keeps streaming what it can consume incrementally. Bring the choice to the maintainer in the PR if the evidence points elsewhere. -- #7232 has landed the fMP4 side (`MAX_HELD_BYTES` and `MAX_FRAGMENT_SAMPLES` in `rs/moq-mux/src/container/fmp4/mod.rs`). Note the MKV avcC bound (about 34 MiB) sits above fMP4's 32 MiB `MAX_HELD_BYTES`. -- Inventory what each importer still holds whole, and how much parsed state it builds from it (#7232 noted a 32 MiB `moof` decodes into larger structures). +- The fMP4 side has landed (`MAX_HELD_BYTES`, `MAX_METADATA_BYTES`, `MAX_FRAGMENT_SAMPLES`, and `MAX_TRACKS` in `rs/moq-mux/src/container/fmp4/mod.rs`). Note the MKV avcC bound (about 34 MiB) sits far above fMP4's 2 MiB `MAX_METADATA_BYTES`. +- Inventory what each importer still holds whole, and how much parsed state it builds from it (fMP4's parse expands up to tenfold, which is what sets `MAX_METADATA_BYTES`). - Keep audio and video payloads unbounded in both: streaming each sample into its frame was decided 2026-10-27, and this quest is about metadata only. An fMP4 text sample is the one payload held whole, because its rewrite needs every byte, so `MAX_HELD_BYTES` covers it; keep that exception. MKV has no counterpart, since it refuses subtitle tracks. - Document the policy once, in moq-mux's import docs, and make both importers' bounds `pub const`s as #7232's are. Public API: possibly new `pub const` bounds and a shared refusal error in moq-mux. Wire: none. - -## Related - -- [fMP4 sample plans, trun counts, and trak counts are bounded](/quest/m1/fmp4-metadata-limits.md) - measured what fMP4's bounds let a `moof` or `moov` build, and sets the bounds this policy documents diff --git a/rs/moq-mux/CHANGELOG.md b/rs/moq-mux/CHANGELOG.md index 7238150996d..1d89d2d728e 100644 --- a/rs/moq-mux/CHANGELOG.md +++ b/rs/moq-mux/CHANGELOG.md @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- fMP4 import bounds what a `moov` or `moof` costs to parse and plan: a body over `fmp4::MAX_METADATA_BYTES` (2 MiB) is refused at its header, since parsing expands it up to tenfold, a `moof` of more than `fmp4::MAX_FRAGMENT_SAMPLES` (now 131,072) samples is refused before planning, and a `moov` of more than `fmp4::MAX_TRACKS` (256) tracks with `Error::TooManyTracks` before any is created. `fmp4::MAX_HELD_BYTES` (32 MiB) now bounds only a text sample. A 116-byte `moof` no longer plans 584 MB. +- Declaring a catalog rendition costs the same however many the catalog already holds: it is checked on its own rather than by cloning and serializing the whole catalog, which made N tracks cost N². - `container::Consumer` no longer plays a backlog that landed before its first read from the oldest group: until the first item, the start moves past each group whose first frame trails the newest content by more than the max delay plus the warmup, as `@moq/hang`'s consumer does, keeping the group covering the live edge less `Delay::min` and the warmup. A join starts at the same group in Rust and the browser. Once reading begins, only a blocking group is skipped, as before. - fMP4 import applies each track's edit list to its frame timestamps and drops the samples before the edit, such as AAC priming, along with the video samples decoding from a dropped sync sample, and a passthrough init no longer carries the `edts`. A consumer that offsets AAC priming itself must stop, or it shifts the track twice. An edit list it can't reduce to one shift (several media edits, or a rate other than 1) is refused with `Error::UnsupportedEditList`. diff --git a/rs/moq-mux/src/allocation.rs b/rs/moq-mux/src/allocation.rs index 834ba1c418f..816ba3f5542 100644 --- a/rs/moq-mux/src/allocation.rs +++ b/rs/moq-mux/src/allocation.rs @@ -16,6 +16,15 @@ pub struct Usage { pub freed: usize, /// Calls to allocate or reallocate. pub allocations: usize, + /// The most bytes held at once beyond what the thread held when the call began. + pub peak: usize, +} + +impl Usage { + /// Bytes allocated and not yet freed; freeing what the call did not allocate lowers it. + fn live(&self) -> usize { + self.allocated.saturating_sub(self.freed) + } } thread_local! { @@ -41,6 +50,7 @@ unsafe impl GlobalAlloc for Counting { record(|usage| { usage.allocated += layout.size(); usage.allocations += 1; + usage.peak = usage.peak.max(usage.live()); }); unsafe { System.alloc(layout) } } @@ -55,6 +65,8 @@ unsafe impl GlobalAlloc for Counting { usage.allocated += new_size; usage.freed += layout.size(); usage.allocations += 1; + // Both blocks are live while a realloc copies between them. + usage.peak = usage.peak.max(usage.live() + layout.size()); }); unsafe { System.realloc(ptr, layout, new_size) } } diff --git a/rs/moq-mux/src/catalog/tracks.rs b/rs/moq-mux/src/catalog/tracks.rs index f45658282d7..bccc3cab554 100644 --- a/rs/moq-mux/src/catalog/tracks.rs +++ b/rs/moq-mux/src/catalog/tracks.rs @@ -384,6 +384,20 @@ fn quantize(published: Option, measured: Option, period: Dur } } +/// Write `config` under `name`, refusing it before any change if it redefines the rendition's +/// identity or won't serialize, so the reserved snapshot never retains a bad edit. +/// +/// The entry is checked in a catalog holding only it: the rest was checked when it was written, and +/// cloning or serializing the whole catalog would make declaring N renditions cost N². +fn write>(catalog: &mut Catalog, name: &str, config: C) -> crate::Result<()> { + let mut alone = Catalog::default(); + config.clone().insert(&mut alone, name); + check_identity(catalog, &alone, name)?; + serde_json::to_writer(std::io::sink(), &alone).map_err(moq_json::Error::from)?; + config.insert(catalog, name); + Ok(()) +} + /// Refuse an edit that redefines rendition `name`'s track identity in place: a publisher whose /// decoder identity changed publishes under a new name instead. fn check_identity(previous: &Catalog, next: &Catalog, name: &str) -> crate::Result<()> { @@ -669,13 +683,8 @@ impl> Rendition { config.set_estimate(resolved.clone()); { let mut guard = self.catalog.modify()?; - let mut next = (*guard).clone(); - config.insert(&mut next, &self.name); - check_identity(&guard, &next, &self.name)?; - // Serialization must succeed before the reserved snapshot retains the edit. - serde_json::to_writer(std::io::sink(), &next).map_err(moq_json::Error::from)?; + write(&mut guard, &self.name, config)?; self.present = true; - *guard = next; guard.list::(&self.name); if C::detects() { guard.hint::(&self.name, supplied.clone()); @@ -816,11 +825,7 @@ impl> Rendition { } Self::check_decrease(&self.config()?.estimate(), &config.estimate())?; let mut guard = self.catalog.modify()?; - let mut next = (*guard).clone(); - config.insert(&mut next, &self.name); - check_identity(&guard, &next, &self.name)?; - serde_json::to_writer(std::io::sink(), &next).map_err(moq_json::Error::from)?; - *guard = next; + write(&mut guard, &self.name, config)?; Ok(guard) } } diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index 0641bda3c7a..7e37eed9d4b 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -351,10 +351,12 @@ impl Import { /// `default_base_moof`. Forward gaps and interleaved tracks are supported. /// /// Media samples stream into their frames as bytes arrive. What must be held whole is - /// bounded: a `moov` or `moof` body, or a text sample, larger than - /// [`MAX_HELD_BYTES`](super::MAX_HELD_BYTES), or a `moof` declaring more than - /// [`MAX_FRAGMENT_SAMPLES`](super::MAX_FRAGMENT_SAMPLES) samples, is refused before it is - /// held. + /// bounded: a `moov` or `moof` body larger than + /// [`MAX_METADATA_BYTES`](super::MAX_METADATA_BYTES), or a text sample larger than + /// [`MAX_HELD_BYTES`](super::MAX_HELD_BYTES), is refused before it is held. A `moof` declaring + /// more than [`MAX_FRAGMENT_SAMPLES`](super::MAX_FRAGMENT_SAMPLES) samples is refused before + /// they are planned, and a `moov` declaring more than [`MAX_TRACKS`](super::MAX_TRACKS) + /// tracks before any is created. /// /// A decode error is terminal: later decodes, cuts, seeks and discontinuities return the same /// error without reading input or changing the tracks. @@ -490,7 +492,7 @@ impl Import { return Err(Error::DuplicateMoof.into()); } let size = size.ok_or(mp4_atom::Error::InvalidSize)?; - if size > super::MAX_HELD_BYTES { + if size > super::MAX_METADATA_BYTES { return Err(Error::MetadataTooLarge { kind, size }.into()); } Ok(Body::Metadata { @@ -511,12 +513,7 @@ impl Import { let moof = Moof::decode_body(&mut body.as_ref())?; // Planning resolves every sample, and a run without per-sample fields declares its count // in four bytes, so the byte bound alone does not limit what planning would hold. - let samples: u64 = moof - .traf - .iter() - .flat_map(|traf| &traf.trun) - .map(|trun| trun.samples.len() as u64) - .sum(); + let samples: u64 = moof.traf.iter().map(sample::count).sum(); if samples > super::MAX_FRAGMENT_SAMPLES { return Err(Error::TooManySamples(samples).into()); } @@ -530,6 +527,9 @@ impl Import { if self.moov.is_some() { return Err(Error::DuplicateMoov.into()); } + if moov.trak.len() > super::MAX_TRACKS { + return Err(Error::TooManyTracks(moov.trak.len()).into()); + } // Only `finish()` releases the reservation before a moov, since a fragment needs one. let reserved = self.initial_reservation.clone().ok_or(Error::MoovAfterFinish)?; @@ -1101,11 +1101,15 @@ impl Import { } let mut resolver = sample::Resolver::new(self.moof_span, len.unwrap_or(usize::MAX)); resolver.streaming(); + // `end_metadata` bounded the count, so the plan is sized once instead of growing. + let samples = moof.traf.iter().map(sample::count).sum::() as usize; let mut mdat = Mdat { remaining: len, position: 0, tracks: Vec::new(), - chunks: std::collections::VecDeque::new(), + text: Vec::new(), + staged: BytesMut::new(), + chunks: std::collections::VecDeque::with_capacity(samples), }; // Validate every track before opening any payload writer. Otherwise a later traf's // backwards run or reused bytes could fail after earlier tracks published data. @@ -1113,15 +1117,14 @@ impl Import { for traf in &moof.traf { let id = traf.tfhd.track_id; if let Some(track) = self.text.get_mut(&id) { - for sample in track.prepare(traf, &mut resolver, &mut self.timebase)? { + let samples = track.prepare(traf, &mut resolver, &mut self.timebase)?; + mdat.text.reserve(samples.len()); + for sample in samples { mdat.chunks.push_back(Chunk { range: sample.range.clone(), - output: ChunkOutput::Text { - id, - sample, - data: BytesMut::new(), - }, + output: ChunkOutput::Text(mdat.text.len()), }); + mdat.text.push((id, sample)); } anchored = true; continue; @@ -1190,17 +1193,15 @@ impl Import { let count = (chunk.range.end - mdat.position).min(data.len()); let complete = mdat.position + count == chunk.range.end; match &mut chunk.output { - ChunkOutput::Text { - id, - sample, - data: buffer, - } => { - buffer.extend_from_slice(&data[..count]); + ChunkOutput::Text(index) => { + mdat.staged.extend_from_slice(&data[..count]); if complete { + let (id, sample) = &mdat.text[*index]; + let staged = std::mem::take(&mut mdat.staged); self.text .get_mut(id) .expect("planned text track") - .write(sample, buffer)?; + .write(sample, &staged)?; } } ChunkOutput::Media { @@ -1284,6 +1285,11 @@ struct Mdat { remaining: Option, position: usize, tracks: Vec, + /// Each planned text sample and the track it belongs to. + text: Vec<(u32, super::text::Sample)>, + /// The bytes of the text sample arriving now, which is rewritten once whole. + staged: BytesMut, + /// Every planned sample in `mdat` order, kept small since a fragment may plan many. chunks: std::collections::VecDeque, } @@ -1335,15 +1341,10 @@ struct Chunk { } enum ChunkOutput { - Media { - track: usize, - sample: usize, - }, - Text { - id: u32, - sample: super::text::Sample, - data: BytesMut, - }, + /// A sample of `Mdat::tracks[track]`. + Media { track: usize, sample: usize }, + /// The sample at this index of `Mdat::text`. + Text(usize), } /// Where a fragment lands on its track. @@ -1391,7 +1392,7 @@ impl Fmp4Track { let mut earliest = None; let mut end = None; - let mut samples = Vec::new(); + let mut samples = Vec::with_capacity(sample::count(traf) as usize); let mut dropped = false; for resolved in resolver.resolve(traf, self.defaults)? { let sample::Resolved { sample, cts, range } = resolved?; @@ -2027,7 +2028,7 @@ mod tests { #[test] fn oversized_metadata_is_refused_at_its_header() { for kind in [b"moov", b"moof"] { - for size in [super::super::MAX_HELD_BYTES + 1, 1usize << 62] { + for size in [super::super::MAX_METADATA_BYTES + 1, 1usize << 62] { let mut import = importer(); let header = box_header(kind, Some(size)); let (last, prefix) = header.split_last().unwrap(); @@ -2050,15 +2051,15 @@ mod tests { fn metadata_at_the_bound_holds_only_what_arrived() { let mut import = importer(); import - .decode(&box_header(b"moof", Some(super::super::MAX_HELD_BYTES))) + .decode(&box_header(b"moof", Some(super::super::MAX_METADATA_BYTES))) .unwrap(); import.decode(&[0xA5; 1024]).unwrap(); assert!(matches!(import.body, Some(Body::Metadata { .. }))); assert_eq!(import.buffer.len(), 1024); } - // A moof whose runs carry no per-sample fields, each declaring `count` samples in a few bytes. - fn inherited_moof(id: u32, counts: &[u32]) -> Vec { + // A moof of one traf on track `id` carrying `trun`, whose samples are a byte and 1024 ticks each. + fn runs_moof(id: u32, trun: Vec) -> Vec { let moof = Moof { mfhd: Default::default(), traf: vec![mp4_atom::Traf { @@ -2071,16 +2072,7 @@ mod tests { tfdt: Some(mp4_atom::Tfdt { base_media_decode_time: 0, }), - trun: counts - .iter() - .map(|&count| mp4_atom::Trun { - data_offset: None, - samples: mp4_atom::TrunSamples::Inherited { - count, - first_flags: None, - }, - }) - .collect(), + trun, ..Default::default() }], }; @@ -2089,6 +2081,21 @@ mod tests { data } + // A moof whose runs carry no per-sample fields, each declaring `count` samples in a few bytes. + fn inherited_moof(id: u32, counts: &[u32]) -> Vec { + let trun = counts + .iter() + .map(|&count| mp4_atom::Trun { + data_offset: None, + samples: mp4_atom::TrunSamples::Inherited { + count, + first_flags: None, + }, + }) + .collect(); + runs_moof(id, trun) + } + #[test] fn huge_trun_sample_counts_are_refused() { let max = super::super::MAX_FRAGMENT_SAMPLES; @@ -2118,6 +2125,150 @@ mod tests { assert!(import.moof.is_some()); } + /// A fragment within the bounds plans within [`MAX_HELD_BYTES`](super::super::MAX_HELD_BYTES) + /// however its runs declare their samples, and a `moof` at the byte bound that declares too + /// many parses within it before it is refused. + #[test] + fn fragment_plans_within_the_held_bound() { + use super::super::{MAX_FRAGMENT_SAMPLES, MAX_HELD_BYTES, MAX_METADATA_BYTES}; + let entries = |count: usize, entry: mp4_atom::TrunEntry| { + vec![mp4_atom::Trun { + data_offset: None, + samples: vec![entry; count].into(), + }] + }; + // Parsing expands a 4-byte entry most; all four fields are the most a real run carries. + let small = mp4_atom::TrunEntry { + size: Some(1), + ..Default::default() + }; + let full = mp4_atom::TrunEntry { + duration: Some(1024), + size: Some(1), + flags: Some(0), + cts: Some(0), + }; + // Room for the boxes around the runs. + let room = MAX_METADATA_BYTES - 1024; + let (_, id, _) = initialized(false); + // One sample per run, each a byte past the last, so no two samples' bytes are adjacent. + let gapped = |start: usize| { + (0..room / 20) + .map(|index| mp4_atom::Trun { + data_offset: Some((start + 2 * index) as i32), + samples: mp4_atom::TrunSamples::Inherited { + count: 1, + first_flags: None, + }, + }) + .collect() + }; + let start = runs_moof(id, gapped(0)).len() + 8; + let inputs = [ + ( + "field-less runs", + inherited_moof(id, &[MAX_FRAGMENT_SAMPLES as u32]), + None, + ), + ("full entries", runs_moof(id, entries(room / 16, full)), None), + ( + "4-byte entries", + runs_moof(id, entries(room / 4, small)), + Some(room / 4), + ), + ("one-sample runs", runs_moof(id, gapped(start)), None), + ]; + for passthrough in [false, true] { + for (name, moof, refused) in &inputs { + let (mut import, _, _) = initialized(passthrough); + let mut data = moof.clone(); + assert!(data.len() <= MAX_METADATA_BYTES + 8, "{name}: {} bytes", data.len()); + data.extend_from_slice(&box_header(b"mdat", None)); + let (result, usage) = crate::allocation::measure(|| import.decode(&data)); + match refused { + None => result.unwrap(), + Some(count) => assert!( + matches!(result, Err(crate::Error::Cmaf(Error::TooManySamples(refused))) if refused == *count as u64), + "{name}: {result:?}" + ), + } + assert!( + usage.peak <= MAX_HELD_BYTES, + "{name}, passthrough {passthrough}: held {} bytes", + usage.peak + ); + } + } + } + + // A moov of bbb.mp4's audio track, repeated `count` times under fresh ids. + fn tracks_moov(count: usize) -> Vec { + use mp4_atom::Decode; + let mut data = include_bytes!("test_data/bbb.mp4").as_slice(); + mp4_atom::Ftyp::decode(&mut data).unwrap(); + let mut moov = Moov::decode(&mut data).unwrap(); + let audio = moov + .trak + .iter() + .find(|trak| trak.mdia.hdlr.handler.as_ref() == b"soun") + .unwrap() + .clone(); + moov.mvex = None; + moov.trak = (1..=count as u32) + .map(|id| { + let mut trak = audio.clone(); + trak.tkhd.track_id = id; + trak + }) + .collect(); + let mut data = Vec::new(); + moov.encode(&mut data).unwrap(); + data + } + + /// Each track a moov declares costs the same, whatever came before it, and a moov declaring + /// more than [`MAX_TRACKS`](super::super::MAX_TRACKS) creates none, even at the byte bound. + #[test] + fn moov_tracks_are_bounded() { + use super::super::{MAX_HELD_BYTES, MAX_METADATA_BYTES, MAX_TRACKS}; + let decode = |count: usize| { + let mut import = importer(); + let data = tracks_moov(count); + let (result, usage) = crate::allocation::measure(|| import.decode(&data)); + (import, data.len(), result, usage) + }; + + let (import, _, result, half) = decode(MAX_TRACKS / 2); + result.unwrap(); + assert_eq!(import.tracks.len(), MAX_TRACKS / 2); + let (import, _, result, all) = decode(MAX_TRACKS); + result.unwrap(); + assert_eq!(import.tracks.len(), MAX_TRACKS); + // Twice the tracks; a step revisiting every earlier track would cost about four times. + assert!( + all.allocated < 3 * half.allocated, + "{} tracks allocated {}, {} allocated {}", + MAX_TRACKS / 2, + half.allocated, + MAX_TRACKS, + all.allocated + ); + assert!(all.peak <= MAX_HELD_BYTES, "held {} bytes", all.peak); + + let per_track = tracks_moov(2).len() - tracks_moov(1).len(); + let fill = (MAX_METADATA_BYTES - 1024) / per_track; + for count in [MAX_TRACKS + 1, fill] { + let (import, len, result, usage) = decode(count); + assert!(len <= MAX_METADATA_BYTES + 8); + assert!( + matches!(result, Err(crate::Error::Cmaf(Error::TooManyTracks(refused))) if refused == count), + "{count}: {result:?}" + ); + assert!(import.tracks.is_empty() && import.text.is_empty()); + assert!(usage.peak <= MAX_HELD_BYTES, "{count} tracks held {} bytes", usage.peak); + } + } + #[test] fn media_is_owned_by_the_output_before_mdat_finishes() { const SIZE: usize = 64 * 1024; diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 9f0342d8f7a..360cdf4133e 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -24,21 +24,36 @@ pub use muxer::*; /// A major version bump of `mp4_atom` is a breaking change for moq-mux. pub use mp4_atom; -/// The most input [`Import`] holds whole: a `moov` or `moof` body, or one text sample. +/// The largest text sample [`Import`] holds whole. /// -/// Metadata parses and text rewrites only once their last byte arrives, so a larger declaration -/// is refused at its header rather than retaining later input (across HLS segments, too) until -/// it completes. It matches the transport's per-group bound, -/// [`MAX_CACHE_BYTES`](moq_net::group::MAX_CACHE_BYTES). Real fMP4 metadata is kilobytes. Media -/// samples stream into their frames and have no such bound. +/// A text sample is rewritten only once its last byte arrives, so a larger one is refused before +/// any of it is staged. It matches the transport's per-group bound, +/// [`MAX_CACHE_BYTES`](moq_net::group::MAX_CACHE_BYTES), which the rewritten sample must fit. +/// Media samples stream into their frames and have no such bound. pub const MAX_HELD_BYTES: usize = moq_net::group::MAX_CACHE_BYTES as usize; +/// The largest `moov` or `moof` body [`Import`] parses. +/// +/// Metadata parses only once its last byte arrives, so a larger declaration is refused at its +/// header rather than retaining later input (across HLS segments, too) until it completes. +/// Parsing expands a body up to tenfold (a 4-byte `trun` entry becomes a 40-byte +/// [`TrunEntry`](mp4_atom::TrunEntry)), so this keeps a parse within [`MAX_HELD_BYTES`]. Real +/// fMP4 metadata is kilobytes. +pub const MAX_METADATA_BYTES: usize = MAX_HELD_BYTES / 16; + /// The most samples [`Import`] plans for one `moof`. /// -/// Every sample's byte range is resolved before any of the fragment publishes. A `moof` at -/// [`MAX_HELD_BYTES`] holds this many 16-byte `trun` entries; a run without per-sample fields +/// Every sample's byte range is resolved before any of the fragment publishes, at about 200 bytes +/// a sample, so a fragment at this bound plans within [`MAX_HELD_BYTES`]. A `moof` at +/// [`MAX_METADATA_BYTES`] holds this many 16-byte `trun` entries; a run without per-sample fields /// could otherwise declare billions of samples in a few bytes. -pub const MAX_FRAGMENT_SAMPLES: u64 = (MAX_HELD_BYTES / 16) as u64; +pub const MAX_FRAGMENT_SAMPLES: u64 = (MAX_METADATA_BYTES / 16) as u64; + +/// The most tracks [`Import`] accepts from one `moov`, refused before any is created. +/// +/// Each track costs about 16 KiB to parse and set up, and keeps its catalog entry and output +/// state for as long as the import runs. Real files declare a handful. +pub const MAX_TRACKS: usize = 256; #[cfg(test)] mod export_test; @@ -212,8 +227,8 @@ pub enum Error { BackwardsDataOffset, /// A `moov` or `moof` must be held whole before it parses, so one declaring more than - /// [`MAX_HELD_BYTES`] is refused at its header, before any of its body is buffered. - #[error("{kind} declares {size} bytes, more than the {MAX_HELD_BYTES} an importer holds")] + /// [`MAX_METADATA_BYTES`] is refused at its header, before any of its body is buffered. + #[error("{kind} declares {size} bytes, more than the {MAX_METADATA_BYTES} an importer parses")] MetadataTooLarge { /// The box type. kind: mp4_atom::FourCC, @@ -232,6 +247,10 @@ pub enum Error { #[error("moof declares {0} samples, more than the {MAX_FRAGMENT_SAMPLES} an importer plans")] TooManySamples(u64), + /// A `moov` declaring more than [`MAX_TRACKS`] tracks is refused before any is created. + #[error("moov declares {0} tracks, more than the {MAX_TRACKS} an importer accepts")] + TooManyTracks(usize), + /// Absolute file offsets cannot be resolved in a standalone fragment. #[error("absolute base_data_offset is unsupported; use moof-relative offsets")] UnsupportedBaseDataOffset, diff --git a/rs/moq-mux/src/container/fmp4/sample.rs b/rs/moq-mux/src/container/fmp4/sample.rs index bceb192a73a..5f18533c995 100644 --- a/rs/moq-mux/src/container/fmp4/sample.rs +++ b/rs/moq-mux/src/container/fmp4/sample.rs @@ -74,6 +74,11 @@ impl Sample { } } +/// The samples a `traf`'s runs declare, counted wide enough that no run count overflows it. +pub(crate) fn count(traf: &mp4_atom::Traf) -> u64 { + traf.trun.iter().map(|run| run.samples.len() as u64).sum() +} + /// Resolve one fragment's samples against its mdat, sharing byte ownership across tracks. pub(crate) struct Resolver { data_start: usize, @@ -194,10 +199,15 @@ impl Claimed { return Err(super::Error::EmptySample); } // The taken ranges are disjoint, so the last one starting before `range` ends reaches furthest. - if let Some((_, &end)) = self.0.range(..range.end).next_back() - && end > range.start - { - return Err(super::Error::InvalidDataOffset); + if let Some((_, end)) = self.0.range_mut(..range.end).next_back() { + if *end > range.start { + return Err(super::Error::InvalidDataOffset); + } + // Samples usually sit back to back, so extending the range keeps one entry per run. + if *end == range.start { + *end = range.end; + return Ok(()); + } } self.0.insert(range.start, range.end); Ok(()) diff --git a/rs/moq-mux/src/container/fmp4/text.rs b/rs/moq-mux/src/container/fmp4/text.rs index dad774b6792..23082860a87 100644 --- a/rs/moq-mux/src/container/fmp4/text.rs +++ b/rs/moq-mux/src/container/fmp4/text.rs @@ -62,7 +62,7 @@ impl Track { if self.end_decode_time.is_some_and(|end| dts < end) { return Err(Error::InvalidSubtitle("subtitle fragment overlaps the previous one").into()); } - let mut samples = Vec::new(); + let mut samples = Vec::with_capacity(sample::count(traf) as usize); for resolved in resolver.resolve(traf, self.defaults)? { let sample::Resolved { sample, cts, range } = resolved?; // A text sample is staged whole before it is rewritten. From 5fc9276f74049695dc433e5474245418f186b4ed Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 11:53:40 -0800 Subject: [PATCH 3/3] fix(mux): check a rendition write against the whole catalog extension The entry-alone check could not see an extension whose serializer depends on its combined entries. Seed the scratch catalog with the real extension, which built-in configs leave untouched, so they stay linear. Also name the sgpd exception to the tenfold parse bound. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/catalog/tracks.rs | 54 ++++++++++++++++++++++++++-- rs/moq-mux/src/container/fmp4/mod.rs | 7 ++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/rs/moq-mux/src/catalog/tracks.rs b/rs/moq-mux/src/catalog/tracks.rs index bccc3cab554..844b99adab5 100644 --- a/rs/moq-mux/src/catalog/tracks.rs +++ b/rs/moq-mux/src/catalog/tracks.rs @@ -387,10 +387,13 @@ fn quantize(published: Option, measured: Option, period: Dur /// Write `config` under `name`, refusing it before any change if it redefines the rendition's /// identity or won't serialize, so the reserved snapshot never retains a bad edit. /// -/// The entry is checked in a catalog holding only it: the rest was checked when it was written, and -/// cloning or serializing the whole catalog would make declaring N renditions cost N². +/// The entry is checked in a catalog holding only it and the extension: the media and data sections +/// serialize each entry on its own, and the rest was checked when it was written, so cloning or +/// serializing them would make declaring N renditions cost N². The extension is kept whole because +/// its serializer may depend on its combined entries. fn write>(catalog: &mut Catalog, name: &str, config: C) -> crate::Result<()> { let mut alone = Catalog::default(); + alone.ext = catalog.ext.clone(); config.clone().insert(&mut alone, name); check_identity(catalog, &alone, name)?; serde_json::to_writer(std::io::sink(), &alone).map_err(moq_json::Error::from)?; @@ -1470,6 +1473,17 @@ mod tests { exploding: BTreeMap, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] stubborn: BTreeMap, + #[serde(default, skip_serializing_if = "BTreeMap::is_empty", serialize_with = "at_most_one")] + solo: BTreeMap, + } + + /// Serialize a section that refuses to hold more than one entry, so whether it serializes + /// depends on every entry, not just the one being written. + fn at_most_one(solo: &BTreeMap, serializer: S) -> Result { + if solo.len() > 1 { + return Err(serde::ser::Error::custom("only one solo entry")); + } + solo.serialize(serializer) } impl CatalogExt for TelemetryExt {} @@ -1626,6 +1640,42 @@ mod tests { .expect("a stranded entry would refuse this forever"); } + /// A config whose section serializes only while it holds a single entry. + #[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq)] + struct Solo; + + impl RenditionConfig for Solo { + fn insert(self, catalog: &mut Catalog, name: &str) { + catalog.ext.solo.insert(name.to_string(), self); + } + fn get_mut<'a>(catalog: &'a mut Catalog, name: &str) -> Option<&'a mut Self> { + catalog.ext.solo.get_mut(name) + } + fn remove(catalog: &mut Catalog, name: &str) { + catalog.ext.solo.remove(name); + } + } + + /// An entry that serializes alone but not beside the extension's other entries is refused + /// before the catalog retains it. + #[test] + fn an_entry_that_breaks_the_extension_is_refused() { + let (_broadcast, catalog) = produce(); + let reserved = catalog.reserve(); + let mut first = reserved.init::("a").unwrap(); + let mut second = reserved.init::("b").unwrap(); + + first.set(Solo).unwrap(); + assert!( + matches!(second.set(Solo), Err(crate::Error::Json(_))), + "the second entry makes the extension unserializable" + ); + + let snapshot = catalog.snapshot(); + assert_eq!(snapshot.ext.solo.keys().collect::>(), ["a"]); + serde_json::to_string(&snapshot).expect("the refused edit left the catalog serializable"); + } + /// A custom kind gets the same detection and drop-removal as video/audio. #[test] fn detects_and_advertises() { diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 360cdf4133e..f2a19c68de4 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -37,8 +37,11 @@ pub const MAX_HELD_BYTES: usize = moq_net::group::MAX_CACHE_BYTES as usize; /// Metadata parses only once its last byte arrives, so a larger declaration is refused at its /// header rather than retaining later input (across HLS segments, too) until it completes. /// Parsing expands a body up to tenfold (a 4-byte `trun` entry becomes a 40-byte -/// [`TrunEntry`](mp4_atom::TrunEntry)), so this keeps a parse within [`MAX_HELD_BYTES`]. Real -/// fMP4 metadata is kilobytes. +/// [`TrunEntry`](mp4_atom::TrunEntry)), so this keeps a parse within [`MAX_HELD_BYTES`]. The +/// exception is a box whose entries cost no input, such as a version 0 `sgpd` in mp4-atom 0.17.0, +/// which parses past it until mp4-atom bounds the count +/// ([kixelated/mp4-atom#264](https://github.com/kixelated/mp4-atom/pull/264)). Real fMP4 metadata +/// is kilobytes. pub const MAX_METADATA_BYTES: usize = MAX_HELD_BYTES / 16; /// The most samples [`Import`] plans for one `moof`.