Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuzzing and a target for DynamicAvx2Searcher #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "sliceslice-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
arbitrary = { version = "1.3.0", features = ["derive"] }
libfuzzer-sys = "0.4"
sliceslice = { version = "0.4.1", path = ".." }

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

[profile.release]
debug = 1

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

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use sliceslice::x86::DynamicAvx2Searcher;

#[derive(Arbitrary, Debug)]
struct FuzzInput<'a> {
needle: &'a [u8],
haystack: &'a [u8],
position: usize,
}

fuzz_target!(|input: FuzzInput<'_>| {
let mut input = input;

// This is a documented panic, so avoid it
if !input.needle.is_empty() {
input.position %= input.needle.len();
}

let result = unsafe {
let searcher = DynamicAvx2Searcher::with_position(input.needle, input.position);
searcher.search_in(input.haystack)
};

let expected = match input.needle.len() {
0 => true,
len => input.haystack.windows(len).any(|w| w == input.needle),
};

assert_eq!(result, expected);
});