forge
Explore
Log in

Don't you dare go Hollow

lanes: make a narrowing-rung regression loud, and check the derivation

Browse files at this commit

c755a39130873d449fae1186f020893fe5b9fe19Ahmet Kaan GümüşParent: 0f2d966
The depth fix made the four u8-narrowing rungs correct; the harm that caused
was their SILENCE -- a segfaulting rung leaves a 0-byte file, which downstream
reads as "the oracle has no data for this stage" and becomes a phantom stage
diff, or as evidence about the decoder. Two additions:

1. ec_dump_finish(): all four narrowing rungs now close through a byte-count
   check against the shape they claim (Y+U+V at one byte per sample, crop for
   PREFILT, mi-aligned for the other three) and abort() with one self-naming
   line on a mismatch. Non-vacuous: dropping PREFILT's last luma row makes
   aomdec exit 134 with
   EC_DUMP_ABORT rung=EC_AV1_PREFILT_DUMP bit_depth=12 hbd=1 expect=30720
   wrote=30560; restoring it gives exit 0 / 30720 B. Rung 12 keeps its own
   fclose (charter: do not change rung 12) and the rewriter cannot reach it --
   it keys on a rewritten ec_dump_narrow_row call in the block. Monochrome
   expects luma only; a failed fopen skips the block, so no config writes 0.

2. scripts/check-aom-oracle-rungs.sh: derives decodeframe.c from PRISTINE
   upstream (scratch copy, real tree never written) and asserts 0 legacy row
   loops, 12 narrow_row call sites, 4 finish call sites (one per rung), rung 12
   still converting and not narrowed, and that a second run is a no-op. Exit 0
   today; deleting the depth block from a copy of the script makes it print 7
   FAILs and exit 1 in under a second, with no build.

