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 // SPDX-License-Identifier: GPL-3.0
18 /**
19 * @author Christian <c@ethdev.com>
20 * @date 2016
21 * Framework for executing Solidity contracts and testing them against C++ implementation.
22 */
23
24 #include <test/libsolidity/SolidityExecutionFramework.h>
25
26 #include <liblangutil/DebugInfoSelection.h>
27 #include <liblangutil/Exceptions.h>
28 #include <liblangutil/SourceReferenceFormatter.h>
29
30 #include <boost/test/framework.hpp>
31
32 #include <cstdlib>
33 #include <iostream>
34
35 using namespace solidity;
36 using namespace solidity::frontend;
37 using namespace solidity::frontend::test;
38 using namespace solidity::langutil;
39 using namespace solidity::test;
40 using namespace std;
41
multiSourceCompileContract(map<string,string> const & _sourceCode,optional<string> const & _mainSourceName,string const & _contractName,map<string,Address> const & _libraryAddresses)42 bytes SolidityExecutionFramework::multiSourceCompileContract(
43 map<string, string> const& _sourceCode,
44 optional<string> const& _mainSourceName,
45 string const& _contractName,
46 map<string, Address> const& _libraryAddresses
47 )
48 {
49 if (_mainSourceName.has_value())
50 solAssert(_sourceCode.find(_mainSourceName.value()) != _sourceCode.end(), "");
51 map<string, string> sourcesWithPreamble = _sourceCode;
52 for (auto& entry: sourcesWithPreamble)
53 entry.second = addPreamble(entry.second);
54
55 m_compiler.reset();
56 m_compiler.enableEwasmGeneration(m_compileToEwasm);
57 m_compiler.setSources(sourcesWithPreamble);
58 m_compiler.setLibraries(_libraryAddresses);
59 m_compiler.setRevertStringBehaviour(m_revertStrings);
60 m_compiler.setEVMVersion(m_evmVersion);
61 m_compiler.setOptimiserSettings(m_optimiserSettings);
62 m_compiler.enableEvmBytecodeGeneration(!m_compileViaYul);
63 m_compiler.enableIRGeneration(m_compileViaYul);
64 m_compiler.setRevertStringBehaviour(m_revertStrings);
65 if (!m_compiler.compile())
66 {
67 // The testing framework expects an exception for
68 // "unimplemented" yul IR generation.
69 if (m_compileViaYul)
70 for (auto const& error: m_compiler.errors())
71 if (error->type() == langutil::Error::Type::CodeGenerationError)
72 BOOST_THROW_EXCEPTION(*error);
73 langutil::SourceReferenceFormatter{std::cerr, m_compiler, true, false}
74 .printErrorInformation(m_compiler.errors());
75 BOOST_ERROR("Compiling contract failed");
76 }
77 string contractName(_contractName.empty() ? m_compiler.lastContractName(_mainSourceName) : _contractName);
78 evmasm::LinkerObject obj;
79 if (m_compileViaYul)
80 {
81 if (m_compileToEwasm)
82 obj = m_compiler.ewasmObject(contractName);
83 else
84 {
85 // Try compiling twice: If the first run fails due to stack errors, forcefully enable
86 // the optimizer.
87 for (bool forceEnableOptimizer: {false, true})
88 {
89 OptimiserSettings optimiserSettings = m_optimiserSettings;
90 if (!forceEnableOptimizer && !optimiserSettings.runYulOptimiser)
91 {
92 // Enable some optimizations on the first run
93 optimiserSettings.runYulOptimiser = true;
94 optimiserSettings.yulOptimiserSteps = "uljmul jmul";
95 }
96 else if (forceEnableOptimizer)
97 optimiserSettings = OptimiserSettings::full();
98
99 yul::AssemblyStack asmStack(
100 m_evmVersion,
101 yul::AssemblyStack::Language::StrictAssembly,
102 optimiserSettings,
103 DebugInfoSelection::All()
104 );
105 bool analysisSuccessful = asmStack.parseAndAnalyze("", m_compiler.yulIROptimized(contractName));
106 solAssert(analysisSuccessful, "Code that passed analysis in CompilerStack can't have errors");
107
108 try
109 {
110 asmStack.optimize();
111 obj = move(*asmStack.assemble(yul::AssemblyStack::Machine::EVM).bytecode);
112 obj.link(_libraryAddresses);
113 break;
114 }
115 catch (...)
116 {
117 if (forceEnableOptimizer || optimiserSettings == OptimiserSettings::full())
118 throw;
119 }
120 }
121 }
122 }
123 else
124 obj = m_compiler.object(contractName);
125 BOOST_REQUIRE(obj.linkReferences.empty());
126 if (m_showMetadata)
127 cout << "metadata: " << m_compiler.metadata(contractName) << endl;
128 return obj.bytecode;
129 }
130
compileContract(string const & _sourceCode,string const & _contractName,map<string,Address> const & _libraryAddresses)131 bytes SolidityExecutionFramework::compileContract(
132 string const& _sourceCode,
133 string const& _contractName,
134 map<string, Address> const& _libraryAddresses
135 )
136 {
137 return multiSourceCompileContract(
138 {{"", _sourceCode}},
139 nullopt,
140 _contractName,
141 _libraryAddresses
142 );
143 }
144
addPreamble(string const & _sourceCode)145 string SolidityExecutionFramework::addPreamble(string const& _sourceCode)
146 {
147 // Silence compiler version warning
148 string preamble = "pragma solidity >=0.0;\n";
149 if (_sourceCode.find("// SPDX-License-Identifier:") == string::npos)
150 preamble += "// SPDX-License-Identifier: unlicensed\n";
151 if (
152 solidity::test::CommonOptions::get().useABIEncoderV1 &&
153 _sourceCode.find("pragma experimental ABIEncoderV2;") == string::npos &&
154 _sourceCode.find("pragma abicoder") == string::npos
155 )
156 preamble += "pragma abicoder v1;\n";
157 return preamble + _sourceCode;
158 }
159