From 704f58362a499b0fd7df8faa21da1924ce23815a Mon Sep 17 00:00:00 2001 From: Mauro Toscano <12560266+MauroToscano@users.noreply.github.com> Date: Wed, 20 Sep 2023 17:22:31 -0300 Subject: [PATCH] Fix fuzzers (#564) * Rust cache on check * Fix fuzzer not building * Move deserialize fuzzer to no gpu fuzzers * Move deserialize fuzzer to no gpu fuzzers * Remove duplicated fuzzer * Update fuzzer makefile and readme --- .gitignore | 4 +- Makefile | 18 +++---- README.md | 15 ++++++ fuzz/Cargo.toml | 2 + fuzz/deserialize_stark_proof/Cargo.toml | 24 --------- .../fuzz_targets/deserialize.rs | 12 ----- fuzz/no_gpu_fuzz/Cargo.toml | 9 ++++ .../fuzz_targets/deserialize_stark_proof.rs | 12 +++++ fuzzer/Cargo.toml | 19 ------- fuzzer/README.md | 13 ----- fuzzer/src/cuda_fft_fuzzer.rs | 53 ------------------- 11 files changed, 48 insertions(+), 133 deletions(-) delete mode 100644 fuzz/deserialize_stark_proof/Cargo.toml delete mode 100644 fuzz/deserialize_stark_proof/fuzz_targets/deserialize.rs create mode 100644 fuzz/no_gpu_fuzz/fuzz_targets/deserialize_stark_proof.rs delete mode 100644 fuzzer/Cargo.toml delete mode 100644 fuzzer/README.md delete mode 100644 fuzzer/src/cuda_fft_fuzzer.rs diff --git a/.gitignore b/.gitignore index 18b9f6607..4e203d72b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Generated by Cargo # will have compiled files and executables -/target/ +**/target/** # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html @@ -21,3 +21,5 @@ proving_system/stark/src/cairo_run/program.trace **/.DS_Store ensure-no_std/target +# Files from fuzzers are inside a corpus folder +**/corpus/** diff --git a/Makefile b/Makefile index c2fb00948..cd9a70feb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .PHONY: test clippy docker-shell nix-shell benchmarks benchmark docs build-cuda build-metal clippy-metal test-metal coverage clean +FUZZ_DIR = fuzz/no_gpu_fuzz + ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) CAIRO0_PROGRAMS_DIR=provers/cairo/cairo_programs/cairo0 @@ -76,22 +78,16 @@ build-cuda: docs: cd docs && mdbook serve --open -STARK_DESERIALIZE_FUZZER = deserialize_stark_proof +run-fuzzer: + cargo +nightly fuzz run --fuzz-dir $(FUZZ_DIR) $(FUZZER) + proof-deserializer-fuzzer: - cd fuzz/deserialize_stark_proof - cargo +nightly fuzz run --fuzz-dir . $(STARK_DESERIALIZE_FUZZER) + cargo +nightly fuzz run --fuzz-dir $(FUZZ_DIR) deserialize_stark_proof -FUZZER = field_from_hex -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) + cargo +nightly fuzz run --fuzz-dir $(FUZZ_DIR) fft_diff -CUDAFUZZER = cuda_fft_fuzzer run-cuda-fuzzer: cd fuzz/cuda_fuzz cargo hfuzz run $(CUDAFUZZER) diff --git a/README.md b/README.md index 92337543d..6eecb9369 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,21 @@ If you use ```Lambdaworks``` libraries in your research projects, please cite th ## Fuzzers +Fuzzers are divided between the ones that use only the CPU, the ones that use Metal, and the ones that use CUDA. + +CPU Fuzzers can be run with the command ```bash make run-fuzzer FUZZER=fuzzer_name``` + +For example: + +```bash +make run-fuzzer FUZZER=field_from_hex +``` + +The list of fuzzers can be found in `fuzz/no_gpu_fuzz` + +Fuzzers for FTT in Metal and Cuda can be run with `make run-metal-fuzzer` and `make run-cuda-fuzzer` + + Run a specific fuzzer from the ones contained in **fuzz/fuzz_targets/** folder with`cargo`, for example to run the one for the target `field_from_hex`: ```bash diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index a213eff98..eb88a5592 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = ["no_gpu_fuzz", "metal_fuzz", "cuda_fuzz"] +resolver = "2" [workspace.package] edition = "2021" @@ -9,6 +10,7 @@ version = "0.1.1" [workspace.dependencies] lambdaworks-math = { path = "../math" } lambdaworks-gpu = { path = "../gpu" } +stark-platinum-prover = { path = "../provers/stark" } libfuzzer-sys = "0.4" [profile.release] diff --git a/fuzz/deserialize_stark_proof/Cargo.toml b/fuzz/deserialize_stark_proof/Cargo.toml deleted file mode 100644 index ac6b4554b..000000000 --- a/fuzz/deserialize_stark_proof/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "lambdaworks-stark-fuzz" -version.workspace = true -edition.workspace = true - -[package.metadata] -cargo-fuzz = true - -[dependencies] -lambdaworks-math = { workspace = true } -libfuzzer-sys = { workspace = true } - -[dependencies.lambdaworks-stark] -path = ".." - -# Prevent this from interfering with workspaces -[workspace] -members = ["."] - -[[bin]] -name = "deserialize_stark_proof" -path = "fuzz_targets/deserialize.rs" -test = false -doc = false diff --git a/fuzz/deserialize_stark_proof/fuzz_targets/deserialize.rs b/fuzz/deserialize_stark_proof/fuzz_targets/deserialize.rs deleted file mode 100644 index 8d9d3d801..000000000 --- a/fuzz/deserialize_stark_proof/fuzz_targets/deserialize.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![no_main] -use libfuzzer_sys::fuzz_target; -use lambdaworks_stark::starks::proof::stark::StarkProof; -use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField; -use lambdaworks_math::traits::{Deserializable, Serializable}; - - -fuzz_target!(|data: Vec| { - - let proof = StarkProof::::deserialize(&data); - -}); diff --git a/fuzz/no_gpu_fuzz/Cargo.toml b/fuzz/no_gpu_fuzz/Cargo.toml index f45a36907..0d0096a61 100644 --- a/fuzz/no_gpu_fuzz/Cargo.toml +++ b/fuzz/no_gpu_fuzz/Cargo.toml @@ -10,6 +10,8 @@ cargo-fuzz = true lambdaworks-math = { workspace = true } lambdaworks-gpu = { workspace = true } libfuzzer-sys = { workspace = true } +stark-platinum-prover = { workspace = true } + num-traits = "0.2" ibig = "0.3.6" @@ -31,3 +33,10 @@ path = "fuzz_targets/field_from_raw.rs" test = false doc = false +[[bin]] +name = "deserialize_stark_proof" +path = "fuzz_targets/deserialize_stark_proof.rs" +test = false +doc = false + + diff --git a/fuzz/no_gpu_fuzz/fuzz_targets/deserialize_stark_proof.rs b/fuzz/no_gpu_fuzz/fuzz_targets/deserialize_stark_proof.rs new file mode 100644 index 000000000..c6c945ee2 --- /dev/null +++ b/fuzz/no_gpu_fuzz/fuzz_targets/deserialize_stark_proof.rs @@ -0,0 +1,12 @@ +#![no_main] +use libfuzzer_sys::fuzz_target; +use stark_platinum_prover::proof::stark::StarkProof; +use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField; +use lambdaworks_math::traits::Deserializable; + + +fuzz_target!(|data: Vec| { + + let _proof = StarkProof::::deserialize(&data); + +}); diff --git a/fuzzer/Cargo.toml b/fuzzer/Cargo.toml deleted file mode 100644 index 4a811f13c..000000000 --- a/fuzzer/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[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" diff --git a/fuzzer/README.md b/fuzzer/README.md deleted file mode 100644 index 6d34ee5c9..000000000 --- a/fuzzer/README.md +++ /dev/null @@ -1,13 +0,0 @@ -## 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 ` diff --git a/fuzzer/src/cuda_fft_fuzzer.rs b/fuzzer/src/cuda_fft_fuzzer.rs deleted file mode 100644 index c46e73672..000000000 --- a/fuzzer/src/cuda_fft_fuzzer.rs +++ /dev/null @@ -1,53 +0,0 @@ -#[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| { - 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::::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()) - } - }); - } -}