Skip to content

Support pointer arithmetic in quantifier predicates#4583

Open
feliperodri wants to merge 1 commit into
model-checking:mainfrom
feliperodri:quantifier-pointer-arithmetic
Open

Support pointer arithmetic in quantifier predicates#4583
feliperodri wants to merge 1 commit into
model-checking:mainfrom
feliperodri:quantifier-pointer-arithmetic

Conversation

@feliperodri
Copy link
Copy Markdown
Contributor

@feliperodri feliperodri commented Apr 22, 2026

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 kani-compiler/src/codegen_cprover_gotoc/context/goto_ctx.rs Outdated
Comment thread rfc/src/rfcs/0010-quantifiers.md Outdated
@feliperodri feliperodri force-pushed the quantifier-pointer-arithmetic branch from 546e834 to 56e0b81 Compare May 13, 2026 05:37
Comment on lines +560 to +561
let base = if is_byte { ptr.cast_to(Type::unsigned_int(8).to_pointer()) } else { ptr };
return base.plus(offset);
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.

You need to cast back to the original pointer type after the offset:

Suggested change
let base = if is_byte { ptr.cast_to(Type::unsigned_int(8).to_pointer()) } else { ptr };
return base.plus(offset);
let base = if is_byte { ptr.cast_to(Type::unsigned_int(8).to_pointer()) } else { ptr };
let result = base.plus(offset);
return if is_byte { result.cast_to(arguments[0].typ().clone()) } else { result };

It seems to be working now for some reason (maybe CBMC is doing it automatically?), but it might produce the wrong results in other cases.

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
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