Skip to content

Commit 6bc2e38

Browse files
celinvaladpaco-aws
andauthored
The unstable --c-lib option now requires -Z c-ffi to enable C-FFI support (rust-lang#2425)
This will also give visibility to the compiler to whether this feature is enabled or not which is needed to avoid type mismatch issue (model-checking/kani#1781). Co-authored-by: Adrian Palacios <[email protected]>
1 parent 5fbbed7 commit 6bc2e38

File tree

5 files changed

+47
-80
lines changed

5 files changed

+47
-80
lines changed

kani-driver/src/args.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ pub struct KaniArgs {
137137
pub harnesses: Vec<String>,
138138

139139
/// Link external C files referenced by Rust code.
140-
/// This is an experimental feature and requires `--enable-unstable` to be used
141-
#[arg(long, hide = true, requires("enable_unstable"), num_args(1..))]
140+
/// This is an experimental feature and requires `-Z c-ffi` to be used
141+
#[arg(long, hide = true, num_args(1..))]
142142
pub c_lib: Vec<PathBuf>,
143143
/// Enable test function verification. Only use this option when the entry point is a test function
144144
#[arg(long)]
@@ -361,6 +361,8 @@ pub enum UnstableFeatures {
361361
Stubbing,
362362
/// Generate a C-like file equivalent to input program used for debugging purpose.
363363
GenC,
364+
/// Allow Kani to link against C code.
365+
CFfi,
364366
}
365367

366368
#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
@@ -593,6 +595,17 @@ impl KaniArgs {
593595
}
594596
}
595597

598+
if !self.c_lib.is_empty() && !self.unstable_features.contains(&UnstableFeatures::CFfi) {
599+
if self.enable_unstable {
600+
self.print_deprecated("`--enable-unstable`");
601+
}
602+
return Err(Error::raw(
603+
ErrorKind::MissingRequiredArgument,
604+
"The `--c-lib` argument is unstable and requires `-Z c-ffi` to enable \
605+
unstable C-FFI support.",
606+
));
607+
}
608+
596609
Ok(())
597610
}
598611

tests/cargo-kani/simple-extern/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ edition = "2018"
1010
[workspace]
1111

1212
[kani.flags]
13-
enable-unstable = true
1413
c-lib = ["src/helper.c"]
14+
15+
[kani.unstable]
16+
c-ffi = true

tests/kani/ForeignItems/fixme_main.rs

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// kani-flags: -Z c-ffi --c-lib tests/kani/ForeignItems/lib.c
5+
//! Check how Kani handles declaration of Nullable reference (Option<&T>).
6+
//! See <https://github.com/model-checking/kani/issues/2152> for more details.
7+
8+
extern "C" {
9+
// In Rust, you say nullable pointer by using option of reference.
10+
// Rust guarantees that this has the bitwise representation:
11+
// - Some(&x) => &x;
12+
// - None => NULL;
13+
// FIXME: we need to notice when this happens and do a bitcast, or C is unhappy
14+
// <https://github.com/model-checking/kani/issues/3>
15+
fn takes_ptr_option(p: Option<&u32>) -> u32;
16+
}
17+
18+
#[kani::proof]
19+
fn main() {
20+
unsafe {
21+
// if (ptr) { *ptr - 1 }
22+
assert!(takes_ptr_option(Some(&5)) == 4);
23+
// if (!ptr) { 0 }
24+
assert!(takes_ptr_option(None) == 0);
25+
}
26+
}

tests/kani/ForeignItems/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! To run this test, do
44
//! kani main.rs -- lib.c
55
6-
// kani-flags: --enable-unstable --c-lib tests/kani/ForeignItems/lib.c
6+
// kani-flags: -Z c-ffi --c-lib tests/kani/ForeignItems/lib.c
77

88
#[repr(C)]
99
pub struct Foo {
@@ -57,7 +57,9 @@ fn main() {
5757
assert!(takes_struct(f) == 19);
5858

5959
let f2 = Foo2 { i: 12, c: 7, i2: 8 };
60+
// f2.i + f2.c
6061
assert!(takes_struct_ptr2(&f2) == 19);
62+
// f2.i + f2.i2
6163
assert!(takes_struct2(f2) == 20);
6264
}
6365
}

0 commit comments

Comments
 (0)