//! Fuzzes the host's video packetizer against the client's depacketizer (`docs/PROTOCOL.md` 5.2, 5.3, 7). //! Arbitrary frame bytes and parameters (packet size, FEC percentage, minimum parity, encryption, frame type, //! latency) → packetize two frames → lose up to `m` packets of every block → depacketize: both frames must come back //! byte-exact, in order, with their headers. Every valid configuration must be accepted and every frame within //! `max_bitstream_len` packetized. //! How to run: see the comment at the top of `fuzz/Cargo.toml`. #![no_main] use libfuzzer_sys::fuzz_target; use core::time::Duration; use std::collections::BTreeMap; use domistream::Instant; use domistream::fec::parity_count; use domistream::video::packet::parse_video_packet; use domistream::video::packetizer::{EncodedFrame, VideoPacketizer, VideoPacketizerConfig}; use domistream::video::{FrameType, VideoCipher, VideoConfig, VideoDepacketizer, VideoEvent}; const KEY: [u8; 16] = *b"fuzz-video-key!!"; /// xorshift64*, seeded from the input: which packets are lost. struct Rng(u64); impl Rng { fn below(&mut self, n: usize) -> usize { self.0 ^= self.0 >> 12; self.0 ^= self.0 << 25; self.0 ^= self.0 >> 27; (self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 33) as usize % n.max(1) } } fuzz_target!(|data: &[u8]| { let Some((head, body)) = data.split_first_chunk::<10>() else { return }; let [s0, s1, pct, min, flags, loss, split, l0, l1, first] = *head; // 17..=4048: every packet size the receiver accepts, sealed or not. let packet_size = 17 + u16::from_le_bytes([s0, s1]) % 4032; let encrypt = flags & 1 != 0; let frame_type = match (flags >> 1) & 3 { 0 => FrameType::Predicted, 1 => FrameType::Key, 2 => FrameType::Recovery, _ => FrameType::Other((flags >> 3) | 0x08), // 8..=31: never a value with its own name (1, 2, 5) }; let lossy = flags & 0x80 != 0; let cipher = || encrypt.then(|| VideoCipher::new(KEY)); let config = VideoPacketizerConfig { packet_size, fec_percentage: pct, min_required_fec_packets: min, cipher: cipher(), first_sequence: u16::from_le_bytes([first, s0]), }; let mut tx = VideoPacketizer::new(config).expect("a valid configuration is accepted"); let mut rx = VideoDepacketizer::new(VideoConfig { packet_size, cipher: cipher(), frame_timeout: Duration::from_millis(20) }) .expect("the receiver accepts the same packet size"); let cut = body.len() * usize::from(split) / 255; let (a, b) = body.split_at(cut); let frames = [a, b]; let mut rng = Rng(u64::from_le_bytes([s0, s1, pct, min, flags, loss, split, l0]) | 1); let mut datagrams = Vec::new(); for (i, bitstream) in frames.iter().enumerate() { assert!(bitstream.len() <= tx.max_bitstream_len(), "fuzz inputs stay far below the frame cap"); let frame = EncodedFrame { frame_index: 0xFFFF_FFFF_u32.wrapping_add(i as u32), frame_type, rtp_timestamp: u32::from(l1) << 8, processing_latency: u16::from_le_bytes([l0, l1]), bitstream, }; let out = tx.packetize(&frame).expect("a frame within the cap is packetized"); // Group by block (from the plaintext), then lose up to m of each block. let mut blocks: BTreeMap, usize)> = BTreeMap::new(); for (j, d) in out.iter().enumerate() { let plain = if encrypt { VideoCipher::new(KEY).decrypt(d).expect("our own seal opens") } else { d.clone() }; let p = parse_video_packet(&plain).expect("our own packet parses"); let m = parity_count(usize::from(p.nv.fec.data_shards), p.nv.fec.fec_percentage).expect("consistent FEC"); blocks.entry(p.nv.fec.block_index).or_insert_with(|| (Vec::new(), m)).0.push(j); } let mut keep = vec![true; out.len()]; if lossy { for (positions, m) in blocks.values() { let mut left = positions.clone(); for _ in 0..rng.below(m + 1).min(left.len()) { let pick = left.swap_remove(rng.below(left.len())); if let Some(k) = keep.get_mut(pick) { *k = false; } } } } // Sent in order; every block keeps at least k packets, so frame 0 starts the receiver. datagrams.extend(out.into_iter().zip(keep).filter(|(_, k)| *k).map(|(d, _)| d)); } let mut us = 1_000u64; let mut got = Vec::new(); for d in &datagrams { rx.push(Instant::from_micros(us), d).expect("within the receiver's cap"); us += 10; while let Some(e) = rx.poll_event() { got.push(e); } } for _ in 0..8 { us += 1_000_000; let _ = rx.handle_timeout(Instant::from_micros(us)); while let Some(e) = rx.poll_event() { got.push(e); } } assert_eq!(got.len(), 2, "two frames out: {got:?}"); for (i, (e, want)) in got.iter().zip(frames).enumerate() { let VideoEvent::Frame(f) = e else { panic!("frame {i} lost: {e:?}") }; assert_eq!(f.frame_index, 0xFFFF_FFFF_u32.wrapping_add(i as u32)); assert!(f.bitstream == want, "frame {i}: {} bytes out, {} in", f.bitstream.len(), want.len()); assert_eq!(f.header.frame_type, frame_type); assert_eq!(f.header.processing_latency, u16::from_le_bytes([l0, l1])); assert_eq!(f.rtp_timestamp, u32::from(l1) << 8); } });