communicate with python over a pipe to increase fuzzing performance

This commit is contained in:
John Turner
2025-11-17 20:02:16 +00:00
parent 0cc3ac8e84
commit b74471706b
2 changed files with 69 additions and 35 deletions

View File

@@ -2,49 +2,83 @@ use core::slice;
use gentoo_utils::{Parseable, atom::Atom}; use gentoo_utils::{Parseable, atom::Atom};
use mon::{Parser, ParserFinishedError, input::InputIter}; use mon::{Parser, ParserFinishedError, input::InputIter};
use std::{ use std::{
io::Write, io::{BufRead, BufReader, Write},
process::{Command, Stdio}, 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)] #[allow(clippy::missing_safety_doc, clippy::needless_return)]
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 { pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 {
let slice = unsafe { slice::from_raw_parts(input, len) }; static PY_PROCESS: LazyLock<PyProcess> = LazyLock::new(|| {
#[allow(clippy::zombie_processes)]
let atom = match str::from_utf8(slice) {
Ok(str) => str.trim(),
_ => return -1,
};
let mut proc = Command::new("atom.py") let mut proc = Command::new("atom.py")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn() .spawn()
.expect("failed to start atom.py"); .expect("failed to spawn atom.py");
proc.stdin let stdin = Mutex::new(proc.stdin.take().unwrap());
.as_mut() let stdout = Mutex::new(BufReader::new(proc.stdout.take().unwrap()));
.unwrap()
.write_all(atom.as_bytes())
.unwrap();
let status = proc.wait().unwrap(); PyProcess {
stdin,
stdout,
buffer: Mutex::new(String::new()),
}
});
let result = Atom::parser().check_finished(InputIter::new(atom)); let slice = unsafe { slice::from_raw_parts(input, len) };
match (status.success(), result) { let str = match str::from_utf8(slice) {
(true, Ok(_)) => { Ok(str) => str,
Err(_) => return -1,
};
let atom = str.trim();
let mut stdin = PY_PROCESS.stdin.lock().expect("failed to get stdin lock");
writeln!(&mut stdin, "{atom}").expect("failed to write to python stdin");
let mut stdout = PY_PROCESS.stdout.lock().expect("failed to get stdout lock");
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"); eprintln!("agreement that {atom} is valid");
}
(false, false) => {
eprintln!("agreement that {atom} is invalid");
}
(true, false) => {
panic!("rejected valid atom: {atom}");
}
(false, true) => {
panic!("accpeted invalid atom: {atom}")
}
}
return 0; 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}");
}
}
}

View File

@@ -1,13 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import portage.dep as dep from portage.dep import Atom
input = sys.stdin.read().strip()
for line in sys.stdin.buffer:
try: try:
dep.Atom(input) Atom(line.decode().strip())
except Exception as e: sys.stdout.buffer.write(b"0\n")
sys.exit(1) except:
sys.stdout.buffer.write(b"1\n")
sys.exit(0) finally:
sys.stdout.buffer.flush()