Home / Smart Contract Auditing / Solidity Compiler Vulnerability Discovered in Yul Optimizer: Medium-Severity Bug Affects IR Pipeline Versions 0.7.2 to 0.8.35

Solidity Compiler Vulnerability Discovered in Yul Optimizer: Medium-Severity Bug Affects IR Pipeline Versions 0.7.2 to 0.8.35

The Solidity core development team has disclosed a medium-severity compiler bug residing within the Yul optimizer’s call graph analysis component. Discovered on May 11, 2026, by team member clonker, the flaw under specific, narrow conditions caused mutually recursive functions to be incorrectly categorized as non-recursive. As a consequence, these functions were improperly subjected to a stack-to-memory spilling mechanism that is fundamentally incompatible with recursion.

While the vulnerability spans a wide range of compiler versions—affecting all releases from Solidity 0.7.2 through 0.8.35—the development team has emphasized that the likelihood of real-world exploitation is exceptionally low. A comprehensive database audit of approximately 207,000 contracts utilizing the --via-ir pipeline via Sourcify revealed that only 272 contracts contained functions misclassified by the flawed analysis. Furthermore, none of those specific contracts actually relocated variables to memory inside the misclassified functions, meaning no known deployed smart contract has suffered operational disruption or variable corruption as a result of the bug. The issue has been fully resolved with the release of Solidity 0.8.36.

Understanding the Vulnerability and the Intermediate Representation Pipeline

The core issue centers on how the Solidity compiler handles the Ethereum Virtual Machine’s strict hardware constraints regarding stack depth. The EVM natively permits direct access exclusively to the topmost 16 stack slots. When a complex function utilizes a high number of simultaneously live local variables, it risks exceeding this threshold, historically throwing a classic "Stack too deep" compilation error.

To mitigate this limitation within the modern intermediate representation (IR) pipeline, the compiler utilizes a stack-to-memory mover, sometimes referred to as the stack limit evader. When a function’s variables exceed available stack space, the compiler shifts selected variables to fixed memory offsets, substituting standard stack operations with mstore and mload instructions targeting those specific memory locations.

However, this optimization technique is only safe for non-recursive functions. Fixed memory slots are static and shared across every single invocation of a given function. If a function holds data in a static memory offset across a call that subsequently re-enters that same function—either directly or through a multi-step recursive cycle—the inner activation invocation overwrites the shared memory slot. Consequently, when the outer activation attempts to resume and read back its data, it encounters corrupted or overwritten values.

To prevent this dangerous condition, the compiler is designed to build a Yul call graph and execute cycle-detection algorithms, ensuring that any function participating in a recursive call chain is strictly exempted from the stack-to-memory spilling mechanism.

Technical Root Cause of the Cycle Detection Flaw

The bug originated from a refactoring introduced in Solidity 0.6.12 involving the CallGraphGenerator. Originally, the path-based depth-first search algorithm implemented during this refactor only fed the SideEffectsPropagator, where function misclassifications produced no observable runtime impact. However, with the introduction of the StackLimitEvader in Solidity 0.7.2, the misclassification suddenly gained severe potential consequences.

The flaw manifested during traversal when the algorithm encountered intersecting cycles. The previous cycle-detection mechanism maintained the current evaluation path on a stack. When it reached an invocation of a function already present on that active path, it would flag that function and all intermediary functions as being part of a cycle. Once a function and its callees were fully explored, the node was added to a permanent visited set and immediately short-circuited on any subsequent visits.

This short-circuiting behavior proved unsound when intersecting cycles were present. If a function’s recursive cycle could only be closed by traversing through a node that had already been marked as finished during an earlier, separate cycle analysis, the traversal algorithm would halt prematurely at the finished node. As a result, the algorithm failed to close the second cycle, falsely reporting the target function as non-recursive even though it genuinely resided within a strongly connected component.

Because the order in which callees were visited depended heavily on the compiler’s internal data structures—specifically dictated by the alphabetical or structural name hashes of the Yul functions—whether a contract triggered the bug depended entirely on the exact naming conventions and structural shape of its call graph.

Affected Versions and Strict Trigger Conditions

The vulnerability impacts all Solidity versions from 0.7.2 up to and including 0.8.35. The legacy evmasm pipeline is entirely unaffected. Because the --via-ir compilation flag is not enabled by default, projects that have not explicitly opted into using the IR pipeline face no exposure.

Furthermore, a contract is only vulnerable if an exacting set of narrow conditions occurs simultaneously. The function must be part of a mutual recursion cycle, it must be misclassified as non-recursive due to the cycle-detection oversight, it must contain a high enough variable load to trigger the stack-to-memory relocation mechanism, and it must actively perform variable relocation inside the misclassified function.

On Solidity versions 0.8.21 and later, this behavior occurs independently of whether the optimizer is enabled, because the stack-to-memory relocation operates as a distinct stage within the IR pipeline regardless of optimizer flags. For earlier affected versions spanning from 0.7.2 to 0.8.20, the Yul optimizer—and by extension the stack-to-memory mover—only executed when the --optimize flag was explicitly passed, requiring both --via-ir and --optimize to activate the path leading to the bug.

Remediation and Transition to Tarjan’s Algorithm

To permanently eliminate the flaw, the Solidity development team replaced the flawed path-based depth-first search with Tarjan’s strongly connected components algorithm, a mathematically rigorous and standard graph theory approach for identifying cycles.

With Tarjan’s algorithm implemented in Solidity 0.8.36, the compiler partitions the call graph in a single pass without relying on traversal order, reliably identifying every function involved in a recursive cycle. Functions caught in mutual recursion are now correctly protected from variable spilling.

Developers upgrading to Solidity 0.8.36 may occasionally encounter a new "Stack too deep" compilation error on contracts that previously compiled successfully. The development team clarifies that this represents correct compiler behavior. In cases where contracts previously compiled by relying on unsound, silent memory-spilling of recursive functions, the compiler now correctly halts compilation and surfaces the architectural stack limitation, preventing the silent generation of flawed bytecode.

Projects utilizing the IR pipeline are advised to upgrade to Solidity 0.8.36 or later, review their test suites under --via-ir, and verify that any complex recursive patterns are refactored to accommodate the EVM’s native stack limitations safely.

Tagged:

Leave a Reply

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