Skip to content

Stack depth propagation must be monotone to converge - #628

Open
XHuoHuaX wants to merge 1 commit into
SamboyCoding:developmentfrom
XHuoHuaX:pr/stack-monotone
Open

XHuoHuaX wants to merge 1 commit into
SamboyCoding:developmentfrom
XHuoHuaX:pr/stack-monotone

Conversation

@XHuoHuaX

@XHuoHuaX XHuoHuaX commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

The problem

StackAnalyzer.TraverseGraph re-queues a block whenever its incoming depth merely differs from the recorded one, while overwriting the recorded value outright:

if (existingState.Size != currentState.Size)
{
    _inComingState[successor] = currentState.Copy();   // unconditional overwrite
    blockLevelState.Push((successor, visitedBlockCount + 1));
}

That is not a monotone dataflow, so it is not guaranteed to converge:

  • if a join has predecessors that disagree on the depth, each visit flips the block between the two values;
  • if a loop raises the depth once per iteration, it climbs forever.

MaxBlockVisitCount = 500000 only converts the hang into "one exception per method", and each victim still burns the whole 500,000-block budget.

Where it shows up

A Unity 2018.2.14 Windows x64 IL2CPP build (metadata version 24):

[Warn] [Program] Skipping Pathfinding.GridGraph::OnDrawGizmos: Decompilation failed: Stack state not settling! (500001 blocks already visited)

1,969 methods and still climbing, with the log at 216,656 lines and the run never finishing (>20 min, killed). The same binary handles an Android ARM64 build (metadata 24.5) fine, because there every join happens to agree - which is presumably why this has gone unnoticed.

The fix

Propagate monotonically: only ever raise a block's incoming depth.

  • It cannot change a well-formed method, because a block's depth is unique there.
  • A depth that would drop is kept at the larger value and the method gets a warning (AddWarning) so it is not silent.
  • A 512-byte ceiling bounds the pathological "grows once per iteration" case, likewise flagged. This is what makes the walk terminate without relying on the 500,000-block backstop at all.

Results

before after
Stack state not settling methods 1,969+ (still climbing) 0
wall clock >20 min, never finished 268 s
assemblies emitted - 64
analysis exceptions - 0

On the unrelated Android ARM64 build the emitted output is unchanged (verified by compiling all 23 game assemblies and comparing the error count, which stayed at 418, assembly for assembly).

One caveat worth stating plainly: this eliminates the hang, not the underlying disagreement. If two predecessors really do disagree about the depth, the method now proceeds with the deeper value rather than looping or being skipped, and says so through AddWarning.

TraverseGraph re-queued a block whenever its incoming depth merely differed from the recorded one,
and overwrote the recorded value outright. That is not a monotone dataflow, so it is not guaranteed to
converge: when a join has predecessors that disagree on the depth, each visit flips the block between
the two values, and when a loop raises the depth once per iteration it climbs forever.

It stays hidden on code where every join agrees, which is why it can survive a long time. It shows up
on a Unity 2018.2.14 Windows x64 build (metadata version 24): 1,969 methods and still climbing hit the
500,000-block backstop, each one burning the entire budget, and the run had to be abandoned.

Raise-only propagation converges, and cannot change a well-formed method because a block's depth is
unique there. A depth that would drop is kept at the larger value and the method is flagged, and a
512-byte ceiling bounds pathological growth, so neither case passes silently.

Before: 1,969+ methods lost to 'Stack state not settling', run never finished.
After:  0 such methods, run completes in 268s, 64 assemblies emitted, zero analysis exceptions.
On a second, unrelated build (Android ARM64, metadata 24.5) the compiled output is unchanged.
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.

1 participant