Skip to content

Commit c6981c0

Browse files
zhassan-awstedinski
authored andcommitted
Error out for crates with global ASM (rust-lang#1112)
1 parent e862e49 commit c6981c0

File tree

18 files changed

+168
-6
lines changed

18 files changed

+168
-6
lines changed

kani-compiler/kani_queries/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub trait UserInput {
1515

1616
fn set_output_pretty_json(&mut self, pretty_json: bool);
1717
fn get_output_pretty_json(&self) -> bool;
18+
19+
fn set_ignore_global_asm(&mut self, global_asm: bool);
20+
fn get_ignore_global_asm(&self) -> bool;
1821
}
1922

2023
#[derive(Debug, Default)]
@@ -23,6 +26,7 @@ pub struct QueryDb {
2326
emit_vtable_restrictions: AtomicBool,
2427
symbol_table_passes: Vec<String>,
2528
json_pretty_print: AtomicBool,
29+
ignore_global_asm: AtomicBool,
2630
}
2731

2832
impl UserInput for QueryDb {
@@ -57,4 +61,12 @@ impl UserInput for QueryDb {
5761
fn get_output_pretty_json(&self) -> bool {
5862
self.json_pretty_print.load(Ordering::Relaxed)
5963
}
64+
65+
fn set_ignore_global_asm(&mut self, global_asm: bool) {
66+
self.ignore_global_asm.store(global_asm, Ordering::Relaxed);
67+
}
68+
69+
fn get_ignore_global_asm(&self) -> bool {
70+
self.ignore_global_asm.load(Ordering::Relaxed)
71+
}
6072
}

kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,18 @@ impl CodegenBackend for GotocCodegenBackend {
8484
);
8585
}
8686
MonoItem::GlobalAsm(_) => {
87-
warn!(
88-
"Crate {} contains global ASM, which is not handled by Kani",
89-
c.short_crate_name()
90-
);
87+
if !self.queries.get_ignore_global_asm() {
88+
let error_msg = format!(
89+
"Crate {} contains global ASM, which is not supported by Kani. Rerun with `--enable-unstable --ignore-global-asm` to suppress this error (**Verification results may be impacted**).",
90+
c.short_crate_name()
91+
);
92+
tcx.sess.err(&error_msg);
93+
} else {
94+
warn!(
95+
"Ignoring global ASM in crate {}. Verification results may be impacted.",
96+
c.short_crate_name()
97+
);
98+
}
9199
}
92100
}
93101
}

kani-compiler/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ fn main() -> Result<(), &'static str> {
9292
queries.set_emit_vtable_restrictions(matches.is_present(parser::RESTRICT_FN_PTRS));
9393
queries.set_check_assertion_reachability(matches.is_present(parser::ASSERTION_REACH_CHECKS));
9494
queries.set_output_pretty_json(matches.is_present(parser::PRETTY_OUTPUT_FILES));
95+
queries.set_ignore_global_asm(matches.is_present(parser::IGNORE_GLOBAL_ASM));
9596

9697
// Generate rustc args.
9798
let rustc_args = generate_rustc_args(&matches);

kani-compiler/src/parser.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ pub const COLOR_OUTPUT: &'static str = "color-output";
2828
/// Option name used to dump function pointer restrictions.
2929
pub const RESTRICT_FN_PTRS: &'static str = "restrict-vtable-fn-ptrs";
3030

31-
/// Option name used to enable assertion reachability checks
31+
/// Option name used to enable assertion reachability checks.
3232
pub const ASSERTION_REACH_CHECKS: &'static str = "assertion-reach-checks";
3333

3434
/// Option name used to use json pretty-print for output files.
3535
pub const PRETTY_OUTPUT_FILES: &'static str = "pretty-json-files";
3636

37+
/// Option used for suppressing global ASM error.
38+
pub const IGNORE_GLOBAL_ASM: &'static str = "ignore-global-asm";
39+
3740
/// Option name used to override the sysroot.
3841
pub const SYSROOT: &'static str = "sysroot";
3942

@@ -135,6 +138,11 @@ pub fn parser<'a, 'b>() -> App<'a, 'b> {
135138
.long("--pretty-json-files")
136139
.help("Output json files in a more human-readable format (with spaces)."),
137140
)
141+
.arg(
142+
Arg::with_name(IGNORE_GLOBAL_ASM)
143+
.long("--ignore-global-asm")
144+
.help("Suppress error due to the existence of global_asm in a crate"),
145+
)
138146
}
139147

