Repository navigation
Fix nested quantifier capture projection and guard unsupported safety checks - #5136
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved findings identify unsound existential verification and a compiler crash with extra UB flags enabled, plus missing nested-capture coverage through the public quantifier macros.
3 open issues
What changed in this PR
This PR improves nested quantifier capture handling in Kani and adds conservative rejection when enabled extra undefined-behavior checks cannot be preserved in predicates.
Changes:
- Add type-checked capture projection and recursive side-effect detection.
- Scan predicate expressions and helper bodies for unsupported safety checks.
- Add semantic and diagnostic regressions, and document the limitation.
| File | Description |
|---|---|
tests/ui/quantifier-safety-checks/uninit.rs |
Tests rejection of uninitialized reads in nested predicates. |
tests/ui/quantifier-safety-checks/uninit.expected |
Records the expected unsupported diagnostic. |
tests/ui/quantifier-safety-checks/invalid_value.rs |
Tests rejection of invalid boolean reads in predicates. |
tests/ui/quantifier-safety-checks/invalid_value.expected |
Records the expected unsupported diagnostic. |
tests/ui/quantifier-safety-checks/helper_validity.rs |
Tests invalid-value checks inside helpers. |
tests/ui/quantifier-safety-checks/helper_validity.expected |
Records the helper rejection diagnostic. |
tests/ui/quantifier-safety-checks/helper_uninit.rs |
Tests uninitialized reads in helpers with unused results. |
tests/ui/quantifier-safety-checks/helper_uninit.expected |
Records the helper rejection diagnostic. |
tests/kani/Quantifiers/nested_capture_semantics.rs |
Adds positive and negative nested-capture tests. |
tests/kani/Quantifiers/nested_capture_pointer.rs |
Tests nested pointer captures and witness semantics. |
tests/kani/Quantifiers/helper_safety_checks.rs |
Tests valid helpers, false predicates, and unused results. |
tests/expected/quantifier-capture/uninit.rs |
Tests initialization checks before closure construction. |
tests/expected/quantifier-capture/uninit.expected |
Records failing and passing initialization results. |
tests/expected/quantifier-capture/invalid_value.rs |
Tests validity checks before closure construction. |
tests/expected/quantifier-capture/invalid_value.expected |
Records failing and passing validity results. |
kani-compiler/src/codegen_cprover_gotoc/overrides/mod.rs |
Exposes shared safety-scanning utilities. |
kani-compiler/src/codegen_cprover_gotoc/overrides/hooks.rs |
Applies capture substitution and scans predicate safety checks. |
kani-compiler/src/codegen_cprover_gotoc/context/goto_ctx.rs |
Checks helper bodies before quantifier inlining. |
docs/src/reference/experimental/quantifiers.md |
Documents conservative safety-check rejection. |
cprover_bindings/src/goto_program/expr.rs |
Adds guarded capture projection, side-effect detection, and unit tests. |
Review effort: Balanced
Reduce capture projections only along successful parameter substitutions. Use declared field components and a conservative evaluation whitelist; retain pointer reads, indexing, computations and ordinary nondeterminism. Recurse through ReadOk and quantifiers during side-effect classification. With extra UB checks enabled, reject predicate extraction that would discard instrumented checks. Traverse statements and expressions exhaustively, including nested statement expressions and all four safety property classes. Preserve unused helper calls and inspect callee bodies during quantifier inlining. Reject existential fallback under these flags to avoid CBMC #9189, and diagnose calls with no available GOTO body instead of panicking. Cover AST preservation and scanner regressions, move and public-macro borrow captures, real pointer solver proofs, finite positive and negative quantifiers, invalid and uninitialized captures, checked helpers and check-free controls. Add separate extra-UB regressions for unused existential helper calls and bodyless intrinsics, and document the conservative support boundary.
9bf239b to
d984328
Compare
feliperodri
left a comment
There was a problem hiding this comment.
The capture simplification is careful: field elision is limited to symbols and constants, and the unit tests cover every unsafe shape. The guard fails closed. All three Copilot threads are fixed, and I resolved them.
Before merge:
- Please link the issue this fixes, and say what the nested-capture tests do on
main(crash, wrong verdict, or bad GOTO). Today the description doesn't show that the new tests fail without the change. - The guard rejects
arithmetic_overflow,exact_divandfinite_check, which Kani emits by default, but only when an extra UB flag is on. So a predicate like|i| i + 1 > 0is rejected with-Z uninit-checksand accepted without it, and in the accepted case the overflow check is dropped. Please confirm this is intended, and open a tracking issue for default checks dropped from predicates. - With either flag, any
exists!whose body calls a function now errors, including the commonunsafe { *ptr.add(i) == x }pattern. Please add that example to the limitations doc.
I added [C] Bug and [F] Soundness.
|
@feliperodri Thanks for the review! I've addressed the feedback:
120 related tests passed locally, along with build, formatting, and Clippy checks. Thanks again! |
feliperodri
left a comment
There was a problem hiding this comment.
Thanks, all addressed. The PR body shows the crashes on main, #5141 tracks the default-check gap, and the new nested-predicate check turns the overflow false success into an error even without extra flags. Errors now point at the quantifier, and the ptr.add example is in the docs. The compile-timer-short failure is noise: +1.2% total, on crates without quantifiers.
1cdef01


Summary
Fix nested quantifier capture projection while conservatively rejecting predicates whose checks or function calls could be lost during extraction.
This improves nested capture handling without relaxing the existing projection safety restrictions, and replaces several backend failures with source-located diagnostics.
Changes
exists!(... *ptr.add(i) == x)and nested pointer dereferences.Regression evidence
On upstream
mainatd0b7082524140b889650c4012ca168dd4c462e04, the nested-capture semantic tests and the final pointer-comparison capture tests trigger agoto-ccinvariant abort (quantifier must not contain side effects). With the PR changes, the corresponding positive and negative solver tests produce the expected results.The original reproducer from #4902 also crashes on
main. It now produces an explicit unsupported diagnostic rather than a backend abort. This does not add general support for nested arithmetic predicates.An additional nested overflowing-addition case incorrectly verified with the earlier PR implementation. The follow-up conservatively rejects it, even without extra UB flags.
Pure existential predicates remain supported, including with both extra UB options enabled.
Testing
At tested commit
d2cb6388cb464bfa5a047c348cb2b29b6dfe198a:cprover_bindingsunit tests-D warnings) passed.The PR branch was subsequently updated with
main; the results above refer to the explicitly tested commit, not an independent rerun of the latest HEAD.Full repository CI and the complete Challenge 21 proofs have not been run locally.
Limitations
&&,||orifkeep only one branch, so a trueforall!can fail andassume(forall!(..))can make a harness vacuous #5004 and Bugs in Quantifier feature #5010 remain unresolved.Related work
Addresses #4902 by replacing its original backend crash with an explicit unsupported diagnostic, without claiming support for arbitrary nested arithmetic.
Related issues: #5004, #5010, #5141.
Builds on #4567, #4578, and #4583.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.