Skip to content

Commit

Permalink
fzf: add fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 12, 2023
1 parent a4c8040 commit c61eb61
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
corpus
artifacts
121 changes: 121 additions & 0 deletions fuzz/Cargo.lock

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

23 changes: 23 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "norm-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
norm = { path = "..", features = ["fzf-v1", "fzf-v2"] }
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

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

use libfuzzer_sys::arbitrary::{self, Arbitrary};
use libfuzzer_sys::fuzz_target;
use norm::fzf::{FzfParser, FzfScheme, FzfV1, FzfV2};
use norm::{CaseSensitivity, Metric};

#[derive(Arbitrary, Copy, Clone, Debug)]
struct Query<'a>(&'a str);

#[derive(Arbitrary, Clone, Debug)]
struct Candidate<'a>(&'a str);

fn with_opts<F>(mut fun: F)
where
F: FnMut(CaseSensitivity, bool, FzfScheme),
{
for case_sensitivity in [
CaseSensitivity::Sensitive,
CaseSensitivity::Insensitive,
CaseSensitivity::Smart,
] {
for normalization in [true, false] {
for scheme in
[FzfScheme::Default, FzfScheme::Path, FzfScheme::History]
{
fun(case_sensitivity, normalization, scheme)
}
}
}
}

fuzz_target!(|data: (Query, Candidate)| {
let (Query(query), Candidate(candidate)) = data;

let mut parser = FzfParser::new();

let query = parser.parse(query);

let mut fzf_v1 = FzfV1::new();

let mut fzf_v2 = FzfV2::new();

with_opts(|case_sensitivity, normalization, scheme| {
fzf_v1 = core::mem::take(&mut fzf_v1)
.with_case_sensitivity(case_sensitivity)
.with_normalization(normalization)
.with_scoring_scheme(scheme);

if let Some(mach) = fzf_v1.distance(query, candidate) {
for range in mach.matched_ranges() {
let _s = &candidate[range.clone()];
}
}

fzf_v2 = core::mem::take(&mut fzf_v2)
.with_case_sensitivity(case_sensitivity)
.with_normalization(normalization)
.with_scoring_scheme(scheme);

if let Some(mach) = fzf_v2.distance(query, candidate) {
for range in mach.matched_ranges() {
let _s = &candidate[range.clone()];
}
}
});
});

0 comments on commit c61eb61

Please sign in to comment.