2019-01-17 10:19:54 +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
|
2019-01-17 10:19:54 +00:00
|
|
|
/**
|
|
|
|
* Executable for use with AFL <http://lcamtuf.coredump.cx/afl>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/tools/fuzzer_common.h>
|
2019-01-29 16:14:28 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
2019-01-29 16:14:28 +00:00
|
|
|
|
2019-01-17 10:19:54 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2019-01-29 16:14:28 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-01-17 10:19:54 +00:00
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
2019-01-17 10:19:54 +00:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
po::options_description options(
|
|
|
|
R"(solfuzzer, fuzz-testing binary for use with AFL.
|
|
|
|
Usage: solfuzzer [Options] < input
|
|
|
|
Reads a single source from stdin, compiles it and signals a failure for internal errors.
|
|
|
|
|
|
|
|
Allowed options)",
|
|
|
|
po::options_description::m_default_line_length,
|
|
|
|
po::options_description::m_default_line_length - 23);
|
|
|
|
options.add_options()
|
|
|
|
("help", "Show this help screen.")
|
|
|
|
("quiet", "Only output errors.")
|
|
|
|
(
|
|
|
|
"standard-json",
|
|
|
|
"Test via the standard-json interface, i.e. "
|
|
|
|
"input is expected to be JSON-encoded instead of "
|
|
|
|
"plain source file."
|
|
|
|
)
|
|
|
|
(
|
|
|
|
"const-opt",
|
|
|
|
"Run the constant optimizer instead of compiling. "
|
|
|
|
"Expects a binary string of up to 32 bytes on stdin."
|
|
|
|
)
|
|
|
|
(
|
|
|
|
"input-file",
|
|
|
|
po::value<string>(),
|
|
|
|
"input file"
|
2019-04-01 14:57:32 +00:00
|
|
|
)(
|
|
|
|
"input-files",
|
|
|
|
po::value<std::vector<string>>()->multitoken(),
|
|
|
|
"input files"
|
2019-01-17 10:19:54 +00:00
|
|
|
)
|
|
|
|
(
|
|
|
|
"without-optimizer",
|
|
|
|
"Run without optimizations. Cannot be used together with standard-json."
|
|
|
|
);
|
|
|
|
|
|
|
|
// All positional options should be interpreted as input files
|
|
|
|
po::positional_options_description filesPositions;
|
|
|
|
filesPositions.add("input-file", 1);
|
|
|
|
bool quiet = false;
|
|
|
|
|
|
|
|
po::variables_map arguments;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
po::command_line_parser cmdLineParser(argc, argv);
|
|
|
|
cmdLineParser.options(options).positional(filesPositions);
|
|
|
|
po::store(cmdLineParser.run(), arguments);
|
|
|
|
}
|
|
|
|
catch (po::error const& _exception)
|
|
|
|
{
|
|
|
|
cerr << _exception.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-01 13:50:34 +00:00
|
|
|
if (arguments.count("quiet"))
|
|
|
|
quiet = true;
|
|
|
|
|
|
|
|
if (arguments.count("help"))
|
|
|
|
{
|
|
|
|
cout << options;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-01 14:57:32 +00:00
|
|
|
vector<string> inputs;
|
2019-01-17 10:19:54 +00:00
|
|
|
if (arguments.count("input-file"))
|
2019-04-01 14:57:32 +00:00
|
|
|
inputs.push_back(arguments["input-file"].as<string>());
|
|
|
|
else if (arguments.count("input-files"))
|
|
|
|
inputs = arguments["input-files"].as<vector<string>>();
|
2019-01-17 10:19:54 +00:00
|
|
|
else
|
2020-04-01 02:39:38 +00:00
|
|
|
inputs.emplace_back("");
|
2019-01-17 10:19:54 +00:00
|
|
|
|
2019-04-01 14:57:32 +00:00
|
|
|
bool optimize = !arguments.count("without-optimizer");
|
|
|
|
int retResult = 0;
|
|
|
|
|
|
|
|
for (string const& inputFile: inputs)
|
|
|
|
{
|
|
|
|
string input;
|
|
|
|
if (inputFile.size() == 0)
|
|
|
|
input = readStandardInput();
|
|
|
|
else
|
|
|
|
input = readFileAsString(inputFile);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (arguments.count("const-opt"))
|
|
|
|
FuzzerUtil::testConstantOptimizer(input, quiet);
|
|
|
|
else if (arguments.count("standard-json"))
|
|
|
|
FuzzerUtil::testStandardCompiler(input, quiet);
|
|
|
|
else
|
2020-02-28 11:48:59 +00:00
|
|
|
FuzzerUtil::testCompilerJsonInterface(input, optimize, quiet);
|
2019-04-01 14:57:32 +00:00
|
|
|
}
|
2019-04-04 12:03:32 +00:00
|
|
|
catch (...)
|
2019-04-01 14:57:32 +00:00
|
|
|
{
|
|
|
|
retResult = 1;
|
|
|
|
|
|
|
|
if (inputFile.size() == 0)
|
2019-04-04 12:03:32 +00:00
|
|
|
throw;
|
2019-04-01 14:57:32 +00:00
|
|
|
|
|
|
|
cerr << "Fuzzer "
|
|
|
|
<< (optimize ? "" : "(without optimizer) ")
|
|
|
|
<< "failed on "
|
|
|
|
<< inputFile;
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 10:19:54 +00:00
|
|
|
|
2019-04-01 14:57:32 +00:00
|
|
|
return retResult;
|
2019-01-29 16:14:28 +00:00
|
|
|
}
|