Skip to content

evmasm test case #16012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions libevmasm/EVMAssemblyStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ void EVMAssemblyStack::assemble()
LinkerObject const& EVMAssemblyStack::object(std::string const& _contractName) const
{
solAssert(_contractName == m_name);
return m_object;
return object();
}

LinkerObject const& EVMAssemblyStack::runtimeObject(std::string const& _contractName) const
{
solAssert(_contractName == m_name);
return m_runtimeObject;
return runtimeObject();
}

std::map<std::string, unsigned> EVMAssemblyStack::sourceIndices() const
Expand All @@ -95,13 +95,13 @@ std::map<std::string, unsigned> EVMAssemblyStack::sourceIndices() const
std::string const* EVMAssemblyStack::sourceMapping(std::string const& _contractName) const
{
solAssert(_contractName == m_name);
return &m_sourceMapping;
return &sourceMapping();
}

std::string const* EVMAssemblyStack::runtimeSourceMapping(std::string const& _contractName) const
{
solAssert(_contractName == m_name);
return &m_runtimeSourceMapping;
return &runtimeSourceMapping();
}

Json EVMAssemblyStack::ethdebug(std::string const& _contractName) const
Expand All @@ -123,20 +123,30 @@ Json EVMAssemblyStack::ethdebug() const
return {};
}

Json EVMAssemblyStack::assemblyJSON(std::string const& _contractName) const
Json EVMAssemblyStack::assemblyJSON() const
{
solAssert(_contractName == m_name);
solAssert(m_evmAssembly);
return m_evmAssembly->assemblyJSON(sourceIndices());
}

std::string EVMAssemblyStack::assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const
Json EVMAssemblyStack::assemblyJSON(std::string const& _contractName) const
{
solAssert(_contractName == m_name);
return assemblyJSON();
}

std::string EVMAssemblyStack::assemblyString(StringMap const& _sourceCodes) const
{
solAssert(m_evmAssembly);
return m_evmAssembly->assemblyString(m_debugInfoSelection, _sourceCodes);
}

std::string EVMAssemblyStack::assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const
{
solAssert(_contractName == m_name);
return assemblyString(_sourceCodes);
}

