Skip to content

Simplify casts of integer-address pointer constants to integers - #9238

Merged
feliperodri merged 1 commit into
diffblue:developfrom
feliperodri:simplify-pointer-constant-typecast
Oct 26, 2026
Merged

feliperodri merged 1 commit into
diffblue:developfrom
feliperodri:simplify-pointer-constant-typecast

Conversation

@feliperodri

Copy link
Copy Markdown
Collaborator

Why

Values read from shadow memory of a pointer-typed object never become constants during symbolic execution, so goto-symex explores branches on them that can never be taken. A get_field on such an object yields (unsignedbv)c, where c is a pointer constant holding the field value repeated over the pointer's bytes. simplify_typecast folds this cast for NULL but not for other integer-address constants.

The cost is real. We hit this in Kani, which is adding Tree Borrows aliasing checks that keep the tags of pointers stored in memory in shadow memory. Every loaded tag stayed symbolic, so the monitor's branches were all explored. On our tests, harnesses whose stack objects hold pointers got up to 12x slower with this missing, and some ran out of 12 GB. Heap objects were unaffected, because their shadow is a byte array. The new regression test is a minimal version: 955 symex steps and 100 VCCs remaining without the fold, 48 steps and 0 VCCs with it.

Change

(τ)c is rewritten to the bit-vector constant with c's bits when:

  • c is a non-NULL pointer constant_exprt;
  • τ is unsignedbv, signedbv or bv;
  • τ has the same width as c.

This matches the encoding in both back ends:

  • bv_pointerst::convert_pointer_type encodes a non-NULL pointer constant as build_constant(bvrep2integer(value)), and a pointer-to-integer cast as zero_extension, which is the identity at equal width.
  • smt2_convt prints the constant as (_ bvK w) and the cast as ((_ extract w-1 0) ·).

So the rewrite replaces a closed term by one with the same value. Symbolic execution can only prune branches that are infeasible either way.

Unaffected:

  • NULL, address_of and other non-constant pointers;
  • casts that change width.

Where I'd like input

  • Is simplify_typecast the right place? The alternative is to make shadow memory avoid pointer-typed shadow cells, which I haven't explored.
  • I kept the rule to equal widths. Folding wider casts through zero extension would be just as sound, but I had no use for it.

Testing

  • The new unit test in unit/util/simplify_expr.cpp fails without the change and passes with it. All [core][util] unit tests pass.
  • The new regression/cbmc-shadow-memory/pointer-object-constant1 fails without the change and passes with it. It checks that the infeasible branch leaves nothing in --program-only.
  • regression/cbmc-shadow-memory and regression/cbmc-primitives pass.
  • regression/cbmc: 1143 pass. 4 fail identically with and without the change, because the host's gcc 7 lacks -std=gnu2x (_BitInt1, predefined-constants1, static_assert1, typeof2).

  • Each commit message has a non-empty body, explaining why the change was made.
  • Regression tests are included, in the same commit as the fix.
  • My PR is restricted to a single bugfix.
  • No unrelated white-space or formatting changes.

simplify_typecast left (uintN)c alone when c is a non-NULL pointer constant
of width N. Both back ends encode such a constant by the bits of its value,
and a cast from a pointer to a bit vector of the same width as the identity,
so the cast is the bit-vector constant with the same bits.

Shadow memory produces these casts. The shadow of a pointer-typed object
holds the field's value replicated over the pointer's bytes, as a pointer
constant, and get_field reads it back through a cast to a bit vector. Because
the cast was not folded, a value read from shadow memory was a term rather
than a constant during symbolic execution, and branches on it were explored
even when they could not be taken.

NULL, addresses of objects, non-constant pointers and casts that change the
width are unaffected.
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.91%. Comparing base (8fcda33) to head (5a3c19e).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #9238   +/-   ##
========================================
  Coverage    80.91%   80.91%           
========================================
  Files         1718     1718           
  Lines       190292   190320   +28     
  Branches        73       73           
========================================
+ Hits        153974   153999   +25     
- Misses       36318    36321    +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@peterschrammel

peterschrammel commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

The alternative is to make shadow memory avoid pointer-typed shadow cells, which I haven't explored.

IMO, this would complicate things unnecessarily. For example, you would need to redeclare pointer-free versions of structs containing pointer-typed fields.

@feliperodri
feliperodri merged commit 5783f86 into diffblue:develop Oct 26, 2026
43 checks passed
// back-ends encode both (bv_pointerst::convert_pointer_type and
// smt2_convt::convert_constant); shadow memory of pointer-typed objects
// yields such casts
if(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try to find out whether this is at all guaranteed by the standard.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard doesn't guarantee it: integer<-->pointer conversions are implementation-defined (C11 6.3.2.3p5–6), and the only round trip it promises is pointer -> uintptr_t -> pointer (7.20.1.4). The fold relies on CBMC's own definition instead, and the simplifier has to agree with the back-ends. All three already encode a non-null pointer constant by its value bits and a same-width pointer-to-integer cast as the identity:

  • bv_pointers: build_constant for the constant, zero_extension for the cast
  • smt2_conv: (_ bvV W) and extract (W-1) 0
  • incremental SMT: a bit-vector literal of the value

so the solver already gave (uintN)c the bits of c; the patch makes symex see that too. That matches GCC's and Clang's documented behaviour (bits unchanged at equal width). If CBMC ever encodes integer-address pointers differently, for example as object/offset pairs, this fold has to change with it. I can add a comment saying so, or a unit test that pins the fold to the bv_pointers result.

feliperodri added a commit to feliperodri/kani that referenced this pull request Nov 3, 2026
Seven aliasing tests take 2 to more than 8 GB and up to 6 minutes each
with CBMC 6.11.0, because without diffblue/cbmc#9238 the tags read from
shadow memory stay symbolic. Four of them in parallel exhaust a CI
runner's memory, which killed the regression job. Move them to a `fixme`
directory, which compiletest skips, until Kani's CBMC includes #9238.
The other 69 aliasing tests pass with CBMC 6.11.0 in 4 minutes.
tautschnig added a commit to tautschnig/cbmc that referenced this pull request Nov 23, 2026
diffblue#9238 folds (uintN)c, with c a pointer constant of width N, to the
integer with c's bits. The rule accepted every pointer constant other
than NULL. Pointer constants need not hold an address, though:
pointer_logict builds "INVALID" and "INVALID-<n>", and the incremental
SMT back-end uses ID_invalid. constant_exprt::check permits such values
for pointer types only, so the fold produced integer constants that fail
validation, and any later to_constant_expr, numeric_cast or
bvrep2integer on them would abort or misbehave. Fold only constants
whose value is a bit-vector representation. Put that test in a helper,
is_integer_address_constant, so that other simplifier rules that convert
such constants to bits can share one definition.

The fold is not implied by C either: C11 6.3.2.3p6 leaves
pointer-to-integer conversions implementation-defined. It is correct
only because every back-end encodes such a constant by its value bits
and a cast to an integer of the same width as the identity. The code
comment named only two of the three back-ends, did not say that the
result is CBMC's own choice, and did not record why the rule must stay
at equal width: for wider casts bv_pointerst zero-extends while
smt2_convt sign-extends, so any fold would disagree with one of them.
Nothing would fail if the pointer encoding changed. Extend the comment,
and add a unit test that checks with bv_pointerst that each cast equals
its simplified form, for values with and without the sign bit set. Also
test that a cast to a wider integer is not folded, and add a regression
test covering shadow memory of pointers that are struct members, union
members and array elements, which the existing regression test did not
cover: without the fold, three of its assertions remain for the solver.

Co-authored-by: Kiro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants