Skip to content

Commit

Permalink
🔒️ Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Oct 21, 2024
1 parent 99cdeb5 commit 5914726
Show file tree
Hide file tree
Showing 58 changed files with 831 additions and 785 deletions.
1 change: 0 additions & 1 deletion examples/fuzz-tests/arbitrary-custom-types-4/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ node_modules
test-ledger
.yarn
trident-tests/fuzz_tests/fuzzing/honggfuzz/hfuzz_target
trident-tests/fuzz_tests/fuzzing/afl/afl_target
27 changes: 10 additions & 17 deletions examples/fuzz-tests/arbitrary-custom-types-4/Cargo.lock

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

1 change: 1 addition & 0 deletions examples/fuzz-tests/arbitrary-custom-types-4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["programs/*", "trident-tests/fuzz_tests"]

[profile.release]
overflow-checks = true
lto = "fat"
Expand Down
36 changes: 0 additions & 36 deletions examples/fuzz-tests/arbitrary-custom-types-4/Trident.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,6 @@ max_file_size = 1048576
# Save all test-cases (not only the unique ones) by appending the current time-stamp to the filenames (default: false)
save_all = false

[afl]
# Target compilation directory,
# (default: "" ["trident-tests/fuzz_tests/fuzzing/afl/afl_target"]).
# To not clash with cargo build's default target directory.
cargo_target_dir = ""
# AFL working input directory,
# (default: "" ["trident-tests/fuzz_tests/fuzzing/afl/afl_workspace/in"]).
afl_workspace_in = ""
# AFL working output directory,
# (default: "" ["trident-tests/fuzz_tests/fuzzing/afl/afl_workspace/out"]).
afl_workspace_out = ""
# fuzz for an approx. no. of total executions then terminate
# Note: not precise and can have several more executions.
# (default: 0 [no limit]).
execs = 0
# fuzz for a specified time then terminate (fuzz time only!)
# (default: 0 [no limit]).
seconds = 0

[[afl.seeds]]
# Filename under which the test input is generated.
# The location of file is afl_workspace_in directory.
# (default: "" ["trident-seed"]).
file_name = "trident-seed"
# String used as seed.
# (default: "" ["0"]).
seed = ""
# If the file already exists at specific location,
# select if override.
# (default: false).
override_file = false
# Number of randomly generated bytes.
# (default: 0).
bytes_count = 20


[fuzz]
# Allow processing of duplicate transactions. Setting to true might speed up fuzzing but can cause false positive crashes (default: false)
allow_duplicate_txs = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ idl-build = ["anchor-lang/idl-build"]
trident-fuzzing = ["dep:trident-fuzz"]

[dependencies]
trident-derive-accounts-snapshots = { path = "../../../../../crates/fuzz/derive/accounts_snapshots" }
trident-fuzz = { path = "../../../../../crates/fuzz", optional = true }
anchor-lang = "0.30.1"

[dependencies.trident-derive-accounts-snapshots]
path = "../../../../../crates/fuzz/derive/accounts_snapshots"

[dependencies.trident-fuzz]
path = "../../../../../crates/fuzz"
optional = true
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use solana_sdk::native_token::LAMPORTS_PER_SOL;
use trident_client::fuzzing::*;

