//! Fuzzes Reed-Solomon reconstruction with fuzzer-chosen shapes and erasures. //! Asserts invariants, not only "no panic": up to m erasures always rebuild the encoded data byte-exact; more are refused; hostile shard sets never panic. //! How to run: see the comment at the top of `fuzz/Cargo.toml`. #![no_main] use libfuzzer_sys::fuzz_target; use domistream::fec::ReedSolomon; fuzz_target!(|data: &[u8]| { if data.len() < 4 { return; } let k = 1 + usize::from(data[0]) % 32; let m = 1 + usize::from(data[1]) % 16; let len = 1 + usize::from(data[2]) % 32; let Ok(code) = ReedSolomon::new(k, m) else { return }; let body = &data[3..]; let mut shards: Vec> = (0..k).map(|i| (0..len).map(|x| body[(i * len + x) % body.len()]).collect()).collect(); shards.extend((0..m).map(|_| vec![0; len])); code.encode(&mut shards).expect("encode"); let mask = u32::from(body[body.len() - 1]); let lost: Vec = (0..k + m).filter(|&i| (mask.rotate_left(i as u32) ^ (i as u32 * 7)) % 3 == 0).collect(); let mut s: Vec>> = shards.iter().enumerate().map(|(i, v)| (!lost.contains(&i)).then(|| v.clone())).collect(); let r = code.reconstruct(&mut s); if lost.len() <= m { r.expect("<= m erasures rebuild"); for (a, b) in s.iter().zip(&shards) { assert_eq!(a.as_deref(), Some(b.as_slice())); } } else { assert!(matches!(r, Err(domistream::Error::Truncated { .. }))); } // The raw bytes as a hostile shard set. let mut hostile: Vec>> = body.chunks(len).map(|c| (c[0] & 1 == 0).then(|| c.to_vec())).collect(); let _ = code.reconstruct(&mut hostile); });