lanes: oracle u8-narrowing rungs were reading a HALVED plane pointer
Browse files at this commit0f2d9668e8a1b08ce51e8acb42b6d696f8cabe48Parent: 4abac362 changed files +309 -0
- lanes/av1oraclehbd.report.md +216 -0
- scripts/instrument-aom-oracle.sh +93 -0
lanes/av1oraclehbd.report.mdadded+216 -0
| @@ -0,0 +1,216 @@ | ||
| 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. |
scripts/instrument-aom-oracle.shmodified+93 -0
| @@ -902,3 +902,96 @@ s = s.replace(anchor, anchor + dump, 1) | ||
| 902 | 902 | open(path, "w").write(s) |
| 903 | 903 | print("postcdef dump instrumented") |
| 904 | 904 | 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 | +''' | |
| 965 | + | |
| 966 | +if "EC_INSTRUMENTED_NARROW_ROW" not in s: | |
| 967 | + # Must precede every user: the POSTCDEF rung was refactored into a static | |
| 968 | + # helper that sits ABOVE av1_decode_tg_tiles_and_wrapup, so anchor there | |
| 969 | + # 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) {", | |
| 971 | + "void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,"): | |
| 972 | + if anchor in s: | |
| 973 | + s = s.replace(anchor, helper + anchor, 1) | |
| 974 | + break | |
| 975 | + else: | |
| 976 | + raise SystemExit("narrow-row helper: no anchor before the dump rungs") | |
| 977 | + print("narrow-row helper inserted") | |
| 978 | + | |
| 979 | +row_loop = re.compile( | |
| 980 | + r"for \(int ec_r = 0; ec_r < ec_b->(\w+); \+\+ec_r\)\n" | |
| 981 | + r"(\s*)fwrite\(ec_b->(\w+)_buffer \+ ec_r \* ec_b->(\w+), 1,\s*\n?\s*ec_b->(\w+),\s*\n?\s*ec_f\);") | |
| 982 | + | |
| 983 | + | |
| 984 | +def narrow(match): | |
| 985 | + rows, indent, plane, stride, width = match.groups() | |
| 986 | + return ("for (int ec_r = 0; ec_r < ec_b->%s; ++ec_r)\n%s" | |
| 987 | + "ec_dump_narrow_row(ec_f, ec_b, ec_b->%s_buffer, ec_b->%s, ec_r,\n" | |
| 988 | + "%s%s ec_b->%s);" % (rows, indent, plane, stride, indent, indent, width)) | |
| 989 | + | |
| 990 | + | |
| 991 | +s, rewritten = row_loop.subn(narrow, s) | |
| 992 | +if rewritten: | |
| 993 | + print("narrow-row depth fix: rewrote %d plane row loops" % rewritten) | |
| 994 | +else: | |
| 995 | + print("narrow-row depth fix: no legacy row loop left (already depth-correct)") | |
| 996 | +open(path, "w").write(s) | |
| 997 | +PYN |