diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff4c62a..0cf67f2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ incremented upon a breaking change and the patch version will be incremented for **Added** +- add pre_sequence!, middle_sequence! and post_sequence! for easier sequence definition ([220](https://github.com/Ackee-Blockchain/trident/pull/220)) - add/ add support for Clock sysvar manipulations with the client(i.e. warp to slot/epoch and forward in time) ([217](https://github.com/Ackee-Blockchain/trident/pull/217)) ## [0.8.0] - 2024-10-21 diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index fdd41e69..8118daba 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -36,6 +36,9 @@ pub mod fuzzing { /// trident macros pub use trident_fuzz::convert_entry; pub use trident_fuzz::fuzz_trident; + pub use trident_fuzz::middle_sequence; + pub use trident_fuzz::post_sequence; + pub use trident_fuzz::pre_sequence; pub use trident_fuzz::show_account; pub use trident_fuzz::*; diff --git a/crates/client/src/source_code_generators/test_fuzz_generator.rs b/crates/client/src/source_code_generators/test_fuzz_generator.rs index 8f521f1e..1ca139d4 100644 --- a/crates/client/src/source_code_generators/test_fuzz_generator.rs +++ b/crates/client/src/source_code_generators/test_fuzz_generator.rs @@ -17,20 +17,20 @@ pub fn generate_source_code(idl_instructions: &[Idl]) -> String { #(#program_names)* - struct MyFuzzData; + struct InstructionsSequence; /// 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: + /// `pre` runs at the start, `middle` in the middle, and `post` at the end. + /// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during each fuzzing iteration: /// ``` - /// fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - /// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); - /// Ok(vec![init]) - /// } + /// impl FuzzDataBuilder for InstructionsSequence { + /// pre_sequence!(InitializeFn,UpdateFn); + /// middle_sequence!(WithdrawFn); + ///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/latest/features/instructions-sequences/#instructions-sequences - impl FuzzDataBuilder for MyFuzzData {} + impl FuzzDataBuilder for InstructionsSequence {} /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -47,7 +47,7 @@ pub fn generate_source_code(idl_instructions: &[Idl]) -> String { fn main() { let config = Config::new(); - fuzz_trident!(fuzz_ix: FuzzInstruction, |fuzz_data: MyFuzzData| { + fuzz_trident!(fuzz_ix: FuzzInstruction, |fuzz_data: InstructionsSequence| { fuzz_iteration(fuzz_data,&config); diff --git a/crates/client/tests/expected_source_codes/expected_test_fuzz.rs b/crates/client/tests/expected_source_codes/expected_test_fuzz.rs index ecb51ed6..0fe59e7e 100644 --- a/crates/client/tests/expected_source_codes/expected_test_fuzz.rs +++ b/crates/client/tests/expected_source_codes/expected_test_fuzz.rs @@ -7,19 +7,19 @@ use dummy_example::ID as PROGRAM_ID_DUMMY_EXAMPLE; use fuzz_instructions::FuzzInstruction; const PROGRAM_NAME_DUMMY_2: &str = "dummy_2"; const PROGRAM_NAME_DUMMY_EXAMPLE: &str = "dummy_example"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/latest/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData {} +impl FuzzDataBuilder for InstructionsSequence {} /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. fn fuzz_iteration + std::fmt::Display, U>( @@ -45,5 +45,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/crates/fuzz/src/instructions_sequence.rs b/crates/fuzz/src/instructions_sequence.rs new file mode 100644 index 00000000..6b608f20 --- /dev/null +++ b/crates/fuzz/src/instructions_sequence.rs @@ -0,0 +1,46 @@ +#[macro_export] +macro_rules! pre_sequence { + // Accept a list of FuzzInstruction variants using parentheses `()` + ($($ix_variant:ident),* $(,)?) => { + fn pre_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { + let mut instructions = Vec::new(); + $( + let ix = FuzzInstruction::$ix_variant($ix_variant::arbitrary(_u)?); + instructions.push(ix); + )* + Ok(instructions) + } + }; +} + +#[macro_export] +macro_rules! middle_sequence { + // Accept a list of FuzzInstruction variants (which may include duplicates) + ($($ix_variant:ident),* $(,)?) => { + fn ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { + #[allow(unused_mut)] + let mut instructions = Vec::new(); + $( + let ix = FuzzInstruction::$ix_variant($ix_variant::arbitrary(_u)?); + instructions.push(ix); + )* + Ok(instructions) + } + }; +} + +#[macro_export] +macro_rules! post_sequence { + // Accept a list of FuzzInstruction variants (which may include duplicates) + ($($ix_variant:ident),* $(,)?) => { + fn post_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { + #[allow(unused_mut)] + let mut instructions = Vec::new(); + $( + let ix = FuzzInstruction::$ix_variant($ix_variant::arbitrary(_u)?); + instructions.push(ix); + )* + Ok(instructions) + } + }; +} diff --git a/crates/fuzz/src/lib.rs b/crates/fuzz/src/lib.rs index 61457585..7fda9222 100644 --- a/crates/fuzz/src/lib.rs +++ b/crates/fuzz/src/lib.rs @@ -10,5 +10,6 @@ pub mod fuzz_client; pub mod fuzz_deserialize; pub mod fuzz_test_executor; pub mod fuzz_trident; +pub mod instructions_sequence; pub mod ix_ops; pub mod transaction_executor; diff --git a/documentation/docs/features/instructions-sequences.md b/documentation/docs/features/instructions-sequences.md index f718302c..404fbf4d 100644 --- a/documentation/docs/features/instructions-sequences.md +++ b/documentation/docs/features/instructions-sequences.md @@ -1,22 +1,52 @@ # Instructions Sequences -Trident allows you to specify Custom Instruction Sequences you would like to execute. +Trident allows you to specify __custom Instruction squences__ you would like to execute. Possible Instruction sequences are split into 3 parts -- pre-Instructions -- Instructions -- post-Instructions +- __pre-Instructions__ +- __Instructions__ +- __post-Instructions__ -For example if you program always needs to start with some kind of Initialization instruction, you can specify this Initialize Instruction in `pre_ixs` as shown in the source code below. +For example if you program always needs to start with some kind of Initialization instruction, you can specify this Initialize Instruction using the `pre_sequence()` macro as shown in the source code below. +```rust +// test_fuzz.rs + +/// ... + +struct InstructionsSequence; +/// Define instruction sequences for invocation. +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: +/// ``` +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} +/// ``` +/// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(InitializeFn); + middle_sequence!(); + post_sequence!(); +} +/// ... + +``` !!! tip - - returning `Ok(vec![])` will result in None Instructions executed in the corresponding part. + - The arguments to the macro are variants of `FuzzInstruction` specified in `fuzz_instructions.rs`. + - Empty macro parameters (such as `middle_sequence!()`), will skip that section, meaning no instructions will be executed during the section. + - If no macro is defined for a section, a random instruction sequence will be generated for the section. + +## Manual trait override +It is not necessary to use the macro as explained above. The trait implementation (i.e., the methods) can be implemented manually, as shown in the code below. This approach allows for greater customization if needed. The rules are the same as described above. ```rust // test_fuzz.rs diff --git a/examples/fuzz-tests/arbitrary-custom-types-4/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/arbitrary-custom-types-4/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index dff43379..f8908d96 100644 --- a/examples/fuzz-tests/arbitrary-custom-types-4/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/arbitrary-custom-types-4/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -6,24 +6,20 @@ 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; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init = FuzzInstruction::Initialize(Initialize::arbitrary(u)?); - let update = FuzzInstruction::Update(Update::arbitrary(u)?); - Ok(vec![init, update]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(Initialize, Update); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -43,5 +39,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/arbitrary-limit-inputs-5/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/arbitrary-limit-inputs-5/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index da70edad..89116464 100644 --- a/examples/fuzz-tests/arbitrary-limit-inputs-5/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/arbitrary-limit-inputs-5/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -6,31 +6,22 @@ use arbitrary_limit_inputs_5::entry as entry_arbitrary_limit_inputs_5; use arbitrary_limit_inputs_5::ID as PROGRAM_ID_ARBITRARY_LIMIT_INPUTS_5; use fuzz_instructions::FuzzInstruction; const PROGRAM_NAME_ARBITRARY_LIMIT_INPUTS_5: &str = "arbitrary_limit_inputs_5"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init_ix = FuzzInstruction::InitVesting(InitVesting::arbitrary(u)?); - - Ok(vec![init_ix]) - } - fn ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let withdraw_ix = FuzzInstruction::WithdrawUnlocked(WithdrawUnlocked::arbitrary(u)?); - Ok(vec![withdraw_ix]) - } - fn post_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(InitVesting); + middle_sequence!(WithdrawUnlocked); + post_sequence!(); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -50,5 +41,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/cpi-metaplex-7/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/cpi-metaplex-7/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index a1d7f2cc..e55b42e9 100644 --- a/examples/fuzz-tests/cpi-metaplex-7/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/cpi-metaplex-7/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -5,29 +5,22 @@ use cpi_metaplex_7::entry as entry_cpi_metaplex_7; use cpi_metaplex_7::ID as PROGRAM_ID_CPI_METAPLEX_7; use fuzz_instructions::FuzzInstruction; const PROGRAM_NAME_CPI_METAPLEX_7: &str = "cpi_metaplex_7"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init = FuzzInstruction::Initialize(Initialize::arbitrary(u)?); - Ok(vec![init]) - } - fn ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } - fn post_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(Initialize); + middle_sequence!(); + post_sequence!(); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -46,5 +39,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/hello_world/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/hello_world/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index 9df4c363..bcd4f41c 100644 --- a/examples/fuzz-tests/hello_world/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/hello_world/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -5,29 +5,22 @@ use fuzz_instructions::FuzzInstruction; use hello_world::entry as entry_hello_world; use hello_world::ID as PROGRAM_ID_HELLO_WORLD; const PROGRAM_NAME_HELLO_WORLD: &str = "hello_world"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); - Ok(vec![init]) - } - fn ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } - fn post_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(InitializeFn); + middle_sequence!(); + post_sequence!(); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -46,5 +39,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/incorrect-integer-arithmetic-3/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/incorrect-integer-arithmetic-3/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index 68d6410a..c3390fc5 100644 --- a/examples/fuzz-tests/incorrect-integer-arithmetic-3/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/incorrect-integer-arithmetic-3/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -5,24 +5,20 @@ use fuzz_instructions::FuzzInstruction; use incorrect_integer_arithmetic_3::entry as entry_incorrect_integer_arithmetic_3; use incorrect_integer_arithmetic_3::ID as PROGRAM_ID_INCORRECT_INTEGER_ARITHMETIC_3; const PROGRAM_NAME_INCORRECT_INTEGER_ARITHMETIC_3: &str = "incorrect_integer_arithmetic_3"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init_ix = FuzzInstruction::InitVesting(InitVesting::arbitrary(u)?); - - Ok(vec![init_ix]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(InitVesting); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -42,5 +38,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/incorrect-ix-sequence-1/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/incorrect-ix-sequence-1/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index 8b0cbe10..eae53e69 100644 --- a/examples/fuzz-tests/incorrect-ix-sequence-1/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/incorrect-ix-sequence-1/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -5,23 +5,20 @@ use fuzz_instructions::FuzzInstruction; use incorrect_ix_sequence_1::entry as entry_incorrect_ix_sequence_1; use incorrect_ix_sequence_1::ID as PROGRAM_ID_INCORRECT_IX_SEQUENCE_1; const PROGRAM_NAME_INCORRECT_IX_SEQUENCE_1: &str = "incorrect_ix_sequence_1"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init_ix = FuzzInstruction::Initialize(Initialize::arbitrary(u)?); - Ok(vec![init_ix]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(Initialize); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -40,5 +37,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/simple-cpi-6/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/simple-cpi-6/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index 4a860ade..63c85243 100644 --- a/examples/fuzz-tests/simple-cpi-6/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/simple-cpi-6/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -8,29 +8,22 @@ use caller::ID as PROGRAM_ID_CALLER; use fuzz_instructions::FuzzInstruction; const PROGRAM_NAME_CALLEE: &str = "callee"; const PROGRAM_NAME_CALLER: &str = "caller"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init_caller = FuzzInstruction::InitializeCaller(InitializeCaller::arbitrary(u)?); - Ok(vec![init_caller]) - } - fn ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } - fn post_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(InitializeCaller); + middle_sequence!(); + post_sequence!(); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -55,5 +48,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/unauthorized-access-2/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/unauthorized-access-2/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index b1637600..96b9a71c 100644 --- a/examples/fuzz-tests/unauthorized-access-2/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/unauthorized-access-2/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -5,24 +5,20 @@ use fuzz_instructions::FuzzInstruction; use unauthorized_access_2::entry as entry_unauthorized_access_2; use unauthorized_access_2::ID as PROGRAM_ID_UNAUTHORIZED_ACCESS_2; const PROGRAM_NAME_UNAUTHORIZED_ACCESS_2: &str = "unauthorized_access_2"; -struct MyFuzzData; +struct InstructionsSequence; /// 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: +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: /// ``` -/// fn pre_ixs(u: &mut arbitrary::Unstructured) -> -/// arbitrary::Result> { -/// let init = FuzzInstruction::InitializeFn(InitializeFn::arbitrary(u)?); -/// Ok(vec![init]) -/// } +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} /// ``` /// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init_ix = FuzzInstruction::Initialize(Initialize::arbitrary(u)?); - - Ok(vec![init_ix]) - } +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(Initialize); } /// `fn fuzz_iteration` runs during every fuzzing iteration. /// Modification is not required. @@ -41,5 +37,5 @@ fn fuzz_iteration + std::fmt::Display, U>( } fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); } diff --git a/examples/fuzz-tests/unchecked-arithmetic-0/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs b/examples/fuzz-tests/unchecked-arithmetic-0/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs index f039188f..8eba1bfd 100644 --- a/examples/fuzz-tests/unchecked-arithmetic-0/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs +++ b/examples/fuzz-tests/unchecked-arithmetic-0/trident-tests/fuzz_tests/fuzz_0/test_fuzz.rs @@ -8,21 +8,21 @@ use unchecked_arithmetic_0::entry as entry_unchecked_arithmetic_0; use unchecked_arithmetic_0::ID as PROGRAM_ID_UNCHECKED_ARITHMETIC_0; const PROGRAM_NAME_UNCHECKED_ARITHMETIC_0: &str = "unchecked_arithmetic_0"; - -struct MyFuzzData; - -impl FuzzDataBuilder for MyFuzzData { - fn pre_ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let init = FuzzInstruction::Initialize(Initialize::arbitrary(u)?); - Ok(vec![init]) - } - fn ixs(u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - let update = FuzzInstruction::Update(Update::arbitrary(u)?); - Ok(vec![update]) - } - fn post_ixs(_u: &mut arbitrary::Unstructured) -> arbitrary::Result> { - Ok(vec![]) - } +struct InstructionsSequence; +/// Define instruction sequences for invocation. +/// `pre` runs at the start, `middle` in the middle, and `post` at the end. +/// For example, to call `InitializeFn`, `UpdateFn` and then `WithdrawFn` during +/// each fuzzing iteration: +/// ``` +/// impl FuzzDataBuilder for InstructionsSequence { +/// pre_sequence!(InitializeFn,UpdateFn); +/// middle_sequence!(WithdrawFn); +///} +/// ``` +/// For more details, see: https://ackee.xyz/trident/docs/dev/features/instructions-sequences/#instructions-sequences +impl FuzzDataBuilder for InstructionsSequence { + pre_sequence!(Initialize); + middle_sequence!(Update); } fn fuzz_iteration + std::fmt::Display, U>( @@ -44,5 +44,5 @@ fn fuzz_iteration + std::fmt::Display, U>( fn main() { let config = Config::new(); - fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data , & config) ; }); + fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : InstructionsSequence | { fuzz_iteration (fuzz_data , & config) ; }); }