Files
filmcraft/crates/mpeg2v/tests/hostile_dims.rs
T
Logan JenningsandClaude Opus 4.7 cb2ac66c3b Harden AV1 and MPEG-2 decoders against hostile input; add mutation-fuzz tests for the video codecs
- av1: a 4:2:2 stream (profile bit flipped) could reach blocks with no chroma size (BLOCK_INVALID) and
  index MAX_TX_SIZE_RECT out of bounds; reject them with an error in decode_block.
- av1: frame size accepted up to 65536x65536 (gigabytes of buffers from a few header bytes); cap at 16384 per
  side and 2^27 samples.
- mpeg2v: 16383x16383 4:4:4 sequences (gigabytes per picture) are refused; area capped at 2^25.
- fuzz_hostile tests (truncation, header byte/bit sweeps, random mutation) for h264, hevc, vp8, vp9, av1,
  mpeg2v, apv, prores and dnx; ffmpeg-based ones skip without ffmpeg.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-10-13 13:40:36 -07:00

74 lines
2.1 KiB
Rust

//! A 14-byte sequence header must not make the decoder size its frame buffers at 16383 x 16383
//! (gigabytes per picture). Hand-built, no ffmpeg.
use filmcraft_mpeg2v::Decoder;
#[derive(Default)]
struct Bits {
bytes: Vec<u8>,
n: usize,
}
impl Bits {
fn put(&mut self, value: u32, bits: usize) {
for i in (0..bits).rev() {
if self.n.is_multiple_of(8) {
self.bytes.push(0);
}
if (value >> i) & 1 == 1
&& let Some(b) = self.bytes.last_mut()
{
*b |= 0x80 >> (self.n % 8);
}
self.n += 1;
}
}
}
/// Sequence header plus sequence extension for a `w` x `h` 4:4:4 stream.
fn sequence(w: u32, h: u32, chroma_format: u32) -> Vec<u8> {
let mut s = Bits::default();
s.put(w & 0xfff, 12);
s.put(h & 0xfff, 12);
s.put(1, 4); // aspect_ratio_information
s.put(4, 4); // frame_rate_code
s.put(1000, 18); // bit_rate_value
s.put(1, 1); // marker
s.put(100, 10); // vbv_buffer_size_value
s.put(0, 3); // constrained_parameters_flag, load_intra, load_non_intra
let mut e = Bits::default();
e.put(1, 4); // sequence_extension
e.put(0x48, 8); // profile_and_level_indication
e.put(1, 1); // progressive_sequence
e.put(chroma_format, 2);
e.put(w >> 12, 2);
e.put(h >> 12, 2);
e.put(0, 12); // bit_rate_extension
e.put(1, 1); // marker
e.put(0, 8 + 1 + 2 + 5); // vbv_buffer_size_extension, low_delay, frame_rate_extension_n/d
let mut out = vec![0, 0, 1, 0xb3];
out.extend(s.bytes);
out.extend([0, 0, 1, 0xb5]);
out.extend(e.bytes);
out
}
fn accepted(w: u32, h: u32, chroma_format: u32) -> bool {
let mut dec = Decoder::new();
let _ = dec.decode(&sequence(w, h, chroma_format), 0);
dec.sequence().is_some()
}
#[test]
fn ordinary_sequences_are_accepted() {
assert!(accepted(1920, 1080, 1));
assert!(accepted(7680, 4320, 3));
}
#[test]
fn gigantic_sequences_are_refused() {
assert!(!accepted(16383, 16383, 3));
assert!(!accepted(16383, 16383, 1));
assert!(!accepted(8192, 8192, 1));
}