Skip to content

[Bug-Candidate]: Invalid solc compilation Compiler error: Stack too deep. Try compiling with --via-ir... #1825

Description

@webthethird

Describe the issue:

CryticCompile is raising an InvalidCompilation exception when trying to compile the Blur.io marketplace contract.

It seems related to the number of local variables used in some inline assembly. The longest block of inline assembly is in BlurMarketplace.bulkExecute():

            assembly {
                let memPointer := mload(0x40)

                let order_location := calldataload(add(executions.offset, mul(i, 0x20)))
                let order_pointer := add(executions.offset, order_location)

                let size
                switch eq(add(i, 0x01), executionsLength)
                case 1 {
                    size := sub(calldatasize(), order_pointer)
                }
                default {
                    let next_order_location := calldataload(add(executions.offset, mul(add(i, 0x01), 0x20)))
                    let next_order_pointer := add(executions.offset, next_order_location)
                    size := sub(next_order_pointer, order_pointer)
                }

                mstore(memPointer, 0xe04d94ae00000000000000000000000000000000000000000000000000000000) // _execute
                calldatacopy(add(0x04, memPointer), order_pointer, size)
                // must be put in separate transaction to bypass failed executions
                // must be put in delegatecall to maintain the authorization from the caller
                let result := delegatecall(gas(), address(), memPointer, add(size, 0x04), 0, 0)
            }

I tried reducing it to the following, but to not avail:

            assembly {
                let order_pointer := add(executions.offset, calldataload(add(executions.offset, mul(i, 0x20))))

                let size
                switch eq(add(i, 0x01), executionsLength)
                case 1 {
                    size := sub(calldatasize(), order_pointer)
                }
                default {
                    let next_order_pointer := add(executions.offset, calldataload(add(executions.offset, mul(add(i, 0x01), 0x20))))
                    size := sub(next_order_pointer, order_pointer)
                }

                mstore(mload(0x40), 0xe04d94ae00000000000000000000000000000000000000000000000000000000) // _execute
                calldatacopy(add(0x04, mload(0x40)), order_pointer, size)
                // must be put in separate transaction to bypass failed executions
                // must be put in delegatecall to maintain the authorization from the caller
                let result := delegatecall(gas(), address(), mload(0x40), add(size, 0x04), 0, 0)
            }

So I've tried to remove local variables from the assembly block, as suggested by the error log output, but that didn't work. The alternative suggestion is compiling with the --via-ir flag, but I'm not sure how to do that when using Slither as a library.

Code example to reproduce the issue:

https://etherscan.io/address/0x983e96c26782a8db500a6fb8ab47a52e1b44862d#code

Version:

0.9.3

Relevant log output:

Switched global version to 0.8.17
Compilation warnings/errors on contracts/test/blur/before/BlurExchange/contracts/BlurExchange.sol:
Compiler error: Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables. When compiling inline assembly: Variable value0 is 2 slot(s) too deep inside the stack. Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables.


Traceback (most recent call last):
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 561, in _run_solc
    ret: Dict = json.loads(stdout)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/webthethird/.local/lib/python3.10/site-packages/slither/__main__.py", line 837, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/home/webthethird/.local/lib/python3.10/site-packages/slither/__main__.py", line 90, in process_all
    compilations = compile_all(target, **vars(args))
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/crytic_compile.py", line 643, in compile_all
    compilations.append(CryticCompile(target, **kwargs))
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/crytic_compile.py", line 131, in __init__
    self._compile(**kwargs)
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/crytic_compile.py", line 553, in _compile
    self._platform.compile(self, **kwargs)
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 156, in compile
    targets_json = _get_targets_json(compilation_unit, self._target, **kwargs)
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 285, in _get_targets_json
    return _run_solc(
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 565, in _run_solc
    raise InvalidCompilation(f"Invalid solc compilation {stderr}")
crytic_compile.platform.exceptions.InvalidCompilation: Invalid solc compilation Compiler error: Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables. When compiling inline assembly: Variable value0 is 2 slot(s) too deep inside the stack. Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables.


ERROR:root:Error in contracts/test/blur/before/BlurExchange/contracts/BlurExchange.sol
ERROR:root:Traceback (most recent call last):
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 561, in _run_solc
    ret: Dict = json.loads(stdout)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/webthethird/.local/lib/python3.10/site-packages/slither/__main__.py", line 837, in main_impl
    ) = process_all(filename, args, detector_classes, printer_classes)
  File "/home/webthethird/.local/lib/python3.10/site-packages/slither/__main__.py", line 90, in process_all
    compilations = compile_all(target, **vars(args))
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/crytic_compile.py", line 643, in compile_all
    compilations.append(CryticCompile(target, **kwargs))
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/crytic_compile.py", line 131, in __init__
    self._compile(**kwargs)
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/crytic_compile.py", line 553, in _compile
    self._platform.compile(self, **kwargs)
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 156, in compile
    targets_json = _get_targets_json(compilation_unit, self._target, **kwargs)
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 285, in _get_targets_json
    return _run_solc(
  File "/home/webthethird/.local/lib/python3.10/site-packages/crytic_compile/platform/solc.py", line 565, in _run_solc
    raise InvalidCompilation(f"Invalid solc compilation {stderr}")
crytic_compile.platform.exceptions.InvalidCompilation: Invalid solc compilation Compiler error: Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables. When compiling inline assembly: Variable value0 is 2 slot(s) too deep inside the stack. Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug-candidateBugs reports that are not yet confirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions