Skip to content

Commit

Permalink
add crc32 fuzzer (to find memory issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Feb 16, 2024
1 parent d59834a commit 70cdf34
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ features = ["arbitrary-derive"]
libc = "0.2.151"
libz-ng-sys = "1.1.12"
libloading = "0.8.1"
crc32fast = "1.3.2"

[dependencies.zlib-rs]
path = "../zlib-rs"
Expand Down Expand Up @@ -60,3 +61,9 @@ name = "end_to_end"
path = "fuzz_targets/end_to_end.rs"
test = false
doc = false

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

//! the tests provide good coverage, the purose of this fuzzer is to
//! discover memory safety issues in the SIMD implementations.

use libfuzzer_sys::fuzz_target;

use zlib_rs::deflate::DeflateConfig;
use zlib_rs::inflate::InflateConfig;
use zlib_rs::{Flush, ReturnCode};

use std::ffi::{c_char, c_int, c_uint};

fuzz_target!(|input: (Vec<u8>, u32)| {
let (input, start) = input;

{
let expected = {
let mut h = crc32fast::Hasher::new_with_initial(start);
h.update(&input[..]);
h.finalize()
};

let actual = zlib_rs::crc32::crc32(input.as_slice(), start);

assert_eq!(expected, actual);
}

{
let mut dst = [0; 1 << 16];

let expected = {
let mut h = crc32fast::Hasher::new_with_initial(0);
h.update(&input[..]);
h.finalize()
};

let actual = zlib_rs::crc32::crc32_copy(&mut dst[..input.len()], input.as_slice());

assert_eq!(expected, actual);

assert_eq!(input, &dst[..input.len()]);
}
});

0 comments on commit 70cdf34

Please sign in to comment.