140148
/// Retrieves the arguments from the command line and process hack to incorporate CARGO arguments.

kani-driver/src/args.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ pub struct KaniArgs {
143143
/// Turn off assertion reachability checks
144144
#[structopt(long)]
145145
pub no_assertion_reach_checks: bool,
146+
147+
/// Do not error out for crates containing `global_asm!`.
148+
/// This option may impact the soundness of the analysis and may cause false proofs and/or counterexamples
149+
#[structopt(long, hidden_short_help(true), requires("enable-unstable"))]
150+
pub ignore_global_asm: bool,
146151
/*
147152
The below is a "TODO list" of things not yet implemented from the kani_flags.py script.
148153

kani-driver/src/call_single_file.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ impl KaniSession {
117117
if self.args.assertion_reach_checks() {
118118
flags.push("--assertion-reach-checks".into());
119119
}
120+
if self.args.ignore_global_asm {
121+
flags.push("--ignore-global-asm".into());
122+
}
120123

121124
// Stratification point!
122125
// Above are arguments that should be parsed by kani-compiler

scripts/std-lib-regression.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cp ${KANI_DIR}/rust-toolchain.toml .
4444

4545
echo "Starting cargo build with Kani"
4646
export RUSTC_LOG=error
47-
export KANIFLAGS="--goto-c"
47+
export KANIFLAGS="--goto-c --ignore-global-asm"
4848
export RUSTFLAGS="--kani-flags"
4949
export RUSTC="$KANI_DIR/target/debug/kani-compiler"
5050
$WRAPPER cargo build --verbose -Z build-std --lib --target $TARGET
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "global"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
crate_with_global_asm = { path = "crate_with_global_asm" }
13+
14+
[package.metadata.kani]
15+
flags = { enable-unstable = true, ignore-global-asm = true }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Status: SUCCESS\
2+
Description: "assertion failed: x * y == 33"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "crate_with_global_asm"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// A crate with global ASM
5+
6+
// defines a function `foo`
7+
std::arch::global_asm!(".global foo", "foo:", "nop",);
8+
9+
pub static mut STATIC_VAR: u16 = 98;
10+
11+
// exports the fn `foo`
12+
extern "C" {
13+
pub fn foo();
14+
}
15+
16+
// a function that does not involve any ASM
17+
pub fn eleven() -> i32 {
18+
11
19+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Status: SUCCESS\
2+
Description: "assertion failed: x == 98"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Call a function from crate with global ASM
5+
// Should pass with --ignore-global-asm
6+
#[kani::proof]
7+
fn calls_crate_with_global_asm() {
8+
let x = 3;
9+
let y = crate_with_global_asm::eleven();
10+
assert_eq!(x * y, 33);
11+
}
12+
13+
// Access a static variable from crate with global ASM
14+
// Should pass with --ignore-global-asm
15+
#[kani::proof]
16+
fn reads_static_var_in_crate_with_global_asm() {
17+
let x = unsafe { crate_with_global_asm::STATIC_VAR };
18+
assert_eq!(x, 98);
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "global"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
crate_with_global_asm = { path = "crate_with_global_asm" }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "crate_with_global_asm"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// A crate with global ASM
5+
6+
// defines a function `foo`
7+
std::arch::global_asm!(".global foo", "foo:", "nop",);
8+
9+
pub static mut STATIC_VAR: u16 = 98;
10+
11+
// exports the fn `foo`
12+
extern "C" {
13+
pub fn foo();
14+
}
15+
16+
// a function that does not involve any ASM
17+
pub fn eleven() -> i32 {
18+
11
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: Crate crate_with_global_asm contains global ASM, which is not supported by Kani. Rerun with `--enable-unstable --ignore-global-asm` to suppress this error (**Verification results may be impacted**).
2+
Error: "Failed to compile crate."
3+
error: could not compile `crate_with_global_asm` due to previous error
4+
Error: cargo exited with status exit status: 101
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Kani should error out if run without --ignore-global-asm even if the crate
5+
// with global ASM is not called
6+
#[kani::proof]
7+
fn doesnt_call_crate_with_global_asm() {
8+
let x = 3;
9+
let y = 11;
10+
assert_eq!(x * y, 33);
11+
}

0 commit comments

Comments
 (0)