|
1 | 1 | # Kani Rust Verifier
|
2 | 2 |
|
3 |
| -The Kani Rust Verifier is a bit-precise model checker for Rust. |
4 |
| -This page will give more details on how to use Kani to verify the standard library. |
5 |
| -You can find more informations about how to install and use Kani in the |
6 |
| -[Kani book](https://model-checking.github.io/kani/). |
| 3 | +The [Kani Rust Verifier](https://github.com/model-checking/kani) is a bit-precise model checker for Rust. |
| 4 | +Kani is designed to prove safety properties in your code as well as |
| 5 | +the absence of some forms of undefined behavior. It uses model checking under the hood to ensure that |
| 6 | +Rust programs adhere to user specified properties. |
| 7 | + |
| 8 | +You can find more information about how to install in [this section of the Kani book](https://model-checking.github.io/kani/install-guide.html). |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +Consider a Rust function that takes an integer and returns its absolute value and |
| 13 | +a Kani proof that invokes the function that you want to verify |
| 14 | + |
| 15 | +``` rust |
| 16 | +fn abs_diff(a: i32, b: i32) -> i32 { |
| 17 | + if a > b { |
| 18 | + a - b |
| 19 | + } else { |
| 20 | + b - a |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(kani)] |
| 25 | +#[kani::proof] |
| 26 | +fn harness() { |
| 27 | + let a = kani::any::<i32>(); |
| 28 | + let b = kani::any::<i32>(); |
| 29 | + let result = abs_diff(a, b); |
| 30 | + kani::assert(result >= 0, "Result should always be more than 0");} |
| 31 | +``` |
| 32 | + |
| 33 | +Running the command `cargo kani` in your cargo crate will give the result |
| 34 | + |
| 35 | +``` |
| 36 | +RESULTS: |
| 37 | +Check 1: abs_diff.assertion.1 |
| 38 | + - Status: FAILURE |
| 39 | + - Description: "attempt to subtract with overflow" |
| 40 | + - Location: src/main.rs:5:9 in function abs_diff |
| 41 | +
|
| 42 | +... Other properties that might have failed or succeeded. |
| 43 | +
|
| 44 | +Summary: |
| 45 | +Verification failed for - harness |
| 46 | +Complete - 0 successfully verified harnesses, 1 failures, 1 total. |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +## Using Kani to verify the Rust Standard Library |
| 51 | + |
| 52 | +To aid the Rust Standard Library verification effort, Kani provides a sub-command out of the box to help you get started. |
| 53 | + |
| 54 | +### Step 1 |
| 55 | + |
| 56 | +Modify your local copy of the Rust Standard Library by writing proofs for the functions/methods that you want to verify. |
| 57 | + |
| 58 | +For example, insert this short blob into your copy of the library. This blob imports the building-block APIs such as |
| 59 | +`assert`, `assume`, `proof` and [function-contracts](https://github.com/model-checking/kani/blob/main/rfc/src/rfcs/0009-function-contracts.md) such as `proof_for_contract` and `fake_function`. |
| 60 | + |
| 61 | +``` rust |
| 62 | +#[cfg(kani)] |
| 63 | +kani_core::kani_lib!(core); |
7 | 64 |
|
| 65 | +#[cfg(kani)] |
| 66 | +#[unstable(feature = "kani", issue = "none")] |
| 67 | +pub mod verify { |
| 68 | + use crate::kani; |
| 69 | + |
| 70 | + #[kani::proof] |
| 71 | + pub fn harness() { |
| 72 | + kani::assert(true, "yay"); |
| 73 | + } |
| 74 | + |
| 75 | + #[kani::proof_for_contract(trivial_function)] |
| 76 | + fn dummy_proof() { |
| 77 | + trivial_function(true); |
| 78 | + } |
| 79 | + |
| 80 | + #[kani::requires(x == true)] |
| 81 | + fn trivial_function(x: bool) -> bool { |
| 82 | + x |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Step 2 |
| 88 | + |
| 89 | +Run the following command in your local terminal: |
| 90 | + |
| 91 | +`kani verify-std -Z unstable-options "path/to/library" --target-dir "/path/to/target" -Z function-contracts -Z stubbing`. |
| 92 | + |
| 93 | +The command `kani verify-std` is a sub-command of the `kani`. This specific sub-command is used to verify the Rust Standard Library with the following arguments. |
| 94 | + |
| 95 | +- `"path/to/library"`: This argument specifies the path to the modified Rust Standard Library that was prepared earlier in the script. |
| 96 | +- `--target-dir "path/to/target"`: This optional argument sets the target directory where Kani will store its output and intermediate files. |
| 97 | + |
| 98 | +Apart from these, you can use your regular `kani-args` such as `-Z function-contracts` and `-Z stubbing` depending on your verification needs. |
| 99 | +For more details on Kani's features, refer to [the features section in the Kani Book](https://model-checking.github.io/kani/reference/attributes.html) |
| 100 | + |
| 101 | +### Step 3 |
| 102 | + |
| 103 | +After running the command, you can expect an output that looks like this: |
| 104 | + |
| 105 | +``` |
| 106 | +SUMMARY: |
| 107 | + ** 0 of 1 failed |
| 108 | +
|
| 109 | +VERIFICATION:- SUCCESSFUL |
| 110 | +Verification Time: 0.017101772s |
| 111 | +
|
| 112 | +Complete - 2 successfully verified harnesses, 0 failures, 2 total. |
| 113 | +``` |
| 114 | + |
| 115 | +## More details |
| 116 | + |
| 117 | +You can find more information about how to install and how you can customize your use of Kani in the |
| 118 | +[Kani book](https://model-checking.github.io/kani/). |
0 commit comments