{"id":4218,"date":"2026-11-19T07:15:15","date_gmt":"2026-11-19T07:15:15","guid":{"rendered":"https:\/\/smartcryptosecurity.com\/?p=4218"},"modified":"2026-11-19T07:15:15","modified_gmt":"2026-11-19T07:15:15","slug":"solidity-0-8-36-released-critical-yul-optimizer-bug-fix-addresses-unsound-memory-spilling-in-recursive-functions","status":"publish","type":"post","link":"https:\/\/smartcryptosecurity.com\/?p=4218","title":{"rendered":"Solidity 0.8.36 Released: Critical Yul Optimizer Bug Fix Addresses Unsound Memory Spilling in Recursive Functions"},"content":{"rendered":"<p>The Solidity development team has officially released version 0.8.36, featuring a crucial fix for a medium-severity bug discovered in the Yul optimizer&#8217;s call graph analysis. The vulnerability, which had quietly persisted across multiple minor releases, could under specific circumstances lead to mutually recursive functions being incorrectly classified as non-recursive. This misclassification exempted them from essential safety exclusions within the compiler&#8217;s memory spilling mechanism, potentially resulting in corrupted data storage during execution. <\/p>\n<p>Discovered internally on May 11, 2026, by a developer known as clonker from the Solidity team, the issue exclusively impacts the modern IR (Intermediate Representation) compilation pipeline via the <code>--via-ir<\/code> flag. Because this flag is not enabled by default in standard Solidity configurations, projects that have not explicitly opted into the IR-based pipeline remain entirely unaffected. Furthermore, the legacy <code>evmasm<\/code> compilation pipeline is completely insulated from this vulnerability.<\/p>\n<h2>Understanding the Scope and Affected Versions<\/h2>\n<p>The root of the issue stretches back significantly further than its recent discovery date. According to the Solidity team&#8217;s technical analysis, the faulty cycle detection logic was originally introduced during a compiler refactor implemented in version 0.6.12. At that time, however, the affected component had only a single consumer\u2014the <code>SideEffectsPropagator<\/code>\u2014where a misclassification yielded no observable runtime consequences.<\/p>\n<p>It was not until version 0.7.2, with the introduction of the <code>StackLimitEvader<\/code>, that the misclassification began to matter. Consequently, the affected version range spans all releases from Solidity 0.7.2 through 0.8.35. While reproducible miscompilations could reliably be triggered from version 0.8.8 onward (or version 0.8.10 via the experimental <code>--experimental-via-ir<\/code> entry point), the development team conservatively designated the entire 0.7.2 to 0.8.35 bracket as affected due to the impossibility of proving the bug entirely unreachable in earlier versions, where tests typically encountered &quot;Stack too deep&quot; errors first.<\/p>\n<p>To gauge the real-world impact of the bug prior to patching, researchers conducted an extensive automated scan of approximately 207,000 contracts utilizing the <code>--via-ir<\/code> flag within the Sourcify database. The scan revealed that only 272 contracts\u2014representing roughly 0.1% of the total evaluated corpus\u2014contained functions misclassified by the flawed analysis. Crucially, none of those specific contracts actually relocated variables to memory inside the misclassified functions, meaning no known deployed smart contracts have suffered from exploitation or active corruption as a result of this bug.<\/p>\n<h2>Technical Mechanics: Recursion, Stack Limits, and Memory Spilling<\/h2>\n<p>To fully grasp how the vulnerability manifests, it is necessary to examine how the Ethereum Virtual Machine (EVM) manages execution context and stack depth. The EVM natively allows direct access only to the topmost 16 stack slots. When a complex function features a high number of simultaneously live local variables, it frequently exceeds this rigid threshold, historically resulting in the familiar &quot;Stack too deep&quot; compilation error.<\/p>\n<p>To mitigate this limitation and improve developer ergonomics, the modern IR pipeline incorporates a specialized stack-to-memory mover, often referred to as the stack limit evader. When a function cannot accommodate all of its variables directly on the evaluation stack, the compiler intelligently relocates a subset of those variables to fixed memory offsets, substituting standard stack operations with <code>mstore<\/code> and <code>mload<\/code> instructions targeting those specific memory locations.<\/p>\n<p>However, this optimization strategy is fundamentally unsound when applied to recursive functions. Because fixed memory offsets are shared identically across every single activation or invocation of a given function, a recursive call introduces dangerous concurrency into memory access. If a function holds a variable in a fixed memory slot across a subroutine call that eventually re-enters the same function\u2014either directly or through an intricate cycle of mutual recursion\u2014the inner activation will inevitably overwrite that shared memory slot. When execution returns to the outer activation, it reads back corrupted, overwritten data instead of its original local state.<\/p>\n<p>To prevent this catastrophic memory collision, the compiler is explicitly designed to identify any function involved in a recursive call chain and exclude it entirely from the stack-to-memory relocation mechanism. This identification relies on constructing a Yul call graph and running a cycle-detection algorithm to flag strongly connected components.<\/p>\n<h2>The Flaw in Cycle Detection<\/h2>\n<p>The vulnerability stemmed from a fundamental limitation within the path-based depth-first search algorithm previously utilized by the <code>CallGraphGenerator<\/code>. During traversal, the algorithm maintained the active path on an internal stack. Upon encountering a function call pointing to an entity already present on that active stack, it marked that function\u2014along with any intermediate functions called between the two points\u2014as being part of a recursive cycle. Once fully explored, the function was popped from the path and permanently added to a visited set, causing any subsequent visits to short-circuit immediately.<\/p>\n<p>While efficient, this short-circuit mechanism proved unsound in the presence of intersecting cycles. Because finished nodes were marked as visited and never re-entered, the traversal would prematurely halt if a cycle&#8217;s closure required passing through a node that had already been marked as finished during an earlier, separate traversal path. Consequently, the algorithm would incorrectly report the function as non-recursive, despite its active participation in a complex cycle.<\/p>\n<p>Whether this flawed logic triggered a miscompilation depended heavily on traversal order, which was dictated by the internal data structure ordering determined by the Yul names&#8217; cryptographic hashes. A different arrangement of function names could successfully evade the bug, highlighting why the issue was so elusive and dependent on specific structural patterns within the call graph.<\/p>\n<h2>Examples and Observable Behavior<\/h2>\n<p>The bug requires a very specific convergence of conditions to manifest: the presence of mutual recursion, a function complex enough to exceed stack limitations and trigger memory relocation, and traversal ordering that causes the cycle detection algorithm to fail. <\/p>\n<p>When these precise criteria align, the compiler treats a recursive function as non-recursive, safely moving its local variables to fixed memory slots. Upon re-entry through mutual recursion, those memory slots are overwritten, causing state variables to resolve incorrectly at runtime. Unlike many historical compiler bugs that triggered explicit reverts or failed loudly, this vulnerability manifested as silent state corruption, returning unexpected logical values rather than crashing outright.<\/p>\n<p>Projects that maintained comprehensive test suites executed under <code>--via-ir<\/code> configurations would likely have caught the discrepancy during routine testing, even if the underlying compiler flaw was not immediately obvious. Fortunately, the stringent overlap of necessary conditions ensured that the real-world probability of encountering the bug remained exceptionally low.<\/p>\n<h2>The Resolution and Recommended Actions<\/h2>\n<p>With the release of Solidity 0.8.36, the vulnerable path-based depth-first search has been completely replaced with Tarjan&#8217;s strongly connected components algorithm. This industry-standard graph algorithm partitions call graphs in a single pass without depending on arbitrary traversal order, ensuring that any function participating in a cycle is reliably and accurately flagged as recursive. As a result, the compiler now correctly leaves the local variables of such functions on the stack.<\/p>\n<p>An important side effect of this rigorous fix is that certain contracts which previously compiled successfully\u2014albeit through unsound memory relocation\u2014may now trigger a &quot;Stack too deep&quot; compilation error when upgraded to version 0.8.36. The Solidity development team emphasizes that this is the correct and expected behavior. Such recursive functions cannot be safely compiled by spilling variables to fixed-size memory areas; the new compiler version correctly surfaces the underlying structural limitation rather than silently emitting potentially corrupted bytecode.<\/p>\n<p>Developers utilizing the modern IR pipeline are strongly advised to update their toolchains to Solidity 0.8.36 immediately. Teams encountering new &quot;Stack too deep&quot; errors following the upgrade should review their codebase&#8217;s recursion patterns and refactor affected functions to reduce local variable density or eliminate unsafe recursive patterns.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Solidity development team has officially released version 0.8.36, featuring a crucial fix for a&#8230;<\/p>\n","protected":false},"author":15,"featured_media":4217,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[1278,401,201,3268,960,2002,3350,2001,5,400,402,2275,3050],"class_list":["post-4218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-smart-contract-auditing","tag-addresses","tag-auditing","tag-critical","tag-functions","tag-memory","tag-optimizer","tag-recursive","tag-released","tag-security","tag-smart-contracts","tag-solidity","tag-spilling","tag-unsound"],"_links":{"self":[{"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/posts\/4218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4218"}],"version-history":[{"count":0,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/posts\/4218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/media\/4217"}],"wp:attachment":[{"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}