Skip to content

Commit

Permalink
Add command to run Kani from the root and from CI w/ cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jaisnan committed Sep 6, 2024
1 parent 1f0fc95 commit 78b3c28
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Session.vim
## Build
/book/
/build/
/kani_build/
/target
/library/target
*.rlib
*.rmeta
*.mir
Expand Down
1 change: 1 addition & 0 deletions doc/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build-dir = "../book"
site-url = "/verify-rust-std/"
git-repository-url = "https://github.com/model-checking/verify-rust-std"
edit-url-template = "https://github.com/model-checking/verify-rust-std/edit/main/doc/{path}"
no-section-label = true

[output.html.playground]
runnable = false
Expand Down
6 changes: 6 additions & 0 deletions kani-version.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This version should be updated whenever there is a change that makes this version of kani
# incomaptible with the verify-std repo.

[kani]
kani-version = "0.54.0"
commit = "e2a209bcb847803e8a5abb20ef7002b109ed0bf6"
172 changes: 172 additions & 0 deletions run-kani.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/bin/bash

set -e

usage() {
echo "Usage: $0 [directory]"
echo "If directory is not provided, the current directory will be used."
}

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
exit 0
fi

# Set working directory
if [[ -n "$1" ]]; then
if [[ ! -d "$1" ]]; then
echo "Error: Specified directory does not exist."
usage
exit 1
fi
WORK_DIR=$(realpath "$1")
else
WORK_DIR=$(pwd)
fi

cd "$WORK_DIR" || exit 1

log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

error_exit() {
log "ERROR: $1"
exit 1
}

# Default values
DEFAULT_TOML_FILE="kani-version.toml"
DEFAULT_REPO_URL="https://github.com/model-checking/kani.git"
DEFAULT_TARGET_DIR="kani_repo"
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}
TARGET_DIR=${KANI_TARGET_DIR:-$DEFAULT_TARGET_DIR}
BRANCH_NAME=${KANI_BRANCH_NAME:-$DEFAULT_BRANCH_NAME}

os_name=$(uname -s)

# 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_repo() {
local repo_url="$1"
local directory="$2"
local branch="$3"
local commit="$4"
git clone --depth 1 -b "$branch" "$repo_url" "$directory"
cd "$directory"
git checkout "$commit"
cd - > /dev/null
}

checkout_commit() {
local directory="$1"
local commit="$2"
cd "$directory"
git checkout "$commit"
cd ..
}

get_current_commit() {
local directory="$1"
if [ -d "$directory/.git" ]; then
git -C "$directory" rev-parse HEAD
else
echo ""
fi
}

build_project() {
local directory="$1"
cd "$directory"
cargo build-dev --release
cd ..
}

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="$build_dir/scripts/kani"

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 current_dir=$(pwd)
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")
echo "Kani commit: $commit"

# 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_repo "$REPO_URL" "$build_dir" "$BRANCH_NAME" "$commit"

# Build project
build_project "$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..."
cd $current_dir
"$kani_path" verify-std -Z unstable-options ./library --target-dir "$temp_dir_target" -Z function-contracts -Z mem-predicates
}

main
44 changes: 22 additions & 22 deletions scripts/run_update_with_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,47 +135,47 @@ fi

echo "------------------------------------"
# Call the first script to update the subtree
echo "Update subtree in Main"
echo "Update subtree in main"
source $BASE_HOME_DIR/scripts/pull_from_upstream.sh "$TEMP_HOME_DIR" $TOOLCHAIN_DATE $COMMIT_HASH
OUTPUT_SCRIPT1=("$?")
if [ "${#OUTPUT_SCRIPT1[@]}" -eq 0 ]; then
echo "script1.sh failed to run."
pull_from_upstream=("$?")
if [ "${#pull_from_upstream[@]}" -eq 0 ]; then
echo "pull_from_upstream.sh failed to run."
exit 1
else
echo "script1.sh completed successfully."
echo "Pulled from upstream succesfully."
fi

# Call the second script
echo "Running script2.sh..."
echo "Updating toolchain..."
source $BASE_HOME_DIR/scripts/update_toolchain_date.sh "$TEMP_HOME_DIR" "$TOOLCHAIN_DATE"
OUTPUT_SCRIPT2=("$?")
if [ "${#OUTPUT_SCRIPT2[@]}" -eq 0 ]; then
echo "script2.sh failed to run."
update_toolchain_date=("$?")
if [ "${#update_toolchain_date[@]}" -eq 0 ]; then
echo "update_toolchain_date.sh failed to run."
exit 1
else
echo "Update toolchain ran successfully."
echo "Toolchain updated succesfully."
fi

# Call the third script
echo "Running script3.sh..."
echo "echo "Checking compatiblity with kani..."
source $BASE_HOME_DIR/scripts/check_rustc.sh "$TEMP_HOME_DIR"
OUTPUT_SCRIPT3=("$?")
if [ "${#OUTPUT_SCRIPT3[@]}" -eq 0 ]; then
echo "script3.sh failed to run."
check_rustc=("$?")
if [ "${#check_rustc[@]}" -eq 0 ]; then
echo "check_rustc.sh failed to run."
exit 1
else
echo "script3.sh completed successfully."
echo "Changes compatible with latest rustc."
fi
# Call the fourth script
echo "Running script4.sh..."
source $BASE_HOME_DIR/scripts/check_kani.sh "$TEMP_HOME_DIR"
OUTPUT_SCRIPT4=("$?")
if [ "${#OUTPUT_SCRIPT4[@]}" -eq 0 ]; then
echo "script4.sh failed to run."
# Checking compatiblity with kani
echo "Checking compatiblity with kani..."
source $BASE_HOME_DIR/run-kani.sh "$TEMP_HOME_DIR"
check_kani=("$?")
if [ "${#check_kani[@]}" -eq 0 ]; then
echo "check_kani.sh failed to run."
exit 1
else
echo "script4.sh completed successfully."
echo "Changes compatible with kani's features/verify-std branch."
fi
# TODO: Issue a Pull Request from the sync branch of the temp repo
Expand Down

0 comments on commit 78b3c28

Please sign in to comment.