forked from gentoo-utils/gentoo-utils
51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
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}");
|
|
}
|
|
}
|
|
}
|