2021-02-02 14:23:02 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
|
|
|
|
#include <test/tools/ossfuzz/SolidityEvmoneInterface.h>
|
|
|
|
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
|
|
|
|
|
|
|
#include <range/v3/algorithm/all_of.hpp>
|
|
|
|
#include <range/v3/span.hpp>
|
|
|
|
|
|
|
|
using namespace solidity::test::fuzzer;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
optional<CompilerOutput> SolidityCompilationFramework::compileContract()
|
|
|
|
{
|
2021-03-23 12:15:11 +00:00
|
|
|
m_compiler.setSources(m_compilerInput.sourceCode);
|
2021-02-02 14:23:02 +00:00
|
|
|
m_compiler.setLibraries(m_compilerInput.libraryAddresses);
|
|
|
|
m_compiler.setEVMVersion(m_compilerInput.evmVersion);
|
|
|
|
m_compiler.setOptimiserSettings(m_compilerInput.optimiserSettings);
|
2021-03-23 12:15:11 +00:00
|
|
|
m_compiler.setViaIR(m_compilerInput.viaIR);
|
2021-02-02 14:23:02 +00:00
|
|
|
if (!m_compiler.compile())
|
|
|
|
{
|
|
|
|
if (m_compilerInput.debugFailure)
|
|
|
|
{
|
|
|
|
cerr << "Compiling contract failed" << endl;
|
|
|
|
for (auto const& error: m_compiler.errors())
|
2021-06-29 12:38:59 +00:00
|
|
|
cerr << SourceReferenceFormatter::formatErrorInformation(
|
2021-02-02 14:23:02 +00:00
|
|
|
*error,
|
2021-06-29 12:38:59 +00:00
|
|
|
m_compiler
|
2021-02-02 14:23:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string contractName;
|
|
|
|
if (m_compilerInput.contractName.empty())
|
|
|
|
contractName = m_compiler.lastContractName();
|
|
|
|
else
|
|
|
|
contractName = m_compilerInput.contractName;
|
|
|
|
evmasm::LinkerObject obj = m_compiler.object(contractName);
|
2022-02-01 14:42:08 +00:00
|
|
|
Json::Value methodIdentifiers = m_compiler.interfaceSymbols(contractName)["methods"];
|
2021-02-02 14:23:02 +00:00
|
|
|
return CompilerOutput{obj.bytecode, methodIdentifiers};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EvmoneUtility::zeroWord(uint8_t const* _result, size_t _length)
|
|
|
|
{
|
|
|
|
return _length == 32 &&
|
|
|
|
ranges::all_of(
|
2021-03-23 12:15:11 +00:00
|
|
|
ranges::span(_result, static_cast<long>(_length)),
|
2021-02-02 14:23:02 +00:00
|
|
|
[](uint8_t _v) { return _v == 0; });
|
|
|
|
}
|
|
|
|
|
|
|
|
evmc_message EvmoneUtility::initializeMessage(bytes const& _input)
|
|
|
|
{
|
|
|
|
// Zero initialize all message fields
|
|
|
|
evmc_message msg = {};
|
|
|
|
// Gas available (value of type int64_t) is set to its maximum
|
|
|
|
// value.
|
|
|
|
msg.gas = std::numeric_limits<int64_t>::max();
|
|
|
|
msg.input_data = _input.data();
|
|
|
|
msg.input_size = _input.size();
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result EvmoneUtility::executeContract(
|
2021-02-02 14:23:02 +00:00
|
|
|
bytes const& _functionHash,
|
|
|
|
evmc_address _deployedAddress
|
|
|
|
)
|
|
|
|
{
|
|
|
|
evmc_message message = initializeMessage(_functionHash);
|
2022-09-16 10:56:23 +00:00
|
|
|
message.recipient = _deployedAddress;
|
|
|
|
message.code_address = _deployedAddress;
|
2021-02-02 14:23:02 +00:00
|
|
|
message.kind = EVMC_CALL;
|
|
|
|
return m_evmHost.call(message);
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result EvmoneUtility::deployContract(bytes const& _code)
|
2021-02-02 14:23:02 +00:00
|
|
|
{
|
|
|
|
evmc_message message = initializeMessage(_code);
|
|
|
|
message.kind = EVMC_CREATE;
|
|
|
|
return m_evmHost.call(message);
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result EvmoneUtility::deployAndExecute(
|
2021-02-02 14:23:02 +00:00
|
|
|
bytes const& _byteCode,
|
|
|
|
string const& _hexEncodedInput
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Deploy contract and signal failure if deploy failed
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result createResult = deployContract(_byteCode);
|
2021-02-02 14:23:02 +00:00
|
|
|
solAssert(
|
|
|
|
createResult.status_code == EVMC_SUCCESS,
|
|
|
|
"SolidityEvmoneInterface: Contract creation failed"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Execute test function and signal failure if EVM reverted or
|
|
|
|
// did not return expected output on successful execution.
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result callResult = executeContract(
|
2021-02-02 14:23:02 +00:00
|
|
|
util::fromHex(_hexEncodedInput),
|
|
|
|
createResult.create_address
|
|
|
|
);
|
|
|
|
|
|
|
|
// We don't care about EVM One failures other than EVMC_REVERT
|
|
|
|
solAssert(
|
|
|
|
callResult.status_code != EVMC_REVERT,
|
|
|
|
"SolidityEvmoneInterface: EVM One reverted"
|
|
|
|
);
|
|
|
|
return callResult;
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result EvmoneUtility::compileDeployAndExecute(string _fuzzIsabelle)
|
2021-02-02 14:23:02 +00:00
|
|
|
{
|
|
|
|
map<string, h160> libraryAddressMap;
|
|
|
|
// Stage 1: Compile and deploy library if present.
|
|
|
|
if (!m_libraryName.empty())
|
|
|
|
{
|
|
|
|
m_compilationFramework.contractName(m_libraryName);
|
|
|
|
auto compilationOutput = m_compilationFramework.compileContract();
|
2022-08-08 08:59:58 +00:00
|
|
|
solAssert(compilationOutput.has_value(), "Compiling library failed");
|
|
|
|
CompilerOutput cOutput = compilationOutput.value();
|
|
|
|
// Deploy contract and signal failure if deploy failed
|
2022-09-16 10:56:23 +00:00
|
|
|
evmc::Result createResult = deployContract(cOutput.byteCode);
|
2022-08-08 08:59:58 +00:00
|
|
|
solAssert(
|
|
|
|
createResult.status_code == EVMC_SUCCESS,
|
|
|
|
"SolidityEvmoneInterface: Library deployment failed"
|
|
|
|
);
|
|
|
|
libraryAddressMap[m_libraryName] = EVMHost::convertFromEVMC(createResult.create_address);
|
|
|
|
m_compilationFramework.libraryAddresses(libraryAddressMap);
|
2021-02-02 14:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stage 2: Compile, deploy, and execute contract, optionally using library
|
|
|
|
// address map.
|
|
|
|
m_compilationFramework.contractName(m_contractName);
|
|
|
|
auto cOutput = m_compilationFramework.compileContract();
|
2022-08-08 08:59:58 +00:00
|
|
|
solAssert(cOutput.has_value(), "Compiling contract failed");
|
|
|
|
solAssert(
|
|
|
|
!cOutput->byteCode.empty() && !cOutput->methodIdentifiersInContract.empty(),
|
|
|
|
"SolidityEvmoneInterface: Invalid compilation output."
|
|
|
|
);
|
2021-02-02 14:23:02 +00:00
|
|
|
|
2022-08-08 08:59:58 +00:00
|
|
|
string methodName;
|
|
|
|
if (!_fuzzIsabelle.empty())
|
|
|
|
// TODO: Remove this once a cleaner solution is found for querying
|
|
|
|
// isabelle test entry point. At the moment, we are sure that the
|
|
|
|
// entry point is the second method in the contract (hence the ++)
|
|
|
|
// but not its name.
|
|
|
|
methodName = (++cOutput->methodIdentifiersInContract.begin())->asString() +
|
|
|
|
_fuzzIsabelle.substr(2, _fuzzIsabelle.size());
|
2021-02-02 14:23:02 +00:00
|
|
|
else
|
2022-08-08 08:59:58 +00:00
|
|
|
methodName = cOutput->methodIdentifiersInContract[m_methodName].asString();
|
|
|
|
|
|
|
|
return deployAndExecute(
|
|
|
|
cOutput->byteCode,
|
|
|
|
methodName
|
|
|
|
);
|
2021-02-02 14:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optional<CompilerOutput> EvmoneUtility::compileContract()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return m_compilationFramework.compileContract();
|
|
|
|
}
|
|
|
|
catch (evmasm::StackTooDeepException const&)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|