forked from gentoo-utils/gentoo-utils
add fuzzer
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
project(
|
||||
'gentoo-utils',
|
||||
'rust',
|
||||
['rust', 'cpp'],
|
||||
meson_version: '>=1.9.1',
|
||||
default_options: {'rust_std': '2024', 'rust_nightly': 'enabled'},
|
||||
)
|
||||
@@ -16,5 +16,7 @@ gentoo_utils = static_library(
|
||||
'gentoo_utils',
|
||||
'src/lib.rs',
|
||||
dependencies: [mon, get, itertools],
|
||||
link_with: [thiserror]
|
||||
link_with: [thiserror],
|
||||
)
|
||||
|
||||
subdir('tests')
|
||||
|
||||
1
meson.options
Normal file
1
meson.options
Normal file
@@ -0,0 +1 @@
|
||||
option('fuzz', type: 'feature', value: 'disabled')
|
||||
13
scripts/atom.py
Executable file
13
scripts/atom.py
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import portage.dep as dep
|
||||
|
||||
input = sys.stdin.read().strip()
|
||||
|
||||
try:
|
||||
dep.Atom(input)
|
||||
except Exception as e:
|
||||
sys.exit(1)
|
||||
|
||||
sys.exit(0)
|
||||
50
tests/fuzz.rs
Normal file
50
tests/fuzz.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use core::slice;
|
||||
use gentoo_utils::{Parseable, atom::Atom};
|
||||
use mon::{Parser, ParserFinishedError, input::InputIter};
|
||||
use std::{
|
||||
io::Write,
|
||||
process::{Command, Stdio},
|
||||
};
|
||||
|
||||
#[allow(clippy::missing_safety_doc, clippy::needless_return)]
|
||||
#[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 atom = match str::from_utf8(slice) {
|
||||
Ok(str) => str.trim(),
|
||||
_ => return -1,
|
||||
};
|
||||
|
||||
let mut proc = Command::new("atom.py")
|
||||
.stdin(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
proc.stdin
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.write_all(atom.as_bytes())
|
||||
.unwrap();
|
||||
|
||||
let status = proc.wait().unwrap();
|
||||
|
||||
let result = Atom::parser().check_finished(InputIter::new(atom));
|
||||
|
||||
match (status.success(), result) {
|
||||
(true, Ok(_)) => {
|
||||
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(_)) => {
|
||||
eprintln!("agreement that {atom} is invalid");
|
||||
return -1;
|
||||
}
|
||||
(false, Ok(_)) => {
|
||||
panic!("gentoo-utils accepted invalid atom: {atom}");
|
||||
}
|
||||
}
|
||||
}
|
||||
27
tests/meson.build
Normal file
27
tests/meson.build
Normal file
@@ -0,0 +1,27 @@
|
||||
cbindgen = find_program('cbindgen')
|
||||
|
||||
fuzz_h = custom_target(
|
||||
'fuzz_h',
|
||||
input: 'fuzz.rs',
|
||||
output: 'fuzz.h',
|
||||
command: [cbindgen, '@INPUT@', '-o', '@OUTPUT'],
|
||||
)
|
||||
|
||||
fuzz_rs = static_library(
|
||||
'fuzz_rs',
|
||||
'fuzz.rs',
|
||||
rust_abi: 'c',
|
||||
rust_args: [
|
||||
'-Cpasses=sancov-module',
|
||||
'-Cllvm-args=-sanitizer-coverage-level=3',
|
||||
'-Cllvm-args=-sanitizer-coverage-inline-8bit-counters',
|
||||
],
|
||||
dependencies: [mon],
|
||||
link_with: [gentoo_utils],
|
||||
)
|
||||
|
||||
fuzz_cpp = executable(
|
||||
'fuzz',
|
||||
link_args: ['-fsanitize=fuzzer'],
|
||||
link_with: [fuzz_rs],
|
||||
)
|
||||
Reference in New Issue
Block a user