Skip to content

Commit 214f966

Browse files
authored
Move std-analysis.sh script from Kani repository (#261)
Copied from model-checking/kani@8d46dc61d89b8b1cb8a with the change that the generated Cargo.toml is forced to edition2024 support to make it work with Rust releases after 2025-02-26. This will re-enable successfully gathering metrics. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 0027317 commit 214f966

File tree

2 files changed

+122
-4
lines changed

2 files changed

+122
-4
lines changed
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env bash
2+
# Copyright Kani Contributors
3+
# SPDX-License-Identifier: Apache-2.0 OR MIT
4+
5+
# Collect some metrics related to the crates that compose the standard library.
6+
#
7+
# Files generates so far:
8+
#
9+
# - ${crate}_scan_overall.csv: Summary of function metrics, such as safe vs unsafe.
10+
# - ${crate}_scan_input_tys.csv: Detailed information about the inputs' type of each
11+
# function found in this crate.
12+
#
13+
# How we collect metrics:
14+
#
15+
# - Compile the standard library using the `scan` tool to collect some metrics.
16+
# - After compilation, move all CSV files that were generated by the scanner,
17+
# to the results folder.
18+
set -eu
19+
20+
# Test for platform
21+
PLATFORM=$(uname -sp)
22+
if [[ $PLATFORM == "Linux x86_64" ]]
23+
then
24+
TARGET="x86_64-unknown-linux-gnu"
25+
# 'env' necessary to avoid bash built-in 'time'
26+
WRAPPER="env time -v"
27+
elif [[ $PLATFORM == "Darwin i386" ]]
28+
then
29+
TARGET="x86_64-apple-darwin"
30+
# mac 'time' doesn't have -v
31+
WRAPPER="time"
32+
elif [[ $PLATFORM == "Darwin arm" ]]
33+
then
34+
TARGET="aarch64-apple-darwin"
35+
# mac 'time' doesn't have -v
36+
WRAPPER="time"
37+
else
38+
echo
39+
echo "Std-Lib codegen regression only works on Linux or OSX x86 platforms, skipping..."
40+
echo
41+
exit 0
42+
fi
43+
44+
# Get Kani root
45+
if [[ $# -ne 1 ]]
46+
then
47+
echo "Required command-line argument <KANI-DIR> missing"
48+
exit 1
49+
fi
50+
KANI_DIR=$1
51+
52+
echo "-------------------------------------------------------"
53+
echo "-- Starting analysis of the Rust standard library... --"
54+
echo "-------------------------------------------------------"
55+
56+
echo "-- Build scanner"
57+
cd $KANI_DIR
58+
cargo build -p scanner
59+
60+
echo "-- Build std"
61+
cd /tmp
62+
if [ -d std_lib_analysis ]
63+
then
64+
rm -rf std_lib_analysis
65+
fi
66+
cargo new std_lib_analysis --lib
67+
cd std_lib_analysis
68+
sed -i '1i cargo-features = ["edition2024"]' Cargo.toml
69+
70+
echo '
71+
pub fn dummy() {
72+
}
73+
' > src/lib.rs
74+
75+
# Use same nightly toolchain used to build Kani
76+
cp ${KANI_DIR}/rust-toolchain.toml .
77+
78+
export RUST_BACKTRACE=1
79+
export RUSTC_LOG=error
80+
81+
RUST_FLAGS=(
82+
"-Cpanic=abort"
83+
"-Zalways-encode-mir"
84+
)
85+
export RUSTFLAGS="${RUST_FLAGS[@]}"
86+
export RUSTC="$KANI_DIR/target/debug/scan"
87+
# Compile rust with our extension
88+
$WRAPPER cargo build --verbose -Z build-std --lib --target $TARGET
89+
90+
echo "-- Process results"
91+
92+
# Move files to results folder
93+
results=/tmp/std_lib_analysis/results
94+
mkdir $results
95+
find /tmp/std_lib_analysis/target -name "*.csv" -exec mv {} $results \;
96+
97+
# Create a summary table
98+
summary=$results/summary.csv
99+
100+
# write header
101+
echo -n "crate," > $summary
102+
tr -d "[:digit:],;" < $results/alloc_scan_overall.csv \
103+
| tr -s '\n' ',' >> $summary
104+
echo "" >> $summary
105+
106+
# write body
107+
for f in $results/*overall.csv; do
108+
# Join all crate summaries into one table
109+
fname=$(basename $f)
110+
crate=${fname%_scan_overall.csv}
111+
echo -n "$crate," >> $summary
112+
tr -d '[:alpha:]_,;' < $f | tr -s '\n' ',' \
113+
>> $summary
114+
echo "" >> $summary
115+
done
116+
117+
echo "-------------------------------------------------------"
118+
echo "Finished analysis successfully..."
119+
echo "- See results at ${results}"
120+
echo "-------------------------------------------------------"

scripts/run-kani.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,9 @@ main() {
304304
local current_dir=$(pwd)
305305
echo "Running Kani list command..."
306306
"$kani_path" list -Z list $unstable_args ./library --std --format json
307-
echo "Running Kani's std-analysis command..."
308-
pushd $build_dir
309-
./scripts/std-analysis.sh
310-
popd
311307
pushd scripts/kani-std-analysis
308+
echo "Running Kani's std-analysis command..."
309+
./std-analysis.sh $build_dir
312310
pip install -r requirements.txt
313311
echo "Computing Kani-specific metrics..."
314312
./kani_std_analysis.py --kani-list-file $current_dir/kani-list.json

0 commit comments

Comments
 (0)