Skip to content

Commit bc134ce

Browse files
authored
Stub linker to avoid missing symbols errors (#3858)
Kani compiler does not generate real object files when compiling a crate and its dependencies for verification. Instead, the compiler generates goto binaries. This can cause linking errors. Instead, stub it with echo. We should consider creating a stub linker instead, but echo will do it for now. Note that this change only applies to target artifacts, not those compiled for the host, such as build dependencies and build scripts. Resolves #3817 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 20998ef commit bc134ce

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

kani-driver/src/call_single_file.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ impl KaniSession {
181181
"-Z",
182182
"mir-enable-passes=-RemoveStorageMarkers",
183183
"--check-cfg=cfg(kani)",
184+
// Do not invoke the linker since the compiler will not generate real object files
185+
"-Clinker=echo",
184186
]
185187
.map(OsString::from),
186188
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright Kani Contributors
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "issue-3817"
6+
version = "0.1.0"
7+
edition = "2024"
8+
description = "Issue with linking cdylib and another shared library"
9+
10+
[lib]
11+
crate-type = ["cdylib", "rlib"]
12+
13+
[dependencies]
14+
bzip2 = "0.5.0"
15+
16+
[profile.release]
17+
lto = true

tests/cargo-kani/issue-3817/expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Checking harness check_unreachable_extern_fn...
2+
VERIFICATION:- SUCCESSFUL
3+
4+
Checking harness check_missing_extern_fn...
5+
Failed Checks: call to foreign "C" function `BZ2_bzCompressInit` is not currently supported by Kani.
6+
VERIFICATION:- FAILED
7+
8+
Verification failed for - check_missing_extern_fn
9+
1 successfully verified harnesses, 1 failures, 2 total
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
//! Check that Kani can compile a crate that depends on bzip, and the analysis will only
4+
//! fail if a missing symbol is reachable.
5+
6+
use bzip2::Compression;
7+
use bzip2::read::BzEncoder;
8+
9+
#[kani::proof]
10+
fn check_missing_extern_fn() {
11+
// Call bzip compressor
12+
let data: [u8; 10] = kani::any();
13+
let compressor = BzEncoder::new(&data[..], Compression::best());
14+
assert_eq!(compressor.total_in(), data.len().try_into().unwrap());
15+
}
16+
17+
#[kani::proof]
18+
fn check_unreachable_extern_fn() {
19+
let positive = kani::any_where(|v: &i8| *v > 0);
20+
if positive == 0 {
21+
// This should be unreachable so verification should succeed.
22+
check_missing_extern_fn();
23+
}
24+
}

0 commit comments

Comments
 (0)