Skip to content

Commit

Permalink
Add new examples for Fuzzer (#110)
Browse files Browse the repository at this point in the history
* πŸ§ͺ add fuzz_example1

* πŸ§ͺ added fuzz_example0, this is copy of 'fuzzer' example but with the new structure

* βœ… work on example0

* βœ… working on example1

* βœ… added fresh example2

* βœ… fuzz test for example2

* βœ… slightly different fuz test for fuzz example 1

* βœ… added fuzz example3 - escrow with bad math

* βœ… fuzz test for fuzz example3

* πŸš€ added check for register Subject and added initial problem description

* βœ… add crashes for both checks

* βœ… fuzz example 0 in progress

* πŸ“ readme updates

* πŸ“ example1 small readme  update

* βœ… small program update

* πŸ“ readme + added fuzzer output

* βœ… minor program example updates

* βœ… remove unnecessary coments , add few notes , add new withdraw check

* βœ… add crash files

* πŸ“ add readme template

* πŸ› update program code and fix accounts snapshot

* πŸ“ add readme + crash file

* πŸ™ˆ gitignore

* βœ… remove unnecessary code + update fuzz test check

* βœ… add crashes

* πŸ“ initial readme

* πŸ“ readme update + minor code update

* πŸ“ the final check part

* πŸ“ code format

* πŸ“ better grammar and spelling

* πŸ“ better grammar and spelling

* πŸ“ better grammar and spelling

* πŸ“ link to uncomment check

* 🚧 todo find crash file for the todo check

* πŸ”₯ removed incorrect check from withdraw + update readme

* ✏️ fix fuzz_example0 - Update readme and rename fuzzer to fuzz_example0

* ✏️ fix fuzz_example2 comments

* ✏️ fixes for fuzz_example3 - remove unnecessary code parts

* ✏️ fix fuzz_example3 - remove check for Invest function, update Readme

* Fixed snapshot deserialization

* Removed unused imports

* Rebase fixes

* πŸ§ͺ added fuzz_example0, this is copy of 'fuzzer' example but with the new structure

* βœ… work on example0

* βœ… working on example1

* βœ… added fresh example2

* βœ… fuzz test for example2

* βœ… slightly different fuz test for fuzz example 1

* βœ… added fuzz example3 - escrow with bad math

* βœ… fuzz test for fuzz example3

* πŸš€ added check for register Subject and added initial problem description

* βœ… add crashes for both checks

* βœ… fuzz example 0 in progress

* πŸ“ readme updates

* πŸ“ example1 small readme  update

* βœ… small program update

* πŸ“ readme + added fuzzer output

* βœ… minor program example updates

* βœ… remove unnecessary coments , add few notes , add new withdraw check

* βœ… add crash files

* πŸ“ add readme template

* πŸ› update program code and fix accounts snapshot

* πŸ“ add readme + crash file

* πŸ™ˆ gitignore

* βœ… remove unnecessary code + update fuzz test check

* βœ… add crashes

* πŸ“ initial readme

* πŸ“ readme update + minor code update

* πŸ“ the final check part

* πŸ“ code format

* πŸ“ better grammar and spelling

* πŸ“ better grammar and spelling

* πŸ“ better grammar and spelling

* πŸ“ link to uncomment check

* 🚧 todo find crash file for the todo check

* πŸ”₯ removed incorrect check from withdraw + update readme

* ✏️ fix fuzz_example0 - Update readme and rename fuzzer to fuzz_example0

* ✏️ fix fuzz_example2 comments

* ✏️ fixes for fuzz_example3 - remove unnecessary code parts

* ✏️ fix fuzz_example3 - remove check for Invest function, update Readme

* 🎨 cargo fmt

* 🎨 clippy allow too many arguments

---------

Co-authored-by: lukacan <[email protected]>
Co-authored-by: Ikrk <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2024
1 parent 82ac6bb commit c6a8c49
Show file tree
Hide file tree
Showing 75 changed files with 18,296 additions and 127 deletions.
1 change: 1 addition & 0 deletions crates/client/src/fuzzer/accounts_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl AccountsStorage<Keypair> {
}

impl AccountsStorage<TokenStore> {
#[allow(clippy::too_many_arguments)]
pub fn get_or_create_account(
&mut self,
account_id: AccountId,
Expand Down
1 change: 1 addition & 0 deletions crates/client/src/fuzzer/data_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub trait FuzzClient {
// TODO add methods to modify current accounts
// TODO check if self must be mutable
fn set_account(&mut self, lamports: u64) -> Keypair;
#[allow(clippy::too_many_arguments)]
fn set_token_account(
&mut self,
mint: Pubkey,
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/fuzzer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod fuzzer_generator;
pub mod accounts_storage;
pub mod data_builder;
pub mod fuzzer_generator;
#[cfg(feature = "fuzzing")]
pub mod program_test_client_blocking;
pub mod snapshot;
pub mod accounts_storage;
pub mod snapshot_generator;

pub type AccountId = u8;
8 changes: 4 additions & 4 deletions crates/client/src/fuzzer/program_test_client_blocking.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::fuzzing::ProgramTest;
use crate::fuzzing::{ProgramTestContext, SYSTEM_PROGRAM_ID};
use crate::solana_sdk::account::Account;
use solana_program_runtime::invoke_context::ProcessInstructionWithContext;
use crate::fuzzing::{ProgramTestContext, SYSTEM_PROGRAM_ID};
use crate::fuzzing::ProgramTest;
use solana_sdk::{
account::AccountSharedData, hash::Hash, instruction::AccountMeta, program_option::COption,
program_pack::Pack, pubkey::Pubkey, rent::Rent, signature::Keypair,
transaction::VersionedTransaction, signature::Signer
program_pack::Pack, pubkey::Pubkey, rent::Rent, signature::Keypair, signature::Signer,
transaction::VersionedTransaction,
};
use spl_token::state::Mint;
use tokio::runtime::Builder;
Expand Down
4 changes: 1 addition & 3 deletions crates/client/src/fuzzer/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use solana_sdk::{account::Account, instruction::AccountMeta};

use crate::data_builder::{FuzzDeserialize, FuzzClient, FuzzClientError, FuzzingError};


use crate::data_builder::{FuzzClient, FuzzClientError, FuzzDeserialize, FuzzingError};

pub struct Snapshot<'info, T> {
before: Vec<Option<Account>>,
Expand Down
9 changes: 5 additions & 4 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ pub use anyhow::{self, Error};
pub mod fuzzing {
pub use super::{
anchor_lang, anchor_lang::system_program::ID as SYSTEM_PROGRAM_ID,
anchor_lang::InstructionData, anchor_lang::ToAccountMetas, solana_sdk::account::Account,
solana_sdk::transaction::Transaction, Instruction, Keypair, Pubkey, Signer, TempClone, fuzz_trd,
anchor_lang::InstructionData, anchor_lang::ToAccountMetas, fuzz_trd,
solana_sdk::account::Account, solana_sdk::transaction::Transaction, Instruction, Keypair,
Pubkey, Signer, TempClone,
};
pub use anchor_lang::solana_program::instruction::AccountMeta;
pub use anchor_client::anchor_lang::solana_program::hash::Hash;
pub use anchor_lang::solana_program::instruction::AccountMeta;
pub use arbitrary;
pub use arbitrary::Arbitrary;
pub use honggfuzz::fuzz;
Expand All @@ -42,10 +43,10 @@ pub mod fuzzing {
pub use super::fuzzer::program_test_client_blocking::ProgramTestClientBlocking;
pub use super::fuzzer::snapshot::Snapshot;
pub use super::fuzzer::*;
pub use std::cell::RefCell;
pub use trdelnik_derive_displayix::DisplayIx;
pub use trdelnik_derive_fuzz_deserialize::FuzzDeserialize;
pub use trdelnik_derive_fuzz_test_executor::FuzzTestExecutor;
pub use std::cell::RefCell;
}

pub use futures::{self, FutureExt};
Expand Down
16 changes: 8 additions & 8 deletions crates/client/src/test_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,12 @@ impl TestGenerator {
let fuzz_test_content = if let Some(lib) = libs.first() {
let use_entry = format!("use {}::entry;\n", lib);
let use_instructions = format!("use program_client::{}_instruction::*;\n", lib);
let use_fuzz_instructions = format!("use trdelnik_tests::fuzz_instructions::{}_fuzz_instructions::FuzzInstruction;\n", lib);
let template = format!("{use_entry}{use_instructions}{use_fuzz_instructions}{fuzz_test_content}");
let use_fuzz_instructions = format!(
"use trdelnik_tests::fuzz_instructions::{}_fuzz_instructions::FuzzInstruction;\n",
lib
);
let template =
format!("{use_entry}{use_instructions}{use_fuzz_instructions}{fuzz_test_content}");
template.replace("###PROGRAM_NAME###", lib)
} else {
throw!(Error::NoProgramsFound)
Expand All @@ -200,12 +204,8 @@ impl TestGenerator {
.join(TESTS_WORKSPACE)
.join("src")
.join(FUZZ_LIB_FILE_NAME);
self.create_file(
&fuzz_lib_path,
FUZZ_LIB_FILE_NAME,
fuzz_lib_content,
)
.await?;
self.create_file(&fuzz_lib_path, FUZZ_LIB_FILE_NAME, fuzz_lib_content)
.await?;

// create fuzz instructions file
let fuzz_instructions_path = root
Expand Down
9 changes: 9 additions & 0 deletions examples/fuzz_example0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

.anchor
.DS_Store
target
**/*.rs.bk
node_modules
test-ledger
.yarn
trdelnik-tests/hfuzz_target
8 changes: 8 additions & 0 deletions examples/fuzz_example0/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.anchor
.DS_Store
target
node_modules
dist
build
test-ledger
11 changes: 11 additions & 0 deletions examples/fuzz_example0/.program_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "program_client"
version = "0.1.0"
edition = "2018"

[dependencies.trdelnik-client]
path = "../../../crates/client"

[dependencies.fuzz_example0]
path = "../programs/fuzz_example0"
features = ["no-entrypoint"]
88 changes: 88 additions & 0 deletions examples/fuzz_example0/.program_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// DO NOT EDIT - automatically generated file (except `use` statements inside the `*_instruction` module
pub mod fuzz_example0_instruction {
use trdelnik_client::*;
pub static PROGRAM_ID: Pubkey = Pubkey::new_from_array([
170u8, 64u8, 48u8, 229u8, 53u8, 121u8, 89u8, 247u8, 36u8, 222u8, 119u8, 168u8, 36u8, 42u8,
8u8, 162u8, 161u8, 90u8, 85u8, 0u8, 151u8, 100u8, 169u8, 133u8, 216u8, 142u8, 250u8, 145u8,
26u8, 46u8, 170u8, 146u8,
]);
pub async fn initialize(
client: &Client,
a_counter: Pubkey,
a_user: Pubkey,
a_system_program: Pubkey,
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
client
.send_instruction(
PROGRAM_ID,
fuzz_example0::instruction::Initialize {},
fuzz_example0::accounts::Initialize {
counter: a_counter,
user: a_user,
system_program: a_system_program,
},
signers,
)
.await
}
pub fn initialize_ix(
a_counter: Pubkey,
a_user: Pubkey,
a_system_program: Pubkey,
) -> Instruction {
Instruction {
program_id: PROGRAM_ID,
data: fuzz_example0::instruction::Initialize {}.data(),
accounts: fuzz_example0::accounts::Initialize {
counter: a_counter,
user: a_user,
system_program: a_system_program,
}
.to_account_metas(None),
}
}
pub async fn update(
client: &Client,
i_input1: u8,
i_input2: u8,
a_counter: Pubkey,
a_authority: Pubkey,
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
client
.send_instruction(
PROGRAM_ID,
fuzz_example0::instruction::Update {
input1: i_input1,
input2: i_input2,
},
fuzz_example0::accounts::Update {
counter: a_counter,
authority: a_authority,
},
signers,
)
.await
}
pub fn update_ix(
i_input1: u8,
i_input2: u8,
a_counter: Pubkey,
a_authority: Pubkey,
) -> Instruction {
Instruction {
program_id: PROGRAM_ID,
data: fuzz_example0::instruction::Update {
input1: i_input1,
input2: i_input2,
}
.data(),
accounts: fuzz_example0::accounts::Update {
counter: a_counter,
authority: a_authority,
}
.to_account_metas(None),
}
}
}
15 changes: 15 additions & 0 deletions examples/fuzz_example0/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[features]
seeds = false
skip-lint = false
[programs.localnet]
fuzz_example0 = "CTb5cKBLEGgFEw3jDGYCHCiHJ2PY2LFoxXhEPRhFFL4h"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
Loading

0 comments on commit c6a8c49

Please sign in to comment.