Skip to content

Commit

Permalink
SeaOfNodes chapter08: basic fuzzer on random strings
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertObkircher committed Aug 18, 2024
1 parent 840c6ca commit 5521651
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
21 changes: 21 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "simple-rust-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.simple-rust]
path = ".."

[[bin]]
name = "basic"
path = "fuzz_targets/basic.rs"
test = false
doc = false
bench = false
14 changes: 14 additions & 0 deletions fuzz/fuzz_targets/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use simple_rust::datastructures::arena::Arena;
use simple_rust::sea_of_nodes::parser::Parser;
use simple_rust::sea_of_nodes::types::Types;

fuzz_target!(|source: &str| {
let arena = Arena::new();
let mut types = Types::new(&arena);
let mut parser = Parser::new(source, &mut types);
parser.disable_show_graph_println = true;
let _ = parser.parse();
});
10 changes: 7 additions & 3 deletions src/sea_of_nodes/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Parser<'s, 'mt, 't> {

continue_scope: Option<NodeId>,
break_scope: Option<NodeId>,
pub disable_show_graph_println: bool,
}

type ParseErr = String;
Expand Down Expand Up @@ -68,6 +69,7 @@ impl<'s, 'mt, 't> Parser<'s, 'mt, 't> {
x_scopes: vec![],
continue_scope: None,
break_scope: None,
disable_show_graph_println: false,
}
}

Expand Down Expand Up @@ -376,9 +378,11 @@ impl<'s, 'mt, 't> Parser<'s, 'mt, 't> {
/// Dumps out the node graph
pub fn show_graph(&mut self) {
let dot = graph_visualizer::generate_dot_output(self, false).unwrap();
println!("{dot}");
// uncomment to open in browser:
// graph_visualizer::run_graphviz_and_chromium(dot);
if !self.disable_show_graph_println {
println!("{dot}");
// uncomment to open in browser:
// graph_visualizer::run_graphviz_and_chromium(dot);
}
}

pub fn print(&mut self, node: NodeId) -> String {
Expand Down

0 comments on commit 5521651

Please sign in to comment.