forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

malformed_no_panic.rs113 lines, 3.6 KiBHistoryRawDownload
1//! Robustness witness: malformed VP8 input must produce an error, never a
2//! panic. Every IVF fixture is decoded truncated at 10 positions and with 50
3//! seeded single-byte corruptions; each decode runs under `catch_unwind` so a
4//! failure names the file and the damage that caused it.
5
6// Only `fixture_dir` is used here; the IVF parser serves the other test crates.
7#[allow(dead_code)]
8#[path = "it/common.rs"]
9mod ivf;
10
11use std::panic::{AssertUnwindSafe, catch_unwind};
12
13use ec_vp8::stream::decode_stream;
14
15/// The 12 IVF fixtures of the witness set (same list as `stream_exact.rs`).
16const FIXTURES: &[&str] = &[
17 "kf-16x16-black-q0.ivf",
18 "kf-32x16-q35.ivf",
19 "kf-76x52-q30.ivf",
20 "kf-96x80-q63.ivf",
21 "kf-160x96-q20.ivf",
22 "kf-160x96-q40.ivf",
23 "kf-172x144-q25.ivf",
24 "gop-96x80-v1.ivf",
25 "gop-160x96.ivf",
26 "mparts-160x96.ivf",
27 "altref-160x96.ivf",
28 "clip-obs-320x192.ivf",
29];
30
31const TRUNCATIONS: usize = 10;
32const CORRUPTIONS: usize = 50;
33const SEED: u64 = 0x5EED_0F08;
34
35/// splitmix64: a small, fixed-seed generator so every run sees the same damage.
36struct Rng(u64);
37
38impl Rng {
39 fn next(&mut self) -> u64 {
40 self.0 = self.0.wrapping_add(0x9e37_79b9_7f4a_7c15);
41 let mut z = self.0;
42 z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
43 z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
44 z ^ (z >> 31)
45 }
46
47 fn below(&mut self, n: usize) -> usize {
48 (self.next() % n as u64) as usize
49 }
50}
51
52/// Decodes `data` and fails the test if the decoder panics. Errors are fine.
53fn decode_must_not_panic(data: &[u8], what: &str) {
54 let outcome = catch_unwind(AssertUnwindSafe(|| {
55 let _ = decode_stream(data);
56 }));
57 assert!(outcome.is_ok(), "decoder panicked on {what}");
58}
59
60#[test]
61fn truncated_and_corrupted_fixtures_never_panic() {
62 let Some(dir) = ivf::fixture_dir() else {
63 panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh");
64 };
65 // Silence the default panic hook while probing; failures are reported
66 // by the assertion above, which names the file and the damage.
67 let hook = std::panic::take_hook();
68 std::panic::set_hook(Box::new(|_| {}));
69
70 let mut rng = Rng(SEED);
71 let mut failures = Vec::new();
72 for name in FIXTURES {
73 let bytes = std::fs::read(dir.join(name)).unwrap();
74 assert!(bytes.len() > 32, "{name}: fixture too small");
75
76 for k in 1..=TRUNCATIONS {
77 let cut = bytes.len() * k / (TRUNCATIONS + 1);
78 let what = format!("{name} truncated to {cut} of {} bytes", bytes.len());
79 if catch_unwind(AssertUnwindSafe(|| {
80 let _ = decode_stream(&bytes[..cut]);
81 }))
82 .is_err()
83 {
84 failures.push(what);
85 }
86 }
87
88 for i in 0..CORRUPTIONS {
89 let at = rng.below(bytes.len());
90 let flip = (rng.below(255) + 1) as u8;
91 let mut damaged = bytes.clone();
92 damaged[at] ^= flip;
93 let what = format!("{name} corruption {i}: byte {at} ^= {flip:#04x}");
94 if catch_unwind(AssertUnwindSafe(|| {
95 let _ = decode_stream(&damaged);
96 }))
97 .is_err()
98 {
99 failures.push(what);
100 }
101 }
102 }
103
104 std::panic::set_hook(hook);
105 assert!(failures.is_empty(), "decoder panicked on {} inputs: {failures:#?}", failures.len());
106}
107
108#[test]
109fn decode_must_not_panic_helper_accepts_errors() {
110 // Garbage that no VP8 decoder can accept; the contract is only "no panic".
111 decode_must_not_panic(&[0xff; 64], "all-0xff buffer");
112 decode_must_not_panic(&[], "empty buffer");
113}