forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

no_panic.rs96 lines, 3.1 KiBHistoryRawDownload
1//! No-panic floor for the decoder on malformed input.
2//!
3//! For each of the first 10 JVT conformance streams (the same name-sorted walk
4//! `conformance.rs` uses), truncate at 8 positions and flip one byte at 30
5//! fixed-seed positions. Every mutant is decoded end to end. An `Err` is fine;
6//! a panic fails the test.
7//!
8//! Fixture-gated like the other conformance tests: without `fixtures/` the test
9//! skips loudly, or fails when `EC_REQUIRE_FIXTURES` is set.
10
11use std::path::{Path, PathBuf};
12
13use ec_h264::{Decoder, NalOutcome};
14
15use super::common::find_stream;
16
17const STREAMS: usize = 10;
18const TRUNCATIONS: usize = 8;
19const CORRUPTIONS: usize = 30;
20const SEED: u32 = 0x9E37_79B9;
21
22fn vectors_dir() -> PathBuf {
23 Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/vectors/h264-jvt")
24}
25
26/// Every vector directory, in name order (the order `conformance.rs` walks).
27fn vector_names(base: &Path) -> Vec<String> {
28 let mut names: Vec<String> = std::fs::read_dir(base)
29 .into_iter()
30 .flatten()
31 .flatten()
32 .filter(|e| e.path().is_dir())
33 .map(|e| e.file_name().to_string_lossy().into_owned())
34 .collect();
35 names.sort();
36 names
37}
38
39/// Decode a whole byte stream, ignoring every error. Only a panic matters here.
40fn decode_everything(bytes: &[u8]) {
41 let mut dec = Decoder::new();
42 for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
43 if let Ok(NalOutcome::PictureBoundary) = dec.push_nal(nal) {
44 let _ = dec.end_picture();
45 let _ = dec.push_nal(nal);
46 }
47 while dec.next_frame().is_some() {}
48 }
49 let _ = dec.flush();
50 while dec.next_frame().is_some() {}
51}
52
53#[test]
54fn truncated_and_corrupted_conformance_streams_never_panic() {
55 let base = vectors_dir();
56 if !base.exists() {
57 assert!(
58 std::env::var_os("EC_REQUIRE_FIXTURES").is_none(),
59 "JVT vectors missing at {} — run scripts/link-fixtures.sh or scripts/fetch-vectors.sh",
60 base.display()
61 );
62 eprintln!("SKIP JVT vectors missing at {}", base.display());
63 return;
64 }
65 let mut streams = 0usize;
66 for name in vector_names(&base).into_iter().take(STREAMS) {
67 let Some(stream) = find_stream(&base.join(&name)) else {
68 continue;
69 };
70 let bytes = std::fs::read(&stream).expect("read conformance stream");
71 if bytes.is_empty() {
72 continue;
73 }
74 streams += 1;
75
76 for k in 1..=TRUNCATIONS {
77 decode_everything(&bytes[..bytes.len() * k / (TRUNCATIONS + 1)]);
78 }
79
80 let mut seed = SEED;
81 for _ in 0..CORRUPTIONS {
82 seed ^= seed << 13;
83 seed ^= seed >> 17;
84 seed ^= seed << 5;
85 let pos = seed as usize % bytes.len();
86 // Never xor by zero: that would leave the stream unchanged.
87 let flip = ((seed >> 24) as u8) | 1;
88 let mut m = bytes.clone();
89 m[pos] ^= flip;
90 decode_everything(&m);
91 }
92 }
93 eprintln!(
94 "no-panic sweep: {streams} streams, {TRUNCATIONS} truncations + {CORRUPTIONS} corruptions each"
95 );
96}