Repository navigation
Fix arith_offset for zero-sized pointees - #5202
Conversation
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.
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
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 tooffset_zst.rswould 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
pluslater. - Optional: it would be worth reporting the scaling issue upstream to diffblue/cbmc, since any
pluson a ZST pointer is affected.BinOp::Offsetcodegen inrvalue.rsuses the same rawplus. I couldn't find a reachable ZST path there today, becauseOffsetModelreturns early, but it is the same pattern. - Nit:
tests/expected/zst-arith-offset-operand.rsis the only top-level file intests/expected. Consider moving it totests/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.
Document CBMC's SMT2 encoding of zero-sized pointer arithmetic and place the operand evaluation guard in the standard expected-test directory.
|
@feliperodri Thanks for the thorough review! I've updated the PR in
I also reported the underlying CBMC issue upstream: diffblue/cbmc#9546. |
db890b2
Summary
Preserve the original pointer when
arith_offsethas 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
count * 0, so the model returns the original pointer and retains its allocation and provenance.Validation
Validated revision
6eacbe5against upstream basec25a57bcwithnightly-2026-11-18, CBMC 6.11.0, and CVC5 1.3.0.()andAlignedZstCVC5 harnesses fail their address checks on upstream main and pass with the fix. Equivalent runs with the default SAT solver pass on both versions.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.