summaryrefslogtreecommitdiff
path: root/fuzz/fuzz.rs
blob: 72f751beae5e297fdcc8c22414f70794b66264bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use core::slice;
use gentoo_utils::{Parseable, atom::Atom};
use mon::{Parser, ParserFinishedError, input::InputIter};
use std::{
    io::Write,
    process::{Command, Stdio},
};

#[allow(clippy::missing_safety_doc, clippy::needless_return)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 {
    let slice = unsafe { slice::from_raw_parts(input, len) };

    let atom = match str::from_utf8(slice) {
        Ok(str) => str.trim(),
        _ => return -1,
    };

    let mut proc = Command::new("atom.py")
        .stdin(Stdio::piped())
        .spawn()
        .expect("failed to start atom.py");

    proc.stdin
        .as_mut()
        .unwrap()
        .write_all(atom.as_bytes())
        .unwrap();

    let status = proc.wait().unwrap();

    let result = Atom::parser().check_finished(InputIter::new(atom));

    match (status.success(), result) {
        (true, Ok(_)) => {
            eprintln!("agreement that {atom} is valid");
            return 0;
        }
        (true, Err(ParserFinishedError::Err(it) | ParserFinishedError::Unfinished(it))) => {
            panic!("gentoo-utils rejected valid atom: {atom}: {}", it.rest());
        }
        (false, Err(_)) => {
            eprintln!("agreement that {atom} is invalid");
            return -1;
        }
        (false, Ok(_)) => {
            panic!("gentoo-utils accepted invalid atom: {atom}");
        }
    }
}