Skip to content

Commit c039f65

Browse files
zhassan-awstedinski
authored andcommitted
Add documentation of Kani result types (rust-lang#864)
1 parent 870e37b commit c039f65

File tree

9 files changed

+145
-1
lines changed

9 files changed

+145
-1
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ It also means the necessary `kani-` flag-comments must appear in each file.
1818
To run just these tests, return to the Kani root directory and run:
1919

2020
```
21-
COMPILETEST_FORCE_STAGE0=1 ./x.py test -i --stage 0 kani-docs
21+
cargo run -p compiletest --quiet -- --suite kani-docs --mode cargo-kani --quiet
2222
```

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [Comparison with other tools](./tool-comparison.md)
66
- [Kani on a single file](./kani-single-file.md)
77
- [Kani on a package](./cargo-kani.md)
8+
- [Kani result types](./kani-result-types.md)
89
- [Debugging failures]()
910
- [Debugging non-termination]()
1011
- [Debugging coverage]()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "result-types"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
8+
[workspace]
9+
10+
[package.metadata.kani]
11+
flags = {output-format = "regular", assertion-reach-checks = true}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FAILURE
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[kani::proof]
2+
// ANCHOR: success_example
3+
fn success_example() {
4+
let mut sum = 0;
5+
for i in 1..4 {
6+
sum += i;
7+
}
8+
assert_eq!(sum, 6);
9+
}
10+
// ANCHOR_END: success_example
11+
12+
#[kani::proof]
13+
// ANCHOR: failure_example
14+
fn failure_example() {
15+
let arr = [1, 2, 3];
16+
assert_ne!(arr.len(), 3);
17+
}
18+
// ANCHOR_END: failure_example
19+
20+
#[kani::proof]
21+
// ANCHOR: unreachable_example
22+
fn unreachable_example() {
23+
let x = 5;
24+
let y = x + 2;
25+
if x > y {
26+
assert!(x < 8);
27+
}
28+
}
29+
// ANCHOR_END: unreachable_example
30+
31+
#[kani::proof]
32+
// ANCHOR: undetermined_example
33+
fn undetermined_example() {
34+
let mut x = 0;
35+
unsupp(&mut x);
36+
assert!(x == 0);
37+
}
38+
39+
#[feature(asm)]
40+
fn unsupp(x: &mut u8) {
41+
unsafe {
42+
std::arch::asm!("nop");
43+
}
44+
}
45+
46+
// ANCHOR_END: undetermined_example
47+
48+
fn main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SUCCESS
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UNDETERMINED
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UNREACHABLE

docs/src/kani-result-types.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Kani result types
2+
3+
The result of a check in Kani can be one of the following:
4+
5+
1. `SUCCESS`: This indicates that the check passed, i.e., the property holds.
6+
Note that in some cases, the property may hold _vacuously_. This can occur
7+
because the property is unreachable, or because the harness is
8+
_over-constrained_.
9+
10+
Example:
11+
```rust
12+
{{#include getting-started/result-types/src/main.rs:success_example}}
13+
```
14+
The output from Kani indicates that the assertion holds:
15+
```
16+
Check 4: success_example.assertion.4
17+
- Status: SUCCESS
18+
- Description: "assertion failed: sum == 6"
19+
```
20+
21+
2. `FAILURE`: This indicates that the check failed, i.e., the property doesn't
22+
hold. In this case, you can view the failure trace by re-running with the
23+
`--visualize` option. This generates a directory that contains the trace
24+
information. Load the `html/index.html` file from that directory in a browser to
25+
examine the trace.
26+
27+
Example:
28+
```rust
29+
{{#include getting-started/result-types/src/main.rs:failure_example}}
30+
```
31+
The assertion doesn't hold as Kani's output indicates:
32+
```
33+
Check 2: failure_example.assertion.2
34+
- Status: FAILURE
35+
- Description: "assertion failed: arr.len() != 3"
36+
```
37+
38+
3. `UNREACHABLE` (requires `--assertion-reach-checks`): This indicates that the check is unreachable. This occurs when
39+
there is no possible execution trace that can reach the check's line of code.
40+
This may be because the function that contains the check is unused, or that the
41+
harness does not trigger the condition under which the check is invoked. Kani
42+
currently checks the reachability of the following assertion types:
43+
1. Assert macros (e.g. `assert`, `assert_eq`, etc.)
44+
2. Arithmetic overflow checks
45+
3. Negation overflow checks
46+
4. Index out-of-bound checks
47+
5. Divide/remainder-by-zero checks
48+
49+
50+
Example:
51+
52+
```rust
53+
{{#include getting-started/result-types/src/main.rs:unreachable_example}}
54+
```
55+
56+
The output from Kani when run with `--assertion-reach-checks` indicates that
57+
the assertion is unreachable:
58+
```
59+
Check 2: unreachable_example.assertion.2
60+
- Status: UNREACHABLE
61+
- Description: "assertion failed: x < 8"
62+
```
63+
64+
4. `UNDETERMINED`: This indicates that Kani was not able to conclude whether the
65+
property holds or not. This can occur when the Rust program contains a construct
66+
that is not currently supported by Kani. See
67+
[Limitations](./limitations.md#limitations) for Kani's current support of the
68+
Rust language features.
69+
70+
Example:
71+
```rust
72+
{{#include getting-started/result-types/src/main.rs:undetermined_example}}
73+
```
74+
The output from Kani indicates that the assertion is undetermined due to the
75+
missing support for inline assembly in Kani:
76+
```
77+
Check 2: undetermined_example.assertion.2
78+
- Status: UNDETERMINED
79+
- Description: "assertion failed: x == 0"
80+
```

0 commit comments

Comments
 (0)