Skip to content

Keep field reads as the value read, and forget a field written through another object - #567

Open
CatarinaGamboa wants to merge 2 commits into
mainfrom
fix/field-writes-soundness
Open

CatarinaGamboa wants to merge 2 commits into
mainfrom
fix/field-writes-soundness

Conversation

@CatarinaGamboa

@CatarinaGamboa CatarinaGamboa commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #438, fixes #565.

Root causes

#565: a copy of a field followed later writes. A read of a field of this object (f, this.f) was given the refinement _ == this#C.f, the field's own name. That name stands for the field's latest instance (Variable.getRefinement adds this#C.f == <last instance>), so int y = f; f = 5; made y == 5. Reads of locals already name the instance current at the read.

#438: o.x = 5 was applied to this.x. visitAssignment named every field write with Utils.qualifyFieldName, whatever the target, so the write became the new value of this.x. A compound write (o.x += 1, o.x--) also started from this.x's value.

Fix

  • visitCtFieldRead names the field's current instance, as a read of a local does. Each method and constructor gives every field that has no instance yet one on entry (snapshotFields, like snapshotWrittenParameters), so a read always names a fixed value.
  • A write through another object forgets this.f before the write, since other.f's old value is not this.f's, and again after it, since other may be this (forgetFieldOfOtherObject). The value before the write is shown as other.f, so other.level-- now reads found other.level@48 >= 0 && other.level == other.level@48 - 1.
  • updateGhostField (the ghost of a state-tracked int field, as in t.n = 1) keeps the instance written, not the field's name. Otherwise a later write to this.n would change n(t).

