Skip to content

Commit

Permalink
Merge pull request #23 from FuzzingLabs/dev/antonin
Browse files Browse the repository at this point in the history
Update detectors management
  • Loading branch information
Rog3rSm1th authored Sep 19, 2024
2 parents 7fb790e + edfeb9c commit 148c275
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ When running the detectors we can generate test cases for each path in the funct


```
cargo run -- -f ./examples/sierra/fib_array.sierra -d
[...]
cargo run -- -f ./examples/sierra/symbolic_execution_test.sierra -d --detector-names tests
[Informational] Tests generator
[Testing] Tests generator
- symbolic::symbolic::symbolic_execution_test :
- v0: 102, v1: 0, v2: 0, v3: 0
- v0: 103, v1: 0, v2: 0, v3: 0
Expand Down
29 changes: 23 additions & 6 deletions bin/sierra-decompiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio;

use cairo_lang_starknet_classes::contract_class::ContractClass;
use sierra_analyzer_lib::decompiler::decompiler::Decompiler;
use sierra_analyzer_lib::detectors::detector::DetectorType;
use sierra_analyzer_lib::detectors::get_detectors;
use sierra_analyzer_lib::graph::graph::save_svg_graph_to_file;
use sierra_analyzer_lib::provider::NetworkConfig;
Expand Down Expand Up @@ -52,9 +53,13 @@ struct Args {
verbose: bool,

/// Run the detectors
#[clap(short, long)]
#[clap(short = 'd', long)]
detectors: bool,

/// List of detector names to run
#[clap(long, use_value_delimiter = true)]
detector_names: Vec<String>,

/// Remote contract class address
#[clap(long, default_value = "")]
remote: String,
Expand Down Expand Up @@ -107,7 +112,7 @@ async fn main() {
}
// Detectors
else if args.detectors {
handle_detectors(&mut decompiler);
handle_detectors(&mut decompiler, args.detector_names);
}
// Decompiler (default)
else {
Expand Down Expand Up @@ -223,12 +228,22 @@ fn handle_callgraph(args: &Args, decompiler: &mut Decompiler, file_stem: &str) {
}

/// Handle the running of detectors and printing their results
fn handle_detectors(decompiler: &mut Decompiler) {
fn handle_detectors(decompiler: &mut Decompiler, detector_names: Vec<String>) {
let mut detectors = get_detectors();
let mut output = String::new();

// Run all the detectors
// Run the specified detectors
for detector in detectors.iter_mut() {
// Skip TESTING detectors if no specific detector names are provided
if detector_names.is_empty() && detector.detector_type() == DetectorType::TESTING {
continue;
}

// Skip detectors not in the provided names if names are provided
if !detector_names.is_empty() && !detector_names.contains(&detector.id().to_string()) {
continue;
}

let result = detector.detect(decompiler);
if !result.trim().is_empty() {
// Each detector output is formatted like
Expand All @@ -249,6 +264,8 @@ fn handle_detectors(decompiler: &mut Decompiler) {
}
}

// Print the detectors result
println!("{}", output.trim());
// Print the detectors result if not empty
if !output.trim().is_empty() {
println!("{}", output.trim());
}
}
6 changes: 5 additions & 1 deletion lib/src/detectors/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use std::fmt::Debug;
use crate::decompiler::decompiler::Decompiler;

/// Possible types of a detector
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum DetectorType {
INFORMATIONAL,
SECURITY,
TESTING,
}

impl DetectorType {
Expand All @@ -20,6 +21,9 @@ impl DetectorType {

// Security detectors types are blue
DetectorType::SECURITY => "Security".normal().blue(),

// Testing detectors types are yellow
DetectorType::TESTING => "Testing".normal().yellow(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/detectors/functions_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ impl Detector for FunctionsDetector {
if index < total_functions - 1 {
result += "\n";
}

// Append the ANSI reset sequence
result += &"\x1b[0m".to_string();
}
}
result
Expand Down
3 changes: 2 additions & 1 deletion lib/src/detectors/tests_generator_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ impl Detector for TestsGeneratorDetector {
}

/// Returns the type of the detector
/// Detectors in the TESTING category are not displayed by default using the --detector flag
#[inline]
fn detector_type(&self) -> DetectorType {
DetectorType::INFORMATIONAL
DetectorType::TESTING
}

/// Returns the generated unit tests for the function if they exist
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/detectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ fn test_functions_detector() {
// functions names
let functions_names = detector.detect(&mut decompiler);

let expected_output = r#"examples::fib_array::fib
examples::fib_array::fib_inner"#;
let expected_output =
"examples::fib_array::fib\n\u{1b}[0mexamples::fib_array::fib_inner\u{1b}[0m";

assert_eq!(functions_names, expected_output);
}
Expand Down

0 comments on commit 148c275

Please sign in to comment.