Skip to content

Support pointer arithmetic in quantifier predicates - #4583

Merged
feliperodri merged 2 commits into
model-checking:mainfrom
feliperodri:quantifier-pointer-arithmetic
May 13, 2026
Merged

feliperodri merged 2 commits into
model-checking:mainfrom
feliperodri:quantifier-pointer-arithmetic

Conversation

@feliperodri

@feliperodri feliperodri commented Apr 22, 2026 •

Copy link
Copy Markdown
Member

Lower wrapping pointer arithmetic intrinsics (wrapping_add, wrapping_byte_offset) directly to CBMC pointer Plus expressions in the pure expression inliner. These intrinsics have no GOTO body to inline, so they need special handling.

Problem

Quantifier predicates that dereference pointers with offsets — e.g., *ptr.wrapping_byte_offset(i as isize) — failed because the inline_as_pure_expr inliner couldn't resolve pointer arithmetic intrinsics. These functions have no body in the symbol table (they're compiler intrinsics), so the inliner returned the original expression unchanged, leaving unresolved function calls that CBMC rejected.

Solution

In inline_call_as_pure_expr (goto_ctx.rs), before attempting to look up the function body, check if the function name matches a known wrapping pointer arithmetic intrinsic (wrapping_add, wrapping_byte_offset). If so, directly emit ptr.plus(offset) — the CBMC expression for pointer arithmetic. A type guard (arguments[0].typ().is_pointer()) prevents misapplication to non-pointer functions.

Non-wrapping variants (offset, add, arith_offset) are intentionally excluded because they trigger CBMC bounds checks inside quantifier bodies, which fail in the symbolic evaluation context.

Example

kani::forall!(|i in (0, len)| unsafe { *ptr.wrapping_byte_offset(i as isize) == 0 })

Changes

  • goto_ctx.rs: Recognize wrapping pointer arithmetic intrinsics and lower to ptr.plus(offset)
  • rfc/src/rfcs/0010-quantifiers.md: Document pointer arithmetic intrinsic lowering in the Detailed Design section
  • tests/kani/Quantifiers/pointer_arithmetic.rs: 4 harnesses covering wrapping_byte_offset (forall + exists) and wrapping_add (u32 + u8)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@feliperodri feliperodri added this to the Contracts milestone Apr 22, 2026
@feliperodri feliperodri added [C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-Contracts Issue related to code contracts labels Apr 22, 2026
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Apr 22, 2026
@feliperodri
feliperodri force-pushed the quantifier-pointer-arithmetic branch 4 times, most recently from b3ebaec to 546e834 Compare April 22, 2026 20:12
@feliperodri
feliperodri marked this pull request as ready for review April 22, 2026 20:14
@feliperodri
feliperodri requested a review from a team as a code owner April 22, 2026 20:14
Comment thread rfc/src/rfcs/0010-quantifiers.md Resolved
@feliperodri
feliperodri force-pushed the quantifier-pointer-arithmetic branch from 546e834 to 56e0b81 Compare May 13, 2026 05:37
Lower known pointer arithmetic intrinsics directly to CBMC pointer Plus
expressions in the pure expression inliner. These intrinsics have no
GOTO body to inline, so they need special handling in
inline_call_as_pure_expr.

Supported intrinsics:
- Element-offset variants: wrapping_add, wrapping_sub, wrapping_offset
- Byte-offset variants: wrapping_byte_offset, wrapping_byte_add,
  wrapping_byte_sub

Since CBMC pointer Plus scales the offset by the pointee size:
- Element-offset variants apply Plus directly (scaling matches).
- Byte-offset variants first cast the pointer to *u8 so Plus scales
  by 1 byte (matching Rust semantics that cast to u8 internally).
- Sub variants negate the offset before adding.

This enables quantifier predicates like:
  forall!(|i in (0, len)| unsafe { *ptr.wrapping_byte_offset(i as isize) == 0 })

Changes:
- goto_ctx.rs: Recognize pointer arithmetic intrinsics by name and emit
  correctly scaled ptr.plus(offset) expressions.
- rfc/0010-quantifiers.md: Document all supported variants and scaling
  rules in the Detailed Design section.
- tests/kani/Quantifiers/pointer_arithmetic.rs: 8 harnesses covering
  wrapping_byte_offset on u8 and u32 (scaling correctness),
  wrapping_add/sub on u32, wrapping_byte_sub, and exists/forall.
@feliperodri
feliperodri force-pushed the quantifier-pointer-arithmetic branch from 56e0b81 to d3192cb Compare May 13, 2026 18:25
@feliperodri
feliperodri enabled auto-merge May 13, 2026 19:58
@feliperodri
feliperodri added this pull request to the merge queue May 13, 2026
Merged via the queue into model-checking:main with commit 71ab36e May 13, 2026
32 of 34 checks passed
@feliperodri
feliperodri deleted the quantifier-pointer-arithmetic branch May 13, 2026 23:58
wodex1nhaoIeng pushed a commit to wodex1nhaoIeng/kani that referenced this pull request Nov 10, 2026
… checks (model-checking#5136)

## 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 model-checking#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

- Nested predicates containing detected checks or function calls,
including pointer dereferences with default null-pointer checks, are
conservatively rejected.
- **Non-nested predicate extraction can still discard default checks and
Rust assertions.** This is tracked separately in model-checking#5141.
- General branching and existential-fallback issues in model-checking#5004 and model-checking#5010
remain unresolved.
- Existing Z3 quantifier backend limitations remain unresolved.

## Related work

Addresses model-checking#4902 by replacing its original backend crash with an explicit
unsupported diagnostic, without claiming support for arbitrary nested
arithmetic.

Related issues: model-checking#5004, model-checking#5010, model-checking#5141.

Builds on model-checking#4567, model-checking#4578, and model-checking#4583.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-CompilerBenchCI Tag a PR to run benchmark CI Z-Contracts Issue related to code contracts Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants