|
| 1 | +#!/bin/bash |
| 2 | +# Copyright Kani Contributors |
| 3 | +# SPDX-License-Identifier: Apache-2.0 OR MIT |
| 4 | + |
| 5 | + |
| 6 | +DOCUMENTATION=\ |
| 7 | +'kani-run-on-repos.sh -- script to clone and compile many remote git repositories with Kani. |
| 8 | +
|
| 9 | +WARNING: Because this script clones repositories at the HEAD, the |
| 10 | +results may not be stable when the target code changes. |
| 11 | +
|
| 12 | +USAGE: |
| 13 | +./scripts/kani-run-on-repos.sh path/to/url-list |
| 14 | +
|
| 15 | +Download the top 100 crates and runs kani on them. Prints out the |
| 16 | +errors and warning when done. Xargs is required for this script to |
| 17 | +work. |
| 18 | +
|
| 19 | +url-list: A list of URLs to run Kani on. One per line. |
| 20 | +
|
| 21 | +ENV: |
| 22 | +- PRINT_STDOUT=1 forces this script to search for warning in |
| 23 | + STDOUT in addition to STDERR |
| 24 | +
|
| 25 | +EDITING: |
| 26 | +- To adjust the git clone or kani args, modify the function |
| 27 | + `clone_and_run_kani`. |
| 28 | +- To adjust the errors this script searches for, edit the function |
| 29 | + `print_errors_for_each_repo_result` |
| 30 | +
|
| 31 | +Copyright Kani Contributors |
| 32 | +SPDX-License-Identifier: Apache-2.0 OR MIT' |
| 33 | + |
| 34 | +export SELF_SCRIPT=$0 |
| 35 | +export SELF_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 36 | +NPROC=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 4) # Linux or Mac or hard-coded default of 4 |
| 37 | +export WORK_DIRECTORY_PREFIX="$SELF_DIR/../../target/remote-repos" |
| 38 | + |
| 39 | + |
| 40 | +export STDOUT_SUFFIX='stdout.cargo-kani' |
| 41 | +export STDERR_SUFFIX='stderr.cargo-kani' |
| 42 | +export EXIT_CODE_SUFFIX='exit-code.cargo-kani' |
| 43 | +# worker function that clones target repos and runs kani over |
| 44 | +# them. This functions is called in parallel by |
| 45 | +# parallel_clone_and_run, and should not be run explicitly |
| 46 | +function clone_and_run_kani { |
| 47 | + WORK_NUMBER_ID=$(echo $1 | awk -F ',' '{ print $1}') |
| 48 | + REPOSITORY_URL=$(echo $1 | awk -F ',' '{ print $2}') |
| 49 | + REPO_DIRECTORY="$WORK_DIRECTORY_PREFIX/$WORK_NUMBER_ID" |
| 50 | + echo "work# $WORK_NUMBER_ID -- $REPOSITORY_URL" |
| 51 | + |
| 52 | + # clone or update repository |
| 53 | + (git clone $REPOSITORY_URL $REPO_DIRECTORY 2> /dev/null || git -C $REPO_DIRECTORY pull) |
| 54 | + |
| 55 | + # run cargo kani compile on repo. save results to file. |
| 56 | + PATH=$PATH:$(readlink -f $SELF_DIR/..) |
| 57 | + (cd $REPO_DIRECTORY; nice -n15 cargo kani --only-codegen) \ |
| 58 | + 1> $REPO_DIRECTORY/$STDOUT_SUFFIX \ |
| 59 | + 2> $REPO_DIRECTORY/$STDERR_SUFFIX |
| 60 | + echo $? > $REPO_DIRECTORY/$EXIT_CODE_SUFFIX |
| 61 | +} |
| 62 | +export -f clone_and_run_kani |
| 63 | + |
| 64 | +OVERALL_EXIT_CODE='0' |
| 65 | +TARGET_ERROR_REGEX='warning:\sFound\sthe\sfollowing\sunsupported\sconstructs:\|WARN' |
| 66 | +# printing function that greps the error logs and exit code. |
| 67 | +function print_errors_for_each_repo_result { |
| 68 | + DIRECTORY=$1 |
| 69 | + IS_FAIL='0' |
| 70 | + |
| 71 | + error_code="$(cat $DIRECTORY/$EXIT_CODE_SUFFIX)" |
| 72 | + if [ "$error_code" != "0" ]; then |
| 73 | + echo -e "Error exit in $DIRECTORY: code $error_code\n" |
| 74 | + IS_FAIL='1' |
| 75 | + fi |
| 76 | + |
| 77 | + STDERR_GREP=$(grep -A3 -n $TARGET_ERROR_REGEX $DIRECTORY/$STDERR_SUFFIX 2> /dev/null && echo 'STDERR has warnings') |
| 78 | + if [[ "$STDERR_GREP" =~ [a-zA-Z0-9] ]]; then |
| 79 | + echo -e "STDERR Warnings (Plus 3 lines after) $DIRECTORY/$STDERR_SUFFIX -----\n$STDERR_GREP" |
| 80 | + IS_FAIL='1' |
| 81 | + fi |
| 82 | + |
| 83 | + STDOUT_GREP=$(grep -A3 -n $TARGET_ERROR_REGEX $DIRECTORY/$STDOUT_SUFFIX 2> /dev/null && echo 'STDOUT has warnings') |
| 84 | + if [[ "$STDOUT_GREP" =~ [a-zA-Z0-9] ]] && [ "$PRINT_STDOUT" = '1' ]; then |
| 85 | + echo -e "STDOUT Warnings (Plus 3 lines after) $DIRECTORY/$STDOUT_SUFFIX -----\n$STDOUT_GREP" |
| 86 | + IS_FAIL='1' |
| 87 | + fi |
| 88 | + |
| 89 | + if [ "$IS_FAIL" -eq "0" ]; then |
| 90 | + echo 'Ok' |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +if ! which xargs 1>&2 1> /dev/null; then |
| 95 | + echo "Need to have xargs installed. Please install with `apt-get install -y xargs`" |
| 96 | + exit -1 |
| 97 | +elif [[ "$*" == *"--help"* ]]; then |
| 98 | + echo -e "$DOCUMENTATION" |
| 99 | +elif [ "$#" -eq "1" ]; then |
| 100 | + # top level logic that runs clone_and_run_kani in parallel with xargs. |
| 101 | + echo "Reading URLs from $1..."; |
| 102 | + LIST_OF_CRATE_GIT_URLS=$(cat $1) |
| 103 | + if [[ -z "$(echo $LIST_OF_CRATE_GIT_URLS | sed 's/\s//g')" ]]; then |
| 104 | + echo 'No targets found.' |
| 105 | + exit -1 |
| 106 | + fi |
| 107 | + |
| 108 | + mkdir -p $WORK_DIRECTORY_PREFIX |
| 109 | + echo -e "$LIST_OF_CRATE_GIT_URLS" | \ |
| 110 | + awk -F '\n' 'BEGIN{ a=0 }{ print a++ "," $1 }' | \ |
| 111 | + xargs -n1 -I {} -P $NPROC bash -c "clone_and_run_kani {}" |
| 112 | + |
| 113 | + # serially print out the ones that failed. |
| 114 | + num_failed="0" |
| 115 | + num_with_warning='0' |
| 116 | + for directory in $(ls $WORK_DIRECTORY_PREFIX); do |
| 117 | + REPOSITORY=$(git -C $WORK_DIRECTORY_PREFIX/$directory remote -v | awk '{ print $2 }' | head -1) |
| 118 | + echo "repository: $REPOSITORY" |
| 119 | + |
| 120 | + ERROR_OUTPUTS=$(print_errors_for_each_repo_result $WORK_DIRECTORY_PREFIX/$directory) |
| 121 | + if [[ "$ERROR_OUTPUTS" =~ 'STDERR Warnings' ]]; then |
| 122 | + OVERALL_EXIT_CODE='1' |
| 123 | + num_with_warning=$(($num_with_warning + 1)) |
| 124 | + fi |
| 125 | + if [[ "$ERROR_OUTPUTS" =~ 'Error exit in' ]]; then |
| 126 | + num_failed=$(($num_failed + 1)) |
| 127 | + fi |
| 128 | + |
| 129 | + echo -e "$ERROR_OUTPUTS" | sed 's/^/ /' |
| 130 | + done |
| 131 | + |
| 132 | + echo -e '\n--- OVERALL STATS ---' |
| 133 | + echo "$num_failed crates failed to compile" |
| 134 | + echo "$num_with_warning crates had warning(s)" |
| 135 | +else |
| 136 | + echo -e 'Needs exactly 1 argument path/to/url-list.\n' |
| 137 | + echo -e "$DOCUMENTATION" |
| 138 | +fi |
| 139 | + |
| 140 | +exit $OVERALL_EXIT_CODE |
0 commit comments