U14: ec-mpeg2 delete the EC_MPEG2_TRACE debug aid (slice errors become unit errors) and robust.rs; its named regression test moves to tests/regression.rs
Browse files at this commit06551e9685970b7b577146eaab9ed681c8540fdeParent: c93fdfb3 changed files +37 -240
- crates/ec-mpeg2/src/decoder.rs +18 -26
- crates/ec-mpeg2/tests/regression.rs +19 -0
- crates/ec-mpeg2/tests/robust.rs +0 -214
crates/ec-mpeg2/src/decoder.rsmodified+18 -26
| @@ -660,9 +660,9 @@ impl Core { | ||
| 660 | 660 | |
| 661 | 661 | #[allow(clippy::too_many_lines)] |
| 662 | 662 | fn decode_slice(&mut self, code: u8, data: &[u8]) -> std::result::Result<(), ()> { |
| 663 | - let seq = self.seq.clone().ok_or_else(|| trace(line!()))?; | |
| 663 | + let seq = self.seq.clone().ok_or(())?; | |
| 664 | 664 | let t = tables(); |
| 665 | - let mut st = self.pic.take().ok_or_else(|| trace(line!()))?; | |
| 665 | + let mut st = self.pic.take().ok_or(())?; | |
| 666 | 666 | let r = self.slice_inner(&seq, t, &mut st, code, data); |
| 667 | 667 | self.pic = Some(st); |
| 668 | 668 | r |
| @@ -685,19 +685,19 @@ impl Core { | ||
| 685 | 685 | } |
| 686 | 686 | let mw = seq.mb_width(); |
| 687 | 687 | if mb_row >= st.rows { |
| 688 | - return Err(trace(line!())); | |
| 688 | + return Err(()); | |
| 689 | 689 | } |
| 690 | 690 | let qcode = b.read(5) as usize; |
| 691 | 691 | if qcode == 0 { |
| 692 | - return Err(trace(line!())); | |
| 692 | + return Err(()); | |
| 693 | 693 | } |
| 694 | 694 | while b.read1() { |
| 695 | 695 | b.skip(8); |
| 696 | 696 | } |
| 697 | - let cur_idx = self.cur.as_ref().ok_or_else(|| trace(line!()))?.idx; | |
| 697 | + let cur_idx = self.cur.as_ref().ok_or(())?.idx; | |
| 698 | 698 | // The picture must match this slice's geometry, or macroblock rows fall outside its planes. |
| 699 | 699 | if self.pool[cur_idx].height[0] != seq.mb_height() * 16 { |
| 700 | - return Err(trace(line!())); | |
| 700 | + return Err(()); | |
| 701 | 701 | } |
| 702 | 702 | let ptype = hdr.picture_type; |
| 703 | 703 | let field_pic = hdr.structure != Structure::Frame; |
| @@ -728,7 +728,7 @@ impl Core { | ||
| 728 | 728 | // macroblock_address_increment |
| 729 | 729 | let mut inc = 0usize; |
| 730 | 730 | loop { |
| 731 | - let v = b.vlc(&t.mba).ok_or_else(|| trace(line!()))?; | |
| 731 | + let v = b.vlc(&t.mba).ok_or(())?; | |
| 732 | 732 | if v == 34 { |
| 733 | 733 | inc += 33; |
| 734 | 734 | } else if v == 35 { |
| @@ -738,7 +738,7 @@ impl Core { | ||
| 738 | 738 | break; |
| 739 | 739 | } |
| 740 | 740 | if b.overrun() { |
| 741 | - return Err(trace(line!())); | |
| 741 | + return Err(()); | |
| 742 | 742 | } |
| 743 | 743 | } |
| 744 | 744 | if first { |
| @@ -749,7 +749,7 @@ impl Core { | ||
| 749 | 749 | for _ in 1..inc { |
| 750 | 750 | addr += 1; |
| 751 | 751 | if addr as usize >= total { |
| 752 | - return Err(trace(line!())); | |
| 752 | + return Err(()); | |
| 753 | 753 | } |
| 754 | 754 | let a = addr as usize; |
| 755 | 755 | // Skipped macroblock. |
| @@ -776,7 +776,7 @@ impl Core { | ||
| 776 | 776 | motion = m; |
| 777 | 777 | } |
| 778 | 778 | if ptype == PictureType::I { |
| 779 | - return Err(trace(line!())); | |
| 779 | + return Err(()); | |
| 780 | 780 | } |
| 781 | 781 | let info = MbInfo { intra: false, m, dct_field: false, cbp: 0, last: [0; 8] }; |
| 782 | 782 | self.recon_mb(seq, &hdr, st, cur_idx, a % mw, a / mw, &info, &mut blocks); |
| @@ -785,7 +785,7 @@ impl Core { | ||
| 785 | 785 | addr += 1; |
| 786 | 786 | } |
| 787 | 787 | if addr < 0 || addr as usize >= total { |
| 788 | - return Err(trace(line!())); | |
| 788 | + return Err(()); | |
| 789 | 789 | } |
| 790 | 790 | let a = addr as usize; |
| 791 | 791 | let mb_x = a % mw; |
| @@ -797,7 +797,7 @@ impl Core { | ||
| 797 | 797 | PictureType::P => 1, |
| 798 | 798 | PictureType::B => 2, |
| 799 | 799 | }; |
| 800 | - let flags = b.vlc(&t.mbtype[ti]).ok_or_else(|| trace(line!()))? as u16; | |
| 800 | + let flags = b.vlc(&t.mbtype[ti]).ok_or(())? as u16; | |
| 801 | 801 | let intra = flags & MB_INTRA != 0; |
| 802 | 802 | let mut m = Motion { fwd: flags & MB_FWD != 0, bwd: flags & MB_BWD != 0, ..Motion::default() }; |
| 803 | 803 | let mut dct_field = false; |
| @@ -808,7 +808,7 @@ impl Core { | ||
| 808 | 808 | } else { |
| 809 | 809 | kind_code = b.read(2); |
| 810 | 810 | if kind_code == 0 { |
| 811 | - return Err(trace(line!())); | |
| 811 | + return Err(()); | |
| 812 | 812 | } |
| 813 | 813 | } |
| 814 | 814 | } else if mpeg1 { |
| @@ -820,7 +820,7 @@ impl Core { | ||
| 820 | 820 | if flags & MB_QUANT != 0 { |
| 821 | 821 | let qc = b.read(5) as usize; |
| 822 | 822 | if qc == 0 { |
| 823 | - return Err(trace(line!())); | |
| 823 | + return Err(()); | |
| 824 | 824 | } |
| 825 | 825 | qscale = qscale_of(qc); |
| 826 | 826 | } |
| @@ -872,7 +872,7 @@ impl Core { | ||
| 872 | 872 | if intra { |
| 873 | 873 | cbp = (1 << nblocks) - 1; |
| 874 | 874 | } else if flags & MB_PAT != 0 { |
| 875 | - cbp = b.vlc(&t.cbp).ok_or_else(|| trace(line!()))?; | |
| 875 | + cbp = b.vlc(&t.cbp).ok_or(())?; | |
| 876 | 876 | if seq.chroma_format == 2 { |
| 877 | 877 | cbp = (cbp << 2) | b.read(2); |
| 878 | 878 | } |
| @@ -927,7 +927,7 @@ impl Core { | ||
| 927 | 927 | for blk in blocks.iter_mut() { |
| 928 | 928 | *blk = [0; 64]; |
| 929 | 929 | } |
| 930 | - return Err(trace(line!())); | |
| 930 | + return Err(()); | |
| 931 | 931 | } |
| 932 | 932 | let info = MbInfo { intra, m, dct_field, cbp, last }; |
| 933 | 933 | self.recon_mb(seq, &hdr, st, cur_idx, mb_x, mb_y, &info, &mut blocks); |
| @@ -939,7 +939,7 @@ impl Core { | ||
| 939 | 939 | motion = Motion { fwd: true, ..Motion::default() }; |
| 940 | 940 | } |
| 941 | 941 | if b.overrun() { |
| 942 | - return Err(trace(line!())); | |
| 942 | + return Err(()); | |
| 943 | 943 | } |
| 944 | 944 | if b.peek(23) == 0 { |
| 945 | 945 | break; |
| @@ -1223,7 +1223,7 @@ fn read_motion( | ||
| 1223 | 1223 | if tc == 1 && frame_field { |
| 1224 | 1224 | pred >>= 1; |
| 1225 | 1225 | } |
| 1226 | - let v = decode_component(b, t, hdr.f_code[s][tc], pred).ok_or_else(|| trace(line!()))?; | |
| 1226 | + let v = decode_component(b, t, hdr.f_code[s][tc], pred).ok_or(())?; | |
| 1227 | 1227 | pmv[r][s][tc] = if tc == 1 && frame_field { v << 1 } else { v }; |
| 1228 | 1228 | m.mv[s][r][tc] = v << shift; |
| 1229 | 1229 | if dual { |
| @@ -1281,14 +1281,6 @@ fn conceal_mb( | ||
| 1281 | 1281 | } |
| 1282 | 1282 | } |
| 1283 | 1283 | |
| 1284 | -/// Debug aid: with `EC_MPEG2_TRACE` set, report where a slice gave up. | |
| 1285 | -fn trace(line: u32) { | |
| 1286 | - static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | |
| 1287 | - if *ON.get_or_init(|| std::env::var_os("EC_MPEG2_TRACE").is_some()) { | |
| 1288 | - eprintln!("ec-mpeg2: slice error at decoder.rs:{line}"); | |
| 1289 | - } | |
| 1290 | -} | |
| 1291 | - | |
| 1292 | 1284 | fn chroma_shift(cf: u8) -> (usize, usize) { |
| 1293 | 1285 | match cf { |
| 1294 | 1286 | 1 => (1, 1), |
crates/ec-mpeg2/tests/regression.rsadded+19 -0
| @@ -0,0 +1,19 @@ | ||
| 1 | +use ec_core::registry::{CodecId, CodecParameters}; | |
| 2 | +use ec_mpeg2::Mpeg2Decoder; | |
| 3 | + | |
| 4 | +/// `decode_mpeg2` panic: a sequence extension changed the MB-row count (9 to 10 at 176x144) | |
| 5 | +/// without changing the size, so pictures allocated under the old geometry were decoded under | |
| 6 | +/// the new one and a macroblock row was written past the end of the plane. The decoder must | |
| 7 | +/// refuse the damage or conceal it and mark the frame corrupt, never panic. | |
| 8 | +#[test] | |
| 9 | +fn mb_height_change_without_resize_is_concealed() { | |
| 10 | + let data = include_bytes!("data/fuzz/decode_mpeg2-mb-height-change.m2v"); | |
| 11 | + let mut dec = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)).unwrap(); | |
| 12 | + let mut failed = dec.send_bytes(data, None).is_err(); | |
| 13 | + failed |= dec.flush_stream().is_err(); | |
| 14 | + let mut corrupt = false; | |
| 15 | + while let Some((_, info)) = dec.pop_picture() { | |
| 16 | + corrupt |= info.corrupt; | |
| 17 | + } | |
| 18 | + assert!(failed || corrupt, "damage must surface as an error or a corrupt frame"); | |
| 19 | +} |
crates/ec-mpeg2/tests/robust.rsdeleted+0 -214
| @@ -1,214 +0,0 @@ | ||
| 1 | -//! Robustness: truncated, corrupted and hostile streams must never panic and | |
| 2 | -//! must keep the output queue bounded. | |
| 3 | -#![allow(clippy::needless_range_loop, clippy::field_reassign_with_default, clippy::too_many_arguments, clippy::type_complexity)] | |
| 4 | - | |
| 5 | -mod common; | |
| 6 | -use common::synth::{SynthCfg, generate}; | |
| 7 | -use common::*; | |
| 8 | -use ec_core::registry::{CodecId, CodecParameters}; | |
| 9 | -use ec_mpeg2::Mpeg2Decoder; | |
| 10 | - | |
| 11 | -const MAX_QUEUE: usize = 64; | |
| 12 | - | |
| 13 | -struct Rng(u64); | |
| 14 | -impl Rng { | |
| 15 | - fn next(&mut self) -> u64 { | |
| 16 | - self.0 ^= self.0 << 13; | |
| 17 | - self.0 ^= self.0 >> 7; | |
| 18 | - self.0 ^= self.0 << 17; | |
| 19 | - self.0 | |
| 20 | - } | |
| 21 | - fn below(&mut self, n: usize) -> usize { | |
| 22 | - (self.next() % n.max(1) as u64) as usize | |
| 23 | - } | |
| 24 | -} | |
| 25 | - | |
| 26 | -fn streams() -> Vec<Vec<u8>> { | |
| 27 | - [(1u64, false, false), (2, true, false), (3, false, true), (4, true, true)] | |
| 28 | - .into_iter() | |
| 29 | - .map(|(seed, fields, p422)| { | |
| 30 | - generate(&SynthCfg { | |
| 31 | - mb_w: 3, | |
| 32 | - mb_h: 2, | |
| 33 | - chroma422: p422, | |
| 34 | - progressive_sequence: false, | |
| 35 | - frames: 5, | |
| 36 | - seed, | |
| 37 | - fields, | |
| 38 | - heavy: fields, | |
| 39 | - load_matrices: seed % 2 == 0, | |
| 40 | - }) | |
| 41 | - }) | |
| 42 | - .collect() | |
| 43 | -} | |
| 44 | - | |
| 45 | -/// Decode `data` in chunks of `chunk`; return the frame count. Errors are ignored. | |
| 46 | -fn run(data: &[u8], chunk: usize) -> usize { | |
| 47 | - let mut dec = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)).unwrap(); | |
| 48 | - let mut n = 0; | |
| 49 | - for c in data.chunks(chunk.max(1)) { | |
| 50 | - let _ = dec.send_bytes(c, None); | |
| 51 | - while dec.pop_picture().is_some() { | |
| 52 | - n += 1; | |
| 53 | - } | |
| 54 | - } | |
| 55 | - let _ = dec.flush_stream(); | |
| 56 | - while dec.pop_picture().is_some() { | |
| 57 | - n += 1; | |
| 58 | - } | |
| 59 | - n | |
| 60 | -} | |
| 61 | - | |
| 62 | -/// Like `run` but never drains until the end: the queue must stay bounded. | |
| 63 | -fn run_undrained(data: &[u8], chunk: usize) { | |
| 64 | - let mut dec = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)).unwrap(); | |
| 65 | - for c in data.chunks(chunk.max(1)) { | |
| 66 | - let _ = dec.send_bytes(c, None); | |
| 67 | - } | |
| 68 | - let _ = dec.flush_stream(); | |
| 69 | - let mut n = 0; | |
| 70 | - while dec.pop_picture().is_some() { | |
| 71 | - n += 1; | |
| 72 | - } | |
| 73 | - assert!(n <= MAX_QUEUE, "queued {n} frames"); | |
| 74 | -} | |
| 75 | - | |
| 76 | -#[test] | |
| 77 | -fn truncation_prefixes() { | |
| 78 | - for es in streams() { | |
| 79 | - assert!(run(&es, es.len()) > 0, "clean stream decodes nothing"); | |
| 80 | - let step = (es.len() / 40).max(1); | |
| 81 | - for cut in (0..es.len()).step_by(step).chain([es.len() - 1, es.len() - 2]) { | |
| 82 | - run(&es[..cut], 97); | |
| 83 | - run_undrained(&es[..cut], usize::MAX); | |
| 84 | - } | |
| 85 | - } | |
| 86 | -} | |
| 87 | - | |
| 88 | -#[test] | |
| 89 | -fn chunking_is_invariant() { | |
| 90 | - for es in streams() { | |
| 91 | - let mk = |chunk: usize| { | |
| 92 | - let a = decode_ours(&es, chunk).expect("clean stream"); | |
| 93 | - a.iter().map(|(f, _)| frame_bytes(f)).collect::<Vec<_>>() | |
| 94 | - }; | |
| 95 | - let whole = mk(es.len()); | |
| 96 | - assert!(!whole.is_empty()); | |
| 97 | - assert_eq!(whole, mk(1), "1-byte chunks differ"); | |
| 98 | - assert_eq!(whole, mk(7), "7-byte chunks differ"); | |
| 99 | - } | |
| 100 | -} | |
| 101 | - | |
| 102 | -#[test] | |
| 103 | -fn random_corruptions() { | |
| 104 | - let mut rng = Rng(0x1234_5678_9ABC_DEF1); | |
| 105 | - let all = streams(); | |
| 106 | - for i in 0..300 { | |
| 107 | - let mut es = all[i % all.len()].clone(); | |
| 108 | - for _ in 0..1 + rng.below(6) { | |
| 109 | - if es.is_empty() { | |
| 110 | - break; | |
| 111 | - } | |
| 112 | - let p = rng.below(es.len()); | |
| 113 | - match rng.below(4) { | |
| 114 | - 0 => es[p] ^= 1 << rng.below(8), | |
| 115 | - 1 => es[p] = rng.next() as u8, | |
| 116 | - 2 => { | |
| 117 | - let n = 1 + rng.below(8); | |
| 118 | - es.drain(p..(p + n).min(es.len())); | |
| 119 | - } | |
| 120 | - _ => { | |
| 121 | - for _ in 0..1 + rng.below(8) { | |
| 122 | - es.insert(p, rng.next() as u8); | |
| 123 | - } | |
| 124 | - } | |
| 125 | - } | |
| 126 | - } | |
| 127 | - run(&es, 1 + rng.below(300)); | |
| 128 | - run_undrained(&es, usize::MAX); | |
| 129 | - } | |
| 130 | -} | |
| 131 | - | |
| 132 | -#[test] | |
| 133 | -fn garbage_is_refused() { | |
| 134 | - let mut rng = Rng(0xDEAD_BEEF_CAFE_F00D); | |
| 135 | - for len in [0usize, 1, 3, 4, 100, 5000, 200_000] { | |
| 136 | - let g: Vec<u8> = (0..len).map(|_| rng.next() as u8).collect(); | |
| 137 | - run(&g, 1000); | |
| 138 | - run_undrained(&g, usize::MAX); | |
| 139 | - } | |
| 140 | - // Garbage dense with start codes. | |
| 141 | - let mut g = Vec::new(); | |
| 142 | - for _ in 0..2000 { | |
| 143 | - g.extend_from_slice(&[0, 0, 1, rng.next() as u8]); | |
| 144 | - for _ in 0..rng.below(20) { | |
| 145 | - g.push(rng.next() as u8); | |
| 146 | - } | |
| 147 | - } | |
| 148 | - run(&g, 333); | |
| 149 | - run_undrained(&g, usize::MAX); | |
| 150 | -} | |
| 151 | - | |
| 152 | -#[test] | |
| 153 | -fn huge_dimensions_are_bounded() { | |
| 154 | - // Sequence header 4095x4095 + sequence_extension adding 3<<12 to both: 16383x16383. | |
| 155 | - let mut es = vec![0, 0, 1, 0xB3, 0xFF, 0xFF, 0xFF, 0x13, 0xFF, 0xFF, 0xE0, 0x18]; | |
| 156 | - // sequence_extension: id=1, profile/level, progressive, chroma 4:2:0, size ext 3/3. | |
| 157 | - es.extend_from_slice(&[0, 0, 1, 0xB5, 0x14, 0x8A, 0x00, 0x01, 0x00, 0x00]); | |
| 158 | - es.extend_from_slice(&[0, 0, 1, 0xB5, 0x14, 0x8F, 0xFF, 0x01, 0x00, 0x00]); | |
| 159 | - // GOP + picture header (I) + a slice, repeated. | |
| 160 | - for _ in 0..3 { | |
| 161 | - es.extend_from_slice(&[0, 0, 1, 0xB8, 0, 0x08, 0, 0x40]); | |
| 162 | - es.extend_from_slice(&[0, 0, 1, 0x00, 0x00, 0x0F, 0xFF, 0xF8]); | |
| 163 | - es.extend_from_slice(&[0, 0, 1, 0xB5, 0x8F, 0xFF, 0xF3, 0x41, 0x00]); | |
| 164 | - es.extend_from_slice(&[0, 0, 1, 0x01, 0x10, 0x00, 0x00, 0x00]); | |
| 165 | - } | |
| 166 | - let mut dec = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)).unwrap(); | |
| 167 | - let r = dec.send_bytes(&es, None); | |
| 168 | - let _ = dec.flush_stream(); | |
| 169 | - let mut n = 0; | |
| 170 | - while let Some((f, _)) = dec.pop_picture() { | |
| 171 | - assert!(u64::from(f.width) * u64::from(f.height) <= 8192 * 4352); | |
| 172 | - n += 1; | |
| 173 | - } | |
| 174 | - assert!(n <= MAX_QUEUE); | |
| 175 | - if let Some(seq) = dec.sequence() { | |
| 176 | - eprintln!("sequence {}x{} (send: {:?})", seq.width, seq.height, r.is_ok()); | |
| 177 | - } | |
| 178 | - // Also at the 12-bit maximum without an extension, and with the max-dimension edge. | |
| 179 | - for (w, h) in [(4095u32, 4095u32), (8192, 8192), (16383, 1), (1, 16383)] { | |
| 180 | - let mut s = vec![0, 0, 1, 0xB3, (w >> 4) as u8, (((w & 15) << 4) | (h >> 8)) as u8, h as u8, 0x13, 0xFF, 0xFF, 0xE0, 0x18]; | |
| 181 | - s.extend_from_slice(&[0, 0, 1, 0x00, 0x00, 0x0F, 0xFF, 0xF8, 0, 0, 1, 0x01, 0x10, 0, 0]); | |
| 182 | - run(&s, usize::MAX); | |
| 183 | - run_undrained(&s, 5); | |
| 184 | - } | |
| 185 | -} | |
| 186 | - | |
| 187 | -/// `decode_mpeg2` panic: a sequence extension changed the MB-row count (9 to 10 at 176x144) | |
| 188 | -/// without changing the size, so pictures allocated under the old geometry were decoded under | |
| 189 | -/// the new one and a macroblock row was written past the end of the plane. The decoder must | |
| 190 | -/// refuse the damage or conceal it and mark the frame corrupt, never panic. | |
| 191 | -#[test] | |
| 192 | -fn mb_height_change_without_resize_is_concealed() { | |
| 193 | - let data = include_bytes!("data/fuzz/decode_mpeg2-mb-height-change.m2v"); | |
| 194 | - let mut dec = Mpeg2Decoder::new(CodecParameters::new(CodecId::Mpeg2Video)).unwrap(); | |
| 195 | - let mut failed = dec.send_bytes(data, None).is_err(); | |
| 196 | - failed |= dec.flush_stream().is_err(); | |
| 197 | - let mut corrupt = false; | |
| 198 | - while let Some((_, info)) = dec.pop_picture() { | |
| 199 | - corrupt |= info.corrupt; | |
| 200 | - } | |
| 201 | - assert!(failed || corrupt, "damage must surface as an error or a corrupt frame"); | |
| 202 | -} | |
| 203 | - | |
| 204 | -/// Writes the synthetic streams as fuzz seeds: | |
| 205 | -/// `EC_SEED_DIR=crates/ec-mpeg2/fuzz/corpus/decode_mpeg2 cargo test --test robust -- --ignored dump_seeds` | |
| 206 | -#[test] | |
| 207 | -#[ignore] | |
| 208 | -fn dump_seeds() { | |
| 209 | - let Some(dir) = std::env::var_os("EC_SEED_DIR") else { return }; | |
| 210 | - std::fs::create_dir_all(&dir).unwrap(); | |
| 211 | - for (i, s) in streams().iter().enumerate() { | |
| 212 | - std::fs::write(std::path::Path::new(&dir).join(format!("syn{i}.m2v")), s).unwrap(); | |
| 213 | - } | |
| 214 | -} |