2020-04-20 14:08:31 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-04-20 14:08:31 +00:00
|
|
|
|
|
|
|
#include <test/tools/ossfuzz/protoToSol.h>
|
2021-02-02 14:23:02 +00:00
|
|
|
#include <test/tools/ossfuzz/SolidityEvmoneInterface.h>
|
2020-04-20 14:08:31 +00:00
|
|
|
#include <test/tools/ossfuzz/solProto.pb.h>
|
2021-02-02 14:23:02 +00:00
|
|
|
|
2020-04-20 14:08:31 +00:00
|
|
|
#include <test/EVMHost.h>
|
|
|
|
|
|
|
|
#include <evmone/evmone.h>
|
|
|
|
#include <src/libfuzzer/libfuzzer_macro.h>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
static evmc::VM evmone = evmc::VM{evmc_create_evmone()};
|
|
|
|
|
2021-02-02 14:23:02 +00:00
|
|
|
using namespace solidity::test::fuzzer;
|
2020-04-20 14:08:31 +00:00
|
|
|
using namespace solidity::test::solprotofuzzer;
|
|
|
|
using namespace solidity;
|
2021-02-02 14:23:02 +00:00
|
|
|
using namespace solidity::frontend;
|
2020-04-20 14:08:31 +00:00
|
|
|
using namespace solidity::test;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
DEFINE_PROTO_FUZZER(Program const& _input)
|
|
|
|
{
|
|
|
|
ProtoConverter converter;
|
|
|
|
string sol_source = converter.protoToSolidity(_input);
|
|
|
|
|
|
|
|
if (char const* dump_path = getenv("PROTO_FUZZER_DUMP_PATH"))
|
|
|
|
{
|
|
|
|
// With libFuzzer binary run this to generate a YUL source file x.yul:
|
|
|
|
// PROTO_FUZZER_DUMP_PATH=x.yul ./a.out proto-input
|
|
|
|
ofstream of(dump_path);
|
2020-06-15 10:51:58 +00:00
|
|
|
of.write(sol_source.data(), static_cast<streamsize>(sol_source.size()));
|
2020-04-20 14:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (char const* dump_path = getenv("SOL_DEBUG_FILE"))
|
|
|
|
{
|
|
|
|
sol_source.clear();
|
|
|
|
// With libFuzzer binary run this to generate a YUL source file x.yul:
|
|
|
|
// PROTO_FUZZER_LOAD_PATH=x.yul ./a.out proto-input
|
|
|
|
ifstream ifstr(dump_path);
|
|
|
|
sol_source = {
|
|
|
|
std::istreambuf_iterator<char>(ifstr),
|
|
|
|
std::istreambuf_iterator<char>()
|
|
|
|
};
|
|
|
|
std::cout << sol_source << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:23:02 +00:00
|
|
|
// We target the default EVM which is the latest
|
|
|
|
langutil::EVMVersion version;
|
|
|
|
EVMHost hostContext(version, evmone);
|
|
|
|
string contractName = ":C";
|
|
|
|
string libraryName = converter.libraryTest() ? converter.libraryName() : "";
|
|
|
|
string methodName = "test()";
|
2021-03-23 12:15:11 +00:00
|
|
|
CompilerInput cInput(version, {{"test.sol", sol_source}}, contractName, OptimiserSettings::minimal(), {});
|
2021-02-02 14:23:02 +00:00
|
|
|
EvmoneUtility evmoneUtil(
|
|
|
|
hostContext,
|
|
|
|
cInput,
|
|
|
|
contractName,
|
|
|
|
libraryName,
|
|
|
|
methodName
|
2020-04-20 14:08:31 +00:00
|
|
|
);
|
2021-02-02 14:23:02 +00:00
|
|
|
auto minimalResult = evmoneUtil.compileDeployAndExecute();
|
|
|
|
if (minimalResult.has_value())
|
|
|
|
{
|
|
|
|
solAssert(minimalResult->status_code != EVMC_REVERT, "Sol proto fuzzer: Evmone reverted.");
|
|
|
|
if (minimalResult->status_code == EVMC_SUCCESS)
|
2020-04-20 14:08:31 +00:00
|
|
|
solAssert(
|
2021-02-02 14:23:02 +00:00
|
|
|
EvmoneUtility::zeroWord(minimalResult->output_data, minimalResult->output_size),
|
2020-04-20 14:08:31 +00:00
|
|
|
"Proto solc fuzzer: Output incorrect"
|
|
|
|
);
|
2021-02-02 14:23:02 +00:00
|
|
|
}
|
2020-04-20 14:08:31 +00:00
|
|
|
}
|