EWasm translation tests.

This commit is contained in:
chriseth 2019-11-19 23:08:33 +01:00
parent 6625f634fc
commit d4d0df021d
13 changed files with 334 additions and 0 deletions

View File

@ -129,6 +129,8 @@ set(libyul_sources
libyul/Common.cpp
libyul/Common.h
libyul/CompilabilityChecker.cpp
libyul/EWasmTranslationTest.cpp
libyul/EWasmTranslationTest.h
libyul/FunctionSideEffects.cpp
libyul/FunctionSideEffects.h
libyul/Inliner.cpp

View File

@ -24,6 +24,7 @@
#include <test/libsolidity/SyntaxTest.h>
#include <test/libsolidity/SemanticTest.h>
#include <test/libsolidity/SMTCheckerJSONTest.h>
#include <test/libyul/EWasmTranslationTest.h>
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulInterpreterTest.h>
#include <test/libyul/ObjectCompilerTest.h>
@ -54,6 +55,7 @@ struct Testsuite
Testsuite const g_interactiveTestsuites[] = {
/*
Title Path Subpath SMT NeedsVM Creator function */
{"EWasm Translation", "libyul", "ewasmTranslationTests",false,false, &yul::test::EWasmTranslationTest::create},
{"Yul Optimizer", "libyul", "yulOptimizerTests", false, false, &yul::test::YulOptimizerTest::create},
{"Yul Interpreter", "libyul", "yulInterpreterTests", false, false, &yul::test::YulInterpreterTest::create},
{"Yul Object Compiler", "libyul", "objectCompiler", false, false, &yul::test::ObjectCompilerTest::create},

View File

@ -0,0 +1,156 @@
/*
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/>.
*/
#include <test/libyul/EWasmTranslationTest.h>
#include <test/tools/yulInterpreter/Interpreter.h>
#include <test/Options.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/backends/wasm/EVMToEWasmTranslator.h>
#include <libyul/AsmParser.h>
#include <libyul/AssemblyStack.h>
#include <libyul/AsmAnalysisInfo.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <libdevcore/AnsiColorized.h>
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
using namespace dev;
using namespace langutil;
using namespace yul;
using namespace yul::test;
using namespace dev::solidity;
using namespace dev::solidity::test;
using namespace std;
EWasmTranslationTest::EWasmTranslationTest(string const& _filename)
{
boost::filesystem::path path(_filename);
ifstream file(_filename);
if (!file)
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test case: \"" + _filename + "\"."));
file.exceptions(ios::badbit);
m_source = parseSourceAndSettings(file);
m_expectation = parseSimpleExpectations(file);
}
TestCase::TestResult EWasmTranslationTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
if (!parse(_stream, _linePrefix, _formatted))
return TestResult::FatalError;
*m_object = EVMToEWasmTranslator(
EVMDialect::strictAssemblyForEVMObjects(dev::test::Options::get().evmVersion())
).run(*m_object);
// Add call to "main()".
m_object->code->statements.emplace_back(
ExpressionStatement{{}, FunctionCall{{}, Identifier{{}, "main"_yulstring}, {}}}
);
m_obtainedResult = interpret();
if (m_expectation != m_obtainedResult)
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
void EWasmTranslationTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void EWasmTranslationTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
}
void EWasmTranslationTest::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
{
stringstream output(_output);
string line;
while (getline(output, line))
_stream << _linePrefix << line << endl;
}
bool EWasmTranslationTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
AssemblyStack stack(
dev::test::Options::get().evmVersion(),
AssemblyStack::Language::StrictAssembly,
dev::solidity::OptimiserSettings::none()
);
if (stack.parseAndAnalyze("", m_source))
{
m_object = stack.parserResult();
return true;
}
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, stack.errors());
return false;
}
}
string EWasmTranslationTest::interpret()
{
InterpreterState state;
state.maxTraceSize = 10000;
state.maxSteps = 10000;
WasmDialect dialect;
Interpreter interpreter(state, dialect);
try
{
interpreter(*m_object->code);
}
catch (InterpreterTerminatedGeneric const&)
{
}
stringstream result;
state.dumpTraceAndState(result);
return result.str();
}
void EWasmTranslationTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}

View File