std::string const EVMAssemblyStack::filesystemFriendlyName(std::string const& _contractName) const
{
solAssert(_contractName == m_name);
Expand Down
6 changes: 6 additions & 0 deletions libevmasm/EVMAssemblyStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ class EVMAssemblyStack: public AbstractAssemblyStack

std::string const& name() const { return m_name; }

LinkerObject const& object() const { return m_object; }
LinkerObject const& object(std::string const& _contractName) const override;
LinkerObject const& runtimeObject() const { return m_runtimeObject; }
LinkerObject const& runtimeObject(std::string const& _contractName) const override;

std::shared_ptr<evmasm::Assembly> const& evmAssembly() const { return m_evmAssembly; }
std::shared_ptr<evmasm::Assembly> const& evmRuntimeAssembly() const { return m_evmRuntimeAssembly; }

std::string const& sourceMapping() const { return m_sourceMapping; }
std::string const* sourceMapping(std::string const& _contractName) const override;
std::string const& runtimeSourceMapping() const { return m_runtimeSourceMapping; }
std::string const* runtimeSourceMapping(std::string const& _contractName) const override;

Json ethdebug(std::string const& _contractName) const override;
Json ethdebugRuntime(std::string const& _contractName) const override;
Json ethdebug() const override;

Json assemblyJSON() const;
Json assemblyJSON(std::string const& _contractName) const override;
std::string assemblyString(StringMap const& _sourceCodes) const;
std::string assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const override;

std::string const filesystemFriendlyName(std::string const& _contractName) const override;
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace solidity;
using namespace solidity::util;
using namespace solidity::evmasm;

std::map<std::string, Instruction> const solidity::evmasm::c_instructions =
std::map<std::string, Instruction, std::less<>> const solidity::evmasm::c_instructions =
{
{ "STOP", Instruction::STOP },
{ "ADD", Instruction::ADD },
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,6 @@ InstructionInfo instructionInfo(Instruction _inst, langutil::EVMVersion _evmVers
bool isValidInstruction(Instruction _inst);

/// Convert from string mnemonic to Instruction type.
extern const std::map<std::string, Instruction> c_instructions;
extern const std::map<std::string, Instruction, std::less<>> c_instructions;

}
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ detect_stray_source_files("${libsolutil_sources}" "libsolutil/")

set(libevmasm_sources
libevmasm/Assembler.cpp
libevmasm/EVMAssemblyTest.cpp
libevmasm/EVMAssemblyTest.h
libevmasm/Optimiser.cpp
libevmasm/PlainAssemblyParser.cpp
libevmasm/PlainAssemblyParser.h
)
detect_stray_source_files("${libevmasm_sources}" "libevmasm/")

Expand Down
3 changes: 3 additions & 0 deletions test/InteractiveTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include <test/libyul/StackShufflingTest.h>
#include <test/libyul/SyntaxTest.h>

#include <test/libevmasm/EVMAssemblyTest.h>

#include <boost/filesystem.hpp>

namespace solidity::frontend::test
Expand All @@ -64,6 +66,7 @@ struct Testsuite
Testsuite const g_interactiveTestsuites[] = {
/*
Title Path Subpath SMT NeedsVM Creator function */
{"EVM Assembly", "libevmasm", "evmAssemblyTests", false, false, &evmasm::test::EVMAssemblyTest::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},
Expand Down
7 changes: 4 additions & 3 deletions test/TestCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ void TestCase::printUpdatedSettings(std::ostream& _stream, std::string const& _l
bool TestCase::isTestFilename(boost::filesystem::path const& _filename)
{
std::string extension = _filename.extension().string();
return (extension == ".sol" || extension == ".yul" || extension == ".stack") &&
!boost::starts_with(_filename.string(), "~") &&
!boost::starts_with(_filename.string(), ".");
// NOTE: .asmjson rather than .json because JSON files that do not represent test cases exist in some test dirs.
return (extension == ".sol" || extension == ".yul" || extension == ".asm" || extension == ".asmjson" || extension == ".stack") &&
!boost::starts_with(_filename.string(), "~") &&
!boost::starts_with(_filename.string(), ".");
}

bool TestCase::shouldRun()
Expand Down
1 change: 1 addition & 0 deletions test/TestCaseReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class TestCaseReader
std::size_t lineNumber() const { return m_lineNumber; }
std::map<std::string, std::string> const& settings() const { return m_settings; }
std::ifstream& stream() { return m_fileStream; }
boost::filesystem::path const& fileName() const { return m_fileName; }

std::string simpleExpectations();

Expand Down
181 changes: 181 additions & 0 deletions test/libevmasm/EVMAssemblyTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
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/libevmasm/EVMAssemblyTest.h>

#include <test/libevmasm/PlainAssemblyParser.h>

#include <test/Common.h>

#include <libevmasm/Disassemble.h>
#include <libevmasm/EVMAssemblyStack.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>

#include <range/v3/view/map.hpp>

using namespace std::string_literals;
using namespace solidity;
using namespace solidity::test;
using namespace solidity::evmasm;
using namespace solidity::evmasm::test;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::langutil;
using namespace solidity::util;

std::vector<std::string> const EVMAssemblyTest::c_outputLabels = {
"InputAssemblyJSON",
"Assembly",
"Bytecode",
"Opcodes",
"SourceMappings",
};

std::unique_ptr<TestCase> EVMAssemblyTest::create(Config const& _config)
{
return std::make_unique<EVMAssemblyTest>(_config.filename);
}

EVMAssemblyTest::EVMAssemblyTest(std::string const& _filename):
EVMVersionRestrictedTestCase(_filename)
{
m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();

if (boost::algorithm::ends_with(_filename, ".asmjson"))
m_assemblyFormat = AssemblyFormat::JSON;
else if (boost::algorithm::ends_with(_filename, ".asm"))
m_assemblyFormat = AssemblyFormat::Plain;
else
BOOST_THROW_EXCEPTION(std::runtime_error("Not an assembly test: \"" + _filename + "\". Allowed extensions: .asm, .asmjson."));

m_selectedOutputs = m_reader.stringSetting("outputs", "Assembly,Bytecode,Opcodes,SourceMappings");
OptimisationPreset optimizationPreset = m_reader.enumSetting<OptimisationPreset>(
"optimizationPreset",
{
{"none", OptimisationPreset::None},
{"minimal", OptimisationPreset::Minimal},
{"standard", OptimisationPreset::Standard},
{"full", OptimisationPreset::Full},
},
"none"
);
m_optimizerSettings = Assembly::OptimiserSettings::translateSettings(OptimiserSettings::preset(optimizationPreset));
m_optimizerSettings.expectedExecutionsPerDeployment = m_reader.sizetSetting(
"optimizer.expectedExecutionsPerDeployment",
m_optimizerSettings.expectedExecutionsPerDeployment
);

auto const optimizerComponentSetting = [&](std::string const& _component, bool& _setting) {
_setting = m_reader.boolSetting("optimizer." + _component, _setting);
};
optimizerComponentSetting("inliner", m_optimizerSettings.runInliner);
optimizerComponentSetting("jumpdestRemover", m_optimizerSettings.runJumpdestRemover);
optimizerComponentSetting("peephole", m_optimizerSettings.runPeephole);
optimizerComponentSetting("deduplicate", m_optimizerSettings.runDeduplicate);
optimizerComponentSetting("cse", m_optimizerSettings.runCSE);
optimizerComponentSetting("constantOptimizer", m_optimizerSettings.runConstantOptimiser);

// TODO: Enable when assembly import for EOF is implemented.
if (CommonOptions::get().eofVersion().has_value())
m_shouldRun = false;
Comment on lines +96 to +98
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case can easily be extended support EOF, but we need to implement assembly import for EOF first.

}

TestCase::TestResult EVMAssemblyTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted)
{
EVMAssemblyStack evmAssemblyStack(
CommonOptions::get().evmVersion(),
CommonOptions::get().eofVersion(),
m_optimizerSettings
);

evmAssemblyStack.selectDebugInfo(DebugInfoSelection::AllExceptExperimental());

std::string assemblyJSON;
switch (m_assemblyFormat)
{
case AssemblyFormat::JSON:
assemblyJSON = m_source;
break;
case AssemblyFormat::Plain:
assemblyJSON = jsonPrint(
PlainAssemblyParser{}.parse(m_reader.fileName().filename().string(), m_source),
{JsonFormat::Pretty, 4}
);
break;
}

try
{
evmAssemblyStack.parseAndAnalyze(m_reader.fileName().filename().string(), assemblyJSON);
}
catch (AssemblyImportException const& _exception)
{
m_obtainedResult = "AssemblyImportException: "s + _exception.what() + "\n";
return checkResult(_stream, _linePrefix, _formatted);
}

try
{
evmAssemblyStack.assemble();
}
catch (Error const& _error)
{
// TODO: EVMAssemblyStack should catch these on its own and provide an error reporter.
soltestAssert(_error.comment(), "Errors must include a message for the user.");
m_obtainedResult = Error::formatErrorType(_error.type()) + ": " + *_error.comment() + "\n";
return checkResult(_stream, _linePrefix, _formatted);
}
soltestAssert(evmAssemblyStack.compilationSuccessful());

auto const produceOutput = [&](std::string const& _output) {
if (_output == "InputAssemblyJSON")
return assemblyJSON;
if (_output == "Assembly")
return evmAssemblyStack.assemblyString({{m_reader.fileName().filename().string(), m_source}});
if (_output == "Bytecode")
return util::toHex(evmAssemblyStack.object().bytecode);
if (_output == "Opcodes")
return disassemble(evmAssemblyStack.object().bytecode, CommonOptions::get().evmVersion());
if (_output == "SourceMappings")
return evmAssemblyStack.sourceMapping();
soltestAssert(false);
unreachable();
};

std::set<std::string> selectedOutputSet;
boost::split(selectedOutputSet, m_selectedOutputs, boost::is_any_of(","));
for (std::string const& output: c_outputLabels)
if (selectedOutputSet.contains(output))
{
if (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n')
m_obtainedResult += "\n";

// Don't trim on the left to avoid stripping indentation.
std::string content = produceOutput(output);
boost::trim_right(content);
std::string separator = (content.empty() ? "" : (output == "Assembly" ? "\n" : " "));
m_obtainedResult += output + ":" + separator + content;
}
if (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n')
m_obtainedResult += "\n";

return checkResult(_stream, _linePrefix, _formatted);
}
Loading