feat: add nix fuzz devShell and fuzz-usbip app with --fork support

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Davíð Steinn Geirsson 2026-03-25 22:08:55 +00:00
parent dfc7e366d9
commit 565e0f5834
2 changed files with 80 additions and 4 deletions

View file

@ -4,9 +4,13 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils }:
outputs = { self, nixpkgs, flake-utils, rust-overlay }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
@ -44,6 +48,8 @@
mainProgram = "usbip-rs";
};
};
rust-nightly = rust-overlay.packages.${system}.rust-nightly;
in
{
packages = {
@ -55,8 +61,57 @@
default = usbip-rs;
};
devShells.default = pkgs.mkShell {
inherit nativeBuildInputs buildInputs;
devShells = {
default = pkgs.mkShell {
inherit nativeBuildInputs buildInputs;
};
fuzz = pkgs.mkShell {
buildInputs = [
rust-nightly
pkgs.cargo-fuzz
pkgs.libusb1
] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.udev
];
nativeBuildInputs = [ pkgs.stdenv.cc pkgs.pkg-config ];
};
};
apps = let
fuzz-usbip = pkgs.writeShellScriptBin "fuzz-usbip" ''
set -euo pipefail
export PATH="${rust-nightly}/bin:${pkgs.cargo-fuzz}/bin:${pkgs.stdenv.cc}/bin:${pkgs.pkg-config}/bin:$PATH"
export PKG_CONFIG_PATH="${pkgs.libusb1.dev}/lib/pkgconfig''${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"${pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux '':${pkgs.udev.dev}/lib/pkgconfig''}
cd "$(${pkgs.git}/bin/git rev-parse --show-toplevel)/lib"
if [ $# -eq 0 ]; then
cargo fuzz list
else
target="$1"
shift
fork=0
args=()
for arg in "$@"; do
case "$arg" in
--fork=*) fork=''${arg#--fork=} ;;
*) args+=("$arg") ;;
esac
done
if [ "$fork" -gt 0 ]; then
while true; do
cargo fuzz run "$target" -- -max_len=1048576 "-fork=$fork" "''${args[@]}" || true
echo "--- fuzzer exited, restarting (artifacts saved) ---"
done
else
cargo fuzz run "$target" -- -max_len=1048576 "''${args[@]}"
fi
fi
'';
in {
fuzz-usbip = {
type = "app";
program = "${fuzz-usbip}/bin/fuzz-usbip";
};
};
});
}