argotorg / argotorg/solidity

Higher optimize-runs parameter results in yul::StackTooDeepError

Open
#15,233 0 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
2d 19h
Merged PRs (30d)
29

Description

## Description

Compiling the following code with solc 0.8.25 results in yul::StackTooDeepError.

Compiling command: `solc-0.8.25 out.sol --optimize --via-ir --optimize-runs 10000 --bin`

```solidity
contract C {
uint test1;
uint test2;
uint test3;
uint test4;
uint test5;
uint test6;
uint test7;
function test(string memory s, uint n, uint a) public payable returns(uint) {
if (test2 > 3) {
uint256 v1 = (test5 * test1) / (test2 - ((test3 * a) / (test6 - (test5 / 2)) / 2));
uint256 v2 = test2 / 2 + 1;
uint256 v3 = (~(2 ** (8 * (32 - v1)) - 1) * test4);
v3 /= (test2 - (v1 - test7) / test4 - (test6 / 2));
while ((uint256(1) << test1) < n) {
v2 = ((((test3 * (2 ** (8 * (32 - test4)) - 1)) / (test3 - (test2 / 2))) / (test1 * test2)) / (a - (test1 / 2)) + v2) / 2;
}
} else if (test3 != 0) {
if (n > 3) {
if (test2 > test6) {
test4 = test3 - (test7 - (2 ** (8 * (32 - test1)) - 1)) / test2;
uint256 v4 = a - ((test4 * test1) / 1e18);
}
}
}
uint[] memory x = new uint[](test6);
return x[a];
}
}
```

Result:

```
Uncaught exception:
/solidity/libyul/backends/evm/EVMObjectCompiler.cpp(106): Throw in function void solidity::yul::EVMObjectCompiler::run(solidity::yul::Object&, bool)
Dynamic exception type: boost::wrapexcept
std::exception::what: Variable value is 1 too deep in the stack [ value _5 _4 _1 _21 _6 r_1 _13 _14 _15 r _19 product _23 _22 var_v diff_5 ]
memoryguard was present.
[solidity::util::tag_comment*] = Variable value is 1 too deep in the stack [ value _5 _4 _1 _21 _6 r_1 _13 _14 _15 r _19 product _23 _22 var_v diff_5 ]
memoryguard was present.
```

No exceptions are thrown when setting a smaller optimize-runs parameter, such as 200.

## Environment

- Compiler version: 0.8.25

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with solc 0.8.25 using the provided out.sol contract and the --optimize --via-ir --optimize-runs 10000 --bin command, then compare it with optimize-runs 200. Start at libyul/backends/evm/EVMObjectCompiler.cpp around line 106, where the reported StackTooDeepError is thrown. Done means the example no longer fails at the higher optimize-runs value.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, solidity
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.