forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

robust.rs138 lines, 4.2 KiBHistoryRawDownload
1//! Robustness: truncated, corrupted and hostile frames must never panic, and refusals must name
2//! what they refused.
3
4mod common;
5use common::*;
6use ec_core::Error;
7
8struct Rng(u64);
9impl Rng {
10 fn next(&mut self) -> u64 {
11 self.0 ^= self.0 << 13;
12 self.0 ^= self.0 >> 7;
13 self.0 ^= self.0 << 17;
14 self.0
15 }
16 fn below(&mut self, n: usize) -> usize {
17 (self.next() % n.max(1) as u64) as usize
18 }
19}
20
21/// Small frames: progressive 422, interlaced 422, 4444 with alpha.
22fn frames() -> Option<Vec<Vec<u8>>> {
23 static CACHE: std::sync::OnceLock<Option<Vec<Vec<u8>>>> = std::sync::OnceLock::new();
24 CACHE.get_or_init(make_frames).clone()
25}
26
27fn make_frames() -> Option<Vec<Vec<u8>>> {
28 if !ffmpeg_or_skip() {
29 return None;
30 }
31 let dir = scratch("robust");
32 let mut out = Vec::new();
33 for (i, (prof, fmt, extra)) in [
34 ("3", "yuv422p10le", ""),
35 ("1", "yuv422p10le", "+ildct"),
36 ("4", "yuva444p10le", ""),
37 ]
38 .into_iter()
39 .enumerate()
40 {
41 let mov = dir.join(format!("r{i}.mov"));
42 let mut a = vec![
43 "-f", "lavfi", "-i", "testsrc2=size=100x70:rate=25,noise=alls=20:allf=t", "-frames:v", "1",
44 "-c:v", "prores_ks", "-profile", prof, "-pix_fmt", fmt,
45 ];
46 if !extra.is_empty() {
47 a.extend(["-flags", extra]);
48 }
49 a.push(mov.to_str().unwrap());
50 assert!(run(&a));
51 out.push(packets(&std::fs::read(&mov).unwrap()).remove(0));
52 }
53 let _ = std::fs::remove_dir_all(dir);
54 Some(out)
55}
56
57#[test]
58fn truncations_never_panic() {
59 let Some(fs) = frames() else { return };
60 for f in &fs {
61 let mut d = decoder(2);
62 assert!(d.decode(f).is_ok());
63 for n in (0..f.len()).step_by(7).chain(0..40) {
64 let _ = d.decode(&f[..n.min(f.len())]);
65 }
66 }
67}
68
69#[test]
70fn corruptions_never_panic() {
71 let Some(fs) = frames() else { return };
72 let mut rng = Rng(0x9E3779B97F4A7C15);
73 for f in &fs {
74 let mut d = decoder(3);
75 for round in 0..400 {
76 let mut g = f.clone();
77 let flips = 1 + rng.below(8);
78 for _ in 0..flips {
79 // Half the hits land in the headers, where the structure lives.
80 let at = if round % 2 == 0 { rng.below(g.len().min(200)) } else { rng.below(g.len()) };
81 g[at] ^= 1 << rng.below(8);
82 }
83 let _ = d.decode(&g);
84 }
85 }
86}
87
88#[test]
89fn garbage_is_refused_by_name() {
90 let mut d = decoder(1);
91 let mut rng = Rng(7);
92 for len in [0usize, 1, 7, 8, 27, 28, 200, 5000] {
93 let g: Vec<u8> = (0..len).map(|_| rng.next() as u8).collect();
94 assert!(d.decode(&g).is_err());
95 }
96 // Right magic, nonsense after it.
97 let mut g = vec![0u8; 300];
98 g[..4].copy_from_slice(&300u32.to_be_bytes());
99 g[4..8].copy_from_slice(b"icpf");
100 match d.decode(&g) {
101 Err(Error::Unsupported { .. } | Error::Corrupt { .. }) => {}
102 other => panic!("expected a named refusal, got {:?}", other.map(|f| f.width)),
103 }
104}
105
106#[test]
107fn hostile_dimensions_are_bounded() {
108 let Some(fs) = frames() else { return };
109 let mut g = fs[0].clone();
110 // width/height at frame-header bytes 8..12 (after size+icpf: offset 8+8).
111 g[16..20].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
112 let t = std::time::Instant::now();
113 assert!(decoder(1).decode(&g).is_err());
114 assert!(t.elapsed().as_secs() < 5);
115 g[16..20].copy_from_slice(&[0, 0, 0, 0]);
116 assert!(decoder(1).decode(&g).is_err());
117}
118
119#[test]
120fn wrong_codec_is_unsupported() {
121 use ec_core::registry::{CodecId, CodecParameters};
122 match ec_prores::ProResDecoder::new(CodecParameters::new(CodecId::H264)) {
123 Err(Error::Unsupported { .. }) => {}
124 _ => panic!("expected Unsupported"),
125 }
126}
127
128/// `EC_SEED_DIR=crates/ec-prores/fuzz/corpus/decode_prores cargo test -p ec-prores --test robust -- --ignored dump_seeds`
129#[test]
130#[ignore]
131fn dump_seeds() {
132 let dir = std::env::var("EC_SEED_DIR").expect("EC_SEED_DIR");
133 std::fs::create_dir_all(&dir).unwrap();
134 let Some(fs) = frames() else { return };
135 for (i, f) in fs.iter().enumerate() {
136 std::fs::write(format!("{dir}/seed{i}.bin"), f).unwrap();
137 }
138}