Skip to content

11.0.2

Choose a tag to compare

@github-actions github-actions released this 16 Oct 08:27
· 74 commits to main since this release
5b1026b
Share PointsTo state entries between duplicated states (#2934)

* Share state entries between duplicated states

Every EOG edge which is currently live holds its own deep copy of the
PointsToState, and duplicating a state copied every single entry even
though the vast majority of them are never touched again. This is what
makes the pass run out of memory on large functions.

Duplicating a state now shares its entries with the original instead of
copying them, and an entry is only copied once somebody actually wants
to modify it. `getForUpdate` is the barrier for that: it hands out a
private copy of the entry, everything else keeps handing out the shared
one. Storing a shared value in another state goes through `putShared`,
which marks it, so `put` can insist on receiving a private value.

Sharing is opt-in per element type (`supportsSharing`) and elements
which do not support it are still deep-copied, so this is transparent
for the other lattices. Since a shared value must not be modified in
place, the powersets check this on every mutation: an unsound in-place
modification fails loudly instead of silently corrupting the state of
another EOG edge. Tuples and triples report themselves as shared as
soon as one of their components is, so wrapping a shared powerset in a
fresh triple cannot hide the sharing either.

Measured on a state with 1000 entries which is duplicated 200 times,
this brings the cost of a duplicated entry down from about 508 bytes to
about 40 bytes; entries which are actually modified cost the same as
before.

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

* Address PR 2934 review: avoid crashing on copy-on-write bugs where possible

- ConcurrentMapLattice.Element.put() now stores a private copy instead of
  throwing when handed an already-shared value (the same privatization
  getForUpdate/computeIfAbsent already do), since it hasn't stored the
  value anywhere yet and is free to substitute a copy.
- PowersetLattice.Element.onMutate() logs instead of throwing when a
  shared set is mutated directly. Unlike put(), this one genuinely cannot
  self-heal: by the time it runs, the caller already holds the shared
  instance itself, and only the entry's owner (via getForUpdate) can swap
  in a private copy. Favor a locally-corrupted result over crashing the
  whole analysis.
- Replaced computeIfAbsent's bare !! with checkNotNull and a message.

Updated testDuplicateSharesEntries/testLubSharesEntries, which asserted
the old throw-on-shared-mutation behavior, to instead verify the mutation
is applied and visible through every owner sharing the object. Added
testPutPrivatizesAnAlreadySharedValue for the new put() behavior.

* Fix a copy-on-write race in fetchValueFromDeclarationState

computeIfAbsent's whole purpose is to hand back a reference guaranteed
to be private (not shared with another state) - either a freshly built
entry or a privatized copy of an existing shared one. This call site
discarded that result and did a second, independent plain lookup
instead, which only happens to return the same object in the absence of
concurrent modification: another coroutine could duplicate() this map
between the two lines and re-share the entry computeIfAbsent just
privatized, right before this code mutated it directly.

Audited every other generalState/declarationsState touchpoint in
PointsToPass.kt plus every other consumer of the copy-on-write
PowersetLattice.Element/ConcurrentMapLattice.Element in the codebase.
No other instance of this pattern found:
- SymbolResolver's LocalDeclarationElement only ever merges via lub()
  or constructs fresh elements, never mutates a fetched one directly.
- ControlDependenceGraphPass uses PersistentMapLattice, which doesn't
  use the shareValue/isShared mechanism at all - every "mutation"
  produces a new, independent map.
- AbstractIntervalEvaluator's and UnreachableEOGPass's ConcurrentMapLattice
  values (NewIntervalLattice.Element, ReachabilityLattice.Element) don't
  override supportsSharing, so they are always deep-copied and can never
  actually become shared.
- ControlFlowSensitiveDFGPass and EOGWorklist use the older, unrelated
  de.fraunhofer.aisec.cpg.helpers.PowersetLattice, not the copy-on-write
  one.

* Address oxisto's review: restore the assert for shared-state mutation, plus smaller fixes

Blocking: the previous "log instead of throw" change to
PowersetLattice.Element.onMutate() (43b7af9) undermined the whole
soundness argument of this PR, not just traded a crash for a local
corruption. compare() takes a reference-equality fast path, so once two
states hold the same (now directly-mutated) object, iterateEOG sees
them as unchanged and silently stops propagating the update - a wrong
fixpoint with only a log line to show for it, and the PointsTo test
suite can no longer catch a missed getForUpdate barrier because the
tests were changed to expect the corruption instead of rejecting it.

Restores the check via assert(), which throws AssertionError exactly
when assertions are enabled - true for our own test suite (Gradle's
Test task defaults enableAssertions to true) where a silent wrong
answer must fail loudly, but not for a normal production JVM, so one
caller's bug still cannot take a whole analysis run down. Also dropped
the per-call log to once per instance (addAll was logging once per
element). Reverted the three tests back to asserting the throw.

Other findings from the same review:
- getLastWrites/fetchValueFromDeclarationState handed out the live,
  state-owned PowersetLattice.Element instead of a copy in three spots -
  nothing currently mutates what they return, but the type invites it.
  Now returns defensive copies (cheap, thanks to the addAllWithoutCheck
  fast path).
- handleNew privatized (deep-copied) an entry via getForUpdate only to
  immediately clear it. Replaced with a direct put of a fresh, empty
  triple - nothing worth privatizing content we are about to discard.
- Documented isShared's happens-before caveat and that
  addAllWithoutCheck's fast-path overloads don't go through onMutate
  (both doc-accuracy points, not bugs).
- Marked TupleLattice.Element/TripleLattice.Element's isShared
  @Transient: it is in-memory copy-on-write bookkeeping, not meaningful
  graph data, and both classes are Serializable.
- Fixed a broken line wrap in TripleLattice's isShared comment.

Not addressed here (see PR reply for reasoning): the putShared-vs-put
performance suggestion for push/pushToDeclarationsState's throwaway
merges, since it would need a new lub parameter and more careful
correctness review than this reply should rush.

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>