Skip to content

Index the outer array first for a C + K * i byte offset - #9627

Draft
tautschnig wants to merge 2 commits into
diffblue:developfrom
tautschnig:kiro/subexpression-at-symbolic-offset
Draft

tautschnig wants to merge 2 commits into
diffblue:developfrom
tautschnig:kiro/subexpression-at-symbolic-offset

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

For an offset C + x, get_subexpression_at_offset() first looked at the
member at offset C and, when that was element 0 of an inner array, indexed
that inner array with x. When x is K * i with K a multiple of the outer
element size, x selects the outer element instead: i * 16 + 8 into an array
of { uint64_t t; uint8_t node[8]; } became a[0].node[i * 16] rather than
a[i].node[0]. A read then gave an unconstrained value and a write was
lost, so false assertions verified (#9391).

For K * i + C and C + K * i (K a multiple of the element size, C a
constant), index the array with K / elem_size * i + C / elem_size and look
at offset C % elem_size within that element. Offsets with a non-constant
remainder are resolved as before.

The KNOWNBUG tests are the remaining case: a byte offset that, unlike a
typed in-bounds access, crosses the element or member it is resolved into.

This is @ocochard's proposed change cherry-picked from d5a4344 on branch fix-subexpression-at-symbolic-offset
of https://github.com/ocochard/cbmc (the fix proposed in #9391), with the
offsets adapted to bytest, which develop now uses.

Co-authored-by: Kiro kiro-agent@users.noreply.github.com

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

ocochard and others added 2 commits December 1, 2026 05:46
For an offset C + x, get_subexpression_at_offset() first looked at the
member at offset C and, when that was element 0 of an inner array, indexed
that inner array with x.  When x is K * i with K a multiple of the outer
element size, x selects the outer element instead: i * 16 + 8 into an array
of { uint64_t t; uint8_t node[8]; } became a[0].node[i * 16] rather than
a[i].node[0].  A read then gave an unconstrained value and a write was
lost, so false assertions verified (diffblue#9391).

For K * i + C and C + K * i (K a multiple of the element size, C a
constant), index the array with K / elem_size * i + C / elem_size and look
at offset C % elem_size within that element.  Offsets with a non-constant
remainder are resolved as before.

The KNOWNBUG tests are the remaining case: a byte offset that, unlike a
typed in-bounds access, crosses the element or member it is resolved into.

Cherry-picked from d5a4344 on branch fix-subexpression-at-symbolic-offset
of https://github.com/ocochard/cbmc (the fix proposed in diffblue#9391), with the
offsets adapted to bytest, which develop now uses.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
A write through a pointer into a three-dimensional array with a symbolic
outer index was lost, and a read gave an unconstrained value: for
char c[2][3][10], d = c[n][1] is at byte offset n * 30 + 10, and
get_subexpression_at_offset() resolved d[0] as c[0][1][n * 30] instead of
c[n][1][0]. cbmc then failed the true assertion c[n][1][0] == 'i' and
proved the false one n == 0 || c[1][1][0] != 'i'. The same happens for a
two-dimensional array member of a struct array element. The cause is the
one of diffblue#9391 (the "C + x" branch indexing the inner array at offset C with
x), fixed by the previous commit; these tests cover the cases with arrays
of arrays.

CBMC's own SMT solver (--cprover-smt2) fails these assertions also when
the program accesses c[n][2][0] directly, without a pointer, and the
incremental SMT back end does not support the struct, so the tests are
tagged broken-cprover-smt-backend and no-new-smt accordingly.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@kroening

kroening commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Can we consolidate the tests?

@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.50746% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.34%. Comparing base (3e1f386) to head (76c285a).
⚠️ Report is 36 commits behind head on develop.

Files with missing lines Patch % Lines
src/util/pointer_offset_size.cpp 97.29% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9627      +/-   ##
===========================================
+ Coverage    81.29%   81.34%   +0.04%     
===========================================
  Files         1725     1727       +2     
  Lines       191680   191764      +84     
  Branches        73       73              
===========================================
+ Hits        155835   155987     +152     
+ Misses       35845    35777      -68     

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

tautschnig added a commit to tautschnig/cbmc that referenced this pull request Dec 3, 2026
The wrong proof of ~/bench/defects/union-copy-stale (also on upstream
develop 2c585e2e9dd and cce28f5): with a global union x of an array
member, `x.a[i] = 5; y = x;` at a non-constant i gave y the union's value
from before the write, so that `assert(y.a[i] != 5)` was proved and
`assert(y.a[i] == 5)` failed. Field sensitivity divided x into the
symbols of its members, the write updated only that of x.a, and the copy
took the symbol of x as a whole. Local unions and constant indices were
not affected, and neither the size of the array (also 600 elements, above
field sensitivity's limit on the elements it divides) nor
--max-field-sensitivity-array-size 0 mattered.

PR diffblue#9544's second commit (cherry-picked as 6fab9e3: a write to a
member of a union updates the union as a whole) fixes it; diffblue#9627 does not
(its head, 76c285a, gives the same wrong proof). The test fails with
the build of 2c86ff0 and passes now.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>

This branch has not been deployed

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