18 Nov 2026, Wed

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

On May 11, 2026, a member of the Solidity development team, clonker, discovered a significant medium-severity bug residing within the Yul optimizer’s call graph analysis. The vulnerability under the hood involved the misclassification of mutually recursive functions, which occasionally led the compiler to treat them as non-recursive. Because of this incorrect classification, these functions were mistakenly subjected to a memory-spilling optimization mechanism that is fundamentally incompatible with recursive workflows. The Solidity core team has officially addressed and resolved the issue in Solidity version 0.8.36, urging developers utilizing the IR-based code generation pipeline to update their toolchains.

The underlying stack-to-memory relocation mechanism is designed specifically to work around the Ethereum Virtual Machine’s strict stack depth limit, which restricts direct access to only the topmost 16 stack slots. When a function’s simultaneously-live local variables exceed this limit, the compiler traditionally flags a "Stack too deep" compilation error. To mitigate this constraint within the IR pipeline, the stack-to-memory mover relocates local variables to fixed memory offsets, replacing direct stack operations with mstore and mload instructions targeting those specific memory locations. However, this optimization is only safe for non-recursive functions. Fixed memory offsets are inherently shared across every single invocation of the same function. If a recursive function or a mutually recursive cycle holds a value in a fixed slot across a call that re-enters that exact function, an inner activation will overwrite the slot, causing an outer activation to read back corrupted data.

To prevent this dangerous behavior, the compiler relies on a call graph and a cycle detection algorithm to ensure that any function participating in a recursive call chain is strictly excluded from the stack-to-memory spilling mechanism. The discovery by the Solidity team revealed that the path-based depth-first search employed by the compiler’s CallGraphGenerator since version 0.6.12 contained a critical flaw regarding short-circuiting logic in the presence of intersecting cycles. Once a function had been fully explored, it was marked as visited and skipped during subsequent traversals. Consequently, if a function’s cycle could only be closed by passing through a node that had already been finished as part of an earlier, unrelated cycle, the graph traversal would prematurely stop at that finished node. This left the affected function incorrectly reported as non-recursive, despite genuinely sitting on a recursive call cycle.

The bug exclusively impacts the IR (Intermediate Representation) compilation pipeline. Projects utilizing the legacy evmasm pipeline remain entirely unaffected. Furthermore, because the --via-ir flag is not enabled by default within standard Solidity project configurations, development teams that have not explicitly opted into using the IR pipeline are completely safe from the issue. For projects that did enable the pipeline, the vulnerable code has technically been reachable across a broad range of Solidity versions spanning from 0.7.2 up to 0.8.35. Although the precise conditions required to trigger the bug fluctuate across different compiler releases due to internal optimizations and code layout variations, the core flaw persisted across all versions in that range before being remediated in version 0.8.36.

To measure the real-world impact of the vulnerability, the Solidity team conducted a comprehensive scan of approximately 207,000 contracts utilizing --via-ir within the Sourcify database. The analysis identified only 272 contracts—representing roughly 0.1% of the scanned corpus—that contained a function misclassified by the flawed analysis. Crucially, a deeper examination revealed that none of these specific contracts actually relocated variables to memory inside their respective misclassified functions. As a result, the development team confirmed that no known deployed smart contract on public networks was compromised or adversely affected by the optimizer bug.

The conditions required for a contract to be vulnerable are remarkably narrow and must all coincide simultaneously, making the problematic pattern exceptionally rare in practice. First and foremost, a contract must exhibit mutual recursion, as codebases lacking mutual recursion are entirely immune. Additionally, the specific function involved must declare enough local variables simultaneously to exceed the EVM stack threshold, forcing the compiler to trigger the stack-to-memory mover. Finally, the traversal order of the call graph—which is dictated by internal Yul name hashing and structural data organization—must align in such a way that the cycle detection algorithm fails to recognize the function as recursive.

The severity of the issue was assessed as medium because the compiler emits no warnings during compilation, and the generated bytecode does not trigger an explicit revert at runtime. Instead, data corruption manifests silently as unexpected computational results, complicating debugging efforts for developers. Unlike many historical compiler bugs that required inline assembly blocks to manifest, this issue could be triggered using pure Solidity code. However, given the stringent combination of architectural prerequisites, the overall likelihood of encountering the bug in production environments was determined to be extremely low.

To resolve the vulnerability permanently, the Solidity team replaced the faulty path-based depth-first search in version 0.8.36 with Tarjan’s strongly connected components algorithm, a well-established and robust graph theory approach. Tarjan’s algorithm partitions the call graph into strongly connected components in a single pass without relying on sequential traversal orders, allowing the compiler to reliably determine whether any given function is part of a recursive cycle. With this fix implemented, mutually recursive functions are accurately identified and safely excluded from variable spilling.

As a direct consequence of the fix, developers upgrading to Solidity 0.8.36 may occasionally encounter a "Stack too deep" compilation error in codebases that previously compiled successfully. The team clarified that this behavior is actually correct and expected. Prior to the patch, those specific contracts only compiled because the compiler performed an unsound memory relocation on recursive functions. Once those variables are correctly forced to remain on the stack, functions exceeding the 16-slot limit correctly surface the underlying stack-depth limitation. Rather than silently generating corrupted bytecode, the compiler now informs developers of the structural constraint, preventing unsafe execution paths.

Developers are strongly encouraged to update their compiler toolchains to Solidity 0.8.36, particularly if their projects make use of the experimental or production --via-ir compilation pipeline. Project maintainers encountering new stack-depth errors following the upgrade should refactor their affected functions to reduce local variable density or decouple complex logic to maintain compliance with EVM limitations safely.

By Muslim

Leave a Reply

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