Skip to content

Commit 919d5b6

Browse files
committed
Add integration test for kani-compiler default rustc flags
Invokes kani-compiler directly with the minimal flag set (install paths, --reachability, --kani-compiler, -Zunstable-options, -Zcrate-attr) and asserts a #[cfg(kani)]-gated harness appears in the metadata. Without the new defaults, --cfg=kani would be unset, the harness module would be invisible, and the metadata would have 0 proof_harnesses - a vacuous result with nothing verified. Verified red-first: against a kani-compiler without the change, the same invocation errors immediately ('Kani can only handle abort panic strategy'); with the change, the harness compiles and appears.
1 parent 72ec02b commit 919d5b6

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[TEST] kani-compiler exited 0
2+
[TEST] proof_harnesses: 1
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
# Copyright Kani Contributors
3+
# SPDX-License-Identifier: Apache-2.0 OR MIT
4+
5+
# Test that kani-compiler sets its required rustc flags unconditionally.
6+
#
7+
# Invoke kani-compiler directly with the MINIMAL flag set — install paths,
8+
# the routing marker (--kani-compiler), the kanitool namespace registration
9+
# (-Zcrate-attr — NOT a default because it errors on duplicate registration),
10+
# and an intent flag (--reachability) — and assert a #[cfg(kani)]-gated
11+
# harness appears in the metadata. Without the defaults, --cfg=kani would be
12+
# unset, the harness module would be invisible, and the metadata would have
13+
# 0 proof_harnesses: a vacuous "verification" with nothing verified.
14+
15+
set -eu
16+
17+
OUTDIR="tmp_compiler_defaults"
18+
rm -rf "${OUTDIR}"
19+
mkdir "${OUTDIR}"
20+
21+
# Locate kani-compiler and the kani sysroot in the dev build: `cargo build-dev`
22+
# populates target/kani/ (KANI_SYSROOT in .cargo/config.toml) with bin/ and
23+
# lib/, mirroring the release install layout. Same pattern as
24+
# std_codegen/codegen_std.sh. KANI_HOME is what `kani` itself passes as
25+
# --sysroot: LibConfig::new() uses the parent of the lib/ folder.
26+
KANI_DIR=$(git rev-parse --show-toplevel)
27+
KANI_HOME="${KANI_DIR}/target/kani"
28+
KANI_COMPILER="${KANI_HOME}/bin/kani-compiler"
29+
KANI_LIB="${KANI_HOME}/lib"
30+
31+
[[ -x "${KANI_COMPILER}" ]] || {
32+
echo "ERROR: kani-compiler not found at ${KANI_COMPILER}"
33+
echo "Run 'cargo build-dev' first."
34+
exit 1
35+
}
36+
37+
# Minimal invocation. Deliberately omits every flag KANI_REQUIRED_RUSTC_ARGS
38+
# now defaults: -Cpanic=abort, -Coverflow-checks=on, -Csymbol-mangling-version,
39+
# -Zalways-encode-mir, -Zpanic_abort_tests, -Zmir-enable-passes, --cfg=kani,
40+
# --check-cfg=cfg(kani). What remains is what every caller still has to pass:
41+
# install paths, the routing marker, -Zunstable-options (gates `--extern
42+
# noprelude:`), -Zcrate-attr (errors on duplicate so it stays caller-supplied),
43+
# and the reachability intent.
44+
"${KANI_COMPILER}" \
45+
--kani-compiler \
46+
--crate-type lib \
47+
--crate-name fixture \
48+
--out-dir "${OUTDIR}" \
49+
--sysroot "${KANI_HOME}" \
50+
-L "${KANI_LIB}" \
51+
-Z unstable-options \
52+
--extern kani \
53+
--extern noprelude:std="${KANI_LIB}/libstd.rlib" \
54+
-Z crate-attr="feature(register_tool)" \
55+
-Z crate-attr="register_tool(kanitool)" \
56+
-Cllvm-args=--reachability=harnesses \
57+
fixture.rs
58+
59+
echo "[TEST] kani-compiler exited 0"
60+
61+
# The metadata must list the #[cfg(kani)]-gated harness — proves --cfg=kani
62+
# was set internally.
63+
META=$(ls "${OUTDIR}"/*.kani-metadata.json)
64+
HARNESSES=$(jq '.proof_harnesses | length' "${META}")
65+
echo "[TEST] proof_harnesses: ${HARNESSES}"
66+
67+
rm -rf "${OUTDIR}"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
script: compiler_defaults.sh
4+
expected: compiler_defaults.expected
5+
exit_code: 0
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! A `#[cfg(kani)]`-gated harness. If kani-compiler sets `--cfg=kani` as a
5+
//! default, the harness compiles; if not, the module is invisible and 0
6+
//! harnesses appear in the metadata — a vacuous "verification" with nothing
7+
//! verified.
8+
9+
#[cfg(kani)]
10+
mod verify {
11+
#[kani::proof]
12+
fn check_with_defaults() {
13+
let x: u8 = kani::any();
14+
assert_eq!(x.wrapping_mul(1), x);
15+
}
16+
}

0 commit comments

Comments
 (0)