Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running verify-std no longer changes Cargo files #3577

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions kani-driver/src/call_cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,39 @@ pub struct CargoOutputs {

impl KaniSession {
/// Create a new cargo library in the given path.
///
/// Since we cannot create a new workspace with `cargo init --lib`, we create the dummy
/// crate manually. =( See <https://github.com/rust-lang/cargo/issues/8365>.
///
/// Without setting up a new workspace, cargo init will modify the workspace where this is
celinval marked this conversation as resolved.
Show resolved Hide resolved
/// running. See <https://github.com/model-checking/kani/issues/3574> for details.
pub fn cargo_init_lib(&self, path: &Path) -> Result<()> {
let mut cmd = setup_cargo_command()?;
cmd.args(["init", "--lib", path.to_string_lossy().as_ref()]);
self.run_terminal(cmd)
let toml_path = path.join("Cargo.toml");
if toml_path.exists() {
bail!("Cargo.toml already exists in {}", path.display());
}

// Create folder for library
fs::create_dir_all(path.join("src"))?;

// Create dummy crate and write dummy body
let lib_path = path.join("src/lib.rs");
fs::write(&lib_path, "pub fn dummy() {}")?;

// Create Cargo.toml
fs::write(
&toml_path,
r#"[package]
name = "dummy"
carolynzech marked this conversation as resolved.
Show resolved Hide resolved
version = "0.1.0"

[lib]
crate-type = ["lib"]

[workspace]
"#,
)?;
Ok(())
}

pub fn cargo_build_std(&self, std_path: &Path, krate_path: &Path) -> Result<Vec<Artifact>> {
Expand Down
4 changes: 4 additions & 0 deletions tests/script-based-pre/verify_std_cmd/verify_std.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[TEST] Only codegen inside library folder
No kani crate inside Cargo.toml as expected


[TEST] Run kani verify-std

Checking harness verify::dummy_proof...
Expand Down
39 changes: 33 additions & 6 deletions tests/script-based-pre/verify_std_cmd/verify_std.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,44 @@ 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

# Test that the command works inside the library folder and does not change
# the existing workspace
# See https://github.com/model-checking/kani/issues/3574
echo "[TEST] Only codegen inside library folder"
pushd "${TMP_DIR}/library" >& /dev/null
RUSTFLAGS="--cfg=uninit_checks" kani verify-std \
-Z unstable-options \
. \
-Z function-contracts \
-Z stubbing \
-Z mem-predicates \
--only-codegen
popd
# Grep should not find anything and exit status is 1.
grep -c kani ${TMP_DIR}/library/Cargo.toml \
&& echo "Unexpected kani crate inside Cargo.toml" \
|| echo "No kani crate inside Cargo.toml as expected"

echo "[TEST] Run kani verify-std"
export RUST_BACKTRACE=1
kani verify-std -Z unstable-options "${TMP_DIR}/library" \
--target-dir "${TMP_DIR}/target" -Z function-contracts -Z stubbing \
kani verify-std \
-Z unstable-options \
"${TMP_DIR}/library" \
--target-dir "${TMP_DIR}/target" \
-Z function-contracts \
-Z stubbing \
-Z mem-predicates

# Test that uninit-checks basic setup works on a no-core library
echo "[TEST] Run kani verify-std -Z uninit-checks"
export RUSTFLAGS="--cfg=uninit_checks"
kani verify-std -Z unstable-options "${TMP_DIR}/library" \
--target-dir "${TMP_DIR}/target" -Z function-contracts -Z stubbing \
-Z mem-predicates -Z uninit-checks
RUSTFLAGS="--cfg=uninit_checks" kani verify-std \
-Z unstable-options \
"${TMP_DIR}/library" \
--target-dir "${TMP_DIR}/target" \
-Z function-contracts \
-Z stubbing \
-Z mem-predicates \
-Z uninit-checks

# Cleanup
rm -r ${TMP_DIR}
Loading