Executive Summary & Direct Answer

Solidity’s Yul optimizer stale-bookkeeping bug can make an optimized load return an outdated value or remove a store that is still required. It affects Solidity 0.8.19 through 0.8.37 when the Yul optimizer is enabled and the optimizer sequence includes LoadResolver or EqualStoreEliminator; Solidity 0.8.38 fixes it. The key risk is aliasing: a Yul variable used to derive memory or storage addresses is reassigned, and addresses computed on either side of that reassignment can coincide at runtime despite having different constant offsets.

For production, upgrade to Solidity 0.8.38 or later and rebuild and retest affected artifacts. If an upgrade is temporarily impossible, assess the compiler pipeline and optimizer sequence, and use a non-Yul-optimized build only as a carefully tested mitigation. A passing test on the vulnerable compiler does not establish safety: compare behavior against a fixed compiler or an unoptimized reference build.

Environment and baseline specifications

This guide targets Solidity 0.8.38 or later, with Foundry for examples and tests. The affected range reported by the Solidity team is 0.8.19–0.8.37. The bug requires Yul optimization and a relevant optimizer step; the default Yul optimizer sequence includes both LoadResolver and EqualStoreEliminator.

A minimal Foundry configuration for the fixed compiler is:

# foundry.toml
[profile.default]
solc_version = "0.8.38"
optimizer = true
optimizer_runs = 200
via_ir = true

via_ir = true is not itself the bug. It matters because the reported issue can affect any code compiled through the Yul pipeline, including ordinary Solidity code without inline assembly. With the default legacy evm pipeline, the reported exposure is narrower: compiler-generated Yul utility functions and inline assembly that does not refer to Solidity variables or functions may be affected.

Treat compiler version and build settings as part of the artifact’s provenance. Pin them in CI, record the Standard JSON settings or Foundry configuration used for release builds, and rebuild deployed-source artifacts after a compiler upgrade.

What goes wrong: stale alias knowledge

The optimizer performs transformations using facts it has learned about expressions and memory or storage accesses. The bug is in bookkeeping: after a Yul variable used in address calculations is reassigned, optimizer knowledge can become stale. It may then treat two addresses as distinct even though they resolve to the same runtime location.

That mistaken distinction can enable incorrect optimization:

  • Load resolution: a load may be replaced with a previously known value that is no longer valid for the aliased location.
  • Equal-store elimination: a store the program needs may be removed because the optimizer incorrectly concludes that another store already covers the location.

The source report identifies an important shape: an address is derived from a variable’s value before reassignment, and another is derived from its value afterward, using different constant offsets. At runtime, those address expressions can coincide. This is not a general claim that Yul address arithmetic is unsafe; it is a specific compiler-optimizer correctness issue.

A tempting but insufficient way to investigate

A common first response is to test only the release compiler and assume that passing tests prove the optimized result is correct:

function readAfterWrite(uint256 slot) external returns (uint256) {
    assembly {
        mstore(slot, 7)
        let result := mload(slot)
        mstore(0, result)
        return(0, 32)
    }
}

This example does not demonstrate the reported bug. It lacks the critical reassignment-and-aliasing shape, and a test on just one compiler build has no independent reference result. More generally, adding an assembly test that happens to pass does not exclude a misoptimization: the failure depends on the optimizer’s intermediate Yul and step sequence.

A focused Yul shape for investigation

The following sketch illustrates the address relationship to look for. It is a shape for investigation, not a guaranteed reproducer for every affected compiler: optimizer behavior depends on the exact intermediate Yul and optimization sequence.

{
    // Assume p is supplied dynamically and is in a safe memory range.
    let p := calldataload(0)

    // Address derived before p is reassigned.
    let before := add(p, 0x20)

    // Reassign the address-computation variable.
    p := sub(p, 0x20)

    // Different constant offset, derived after reassignment.
    let after := add(p, 0x40)

    mstore(before, 0x1234)
    let observed := mload(after)

    mstore(0, observed)
    return(0, 0x20)
}

For inputs where the calculated addresses coincide, correct EVM execution reads the value written by mstore. The risk is that an affected optimizer reasons from stale bookkeeping and transforms a load or store as if the locations were distinct. Use bounded, valid test inputs: arbitrary calldata can create huge memory offsets and run out of gas, hiding the behavior under test.

Do not use this sketch as a release gate or a claim that a particular compiler version will miscompile it. To establish a concrete failure, preserve the exact source, compiler binary, optimizer settings, and generated bytecode for the reproducing build.

Production fix: upgrade, rebuild, and compare

The primary fix is to compile with Solidity 0.8.38 or later. An upgrade changes the compiler’s optimizer implementation; changing application code or merely disabling inline assembly is not a general fix, especially for viaIR, where ordinary Solidity code can be affected.

A useful regression strategy is to run the same bounded test cases through:

  1. The exact affected compiler and production optimizer settings, to reproduce a known failure if one exists.
  2. Solidity 0.8.38 or later with the same settings.
  3. Optionally, a reference build with Yul optimization disabled, to help identify optimizer-dependent behavior.

For a differential test, define the expected result from the program’s intended EVM semantics rather than treating one compiler build as the oracle. If outputs or state changes differ across builds, reduce the test case and retain it as a regression test. The reduction should preserve the production compiler settings and the dynamic inputs responsible for the alias.

