Interpreter tests.

This commit is contained in:
chriseth 2018-02-08 18:20:54 +01:00
parent e91be8222c
commit 5eb155b894
12 changed files with 332 additions and 2 deletions

View File

@ -30,7 +30,7 @@ add_executable(soltest ${sources} ${headers}
${libsolidity_sources} ${libsolidity_headers}
${libsolidity_util_sources} ${libsolidity_util_headers}
)
target_link_libraries(soltest PRIVATE libsolc yul solidity evmasm devcore ${Boost_PROGRAM_OPTIONS_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
target_link_libraries(soltest PRIVATE libsolc yul solidity yulInterpreter evmasm devcore ${Boost_PROGRAM_OPTIONS_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
if (LLL)
target_link_libraries(soltest PRIVATE lll)

View File

@ -23,6 +23,7 @@
#include <test/libsolidity/SemanticTest.h>
#include <test/libsolidity/SMTCheckerJSONTest.h>
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulInterpreterTest.h>
#include <test/libyul/ObjectCompilerTest.h>
#include <boost/filesystem.hpp>
@ -51,6 +52,7 @@ Testsuite const g_interactiveTestsuites[] = {
/*
Title Path Subpath SMT IPC Creator function */
{"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},
{"Syntax", "libsolidity", "syntaxTests", false, false, &SyntaxTest::create},
{"Semantic", "libsolidity", "semanticTests", false, true, &SemanticTest::create},

View File

@ -0,0 +1,160 @@
/*
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/YulInterpreterTest.h>
#include <test/tools/yulInterpreter/Interpreter.h>
#include <test/Options.h>
#include <libyul/backends/evm/EVMDialect.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;
YulInterpreterTest::YulInterpreterTest(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);
string line;
while (getline(file, line))
{
if (boost::algorithm::starts_with(line, "// ----"))
break;
m_source += std::move(line) + "\n";
}
while (getline(file, line))
if (boost::algorithm::starts_with(line, "// "))
m_expectation += line.substr(3) + "\n";
else
m_expectation += line + "\n";
}
bool YulInterpreterTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
if (!parse(_stream, _linePrefix, _formatted))
return false;
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 false;
}
return true;
}
void YulInterpreterTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void YulInterpreterTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
}
void YulInterpreterTest::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
{
stringstream output(_output);
string line;
while (getline(output, line))
_stream << _linePrefix << line << endl;
}
bool YulInterpreterTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
AssemblyStack stack(dev::test::Options::get().evmVersion(), AssemblyStack::Language::StrictAssembly);
if (stack.parseAndAnalyze("", m_source))
{
m_ast = stack.parserResult()->code;
m_analysisInfo = stack.parserResult()->analysisInfo;
return true;
}
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, stack.errors());
return false;
}
}
string YulInterpreterTest::interpret()
{
InterpreterState state;
state.maxTraceSize = 10000;
Interpreter interpreter(state);
try
{
interpreter(*m_ast);
}
catch (InterpreterTerminated const&)
{
}
stringstream result;
result << "Trace:" << endl;;
for (auto const& line: interpreter.trace())
result << " " << line << endl;
result << "Memory dump:\n";
for (size_t i = 0; i < state.memory.size(); i += 0x20)
result << " " << std::hex << std::setw(4) << i << ": " << toHex(bytesConstRef(state.memory.data() + i, 0x20).toBytes()) << endl;
result << "Storage dump:" << endl;
for (auto const& slot: state.storage)
result << " " << slot.first.hex() << ": " << slot.second.hex() << endl;
return result.str();
}
void YulInterpreterTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream);
for (auto const& error: _errors)
formatter.printExceptionInformation(
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
}

View File

@ -0,0 +1,72 @@
/*
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>
namespace langutil
{
class Scanner;
class Error;
using ErrorList = std::vector<std::shared_ptr<Error const>>;
}
namespace yul
{
struct AsmAnalysisInfo;
struct Block;
struct Dialect;
}
namespace yul
{
namespace test
{
class YulInterpreterTest: public dev::solidity::test::TestCase
{
public:
static std::unique_ptr<TestCase> create(std::string const& _filename)
{
return std::unique_ptr<TestCase>(new YulInterpreterTest(_filename));
}
explicit YulInterpreterTest(std::string const& _filename);
bool 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<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
std::string m_obtainedResult;
};
}
}

View File

@ -0,0 +1,16 @@
{
{
let a := 0x20
mstore(a, 2)
}
let a
mstore(a, 3)
}
// ----
// Trace:
// MSTORE_AT_SIZE(32, 32) [0000000000000000000000000000000000000000000000000000000000000002]
// MSTORE_AT_SIZE(0, 32) [0000000000000000000000000000000000000000000000000000000000000003]
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000003
// 20: 0000000000000000000000000000000000000000000000000000000000000002
// Storage dump:

View File

@ -0,0 +1,17 @@
{
let x := call(gas(), 0x45, 0x5, 0, 0x20, 0x30, 0x20)
sstore(100, x)
}
// ----
// Trace:
// GAS()
// MLOAD_FROM_SIZE(0, 32)
// MSTORE_AT_SIZE(48, 32)
// CALL(153, 69, 5, 0, 32, 48, 32)
// SSTORE(100, 1)
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000000
// 20: 0000000000000000000000000000000000000000000000000000000000000000
// 40: 0000000000000000000000000000000000000000000000000000000000000000
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000064: 0000000000000000000000000000000000000000000000000000000000000001

View File

@ -0,0 +1,14 @@
{
function f(a, b) -> x, y {
x := add(a, b)
y := mul(a, b)
}
let r, t := f(6, 7)
sstore(r, t)
}
// ----
// Trace:
// SSTORE(13, 42)
// Memory dump:
// Storage dump:
// 000000000000000000000000000000000000000000000000000000000000000d: 000000000000000000000000000000000000000000000000000000000000002a

View File

@ -0,0 +1,20 @@
{
for { let x := 2 } lt(x, 10) { x := add(x, 1) } {
mstore(mul(x, 5), mul(x, 0x1000))
}
}
// ----
// Trace:
// MSTORE_AT_SIZE(10, 32) [0000000000000000000000000000000000000000000000000000000000002000]
// MSTORE_AT_SIZE(15, 32) [0000000000000000000000000000000000000000000000000000000000003000]
// MSTORE_AT_SIZE(20, 32) [0000000000000000000000000000000000000000000000000000000000004000]
// MSTORE_AT_SIZE(25, 32) [0000000000000000000000000000000000000000000000000000000000005000]
// MSTORE_AT_SIZE(30, 32) [0000000000000000000000000000000000000000000000000000000000006000]
// MSTORE_AT_SIZE(35, 32) [0000000000000000000000000000000000000000000000000000000000007000]
// MSTORE_AT_SIZE(40, 32) [0000000000000000000000000000000000000000000000000000000000008000]
// MSTORE_AT_SIZE(45, 32) [0000000000000000000000000000000000000000000000000000000000009000]
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000000
// 20: 0000000000000000000000000000000000000000000000000000000000000000
// 40: 0000000000000000000000900000000000000000000000000000000000000000
// Storage dump:

View File

@ -0,0 +1,10 @@
{
mstore(10, 11)
}
// ----
// Trace:
// MSTORE_AT_SIZE(10, 32) [000000000000000000000000000000000000000000000000000000000000000b]
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000000
// 20: 0000000000000000000b00000000000000000000000000000000000000000000
// Storage dump:

View File

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

View File

@ -0,0 +1,13 @@
{
switch 7
case 7 { mstore(1, 2) }
case 3 { mstore(6, 7) }
default { mstore(8, 9) }
}
// ----
// Trace:
// MSTORE_AT_SIZE(1, 32) [0000000000000000000000000000000000000000000000000000000000000002]
// Memory dump:
// 0: 0000000000000000000000000000000000000000000000000000000000000000
// 20: 0200000000000000000000000000000000000000000000000000000000000000
// Storage dump:

View File

@ -30,6 +30,7 @@ add_executable(isoltest
../libsolidity/SMTCheckerJSONTest.cpp
../libyul/ObjectCompilerTest.cpp
../libyul/YulOptimizerTest.cpp
../libyul/YulInterpreterTest.cpp
)
target_link_libraries(isoltest PRIVATE libsolc solidity evmasm ${Boost_PROGRAM_OPTIONS_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
target_link_libraries(isoltest PRIVATE libsolc solidity yulInterpreter evmasm ${Boost_PROGRAM_OPTIONS_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})