petitcarnets 0.0.11

Terminal user interfaces in the Elm architecture (model, update, view), with no dependencies.
Documentation
//! The properties every input must keep through the decoder and the
//! scanner, shared by the deterministic fuzz test and the cargo-fuzz
//! target (`fuzz/`): no read past the buffer, no input stuck, every byte
//! drained once the escape timeout has passed.

use super::decode;
use super::scanner::Scanner;

/// A xorshift generator, so a run is the same every time.
pub(crate) struct XorShift(pub(crate) u64);

impl XorShift {
    /// A generator seeded from `bytes` (FNV-1a), so the fuzz target
    /// cuts the same input into the same chunks on every replay.
    pub(crate) fn seeded(bytes: &[u8]) -> Self {
        let hash = bytes.iter().fold(0xcbf2_9ce4_8422_2325_u64, |h, &b| {
            (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)
        });
        // Xorshift never leaves zero.
        Self(hash | 1)
    }

    pub(crate) fn next(&mut self) -> u64 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 7;
        self.0 ^= self.0 << 17;
        self.0
    }

    pub(crate) fn below(&mut self, n: usize) -> usize {
        usize::try_from(self.next() % n as u64).unwrap()
    }
}

/// Decodes `bytes` once whole, then feeds them to a scanner in chunks
/// `rng` picks, and panics if a property breaks.
pub(crate) fn check(rng: &mut XorShift, bytes: &[u8]) {
    if !bytes.is_empty() {
        let (n, _) = decode(bytes);
        assert!(n >= 1 && n <= bytes.len(), "{bytes:02x?}");
    }
    let mut scanner = Scanner::default();
    let pending = feed(rng, &mut scanner, bytes);
    drain(&mut scanner, pending, bytes);
}

/// Feeds `bytes` in chunks, the escape timeout passing at random, and
/// returns what the scanner holds back: every scan uses no more than it
/// has, and an expired scan makes progress.
fn feed(rng: &mut XorShift, scanner: &mut Scanner, bytes: &[u8]) -> Vec<u8> {
    let (mut pending, mut events) = (Vec::new(), Vec::new());
    let mut rest = bytes;
    while !rest.is_empty() {
        let (chunk, tail) = rest.split_at(1 + rng.below(rest.len()));
        rest = tail;
        pending.extend_from_slice(chunk);
        let expired = rng.below(4) == 0;
        let used = scanner.scan(&pending, expired, &mut events);
        assert!(used <= pending.len(), "{bytes:02x?}");
        assert!(
            !expired || used > 0 || pending.is_empty(),
            "stuck on {bytes:02x?}"
        );
        let _ = pending.drain(..used);
    }
    pending
}

/// Flushes what is held back with the timeout passed: each scan makes
/// progress, and everything drains.
fn drain(scanner: &mut Scanner, mut pending: Vec<u8>, bytes: &[u8]) {
    let mut events = Vec::new();
    for _ in 0..=bytes.len() {
        if pending.is_empty() {
            return;
        }
        let used = scanner.scan(&pending, true, &mut events);
        assert!(used > 0 && used <= pending.len(), "stuck on {bytes:02x?}");
        let _ = pending.drain(..used);
    }
    assert!(pending.is_empty(), "never drained {bytes:02x?}");
}

/// One fuzz input: the chunking comes from the input itself.
pub(crate) fn one(bytes: &[u8]) {
    check(&mut XorShift::seeded(bytes), bytes);
}