Skip to content

Commit

Permalink
Change how we test std codegen
Browse files Browse the repository at this point in the history
Instead of running a script at the end, use our script based suite.
Also, inject `kani_core` so we can stop special casing Kani compiler
to handle this job.
  • Loading branch information
celinval committed Nov 15, 2024
1 parent 14eb1de commit 2613708
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 89 deletions.
3 changes: 0 additions & 3 deletions scripts/kani-regression.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ for testp in "${TESTS[@]}"; do
--quiet --no-fail-fast
done

# Check codegen for the standard library
time "$SCRIPT_DIR"/std-lib-regression.sh

# We rarely benefit from re-using build artifacts in the firecracker test,
# and we often end up with incompatible leftover artifacts:
# "error[E0514]: found crate `serde_derive` compiled by an incompatible version of rustc"
Expand Down
86 changes: 0 additions & 86 deletions scripts/std-lib-regression.sh

This file was deleted.

3 changes: 3 additions & 0 deletions tests/script-based-pre/std_codegen/codegen_std.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[TEST] Copy standard library from the current toolchain
[TEST] Modify library
------ Build succeeded -------
71 changes: 71 additions & 0 deletions tests/script-based-pre/std_codegen/codegen_std.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT

# Test that we can codegen the entire standard library.
# 1. Make a copy of the rust standard library.
# 2. Add a few Kani definitions to it
# 3. Run Kani compiler

set -e
set -u

KANI_DIR=$(git rev-parse --show-toplevel)
TMP_DIR="tmp_dir"

rm -rf ${TMP_DIR}
mkdir ${TMP_DIR}

cp -r dummy ${TMP_DIR}

# Create a custom standard library.
echo "[TEST] Copy standard library from the current toolchain"
SYSROOT=$(rustc --print sysroot)
STD_PATH="${SYSROOT}/lib/rustlib/src/rust/library"
cp -r "${STD_PATH}" "${TMP_DIR}"

# Insert Kani definitions.
CORE_CODE='
#[cfg(kani)]
kani_core::kani_lib!(core);
'

echo "[TEST] Modify library"
echo "${CORE_CODE}" >> ${TMP_DIR}/library/core/src/lib.rs

# Note: Prepending with sed doesn't work on MacOs the same way it does in linux.
# sed -i '1s/^/#![cfg_attr(kani, feature(kani))]\n/' ${TMP_DIR}/library/std/src/lib.rs
cp ${TMP_DIR}/library/std/src/lib.rs ${TMP_DIR}/std_lib.rs
echo '#![cfg_attr(kani, feature(kani))]' > ${TMP_DIR}/library/std/src/lib.rs
cat ${TMP_DIR}/std_lib.rs >> ${TMP_DIR}/library/std/src/lib.rs

export RUST_BACKTRACE=1
export RUSTC_LOG=error
export __CARGO_TESTS_ONLY_SRC_ROOT=$(readlink -f ${TMP_DIR})/library
RUST_FLAGS=(
"--kani-compiler"
"-Cpanic=abort"
"-Zalways-encode-mir"
"-Cllvm-args=--backend=cprover"
"-Cllvm-args=--ignore-global-asm"
"-Cllvm-args=--reachability=pub_fns"
"-L${KANI_DIR}/target/kani/no_core/lib"
"--extern=kani_core"
"--cfg=kani"
"-Zcrate-attr=feature(register_tool)"
"-Zcrate-attr=register_tool(kanitool)"
)
export RUSTFLAGS="${RUST_FLAGS[@]}"
export RUSTC="$KANI_DIR/target/kani/bin/kani-compiler"
export KANI_LOGS=kani_compiler::kani_middle=debug
TARGET=$(rustc -vV | awk '/^host/ { print $2 }')

pushd ${TMP_DIR}/dummy > /dev/null
# Compile the standard library to iRep
cargo build --verbose -Z build-std --lib --target ${TARGET}
popd > /dev/null

echo "------ Build succeeded -------"

# Cleanup
rm -r ${TMP_DIR}
4 changes: 4 additions & 0 deletions tests/script-based-pre/std_codegen/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT
script: codegen_std.sh
expected: codegen_std.expected
9 changes: 9 additions & 0 deletions tests/script-based-pre/std_codegen/dummy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT

[package]
name = "dummy"
version = "0.1.0"
edition = "2021"

[workspace]
7 changes: 7 additions & 0 deletions tests/script-based-pre/std_codegen/dummy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Just a dummy file. We are interested in the standard library only
pub fn void() {
todo!()
}

0 comments on commit 2613708

Please sign in to comment.