Skip to content

Commit

Permalink
🎨 move fuzz to separate crate and update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Jun 28, 2024
1 parent 9e4adf1 commit 2da674a
Show file tree
Hide file tree
Showing 40 changed files with 312 additions and 144 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
incremented upon a breaking change and the patch version will be incremented for features.

## [dev] - Unreleased
- feat/fuzzing moved to separate crate trident-fuzz ([#175](https://github.com/Ackee-Blockchain/trident/pull/175))
- feat/unify dependencies provided by the Trident ([#172](https://github.com/Ackee-Blockchain/trident/pull/172))
- fix/in case of fuzzing failure throw error instead of only printing message ([#167](https://github.com/Ackee-Blockchain/trident/pull/167))
- del/remove Trident explorer ([#171](https://github.com/Ackee-Blockchain/trident/pull/171))
Expand Down
53 changes: 42 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/cli", "crates/client", "crates/test"]
members = ["crates/cli", "crates/client", "crates/test", "crates/fuzz"]
exclude = ["examples/"]
resolver = "1"

Expand Down
18 changes: 5 additions & 13 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ readme = "../../README.md"
description = "The trident_client crate helps you build and deploy an Anchor program to a local cluster and run a test suite against it."

[features]
fuzzing = [
"dep:solana-program-test",
"dep:honggfuzz",
"quinn-proto/arbitrary",
"dep:solana-program-runtime",
]
fuzzing = ["dep:solana-program-test", "dep:honggfuzz", "quinn-proto/arbitrary"]

[build-dependencies]
anyhow = { version = "1.0.45", features = ["std"], default-features = false }
Expand All @@ -23,11 +18,11 @@ pretty_assertions = "1.1.0"

[dependencies]
# TRIDENT
trident-derive-displayix = { path = "./derive/display_ix", version = "0.0.1" }
trident-derive-fuzz-deserialize = { path = "./derive/fuzz_deserialize", version = "0.0.1" }
trident-derive-fuzz-test-executor = { path = "./derive/fuzz_test_executor", version = "0.0.1" }
trident-derive-displayix = { path = "../fuzz/derive/display_ix", version = "0.0.1" }
trident-derive-fuzz-deserialize = { path = "../fuzz/derive/fuzz_deserialize", version = "0.0.1" }
trident-derive-fuzz-test-executor = { path = "../fuzz/derive/fuzz_test_executor", version = "0.0.1" }
trident-test = { path = "../test", version = "0.3.2" }

trident-fuzz = { path = "../fuzz", version = "0.1.0" }

# ANCHOR
# INFO: Anchor-spl is here as dependency only to activate the idl-build feature, so that
Expand All @@ -44,8 +39,6 @@ solana-transaction-status = { workspace = true }
solana-account-decoder = { workspace = true }
spl-token = { workspace = true }
spl-associated-token-account = { workspace = true }
solana-banks-client = { workspace = true }
solana-program-runtime = { workspace = true, optional = true }
solana-program-test = { workspace = true, optional = true }


Expand Down Expand Up @@ -80,4 +73,3 @@ quinn-proto = { version = "0.10.6", optional = true }
pathdiff = "0.2.1"
indicatif = "0.17.8"
regex = "1.10.3"
prettytable = "0.10.0"
2 changes: 1 addition & 1 deletion crates/client/src/commander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use tokio::{
};

use crate::constants::*;
use crate::fuzzer::fuzzing_stats::FuzzingStatistics;
use tokio::io::AsyncBufReadExt;
use trident_fuzz::fuzzing_stats::FuzzingStatistics;

#[derive(Error, Debug)]
pub enum Error {
Expand Down
33 changes: 15 additions & 18 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ pub mod fuzzing {
pub use trident_derive_fuzz_deserialize::FuzzDeserialize;
pub use trident_derive_fuzz_test_executor::FuzzTestExecutor;

pub use trident_fuzz::convert_entry;
pub use trident_fuzz::fuzz_trident;
pub use trident_fuzz::show_account;
/// trident macros
pub use super::fuzzer::*;
pub use crate::convert_entry;
pub use crate::fuzz_trident;
pub use crate::show_account;
pub use trident_fuzz::*;

pub use super::fuzzer::program_test_client_blocking::ProgramEntry;
pub use solana_program_test::processor;
pub use trident_fuzz::program_test_client_blocking::ProgramEntry;

/// trident methods
pub use super::fuzzer::accounts_storage::*;
pub use super::fuzzer::data_builder::build_ix_fuzz_data;
pub use super::fuzzer::data_builder::*;
pub use super::fuzzer::error::*;
pub use super::fuzzer::fuzzing_stats::FuzzingStatistics;
pub use super::fuzzer::program_test_client_blocking::ProgramTestClientBlocking;
pub use super::fuzzer::snapshot::Snapshot;
pub use super::temp_clone::*;
/// trident methods
pub use trident_fuzz::accounts_storage::*;
pub use trident_fuzz::data_builder::build_ix_fuzz_data;
pub use trident_fuzz::data_builder::*;
pub use trident_fuzz::error::*;
pub use trident_fuzz::fuzzing_stats::FuzzingStatistics;
pub use trident_fuzz::program_test_client_blocking::ProgramTestClientBlocking;
pub use trident_fuzz::snapshot::Snapshot;

pub use std::cell::RefCell;
pub use std::collections::HashMap;
Expand Down Expand Up @@ -92,11 +92,10 @@ mod client;
mod commander;
mod config;
mod error_reporter;
mod fuzzer;
mod idl;
mod keys;
mod program_client_generator;
mod reader;
mod source_code_generators;
mod temp_clone;
mod test_generator;
mod tester;
Expand All @@ -108,12 +107,10 @@ pub mod ___private {
pub use super::commander::Error;
pub use super::commander::LocalnetHandle;
pub use super::error_reporter::*;
pub use super::fuzzer::fuzzer_generator;
pub use super::fuzzer::snapshot_generator;
pub use super::idl::*;
pub use super::keys::*;
pub use super::program_client_generator::*;
pub use super::reader::*;
pub use super::source_code_generators::*;
pub use super::temp_clone::TempClone;
pub use super::test_generator::ProgramData;
pub use super::test_generator::TestGenerator;
Expand Down
3 changes: 3 additions & 0 deletions crates/client/src/source_code_generators/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod fuzzer_generator;
pub mod program_client_generator;
pub mod snapshot_generator;
13 changes: 7 additions & 6 deletions crates/client/src/test_generator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{
commander::{Commander, Error as CommanderError},
fuzzer,
idl::IdlProgram,
program_client_generator,
};

use crate::fuzzer::snapshot_generator::generate_snapshots_code;
use crate::source_code_generators::fuzzer_generator;
use crate::source_code_generators::program_client_generator;
use crate::source_code_generators::snapshot_generator;

use cargo_metadata::{camino::Utf8PathBuf, Package};
use fehler::{throw, throws};
use std::{fs::File, io::prelude::*};
Expand Down Expand Up @@ -491,16 +492,16 @@ impl TestGenerator {

// create fuzz instructions file
let fuzz_instructions_path = new_fuzz_test_dir.join(FUZZ_INSTRUCTIONS_FILE_NAME);
let program_fuzzer = fuzzer::fuzzer_generator::generate_source_code(&self.programs_data);
let program_fuzzer = fuzzer_generator::generate_source_code(&self.programs_data);
let program_fuzzer = Commander::format_program_code(&program_fuzzer).await?;

self.create_file(&fuzz_instructions_path, &program_fuzzer)
.await?;

// // create accounts_snapshots file
let accounts_snapshots_path = new_fuzz_test_dir.join(ACCOUNTS_SNAPSHOTS_FILE_NAME);
let fuzzer_snapshots =
generate_snapshots_code(&self.programs_data).map_err(Error::ReadProgramCodeFailed)?;
let fuzzer_snapshots = snapshot_generator::generate_snapshots_code(&self.programs_data)
.map_err(Error::ReadProgramCodeFailed)?;
let fuzzer_snapshots = Commander::format_program_code(&fuzzer_snapshots).await?;

self.create_file(&accounts_snapshots_path, &fuzzer_snapshots)
Expand Down
5 changes: 4 additions & 1 deletion crates/client/tests/test_program_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ pub async fn generate_program_client() {
let program_data = vec![program_data];

let use_modules: Vec<syn::ItemUse> = vec![syn::parse_quote! { use trident_client::*; }];
let client_code = trident_client::___private::generate_source_code(&program_data, &use_modules);
let client_code = trident_client::___private::program_client_generator::generate_source_code(
&program_data,
&use_modules,
);
let client_code =
trident_client::___private::Commander::format_program_code(&client_code).await?;

Expand Down
39 changes: 39 additions & 0 deletions crates/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "trident-fuzz"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/Ackee-Blockchain/trident"
license-file = "../../LICENSE"
readme = "../../README.md"
description = "The trident_fuzz crate helps you to write Rust Fuzz Tests for your programs with Trident."

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

[dependencies]
# SOLANA
solana-sdk = { workspace = true }
solana-banks-client = { workspace = true }
spl-token = { workspace = true }
solana-program-runtime = { workspace = true }
solana-program-test = { workspace = true }

# ANCHOR
anchor-lang = { workspace = true, features = ["idl-build", "init-if-needed"] }
anchor-syn = { workspace = true }


# ARBITRARY
arbitrary = { version = "1.3.0", features = ["derive"] }


# MISC
thiserror = "1.0.30"
syn = { version = "1.0.109", features = ["visit"] }
proc-macro2 = { version = "1.0.66", default-features = false }
quote = "1.0.14"
heck = { version = "0.4.0", default-features = false }
regex = "1.10.3"
prettytable = "0.10.0"
serde = { version = "1.0.136", default-features = false }
serde_json = "1.0.72"
tokio = "1"
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;

use solana_sdk::{pubkey::Pubkey, signature::Keypair};

use crate::fuzzer::{data_builder::FuzzClient, AccountId};
use crate::{data_builder::FuzzClient, AccountId};

pub struct PdaStore {
pub pubkey: Pubkey,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(dead_code)]

use anchor_client::anchor_lang::solana_program::account_info::{Account as Acc, AccountInfo};
use anchor_client::anchor_lang::solana_program::hash::Hash;
use anchor_lang::prelude::Rent;
use anchor_lang::solana_program::account_info::{Account as Acc, AccountInfo};
use anchor_lang::solana_program::hash::Hash;
use arbitrary::Arbitrary;
use arbitrary::Unstructured;
use solana_sdk::account::{Account, AccountSharedData};
Expand All @@ -15,7 +15,7 @@ use std::collections::HashMap;
use std::error::Error;
use std::fmt::Display;

use crate::fuzzer::error::*;
use crate::error::*;

pub struct FuzzData<T, U> {
pub pre_ixs: Vec<T>,
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 2da674a

Please sign in to comment.