Skip to content

Commit

Permalink
chore: fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Feb 13, 2025
1 parent 30c967b commit e3aeec8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 39 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/fuzzing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
${{ runner.os }}-go-
- name: Fuzz sonic
run: |
cd ./fuzz
make fuzz
make runopt
run: ./scripts/fuzz run

- name: Fuzz sonic VM
run: ./scripts/fuzz runopt
19 changes: 0 additions & 19 deletions fuzz/Makefile

This file was deleted.

109 changes: 109 additions & 0 deletions scripts/fuzz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/bin/bash

set -eo pipefail

FUZZ_DIR="./fuzz"
TEST_NAME="FuzzMain"
MANUAL_CORPUS="${FUZZ_DIR}/corpus"

TEST_DATA="${FUZZ_DIR}/testdata/fuzz/${TEST_NAME}"
CORPUS_DIR="${FUZZ_DIR}/go-fuzz-corpus"
FUZZ_CORPUS_REPO="https://github.com/dvyukov/go-fuzz-corpus.git"
FILE2FUZZ_VERSION="v0.10.0"

source "$(dirname "$0")/../scripts/go_flags.sh"
compile_flag=$(get_go_linkname_flag || echo "")

log_info() {
echo -e "\033[34m[INFO]\033[0m $1"
}

log_error() {
echo -e "\033[31m[ERROR]\033[0m $1" >&2
}

install_file2fuzz() {
if ! command -v file2fuzz >/dev/null; then
log_info "Installing file2fuzz@${FILE2FUZZ_VERSION}..."
go install "golang.org/x/tools/cmd/file2fuzz@${FILE2FUZZ_VERSION}" || {
log_error "Failed to install file2fuzz"
return 1
}
fi
}

init_corpus() {
log_info "Initializing fuzz corpus..."

mkdir -p "${TEST_DATA}" || {
log_error "Failed to create corpus directory"
return 1
}

if [ -d "${CORPUS_DIR}" ]; then
log_info "Removing existing corpus..."
rm -rf "${CORPUS_DIR}"
fi

log_info "Cloning fuzz corpus repository..."
git clone --depth 1 "${FUZZ_CORPUS_REPO}" "${CORPUS_DIR}" || {
log_error "Failed to clone corpus repository"
return 1
}

install_file2fuzz || return 1

log_info "Generating test corpus..."
file2fuzz -o "${TEST_DATA}" \
"${CORPUS_DIR}/json/corpus/*" \
"${MANUAL_CORPUS}/*" || {
log_error "Failed to generate test corpus"
return 1
}
}

run_fuzz() {
log_info "Running basic fuzz test..."
export SONIC_FUZZ_MEM_LIMIT=2
export GOMAXPROCS=2
go test "$compile_flag" -fuzz="${TEST_NAME}" -v -fuzztime 15m "${FUZZ_DIR}"
}

run_optimized_fuzz() {
log_info "Running optimized fuzz test..."
export SONIC_FUZZ_MEM_LIMIT=2
export SONIC_USE_OPTDEC=1
export SONIC_USE_FASTMAP=1
export SONIC_ENCODER_USE_VM=1
export GOMAXPROCS=2
go test "$compile_flag" -fuzz="${TEST_NAME}" -v -fuzztime 15m "${FUZZ_DIR}"
}

cleanup() {
log_info "Cleaning up..."
rm -vrf "${CORPUS_DIR:?}"/
rm -vrf "{FUZZ_DIR}/testdata/"
}

main() {
case "$1" in
fuzz)
init_corpus
;;
run)
run_fuzz
;;
runopt)
run_optimized_fuzz
;;
clean)
cleanup
;;
*)
echo "Usage: $0 {fuzz|run|runopt|clean}"
exit 1
;;
esac
}

main "$@"
26 changes: 26 additions & 0 deletions scripts/go_flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -xe

if [ -n "$_GO_FLAGS_LOADED" ]; then
return
fi
_GO_FLAGS_LOADED=1

get_go_linkname_flag() {
if ! command -v go &> /dev/null; then
return 1
fi

local go_version
go_version=$(go version | awk '{print $3}' | sed -E 's/go([0-9]+\.[0-9]+(\.[0-9]+)?).*/\1/')
IFS='.' read -r major minor _ <<< "$go_version"

if ! [[ "$major" =~ ^[0-9]+$ ]] || ! [[ "$minor" =~ ^[0-9]+$ ]]; then
return 1
fi

if (( major > 1 || (major == 1 && minor >= 23) )); then
echo "-ldflags=-checklinkname=0"
fi
}
19 changes: 3 additions & 16 deletions scripts/test_race.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@

set -xe

get_go_linkname_flag() {
if ! command -v go &> /dev/null; then
return
fi
source "$(dirname "$0")/../scripts/go_flags.sh"

local go_version=$(go version | awk '{print $3}' | sed -E 's/go([0-9]+\.[0-9]+(\.[0-9]+)?).*/\1/')
IFS='.' read -r major minor _ <<< "$go_version"

if ! [[ "$major" =~ ^[0-9]+$ ]] || ! [[ "$minor" =~ ^[0-9]+$ ]]; then
return
fi

if (( major > 1 || (major == 1 && minor >= 23) )); then
echo "-ldflags=-checklinkname=0"
fi
}
compile_flag=$(get_go_linkname_flag || echo "")

cd ./issue_test
cp race_test_go race_test.go
go test "$(get_go_linkname_flag)" -v -run=TestRaceEncode -race -count=100 . > test_race.log || true
go test "$compile_flag" -v -run=TestRaceEncode -race -count=100 . > test_race.log || true
if ! grep -q "WARNING: DATA RACE" ./test_race.log; then
echo "TEST FAILED: should data race here"
exit 1
Expand Down

0 comments on commit e3aeec8

Please sign in to comment.