|
| 1 | +/* |
| 2 | + This file is part of solidity. |
| 3 | +
|
| 4 | + solidity is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + solidity is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with solidity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <test/libevmasm/EVMAssemblyTest.h> |
| 19 | + |
| 20 | +#include <test/Common.h> |
| 21 | + |
| 22 | +#include <libevmasm/Disassemble.h> |
| 23 | +#include <libevmasm/EVMAssemblyStack.h> |
| 24 | + |
| 25 | +#include <boost/algorithm/string/predicate.hpp> |
| 26 | +#include <boost/algorithm/string/split.hpp> |
| 27 | +#include <boost/algorithm/string/trim.hpp> |
| 28 | + |
| 29 | +#include <range/v3/view/map.hpp> |
| 30 | + |
| 31 | +using namespace std::string_literals; |
| 32 | +using namespace solidity; |
| 33 | +using namespace solidity::test; |
| 34 | +using namespace solidity::evmasm; |
| 35 | +using namespace solidity::evmasm::test; |
| 36 | +using namespace solidity::frontend; |
| 37 | +using namespace solidity::frontend::test; |
| 38 | +using namespace solidity::langutil; |
| 39 | +using namespace solidity::util; |
| 40 | + |
| 41 | +std::vector<std::string> const EVMAssemblyTest::c_outputLabels = { |
| 42 | + "Assembly", |
| 43 | + "Bytecode", |
| 44 | + "Opcodes", |
| 45 | + "SourceMappings", |
| 46 | +}; |
| 47 | + |
| 48 | +std::unique_ptr<TestCase> EVMAssemblyTest::create(Config const& _config) |
| 49 | +{ |
| 50 | + return std::make_unique<EVMAssemblyTest>(_config.filename); |
| 51 | +} |
| 52 | + |
| 53 | +EVMAssemblyTest::EVMAssemblyTest(std::string const& _filename): |
| 54 | + EVMVersionRestrictedTestCase(_filename) |
| 55 | +{ |
| 56 | + m_source = m_reader.source(); |
| 57 | + m_expectation = m_reader.simpleExpectations(); |
| 58 | + |
| 59 | + if (!boost::algorithm::ends_with(_filename, ".asmjson")) |
| 60 | + BOOST_THROW_EXCEPTION(std::runtime_error("Not an assembly test: \"" + _filename + "\". Allowed extensions: .asmjson.")); |
| 61 | + |
| 62 | + m_selectedOutputs = m_reader.stringSetting("outputs", "Assembly,Bytecode,Opcodes,SourceMappings"); |
| 63 | + OptimisationPreset optimizationPreset = m_reader.enumSetting<OptimisationPreset>( |
| 64 | + "optimizationPreset", |
| 65 | + { |
| 66 | + {"none", OptimisationPreset::None}, |
| 67 | + {"minimal", OptimisationPreset::Minimal}, |
| 68 | + {"standard", OptimisationPreset::Standard}, |
| 69 | + {"full", OptimisationPreset::Full}, |
| 70 | + }, |
| 71 | + "none" |
| 72 | + ); |
| 73 | + m_optimizerSettings = Assembly::OptimiserSettings::translateSettings(OptimiserSettings::preset(optimizationPreset)); |
| 74 | + m_optimizerSettings.expectedExecutionsPerDeployment = m_reader.sizetSetting( |
| 75 | + "optimizer.expectedExecutionsPerDeployment", |
| 76 | + m_optimizerSettings.expectedExecutionsPerDeployment |
| 77 | + ); |
| 78 | + |
| 79 | + auto const optimizerComponentSetting = [&](std::string const& _component, bool& _setting) { |
| 80 | + _setting = m_reader.boolSetting("optimizer." + _component, _setting); |
| 81 | + }; |
| 82 | + optimizerComponentSetting("inliner", m_optimizerSettings.runInliner); |
| 83 | + optimizerComponentSetting("jumpdestRemover", m_optimizerSettings.runJumpdestRemover); |
| 84 | + optimizerComponentSetting("peephole", m_optimizerSettings.runPeephole); |
| 85 | + optimizerComponentSetting("deduplicate", m_optimizerSettings.runDeduplicate); |
| 86 | + optimizerComponentSetting("cse", m_optimizerSettings.runCSE); |
| 87 | + optimizerComponentSetting("constantOptimizer", m_optimizerSettings.runConstantOptimiser); |
| 88 | + |
| 89 | + // TODO: Enable when assembly import for EOF is implemented. |
| 90 | + if (CommonOptions::get().eofVersion().has_value()) |
| 91 | + m_shouldRun = false; |
| 92 | +} |
| 93 | + |
| 94 | +TestCase::TestResult EVMAssemblyTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted) |
| 95 | +{ |
| 96 | + EVMAssemblyStack evmAssemblyStack( |
| 97 | + CommonOptions::get().evmVersion(), |
| 98 | + CommonOptions::get().eofVersion(), |
| 99 | + m_optimizerSettings |
| 100 | + ); |
| 101 | + |
| 102 | + evmAssemblyStack.selectDebugInfo(DebugInfoSelection::AllExceptExperimental()); |
| 103 | + |
| 104 | + try |
| 105 | + { |
| 106 | + evmAssemblyStack.parseAndAnalyze(m_reader.fileName().filename().string(), m_source); |
| 107 | + } |
| 108 | + catch (AssemblyImportException const& _exception) |
| 109 | + { |
| 110 | + m_obtainedResult = "AssemblyImportException: "s + _exception.what() + "\n"; |
| 111 | + return checkResult(_stream, _linePrefix, _formatted); |
| 112 | + } |
| 113 | + |
| 114 | + try |
| 115 | + { |
| 116 | + evmAssemblyStack.assemble(); |
| 117 | + } |
| 118 | + catch (Error const& _error) |
| 119 | + { |
| 120 | + // TODO: EVMAssemblyStack should catch these on its own and provide an error reporter. |
| 121 | + soltestAssert(_error.comment(), "Errors must include a message for the user."); |
| 122 | + m_obtainedResult = Error::formatErrorType(_error.type()) + ": " + *_error.comment() + "\n"; |
| 123 | + return checkResult(_stream, _linePrefix, _formatted); |
| 124 | + } |
| 125 | + soltestAssert(evmAssemblyStack.compilationSuccessful()); |
| 126 | + |
| 127 | + auto const produceOutput = [&](std::string const& _output) { |
| 128 | + if (_output == "Assembly") |
| 129 | + return evmAssemblyStack.assemblyString({{m_reader.fileName().filename().string(), m_source}}); |
| 130 | + if (_output == "Bytecode") |
| 131 | + return util::toHex(evmAssemblyStack.object().bytecode); |
| 132 | + if (_output == "Opcodes") |
| 133 | + return disassemble(evmAssemblyStack.object().bytecode, CommonOptions::get().evmVersion()); |
| 134 | + if (_output == "SourceMappings") |
| 135 | + return evmAssemblyStack.sourceMapping(); |
| 136 | + soltestAssert(false); |
| 137 | + unreachable(); |
| 138 | + }; |
| 139 | + |
| 140 | + std::set<std::string> selectedOutputSet; |
| 141 | + boost::split(selectedOutputSet, m_selectedOutputs, boost::is_any_of(",")); |
| 142 | + for (std::string const& output: c_outputLabels) |
| 143 | + if (selectedOutputSet.contains(output)) |
| 144 | + { |
| 145 | + if (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n') |
| 146 | + m_obtainedResult += "\n"; |
| 147 | + |
| 148 | + // Don't trim on the left to avoid stripping indentation. |
| 149 | + std::string content = produceOutput(output); |
| 150 | + boost::trim_right(content); |
| 151 | + std::string separator = (content.empty() ? "" : (output == "Assembly" ? "\n" : " ")); |
| 152 | + m_obtainedResult += output + ":" + separator + content; |
| 153 | + } |
| 154 | + if (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n') |
| 155 | + m_obtainedResult += "\n"; |
| 156 | + |
| 157 | + return checkResult(_stream, _linePrefix, _formatted); |
| 158 | +} |
0 commit comments