mirror of
https://github.com/storytold/filmcraft.git
synced 2026-10-24 01:58:19 -06:00
* Tests: one integration-test binary per crate Each crate with several tests/*.rs files keeps them as modules of tests/it/, so cargo links one test executable per crate instead of one per file. Files that change process-wide state or time a workload stay binaries of their own. * Profiles: line tables for workspace crates, no debug info for dependencies * Windows ARM64: nightly and on release pushes instead of every push to main * xtask ci: clippy and tests in the dev profile The release profile's thin LTO and single codegen unit made linking the test binaries take most of the gate. The codec and audio crates stay at opt-level 3 in the dev profile, so their tests run at release speed. * xtask ci: nextest, doctests, no examples, one wasm check Tests run with cargo nextest when it is installed (cargo test otherwise) and build with --lib --bins --tests, so examples aren't linked; doctests run separately. The wasm check keeps going past the first failing crate. VP8 fixture generation locks across processes, since nextest runs each test in its own. * VP9 synthetic fixtures: write aside and rename A test in another process could read a fixture ffmpeg was still writing. * Fuzz tests: a third of each sweep by default, all of it with FILMCRAFT_FUZZ_ITERS Every seed, mutation kind and thread count still runs. Header bytes each get one overwrite value and two bit flips in rotation, so every value and bit index still occurs across the region. FILMCRAFT_FUZZ_ITERS=N runs the whole sweep plus N random rounds per seed, as before. * Platform fallback tests: one failure point per hybrid path The failure-anywhere test injected a failure at four points around every restart point; the hybrid's paths differ only in the first GOP, around the second and last restart points, at the end and never. The replay test needs the first two GOPs. * Export tests: shorter ranges with the same checks The bitrate test measures 0.5–1.5 s, where each preset reaches its own target (on 0–3 s the simple demo left two presets at the same rate). The every-preset test exports three frames per preset. * Audio tests: effects in parallel, a table-driven FFT for the Opus quality metric The extreme-settings test keeps both settings and the 40 s tail for every effect, one thread each. The Opus quality metric's FFT builds its twiddles once and allocates nothing per transform (same algorithm and summation order). * CI: dev-profile gate with nextest xtask ci no longer builds with --release, so the release-profile overrides go; nextest is installed for the test step. * CI: apt packages through the cached install-apt-packages action * Review fixes: fuzz tests with a panic hook in their own binaries, loud fixture lock The fuzz tests of av1, h264, hevc, mpeg2v, vp8 and vp9 count panics through a process-wide hook, so a panic in another test of the same process would fail them; they are binaries of their own again. The VP8 fixture lock fails the test instead of skipping it silently, and the full-sweep doc comments say how many truncation points the full run samples. * Fuzz tests: rotate the default subset per run (FILMCRAFT_FUZZ_SEED replays one) * platform tests: decode-switch tests in their own binaries, one NVENC lock
195 lines
7.7 KiB
Rust
195 lines
7.7 KiB
Rust
//! Hostile-input robustness: header-region sweeps (SPS/PPS/first slice headers), truncation and
|
|
//! random mutation of small libx264 streams must never panic or run away. Skips without ffmpeg.
|
|
|
|
#[path = "it/common/mod.rs"]
|
|
mod common;
|
|
|
|
use std::panic::{AssertUnwindSafe, catch_unwind};
|
|
use std::process::{Command, Stdio};
|
|
|
|
use filmcraft_h264::Decoder;
|
|
|
|
struct Rng(u64);
|
|
impl Rng {
|
|
fn next(&mut self) -> u64 {
|
|
self.0 ^= self.0 << 13;
|
|
self.0 ^= self.0 >> 7;
|
|
self.0 ^= self.0 << 17;
|
|
self.0
|
|
}
|
|
fn below(&mut self, n: usize) -> usize {
|
|
(self.next() % n.max(1) as u64) as usize
|
|
}
|
|
}
|
|
|
|
/// (name, extra x264 args, pix_fmt)
|
|
const SEEDS: &[(&str, &str, &str)] = &[
|
|
("h264_fz_cabac_b", "-profile:v high -bf 2 -g 8 -x264-params cabac=1:weightp=2:8x8dct=1", "yuv420p"),
|
|
("h264_fz_cavlc", "-profile:v baseline -g 6 -x264-params cabac=0", "yuv420p"),
|
|
("h264_fz_interlaced", "-profile:v high -flags +ildct+ilme -bf 1 -g 6 -x264-params tff=1:interlaced=1", "yuv420p"),
|
|
("h264_fz_10bit_422", "-profile:v high422 -bf 1 -g 6", "yuv422p10le"),
|
|
("h264_fz_multislice", "-profile:v main -g 6 -x264-params slices=3:cabac=1", "yuv420p"),
|
|
];
|
|
|
|
fn make(ff: &std::path::Path, name: &str, args: &str, pix: &str) -> Option<Vec<u8>> {
|
|
let out = filmcraft_testkit::fixtures_dir("h264").join(format!("{name}.h264"));
|
|
let p = filmcraft_testkit::fixtures::generate(&out, |tmp| {
|
|
let a: Vec<&str> = args.split_whitespace().collect();
|
|
matches!(
|
|
Command::new(ff)
|
|
.args(["-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x48:rate=25", "-frames:v", "7", "-pix_fmt", pix, "-c:v", "libx264"])
|
|
.args(a)
|
|
.args(["-f", "h264"])
|
|
.arg(tmp)
|
|
.stdin(Stdio::null())
|
|
.status(),
|
|
Ok(s) if s.success()
|
|
)
|
|
})?;
|
|
std::fs::read(p).ok()
|
|
}
|
|
|
|
static PANICS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
|
|
|
/// Decoder worker threads catch panics and turn them into errors; count them so they still fail the test.
|
|
fn count_panics() {
|
|
static ONCE: std::sync::Once = std::sync::Once::new();
|
|
ONCE.call_once(|| {
|
|
std::panic::set_hook(Box::new(|info| {
|
|
PANICS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
|
eprintln!("PANIC: {info}");
|
|
}));
|
|
});
|
|
}
|
|
|
|
fn run_all(d: &[u8], label: &str) {
|
|
filmcraft_testkit::set_progress(label);
|
|
count_panics();
|
|
let before = PANICS.load(std::sync::atomic::Ordering::SeqCst);
|
|
let res = catch_unwind(AssertUnwindSafe(|| {
|
|
for threads in [1, 3] {
|
|
let mut dec = Decoder::with_threads(threads);
|
|
for au in common::split_access_units(d) {
|
|
let _ = dec.decode(au, 0);
|
|
}
|
|
let _ = dec.decode(d, 1);
|
|
let _ = dec.flush();
|
|
let _ = dec.take_error();
|
|
}
|
|
}));
|
|
assert!(res.is_ok() && PANICS.load(std::sync::atomic::Ordering::SeqCst) == before, "panic: {label}");
|
|
}
|
|
|
|
/// Seconds the whole sweep may take before it counts as a hang (override: FILMCRAFT_HOSTILE_TIMEOUT_SECS).
|
|
const DEADLINE_SECS: u64 = 600;
|
|
|
|
/// CI runs a rotating share of every part of the sweep; `FILMCRAFT_FUZZ_ITERS=400` runs all of it (see [`Depth`]).
|
|
#[test]
|
|
fn hostile_streams_never_panic() {
|
|
filmcraft_testkit::with_deadline(DEADLINE_SECS, sweep);
|
|
}
|
|
|
|
fn sweep() {
|
|
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("h264 fuzz") else { return };
|
|
let depth = Depth::from_env();
|
|
for &(name, args, pix) in SEEDS {
|
|
if !filmcraft_testkit::ffmpeg_has_encoder(&ff, "libx264") {
|
|
filmcraft_testkit::skip("h264 fuzz", &format!("this ffmpeg has no libx264 encoder (seed {name})"));
|
|
continue;
|
|
}
|
|
let Some(seed) = make(&ff, name, args, pix) else {
|
|
panic!("ffmpeg has libx264 but could not encode the {name} seed");
|
|
};
|
|
for n in depth.truncations(seed.len()) {
|
|
run_all(&seed[..n], &format!("{name} truncate {n}"));
|
|
}
|
|
// SPS/PPS/first slice header region: every byte to a few values, every bit
|
|
for p in 0..seed.len().min(120) {
|
|
for (i, v) in VALUES.into_iter().enumerate() {
|
|
if !depth.value(p, i) {
|
|
continue;
|
|
}
|
|
let mut d = seed.clone();
|
|
d[p] = v;
|
|
run_all(&d, &format!("{name} byte {p}={v:#x}"));
|
|
}
|
|
for b in 0..8 {
|
|
if !depth.bit(p, b) {
|
|
continue;
|
|
}
|
|
let mut d = seed.clone();
|
|
d[p] ^= 1 << b;
|
|
run_all(&d, &format!("{name} flip {p}.{b}"));
|
|
}
|
|
}
|
|
let mut rng = Rng(0x9e3779b97f4a7c15 ^ name.len() as u64);
|
|
// Skipped rounds still draw their mutations, so round i matches round i of a full run.
|
|
for i in 0..depth.rounds {
|
|
let mut d = seed.clone();
|
|
for _ in 0..1 + rng.below(6) {
|
|
let p = rng.below(d.len());
|
|
if rng.below(2) == 0 {
|
|
d[p] ^= 1 << rng.below(8);
|
|
} else {
|
|
d[p] = rng.next() as u8;
|
|
}
|
|
}
|
|
if depth.round(i) {
|
|
run_all(&d, &format!("{name} random {i}"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const VALUES: [u8; 5] = [0, 0xff, 0x80, 0x01, 0x7f];
|
|
|
|
/// How much of the sweep runs. `FILMCRAFT_FUZZ_ITERS=N` runs all of it (about 300 truncation
|
|
/// points spread over each seed, every header byte, overwrite value and bit flip) plus N random
|
|
/// rounds per seed: 400 is the full depth, more hunts longer. Unset, CI runs a third of the
|
|
/// truncation points and of the full depth's random rounds and, for each header byte, one value
|
|
/// and two bits. A rotation offset shifts which share runs (`FILMCRAFT_FUZZ_SEED`, else the UTC
|
|
/// day number), so every case of the full sweep runs within a few days, and a random round's
|
|
/// mutations match the same round of a full run.
|
|
struct Depth {
|
|
full: bool,
|
|
truncations: usize,
|
|
rounds: usize,
|
|
rotation: usize,
|
|
}
|
|
|
|
impl Depth {
|
|
fn from_env() -> Self {
|
|
match std::env::var("FILMCRAFT_FUZZ_ITERS").ok().and_then(|s| s.parse().ok()) {
|
|
Some(rounds) => Depth { full: true, truncations: 300, rounds, rotation: 0 },
|
|
None => {
|
|
let rotation = rotation();
|
|
eprintln!("fuzz subset rotation {rotation} (FILMCRAFT_FUZZ_SEED={rotation} replays it; FILMCRAFT_FUZZ_ITERS=400 runs everything)");
|
|
Depth { full: false, truncations: 300, rounds: 400, rotation }
|
|
}
|
|
}
|
|
}
|
|
/// Truncation lengths for a `len`-byte input: every point of the full sweep, or every third one.
|
|
fn truncations(&self, len: usize) -> std::iter::StepBy<std::ops::Range<usize>> {
|
|
let stride = (len / self.truncations).max(1);
|
|
let (start, step) = if self.full { (0, stride) } else { ((self.rotation % 3) * stride, 3 * stride) };
|
|
(start..len).step_by(step)
|
|
}
|
|
fn value(&self, p: usize, i: usize) -> bool {
|
|
self.full || i == (p + self.rotation % VALUES.len()) % VALUES.len()
|
|
}
|
|
fn bit(&self, p: usize, b: usize) -> bool {
|
|
self.full || (b + p + self.rotation % 4).is_multiple_of(4)
|
|
}
|
|
fn round(&self, i: usize) -> bool {
|
|
self.full || i % 3 == self.rotation % 3
|
|
}
|
|
}
|
|
|
|
/// The default subset's rotation offset: `FILMCRAFT_FUZZ_SEED`, else days since the Unix epoch (UTC).
|
|
fn rotation() -> usize {
|
|
std::env::var("FILMCRAFT_FUZZ_SEED").ok().and_then(|s| s.parse().ok()).unwrap_or_else(|| {
|
|
let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs());
|
|
usize::try_from(secs / 86_400).unwrap_or(0)
|
|
})
|
|
}
|