mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #5923 from ethereum/detectStackProblem
Detect stack problem
This commit is contained in:
commit
2c75c08a89
@ -184,18 +184,20 @@ void CodeGenerator::assemble(
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
EthAssemblyAdapter assemblyAdapter(_assembly);
|
EthAssemblyAdapter assemblyAdapter(_assembly);
|
||||||
try
|
shared_ptr<EVMDialect> dialect = EVMDialect::strictAssemblyForEVM();
|
||||||
{
|
CodeTransform transform(
|
||||||
CodeTransform(
|
|
||||||
assemblyAdapter,
|
assemblyAdapter,
|
||||||
_analysisInfo,
|
_analysisInfo,
|
||||||
_parsedData,
|
_parsedData,
|
||||||
*EVMDialect::strictAssemblyForEVM(),
|
*dialect,
|
||||||
_optimize,
|
_optimize,
|
||||||
false,
|
false,
|
||||||
_identifierAccess,
|
_identifierAccess,
|
||||||
_useNamedLabelsForFunctions
|
_useNamedLabelsForFunctions
|
||||||
)(_parsedData);
|
);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
transform(_parsedData);
|
||||||
}
|
}
|
||||||
catch (StackTooDeepError const& _e)
|
catch (StackTooDeepError const& _e)
|
||||||
{
|
{
|
||||||
@ -205,4 +207,5 @@ void CodeGenerator::assemble(
|
|||||||
(_e.comment() ? ": " + *_e.comment() : ".")
|
(_e.comment() ? ": " + *_e.comment() : ".")
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
solAssert(transform.stackErrors().empty(), "Stack errors present but not thrown.");
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,26 @@ bool AsmAnalyzer::analyze(Block const& _block)
|
|||||||
return (*this)(_block);
|
return (*this)(_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(
|
||||||
|
shared_ptr<Dialect> _dialect,
|
||||||
|
EVMVersion _evmVersion,
|
||||||
|
Block const& _ast
|
||||||
|
)
|
||||||
|
{
|
||||||
|
ErrorList errorList;
|
||||||
|
langutil::ErrorReporter errors(errorList);
|
||||||
|
yul::AsmAnalysisInfo analysisInfo;
|
||||||
|
bool success = yul::AsmAnalyzer(
|
||||||
|
analysisInfo,
|
||||||
|
errors,
|
||||||
|
_evmVersion,
|
||||||
|
Error::Type::SyntaxError,
|
||||||
|
_dialect
|
||||||
|
).analyze(_ast);
|
||||||
|
solAssert(success && errorList.empty(), "Invalid assembly/yul code.");
|
||||||
|
return analysisInfo;
|
||||||
|
}
|
||||||
|
|
||||||
bool AsmAnalyzer::operator()(Label const& _label)
|
bool AsmAnalyzer::operator()(Label const& _label)
|
||||||
{
|
{
|
||||||
solAssert(!_label.name.empty(), "");
|
solAssert(!_label.name.empty(), "");
|
||||||
|
@ -72,6 +72,12 @@ public:
|
|||||||
|
|
||||||
bool analyze(Block const& _block);
|
bool analyze(Block const& _block);
|
||||||
|
|
||||||
|
static AsmAnalysisInfo analyzeStrictAssertCorrect(
|
||||||
|
std::shared_ptr<Dialect> _dialect,
|
||||||
|
dev::solidity::EVMVersion _evmVersion,
|
||||||
|
Block const& _ast
|
||||||
|
);
|
||||||
|
|
||||||
bool operator()(Instruction const&);
|
bool operator()(Instruction const&);
|
||||||
bool operator()(Literal const& _literal);
|
bool operator()(Literal const& _literal);
|
||||||
bool operator()(Identifier const&);
|
bool operator()(Identifier const&);
|
||||||
|
@ -13,6 +13,8 @@ add_library(yul
|
|||||||
AsmScope.h
|
AsmScope.h
|
||||||
AsmScopeFiller.cpp
|
AsmScopeFiller.cpp
|
||||||
AsmScopeFiller.h
|
AsmScopeFiller.h
|
||||||
|
CompilabilityChecker.cpp
|
||||||
|
CompilabilityChecker.h
|
||||||
Dialect.cpp
|
Dialect.cpp
|
||||||
Dialect.h
|
Dialect.h
|
||||||
Exceptions.h
|
Exceptions.h
|
||||||
@ -32,6 +34,8 @@ add_library(yul
|
|||||||
backends/evm/EVMDialect.h
|
backends/evm/EVMDialect.h
|
||||||
backends/evm/EVMObjectCompiler.cpp
|
backends/evm/EVMObjectCompiler.cpp
|
||||||
backends/evm/EVMObjectCompiler.h
|
backends/evm/EVMObjectCompiler.h
|
||||||
|
backends/evm/NoOutputAssembly.h
|
||||||
|
backends/evm/NoOutputAssembly.cpp
|
||||||
optimiser/ASTCopier.cpp
|
optimiser/ASTCopier.cpp
|
||||||
optimiser/ASTCopier.h
|
optimiser/ASTCopier.h
|
||||||
optimiser/ASTWalker.cpp
|
optimiser/ASTWalker.cpp
|
||||||
|
64
libyul/CompilabilityChecker.cpp
Normal file
64
libyul/CompilabilityChecker.cpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/*(
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Component that checks whether all variables are reachable on the stack.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <libyul/CompilabilityChecker.h>
|
||||||
|
|
||||||
|
#include <libyul/AsmAnalysis.h>
|
||||||
|
#include <libyul/AsmAnalysisInfo.h>
|
||||||
|
|
||||||
|
#include <libyul/backends/evm/EVMCodeTransform.h>
|
||||||
|
#include <libyul/backends/evm/NoOutputAssembly.h>
|
||||||
|
|
||||||
|
#include <liblangutil/EVMVersion.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace yul;
|
||||||
|
using namespace dev;
|
||||||
|
using namespace dev::solidity;
|
||||||
|
|
||||||
|
std::map<YulString, int> CompilabilityChecker::run(std::shared_ptr<Dialect> _dialect, Block const& _ast)
|
||||||
|
{
|
||||||
|
if (_dialect->flavour == AsmFlavour::Yul)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
solAssert(_dialect->flavour == AsmFlavour::Strict, "");
|
||||||
|
|
||||||
|
EVMDialect const& evmDialect = dynamic_cast<EVMDialect const&>(*_dialect);
|
||||||
|
|
||||||
|
bool optimize = true;
|
||||||
|
yul::AsmAnalysisInfo analysisInfo =
|
||||||
|
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, EVMVersion(), _ast);
|
||||||
|
NoOutputAssembly assembly;
|
||||||
|
CodeTransform transform(assembly, analysisInfo, _ast, evmDialect, optimize);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
transform(_ast);
|
||||||
|
}
|
||||||
|
catch (StackTooDeepError const&)
|
||||||
|
{
|
||||||
|
solAssert(!transform.stackErrors().empty(), "Got stack too deep exception that was not stored.");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<YulString, int> functions;
|
||||||
|
for (StackTooDeepError const& error: transform.stackErrors())
|
||||||
|
functions[error.functionName] = max(error.depth, functions[error.functionName]);
|
||||||
|
|
||||||
|
return functions;
|
||||||
|
}
|
45
libyul/CompilabilityChecker.h
Normal file
45
libyul/CompilabilityChecker.h
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Component that checks whether all variables are reachable on the stack.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <libyul/Dialect.h>
|
||||||
|
#include <libyul/AsmDataForward.h>
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace yul
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Component that checks whether all variables are reachable on the stack and
|
||||||
|
* returns a mapping from function name to the largest stack difference found
|
||||||
|
* in that function (no entry present if that function is compilable).
|
||||||
|
* This only works properly if the outermost block is compilable and
|
||||||
|
* functions are not nested. Otherwise, it might miss reporting some functions.
|
||||||
|
*/
|
||||||
|
class CompilabilityChecker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static std::map<YulString, int> run(std::shared_ptr<Dialect> _dialect, Block const& _ast);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -227,6 +227,18 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl)
|
|||||||
checkStackHeight(&_varDecl);
|
checkStackHeight(&_varDecl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CodeTransform::stackError(StackTooDeepError _error, int _targetStackHeight)
|
||||||
|
{
|
||||||
|
m_assembly.appendInstruction(solidity::Instruction::INVALID);
|
||||||
|
// Correct the stack.
|
||||||
|
while (m_assembly.stackHeight() > _targetStackHeight)
|
||||||
|
m_assembly.appendInstruction(solidity::Instruction::POP);
|
||||||
|
while (m_assembly.stackHeight() < _targetStackHeight)
|
||||||
|
m_assembly.appendConstant(u256(0));
|
||||||
|
// Store error.
|
||||||
|
m_stackErrors.emplace_back(std::move(_error));
|
||||||
|
}
|
||||||
|
|
||||||
void CodeTransform::operator()(Assignment const& _assignment)
|
void CodeTransform::operator()(Assignment const& _assignment)
|
||||||
{
|
{
|
||||||
int height = m_assembly.stackHeight();
|
int height = m_assembly.stackHeight();
|
||||||
@ -513,6 +525,8 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
|
|||||||
m_assembly.appendConstant(u256(0));
|
m_assembly.appendConstant(u256(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
CodeTransform(
|
CodeTransform(
|
||||||
m_assembly,
|
m_assembly,
|
||||||
m_info,
|
m_info,
|
||||||
@ -525,6 +539,18 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
|
|||||||
localStackAdjustment,
|
localStackAdjustment,
|
||||||
m_context
|
m_context
|
||||||
)(_function.body);
|
)(_function.body);
|
||||||
|
}
|
||||||
|
catch (StackTooDeepError const& _error)
|
||||||
|
{
|
||||||
|
// This exception will be re-thrown after the end of the surrounding block.
|
||||||
|
// It enables us to see which functions compiled successfully and which did not.
|
||||||
|
// Even if we emit actual code, add an illegal instruction to make sure that tests
|
||||||
|
// will catch it.
|
||||||
|
StackTooDeepError error(_error);
|
||||||
|
if (error.functionName.empty())
|
||||||
|
error.functionName = _function.name;
|
||||||
|
stackError(error, height);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// The stack layout here is:
|
// The stack layout here is:
|
||||||
@ -544,13 +570,19 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
|
|||||||
stackLayout.push_back(i); // Move return values down, but keep order.
|
stackLayout.push_back(i); // Move return values down, but keep order.
|
||||||
|
|
||||||
if (stackLayout.size() > 17)
|
if (stackLayout.size() > 17)
|
||||||
BOOST_THROW_EXCEPTION(StackTooDeepError() << errinfo_comment(
|
{
|
||||||
|
StackTooDeepError error(_function.name, YulString{}, stackLayout.size() - 17);
|
||||||
|
error << errinfo_comment(
|
||||||
"The function " +
|
"The function " +
|
||||||
_function.name.str() +
|
_function.name.str() +
|
||||||
" has " +
|
" has " +
|
||||||
to_string(stackLayout.size() - 17) +
|
to_string(stackLayout.size() - 17) +
|
||||||
" parameters or return variables too many to fit the stack size."
|
" parameters or return variables too many to fit the stack size."
|
||||||
));
|
);
|
||||||
|
stackError(error, m_assembly.stackHeight() - _function.parameters.size());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
while (!stackLayout.empty() && stackLayout.back() != int(stackLayout.size() - 1))
|
while (!stackLayout.empty() && stackLayout.back() != int(stackLayout.size() - 1))
|
||||||
if (stackLayout.back() < 0)
|
if (stackLayout.back() < 0)
|
||||||
{
|
{
|
||||||
@ -565,7 +597,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
|
|||||||
for (int i = 0; size_t(i) < stackLayout.size(); ++i)
|
for (int i = 0; size_t(i) < stackLayout.size(); ++i)
|
||||||
solAssert(i == stackLayout[i], "Error reshuffling stack.");
|
solAssert(i == stackLayout[i], "Error reshuffling stack.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (m_evm15)
|
if (m_evm15)
|
||||||
m_assembly.appendReturnsub(_function.returnVariables.size(), stackHeightBefore);
|
m_assembly.appendReturnsub(_function.returnVariables.size(), stackHeightBefore);
|
||||||
else
|
else
|
||||||
@ -623,6 +655,9 @@ void CodeTransform::operator()(Block const& _block)
|
|||||||
|
|
||||||
finalizeBlock(_block, blockStartStackHeight);
|
finalizeBlock(_block, blockStartStackHeight);
|
||||||
m_scope = originalScope;
|
m_scope = originalScope;
|
||||||
|
|
||||||
|
if (!m_stackErrors.empty())
|
||||||
|
BOOST_THROW_EXCEPTION(m_stackErrors.front());
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _identifier)
|
AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _identifier)
|
||||||
@ -734,20 +769,24 @@ void CodeTransform::generateAssignment(Identifier const& _variableName)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap) const
|
int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap)
|
||||||
{
|
{
|
||||||
solAssert(m_context->variableStackHeights.count(&_var), "");
|
solAssert(m_context->variableStackHeights.count(&_var), "");
|
||||||
int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var];
|
int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var];
|
||||||
solAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable.");
|
solAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable.");
|
||||||
int limit = _forSwap ? 17 : 16;
|
int limit = _forSwap ? 17 : 16;
|
||||||
if (heightDiff > limit)
|
if (heightDiff > limit)
|
||||||
BOOST_THROW_EXCEPTION(StackTooDeepError() << errinfo_comment(
|
{
|
||||||
|
m_stackErrors.emplace_back(_varName, heightDiff - limit);
|
||||||
|
m_stackErrors.back() << errinfo_comment(
|
||||||
"Variable " +
|
"Variable " +
|
||||||
_varName.str() +
|
_varName.str() +
|
||||||
" is " +
|
" is " +
|
||||||
to_string(heightDiff - limit) +
|
to_string(heightDiff - limit) +
|
||||||
" slot(s) too deep inside the stack."
|
" slot(s) too deep inside the stack."
|
||||||
));
|
);
|
||||||
|
BOOST_THROW_EXCEPTION(m_stackErrors.back());
|
||||||
|
}
|
||||||
return heightDiff;
|
return heightDiff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,16 @@ namespace yul
|
|||||||
struct AsmAnalysisInfo;
|
struct AsmAnalysisInfo;
|
||||||
class EVMAssembly;
|
class EVMAssembly;
|
||||||
|
|
||||||
struct StackTooDeepError: virtual YulException {};
|
struct StackTooDeepError: virtual YulException
|
||||||
|
{
|
||||||
|
StackTooDeepError(YulString _variable, int _depth): variable(_variable), depth(_depth) {}
|
||||||
|
StackTooDeepError(YulString _functionName, YulString _variable, int _depth):
|
||||||
|
functionName(_functionName), variable(_variable), depth(_depth)
|
||||||
|
{}
|
||||||
|
YulString functionName;
|
||||||
|
YulString variable;
|
||||||
|
int depth;
|
||||||
|
};
|
||||||
|
|
||||||
struct CodeTransformContext
|
struct CodeTransformContext
|
||||||
{
|
{
|
||||||
@ -115,6 +124,8 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<StackTooDeepError> const& stackErrors() const { return m_stackErrors; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Context = CodeTransformContext;
|
using Context = CodeTransformContext;
|
||||||
|
|
||||||
@ -178,12 +189,16 @@ private:
|
|||||||
/// Determines the stack height difference to the given variables. Throws
|
/// Determines the stack height difference to the given variables. Throws
|
||||||
/// if it is not yet in scope or the height difference is too large. Returns
|
/// if it is not yet in scope or the height difference is too large. Returns
|
||||||
/// the (positive) stack height difference otherwise.
|
/// the (positive) stack height difference otherwise.
|
||||||
int variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap) const;
|
int variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap);
|
||||||
|
|
||||||
void expectDeposit(int _deposit, int _oldHeight) const;
|
void expectDeposit(int _deposit, int _oldHeight) const;
|
||||||
|
|
||||||
void checkStackHeight(void const* _astElement) const;
|
void checkStackHeight(void const* _astElement) const;
|
||||||
|
|
||||||
|
/// Stores the stack error in the list of errors, appends an invalid opcode
|
||||||
|
/// and corrects the stack height to the target stack height.
|
||||||
|
void stackError(StackTooDeepError _error, int _targetStackSize);
|
||||||
|
|
||||||
AbstractAssembly& m_assembly;
|
AbstractAssembly& m_assembly;
|
||||||
AsmAnalysisInfo& m_info;
|
AsmAnalysisInfo& m_info;
|
||||||
Scope* m_scope = nullptr;
|
Scope* m_scope = nullptr;
|
||||||
@ -204,6 +219,8 @@ private:
|
|||||||
/// statement level in the scope where the variable was defined.
|
/// statement level in the scope where the variable was defined.
|
||||||
std::set<Scope::Variable const*> m_variablesScheduledForDeletion;
|
std::set<Scope::Variable const*> m_variablesScheduledForDeletion;
|
||||||
std::set<int> m_unusedStackSlots;
|
std::set<int> m_unusedStackSlots;
|
||||||
|
|
||||||
|
std::vector<StackTooDeepError> m_stackErrors;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -62,5 +62,7 @@ void EVMObjectCompiler::run(Object& _object, bool _optimize)
|
|||||||
yulAssert(_object.code, "No code.");
|
yulAssert(_object.code, "No code.");
|
||||||
// We do not catch and re-throw the stack too deep exception here because it is a YulException,
|
// We do not catch and re-throw the stack too deep exception here because it is a YulException,
|
||||||
// which should be native to this part of the code.
|
// which should be native to this part of the code.
|
||||||
CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, m_dialect, _optimize, m_evm15}(*_object.code);
|
CodeTransform transform{m_assembly, *_object.analysisInfo, *_object.code, m_dialect, _optimize, m_evm15};
|
||||||
|
transform(*_object.code);
|
||||||
|
yulAssert(transform.stackErrors().empty(), "Stack errors present but not thrown.");
|
||||||
}
|
}
|
||||||
|
143
libyul/backends/evm/NoOutputAssembly.cpp
Normal file
143
libyul/backends/evm/NoOutputAssembly.cpp
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Assembly interface that ignores everything. Can be used as a backend for a compilation dry-run.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <libyul/backends/evm/NoOutputAssembly.h>
|
||||||
|
|
||||||
|
#include <libevmasm/Instruction.h>
|
||||||
|
|
||||||
|
#include <liblangutil/Exceptions.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace dev;
|
||||||
|
using namespace langutil;
|
||||||
|
using namespace yul;
|
||||||
|
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendInstruction(solidity::Instruction _instr)
|
||||||
|
{
|
||||||
|
m_stackHeight += solidity::instructionInfo(_instr).ret - solidity::instructionInfo(_instr).args;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendConstant(u256 const&)
|
||||||
|
{
|
||||||
|
appendInstruction(solidity::pushInstruction(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendLabel(LabelID)
|
||||||
|
{
|
||||||
|
appendInstruction(solidity::Instruction::JUMPDEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendLabelReference(LabelID)
|
||||||
|
{
|
||||||
|
solAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
|
||||||
|
appendInstruction(solidity::pushInstruction(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
NoOutputAssembly::LabelID NoOutputAssembly::newLabelId()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractAssembly::LabelID NoOutputAssembly::namedLabel(string const&)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendLinkerSymbol(string const&)
|
||||||
|
{
|
||||||
|
solAssert(false, "Linker symbols not yet implemented.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendJump(int _stackDiffAfter)
|
||||||
|
{
|
||||||
|
solAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
|
||||||
|
appendInstruction(solidity::Instruction::JUMP);
|
||||||
|
m_stackHeight += _stackDiffAfter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
|
||||||
|
{
|
||||||
|
if (m_evm15)
|
||||||
|
m_stackHeight += _stackDiffAfter;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
appendLabelReference(_labelId);
|
||||||
|
appendJump(_stackDiffAfter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendJumpToIf(LabelID _labelId)
|
||||||
|
{
|
||||||
|
if (m_evm15)
|
||||||
|
m_stackHeight--;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
appendLabelReference(_labelId);
|
||||||
|
appendInstruction(solidity::Instruction::JUMPI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendBeginsub(LabelID, int _arguments)
|
||||||
|
{
|
||||||
|
solAssert(m_evm15, "BEGINSUB used for EVM 1.0");
|
||||||
|
solAssert(_arguments >= 0, "");
|
||||||
|
m_stackHeight += _arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendJumpsub(LabelID, int _arguments, int _returns)
|
||||||
|
{
|
||||||
|
solAssert(m_evm15, "JUMPSUB used for EVM 1.0");
|
||||||
|
solAssert(_arguments >= 0 && _returns >= 0, "");
|
||||||
|
m_stackHeight += _returns - _arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
|
||||||
|
{
|
||||||
|
solAssert(m_evm15, "RETURNSUB used for EVM 1.0");
|
||||||
|
solAssert(_returns >= 0, "");
|
||||||
|
m_stackHeight += _stackDiffAfter - _returns;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendAssemblySize()
|
||||||
|
{
|
||||||
|
appendInstruction(solidity::Instruction::PUSH1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> NoOutputAssembly::createSubAssembly()
|
||||||
|
{
|
||||||
|
solAssert(false, "Sub assemblies not implemented.");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendDataOffset(AbstractAssembly::SubID)
|
||||||
|
{
|
||||||
|
appendInstruction(solidity::Instruction::PUSH1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NoOutputAssembly::appendDataSize(AbstractAssembly::SubID)
|
||||||
|
{
|
||||||
|
appendInstruction(solidity::Instruction::PUSH1);
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractAssembly::SubID NoOutputAssembly::appendData(bytes const&)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
75
libyul/backends/evm/NoOutputAssembly.h
Normal file
75
libyul/backends/evm/NoOutputAssembly.h
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Assembly interface that ignores everything. Can be used as a backend for a compilation dry-run.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <libyul/backends/evm/AbstractAssembly.h>
|
||||||
|
|
||||||
|
#include <libevmasm/LinkerObject.h>
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace langutil
|
||||||
|
{
|
||||||
|
struct SourceLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace yul
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assembly class that just ignores everything and only performs stack counting.
|
||||||
|
* The purpose is to use this assembly for compilation dry-runs.
|
||||||
|
*/
|
||||||
|
class NoOutputAssembly: public AbstractAssembly
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit NoOutputAssembly(bool _evm15 = false): m_evm15(_evm15) { }
|
||||||
|
virtual ~NoOutputAssembly() = default;
|
||||||
|
|
||||||
|
void setSourceLocation(langutil::SourceLocation const&) override {}
|
||||||
|
int stackHeight() const override { return m_stackHeight; }
|
||||||
|
void appendInstruction(dev::solidity::Instruction _instruction) override;
|
||||||
|
void appendConstant(dev::u256 const& _constant) override;
|
||||||
|
void appendLabel(LabelID _labelId) override;
|
||||||
|
void appendLabelReference(LabelID _labelId) override;
|
||||||
|
LabelID newLabelId() override;
|
||||||
|
LabelID namedLabel(std::string const& _name) override;
|
||||||
|
void appendLinkerSymbol(std::string const& _name) override;
|
||||||
|
|
||||||
|
void appendJump(int _stackDiffAfter) override;
|
||||||
|
void appendJumpTo(LabelID _labelId, int _stackDiffAfter) override;
|
||||||
|
void appendJumpToIf(LabelID _labelId) override;
|
||||||
|
void appendBeginsub(LabelID _labelId, int _arguments) override;
|
||||||
|
void appendJumpsub(LabelID _labelId, int _arguments, int _returns) override;
|
||||||
|
void appendReturnsub(int _returns, int _stackDiffAfter) override;
|
||||||
|
|
||||||
|
void appendAssemblySize() override;
|
||||||
|
std::pair<std::shared_ptr<AbstractAssembly>, SubID> createSubAssembly() override;
|
||||||
|
void appendDataOffset(SubID _sub) override;
|
||||||
|
void appendDataSize(SubID _sub) override;
|
||||||
|
SubID appendData(dev::bytes const& _data) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_evm15 = false; ///< if true, switch to evm1.5 mode
|
||||||
|
int m_stackHeight = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
225
test/libyul/CompilabilityChecker.cpp
Normal file
225
test/libyul/CompilabilityChecker.cpp
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Unit tests for the compilability checker.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <test/Options.h>
|
||||||
|
|
||||||
|
#include <test/libyul/Common.h>
|
||||||
|
#include <libyul/backends/evm/EVMDialect.h>
|
||||||
|
|
||||||
|
#include <libyul/CompilabilityChecker.h>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace yul
|
||||||
|
{
|
||||||
|
namespace test
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
string check(string const& _input)
|
||||||
|
{
|
||||||
|
shared_ptr<Block> ast = yul::test::parse(_input, false).first;
|
||||||
|
BOOST_REQUIRE(ast);
|
||||||
|
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(), *ast);
|
||||||
|
string out;
|
||||||
|
for (auto const& function: functions)
|
||||||
|
out += function.first.str() + ": " + to_string(function.second) + " ";
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(CompilabilityChecker)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(smoke_test)
|
||||||
|
{
|
||||||
|
string out = check("{}");
|
||||||
|
BOOST_CHECK_EQUAL(out, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(simple_function)
|
||||||
|
{
|
||||||
|
string out = check("{ function f(a, b) -> x, y { x := a y := b } }");
|
||||||
|
BOOST_CHECK_EQUAL(out, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(many_variables_few_uses)
|
||||||
|
{
|
||||||
|
string out = check(R"({
|
||||||
|
function f(a, b) -> x, y {
|
||||||
|
let r1 := 0
|
||||||
|
let r2 := 0
|
||||||
|
let r3 := 0
|
||||||
|
let r4 := 0
|
||||||
|
let r5 := 0
|
||||||
|
let r6 := 0
|
||||||
|
let r7 := 0
|
||||||
|
let r8 := 0
|
||||||
|
let r9 := 0
|
||||||
|
let r10 := 0
|
||||||
|
let r11 := 0
|
||||||
|
let r12 := 0
|
||||||
|
let r13 := 0
|
||||||
|
let r14 := 0
|
||||||
|
let r15 := 0
|
||||||
|
let r16 := 0
|
||||||
|
let r17 := 0
|
||||||
|
let r18 := 0
|
||||||
|
x := add(add(add(add(add(add(add(add(add(x, r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
||||||
|
}
|
||||||
|
})");
|
||||||
|
BOOST_CHECK_EQUAL(out, "f: 4 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(many_variables_many_uses)
|
||||||
|
{
|
||||||
|
string out = check(R"({
|
||||||
|
function f(a, b) -> x, y {
|
||||||
|
let r1 := 0
|
||||||
|
let r2 := 0
|
||||||
|
let r3 := 0
|
||||||
|
let r4 := 0
|
||||||
|
let r5 := 0
|
||||||
|
let r6 := 0
|
||||||
|
let r7 := 0
|
||||||
|
let r8 := 0
|
||||||
|
let r9 := 0
|
||||||
|
let r10 := 0
|
||||||
|
let r11 := 0
|
||||||
|
let r12 := 0
|
||||||
|
let r13 := 0
|
||||||
|
let r14 := 0
|
||||||
|
let r15 := 0
|
||||||
|
let r16 := 0
|
||||||
|
let r17 := 0
|
||||||
|
let r18 := 0
|
||||||
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
||||||
|
}
|
||||||
|
})");
|
||||||
|
BOOST_CHECK_EQUAL(out, "f: 10 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(many_return_variables)
|
||||||
|
{
|
||||||
|
string out = check(R"({
|
||||||
|
function f(a, b) -> r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19 {
|
||||||
|
}
|
||||||
|
})");
|
||||||
|
BOOST_CHECK_EQUAL(out, "f: 5 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(multiple_functions)
|
||||||
|
{
|
||||||
|
string out = check(R"({
|
||||||
|
function f(a, b) -> r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19 {
|
||||||
|
}
|
||||||
|
function g(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19) -> x, y {
|
||||||
|
}
|
||||||
|
function h(x) {
|
||||||
|
let r1 := 0
|
||||||
|
let r2 := 0
|
||||||
|
let r3 := 0
|
||||||
|
let r4 := 0
|
||||||
|
let r5 := 0
|
||||||
|
let r6 := 0
|
||||||
|
let r7 := 0
|
||||||
|
let r8 := 0
|
||||||
|
let r9 := 0
|
||||||
|
let r10 := 0
|
||||||
|
let r11 := 0
|
||||||
|
let r12 := 0
|
||||||
|
let r13 := 0
|
||||||
|
let r14 := 0
|
||||||
|
let r15 := 0
|
||||||
|
let r16 := 0
|
||||||
|
let r17 := 0
|
||||||
|
let r18 := 0
|
||||||
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
||||||
|
}
|
||||||
|
})");
|
||||||
|
BOOST_CHECK_EQUAL(out, "h: 9 g: 5 f: 5 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(nested)
|
||||||
|
{
|
||||||
|
string out = check(R"({
|
||||||
|
function h(x) {
|
||||||
|
let r1 := 0
|
||||||
|
let r2 := 0
|
||||||
|
let r3 := 0
|
||||||
|
let r4 := 0
|
||||||
|
let r5 := 0
|
||||||
|
let r6 := 0
|
||||||
|
let r7 := 0
|
||||||
|
let r8 := 0
|
||||||
|
let r9 := 0
|
||||||
|
let r10 := 0
|
||||||
|
let r11 := 0
|
||||||
|
let r12 := 0
|
||||||
|
let r13 := 0
|
||||||
|
let r14 := 0
|
||||||
|
let r15 := 0
|
||||||
|
let r16 := 0
|
||||||
|
let r17 := 0
|
||||||
|
let r18 := 0
|
||||||
|
function f(a, b) -> t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19 {
|
||||||
|
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
||||||
|
}
|
||||||
|
})");
|
||||||
|
BOOST_CHECK_EQUAL(out, "h: 9 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(also_in_outer_block)
|
||||||
|
{
|
||||||
|
string out = check(R"({
|
||||||
|
let x := 0
|
||||||
|
let r1 := 0
|
||||||
|
let r2 := 0
|
||||||
|
let r3 := 0
|
||||||
|
let r4 := 0
|
||||||
|
let r5 := 0
|
||||||
|
let r6 := 0
|
||||||
|
let r7 := 0
|
||||||
|
let r8 := 0
|
||||||
|
let r9 := 0
|
||||||
|
let r10 := 0
|
||||||
|
let r11 := 0
|
||||||
|
let r12 := 0
|
||||||
|
let r13 := 0
|
||||||
|
let r14 := 0
|
||||||
|
let r15 := 0
|
||||||
|
let r16 := 0
|
||||||
|
let r17 := 0
|
||||||
|
let r18 := 0
|
||||||
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
||||||
|
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
|
||||||
|
}
|
||||||
|
})");
|
||||||
|
BOOST_CHECK_EQUAL(out, ": 9 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user