|
| 1 | +# Debugging Slow Proofs |
| 2 | + |
| 3 | +Kani uses SAT/SMT solvers to verify code, which can sometimes result in slow or non-terminating proofs. This chapter outlines common causes of slowness and strategies to debug and improve proof performance. |
| 4 | + |
| 5 | +## Common Causes of Slow Proofs |
| 6 | + |
| 7 | +### Complex/Large Non-deterministic Types |
| 8 | +Some types are inherently more expensive to represent symbolically, e.g. strings, which have complex validation rules for UTF-8 encoding, |
| 9 | +or large bounded collections, like a vector with a large size. |
| 10 | + |
| 11 | +### Large Value Operations |
| 12 | +Mathematical operations on large values can be expensive, e.g., multiplication/division/modulo, especially with larger types (e.g., `u64`). |
| 13 | + |
| 14 | +### Unbounded Loops |
| 15 | +If Kani cannot determine a loop bound, it will unwind forever, c.f. [the loop unwinding tutorial](./tutorial-loop-unwinding.md). |
| 16 | + |
| 17 | +## Debugging Strategies |
| 18 | + |
| 19 | +These are some strategies to debug slow proofs, ordered roughly in terms of in the order you should try them: |
| 20 | + |
| 21 | +### Limit Loop Iterations |
| 22 | + |
| 23 | +First, identify whether (unbounded) loop unwinding may be the root cause. Try the `#[kani::unwind]` attribute or the `--unwind` option to limit [loop unwinding](./tutorial-loop-unwinding.md). If the proof fails because the unwind value is too low, but raising it causing the proof to be too slow, try specifying a [loop contract](./reference/experimental/loop-contracts.md) instead. |
| 24 | + |
| 25 | +### Use Different Solvers |
| 26 | + |
| 27 | +Kani supports multiple SAT/SMT solvers that may perform differently on your specific problem. Try out different solvers with the `#[kani::solver]` [attribute](./reference/attributes.md) or `--solver` option. |
| 28 | + |
| 29 | +### Remove Sources of Nondeterminism |
| 30 | + |
| 31 | +Start by replacing `kani::any()` calls with concrete values to isolate the problem: |
| 32 | + |
| 33 | +```rust |
| 34 | +#[kani::proof] |
| 35 | +fn slow_proof() { |
| 36 | + // Instead of this: |
| 37 | + // let x: u64 = kani::any(); |
| 38 | + // let y: u64 = kani::any(); |
| 39 | + |
| 40 | + // Try this: |
| 41 | + let x: u64 = 42; |
| 42 | + let y: u64 = 100; |
| 43 | + |
| 44 | + let result = complex_function(x, y); |
| 45 | + assert!(result > 0); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +If the proof becomes fast with concrete values, the issue is likely with the symbolic representation of your inputs. In that case, see you can [partition the proof](#partition-the-input-space) to cover different ranges of possible values, or restrict the proof to a smaller range of values if that is acceptable for your use case. |
| 50 | + |
| 51 | +### Reduce Collection Sizes |
| 52 | + |
| 53 | +Similarly, if smaller values are acceptable for your proof, use those instead: |
| 54 | + |
| 55 | +```rust |
| 56 | +#[kani::proof] |
| 57 | +fn test_with_small_collection() { |
| 58 | + // Instead of a large Vec |
| 59 | + // let vec: Vec<u8> = kani::bounded_any::<_, 100>(); |
| 60 | + |
| 61 | + // Start with a small size |
| 62 | + let vec: Vec<u8> = kani::bounded_any::<_, 2>(); |
| 63 | + |
| 64 | + process_collection(&vec); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Partition the Input Space |
| 69 | + |
| 70 | +Break down complex proofs by partitioning the input space: |
| 71 | + |
| 72 | +```rust |
| 73 | +// Instead of one slow proof with large inputs |
| 74 | +#[kani::proof] |
| 75 | +fn test_multiplication_slow() { |
| 76 | + let x: u64 = kani::any(); |
| 77 | + let y: u64 = kani::any(); |
| 78 | + |
| 79 | + // This might be too slow for the solver |
| 80 | + let result = x.saturating_mul(y); |
| 81 | + assert!(result >= x || x == 0); |
| 82 | +} |
| 83 | + |
| 84 | +// Split into multiple proofs with bounded inputs |
| 85 | +#[kani::proof] |
| 86 | +fn test_multiplication_small_values() { |
| 87 | + let x: u64 = kani::any_where(|x| *x <= 100); |
| 88 | + let y: u64 = kani::any_where(|y| *y <= 100); |
| 89 | + |
| 90 | + let result = x.saturating_mul(y); |
| 91 | + assert!(result >= x || x == 0); |
| 92 | +} |
| 93 | + |
| 94 | +// Insert harnesses for other ranges of `x` and `y` |
| 95 | +``` |
| 96 | + |
| 97 | +See this [tracking issue](https://github.com/model-checking/kani/issues/3006) for adding support for such partitioning automatically. |
| 98 | + |
| 99 | +### Use Stubs |
| 100 | + |
| 101 | +If a function has a complex body, consider using a [stub](./reference/experimental/stubbing.md) or a [verified stub](./reference/experimental/contracts.md) to stub the body with a simpler abstraction. |
| 102 | + |
| 103 | +### Disable Unnecessary Checks |
| 104 | + |
| 105 | +If you're focusing on functional correctness rather than safety, you may disable memory safety checks (run `kani --help` for a list of options to do so). Note that disabling these checks may cause Kani to miss undefined behavior, so use it with caution. |
| 106 | + |
| 107 | +Alternatively, to assume that all assertions succeed and only focus on finding safety violations, use the `--prove-safety-only` option. |
0 commit comments