forked from gentoo-utils/gentoo-utils
communicate with python over a pipe to increase fuzzing performance
This commit is contained in:
86
fuzz/fuzz.rs
86
fuzz/fuzz.rs
@@ -2,49 +2,83 @@ use core::slice;
|
||||
use gentoo_utils::{Parseable, atom::Atom};
|
||||
use mon::{Parser, ParserFinishedError, input::InputIter};
|
||||
use std::{
|
||||
io::Write,
|
||||
process::{Command, Stdio},
|
||||
io::{BufRead, BufReader, Write},
|
||||
process::{ChildStdin, ChildStdout, Command, Stdio},
|
||||
sync::{LazyLock, Mutex},
|
||||
};
|
||||
|
||||
struct PyProcess {
|
||||
stdin: Mutex<ChildStdin>,
|
||||
stdout: Mutex<BufReader<ChildStdout>>,
|
||||
buffer: Mutex<String>,
|
||||
}
|
||||
|
||||
#[allow(clippy::missing_safety_doc, clippy::needless_return)]
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 {
|
||||
static PY_PROCESS: LazyLock<PyProcess> = LazyLock::new(|| {
|
||||
#[allow(clippy::zombie_processes)]
|
||||
let mut proc = Command::new("atom.py")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to spawn atom.py");
|
||||
|
||||
let stdin = Mutex::new(proc.stdin.take().unwrap());
|
||||
let stdout = Mutex::new(BufReader::new(proc.stdout.take().unwrap()));
|
||||
|
||||
PyProcess {
|
||||
stdin,
|
||||
stdout,
|
||||
buffer: Mutex::new(String::new()),
|
||||
}
|
||||
});
|
||||
|
||||
let slice = unsafe { slice::from_raw_parts(input, len) };
|
||||
|
||||
let atom = match str::from_utf8(slice) {
|
||||
Ok(str) => str.trim(),
|
||||
_ => return -1,
|
||||
let str = match str::from_utf8(slice) {
|
||||
Ok(str) => str,
|
||||
Err(_) => return -1,
|
||||
};
|
||||
|
||||
let mut proc = Command::new("atom.py")
|
||||
.stdin(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to start atom.py");
|
||||
let atom = str.trim();
|
||||
|
||||
proc.stdin
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.write_all(atom.as_bytes())
|
||||
.unwrap();
|
||||
let mut stdin = PY_PROCESS.stdin.lock().expect("failed to get stdin lock");
|
||||
|
||||
let status = proc.wait().unwrap();
|
||||
writeln!(&mut stdin, "{atom}").expect("failed to write to python stdin");
|
||||
|
||||
let result = Atom::parser().check_finished(InputIter::new(atom));
|
||||
let mut stdout = PY_PROCESS.stdout.lock().expect("failed to get stdout lock");
|
||||
|
||||
match (status.success(), result) {
|
||||
(true, Ok(_)) => {
|
||||
let mut buffer = PY_PROCESS.buffer.lock().expect("failed to get buffer lock");
|
||||
|
||||
buffer.clear();
|
||||
|
||||
stdout
|
||||
.read_line(&mut buffer)
|
||||
.expect("failed to readline from python");
|
||||
|
||||
let portage_result = match buffer.as_str().trim() {
|
||||
"0" => true,
|
||||
"1" => false,
|
||||
result => panic!("got unexpected result from python: {result}"),
|
||||
};
|
||||
|
||||
let gentoo_utils_result = Atom::parser().check_finished(InputIter::new(atom)).is_ok();
|
||||
|
||||
match (portage_result, gentoo_utils_result) {
|
||||
(true, true) => {
|
||||
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(_)) => {
|
||||
(false, false) => {
|
||||
eprintln!("agreement that {atom} is invalid");
|
||||
return -1;
|
||||
}
|
||||
(false, Ok(_)) => {
|
||||
panic!("gentoo-utils accepted invalid atom: {atom}");
|
||||
(true, false) => {
|
||||
panic!("rejected valid atom: {atom}");
|
||||
}
|
||||
(false, true) => {
|
||||
panic!("accpeted invalid atom: {atom}")
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user