Skip to content

Fix nested quantifier capture projection and guard unsupported safety checks - #5136

Merged
feliperodri merged 4 commits into
model-checking:mainfrom
v3risec:fix/quantifier-capture-projection
Nov 9, 2026
Merged

feliperodri merged 4 commits into
model-checking:mainfrom
v3risec:fix/quantifier-capture-projection

Conversation

@v3risec

@v3risec v3risec commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Add type-checked simplification for captured values and field projections, restricted to expressions whose evaluation can safely be elided.
  • Improve recursive side-effect detection and scan GOTO statements and expressions for unsupported checks and calls.
  • Conservatively reject checked or call-containing nested predicates, including default checks, to avoid unsound successful verification.
  • Preserve quantifier source spans during recursive inlining and improve diagnostics for safety checks, missing GOTO bodies, and indirect calls.
  • Add explicit termination after fatal diagnostics and extract shared error messages into constants.
  • Document the limitations of exists!(... *ptr.add(i) == x) and nested pointer dereferences.
  • Add positive and negative regressions for public-macro captures, pointer comparisons, nested checks, and unsupported cases.

Regression evidence

On upstream main at d0b7082524140b889650c4012ca168dd4c462e04, the nested-capture semantic tests and the final pointer-comparison capture tests trigger a goto-cc invariant 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:

  • 120 related test items passed, 0 failed:
    • 48 compiler unit tests
    • 42 cprover_bindings unit tests
    • 14 Quantifiers tests
    • 14 quantifier-safety-checks UI tests
    • 2 quantifier-capture expected tests
  • Release development build, formatting, and workspace/tests Clippy (-D warnings) passed.
  • Additional targeted probes checked nested overflow, deeper nesting, user assertions, indirect calls, and generated GOTO/C expressions.

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

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.
@v3risec
v3risec force-pushed the fix/quantifier-capture-projection branch from 9bf239b to d984328 Compare November 8, 2026 04:09
@feliperodri feliperodri added the Z-Quantifiers Issues related to quantifiers label Nov 8, 2026
@feliperodri feliperodri added this to the Contracts milestone Nov 8, 2026

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. The guard rejects arithmetic_overflow, exact_div and finite_check, which Kani emits by default, but only when an extra UB flag is on. So a predicate like |i| i + 1 > 0 is rejected with -Z uninit-checks and 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.
  3. With either flag, any exists! whose body calls a function now errors, including the common unsafe { *ptr.add(i) == x } pattern. Please add that example to the limitations doc.

I added [C] Bug and [F] Soundness.

@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue labels Nov 8, 2026
@v3risec

v3risec commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@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 feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@feliperodri
feliperodri added this pull request to the merge queue Nov 9, 2026
Merged via the queue into model-checking:main with commit 1cdef01 Nov 9, 2026
22 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-Quantifiers Issues related to quantifiers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants