Commit 0024103
Carolyn Zech
Autoharness: Derive
While generating automatic harnesses, derive `Arbitrary` implementations
for structs and enums that don't have implementations.
## Implementation Overview
1. In `automatic_harness_partition`, we mark a function as eligible for
autoharness if its arguments either:
a. implement `Arbitrary`, or
b. are structs/enums whose fields implement `Arbitrary`.
2. `AutomaticHarnessPass` runs as before, but now may generate harnesses
that call `kani::any()` for structs/enums that do not actually implement
`Arbitrary`. See the definition of `kani::any()`:
https://github.com/model-checking/kani/blob/b64e59de669cd77b625cc8c0b9a94f29117a0ff7/library/kani_core/src/lib.rs#L274-L276
The initial `kani::any()` call resolves fine, but the compiler would ICE
if it tried to resolve `T::any()`.
3. To avoid the ICE, we add a new `AutomaticArbitraryPass` that runs
after the `AutomaticHarnessPass`. This pass detects the calls to
nonexistent `T::any()`s and replaces them with inlined `T::any()`
implementations. These implementations are based on what Kani's derive
macros for structs and enums produce.
## Example
Given the automatic harness `foo_harness` produced by
`AutomaticHarnessPass`:
```rust
struct Foo(u32)
fn function_with_foo(foo: Foo) { ... }
// pretend this is an autoharness, just written in source code for the example
#[kani::proof]
fn foo_harness() {
let foo = kani::any();
function_with_foo(foo);
}
```
`AutomaticArbitraryPass` will rewrite `kani::any()`:
```rust
pub fn any() -> Foo {
Foo::any()
}
```
as:
```rust
pub fn any() -> Foo {
Self(kani::any())
}
```
i.e., replace the call to `Foo::any()` with what the derive macro would
have produced for the body of `Foo::any()` (had the programmer used the
derive macro instead).
## Limitations
The fields need to have `Arbitrary` implementations; there is no support
for recursive derivation. See the tests for examples; this should be
resolvable through further engineering effort.
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.Arbitrary for structs and enums (#4167)1 parent ada8ed3 commit 0024103
File tree
20 files changed
+819
-61
lines changed- docs/src/reference/experimental
- kani-compiler
- src/kani_middle
- transform
- tests/script-based-pre
- autoderive_arbitrary_enums
- src
- autoderive_arbitrary_structs
- src
- cargo_autoharness_filter
- src
20 files changed
+819
-61
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1084 | 1084 | | |
1085 | 1085 | | |
1086 | 1086 | | |
| 1087 | + | |
1087 | 1088 | | |
1088 | 1089 | | |
1089 | 1090 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | | - | |
30 | | - | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
394 | 396 | | |
395 | 397 | | |
396 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
397 | 402 | | |
398 | 403 | | |
399 | 404 | | |
400 | | - | |
| 405 | + | |
401 | 406 | | |
402 | 407 | | |
403 | 408 | | |
| |||
432 | 437 | | |
433 | 438 | | |
434 | 439 | | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
454 | 446 | | |
455 | 447 | | |
456 | 448 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
165 | 168 | | |
166 | 169 | | |
167 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
0 commit comments