ec-av1: route every test-pin write through dumpio::LoudDump
Browse files at this commitf3fd848913ff55a9b17ada3e2f56f3252f9da278Parent: 79a4e313 changed files +204 -32
- crates/ec-av1/src/dumpio.rs +166 -5
- crates/ec-av1/src/msac.rs +9 -9
- crates/ec-av1/src/stream.rs +29 -18
crates/ec-av1/src/dumpio.rsmodified+166 -5
| @@ -155,7 +155,9 @@ impl LoudDump { | ||
| 155 | 155 | /// filesystem-level reservation failure all land here rather than in |
| 156 | 156 | /// `write`. |
| 157 | 157 | 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 | + }; | |
| 159 | 161 | if let Err(e) = file.flush() { |
| 160 | 162 | abort( |
| 161 | 163 | &self.site, |
| @@ -193,18 +195,83 @@ impl LoudDump { | ||
| 193 | 195 | /// The single failure line every short dump now ends on. `site`, the path, the |
| 194 | 196 | /// bytes on disk vs the bytes owed, and the reason. |
| 195 | 197 | 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 { | |
| 196 | 204 | let owed = match expected { |
| 197 | 205 | Some(e) => format!("{e}"), |
| 198 | 206 | None => "an unknown number of".to_string(), |
| 199 | 207 | }; |
| 200 | - panic!( | |
| 208 | + format!( | |
| 201 | 209 | "ec-av1 dump FAILED [{}] {}: wrote {} of {} bytes: {}", |
| 202 | 210 | site, |
| 203 | 211 | path.display(), |
| 204 | 212 | got, |
| 205 | 213 | owed, |
| 206 | 214 | 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(()) | |
| 208 | 275 | } |
| 209 | 276 | |
| 210 | 277 | /// Write `planes` (already narrowed to their dump's sample width) as ONE dump |
| @@ -391,9 +458,103 @@ mod tests { | ||
| 391 | 458 | if std::env::var("EC_AV1_DUMPLOUD_CHILD").is_err() { |
| 392 | 459 | return; |
| 393 | 460 | } |
| 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"); | |
| 396 | 463 | let bytes = std::fs::read(&path).expect("the pinned fixture"); |
| 397 | 464 | let _ = crate::stream::decode_stream(&bytes); |
| 398 | 465 | } |
| 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 | + } | |
| 399 | 560 | } |
crates/ec-av1/src/msac.rsmodified+9 -9
| @@ -1133,16 +1133,16 @@ pub(crate) mod symtrace { | ||
| 1133 | 1133 | let (buf, n) = &mut *b.borrow_mut(); |
| 1134 | 1134 | if !buf.is_empty() { |
| 1135 | 1135 | // 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. | |
| 1138 | 1140 | 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 | + ); | |
| 1146 | 1146 | } |
| 1147 | 1147 | buf.clear(); |
| 1148 | 1148 | *n = 0; |
crates/ec-av1/src/stream.rsmodified+29 -18
| @@ -2214,7 +2214,11 @@ fn decode_frame( | ||
| 2214 | 2214 | crate::dumpio::write_planes( |
| 2215 | 2215 | "EC_AV1_DECODE_ORDER_DUMP", |
| 2216 | 2216 | 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 | + ], | |
| 2218 | 2222 | ); |
| 2219 | 2223 | } |
| 2220 | 2224 | // lane-hidden r1: the frame exactly as it is about to be stored into |
| @@ -2257,8 +2261,11 @@ fn decode_frame( | ||
| 2257 | 2261 | // writer that builds the wrong buffer still gets caught. |
| 2258 | 2262 | let sample_width = if bit_depth == 8 { 1 } else { 2 }; |
| 2259 | 2263 | 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 | + ); | |
| 2262 | 2269 | for plane in [&picture.y, &picture.u, &picture.v] { |
| 2263 | 2270 | if bit_depth == 8 { |
| 2264 | 2271 | f.write(&plane.iter().map(|&s| s as u8).collect::<Vec<u8>>()); |
| @@ -14188,7 +14195,7 @@ exit 0 | ||
| 14188 | 14195 | assert_eq!(a.len(), b.len(), "{name}: frame {i} {plane} plane length"); |
| 14189 | 14196 | if a != b { |
| 14190 | 14197 | 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); | |
| 14192 | 14199 | let at = a |
| 14193 | 14200 | .iter() |
| 14194 | 14201 | .zip(b.iter()) |
| @@ -18198,7 +18205,7 @@ exit 0 | ||
| 18198 | 18205 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 18199 | 18206 | if got.y != want.y || got.u != want.u || got.v != want.v { |
| 18200 | 18207 | 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); | |
| 18202 | 18209 | let first = |a: &Vec<u16>, b: &Vec<u16>| { |
| 18203 | 18210 | a.iter() |
| 18204 | 18211 | .zip(b) |
| @@ -23167,7 +23174,7 @@ exit 0 | ||
| 23167 | 23174 | Ok(frames) => frames, |
| 23168 | 23175 | Err(e) => { |
| 23169 | 23176 | 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); | |
| 23171 | 23178 | panic!( |
| 23172 | 23179 | "{name}: decode_stream refused a real aomenc stream: {e} (pinned at {})", |
| 23173 | 23180 | pin.display() |
| @@ -23187,7 +23194,7 @@ exit 0 | ||
| 23187 | 23194 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 23188 | 23195 | if got.y != want.y || got.u != want.u || got.v != want.v { |
| 23189 | 23196 | 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); | |
| 23191 | 23198 | panic!( |
| 23192 | 23199 | "{name}: frame {i} mismatch vs ffmpeg -- stream pinned at {}", |
| 23193 | 23200 | pin.display() |
| @@ -23586,7 +23593,7 @@ exit 0 | ||
| 23586 | 23593 | } |
| 23587 | 23594 | Err(e) => { |
| 23588 | 23595 | 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); | |
| 23590 | 23597 | panic!( |
| 23591 | 23598 | "{name}: decode_stream failed on a real aomenc stream (cq={cq} period={period}): {e} (pinned at {})", |
| 23592 | 23599 | pin.display() |
| @@ -23608,7 +23615,7 @@ exit 0 | ||
| 23608 | 23615 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 23609 | 23616 | if got.y != want.y || got.u != want.u || got.v != want.v { |
| 23610 | 23617 | 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); | |
| 23612 | 23619 | panic!( |
| 23613 | 23620 | "{name}: frame {i} mismatch vs ffmpeg (cq={cq} period={period}) -- stream pinned at {}", |
| 23614 | 23621 | pin.display() |
| @@ -27901,7 +27908,7 @@ exit 0 | ||
| 27901 | 27908 | "ec-av1-deblocking-gate-fail-s{}-cq{cq}.obu", |
| 27902 | 27909 | 42 + round |
| 27903 | 27910 | )); |
| 27904 | - let _ = std::fs::write(&pin, &stream); | |
| 27911 | + crate::dumpio::pin("EC_AV1_PIN", &pin, &stream); | |
| 27905 | 27912 | panic!( |
| 27906 | 27913 | "frame {i} mismatch vs ffmpeg (seed {} cq {cq}, deblock fired: \ |
| 27907 | 27914 | {counted}) -- stream pinned at {}", |
| @@ -28134,14 +28141,18 @@ exit 0 | ||
| 28134 | 28141 | // The pin goes to `fixtures/`, which outlives the session: an |
| 28135 | 28142 | // earlier version wrote to a session scratchpad, so once tmpfs |
| 28136 | 28143 | // 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. | |
| 28139 | 28150 | let sp = pin_dir(); |
| 28140 | 28151 | let stream_pin = sp.join(format!("cdfflake-stream-seed{seed}.bin")); |
| 28141 | 28152 | let ref_pin = sp.join(format!("cdfflake-ffmpeg-raw-seed{seed}.bin")); |
| 28142 | 28153 | 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}"); | |
| 28145 | 28156 | } |
| 28146 | 28157 | } |
| 28147 | 28158 | eprintln!( |
| @@ -28893,9 +28904,9 @@ exit 0 | ||
| 28893 | 28904 | if mismatched { |
| 28894 | 28905 | let pin = |
| 28895 | 28906 | 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); | |
| 28897 | 28908 | 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); | |
| 28899 | 28910 | eprintln!( |
| 28900 | 28911 | "EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}" |
| 28901 | 28912 | ); |
| @@ -29103,9 +29114,9 @@ exit 0 | ||
| 29103 | 29114 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 29104 | 29115 | if mismatched { |
| 29105 | 29116 | 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); | |
| 29107 | 29118 | 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); | |
| 29109 | 29120 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 29110 | 29121 | } |
| 29111 | 29122 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |