You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR introduces an initial implementation for the `autoharness`
subcommand and a book chapter describing the feature. I recommend
reading the book chapter before reviewing the code (or proceeding
further in this PR description).
## Callouts
`--harness` is to manual harnesses what `--include-function` and
`--exclude-function` are to autoharness; both allow the user to filter
which harnesses/functions get verified. Their implementation is
different, however--`--harness` is a driver-only flag, i.e., we still
compile every harness, but then only call CBMC on the ones specified in
`--harness`. `--include-function` and `--exclude-function` get passed to
the compiler. I made this design choice to try to be more aggressive
about improving compiler performance--if a user only asks to verify one
function and we go try to codegen a thousand, the compiler will take
much longer than it needs to. I thought this more aggressive
optimization was warranted given that crates are likely to have far many
more functions eligible for autoverification than manual Kani harnesses.
(See also the limitations in the book chapter).
## Testing
Besides the new tests in this PR, I also ran against
[s2n-quic](https://github.com/aws/s2n-quic) to confirm that the
subcommand works on larger crates.
Towards #3832
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
This harness first declares a local variable `x` using `kani::any()`, then calls `estimate_size` with argument `x`.
9
+
Many proof harnesses follow this predictable format—to verify a function `foo`, we create arbitrary values for each of `foo`'s arguments, then call `foo` on those arguments.
10
+
11
+
The `autoharness` subcommand leverages this observation to automatically generate and run harnesses.
12
+
Kani scans the crate for functions whose arguments all implement the `kani::Arbitrary` trait, generates harnesses for them, then runs them.
13
+
These harnesses are internal to Kani--i.e., Kani does not make any changes to your source code.
14
+
15
+
## Usage
16
+
Run either:
17
+
```
18
+
# cargo kani autoharness -Z unstable-options
19
+
```
20
+
or
21
+
```
22
+
# kani autoharness -Z unstable-options <FILE>
23
+
```
24
+
25
+
If Kani detects that all of a function `foo`'s arguments implement `kani::Arbitrary`, it will generate and run a `#[kani::proof]` harness, which prints:
26
+
27
+
```
28
+
Autoharness: Checking function foo against all possible inputs...
29
+
<VERIFICATION RESULTS>
30
+
```
31
+
32
+
However, if Kani detects that `foo` has a [contract](./contracts.md), it will instead generate a `#[kani::proof_for_contract]` harness and verify the contract:
33
+
```
34
+
Autoharness: Checking function foo's contract against all possible inputs...
35
+
<VERIFICATION RESULTS>
36
+
```
37
+
38
+
Kani generates and runs these harnesses internally—the user only sees the verification results.
39
+
40
+
The `autoharness` subcommand has options `--include-function` and `--exclude-function` to include and exclude particular functions.
41
+
These flags look for partial matches against the fully qualified name of a function.
42
+
43
+
For example, if a module `my_module` has many functions, but we are only interested in `my_module::foo` and `my_module::bar`, we can run:
44
+
```
45
+
cargo run autoharness -Z unstable-options --include-function foo include-function bar
46
+
```
47
+
To exclude `my_module` entirely, run:
48
+
```
49
+
cargo run autoharness -Z unstable-options --exclude-function my_module
50
+
```
51
+
52
+
## Example
53
+
Using the `estimate_size` example from [First Steps](../../tutorial-first-steps.md) again:
This feature is experimental and is therefore subject to change.
75
+
If you have ideas for improving the user experience of this feature,
76
+
please add them to [this GitHub issue](https://github.com/model-checking/kani/issues/3832).
77
+
78
+
## Limitations
79
+
Kani will only generate an automatic harness for a function if it can determine that all of the function's arguments implement Arbitrary.
80
+
It does not attempt to derive/implement Arbitrary for any types, even if those types could implement Arbitrary.
81
+
82
+
If a function contains a loop with a loop contract, Kani will detect the presence of a loop contract and verify that contract.
83
+
If, however, the loop does not have a contract, then there is currently no way to specify an unwinding bound for the function, meaning that Kani may hang as it tries to unwind the loop.
84
+
We recommend using the `--exclude-function` option to exclude any functions that have this issue (or `--harness-timeout` to bail after attempting verification for some amount of time).
0 commit comments