Skip to content

Commit

Permalink
feat(fuzz-testing): fixed so that we don't have to define udf at the …
Browse files Browse the repository at this point in the history
…top of each property-fuzz files.
  • Loading branch information
Chae authored and Chae committed Oct 18, 2024
1 parent 15a6c31 commit f30dd9f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 39 deletions.
1 change: 1 addition & 0 deletions lattices/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.0.0"
publish = false
edition = "2021"


[package.metadata]
cargo-fuzz = true

Expand Down
12 changes: 5 additions & 7 deletions lattices/fuzz/fuzz_targets/associativity_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
extern crate libfuzzer_sys;
use libfuzzer_sys::fuzz_target;
use lattices::algebra::associativity_single;
use once_cell::sync::Lazy;
use lattices_fuzz::algebra_functions::FuzzFunctions;


#[macro_use]
extern crate lattices_fuzz;

type InputType = u8;
static FUNCTIONS: Lazy<FuzzFunctions<InputType>> = Lazy::new(|| FuzzFunctions::new(
|a: u8, b: u8| a ^ b,
Some(|a: u8| a),
Some(|a: u8, b: u8| a.wrapping_mul(b)),
));

create_fuzz_functions!(InputType, FUNCTIONS);
fuzz_target!(|data: &[u8]| {
if data.len() < 3 {
println!("Not enough data for associativity test.");
Expand Down
13 changes: 6 additions & 7 deletions lattices/fuzz/fuzz_targets/commutativity_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

extern crate libfuzzer_sys;
use lattices::algebra::commutativity_single;
use lattices_fuzz::algebra_functions::FuzzFunctions;
use libfuzzer_sys::fuzz_target;
use once_cell::sync::Lazy;


#[macro_use]
extern crate lattices_fuzz;

type InputType = u8;
static FUNCTIONS: Lazy<FuzzFunctions<InputType>> = Lazy::new(|| FuzzFunctions::new(
|a: u8, b: u8| a ^ b,
Some(|a: u8| a),
Some(|a: u8, b: u8| a.wrapping_mul(b)),
));

create_fuzz_functions!(InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
if data.len() < 2 {
Expand Down
13 changes: 6 additions & 7 deletions lattices/fuzz/fuzz_targets/distributivity_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

extern crate libfuzzer_sys;
use lattices::algebra::distributive_single;
use lattices_fuzz::algebra_functions::FuzzFunctions;
use libfuzzer_sys::fuzz_target;
use once_cell::sync::Lazy;


#[macro_use]
extern crate lattices_fuzz;

type InputType = u8;
static FUNCTIONS: Lazy<FuzzFunctions<InputType>> = Lazy::new(|| FuzzFunctions::new(
|a: u8, b: u8| a ^ b,
Some(|a: u8| a),
Some(|a: u8, b: u8| a.wrapping_mul(b)),
));

create_fuzz_functions!(InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
if data.len() < 3 {
Expand Down
14 changes: 6 additions & 8 deletions lattices/fuzz/fuzz_targets/linearity_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

extern crate libfuzzer_sys;
use lattices::algebra::linearity_single;
use lattices_fuzz::algebra_functions::FuzzFunctions;
use libfuzzer_sys::fuzz_target;
use once_cell::sync::Lazy;


#[macro_use]
extern crate lattices_fuzz;

type InputType = u8;
static FUNCTIONS: Lazy<FuzzFunctions<InputType>> = Lazy::new(|| FuzzFunctions::new(
|a: u8, b: u8| a ^ b,
Some(|a: u8| a),
Some(|a: u8, b: u8| a.wrapping_mul(b)),
));

create_fuzz_functions!(InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
if data.len() < 2 {
Expand All @@ -29,4 +28,3 @@ fuzz_target!(|data: &[u8]| {
println!("Skipping linearity test because g or q is not available.");
}
});

13 changes: 6 additions & 7 deletions lattices/fuzz/fuzz_targets/monotonicity_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

extern crate libfuzzer_sys;
use lattices::algebra::is_monotonic_single;
use lattices_fuzz::algebra_functions::FuzzFunctions;
use libfuzzer_sys::fuzz_target;
use once_cell::sync::Lazy;


#[macro_use]
extern crate lattices_fuzz;

type InputType = u8;
static FUNCTIONS: Lazy<FuzzFunctions<InputType>> = Lazy::new(|| FuzzFunctions::new(
|a: u8, b: u8| a ^ b,
Some(|a: u8| a),
Some(|a: u8, b: u8| a.wrapping_mul(b)),
));

create_fuzz_functions!(InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
// Check if there is enough data for the test (at least 2 values are needed)
Expand Down
15 changes: 15 additions & 0 deletions lattices/fuzz/src/fuzz_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// fuzz_functions.rs

// use once_cell::sync::Lazy;
// use crate::algebra_functions::FuzzFunctions;

#[macro_export]
macro_rules! create_fuzz_functions {
($type:ty, $functions:ident) => {
static $functions: once_cell::sync::Lazy<lattices_fuzz::algebra_functions::FuzzFunctions<$type>> = once_cell::sync::Lazy::new(|| lattices_fuzz::algebra_functions::FuzzFunctions::new(
|a: $type, b: $type| a ^ b,
Some(|a: $type| a),
Some(|a: $type, b: $type| a.wrapping_mul(b)),
));
};
}
3 changes: 2 additions & 1 deletion lattices/fuzz/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod algebra_functions;
pub mod algebra_functions;
pub mod fuzz_functions;
5 changes: 3 additions & 2 deletions lattices/fuzz/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn main() {
"associativity_fuzz",
"linearity_fuzz",
"monotonicity_fuzz",
"distributivity_fuzz",
];

for fuzz_target in fuzz_targets {
Expand All @@ -17,8 +18,8 @@ fn main() {
.arg("run")
.arg(fuzz_target)
.arg("--")
.arg("-max_total_time=5") // Run each fuzz target for 5 seconds
.current_dir(&manifest_dir) // Set the current working directory
.arg("-max_total_time=1") // Run each fuzz target for 5 seconds
.current_dir(&manifest_dir)
.status()
.expect("Failed to execute fuzz target");

Expand Down

0 comments on commit f30dd9f

Please sign in to comment.