Skip to content

Commit 8b8b897

Browse files
RFC: Partitioned proofs (#4228)
This discusses a prototype implementation of the feature suggested in #3006 that would allow users to soundly partition proofs into harnesses that could be verified separately and in parallel. [Rendered](https://github.com/AlexanderPortland/kani/blob/harness-partitioning-rfc/rfc/src/rfcs/0014-harness-partition.md) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Co-authored-by: Michael Tautschnig <[email protected]>
1 parent a7d917d commit 8b8b897

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
- **Feature Name:** Partitioned Proofs (`partition-proof`)
2+
- **Feature Request Issue:** [#3006](https://github.com/model-checking/kani/issues/3006)
3+
- **RFC PR:** https://github.com/model-checking/kani/pull/4228
4+
- **Status:** Under Review
5+
- **Version:** 0
6+
- **Proof-of-concept:** None yet, the previous prototype uses an out of date API.
7+
8+
-------------------
9+
10+
## Summary
11+
12+
It can often be useful to subdivide an expensive proof harness so that different parts of the input space are verified separately.
13+
This is also known as *proof by cases* or a *case split*.
14+
Adding the built-in ability to partition a proof harness into different pieces (that each make differing assumptions about their inputs) could reduce the cost of expensive proofs, while allowing Kani to automatically check that the partitions cover the entire input space and, thus, will be equivalent to a single-harness proof.
15+
16+
## User Impact
17+
18+
Imagine that you have a function to verify like the following (based on the example from [#3006](https://github.com/model-checking/kani/issues/3006)).
19+
20+
```rust
21+
pub fn target_fn(input: i32) -> isize {
22+
let val = if input > 0 {
23+
very_complex_fn_1(input)
24+
} else {
25+
very_complex_fn_2(input)
26+
};
27+
very_complex_fn_3(val)
28+
}
29+
30+
#[kani::proof]
31+
pub fn proof_harness() {
32+
let input = kani::any();
33+
assert!(target_fn(input) > 0)
34+
}
35+
```
36+
37+
Since there are two tricky to analyze function calls, but only one will ever be called on a given input, you might want to verify all values of `input` where `input > 0` that will take the first branch separately from those that will take the second.
38+
This way, each verification run will only have to reason about two of the three complex function calls, and you could use Kani's parallel proof runner to run both proofs at once.
39+
40+
The best way to currently do this is by manually partitioning out these paths into two proof harnesses.
41+
42+
```rust
43+
#[kani::proof]
44+
pub fn first_branch_harness() {
45+
let input = kani::any_where(|i: &i32| *i > 0i32);
46+
assert!(target_fn(input) > 0)
47+
}
48+
49+
#[kani::proof]
50+
pub fn second_branch_harness() {
51+
let input = kani::any_where(|i: &i32| *i < 0i32); // ERROR: This should've been i <= 0
52+
assert!(target_fn(input) > 0)
53+
}
54+
```
55+
56+
One could also write preconditions for `very_complex_fn_{1_2}` that restrict `input` accordingly, write `proof_for_contract` harnesses for those, and then use `stub_verified` on the `target_fn` harness.
57+
58+
However, either of these strategies:
59+
- **can affect soundness**--there's no guarantee that your partitions will fully span the space of possible inputs.
60+
The only way to determine that a set of proofs like the one above are incomplete (as it forgets to verify the value of 0 for `input`) is by manual inspection.
61+
This gets infeasible for proofs with complex partition rules like those found in the [proofs for the standard library's unchecked multiplication](https://github.com/model-checking/verify-rust-std/blob/1c4ea17a99b9202f96608473083998b116bb6508/library/core/src/num/mod.rs#L1818-L1836).
62+
- **increases user burden**--instead of having to write and maintain a single proof, the user now has to handle a proof for each partition.
63+
64+
Instead, Kani should provide a feature to automatically partition a proof based on certain conditions and ensure that the newly partitioned harnesses are equivalent to a single-harness proof.
65+
66+
## User Experience
67+
68+
Kani will introduce a new `kani::partition` function, gated behind an unstable feature flag.
69+
When calling this function, the user would provide the type of the partition variable, as well as a set of partition conditions and a closure with the actual code to run on the partitioned variable (we'll call this the *partition closure*).
70+
71+
For example, the above would become
72+
73+
```rust
74+
#[kani::proof]
75+
pub fn partitioned_proof() {
76+
kani::partition::<i32, _, _>([|a| *a > 0, |a| *a < 0], |input: i32| assert!(target_fn(input) > 0));
77+
}
78+
```
79+
80+
Kani would then automatically handle generating the partitioned proofs checking that the conditions fully cover the domain of the partition variable.
81+
Overlaps would be allowed as they don't affect soundness and may be useful in certain cases (see [below](#1-allowing-overlapping-partition-conditions) for more discussion).
82+
83+
When running such a partitioned proof, the user's output would be as shown below. Notice the separate proof harnesses generated for each partition and the additional one generated to prove that the partitions fully cover the domain of the partition variable.
84+
85+
```
86+
Checking partition #1 (*input > 0) of harness partitioned_proof...
87+
CBMC 6.7.1 (cbmc-6.7.1)
88+
/* MORE CBMC OUTPUT */
89+
RESULTS:
90+
Check 1: partitioned_proof_partition_1.assertion.1
91+
- Status: SUCCESS
92+
- Description: "assertion failed: target_fn(input) > 0"
93+
- Location: src/pre_partition.rs:8:5 in function partitioned_proof_partition_1
94+
95+
Checking partition #2 (*input < 0) of harness partitioned_proof...
96+
CBMC 6.7.1 (cbmc-6.7.1)
97+
/* MORE CBMC OUTPUT */
98+
RESULTS:
99+
Check 1: partitioned_proof_partition_2.assertion.1
100+
- Status: SUCCESS
101+
- Description: "assertion failed: target_fn(input) > 0"
102+
- Location: src/pre_partition.rs:8:5 in function partitioned_proof_partition_2
103+
104+
Checking coverage of partitions for harness partitioned_proof...
105+
CBMC 6.7.1 (cbmc-6.7.1)
106+
/* MORE CBMC OUTPUT */
107+
RESULTS:
108+
Check 1: partitioned_proof_coverage.assertion.1
109+
- Status: FAILURE
110+
- Description: "partitions for partitioned_proof do not cover all possible values of the i32 type."
111+
- Location: src/pre_partition.rs:11:5 in function partitioned_proof_coverage
112+
```
113+
114+
Note that the coverage check fails, since we're missing the `i = 0` case.
115+
116+
## Software Design
117+
118+
This change would introduce the new `kani::partition` function, with the following signature:
119+
120+
```rust
121+
pub fn partition<T: Arbitrary, R, const N: usize>(
122+
conditions: [fn(&T) -> bool; N],
123+
and_run: fn(T) -> R, // the "partition closure"
124+
) -> R
125+
```
126+
127+
This signature makes a few key design decisions:
128+
- *It takes in an array of `conditions` with the specific length `const N`*, ensuring the number of harnesses we have to generate is known at compile time when we're doing partition generation.
129+
- *Partition conditions & the partition closure are represented by function pointers*.
130+
This allows the user to provide closures of the same signature, but only if those closures do not capture any state.
131+
Allowing conditions closures to capture runtime state like local variables would be unwise as partitions are conceptually static divisions of a proof which should not depend on runtime values.
132+
- *The function returns the same type `R` as the underlying partition closure*, with the thought being that value could then be used in later computation without affecting the mechanics of a partition.
133+
- *The partition variable is bound by `T: Arbitrary`*.
134+
This would allow us to construct a new non-deterministic value with `kani::any()` that can then be passed to the partition closure.
135+
136+
When the `kani-compiler` encounters a call to this function, it will transparently replace the entire `#[kani::proof]` that called it with a set of new generated functions that, as a whole, will represent the partitioned proof. This new partitioned proof would contain:
137+
138+
1. one new proof harness for each partition
139+
140+
In the above example, this would be along the lines of:
141+
142+
```rust
143+
#[kani::proof]
144+
/// (auto-generated partition of `input` for condition `|a| *a > 0`)
145+
pub fn partitioned_proof_partition_1() {
146+
/* OTHER CODE BEFORE THE CALL TO `kani::partition` IF THERE WAS ANY */
147+
let partition_variable = kani::any::<i32>();
148+
kani::assume((|a: &i32| *a > 0)(&partition_variable));
149+
(|input: i32| assert!(target_fn(input) > 0))(partition_variable);
150+
}
151+
```
152+
153+
2. an additional proof harness to check that the partitions fully cover the domain of the variable
154+
155+
This would be along the lines of:
156+
```rust
157+
pub fn partitioned_proof_coverage() {
158+
/* OTHER CODE BEFORE THE CALL TO `kani::partition` IF THERE WAS ANY */
159+
let partition_variable = kani::any::<i32>();
160+
161+
// checks that any possible value of partition_variable will fall into at least one partition.
162+
let is_in_partitions = [|a: &i32| *a > 0, |a: &i32| *a < 0].into_iter().any(|cond| cond(&partition_variable));
163+
kani::assert(is_in_partitions, "partition conditions for partitioned_proof do not cover all possible values of a i32.");
164+
}
165+
```
166+
167+
### Corner cases
168+
Under the type signature of `kani::partition`, the user can generate the partition conditions conditionally, e.g.:
169+
170+
```rust
171+
#[kani::proof]
172+
pub fn strange_partitioned_proof() {
173+
let f: fn(&i32) -> bool = if kani::any::<i32>() > 10 {
174+
|a: &i32| *a < 0
175+
} else {
176+
|a: &i32| *a < -2
177+
};
178+
179+
kani::partition::<i32>([f, |a: &i32| *a < 0], ...);
180+
}
181+
```
182+
183+
The behavior of a partition in this case seems ill-defined, so Kani will detect cases where the partition conditions are not constant closures or function items and emit a compile error.
184+
This would likely require new analysis from Kani to properly detect these cases.
185+
186+
## Rationale and alternatives
187+
188+
### 1. `kani::partition` as an attribute
189+
We had initially considered implementing the partition as an attribute placed on a proof harness, similar to `kani::proof`.
190+
However, we decided it would be best implemented as a function because a partition conceptually represents a split on variables in the proof, rather than something inherent to the proof itself.
191+
Similarly, we wanted to maintain the convention that proof harnesses do not take in any inputs, and all attribute solutions would have to break that.
192+
193+
### 2. Allowing overlapping partition conditions
194+
As a corollary to checking that partitions span the space of all possible inputs, I had initially considered giving a warning if partitions are overlapping as this could indicate ill-defined bounds.
195+
196+
However, this kind of overlap would not affect the correctness of the partition, as every possible value of the partition variable would still be checked, just potentially more than once.
197+
Admittedly, it's fairly difficult to come up with a realistic use case in which an overlap is helpful for users, so whether this should be allowed is up for debate.
198+
199+
## Open questions
200+
201+
1. How can we ensure that type errors in the `kani::partition` function call are clear, understandable and actionable for users?
202+
2. How can we test that there are no correctness issues introduced by our partition implementation?
203+
3. Should verification fully panic if a partitioned proof is lacking full coverage or just have that one assertion fail verification?
204+
4. What's the best way to provide support for partitioning `BoundedArbitrary` types?
205+
Potentially through a separate function `partition_bounded` which would be generic over `T: BoundedArbitrary` and a `const N: usize` which would be used for the call to `kani::bounded_any()`.
206+
5. How should users be able to control our coverage checks?
207+
Users may only want to test a subset of the input space (e.g., only testing multiplication for certain integer ranges), but the current design mandates that they cover the whole input space.
208+
We could potentially introduce a separate API, e.g. `unsafe_partition`, that allows this, or some other mechanism to skip the coverage check.
209+
6. Should overlapping partition conditions be allowed?
210+
7. (Similar to #3) How can a coverage check failure be best presented to users?
211+
Perhaps by running the coverage harness with concrete playback to generate a test case that doesn't fall into the partitions?
212+
213+
[^unstable_feature]: This unique ident should be used to enable features proposed in the RFC using `-Z <ident>` until the feature has been stabilized.

0 commit comments

Comments
 (0)