Skip to content

Commit f306a44

Browse files
authored
Add tests for issues related to std linking (rust-lang#1801)
Add tests for rust-lang#564, rust-lang#208 and rust-lang#87. Unfortunately, it looks like there are still issues with with how we are encoding panic threads and dynamic objects which are causing two of the test cases to fail.
1 parent 578bc84 commit f306a44

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

tests/kani/SizeAndAlignOfDst/main_assert_fixme.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Copyright Kani Contributors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3-
// This test takes too long with all the std symbols. Use --legacy-linker for now.
4-
// kani-flags: --legacy-linker
3+
// The original harness takes too long so we introduced a simplified version to run in CI.
4+
// kani-flags: --harness simplified
55

66
//! This is a regression test for size_and_align_of_dst computing the
77
//! size and alignment of a dynamically-sized type like
88
//! Arc<Mutex<dyn Subscriber>>.
9-
//! https://github.com/model-checking/kani/issues/426
9+
//! We added a simplified version of the original harness from:
10+
//! <https://github.com/model-checking/kani/issues/426>
1011
1112
use std::sync::Arc;
1213
use std::sync::Mutex;
@@ -37,9 +38,17 @@ impl Subscriber for DummySubscriber {
3738
}
3839
}
3940

41+
#[kani::proof]
42+
#[kani::unwind(2)]
43+
fn simplified() {
44+
let s: Arc<Mutex<dyn Subscriber>> = Arc::new(Mutex::new(DummySubscriber::new()));
45+
let data = s.lock().unwrap();
46+
assert!(data.get() == 0);
47+
}
48+
4049
#[kani::proof]
4150
#[kani::unwind(1)]
42-
fn main() {
51+
fn original() {
4352
let s: Arc<Mutex<dyn Subscriber>> = Arc::new(Mutex::new(DummySubscriber::new()));
4453
let mut data = s.lock().unwrap();
4554
data.increment();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! This test was created to cover panic hook handling by Kani.
5+
//! Tracking issue: <https://github.com/model-checking/kani/issues/208>
6+
use std::panic;
7+
8+
#[kani::proof]
9+
#[kani::unwind(2)]
10+
fn custom_hook() {
11+
panic::set_hook(Box::new(|_| {
12+
assert!(false);
13+
}));
14+
15+
let _ = panic::take_hook();
16+
17+
panic!("Normal panic");
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
//
3+
// Modifications Copyright Kani Contributors
4+
// See GitHub history for details.
5+
6+
//! This test is to check how Kani handle some std functions. The original issue was:
7+
//! <https://github.com/model-checking/kani/issues/564>
8+
//! This code was extracted from Rust by Example book.
9+
trait UsernameWidget {
10+
// Get the selected username out of this widget
11+
fn get(&self) -> String;
12+
}
13+
14+
trait AgeWidget {
15+
// Get the selected age out of this widget
16+
fn get(&self) -> u8;
17+
}
18+
19+
// A form with both a UsernameWidget and an AgeWidget
20+
struct Form {
21+
username: String,
22+
age: u8,
23+
}
24+
25+
impl UsernameWidget for Form {
26+
fn get(&self) -> String {
27+
self.username.clone()
28+
}
29+
}
30+
31+
impl AgeWidget for Form {
32+
fn get(&self) -> u8 {
33+
self.age
34+
}
35+
}
36+
37+
#[kani::proof]
38+
pub fn main() {
39+
let form = Form { username: "rustacean".to_owned(), age: 28 };
40+
41+
// If you uncomment this line, you'll get an error saying
42+
// "multiple `get` found". Because, after all, there are multiple methods
43+
// named `get`.
44+
// println!("{}", form.get());
45+
46+
let username = <Form as UsernameWidget>::get(&form);
47+
assert_eq!("rustacean".to_owned(), username);
48+
let age = <Form as AgeWidget>::get(&form);
49+
assert_eq!(28, age);
50+
}

0 commit comments

Comments
 (0)