Skip to content

Commit

Permalink
add fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
xerbalind committed Oct 9, 2024
1 parent 3779125 commit 341f79f
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 4 deletions.
53 changes: 52 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ members = [
"zns",
"zns-cli",
"zns-daemon",
"fuzz"
]
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
19 changes: 19 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "zns-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
zns = {path = "../zns", features = ["arbitrary"]}

[[bin]]
name = "parser"
path = "fuzz_targets/parser.rs"
test = false
doc = false
bench = false
42 changes: 42 additions & 0 deletions fuzz/cov.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/env bash
set -e

# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <llvm-cov> <fuzz_target>"
exit 1
fi

# Assign the first argument to the fuzz_target variable
COMMAND=$1
FUZZ_TARGET=$2


if ! command -v $(COMMAND) &> /dev/null; then
echo "llvm-cov could not be found, please install LLVM."
exit 1
fi

if ! command -v rustfilt &> /dev/null; then
echo "rustfilt could not be found, please install rustfilt."
exit 1
fi

cargo fuzz coverage "$FUZZ_TARGET"

TARGET_DIR="target/x86_64-unknown-linux-gnu/coverage/x86_64-unknown-linux-gnu/release"
PROF_DATA="coverage/$FUZZ_TARGET/coverage.profdata"
OUTPUT_FILE="coverage/index.html"

if [ ! -f "$PROF_DATA" ]; then
echo "Coverage data file $PROF_DATA not found."
exit 1
fi

$COMMAND show "$TARGET_DIR/$FUZZ_TARGET" --format=html \
-Xdemangler=rustfilt \
--ignore-filename-regex="\.cargo" \
-instr-profile="$PROF_DATA" \
> "$OUTPUT_FILE"

echo "Coverage report generated as $OUTPUT_FILE"
82 changes: 82 additions & 0 deletions fuzz/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions fuzz/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
description = "flake for fuzzer as it needs nightly";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
};
outputs = {self, nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
in
with pkgs;
{
devShell = mkShell {
buildInputs = [
(rust-bin.nightly.latest.default.override { extensions = [ "llvm-tools-preview" ]; })
cargo-fuzz
rustfilt
];
};
});
}
9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use zns::{parser::FromBytes, reader::Reader, structs::Message};

fuzz_target!(|data: &[u8]| {
let mut reader = Reader::new(data);
let _ = Message::from_bytes(&mut reader);
});
1 change: 1 addition & 0 deletions zns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test-utils = []
base64 = "0.22.0"
int-enum = "1.1"
thiserror = "1.0"
arbitrary = { version = "^1.3.2", optional = true, features = ["derive"] }

[dev-dependencies]
zns = { path = ".", features = ["test-utils"] }
Expand Down
1 change: 1 addition & 0 deletions zns/src/labelstring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Display;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct LabelString(Vec<String>);

pub fn labels_equal(vec1: &LabelString, vec2: &LabelString) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions zns/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ impl<'a> Reader<'a> {
}

pub fn seek(&self, position: usize) -> Result<Self> {
if position >= self.position - 2 {
if self.position < 2 || position >= self.position - 2 {
Err(ZNSError::Reader {
message: String::from("Seeking into the future is not allowed!!"),
})
} else {
let mut reader = Reader::new(&self.buffer[..self.position]);
let mut reader = Reader::new(&self.buffer[..self.position - 1]);
reader.position = position;
Ok(reader)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ mod tests {

let new_reader = reader.seek(1);
assert!(new_reader.is_ok());
assert_eq!(new_reader.unwrap().unread_bytes(), 10);
assert_eq!(new_reader.unwrap().unread_bytes(), 9);

let new_reader = reader.seek(100);
assert!(new_reader.is_err());
Expand Down
Loading

0 comments on commit 341f79f

Please sign in to comment.