From b0f68fa7e0e5e60507aad249d67d643d01132d91 Mon Sep 17 00:00:00 2001 From: John Turner Date: Mon, 15 Dec 2025 00:33:35 +0000 Subject: [PATCH 1/4] remove static variables from atom parser fuzzer Stdin and Stdout are already synchronized and available to multiple threads as needed, we don't need to hold onto instances in a static variable. --- fuzz/atom/parser/fuzz.rs | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/fuzz/atom/parser/fuzz.rs b/fuzz/atom/parser/fuzz.rs index 83d5787..b3539d6 100644 --- a/fuzz/atom/parser/fuzz.rs +++ b/fuzz/atom/parser/fuzz.rs @@ -3,26 +3,10 @@ use core::slice; use gentoo_utils::{Parseable, atom::Atom}; -use std::{ - io::{self, Write}, - sync::{LazyLock, Mutex}, -}; - -#[derive(Debug)] -struct State { - input: io::Stdin, - output: io::Stdout, -} +use std::io::{self, Write}; #[unsafe(no_mangle)] pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 { - static PIPES: LazyLock> = LazyLock::new(|| { - Mutex::new(State { - input: io::stdin(), - output: io::stdout(), - }) - }); - let slice = unsafe { slice::from_raw_parts(input, len) }; let str = str::from_utf8(slice).expect("expected ascii input"); @@ -30,13 +14,13 @@ pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> return -1; } - let mut state = PIPES.lock().unwrap(); - - writeln!(&mut state.output, "{str}").unwrap(); - + let stdin = io::stdin(); + let mut stdout = io::stdout(); let mut buffer = String::new(); - state.input.read_line(&mut buffer).unwrap(); + writeln!(&mut stdout, "{str}").unwrap(); + + stdin.read_line(&mut buffer).unwrap(); let control = match buffer.as_str().trim() { "0" => Ok(()), From a3ff953e5084d83a6bede0582465c37adb35c2ea Mon Sep 17 00:00:00 2001 From: John Turner Date: Mon, 15 Dec 2025 00:36:12 +0000 Subject: [PATCH 2/4] except InvalidAtom in atom.py --- scripts/atom.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/atom.py b/scripts/atom.py index 2e8ca7f..a93251a 100755 --- a/scripts/atom.py +++ b/scripts/atom.py @@ -2,12 +2,13 @@ import sys from portage.dep import Atom +from portage.exception import InvalidAtom for line in sys.stdin.buffer: try: Atom(line.decode().strip()) sys.stdout.buffer.write(b"0\n") - except: + except InvalidAtom: sys.stdout.buffer.write(b"1\n") finally: sys.stdout.buffer.flush() From 5178a7b8eaf1f5ed6259ce95b7badf88518d9cf0 Mon Sep 17 00:00:00 2001 From: John Turner Date: Mon, 15 Dec 2025 00:37:47 +0000 Subject: [PATCH 3/4] check for duplicate flags in usedeps, instead of duplicate usedeps --- fuzz/atom/parser/fuzz.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fuzz/atom/parser/fuzz.rs b/fuzz/atom/parser/fuzz.rs index b3539d6..77988c7 100644 --- a/fuzz/atom/parser/fuzz.rs +++ b/fuzz/atom/parser/fuzz.rs @@ -41,10 +41,13 @@ pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> 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 == *u).count() > 1) => + if atom.usedeps().iter().any(|usedep| { + atom.usedeps() + .iter() + .filter(|u| *usedep.flag() == *u.flag()) + .count() + > 1 + }) => { eprintln!( "disagreement, but we will allow it since its probably because of duplicated usdeps" From 7348681b6559c6d623fd47fbc656f924e9c7bcdc Mon Sep 17 00:00:00 2001 From: John Turner Date: Mon, 15 Dec 2025 01:09:16 +0000 Subject: [PATCH 4/4] gate atom parser fuzzer debug printing --- fuzz/atom/parser/fuzz.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/fuzz/atom/parser/fuzz.rs b/fuzz/atom/parser/fuzz.rs index 77988c7..b727c24 100644 --- a/fuzz/atom/parser/fuzz.rs +++ b/fuzz/atom/parser/fuzz.rs @@ -3,7 +3,10 @@ use core::slice; use gentoo_utils::{Parseable, atom::Atom}; -use std::io::{self, Write}; +use std::{ + env::{self}, + io::{self, Write}, +}; #[unsafe(no_mangle)] pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> i32 { @@ -32,10 +35,14 @@ pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> match (control, gentoo_utils) { (Ok(_), Ok(_)) => { - eprintln!("agreement that {str} is valid"); + if env::var("FUZZER_LOG").is_ok() { + eprintln!("agreement that {str} is valid"); + } } (Err(_), Err(_)) => { - eprintln!("agreement that {str} is invalid"); + 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})"); @@ -49,9 +56,11 @@ pub unsafe extern "C" fn LLVMFuzzerTestOneInput(input: *const u8, len: usize) -> > 1 }) => { - eprintln!( - "disagreement, but we will allow it since its probably because of duplicated usdeps" - ); + 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")