Skip to content

Commit ed3b441

Browse files
committed
Add cargo-fuzz target for DynamicAvx2Searcher
Add fuzzing to the project via cargo-fuzz and a target to test DynamicAvx2Searcher.
1 parent b69061b commit ed3b441

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

fuzz/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
corpus
3+
artifacts
4+
coverage

fuzz/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "sliceslice-fuzz"
3+
version = "0.0.0"
4+
publish = false
5+
edition = "2021"
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[dependencies]
11+
arbitrary = { version = "1.3.0", features = ["derive"] }
12+
libfuzzer-sys = "0.4"
13+
sliceslice = { version = "0.4.1", path = ".." }
14+
15+
# Prevent this from interfering with workspaces
16+
[workspace]
17+
members = ["."]
18+
19+
[profile.release]
20+
debug = 1
21+
22+
[[bin]]
23+
name = "dynamic_avx2"
24+
path = "fuzz_targets/dynamic_avx2.rs"
25+
test = false
26+
doc = false

fuzz/fuzz_targets/dynamic_avx2.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![no_main]
2+
3+
use arbitrary::{Arbitrary, Unstructured};
4+
use libfuzzer_sys::fuzz_target;
5+
use sliceslice::x86::DynamicAvx2Searcher;
6+
7+
#[derive(arbitrary::Arbitrary, Debug)]
8+
struct FuzzInput<'a> {
9+
needle: &'a [u8],
10+
haystack: &'a [u8],
11+
position: usize,
12+
}
13+
14+
fuzz_target!(|data: &[u8]| {
15+
let Ok(mut input) = FuzzInput::arbitrary(&mut Unstructured::new(data)) else {
16+
return;
17+
};
18+
19+
// This is a documented panic, so avoid it
20+
if !input.needle.is_empty() {
21+
input.position %= input.needle.len();
22+
}
23+
24+
let result = unsafe {
25+
let searcher = DynamicAvx2Searcher::with_position(input.needle, input.position);
26+
searcher.search_in(input.haystack)
27+
};
28+
29+
let expected = match input.needle.len() {
30+
0 => true,
31+
len => input.haystack.windows(len).any(|w| w == input.needle),
32+
};
33+
34+
assert_eq!(result, expected);
35+
});

0 commit comments

Comments
 (0)