forked from model-checking/verify-rust-std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-kani.sh
executable file
·196 lines (162 loc) · 4.81 KB
/
run-kani.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
set -e
usage() {
echo "Usage: $0 [options] [-p <path>] [--kani-args <command arguments>]"
echo "Options:"
echo " -h, --help Show this help message"
echo " -p, --path <path> Optional: Specify a path to a copy of the std library. For example, if you want to run the script from an outside directory."
echo " --kani-args <command arguments to kani> Optional: Arguments to pass to the command. Simply pass them in the same way you would to the Kani binary. This should be the last argument."
exit 1
}
# Initialize variables
command_args=""
path=""
# Parse command line arguments
# TODO: Improve parsing with getopts
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-p|--path)
if [[ -n $2 ]]; then
path=$2
shift 2
else
echo "Error: Path argument is missing"
usage
fi
;;
--kani-args)
shift
command_args="$@"
break
;;
*)
break
;;
esac
done
# Set working directory
if [[ -n "$path" ]]; then
if [[ ! -d "$path" ]]; then
echo "Error: Specified directory does not exist."
usage
fi
WORK_DIR=$(realpath "$path")
else
WORK_DIR=$(pwd)
fi
cd "$WORK_DIR"
# Default values
DEFAULT_TOML_FILE="tool_config/kani-version.toml"
DEFAULT_REPO_URL="https://github.com/model-checking/kani.git"
DEFAULT_BRANCH_NAME="features/verify-rust-std"
# Use environment variables if set, otherwise use defaults
TOML_FILE=${KANI_TOML_FILE:-$DEFAULT_TOML_FILE}
REPO_URL=${KANI_REPO_URL:-$DEFAULT_REPO_URL}
BRANCH_NAME=${KANI_BRANCH_NAME:-$DEFAULT_BRANCH_NAME}
# Function to read commit ID from TOML file
read_commit_from_toml() {
local file="$1"
if [[ ! -f "$file" ]]; then
echo "Error: TOML file not found: $file" >&2
exit 1
fi
local commit=$(grep '^commit *=' "$file" | sed 's/^commit *= *"\(.*\)"/\1/')
if [[ -z "$commit" ]]; then
echo "Error: 'commit' field not found in TOML file" >&2
exit 1
fi
echo "$commit"
}
clone_kani_repo() {
local repo_url="$1"
local directory="$2"
local branch="$3"
local commit="$4"
git clone "$repo_url" "$directory"
pushd "$directory"
git checkout "$commit"
popd
}
get_current_commit() {
local directory="$1"
if [ -d "$directory/.git" ]; then
git -C "$directory" rev-parse HEAD
else
echo ""
fi
}
build_kani() {
local directory="$1"
pushd "$directory"
os_name=$(uname -s)
if [[ "$os_name" == "Linux" ]]; then
./scripts/setup/ubuntu/install_deps.sh
elif [[ "$os_name" == "Darwin" ]]; then
./scripts/setup/macos/install_deps.sh
else
echo "Unknown operating system"
fi
git submodule update --init --recursive
cargo build-dev --release
popd
}
get_kani_path() {
local build_dir="$1"
echo "$(realpath "$build_dir/scripts/kani")"
}
run_kani_command() {
local kani_path="$1"
shift
"$kani_path" "$@"
}
# Check if binary exists and is up to date
check_binary_exists() {
local build_dir="$1"
local expected_commit="$2"
local kani_path=$(get_kani_path "$build_dir")
if [[ -f "$kani_path" ]]; then
local current_commit=$(get_current_commit "$build_dir")
if [[ "$current_commit" = "$expected_commit" ]]; then
return 0
fi
fi
return 1
}
main() {
local build_dir="$WORK_DIR/kani_build"
local temp_dir_target=$(mktemp -d)
echo "Using TOML file: $TOML_FILE"
echo "Using repository URL: $REPO_URL"
# Read commit ID from TOML file
commit=$(read_commit_from_toml "$TOML_FILE")
# Check if binary already exists and is up to date
if check_binary_exists "$build_dir" "$commit"; then
echo "Kani binary is up to date. Skipping build."
else
echo "Building Kani from commit: $commit"
# Remove old build directory if it exists
rm -rf "$build_dir"
mkdir -p "$build_dir"
# Clone repository and checkout specific commit
clone_kani_repo "$REPO_URL" "$build_dir" "$BRANCH_NAME" "$commit"
# Build project
build_kani "$build_dir"
echo "Kani build completed successfully!"
fi
# Get the path to the Kani executable
kani_path=$(get_kani_path "$build_dir")
echo "Kani executable path: $kani_path"
echo "Running Kani command..."
"$kani_path" --version
echo "Running Kani verify-std command..."
"$kani_path" verify-std -Z unstable-options ./library --target-dir "$temp_dir_target" -Z function-contracts -Z mem-predicates -Z loop-contracts --output-format=terse $command_args --enable-unstable --cbmc-args --object-bits 12
}
main
cleanup()
{
rm -rf "$temp_dir_target"
}
trap cleanup EXIT