Skip to content

Commit

Permalink
Add gix-commitgraph::File fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-brough committed Dec 24, 2023
1 parent c1e4c62 commit 0c4976a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gix-commitgraph/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
30 changes: 30 additions & 0 deletions gix-commitgraph/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "gix-commitgraph-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
anyhow = "1.0.76"
arbitrary = { version = "1.3.2", features = ["derive"] }
libfuzzer-sys = "0.4"
tempfile = "3.8.1"

[dependencies.gix-commitgraph]
path = ".."

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

[profile.release]
debug = 1

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

use anyhow::Result;
use arbitrary::Arbitrary;
use gix_commitgraph::File;
use libfuzzer_sys::fuzz_target;
use std::fs;
use std::hint::black_box;
use tempfile::NamedTempFile;

fn fuzz(data: &[u8]) -> Result<()> {
let named_temp_file = NamedTempFile::new()?;
fs::write(named_temp_file.path(), data).expect("Unable to write fuzzed file");
let file = File::try_from(named_temp_file.path())?;

_ = black_box(file.iter_base_graph_ids().count());
_ = black_box(file.iter_commits().count());
_ = black_box(file.iter_ids().count());

let _ = black_box(file.checksum());
let _ = black_box(file.verify_checksum());
let _ = black_box(file.object_hash());

Ok(())
}

fuzz_target!(|data: &[u8]| {
_ = black_box(fuzz(data));
});

0 comments on commit 0c4976a

Please sign in to comment.