lane-unwritten-dep r5: the unsafe in fresh_plane is justified by a retracted measurement -- filed, not hidden
Browse files at this commit6e36a7de49dbcf82a7f03480f9a39abe6f1dcc21Parent: c14e1db2 changed files +144 -14
- crates/ec-av1/src/decode.rs +73 -14
- lanes/unwritten-dep.report.md +71 -0
crates/ec-av1/src/decode.rsmodified+73 -14
| @@ -21117,8 +21117,25 @@ pub(crate) fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { | ||
| 21117 | 21117 | /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written |
| 21118 | 21118 | /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. |
| 21119 | 21119 | /// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of |
| 21120 | -/// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects) -- | |
| 21121 | -/// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). | |
| 21120 | +/// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects). | |
| 21121 | +/// lane-unwritten-dep: this doc previously said the dump "matches aomdec's own | |
| 21122 | +/// `ec_dump16` rung byte for byte (crop dims, u16 LE)", which was true only of | |
| 21123 | +/// the 4:2:0 crop it then used. The crop is now PER-AXIS, which is what the | |
| 21124 | +/// oracle's rung 1 actually does -- it writes `y_crop_width` x `y_crop_height`, | |
| 21125 | +/// `uv_crop_width` x `uv_crop_height`, `uv_crop_width` x `uv_crop_height` (the | |
| 21126 | +/// libaom `YV12_BUFFER_CONFIG` fields, each already per-axis). Byte shape per | |
| 21127 | +/// subsampling, `fw x fh` luma then both chroma planes, u16 LE, so: | |
| 21128 | +/// | |
| 21129 | +/// ```text | |
| 21130 | +/// ss_x ss_y format luma chroma (each) | |
| 21131 | +/// 0 0 4:4:4 fw x fh fw x fh fw*fh*3 samples | |
| 21132 | +/// 1 1 4:2:0 fw x fh fw/2 x fh/2 fw*fh*3/2 samples | |
| 21133 | +/// 1 0 4:2:2 fw x fh fw/2 x fh fw*fh*2 samples | |
| 21134 | +/// 0 1 4:4:0 fw x fh fw x fh/2 fw*fh*3/2 samples | |
| 21135 | +/// ``` | |
| 21136 | +/// | |
| 21137 | +/// (`round_ss` is the rounding the decoder's own plane extents use; at ss = 1 it | |
| 21138 | +/// is `div_ceil(2)`, so every 4:2:0 dump is byte-for-byte unchanged by this.) | |
| 21122 | 21139 | /// lane-mc64 r1: the decode-order index the stage dumps below key their file |
| 21123 | 21140 | /// name on. `PREFILT_PICTURE_IDX` is bumped once per frame just BEFORE the |
| 21124 | 21141 | /// 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]) { | ||
| 21718 | 21735 | |
| 21719 | 21736 | /// A frame plane's own backing store, `n` samples, UNINITIALISED. |
| 21720 | 21737 | /// |
| 21721 | -/// (lane-picalloc) The SB-padded coding surface is written sample-for-sample | |
| 21722 | -/// by the tile walk's reconstruction before anything reads it, so the | |
| 21723 | -/// `vec![0u16; n]` this replaces spent ~3% of the frame thread's cycles | |
| 21724 | -/// zeroing 25 MB (at 4K) that the very next pass overwrites -- serially, at | |
| 21725 | -/// the frame's head, on the critical path. `EC_AV1_PLANE_SENTINEL=1` swaps the | |
| 21726 | -/// uninitialised buffer for a [`PLANE_SENTINEL`] fill; the gate hashes both | |
| 21727 | -/// ways (see the lane report) and the two agree, which is the proof that no | |
| 21728 | -/// unwritten sample reaches the output. | |
| 21738 | +/// (lane-picalloc) Replaced a `vec![0u16; n]` that zeroed the whole plane | |
| 21739 | +/// buffer: ~3% of the frame thread's cycles at 4K, serially at the frame's head, | |
| 21740 | +/// to initialise 25 MB the next pass overwrites. | |
| 21741 | +/// | |
| 21742 | +/// # THE `unsafe` BELOW IS NOT FULLY JUSTIFIED -- read this before approving it | |
| 21743 | +/// | |
| 21744 | +/// This comment previously claimed the SB-padded surface "is written | |
| 21745 | +/// sample-for-sample by the tile walk's reconstruction before anything reads | |
| 21746 | +/// it", and cited the sentinel gate as proof. **That claim is RETRACTED by | |
| 21747 | +/// measurement, in this same file**: on `440_request_is_422` the decode produced | |
| 21748 | +/// TWO different output hashes across five runs with uninitialised planes, one | |
| 21749 | +/// hash under `EC_AV1_PLANE_SENTINEL=1`, and a pre-deblock census of 112 | |
| 21750 | +/// unwritten samples per chroma plane (224 total) -- see | |
| 21751 | +/// `chroma_plane_block_codable`'s "What led here" paragraph and | |
| 21752 | +/// `lanes/av1unwritten.report.md`. Output that varies with the buffer's initial | |
| 21753 | +/// content means an uninitialised sample was READ. A reviewer approving this | |
| 21754 | +/// `unsafe` on the old text was approving it on a withdrawn proof. | |
| 21755 | +/// | |
| 21756 | +/// WHAT IS ACTUALLY ESTABLISHED, and it is weaker: | |
| 21757 | +/// | |
| 21758 | +/// * The sentinel gate ([`census_unwritten_final`], | |
| 21759 | +/// `the_frame_the_caller_receives_carries_no_unwritten_plane_sample`) proves | |
| 21760 | +/// that no sample the tile walk never wrote SURVIVES into the frame the caller | |
| 21761 | +/// receives, on the corpus measured. It counts at two points -- pre-deblock and | |
| 21762 | +/// the output point -- so it measures SURVIVAL, not reads. | |
| 21763 | +/// * It does NOT prove "every sample is written before it is read". A sample read | |
| 21764 | +/// as a prediction or filter neighbour and overwritten a moment later is | |
| 21765 | +/// invisible to both counts. **No such read has been observed, and no | |
| 21766 | +/// instrument here measures for one.** | |
| 21767 | +/// * On `main` = `e45cc748` the one path where a read was demonstrated is | |
| 21768 | +/// UNREACHABLE: lane/av1unwritten refuses the subsize before descending. | |
| 21769 | +/// | |
| 21770 | +/// Honest status: a read-before-write is DEMONSTRATED on a path that is now | |
| 21771 | +/// refused, UNMEASURED on every other path, and this `unsafe` is sound only under | |
| 21772 | +/// the unproven "written before read" premise. Filed as an open UB-class item | |
| 21773 | +/// (`lanes/unwritten-dep.report.md` §12) with two closures: (a) zero the | |
| 21774 | +/// allocation -- the safe default, at the ~3% this removed; or (b) an instrument | |
| 21775 | +/// counting reads-before-write per sample, so the premise can be proved rather | |
| 21776 | +/// than assumed. | |
| 21729 | 21777 | fn fresh_plane(n: usize) -> Vec<u16> { |
| 21730 | 21778 | if plane_sentinel_on() { |
| 21731 | 21779 | return vec![PLANE_SENTINEL; n]; |
| 21732 | 21780 | } |
| 21733 | 21781 | let mut v: Vec<u16> = Vec::with_capacity(n); |
| 21734 | - // SAFETY: `u16` has no invalid bit patterns and no `Drop`, and the | |
| 21735 | - // capacity was just reserved, so the vector owns `n` initialised-or-not | |
| 21736 | - // `u16` slots. Every one is written by reconstruction before it is read | |
| 21737 | - // (proven by the sentinel gate above). | |
| 21782 | + // SAFETY, PARTIAL -- the premise is NOT established; see the block comment | |
| 21783 | + // above ("THE `unsafe` BELOW IS NOT FULLY JUSTIFIED"). | |
| 21784 | + // | |
| 21785 | + // What IS sound: `u16` has no invalid bit patterns and no `Drop`, and the | |
| 21786 | + // capacity was just reserved, so the vector owns `n` slots that are valid to | |
| 21787 | + // OWN. Creating a `Vec` over uninitialised memory is not itself UB. | |
| 21788 | + // | |
| 21789 | + // What is NOT sound, and is the reason this is a filed item rather than a | |
| 21790 | + // settled one: reading an uninitialised `u16` IS UB, and the only thing that | |
| 21791 | + // makes the reads sound is the premise that reconstruction writes every | |
| 21792 | + // sample before any read. That premise was RETRACTED by measurement (224 | |
| 21793 | + // unwritten samples, output varying with initial content) on a path that is | |
| 21794 | + // now refused, and is UNMEASURED everywhere else. The sentinel gate measures | |
| 21795 | + // survival at two points, not reads. So this `unsafe` is sound under a | |
| 21796 | + // premise no current instrument proves. | |
| 21738 | 21797 | #[allow(unsafe_code)] |
| 21739 | 21798 | unsafe { |
| 21740 | 21799 | v.set_len(n); |
lanes/unwritten-dep.report.mdmodified+71 -0
| @@ -336,3 +336,74 @@ number moves. | ||
| 336 | 336 | |
| 337 | 337 | `lanes/unwritten-dep.report.md` (this file) is the only other DUMP16 reference |
| 338 | 338 | touching a non-4:2:0 frame, and its numbers are the corrected ones above. |
| 339 | + | |
| 340 | +## 12. OPEN, filed: the `unsafe { v.set_len(n) }` in `fresh_plane` is not fully justified | |
| 341 | + | |
| 342 | +**This is the highest-risk item this lane found, and it is NOT closed here.** It | |
| 343 | +is filed rather than fixed because closing it means either restoring a ~3% frame- | |
| 344 | +head cost or building a reads-before-write instrument, and both are decisions | |
| 345 | +that belong to a lane chartered for them. The comments in `decode.rs` now say | |
| 346 | +this, so a reviewer can no longer approve the `unsafe` on a withdrawn proof. | |
| 347 | + | |
| 348 | +**The retracted claim.** `fresh_plane`'s doc asserted the SB-padded surface "is | |
| 349 | +written sample-for-sample by the tile walk's reconstruction before anything reads | |
| 350 | +it", and cited the sentinel gate as proof ("proven by the sentinel gate above"). | |
| 351 | +**The same file retracts it**, at `chroma_plane_block_codable`'s "What led here": | |
| 352 | +on `440_request_is_422`, five runs with uninitialised planes give **two different | |
| 353 | +output hashes**, five sentinel runs give one, and the pre-deblock census finds | |
| 354 | +**112 unwritten samples per chroma plane, 224 total** | |
| 355 | +(`lanes/av1unwritten.report.md:126-128`). Output that varies with the buffer's | |
| 356 | +initial content means an uninitialised sample was **read**. | |
| 357 | + | |
| 358 | +**What the gate actually proves — the weaker claim.** The sentinel census counts | |
| 359 | +at two points, pre-deblock and the output point, so it measures **survival, not | |
| 360 | +reads**: | |
| 361 | + | |
| 362 | +* PROVEN: no sample the tile walk never wrote **survives into the frame the | |
| 363 | + caller receives**, on the corpus measured. | |
| 364 | +* NOT PROVEN: "every sample is written before it is read". A sample read as a | |
| 365 | + prediction or filter neighbour and overwritten a moment later is invisible to | |
| 366 | + both counts. **No such read has been observed, and no instrument in this crate | |
| 367 | + measures for one.** | |
| 368 | +* On `main` = `e45cc748` the one path where a read was **demonstrated** is now | |
| 369 | + unreachable — lane/av1unwritten refuses the subsize before descending. | |
| 370 | + | |
| 371 | +**Why this is UB-class and not a style issue.** `Vec::set_len` over | |
| 372 | +uninitialised memory is not itself UB: `u16` has no invalid bit patterns and no | |
| 373 | +`Drop`, so the vector validly *owns* `n` slots. The UB is in **reading** an | |
| 374 | +uninitialised `u16`. `set_len` is therefore sound only under the "written before | |
| 375 | +read" premise, and that premise is the one the measurement withdrew. | |
| 376 | + | |
| 377 | +**Two closures, neither taken here:** | |
| 378 | + | |
| 379 | +1. **Zero the allocation** — restore `vec![0u16; n]`. Sound unconditionally, at | |
| 380 | + the ~3% of frame-thread cycles at 4K that lane-picalloc removed. This is the | |
| 381 | + safe default and the honest cost of the optimisation. | |
| 382 | +2. **Instrument reads-before-write** — a per-sample read/write map, so the premise | |
| 383 | + can be proved rather than assumed. That is a real instrument, not a comment, | |
| 384 | + and it is the only route that keeps the optimisation. | |
| 385 | + | |
| 386 | +**Reachability evidence** (what makes this fileable rather than theoretical): a | |
| 387 | +demonstrated read on `440_request_is_422` at `8d6998d7` — 224 unwritten samples, | |
| 388 | +output varying with initial content, two distinct plain hashes across five runs | |
| 389 | +— and that path is refused on `main`, so the corpus contains no *currently | |
| 390 | +reachable* instance. The claim that needs proving is universal ("every sample is | |
| 391 | +written before it is read"), and one refused witness cannot establish it for the | |
| 392 | +other 126 fixtures. | |
| 393 | + | |
| 394 | +## 13. Does this lane's gate supply the reader `take_unwritten_samples` lacked? — NO | |
| 395 | + | |
| 396 | +Checked, and the answer is the unflattering one. `take_unwritten_samples` and | |
| 397 | +`take_census_scanned` (lane/av1unwritten's pre-deblock census API) still have | |
| 398 | +**no committed reader** on `main` = `e45cc748`: the only references to them | |
| 399 | +anywhere in `crates/ec-av1/src` are in their own doc comments. `grep` over | |
| 400 | +`stream.rs` and the rest finds no call site. | |
| 401 | + | |
| 402 | +My gate reads only its OWN pair, `take_final_unwritten_samples` / | |
| 403 | +`take_final_census_scanned`. So the r4 pairing is a **template** for how a census | |
| 404 | +gate should be written (count AND scanned, so "scanned and found nothing" is | |
| 405 | +distinguishable from "never scanned") — it is **not** a committed gate over the | |
| 406 | +pre-deblock census, and this report does not claim one. Closing that gap means | |
| 407 | +either adding the pre-deblock pair to an existing gate's assertions or giving it | |
| 408 | +its own; that is a decision for the lane that owns the instrument, and it is named | |
| 409 | +here rather than assumed done. |