Skip to content

Commit

Permalink
refactor into smaller components
Browse files Browse the repository at this point in the history
  • Loading branch information
jouho committed Dec 11, 2024
1 parent 8e761c6 commit 7ced542
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 72 deletions.
14 changes: 0 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -683,18 +683,6 @@ if (BUILD_TESTING)
set(FUZZ_TIMEOUT_SEC 60)
endif()

if(DEFINED ENV{CORPUS_UPLOAD_LOC})
set(CORPUS_UPLOAD_LOC $ENV{CORPUS_UPLOAD_LOC})
else()
set(CORPUS_UPLOAD_LOC "none")
endif()

if(DEFINED ENV{ARTIFACT_UPLOAD_LOC})
set(ARTIFACT_UPLOAD_LOC $ENV{ARTIFACT_UPLOAD_LOC})
else()
set(ARTIFACT_UPLOAD_LOC "none")
endif()

# Build LD_PRELOAD shared libraries
file(GLOB LIBRARY_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/tests/fuzz/LD_PRELOAD/*.c")
foreach(SRC ${LIBRARY_SRCS})
Expand Down Expand Up @@ -728,8 +716,6 @@ if (BUILD_TESTING)
bash ${SCRIPT_PATH}
${TEST_NAME}
${FUZZ_TIMEOUT_SEC}
${CORPUS_UPLOAD_LOC}
${ARTIFACT_UPLOAD_LOC}
${CMAKE_CURRENT_SOURCE_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests/fuzz
)
Expand Down
41 changes: 41 additions & 0 deletions codebuild/bin/fuzz_corpus_download.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#

# Clean the environment before copying corpuses from the S3 bucket.
# The LD variables may interfere with certificate validation when communicating with AWS S3.
unset LD_PRELOAD
unset LD_LIBRARY_PATH

for FUZZ_TEST in tests/fuzz/*.c; do
# extract file name without extension
TEST_NAME=$(basename "$FUZZ_TEST")
TEST_NAME="${TEST_NAME%.*}"

# temp corpus folder to store downloaded corpus files
TEMP_CORPUS_DIR="./tests/fuzz/temp_corpus_${TEST_NAME}"

printf "Downloading corpus files from S3 bucket...\n"

# Check if corpus.zip exists in the specified S3 location.
# `> /dev/null 2>&1` redirects output to /dev/null.
# If the file is not found, `aws s3 ls` returns a non-zero exit code.
if aws s3 ls "s3://s2n-tls-fuzz-corpus/${TEST_NAME}/corpus.zip" > /dev/null 2>&1; then
aws s3 cp "s3://s2n-tls-fuzz-corpus/${TEST_NAME}/corpus.zip" "${TEMP_CORPUS_DIR}/corpus.zip"
unzip -o "${TEMP_CORPUS_DIR}/corpus.zip" -d "${TEMP_CORPUS_DIR}"
else
printf "corpus.zip not found for ${TEST_NAME}"
fi
printf "Finished downloading corpus files from S3 bucket."
done
41 changes: 41 additions & 0 deletions codebuild/bin/fuzz_corpus_upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#

# Clean the environment before copying corpuses to the S3 bucket.
# The LD variables may interfere with certificate validation when communicating with AWS S3.
unset LD_PRELOAD
unset LD_LIBRARY_PATH

for FUZZ_TEST in tests/fuzz/*.c; do
# extract file name without extension
TEST_NAME=$(basename "$FUZZ_TEST")
TEST_NAME="${TEST_NAME%.*}"

# Store generated corpus files in the S3 bucket.
printf "Zipping corpus files...\n"
zip -r ./tests/fuzz/corpus/${TEST_NAME}.zip ./tests/fuzz/corpus/${TEST_NAME}/

printf "Uploading zipped corpus file to S3 bucket...\n"
aws s3 cp ./tests/fuzz/corpus/${TEST_NAME}.zip s3://s2n-tls-fuzz-corpus/${TEST_NAME}/corpus.zip

printf "Finished downloading corpus files from S3 bucket.\n"
printf "Uploading test log output...\n"

# Store generated output files in the S3 bucket.
printf "Uploading output files to S3 bucket...\n"
aws s3 cp ./tests/fuzz/${TEST_NAME}_output.txt ${ARTIFACT_UPLOAD_LOC}/${TEST_NAME}/output_$(date +%Y-%m-%d-%T).txt
aws s3 cp ./tests/fuzz/${TEST_NAME}_results.txt ${ARTIFACT_UPLOAD_LOC}/${TEST_NAME}/results_$(date +%Y-%m-%d-%T).txt
done

File renamed without changes.
4 changes: 3 additions & 1 deletion codebuild/spec/buildspec_fuzz_scheduled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ phases:
post_build:
on-failure: ABORT
commands:
- ./codebuild/bin/fuzz_corpus_download.sh
# -L: Restrict tests to labels matching the pattern 'fuzz'
# --timeout: override ctest's default timeout of 1500
- |
LLVM_PROFILE_FILE="./profiles/${TEST_NAME}/${TEST_NAME}.%p.profraw" \
cmake --build build/ --target test -- ARGS="-L fuzz --output-on-failure -j $(nproc) --timeout 28800"
- ./tests/fuzz/generate_fuzz_coverage.sh
- ./codebuild/bin/fuzz_corpus_upload.sh
- ./codebuild/bin/generate_fuzz_coverage.sh

artifacts:
# upload all files in the fuzz_coverage_report directory
Expand Down
62 changes: 5 additions & 57 deletions tests/fuzz/runFuzzTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@
set -e

usage() {
echo "Usage: runFuzzTest.sh TEST_NAME FUZZ_TIMEOUT_SEC CORPUS_UPLOAD_LOC ARTIFACT_UPLOAD_LOC S2N_ROOT"
echo "Usage: runFuzzTest.sh TEST_NAME FUZZ_TIMEOUT_SEC S2N_ROOT"
exit 1
}

if [ "$#" -ne "5" ]; then
if [ "$#" -ne "3" ]; then
usage
fi

TEST_NAME=$1
FUZZ_TIMEOUT_SEC=$2
CORPUS_UPLOAD_LOC=$3
ARTIFACT_UPLOAD_LOC=$4
S2N_ROOT=$5
S2N_ROOT=$3

MIN_TEST_PER_SEC="1000"
MIN_FEATURES_COVERED="100"
Expand All @@ -46,7 +44,6 @@ fi
ASAN_OPTIONS+="symbolize=1"
LSAN_OPTIONS+="log_threads=1"
UBSAN_OPTIONS+="print_stacktrace=1"
NUM_CPU_THREADS=$(nproc)
LIBFUZZER_ARGS+="-timeout=5 -max_len=4096 -print_final_stats=1 -max_total_time=${FUZZ_TIMEOUT_SEC}"

TEST_SPECIFIC_OVERRIDES="${S2N_ROOT}/build/lib/lib${TEST_NAME}_overrides.so"
Expand Down Expand Up @@ -80,27 +77,8 @@ ACTUAL_TEST_FAILURE=0

# Copy existing Corpus to a temp directory so that new inputs from fuzz tests runs will add new inputs to the temp directory.
# This allows us to minimize new inputs before merging to the original corpus directory.
# If s3 directory is specified, use corpuses stored in S3 bucket instead.
TEMP_CORPUS_DIR="$(mktemp -d)"
if [ "$CORPUS_UPLOAD_LOC" != "none" ]; then
(
# Clean the environment before copying corpuses from the S3 bucket.
# The LD variables interferes with certificate validation when communicating with AWS S3.
unset LD_PRELOAD
unset LD_LIBRARY_PATH

# Check if corpus.zip exists in the specified S3 location.
# `> /dev/null 2>&1` redirects output to /dev/null.
# If the file is not found, `aws s3 ls` returns a non-zero exit code.
if aws s3 ls "${CORPUS_UPLOAD_LOC}/${TEST_NAME}/corpus.zip" > /dev/null 2>&1; then
printf "corpus.zip found, downloading from S3 bucket and unzipping...\n"
aws s3 cp "${CORPUS_UPLOAD_LOC}/${TEST_NAME}/corpus.zip" "${TEMP_CORPUS_DIR}/corpus.zip"
unzip -o "${TEMP_CORPUS_DIR}/corpus.zip" -d "${TEMP_CORPUS_DIR}"
fi
)
else
cp -r ./corpus/${TEST_NAME}/. "${TEMP_CORPUS_DIR}"
fi
TEMP_CORPUS_DIR="temp_corpus_${TEST_NAME}"
cp -r ./corpus/${TEST_NAME}/. "${TEMP_CORPUS_DIR}"

# Run fuzz test executable and store results to an output file
env LD_PRELOAD="$LD_PRELOAD_" \
Expand Down Expand Up @@ -141,39 +119,9 @@ then
printf "\033[31;1mERROR!\033[0m ${TEST_NAME} only covers ${FEATURE_COVERAGE} features, which is below ${MIN_FEATURES_COVERED}! This may be due to missing corpus files or a bug.\n"
exit -1;
fi

# Store generated corpus files in the S3 bucket.
unset LD_PRELOAD
unset LD_LIBRARY_PATH
if [ "$CORPUS_UPLOAD_LOC" != "none" ]; then
printf "Zipping corpus files...\n"
zip -r ./corpus/${TEST_NAME}.zip ./corpus/${TEST_NAME}/

printf "Uploading zipped corpus file to S3 bucket...\n"
aws s3 cp ./corpus/${TEST_NAME}.zip $CORPUS_UPLOAD_LOC/${TEST_NAME}/corpus.zip
fi
fi

else
cat ${TEST_NAME}_output.txt
printf "\033[31;1mFAILED\033[0m %s, %6d features covered\n" "$TEST_INFO" $FEATURE_COVERAGE

# Store corpus to S3 to be used for debugging if the test fails
unset LD_PRELOAD
unset LD_LIBRARY_PATH
if [ "$CORPUS_UPLOAD_LOC" != "none" ]; then
printf "Zipping corpus files...\n"
zip -r ./corpus/${TEST_NAME}.zip ./corpus/${TEST_NAME}/

printf "Uploading zipped corpus file to S3 bucket...\n"
aws s3 cp ./corpus/${TEST_NAME}.zip $CORPUS_UPLOAD_LOC/${TEST_NAME}/corpus_$(date +%Y-%m-%d-%T).zip
fi

# Store generated output files in the S3 bucket.
if [ "$ARTIFACT_UPLOAD_LOC" != "none" ]; then
printf "Uploading output files to S3 bucket...\n"
aws s3 cp ./${TEST_NAME}_output.txt ${ARTIFACT_UPLOAD_LOC}/${TEST_NAME}/output_$(date +%Y-%m-%d-%T).txt
aws s3 cp ./${TEST_NAME}_results.txt ${ARTIFACT_UPLOAD_LOC}/${TEST_NAME}/results_$(date +%Y-%m-%d-%T).txt
fi
exit -1
fi

0 comments on commit 7ced542

Please sign in to comment.