The first change exposed two related bugs, which are fixed here too:

  • Loops did not forget the fields they write. changedIn named a field this#f instead of this#C.f, so a loop that wrote a field without calling a method did not havoc it. f = 0; while (...) { k = f /* == 0 */; f = 1; } passed on main.
  • f = !f on a boolean field threw a NullPointerException. It happened anywhere, not only in loops (a finding from the Give a conditional expression the value of its condition, not its refinement #561 review). The same-variable branch of getUnaryOpRefinements looked the field up by its simple name. Field reads now take the general path. h.v-- on a field of another class also threw a NullPointerException on main and now works.

Tests

All tests pass: mvn test, 771 tests.

Other programs tried (outside the suite): each of these is rejected now.

  • writes through o, through an alias of o, and through o in a constructor and in an inner class (M.this.f vs o.f);
  • this.x = 5; o.x = 3; then this.x == 5;
  • this.lim = 5; o.lim += 1 and o.lim++ against _ < 7;
  • copies of f and this.f before writes by f =, this.f =, f++, f += 2, and static s = / C.s =;
  • the same copies before writes in if, switch, try, while, do, for, for-each, a lambda, and a loop that calls a method writing the field.

These still pass: writes to other.f of another class (h.v = 1; h.v++; h.v += 2), x = 5; o.y = 3 then x == 5, field initializers, if (f > 0) then f > 0, and f = 3; f++ then f == 4.

Several precision cases that failed on main now pass, e.g. int v = f; f = v + 1; then f - v == 1.

Behaviour changes

  • other.f = v makes this.f unknown (its declared refinement only), since other may be this.
  • A loop that writes a field now forgets it, keeping only its declared refinement. Loop invariants are inferred only for int locals, so f = 0; while (...) { f = 1; } followed by f >= 0 is rejected when f has no refinement. Main accepted it only because the loop did not forget f.
  • A copy of a field taken before an if that writes the field in one branch is no longer known to equal the field after the if when the other branch leaves it alone. This is the existing if join, which copies refinements rather than equating instances. Parameters already behaved this way. The previous "pass" came from the aliasing bug.

Overlap with #393

The draft #393 also gives non-final fields a fresh instance at method entry (forgetMutableFieldValues). Both edit visitCtMethod. If #393 lands, snapshotFields simply finds those fields already have an instance and skips them.

🤖 Generated with Claude Code

…h another object

A tracked field read named the field itself, which stands for its latest
instance, so a copy of the field followed later writes. Field reads now name
the current instance, and each method gives the fields without one an
instance on entry. A write through another object (other.f = v, other.f++)
was applied to this.f; this.f is now forgotten before (its old value is not
other.f's) and after the write (other may be this). The ghost of a state
tracked int field keeps the instance written.

Loops now havoc the fields they write, which were named this#f instead of the
qualified name, and f = !f on a field no longer throws a NullPointerException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 1

Codex (codex exec) ran 32 small programs, each against this branch and main. The run stopped early on an API content flag, so these are its results up to that point.

Should be rejected (15 programs): all are rejected on this branch. On main, 9 of them passed:

  • other.f = v then reading this.f;
  • other.f += 1, other.f -= 1 and other.f++ starting from this.f's value;
  • loops that write a field;
  • a copy of a field read before a write.

Correct programs rejected on this branch:

Program Main Relevant?
P other = this; f = 1; other.f = 2; then f == 2 (also with other.f++) passes No. This is the intended loss: a write through another variable forgets this.f, since other may or may not be this. Main passed only because it applied every other.f write to this.f.
f = 0; while (n > 0) { f = 1; n--; } then f >= 0 passes No, but it is a behaviour change. Loops now forget the fields they write (main named them wrongly and kept them), and loop invariants are inferred only for int locals. Main accepted it through the same naming bug that also accepted f = 0; while (...) { k = f /* == 0 */; f = 1; }. Added to the PR body.
other.f = 2; then other.f == 2 rejected No: fields of other objects are not tracked.
int x = f++; then x == 1, and int x = ++f; then x == 2 rejected Not this PR. The value of a field increment as an expression is not modelled, same as on main. Listed as a separate finding.
if (other == this) { ... } Sorts P and Bool are incompatible Not this PR: it is the same on main.

Nothing relevant to fix from this round.

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 2

Codex ran 36 programs against this branch and main. They covered constructors and this(...), static fields and methods, inner classes and C.this.f, lambdas, try/finally, switch expressions, break/continue, final fields, field initializers, refinements that mention other fields, compound assignments in conditions, and writes through fresh objects.

No program that should be rejected passes on this branch. Two loops, f = 1; while (true) { f = 2; break; } and the same with continue, followed by f == 1, passed on main. They are now rejected.

Finding: a write to a fresh object inside a loop forgets this.f.

class C { int f; void test(int n){ C other = new C(); f = 1; while (n > 0) { other.f = 2; n--; } @Refinement("_ == 1") int x = f; } }

This branch rejects it; main accepts it. other is freshly allocated, so it cannot be this. But the checker has no alias information for locals: other may later be reassigned, for example to this. So a write through any object other than this forgets this.f. The straight-line form (other.f = 2; then f == 1, without the loop) is rejected on main too. There, other.f = 2 was applied to this.f. Main accepted the loop form only because loops did not forget fields they write. Keeping this.f would need knowledge that other is not this. Not relevant for this PR: the result is sound, only less precise.

Same on both (not this PR):

Nothing relevant to fix from this round.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Clean-code review

Audit of the diff using the clean-code-principles checklist.

  • RefinementTypeChecker.java snapshotFields: [core-dry]. It rebuilt the instance by hand, the same way forgetChanges does: a fresh instance with the declared refinement, then attached to the variable. Applied: it now calls forgetChanges, so each field is still forgotten to its declared refinement on entry (3 lines instead of 9).
  • RefinementTypeChecker.java forgetFieldOfOtherObject(..., boolean before): [func-few-arguments], a flag argument. The flag only adds the other.f display name to the value before the write. Two methods would repeat the guard and the forgetChanges call. Kept as is: the Javadoc explains the before/after pair.
  • OperationsChecker.java getRefinementUnaryVariableWrite and RefinementTypeChecker.forgetFieldOfOtherObject: [core-dry]. Both test write instanceof CtFieldWrite && !Utils.accessesTrackedField(write), the same test as Utils.untrackedWriteReceiver. It is a single line, so a new helper would not pay off. Kept.
  • AuxStateHandler.updateGhostField: [core-kiss]. One substitution after the state is built, with a comment saying why. Fine as is.
  • OperationsChecker.getUnaryOpRefinements: excluding field reads keeps the existing same-variable branch for locals, and the comment says why fields go the general way. Fine as is.

Re-checked after the change: mvn test passes, and the study check matches the redesign baseline again: identical verdicts and messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant