Oliver Gugger
4 years ago
3 changed files with 149 additions and 0 deletions
@ -0,0 +1,34 @@
|
||||
FUZZPKG = brontide lnwire wtwire
|
||||
FUZZ_TEST_RUN_TIME = 30
|
||||
FUZZ_TEST_TIMEOUT = 20
|
||||
FUZZ_NUM_PROCESSES = 4
|
||||
FUZZ_BASE_WORKDIR = $(shell pwd)/fuzz
|
||||
|
||||
# If specific package is being fuzzed, construct the full name of the
|
||||
# subpackage.
|
||||
ifneq ($(pkg),) |
||||
FUZZPKG := $(pkg)
|
||||
endif |
||||
|
||||
# The default run time per fuzz test is pretty low and normally will be
|
||||
# overwritten by a user depending on the time they have available.
|
||||
ifneq ($(run_time),) |
||||
FUZZ_TEST_RUN_TIME := $(run_time)
|
||||
endif |
||||
|
||||
# If the timeout needs to be increased, overwrite the default value.
|
||||
ifneq ($(timeout),) |
||||
FUZZ_TEST_TIMEOUT := $(timeout)
|
||||
endif |
||||
|
||||
# Overwrites the number of parallel processes. Should be set to the number of
|
||||
# processor cores in a system.
|
||||
ifneq ($(processes),) |
||||
FUZZ_NUM_PROCESSES := $(processes)
|
||||
endif |
||||
|
||||
# Overwrite the base work directory for the fuzz run. Can be used to supply any
|
||||
# previously generated corpus.
|
||||
ifneq ($(base_workdir),) |
||||
FUZZ_BASE_WORKDIR := $(base_workdir)
|
||||
endif |
@ -0,0 +1,90 @@
|
||||
#!/bin/bash |
||||
|
||||
set -e |
||||
|
||||
function build_fuzz() { |
||||
PACKAGES=$1 |
||||
|
||||
for pkg in $PACKAGES; do |
||||
pushd fuzz/$pkg |
||||
|
||||
for file in *.go; do |
||||
if [[ "$file" == "fuzz_utils.go" ]]; then |
||||
continue |
||||
fi |
||||
|
||||
NAME=$(echo $file | sed 's/\.go$//1') |
||||
echo "Building zip file for $pkg/$NAME" |
||||
go-fuzz-build -func "Fuzz_$NAME" -o "$pkg-$NAME-fuzz.zip" "github.com/lightningnetwork/lnd/fuzz/$pkg" |
||||
done |
||||
|
||||
popd |
||||
done |
||||
} |
||||
|
||||
# timeout is a cross platform alternative to the GNU timeout command that |
||||
# unfortunately isn't available on macOS by default. |
||||
timeout() { |
||||
time=$1 |
||||
$2 & |
||||
pid=$! |
||||
sleep $time |
||||
kill -s SIGINT $pid |
||||
} |
||||
|
||||
function run_fuzz() { |
||||
PACKAGES=$1 |
||||
RUN_TIME=$2 |
||||
TIMEOUT=$3 |
||||
PROCS=$4 |
||||
BASE_WORKDIR=$5 |
||||
|
||||
for pkg in $PACKAGES; do |
||||
pushd fuzz/$pkg |
||||
|
||||
for file in *.go; do |
||||
if [[ "$file" == "fuzz_utils.go" ]]; then |
||||
continue |
||||
fi |
||||
|
||||
NAME=$(echo $file | sed 's/\.go$//1') |
||||
WORKDIR=$BASE_WORKDIR/$pkg/$NAME |
||||
mkdir -p $WORKDIR |
||||
echo "Running fuzzer $pkg-$NAME-fuzz.zip with $PROCS processors for $RUN_TIME seconds" |
||||
COMMAND="go-fuzz -bin=$pkg-$NAME-fuzz.zip -workdir=$WORKDIR -procs=$PROCS -timeout=$TIMEOUT" |
||||
echo "$COMMAND" |
||||
timeout "$RUN_TIME" "$COMMAND" |
||||
done |
||||
|
||||
popd |
||||
done |
||||
} |
||||
|
||||
# usage prints the usage of the whole script. |
||||
function usage() { |
||||
echo "Usage: " |
||||
echo "fuzz.sh build <packages>" |
||||
echo "fuzz.sh run <packages> <run_time> <timeout>" |
||||
} |
||||
|
||||
# Extract the sub command and remove it from the list of parameters by shifting |
||||
# them to the left. |
||||
SUBCOMMAND=$1 |
||||
shift |
||||
|
||||
# Call the function corresponding to the specified sub command or print the |
||||
# usage if the sub command was not found. |
||||
case $SUBCOMMAND in |
||||
build) |
||||
echo "Building fuzz packages" |
||||
build_fuzz "$@" |
||||
;; |
||||
run) |
||||
echo "Running fuzzer" |
||||
run_fuzz "$@" |
||||
;; |
||||
*) |
||||
usage |
||||
exit 1 |
||||
;; |
||||
esac |
Loading…
Reference in new issue