From 6e36a7de49dbcf82a7f03480f9a39abe6f1dcc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 1 Oct 2026 19:27:21 +0300 Subject: [PATCH] lane-unwritten-dep r5: the unsafe in fresh_plane is justified by a retracted measurement -- filed, not hidden Three items. The first is the highest-risk thing this lane found and it is NOT closed here; it is filed with its reachability evidence and the comments now say so, so a reviewer can no longer approve the unsafe on a withdrawn proof. 1 -- fresh_plane's SAFETY ARGUMENT WAS A RETRACTED MEASUREMENT. The doc claimed the SB-padded surface "is written sample-for-sample by the tile walk's reconstruction before anything reads it" and cited the sentinel gate ("proven by the sentinel gate above"). The same file retracts it: on 440_request_is_422 five runs with uninitialised planes give TWO different output hashes, five sentinel runs give one, and the pre-deblock census finds 112 unwritten samples per chroma plane, 224 total. Output varying with the buffer's initial content means an uninitialised sample was READ. (a) Both comments -- the block doc and the inline SAFETY note -- now carry the true, weaker claim. The gate proves no unwritten sample SURVIVES into the frame the caller receives; it does NOT prove every sample is written before it is read, because it counts at two points (pre-deblock, output) and therefore measures survival, not reads. A sample read as a prediction or filter neighbour and overwritten a moment later is invisible to both counts; no such read has been observed and no instrument here measures for one. On main = e45cc748 the one path where a read was demonstrated is unreachable (lane/av1unwritten refuses the subsize before descending). (b) Not silently reworded, and the unsafe is UNCHANGED -- the block says "THE `unsafe` BELOW IS NOT FULLY JUSTIFIED" and the inline note says "SAFETY, PARTIAL -- the premise is NOT established". Filed as report section 12 with the two closures: (i) zero the allocation, the sound default, at the ~3% of frame-thread cycles at 4K that lane-picalloc removed; or (ii) a per-sample reads-before-write instrument, the only route that keeps the optimisation. Neither taken here: both are decisions for a lane chartered for them. Report section 12 also carries the reachability evidence -- a demonstrated read on 440_request_is_422 at 8d6998d7, now refused, and one refused witness cannot establish the universal claim for the other 126 fixtures. 2 -- dump_stage16's DOC CONTRADICTED THE CODE. It still said it "matches aomdec's own ec_dump16 rung byte for byte (crop dims, u16 LE)", true only of the 4:2:0 crop that was removed. Rewritten to the per-axis extent and to the oracle's actual crop (rung 1 writes y_crop_width x y_crop_height and uv_crop_width x uv_crop_height, each already per-axis), with the byte shape tabulated for all four subsampling modes. Notes that round_ss at ss=1 is div_ceil(2), so every 4:2:0 dump is byte-for-byte unchanged. 3 -- DOES THE GATE SUPPLY THE READER take_unwritten_samples LACKED? NO, and the report says so. Checked: take_unwritten_samples and take_census_scanned still have NO committed reader on main = e45cc748 -- the only references anywhere in crates/ec-av1/src are in their own doc comments. This gate reads only its own pair, take_final_unwritten_samples / take_final_census_scanned. So the r4 pairing is a TEMPLATE for how a census gate should be written (count AND scanned, so "scanned and found nothing" is distinguishable from "never scanned"); it is NOT a committed gate over the pre-deblock census, and no such claim is made. Report section 13 names the gap rather than assuming it closed. Green on the merged tree: 1 passed; 0 failed; 816 filtered out; 70.92s. Not touched, per instruction: the other stale-prose sites in these files (census_unwritten's "each frame's pre-deblock point" doc, the dead stage_dump_idx(), the POSTFILT_DUMP rung reference), and out of scope: the decode path, film_grain.rs, the reserved 4:2:0 group-tail chroma SKIP arm, decode_rect4_16_intrabc, the dump writers' error swallowing. --- crates/ec-av1/src/decode.rs | 87 +++++++++++++++++++++++++++++------ lanes/unwritten-dep.report.md | 71 ++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 14 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 9dc706a3..9652249f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -21117,8 +21117,25 @@ pub(crate) fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. /// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of -/// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects) -- -/// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). +/// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects). +/// lane-unwritten-dep: this doc previously said the dump "matches aomdec's own +/// `ec_dump16` rung byte for byte (crop dims, u16 LE)", which was true only of +/// the 4:2:0 crop it then used. The crop is now PER-AXIS, which is what the +/// oracle's rung 1 actually does -- it writes `y_crop_width` x `y_crop_height`, +/// `uv_crop_width` x `uv_crop_height`, `uv_crop_width` x `uv_crop_height` (the +/// libaom `YV12_BUFFER_CONFIG` fields, each already per-axis). Byte shape per +/// subsampling, `fw x fh` luma then both chroma planes, u16 LE, so: +/// +/// ```text +/// ss_x ss_y format luma chroma (each) +/// 0 0 4:4:4 fw x fh fw x fh fw*fh*3 samples +/// 1 1 4:2:0 fw x fh fw/2 x fh/2 fw*fh*3/2 samples +/// 1 0 4:2:2 fw x fh fw/2 x fh fw*fh*2 samples +/// 0 1 4:4:0 fw x fh fw x fh/2 fw*fh*3/2 samples +/// ``` +/// +/// (`round_ss` is the rounding the decoder's own plane extents use; at ss = 1 it +/// is `div_ceil(2)`, so every 4:2:0 dump is byte-for-byte unchanged by this.) /// lane-mc64 r1: the decode-order index the stage dumps below key their file /// name on. `PREFILT_PICTURE_IDX` is bumped once per frame just BEFORE the /// deblock/CDEF stages run, so the frame being dumped here is `idx - 1` -- @@ -21718,23 +21735,65 @@ pub(crate) fn census_unwritten_final(y: &[u16], u: &[u16], v: &[u16]) { /// A frame plane's own backing store, `n` samples, UNINITIALISED. /// -/// (lane-picalloc) The SB-padded coding surface is written sample-for-sample -/// by the tile walk's reconstruction before anything reads it, so the -/// `vec![0u16; n]` this replaces spent ~3% of the frame thread's cycles -/// zeroing 25 MB (at 4K) that the very next pass overwrites -- serially, at -/// the frame's head, on the critical path. `EC_AV1_PLANE_SENTINEL=1` swaps the -/// uninitialised buffer for a [`PLANE_SENTINEL`] fill; the gate hashes both -/// ways (see the lane report) and the two agree, which is the proof that no -/// unwritten sample reaches the output. +/// (lane-picalloc) Replaced a `vec![0u16; n]` that zeroed the whole plane +/// buffer: ~3% of the frame thread's cycles at 4K, serially at the frame's head, +/// to initialise 25 MB the next pass overwrites. +/// +/// # THE `unsafe` BELOW IS NOT FULLY JUSTIFIED -- read this before approving it +/// +/// This comment previously claimed the SB-padded surface "is written +/// sample-for-sample by the tile walk's reconstruction before anything reads +/// it", and cited the sentinel gate as proof. **That claim is RETRACTED by +/// measurement, in this same file**: on `440_request_is_422` the decode produced +/// TWO different output hashes across five runs with uninitialised planes, one +/// hash under `EC_AV1_PLANE_SENTINEL=1`, and a pre-deblock census of 112 +/// unwritten samples per chroma plane (224 total) -- see +/// `chroma_plane_block_codable`'s "What led here" paragraph and +/// `lanes/av1unwritten.report.md`. Output that varies with the buffer's initial +/// content means an uninitialised sample was READ. A reviewer approving this +/// `unsafe` on the old text was approving it on a withdrawn proof. +/// +/// WHAT IS ACTUALLY ESTABLISHED, and it is weaker: +/// +/// * The sentinel gate ([`census_unwritten_final`], +/// `the_frame_the_caller_receives_carries_no_unwritten_plane_sample`) proves +/// that no sample the tile walk never wrote SURVIVES into the frame the caller +/// receives, on the corpus measured. It counts at two points -- pre-deblock and +/// the output point -- so it measures SURVIVAL, not reads. +/// * It does NOT prove "every sample is written before it is read". A sample read +/// as a prediction or filter neighbour and overwritten a moment later is +/// invisible to both counts. **No such read has been observed, and no +/// instrument here measures for one.** +/// * On `main` = `e45cc748` the one path where a read was demonstrated is +/// UNREACHABLE: lane/av1unwritten refuses the subsize before descending. +/// +/// Honest status: a read-before-write is DEMONSTRATED on a path that is now +/// refused, UNMEASURED on every other path, and this `unsafe` is sound only under +/// the unproven "written before read" premise. Filed as an open UB-class item +/// (`lanes/unwritten-dep.report.md` §12) with two closures: (a) zero the +/// allocation -- the safe default, at the ~3% this removed; or (b) an instrument +/// counting reads-before-write per sample, so the premise can be proved rather +/// than assumed. fn fresh_plane(n: usize) -> Vec { if plane_sentinel_on() { return vec![PLANE_SENTINEL; n]; } let mut v: Vec = Vec::with_capacity(n); - // SAFETY: `u16` has no invalid bit patterns and no `Drop`, and the - // capacity was just reserved, so the vector owns `n` initialised-or-not - // `u16` slots. Every one is written by reconstruction before it is read - // (proven by the sentinel gate above). + // SAFETY, PARTIAL -- the premise is NOT established; see the block comment + // above ("THE `unsafe` BELOW IS NOT FULLY JUSTIFIED"). + // + // What IS sound: `u16` has no invalid bit patterns and no `Drop`, and the + // capacity was just reserved, so the vector owns `n` slots that are valid to + // OWN. Creating a `Vec` over uninitialised memory is not itself UB. + // + // What is NOT sound, and is the reason this is a filed item rather than a + // settled one: reading an uninitialised `u16` IS UB, and the only thing that + // makes the reads sound is the premise that reconstruction writes every + // sample before any read. That premise was RETRACTED by measurement (224 + // unwritten samples, output varying with initial content) on a path that is + // now refused, and is UNMEASURED everywhere else. The sentinel gate measures + // survival at two points, not reads. So this `unsafe` is sound under a + // premise no current instrument proves. #[allow(unsafe_code)] unsafe { v.set_len(n); diff --git a/lanes/unwritten-dep.report.md b/lanes/unwritten-dep.report.md index a362728b..d1064a12 100644 --- a/lanes/unwritten-dep.report.md +++ b/lanes/unwritten-dep.report.md @@ -336,3 +336,74 @@ number moves. `lanes/unwritten-dep.report.md` (this file) is the only other DUMP16 reference touching a non-4:2:0 frame, and its numbers are the corrected ones above. + +## 12. OPEN, filed: the `unsafe { v.set_len(n) }` in `fresh_plane` is not fully justified + +**This is the highest-risk item this lane found, and it is NOT closed here.** It +is filed rather than fixed because closing it means either restoring a ~3% frame- +head cost or building a reads-before-write instrument, and both are decisions +that belong to a lane chartered for them. The comments in `decode.rs` now say +this, so a reviewer can no longer approve the `unsafe` on a withdrawn proof. + +**The retracted claim.** `fresh_plane`'s doc asserted the SB-padded surface "is +written sample-for-sample by the tile walk's reconstruction before anything reads +it", and cited the sentinel gate as proof ("proven by the sentinel gate above"). +**The same file retracts it**, at `chroma_plane_block_codable`'s "What led here": +on `440_request_is_422`, five runs with uninitialised planes give **two different +output hashes**, five sentinel runs give one, and the pre-deblock census finds +**112 unwritten samples per chroma plane, 224 total** +(`lanes/av1unwritten.report.md:126-128`). Output that varies with the buffer's +initial content means an uninitialised sample was **read**. + +**What the gate actually proves — the weaker claim.** The sentinel census counts +at two points, pre-deblock and the output point, so it measures **survival, not +reads**: + +* PROVEN: no sample the tile walk never wrote **survives into the frame the + caller receives**, on the corpus measured. +* NOT PROVEN: "every sample is written before it is read". A sample read as a + prediction or filter neighbour and overwritten a moment later is invisible to + both counts. **No such read has been observed, and no instrument in this crate + measures for one.** +* On `main` = `e45cc748` the one path where a read was **demonstrated** is now + unreachable — lane/av1unwritten refuses the subsize before descending. + +**Why this is UB-class and not a style issue.** `Vec::set_len` over +uninitialised memory is not itself UB: `u16` has no invalid bit patterns and no +`Drop`, so the vector validly *owns* `n` slots. The UB is in **reading** an +uninitialised `u16`. `set_len` is therefore sound only under the "written before +read" premise, and that premise is the one the measurement withdrew. + +**Two closures, neither taken here:** + +1. **Zero the allocation** — restore `vec![0u16; n]`. Sound unconditionally, at + the ~3% of frame-thread cycles at 4K that lane-picalloc removed. This is the + safe default and the honest cost of the optimisation. +2. **Instrument reads-before-write** — a per-sample read/write map, so the premise + can be proved rather than assumed. That is a real instrument, not a comment, + and it is the only route that keeps the optimisation. + +**Reachability evidence** (what makes this fileable rather than theoretical): a +demonstrated read on `440_request_is_422` at `8d6998d7` — 224 unwritten samples, +output varying with initial content, two distinct plain hashes across five runs +— and that path is refused on `main`, so the corpus contains no *currently +reachable* instance. The claim that needs proving is universal ("every sample is +written before it is read"), and one refused witness cannot establish it for the +other 126 fixtures. + +## 13. Does this lane's gate supply the reader `take_unwritten_samples` lacked? — NO + +Checked, and the answer is the unflattering one. `take_unwritten_samples` and +`take_census_scanned` (lane/av1unwritten's pre-deblock census API) still have +**no committed reader** on `main` = `e45cc748`: the only references to them +anywhere in `crates/ec-av1/src` are in their own doc comments. `grep` over +`stream.rs` and the rest finds no call site. + +My gate reads only its OWN pair, `take_final_unwritten_samples` / +`take_final_census_scanned`. So the r4 pairing is a **template** for how a census +gate should be written (count AND scanned, so "scanned and found nothing" is +distinguishable from "never scanned") — it is **not** a committed gate over the +pre-deblock census, and this report does not claim one. Closing that gap means +either adding the pre-deblock pair to an existing gate's assertions or giving it +its own; that is a decision for the lane that owns the instrument, and it is named +here rather than assumed done.