Merge pull request #12983 from ethereum/hint-via-ir

Recommend `via-ir` whenever a stack too deep error is encountered.
This commit is contained in:
Daniel Kirchner 2022-05-20 12:26:25 +02:00 committed by GitHub
commit 953bc9fed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 59 additions and 20 deletions

View File

@ -26,6 +26,7 @@
#include <libsolutil/Keccak256.h> #include <libsolutil/Keccak256.h>
#include <libevmasm/CommonSubexpressionEliminator.h> #include <libevmasm/CommonSubexpressionEliminator.h>
#include <libevmasm/AssemblyItem.h> #include <libevmasm/AssemblyItem.h>
#include <libsolutil/StackTooDeepString.h>
#include <range/v3/view/reverse.hpp> #include <range/v3/view/reverse.hpp>
@ -163,7 +164,7 @@ AssemblyItems CSECodeGenerator::generateCode(
// Invalid sequenced operation. // Invalid sequenced operation.
// @todo quick fix for now. Proper fix needs to choose representative with higher // @todo quick fix for now. Proper fix needs to choose representative with higher
// sequence number during dependency analysis. // sequence number during dependency analysis.
assertThrow(seqNr >= _initialSequenceNumber, StackTooDeepException, ""); assertThrow(seqNr >= _initialSequenceNumber, StackTooDeepException, util::stackTooDeepString);
sequencedExpressions.insert(make_pair(seqNr, id)); sequencedExpressions.insert(make_pair(seqNr, id));
} }
@ -471,7 +472,7 @@ void CSECodeGenerator::appendDup(int _fromPosition, SourceLocation const& _locat
{ {
assertThrow(_fromPosition != c_invalidPosition, OptimizerException, ""); assertThrow(_fromPosition != c_invalidPosition, OptimizerException, "");
int instructionNum = 1 + m_stackHeight - _fromPosition; int instructionNum = 1 + m_stackHeight - _fromPosition;
assertThrow(instructionNum <= 16, StackTooDeepException, "Stack too deep, try removing local variables."); assertThrow(instructionNum <= 16, StackTooDeepException, util::stackTooDeepString);
assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access."); assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access.");
appendItem(AssemblyItem(dupInstruction(static_cast<unsigned>(instructionNum)), _location)); appendItem(AssemblyItem(dupInstruction(static_cast<unsigned>(instructionNum)), _location));
m_stack[m_stackHeight] = m_stack[_fromPosition]; m_stack[m_stackHeight] = m_stack[_fromPosition];
@ -484,7 +485,7 @@ void CSECodeGenerator::appendOrRemoveSwap(int _fromPosition, SourceLocation cons
if (_fromPosition == m_stackHeight) if (_fromPosition == m_stackHeight)
return; return;
int instructionNum = m_stackHeight - _fromPosition; int instructionNum = m_stackHeight - _fromPosition;
assertThrow(instructionNum <= 16, StackTooDeepException, "Stack too deep, try removing local variables."); assertThrow(instructionNum <= 16, StackTooDeepException, util::stackTooDeepString);
assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access."); assertThrow(1 <= instructionNum, OptimizerException, "Invalid stack access.");
appendItem(AssemblyItem(swapInstruction(static_cast<unsigned>(instructionNum)), _location)); appendItem(AssemblyItem(swapInstruction(static_cast<unsigned>(instructionNum)), _location));

View File

@ -31,6 +31,7 @@
#include <libsolutil/FunctionSelector.h> #include <libsolutil/FunctionSelector.h>
#include <libsolutil/Whiskers.h> #include <libsolutil/Whiskers.h>
#include <libsolutil/StackTooDeepString.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
@ -242,7 +243,7 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
assertThrow( assertThrow(
2 + byteOffsetSize + sourceBaseType->sizeOnStack() <= 16, 2 + byteOffsetSize + sourceBaseType->sizeOnStack() <= 16,
StackTooDeepError, StackTooDeepError,
"Stack too deep, try removing local variables." util::stackTooDeepString
); );
// fetch target storage reference // fetch target storage reference
_context << dupInstruction(2 + byteOffsetSize + sourceBaseType->sizeOnStack()); _context << dupInstruction(2 + byteOffsetSize + sourceBaseType->sizeOnStack());

View File

@ -43,6 +43,7 @@
#include <libsolutil/Whiskers.h> #include <libsolutil/Whiskers.h>
#include <libsolutil/FunctionSelector.h> #include <libsolutil/FunctionSelector.h>
#include <libsolutil/StackTooDeepString.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
@ -422,7 +423,7 @@ void CompilerContext::appendInlineAssembly(
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(nativeLocationOf(_identifier)) << errinfo_sourceLocation(nativeLocationOf(_identifier)) <<
util::errinfo_comment("Stack too deep (" + to_string(stackDiff) + "), try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
if (_context == yul::IdentifierContext::RValue) if (_context == yul::IdentifierContext::RValue)
_assembly.appendInstruction(dupInstruction(static_cast<unsigned>(stackDiff))); _assembly.appendInstruction(dupInstruction(static_cast<unsigned>(stackDiff)));

View File

@ -31,6 +31,7 @@
#include <libsolutil/FunctionSelector.h> #include <libsolutil/FunctionSelector.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libsolutil/Whiskers.h> #include <libsolutil/Whiskers.h>
#include <libsolutil/StackTooDeepString.h>
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
@ -476,7 +477,7 @@ void CompilerUtils::encodeToMemory(
assertThrow( assertThrow(
(argSize + dynPointers) < 16, (argSize + dynPointers) < 16,
StackTooDeepError, StackTooDeepError,
"Stack too deep, try using fewer variables." util::stackTooDeepString
); );
} }
else else
@ -537,7 +538,7 @@ void CompilerUtils::encodeToMemory(
assertThrow( assertThrow(
(2 + dynPointers) <= 16, (2 + dynPointers) <= 16,
StackTooDeepError, StackTooDeepError,
"Stack too deep(" + to_string(2 + dynPointers) + "), try using fewer variables." util::stackTooDeepString
); );
m_context << dupInstruction(2 + dynPointers) << Instruction::DUP2; m_context << dupInstruction(2 + dynPointers) << Instruction::DUP2;
m_context << Instruction::SUB; m_context << Instruction::SUB;
@ -1418,7 +1419,7 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_variable.location()) << errinfo_sourceLocation(_variable.location()) <<
util::errinfo_comment("Stack too deep, try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
for (unsigned i = 0; i < size; ++i) for (unsigned i = 0; i < size; ++i)
m_context << swapInstruction(stackPosition - size + 1) << Instruction::POP; m_context << swapInstruction(stackPosition - size + 1) << Instruction::POP;
@ -1429,7 +1430,7 @@ void CompilerUtils::copyToStackTop(unsigned _stackDepth, unsigned _itemSize)
assertThrow( assertThrow(
_stackDepth <= 16, _stackDepth <= 16,
StackTooDeepError, StackTooDeepError,
"Stack too deep, try removing local variables." util::stackTooDeepString
); );
for (unsigned i = 0; i < _itemSize; ++i) for (unsigned i = 0; i < _itemSize; ++i)
m_context << dupInstruction(_stackDepth); m_context << dupInstruction(_stackDepth);
@ -1455,7 +1456,7 @@ void CompilerUtils::rotateStackUp(unsigned _items)
assertThrow( assertThrow(
_items - 1 <= 16, _items - 1 <= 16,
StackTooDeepError, StackTooDeepError,
"Stack too deep, try removing local variables." util::stackTooDeepString
); );
for (unsigned i = 1; i < _items; ++i) for (unsigned i = 1; i < _items; ++i)
m_context << swapInstruction(_items - i); m_context << swapInstruction(_items - i);
@ -1466,7 +1467,7 @@ void CompilerUtils::rotateStackDown(unsigned _items)
assertThrow( assertThrow(
_items - 1 <= 16, _items - 1 <= 16,
StackTooDeepError, StackTooDeepError,
"Stack too deep, try removing local variables." util::stackTooDeepString
); );
for (unsigned i = 1; i < _items; ++i) for (unsigned i = 1; i < _items; ++i)
m_context << swapInstruction(i); m_context << swapInstruction(i);

View File

@ -48,6 +48,7 @@
#include <libsolutil/Whiskers.h> #include <libsolutil/Whiskers.h>
#include <libsolutil/FunctionSelector.h> #include <libsolutil/FunctionSelector.h>
#include <libsolutil/StackTooDeepString.h>
#include <range/v3/view/reverse.hpp> #include <range/v3/view/reverse.hpp>
@ -672,7 +673,7 @@ bool ContractCompiler::visit(FunctionDefinition const& _function)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_function.location()) << errinfo_sourceLocation(_function.location()) <<
util::errinfo_comment("Stack too deep, try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
while (!stackLayout.empty() && stackLayout.back() != static_cast<int>(stackLayout.size() - 1)) while (!stackLayout.empty() && stackLayout.back() != static_cast<int>(stackLayout.size() - 1))
if (stackLayout.back() < 0) if (stackLayout.back() < 0)
@ -842,7 +843,7 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_inlineAssembly.location()) << errinfo_sourceLocation(_inlineAssembly.location()) <<
util::errinfo_comment("Stack too deep, try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
_assembly.appendInstruction(dupInstruction(stackDiff)); _assembly.appendInstruction(dupInstruction(stackDiff));
} }
@ -916,7 +917,7 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_inlineAssembly.location()) << errinfo_sourceLocation(_inlineAssembly.location()) <<
util::errinfo_comment("Stack too deep(" + to_string(stackDiff) + "), try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
_assembly.appendInstruction(swapInstruction(stackDiff)); _assembly.appendInstruction(swapInstruction(stackDiff));
_assembly.appendInstruction(Instruction::POP); _assembly.appendInstruction(Instruction::POP);

View File

@ -37,6 +37,7 @@
#include <libsolutil/FunctionSelector.h> #include <libsolutil/FunctionSelector.h>
#include <libsolutil/Keccak256.h> #include <libsolutil/Keccak256.h>
#include <libsolutil/Whiskers.h> #include <libsolutil/Whiskers.h>
#include <libsolutil/StackTooDeepString.h>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <numeric> #include <numeric>
@ -268,7 +269,7 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_varDecl.location()) << errinfo_sourceLocation(_varDecl.location()) <<
util::errinfo_comment("Stack too deep.") util::errinfo_comment(util::stackTooDeepString)
); );
m_context << dupInstruction(retSizeOnStack + 1); m_context << dupInstruction(retSizeOnStack + 1);
m_context.appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction); m_context.appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction);
@ -350,7 +351,7 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_assignment.location()) << errinfo_sourceLocation(_assignment.location()) <<
util::errinfo_comment("Stack too deep, try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
// value [lvalue_ref] updated_value // value [lvalue_ref] updated_value
for (unsigned i = 0; i < itemSize; ++i) for (unsigned i = 0; i < itemSize; ++i)

View File

@ -28,6 +28,8 @@
#include <libsolidity/codegen/CompilerUtils.h> #include <libsolidity/codegen/CompilerUtils.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libsolutil/StackTooDeepString.h>
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::evmasm; using namespace solidity::evmasm;
@ -50,7 +52,7 @@ void StackVariable::retrieveValue(SourceLocation const& _location, bool) const
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_location) << errinfo_sourceLocation(_location) <<
util::errinfo_comment("Stack too deep, try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
solAssert(stackPos + 1 >= m_size, "Size and stack pos mismatch."); solAssert(stackPos + 1 >= m_size, "Size and stack pos mismatch.");
for (unsigned i = 0; i < m_size; ++i) for (unsigned i = 0; i < m_size; ++i)
@ -64,7 +66,7 @@ void StackVariable::storeValue(Type const&, SourceLocation const& _location, boo
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
StackTooDeepError() << StackTooDeepError() <<
errinfo_sourceLocation(_location) << errinfo_sourceLocation(_location) <<
util::errinfo_comment("Stack too deep, try removing local variables.") util::errinfo_comment(util::stackTooDeepString)
); );
else if (stackDiff > 0) else if (stackDiff > 0)
for (unsigned i = 0; i < m_size; ++i) for (unsigned i = 0; i < m_size; ++i)

