-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work around loop identification issues with CBMC (#2181)
CBMC has a heuristic to identify loops that identifies any jump to a previously declared basic block as the back edge of a loop. For rust, this happens often due to drop elaboration. See https://rustc-dev-guide.rust-lang.org/mir/drop-elaboration.html#drop-elaboration-1 for more details. To avoid that issue, we now codegen basic blocks in topological order.
- Loading branch information
Showing
7 changed files
with
107 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! This test checks whether dropping objects passed through | ||
//! std::sync::mpsc::channel is handled. | ||
//! This test only passes on MacOS today, so we duplicate the test for now. | ||
#![cfg(target_os = "macos")] | ||
|
||
use std::sync::mpsc::*; | ||
|
||
static mut CELL: i32 = 0; | ||
|
||
struct DropSetCELLToOne {} | ||
|
||
impl Drop for DropSetCELLToOne { | ||
fn drop(&mut self) { | ||
unsafe { | ||
CELL = 1; | ||
} | ||
} | ||
} | ||
|
||
#[kani::unwind(1)] | ||
#[kani::proof] | ||
fn main() { | ||
{ | ||
let (send, recv) = channel::<DropSetCELLToOne>(); | ||
send.send(DropSetCELLToOne {}).unwrap(); | ||
let _to_drop: DropSetCELLToOne = recv.recv().unwrap(); | ||
} | ||
assert_eq!(unsafe { CELL }, 1, "Drop should be called"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
//! Ensure that Kani identifies that there is not loop in this code. | ||
//! This was related to https://github.com/model-checking/kani/issues/2164 | ||
fn loop_free<T: Default>(b: bool, other: T) -> T { | ||
match b { | ||
true => T::default(), | ||
false => other, | ||
} | ||
} | ||
|
||
/// Set the unwind to 1 so this test will fail instead of running forever. | ||
#[kani::proof] | ||
#[kani::unwind(1)] | ||
fn check_no_loop() { | ||
let b: bool = kani::any(); | ||
let result = loop_free(b, 5); | ||
assert!(result == 5 || (b && result == 0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
//! Ensure that Kani correctly unwinds the loop with drop instructions. | ||
//! This was related to https://github.com/model-checking/kani/issues/2164 | ||
|
||
/// Dummy function with a for loop that only runs 2 iterations. | ||
fn bounded_loop<T: Default>(b: bool, other: T) -> T { | ||
let mut ret = other; | ||
for i in 0..2 { | ||
ret = match b { | ||
true => T::default(), | ||
false => ret, | ||
}; | ||
} | ||
return ret; | ||
} | ||
|
||
/// Harness that should succeed. We add a conservative loop bound. | ||
#[kani::proof] | ||
#[kani::unwind(3)] | ||
fn harness() { | ||
let _ = bounded_loop(kani::any(), ()); | ||
} |