forge
Explore
Log in

Don't you dare go Hollow

ec-av1: route every test-pin write through dumpio::LoudDump

Browse files at this commit

f3fd848913ff55a9b17ada3e2f56f3252f9da278Ahmet Kaan GümüşParent: 79a4e31
The self-pin sites in stream.rs wrote with `let _ = std::fs::write(..)`
and then named the path in the panic that followed ("stream pinned at
{}"), so an ENOSPC/EFBIG left a TRUNCATED stream at exactly the path the
reader was told to replay -- the same silent-truncation class as the
dump path, one hop later. dumpio gains pin() (fatal) and pin_reporting()
(same failure line, returned not panicked); msac's symtrace dump folds its
hand-rolled check into the same writer.
crates/ec-av1/src/dumpio.rsmodified+166 -5
@@ -155,7 +155,9 @@ impl LoudDump {
155155 /// filesystem-level reservation failure all land here rather than in
156156 /// `write`.
157157 pub fn finish(mut self) {
158- let Some(mut file) = self.file.take() else { return };
158+ let Some(mut file) = self.file.take() else {
159+ return;
160+ };
159161 if let Err(e) = file.flush() {
160162 abort(
161163 &self.site,
@@ -193,18 +195,83 @@ impl LoudDump {
193195 /// The single failure line every short dump now ends on. `site`, the path, the
194196 /// bytes on disk vs the bytes owed, and the reason.
195197 fn abort(site: &str, path: &Path, got: usize, expected: Option<usize>, why: &str) -> ! {
198+ panic!("{}", failure_line(site, path, got, expected, why));
199+}
200+
201+/// The failure line itself, so the fatal and the reporting shapes cannot
202+/// drift apart.
203+fn failure_line(site: &str, path: &Path, got: usize, expected: Option<usize>, why: &str) -> String {
196204 let owed = match expected {
197205 Some(e) => format!("{e}"),
198206 None => "an unknown number of".to_string(),
199207 };
200- panic!(
208+ format!(
201209 "ec-av1 dump FAILED [{}] {}: wrote {} of {} bytes: {}",
202210 site,
203211 path.display(),
204212 got,
205213 owed,
206214 why
207- );
215+ )
216+}
217+
218+/// Write `bytes` to `path` as a FATAL pin, loud on any shortfall -- [`pin`]'s
219+/// counterpart to [`write_planes`], for the single-blob case.
220+///
221+/// lane-av1pinloud: the test-pin sites in `stream.rs` wrote with
222+/// `let _ = std::fs::write(..)` and then named the path in the panic that
223+/// followed ("stream pinned at {}"), so an `ENOSPC`/`EFBIG` left a TRUNCATED
224+/// stream at exactly the path the reader was told to replay -- the same
225+/// silent-truncation class as the dump path, one hop later. Success path is
226+/// byte-identical: same bytes, same path, truncate-then-write.
227+pub fn pin(site: &str, path: impl AsRef<Path>, bytes: &[u8]) {
228+ let mut d = LoudDump::create(site, path, bytes.len());
229+ d.write(bytes);
230+ d.finish();
231+}
232+
233+/// [`pin`] for a site that must NOT die on a failed pin -- a gate whose real
234+/// failure is the assert that comes after the pin attempt, and whose comment
235+/// says so. Non-fatal, but not silent: a shortfall comes back as the very same
236+/// failure line [`pin`] would have panicked with, expected-vs-on-disk counts
237+/// included, for the caller to print as a warning.
238+pub fn pin_reporting(site: &str, path: impl AsRef<Path>, bytes: &[u8]) -> Result<(), String> {
239+ let path = path.as_ref();
240+ let expected = bytes.len();
241+ if let Err(e) = std::fs::write(path, bytes) {
242+ let on_disk = std::fs::metadata(path)
243+ .map(|m| m.len() as usize)
244+ .unwrap_or(0);
245+ return Err(failure_line(
246+ site,
247+ path,
248+ on_disk,
249+ Some(expected),
250+ &format!("could not write the pin: {e}"),
251+ ));
252+ }
253+ let on_disk = match std::fs::metadata(path) {
254+ Ok(m) => m.len() as usize,
255+ Err(e) => {
256+ return Err(failure_line(
257+ site,
258+ path,
259+ expected,
260+ Some(expected),
261+ &format!("could not stat the written pin: {e}"),
262+ ));
263+ }
264+ };
265+ if on_disk != expected {
266+ return Err(failure_line(
267+ site,
268+ path,
269+ on_disk,
270+ Some(expected),
271+ "the pinned file on disk is not the length of the stream",
272+ ));
273+ }
274+ Ok(())
208275 }
209276
210277 /// Write `planes` (already narrowed to their dump's sample width) as ONE dump
@@ -391,9 +458,103 @@ mod tests {
391458 if std::env::var("EC_AV1_DUMPLOUD_CHILD").is_err() {
392459 return;
393460 }
394- let path =
395- std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/420_mixll_256x128_6f.obu");
461+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
462+ .join("fixtures/420_mixll_256x128_6f.obu");
396463 let bytes = std::fs::read(&path).expect("the pinned fixture");
397464 let _ = crate::stream::decode_stream(&bytes);
398465 }
466+
467+ #[test]
468+ fn pin_success_path_is_byte_identical() {
469+ let p = scratch("pin-ok");
470+ pin("EC_AV1_PIN", &p, b"stream bytes");
471+ assert_eq!(std::fs::read(&p).unwrap(), b"stream bytes");
472+ let _ = std::fs::remove_file(&p);
473+ }
474+
475+ #[test]
476+ #[cfg(unix)]
477+ fn pin_to_a_full_device_is_loud() {
478+ let msg = caught(|| pin("EC_AV1_PIN", "/dev/full", b"stream bytes"));
479+ assert!(
480+ msg.starts_with("ec-av1 dump FAILED [EC_AV1_PIN] /dev/full: wrote "),
481+ "{msg}"
482+ );
483+ assert!(msg.contains("No space left on device"), "{msg}");
484+ assert!(msg.contains(" of 12 bytes"), "{msg}");
485+ }
486+
487+ /// The reporting shape: same failure line, returned instead of panicked,
488+ /// so a gate whose real failure is the assert after the pin keeps that
489+ /// assert -- but the reader still learns the pin is short.
490+ #[test]
491+ #[cfg(unix)]
492+ fn pin_reporting_is_loud_but_not_fatal() {
493+ let line = pin_reporting("EC_AV1_PIN", "/dev/full", b"stream bytes").unwrap_err();
494+ assert!(
495+ line.contains("ec-av1 dump FAILED [EC_AV1_PIN] /dev/full"),
496+ "{line}"
497+ );
498+ assert!(line.contains(" of 12 bytes"), "{line}");
499+ let p = scratch("pin-reporting-ok");
500+ pin_reporting("EC_AV1_PIN", &p, b"stream bytes").expect("a healthy pin reports nothing");
501+ assert_eq!(std::fs::read(&p).unwrap(), b"stream bytes");
502+ let _ = std::fs::remove_file(&p);
503+ }
504+
505+ /// The pin path's own reproduction: a real `RLIMIT_FSIZE` leaves a real
506+ /// truncated stream at the path the panic names. Without the check the
507+ /// reader replays the fragment and blames the decoder.
508+ #[test]
509+ #[cfg(unix)]
510+ fn a_pin_under_a_file_size_cap_fails_loudly() {
511+ let dir = std::env::temp_dir().join(format!("ec-av1-pin-cap-{}", std::process::id()));
512+ let _ = std::fs::remove_dir_all(&dir);
513+ std::fs::create_dir_all(&dir).unwrap();
514+ let out = Command::new("bash")
515+ .args([
516+ "-c",
517+ "ulimit -f 8; trap \"\" XFSZ; exec \"$0\" \"$@\"",
518+ std::env::current_exe().unwrap().to_str().unwrap(),
519+ "--exact",
520+ "dumpio::tests::pin_child_under_cap",
521+ "--nocapture",
522+ ])
523+ .env("EC_AV1_PINLOUD_CHILD", &dir)
524+ .output()
525+ .expect("spawning the capped child");
526+ let stderr = String::from_utf8_lossy(&out.stderr);
527+ assert!(!out.status.success(), "a short pin must fail\n{stderr}");
528+ assert!(
529+ stderr.contains("ec-av1 dump FAILED [EC_AV1_PIN]"),
530+ "{stderr}"
531+ );
532+ assert!(stderr.contains("File too large"), "{stderr}");
533+ let frag = std::fs::metadata(dir.join("cap-pinned.obu")).unwrap().len();
534+ assert!(
535+ frag < 32768,
536+ "the capped child left {frag} bytes where 32768 B was owed"
537+ );
538+ assert!(
539+ stderr.contains(&format!("wrote {frag} of 32768 bytes")),
540+ "{stderr}"
541+ );
542+ let _ = std::fs::remove_dir_all(&dir);
543+ }
544+
545+ /// Child half of [`a_pin_under_a_file_size_cap_fails_loudly`]. Not a gate:
546+ /// with no `EC_AV1_PINLOUD_CHILD` set it writes nothing and asserts nothing.
547+ #[test]
548+ fn pin_child_under_cap() {
549+ let Ok(dir) = std::env::var("EC_AV1_PINLOUD_CHILD") else {
550+ return;
551+ };
552+ // `ulimit -f 8` is 8 KiB, so 4x that is guaranteed to run PAST the cap
553+ // (a write of exactly the cap size succeeds) and land a fragment.
554+ pin(
555+ "EC_AV1_PIN",
556+ Path::new(&dir).join("cap-pinned.obu"),
557+ &[7u8; 32768],
558+ );
559+ }
399560 }
crates/ec-av1/src/msac.rsmodified+9 -9
@@ -1133,16 +1133,16 @@ pub(crate) mod symtrace {
11331133 let (buf, n) = &mut *b.borrow_mut();
11341134 if !buf.is_empty() {
11351135 // lane-av1dumploud: loud on a short write. `std::fs::write`
1136- // returns a `Result` that this used to discard; the expected
1137- // length is exactly the buffer's own.
1136+ // returned a `Result` this used to discard, and the check was
1137+ // hand-rolled against the buffer length only; lane-av1pinloud
1138+ // folded it into the shared writer so this site also gets the
1139+ // on-disk length check the dump path already had.
11381140 let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1139- let (bytes, at) = (buf.as_bytes(), format!("{dir}/{side}-{seq:05}.txt"));
1140- if let Err(e) = std::fs::write(&at, bytes) {
1141- panic!(
1142- "ec-av1 dump FAILED [EC_AV1_SYMTRACE] {at}: wrote 0 of {} bytes: {e}",
1143- bytes.len()
1144- );
1145- }
1141+ crate::dumpio::pin(
1142+ "EC_AV1_SYMTRACE",
1143+ format!("{dir}/{side}-{seq:05}.txt"),
1144+ buf.as_bytes(),
1145+ );
11461146 }
11471147 buf.clear();
11481148 *n = 0;
crates/ec-av1/src/stream.rsmodified+29 -18
@@ -2214,7 +2214,11 @@ fn decode_frame(
22142214 crate::dumpio::write_planes(
22152215 "EC_AV1_DECODE_ORDER_DUMP",
22162216 format!("{path}.f{decode_idx}"),
2217- &[&narrow(&picture.y), &narrow(&picture.u), &narrow(&picture.v)],
2217+ &[
2218+ &narrow(&picture.y),
2219+ &narrow(&picture.u),
2220+ &narrow(&picture.v),
2221+ ],
22182222 );
22192223 }
22202224 // lane-hidden r1: the frame exactly as it is about to be stored into
@@ -2257,8 +2261,11 @@ fn decode_frame(
22572261 // writer that builds the wrong buffer still gets caught.
22582262 let sample_width = if bit_depth == 8 { 1 } else { 2 };
22592263 let expected = (picture.y.len() + picture.u.len() + picture.v.len()) * sample_width;
2260- let mut f =
2261- crate::dumpio::LoudDump::create("EC_AV1_FINAL_DUMP", format!("{prefix}.f{decode_idx}"), expected);
2264+ let mut f = crate::dumpio::LoudDump::create(
2265+ "EC_AV1_FINAL_DUMP",
2266+ format!("{prefix}.f{decode_idx}"),
2267+ expected,
2268+ );
22622269 for plane in [&picture.y, &picture.u, &picture.v] {
22632270 if bit_depth == 8 {
22642271 f.write(&plane.iter().map(|&s| s as u8).collect::<Vec<u8>>());
@@ -14188,7 +14195,7 @@ exit 0
1418814195 assert_eq!(a.len(), b.len(), "{name}: frame {i} {plane} plane length");
1418914196 if a != b {
1419014197 let pin = std::env::temp_dir().join(format!("{name}-mismatch.obu"));
14191- let _ = std::fs::write(&pin, &stream);
14198+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
1419214199 let at = a
1419314200 .iter()
1419414201 .zip(b.iter())
@@ -18198,7 +18205,7 @@ exit 0
1819818205 for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
1819918206 if got.y != want.y || got.u != want.u || got.v != want.v {
1820018207 let pin = std::env::temp_dir().join("ec-av1-10bit-inter-gate-fail.obu");
18201- let _ = std::fs::write(&pin, &stream);
18208+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
1820218209 let first = |a: &Vec<u16>, b: &Vec<u16>| {
1820318210 a.iter()
1820418211 .zip(b)
@@ -23167,7 +23174,7 @@ exit 0
2316723174 Ok(frames) => frames,
2316823175 Err(e) => {
2316923176 let pin = std::env::temp_dir().join(format!("{name}-refused.obu"));
23170- let _ = std::fs::write(&pin, &stream);
23177+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2317123178 panic!(
2317223179 "{name}: decode_stream refused a real aomenc stream: {e} (pinned at {})",
2317323180 pin.display()
@@ -23187,7 +23194,7 @@ exit 0
2318723194 for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
2318823195 if got.y != want.y || got.u != want.u || got.v != want.v {
2318923196 let pin = std::env::temp_dir().join(format!("{name}-mismatch.obu"));
23190- let _ = std::fs::write(&pin, &stream);
23197+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2319123198 panic!(
2319223199 "{name}: frame {i} mismatch vs ffmpeg -- stream pinned at {}",
2319323200 pin.display()
@@ -23586,7 +23593,7 @@ exit 0
2358623593 }
2358723594 Err(e) => {
2358823595 let pin = std::env::temp_dir().join(format!("{name}-refused.obu"));
23589- let _ = std::fs::write(&pin, &stream);
23596+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2359023597 panic!(
2359123598 "{name}: decode_stream failed on a real aomenc stream (cq={cq} period={period}): {e} (pinned at {})",
2359223599 pin.display()
@@ -23608,7 +23615,7 @@ exit 0
2360823615 for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
2360923616 if got.y != want.y || got.u != want.u || got.v != want.v {
2361023617 let pin = std::env::temp_dir().join(format!("{name}-mismatch.obu"));
23611- let _ = std::fs::write(&pin, &stream);
23618+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2361223619 panic!(
2361323620 "{name}: frame {i} mismatch vs ffmpeg (cq={cq} period={period}) -- stream pinned at {}",
2361423621 pin.display()
@@ -27901,7 +27908,7 @@ exit 0
2790127908 "ec-av1-deblocking-gate-fail-s{}-cq{cq}.obu",
2790227909 42 + round
2790327910 ));
27904- let _ = std::fs::write(&pin, &stream);
27911+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2790527912 panic!(
2790627913 "frame {i} mismatch vs ffmpeg (seed {} cq {cq}, deblock fired: \
2790727914 {counted}) -- stream pinned at {}",
@@ -28134,14 +28141,18 @@ exit 0
2813428141 // The pin goes to `fixtures/`, which outlives the session: an
2813528142 // earlier version wrote to a session scratchpad, so once tmpfs
2813628143 // reaped it a real mismatch aborted on the write instead of
28137- // reporting itself. A failed write must not mask the assert
28138- // below either, so it warns rather than panicking.
28144+ // reporting itself. A failed write must not mask the asserts
28145+ // below either, so it warns rather than panicking -- lane-av1pinloud
28146+ // routed it through `pin_reporting` rather than bare `fs::write`,
28147+ // so the warning now carries on-disk-vs-owed byte counts: a
28148+ // truncated pin was as silent here as anywhere else, and this
28149+ // is the one site where dying on the write is wrong.
2813928150 let sp = pin_dir();
2814028151 let stream_pin = sp.join(format!("cdfflake-stream-seed{seed}.bin"));
2814128152 let ref_pin = sp.join(format!("cdfflake-ffmpeg-raw-seed{seed}.bin"));
2814228153 for (path, bytes) in [(&stream_pin, &stream), (&ref_pin, &ffmpeg_raw)] {
28143- if let Err(e) = std::fs::write(path, bytes) {
28144- eprintln!("cdfflake: could not pin to {}: {e}", path.display());
28154+ if let Err(line) = crate::dumpio::pin_reporting("EC_AV1_PIN", path, bytes) {
28155+ eprintln!("cdfflake: {line}");
2814528156 }
2814628157 }
2814728158 eprintln!(
@@ -28893,9 +28904,9 @@ exit 0
2889328904 if mismatched {
2889428905 let pin =
2889528906 std::env::temp_dir().join("ec-av1-reference-select-gate-fail.obu");
28896- let _ = std::fs::write(&pin, &stream);
28907+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2889728908 if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") {
28898- std::fs::write(&path, &stream).expect("writing pinned stream");
28909+ crate::dumpio::pin("EC_AV1_GATE_DUMP", &path, &stream);
2889928910 eprintln!(
2890028911 "EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"
2890128912 );
@@ -29103,9 +29114,9 @@ exit 0
2910329114 .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v);
2910429115 if mismatched {
2910529116 let pin = std::env::temp_dir().join("ec-av1-compound-refs-gate-fail.obu");
29106- let _ = std::fs::write(&pin, &stream);
29117+ crate::dumpio::pin("EC_AV1_PIN", &pin, &stream);
2910729118 if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") {
29108- std::fs::write(&path, &stream).expect("writing pinned stream");
29119+ crate::dumpio::pin("EC_AV1_GATE_DUMP", &path, &stream);
2910929120 eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}");
2911029121 }
2911129122 for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() {