Debian Codegraph @

crt/s2n/tests/fuzz/runFuzzTest.sh

raw symbols SCIP
1 #!/bin/bash
2 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License").
5 # You may not use this file except in compliance with the License.
6 # A copy of the License is located at
7 #
8 # http://aws.amazon.com/apache2.0
9 #
10 # or in the "license" file accompanying this file. This file is distributed
11 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 # express or implied. See the License for the specific language governing
13 # permissions and limitations under the License.
14 #
15
16 # The timeout command sends a TERM and under normal circumstances returns
17 # exit code 124. We'll undo this later.
18 set -e
19
20 usage() {
21 echo "Usage: runFuzzTest.sh TEST_NAME FUZZ_TIMEOUT_SEC S2N_ROOT"
22 exit 1
23 }
24
25 if [ "$#" -ne "3" ]; then
26 usage
27 fi
28
29 TEST_NAME=$1
30 FUZZ_TIMEOUT_SEC=$2
31 S2N_ROOT=$3
32
33 MIN_TEST_PER_SEC="1000"
34 MIN_FEATURES_COVERED="100"
35
36 # Failures for negative tests can be ignored.
37 if [[ $TEST_NAME == *_negative_test ]];
38 then
39 EXPECTED_TEST_FAILURE=1
40 else
41 EXPECTED_TEST_FAILURE=0
42 fi
43
44 LIBFUZZER_ARGS+="-timeout=5 -max_len=4096 -print_final_stats=1 -max_total_time=${FUZZ_TIMEOUT_SEC}"
45
46 TEST_SPECIFIC_OVERRIDES="${S2N_ROOT}/build/lib/lib${TEST_NAME}_overrides.so"
47 GLOBAL_OVERRIDES="${S2N_ROOT}/build/lib/libglobal_overrides.so"
48
49 FUZZCOV_SOURCES="${S2N_ROOT}/api ${S2N_ROOT}/bin ${S2N_ROOT}/crypto ${S2N_ROOT}/error ${S2N_ROOT}/stuffer ${S2N_ROOT}/tls ${S2N_ROOT}/utils"
50
51 # Use LD_PRELOAD_ to prevent symbol lookup errors in commands like mkdir.
52 if [ -e $TEST_SPECIFIC_OVERRIDES ];
53 then
54 export LD_PRELOAD_="$TEST_SPECIFIC_OVERRIDES $GLOBAL_OVERRIDES"
55 else
56 export LD_PRELOAD_="$GLOBAL_OVERRIDES"
57 fi
58
59 if [ ! -d "./corpus/${TEST_NAME}" ];
60 then
61 printf "\033[33;1mWARNING!\033[0m ./corpus/${TEST_NAME} directory does not exist, feature coverage may be below minimum.\n\n"
62 fi
63
64 # Make directory if it doesn't exist
65 mkdir -p "./corpus/${TEST_NAME}"
66
67 ACTUAL_TEST_FAILURE=0
68
69 # Copy existing Corpus to a temp directory so that new inputs from fuzz tests runs will add new inputs to the temp directory.
70 # This allows us to minimize new inputs before merging to the original corpus directory.
71 TEMP_CORPUS_DIR="temp_corpus_${TEST_NAME}"
72 cp -r ./corpus/${TEST_NAME}/. "${TEMP_CORPUS_DIR}"
73
74 # Run fuzz test executable and store results to an output file
75 env LD_PRELOAD="$LD_PRELOAD_" \
76 LLVM_PROFILE_FILE="./profiles/${TEST_NAME}/${TEST_NAME}.%p.profraw" \
77 ${S2N_ROOT}/build/bin/${TEST_NAME} ${LIBFUZZER_ARGS} ${TEMP_CORPUS_DIR} \
78 > ${TEST_NAME}_output.txt 2>&1 || ACTUAL_TEST_FAILURE=1
79
80 TEST_INFO=$(
81 grep -o "stat::number_of_executed_units: [0-9]*" ${TEST_NAME}_output.txt | \
82 awk -v timeout=$FUZZ_TIMEOUT_SEC '{count += int($2); rate = int(count / timeout)} END {print count, "tests, " rate " test/sec"}' \
83 )
84 TESTS_PER_SEC=$(echo "$TEST_INFO" | cut -d ' ' -f 3)
85 FEATURE_COVERAGE=`grep -o "ft: [0-9]*" ${TEST_NAME}_output.txt | awk '{print $2}' | sort | tail -1`
86
87 if [ $ACTUAL_TEST_FAILURE == $EXPECTED_TEST_FAILURE ];
88 then
89 if [ $EXPECTED_TEST_FAILURE == 1 ];
90 then
91 # Clean up LibFuzzer corpus files if the test is negative.
92 rm -f leak-* crash-*
93 else
94 # TEMP_CORPUS_DIR may contain many new inputs that only covers a small set of new branches.
95 # Instead of copying all new inputs to the corpus directory, only copy back minimum number of new inputs that reach new branches.
96 ${S2N_ROOT}/build/bin/${TEST_NAME} \
97 -merge=1 "./corpus/${TEST_NAME}" "${TEMP_CORPUS_DIR}" \
98 > ${TEST_NAME}_results.txt 2>&1
99
100 if [ "$TESTS_PER_SEC" -lt $MIN_TEST_PER_SEC ]; then
101 printf "\033[33;1mWARNING!\033[0m ${TEST_NAME} is only ${TESTS_PER_SEC} tests/sec, which is below ${MIN_TEST_PER_SEC}/sec! Fuzz tests are more effective at higher rates.\n\n"
102 fi
103
104 COVERAGE_FAILURE_ALLOWED=0
105 if grep -Fxq ${TEST_NAME} ./allowed_coverage_failures.cfg
106 then
107 COVERAGE_FAILURE_ALLOWED=1
108 fi
109
110 if [[ "$FEATURE_COVERAGE" -lt $MIN_FEATURES_COVERED && COVERAGE_FAILURE_ALLOWED -eq 0 ]]; then
111 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"
112 exit -1;
113 fi
114 fi
115 else
116 cat ${TEST_NAME}_output.txt
117 printf "\033[31;1mFAILED\033[0m %s, %6d features covered\n" "$TEST_INFO" $FEATURE_COVERAGE
118 # Upload failure log.
119 FAILURE_TIMESTAMP=$(date +%Y-%m-%d-%T)
120 aws s3 cp ./${TEST_NAME}_output.txt ${ARTIFACT_UPLOAD_LOC}/${TEST_NAME}/output_${FAILURE_TIMESTAMP}.txt
121 # Upload libfuzzer failure artifacts (the exact inputs that triggered the failure).
122 # To reproduce locally, download the artifact from S3 and run:
123 # ./build/bin/<TEST_NAME> <artifact>
124 # e.g. ./build/bin/s2n_example_test leak-abcdef1234567890
125 for artifact in crash-* timeout-* leak-* oom-*; do
126 [ -e "$artifact" ] && aws s3 cp "./$artifact" ${ARTIFACT_UPLOAD_LOC}/${TEST_NAME}/${artifact}_${FAILURE_TIMESTAMP}
127 done
128 exit -1
129 fi