| 1 | #![no_main] |
| 2 | |
| 3 | use ec_vp9::decode::Decoder; |
| 4 | use libfuzzer_sys::fuzz_target; |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | const MAX_DIM: u32 = 128; |
| 10 | |
| 11 | fuzz_target!(|data: &[u8]| { |
| 12 | if oversized_frame(data) { |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | let _ = Decoder::new().decode(data); |
| 17 | |
| 18 | |
| 19 | |
| 20 | if let Some(parts) = split(data) { |
| 21 | let mut dec = Decoder::new(); |
| 22 | for part in parts { |
| 23 | if oversized_frame(part) { |
| 24 | break; |
| 25 | } |
| 26 | let _ = dec.decode(part); |
| 27 | } |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | fn oversized_frame(frame: &[u8]) -> bool { |
| 32 | matches!(frame_dims(frame), Some((w, h)) if w > MAX_DIM || h > MAX_DIM) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | fn frame_dims(frame: &[u8]) -> Option<(u32, u32)> { |
| 38 | let mut r = Bits { data: frame, pos: 0 }; |
| 39 | if r.bits(2)? != 2 { |
| 40 | return None; |
| 41 | } |
| 42 | let profile = (r.bits(1)? << 1) | r.bits(1)?; |
| 43 | if profile == 3 { |
| 44 | r.bits(1)?; |
| 45 | } |
| 46 | if r.bits(1)? == 1 { |
| 47 | return None; |
| 48 | } |
| 49 | let key = r.bits(1)? == 0; |
| 50 | let show_frame = r.bits(1)?; |
| 51 | let error_resilient = r.bits(1)?; |
| 52 | let intra_only = if key { |
| 53 | false |
| 54 | } else { |
| 55 | let intra_only = if show_frame == 0 { r.bits(1)? == 1 } else { false }; |
| 56 | if error_resilient == 0 { |
| 57 | r.bits(2)?; |
| 58 | } |
| 59 | intra_only |
| 60 | }; |
| 61 | if !key && !intra_only { |
| 62 | return None; |
| 63 | } |
| 64 | if r.bits(24)? != 0x49_83_42 { |
| 65 | return None; |
| 66 | } |
| 67 | if key || profile > 0 { |
| 68 | color_config(&mut r, profile)?; |
| 69 | } |
| 70 | if !key { |
| 71 | r.bits(8)?; |
| 72 | } |
| 73 | let width = r.bits(16)? + 1; |
| 74 | let height = r.bits(16)? + 1; |
| 75 | Some((width, height)) |
| 76 | } |
| 77 | |
| 78 | fn color_config(r: &mut Bits, profile: u32) -> Option<()> { |
| 79 | if profile >= 2 { |
| 80 | r.bits(1)?; |
| 81 | } |
| 82 | let color_space = r.bits(3)?; |
| 83 | let odd_profile = profile == 1 || profile == 3; |
| 84 | if color_space != 7 { |
| 85 | r.bits(1)?; |
| 86 | if odd_profile { |
| 87 | r.bits(3)?; |
| 88 | } |
| 89 | } else if odd_profile { |
| 90 | r.bits(1)?; |
| 91 | } |
| 92 | Some(()) |
| 93 | } |
| 94 | |
| 95 | struct Bits<'a> { |
| 96 | data: &'a [u8], |
| 97 | pos: usize, |
| 98 | } |
| 99 | |
| 100 | impl Bits<'_> { |
| 101 | fn bits(&mut self, n: u32) -> Option<u32> { |
| 102 | let mut value = 0; |
| 103 | for _ in 0..n { |
| 104 | let byte = *self.data.get(self.pos >> 3)?; |
| 105 | value = (value << 1) | u32::from((byte >> (7 - (self.pos & 7))) & 1); |
| 106 | self.pos += 1; |
| 107 | } |
| 108 | Some(value) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | fn split(data: &[u8]) -> Option<Vec<&[u8]>> { |
| 115 | let (&ctl, body) = data.split_first()?; |
| 116 | let n = body.len(); |
| 117 | if n < 2 { |
| 118 | return None; |
| 119 | } |
| 120 | if ctl & 1 == 1 && n >= 3 { |
| 121 | let a = 1 + ((ctl as usize) >> 1) % (n - 2); |
| 122 | let b = a + 1 + (body[0] as usize) % (n - a - 1); |
| 123 | Some(vec![&body[..a], &body[a..b], &body[b..]]) |
| 124 | } else { |
| 125 | let a = 1 + ((ctl as usize) >> 1) % (n - 1); |
| 126 | Some(vec![&body[..a], &body[a..]]) |
| 127 | } |
| 128 | } |