From 28c7123eb158f637a43edbe3cf6bea8da668372d Mon Sep 17 00:00:00 2001 From: lukacan Date: Thu, 2 Jan 2025 19:46:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Code=20quality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/config/src/fuzz.rs | 2 +- crates/config/src/honggfuzz.rs | 24 +++++----- crates/config/src/lib.rs | 84 ++++++++++++++++------------------ crates/fuzz/src/fuzz_client.rs | 2 +- 4 files changed, 54 insertions(+), 58 deletions(-) diff --git a/crates/config/src/fuzz.rs b/crates/config/src/fuzz.rs index ba83fa76..50761f85 100644 --- a/crates/config/src/fuzz.rs +++ b/crates/config/src/fuzz.rs @@ -1,3 +1,4 @@ +use crate::utils::resolve_path; use base64::{prelude::BASE64_STANDARD, Engine}; use serde::{Deserialize, Serialize}; use solana_sdk::{ @@ -5,7 +6,6 @@ use solana_sdk::{ pubkey::Pubkey, }; use std::{fs, str::FromStr}; -use crate::utils::resolve_path; #[derive(Debug, Deserialize, Clone, Default)] pub struct Fuzz { diff --git a/crates/config/src/honggfuzz.rs b/crates/config/src/honggfuzz.rs index 59ee091f..cbc026c7 100644 --- a/crates/config/src/honggfuzz.rs +++ b/crates/config/src/honggfuzz.rs @@ -267,7 +267,7 @@ mod tests { honggfuzz.timeout = Some(timeout); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-t 10"]); + assert_eq!(arg, vec!["-t", "10"]); } #[test] fn test_iterations() { @@ -278,7 +278,7 @@ mod tests { honggfuzz.iterations = Some(iterations); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-N 1000"]); + assert_eq!(arg, vec!["-N", "1000"]); } #[test] fn test_threads() { @@ -289,7 +289,7 @@ mod tests { honggfuzz.threads = Some(threads); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-n 15"]); + assert_eq!(arg, vec!["-n", "15"]); } #[test] fn test_keep_output() { @@ -299,7 +299,7 @@ mod tests { honggfuzz.keep_output = Some(true); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-Q"]); + assert_eq!(arg, vec!["-Q", ""]); } #[test] fn test_verbose() { @@ -309,7 +309,7 @@ mod tests { honggfuzz.verbose = Some(true); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-v"]); + assert_eq!(arg, vec!["-v", ""]); } #[test] fn test_exit_upon_crash() { @@ -319,7 +319,7 @@ mod tests { honggfuzz.exit_upon_crash = Some(true); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["--exit_upon_crash"]); + assert_eq!(arg, vec!["--exit_upon_crash", ""]); } #[test] fn test_mutations_per_run() { @@ -330,7 +330,7 @@ mod tests { honggfuzz.mutations_per_run = Some(mutations_per_run); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-r 33"]); + assert_eq!(arg, vec!["-r", "33"]); } #[test] fn test_crashdir() { @@ -340,7 +340,7 @@ mod tests { honggfuzz.crashdir = Some(crash_dir.to_string()); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["--crashdir crashdir1"]); + assert_eq!(arg, vec!["--crashdir", "crashdir1"]); } #[test] fn test_extension() { @@ -351,7 +351,7 @@ mod tests { honggfuzz.extension = Some(extension.to_string()); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-e sol"]); + assert_eq!(arg, vec!["-e", "sol"]); } #[test] fn test_run_time() { @@ -362,7 +362,7 @@ mod tests { honggfuzz.run_time = Some(run_time); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["--run_time 13"]); + assert_eq!(arg, vec!["--run_time", "13"]); } #[test] fn test_max_file_size() { @@ -373,7 +373,7 @@ mod tests { honggfuzz.max_file_size = Some(max_file_size); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-F 500"]); + assert_eq!(arg, vec!["-F", "500"]); } #[test] fn test_save_all() { @@ -383,7 +383,7 @@ mod tests { honggfuzz.save_all = Some(true); let arg = honggfuzz.get_collect_fuzz_args(); - assert_eq!(arg, vec!["-u"]); + assert_eq!(arg, vec!["-u", ""]); } #[test] fn test_cargo_target_dir() { diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 128ea26f..9e8a5e5b 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -115,65 +115,61 @@ impl Config { full_path.to_str().unwrap().to_string() } pub fn get_afl_build_args(&self) -> Vec { - if let Some(afl) = &self.afl { - afl.get_collect_build_args() - } else { - Vec::default() - } + self.afl + .as_ref() + .map(|afl| afl.get_collect_build_args()) + .unwrap_or_default() } pub fn get_afl_fuzz_args(&self) -> Vec { - if let Some(afl) = &self.afl { - afl.get_collect_fuzz_args() - } else { - Vec::default() - } + self.afl + .as_ref() + .map(|afl| afl.get_collect_fuzz_args()) + .unwrap_or_default() } pub fn get_initial_seed(&self) -> Vec { - if let Some(afl) = &self.afl { - afl.get_seeds() - } else { - // if nothing is provided, use the default seed - vec![AflSeed::default()] - } + self.afl + .as_ref() + .map(|afl| afl.get_seeds()) + .unwrap_or_else(|| vec![AflSeed::default()]) } // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* // fuzz pub fn get_fuzzing_with_stats(&self) -> bool { - if let Some(fuzz) = &self.fuzz { - fuzz.get_fuzzing_with_stats() - } else { - false - } + self.fuzz + .as_ref() + .map(|fuzz| fuzz.get_fuzzing_with_stats()) + .unwrap_or_default() } pub fn get_allow_duplicate_txs(&self) -> bool { - if let Some(fuzz) = &self.fuzz { - fuzz.get_allow_duplicate_txs() - } else { - false - } + self.fuzz + .as_ref() + .map(|fuzz| fuzz.get_allow_duplicate_txs()) + .unwrap_or_default() } pub fn programs(&self) -> Vec { - if let Some(fuzz) = &self.fuzz { - if let Some(programs) = &fuzz.programs { - programs.iter().map(FuzzProgram::from).collect() - } else { - Vec::default() - } - } else { - Vec::default() - } + self.fuzz + .as_ref() + .map(|fuzz| { + if let Some(programs) = &fuzz.programs { + programs.iter().map(FuzzProgram::from).collect() + } else { + Vec::default() + } + }) + .unwrap_or_default() } pub fn accounts(&self) -> Vec { - if let Some(fuzz) = &self.fuzz { - if let Some(accounts) = &fuzz.accounts { - accounts.iter().map(FuzzAccount::from).collect() - } else { - Vec::default() - } - } else { - Vec::default() - } + self.fuzz + .as_ref() + .map(|fuzz| { + if let Some(accounts) = &fuzz.accounts { + accounts.iter().map(FuzzAccount::from).collect() + } else { + Vec::default() + } + }) + .unwrap_or_default() } } diff --git a/crates/fuzz/src/fuzz_client.rs b/crates/fuzz/src/fuzz_client.rs index f310de49..f03df694 100644 --- a/crates/fuzz/src/fuzz_client.rs +++ b/crates/fuzz/src/fuzz_client.rs @@ -1,6 +1,5 @@ #![allow(dead_code)] -use trident_config::Config; use crate::error::*; use solana_sdk::account::AccountSharedData; use solana_sdk::hash::Hash; @@ -8,6 +7,7 @@ use solana_sdk::instruction::Instruction; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::Keypair; use solana_sdk::sysvar::Sysvar; +use trident_config::Config; use trident_svm::utils::ProgramEntrypoint; /// A trait providing methods to read and write (manipulate) accounts