From 0701c95f3dc8385d62ab16cb5376086bb50fe52a Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Tue, 30 Nov 2021 16:21:03 +0000 Subject: [PATCH] Add fuzzer for the encoder --- fuzz/.gitignore | 3 +++ fuzz/Cargo.toml | 25 +++++++++++++++++++++++++ fuzz/fuzz_targets/qoi_encode.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/fuzz_targets/qoi_encode.rs diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 0000000..a092511 --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,3 @@ +target +corpus +artifacts diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..6a03228 --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "qoi-fast-fuzz" +version = "0.1.0" +authors = ["Ivan Smirnov "] +publish = false +edition = "2021" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +# internal +qoi-fast = { path = ".." } +# external +libfuzzer-sys = "0.4" + +# Prevent this from interfering with workspaces +[workspace] +members = ["."] + +[[bin]] +name = "qoi_encode" +path = "fuzz_targets/qoi_encode.rs" +test = false +doc = false diff --git a/fuzz/fuzz_targets/qoi_encode.rs b/fuzz/fuzz_targets/qoi_encode.rs new file mode 100644 index 0000000..5875377 --- /dev/null +++ b/fuzz/fuzz_targets/qoi_encode.rs @@ -0,0 +1,29 @@ +#![no_main] +use libfuzzer_sys::fuzz_target; + +fuzz_target!(|input: (bool, u8, &[u8])| { + let (is_4, w_frac, data) = input; + let channels = if is_4 { 4 } else { 3 }; + let size = data.len(); + let n_pixels = size / channels as usize; + let (w, h) = if n_pixels == 0 { + (0, 0) + } else { + let w = ((n_pixels * (1 + w_frac as usize)) / 256).max(1); + let h = n_pixels / w; + (w, h) + }; + let out = qoi_fast::qoi_encode_to_vec( + &data[..(w * h * channels as usize)], + w as u32, + h as u32, + channels, + 0, + ); + if w * h != 0 { + let out = out.unwrap(); + assert!(out.len() <= qoi_fast::encode_size_required(w as u32, h as u32, channels)); + } else { + assert!(out.is_err()); + } +});