Skip to content

Fix arith_offset for zero-sized pointees - #5202

Merged
feliperodri merged 3 commits into
model-checking:mainfrom
v3risec:fix/zst-arith-offset
Nov 21, 2026
Merged

feliperodri merged 3 commits into
model-checking:mainfrom
v3risec:fix/zst-arith-offset

Conversation

@v3risec

@v3risec v3risec commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Preserve the original pointer when arith_offset has a zero-sized Rust pointee.

CBMC 6.11.0's SMT2 backend leaves the pointer-plus element count unscaled when the pointee size is zero, effectively treating zero-sized pointees as one-byte elements. This affects all zero-sized pointees, including () and [u64; 0].

CVC5 reproduces the incorrect pointer offset on upstream main. The default SAT backend handles the same cases correctly.

Changes

  • Use Rust's pointee layout to select the zero-size case. The byte offset is count * 0, so the model returns the original pointer and retains its allocation and provenance.
  • Evaluate both call operands in order. A projected offset operand can read memory even when its value has no effect on the address.

Validation

Validated revision 6eacbe5 against upstream base c25a57bc with nightly-2026-11-18, CBMC 6.11.0, and CVC5 1.3.0.

  • The () and AlignedZst CVC5 harnesses fail their address checks on upstream main and pass with the fix. Equivalent runs with the default SAT solver pass on both versions.
  • All three ArithOffset regression files pass. The aligned ZST harness checks a literal zero offset, allocation identity, and write-back.
  • tests/expected/zst-arith-offset-operand/ reports the expected null pointer dereference on both versions. It guards evaluation of the projected offset operand in the ZST branch.

Related to #3589 and #4671.

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

Return the original pointer when a Rust pointee has zero size. Evaluate the pointer and offset in order so projected operand reads retain their checks.

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.

🟢 Approval recommended

No active review issues remain for the zero-sized-pointee offset handling change.

No open issues 🎉

What changed in this PR

Fixes arith_offset handling for zero-sized pointees in Kani’s CBMC backend, addressing pointer changes for over-aligned types on the SMT solver path.

Changes:

  • Use Rust’s pointee layout to identify zero-sized types and preserve the original pointer and provenance.
  • Evaluate both operands in order, retaining safety checks even when the offset cannot change the address.
  • Add regressions for pointer preservation, write-back, and null-pointer reads in offset operands.
File Description
tests/​kani/​Intrinsics/​ArithOffset/​offset_zst.rs Adds a CVC5 regression checking address, allocation, and write-back preservation.
tests/​expected/​zst-arith-offset-operand.rs Adds an explicit MIR regression ensuring the offset operand is evaluated.
tests/​expected/​zst-arith-offset-operand.expected Requires the operand regression to report a null-pointer dereference failure.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​intrinsic.rs Preserves zero-sized pointee pointers while evaluating both operands in order.

Review effort: Balanced

@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 for this fix. I reproduced the bug on main. With --solver cvc5, zst.wrapping_offset(n) == zst fails for any ZST pointee, including () and [u64; 0], not only over-aligned ones. The default SAT backend gets it right. The cause is in CBMC's SMT2 backend: smt2_conv.cpp only scales the pointer-plus index when element_size >= 2, so a zero-sized pointee is treated like a 1-byte one. Returning the original pointer for ZSTs matches what OffsetModel already does for offset, and matches the Rust semantics (byte offset = count * 0).

I also checked that the change does not weaken anything. Non-ZST wrapping_offset followed by an out-of-bounds dereference is still reported. The ZST result stays in the same allocation and writes through it are visible. Extreme and symbolic counts (isize::MIN/MAX, wrapping_add/wrapping_sub with a symbolic usize) leave the pointer unchanged with no spurious overflow check. A harness asserting p.wrapping_offset(1) != p for a ZST still fails as expected, so the checks are not vacuous.

A few small, non-blocking requests:

  • Could you update the PR description and the test to say the problem affects all ZST pointees under the SMT backends? "Over-aligned" undersells it. Adding a () case to offset_zst.rs would cover the common case.
  • Please add a sentence to the code comment explaining why the ZST special case exists (CBMC's SMT2 encoding of pointer arithmetic with zero-sized pointees), so it doesn't get "simplified" back to plus later.
  • Optional: it would be worth reporting the scaling issue upstream to diffblue/cbmc, since any plus on a ZST pointer is affected. BinOp::Offset codegen in rvalue.rs uses the same raw plus. I couldn't find a reachable ZST path there today, because OffsetModel returns early, but it is the same pattern.
  • Nit: tests/expected/zst-arith-offset-operand.rs is the only top-level file in tests/expected. Consider moving it to tests/expected/zst-arith-offset-operand/{main.rs,expected} to match the other tests. Also note that this test passes on main too, so it guards the operand evaluation rather than the bug itself. That's fine, but worth a line in its doc comment.

The BuildBundle-MacOs-x86_64 failure is the Homebrew issue on Intel macOS runners, unrelated to this PR.

@feliperodri feliperodri removed their assignment Nov 21, 2026
@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue labels Nov 21, 2026
Document CBMC's SMT2 encoding of zero-sized pointer arithmetic and place the operand evaluation guard in the standard expected-test directory.
@v3risec

v3risec commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@feliperodri Thanks for the thorough review! I've updated the PR in 6eacbe5:

  • Broader ZST coverage: Added a CVC5 regression for the ordinary () pointee while retaining the over-aligned AlignedZst case. Both fail on the upstream base and pass with the fix; the default SAT backend passes on both.
  • Documented the root cause: Added a code comment explaining the CBMC SMT2 zero-sized pointer-arithmetic issue and why Rust's ZST arith_offset must preserve the original pointer.
  • Standardized the expected test layout: Moved the projected-operand regression to tests/expected/zst-arith-offset-operand/{main.rs,expected} and clarified that it guards operand evaluation and its null-pointer safety check.
  • Updated the PR description: Clarified that the issue affects zero-sized pointees generally, not only over-aligned ZSTs, and documented the CVC5 versus default SAT results.

I also reported the underlying CBMC issue upstream: diffblue/cbmc#9546.

@feliperodri
feliperodri added this pull request to the merge queue Nov 21, 2026
Merged via the queue into model-checking:main with commit db890b2 Nov 21, 2026
32 of 34 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants