Repository navigation
Fix OffsetModel missing UB for offsets wrapping CBMC's pointer encoding - #4671
Merged
feliperodri merged 2 commits intoAug 14, 2026
Merged
Conversation
CBMC encodes pointers as an (object, offset) pair where the offset field has pointer_width - object_bits bits. Pointer arithmetic therefore wraps offsets at 2^(64 - object_bits) rather than 2^64 (model-checking#1150). For the OffsetModel this meant that a symbolic byte offset that is a non-zero multiple of 2^(64 - object_bits) (2^48 with Kani's default of 16 object bits) made `wrapping_byte_offset` alias the original pointer, so the subsequent same-allocation safety check passed spuriously and genuine out-of-bounds `offset` calls verified successfully - including "proving" the false fact that the result equals the base pointer. Detect the wrap by additionally requiring that the address of the result is consistent with full-width integer arithmetic on the address of the original pointer; any truncation in the pointer addition falsifies this equation. Two alternative formulations do not work: * Computing the new pointer via integer arithmetic (addr().wrapping_add_signed(off) followed by a cast back, as previously suggested in a FIXME here) is sound, but makes the solver case-split the integer address over every object; harnesses using e.g. `sort()` then run out of memory. These are the "unexpected failures" mentioned in the removed comment (reproduced on: expected/cover/cover-pass, expected/string-repeat, expected/loop-backedge, expected/shadow/unsupported_num_objects). * Comparing POINTER_OFFSET(new) against full-width POINTER_OFFSET(orig) + offset is defeated by CBMC's simplifier, which rewrites POINTER_OFFSET(p + k) to POINTER_OFFSET(p) + k in full-width arithmetic, making the equation trivially true. The `addr()` transmute is opaque to that simplification, keeps all operations in the pointer domain, and adds negligible solver cost: the full expected (459) and kani (593) suites pass unchanged. The underlying encoding wrap is fixed on the CBMC side in diffblue/cbmc#9134 (out-of-range results are directed to the invalid object). The equation here also states Rust's address-arithmetic contract for `offset` and produces the same verdicts with and without that CBMC fix, so it is kept as a semantic guard rather than a version-specific workaround. Partially addresses model-checking#1150: this fixes the missed-UB soundness hole in the checked `offset`/`arith_offset` intrinsic model. Not addressed is the `wrapping_offset` family, which Kani codegens to plain CBMC pointer addition: under CBMC versions without the encoding fix its result aliases the base pointer for wrap-multiple offsets (tests/kani/PointerOffset/fixme_wrap_nonzero_offset.rs), and even with the CBMC fix the result address is indeterminate rather than the exactly-wrapped address that Rust defines. Exact modeling would require integer arithmetic plus an integer-to-pointer cast, which makes the solver case-split over all objects (see above). Co-authored-by: Kiro <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request strengthens Kani’s OffsetModel safety checking to detect and reject undefined behavior that was previously missed due to CBMC’s pointer encoding truncating/wrapping the offset field (issue #1150), and adds a regression test to ensure the soundness hole stays closed across CBMC versions.
Changes:
- Add an address-consistency guard (
addr()-based) to detect offset truncation/wrap in CBMC’s pointer encoding before relying on same-allocation checks. - Add a new expected-suite regression test covering symbolic high-offset wrap cases (positive, negative, and
isize::MIN), plus an in-bounds control harness. - Add corresponding
.expectedoutput asserting the new failures are reported.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/expected/offset-bounds-check/offset_wrap_sym.rs | New regression harnesses exercising the high-offset wrap soundness hole and a valid in-bounds negative-offset case. |
| tests/expected/offset-bounds-check/offset_wrap_sym.expected | Expected output for the new regression tests (3 failures, 1 success). |
| library/kani_core/src/models.rs | Tightens OffsetModel UB detection by adding an address-arithmetic consistency check alongside same-allocation checking. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
feliperodri
approved these changes
Jul 29, 2026
feliperodri
left a comment
Member
There was a problem hiding this comment.
I'd implement the Copilot comments before merging it.
…checks - Replace `orig_ptr.addr().wrapping_add(byte_offset as usize)` with `wrapping_add_signed(byte_offset)` so the signed-cast dependence is spelled out in the primitive rather than left implicit in a two's- complement cast (byte_offset is isize). - Split the conjoined safety check into two separate `safety_check`s with distinct messages, so a wrap-arithmetic failure is no longer reported as a same-allocation escape. Update the affected .expected outputs: offset_wrap_sym (all three wrap harnesses), out_of_bounds_ub_check (both harnesses were wrap cases in disguise because CBMC encodes negative offsets by adding 2^offset_bits), and start_from_oob (the return-to-original-object case is also detected by the address-arithmetic mismatch, not by same-allocation). Signed-off-by: Felipe Monteiro <[email protected]>
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Aug 13, 2026
Merged
via the queue into
model-checking:main
with commit Aug 14, 2026
fb037f3
33 of 37 checks passed
feliperodri
added a commit
to model-checking/verify-rust-std
that referenced
this pull request
Sep 23, 2026
The offset/add/sub contracts bounded `count` with `same_allocation(self, self.wrapping_*(count))`. CBMC represents a pointer as an (allocation, offset) pair whose offset field is narrower than the address space, so pointer arithmetic truncates byte offsets that are multiples of that field's range and aliases the result back into the original allocation. The same_allocation check then held spuriously, so the preconditions did not in fact bound count to the allocation and genuinely out-of-bounds offsets verified. model-checking/kani#4671 added a safety check to the offset model that detects the truncation, which surfaced this as 193 failing harnesses across ptr::const_ptr, ptr::mut_ptr and ptr::non_null. The harnesses were unsound before, not broken by that change. Add `byte_offset_does_not_wrap` and require it ahead of each same_allocation conjunct, so same_allocation is asked about a pointer that has not been truncated. No harness stubs these contracts (`stub_verified`), so the stronger precondition only affects their own proof_for_contract harnesses. Verified locally with the pinned Kani (783e7f7): all 193 previously-failing harnesses pass, and all 375 harnesses under `ptr::` pass. Confirmed non-vacuous by temporarily asserting `count == 0` in the generated harnesses, which fails as expected.
calef
pushed a commit
to calef/kani
that referenced
this pull request
Nov 23, 2026
## 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 model-checking#3589 and model-checking#4671. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Co-authored-by: Mingzhi Gao <[email protected]> Co-authored-by: v3risec <[email protected]> Co-authored-by: Felipe R. Monteiro <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CBMC encodes pointers as an (object, offset) pair where the offset field has pointer_width - object_bits bits. Pointer arithmetic therefore wraps offsets at 2^(64 - object_bits) rather than 2^64 (#1150). For the OffsetModel this meant that a symbolic byte offset that is a non-zero multiple of 2^(64 - object_bits) (2^48 with Kani's default of 16 object bits) made
wrapping_byte_offsetalias the original pointer, so the subsequent same-allocation safety check passed spuriously and genuine out-of-boundsoffsetcalls verified successfully - including "proving" the false fact that the result equals the base pointer.Detect the wrap by additionally requiring that the address of the result is consistent with full-width integer arithmetic on the address of the original pointer; any truncation in the pointer addition falsifies this equation. Two alternative formulations do not work:
Computing the new pointer via integer arithmetic (addr().wrapping_add_signed(off) followed by a cast back, as previously suggested in a FIXME here) is sound, but makes the solver case-split the integer address over every object; harnesses using e.g.
sort()then run out of memory. These are the "unexpected failures" mentioned in the removed comment (reproduced on: expected/cover/cover-pass, expected/string-repeat, expected/loop-backedge, expected/shadow/unsupported_num_objects).Comparing POINTER_OFFSET(new) against full-width POINTER_OFFSET(orig) + offset is defeated by CBMC's simplifier, which rewrites POINTER_OFFSET(p + k) to POINTER_OFFSET(p) + k in full-width arithmetic, making the equation trivially true.
The
addr()transmute is opaque to that simplification, keeps all operations in the pointer domain, and adds negligible solver cost: the full expected (459) and kani (593) suites pass unchanged.The underlying encoding wrap is fixed on the CBMC side in diffblue/cbmc#9134 (out-of-range results are directed to the invalid object). The equation here also states Rust's address-arithmetic contract for
offsetand produces the same verdicts with and without that CBMC fix, so it is kept as a semantic guard rather than a version-specific workaround.Partially addresses #1150: this fixes the missed-UB soundness hole in the checked
offset/arith_offsetintrinsic model. Not addressed is thewrapping_offsetfamily, which Kani codegens to plain CBMC pointer addition: under CBMC versions without the encoding fix its result aliases the base pointer for wrap-multiple offsets (tests/kani/PointerOffset/fixme_wrap_nonzero_offset.rs), and even with the CBMC fix the result address is indeterminate rather than the exactly-wrapped address that Rust defines. Exact modeling would require integer arithmetic plus an integer-to-pointer cast, which makes the solver case-split over all objects (see above).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.