forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

decode_stream.rs62 lines, 2.0 KiBHistoryRawDownload
1#![no_main]
2
3// Untrusted entry: a raw AV1 OBU stream through the full decoder
4// (ec_av1::stream::decode_stream_with_threads). Errors are expected for malformed
5// input; only panics, hangs and OOM count.
6//
7// Caps, so one iteration stays in the milliseconds and the fuzzer cannot OOM:
8// - a sequence header larger than MAX_WIDTH x MAX_HEIGHT is not decoded at all;
9// - at most MAX_FRAMES frame OBUs are decoded: the stream is cut before the next one;
10// - the decoder runs on one thread.
11
12use ec_av1::stream::decode_stream_with_threads;
13use ec_av1_syntax::{Av1Parser, ObuKind};
14use libfuzzer_sys::fuzz_target;
15
16/// Largest frame decoded.
17const MAX_WIDTH: u32 = 64;
18const MAX_HEIGHT: u32 = 64;
19/// Frame OBUs decoded per input.
20const MAX_FRAMES: usize = 8;
21
22/// Pre-scan the OBUs: `None` when a sequence header is over the size limit, otherwise the
23/// byte length of the prefix that holds at most MAX_FRAMES frames.
24fn capped_len(data: &[u8]) -> Option<usize> {
25 let mut parser = Av1Parser::new();
26 let mut pos = 0usize;
27 let mut frames = 0usize;
28 while let Some(rest) = data.get(pos..).filter(|r| !r.is_empty()) {
29 let Ok(obu) = parser.parse_obu(rest) else {
30 break;
31 };
32 if obu.total_size == 0 {
33 break;
34 }
35 match &obu.kind {
36 ObuKind::SequenceHeader(seq) => {
37 if seq.max_frame_width > MAX_WIDTH || seq.max_frame_height > MAX_HEIGHT {
38 return None;
39 }
40 }
41 ObuKind::Frame(..) | ObuKind::FrameHeader(..) => {
42 frames += 1;
43 if frames > MAX_FRAMES {
44 return Some(pos);
45 }
46 }
47 _ => {}
48 }
49 pos += obu.total_size;
50 }
51 Some(data.len())
52}
53
54fuzz_target!(|data: &[u8]| {
55 let Some(len) = capped_len(data) else {
56 return;
57 };
58 let _ = decode_stream_with_threads(&data[..len], 1, |picture, _index, _shown| {
59 std::hint::black_box(picture);
60 Ok(())
61 });
62});