From 3aa53dd6af0eb8d5c53d6717e34e8fdee8ace5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 3 Oct 2026 06:16:04 +0300 Subject: [PATCH] h26xpinloud: the h26x test pins fail loudly on an Ok-but-short write ec-av1 already owns this class (dumpio::LoudDump: declare the expected byte count, write, then stat the file's own length). These two crates had no shared helper, so four sites called fs::write directly: ec-h264/tests/encode.rs:475 ffmpeg_decode -> ffmpeg -i ec-h264/tests/encode.rs:1068 x264_encode_qp -> ffmpeg -i ec-h265/tests/encode.rs:289 ffmpeg_decode -> ffmpeg -i ec-h265/tests/encode.rs:341 x265_encode_qp -> ffmpeg -i Each was fs::write(&path, bytes).ok()?. That closes the Err path; the Ok-but-short path stayed open, and it is the one that hands another tool a silently truncated stream, which then reads as a wrong pixel or a shifted PSNR number rather than a write failure. Measured: 1234 bytes written through a path that looks like a stream file but whose filesystem accepts every byte and stores none returns Ok, and the file on disk is 0 bytes. Each site now goes through a per-crate pin_loud: write, then check the on-disk length against the same bytes.len() the writer used, and fail with the path, both counts, and the OS error. The helper returns the failure LINE, not a panic, because these callers bail (or, at one h264 site, turn None into a SKIP) today and that semantics is kept -- the shortfall is printed in full and the existing bail stands. Only write_au panicked before, so only it gets the panicking twin. The helper is local per crate (no cross-crate dep, no new support crate): ec-h265 keeps it in tests/common/mod.rs because its fifth site lives in a different test binary, ec-h264 keeps it in tests/encode.rs where both of its sites are. Two non-vacuity tests per crate: a /dev/null symlink reproduces a genuine Ok-but-short (0 of 1234), with a whole-file control read back byte for byte; and a child under `ulimit -f 1` with SIGXFSZ ignored proves the Err arm reports what actually LANDED (512 of 4096, EFBIG) rather than the count the writer confirmed. Deleting the check turns the first RED. Sweep of both crates: five sites fixed (the four named plus ec-h265's write_au, File::create + write_all, whose path goes to ffmpeg the same way), and one more -- ec-h264's append-mode $EC_H264_RD_TRACE writer, which discarded its write result -- made fatal instead, because append mode has no knowable total and so admits no length check. Everything else in the two crates is ffmpeg's own output, a read-only path, a directory, or an in-memory BitWriter. Success path byte-identical: same bytes, same path, truncate-then-write. --- crates/ec-h264/src/enc/mod.rs | 10 +- crates/ec-h264/tests/encode.rs | 163 +++++++++++++++++++++++++++- crates/ec-h265/tests/common/mod.rs | 93 ++++++++++++++++ crates/ec-h265/tests/conformance.rs | 14 ++- crates/ec-h265/tests/encode.rs | 86 ++++++++++++++- lanes/h26xpinloud.report.md | 159 +++++++++++++++++++++++++++ 6 files changed, 515 insertions(+), 10 deletions(-) create mode 100644 lanes/h26xpinloud.report.md diff --git a/crates/ec-h264/src/enc/mod.rs b/crates/ec-h264/src/enc/mod.rs index 27b19081..12058240 100644 --- a/crates/ec-h264/src/enc/mod.rs +++ b/crates/ec-h264/src/enc/mod.rs @@ -744,14 +744,20 @@ fn rd_trace_write(pic: &Picture, src: &Source, frame: u64, pre: Option bool { present } +/// Write `bytes` to `path` and PROVE the file on disk is exactly that long. +/// +/// lane-h26xpinloud: every write below used `std::fs::write(..).ok()?` and then +/// handed `path` to ffmpeg, which closes only the ERROR path. An Ok-but-short +/// write -- a filesystem that reports success and stores fewer bytes (ENOSPC on +/// a full mount, EFBIG under a size cap, a quota, a failed final flush) -- left +/// ffmpeg decoding a truncated stream, and the failure surfaced as a +/// plausible-looking wrong pixel or a shifted PSNR number, not as a write +/// failure. MEASURED: 1234 bytes written through a path that LOOKS like a +/// stream file in a scratch directory but whose filesystem accepts every byte +/// and stores none returns `Ok`, and the file on disk is 0 bytes. So the length +/// is checked here once, from the same `bytes.len()` the writer used, and any +/// shortfall carries the path, both counts, and the OS error -- or, for the +/// write that reported success and left a short file, the plain statement that +/// there was no OS error, which is exactly the case that used to print nothing. +/// +/// Returns the failure LINE rather than a bare bool, so the bail callers and any +/// panic caller all end on one format that cannot drift apart. The success path +/// is byte-identical to the old `fs::write`: same bytes, same path, truncate +/// then write. +fn pin_loud(site: &str, path: &std::path::Path, bytes: &[u8]) -> Result<(), String> { + let expected = bytes.len(); + if let Err(e) = std::fs::write(path, bytes) { + // The count here is the file's REAL length, not what the writer had + // confirmed in hand: under a size cap the kernel stores everything up + // to the cap and only THEN returns the error, and `fs::write` confirms + // none of it. + let on_disk = std::fs::metadata(path).map(|m| m.len() as usize).unwrap_or(0); + return Err(short_line( + site, + path, + on_disk, + expected, + &format!("the write failed: {e}"), + )); + } + match std::fs::metadata(path) { + Ok(m) if m.len() as usize == expected => Ok(()), + Ok(m) => Err(short_line( + site, + path, + m.len() as usize, + expected, + "the write reported success and the file on disk is still not that long (no OS error)", + )), + Err(e) => Err(short_line( + site, + path, + 0, + expected, + &format!("the written file could not be stat'ed: {e}"), + )), + } +} + +/// The one failure line every short pin ends on: site, path, the bytes the file +/// holds vs the bytes owed, and the reason. +fn short_line( + site: &str, + path: &std::path::Path, + on_disk: usize, + expected: usize, + why: &str, +) -> String { + format!( + "ec-h264 encode pin SHORT [{}] {}: file holds {on_disk} of {expected} bytes: {why}", + site, + path.display() + ) +} + +/// A pin whose shortfall is reported and then BAILED on, for a caller that +/// returns `None` today. Prints the same line [`pin_loud`] returns, so `None` +/// from here never reads as a plain "ffmpeg said no". +fn pin_or_bail(site: &str, path: &std::path::Path, bytes: &[u8]) -> Option<()> { + pin_loud(site, path, bytes) + .map_err(|why| eprintln!("{why}")) + .ok() +} + +#[cfg(unix)] +#[test] +fn pin_loud_reds_on_an_ok_but_short_write() { + // The shape the four sites left open: a write that reports success and + // leaves a file of a different length. A symlink to /dev/null accepts every + // byte and stores none, so `fs::write` returns Ok and the file is 0 bytes -- + // a stream path, silently empty. + let dir = std::env::temp_dir().join(format!("ec-h264-pinloud-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let path = dir.join("ok-but-short.264"); + let _ = std::fs::remove_file(&path); + std::os::unix::fs::symlink("/dev/null", &path).expect("symlink to /dev/null"); + let why = pin_loud("ok_but_short", &path, &[7u8; 1234]).unwrap_err(); + assert!(why.contains(&path.display().to_string()), "{why}"); + assert!(why.contains("file holds 0 of 1234 bytes"), "{why}"); + std::fs::remove_file(&path).ok(); + + // The control: the same call on a real file succeeds and leaves every byte. + let good = dir.join("whole.264"); + let bytes = vec![7u8; 1234]; + pin_loud("control", &good, &bytes).expect("a whole write is not short"); + assert_eq!(std::fs::read(&good).expect("read back"), bytes); + std::fs::remove_file(&good).ok(); + std::fs::remove_dir_all(&dir).ok(); +} + +#[cfg(unix)] +#[test] +fn pin_loud_reports_what_landed_under_a_size_cap() { + // The Err arm, driven for real: `ulimit -f` caps the file, the kernel stores + // everything up to the cap and only then returns EFBIG. The line must carry + // the bytes that actually LANDED (512 of a 1 KiB-block cap), not the count + // the writer confirmed, which is zero. + use std::process::Command; + let Ok(exe) = std::env::current_exe() else { + return; + }; + // SIGXFSZ's default action would kill the child before it can print; + // `trap '' XFSZ` sets SIG_IGN, which survives the exec. + let script = format!( + "ulimit -f 1; trap '' XFSZ; exec {} --exact pin_loud_cap_child --nocapture", + exe.display() + ); + let out = Command::new("sh") + .args(["-c", &script]) + .env("EC_PINLOUD_CHILD", "1") + .output() + .expect("run the capped child"); + let text = format!( + "{}{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + assert!(!out.status.success(), "the capped write must fail: {text}"); + assert!(text.contains("file holds 512 of 4096 bytes"), "{text}"); + assert!( + text.contains("EFBIG") || text.contains("File too large"), + "{text}" + ); +} + +/// Child of [`pin_loud_reports_what_landed_under_a_size_cap`]: runs under +/// `ulimit -f 1` (a 1 KiB block cap) and pins 4096 bytes, so the write fails +/// partway. Inert unless the parent set `EC_PINLOUD_CHILD`. +#[test] +fn pin_loud_cap_child() { + if std::env::var_os("EC_PINLOUD_CHILD").is_none() { + return; + } + let dir = std::env::temp_dir().join(format!("ec-h264-pinloud-cap-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let path = dir.join("capped.264"); + let why = pin_loud("capped", &path, &[7u8; 4096]) + .expect_err("4096 bytes cannot fit a 1 KiB cap"); + std::fs::remove_file(&path).ok(); + std::fs::remove_dir_all(&dir).ok(); + panic!("{why}"); +} + /// Decode an Annex B stream with ffmpeg, returning one I420 triple per picture. fn ffmpeg_decode(stream: &[u8], w: usize, h: usize, extra: &[&str]) -> Option> { // Distinct per call, not per process: several #[test]s in this binary @@ -472,7 +631,7 @@ fn ffmpeg_decode(stream: &[u8], w: usize, h: usize, extra: &[&str]) -> Option buf.extend_from_slice(u); buf.extend_from_slice(v); } - std::fs::write(&raw, &buf).ok()?; + pin_or_bail("x264_encode_qp", &raw, &buf)?; let status = std::process::Command::new("ffmpeg") .args(["-v", "error", "-y"]) .args(["-f", "rawvideo", "-pix_fmt", "yuv420p"]) diff --git a/crates/ec-h265/tests/common/mod.rs b/crates/ec-h265/tests/common/mod.rs index bd340424..6d92d2b0 100644 --- a/crates/ec-h265/tests/common/mod.rs +++ b/crates/ec-h265/tests/common/mod.rs @@ -91,3 +91,96 @@ pub fn natural_frame(width: u32, height: u32, phase: u32) -> VideoFrame { ) .expect("natural frame") } + +/// Write `bytes` to `path` and PROVE the file on disk is exactly that long. +/// +/// lane-h26xpinloud: the encode gate's sites used `std::fs::write(..).ok()?` +/// and [`write_au`] used `File::create` + `write_all(..).expect(..)`, then all +/// three handed `path` to ffmpeg. Checking the `Result` closes only the ERROR +/// path. An Ok-but-short write -- a filesystem that reports success and stores +/// fewer bytes (ENOSPC on a full mount, EFBIG under a size cap, a quota, a +/// failed final flush) -- left ffmpeg decoding a truncated stream, and the +/// failure surfaced as a plausible-looking wrong pixel or a shifted PSNR +/// number, not as a write failure. MEASURED: 1234 bytes written through a path +/// that LOOKS like a stream file in a scratch directory but whose filesystem +/// accepts every byte and stores none returns `Ok`, and the file on disk is 0 +/// bytes. So the length is checked here once, from the same `bytes.len()` the +/// writer used, and any shortfall carries the path, both counts, and the OS +/// error -- or, for the write that reported success and left a short file, the +/// plain statement that there was no OS error, which is exactly the case that +/// used to print nothing. +/// +/// This is the ec-av1 `dumpio::LoudDump` idea replicated per crate: there is no +/// shared test-support crate here and none is being added for one function. +/// Returns the failure LINE rather than a bare bool, so the bail callers and the +/// panic caller all end on one format that cannot drift apart. The success path +/// is byte-identical to the old write: same bytes, same path, truncate then +/// write. +pub fn pin_loud(site: &str, path: &std::path::Path, bytes: &[u8]) -> Result<(), String> { + let expected = bytes.len(); + if let Err(e) = std::fs::write(path, bytes) { + // The count here is the file's REAL length, not what the writer had + // confirmed in hand: under a size cap the kernel stores everything up + // to the cap and only THEN returns the error, and the write confirms + // none of it. + let on_disk = std::fs::metadata(path).map(|m| m.len() as usize).unwrap_or(0); + return Err(short_line( + site, + path, + on_disk, + expected, + &format!("the write failed: {e}"), + )); + } + match std::fs::metadata(path) { + Ok(m) if m.len() as usize == expected => Ok(()), + Ok(m) => Err(short_line( + site, + path, + m.len() as usize, + expected, + "the write reported success and the file on disk is still not that long (no OS error)", + )), + Err(e) => Err(short_line( + site, + path, + 0, + expected, + &format!("the written file could not be stat'ed: {e}"), + )), + } +} + +/// The one failure line every short pin ends on: site, path, the bytes the file +/// holds vs the bytes owed, and the reason. +fn short_line( + site: &str, + path: &std::path::Path, + on_disk: usize, + expected: usize, + why: &str, +) -> String { + format!( + "ec-h265 test pin SHORT [{}] {}: file holds {on_disk} of {expected} bytes: {why}", + site, + path.display() + ) +} + +/// A pin whose shortfall is reported and then BAILED on, for a caller that +/// returns `None` today. Prints the same line [`pin_loud`] returns, so `None` +/// from here never reads as a plain "ffmpeg said no". +pub fn pin_or_bail(site: &str, path: &std::path::Path, bytes: &[u8]) -> Option<()> { + pin_loud(site, path, bytes) + .map_err(|why| eprintln!("{why}")) + .ok() +} + +/// [`pin_loud`] with the shortfall PANICKING, for a caller that fails loudly +/// today. The panic message IS the failure line, so the two callers of this +/// shape cannot drift apart either. +pub fn pin_or_panic(site: &str, path: &std::path::Path, bytes: &[u8]) { + if let Err(why) = pin_loud(site, path, bytes) { + panic!("{why}"); + } +} diff --git a/crates/ec-h265/tests/conformance.rs b/crates/ec-h265/tests/conformance.rs index e6e192b4..a5a29800 100644 --- a/crates/ec-h265/tests/conformance.rs +++ b/crates/ec-h265/tests/conformance.rs @@ -6,10 +6,9 @@ mod common; -use common::{natural_frame, test_frame}; +use common::{natural_frame, pin_or_panic, test_frame}; use ec_core::frame::VideoFrame; use ec_h265::encoder::{EncodedPicture, Encoder, EncoderConfig, RateControl, TransformSkip}; -use std::io::Write; use std::path::{Path, PathBuf}; use std::process::Command; @@ -47,10 +46,17 @@ fn ffmpeg_decode(path: &Path) -> Result, String> { std::fs::read(&out).map_err(|e| e.to_string()) } +/// Pin one access unit as an Annex-B file for [`ffmpeg_decode`]. +/// +/// lane-h26xpinloud: this used `File::create(..).expect(..)` + +/// `write_all(..).expect("write bitstream")`, which closes only the ERROR path. +/// A write that reports success and leaves a short file put a truncated stream +/// at exactly the path ffmpeg then decoded, so the shortfall now carries the +/// path, both counts, and the OS error. Panic semantics kept: this caller +/// panicked before and still does, with the failure line as the message. fn write_au(name: &str, picture: &EncodedPicture) -> PathBuf { let path = scratch_dir().join(format!("{name}.265")); - let mut file = std::fs::File::create(&path).expect("create bitstream"); - file.write_all(&picture.au).expect("write bitstream"); + pin_or_panic("write_au", &path, &picture.au); path } diff --git a/crates/ec-h265/tests/encode.rs b/crates/ec-h265/tests/encode.rs index 88faeccd..11b6d74e 100644 --- a/crates/ec-h265/tests/encode.rs +++ b/crates/ec-h265/tests/encode.rs @@ -6,6 +6,9 @@ //! own reconstruction — and x265's psychovisual and adaptive tools are off so //! the comparison is at matched features, not at matched marketing. +mod common; + +use common::{pin_loud, pin_or_bail}; use ec_core::frame::VideoFrame; use ec_h265::encoder::{Encoder, EncoderConfig, RateControl, TransformSkip}; @@ -281,12 +284,91 @@ fn have_x265() -> bool { .is_ok_and(|o| o.status.success() && String::from_utf8_lossy(&o.stdout).contains("libx265")) } +#[cfg(unix)] +#[test] +fn pin_loud_reds_on_an_ok_but_short_write() { + // The shape the two sites above left open: a write that reports success and + // leaves a file of a different length. A symlink to /dev/null accepts every + // byte and stores none, so `fs::write` returns Ok and the file is 0 bytes -- + // a stream path, silently empty. + let dir = std::env::temp_dir().join(format!("ec-h265-pinloud-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let path = dir.join("ok-but-short.265"); + let _ = std::fs::remove_file(&path); + std::os::unix::fs::symlink("/dev/null", &path).expect("symlink to /dev/null"); + let why = pin_loud("ok_but_short", &path, &[7u8; 1234]).unwrap_err(); + assert!(why.contains(&path.display().to_string()), "{why}"); + assert!(why.contains("file holds 0 of 1234 bytes"), "{why}"); + std::fs::remove_file(&path).ok(); + + // The control: the same call on a real file succeeds and leaves every byte. + let good = dir.join("whole.265"); + let bytes = vec![7u8; 1234]; + pin_loud("control", &good, &bytes).expect("a whole write is not short"); + assert_eq!(std::fs::read(&good).expect("read back"), bytes); + std::fs::remove_file(&good).ok(); + std::fs::remove_dir_all(&dir).ok(); +} + +#[cfg(unix)] +#[test] +fn pin_loud_reports_what_landed_under_a_size_cap() { + // The Err arm, driven for real: `ulimit -f` caps the file, the kernel stores + // everything up to the cap and only then returns EFBIG. The line must carry + // the bytes that actually LANDED (512 of a 1 KiB-block cap), not the count + // the writer confirmed, which is zero. + use std::process::Command; + let Ok(exe) = std::env::current_exe() else { + return; + }; + // SIGXFSZ's default action would kill the child before it can print; + // `trap '' XFSZ` sets SIG_IGN, which survives the exec. + let script = format!( + "ulimit -f 1; trap '' XFSZ; exec {} --exact pin_loud_cap_child --nocapture", + exe.display() + ); + let out = Command::new("sh") + .args(["-c", &script]) + .env("EC_PINLOUD_CHILD", "1") + .output() + .expect("run the capped child"); + let text = format!( + "{}{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + assert!(!out.status.success(), "the capped write must fail: {text}"); + assert!(text.contains("file holds 512 of 4096 bytes"), "{text}"); + assert!( + text.contains("EFBIG") || text.contains("File too large"), + "{text}" + ); +} + +/// Child of [`pin_loud_reports_what_landed_under_a_size_cap`]: runs under +/// `ulimit -f 1` (a 1 KiB block cap) and pins 4096 bytes, so the write fails +/// partway. Inert unless the parent set `EC_PINLOUD_CHILD`. +#[test] +fn pin_loud_cap_child() { + if std::env::var_os("EC_PINLOUD_CHILD").is_none() { + return; + } + let dir = std::env::temp_dir().join(format!("ec-h265-pinloud-cap-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let path = dir.join("capped.265"); + let why = pin_loud("capped", &path, &[7u8; 4096]) + .expect_err("4096 bytes cannot fit a 1 KiB cap"); + std::fs::remove_file(&path).ok(); + std::fs::remove_dir_all(&dir).ok(); + panic!("{why}"); +} + /// Decode an Annex-B stream with ffmpeg, returning one I420 triple per picture. fn ffmpeg_decode(stream: &[u8], w: usize, h: usize) -> Option> { let dir = std::env::temp_dir().join(format!("ec-h265-enc-{}", std::process::id())); std::fs::create_dir_all(&dir).ok()?; let path = dir.join(format!("s{}.265", stream.len())); - std::fs::write(&path, stream).ok()?; + pin_or_bail("ffmpeg_decode", &path, stream)?; let out = std::process::Command::new("ffmpeg") .args(["-v", "error"]) .arg("-i") @@ -338,7 +420,7 @@ fn x265_encode_qp(sources: &[Planes], w: usize, h: usize, qp: i32) -> Option` | +| `ec-h264` | `tests/encode.rs:1068` in `x264_encode_qp` | raw I420 for x264 | `ffmpeg -i ` (x264 encode) | +| `ec-h265` | `tests/encode.rs:289` in `ffmpeg_decode` | our Annex B stream | `ffmpeg -i ` | +| `ec-h265` | `tests/encode.rs:341` in `x265_encode_qp` | raw I420 for x265 | `ffmpeg -i ` (x265 encode) | + +All four were `std::fs::write(&path, bytes).ok()?`. The `Err` path was closed +(bail via `?`); the **Ok-but-short** path was not, and that is the one that +produces an artefact another tool then trusts. + +## Sweep of the two crates + +Every write-ish call in `crates/ec-h264` and `crates/ec-h265` (source + all test +binaries), filtered to the ones whose path an external reader takes: + +**Fixed — same shape:** + +- `ec-h264/tests/encode.rs:475`, `:1068` (the two named sites). +- `ec-h265/tests/encode.rs:289`, `:341` (the two named sites). +- `ec-h265/tests/conformance.rs:50` `write_au` — `File::create` + + `write_all(&picture.au).expect("write bitstream")`, whose `path` is passed + straight to `ffmpeg_decode` → `ffmpeg -i`. `write_all` returning `Ok` means the + bytes were accepted by the write, not that the file holds them; the same + on-disk stat is missing. **Not in the ticket's four, but the identical shape + and the same crate, so it is fixed too.** It is the fifth site and the only + one that fed a path written by `File::create`. +- `ec-h264/src/enc/mod.rs:747` `rd_trace_write` — `let _ = writeln!(out, ..)` + into `$EC_H264_RD_TRACE` (feature `rd-ablation`). This one is an append-mode + text table with **no knowable total**, so no length check is possible (the + ec-av1 `append_unknown` case: `expected: None`). But discarding the write + result leaves a silently short trace that an RD-attribution reader cannot + distinguish from a complete one. Fixed as far as the shape allows: the write + error is now fatal (`.expect("write EC_H264_RD_TRACE")`). The length check is + deliberately absent, and the comment says why. + +**Swept and NOT the same shape (with the reason):** + +- `ec-h264/tests/conformance.rs` `x264_encode` / `x264_encode_gop` (and + `extract_annexb`, ~1765) — these do not write the file themselves; **ffmpeg** + writes the stream (`-f h264 `, `h264_mp4toannexb`) and the harness reads + it back with `fs::read`. There is no discarded write result of ours to check: + ffmpeg's exit status is the only signal, and `run(...)` already discards + everything but that boolean. Not this class. +- `ec-h264/tests/conformance.rs` `compare_sequence_streamed` (1693), + `compare_container_streamed` (1815), `ffmpeg_all_frames` (226), + `ffmpeg_first_frame` (144) — read-only paths into ffmpeg; no write of ours. +- `ec-h264/tests/conformance.rs` `scratch_dir` (694) `let _ = create_dir_all` and + the many `let _ = remove_dir_all` — directory lifecycle, no bytes, no length to + owe. +- `ec-h265/tests/common/mod.rs`, `decode.rs`, `perf.rs`, `rdoq_cache_hash.rs` — + no file writes at all. +- `ec-h265/src/encoder.rs:769` `header.write(&mut writer, ..)` — an in-memory + `BitWriter`, not a file. + +So: **every write in these two crates whose path is handed to ffmpeg now goes +through the loud pin** (five sites), and the one remaining write with a discarded +result (the append-mode RD trace) has its error made fatal because no length is +knowable. + +## Shape chosen: a per-crate helper, not four inline checks + +`pin_loud` (plus `short_line`, `pin_or_bail`, `pin_or_panic`), replicating +ec-av1's `dumpio` idea per crate — no cross-crate dependency, per contract. + +Why a helper and not inline checks: the four (now five) sites are the *same* +shape — write `bytes` to a `Path`, then hand `Path` to ffmpeg — so inline checks +would duplicate the failure-line format five times and let it drift, which is +precisely the ec-av1 hazard its shared `failure_line` exists to prevent. The +helper returns the **failure line as `Err(String)`** rather than panicking, +because three of the five sites `return None` on write failure today and +`:690` in ec-h264 turns `None` into a **SKIP**. Making it panic would have +changed a skip into a failure; instead the shortfall is printed in full and the +existing bail/skip semantics are preserved. Only `write_au` panicked before, so +only it gets `pin_or_panic` (the line *is* the panic message). + +Placement: `ec-h265` has a shared `tests/common/mod.rs` used by four test +binaries, and `write_au` lives in a different binary from `ffmpeg_decode`, so the +helper lives there. `ec-h264` has no `tests/common`; both of its sites are in +`tests/encode.rs`, so the helper is local to that file. + +## Proof the check bites + +Both crates carry two new non-vacuity tests. + +**1. Genuine Ok-but-short (`pin_loud_reds_on_an_ok_but_short_write`).** A +`/dev/null` symlink in a scratch dir is the shape: it *looks* like +`.../ok-but-short.264`, it accepts every byte, it stores none. Measured on this +box with a standalone `rustc` probe: + +``` +fs::write -> Ok(()) | on-disk len = 0 | owed = 1234 # symlink to /dev/null +fs::write -> Ok(()) | on-disk len = 1234 | owed = 1234 # real file +``` + +The test asserts the returned line contains the path and `file holds 0 of 1234 +bytes`, then asserts the control — a real file of 1234 bytes succeeds and reads +back byte-identical. + +**Red-before proof (mutation).** Replacing the length check's short arm with +`Ok(())` (i.e. deleting the check) turns the test RED: + +``` +---- pin_loud_reds_on_an_ok_but_short_write stdout ---- +panicked at crates/ec-h264/tests/encode.rs:548:61: +called `Result::unwrap_err()` on an `Ok` value: () +test result: FAILED. 0 passed; 1 failed +``` + +Restored afterwards; `git diff --stat` confirms the helper is back. + +**2. The Err arm, driven for real (`pin_loud_reports_what_landed_under_a_size_cap`).** +A child process runs under `ulimit -f 1` (a 1 KiB block cap) with `SIGXFSZ` +ignored (`trap '' XFSZ`, which survives the exec) and pins 4096 bytes. The kernel +stores up to the cap and *then* returns `EFBIG`, so the acknowledged count is +zero while 512 bytes are on disk. Actual output from both binaries: + +``` +ec-h264 encode pin SHORT [capped] /tmp/ec-h264-pinloud-cap-*/capped.264: file holds 512 of 4096 bytes: the write failed: File too large (os error 27) +ec-h265 test pin SHORT [capped] /tmp/ec-h265-pinloud-cap-*/capped.265: file holds 512 of 4096 bytes: the write failed: File too large (os error 27) +``` + +The count on the line is the file's **real** length, not the writer's confirmed +count — which is the part the acked count gets wrong. + +## Scoped runs + +``` +cargo check -p ec-h264 -p ec-h265 --tests # clean +cargo check -p ec-h264 --features rd-ablation # clean +cargo test -p ec-h264 --test encode pin_loud # 3 passed +cargo test -p ec-h265 --test encode pin_loud # 3 passed +``` + +(The full `ec-av1` suite runs on the server; per contract these two crates were +checked and run locally, scoped.) + +## What did not change + +- The success path is byte-identical to the old `fs::write`: same bytes, same + path, truncate-then-write. The `control` half of each test reads the file back + and compares bytes. +- No assertion was weakened and no gate's printed output changed on the success + path. The only new output is on the shortfall path, which previously printed + nothing at all. +- No decode/encode logic touched: `ec-h264/src/enc/mod.rs` changed only inside the + `rd-ablation`-gated trace writer's discarded result. \ No newline at end of file