{"id":5024,"date":"2026-12-03T15:15:22","date_gmt":"2026-12-03T15:15:22","guid":{"rendered":"https:\/\/smartcryptosecurity.com\/?p=5024"},"modified":"2026-12-03T15:15:22","modified_gmt":"2026-12-03T15:15:22","slug":"critical-compiler-bug-discovered-in-solidity-ir-pipeline-fixed-in-version-0-8-37","status":"publish","type":"post","link":"https:\/\/smartcryptosecurity.com\/?p=5024","title":{"rendered":"Critical Compiler Bug Discovered in Solidity IR Pipeline; Fixed in Version 0.8.37"},"content":{"rendered":"<p>A critical software flaw has been discovered within the Solidity compiler\u2019s intermediate representation (IR) pipeline, specifically affecting how the compiler handles memory allocation for local variables under deep recursion. The vulnerability, which can lead to silent variable overwrites and incorrect contract execution, was officially disclosed following a security report submitted via the Ethereum Foundation&#8217;s bug bounty program. <\/p>\n<p>The bug was brought to light on July 30, 2026, by researcher Ng Sze Hon, who provided a comprehensive analysis and a working reproducer. In response to the finding, the Solidity development team implemented a fix in version 0.8.37. Subsequent scans of roughly 319,000 verified smart contracts on Sourcify indicate that no currently deployed contracts are known to have been compiled into vulnerable bytecode. However, developers working with specific compiler configurations are strongly urged to update their toolchains.<\/p>\n<h2>Understanding the Stack-to-Memory Mover and the Root Cause<\/h2>\n<p>To understand how the bug manifests, it is necessary to examine the underlying architecture of the Ethereum Virtual Machine (EVM) and the Solidity compiler. The EVM imposes a strict architectural limit on stack operations, allowing access to no more than the topmost 16 stack slots at any given time. When a smart contract function contains a high number of simultaneously live local variables, it routinely exceeds this limitation, triggering the familiar &quot;Stack too deep&quot; compilation error.<\/p>\n<p>To work around this limitation in the IR pipeline, Solidity utilizes a component known as the stack-to-memory mover, or &quot;stack limit evader.&quot; This feature intervenes by selecting specific local variables and relocating\u2014or &quot;spilling&quot;\u2014them from the stack into fixed memory offsets known as spill slots. These slots are allocated within a reserved region at the beginning of the contract&#8217;s memory space, with the size of this region communicated to the Yul optimizer via the <code>memoryguard<\/code> built-in function.<\/p>\n<p>Because spill slots are designed to be reused across different function executions to conserve memory, the compiler must carefully track function call graphs to ensure that two active variables never occupy the same slot simultaneously. The bug discovered by Ng Sze Hon occurs precisely within this slot allocation and caching mechanism when mutual recursion is present in the call graph.<\/p>\n<p>During compilation, the slot allocator performs a depth-first traversal of the call graph, calculating the slot requirements for each function and caching the results. To prevent infinite loops caused by recursive call chains, the allocator inserts a provisional value of zero into the cache when it first encounters a function. This provisional value is only updated to the true requirement once all callees have been fully explored.<\/p>\n<p>The core defect stems from the fact that the traversal algorithm can inadvertently read this provisional zero as if it were a finalized requirement. When a function is revisited through a cyclical path, it processes the zero instead of the actual slot requirement of the called function. This undercount propagates upward through subsequent callers, causing the compiler to miscalculate the total size of the reserved memory region and assign the exact same spill slot to variables belonging to completely independent functions that happen to be live at the same time.<\/p>\n<p>At runtime, when one function executes and writes to its local variable, it silently overwrites the memory location used by another active variable. Consequently, the affected contract computes, stores, or returns values that were never explicitly assigned by the original source code.<\/p>\n<h2>Affected Versions and Scope of the Vulnerability<\/h2>\n<p>The vulnerability has been assigned a &quot;low\/medium&quot; severity rating on the internal scale maintained by the Solidity team. Several strict conditions must align simultaneously for a contract to be vulnerable, making the affected pattern exceptionally rare in real-world production environments. <\/p>\n<p>The bug exclusively impacts the IR pipeline (<code>--via-ir<\/code>). The legacy <code>evmasm<\/code> pipeline is entirely unaffected. Furthermore, because the <code>--via-ir<\/code> flag is not enabled by default in Solidity projects, codebases that have not explicitly opted into the IR pipeline face no risk. Contracts that completely lack mutually recursive functions\u2014either in Solidity source code or within inline assembly blocks\u2014are likewise immune.<\/p>\n<p>The problematic code path has technically been reachable since Solidity version 0.7.2, coinciding with the introduction of the stack limit evader. Consequently, all compiler versions ranging from 0.7.2 through 0.8.36 are affected. <\/p>\n<p>The bug is also closely related to, yet distinct from, a previous issue known as the &quot;Unsound Spill In Mutual Recursion Bug,&quot; which was patched in Solidity 0.8.36. While that earlier defect involved misclassifying recursive functions as non-recursive and inappropriately spilling their variables, the newly discovered bug involves correct classification but flawed slot allocation across mutual recursion boundaries. As a result, the new vulnerability remains present even in version 0.8.36.<\/p>\n<p>The behavioral conditions required to trigger the bug also depend on the compiler version and optimizer settings. In Solidity versions 0.8.21 and later, the stack-to-memory relocation runs as an independent stage of the IR pipeline regardless of whether the optimizer is enabled, meaning disabling the optimizer does not circumvent the issue. Conversely, in older affected versions ranging from 0.7.2 through 0.8.20, the Yul optimizer must be explicitly invoked with the <code>--optimize<\/code> flag for the stack-to-memory mover to run, meaning exploitation on those versions requires both <code>--via-ir<\/code> and <code>--optimize<\/code> to be active.<\/p>\n<h2>Verification and Remediation<\/h2>\n<p>To ensure the safety of the broader ecosystem, the Solidity team performed a comprehensive rescan of the Sourcify verified contract database. Compiling approximately 319,000 verified contracts using compiler builds both with and without the patch revealed identical bytecode outputs across the board, confirming that no known deployed contracts were compromised by this compiler defect.<\/p>\n<p>The official resolution is delivered in Solidity version 0.8.37. The fix fundamentally alters how the compiler traverses call graphs during memory allocation. Instead of performing a traversal per individual function, the updated compiler analyzes strongly connected components (SCCs) of the call graph\u2014representing maximal sets of mutually recursive functions, with non-recursive functions treated as individual components.<\/p>\n<p>Within this revamped architecture, slot requirements are computed and cached at the component level, and internal calls between members of the same mutually recursive component are ignored during the walk. Because the resulting components form a strictly acyclic graph, a component can never be revisited while it is still actively being processed. This eliminates provisional caching errors, ensures accurate memory allocation, and prevents spill slot collisions.<\/p>\n<p>Unlike patches for certain historical compiler bugs, upgrading to Solidity 0.8.37 will not cause existing builds to break or trigger unexpected &quot;Stack too deep&quot; compilation errors. Developers utilizing the IR pipeline and mutual recursion are strongly advised to update their compiler version to 0.8.37 or later to ensure long-term code integrity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A critical software flaw has been discovered within the Solidity compiler\u2019s intermediate representation (IR) pipeline,&#8230;<\/p>\n","protected":false},"author":21,"featured_media":5023,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[401,1230,201,962,1340,2223,5,400,402,1341],"class_list":["post-5024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-smart-contract-auditing","tag-auditing","tag-compiler","tag-critical","tag-discovered","tag-fixed","tag-pipeline","tag-security","tag-smart-contracts","tag-solidity","tag-version"],"_links":{"self":[{"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/posts\/5024","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\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5024"}],"version-history":[{"count":0,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/posts\/5024\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=\/wp\/v2\/media\/5023"}],"wp:attachment":[{"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartcryptosecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}