Files
gentoo-utils-gitea/fuzz/atom/parser/fuzz.rs
2025-12-11 21:55:12 +00:00

66 lines
1.6 KiB
Rust

#![deny(unused_imports)]
#![allow(clippy::missing_safety_doc)]
use core::slice;
use gentoo_utils::{Parseable, atom::Atom};
use std::{
io::{self, Write},
sync::{LazyLock, Mutex},
};
#[derive(Debug)]
struct State {
input: io::Stdin,
output: io::Stdout,
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 {
static PIPES: LazyLock<Mutex<State>> = LazyLock::new(|| {
Mutex::new(State {
input: io::stdin(),
output: io::stdout(),
})
});
let slice = unsafe { slice::from_raw_parts(input, len) };
let str = str::from_utf8(slice).expect("expected ascii input");
if str.chars().any(|c| !c.is_ascii_graphic()) {
return -1;
}
let mut state = PIPES.lock().unwrap();
writeln!(&mut state.output, "{str}").unwrap();
let mut buffer = String::new();
state.input.read_line(&mut buffer).unwrap();
let control = match buffer.as_str().trim() {
"0" => Ok(()),
"1" => Err(()),
other => panic!("unexpected input from pipes: {other}"),
};
let gentoo_utils = Atom::parse(str);
match (control, gentoo_utils) {
(Ok(_), Ok(_)) => {
eprintln!("agreement that {str} is valid");
}
(Err(_), Err(_)) => {
eprintln!("agreement that {str} is invalid");
}
(Ok(_), Err(rest)) => {
panic!("disagreement on {str}\ncontrol:Ok\ngentoo-utils:Err({rest})");
}
(Err(_), Ok(_)) => {
panic!("disagreement on {str}\ncontrol:Err\ngentoo-utils:Ok")
}
}
0
}