Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load Config before fuzzing loop + Implement logic dispatch within the FuzzExecutor macro #204

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ incremented upon a breaking change and the patch version will be incremented for

**Changed**

- impr/ fuzz flags are read at start of fuzzing session from Config instead of env variable and transaction dispatch was added to increase FuzzTestExecutor readability ([204](https://github.com/Ackee-Blockchain/trident/pull/204))
- impr/ allow various instructions to be generated in case of multiple programs in the Anchor workspace ([200](https://github.com/Ackee-Blockchain/trident/pull/200))
- feat/ option to add account into Fuzz Test environment with base64 data ([197](https://github.com/Ackee-Blockchain/trident/pull/197))
- impr/ instead of parsing source code and creating our IDL, read anchor IDL ([198](https://github.com/Ackee-Blockchain/trident/pull/196))
Expand Down
13 changes: 3 additions & 10 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pretty_assertions = "1.1.0"
[dependencies]
# TRIDENT
trident-derive-displayix = { path = "../fuzz/derive/display_ix", version = "0.0.2" }
trident-derive-fuzz-deserialize = { path = "../fuzz/derive/fuzz_deserialize", 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" }

Expand Down
33 changes: 30 additions & 3 deletions crates/client/src/cleaner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::Config;
use anyhow::Context;
use fehler::{throw, throws};
use std::{
io,
env, io,
path::{Path, PathBuf},
};
use thiserror::Error;
Expand All @@ -15,6 +15,8 @@ pub enum Error {
Io(#[from] io::Error),
#[error("Cannot find the Anchor.toml file to locate the root folder")]
BadWorkspace,
#[error("{0:?}")]
Anyhow(#[from] anyhow::Error),
}

pub struct Cleaner;
Expand All @@ -31,7 +33,7 @@ impl Cleaner {
}
#[throws]
pub async fn clean_target(&self) {
let root = match Config::discover_root() {
let root = match discover_root() {
Ok(root) => root,
Err(_) => throw!(Error::BadWorkspace),
};
Expand Down Expand Up @@ -60,3 +62,28 @@ impl Cleaner {
}
}
}

/// Tries to find the root directory with the `Anchor.toml` file.
/// Throws an error when there is no directory with the `Anchor.toml` file
pub fn discover_root() -> Result<PathBuf, Error> {
let current_dir = env::current_dir()?;
let mut dir = Some(current_dir.as_path());
while let Some(cwd) = dir {
for file in std::fs::read_dir(cwd)
.with_context(|| format!("Error reading the directory with path: {}", cwd.display()))?
{
let path = file
.with_context(|| {
format!("Error reading the directory with path: {}", cwd.display())
})?
.path();
if let Some(filename) = path.file_name() {
if filename.to_str() == Some(ANCHOR_TOML) {
return Ok(PathBuf::from(cwd));
}
}
}
dir = cwd.parent();
}
throw!(Error::BadWorkspace)
}
23 changes: 3 additions & 20 deletions crates/client/src/commander.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::config::Config;
use fehler::{throw, throws};
use std::path::PathBuf;
use std::process;
Expand All @@ -9,6 +8,7 @@ use tokio::{
process::{Child, Command},
signal,
};
use trident_fuzz::config::Config;

use crate::constants::*;
use tokio::io::AsyncBufReadExt;
Expand Down Expand Up @@ -86,10 +86,6 @@ impl Commander {

let genesis_folder = PathBuf::from(self.root.to_string()).join("trident-genesis");

let rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();

let rustflags = config.get_rustflags_args(rustflags);

let mut fuzz_args = config.get_honggfuzz_args(hfuzz_run_args);

// let cargo_target_dir = std::env::var("CARGO_TARGET_DIR").unwrap_or_default();
Expand Down Expand Up @@ -118,7 +114,7 @@ impl Commander {
}
}

match rustflags.contains("fuzzing_with_stats") {
match config.get_fuzzing_with_stats() {
true => {
// enforce keep output to be true
fuzz_args.push_str("--keep_output");
Expand All @@ -127,7 +123,6 @@ impl Commander {
.env("HFUZZ_RUN_ARGS", fuzz_args)
.env("CARGO_TARGET_DIR", cargo_target_dir)
.env("HFUZZ_WORKSPACE", hfuzz_workspace)
.env("RUSTFLAGS", rustflags)
.arg("hfuzz")
.arg("run")
.arg(target)
Expand All @@ -141,7 +136,6 @@ impl Commander {
.env("HFUZZ_RUN_ARGS", fuzz_args)
.env("CARGO_TARGET_DIR", cargo_target_dir)
.env("HFUZZ_WORKSPACE", hfuzz_workspace)
.env("RUSTFLAGS", rustflags)
.arg("hfuzz")
.arg("run")
.arg(target)
Expand Down Expand Up @@ -177,11 +171,7 @@ impl Commander {

let mut fuzz_args = config.get_honggfuzz_args(hfuzz_run_args);

let rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();

let rustflags = config.get_rustflags_args(rustflags);

match rustflags.contains("fuzzing_with_stats") {
match config.get_fuzzing_with_stats() {
true => {
// enforce keep output to be true
fuzz_args.push_str("--keep_output");
Expand All @@ -190,7 +180,6 @@ impl Commander {
.env("HFUZZ_RUN_ARGS", fuzz_args)
.env("CARGO_TARGET_DIR", cargo_target_dir)
.env("HFUZZ_WORKSPACE", hfuzz_workspace)
.env("RUSTFLAGS", rustflags)
.arg("hfuzz")
.arg("run")
.arg(target)
Expand All @@ -204,7 +193,6 @@ impl Commander {
.env("HFUZZ_RUN_ARGS", fuzz_args)
.env("CARGO_TARGET_DIR", cargo_target_dir)
.env("HFUZZ_WORKSPACE", hfuzz_workspace)
.env("RUSTFLAGS", rustflags)
.arg("hfuzz")
.arg("run")
.arg(target)
Expand Down Expand Up @@ -336,15 +324,10 @@ impl Commander {
let cargo_target_dir = std::env::var("CARGO_TARGET_DIR")
.unwrap_or_else(|_| config.get_env_arg("CARGO_TARGET_DIR"));

let rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();

let rustflags = config.get_rustflags_args(rustflags);

// using exec rather than spawn and replacing current process to avoid unflushed terminal output after ctrl+c signal
std::process::Command::new("cargo")
.env("GENESIS_FOLDER", genesis_folder)
.env("CARGO_TARGET_DIR", cargo_target_dir)
.env("RUSTFLAGS", rustflags)
.arg("hfuzz")
.arg("run-debug")
.arg(target)
Expand Down
Loading
Loading