forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1oraclehbd'

Browse files at this commit

3bf8d88091bcdf2fd359121a4c9cb50e6c17edbbAhmet Kaan GümüşParent: 434bf374b63be1
lanes/av1oraclehbd.report.mdadded+426 -0
@@ -0,0 +1,426 @@
1+# lane-av1oraclehbd — the four u8-narrowing oracle rungs, root cause and fix
2+
3+Branch `lane-av1oraclehbd`, branched from `3eec02c8` ("lanes: merge-wave-3a
4+report"). Worktree `~/.cache/wt/av1oraclehbd`. Private oracle copy
5+`~/.cache/aom-oracle3` (same source revision as the shared oracle,
6+`9bb526a8fe938500d0392c93f20e30bf7bdda0a9` = `v3.13.3-7-g9bb526a`, copied with
7+`cp -a` from `~/.cache/aom-oracle/src` and configured/built in place by
8+`scripts/build-aom-oracle.sh AOM_ORACLE_ROOT=~/.cache/aom-oracle3`). The
9+shared oracle, `~/.cache/aom-oracle2`, and every gate were not touched. The
10+primary checkout was never edited (`git status --porcelain` there is empty).
11+
12+Diff: `scripts/instrument-aom-oracle.sh` only, +93 lines, one new trailing
13+block. No C file in the repo, no gate, no decoder change.
14+
15+## 1. Root cause — it is not the element size, it is a HALVED pointer
16+
17+The charter's hypothesis was "`fwrite(..., 1, w, f)` on a u16 plane reads past
18+the buffer". That is a real second defect, but it is not what segfaults, and
19+fixing only it would leave every rung emitting the wrong row. The faulting
20+mistake is in the **address**, and it is libaom's own pointer encoding:
21+
22+`aom_ports/mem.h:75-76`
23+
24+```c
25+#define CONVERT_TO_SHORTPTR(x) ((uint16_t *)(((uintptr_t)(x)) << 1))
26+#define CONVERT_TO_BYTEPTR(x) ((uint8_t *)(((uintptr_t)(x)) >> 1))
27+```
28+
29+`YV12_BUFFER_CONFIG` stores plane pointers in **halved** form at high bit
30+depth. `aom_scale/generic/yv12config.c:161-180`:
31+
32+```c
33+ buf = ybf->buffer_alloc;
34+ if (use_highbitdepth) {
35+ buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc); /* == buffer_alloc >> 1 */
36+ ybf->flags = YV12_FLAG_HIGHBITDEPTH;
37+ }
38+ ybf->y_buffer = (uint8_t *)aom_align_addr(
39+ buf + (border * y_stride) + border, aom_byte_align);
40+```
41+
42+so `ybf->y_buffer` is `real_address >> 1`, and the real byte address is
43+`CONVERT_TO_SHORTPTR(y_buffer)` (`y_buffer << 1`). The four rungs did
44+
45+```c
46+fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_crop_width, ec_f);
47+```
48+
49+i.e. pointer arithmetic on the *halved* value. Two errors compound:
50+
51+1. **Wrong address.** `y_buffer` itself is half the real address, so the very
52+ first `fwrite` reads an unrelated, unmapped address. Measured on
53+ `av112bit-key.obu`: `y_buffer = 0x3ffffbbb7850` while the real plane is
54+ `0x7ffff776f0a0` (`y_buffer << 1`, and `0x7ffff776f0a0 - 0x7ffff7766020 =
55+ 36992 = 2 * (border*y_stride + border) = 2 * (64*288 + 64)`, which is the
56+ allocation base plus the border offset in BYTES — the halving is confirmed
57+ arithmetically, not inferred). `write(2)` to `/dev/null` "succeeds" on that
58+ address, which is why a naive probe lies; a `mincore()` and a real load both
59+ report it unmapped.
60+2. **Wrong row.** `y_stride` / `uv_stride` count SAMPLES (`288` = `160 + 2*64`
61+ border, `144` = `288 >> ss_x`), so `+ r*stride` in byte units also addresses
62+ the wrong row even where the address happens to be readable.
63+
64+At 8 bit `use_highbitdepth` is 0, `buf` is the real pointer and no halving
65+happens — which is exactly why the same line is correct on an 8-bit stream and
66+faults on every 10/12-bit stream.
67+
68+`gdb` on the unfixed binary (`EC_AV1_PREFILT_DUMP` set, 12-bit fixture):
69+SIGSEGV in `__memmove_avx512_unaligned_erms` ← `_IO_default_xsputn` ←
70+`_IO_file_xsputn` ← `fwrite` ← `av1_decode_tg_tiles_and_wrapup`, with
71+`rsi = 0x3ffffbbb7850` (= `y_buffer`, row 0), `rdx = 0xa0` (160).
72+
73+Rung 12 (`EC_AV1_FINAL_DUMP`) is depth-correct because it does the conversion:
74+`CONVERT_TO_SHORTPTR(ec_p8[ec_pl]) + (size_t)ec_r * ec_st[ec_pl]` with
75+`fwrite(..., 2, w, f)`. That is the shape every one of the four needed.
76+
77+## 2. Reproduction (before)
78+
79+Private oracle, `av112bit-key.obu` (sha256
80+`c74f62b57f71f7c36c49ea1feeb482786a2650e5c7f66610949b5f65fbbed8d1`, 160x128
81+4:2:0, bit_depth 12, one frame):
82+
83+| rung | exit | file |
84+|---|---|---|
85+| `EC_AV1_PREFILT_DUMP` | 139 | 0 B |
86+| `EC_AV1_POSTDEBLOCK_DUMP` | 139 | 0 B |
87+| `EC_AV1_PREFILT_WIDE_DUMP` | 139 | 0 B |
88+| `EC_AV1_POSTCDEF_DUMP` | 139 | 0 B |
89+| `EC_AV1_FINAL_DUMP` (rung 12, untouched) | 0 | 61440 B = `160*128*2 + 2*80*64*2` |
90+
91+The shared oracle (`~/.cache/aom-oracle/build/aomdec`) reproduces the same four
92+139s on the same fixture, so this is a pre-existing property of the committed
93+instrumentation, not of the private copy.
94+
95+## 3. The fix
96+
97+One helper, inserted before its first user, plus every legacy row loop
98+rewritten to call it (`scripts/instrument-aom-oracle.sh`, new trailing block
99+"depth contract for the four u8-narrowing dump rungs"):
100+
101+```c
102+static void ec_dump_narrow_row(FILE *f, const YV12_BUFFER_CONFIG *b,
103+ const uint8_t *plane8, int stride, int row,
104+ int w) {
105+ if (!(b->flags & YV12_FLAG_HIGHBITDEPTH)) {
106+ fwrite(plane8 + (size_t)row * stride, 1, (size_t)w, f); /* unchanged 8-bit bytes */
107+ return;
108+ }
109+ const uint16_t *p = CONVERT_TO_SHORTPTR(plane8) + (size_t)row * stride;
110+ uint8_t *narrow = (uint8_t *)aom_malloc((size_t)w);
111+ if (!narrow) return;
112+ for (int c = 0; c < w; ++c) narrow[c] = (uint8_t)(p[c] & 0xFF);
113+ fwrite(narrow, 1, (size_t)w, f);
114+ aom_free(narrow);
115+}
116+```
117+
118+`p[c] & 0xFF` is the C spelling of the decoder side's `s as u8`. The 8-bit
119+branch is the original expression verbatim, so 8-bit output cannot move. The
120+row buffer is built per row so the rung still issues one `fwrite` per row, as
121+before. Rung 12 is not touched, and no rung the decoder does not pair with is
122+touched.
123+
124+The block is a **repair pass**, not just a fresh-derivation step: it rewrites
125+the loops an earlier instrumentation left behind, so re-running
126+`instrument-aom-oracle.sh` on an already-instrumented tree fixes it in place.
127+
128+Verified three ways on the C source:
129+
130+- repair on the shared-baseline `decodeframe.c` → output **byte-identical** to
131+ the hand-applied fix in `~/.cache/aom-oracle3` (`diff` clean), 12 loops
132+ rewritten;
133+- second run → `no legacy row loop left (already depth-correct)`, file
134+ unchanged (idempotent);
135+- fresh derivation from the pristine upstream `decodeframe.c`
136+ (`git show 92d4c37:av1/decoder/decodeframe.c` from the oracle's own clone)
137+ through the whole script → the same helper text and the same 4 luma + 4 U +
138+ 4 V `ec_dump_narrow_row` call sites, zero legacy loops.
139+
140+## 4. After
141+
142+Same fixture, same binary path, fixed oracle: all five rungs exit 0, and the
143+four fixed ones write `30720 B = 160*128 + 2*80*64` = one byte per sample.
144+
145+## 5. Pairing proof — fixed oracle vs the decoder's own `as u8` dumps
146+
147+Decoder side: `cargo run -p ec-av1 --example decode_probe <fixture>` with the
148+rung's env var set (`decode.rs:19722` `dump_stage`, `:19752`
149+`dump_prefilter_wide`, `:35263` prefilt — all narrow with `s as u8`).
150+
151+**12-bit** (`av112bit-key.obu`, 160x128 4:2:0, bit_depth 12):
152+
153+| rung | oracle | decoder | size | verdict |
154+|---|---|---|---|---|
155+| PREFILT | 0 | 0 | 30720 / 30720 | byte-identical |
156+| POSTDEBLOCK | 0 | 0 | 30720 / 30720 | byte-identical |
157+| PREFILT_WIDE | 0 | 0 | 30720 / 30720 | byte-identical |
158+| POSTCDEF | 0 | 0 | 30720 / 30720 | byte-identical |
159+
160+Not vacuous: 30619 of 30720 bytes are non-zero.
161+
162+**10-bit**, two fixtures, all four rungs byte-identical at 73728 B
163+(= `256*192 + 2*128*96`, one byte per sample):
164+`palette_screen_witness_10bit.obu` (sha256 `a60a8bc132e5db7b…`, 256x192,
165+15 frames) and `palette_screen_strip16_witness_10bit.obu` (sha256
166+`aaabbb001d87f61e…`).
167+
168+**Multi-frame**, all frames identical: `av112bit-inter.obu` (sha256
169+`e679d577e61b4a8b…`, 2 frames) and `av112bit-compound.obu` (sha256
170+`a839cff534b89d5c…`, 6 frames) — 30720 B per frame on both sides, every
171+`.f<N>` equal.
172+
173+**8-bit no-op**: fixed private oracle vs the shared oracle, same env var, same
174+stream, `cmp`:
175+
176+| fixture | rung | shared | fixed | verdict |
177+|---|---|---|---|---|
178+| `422_allskip_2f.obu` (128x128 4:2:2) | all four | 0, 32768 B | 0, 32768 B | identical |
179+| `golden4-pin.obu` (64x64) | all four | 0, 6144 B | 0, 6144 B | identical |
180+| `av112bit-key.obu` (12-bit) | all four | **139, 0 B** | 0, 30720 B | the fix |
181+
182+## 6. What a downstream lane should expect
183+
184+Byte counts, one byte per sample at every depth, Y then U then V concatenated:
185+
186+- `EC_AV1_PREFILT_DUMP` — crop extent: `y_crop_w*y_crop_h + 2*uv_crop_w*uv_crop_h`
187+ (12-bit 160x128 4:2:0 → 30720; 8-bit 64x64 → 6144).
188+- `EC_AV1_PREFILT_WIDE_DUMP`, `EC_AV1_POSTDEBLOCK_DUMP`, `EC_AV1_POSTCDEF_DUMP` —
189+ **mi-ALIGNED** extent `y_width*y_height + 2*uv_width*uv_height`, and
190+ **pre-superres / pre-LR**: the buffer is the coded one, so on a superres
191+ stream these are legitimately *not* the display size. `444_lossy_superres_256x128_d12_10bit.obu`
192+ measures exactly that: PREFILT_WIDE pairs byte-identically (67584 B) while the
193+ three crop-shaped rungs differ in size (65664 / 67584 vs the decoder's
194+ 73728). Prove a rung on a NON-superres fixture, or pair the WIDE rung.
195+- `EC_AV1_FINAL_DUMP` (rung 12, unchanged) is the depth-*correct 16-bit* one:
196+ `2 * (crop samples)`, 61440 B on the 12-bit fixture. It is the only rung
197+ whose file length encodes the depth.
198+
199+Rules of thumb:
200+
201+- a size that is neither `w*h` per plane at one byte per sample (these four
202+ rungs) nor `2*w*h` (rung 12) means **the rung is wrong, not the decoder**;
203+- before blaming the decoder for a crashed aomdec, check the exit code: 139
204+ with a 0-byte dump file is a segfaulting rung, not a decode;
205+- a rung crash is not evidence about pixel content. Nothing was concluded about
206+ any decode from these crashes.
207+
208+## 7. Notes for the next instrumentation addition
209+
210+- Any new libaom rung that touches `YV12_BUFFER_CONFIG` planes must go through
211+ `CONVERT_TO_SHORTPTR` at high bit depth. Grep the new code for
212+ `->_buffer` arithmetic on the raw `uint8_t*`; that pattern is the bug.
213+- Strides in that struct are SAMPLES, not bytes, at every depth.
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 only
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.
299+
300+## 9. Round 3 — the SHARED oracle is fixed too (this is where the harm lived)
301+
302+The private copy was never the problem. `~/.cache/aom-oracle` shipped the same
303+four broken rungs, so **every lane that ever paired against them on a 10/12-bit
304+stream got a 0-byte file**, and a 0-byte file reads downstream as "the oracle has
305+no data for this stage".
306+
307+### 9.1 Zero-window swap (a live lane was using it)
308+
309+`ps` showed `~/.cache/cargo-target-av1lm444corr` running
310+`a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel` with the shared
311+`aomenc` exec'd 0 s earlier, so a plain `ninja -C build aomdec` would have
312+truncated the shared binary in place for the ~3 s of the link. Instead:
313+
314+1. `ln build/aomdec build/aomdec.prefix-20260929-054321` (hardlink backup, no
315+ path gap) and `cp src/av1/decoder/decodeframe.c revert/decodeframe.c.prefix-20260929-054321`;
316+2. the repaired `decodeframe.c` copied into the shared src (a file no running
317+ process reads);
318+3. `ninja -C build libaom.a` only (2.6 s) — the shared `aomdec` path is never
319+ written;
320+4. the link command from `ninja -t commands aomdec` re-run with
321+ `-o ~/.cache/tmp/hbd-swap/aomdec.new`, so the new binary was built and
322+ fully verified OFF-PATH;
323+5. one `mv` (`rename(2)`, same filesystem): inode 177294570 (old) -> 177592760
324+ (new). A concurrent exec gets either the old inode or the new one, never a
325+ partial file.
326+
327+**Revert, one command each:**
328+
329+```
330+mv ~/.cache/aom-oracle/build/aomdec.prefix-20260929-054321 ~/.cache/aom-oracle/build/aomdec
331+git -C ~/.cache/aom-oracle/src checkout -- av1/decoder/decodeframe.c
332+```
333+
334+(The source revert is enough on its own for any future re-derivation;
335+ `ninja -C ~/.cache/aom-oracle/build aomdec` then rebuilds. `av1/common/reconintra.c`
336+ was already modified before this lane and was not touched.)
337+
338+### 9.2 Verified on the SHARED binary, after the flip
339+
340+`~/.cache/aom-oracle/build/aomdec`, five rungs, exit codes and sizes:
341+
342+| fixture | PREFILT | POSTDEBLOCK | PREFILT_WIDE | POSTCDEF | FINAL (rung 12) |
343+|---|---|---|---|---|---|
344+| `av112bit-key.obu` (12-bit) | 0, 30720 B | 0, 30720 B | 0, 30720 B | 0, 30720 B | 0, 61440 B |
345+| `palette_screen_witness_10bit.obu` (10-bit) | 0, 73728 B | 0, 73728 B | 0, 73728 B | 0, 73728 B | 0, 147456 B |
346+
347+Byte-identical to the decoder's own `as u8` rows (decode_probe, same env var):
348+8/8 `IDENTICAL` (4 rungs x 2 fixtures).
349+
350+8-bit no-op on the shared pair — new binary vs the hardlinked OLD one:
351+`422_allskip_2f.obu` 32768 B and `golden4-pin.obu` 6144 B, all five rungs,
352+**10/10 `IDENTICAL`**, both exit 0. Nothing that was correct at 8 bit moved.
353+
354+### 9.3 The loudness is live in the shared tree too
355+
356+The deliberate-mismatch mutant was also built OFF-PATH from the shared source
357+(mutated `decodeframe.c` -> object compiled to a scratch path -> `ar r` into a
358+*copy* of `libaom.a` -> linked to `aomdec.mutant`), so the shared path never
359+held an aborting oracle. One dropped luma row in PREFILT:
360+
361+```
362+12-bit: exit 134 EC_DUMP_ABORT rung=EC_AV1_PREFILT_DUMP bit_depth=12 hbd=1 expect=30720 wrote=30560
363+ 8-bit: exit 134 EC_DUMP_ABORT rung=EC_AV1_PREFILT_DUMP bit_depth=8 hbd=0 expect=6144 wrote=6080
364+```
365+
366+(First attempt at this proof silently passed because `ar q` APPENDS a
367+duplicate member instead of replacing it and the linker took the first — `ar r`
368+ is the replace verb. Worth knowing before anyone repeats the trick.)
369+
370+### 9.4 `check-aom-oracle-rungs.sh` against the shared source
371+
372+`bash scripts/check-aom-oracle-rungs.sh ~/.cache/aom-oracle/src`:
373+
374+```
375+ok legacy u8 row loops left in the derived file 0
376+ok ec_dump_narrow_row call sites (4 rungs x 3 planes) 12
377+ok ec_dump_finish call sites (one per narrowing rung) 4
378+ok byte-count check wired into EC_AV1_PREFILT_DUMP 1
379+ok byte-count check wired into EC_AV1_POSTDEBLOCK_DUMP 1
380+ok byte-count check wired into EC_AV1_PREFILT_WIDE_DUMP 1
381+ok byte-count check wired into EC_AV1_POSTCDEF_DUMP 1
382+ok rung 12 still converts plane pointers 1
383+ok rung 12 not routed through the narrowing checker 0
384+ok instrument-aom-oracle.sh derives the depth-correct, byte-checked rungs (base v3.13.3)
385+```
386+
387+### 9.5 Who could have been misled — the naming
388+
389+**Waves merged today: nobody.** Wave 3a (`3eec02c8`: av1cmpaudit, av1leftwit,
390+av1lossy128x96, av1pinslive, av1txsearch) and wave 3b (`c6112723`:
391+av1rawhelper, av1oracleskip r1+r2+r3) — of those seven lanes:
392+
393+- `lane-av1cmpaudit` NAMES `EC_AV1_PREFILT_DUMP` once, to record that the
394+ decode-order dump rungs "have no in-repo consumer" and that the stale-dump
395+ hazard there is manual-only. It paired against none of them.
396+- `lane-av1rawhelper` names `EC_AV1_PREFILT_DUMP` twice, only as the contract
397+ `examples/dump_yuv.rs` documents itself against. Its gate is
398+ `assert_rawvideo_matches` — `aomdec --rawvideo` plus rung 12 — which is
399+ depth-correct and unaffected.
400+- `lane-av1oracleskip` is the aomenc-**SKIP** class (`have_aomenc()` returning
401+ empty and a gate calling that green). No dump-rung pairing.
402+- the other four wave-3a reports do not mention the four rungs at all.
403+
404+**No automated gate ever paired them**: `grep` over `crates/` and `tools/` finds
405+the four names only in doc comments and in the decoder-side definitions in
406+`decode.rs` — no test sets those env vars. The exposure was always a manual
407+ladder procedure, which is also why it survived so long.
408+
409+**Historical users, and why their conclusions stand**: the rungs were used by
410+earlier 8-bit lanes (av1444edge, av1444, av1444sb, av1422filter, av1422kf,
411+av1422kf2, av1422stripwrite, av1lrflush, av1intrapred, av1wavefront,
412+av1seglvl, hgkf-r2, tiny-r4, sub8-r5, mergefix-r4, sb128c-r4/r5, band63-r1,
413+golomb-r9, mc64-r1, inter4-r4, txselect-r2, sqchroma-r1, superres-r6). At
414+8 bit the old rungs were correct, and §9.2's 10/10 byte-identity against the
415+backed-up old binary is the direct proof that their output did not change.
416+The one report that both sweeps HBD cells and uses the rungs is
417+`av1formatsweep`; its two `EC_AV1_PREFILT_DUMP` pairing claims (lines 238, 290)
418+are on 8-bit 4:4:4 `testsrc2` streams, not on its 10/12-bit cells, and its
419+HBD cells pair through rung 12.
420+
421+**The honest limit of that sweep**: it is name-based over `lanes/*.md` plus a
422+targeted read of every report that mentions both a rung and an HBD marker. It
423+is not a line-by-line audit of every historical report. What it does establish
424+mechanically is the thing that matters: a 0-byte dump only ever occurred on a
425+10/12-bit stream, and any lane that paired on one would have seen a rung that
426+exited 139 — a value no report in `lanes/` quotes as a result.
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+172 -0
@@ -902,3 +902,175 @@ s = s.replace(anchor, anchor + dump, 1)
902902 open(path, "w").write(s)
903903 print("postcdef dump instrumented")
904904 PYC
905+
906+# --- depth contract for the four u8-narrowing dump rungs ------------------
907+# PREFILT (rung 1), POSTDEBLOCK (rung 3), PREFILT_WIDE (rung 7) and POSTCDEF
908+# are paired with ec-av1 decoder rungs that narrow every sample with `as u8`,
909+# so the oracle side owes ONE BYTE PER SAMPLE at every depth. As instrumented
910+# above they wrote `fwrite(plane + r*stride, 1, w, f)`, which is wrong twice
911+# over on a high-bit-depth stream:
912+# 1. YV12_BUFFER_CONFIG stores plane pointers in HALVED form -- aom_ports/mem.h
913+# defines CONVERT_TO_BYTEPTR(x) = x >> 1 -- so the real address is
914+# CONVERT_TO_SHORTPTR(p) (p << 1). Pointer arithmetic on the raw uint8_t*
915+# therefore yields an unrelated half-address, and fwrite faults inside
916+# memmove: SIGSEGV (exit 139) with a 0-byte file, on EVERY HBD stream, for
917+# all four rungs, while rung 12 (which does convert) works. A lane hitting
918+# it sees a crashed aomdec and cannot tell it from a decoder defect.
919+# 2. y_stride/uv_stride count SAMPLES, so `+ r*stride` in bytes also picks the
920+# wrong row even where the address happens to be readable.
921+# At 8 bit neither applies: buf is the real pointer and flags is 0, so the
922+# bytes emitted are bit-for-bit what these rungs always wrote. This block owns
923+# the contract for all four, runs last, and is a REPAIR pass: it rewrites the
924+# loops an earlier (or pre-fix) instrumentation left behind, so re-running this
925+# script on an already-instrumented tree fixes it in place.
926+python3 - "$F" <<'PYN'
927+import re
928+import sys
929+
930+path = sys.argv[1]
931+s = open(path).read()
932+
933+helper = '''/* EC_INSTRUMENTED_NARROW_ROW: emit one plane row as ONE BYTE PER SAMPLE, the
934+ * shape the ec-av1 decoder's own EC_AV1_*_DUMP rungs write (they narrow every
935+ * sample with `as u8`).
936+ *
937+ * Two HBD traps this replaces, both fatal until fixed:
938+ * 1. YV12_BUFFER_CONFIG stores plane pointers in HALVED form: the real
939+ * address is `CONVERT_TO_SHORTPTR(p)` (aom_ports/mem.h defines
940+ * `CONVERT_TO_BYTEPTR(x) = x >> 1`), so `p + r*stride` computed on the raw
941+ * uint8_t* is an unrelated address. `fwrite(y_buffer + r*y_stride, 1, w,
942+ * f)` on a 10/12-bit stream therefore hands fwrite a half-address that
943+ * faults inside memmove -- SIGSEGV with a 0-byte file, which reads as a
944+ * decoder crash rather than a broken rung.
945+ * 2. y_stride/uv_stride count SAMPLES, so a byte-wise `+ r*stride` addresses
946+ * the wrong row as well.
947+ * At 8 bit neither applies: the pointer is the real one and the bytes written
948+ * are exactly what the rung always emitted. */
949+static void ec_dump_narrow_row(FILE *f, const YV12_BUFFER_CONFIG *b,
950+ const uint8_t *plane8, int stride, int row,
951+ int w) {
952+ if (!(b->flags & YV12_FLAG_HIGHBITDEPTH)) {
953+ fwrite(plane8 + (size_t)row * stride, 1, (size_t)w, f);
954+ return;
955+ }
956+ const uint16_t *p = CONVERT_TO_SHORTPTR(plane8) + (size_t)row * stride;
957+ uint8_t *narrow = (uint8_t *)aom_malloc((size_t)w);
958+ if (!narrow) return;
959+ for (int c = 0; c < w; ++c) narrow[c] = (uint8_t)(p[c] & 0xFF);
960+ fwrite(narrow, 1, (size_t)w, f);
961+ aom_free(narrow);
962+}
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+
994+'''
995+
996+MARKER_ROW = "EC_INSTRUMENTED_NARROW_ROW"
997+MARKER_CHECK = "EC_INSTRUMENTED_DUMP_CHECK"
998+missing = ""
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.
1010+ for anchor in ("static void ec_dump_postcdef(const AV1_COMMON *cm, int num_planes) {",
1011+ "void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,"):
1012+ if anchor in s:
1013+ s = s.replace(anchor, missing + anchor, 1)
1014+ break
1015+ else:
1016+ raise SystemExit("dump helpers: no anchor before the dump rungs")
1017+ print("dump helpers inserted: %s"
1018+ % ("narrow-row + byte-count check" if len(missing) == len(helper)
1019+ else "byte-count check only"))
1020+
1021+row_loop = re.compile(
1022+ r"for \(int ec_r = 0; ec_r < ec_b->(\w+); \+\+ec_r\)\n"
1023+ r"(\s*)fwrite\(ec_b->(\w+)_buffer \+ ec_r \* ec_b->(\w+), 1,\s*\n?\s*ec_b->(\w+),\s*\n?\s*ec_f\);")
1024+
1025+
1026+def narrow(match):
1027+ rows, indent, plane, stride, width = match.groups()
1028+ return ("for (int ec_r = 0; ec_r < ec_b->%s; ++ec_r)\n%s"
1029+ "ec_dump_narrow_row(ec_f, ec_b, ec_b->%s_buffer, ec_b->%s, ec_r,\n"
1030+ "%s%s ec_b->%s);" % (rows, indent, plane, stride, indent, indent, width))
1031+
1032+
1033+s, rewritten = row_loop.subn(narrow, s)
1034+if rewritten:
1035+ print("narrow-row depth fix: rewrote %d plane row loops" % rewritten)
1036+else:
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")
1075+open(path, "w").write(s)
1076+PYN