Skip to content

Commit

Permalink
🛠️ Improve error formatting
Browse files Browse the repository at this point in the history
Introduce a `native_ui` function to handle non-TUI `ziggy_fuzz` executions. Enhance error messages with optional backtrace info for better debugging.

Signed-off-by: Kevin Valerio <kevin@srlabs.de>
  • Loading branch information
kevin-valerio committed Sep 6, 2024
1 parent a48cf1b commit 16fb88b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
18 changes: 15 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ pub mod ziggy;
/// # Example
/// `eprintln!("{}", format_error(e));`
pub fn format_error(e: anyhow::Error) -> String {
let mut message = format!("\n{}: {}\n", "Phink got an error...".red().bold(), e);
let maybe_backtrace = if std::env::var("RUST_BACKTRACE").is_err() {
format!(
"\nUse {} to make it more verbose",
"RUST_BACKTRACE=1".italic().yellow()
)
} else {
"".to_string()
};

let mut message = format!(
"\n{}: {e}\n\n{maybe_backtrace}\n",
"Phink got an error...".red().bold(),
);

if e.backtrace().status() == BacktraceStatus::Captured {
message = format!(
"{}\n{}\n{}\n",
"{}\n{}\n{}",
message,
"Backtrace ->".yellow(),
e.backtrace()
Expand All @@ -29,7 +41,7 @@ pub fn format_error(e: anyhow::Error) -> String {

let mut source = e.source();
while let Some(cause) = source {
message = format!("{}\n\n{} {}", message, "Caused by".cyan().bold(), cause);
message = format!("{message}\n\n{} {cause}", "Caused by".cyan().bold());
source = cause.source();
}

Expand Down
41 changes: 34 additions & 7 deletions src/cli/ziggy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::cli::{
},
ui,
};
use anyhow::bail;
use std::{
io::{
self,
Expand Down Expand Up @@ -101,25 +102,53 @@ impl ZiggyConfig {
self.build_llvm_allowlist()?;
"build"
}
ZiggyCommand::Fuzz(..) => "fuzz",
}
.parse()?;

match command {
ZiggyCommand::Fuzz(ui) => {
if ui {
ui::tui::initialize_tui().unwrap();
} else {
self.native_ui(args, env, ziggy_command)?;
}
"fuzz"
}
_ => {}
}
.parse()?;

Ok(())
}

fn native_ui(
&self,
args: Vec<String>,
env: Vec<(String, String)>,
ziggy_command: String,
) -> anyhow::Result<()> {
use std::io::BufRead;
let mut binding = Command::new("cargo");
let command_builder = binding
.arg("ziggy")
.arg(ziggy_command)
.env(FromZiggy.to_string(), "1")
.env(AflForkServerTimeout.to_string(), AFL_FORKSRV_INIT_TMOUT)
.env(AflDebug.to_string(), AFL_DEBUG)
.stdout(Stdio::null())
.stderr(Stdio::null());
.stdout(Stdio::piped());

let mut ziggy_child = command_builder.spawn()?;

if let Some(stdout) = ziggy_child.stdout.take() {
let reader = io::BufReader::new(stdout);
for line in reader.lines() {
println!("{}", line?);
}
}

let status = ziggy_child.wait()?;
if !status.success() {
bail!("`cargo ziggy` failed ({})", status);
}
self.with_allowlist(command_builder)?;

// If there are additional arguments, pass them to the command
Expand All @@ -135,8 +164,6 @@ impl ZiggyConfig {
/// # Arguments
///
/// * `command_builder`: The prepared command to which we'll add the AFL ALLOWLIST
///
/// returns: Result<()>
fn with_allowlist(&self, command_builder: &mut Command) -> anyhow::Result<()> {
if cfg!(not(target_os = "macos")) {
let allowlist = PhinkFiles::new(self.config.fuzz_output.to_owned().unwrap_or_default())
Expand All @@ -148,7 +175,7 @@ impl ZiggyConfig {

pub fn ziggy_fuzz(&self) -> anyhow::Result<()> {
let fuzzoutput = &self.config.fuzz_output;
let dict = PhinkFiles::new(fuzzoutput.clone().unwrap_or_default()).path(DictPath);
let dict = PhinkFiles::new(fuzzoutput.to_owned().unwrap_or_default()).path(DictPath);

let build_args = if !self.config.use_honggfuzz {
vec!["--no-honggfuzz".parse()?]
Expand Down

0 comments on commit 16fb88b

Please sign in to comment.