Skip to content
Business logic Write-up Advanced

The Solidity Compiler has Developed Schizophrenia

The Solidity compiler inconsistently applies integer versus rational arithmetic to mathematically equivalent expressions, producing different results or failing to compile depending on whether values are literals, constants, immutables, state variables, or casts. A second issue is that legacy and via-IR compilation modes initialise state variables at different points during constructor inheritance, so a constructor argument can be 69 in legacy mode but 0 in via-IR mode.

medium.com

Published on medium.com

Please take a seat, anon. We need to have a discussion about the health of your solidity compiler.

Babs, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the compiler decides between rational and integer arithmetic based on whether an expression is a literal and can be precomputed, and the two compilation modes order state-variable initialisation differently during inheritance.

Impact

The text states that neither issue appears immediately exploitable at a critical level, but the inconsistent arithmetic and constructor behaviour could lead to unexpected values in contracts.

What the attacker needed

Solidity compiler version 0.8.x; Use of via-IR or legacy compilation mode

How it was found

  1. Write a contract with equivalent arithmetic expressions using literals, constants, immutables, state variables, and casts.
  2. Compile and call the functions to observe different results or compilation failures.
  3. Write a contract where a derived contract passes its own state variable to a base constructor.
  4. Run the test in legacy mode and in via-IR mode and compare the constructor argument.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A compiler turns human-readable code into machine instructions, and it is expected to treat equivalent expressions the same way. Here, the Solidity compiler sometimes does maths with fractions and sometimes rounds down early, so two expressions that look mathematically identical can give different answers. Separately, two compilation modes initialise contract variables in a different order, so a value passed to a parent constructor can be 69 in one mode and 0 in the other.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.