forge
Explore
Log in

Don't you dare go Hollow

Enumerate read_golomb over the whole value domain spec 5.11.40 writes

Browse files at this commit

d55c5497075c2ca3282cd657c0f82449911dae53Ahmet Kaan GümüşParent: d8a578a
  • test
    • enumerate read_golomb over the whole value domain spec 5.11.40 writes
Show full message
test(av1): enumerate read_golomb over the whole value domain spec 5.11.40 writes

"a Golomb tail longer than this decoder reads" was a bound with nothing
measuring it. Spec 5.11.40 counts the unary prefix and breaks at
length == 20, and 5.11.39 masks the level it feeds with 0xFFFFF, so the
largest tail a conformant stream carries is (1 << 20) - 2 -- and this walks
both ends of every prefix length 1..=20 (plus the small values real streams
use) out through write_golomb and back through read_golomb, so the refusal
is proved to sit outside the value domain rather than inside it. The residue
is a bit pattern, not a value: the spec's break makes the 20th prefix bit a
don't-care, and a 20-zero prefix is still refused (asserted, so the refusal
is not dead code). The cap itself stays where it is -- lifting it is blocked
on the desync it currently masks. Mutation-checked (cap at 10 fails on tail
1023).
crates/ec-av1/src/decode.rsmodified+1 -1
@@ -4157,7 +4157,7 @@ pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option<TxType> {
4157 /// The remainder of a level the base and base-range syntax could not reach4157 /// The remainder of a level the base and base-range syntax could not reach
4158 /// (spec 5.11.40): its bit length in unary, then that many of its own bits,4158 /// (spec 5.11.40): its bit length in unary, then that many of its own bits,
4159 /// most significant first — the exact inverse of [`crate::tile::write_golomb`].4159 /// most significant first — the exact inverse of [`crate::tile::write_golomb`].
4160-fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> {4160+pub(crate) fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> {
4161 // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted4161 // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted
4162 // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom4162 // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom
4163 // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical4163 // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical
crates/ec-av1/src/refusal_inventory.rsmodified+76 -0
@@ -253,6 +253,15 @@ const PROVEN: &[(&str, &str)] = &[
253 "a tx_type symbol outside its CDF's own set: {t}",253 "a tx_type symbol outside its CDF's own set: {t}",
254 "every_tx_type_symbol_of_every_cdf_width_maps_into_its_own_set",254 "every_tx_type_symbol_of_every_cdf_width_maps_into_its_own_set",
255 ),255 ),
256+ // lane-t900 r24, enumeration: the reader covers spec 5.11.40's whole value
257+ // domain (`0..=(1 << 20) - 2`, both ends of every prefix length), so the
258+ // refusal is outside it. What it still names is a bit pattern no encoder
259+ // writes -- a 20th prefix bit of 0, which the spec's `length == 20` break
260+ // makes a don't-care.
261+ (
262+ "a Golomb tail longer than this decoder reads",
263+ "read_golomb_reads_every_value_a_conformant_stream_can_carry",
264+ ),
256 (265 (
257 "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)",266 "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)",
258 "a_twelve_bit_sequence_header_is_refused_by_name",267 "a_twelve_bit_sequence_header_is_refused_by_name",
@@ -733,6 +742,73 @@ mod tests {
733 }742 }
734 }743 }
735 744
745+ /// The Golomb tail, enumerated over every value a conformant stream can
746+ /// carry.
747+ ///
748+ /// Spec 5.11.40 `read_golomb` counts its unary prefix and BREAKS at
749+ /// `length == 20`, then reads `length - 1` payload bits into an `x` whose
750+ /// top bit is implicit -- so no conformant stream carries a value above
751+ /// `(1 << 20) - 2`, and spec 5.11.39 agrees from the other side by masking
752+ /// the level it feeds with `0xFFFFF`. This walks both ends of every
753+ /// prefix length 1..=20 (plus the small values every real stream uses)
754+ /// through [`crate::tile::write_golomb`] and back through the decoder's own
755+ /// [`crate::decode::read_golomb`], so the refusal "a Golomb tail longer
756+ /// than this decoder reads" is proved to sit strictly OUTSIDE the value
757+ /// domain, not inside it.
758+ ///
759+ /// The residue is a bit pattern, not a value: the spec's break makes the
760+ /// 20th prefix bit a don't-care, so a stream whose 20th prefix bit is 0
761+ /// decodes to the same value there and is refused here. Every encoder
762+ /// writes the terminating 1 (our own writer tops out at 19 zeros), and
763+ /// lifting the cap is blocked on the defect it currently masks -- see the
764+ /// comment on `read_golomb`.
765+ #[test]
766+ fn read_golomb_reads_every_value_a_conformant_stream_can_carry() {
767+ let roundtrip = |value: u32| -> ec_core::Result<u32> {
768+ let mut enc = crate::msac::SymbolEncoder::new();
769+ crate::tile::write_golomb(&mut enc, value);
770+ let data = enc.finish();
771+ let mut dec = crate::msac::SymbolDecoder::new(&data);
772+ crate::decode::read_golomb(&mut dec)
773+ };
774+ let mut values: BTreeSet<u32> = (0..=64u32).collect();
775+ for length in 1..=20u32 {
776+ // `x` has `length` bits with its top bit set, and the value is
777+ // `x - 1`: the two ends of what this prefix length can express.
778+ values.insert((1u32 << (length - 1)) - 1);
779+ values.insert(((1u64 << length) - 2) as u32);
780+ }
781+ let max = *values.iter().max().unwrap();
782+ assert_eq!(max, (1u32 << 20) - 2, "the enumeration misses the spec's own ceiling");
783+ // A coefficient is masked to 20 bits (spec 5.11.39), and the base and
784+ // base-range syntax contribute at most 15 of that before the tail, so
785+ // this is the largest tail a legal coefficient can need.
786+ assert!(max >= 0xF_FFFF - 15, "the ceiling is below the largest legal tail");
787+ for value in values {
788+ match roundtrip(value) {
789+ Ok(read) => assert_eq!(read, value, "the Golomb tail {value} read back as {read}"),
790+ Err(e) => panic!(
791+ "the Golomb tail {value} is inside the domain spec 5.11.40 can write but \
792+ this decoder refuses it: {e}"
793+ ),
794+ }
795+ }
796+ // And the refusal still names something: 20 zero prefix bits.
797+ let mut enc = crate::msac::SymbolEncoder::new();
798+ for _ in 0..20 {
799+ enc.literal(0, 1);
800+ }
801+ for _ in 0..20 {
802+ enc.literal(1, 1);
803+ }
804+ let data = enc.finish();
805+ let mut dec = crate::msac::SymbolDecoder::new(&data);
806+ assert!(
807+ crate::decode::read_golomb(&mut dec).is_err(),
808+ "the Golomb refusal is dead code -- drop it from the inventory"
809+ );
810+ }
811+
736 /// Every entry of [`PROVEN`] must still name a live refusal and a test812 /// Every entry of [`PROVEN`] must still name a live refusal and a test
737 /// that exists, and the count it prints is the inventory's numerator.813 /// that exists, and the count it prints is the inventory's numerator.
738 #[test]814 #[test]