diff --git a/crates/nu-parser/fuzz/Cargo.toml b/crates/nu-parser/fuzz/Cargo.toml index 45216accc9..3966ba83ef 100644 --- a/crates/nu-parser/fuzz/Cargo.toml +++ b/crates/nu-parser/fuzz/Cargo.toml @@ -10,6 +10,7 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" nu-protocol = { path = "../../nu-protocol" } +nu-cmd-lang = { path = "../../nu-cmd-lang" } [dependencies.nu-parser] @@ -26,4 +27,10 @@ debug = 1 name = "parse" path = "fuzz_targets/parse.rs" test = false -doc = false \ No newline at end of file +doc = false + +[[bin]] +name = "parse_with_keywords" +path = "fuzz_targets/parse_with_keywords.rs" +test = false +doc = false diff --git a/crates/nu-parser/fuzz/README.md b/crates/nu-parser/fuzz/README.md index 131fb50237..ffad6e0928 100644 --- a/crates/nu-parser/fuzz/README.md +++ b/crates/nu-parser/fuzz/README.md @@ -4,6 +4,10 @@ # Quick start guide - Install cargo-fuzz by `cargo install cargo-fuzz` -- Run `gather_seeds.nu` for preparing the initial seeds corpus -- Make output directory `mkdir out` -- Run the fuzzer with `cargo fuzz run parse out seeds` +- Run `gather_seeds.nu` for preparing the initial seeds corpus. This pulls `.nu` files in the nushell repository as checked out and uses them as a starting of point. You can add additional files to increase diversity. +- Make an output directory `mkdir out` +- Run the fuzzer with `cargo fuzz run parse out seeds` where `parse` is the name of the target + +# Targets +- `parse` just pulls in `nu-parser` and reaches the lexing and parsing logic. No command gets executed. +- `parse_with_keywords` also loads `nu-cmd-lang` providing the command implementations for the core keywords. This permits the fuzzer to reach more code paths as some parts depend on the availability of those declarations. This may also execute the const eval code paths of the keyword commands. As of now this command set should not have negative side effects upon const eval. The overall code is not executed by this target. diff --git a/crates/nu-parser/fuzz/fuzz_targets/parse_with_keywords.rs b/crates/nu-parser/fuzz/fuzz_targets/parse_with_keywords.rs new file mode 100644 index 0000000000..3dfd3accf1 --- /dev/null +++ b/crates/nu-parser/fuzz/fuzz_targets/parse_with_keywords.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; + +use nu_cmd_lang::create_default_context; +use nu_parser::*; +use nu_protocol::engine::StateWorkingSet; + +fuzz_target!(|data: &[u8]| { + let engine_state = create_default_context(); + let mut working_set = StateWorkingSet::new(&engine_state); + + let _block = parse(&mut working_set, None, &data, true); +});