hookup atom parser fuzzer to read and write to stdin/stdout

This commit is contained in:
2025-12-08 01:38:48 +00:00
parent 35b81e40cc
commit f279f75a19
2 changed files with 37 additions and 74 deletions

View File

@@ -1,100 +1,64 @@
#![allow(clippy::missing_safety_doc)]
use core::slice; use core::slice;
use gentoo_utils::{Parseable, atom::Atom}; use gentoo_utils::{Parseable, atom::Atom};
use mon::{Parser, ParserFinishedError, input::InputIter};
use std::{ use std::{
io::{BufRead, BufReader, Write}, io::{self, Write},
process::{ChildStdin, ChildStdout, Command, Stdio},
sync::{LazyLock, Mutex}, sync::{LazyLock, Mutex},
}; };
struct PyProcess { #[derive(Debug)]
stdin: Mutex<ChildStdin>, struct State {
stdout: Mutex<BufReader<ChildStdout>>, input: io::Stdin,
buffer: Mutex<String>, output: io::Stdout,
} }
#[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 {
static PY_PROCESS: LazyLock<PyProcess> = LazyLock::new(|| { static PIPES: LazyLock<Mutex<State>> = LazyLock::new(|| {
#[allow(clippy::zombie_processes)] Mutex::new(State {
let mut proc = Command::new("atom.py") input: io::stdin(),
.stdin(Stdio::piped()) output: io::stdout(),
.stdout(Stdio::piped()) })
.stderr(Stdio::inherit())
.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 slice = unsafe { slice::from_raw_parts(input, len) };
let str = str::from_utf8(slice).expect("expected ascii input");
if slice.iter().any(|b| !b.is_ascii_graphic()) { if str.chars().any(|c| !c.is_ascii_graphic()) {
return -1; return -1;
} }
let str = match str::from_utf8(slice) { let mut state = PIPES.lock().unwrap();
Ok(str) => str,
Err(_) => return -1, 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 atom = str.trim(); let gentoo_utils = Atom::parse(str);
let mut stdin = PY_PROCESS.stdin.lock().expect("failed to get stdin lock"); match (control, gentoo_utils) {
(Ok(_), Ok(_)) => {
writeln!(&mut stdin, "{atom}").expect("failed to write to python stdin"); eprintln!("agreement that {str} is valid");
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().parse_finished(InputIter::new(atom));
match (portage_result, gentoo_utils_result) {
(true, Ok(_)) => {
eprintln!("agreement that {atom} is valid");
} }
(false, Err(_)) => { (Err(_), Err(_)) => {
eprintln!("agreement that {atom} is invalid"); eprintln!("agreement that {str} is invalid");
} }
(true, Err(ParserFinishedError::Err(it) | ParserFinishedError::Unfinished(it))) => { (Ok(_), Err(rest)) => {
panic!("rejected valid atom: {atom}: {}", it.rest()); panic!("disagreement on {str}\ncontrol:Ok\ngentoo-utils:Err({rest})");
} }
(false, Ok(atom)) (Err(_), Ok(_)) => {
if atom.usedeps().iter().any(|usedep| { panic!("disagreement on {str}\ncontrol:Err\ngentoo-utils:Ok")
atom.usedeps()
.iter()
.filter(|u| usedep.flag() == u.flag())
.count()
> 1
}) =>
{
eprintln!("disagreement due to duplicates in usedeps");
}
(false, Ok(_)) => {
panic!("accpeted invalid atom: {atom}")
} }
} }
return 0; 0
} }

View File

@@ -20,7 +20,6 @@ fuzz_rs = static_library(
'-Cllvm-args=-sanitizer-coverage-level=3', '-Cllvm-args=-sanitizer-coverage-level=3',
'-Cllvm-args=-sanitizer-coverage-inline-8bit-counters', '-Cllvm-args=-sanitizer-coverage-inline-8bit-counters',
], ],
dependencies: [mon],
link_with: [gentoo_utils], link_with: [gentoo_utils],
) )