Skip to content

Commit

Permalink
feat(fuzz-testing): doesn't detect input type but works with u8
Browse files Browse the repository at this point in the history
  • Loading branch information
Chae committed Oct 16, 2024
1 parent ebe63c2 commit 15a6c31
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 806 deletions.
574 changes: 0 additions & 574 deletions lattices/fuzz/Cargo.lock

This file was deleted.

71 changes: 35 additions & 36 deletions lattices/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "fuzz"
name = "lattices-fuzz"
path = "src/lib.rs"
version = "0.0.0"
publish = false
edition = "2021"
Expand All @@ -9,46 +10,44 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
lattices = { path = "../" } # Adjust if necessary
rand = "0.8" # or the latest version
chrono = "0.4"
afl = "0.15.10"
url = "*"

[profile.dev]
opt-level = 1
overflow-checks = true
debug = true
lto = true
codegen-units = 1
panic = "abort"

[profile.release]
opt-level = 2
overflow-checks = true
debug = true
lto = true
codegen-units = 1
panic = "abort"

[[bin]]
name = "all_fuzz"
path = "fuzz_targets/all_fuzz.rs"
once_cell = "1.8.0"


[dependencies.lattices]
path = ".."

[[bin]]
name = "associativity_fuzz"
path = "fuzz_targets/associativity_fuzz.rs"
test = false
doc = false
bench = false

[[bin]]
name = "commutativity_fuzz"
path = "fuzz_targets/commutativity_fuzz.rs"
test = false
doc = false
bench = false

[build]
target = "aarch64-apple-darwin"
[[bin]]
name = "distributivity_fuzz"
path = "fuzz_targets/distributivity_fuzz.rs"
test = false
doc = false
bench = false

[target.aarch64-apple-darwin]
linker = "clang"
rustflags = [
"-C", "link-arg=-mmacosx-version-min=14.0",
"-C", "link-arg=-lafl" # Link against the AFL library
]

[[bin]]
name = "linearity_fuzz"
path = "fuzz_targets/linearity_fuzz.rs"
test = false
doc = false
bench = false

[workspace]

[[bin]]
name = "monotonicity_fuzz"
path = "fuzz_targets/monotonicity_fuzz.rs"
test = false
doc = false
bench = false
80 changes: 0 additions & 80 deletions lattices/fuzz/fuzz_targets/all_fuzz.rs

This file was deleted.

48 changes: 21 additions & 27 deletions lattices/fuzz/fuzz_targets/associativity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
// #![no_main]

// extern crate libfuzzer_sys;
// use libfuzzer_sys::fuzz_target;
// use lattices::algebra::associativity_single;

// // Define your function f outside
// fn wrapping_add(x: u8, y: u8) -> u8 {
// x.wrapping_add(y)
// }

// fuzz_target!(|data: &[u8]| {
// if data.len() < 3 {
// return; // Ensure there's enough data for the test
// }

// let a = data[0];
// let b = data[1];
// let c = data[2];

// // Now you can use your wrapping_add function
// let result = associativity_single(a, b, c, wrapping_add);
// println!("Associativity test result: {}", result);
// });
#![no_main]

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;

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)),
));

fuzz_target!(|data: &[u8]| {
if data.len() < 3 {
println!("Not enough data for associativity test.");
return;
}

let a = data[0];
let b = data[1];
let c = data[2];

pub fn fuzz_target(a: u8, b: u8, c: u8, f: fn(u8, u8) -> u8) {
let result = associativity_single(a, b, c, f);
let result = associativity_single(a, b, c, FUNCTIONS.f);
println!("Associativity test result: {}", result);
}
});
44 changes: 22 additions & 22 deletions lattices/fuzz/fuzz_targets/commutativity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// #![no_main]
#![no_main]

// extern crate libfuzzer_sys;
// use libfuzzer_sys::fuzz_target;
// use lattices::algebra::commutativity_single;
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;

// pub fn fuzz_target(data: &[u8], f: fn(u8, u8) -> u8) {
// if data.len() < 2 {
// println!("Not enough data for commutativity test.");
// return;
// }
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)),
));

// let a = data[0];
// let b = data[1];
fuzz_target!(|data: &[u8]| {
if data.len() < 2 {
println!("Not enough data for commutativity test.");
return;
}

// let result = commutativity_single(a, b, f);
// println!("Commutativity test result: {}", result);
// }
#![no_main]
let a = data[0];
let b = data[1];

extern crate libfuzzer_sys;
use libfuzzer_sys::fuzz_target;
use lattices::algebra::commutativity_single;
let result = commutativity_single(a, b, FUNCTIONS.f);

pub fn fuzz_target(a: u8, b: u8, f: fn(u8, u8) -> u8) -> bool {
// Using the provided function f for testing
commutativity_single(a, b, f)
}
println!("Commutativity test result: {}", result);
});
51 changes: 27 additions & 24 deletions lattices/fuzz/fuzz_targets/distributivity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
// #![no_main]
// extern crate libfuzzer_sys;
// use libfuzzer_sys::fuzz_target;
// use lattices::algebra::distributive_single;

// pub fn fuzz_target(data: &[u8], f: fn(u8, u8) -> u8, g: fn(u8, u8) -> u8) {

// if data.len() < 3 {
// println!("Not enough data for distributive test.");
// return;
// }

// let a = data[0];
// let b = data[1];
// let c = data[2];

// let result = distributive_single(a, b, c, f, g);
// println!("Distributive test result: {}", result);
// }
#![no_main]

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

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)),
));

fuzz_target!(|data: &[u8]| {
if data.len() < 3 {
println!("Not enough data for distributivity test.");
return;
}

let a = data[0];
let b = data[1];
let c = data[2];

pub fn fuzz_target(a: u8, b: u8, f: fn(u8, u8) -> u8, g: fn(u8, u8) -> u8) {
let result = distributivity_single(a, b, f, g);
println!("Distributivity test result: {}", result);
}
if let Some(q) = FUNCTIONS.q {
let result = distributive_single(a, b, c, FUNCTIONS.f, q);
println!("Distributivity test result: {}", result);
} else {
println!("Skipping distributivity test because g is not available.");
}
});
33 changes: 21 additions & 12 deletions lattices/fuzz/fuzz_targets/linearity_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
#![no_main]

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

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)),
));

// Define the fuzz target
pub fn fuzz_target(
data: &[u8],
f: fn(u8, u8) -> u8,
q: fn(u8) -> u8,
g: fn(u8, u8) -> u8,
) {
if data.len() < 3 {
fuzz_target!(|data: &[u8]| {
if data.len() < 2 {
println!("Not enough data for linearity test.");
return;
}

let a = data[0];
let b = data[1];

let result = linearity_single(a, b, f, q, g);
println!("Linearity test result: {}", result);
}
if let (Some(g), Some(q)) = (FUNCTIONS.g, FUNCTIONS.q) {
let result = linearity_single(a, b, FUNCTIONS.f, g, q);
println!("Linearity test result: {}", result);
} else {
println!("Skipping linearity test because g or q is not available.");
}
});

Loading

0 comments on commit 15a6c31

Please sign in to comment.