diff --git a/lanes/av1oraclehbd.report.md b/lanes/av1oraclehbd.report.md index 6639f134..e41004f1 100644 --- a/lanes/av1oraclehbd.report.md +++ b/lanes/av1oraclehbd.report.md @@ -214,3 +214,85 @@ Rules of thumb: - Rung 12 was already right and is the model to copy. - A segfaulting rung is silent about pixels; the file being 0 bytes is the only signal, and it looks exactly like a decoder crash from the outside. + +## 8. Addendum (round 2, after PASS) — loud failure and the derivation check + +### 8.1 Blast radius, stated plainly + +Affected rungs, all four of them, all of them on **every** 10/12-bit stream: +`EC_AV1_PREFILT_DUMP`, `EC_AV1_POSTDEBLOCK_DUMP`, `EC_AV1_PREFILT_WIDE_DUMP`, +`EC_AV1_POSTCDEF_DUMP`. `EC_AV1_FINAL_DUMP` (rung 12) was never affected and is +still untouched. + +The **shared** oracle reproduces all four 139s on the same fixture +(`~/.cache/aom-oracle/build/aomdec`, unmodified — see §5's last table), so this +was pre-existing for every lane that ever paired against those four rungs, not +something the private copy introduced. The damage is the *silence*: the rungs +left a 0-byte file behind, which downstream reads as "the oracle has no data +for this stage" and turns into a phantom stage diff — or, after the crash is +noticed, as evidence about the decoder when it is evidence about the oracle. + +Rung 12 was correct all along, and that is what made the fix's shape obvious +rather than a guess: it is the one rung that calls +`CONVERT_TO_SHORTPTR(ec_p8[ec_pl])` and passes `2` as the element size. The +four broken rungs are the same code with that one conversion missing. + +### 8.2 The failure is now loud + +`ec_dump_finish()` is called instead of `fclose()` by all four narrowing rungs +(rung 12 keeps its own `fclose`). It compares the bytes the rung just wrote +against the shape that rung claims — `Y + U + V` at one byte per sample, crop +extent for PREFILT, mi-aligned extent for the other three — and on a mismatch +prints one line and `abort()`s: + +``` +EC_DUMP_ABORT rung=EC_AV1_PREFILT_DUMP bit_depth=12 hbd=1 expect=30720 wrote=30560 (one byte per sample, Y+U+V) +``` + +Measured non-vacuity: dropping PREFILT's last luma row (`ec_r < +y_crop_height - 1`) makes aomdec exit **134** with exactly that line; restoring +it gives exit 0 and 30720 B. A future narrowing regression now dies inside the +oracle with a self-naming message instead of handing the decoder an empty file. + +No configuration legitimately writes 0 bytes: a frame's crop extent is +non-zero by construction, and a failed `fopen` skips the whole block, so a bad +output path is a missing file, not an abort. Monochrome streams are handled +(`expect = Y` when `num_planes == 1`). + +Wiring is keyed on the presence of a rewritten `ec_dump_narrow_row` call in the +block, so rung 12 cannot be reached by the rewriter, and the terminator +alternation (`fclose(ec_f);` or an existing `ec_dump_finish(...)`) keeps each +block matched to its own close — a re-run cannot run one block's body forward +into a later rung's `fclose`. + +### 8.3 Anti-regression for the script itself + +`scripts/check-aom-oracle-rungs.sh` — a runnable command, not a note: + +``` +scripts/check-aom-oracle-rungs.sh [SRC_DIR] [BASE_REF] # defaults: ~/.cache/aom-oracle/src, v3.13.3 +``` + +It copies the oracle source to a scratch dir (the real tree is never written), +replaces `av1/decoder/decodeframe.c` with the **pristine upstream** file from +`$BASE_REF`, runs `instrument-aom-oracle.sh` over the copy, and asserts: + +- 0 legacy `fwrite(ec_b->…_buffer + ec_r*ec_b->…, 1, …)` row loops; +- exactly 12 `ec_dump_narrow_row` call sites (4 rungs x 3 planes); +- exactly 4 `ec_dump_finish` call sites, one per named narrowing rung; +- rung 12 still calls `CONVERT_TO_SHORTPTR` and is NOT routed through the + narrowing checker; +- a second run of the instrument script changes nothing (idempotence). + +Current output: 10 `ok` lines, exit 0. Non-vacuity: with the depth block +deleted from a copy of the script, the same command prints 7 `FAIL` lines — +`legacy u8 row loops left: got 12, want 0` — and exits 1, in under a second and +with no build. A future edit that reintroduces the old expression reds on the +derivation path instead of on somebody's decode. + +### 8.4 Bytes unchanged by the loudness work + +Re-ran the full pairing after wiring the checker: `av112bit-key.obu` (12-bit) +and `palette_screen_witness_10bit.obu` (10-bit), all four rungs, oracle vs +decoder — all `IDENTICAL` (30720 B and 73728 B). The checker only reads +`ftell`; it cannot move a byte. diff --git a/scripts/check-aom-oracle-rungs.sh b/scripts/check-aom-oracle-rungs.sh new file mode 100755 index 00000000..7cc67ac6 --- /dev/null +++ b/scripts/check-aom-oracle-rungs.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# Anti-regression check for the aomdec dump rungs that +# scripts/instrument-aom-oracle.sh derives. +# +# Why this exists: the four u8-narrowing rungs (PREFILT, POSTDEBLOCK, +# PREFILT_WIDE, POSTCDEF) shipped with `fwrite(plane + r*stride, 1, w, f)`, +# which on a 10/12-bit stream reads a HALVED plane pointer +# (CONVERT_TO_BYTEPTR in aom_ports/mem.h) and SIGSEGVs with a 0-byte file +# (lanes/av1oraclehbd.report.md). A rung defect that only shows up as an +# aomdec crash during a decode is found late; this reds on the DERIVATION +# instead, in seconds, without a build. +# +# What it does: copies the oracle source tree to a scratch dir, replaces +# av1/decoder/decodeframe.c with the PRISTINE upstream file, runs the +# instrument script over the copy, and asserts the derived file's shape. The +# real oracle tree is never written to. +# +# Usage: +# scripts/check-aom-oracle-rungs.sh [SRC_DIR] [BASE_REF] +# Defaults: $AOM_ORACLE_SRC or ~/.cache/aom-oracle/src, and base ref v3.13.3 +# (override with AOM_ORACLE_BASE_REF). Exit 0 = all assertions hold. +set -euo pipefail + +SRC="${1:-${AOM_ORACLE_SRC:-$HOME/.cache/aom-oracle/src}}" +BASE="${2:-${AOM_ORACLE_BASE_REF:-v3.13.3}}" +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +DERIVED="$SRC/av1/decoder/decodeframe.c" + +[ -f "$DERIVED" ] || { echo "no oracle source at $DERIVED" >&2; exit 1; } +git -C "$SRC" rev-parse --verify "$BASE^{commit}" >/dev/null 2>&1 || { + echo "base ref '$BASE' is not a commit in $SRC" >&2 + exit 1 +} + +WORK="$(mktemp -d "${TMPDIR:-$HOME/.cache/tmp}/aom-rung-check.XXXXXX")" +trap 'rm -rf "$WORK"' EXIT +cp -a "$SRC" "$WORK/src" +git -C "$SRC" show "$BASE:av1/decoder/decodeframe.c" > "$WORK/src/av1/decoder/decodeframe.c" + +AOM_ORACLE_SRC="$WORK/src" bash "$HERE/instrument-aom-oracle.sh" > "$WORK/derive.log" 2>&1 || { + echo "FAIL: instrument-aom-oracle.sh errored on a pristine tree:" >&2 + tail -20 "$WORK/derive.log" >&2 + exit 1 +} +cp "$WORK/src/av1/decoder/decodeframe.c" "$WORK/derived.c" + +# Re-running must be a no-op, or the repair pass is not idempotent and a +# second run would rewrite a tree under someone's feet. +AOM_ORACLE_SRC="$WORK/src" bash "$HERE/instrument-aom-oracle.sh" > "$WORK/rerun.log" 2>&1 +cmp -s "$WORK/derived.c" "$WORK/src/av1/decoder/decodeframe.c" || { + echo "FAIL: a second instrument-aom-oracle.sh run changed the derived file" >&2 + exit 1 +} + +fail=0 +check() { # description, actual, expected + if [ "$2" = "$3" ]; then + printf 'ok %-58s %s\n' "$1" "$2" + else + printf 'FAIL %-58s got %s, want %s\n' "$1" "$2" "$3" >&2 + fail=1 + fi +} + +legacy=$(grep -c 'fwrite(ec_b->[yuv]_buffer + ec_r \* ec_b->' "$WORK/derived.c" || true) +check "legacy u8 row loops left in the derived file" "$legacy" "0" + +rows=$(grep -c 'ec_dump_narrow_row(ec_f, ec_b, ec_b->[yuv]_buffer' "$WORK/derived.c" || true) +check "ec_dump_narrow_row call sites (4 rungs x 3 planes)" "$rows" "12" + +fin=$(grep -c 'ec_dump_finish(ec_f, "EC_AV1_' "$WORK/derived.c" || true) +check "ec_dump_finish call sites (one per narrowing rung)" "$fin" "4" + +for rung in EC_AV1_PREFILT_DUMP EC_AV1_POSTDEBLOCK_DUMP EC_AV1_PREFILT_WIDE_DUMP \ + EC_AV1_POSTCDEF_DUMP; do + n=$(grep -c "ec_dump_finish(ec_f, \"$rung\"" "$WORK/derived.c" || true) + check "byte-count check wired into $rung" "$n" "1" +done + +# Rung 12 (EC_AV1_FINAL_DUMP) is the depth-correct one and must stay that way: +# it converts, it writes 2 bytes per sample, and it is not routed through the +# narrowing checker. +conv=$(grep -c 'CONVERT_TO_SHORTPTR(ec_p8\[ec_pl\])' "$WORK/derived.c" || true) +check "rung 12 still converts plane pointers" "$conv" "1" +r12=$(grep -c 'ec_dump_finish(ec_f, "EC_AV1_FINAL_DUMP"' "$WORK/derived.c" || true) +check "rung 12 not routed through the narrowing checker" "$r12" "0" + +if [ "$fail" -ne 0 ]; then + echo "FAIL: derived decodeframe.c violates the rung contract (base $BASE)" >&2 + exit 1 +fi +echo "ok instrument-aom-oracle.sh derives the depth-correct, byte-checked rungs (base $BASE)" diff --git a/scripts/instrument-aom-oracle.sh b/scripts/instrument-aom-oracle.sh index 664fd676..c016b23b 100755 --- a/scripts/instrument-aom-oracle.sh +++ b/scripts/instrument-aom-oracle.sh @@ -961,20 +961,62 @@ static void ec_dump_narrow_row(FILE *f, const YV12_BUFFER_CONFIG *b, aom_free(narrow); } +/* EC_INSTRUMENTED_DUMP_CHECK: the harm the depth bug above caused is not a + * wrong number, it is a SILENT 0-byte dump -- downstream that reads as "the + * oracle has no data for this stage" and becomes a phantom stage diff. So + * every narrowing rung checks the byte count it just produced against the + * shape it claims to write and aborts IN THE ORACLE, naming itself, instead + * of handing the decoder an empty file. `expect` is Y + U + V at ONE BYTE PER + * SAMPLE: the crop extent when `aligned` is 0 (PREFILT), the mi-aligned + * extent when 1 (PREFILT_WIDE / POSTDEBLOCK / POSTCDEF). No configuration + * writes 0 bytes: a frame's crop extent is non-zero by construction, and a + * failed fopen skips the block entirely (so a bad path is not an abort). */ +static void ec_dump_finish(FILE *f, const char *rung, + const YV12_BUFFER_CONFIG *b, int num_planes, + int aligned) { + const size_t y = aligned ? (size_t)b->y_width * b->y_height + : (size_t)b->y_crop_width * b->y_crop_height; + const size_t uv = aligned ? (size_t)b->uv_width * b->uv_height + : (size_t)b->uv_crop_width * b->uv_crop_height; + const size_t expect = y + (num_planes > 1 ? 2 * uv : 0); + const long wrote = ftell(f); + if (wrote < 0 || (size_t)wrote != expect || fflush(f) != 0) { + fprintf(stderr, + "EC_DUMP_ABORT rung=%s bit_depth=%u hbd=%d expect=%zu wrote=%ld " + "(one byte per sample, Y+U+V)\\n", + rung, b->bit_depth, + (b->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0, expect, wrote); + abort(); + } + fclose(f); +} + ''' -if "EC_INSTRUMENTED_NARROW_ROW" not in s: - # Must precede every user: the POSTCDEF rung was refactored into a static - # helper that sits ABOVE av1_decode_tg_tiles_and_wrapup, so anchor there - # when it exists and at the function itself otherwise. +MARKER_ROW = "EC_INSTRUMENTED_NARROW_ROW" +MARKER_CHECK = "EC_INSTRUMENTED_DUMP_CHECK" +missing = "" +if MARKER_ROW not in s: + missing = helper +elif MARKER_CHECK not in s: + # The tree already carries the row helper from an earlier run of this + # script: add the byte-count checker on its own so a repaired tree + # converges instead of erroring. + missing = helper[helper.index("/* " + MARKER_CHECK):] +if missing: + # Both helpers must precede every user: the POSTCDEF rung was refactored + # into a static helper that sits ABOVE av1_decode_tg_tiles_and_wrapup, so + # anchor there when it exists and at the function itself otherwise. for anchor in ("static void ec_dump_postcdef(const AV1_COMMON *cm, int num_planes) {", "void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,"): if anchor in s: - s = s.replace(anchor, helper + anchor, 1) + s = s.replace(anchor, missing + anchor, 1) break else: - raise SystemExit("narrow-row helper: no anchor before the dump rungs") - print("narrow-row helper inserted") + raise SystemExit("dump helpers: no anchor before the dump rungs") + print("dump helpers inserted: %s" + % ("narrow-row + byte-count check" if len(missing) == len(helper) + else "byte-count check only")) row_loop = re.compile( r"for \(int ec_r = 0; ec_r < ec_b->(\w+); \+\+ec_r\)\n" @@ -993,5 +1035,42 @@ if rewritten: print("narrow-row depth fix: rewrote %d plane row loops" % rewritten) else: print("narrow-row depth fix: no legacy row loop left (already depth-correct)") + +# Close each narrowing rung through the checker. Keyed on the presence of a +# rewritten row loop, so rung 12 (EC_AV1_FINAL_DUMP, 2 bytes per sample and +# already depth-correct) is never reached: its block has no ec_dump_narrow_row. +# The terminator alternation keeps each block matched to ITS OWN close, so a +# re-run cannot run a block's body forward into a later rung's fclose. +dump_block = re.compile( + r'(FILE \*ec_f = fopen\(ec_path, "wb"\);)(.*?)' + r'((?:fclose\(ec_f\);)|(?:ec_dump_finish\([^;]*\);))', + re.S) + +WIRED = [] + +def finish(match): + opened, body, closed = match.groups() + if "ec_dump_narrow_row" not in body: + return match.group(0) + # The rung name can sit ABOVE the fopen (the POSTCDEF rung was refactored + # into a helper that does getenv -> fopen at the top), so take the nearest + # preceding EC_AV1_* env lookup rather than searching the block body. + preceding = re.findall(r'getenv\("(EC_AV1_[A-Z_]+)"\)', s[:match.start()]) + if not preceding: + raise SystemExit("dump check: no rung env var above a narrowing block") + aligned = 0 if "y_crop_height" in body else 1 + if closed.startswith("fclose"): + WIRED.append(preceding[-1]) + return "%s%s%s" % ( + opened, body, + 'ec_dump_finish(ec_f, "%s", ec_b, num_planes, %d);' % (preceding[-1], aligned)) + + +s, _ = dump_block.subn(finish, s) +if WIRED: + print("dump byte-count check: wired into %d narrowing rung(s): %s" + % (len(WIRED), ", ".join(WIRED))) +else: + print("dump byte-count check: every narrowing rung already checks its bytes") open(path, "w").write(s) PYN