Skip to content

Commit

Permalink
🚸 Improve ux during init, fenerate required parts in packages automat…
Browse files Browse the repository at this point in the history
…ically, if code creation fails , do not generate any file
  • Loading branch information
lukacan committed Oct 12, 2024
1 parent 3407bb6 commit 4aa51a6
Show file tree
Hide file tree
Showing 22 changed files with 705 additions and 454 deletions.
56 changes: 22 additions & 34 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub use init::init;

mod clean;
pub use clean::clean;

mod howto;
pub use howto::howto;
6 changes: 4 additions & 2 deletions crates/cli/src/command/fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use anyhow::{bail, Error};

use clap::Subcommand;
Expand Down Expand Up @@ -105,7 +107,7 @@ pub async fn fuzz(subcmd: FuzzCommand) {
}
};

let commander = Commander::with_root(root.clone());
let commander = Commander::with_root(&Path::new(&root).to_path_buf());

match subcmd {
FuzzCommand::Run_Afl { target } => {
Expand Down Expand Up @@ -135,7 +137,7 @@ pub async fn fuzz(subcmd: FuzzCommand) {
}

FuzzCommand::Add => {
let mut generator = TestGenerator::new_with_root(root);
let mut generator = TestGenerator::new_with_root(&root)?;
generator.add_fuzz_test().await?;
show_howto();
}
Expand Down
9 changes: 9 additions & 0 deletions crates/cli/src/command/howto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use anyhow::Error;
use fehler::throws;

use crate::show_howto;

#[throws]
pub fn howto() {
show_howto();
}
19 changes: 16 additions & 3 deletions crates/cli/src/command/init.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
use std::path::Path;

use anyhow::{bail, Error};
use fehler::throws;
use trident_client::___private::TestGenerator;

use crate::{_discover, show_howto};

pub const ANCHOR_TOML: &str = "Anchor.toml";
pub const TRIDENT_TOML: &str = "Trident.toml";

#[throws]
pub async fn init() {
pub async fn init(force: bool) {
// look for Anchor.toml
let root = if let Some(r) = _discover(ANCHOR_TOML)? {
r
} else {
bail!("It does not seem that Anchor is initialized because the Anchor.toml file was not found in any parent directory!");
};

let mut generator: TestGenerator = TestGenerator::new_with_root(root);
let mut generator: TestGenerator = TestGenerator::new_with_root(&root)?;

generator.generate_fuzz().await?;
if force {
generator.initialize().await?;
} else {
let root_path = Path::new(&root).join(TRIDENT_TOML);
if root_path.exists() {
println!("It looks like Trident is already initialized as the Trident.toml was found in {} directory.",root);
} else {
generator.initialize().await?;
}
}

show_howto();
}
39 changes: 27 additions & 12 deletions crates/cli/src/howto.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@


# New fuzz test template successfully added.
# How To start fuzzing.

## To start fuzzing, follow these steps:

- Derive AccountsSnapshots for each account context in the program:
- Initialize ***Trident*** using

Include the following dependencies in the Cargo.toml of each program:
```rust
trident-derive-accounts-snapshots = "0.8.0"
trident-fuzz = { version = "0.8.0", optional = true }
```bash
trident init
```

- Add the fuzzing feature:
```toml
trident-fuzzing = ["dep:trident-fuzz"]
- Derive ***AccountsSnapshots*** for each account context in the program:

```rust
use trident_derive_accounts_snapshots::AccountsSnapshots;

#[derive(AccountsSnapshots, Accounts)]
pub struct InitializeContext<'info> {
// ...
}

```

- Link Account Context Aliases in the `fuzz_instructions.rs` with desired Snapshots
For example:
- Link Account Context Aliases in the ***fuzz_instructions.rs*** with desired Snapshots

```rust
use hello_world::trident_fuzz_initialize_context_snapshot::InitializeContextAlias;
type InitializeFnSnapshot<'info> = InitializeContextAlias<'info>;
```

- Implement the `todo!` placeholders in `fuzz_instructions.rs` based on the provided descriptions.
- Implement the ***todo!*** placeholders in ***fuzz_instructions.rs*** based on the provided descriptions.

- Run fuzzing with ***Honggfuzz*** or ***AFL***

```bash
trident fuzz run-hfuzz
```

```bash
trident fuzz run-afl
```

### For more details, refer to the Trident documentation: https://ackee.xyz/trident/docs/dev/
21 changes: 16 additions & 5 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ struct Cli {

#[derive(Subcommand)]
enum Command {
#[command(about = "Show the HowTo message.")]
How,
#[command(
about = "Initialize Trident in the current Anchor workspace.",
long_about = "Initialize Trident in the current Anchor workspace.\n\
This command creates a new fuzz test template, adds a Trident.toml configuration file,\n\
and updates the Cargo.toml and .gitignore files accordingly to integrate Trident."
override_usage = "\nTrident will skip initialization if Trident.toml already exists.\
\nIf you are sure that you want to proceed with initialization even though the Trident.toml already exists,\
\ncall trident init -f / --force."
)]
Init,
Init {
#[arg(
short,
long,
required = false,
help = "Force Trident Initialization. Trident dependencies will be updated based on the version of Trident."
)]
force: bool,
},
#[command(
about = "Run fuzz subcommands.",
override_usage = "With fuzz subcommands you can add new fuzz test \
Expand All @@ -59,8 +69,9 @@ pub async fn start() {
let cli = Cli::parse();

match cli.command {
Command::How => command::howto()?,
Command::Fuzz { subcmd } => command::fuzz(subcmd).await?,
Command::Init => command::init().await?,
Command::Init { force } => command::init(force).await?,
Command::Clean => command::clean().await?,
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pretty_assertions = "1.1.0"
# TRIDENT
trident-derive-displayix = { path = "../fuzz/derive/display_ix", version = "0.0.2" }
trident-derive-fuzz-test-executor = { path = "../fuzz/derive/fuzz_test_executor", version = "0.0.2" }
trident-derive-accounts-snapshots = { path = "../fuzz/derive/accounts_snapshots", version = "0.0.1" }

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

# ANCHOR
Expand Down
5 changes: 5 additions & 0 deletions crates/client/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trident_fuzz": "0.1.0",
"trident_derive_accounts_snapshots": "0.0.1",
"trident_client": "0.7.0"
}
2 changes: 1 addition & 1 deletion crates/client/src/commander/afl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Commander {
pub async fn run_afl_debug(&self, target: String, crash_file_path: String) {
let config = Config::new();

let crash_file = std::path::Path::new(&self.root as &str).join(crash_file_path);
let crash_file = self.root.join(crash_file_path);

let cargo_target_dir = config.get_afl_cargo_build_dir();

Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/commander/honggfuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Commander {
pub async fn run_hfuzz_debug(&self, target: String, crash_file_path: String) {
let config = Config::new();

let crash_file = std::path::Path::new(&self.root as &str).join(crash_file_path);
let crash_file = self.root.join(crash_file_path);

if !crash_file.try_exists()? {
println!("{ERROR} The crash file [{:?}] not found", crash_file);
Expand Down
Loading

0 comments on commit 4aa51a6

Please sign in to comment.