#![deny(unused_imports)] #![allow(clippy::missing_safety_doc)] use core::slice; use gentoo_utils::{Parseable, atom::Atom}; use std::{ env::{self}, io::{self, Write}, }; #[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 str = str::from_utf8(slice).expect("expected ascii input"); if str.chars().any(|c| !c.is_ascii_graphic()) { return -1; } let stdin = io::stdin(); let mut stdout = io::stdout(); let mut buffer = String::new(); writeln!(&mut stdout, "{str}").unwrap(); stdin.read_line(&mut buffer).unwrap(); let control = match buffer.as_str().trim() { "0" => Ok(()), "1" => Err(()), other => panic!("unexpected input from pipes: {other}"), }; let gentoo_utils = Atom::parse(str); match (control, gentoo_utils) { (Ok(_), Ok(_)) => { if env::var("FUZZER_LOG").is_ok() { eprintln!("agreement that {str} is valid"); } } (Err(_), Err(_)) => { if env::var("FUZZER_LOG").is_ok() { eprintln!("agreement that {str} is invalid"); } } (Ok(_), Err(rest)) => { panic!("disagreement on {str}\ncontrol:Ok\ngentoo-utils:Err({rest})"); } (Err(_), Ok(atom)) if atom.usedeps().iter().any(|usedep| { atom.usedeps() .iter() .filter(|u| *usedep.flag() == *u.flag()) .count() > 1 }) => { if env::var("FUZZER_LOG").is_ok() { eprintln!( "disagreement, but we will allow it since its probably because of duplicated usdeps" ); } } (Err(_), Ok(_)) => { panic!("disagreement on {str}\ncontrol:Err\ngentoo-utils:Ok") } } 0 }