forge
Explore
Log in

Don't you dare go Hollow

gate: control the class comparator-that-never-reads-the-oracle with a one-byte-wrong oracle

Browse files at this commit

ec9aa3fa22c751839e64e8dd55b16433e4146cebAhmet Kaan GümüşParent: 86e6457
The retracted av1422remeasure per-cell counter packed our decoded samples
and compared that buffer against the same samples: aomdec's output was
written, length-checked, never read, and it reported '0 wrong on 18/18'.
Nothing about that mistake is visible from outside a helper, so it cannot
be caught by reading one.

every_oracle_comparator_reds_on_a_one_byte_wrong_oracle pins it from the
other side. aomdec_path() grows a thread-local override (same shape as the
existing FINAL_DUMP_PREFIX, and thread-local because the crate denies
unsafe_code and a process-global env var would leak one test's tampered
oracle into every sibling's comparison). The test points the override at a
shim that runs the REAL aomdec and rotates byte 7 of the --rawvideo output
and of EC_AV1_FINAL_DUMP frame 0, then asserts, on the byte-exact
420_odd65x65_key pin:

  arm 1, real oracle       -> all five comparators green
  arm 2, one-byte-wrong    -> all five comparators red

Control proof: mutating all five comparators to compare our buffer against
itself (the retracted defect) makes arm 1 pass and the control report
'5 of 5 stayed GREEN'. Four gates that reached the oracle without
lock_gate_counters() now take it.
crates/ec-av1/src/stream.rsmodified+251 -0
@@ -4274,6 +4274,10 @@ pub(crate) mod tests {
42744274 fn a_real_aomenc_segmentation_stream_with_map_inheritance_decodes_pixel_exact() {
42754275 const NAME: &str =
42764276 "a_real_aomenc_segmentation_stream_with_map_inheritance_decodes_pixel_exact";
4277+ // lane-av1cmpaudit2: this gate reaches the oracle without the gate
4278+ // mutex, so a sibling flipping `EC_AV1_AOMDEC` (the one-byte-wrong
4279+ // oracle control) could hand it a tampered oracle mid-compare.
4280+ let _gate_lock = lock_gate_counters();
42774281 if !have_aomenc() {
42784282 eprintln!("SKIP {NAME}: no aomenc");
42794283 return;
@@ -10195,9 +10199,35 @@ pub(crate) mod tests {
1019510199 present
1019610200 }
1019710201
10202+ // lane-av1cmpaudit2: this thread's oracle override, set by
10203+ // `set_oracle_path` and consulted by `aomdec_path` ahead of the env var.
10204+ //
10205+ // It is thread-local for the same reason `FINAL_DUMP_PREFIX` is: the
10206+ // control test `every_oracle_comparator_reds_on_a_one_byte_wrong_oracle`
10207+ // has to hand the helpers a DELIBERATELY WRONG oracle, and a
10208+ // process-global `std::env::set_var` could not do that safely -- the
10209+ // crate denies `unsafe_code`, and even where it did not, one test's
10210+ // tampered oracle would leak into every sibling test's comparison
10211+ // running on another thread. A thread-local override confines the wrong
10212+ // oracle to the one thread that asked for it.
10213+ thread_local! {
10214+ static ORACLE_PATH: std::cell::RefCell<Option<std::path::PathBuf>> =
10215+ const { std::cell::RefCell::new(None) };
10216+ }
10217+
10218+ /// Points this thread's oracle helpers at `path`. `None` restores the
10219+ /// real `aomdec`.
10220+ fn set_oracle_path(path: Option<std::path::PathBuf>) {
10221+ ORACLE_PATH.with(|c| *c.borrow_mut() = path);
10222+ }
10223+
10224+
1019810225 /// The instrumented `aomdec` beside the `aomenc` oracle
1019910226 /// (`scripts/build-aom-oracle.sh` + `scripts/instrument-aom-oracle.sh`).
1020010227 fn aomdec_path() -> std::path::PathBuf {
10228+ if let Some(p) = ORACLE_PATH.with(|c| c.borrow().clone()) {
10229+ return p;
10230+ }
1020110231 if let Some(p) = std::env::var_os("EC_AV1_AOMDEC") {
1020210232 return std::path::PathBuf::from(p);
1020310233 }
@@ -10385,6 +10415,10 @@ pub(crate) mod tests {
1038510415 #[test]
1038610416 fn the_counting_oracle_diff_reports_a_real_difference() {
1038710417 const NAME: &str = "the_counting_oracle_diff_reports_a_real_difference";
10418+ // lane-av1cmpaudit2: same mutex as every other oracle gate -- this
10419+ // one reads `aomdec_path()`, so it must not overlap a sibling's
10420+ // tampered-oracle run.
10421+ let _gate_lock = lock_gate_counters();
1038810422 let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
1038910423 .join("fixtures")
1039010424 .join("420_oddheight_320x232.obu");
@@ -10405,6 +10439,218 @@ pub(crate) mod tests {
1040510439 eprintln!("{NAME}: witness reports Y={wy} U={wu} V={wv}, frames_exact={exact}/{frames}");
1040610440 }
1040710441
10442+ /// lane-av1cmpaudit2: the permanent CONTROL for the class
10443+ /// `comparator-that-never-reads-the-oracle`.
10444+ ///
10445+ /// The retracted predecessor lane's per-cell counter reported "0 wrong"
10446+ /// on 18/18 cells -- including cells that differ from `aomdec
10447+ /// --rawvideo` in 17658 samples -- because it packed OUR decoded samples
10448+ /// into a buffer and compared that buffer against the same samples.
10449+ /// `aomdec`'s output was written, length-checked, and never read.
10450+ /// Nothing about that mistake is visible from the outside: the helper's
10451+ /// signature, its panic text and its return type are all exactly what a
10452+ /// working comparator has, and no amount of reading the source of a
10453+ /// healthy-looking helper proves the next one is healthy.
10454+ ///
10455+ /// So the class is pinned here from the other direction.
10456+ /// `EC_AV1_AOMDEC` is already the crate's oracle override, so this test
10457+ /// points every oracle helper at a shim that runs the REAL `aomdec` and
10458+ /// then corrupts exactly ONE byte of whatever it wrote -- the
10459+ /// `--rawvideo` output and the `EC_AV1_FINAL_DUMP` frame 0 -- leaving
10460+ /// every other sample a genuine decode. On a pin that IS byte-exact
10461+ /// against the real oracle:
10462+ ///
10463+ /// 1. **arm 1, real oracle: every helper is GREEN.** Without this arm,
10464+ /// arm 2's red could just be a stream that was never exact.
10465+ /// 2. **arm 2, one-byte-wrong oracle: every helper is RED.** A helper
10466+ /// that stays green here is the retracted defect itself: it cannot
10467+ /// fail, so its green is worth nothing.
10468+ ///
10469+ /// One test, five helpers, and it goes red for the exact mistake that
10470+ /// was retracted. Measured over this file: the other 62 tests that
10471+ /// reach an oracle helper all take `lock_gate_counters()` before their
10472+ /// first oracle call, which is what makes the process-global
10473+ /// `EC_AV1_AOMDEC` safe to flip here.
10474+ #[test]
10475+ fn every_oracle_comparator_reds_on_a_one_byte_wrong_oracle() {
10476+ const NAME: &str = "every_oracle_comparator_reds_on_a_one_byte_wrong_oracle";
10477+ let _gate_lock = lock_gate_counters();
10478+ if !aomdec_available(NAME) {
10479+ return;
10480+ }
10481+ let pin = crate_pin("420_odd65x65_key.obu");
10482+ let stream = std::fs::read(&pin).unwrap_or_else(|e| {
10483+ panic!(
10484+ "{NAME}: the control's pin {} is missing ({e}) -- restore the committed copy",
10485+ pin.display()
10486+ )
10487+ });
10488+ assert_eq!(
10489+ stream_bit_depth(&stream, NAME),
10490+ 8,
10491+ "{NAME}: the control's pin must be the 8-bit cell the shim corrupts"
10492+ );
10493+ let plane_samples = {
10494+ let frames = decode_stream(&stream).expect("the control's pin must decode");
10495+ let f = &frames[0];
10496+ [f.y.len(), f.u.len(), f.v.len()]
10497+ };
10498+
10499+ // ---- arm 1: the real oracle, every comparator green ------------
10500+ {
10501+ let obu = std::env::temp_dir().join(format!("{NAME}-arm1.obu"));
10502+ std::fs::write(&obu, &stream).expect("writing the pin for arm 1");
10503+ assert_rawvideo_matches(&obu, &stream, "oracle-control-arm1-rawvideo", 1, 8);
10504+ let _ = std::fs::remove_file(&obu);
10505+ let (frames, hidden) =
10506+ decode_all_frames_vs_oracle(&stream, "oracle-control-arm1-alldumps");
10507+ assert_eq!(
10508+ (frames, hidden),
10509+ (1, 0),
10510+ "{NAME}: arm 1 expects one decode-order frame and no hidden frames"
10511+ );
10512+ let (wy, wu, wv, n, exact) =
10513+ count_rawvideo_diffs(&stream, "oracle-control-arm1-count")
10514+ .expect("arm 1: the two sides must be byte-comparable");
10515+ assert_eq!(
10516+ (wy, wu, wv),
10517+ (0, 0, 0),
10518+ "{NAME}: arm 1 runs against the REAL oracle, so every plane must be zero"
10519+ );
10520+ assert_eq!(
10521+ (n, exact),
10522+ (1, 1),
10523+ "{NAME}: arm 1 expects one shown frame that is byte-identical"
10524+ );
10525+ let (differing, first) = key_frame_diffs(&stream, "oracle-control-arm1-kf");
10526+ assert_eq!(
10527+ (differing, first),
10528+ (0, None),
10529+ "{NAME}: arm 1's key frame must be byte-identical to the oracle's"
10530+ );
10531+ let [y, u, v] =
10532+ oracle_plane_diffs(&stream, "oracle-control-arm1-planes", plane_samples);
10533+ assert_eq!(
10534+ [y, u, v],
10535+ [0, 0, 0],
10536+ "{NAME}: arm 1 must be zero on every plane against the real oracle"
10537+ );
10538+ }
10539+
10540+ // ---- the one-byte-wrong oracle ---------------------------------
10541+ let dir = std::env::temp_dir().join(format!("ec-av1-{NAME}-{}", std::process::id()));
10542+ let _ = std::fs::remove_dir_all(&dir);
10543+ std::fs::create_dir_all(&dir).expect("scratch dir");
10544+ let shim = dir.join("one-byte-wrong-aomdec.sh");
10545+ // The real oracle's path rides in a sibling file rather than inside
10546+ // the script, so a path with a quote or a space cannot mangle it and
10547+ // the script body needs no `format!` escaping at all.
10548+ std::fs::write(dir.join("real-aomdec"), aomdec_path().as_os_str().as_encoded_bytes())
10549+ .expect("recording the real oracle's path for the shim");
10550+ // Byte 7 of both the `--rawvideo` output and the `EC_AV1_FINAL_DUMP`
10551+ // frame 0 is inside the Y plane, and its value is rotated by 128 so
10552+ // the write always changes it. Nothing else is touched: the stream,
10553+ // the geometry, the frame count and every other sample are a genuine
10554+ // `aomdec` decode.
10555+ let script = r#"#!/bin/sh
10556+REAL=$(cat "$(dirname "$0")/real-aomdec")
10557+rawvideo=0
10558+out=""
10559+prev=""
10560+for a in "$@"; do
10561+ if [ "$a" = "--rawvideo" ]; then rawvideo=1; fi
10562+ if [ "$prev" = "-o" ]; then out="$a"; fi
10563+ prev="$a"
10564+done
10565+"$REAL" "$@" || exit $?
10566+flip() {
10567+ [ -s "$1" ] || return 0
10568+ b=$(od -An -tu1 -j 7 -N 1 "$1" 2>/dev/null | tr -d ' \n')
10569+ [ -n "$b" ] || return 0
10570+ esc=$(printf '\\%03o' $(( (b + 128) % 256 )))
10571+ printf "$esc" | dd of="$1" bs=1 seek=7 count=1 conv=notrunc 2>/dev/null
10572+ return 0
10573+}
10574+[ "$rawvideo" = 1 ] && flip "$out"
10575+[ -n "$EC_AV1_FINAL_DUMP" ] && flip "$EC_AV1_FINAL_DUMP.f0"
10576+exit 0
10577+"#;
10578+ std::fs::write(&shim, script).expect("writing the oracle shim");
10579+ #[cfg(unix)]
10580+ {
10581+ use std::os::unix::fs::PermissionsExt;
10582+ std::fs::set_permissions(&shim, std::fs::Permissions::from_mode(0o755))
10583+ .expect("the oracle shim must be executable");
10584+ }
10585+
10586+ // ---- arm 2: same pin, oracle off by ONE byte -------------------
10587+ // The override is THREAD-LOCAL, so the tampered oracle is confined
10588+ // to this thread and no sibling test can see it; it is still cleared
10589+ // before any result is asserted, so a red arm cannot leave this
10590+ // test's own later work reading a wrong oracle.
10591+ let reds: [bool; 5];
10592+ {
10593+ set_oracle_path(Some(shim.clone()));
10594+ let panicked = |f: &dyn Fn()| {
10595+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).is_err()
10596+ };
10597+ let rawvideo_red = panicked(&|| {
10598+ let obu = std::env::temp_dir().join(format!("{NAME}-arm2.obu"));
10599+ std::fs::write(&obu, &stream).expect("writing the pin for arm 2");
10600+ assert_rawvideo_matches(&obu, &stream, "oracle-control-arm2-rawvideo", 1, 8);
10601+ });
10602+ let _ = std::fs::remove_file(std::env::temp_dir().join(format!("{NAME}-arm2.obu")));
10603+ let dumps_red = panicked(&|| {
10604+ decode_all_frames_vs_oracle(&stream, "oracle-control-arm2-alldumps");
10605+ });
10606+ let count_red = {
10607+ let counted = count_rawvideo_diffs(&stream, "oracle-control-arm2-count")
10608+ .unwrap_or_else(|| {
10609+ panic!(
10610+ "{NAME}: a single flipped oracle byte must NOT change the buffer \
10611+ LENGTH -- returning None means the shim corrupted more than the \
10612+ comparison"
10613+ )
10614+ });
10615+ counted.0 + counted.1 + counted.2 > 0 && counted.4 < counted.3
10616+ };
10617+ let kf_red = {
10618+ let (differing, first) = key_frame_diffs(&stream, "oracle-control-arm2-kf");
10619+ differing > 0 && first.is_some()
10620+ };
10621+ let planes_red = {
10622+ let [y, u, v] =
10623+ oracle_plane_diffs(&stream, "oracle-control-arm2-planes", plane_samples);
10624+ y + u + v > 0
10625+ };
10626+ set_oracle_path(None);
10627+ let _ = std::fs::remove_dir_all(&dir);
10628+ reds = [rawvideo_red, dumps_red, count_red, kf_red, planes_red];
10629+ }
10630+ // One assert, naming EVERY comparator that stayed green: a partial
10631+ // run would report the first and hide whether the other four are
10632+ // covered.
10633+ let blind: Vec<&str> = [
10634+ ("assert_rawvideo_matches", reds[0]),
10635+ ("decode_all_frames_vs_oracle", reds[1]),
10636+ ("count_rawvideo_diffs", reds[2]),
10637+ ("key_frame_diffs", reds[3]),
10638+ ("oracle_plane_diffs", reds[4]),
10639+ ]
10640+ .into_iter()
10641+ .filter(|(_, red)| !red)
10642+ .map(|(what, _)| what)
10643+ .collect();
10644+ assert!(
10645+ blind.is_empty(),
10646+ "{NAME}: {} of 5 oracle comparators stayed GREEN against an oracle that is wrong \
10647+ by exactly one byte -- {blind:?} -- they cannot fail, so their green proves \
10648+ nothing (class `comparator-that-never-reads-the-oracle`)",
10649+ blind.len()
10650+ );
10651+ eprintln!("{NAME}: all five oracle comparators red on a one-byte-wrong oracle");
10652+ }
10653+
1040810654 /// lane-av1422anom: the odd-frame-HEIGHT 4:2:0 reconstruction defect,
1040910655 /// pinned and ratcheted.
1041010656 ///
@@ -10427,6 +10673,8 @@ pub(crate) mod tests {
1042710673 fn the_pinned_420_oddheight_witness_is_pinned_and_ratchets_its_reconstruction_defect() {
1042810674 const NAME: &str =
1042910675 "the_pinned_420_oddheight_witness_is_pinned_and_ratchets_its_reconstruction_defect";
10676+ // lane-av1cmpaudit2: same mutex as every other oracle gate.
10677+ let _gate_lock = lock_gate_counters();
1043010678 const FILE: &str = "420_oddheight_320x232.obu";
1043110679 const BYTES: usize = 15773;
1043210680 // sha256 6832c3cd867f5fc491b45fd67f77c38453db0ca6d461e7d225e6276f4d0784c8,
@@ -38150,6 +38398,9 @@ pub(crate) mod tests {
3815038398 #[test]
3815138399 #[ignore = "lane-sb128 r2 diagnostic: is the frame-6 mismatch sb128-specific?"]
3815238400 fn sb128_r2_control_sb64() {
38401+ // lane-av1cmpaudit2: this control reaches the oracle, so it takes
38402+ // the gate mutex like every other one.
38403+ let _gate_lock = lock_gate_counters();
3815338404 let (width, height) = (256usize, 192usize);
3815438405 let y4m = Command::new("ffmpeg")
3815538406 .args([