Fail on assertion failures in yul code generation.

This commit is contained in:
chriseth 2020-11-09 14:26:59 +01:00 committed by Alex Beregszaszi
parent 7d036dcb81
commit a4769d446f
2 changed files with 120 additions and 130 deletions

View File

@ -104,6 +104,7 @@ struct ErrorId
{ {
unsigned long long error = 0; unsigned long long error = 0;
bool operator==(ErrorId const& _rhs) const { return error == _rhs.error; } bool operator==(ErrorId const& _rhs) const { return error == _rhs.error; }
bool operator!=(ErrorId const& _rhs) const { return !(*this == _rhs); }
}; };
constexpr ErrorId operator"" _error(unsigned long long _error) { return ErrorId{ _error }; } constexpr ErrorId operator"" _error(unsigned long long _error) { return ErrorId{ _error }; }

View File

@ -30,6 +30,7 @@
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::yul; using namespace solidity::yul;
using namespace solidity::langutil;
using namespace solidity::util; using namespace solidity::util;
using namespace solidity::util::formatting; using namespace solidity::util::formatting;
using namespace solidity::frontend::test; using namespace solidity::frontend::test;
@ -120,37 +121,38 @@ TestCase::TestResult SemanticTest::run(ostream& _stream, string const& _linePref
TestCase::TestResult SemanticTest::runTest(ostream& _stream, string const& _linePrefix, bool _formatted, bool _compileViaYul, bool _compileToEwasm) TestCase::TestResult SemanticTest::runTest(ostream& _stream, string const& _linePrefix, bool _formatted, bool _compileViaYul, bool _compileToEwasm)
{ {
try bool success = true;
if (_compileViaYul && _compileToEwasm)
selectVM(evmc_capabilities::EVMC_CAPABILITY_EWASM);
else
selectVM(evmc_capabilities::EVMC_CAPABILITY_EVM1);
reset();
m_compileViaYul = _compileViaYul;
if (_compileToEwasm)
{ {
bool success = true; soltestAssert(m_compileViaYul, "");
m_compileToEwasm = _compileToEwasm;
}
if (_compileViaYul && _compileToEwasm) m_compileViaYulCanBeSet = false;
selectVM(evmc_capabilities::EVMC_CAPABILITY_EWASM);
else
selectVM(evmc_capabilities::EVMC_CAPABILITY_EVM1);
reset(); if (_compileViaYul)
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Running via Yul:" << endl;
m_compileViaYul = _compileViaYul; for (auto& test: m_tests)
if (_compileToEwasm) test.reset();
{
soltestAssert(m_compileViaYul, "");
m_compileToEwasm = _compileToEwasm;
}
m_compileViaYulCanBeSet = false; map<string, solidity::test::Address> libraries;
if (_compileViaYul) bool constructed = false;
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Running via Yul:" << endl;
for (auto& test: m_tests) for (auto& test: m_tests)
test.reset(); {
map<string, solidity::test::Address> libraries; try
bool constructed = false;
for (auto& test: m_tests)
{ {
if (constructed) if (constructed)
{ {
@ -175,124 +177,111 @@ TestCase::TestResult SemanticTest::runTest(ostream& _stream, string const& _line
soltestAssert(deploy("", 0, bytes(), libraries), "Failed to deploy contract."); soltestAssert(deploy("", 0, bytes(), libraries), "Failed to deploy contract.");
constructed = true; constructed = true;
} }
}
catch (langutil::UnimplementedFeatureError const&)
{
// ignore unimplemented feature errors when forcibly trying to compile via yul.
if (!_compileViaYul || m_runWithYul)
throw;
}
catch (langutil::Error const& _error)
{
// ignore unimplemented feature errors when forcibly trying to compile via yul.
if (_error.errorId() != 1834_error || !_compileViaYul || m_runWithYul)
throw;
}
if (test.call().kind == FunctionCall::Kind::Storage) if (test.call().kind == FunctionCall::Kind::Storage)
{ {
test.setFailure(false); test.setFailure(false);
bytes result(1, !storageEmpty(m_contractAddress)); bytes result(1, !storageEmpty(m_contractAddress));
test.setRawBytes(result); test.setRawBytes(result);
soltestAssert(test.call().expectations.rawBytes().size() == 1, ""); soltestAssert(test.call().expectations.rawBytes().size() == 1, "");
if (test.call().expectations.rawBytes() != result) if (test.call().expectations.rawBytes() != result)
success = false; success = false;
} }
else if (test.call().kind == FunctionCall::Kind::Constructor) else if (test.call().kind == FunctionCall::Kind::Constructor)
{ {
if (m_transactionSuccessful == test.call().expectations.failure) if (m_transactionSuccessful == test.call().expectations.failure)
success = false; success = false;
test.setFailure(!m_transactionSuccessful); test.setFailure(!m_transactionSuccessful);
test.setRawBytes(bytes()); test.setRawBytes(bytes());
} }
else
{
bytes output;
if (test.call().kind == FunctionCall::Kind::LowLevel)
output = callLowLevel(test.call().arguments.rawBytes(), test.call().value.value);
else else
{ {
bytes output; soltestAssert(
if (test.call().kind == FunctionCall::Kind::LowLevel) m_allowNonExistingFunctions ||
output = callLowLevel(test.call().arguments.rawBytes(), test.call().value.value); m_compiler.methodIdentifiers(m_compiler.lastContractName()).isMember(test.call().signature),
else "The function " + test.call().signature + " is not known to the compiler"
{ );
soltestAssert(
m_allowNonExistingFunctions ||
m_compiler.methodIdentifiers(m_compiler.lastContractName()).isMember(test.call().signature),
"The function " + test.call().signature + " is not known to the compiler"
);
output = callContractFunctionWithValueNoEncoding( output = callContractFunctionWithValueNoEncoding(
test.call().signature, test.call().signature,
test.call().value.value, test.call().value.value,
test.call().arguments.rawBytes() test.call().arguments.rawBytes()
); );
}
bool outputMismatch = (output != test.call().expectations.rawBytes());
// Pre byzantium, it was not possible to return failure data, so we disregard
// output mismatch for those EVM versions.
if (test.call().expectations.failure && !m_transactionSuccessful && !m_evmVersion.supportsReturndata())
outputMismatch = false;
if (m_transactionSuccessful != !test.call().expectations.failure || outputMismatch)
success = false;
test.setFailure(!m_transactionSuccessful);
test.setRawBytes(std::move(output));
test.setContractABI(m_compiler.contractABI(m_compiler.lastContractName()));
} }
}
if (success && !m_runWithYul && _compileViaYul) bool outputMismatch = (output != test.call().expectations.rawBytes());
{ // Pre byzantium, it was not possible to return failure data, so we disregard
m_compileViaYulCanBeSet = true; // output mismatch for those EVM versions.
AnsiColorized(_stream, _formatted, {BOLD, YELLOW}) << if (test.call().expectations.failure && !m_transactionSuccessful && !m_evmVersion.supportsReturndata())
_linePrefix << endl << outputMismatch = false;
_linePrefix << "Test can pass via Yul and marked with compileViaYul: false." << endl; if (m_transactionSuccessful != !test.call().expectations.failure || outputMismatch)
return TestResult::Failure; success = false;
}
if (!success && (m_runWithYul || !_compileViaYul)) test.setFailure(!m_transactionSuccessful);
test.setRawBytes(std::move(output));
test.setContractABI(m_compiler.contractABI(m_compiler.lastContractName()));
}
}
if (success && !m_runWithYul && _compileViaYul)
{
m_compileViaYulCanBeSet = true;
AnsiColorized(_stream, _formatted, {BOLD, YELLOW}) <<
_linePrefix << endl <<
_linePrefix << "Test can pass via Yul and marked with compileViaYul: false." << endl;
return TestResult::Failure;
}
if (!success && (m_runWithYul || !_compileViaYul))
{
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
for (auto const& test: m_tests)
{ {
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl; ErrorReporter errorReporter;
for (auto const& test: m_tests) _stream << test.format(errorReporter, _linePrefix, false, _formatted) << endl;
{ _stream << errorReporter.format(_linePrefix, _formatted);
ErrorReporter errorReporter; }
_stream << test.format(errorReporter, _linePrefix, false, _formatted) << endl; _stream << endl;
_stream << errorReporter.format(_linePrefix, _formatted); AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
} for (auto const& test: m_tests)
{
ErrorReporter errorReporter;
_stream << test.format(errorReporter, _linePrefix, true, _formatted) << endl;
_stream << errorReporter.format(_linePrefix, _formatted);
}
AnsiColorized(_stream, _formatted, {BOLD, RED})
<< _linePrefix << endl
<< _linePrefix << "Attention: Updates on the test will apply the detected format displayed." << endl;
if (_compileViaYul && m_runWithoutYul)
{
_stream << _linePrefix << endl << _linePrefix;
AnsiColorized(_stream, _formatted, {RED_BACKGROUND}) << "Note that the test passed without Yul.";
_stream << endl; _stream << endl;
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
for (auto const& test: m_tests)
{
ErrorReporter errorReporter;
_stream << test.format(errorReporter, _linePrefix, true, _formatted) << endl;
_stream << errorReporter.format(_linePrefix, _formatted);
}
AnsiColorized(_stream, _formatted, {BOLD, RED})
<< _linePrefix << endl
<< _linePrefix << "Attention: Updates on the test will apply the detected format displayed." << endl;
if (_compileViaYul && m_runWithoutYul)
{
_stream << _linePrefix << endl << _linePrefix;
AnsiColorized(_stream, _formatted, {RED_BACKGROUND}) << "Note that the test passed without Yul.";
_stream << endl;
}
else if (!_compileViaYul && m_runWithYul)
AnsiColorized(_stream, _formatted, {BOLD, YELLOW})
<< _linePrefix << endl
<< _linePrefix << "Note that the test also has to pass via Yul." << endl;
return TestResult::Failure;
} }
} else if (!_compileViaYul && m_runWithYul)
catch (WhiskersError const&) AnsiColorized(_stream, _formatted, {BOLD, YELLOW})
{ << _linePrefix << endl
// this is an error in Whiskers template, so should be thrown anyway << _linePrefix << "Note that the test also has to pass via Yul." << endl;
throw; return TestResult::Failure;
}
catch (YulException const&)
{
// this should be an error in yul compilation or translation
throw;
}
catch (boost::exception const&)
{
if (!_compileViaYul || m_runWithYul)
throw;
}
catch (std::exception const&)
{
if (!_compileViaYul || m_runWithYul)
throw;
}
catch (...)
{
if (!_compileViaYul || m_runWithYul)
throw;
} }
return TestResult::Success; return TestResult::Success;