Skip to content

Commit

Permalink
Fuzzing workspaces (#495)
Browse files Browse the repository at this point in the history
* Separate into workspaces

* Remove unused name

* Run cargo fix

* Update readme

* update makefile command for fuzzers

* delete command for cuda fuzzer

* Cuda fft fuzzer (#502)

* add cuda fft fuzzer

* add cuda fft fuzzer in honggfuzz

* add power of two check

---------

Co-authored-by: dafifynn <[email protected]>

---------

Co-authored-by: Juanma <[email protected]>
Co-authored-by: dafifynn <[email protected]>
Co-authored-by: Mauro Toscano <[email protected]>
Co-authored-by: daphneherlambda <[email protected]>
  • Loading branch information
5 people authored Aug 31, 2023
1 parent 6ad2e46 commit 96bb966
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 63 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ docs:
cd docs && mdbook serve --open

FUZZER = field_from_hex
run-fuzzer:
cd fuzz
cargo +nightly fuzz run $(FUZZER)
run-no-gpu-fuzzer:
cd fuzz/no_gpu_fuzz
cargo +nightly fuzz run --fuzz-dir . $(FUZZER)

METALFUZZER = fft_diff
run-metal-fuzzer:
cd fuzz/metal_fuzz
cargo +nightly fuzz run --fuzz-dir . $(METALFUZZER)

CUDAFUZZER = cuda_fft_fuzzer
run-cuda-fuzzer:
cd fuzzer
cargo hfuzz run $(CUDAFUZZER)

58 changes: 8 additions & 50 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,58 +1,16 @@
[package]
name = "fuzz"
[workspace]
members = ["no_gpu_fuzz", "metal_fuzz", "cuda_fuzz"]

[workspace.package]
edition = "2021"
publish = false
version = "0.1.1"
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
[workspace.dependencies]
lambdaworks-math = { path = "../math" }
lambdaworks-gpu = { path = "../gpu" }
libfuzzer-sys = "0.4"
lambdaworks-math = { path = "../math", features = ["metal"] }
lambdaworks-gpu = { path = "../gpu", features = ["metal"] }
num-traits = "0.2"
ibig = "0.3.6"

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[profile.release]
debug = 1

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

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

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

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

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

[[bin]]
name = "twiddles_generation"
path = "fuzz_targets/twiddles_generation.rs"
test = false
doc = false
5 changes: 3 additions & 2 deletions fuzz/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Fuzzing
There are three types of fuzzers distributed on different workspaces depending on the features (metal/cuda) they need. So you should make sure you cded into the right folder before running any of the commands.

### Running the fuzzers
`cargo +nightly fuzz run <target_name>`
`cargo +nightly fuzz run --fuzz-dir . <target_name>`
The targets can be found in the `fuzz_targets` directory. Normally the name of the file without the extension should work, if it doesn't, look up the name for that binary target in `Cargo.toml`.

### Debugging
If a crash is found, an `artifacts/<target_name>` folder will be added, inside it you'll find the different reports. To get an lldb dump, run
`cargo +nightly fuzz run <target_name> artifacts/crash-xxx`
`cargo +nightly fuzz run --fuzz-dir . <target_name> artifacts/crash-xxx`
31 changes: 31 additions & 0 deletions fuzz/metal_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "metal_fuzz"
version.workspace = true
edition.workspace = true

[package.metadata]
cargo-fuzz = true

[dependencies]
lambdaworks-math = { workspace = true, features = ["metal"] }
lambdaworks-gpu = { workspace = true, features = ["metal"] }
libfuzzer-sys = { workspace = true }

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

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

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

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use lambdaworks_math::{
field::{
fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
element::FieldElement,
},
unsigned_integer::element::UnsignedInteger
}
};

use lambdaworks_gpu::metal::abstractions::errors::MetalError;
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions fuzz/no_gpu_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "no_gpu_fuzz"
version.workspace = true
edition.workspace = true

[package.metadata]
cargo-fuzz = true

[dependencies]
lambdaworks-math = { workspace = true }
lambdaworks-gpu = { workspace = true }
libfuzzer-sys = { workspace = true }
num-traits = "0.2"
ibig = "0.3.6"

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

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

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

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ fuzz_target!(|values: (u64, u64)| {
let a = FieldElement::<Stark252PrimeField>::from_raw(&value_a);
let b = FieldElement::<Stark252PrimeField>::from_raw(&value_b);

let a_expected = ring.from(value_u64_a);
let b_expected = ring.from(value_u64_b);
let _a_expected = ring.from(value_u64_a);
let _b_expected = ring.from(value_u64_b);

let add_u64 = &a + &b;
let _add_u64 = &a + &b;

let sub_u64 = &a - &b;
let _sub_u64 = &a - &b;

let mul_u64 = &a * &b;
let _mul_u64 = &a * &b;

let pow = &a.pow(b.representative());
let _pow = &a.pow(b.representative());

if value_u64_b != 0 {

Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "fuzzer"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
honggfuzz = "0.5.51"
lambdaworks-math = { path = "../math", features = ["cuda"] }
lambdaworks-gpu = { path = "../gpu", features = ["cuda"] }


[workspace]
members = ["."]

[[bin]]
name = "cuda_fft_fuzzer"
path = "src/cuda_fft_fuzzer.rs"
13 changes: 13 additions & 0 deletions fuzzer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Setup
Run the following commands to get ready the setup.

* `cargo install honggfuzz `
* `apt install build-essential`
* `apt-get install binutils-dev`
* `sudo apt-get install libunwind-dev`
* `sudo apt-get install lldb`

## Run the fuzzer

Run the following command to run the specific fuzzer
`cargo hfuzz run <name of the fuzz target> `
53 changes: 53 additions & 0 deletions fuzzer/src/cuda_fft_fuzzer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[macro_use]
extern crate honggfuzz;
use lambdaworks_math::{
fft::{
gpu::cuda::{ops::fft as fft_cuda, state::CudaState},
cpu::{
roots_of_unity::get_twiddles,
ops::fft as fft_cpu
}
},
field::{
traits::RootsConfig,
fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
element::FieldElement
},
};

fn main() {
loop {
fuzz!(|data: Vec<u64>| {
let mut input_raw = data;
let mut inputs = Vec::new();

if input_raw.len() == 0 {
input_raw.push(0u64);
}

while !input_raw.len().is_power_of_two(){
input_raw.push(input_raw[0]);
}

for i in 0..input_raw.len() {
let input_value = format!("{:x}", input_raw[i]);
inputs.push(FieldElement::<Stark252PrimeField>::from_hex_unchecked(&input_value))
}

let twiddles = get_twiddles(
inputs.len().trailing_zeros() as u64,
RootsConfig::BitReverse,
)
.unwrap();

let state = CudaState::new().unwrap();
println!("inputs {:?}", &inputs);
println!("fft cpu{:?}", fft_cpu(&inputs, &twiddles));

match fft_cpu(&inputs, &twiddles) {
Ok(fft_result) => assert_eq!(fft_result, fft_cuda(&inputs, &twiddles, &state).unwrap()),
Err(_) => assert!(fft_cuda(&inputs, &twiddles, &state).is_err())
}
});
}
}

0 comments on commit 96bb966

Please sign in to comment.