When release timing prevents an immediate compiler upgrade, a temporary build without Yul optimization may reduce exposure to the specific optimizer steps. It is a mitigation, not a substitute for the fixed compiler: it can change gas costs and bytecode, and it requires its own functional, deployment, and performance validation. Confirm the actual optimizer sequence and pipeline used by the build rather than assuming a setting name removes the relevant pass.

Reproduce and verify with Foundry

Keep the reproduction harness separate from the production contract so that you can compile it under multiple compiler configurations without changing application behavior. Test both return values and state effects: stale load replacement can produce a wrong result even when the write occurred, while removed stores require checking the affected memory-derived or persistent state behavior.

For ordinary test development, see Effective Use of Foundry for Testing Solidity Smart Contracts. Add bounded fuzzing around the dynamic address inputs; Testing Solidity with Property-Based Fuzzing covers how to broaden input exploration without accidentally spending most of the test budget on invalid, out-of-gas cases.

A practical validation checklist:

forge clean
forge test -vvv
forge test --fuzz-runs 10000

These commands validate the configured compiler build; they do not automatically compare compiler versions. For a version comparison, build the same minimal harness with separately pinned compiler versions and settings, then compare test outcomes and, when relevant, bytecode. Keep an exact failing seed or calldata value so the case remains reproducible.

Also verify:

  • The build uses the intended solc version and viaIR setting.
  • Yul optimization is enabled or disabled as expected.
  • The optimizer sequence contains the relevant steps, where the toolchain exposes that configuration.
  • Test inputs keep memory offsets within a safe range.
  • Assertions cover the expected result and state effects, not only successful transaction completion.
  • CI reruns the regression test with the fixed compiler used for deployment.

For stateful contracts, avoid assuming that a result from an unoptimized build proves the expected behavior by itself. Make the test’s semantic expectation explicit and, where practical, check it against a small independent harness.

Affected build matrix and quantitative trade-offs

The compiler team’s report gives the following scope and database scan figures. The scan found no bytecode differences in the inspected verified compilations, which is useful evidence about prevalence—not proof that an individual contract is unaffected.

Build or conditionReported exposureQuantitative evidence or trade-off
Solidity 0.8.19–0.8.37, Yul optimizer enabled, relevant step presentPotentially affected if the address-reassignment and runtime-alias conditions also occurBug introduced in 0.8.19; fixed in 0.8.38
viaIR with Yul optimizationAny code may be affected, including code without inline assemblyBroader source-level reach because Solidity is compiled through Yul
Default evm pipeline with Yul optimizationNarrower reported scope: generated Yul utilities and qualifying inline assemblyNot a blanket guarantee that every other compilation is safe
Sourcify scan, viaIRNo bytecode differences found in the scanRoughly 283,000 verified compilations compared
Sourcify scan, evm pipelineNo bytecode differences found in the scanRoughly 607,000 verified compilations compared
Disable Yul optimization temporarilyAvoids the affected optimization steps when actually disabledMay change gas and bytecode; must be measured and tested
Upgrade to Solidity 0.8.38+Compiler fix appliedRecommended remediation; rebuild and validate release artifacts

The scan numbers are not estimates of the bug’s probability in a particular codebase. They describe the number of verified compilations the Solidity team compared; the team explicitly notes that a lack of differing bytecode does not rule out an affected contract.

Production pitfalls and edge cases

Do not equate “no inline assembly” with “not affected.” The reported plain-Solidity case means the viaIR pipeline can expose the issue even when contract authors wrote no assembly themselves.

Do not equate “not using viaIR” with “no risk.” The default evm pipeline has a narrower reported scope, but qualifying inline assembly and compiler-generated Yul utilities remain relevant.

Do not treat optimizer flags as interchangeable. The report’s conditions distinguish enabling Yul optimization from whether the optimizer sequence includes LoadResolver or EqualStoreEliminator. Check the effective build configuration, not just the source-level pragma or a generic “optimize” label.

Do not rely only on bytecode comparison. Bytecode differences are a useful signal, but different bytecode is not necessarily incorrect, and identical bytecode in a scan does not prove a contract cannot be affected. Pair comparisons with behavioral tests and code review of address calculations.

Do not make an unvalidated production flag change. Disabling optimization can affect gas and deployed bytecode. Re-run the full test suite, deployment-size checks, and gas-sensitive tests under the exact intended release settings.

Frequently Asked Questions (FAQ)

Which Solidity compiler versions are affected by the Yul stale-bookkeeping bug?

The reported affected range is Solidity 0.8.19 through 0.8.37, when the Yul optimizer is enabled and the relevant optimizer step and aliasing conditions are present. Solidity 0.8.38 fixes the bug. Check the compiler version and effective optimizer settings used to produce the deployed artifact.

Can ordinary Solidity code be affected without inline assembly?

Yes. The report says that with viaIR, any code may be affected, including code without inline assembly. In the default evm pipeline, the reported scope is narrower and concerns generated Yul utility functions and inline assembly that does not refer to Solidity variables or functions.

Is disabling the Yul optimizer a complete production fix?

It can serve as a temporary mitigation if it actually removes the relevant optimization, but it is not the recommended permanent fix. It can change gas usage and bytecode, and it does not replace testing with the corrected compiler. Upgrade to Solidity 0.8.38 or later and validate the rebuilt artifact.

Learn more with useful resources