forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lightly revise the rest of the documentation (rust-lang#1396)
- Loading branch information
Showing
9 changed files
with
123 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,3 @@ | ||
# Usage on a package | ||
|
||
Kani is also integrated with `cargo` and can be invoked from a crate directory as follows: | ||
|
||
```bash | ||
cargo kani [<kani-args>]* | ||
``` | ||
|
||
`cargo kani` supports all `kani` arguments. | ||
|
||
`cargo kani` is the recommended approach for using Kani on a project, due to its | ||
ability to handle external dependencies and the option add configurations via the `Cargo.toml` file. | ||
|
||
If your proof harness is placed under `tests/`, you will need to run | ||
`cargo kani` with the `--tests` for Kani to be able to find your | ||
harness. | ||
|
||
## Configuration | ||
|
||
Users can add a default configuration to the `Cargo.toml` file for running harnesses in a package. | ||
Kani will extract any arguments from these sections: | ||
* `[kani]` | ||
* `[workspace.metadata.kani]` | ||
* `[package.metadata.kani]` | ||
|
||
For example, say you want to set a loop unwinding bound of `5` for all the harnesses in a package. | ||
You can achieve this by adding the following lines to the package's `Cargo.toml`: | ||
|
||
```toml | ||
[package.metadata.kani] | ||
flags = { default-unwind = "5" } | ||
``` | ||
[See here](./usage.md#usage-on-a-package) |
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 |
---|---|---|
@@ -1,40 +1,3 @@ | ||
# Usage on a single file | ||
|
||
For small examples or initial learning, it's very common to run Kani on just one source file. | ||
|
||
The command line format for invoking Kani directly is the following: | ||
|
||
``` | ||
kani filename.rs [<kani-args>]* | ||
``` | ||
|
||
For example, | ||
|
||
``` | ||
kani example.rs | ||
``` | ||
|
||
runs Kani on all the proof harnesses from file `example.rs`. | ||
A proof harness is simply a function with the `#[kani::proof]` annotation. | ||
|
||
## Common arguments | ||
|
||
The most common `kani` arguments are the following: | ||
|
||
* `--harness <name>`: By default, Kani checks all proof harnesses it finds. You | ||
can switch to checking a single harness using this flag. | ||
|
||
* `--default-unwind <n>`: Set a global upper [loop | ||
unwinding](./tutorial-loop-unwinding.md) bound on all loops. This can force | ||
termination when CBMC tries to unwind loops indefinitely. | ||
|
||
* `--output-format <regular|terse|old>`: By default (`regular`), Kani | ||
post-processes CBMC's output to produce more comprehensible results. In | ||
contrast, `terse` outputs only a summary of these results, and `old` forces | ||
Kani to emit the original output from CBMC. | ||
|
||
* `--visualize`: Generates an HTML report in the local directory accessible | ||
through `report/html/index.html`. This report shows coverage information and | ||
provides traces (i.e., counterexamples) for each failure found by Kani. | ||
|
||
Run `kani --help` to see a complete list of arguments. | ||
[See here](./usage.md#usage-on-a-single-crate) |
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 |
---|---|---|
@@ -1,12 +1,94 @@ | ||
# Usage | ||
# Using Kani | ||
|
||
At present, Kani can used in two ways: | ||
* [On a single file](./kani-single-file.md) with the `kani` command. | ||
* [On a package](./cargo-kani.md) with the `cargo-kani` command. | ||
|
||
Running [Kani on a single file](./kani-single-file.md) is quite useful for small | ||
examples or projects that don't use `cargo`. | ||
* [On a single crate](#usage-on-a-single-crate) with the `kani` command. | ||
* [On a Cargo package](#usage-on-a-package) with the `cargo kani` command. | ||
|
||
However, if you plan to integrate Kani in your projects, the recommended | ||
approach is to use [Kani on a package](./cargo-kani.md) because of its ability | ||
to handle external dependencies. | ||
If you plan to integrate Kani in your projects, the recommended approach is to use `cargo kani`. | ||
If you're already using cargo, this will handle dependencies automatically, and it can be configured (if needed) in `Cargo.toml`. | ||
But `kani` is useful for small examples/tests. | ||
|
||
## Usage on a package | ||
|
||
Kani is integrated with `cargo` and can be invoked from a package as follows: | ||
|
||
```bash | ||
cargo kani [OPTIONS] | ||
``` | ||
|
||
This works like `cargo test` except that it will analyze all proof harnesses instead of running all test harnesses. | ||
|
||
## Common command line flags | ||
|
||
Common to both `kani` and `cargo kani` are many command-line flags: | ||
|
||
* `--visualize`: Generates an HTML report showing coverage information and providing traces (i.e., counterexamples) for each failure found by Kani. | ||
|
||
* `--tests`: Build in "[test mode](https://doc.rust-lang.org/rustc/tests/index.html)", i.e. with `cfg(test)` set and `dev-dependencies` available (when using `cargo kani`). | ||
|
||
* `--harness <name>`: By default, Kani checks all proof harnesses it finds. | ||
You can switch to checking a single harness using this flag. | ||
|
||
* `--default-unwind <n>`: Set a default global upper [loop unwinding](./tutorial-loop-unwinding.md) bound for proof harnesses. | ||
This can force termination when CBMC tries to unwind loops indefinitely. | ||
|
||
Run `cargo kani --help` to see a complete list of arguments. | ||
|
||
## Usage on a single crate | ||
|
||
For small examples or initial learning, it's very common to run Kani on just one source file. | ||
The command line format for invoking Kani directly is the following: | ||
|
||
``` | ||
kani filename.rs [OPTIONS] | ||
``` | ||
|
||
This will build `filename.rs` and run all proof harnesses found within. | ||
|
||
## Configuration in `Cargo.toml` | ||
|
||
Users can add a default configuration to the `Cargo.toml` file for running harnesses in a package. | ||
Kani will extract any arguments from these sections: | ||
|
||
* `[workspace.metadata.kani.flags]` | ||
* `[package.metadata.kani.flags]` | ||
|
||
For example, if you want to set a default loop unwinding bound (when it's not otherwise specified), you can achieve this by adding the following lines to the package's `Cargo.toml`: | ||
|
||
```toml | ||
[package.metadata.kani.flags] | ||
default-unwind = 1 | ||
``` | ||
|
||
The options here are the same as on the command line (`cargo kani --help`), and flags (that is, command line arguments that don't take a value) are enabled by setting them to `true`. | ||
|
||
## The build process | ||
|
||
When Kani builds your code, it does two important things: | ||
|
||
1. It sets `cfg(kani)`. | ||
2. It injects the `kani` crate. | ||
|
||
A proof harness (which you can [learn more about in the tutorial](./kani-tutorial.md)), is a function annotated with `#[kani::proof]` much like a test is annotated with `#[test]`. | ||
But you may experience a similar problem using Kani as you would with `dev-dependencies`: if you try writing `#[kani::proof]` directly in your code, `cargo build` will fail because it doesn't know what the `kani` crate is. | ||
|
||
This is why we recommend the same conventions as are used when writing tests in Rust: wrap your proof harnesses in `cfg(kani)` conditional compilation: | ||
|
||
```rust | ||
#[cfg(kani)] | ||
mod verification { | ||
use super::*; | ||
|
||
#[kani::proof] | ||
pub fn check_something() { | ||
// .... | ||
} | ||
} | ||
``` | ||
|
||
This will ensure that a normal build of your code will be completely unaffected by anything Kani-related. | ||
|
||
This conditional compilation with `cfg(kani)` (as seen above) is still required for Kani proofs placed under `tests/`. | ||
When this code is built by `cargo test`, the `kani` crate is not available, and so it would otherwise cause build failures. | ||
(Whereas the use of `dev-dependencies` under `tests/` does not need to be gated with `cfg(test)` since that code is already only built when testing.) |