Emitted bytes unchanged: re-paired av112bit-key.obu (12-bit) and
palette_screen_witness_10bit.obu (10-bit), all four rungs oracle vs decoder,
all IDENTICAL.
lanes/av1oraclehbd.report.mdmodified+82 -0
@@ -214,3 +214,85 @@ Rules of thumb:
214 - Rung 12 was already right and is the model to copy.214 - Rung 12 was already right and is the model to copy.
215 - A segfaulting rung is silent about pixels; the file being 0 bytes is the only215 - A segfaulting rung is silent about pixels; the file being 0 bytes is the only
216 signal, and it looks exactly like a decoder crash from the outside.216 signal, and it looks exactly like a decoder crash from the outside.
217+
218+## 8. Addendum (round 2, after PASS) — loud failure and the derivation check
219+
220+### 8.1 Blast radius, stated plainly
221+
222+Affected rungs, all four of them, all of them on **every** 10/12-bit stream:
223+`EC_AV1_PREFILT_DUMP`, `EC_AV1_POSTDEBLOCK_DUMP`, `EC_AV1_PREFILT_WIDE_DUMP`,
224+`EC_AV1_POSTCDEF_DUMP`. `EC_AV1_FINAL_DUMP` (rung 12) was never affected and is
225+still untouched.
226+
227+The **shared** oracle reproduces all four 139s on the same fixture
228+(`~/.cache/aom-oracle/build/aomdec`, unmodified — see §5's last table), so this
229+was pre-existing for every lane that ever paired against those four rungs, not
230+something the private copy introduced. The damage is the *silence*: the rungs
231+left a 0-byte file behind, which downstream reads as "the oracle has no data
232+for this stage" and turns into a phantom stage diff — or, after the crash is
233+noticed, as evidence about the decoder when it is evidence about the oracle.
234+
235+Rung 12 was correct all along, and that is what made the fix's shape obvious
236+rather than a guess: it is the one rung that calls
237+`CONVERT_TO_SHORTPTR(ec_p8[ec_pl])` and passes `2` as the element size. The
238+four broken rungs are the same code with that one conversion missing.
239+
240+### 8.2 The failure is now loud
241+
242+`ec_dump_finish()` is called instead of `fclose()` by all four narrowing rungs
243+(rung 12 keeps its own `fclose`). It compares the bytes the rung just wrote
244+against the shape that rung claims — `Y + U + V` at one byte per sample, crop
245+extent for PREFILT, mi-aligned extent for the other three — and on a mismatch
246+prints one line and `abort()`s:
247+
248+```
249+EC_DUMP_ABORT rung=EC_AV1_PREFILT_DUMP bit_depth=12 hbd=1 expect=30720 wrote=30560 (one byte per sample, Y+U+V)
250+```
251+
252+Measured non-vacuity: dropping PREFILT's last luma row (`ec_r <
253+y_crop_height - 1`) makes aomdec exit **134** with exactly that line; restoring
254+it gives exit 0 and 30720 B. A future narrowing regression now dies inside the
255+oracle with a self-naming message instead of handing the decoder an empty file.
256+
257+No configuration legitimately writes 0 bytes: a frame's crop extent is
258+non-zero by construction, and a failed `fopen` skips the whole block, so a bad
259+output path is a missing file, not an abort. Monochrome streams are handled
260+(`expect = Y` when `num_planes == 1`).
261+
262+Wiring is keyed on the presence of a rewritten `ec_dump_narrow_row` call in the
263+block, so rung 12 cannot be reached by the rewriter, and the terminator
264+alternation (`fclose(ec_f);` or an existing `ec_dump_finish(...)`) keeps each
265+block matched to its own close — a re-run cannot run one block's body forward
266+into a later rung's `fclose`.
267+
268+### 8.3 Anti-regression for the script itself
269+
270+`scripts/check-aom-oracle-rungs.sh` — a runnable command, not a note:
271+
272+```
273+scripts/check-aom-oracle-rungs.sh [SRC_DIR] [BASE_REF] # defaults: ~/.cache/aom-oracle/src, v3.13.3
274+```
275+
276+It copies the oracle source to a scratch dir (the real tree is never written),
277+replaces `av1/decoder/decodeframe.c` with the **pristine upstream** file from
278+`$BASE_REF`, runs `instrument-aom-oracle.sh` over the copy, and asserts:
279+
280+- 0 legacy `fwrite(ec_b->…_buffer + ec_r*ec_b->…, 1, …)` row loops;
281+- exactly 12 `ec_dump_narrow_row` call sites (4 rungs x 3 planes);
282+- exactly 4 `ec_dump_finish` call sites, one per named narrowing rung;
283+- rung 12 still calls `CONVERT_TO_SHORTPTR` and is NOT routed through the
284+ narrowing checker;
285+- a second run of the instrument script changes nothing (idempotence).
286+
287+Current output: 10 `ok` lines, exit 0. Non-vacuity: with the depth block
288+deleted from a copy of the script, the same command prints 7 `FAIL` lines —
289+`legacy u8 row loops left: got 12, want 0` — and exits 1, in under a second and
290+with no build. A future edit that reintroduces the old expression reds on the
291+derivation path instead of on somebody's decode.
292+
293+### 8.4 Bytes unchanged by the loudness work
294+
295+Re-ran the full pairing after wiring the checker: `av112bit-key.obu` (12-bit)
296+and `palette_screen_witness_10bit.obu` (10-bit), all four rungs, oracle vs
297+decoder — all `IDENTICAL` (30720 B and 73728 B). The checker only reads
298+`ftell`; it cannot move a byte.
scripts/check-aom-oracle-rungs.shadded+92 -0
@@ -0,0 +1,92 @@
1+#!/usr/bin/env bash
2+# Anti-regression check for the aomdec dump rungs that
3+# scripts/instrument-aom-oracle.sh derives.
4+#
5+# Why this exists: the four u8-narrowing rungs (PREFILT, POSTDEBLOCK,
6+# PREFILT_WIDE, POSTCDEF) shipped with `fwrite(plane + r*stride, 1, w, f)`,
7+# which on a 10/12-bit stream reads a HALVED plane pointer
8+# (CONVERT_TO_BYTEPTR in aom_ports/mem.h) and SIGSEGVs with a 0-byte file
9+# (lanes/av1oraclehbd.report.md). A rung defect that only shows up as an
10+# aomdec crash during a decode is found late; this reds on the DERIVATION
11+# instead, in seconds, without a build.
12+#
13+# What it does: copies the oracle source tree to a scratch dir, replaces
14+# av1/decoder/decodeframe.c with the PRISTINE upstream file, runs the
15+# instrument script over the copy, and asserts the derived file's shape. The
16+# real oracle tree is never written to.
17+#
18+# Usage:
19+# scripts/check-aom-oracle-rungs.sh [SRC_DIR] [BASE_REF]
20+# Defaults: $AOM_ORACLE_SRC or ~/.cache/aom-oracle/src, and base ref v3.13.3
21+# (override with AOM_ORACLE_BASE_REF). Exit 0 = all assertions hold.
22+set -euo pipefail
23+
24+SRC="${1:-${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}}"
25+BASE="${2:-${AOM_ORACLE_BASE_REF:-v3.13.3}}"
26+HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
27+DERIVED="$SRC/av1/decoder/decodeframe.c"
28+
29+[ -f "$DERIVED" ] || { echo "no oracle source at $DERIVED" >&2; exit 1; }
30+git -C "$SRC" rev-parse --verify "$BASE^{commit}" >/dev/null 2>&1 || {
31+ echo "base ref '$BASE' is not a commit in $SRC" >&2
32+ exit 1
33+}
34+
35+WORK="$(mktemp -d "${TMPDIR:-$HOME/.cache/tmp}/aom-rung-check.XXXXXX")"
36+trap 'rm -rf "$WORK"' EXIT
37+cp -a "$SRC" "$WORK/src"
38+git -C "$SRC" show "$BASE:av1/decoder/decodeframe.c" > "$WORK/src/av1/decoder/decodeframe.c"
39+
40+AOM_ORACLE_SRC="$WORK/src" bash "$HERE/instrument-aom-oracle.sh" > "$WORK/derive.log" 2>&1 || {
41+ echo "FAIL: instrument-aom-oracle.sh errored on a pristine tree:" >&2
42+ tail -20 "$WORK/derive.log" >&2
43+ exit 1
44+}
45+cp "$WORK/src/av1/decoder/decodeframe.c" "$WORK/derived.c"
46+
47+# Re-running must be a no-op, or the repair pass is not idempotent and a
48+# second run would rewrite a tree under someone's feet.
49+AOM_ORACLE_SRC="$WORK/src" bash "$HERE/instrument-aom-oracle.sh" > "$WORK/rerun.log" 2>&1
50+cmp -s "$WORK/derived.c" "$WORK/src/av1/decoder/decodeframe.c" || {
51+ echo "FAIL: a second instrument-aom-oracle.sh run changed the derived file" >&2
52+ exit 1
53+}
54+
55+fail=0
56+check() { # description, actual, expected
57+ if [ "$2" = "$3" ]; then
58+ printf 'ok %-58s %s\n' "$1" "$2"
59+ else
60+ printf 'FAIL %-58s got %s, want %s\n' "$1" "$2" "$3" >&2
61+ fail=1
62+ fi
63+}
64+
65+legacy=$(grep -c 'fwrite(ec_b->[yuv]_buffer + ec_r \* ec_b->' "$WORK/derived.c" || true)
66+check "legacy u8 row loops left in the derived file" "$legacy" "0"
67+
68+rows=$(grep -c 'ec_dump_narrow_row(ec_f, ec_b, ec_b->[yuv]_buffer' "$WORK/derived.c" || true)
69+check "ec_dump_narrow_row call sites (4 rungs x 3 planes)" "$rows" "12"
70+
71+fin=$(grep -c 'ec_dump_finish(ec_f, "EC_AV1_' "$WORK/derived.c" || true)
72+check "ec_dump_finish call sites (one per narrowing rung)" "$fin" "4"
73+
74+for rung in EC_AV1_PREFILT_DUMP EC_AV1_POSTDEBLOCK_DUMP EC_AV1_PREFILT_WIDE_DUMP \
75+ EC_AV1_POSTCDEF_DUMP; do
76+ n=$(grep -c "ec_dump_finish(ec_f, \"$rung\"" "$WORK/derived.c" || true)
77+ check "byte-count check wired into $rung" "$n" "1"
78+done
79+
80+# Rung 12 (EC_AV1_FINAL_DUMP) is the depth-correct one and must stay that way:
81+# it converts, it writes 2 bytes per sample, and it is not routed through the
82+# narrowing checker.
83+conv=$(grep -c 'CONVERT_TO_SHORTPTR(ec_p8\[ec_pl\])' "$WORK/derived.c" || true)
84+check "rung 12 still converts plane pointers" "$conv" "1"
85+r12=$(grep -c 'ec_dump_finish(ec_f, "EC_AV1_FINAL_DUMP"' "$WORK/derived.c" || true)
86+check "rung 12 not routed through the narrowing checker" "$r12" "0"
87+
88+if [ "$fail" -ne 0 ]; then
89+ echo "FAIL: derived decodeframe.c violates the rung contract (base $BASE)" >&2
90+ exit 1
91+fi
92+echo "ok instrument-aom-oracle.sh derives the depth-correct, byte-checked rungs (base $BASE)"
scripts/instrument-aom-oracle.shmodified+86 -7
@@ -961,20 +961,62 @@ static void ec_dump_narrow_row(FILE *f, const YV12_BUFFER_CONFIG *b,
961 aom_free(narrow);961 aom_free(narrow);
962 }962 }
963 963
964+/* EC_INSTRUMENTED_DUMP_CHECK: the harm the depth bug above caused is not a
965+ * wrong number, it is a SILENT 0-byte dump -- downstream that reads as "the
966+ * oracle has no data for this stage" and becomes a phantom stage diff. So
967+ * every narrowing rung checks the byte count it just produced against the
968+ * shape it claims to write and aborts IN THE ORACLE, naming itself, instead
969+ * of handing the decoder an empty file. `expect` is Y + U + V at ONE BYTE PER
970+ * SAMPLE: the crop extent when `aligned` is 0 (PREFILT), the mi-aligned
971+ * extent when 1 (PREFILT_WIDE / POSTDEBLOCK / POSTCDEF). No configuration
972+ * writes 0 bytes: a frame's crop extent is non-zero by construction, and a
973+ * failed fopen skips the block entirely (so a bad path is not an abort). */
974+static void ec_dump_finish(FILE *f, const char *rung,
975+ const YV12_BUFFER_CONFIG *b, int num_planes,
976+ int aligned) {
977+ const size_t y = aligned ? (size_t)b->y_width * b->y_height
978+ : (size_t)b->y_crop_width * b->y_crop_height;
979+ const size_t uv = aligned ? (size_t)b->uv_width * b->uv_height
980+ : (size_t)b->uv_crop_width * b->uv_crop_height;
981+ const size_t expect = y + (num_planes > 1 ? 2 * uv : 0);
982+ const long wrote = ftell(f);
983+ if (wrote < 0 || (size_t)wrote != expect || fflush(f) != 0) {
984+ fprintf(stderr,
985+ "EC_DUMP_ABORT rung=%s bit_depth=%u hbd=%d expect=%zu wrote=%ld "
986+ "(one byte per sample, Y+U+V)\\n",
987+ rung, b->bit_depth,
988+ (b->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0, expect, wrote);
989+ abort();
990+ }
991+ fclose(f);
992+}
993+
964 '''994 '''
965 995
966-if "EC_INSTRUMENTED_NARROW_ROW" not in s:996+MARKER_ROW = "EC_INSTRUMENTED_NARROW_ROW"
967- # Must precede every user: the POSTCDEF rung was refactored into a static997+MARKER_CHECK = "EC_INSTRUMENTED_DUMP_CHECK"
968- # helper that sits ABOVE av1_decode_tg_tiles_and_wrapup, so anchor there998+missing = ""
969- # when it exists and at the function itself otherwise.999+if MARKER_ROW not in s:
1000+ missing = helper
1001+elif MARKER_CHECK not in s:
1002+ # The tree already carries the row helper from an earlier run of this
1003+ # script: add the byte-count checker on its own so a repaired tree
1004+ # converges instead of erroring.
1005+ missing = helper[helper.index("/* " + MARKER_CHECK):]
1006+if missing:
1007+ # Both helpers must precede every user: the POSTCDEF rung was refactored
1008+ # into a static helper that sits ABOVE av1_decode_tg_tiles_and_wrapup, so
1009+ # anchor there when it exists and at the function itself otherwise.
970 for anchor in ("static void ec_dump_postcdef(const AV1_COMMON *cm, int num_planes) {",1010 for anchor in ("static void ec_dump_postcdef(const AV1_COMMON *cm, int num_planes) {",
971 "void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,"):1011 "void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,"):
972 if anchor in s:1012 if anchor in s:
973- s = s.replace(anchor, helper + anchor, 1)1013+ s = s.replace(anchor, missing + anchor, 1)
974 break1014 break
975 else:1015 else:
976- raise SystemExit("narrow-row helper: no anchor before the dump rungs")1016+ raise SystemExit("dump helpers: no anchor before the dump rungs")
977- print("narrow-row helper inserted")1017+ print("dump helpers inserted: %s"
1018+ % ("narrow-row + byte-count check" if len(missing) == len(helper)
1019+ else "byte-count check only"))
978 1020
979 row_loop = re.compile(1021 row_loop = re.compile(
980 r"for \(int ec_r = 0; ec_r < ec_b->(\w+); \+\+ec_r\)\n"1022 r"for \(int ec_r = 0; ec_r < ec_b->(\w+); \+\+ec_r\)\n"
@@ -993,5 +1035,42 @@ if rewritten:
993 print("narrow-row depth fix: rewrote %d plane row loops" % rewritten)1035 print("narrow-row depth fix: rewrote %d plane row loops" % rewritten)
994 else:1036 else:
995 print("narrow-row depth fix: no legacy row loop left (already depth-correct)")1037 print("narrow-row depth fix: no legacy row loop left (already depth-correct)")
1038+
1039+# Close each narrowing rung through the checker. Keyed on the presence of a
1040+# rewritten row loop, so rung 12 (EC_AV1_FINAL_DUMP, 2 bytes per sample and
1041+# already depth-correct) is never reached: its block has no ec_dump_narrow_row.
1042+# The terminator alternation keeps each block matched to ITS OWN close, so a
1043+# re-run cannot run a block's body forward into a later rung's fclose.
1044+dump_block = re.compile(
1045+ r'(FILE \*ec_f = fopen\(ec_path, "wb"\);)(.*?)'
1046+ r'((?:fclose\(ec_f\);)|(?:ec_dump_finish\([^;]*\);))',
1047+ re.S)
1048+
1049+WIRED = []
1050+
1051+def finish(match):
1052+ opened, body, closed = match.groups()
1053+ if "ec_dump_narrow_row" not in body:
1054+ return match.group(0)
1055+ # The rung name can sit ABOVE the fopen (the POSTCDEF rung was refactored
1056+ # into a helper that does getenv -> fopen at the top), so take the nearest
1057+ # preceding EC_AV1_* env lookup rather than searching the block body.
1058+ preceding = re.findall(r'getenv\("(EC_AV1_[A-Z_]+)"\)', s[:match.start()])
1059+ if not preceding:
1060+ raise SystemExit("dump check: no rung env var above a narrowing block")
1061+ aligned = 0 if "y_crop_height" in body else 1
1062+ if closed.startswith("fclose"):
1063+ WIRED.append(preceding[-1])
1064+ return "%s%s%s" % (
1065+ opened, body,
1066+ 'ec_dump_finish(ec_f, "%s", ec_b, num_planes, %d);' % (preceding[-1], aligned))
1067+
1068+
1069+s, _ = dump_block.subn(finish, s)
1070+if WIRED:
1071+ print("dump byte-count check: wired into %d narrowing rung(s): %s"
1072+ % (len(WIRED), ", ".join(WIRED)))
1073+else:
1074+ print("dump byte-count check: every narrowing rung already checks its bytes")
996 open(path, "w").write(s)1075 open(path, "w").write(s)
997 PYN1076 PYN