Home / Smart Contract Auditing / Solidity 0.8.36 Released to Address Medium-Severity Yul Optimizer Bug Affecting Mutual Recursion

Solidity 0.8.36 Released to Address Medium-Severity Yul Optimizer Bug Affecting Mutual Recursion

On May 11, 2026, a member of the Solidity development team, clonker, discovered a critical software defect residing within the Yul optimizer’s call graph analysis framework. The vulnerability, which has been formally designated a medium-severity issue on the organization’s internal tracking scale, created conditions where mutually recursive functions could be incorrectly categorized as non-recursive. Consequently, these functions were improperly subjected to the compiler’s memory spilling mechanism—an optimization strategy fundamentally incompatible with recursive function execution due to its reliance on fixed memory offsets.

The core function of the memory spilling mechanism, alternatively known as the stack limit evader, is to mitigate the Ethereum Virtual Machine’s strict stack depth limit, which restricts direct access to only the topmost sixteen stack slots. When a function attempts to utilize a high volume of simultaneously live local variables, it risks triggering a classic "Stack too deep" compilation error. To bypass this limitation, the compiler’s intermediate representation (IR) pipeline automatically relocates selected local variables from the execution stack to designated, fixed memory locations, substituting standard stack operations with low-level memory instructions such as mstore and mload.

However, this aggressive optimization technique is only logically sound when applied strictly to non-recursive functions. Because fixed memory offsets are shared across every single invocation of a given function, a recursive architecture introduces a fatal flaw. If a function holds a variable in a static memory slot across an internal call, and that call subsequently re-enters the exact same function—either directly or through an interconnected cycle of calls—the inner function activation will inevitably overwrite the shared slot. Upon returning control to the outer activation, the program reads back corrupted and erroneous data. To prevent this hazard, the compiler is designed to permanently exclude any function participating in a recursive call chain from the memory spilling optimization.

The root cause of the vulnerability traces back to an architectural refactoring introduced in Solidity 0.6.12, which implemented a path-based depth-first search algorithm designed to detect cycles within call graphs. While the code initially interacted only with a single internal consumer known as the SideEffectsPropagator—where classification discrepancies produced no observable downstream consequences—the risk profile changed dramatically with the introduction of the StackLimitEvader in Solidity 0.7.2. From that version onward, the flawed cycle detection logic began interacting directly with memory relocation routines, creating the potential for silent miscompilation.

Technical analysis of the cycle-detection algorithm reveals that the vulnerability manifested due to an unsound short-circuiting mechanism in the presence of intersecting recursion cycles. The traversal logic relied on maintaining a stack of the current active path, marking functions as visited only after they had been completely explored and popped from the path. This design assumed that once a node was fully processed, it could be safely ignored in subsequent checks.

However, when a call graph featured complex, intersecting cycles—such as a trio of functions where multiple paths fed back into already-finished nodes—the traversal would prematurely halt when encountering a previously visited node before a secondary cycle could be properly closed. As a result, the algorithm falsely reported the target function as non-recursive, despite its active participation in a larger strongly connected component. The precise manifestation of this failure mode was highly dependent on traversal order, which was dictated by the internal data structure ordering of Yul name hashes, explaining why the bug required very specific structural conditions to materialize.

The impact of the bug was restricted exclusively to the IR compilation pipeline via the --via-ir flag. The legacy evmasm pipeline remained entirely unaffected. Furthermore, because the --via-ir flag is not enabled by default, development teams that did not explicitly opt into the IR pipeline experienced no exposure to the vulnerability. Extensive version auditing determined that all Solidity releases ranging from version 0.7.2 through version 0.8.35 are technically affected by the flaw. The vulnerability has been fully remediated in Solidity version 0.8.36.

To gauge the real-world impact of the compiler defect, the Solidity team conducted a comprehensive scan of approximately 207,000 contracts utilizing the --via-ir configuration within the Sourcify database. The analysis identified only 272 contracts—representing roughly 0.1% of the scanned dataset—that contained a function misclassified by the flawed analysis. Crucially, a deeper inspection revealed that none of these specific contracts actually relocated variables to memory inside the misclassified functions, confirming that no known deployed smart contracts suffered runtime exploits or data corruption as a result of the bug.

For a contract to be susceptible to the issue, a narrow and specific set of structural conditions had to coincide simultaneously. The contract was required to use the IR pipeline, involve mutual recursion among functions, trigger the stack-to-memory relocation mechanism within a function incorrectly flagged as non-recursive, and feature a specific naming and structural layout capable of bypassing the faulty cycle-detection short-circuit. Projects entirely devoid of mutual recursion faced zero risk.

The behavioral nuances of the bug also shifted across different compiler releases. In Solidity versions 0.8.21 and later, the bug manifested independently of whether the developer explicitly enabled the optimizer, because the stack-to-memory relocation step operates as an independent stage within the IR pipeline regardless of optimizer flags. Conversely, in older affected versions spanning 0.7.2 through 0.8.20, the stack-to-memory mover only executed when the --optimize parameter was explicitly passed, requiring both flags to be active to trigger the vulnerability.

The release of Solidity 0.8.36 permanently resolves the issue by discarding the flawed path-based depth-first search algorithm in favor of Tarjan’s strongly connected components algorithm, an industry-standard graph theory approach. Tarjan’s algorithm partitions call graphs in a single, comprehensive pass that does not rely on traversal order, ensuring that any function participating in a recursive cycle is reliably and accurately identified. Following this upgrade, functions involved in mutual recursion are correctly protected, and their local variables remain safely on the stack.

An immediate consequence of this fix is that certain legacy codebases that previously compiled successfully under older compiler versions may now encounter "Stack too deep" compilation errors when upgrading to Solidity 0.8.36. The Solidity team emphasizes that this outcome is correct and expected behavior. Contracts encountering this error were previously relying on an unsound and hazardous memory relocation strategy that risked silent data corruption. Rather than generating potentially compromised bytecode, the updated compiler halts compilation, signaling to developers that the affected recursive functions exceed the EVM stack limits and require architectural refactoring.

Development teams utilizing the --via-ir pipeline are strongly advised to upgrade their compiler environments to Solidity 0.8.36. Projects that experience new compilation failures related to stack depth limits following the upgrade should refactor their smart contract logic to reduce local variable density or eliminate unsafe recursion patterns, ensuring robust and reliable execution across all deployments.

Tagged: