Skip to content

Commit

Permalink
fixed most problems -- now we fail the test if there's any panic in o…
Browse files Browse the repository at this point in the history
…verflow.
  • Loading branch information
Chae authored and Chae committed Nov 1, 2024
1 parent e84bc16 commit a06de64
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 311 deletions.
8 changes: 3 additions & 5 deletions lattices/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
target
corpus
artifacts
coverage
fuzz_results
artifacts/
corpus/
fuzz_results/
149 changes: 0 additions & 149 deletions lattices/fuzz/Cargo.lock

This file was deleted.

7 changes: 6 additions & 1 deletion lattices/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ cargo-fuzz = true
once_cell = "1.8.0"
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
arbitrary = { version = "1", features = ["derive"] }

serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
syn = "2.0"
anyhow = "1.0"
rayon = "1.5"

[dependencies.lattices]
path = ".."
Expand Down
7 changes: 7 additions & 0 deletions lattices/fuzz/fuzz_functions_postfix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
,
Some(|a: $type, b: $type| a.wrapping_mul(b)),
Some(|a: $type| a)
)
});
};
}
7 changes: 7 additions & 0 deletions lattices/fuzz/fuzz_functions_prefix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[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(
51 changes: 33 additions & 18 deletions lattices/fuzz/fuzz_targets/associativity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
#![no_main]
use lattices::algebra::associativity_single;
use libfuzzer_sys::{arbitrary::Unstructured, fuzz_target};
use lattices_fuzz::utils;
use lattices_fuzz::utils_integrated;
use std::fs::OpenOptions;
use std::io::Write;
use std::panic;

#[macro_use]
extern crate lattices_fuzz;

create_fuzz_functions!(utils::InputType, FUNCTIONS);

create_fuzz_functions!(utils_integrated::InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
let mut us = Unstructured::new(data);
if let Ok(input) = us.arbitrary::<utils::TestingInput>() {
let result = associativity_single(input.i1.clone(), input.i2.clone(), input.i3.clone(), FUNCTIONS.f);
// println!("Associativity test result: {}", result);
let log_file = if result {
format!("fuzz_results/associativity_PASS_{}.log", std::any::type_name::<utils::InputType>())

} else {
format!("fuzz_results/associativity_FAIL_{}.log", std::any::type_name::<utils::InputType>())
if let Ok(input) = us.arbitrary::<utils_integrated::TestingInput>() {
let result = panic::catch_unwind(|| {
associativity_single(input.i1.clone(), input.i2.clone(), input.i3.clone(), FUNCTIONS.f)
});

};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
match result {
Ok(result) => {
println!("Associativity test result: {}", result);
let log_file = if result {
format!("fuzz_results/associativity_PASS_{}.log", std::any::type_name::<utils_integrated::InputType>())
} else {
format!("fuzz_results/associativity_FAIL_{}.log", std::any::type_name::<utils_integrated::InputType>())
};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
}
Err(_) => {
let log_file = format!("fuzz_results/associativity_PANIC_{}.log", std::any::type_name::<utils_integrated::InputType>());
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
println!("A panic occurred during the associativity test.");
}
}
}
});
49 changes: 34 additions & 15 deletions lattices/fuzz/fuzz_targets/commutativity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
#![no_main]
use lattices::algebra::commutativity_single;
use libfuzzer_sys::{arbitrary::Unstructured, fuzz_target};
use lattices_fuzz::utils;
use lattices_fuzz::utils_integrated;
use std::fs::OpenOptions;
use std::io::Write;
use std::panic;

#[macro_use]
extern crate lattices_fuzz;

create_fuzz_functions!(utils::InputType, FUNCTIONS);
create_fuzz_functions!(utils_integrated::InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
let mut us = Unstructured::new(data);
if let Ok(input) = us.arbitrary::<utils::TestingInput>() {
let result = commutativity_single(input.i1.clone(), input.i2.clone(), FUNCTIONS.f);
let log_file = if result {
format!("fuzz_results/commutativity_PASS_{}.log", std::any::type_name::<utils::InputType>())
} else {
format!("fuzz_results/commutativity_FAIL_{}.log", std::any::type_name::<utils::InputType>())
};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
if let Ok(input) = us.arbitrary::<utils_integrated::TestingInput>() {
let result = panic::catch_unwind(|| {
commutativity_single(input.i1.clone(), input.i2.clone(), FUNCTIONS.f)
});

match result {
Ok(result) => {
println!("Commutativity test result: {}", result);
let log_file = if result {
format!("fuzz_results/commutativity_PASS_{}.log", std::any::type_name::<utils_integrated::InputType>())
} else {
format!("fuzz_results/commutativity_FAIL_{}.log", std::any::type_name::<utils_integrated::InputType>())
};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
}
Err(_) => {
let log_file = format!("fuzz_results/commutativity_PANIC_{}.log", std::any::type_name::<utils_integrated::InputType>());
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
println!("A panic occurred during the commutativity test.");
}
}
}
});
51 changes: 34 additions & 17 deletions lattices/fuzz/fuzz_targets/distributivity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
#![no_main]

use lattices::algebra::commutativity_single;
use lattices::algebra::distributive_single;
use libfuzzer_sys::{arbitrary::Unstructured, fuzz_target};
use lattices_fuzz::utils;
use lattices_fuzz::utils_integrated;
use std::fs::OpenOptions;
use std::io::Write;
use std::panic;

#[macro_use]
extern crate lattices_fuzz;

create_fuzz_functions!(utils::InputType, FUNCTIONS);
create_fuzz_functions!(utils_integrated::InputType, FUNCTIONS);

fuzz_target!(|data: &[u8]| {
let mut us = Unstructured::new(data);
if let Ok(input) = us.arbitrary::<utils::TestingInput>() {
let result = distributive_single(input.i1.clone(), input.i2.clone(), input.i3.clone(), FUNCTIONS.f, FUNCTIONS.g.unwrap_or(utils::default_g));
// println!("Distributive test result: {}", result);
if let Ok(input) = us.arbitrary::<utils_integrated::TestingInput>() {
let result = panic::catch_unwind(|| {
distributive_single(input.i1.clone(), input.i2.clone(), input.i3.clone(), FUNCTIONS.f, FUNCTIONS.g.unwrap_or(utils_integrated::default_g))
});

let log_file = if result {
format!("fuzz_results/distributivity_PASS_{}.log", std::any::type_name::<utils::InputType>())
} else {
format!("fuzz_results/distributivity_FAIL_{}.log", std::any::type_name::<utils::InputType>())
};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
match result {
Ok(result) => {
println!("Distributive test result: {}", result);
let log_file = if result {
format!("fuzz_results/distributivity_PASS_{}.log", std::any::type_name::<utils_integrated::InputType>())
} else {
format!("fuzz_results/distributivity_FAIL_{}.log", std::any::type_name::<utils_integrated::InputType>())
};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
}
Err(_) => {
let log_file = format!("fuzz_results/distributivity_PANIC_{}.log", std::any::type_name::<utils_integrated::InputType>());
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(log_file)
.expect("Unable to open file");
writeln!(file, "Input: {:?}", input).expect("Unable to write to file");
println!("A panic occurred during the distributivity test.");
}
}
}
});
Loading

0 comments on commit a06de64

Please sign in to comment.