5 Dec 2026, Sat

Solidity 0.8.36 Released: Critical Fix Addresses Unsound Memory Spilling in Mutual Recursion

On May 11, 2026, a member of the Solidity core development team, clonker, uncovered a subtle yet severe bug nestled within the Yul optimizer’s call graph analysis. The defect, which managed to evade detection for years across multiple compiler iterations, led to mutually recursive functions being incorrectly classified as non-recursive. Consequently, these functions were improperly subjected to an optimization mechanism designed to work around the Ethereum Virtual Machine’s strict stack depth limitations. This oversight carried the potential to corrupt execution state, prompting the release of Solidity 0.8.36 to patch the vulnerability.

Assigned a "medium" severity rating on the internal scale maintained by the Solidity team, the bug is strictly confined to the IR (Intermediate Representation) compilation pipeline. The traditional legacy evmasm pipeline remains completely unaffected by the issue. Furthermore, because the --via-ir compilation flag is not enabled by default in Solidity projects, codebases that have not explicitly opted into the IR-based pipeline are entirely immune.

Understanding the Scope and Affected Versions

The vulnerable code path has technically been reachable within the compiler architecture since the release of Solidity 0.7.2, though the precise execution conditions required to trigger the bug fluctuate across the affected range. All compiler versions spanning from 0.7.2 through 0.8.35 carry the vulnerable logic. Fortunately, the flaw has been completely resolved in Solidity 0.8.36.

To gauge the real-world impact of the vulnerability, the Solidity team conducted a comprehensive automated scan of roughly 207,000 contracts utilizing the --via-ir flag within the Sourcify database. The analysis identified just 272 contracts—representing approximately 0.1% of the scanned corpus—that contained a function misclassified by the flawed analysis. Crucially, a deeper inspection revealed that none of these deployed contracts actually relocated variables to memory inside the misclassified functions. As a result, the development team has confirmed that no known production contracts have been compromised by the bug.

For a contract to be vulnerable, a specific set of narrow conditions must align perfectly. Most importantly, the target project must utilize mutual recursion. Without a mutually recursive structure involving complex local variable usage, the bug remains entirely dormant.

The relationship between the compiler settings and the bug also evolved over time. In Solidity 0.8.21 and later releases, the vulnerability manifests independently of whether the optimizer is enabled. This occurs because the stack-to-memory relocation phase operates as a distinct, mandatory stage of the IR pipeline that runs regardless of optimization settings, meaning that simply disabling the optimizer does not circumvent the issue. In contrast, across earlier affected versions ranging from 0.7.2 to 0.8.20, the Yul optimizer—and by extension the stack-to-memory mover—only executes when the --optimize flag is explicitly supplied. Consequently, triggering the bug on those older versions required the simultaneous use of both --via-ir and --optimize.

Technical Mechanics: Recursion and the Stack-to-Memory Mover

To fully comprehend the nature of the bug, one must examine how the Ethereum Virtual Machine manages execution state. The EVM architecture restricts direct access to the topmost 16 stack slots. When a Solidity function declares a large number of simultaneously live local variables, it can easily breach this 16-slot threshold, historically resulting in the familiar "Stack too deep" compilation error.

To mitigate this limitation and reduce the frequency of stack overflows, the IR compilation pipeline incorporates a stack-to-memory mover, often referred to as the stack limit evader. When a function cannot accommodate its variables within the stack’s physical constraints, the compiler automatically relocates select variables to fixed memory offsets. It then replaces standard stack operations with mstore and mload instructions pointing to those designated memory slots.

However, this automated memory relocation strategy is only mathematically sound for functions that execute without recursion. Because a fixed memory offset is shared statically by every single activation of a given function, complications arise if recursion enters the picture. If a function holds a variable in a fixed memory slot across a function call, and that call re-enters the same function—either directly or through an indirect cycle—the inner activation invocation inevitably overwrites the shared memory slot. When execution returns to the outer activation, it reads back corrupted data. For this fundamental reason, the compiler’s stack-to-memory mover must strictly avoid relocating variables for any function participating in a recursive call chain.

