From 0f2d9668e8a1b08ce51e8acb42b6d696f8cabe48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 29 Sep 2026 05:33:04 +0300 Subject: [PATCH] lanes: oracle u8-narrowing rungs were reading a HALVED plane pointer The four u8-narrowing aomdec rungs (PREFILT, POSTDEBLOCK, PREFILT_WIDE, POSTCDEF) SIGSEGV on every 10/12-bit stream: exit 139, 0-byte file, so a lane loses HBD debugging through them and cannot tell the crash from a decoder defect. Rung 12 (FINAL) works, and the difference is the whole bug. YV12_BUFFER_CONFIG stores plane pointers HALVED at high bit depth (aom_ports/mem.h: CONVERT_TO_BYTEPTR(x) = x >> 1; yv12config.c:161-180), so the real address is CONVERT_TO_SHORTPTR(p) (p << 1). The rungs did fwrite(y_buffer + r*y_stride, 1, w, f): pointer arithmetic on the halved value addresses an unrelated, unmapped address, and y_stride/uv_stride count SAMPLES so the row is wrong too. At 8 bit no halving happens, which is why the same line is correct there. Fix: one ec_dump_narrow_row helper (CONVERT_TO_SHORTPTR + low byte per sample, the C spelling of the decoder's `s as u8`) and every legacy row loop rewritten to call it. The 8-bit branch is the original expression verbatim. Rung 12 and every rung the decoder does not pair with are untouched. The new trailing block is a repair pass, so re-running instrument-aom-oracle.sh fixes an already-instrumented tree in place; verified byte-identical against a hand application, idempotent, and equal to a fresh derivation from pristine upstream. Measured: 12-bit av112bit-key.obu 139/0B -> 0/30720B; 10-bit palette_screen_witness_10bit and palette_screen_strip16_witness_10bit all four rungs byte-identical to the decoder's as u8 dumps (73728B); 2- and 6-frame 12-bit streams identical frame by frame; 8-bit output byte-identical to the shared oracle on 422_allskip_2f and golden4-pin. --- lanes/av1oraclehbd.report.md | 216 +++++++++++++++++++++++++++++++ scripts/instrument-aom-oracle.sh | 93 +++++++++++++ 2 files changed, 309 insertions(+) create mode 100644 lanes/av1oraclehbd.report.md diff --git a/lanes/av1oraclehbd.report.md b/lanes/av1oraclehbd.report.md new file mode 100644 index 00000000..6639f134 --- /dev/null +++ b/lanes/av1oraclehbd.report.md @@ -0,0 +1,216 @@ +# lane-av1oraclehbd — the four u8-narrowing oracle rungs, root cause and fix + +Branch `lane-av1oraclehbd`, branched from `3eec02c8` ("lanes: merge-wave-3a +report"). Worktree `~/.cache/wt/av1oraclehbd`. Private oracle copy +`~/.cache/aom-oracle3` (same source revision as the shared oracle, +`9bb526a8fe938500d0392c93f20e30bf7bdda0a9` = `v3.13.3-7-g9bb526a`, copied with +`cp -a` from `~/.cache/aom-oracle/src` and configured/built in place by +`scripts/build-aom-oracle.sh AOM_ORACLE_ROOT=~/.cache/aom-oracle3`). The +shared oracle, `~/.cache/aom-oracle2`, and every gate were not touched. The +primary checkout was never edited (`git status --porcelain` there is empty). + +Diff: `scripts/instrument-aom-oracle.sh` only, +93 lines, one new trailing +block. No C file in the repo, no gate, no decoder change. + +## 1. Root cause — it is not the element size, it is a HALVED pointer + +The charter's hypothesis was "`fwrite(..., 1, w, f)` on a u16 plane reads past +the buffer". That is a real second defect, but it is not what segfaults, and +fixing only it would leave every rung emitting the wrong row. The faulting +mistake is in the **address**, and it is libaom's own pointer encoding: + +`aom_ports/mem.h:75-76` + +```c +#define CONVERT_TO_SHORTPTR(x) ((uint16_t *)(((uintptr_t)(x)) << 1)) +#define CONVERT_TO_BYTEPTR(x) ((uint8_t *)(((uintptr_t)(x)) >> 1)) +``` + +`YV12_BUFFER_CONFIG` stores plane pointers in **halved** form at high bit +depth. `aom_scale/generic/yv12config.c:161-180`: + +```c + buf = ybf->buffer_alloc; + if (use_highbitdepth) { + buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc); /* == buffer_alloc >> 1 */ + ybf->flags = YV12_FLAG_HIGHBITDEPTH; + } + ybf->y_buffer = (uint8_t *)aom_align_addr( + buf + (border * y_stride) + border, aom_byte_align); +``` + +so `ybf->y_buffer` is `real_address >> 1`, and the real byte address is +`CONVERT_TO_SHORTPTR(y_buffer)` (`y_buffer << 1`). The four rungs did + +```c +fwrite(ec_b->y_buffer + ec_r * ec_b->y_stride, 1, ec_b->y_crop_width, ec_f); +``` + +i.e. pointer arithmetic on the *halved* value. Two errors compound: + +1. **Wrong address.** `y_buffer` itself is half the real address, so the very + first `fwrite` reads an unrelated, unmapped address. Measured on + `av112bit-key.obu`: `y_buffer = 0x3ffffbbb7850` while the real plane is + `0x7ffff776f0a0` (`y_buffer << 1`, and `0x7ffff776f0a0 - 0x7ffff7766020 = + 36992 = 2 * (border*y_stride + border) = 2 * (64*288 + 64)`, which is the + allocation base plus the border offset in BYTES — the halving is confirmed + arithmetically, not inferred). `write(2)` to `/dev/null` "succeeds" on that + address, which is why a naive probe lies; a `mincore()` and a real load both + report it unmapped. +2. **Wrong row.** `y_stride` / `uv_stride` count SAMPLES (`288` = `160 + 2*64` + border, `144` = `288 >> ss_x`), so `+ r*stride` in byte units also addresses + the wrong row even where the address happens to be readable. + +At 8 bit `use_highbitdepth` is 0, `buf` is the real pointer and no halving +happens — which is exactly why the same line is correct on an 8-bit stream and +faults on every 10/12-bit stream. + +`gdb` on the unfixed binary (`EC_AV1_PREFILT_DUMP` set, 12-bit fixture): +SIGSEGV in `__memmove_avx512_unaligned_erms` ← `_IO_default_xsputn` ← +`_IO_file_xsputn` ← `fwrite` ← `av1_decode_tg_tiles_and_wrapup`, with +`rsi = 0x3ffffbbb7850` (= `y_buffer`, row 0), `rdx = 0xa0` (160). + +Rung 12 (`EC_AV1_FINAL_DUMP`) is depth-correct because it does the conversion: +`CONVERT_TO_SHORTPTR(ec_p8[ec_pl]) + (size_t)ec_r * ec_st[ec_pl]` with +`fwrite(..., 2, w, f)`. That is the shape every one of the four needed. + +## 2. Reproduction (before) + +Private oracle, `av112bit-key.obu` (sha256 +`c74f62b57f71f7c36c49ea1feeb482786a2650e5c7f66610949b5f65fbbed8d1`, 160x128 +4:2:0, bit_depth 12, one frame): + +| rung | exit | file | +|---|---|---| +| `EC_AV1_PREFILT_DUMP` | 139 | 0 B | +| `EC_AV1_POSTDEBLOCK_DUMP` | 139 | 0 B | +| `EC_AV1_PREFILT_WIDE_DUMP` | 139 | 0 B | +| `EC_AV1_POSTCDEF_DUMP` | 139 | 0 B | +| `EC_AV1_FINAL_DUMP` (rung 12, untouched) | 0 | 61440 B = `160*128*2 + 2*80*64*2` | + +The shared oracle (`~/.cache/aom-oracle/build/aomdec`) reproduces the same four +139s on the same fixture, so this is a pre-existing property of the committed +instrumentation, not of the private copy. + +## 3. The fix + +One helper, inserted before its first user, plus every legacy row loop +rewritten to call it (`scripts/instrument-aom-oracle.sh`, new trailing block +"depth contract for the four u8-narrowing dump rungs"): + +```c +static void ec_dump_narrow_row(FILE *f, const YV12_BUFFER_CONFIG *b, + const uint8_t *plane8, int stride, int row, + int w) { + if (!(b->flags & YV12_FLAG_HIGHBITDEPTH)) { + fwrite(plane8 + (size_t)row * stride, 1, (size_t)w, f); /* unchanged 8-bit bytes */ + return; + } + const uint16_t *p = CONVERT_TO_SHORTPTR(plane8) + (size_t)row * stride; + uint8_t *narrow = (uint8_t *)aom_malloc((size_t)w); + if (!narrow) return; + for (int c = 0; c < w; ++c) narrow[c] = (uint8_t)(p[c] & 0xFF); + fwrite(narrow, 1, (size_t)w, f); + aom_free(narrow); +} +``` + +`p[c] & 0xFF` is the C spelling of the decoder side's `s as u8`. The 8-bit +branch is the original expression verbatim, so 8-bit output cannot move. The +row buffer is built per row so the rung still issues one `fwrite` per row, as +before. Rung 12 is not touched, and no rung the decoder does not pair with is +touched. + +The block is a **repair pass**, not just a fresh-derivation step: it rewrites +the loops an earlier instrumentation left behind, so re-running +`instrument-aom-oracle.sh` on an already-instrumented tree fixes it in place. + +Verified three ways on the C source: + +- repair on the shared-baseline `decodeframe.c` → output **byte-identical** to + the hand-applied fix in `~/.cache/aom-oracle3` (`diff` clean), 12 loops + rewritten; +- second run → `no legacy row loop left (already depth-correct)`, file + unchanged (idempotent); +- fresh derivation from the pristine upstream `decodeframe.c` + (`git show 92d4c37:av1/decoder/decodeframe.c` from the oracle's own clone) + through the whole script → the same helper text and the same 4 luma + 4 U + + 4 V `ec_dump_narrow_row` call sites, zero legacy loops. + +## 4. After + +Same fixture, same binary path, fixed oracle: all five rungs exit 0, and the +four fixed ones write `30720 B = 160*128 + 2*80*64` = one byte per sample. + +## 5. Pairing proof — fixed oracle vs the decoder's own `as u8` dumps + +Decoder side: `cargo run -p ec-av1 --example decode_probe ` with the +rung's env var set (`decode.rs:19722` `dump_stage`, `:19752` +`dump_prefilter_wide`, `:35263` prefilt — all narrow with `s as u8`). + +**12-bit** (`av112bit-key.obu`, 160x128 4:2:0, bit_depth 12): + +| rung | oracle | decoder | size | verdict | +|---|---|---|---|---| +| PREFILT | 0 | 0 | 30720 / 30720 | byte-identical | +| POSTDEBLOCK | 0 | 0 | 30720 / 30720 | byte-identical | +| PREFILT_WIDE | 0 | 0 | 30720 / 30720 | byte-identical | +| POSTCDEF | 0 | 0 | 30720 / 30720 | byte-identical | + +Not vacuous: 30619 of 30720 bytes are non-zero. + +**10-bit**, two fixtures, all four rungs byte-identical at 73728 B +(= `256*192 + 2*128*96`, one byte per sample): +`palette_screen_witness_10bit.obu` (sha256 `a60a8bc132e5db7b…`, 256x192, +15 frames) and `palette_screen_strip16_witness_10bit.obu` (sha256 +`aaabbb001d87f61e…`). + +**Multi-frame**, all frames identical: `av112bit-inter.obu` (sha256 +`e679d577e61b4a8b…`, 2 frames) and `av112bit-compound.obu` (sha256 +`a839cff534b89d5c…`, 6 frames) — 30720 B per frame on both sides, every +`.f` equal. + +**8-bit no-op**: fixed private oracle vs the shared oracle, same env var, same +stream, `cmp`: + +| fixture | rung | shared | fixed | verdict | +|---|---|---|---|---| +| `422_allskip_2f.obu` (128x128 4:2:2) | all four | 0, 32768 B | 0, 32768 B | identical | +| `golden4-pin.obu` (64x64) | all four | 0, 6144 B | 0, 6144 B | identical | +| `av112bit-key.obu` (12-bit) | all four | **139, 0 B** | 0, 30720 B | the fix | + +## 6. What a downstream lane should expect + +Byte counts, one byte per sample at every depth, Y then U then V concatenated: + +- `EC_AV1_PREFILT_DUMP` — crop extent: `y_crop_w*y_crop_h + 2*uv_crop_w*uv_crop_h` + (12-bit 160x128 4:2:0 → 30720; 8-bit 64x64 → 6144). +- `EC_AV1_PREFILT_WIDE_DUMP`, `EC_AV1_POSTDEBLOCK_DUMP`, `EC_AV1_POSTCDEF_DUMP` — + **mi-ALIGNED** extent `y_width*y_height + 2*uv_width*uv_height`, and + **pre-superres / pre-LR**: the buffer is the coded one, so on a superres + stream these are legitimately *not* the display size. `444_lossy_superres_256x128_d12_10bit.obu` + measures exactly that: PREFILT_WIDE pairs byte-identically (67584 B) while the + three crop-shaped rungs differ in size (65664 / 67584 vs the decoder's + 73728). Prove a rung on a NON-superres fixture, or pair the WIDE rung. +- `EC_AV1_FINAL_DUMP` (rung 12, unchanged) is the depth-*correct 16-bit* one: + `2 * (crop samples)`, 61440 B on the 12-bit fixture. It is the only rung + whose file length encodes the depth. + +Rules of thumb: + +- a size that is neither `w*h` per plane at one byte per sample (these four + rungs) nor `2*w*h` (rung 12) means **the rung is wrong, not the decoder**; +- before blaming the decoder for a crashed aomdec, check the exit code: 139 + with a 0-byte dump file is a segfaulting rung, not a decode; +- a rung crash is not evidence about pixel content. Nothing was concluded about + any decode from these crashes. + +## 7. Notes for the next instrumentation addition + +- Any new libaom rung that touches `YV12_BUFFER_CONFIG` planes must go through + `CONVERT_TO_SHORTPTR` at high bit depth. Grep the new code for + `->_buffer` arithmetic on the raw `uint8_t*`; that pattern is the bug. +- Strides in that struct are SAMPLES, not bytes, at every depth. +- 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. diff --git a/scripts/instrument-aom-oracle.sh b/scripts/instrument-aom-oracle.sh index fddc2b7a..664fd676 100755 --- a/scripts/instrument-aom-oracle.sh +++ b/scripts/instrument-aom-oracle.sh @@ -902,3 +902,96 @@ s = s.replace(anchor, anchor + dump, 1) open(path, "w").write(s) print("postcdef dump instrumented") PYC + +# --- depth contract for the four u8-narrowing dump rungs ------------------ +# PREFILT (rung 1), POSTDEBLOCK (rung 3), PREFILT_WIDE (rung 7) and POSTCDEF +# are paired with ec-av1 decoder rungs that narrow every sample with `as u8`, +# so the oracle side owes ONE BYTE PER SAMPLE at every depth. As instrumented +# above they wrote `fwrite(plane + r*stride, 1, w, f)`, which is wrong twice +# over on a high-bit-depth stream: +# 1. YV12_BUFFER_CONFIG stores plane pointers in HALVED form -- aom_ports/mem.h +# defines CONVERT_TO_BYTEPTR(x) = x >> 1 -- so the real address is +# CONVERT_TO_SHORTPTR(p) (p << 1). Pointer arithmetic on the raw uint8_t* +# therefore yields an unrelated half-address, and fwrite faults inside +# memmove: SIGSEGV (exit 139) with a 0-byte file, on EVERY HBD stream, for +# all four rungs, while rung 12 (which does convert) works. A lane hitting +# it sees a crashed aomdec and cannot tell it from a decoder defect. +# 2. y_stride/uv_stride count SAMPLES, so `+ r*stride` in bytes also picks the +# wrong row even where the address happens to be readable. +# At 8 bit neither applies: buf is the real pointer and flags is 0, so the +# bytes emitted are bit-for-bit what these rungs always wrote. This block owns +# the contract for all four, runs last, and is a REPAIR pass: it rewrites the +# loops an earlier (or pre-fix) instrumentation left behind, so re-running this +# script on an already-instrumented tree fixes it in place. +python3 - "$F" <<'PYN' +import re +import sys + +path = sys.argv[1] +s = open(path).read() + +helper = '''/* EC_INSTRUMENTED_NARROW_ROW: emit one plane row as ONE BYTE PER SAMPLE, the + * shape the ec-av1 decoder's own EC_AV1_*_DUMP rungs write (they narrow every + * sample with `as u8`). + * + * Two HBD traps this replaces, both fatal until fixed: + * 1. YV12_BUFFER_CONFIG stores plane pointers in HALVED form: the real + * address is `CONVERT_TO_SHORTPTR(p)` (aom_ports/mem.h defines + * `CONVERT_TO_BYTEPTR(x) = x >> 1`), so `p + r*stride` computed on the raw + * uint8_t* is an unrelated address. `fwrite(y_buffer + r*y_stride, 1, w, + * f)` on a 10/12-bit stream therefore hands fwrite a half-address that + * faults inside memmove -- SIGSEGV with a 0-byte file, which reads as a + * decoder crash rather than a broken rung. + * 2. y_stride/uv_stride count SAMPLES, so a byte-wise `+ r*stride` addresses + * the wrong row as well. + * At 8 bit neither applies: the pointer is the real one and the bytes written + * are exactly what the rung always emitted. */ +static void ec_dump_narrow_row(FILE *f, const YV12_BUFFER_CONFIG *b, + const uint8_t *plane8, int stride, int row, + int w) { + if (!(b->flags & YV12_FLAG_HIGHBITDEPTH)) { + fwrite(plane8 + (size_t)row * stride, 1, (size_t)w, f); + return; + } + const uint16_t *p = CONVERT_TO_SHORTPTR(plane8) + (size_t)row * stride; + uint8_t *narrow = (uint8_t *)aom_malloc((size_t)w); + if (!narrow) return; + for (int c = 0; c < w; ++c) narrow[c] = (uint8_t)(p[c] & 0xFF); + fwrite(narrow, 1, (size_t)w, f); + aom_free(narrow); +} + +''' + +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. + 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) + break + else: + raise SystemExit("narrow-row helper: no anchor before the dump rungs") + print("narrow-row helper inserted") + +row_loop = re.compile( + r"for \(int ec_r = 0; ec_r < ec_b->(\w+); \+\+ec_r\)\n" + r"(\s*)fwrite\(ec_b->(\w+)_buffer \+ ec_r \* ec_b->(\w+), 1,\s*\n?\s*ec_b->(\w+),\s*\n?\s*ec_f\);") + + +def narrow(match): + rows, indent, plane, stride, width = match.groups() + return ("for (int ec_r = 0; ec_r < ec_b->%s; ++ec_r)\n%s" + "ec_dump_narrow_row(ec_f, ec_b, ec_b->%s_buffer, ec_b->%s, ec_r,\n" + "%s%s ec_b->%s);" % (rows, indent, plane, stride, indent, indent, width)) + + +s, rewritten = row_loop.subn(narrow, s) +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)") +open(path, "w").write(s) +PYN