move fuzz stuff into its own directory

This commit is contained in:
John Turner
2025-11-16 20:31:19 +00:00
parent 7c404ca8c5
commit 424bd9d072
5 changed files with 36 additions and 29 deletions

50
fuzz/fuzz.rs Normal file
View File

@@ -0,0 +1,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()
.unwrap();
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}");
}
}
}