View File

@ -28,6 +28,7 @@ set(sources
picosha2.h picosha2.h
Result.h Result.h
SetOnce.h SetOnce.h
StackTooDeepString.h
StringUtils.cpp StringUtils.cpp
StringUtils.h StringUtils.h
SwarmHash.cpp SwarmHash.cpp

View File

@ -0,0 +1,28 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <string>
namespace solidity::util
{
static std::string stackTooDeepString =
"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.";
}

View File

@ -26,6 +26,8 @@
#include <libyul/AST.h> #include <libyul/AST.h>
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libsolutil/StackTooDeepString.h>
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::yul; using namespace solidity::yul;
@ -61,7 +63,7 @@ void CodeGenerator::assemble(
assertThrow( assertThrow(
false, false,
langutil::StackTooDeepError, langutil::StackTooDeepError,
"Stack too deep when compiling inline assembly" + util::stackTooDeepString + " When compiling inline assembly" +
(transform.stackErrors().front().comment() ? ": " + *transform.stackErrors().front().comment() : ".") (transform.stackErrors().front().comment() ? ": " + *transform.stackErrors().front().comment() : ".")
); );
} }

View File

@ -1428,7 +1428,7 @@ BOOST_AUTO_TEST_CASE(use_stack_optimization)
BOOST_REQUIRE(result["errors"].isArray()); BOOST_REQUIRE(result["errors"].isArray());
BOOST_CHECK(result["errors"][0]["severity"] == "error"); BOOST_CHECK(result["errors"][0]["severity"] == "error");
BOOST_REQUIRE(result["errors"][0]["message"].isString()); BOOST_REQUIRE(result["errors"][0]["message"].isString());
BOOST_CHECK(result["errors"][0]["message"].asString().find("Stack too deep when compiling inline assembly") != std::string::npos); BOOST_CHECK(result["errors"][0]["message"].asString().find("When compiling inline assembly") != std::string::npos);
BOOST_CHECK(result["errors"][0]["type"] == "CompilerError"); BOOST_CHECK(result["errors"][0]["type"] == "CompilerError");
} }