use arbitrary_custom_types_4::trident_fuzz_initialize_snapshot::InitializeAlias;
use arbitrary_custom_types_4::trident_fuzz_update_snapshot::UpdateAlias;
/// Link the relevant Account Context Alias from the program.
/// Aliases are generated by the `AccountsSnapshots` macro.
type InitializeSnapshot<'info> = InitializeAlias<'info>;
type UpdateSnapshot<'info> = UpdateAlias<'info>;
use arbitrary_custom_types_4::trident_fuzz_initialize_snapshot::InitializeAlias;
use arbitrary_custom_types_4::trident_fuzz_update_snapshot::UpdateAlias;
/// FuzzInstruction contains all available Instructions.
/// Below, the instruction arguments (accounts and data) are defined.
#[derive(Arbitrary, DisplayIx, FuzzTestExecutor)]
pub enum FuzzInstruction {
Initialize(Initialize),
Expand All @@ -21,6 +24,11 @@ pub struct InitializeAccounts {
pub user: AccountId,
pub _system_program: AccountId,
}
/// Custom data types must derive `Debug` and `Arbitrary`.
/// To do this, redefine the type in the fuzz test and implement the `From`
/// trait
/// to convert it into the type defined in the program.
/// For more details, see: https://ackee.xyz/trident/docs/dev/features/arbitrary-data/#custom-data-types
#[derive(Arbitrary, Debug)]
pub struct InitializeData {}
#[derive(Arbitrary, Debug)]
Expand All @@ -33,18 +41,29 @@ pub struct UpdateAccounts {
pub counter: AccountId,
pub authority: AccountId,
}
/// Custom data types must derive `Debug` and `Arbitrary`.
/// To do this, redefine the type in the fuzz test and implement the `From`
/// trait
/// to convert it into the type defined in the program.
/// For more details, see: https://ackee.xyz/trident/docs/dev/features/arbitrary-data/#custom-data-types
#[derive(Arbitrary, Debug)]
pub struct UpdateData {
pub input: InputUpdatePrameters,
pub variant: InputUpdateVariant,
pub input: InputUpdatePrametersTrident,
pub variant: InputUpdateVariantTrident,
}
///IxOps implementation for `Initialize` with all required functions.
impl<'info> IxOps<'info> for Initialize {
type IxData = arbitrary_custom_types_4::instruction::Initialize;
type IxAccounts = FuzzAccounts;
type IxSnapshot = InitializeSnapshot<'info>;
/// Definition of the program ID that the Instruction is associated with.
fn get_program_id(&self) -> solana_sdk::pubkey::Pubkey {
arbitrary_custom_types_4::ID
}
/// Definition of the Instruction data.
/// Use randomly generated data from the fuzzer using `self.data.arg_name`
/// or customize the data as needed.
/// For more details, visit: https://ackee.xyz/trident/docs/dev/features/fuzz-instructions/#get-data
fn get_data(
&self,
_client: &mut impl FuzzClient,
Expand All @@ -53,6 +72,12 @@ impl<'info> IxOps<'info> for Initialize {
let data = arbitrary_custom_types_4::instruction::Initialize {};
Ok(data)
}
/// Definition of of the accounts required by the Instruction.
/// To utilize accounts stored in `FuzzAccounts`, use
/// `fuzz_accounts.account_name.get_or_create_account()`.
/// If no signers are required, leave the vector empty.
/// For AccountMetas use <program>::accounts::<corresponding_metas>
/// For more details, see: https://ackee.xyz/trident/docs/dev/features/fuzz-instructions/#get-accounts
fn get_accounts(
&self,
client: &mut impl FuzzClient,
Expand All @@ -78,13 +103,19 @@ impl<'info> IxOps<'info> for Initialize {
Ok((vec![user, counter], acc_meta))
}
}
///IxOps implementation for `Update` with all required functions.
impl<'info> IxOps<'info> for Update {
type IxData = arbitrary_custom_types_4::instruction::Update;
type IxAccounts = FuzzAccounts;
type IxSnapshot = UpdateSnapshot<'info>;
/// Definition of the program ID that the Instruction is associated with.
fn get_program_id(&self) -> solana_sdk::pubkey::Pubkey {
arbitrary_custom_types_4::ID
}
/// Definition of the Instruction data.
/// Use randomly generated data from the fuzzer using `self.data.arg_name`
/// or customize the data as needed.
/// For more details, visit: https://ackee.xyz/trident/docs/dev/features/fuzz-instructions/#get-data
fn get_data(
&self,
_client: &mut impl FuzzClient,
Expand All @@ -96,6 +127,12 @@ impl<'info> IxOps<'info> for Update {
let data = arbitrary_custom_types_4::instruction::Update { input, variant };
Ok(data)
}
/// Definition of of the accounts required by the Instruction.
/// To utilize accounts stored in `FuzzAccounts`, use
/// `fuzz_accounts.account_name.get_or_create_account()`.
/// If no signers are required, leave the vector empty.
/// For AccountMetas use <program>::accounts::<corresponding_metas>
/// For more details, see: https://ackee.xyz/trident/docs/dev/features/fuzz-instructions/#get-accounts
fn get_accounts(
&self,
client: &mut impl FuzzClient,
Expand All @@ -120,8 +157,8 @@ impl<'info> IxOps<'info> for Update {
Ok((vec![user], acc_meta))
}
}
#[doc = r" Use AccountsStorage<T> where T can be one of:"]
#[doc = r" Keypair, PdaStore, TokenStore, MintStore, ProgramStore"]
/// Use AccountsStorage<T> where T can be one of:
/// Keypair, PdaStore, TokenStore, MintStore, ProgramStore
#[derive(Default)]
pub struct FuzzAccounts {
user: AccountsStorage<Keypair>,
Expand All @@ -135,32 +172,36 @@ pub struct FuzzAccounts {
// -------------------------------------------------------------------
// Use arbitrary section
#[derive(Arbitrary, Debug, Clone, Copy)]
pub struct InputUpdatePrameters {
pub struct InputUpdatePrametersTrident {
pub input1: u8,
pub input2: u8,
}

#[derive(Arbitrary, Debug, Clone, Copy)]
pub enum InputUpdateVariant {
pub enum InputUpdateVariantTrident {
UpdateVariant1,
UpdateVariant2,
}

impl std::convert::From<InputUpdatePrameters> for arbitrary_custom_types_4::InputUpdatePrameters {
fn from(val: InputUpdatePrameters) -> Self {
impl std::convert::From<InputUpdatePrametersTrident>
for arbitrary_custom_types_4::InputUpdatePrameters
{
fn from(val: InputUpdatePrametersTrident) -> Self {
arbitrary_custom_types_4::InputUpdatePrameters {
input1: val.input1,
input2: val.input2,
}
}
}
impl std::convert::From<InputUpdateVariant> for arbitrary_custom_types_4::InputUpdateVariant {
fn from(val: InputUpdateVariant) -> Self {
impl std::convert::From<InputUpdateVariantTrident>
for arbitrary_custom_types_4::InputUpdateVariant
{
fn from(val: InputUpdateVariantTrident) -> Self {
match val {
InputUpdateVariant::UpdateVariant1 => {
InputUpdateVariantTrident::UpdateVariant1 => {
arbitrary_custom_types_4::InputUpdateVariant::UpdateVariant1
}
InputUpdateVariant::UpdateVariant2 => {
InputUpdateVariantTrident::UpdateVariant2 => {
arbitrary_custom_types_4::InputUpdateVariant::UpdateVariant2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ use fuzz_instructions::Initialize;
use fuzz_instructions::Update;
use trident_client::fuzzing::*;
mod fuzz_instructions;

use arbitrary_custom_types_4::entry as entry_arbitrary_custom_types_4;
use arbitrary_custom_types_4::ID as PROGRAM_ID_ARBITRARY_CUSTOM_TYPES_4;
use fuzz_instructions::FuzzInstruction;

const PROGRAM_NAME_ARBITRARY_CUSTOM_TYPES_4: &str = "arbitrary_custom_types_4";

struct MyFuzzData;

/// Define instruction sequences for invocation.
/// `pre_ixs` runs at the start, `ixs` in the middle, and `post_ixs` at the end.
/// For example, to call `InitializeFn` at the start of each fuzzing iteration:
/// ```
/// fn pre_ixs(u: &mut arbitrary::Unstructured) ->
/// arbitrary::Result<Vec<FuzzInstruction>> {
/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?);
/// Ok(vec![init])
/// }
/// ```
/// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences
impl FuzzDataBuilder<FuzzInstruction> for MyFuzzData {
fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Vec<FuzzInstruction>> {
let init = FuzzInstruction::Initialize(Initialize::arbitrary(u)?);
let update = FuzzInstruction::Update(Update::arbitrary(u)?);
Ok(vec![init, update])
}
}

/// `fn fuzz_iteration` runs during every fuzzing iteration.
/// Modification is not required.
fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(
fuzz_data: FuzzData<T, U>,
config: &Config,
Expand All @@ -28,16 +36,12 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(
&PROGRAM_ID_ARBITRARY_CUSTOM_TYPES_4,
processor!(convert_entry!(entry_arbitrary_custom_types_4)),
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_arbitrary_custom_types_4], config)
.unwrap();

let _ = fuzz_data.run_with_runtime(&mut client, config);
}

fn main() {
let config = Config::new();

fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; });
}
1 change: 0 additions & 1 deletion examples/fuzz-tests/arbitrary-limit-inputs-5/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ node_modules
test-ledger
.yarn
trident-tests/fuzz_tests/fuzzing/honggfuzz/hfuzz_target
trident-tests/fuzz_tests/fuzzing/afl/afl_target
Loading

0 comments on commit 5914726

Please sign in to comment.