forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

decode_mpeg2.rs54 lines, 1.7 KiBHistoryRawDownload
1#![no_main]
2
3// Untrusted entry: arbitrary bytes fed to the MPEG-1/2 video byte-stream API
4// in random-sized chunks, then flushed and drained. Errors are expected for
5// malformed input; only panics, hangs and OOM count.
6
7use ec_core::registry::{CodecId, CodecParameters};
8use ec_mpeg2::Mpeg2Decoder;
9use libfuzzer_sys::fuzz_target;
10
11/// Harness cap: pictures larger than this are not fed further (the decoder
12/// itself bounds allocation at 8192x4352; the cap keeps fuzz runs fast).
13const MAX_DIM: u32 = 256;
14
15fn drain(dec: &mut Mpeg2Decoder) {
16 while let Some(p) = dec.pop_picture() {
17 std::hint::black_box(p);
18 }
19}
20
21fuzz_target!(|data: &[u8]| {
22 let Ok(mut dec) = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)) else {
23 return;
24 };
25 // Chunk sizes come from a xorshift seeded by the input length and head bytes.
26 let mut s = 0x9E37_79B9_7F4A_7C15u64 ^ data.len() as u64;
27 for &b in data.iter().take(8) {
28 s = s.rotate_left(8) ^ u64::from(b);
29 }
30 let mut rest = data;
31 while !rest.is_empty() {
32 s ^= s << 13;
33 s ^= s >> 7;
34 s ^= s << 17;
35 let n = match s % 4 {
36 0 => 1 + (s >> 8) as usize % 8,
37 1 => 1 + (s >> 8) as usize % 64,
38 2 => 1 + (s >> 8) as usize % 1024,
39 _ => rest.len(),
40 }
41 .min(rest.len());
42 let (chunk, tail) = rest.split_at(n);
43 rest = tail;
44 let _ = dec.send_bytes(chunk, None);
45 drain(&mut dec);
46 if let Some(seq) = dec.sequence() {
47 if seq.width > MAX_DIM || seq.height > MAX_DIM {
48 break;
49 }
50 }
51 }
52 let _ = dec.flush_stream();
53 drain(&mut dec);
54});