Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company đŸ‘‡

4.8 KB
Raw Blame
// ©AngelaMos | 2026
// reproduce_capture.rs

use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};

use lisdex_core::scratch;
use lisdex_fuzz::runner::{self, MAX_REPORT_BYTES};

const CHATTY_SOURCE: &str = r#"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
    if (argc < 2) { return 1; }
    FILE *f = fopen(argv[1], "rb");
    if (!f) { return 1; }
    char buf[64];
    memset(buf, 0, sizeof buf);
    if (!fgets(buf, sizeof buf, f)) { fclose(f); return 1; }
    fclose(f);
    long n = atol(buf);
    int raw = (strchr(buf, 'r') != NULL);
    for (long i = 0; i < n; i++) {
        fputc(raw ? 0xC3 : ('A' + (int)(i % 26)), stderr);
    }
    fflush(stderr);
    abort();
}
"#;

const REPRODUCE_TIMEOUT: Duration = Duration::from_secs(10);
const SMALL_REPORT_BYTES: usize = 512;

struct Lab {
    root: PathBuf,
    target: PathBuf,
}

impl Lab {
    fn new(label: &str) -> Self {
        let root = scratch::path(label);
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).unwrap();

        let source = root.join("chatty.c");
        std::fs::write(&source, CHATTY_SOURCE).unwrap();

        let target = root.join("chatty");
        let status = Command::new("cc")
            .arg("-O0")
            .arg("-o")
            .arg(&target)
            .arg(&source)
            .status()
            .unwrap();
        assert!(status.success(), "cc failed to build the chatty target");

        Self { root, target }
    }

    fn run(&self, name: &str, bytes: usize, raw: bool) -> (runner::Reproduction, Duration) {
        let input = self.root.join(name);
        let marker = if raw { "r" } else { "" };
        std::fs::write(&input, format!("{bytes}{marker}\n")).unwrap();
        let started = Instant::now();
        let reproduction = runner::reproduce(&self.target, &input, REPRODUCE_TIMEOUT).unwrap();
        (reproduction, started.elapsed())
    }
}

const LARGEST_LIKELY_PIPE_BYTES: usize = 64 * 1024;

#[test]
fn a_report_larger_than_the_stderr_pipe_is_captured_whole_and_is_not_a_hang() {
    let lab = Lab::new("reproduce-capture-overflow");
    let bytes = 200_000;
    assert!(
        bytes > LARGEST_LIKELY_PIPE_BYTES && bytes < MAX_REPORT_BYTES,
        "the probe must clear the pipe and stay under the cap"
    );

    let (reproduction, elapsed) = lab.run("big", bytes, false);

    assert!(
        !reproduction.timed_out,
        "a target that emitted {bytes} bytes and exited was reported as a hang: stderr is not \
         drained while the child runs, so the child blocked on a full pipe"
    );
    assert_eq!(
        reproduction.output.len(),
        bytes,
        "captured {} of {bytes} emitted bytes",
        reproduction.output.len()
    );
    assert!(
        !reproduction.truncated,
        "a report under the cap must not be flagged truncated"
    );
    assert!(
        elapsed < REPRODUCE_TIMEOUT,
        "reproduction burned the whole timeout: {elapsed:?}"
    );
}

#[test]
fn a_report_over_the_cap_is_flagged_truncated_and_still_lets_the_target_exit() {
    let lab = Lab::new("reproduce-capture-capped");
    let bytes = MAX_REPORT_BYTES + 100_000;

    let (reproduction, elapsed) = lab.run("huge", bytes, false);

    assert!(
        reproduction.truncated,
        "a {bytes}-byte report exceeds the {MAX_REPORT_BYTES}-byte cap and must say so"
    );
    assert!(
        !reproduction.timed_out,
        "an over-cap report is not a hang: without a concurrent drain the child blocks on a full \
         pipe long before it reaches the cap"
    );
    assert_eq!(
        reproduction.output.len(),
        MAX_REPORT_BYTES,
        "an over-cap report must be stored at exactly the cap"
    );
    assert!(
        elapsed < REPRODUCE_TIMEOUT,
        "reproduction burned the whole timeout: {elapsed:?}"
    );
}

#[test]
fn a_small_clean_report_is_captured_verbatim() {
    let lab = Lab::new("reproduce-capture-small");

    let (reproduction, _) = lab.run("small", SMALL_REPORT_BYTES, false);

    assert!(!reproduction.timed_out);
    assert!(!reproduction.truncated);
    assert!(!reproduction.lossy);
    assert_eq!(reproduction.output.len(), SMALL_REPORT_BYTES);
}

#[test]
fn a_report_that_is_not_utf8_says_so_instead_of_substituting_in_silence() {
    let lab = Lab::new("reproduce-capture-lossy");

    let (reproduction, _) = lab.run("raw", SMALL_REPORT_BYTES, true);

    assert!(
        reproduction.lossy,
        "the target emitted {SMALL_REPORT_BYTES} bytes that are not UTF-8 and every one was \
         replaced, so the stored report is not what the target wrote"
    );
    assert!(
        !reproduction.timed_out,
        "a small report must not hang regardless of its bytes"
    );
    assert!(
        !reproduction.truncated,
        "the lossy flag must not be conflated with the truncation flag"
    );
}