To enforce this safety rule, the compiler constructs a Yul call graph and executes a cycle detection algorithm to identify recursive functions.

The Root Cause: Flawed Cycle Detection

A function is recursive if and only if it resides within a cycle on the call graph, meaning it either calls itself directly or belongs to a strongly connected component where all participating functions can reach one another.

Prior to the fix, the cycle detection mechanism within the CallGraphGenerator relied on a path-based depth-first search. As the algorithm traversed the graph, it maintained the active traversal path on a stack. Whenever it encountered an invocation pointing to a function already present on that active path, it would flag that function—and every intermediate function called between the two invocations—as part of a cycle. Once all calls originating from a function had been fully explored, that function was popped from the path stack and added to a permanent visited set, ensuring any subsequent visits would short-circuit immediately.

This short-circuiting logic, while efficient, proved unsound when intersecting cycles were present. The traversal only documented a cycle when encountering an edge pointing backward to a node still residing on the active path. Once a function was completely explored, it was permanently marked as visited and never re-evaluated. Consequently, if a function’s recursive cycle could only be closed by passing through a node that had already been marked as finished during an earlier, separate cycle traversal, the algorithm halted prematurely at that finished node before the second cycle could close. The function was subsequently misreported as non-recursive, despite genuinely belonging to a cycle.

This behavior heavily depended on the traversal order, which was dictated by the internal data structure ordering determined by the Yul function name hashes. Different function names produced alternate orderings, meaning some codebases triggered the misclassification while others with identical logic topologies did not.

Real-World Implications and Example

When the compiler misclassified a recursive function as non-recursive, it permitted the stack-to-memory mover to apply fixed-offset memory spilling to that function. If the function was complex enough to exceed stack limits, its local variables were moved to memory. Because the function was actually recursive, re-entry during execution overwrote those fixed memory slots, corrupting data and leading to unexpected runtime results.

The Solidity team provided a minimal reproducer contract to illustrate the failure pattern. In the demonstration, a contract containing three mutually recursive internal functions—a, b, and c—creates a complex call graph where function c is erroneously marked as non-recursive. Because c maintains numerous live variables across a call to b, the compiler relocates its local variable m to memory. When the recursive execution re-enters c, the shared memory slot is overwritten.

In affected versions, calling the trigger function on such a contract returns an untouched initial seed value rather than the expected computed result, because the corrupted variable caused subsequent operations to reference incorrect array indices and values. Notably, this corruption occurs silently without emitting compiler warnings or triggering runtime reverts, making such bugs exceptionally difficult to diagnose outside of comprehensive test suites.

The Solution in Solidity 0.8.36

To resolve the vulnerability permanently, the Solidity development team replaced the flawed path-based depth-first search with Tarjan’s algorithm, a well-established and robust graph theory standard for identifying strongly connected components.

Under the updated implementation, a function is classified as recursive if and only if it is contained within a strongly connected component of the call graph that either spans multiple nodes or consists of a single node with a self-referential edge. Tarjan’s algorithm partitions the entire call graph in a single deterministic pass, eliminating any dependency on traversal ordering or name hashing. With this fix deployed in Solidity 0.8.36, mutually recursive functions like those in the example are correctly identified as recursive, and the stack-to-memory mover leaves their variables safely on the stack.

An important side effect of this correction is that some contracts which previously compiled successfully may now trigger a "Stack too deep" error upon upgrading to Solidity 0.8.36. The development team emphasizes that this represents the correct, intended behavior. Contracts experiencing this error were previously relying on an unsound memory relocation strategy that risked data corruption. Rather than silently generating unsafe code, the updated compiler correctly identifies the stack limitation, requiring developers to refactor their code to manage local variables safely without relying on flawed compiler workarounds.

Developers utilizing the IR compilation pipeline are strongly advised to update their toolchains to Solidity 0.8.36 and run their test suites to verify compatibility. The bug was identified and resolved entirely through internal engineering efforts by the Solidity team as part of ongoing improvements to the IR pipeline’s stack handling infrastructure.

Leave a Reply

Your email address will not be published. Required fields are marked *