@ -0,0 +1,65 @@
/*
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/>.
*/
#pragma once
#include <test/TestCase.h>
#include <libyul/Object.h>
namespace langutil
{
class Scanner;
class Error;
using ErrorList = std::vector<std::shared_ptr<Error const>>;
}
namespace yul
{
namespace test
{
class EWasmTranslationTest: public dev::solidity::test::EVMVersionRestrictedTestCase
{
public:
static std::unique_ptr<TestCase> create(Config const& _config)
{
return std::unique_ptr<TestCase>(new EWasmTranslationTest(_config.filename));
}
explicit EWasmTranslationTest(std::string const& _filename);
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const &_linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private:
void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const;
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
std::string interpret();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_source;
std::string m_expectation;
std::shared_ptr<Object> m_object;
std::string m_obtainedResult;
};
}
}

View File

@ -0,0 +1,20 @@
object "main"
{
code {
datacopy(0, and(dataoffset("main"), 15), and(datasize("main"), 15))
datacopy(32, and(dataoffset("sub"), 15), and(datasize("sub"), 15))
sstore(0, mload(0))
sstore(1, mload(32))
}
object "sub" { code { sstore(0, 1) } }
}
// ----
// Trace:
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000001
// 20: 636f6465636f6465000000000000000000000000000000000000000000000000
// 40: 6465636f00000000000000000000000000000000000000000000000000000000
// 60: 636f6465636f6465000000000000000000000000000000000000000000000000
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000000: 6465636f00000000000000000000000000000000000000000000000000000000
// 0000000000000000000000000000000000000000000000000000000000000001: 636f6465636f6465000000000000000000000000000000000000000000000000

View File

@ -0,0 +1,16 @@
object "main"
{
code {
sstore(0, dataoffset("main"))
sstore(1, dataoffset("sub"))
}
object "sub" { code { sstore(0, 1) } }
}
// ----
// Trace:
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000001
// 20: 000000000000000000000000000000000000000000000000000000000000070c
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000000: 000000000000000000000000000000000000000000000000000000000000006e
// 0000000000000000000000000000000000000000000000000000000000000001: 000000000000000000000000000000000000000000000000000000000000070c

View File

@ -0,0 +1,16 @@
object "main"
{
code {
sstore(0, datasize("main"))
sstore(1, datasize("sub"))
}
object "sub" { code { sstore(0, 1) } }
}
// ----
// Trace:
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000001
// 20: 0000000000000000000000000000000000000000000000000000000000000109
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000000: 0000000000000000000000000000000000000000000000000000000000000b64
// 0000000000000000000000000000000000000000000000000000000000000001: 0000000000000000000000000000000000000000000000000000000000000109

View File

@ -0,0 +1,14 @@
{
mstore(0x20, 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20)
mstore(0x40, mload(0x20))
sstore(1, mload(0x40))
}
// ----
// Trace:
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000001
// 20: 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
// 60: 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
// 80: 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000001: 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20

View File

@ -0,0 +1,17 @@
{
let x := 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
let y := shl(120, x)
let z := shr(136, y)
sstore(0, y)
sstore(1, z)
}
// ====
// EVMVersion: >=constantinople
// ----
// Trace:
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000001
// 20: 0000000000000000000000000000000000101112131415161718191a1b1c1d1e
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000000: 101112131415161718191a1b1c1d1e1f20000000000000000000000000000000
// 0000000000000000000000000000000000000000000000000000000000000001: 0000000000000000000000000000000000101112131415161718191a1b1c1d1e

View File

@ -0,0 +1,8 @@
{
mstore(0x20, 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20)
}
// ----
// Trace:
// Memory dump:
// 60: 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
// Storage dump:

View File

@ -0,0 +1,12 @@
{
sstore(1, 7)
sstore(2, sub(0, 1))
}
// ----
// Trace:
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000002
// 20: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000001: 0000000000000000000000000000000000000000000000000000000000000007
// 0000000000000000000000000000000000000000000000000000000000000002: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

View File

@ -0,0 +1,5 @@
{}
// ----
// Trace:
// Memory dump:
// Storage dump:

View File

@ -31,6 +31,7 @@ add_executable(isoltest
../libsolidity/ASTJSONTest.cpp
../libsolidity/SMTCheckerJSONTest.cpp
../libyul/Common.cpp
../libyul/EWasmTranslationTest.cpp
../libyul/FunctionSideEffects.cpp
../libyul/ObjectCompilerTest.cpp
../libyul/YulOptimizerTest.cpp