| 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 | set -e |
| 17 | |
| 18 | usage() { |
| 19 | echo "Usage: fuzz_coverage_report.sh" |
| 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | if [ "$#" -ne "0" ]; then |
| 24 | usage |
| 25 | fi |
| 26 | |
| 27 | FUZZ_TEST_DIR="tests/fuzz" |
| 28 | FUZZCOV_SOURCES="api bin crypto error stuffer tls utils" |
| 29 | |
| 30 | # generate coverage report for each fuzz test |
| 31 | printf "Generating coverage reports... \n" |
| 32 | |
| 33 | mkdir -p coverage/fuzz |
| 34 | for FUZZ_TEST in "$FUZZ_TEST_DIR"/*.c; do |
| 35 | # extract file name without extension |
| 36 | TEST_NAME=$(basename "$FUZZ_TEST") |
| 37 | TEST_NAME="${TEST_NAME%.*}" |
| 38 | |
| 39 | # merge multiple .profraw files into a single .profdata file |
| 40 | llvm-profdata merge \ |
| 41 | -sparse tests/fuzz/profiles/${TEST_NAME}/*.profraw \ |
| 42 | -o tests/fuzz/profiles/${TEST_NAME}/${TEST_NAME}.profdata |
| 43 | |
| 44 | # generate a coverage report in text format |
| 45 | llvm-cov report \ |
| 46 | -instr-profile=tests/fuzz/profiles/${TEST_NAME}/${TEST_NAME}.profdata build/lib/libs2n.so ${FUZZCOV_SOURCES} \ |
| 47 | -show-functions \ |
| 48 | > coverage/fuzz/${TEST_NAME}_cov.txt |
| 49 | |
| 50 | # exports coverage data in LCOV format |
| 51 | llvm-cov export \ |
| 52 | -instr-profile=tests/fuzz/profiles/${TEST_NAME}/${TEST_NAME}.profdata build/lib/libs2n.so ${FUZZCOV_SOURCES} \ |
| 53 | -format=lcov \ |
| 54 | > coverage/fuzz/${TEST_NAME}_cov.info |
| 55 | |
| 56 | # convert to HTML format |
| 57 | genhtml -q -o coverage/html/${TEST_NAME} coverage/fuzz/${TEST_NAME}_cov.info > /dev/null 2>&1 |
| 58 | done |
| 59 | |
| 60 | # merge all coverage reports into a single report that shows total s2n coverage |
| 61 | printf "Calculating total s2n coverage... \n" |
| 62 | llvm-profdata merge \ |
| 63 | -sparse tests/fuzz/profiles/*/*.profdata \ |
| 64 | -o tests/fuzz/profiles/merged_fuzz.profdata |
| 65 | |
| 66 | llvm-cov report \ |
| 67 | -instr-profile=tests/fuzz/profiles/merged_fuzz.profdata build/lib/libs2n.so ${FUZZCOV_SOURCES} \ |
| 68 | > s2n_fuzz_coverage.txt |
| 69 | |
| 70 | llvm-cov export \ |
| 71 | -instr-profile=tests/fuzz/profiles/merged_fuzz.profdata build/lib/libs2n.so ${FUZZCOV_SOURCES} \ |
| 72 | -format=lcov \ |
| 73 | > s2n_fuzz_cov.info |
| 74 | |
| 75 | genhtml s2n_fuzz_cov.info --branch-coverage -q -o coverage/fuzz/total_fuzz_coverage |